auctex-devel
[Top][All Lists]
Advanced

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

[AUCTeX-devel] Prompt for optional short caption parameter.


From: Matthew Leach
Subject: [AUCTeX-devel] Prompt for optional short caption parameter.
Date: Wed, 16 Mar 2016 00:00:42 +0000
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1.50 (gnu/linux)

Hi all,

I quite frequently use the optional short-caption parameter that one can
pass LaTeX's \caption macro.  However, when defining a new figure, I'm
not prompted for \caption's optional argument and usually have to add
this in manually.

This patch adds a new prompt when creating a new figure environment that
will ask the user for \caption's optional parameter.  However, to
prevent this becoming an inconvenience, the optional parameter is only
prompted for if the length of the caption parameter is >=
`LaTeX-short-caption-prompt-length'.  The default value of the new
length parameter has a default value of 40 which I chose somewhat
arbitrarily, so can be easily changed if people like or removed
altogether.

Feedback welcome :-).

Thanks,
-- 
Matt
>From cc548a7c7af839bf7bfa0436375d4ff8194d5b7a Mon Sep 17 00:00:00 2001
From: Matthew Leach <address@hidden>
Date: Tue, 15 Mar 2016 23:26:29 +0000
Subject: [PATCH] Prompt for optional short caption parameter.

* latex.el (LaTeX-compose-caption-macro): New.
  (LaTeX-short-caption-prompt-length): New.
  (LaTeX-env-figure): Prompt for optional short caption parameter, when
  the caption length is >= `LaTeX-short-caption-prompt-length', and pass
  to `LaTeX-compose-caption-macro'.
---
 latex.el | 37 ++++++++++++++++++++++++++++---------
 1 file changed, 28 insertions(+), 9 deletions(-)

diff --git a/latex.el b/latex.el
index 64c0f58..c0b0baf 100644
--- a/latex.el
+++ b/latex.el
@@ -1032,16 +1032,35 @@ The inserted label is returned, nil if it is empty."
              label)
          nil)))))
 
+(defcustom LaTeX-short-caption-prompt-length 40
+  "The length that the caption of a figure should be before
+  propting for \\caption's optional short-version."
+  :group 'LaTeX-environment
+  :type 'integer)
+
+(defun LaTeX-compose-caption-macro (caption &optional short-caption)
+  "Return a \\caption macro for a given CAPTION as a string.
+If SHORT-CAPTION is non-nil pass it as an optional argument to
+\\caption."
+  (let ((short-caption-string
+         (if (and short-caption
+                  (not (string= short-caption "")))
+             (concat LaTeX-optop short-caption LaTeX-optcl))))
+    (concat TeX-esc "caption" short-caption-string
+            TeX-grop caption TeX-grcl)))
+
 (defun LaTeX-env-figure (environment)
   "Create ENVIRONMENT with \\caption and \\label commands."
-  (let ((float (and LaTeX-float                ; LaTeX-float can be nil, i.e.
+  (let* ((float (and LaTeX-float               ; LaTeX-float can be nil, i.e.
                                        ; do not prompt
-                   (TeX-read-string "(Optional) Float position: " 
LaTeX-float)))
-       (caption (TeX-read-string "Caption: "))
-       (center (y-or-n-p "Center? "))
-       (active-mark (and (TeX-active-mark)
-                         (not (eq (mark) (point)))))
-       start-marker end-marker)
+                     (TeX-read-string "(Optional) Float position: " 
LaTeX-float)))
+         (caption (TeX-read-string "Caption: "))
+         (short-caption (when (>= (length caption) 
LaTeX-short-caption-prompt-length)
+                          (TeX-read-string "(Optional) Short caption: ")))
+         (center (y-or-n-p "Center? "))
+         (active-mark (and (TeX-active-mark)
+                           (not (eq (mark) (point)))))
+         start-marker end-marker)
     (when active-mark
       (if (< (mark) (point))
          (exchange-point-and-mark))
@@ -1064,7 +1083,7 @@ The inserted label is returned, nil if it is empty."
       (if (member environment LaTeX-top-caption-list)
          ;; top caption
          (progn
-           (insert TeX-esc "caption" TeX-grop caption TeX-grcl)
+           (insert (LaTeX-compose-caption-macro caption short-caption))
            ;; If `auto-fill-mode' is active, fill the caption.
            (if auto-fill-function (LaTeX-fill-paragraph))
            (LaTeX-newline)
@@ -1082,7 +1101,7 @@ The inserted label is returned, nil if it is empty."
          ;; If there is an active region point is before the backslash of
          ;; "\end" macro, go one line upwards.
          (when active-mark (forward-line -1) (indent-according-to-mode))
-         (insert TeX-esc "caption" TeX-grop caption TeX-grcl)
+         (insert (LaTeX-compose-caption-macro caption short-caption))
          ;; If `auto-fill-mode' is active, fill the caption.
          (if auto-fill-function (LaTeX-fill-paragraph))
          ;; ask for a label and if necessary insert a new line between caption
-- 
2.7.3


reply via email to

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