auctex-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[AUCTeX-devel] Improve LaTeX macro definition argument prompts


From: Matthew Leach
Subject: [AUCTeX-devel] Improve LaTeX macro definition argument prompts
Date: Tue, 15 Mar 2016 20:16:15 +0000
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1.50 (gnu/linux)

Hi all,

When inserting a \newcommand macro with AUCTeX, I've found a couple of
small annoyances and have attached a patch to try and mitigate them.

First, the user is prompted for the number of arguments that a macro
should take.  If a '0' entered at the prompt, then '[0]' is added at the
point.  However, when no input is provided and the user just presses RET
at the prompt, nothing is inserted.  I'm not sure if there is semantic
difference between

\newcommand{\foo}[0]{Foobar}

and

\newcommand{\foo}{Foobar}

so this may be a stylistic preference for the user.  I think we should
be consistent, though.

Secondly, if a '0' is passed at the prompt of the Number of arguments,
we are still prompted for the default value of the first argument.  I
think this is an error as there is no first argument to have a default
value.

The attached patch creates two new TeX-arg-* functions.
TeX-arg-define-macro-arguments replaces "Number of arguments" in
TeX-add-symbols for the macro definition LaTeX commands.  This function
will prompt the the number of arguments and check the return value and
if it is equal to "0" or there is no input, nothing is inserted.  If
anything else is inserted the user is prompted for the default value for
the first argument, as before.

Feedback welcome!

Thanks,
-- 
Matt
>From e849041fdd2454a727a1eee4eba99fba22771f30 Mon Sep 17 00:00:00 2001
From: Matthew Leach <address@hidden>
Date: Tue, 15 Mar 2016 19:37:10 +0000
Subject: [PATCH] Improve prompts when defining LaTeX macros.

* latex.el (TeX-arg-default-argument-value): New.
  (TeX-arg-define-macro-arguments): New.
  (LaTeX-common-initialization): Use new LaTeX macro argument functions.
---
 latex.el | 56 ++++++++++++++++++++++++++++++++++++++++++--------------
 1 file changed, 42 insertions(+), 14 deletions(-)

diff --git a/latex.el b/latex.el
index 7221fce..a4436e3 100644
--- a/latex.el
+++ b/latex.el
@@ -1969,6 +1969,34 @@ string.  `TeX-read-label-prefix' is used as initial 
input for the
 label."
   (TeX-arg-label optional prompt t))
 
+(defun TeX-arg-default-argument-value (optional &optional prompt)
+  "Prompt for the default value for the first argument of a LaTeX macro.
+
+If OPTIONAL is non-nil, insert the resulting value as an optional
+argument, otherwise as a mandatory one.  Use PROMPT as the prompt
+string."
+  (TeX-argument-insert
+   (TeX-read-string
+    (TeX-argument-prompt optional prompt "Default value for first argument"))
+   optional))
+
+(defun TeX-arg-define-macro-arguments (optional &optional prompt)
+  "Prompt for the number of arguments for a LaTeX macro.  If this
+is non-zero, also prompt for the default value for the first
+argument.
+
+If OPTIONAL is non-nil, insert the resulting value as an optional
+argument, otherwise as a mandatory one.  Use PROMPT as the prompt
+string."
+  (let ((no-args (TeX-read-string
+                  (TeX-argument-prompt optional prompt
+                                       "Number of arguments"
+                                       nil))))
+    (unless (string= no-args "0")
+      (TeX-argument-insert no-args optional)
+      (unless (string-equal LaTeX-version "2")
+        (TeX-arg-default-argument-value optional)))))
+
 (defun TeX-arg-define-macro (optional &optional prompt)
   "Prompt for a TeX macro with completion.
 If OPTIONAL is non-nil, insert the resulting value as an optional
@@ -6022,16 +6050,16 @@ i.e. you do _not_ have to cater for this yourself by 
adding \\\\' or $."
    '("label" TeX-arg-define-label)
    '("pageref" TeX-arg-ref)
    '("ref" TeX-arg-ref)
-   '("newcommand" TeX-arg-define-macro [ "Number of arguments" ] t)
-   '("renewcommand" TeX-arg-macro [ "Number of arguments" ] t)
+   '("newcommand" TeX-arg-define-macro [ TeX-arg-define-macro-arguments ] t)
+   '("renewcommand" TeX-arg-macro [ TeX-arg-define-macro-arguments ] t)
    '("newenvironment" TeX-arg-define-environment
      [ "Number of arguments"] t t)
    '("renewenvironment" TeX-arg-environment
      [ "Number of arguments"] t t)
-   '("providecommand" TeX-arg-define-macro [ "Number of arguments" ] t)
-   '("providecommand*" TeX-arg-define-macro [ "Number of arguments" ] t)
-   '("newcommand*" TeX-arg-define-macro [ "Number of arguments" ] t)
-   '("renewcommand*" TeX-arg-macro [ "Number of arguments" ] t)
+   '("providecommand" TeX-arg-define-macro [ TeX-arg-define-macro-arguments ] 
t)
+   '("providecommand*" TeX-arg-define-macro [ TeX-arg-define-macro-arguments ] 
t)
+   '("newcommand*" TeX-arg-define-macro [ TeX-arg-define-macro-arguments ] t)
+   '("renewcommand*" TeX-arg-macro [ TeX-arg-define-macro-arguments ] t)
    '("newenvironment*" TeX-arg-define-environment
      [ "Number of arguments"] t t)
    '("renewenvironment*" TeX-arg-environment
@@ -6243,21 +6271,21 @@ i.e. you do _not_ have to cater for this yourself by 
adding \\\\' or $."
     (setq TeX-font-replace-function 'TeX-font-replace-macro)
     (TeX-add-symbols
      '("newcommand" TeX-arg-define-macro
-       [ "Number of arguments" ] [ "Default value for first argument" ] t)
+       [ TeX-arg-define-macro-arguments ] t)
      '("renewcommand" TeX-arg-macro
-       [ "Number of arguments" ] [ "Default value for first argument" ] t)
+       [ TeX-arg-define-macro-arguments ] t)
      '("providecommand" TeX-arg-define-macro
-       [ "Number of arguments" ] [ "Default value for first argument" ] t)
+       [ TeX-arg-define-macro-arguments ] t)
      '("providecommand*" TeX-arg-define-macro
-       [ "Number of arguments" ] [ "Default value for first argument" ] t)
+       [ TeX-arg-define-macro-arguments ] t)
      '("newcommand*" TeX-arg-define-macro
-       [ "Number of arguments" ] [ "Default value for first argument" ] t)
+       [ TeX-arg-define-macro-arguments ] t)
      '("renewcommand*" TeX-arg-macro
-       [ "Number of arguments" ] [ "Default value for first argument" ] t)
+       [ TeX-arg-define-macro-arguments ] t)
      '("newenvironment" TeX-arg-define-environment
-       [ "Number of arguments" ] [ "Default value for first argument" ] t t)
+       [ TeX-arg-define-macro-arguments ]  t t)
      '("renewenvironment" TeX-arg-environment
-       [ "Number of arguments" ] [ "Default value for first argument" ] t t)
+       [ TeX-arg-define-macro-arguments ] t t)
      '("usepackage" LaTeX-arg-usepackage)
      '("RequirePackage" LaTeX-arg-usepackage)
      '("ProvidesPackage" (TeX-arg-file-name-sans-extension "Package name")
-- 
2.7.3


reply via email to

[Prev in Thread] Current Thread [Next in Thread]