7

I found the package Automatic File Headers, I cheered and installed it with MELPA. I have found file headers to be very valuable in project development. I always know who has been where and how many times they were there. Most often, I also know what they did. The update count and last modified date are very useful in determining the proper version of a file to use.

There is one thing that I couldn't figure it out.

For example, after installing header2 from MELPA, declare it in your dot-emacs:

;; To have Emacs update file headers automatically whenever you save a
;; file, put this in your init file (~/.emacs):

(autoload 'auto-update-file-header "header2")
(add-hook 'write-file-hooks 'auto-update-file-header)

;; To have Emacs add a file header whenever you create a new file in
;; some mode, put this in your init file (~/.emacs):

(autoload 'auto-make-header "header2")
(add-hook 'emacs-lisp-mode-hook 'auto-make-header)
(add-hook 'c-mode-common-hook   'auto-make-header)

So when you create a new file in Emacs, for example foobar.el. You see a header. And it will automatically update when you write the file. Okay, let's assume I would like to change the text Last-Updated: into Foo-Last-Updated.

So I did a grep on Last-Updated from the package header2 and replaced all occurences with Foo-Last-Updated. I restarted Emacs to be sure. And there is still Last-Updated displayed instead Foo-Last-Updated.

Any suggestion, in order to change the header contents? I found nothing in the manuals.

  • You shouldn't have to modify the package contents. You can redefine the stuff you want after requiring the package. I'll post my example in an answer once I get to a PC. It is still loading the old stuff because you haven't updated the .elc file in the same folder. Delete the .elc files for the package and then your edits in the .el file will be effective. – Kaushal Modi Apr 29 '15 at 10:58
  • @kaushalmodi thanks for your suggestion. I removed the compiled *.elc files. And restarted Emacs. Nothing changes so far... another suggestion? – ReneFroger Apr 29 '15 at 11:42
  • Have you tried it on a brand-new file? If you're using an old one that already has a header, I would wonder if your change fails to update the pre-existing header. – Dan Apr 29 '15 at 12:51
4

Customizing header2 headers

From the documentation in header2.el:

Define individual header elements. These are the building blocks used to construct a site specific header. You may add your own functions either in this file or in your .emacs file. The variable make-header-hook specifies the functions that will actually be called.

Here is an example of how to customize the header to your liking:

(require 'header2)

(defsubst my/header-timestamp ()
  "Insert field for timestamp"
  (insert header-prefix-string  "Time-stamp: <>\n"))

(defsubst my/header-projectname ()
  "Insert Project Name"
  (insert header-prefix-string "Project    : "
          (when (featurep 'projectile)
            (replace-regexp-in-string "/proj/\\(.*?\\)/.*"
                                      "\\1"
                                      (projectile-project-root)))
          "\n"))

(defsubst my/header-description ()
  "Insert \"Description: \" line."
  (insert header-prefix-string "Description: \n"))

(defsubst my/header-dash-line ()
  "Insert dashed line"
  (insert header-prefix-string)
  (insert-char ?- fill-column)
  (insert "\n"))

(setq make-header-hook '(my/header-timestamp
                         header-blank
                         my/header-dash-line
                         my/header-projectname
                         header-file-name
                         header-author
                         my/header-description
                         my/header-dash-line))

(add-hook 'emacs-lisp-mode-hook #'auto-make-header)

Modifying the packages directly

In general this is not recommended because if you like to update the packages using the package manager then your edits will be lost when the package updates.

Starting from version 24.4 of emacs, you can add the below to your emacs init at very beginning before you start loading packages. That will ensure that always the newest file is loaded (be it a .el or .elc file).

(setq load-prefer-newer t)

But if you don't have that, by default, the .elc file is loaded if present (and the .el is ignored).

But again, try not to edit the package .el files directly.

More info

  • Thanks for your very clear explaination, it's clear now. I will apply the changes by your example. And I will not try to edit the package anymore. – ReneFroger Apr 30 '15 at 6:39
  • I changed the contents of my custom header to Last changed : on 30-04-2015 Version : 2 How could I override the default behaviour of header2, that he replaces the contents from last changed and version? – ReneFroger Apr 30 '15 at 23:20

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.