Subject: [PATCH] `tex-current-defun-name' matches the wrong title

From: Arash Esbati <[email-masked]>
Date: Sat, 30 Mar 2019 21:30:02 +0100

Hi all,

please consider this LaTeX file with the latex-mode provided by Emacs
(not AUCTeX).

--8<---------------cut here---------------start------------->8---
\documentclass{article}

\begin{document}

\section{Emacs}

\subsection*[Emacs is the extensible, customizable, self-documenting
real-time display editor]{Emacs is the extensible, customizable,
  self-documenting real-time display editor}

\end{document}
--8<---------------cut here---------------end--------------->8---

In that buffer, eval (goto-char 68) and hit `C-x 4 a'.  Emacs enters the
first section (section{Emacs}) into the ChangeLog buffer.  I think
`tex-current-defun-name' could be more cautious like
`lisp-current-defun-name' is.  A possible patch is attached.  I will add
something like this also to AUCTeX since it currently doesn't set
`add-log-current-defun-function' at all.

Any comments welcome.

Best, Arash

>From 97394551775c27e3de77cd9f07b319cefb72ccad Mon Sep 17 00:00:00 2001
From: Arash Esbati <[email-masked]>
Date: Sat, 30 Mar 2019 21:05:06 +0100
Subject: [PATCH] Match the correct sectioning command

* lisp/textmodes/tex-mode.el (tex-current-defun-name): Select the
current sectioning command when point is on the command itself.
---
 lisp/textmodes/tex-mode.el | 42 +++++++++++++++++++++++++++++++-------
 1 file changed, 35 insertions(+), 7 deletions(-)

diff --git a/lisp/textmodes/tex-mode.el b/lisp/textmodes/tex-mode.el
index 9c91d27b94..88134603b9 100644
--- a/lisp/textmodes/tex-mode.el
+++ b/lisp/textmodes/tex-mode.el
@@ -442,13 +442,41 @@ latex-outline-level
 (defun tex-current-defun-name ()
   "Return the name of the TeX section/paragraph/chapter at point, or nil."
   (save-excursion
-    (when (re-search-backward
-	   "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)"
-	   nil t)
-      (goto-char (match-beginning 0))
-      (buffer-substring-no-properties
-       (1+ (point))	; without initial backslash
-       (line-end-position)))))
+    (let (s1 e1 s2 e2)
+      ;; If we are now precisely at the beginning of a sectioning
+      ;; command, move forward and make sure `re-search-backward'
+      ;;  finds this one rather than the previous one:
+      (or (eobp) (progn
+                   (when (looking-at-p "\\\\")
+                     (forward-char))
+                   (unless (eolp)
+                     (forward-sexp))))
+      (when (re-search-backward
+             "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)\\*?"
+             nil t)
+        ;; Skip over the backslash:
+        (setq s1 (1+ (point)))
+        ;; Skip over the sectioning command, incl. the *:
+        (setq e1 (goto-char (match-end 0)))
+        ;; Skip over the optional argument, if any:
+        (when (looking-at-p "[ \t]*\\[")
+          (forward-sexp))
+        ;; Skip over any chars until the mandatory argument:
+        (skip-chars-forward "^{")
+        ;; Remember the points for the mandatory argument:
+        (setq s2 (point))
+        (setq e2 (progn (forward-sexp)
+                        (point)))
+        ;; Now pick the content: For one-line title, return it
+        ;; incl. the closing brace.  For multi-line, return the first
+        ;; line of the mandatory argument incl. ellipsis and a brace
+        (concat
+         (buffer-substring-no-properties s1 e1)
+         (buffer-substring-no-properties
+          (goto-char s2)
+          (min (line-end-position) e2))
+         (when (> e2 (line-end-position))
+           (concat "..." "}")))))))
 
 ;;;;
 ;;;; Font-Lock support
-- 
2.21.0