2

Similar to the somewhat neat, but in the end too clunky, hydra-goto-line, I'd like to customize goto-line to enable linum-mode, but only during the function call.

I tried add-advice, :before and :after, but nothing kinda happened. So what's the proper way to enable a minor-mode during a single function call?

  • 2
    Please elaborate on exactly what it is that you are trying to achieve. Something which is enabled "only during the function call" is rather unlikely to ever get a chance to affect the display. – phils Oct 16 '17 at 21:38
  • And show the code you tried, for which you say only "nothing kinda happened." Not much to go on, here. You might help yourself by helping others help you. – Drew Oct 17 '17 at 0:30
3

I probably borrowed this from somewhere, but darned if I remember where:

(defun goto-line-with-feedback ()
  "Show line numbers temporarily, while prompting for the line number input."
  (interactive)
  (defvar goto/line 0)
  (unwind-protect
      (progn
        (linum-mode 1)
        (setq goto/line (read-number "Goto line: "))
        (goto-char (point-min))
        (forward-line (1- goto/line)))
    (linum-mode -1)))

I rebind M-g to that; works a treat.

(Note that if you're using Emacs 26, you should use display-line-numbers-mode instead of linum-mode -- just swapping the two function calls is all you need to do.)

As a general rule — to try to address your question in a more generic, "teach you to fish" kind of way — any function of the form foo-mode in Emacs will take a positive argument to unconditionally enable the mode, and a negative (or sometimes nil) argument to unconditionally disable it. The best way to validate that is to read the documentation for the function (C-h f then the function name).

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.