My setup
GNU Emacs GUI (version 24.4.51.1 (x86_64-w64-mingw32) compiled from Harroogan Emacs.) Package header2.el, which I use a lot. If you need more detail about header2, see the emacs wiki . It's a great package, I really recommend it.
Situation:
header2.el lets you define file headers for different types of file (C, shell, EmacsLisp, and so on). You can automatically insert a header when you open a new file buffer, and it modifies them whenever you're saving.
I made a code, that focused only on inserting the version number and incrementing it every time whenever you're saving the file.
The code below inserts the number 0 of version, when you're creating a new file.
(defsubst my/header-update-count ()
"Insert a count of the number of times the file has been saved."
(insert (concat comment-start " Version : 0\n")))
(setq make-header-hook '(my/header-update-count))
Configuring the update of the header whenever you're saving the file.
(defvar file-header-update-alist ()
"Used by `update-file-header' to know what to do in a file.
Is a list of sets of cons cells where the car is a regexp string and the cdr is
the function to call if the string is found near the start of the file.")
(defvar header-prefix-string ""
"Mode-specific comment prefix string for use in headers.")
(defun register-file-header-action (regexp function-to-call)
"Record FUNCTION-TO-CALL as the action to take if REGEXP is found
in the file header when a file is written. The function will be called
with the cursor located just after the matched REGEXP. Calling this twice
with the same args overwrites the previous FUNCTION-TO-CALL."
(let ((ml (assoc regexp file-header-update-alist)))
(if ml
(setcdr ml function-to-call);; overwrite old defn
;; This entry is new to us. Add to the master alist
(setq file-header-update-alist (cons (cons regexp function-to-call)
file-header-update-alist)))))
(register-file-header-action "Version [ \t]*: " 'update-write-count)
(when (boundp 'libreq-file-header)
(register-file-header-action libreq-file-header 'update-lib-requires))
;; 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)
Action:
Now the version numbering works like your version is 0
. When you're saving, it increments it with 1. So if your header have the version 5, when saving, it will change to version 6.
I would like to change the way of version numbering from one number to number dot number. For example, like when you're saving, it will update from 0.1
to 0.2
or from 3.12
to 3.13
.
So in order to modify this, I changed the version numbering with adding a dot and number:
(insert (concat comment-start " Version : 0.0\n"))
When I save the buffer, it updates from 0.0
to 1.0
to 2.0
, which is not the intention.
In my setup the following line uses regexp to update the version numbering
(register-file-header-action "Version [ \t]*: " 'update-write-count)
When I look into the source of the package header2.el, I noticed it uses regexp.
(register-file-header-action "\$VERSION[ \t]*=[ \t]*\"\\([0-9]+\\.\\)+"
'update-write-count)
When I modified the regexp, it didn't gives me the desired behaviour. Notice that adding a file header and updating it are not done by a minor mode. They are done on hooks. Adding a file header is done, for example, on a major-mode hook, and updating is done, for example, on write-file-hooks
. It's a fairly not a difficult modification, but I couldn't even figure it out.
Any suggestion would be appreciated.
"3.9"
to increment to"3.10"
, when should the major version increment from3
to4
?my-update-version-count
:(let* ((v-list (version-to-list "3.9")) (v-major (nth 0 v-list)) (v-minor (nth 1 v-list))) (format "%0d.%0d" v-major (1+ v-minor)))
This snippet returns"3.10"
."3.9"
, you will use the version string as input. And that's why I suggested using that snippet as a reference to write your ownmy-update-version-count
. The crux of the snippet is to useversion-to-list
and to do1+
only onv-minor
.