Review text and code of the `which-function-mode' section

This commit is contained in:
Gerard Vermeulen 2024-02-06 09:24:56 +01:00
parent 62ec3d47f5
commit 194a0062c1

View File

@ -4721,8 +4721,10 @@ it for this buffer is by typing {{{kbd(C-c C-e t U)}}} to export the it to a
:CUSTOM_ID: sec:which-function-mode :CUSTOM_ID: sec:which-function-mode
:END: :END:
The code in listing [[lst:setup-which-function-mode]] has stolen ideas from the The post [[https://codelearn.me/2024/02/02/emacs-which-function-mode.html][Emacs: which-function-mode]] claims that ~which-function-mode~ works in
following links: ~org-mode~. This is true in case all document headlines are simple, but is not
true in case document headlines contain links. The code in listing
[[lst:setup-which-function-mode]] has stolen ideas from the following links:
- [[https://list.orgmode.org/20240205.141235.268481480563517065.teika@gmx.com/][Show current org-mode headline in frame header]]. - [[https://list.orgmode.org/20240205.141235.268481480563517065.teika@gmx.com/][Show current org-mode headline in frame header]].
- [[https://emacs.stackexchange.com/questions/30894/][Show current org-mode headline in modeline]]. - [[https://emacs.stackexchange.com/questions/30894/][Show current org-mode headline in modeline]].
@ -4735,30 +4737,32 @@ following links:
(defvar which-func-functions nil) (defvar which-func-functions nil)
(defun which--org-function () (defun which-func-org-function ()
"Return level and title of the current headline. "Return level and title of the current headline.
Return the document title when point is above the first headline." Return the document title when point is above the first headline."
(interactive) ;; Keep this function interactive for debugging. (interactive) ;; Keep this function interactive for fixing.
(when (eq major-mode 'org-mode) (when (eq major-mode 'org-mode)
(let (text) (let (text)
;; Handle point eventually above the first headline.
(unless (org-at-heading-p) (unless (org-at-heading-p)
(save-excursion (save-excursion
(org-previous-visible-heading 1) (org-previous-visible-heading 1)
(let ((eap (org-element-at-point))) (let ((eap (org-element-at-point)))
(when (org-element-type-p eap 'keyword) (when (org-element-type-p eap 'keyword)
(setq text (format "0|%s" (org-element-property :value eap))))))) (setq text (format "0|%s" (org-element-property :value eap)))))))
;; Handle point anywhere else.
(unless text (unless text
(let* ((chain (org-get-outline-path t)) (let* ((chain (org-get-outline-path t))
(count (length chain))) (count (length chain)))
(setq text (format "%s|%s" count (nth (1- count) chain))))) (setq text (format "%s|%s" count (nth (1- count) chain)))))
text))) text)))
(add-to-list 'which-func-functions 'which--org-function) (add-to-list 'which-func-functions 'which-func-org-function)
(defun which--pdf-function () (defun which-func-pdf-view-function ()
"Return the title of the current headline. "Return the title of the current headline.
Return \"Front Matter\" when point is above the first headline." Return \"Front Matter\" when point is above the first headline."
(interactive) ;; Keep this function interactive for debugging. (interactive) ;; Keep this function interactive for fixing.
(when (eq major-mode 'pdf-view-mode) (when (eq major-mode 'pdf-view-mode)
(let* ((current-page (pdf-view-current-page)) (let* ((current-page (pdf-view-current-page))
(outline (pdf-info-outline (current-buffer))) (outline (pdf-info-outline (current-buffer)))
@ -4774,7 +4778,7 @@ Return \"Front Matter\" when point is above the first headline."
(cl-incf hl-index)) (cl-incf hl-index))
(if (< current-page hl-page) old-title new-title)))) (if (< current-page hl-page) old-title new-title))))
(add-to-list 'which-func-functions 'which--pdf-function) (add-to-list 'which-func-functions 'which-func-pdf-view-function)
#+end_src #+end_src
* [[info:emacs#Saving Emacs Sessions][Saving Emacs sessions (info)]] * [[info:emacs#Saving Emacs Sessions][Saving Emacs sessions (info)]]