Display the beginning of a function in Emacs

This is an attempt to replicate a feature found in WebStorm:

WebStorm screenshot

When you are in the end of a block, and the beginning of the block is outside the viewport, it shows you the beginning in a nice overlay.

In Emacs we can show the beginning of function definition at point in the header line.

Emacs screenshot

Here's the Emacs Lisp code:

(defun show-beginning-of-defun ()
  "Display the beginning of defun in the header line."
  (let (start end)
    (save-excursion
      (beginning-of-defun)
      (setq start (line-beginning-position)
            end (line-end-position)))
    (if (>= start (window-start))
        (setq header-line-format nil)
      (setq header-line-format (buffer-substring-no-properties start end)))))

(add-hook 'prog-mode-hook
          (lambda ()
            (add-hook 'post-command-hook #'show-beginning-of-defun)))

Fork me on GitHub