My day to day being work or personal is to create OpenSource code. As an habit I have taken lately I am adding licenses to all new files I am creating.
I have historically used the `auto-insert-mode with a default template. For example for my newly created python files I would have this for configration :
;AutoInsert
(auto-insert-mode 't)
(setq auto-insert-alist '((python-mode . "python.py")))
and in my `auto-insert-directory directory there would be a python.py template with my license files.
But that’s not so smart, since I wanted to find a mechanism to switch between work email and personal emails for my copyright I needed those templates to be more dynamic.
Things with auto-insert templates taken from a directory they are not so dynamics and reading through the always excellent emacswiki page it seems that you need to `define-auto-insert the templates to get dynamic variables.
I didn’t want to go to the define-auto-insert because I am always using the yasnippet package for my snippets which has a great expansion support and a nice templating language.
As it’s mostly always the case if you have a problem you can be sure it’s already resolved by others, so a quick search on the internet leaded me to a gist file written by a guy three years ago that does exactly what I wanted :
So now that I had my header loaded from yasnippet I could add some smartness into it. I have first written this function :
(defun yas--magit-email-or-default ()
"Get email from GIT or use default"
(if (magit-get-top-dir ".")
(magit-get "user.email")
user-mail-address))
this function would check if we are in a git directory and get the user.email git configuration, if we are not it would just reply by the standard emacs variable `user-mail-address.
I have just to plug that in my template like this :
# -*- mode: snippet -*-
# name: header
# --
# -*- coding: utf-8 -*-
# Author: Chmouel Boudjnah < `(yas--magit-email-or-default)`>
and depending of my local configuration of my git repo it will add my license email according to that.
If I really needed to override the user-mail-address without using git I could always just drop a .dir-locals.el with something like this :
((nil . ((user-mail-address . "[email protected]"))))
which would override the default user-mail-address to whatever I want.