[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: which-func, imenu, AUCTeX
From: |
Greg Bognar |
Subject: |
Re: which-func, imenu, AUCTeX |
Date: |
Fri, 18 Oct 2024 22:08:09 +0200 |
Hi Arash,
Thanks! Meanwhile, I also tried to write a simple function, using
`tex-current-defun-name' from `tex-mode.el' as the starting point:
----------8<----------8<----------8<----------8<----------8<----------
(defun my-latex-current-heading-name ()
"Return the name of the LaTeX section/paragraph/chapter at point, or nil.
To be used by which-func to display heading name in the modeline."
(save-excursion
(when (re-search-backward
"^\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)" nil t)
(goto-char (search-forward "{" nil t))
(buffer-substring-no-properties (point)
(1- (search-forward "}" nil t))))))
----------8<----------8<----------8<----------8<----------8<----------
When I tested both of them, strange and wondrous things started to happen :)
Both seem to work well when I use `add-log-current-defun-function'
(setq add-log-current-defun-function #'my-latex-current-heading-name)
(setq add-log-current-defun-function #'TeX-which-func-defun-name)
But when I use either
(setq which-func-functions #'TeX-which-func-defun-name)
(setq which-func-functions #'my-latex-current-heading-name)
they don't seem to work well. In particular, remember that line in the preamble
of my test file:
\titlespacing\section{0pt}{2\bigskipamount}{\parskip}
They give me section{0pt} in the modeline when I'm before the first \section,
which they shouldn't (since neither does this when set to
`add-log-current-defun-function').
Perhaps this has something to do with `which-func-functions'? Again, both
functions work with `add-log-current-defun-function'.
In addition, your function (but I think mine not, at least so far) sometimes
generates an error with my test file:
Error running timer `which-func-update': (error "Error in which-func-update:
(scan-error \"Containing expression ends prematurely\" 1088 1089)")
The buffer positions which are indicated are in this line:
\bibpunct[:~]{(}{)}{,}{a}{}{,}
specifically, the error points to one of the }{ on this line.
In any case, I'm happy with using `add-log-current-defun-function'. I will also
try to modify the function(s) to pick up on beamer \frametitle's too.
All the best,
Greg
-------------------------------------------------------------------------------
On Fri 18 Oct 2024 at 14:07 Arash Esbati wrote:
>
> 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