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?

1 Answer 1


The 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))
            ((member major-mode '(c-mode c++-mode glsl-mode))
                  (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)
                              (setq section
                                  (match-string-no-properties 1)
                      (?} ;; Section End
                        ;; Do nothing, we're not in a section.

          (when section
            (setq result (concat section ": " (or result which-func-unknown)))))
        (error (message "Error creating vc-backend root name: %s" err)))
  (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 ")")))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.