auctex-devel
[Top][All Lists]

Re: [AUCTeX-devel] Improve LaTeX macro definition argument prompts

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

```Mosè Giordano <address@hidden> writes:

> Hi Matt,

Hi Mosè,

Many thanks for the feedback.

> 2016-03-15 21:16 GMT+01:00 Matthew Leach <address@hidden>:
>> 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.
>
> I've never noticed such problem, probably because I never enter "0" as
> number of arguments, I just press RET.  AUCTeX does the right thing by
> not prompting for the next optional argument only with the default
> settings.

I'm sure it's a problem that people will run into very infrequently, as
you say, most people will probably just hit RET and not encounter any
issues :-).  Initially I thought that it was quite an invasive patch for
such a trivial error but I'm hoping to add some checking on the 'Number
of arguments' parameter the user returns and ensure that it's a valid
number; this patch sets up the foundation for that behavior.

> The behavior of `TeX-insert-macro' is controlled by
> `TeX-insert-macro-default-style'.  By default, after an empty optional
> argument AUCTeX doesn't prompt for further optional arguments, but if
> one sets it to `show-all-optional-args' he/she will be always prompted
> for the default value of the possible optional arguments with the
> a real problem, even though `TeX-insert-macro-default-style' probably
> isn't one of the most known options.
>
> To be honest this is the first time I see
>
>     \newcommand{\foo}[0]{Foobar}
>
> I've always seen and used the version without optional arguments when
> the new macro has no argument.  This is probably the reason why I
> never enter "0" at the "Number of arguments" prompt.

Agreed, I've never seen a \newcommand written as such.  Like yourself, I
always use the version that doesn't contain the optional parameter which
was the main motivation for the patch :-).

>> 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.
>
> Your patch actually swaps the nuisance: now the useless prompt occurs
> when `no-args' is an empty string ;-)

Oops - I thought I'd tested that.  I must have missed that one, sorry!

> A comment regarding the name of this variable: "no-args" to me means
> "no arguments".  It took me a few seconds to figure out that "no"
> stands for "number", I think that using "no" as a shorthand for
> something different from "no" is at least misleading.  But I'm not a
> native English speaker, :-)

That's a great point, I sometimes use silly abbreviations and should be
more verbose when it comes to code :-). I've substituted that for
"arg-count" now, would that be OK?

>> 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.
>
> As I said before, the patch is well-grounded, but please consider my
> suggestion(s) above.

Please find attached a V2 of the patch which takes into account your
suggestions.

Thanks,
--
Matt
```
```>From 07c7315ca9a12a6093c43adfb348416f34dd905f Mon Sep 17 00:00:00 2001
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 | 57 +++++++++++++++++++++++++++++++++++++++++++--------------
1 file changed, 43 insertions(+), 14 deletions(-)

diff --git a/latex.el b/latex.el
index 7221fce..64c0f58 100644
--- a/latex.el
+++ b/latex.el
@@ -1969,6 +1969,35 @@ 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-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."
+                    (TeX-argument-prompt optional prompt
+                                         "Number of arguments"
+                                         nil))))
+    (unless (or (string= arg-count "0")
+                (string= arg-count ""))
+      (TeX-argument-insert arg-count 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 +6051,16 @@ i.e. you do _not_ have to cater for this yourself by
'("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 +6272,21 @@ i.e. you do _not_ have to cater for this yourself by
(setq TeX-font-replace-function 'TeX-font-replace-macro)
'("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

```