For C/C++ code that use doxygen sections, it would be useful to show these in the mode-line, without slowing down the mode-line display significantly.
Is there a good way to add context information to the mode-line?
Emacs Stack Exchange is a question and answer site for those using, extending or developing Emacs. It only takes a minute to sign up.
Sign up to join this communityThe built-in which-function
package already does something similar, getting the function.
You can extend this using advice, this way which-function
handles the timers and accesses the current function, you can add extra information to this as needed.
(with-eval-after-load 'which-func
(defun which-function--add-comment-section (orig-fn)
(let ((result (funcall orig-fn)))
(when result
(setq result (propertize result 'face 'font-lock-keyword-face)))
;; Error's are very unlikely, this is to ensure even the most remote
;; chance of an error, don't cause which-func to fail.
(condition-case err
(let ((section nil))
(cond
((member major-mode '(c-mode c++-mode glsl-mode))
(save-match-data
(save-excursion
(when (re-search-backward "\\([\\\@]\}\\|[\\\@]\{\\)" nil t 1)
(pcase (char-after (+ 1 (point)))
(?{ ;; Section Start
(let ((section-start-pt (point)))
;; Find start of comment.
(when (re-search-backward "\\/\\*" nil t 1)
(when
(re-search-forward
"[\\@]name[[:blank:]]+\\(.*\\)"
section-start-pt
t
1)
(setq section
(propertize
(match-string-no-properties 1)
'face
'font-lock-comment-face))))))
(?} ;; Section End
;; Do nothing, we're not in a section.
nil)))))))
(when section
(setq result (concat section ": " (or result which-func-unknown)))))
(error (message "Error creating vc-backend root name: %s" err)))
result))
(advice-add 'which-function :around #'which-function--add-comment-section))
This matches sections written in this style.
/** \name Main Function
*
* Body text.
* \{ */
/* --- your code --- */
/** \} */
When the cursor is in "your code" the mode line will show the function name and section text.
The mode line needs to include something like this:
'(:eval (when (bound-and-true-p which-func-mode) (list " (" which-func-current ")")))