1

I want to set the title of emacs to be the mode-line when ever it changes.

[EDIT] solution:
As pointed to by the answer below, (setq frame-title-format mode-line-format) solved things elegantly.

Original question bits:

I can set the emacs title via:

(set-frame-parameter nil 'title (concat "*GNU EMACS* " (format-mode-line mode-line-format)))

For this, I would like to hook into the mode-line. But does it have a hook? How could I find out if it has a change-hook?

At present, I use a timer that updates it every 5 seconds, but this is not so elegant.

;-- Timer to update title of emacs during a pomodoro.
(defun my/pomodoro-start-title-update-timer ()
  (interactive)
  (setq pomodoro-title-update-timer    (run-at-time (current-time) 5
                 'my/set-title-to-mode-line)))

;-- I don't use the below, but useful:
(defun my/kill-pomodoro-timer ()
  (interactive)
  (cancel-timer pomodoro-title-update-timer )
)

(defun my/set-title-to-mode-line ()
  "Sets the title of the emacs window to the mode line. Used by pomodoro timer."
  (interactive)
  (set-frame-parameter nil 'title (concat "*GNU EMACS* " (format-mode-line mode-line-format)))
)

;-- Start the timer. TODO: change this to be a hook for when the mode-line changes.
(my/pomodoro-start-title-update-timer)

[EDIT]
I have tried adding a hook to post-command-hook like so:

(add-hook 'post-command-hook 
  (lambda ()
  'my/set-title-to-mode-line
  ))

But this only updates the the title if I change between buffers. The ticking of the pomodoro [Pomodoro~22:56] -> [Pomodoro~22:55] etc.. doesn't actually trigger this hook. So I can use the above for general purpose, and a timer for when pomodoro is active at present.

9
  • Take a look at the post-command-hook. It's overkill, but it will do what you want.
    – PythonNut
    Feb 12, 2015 at 21:29
  • 1
    overkill indeed :-P Feb 12, 2015 at 21:30
  • As far as I can tell, the mode-line does update after every command. It may not change, but it's recalculated.
    – PythonNut
    Feb 12, 2015 at 21:46
  • Oh, I see. I gotta try it out then. I'll do tomorrow. Feb 12, 2015 at 21:54
  • It should be ok, if you just reschedule a idle timer, so the updating does not actually happen after every command.
    – politza
    Feb 12, 2015 at 22:02

1 Answer 1

3

The mode line and frame title are automatically refreshed by evaluating their respective format variables. In general the best approach is to use the appropriate format variables and allow Emacs to decide when to update them. See mode-line-format and frame-title-format.

There are cases where you have something in your mode line (or frame title) that should be updated immediately based on some event. The force-mode-line-update function supports this: you can include a function in your mode-line format and whenever the relevant state changes you can force an update to have your format function called.

There doesn't appear to be an equivalent for the frame title, but from this SO answer is appears you can call (sit-for 0) to force a frame redisplay, which will cause the frame title format to be reevaluated.

If you really want to do something every time the mode line might change, the closest thing is probably the post-command-hook as others have noted in the comments.

If your goal here is to have the frame title match the mode line, this simplest approach is use the same format for both as noted here.

1
  • (setq frame-title-format mode-line-format) solved it. Thank you very much. (as described in the link you gave) Feb 13, 2015 at 16:02

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.