0

I've been using code taken mostly from the header line Emacs wiki to display the file path and current function of the current buffer.

(defmacro with-face
    (str &rest properties)
  `(propertize ,str 'face (list ,@properties)))

(defun sl/make-header ()
  "."
  (let* ((sl/full-header (abbreviate-file-name buffer-file-name))
         (sl/header (file-name-directory sl/full-header))
         (sl/drop-str "[...]")
         )
    (if (> (length sl/full-header)
           (window-body-width))
        (if (> (length sl/header)
               (window-body-width))
            (progn
              (concat (with-face sl/drop-str
                                 :background "blue"
                                 :weight 'bold
                                 )
                      (with-face (substring sl/header
                                            (+ (- (length sl/header)
                                                  (window-body-width))
                                               (length sl/drop-str))
                                            (length sl/header))
                                 ;; :background "red"
                                 :weight 'bold
                                 )))
          (concat
           (with-face sl/header
                      ;; :background "red"
                      :foreground "red"
                      :weight 'bold)))
      (concat (if window-system ;; In the terminal the green is hard to read
                  (with-face sl/header
                             ;; :background "green"
                             ;; :foreground "black"
                             :weight 'bold
                             :foreground "#8fb28f"
                             )
                (with-face sl/header
                           ;; :background "green"
                           ;; :foreground "black"
                           :weight 'bold
                           :foreground "blue"
                           ))
              (with-face (file-name-nondirectory buffer-file-name)
                         :weight 'bold
                         ;; :background "red"
                         )))))

(defun sl/display-header ()
  "Create the header string and display it."
  ;; The dark blue in the header for which-func is terrible to read.
  ;; However, in the terminal it's quite nice
  (if window-system
      (custom-set-faces
       '(which-func ((t (:foreground "#8fb28f")))))
    (custom-set-faces
     '(which-func ((t (:foreground "blue"))))))
  ;; Set the header line
  (setq header-line-format

        (list "-"
              '(which-func-mode ("" which-func-format))
              '("" ;; invocation-name
                (:eval (if (buffer-file-name)
                           (concat "[" (sl/make-header) "]")
                         "[%b]")))
              )
        )
  )
;; Call the header line update
(add-hook 'buffer-list-update-hook
          'sl/display-header)

However, this prevents major modes from setting their own header lines. For instance, when I use GDB with many-windows set to true, this prevents me from toggling between locals/registers and threads/breakpoints.

I tried changing the code block to

(if header-line-format
    nil
  (add-hook 'buffer-list-update-hook 'sl/display-header))

but this does not give the expected result (it seems to check for the first buffer and then not after that).

Is there a way to display my own custom header line only if the current buffer wouldn't display its own?

  • Put your test inside the function you put on the hook (sl/display-header). Have that function do nothing if the test for an existing header line succeeds. – Drew Nov 13 '18 at 14:24
0

Drew's comment resolved the issue. I'm posting the correct sl/display-header function in case anyone else runs into this.

(defun sl/display-header ()
  "Create the header string and display it."
  ;; The dark blue in the header for which-func is terrible to read.
  ;; However, in the terminal it's quite nice
  (if header-line-format
      nil
    (if window-system
        (custom-set-faces
         '(which-func ((t (:foreground "#8fb28f")))))
      (custom-set-faces
       '(which-func ((t (:foreground "blue"))))))
    ;; Set the header line
    (setq header-line-format
          (list "-"
                '(which-func-mode ("" which-func-format))
                '("" ;; invocation-name
                  (:eval (if (buffer-file-name)
                             (concat "[" (sl/make-header) "]")
                           "[%b]")))
                )
          )
    )
  )

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.