[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: which-func, imenu, AUCTeX
From: |
Arash Esbati |
Subject: |
Re: which-func, imenu, AUCTeX |
Date: |
Fri, 18 Oct 2024 14:07:38 +0200 |
User-agent: |
Gnus/5.13 (Gnus v5.13) |
Hi Greg,
Greg Bognar <greg.bognar@startmail.com> writes:
> Thanks for the (always) helpful explanation and pointers!
You're welcome.
> So (since I'm not very competent in elisp) are you suggesting that one
> way to get what I want would be to write a function (perhaps starting
> from `TeX-current-defun-name') that returns only what I want and than
> set the `which-func-functions' variable to this function, like below
> with `add-log-current-defun-function'?
Yes, that would be one solution. The function can look like this:
--8<---------------cut here---------------start------------->8---
(defun TeX-which-func-defun-name ()
"Return the name of the TeX section/paragraph/chapter at point, or nil."
(save-excursion
(let (s1 e1)
;; 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))))
;; Search backward for sectioning command. If
;; `LaTeX-section-label' is buffer-local, assume that a style
;; has changed the value and recalculate the string. Otherwise
;; take the standard one:
(when (re-search-backward
(concat
;; Consider only top-level macros
"^"
(when (eq major-mode 'docTeX-mode) "%")
"[ \t]*"
(if (local-variable-p 'LaTeX-section-label)
(concat "\\\\"
(regexp-opt
(remove "part" (mapcar #'car LaTeX-section-label)))
"\\*?")
"\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)\\*?"))
nil t)
;; Skip over the sectioning command, incl. the *:
(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 s1 (point))
(setq e1 (progn (forward-sexp)
(point)))
;; Now pick the content:
(buffer-substring-no-properties (1+ s1) (1- e1))))))
--8<---------------cut here---------------end--------------->8---
Then you do this in your init file:
(setq which-func-functions #'TeX-which-func-defun-name)
Do you want to give it a roll?
Best, Arash