7

I want to set the frame title as follows:

  • When the current buffer is visiting a file, show the full path name and the Emacs version.
  • When the current buffer has no file, then show the buffer name and the Emacs version.

In my init.el, I put

(setq-default frame-title-format
  (concat (if (buffer-file-name) "%f" "%b") " - " (substring (emacs-version) 0 15)))

But here is the result:screenshot with the title containing a file name without the full path

Why doesn't my code print the file name with the full path?

a_subscriber
  • 4,062
  • 1
  • 18
  • 56

3 Answers3

9

Because you're setting frame-title-format to "%b - GNU Emacs 26.1 ".

You can try the following instead

(setq frame-title-format
      `((buffer-file-name "%f" "%b")
        ,(format " - GNU Emacs %s" emacs-version)))

The following does the same but it probably does some unneeded work (that is, computing the version string) repeatedly

(setq frame-title-format
      (list '(buffer-file-name "%f" "%b")
            '(:eval (format " - GNU Emacs %s" emacs-version))))
xuchunyang
  • 14,527
  • 1
  • 19
  • 39
  • It's work. Nice. But what is different between (format " - GNU Emacs %s" emacs-version)) and '(:eval (format " - GNU Emacs %s" emacs-version)) ? – a_subscriber Nov 15 '18 at 14:22
  • FYI, OP: @rpluim answered the question in your comment. – Drew Nov 15 '18 at 14:35
  • @xuchunyang I'd forgotten about the (symbol then else) form for mode-line-format, nice one. – rpluim Nov 15 '18 at 15:31
  • 2
    @Alexei The value of (format " - GNU Emacs %s" emacs-version) is a constant and is known when Emacs starts. If you use '(:eval (format ...)), Emacs will compute it whenever the frame title needs to be updated, which is unnecessary. – xuchunyang Nov 15 '18 at 17:26
1

Emacs is evaluating your expression at the time when you setq frame-title-format, whereas you want it to be evaluated dynamically. Try wrapping your code in :eval as explained at Mode-Line-Data

rpluim
  • 5,245
  • 11
  • 25
0

To show the real file name in the titlebar, not just the buffer name, with the additional dired buffer case, and the last default to the buffer name:

(setq frame-title-format
      '(buffer-file-name (:eval (abbreviate-file-name buffer-file-name))
                         (dired-directory dired-directory
                                          "%b")))

As the manual explains, in those cases the first symbol specifies a conditional (buffer-file-name, dired-directory). The two steps form and the :eval clause might slow down redisplay, though.

santiagopim
  • 121
  • 3