HeaderLine

The header line is a fixed line displayed at the top of an Emacs window. This feature is new in GnuEmacs 21 (and is not in XEmacs).

The header line is set with the header-line-format variable, the same as mode-line-format for the mode line at the bottom of the window. Unlike the mode line, there’s no global default for the header line, it’s only used by modes that specifically choose to.

Hscroll

The header line, like the mode line, doesn’t follow any Hscrolling. If you’re writing a mode that shows column headings or the like then you probably want it to. SimpleEmacsSpreadsheet is an example of doing that.

The hscroll can be followed by using the :eval feature (described under mode-line-format). Look at window-hscroll and use it to chop off some of the string at the left. For example

    (defconst my-header "some long header line string ...")
    (setq header-line-format '(:eval (substring my-header
                                                (min (length my-header)
                                                     (window-hscroll)))))

Fringe

On the new X graphical displays the header line extends at the left across TheFringe and the scroll bar (if the scroll bar is at the left), so to align a header string to columns of text you need to adjust for that. In Emacs 22.1, you can add a space with an :align-to TextProperty (see specified spaces),

    (setq header-line-format
          (concat (propertize " " 'display '((space :align-to 0)))
                  "some header text"))

In Emacs 21 however this doesn’t work, it’s just an ordinary space. The effect can be approximated there by adding one space for the fringe and two for a left scrollbar. An :eval can dynamically follow any change of scrollbar position.

    (setq header-line-format
          '(:eval
            (concat (and (display-graphic-p)
                         (concat " "  ;; fringe
                                 (and (eq 'left (frame-parameter nil 'vertical-scroll-bars))
                                      "  ")))  ;; left scrollbar
                    "some header text")))

display file path in header line

  (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 "#8fb28f"
                               :weight 'bold
                               )))
        (concat (with-face sl/header
                           ;; :background "green"
                           ;; :foreground "black"
                           :weight 'bold
                           :foreground "#8fb28f"
                           )
                (with-face (file-name-nondirectory buffer-file-name)
                           :weight 'bold
                           ;; :background "red"
                           )))))

  (defun sl/display-header ()
    (setq header-line-format
          '("" ;; invocation-name
            (:eval (if (buffer-file-name)
                       (sl/make-header)
                     "%b")))))



  (add-hook 'buffer-list-update-hook
            'sl/display-header)


CategoryGlossary