5

I am using (which-function-mode t) to show current function name / org-heading on the mode line. I would like to limit the number of characters it shows on the mode line, but I couldn't find any option like max-func-name in which-func.el.

I can limit the string by redefining which-func-update-1 with (which-function) replaced by (truncate-string-to-width (which-function) 30):

(defun which-func-update-1 (window)
  "Update the Which Function mode display for window WINDOW."
  (with-selected-window window
    (when which-func-mode
      (condition-case info
          (let ((current (truncate-string-to-width (which-function) 30)))
            (unless (equal current (gethash window which-func-table))
              (puthash window current which-func-table)
              (force-mode-line-update)))
        (error
         (setq which-func-mode nil)
         (error "Error in which-func-update: %S" info))))))

But it is too hacky..

I tried to use advice as follow:

(defadvice which-func-update-1 (around testing activate)
  (cl-letf (((symbol-function 'which-function) (lambda () (truncate-string-to-width (which-function) 30)))) 
    ad-do-it))

But it generates infinite loop.

Anyone know how to limit the string length without modifying which-func.el? Thanks.

5

Instead of around advice use filter-return which will get the return value of the advised function and can do what it likes with it:

(advice-add 'which-function :filter-return
            (lambda (s) (truncate-string-to-width s 30)))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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