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.

Leo Ufimtsev
  • 4,488
  • 3
  • 22
  • 45
  • Take a look at the `post-command-hook`. It's overkill, but it _will_ do what you want. – PythonNut Feb 12 '15 at 21:29
  • 1
    overkill indeed :-P – Leo Ufimtsev Feb 12 '15 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 '15 at 21:46
  • Oh, I see. I gotta try it out then. I'll do tomorrow. – Leo Ufimtsev Feb 12 '15 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 '15 at 22:02
  • The tag description for `emacs24.4` states: "Do NOT use this tag unless you are certain that your question applies ONLY to Emacs version 24.4." Please stop using it as a general tag. – Dan Feb 13 '15 at 00:12
  • @LeoUfimtsev it's not overkill. On average, the mode line changes (if you have column or line number displays) after almost every command. That's what you'll need to use. But it's actually quite fast, if that's what you are worried about. – Malabarba Feb 13 '15 at 01:20
  • Or, even better, follow glucas answer so the title updates itself : http://emacs.stackexchange.com/a/8196/50 – Malabarba Feb 13 '15 at 01:25
  • Go with @glucas's suggestions. The mode-line is updated during *redisplay*. You don't want to be doing things on hooks in that context. – Drew Feb 13 '15 at 15:49

1 Answers1

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.

glucas
  • 20,175
  • 1
  • 51
  • 83