Emacs自动插入更新文件头——maple-header
一个简单的插件,可以在文件创建时自动插入文件头,或者文件更新时自动更新文件头信息,虽然已经用了好几年,这次把相关代码提取并精简一下做成了插件 maple-header
安装
使用quelpa
1(use-package maple-header 2 :quelpa (:fetcher github :repo "honmaple/emacs-maple-header") 3 :hook (after-init . maple-header-mode))
克隆到本地
1git clone https://github.com/honmaple/emacs-maple-header --depth=1 ~/.emacs/site-lisp/maple-header
配置
1(use-package maple-header 2 :ensure nil 3 :hook (after-init . maple-header-mode))
基础配置
1;; 更新文件头时搜索的文件函数, 9代表只会搜索前9行 2(setq maple-header:lines 9) 3;; 创建新文件时是否自动插入文件头信息 4(setq maple-header:auto-insert t) 5;; 更新文件时是否自动更新文件头信息 6(setq maple-header:auto-update t)
自定义模版
比如我要自定义Python文件的文件头
1(add-to-list 'maple-header:auto-insert-alist 2 '((python-mode . "Python program") nil 3 "#!/usr/bin/env python\n" 4 "# -*- coding: utf-8 -*-\n"))
自定义更新
目前内置了 filename, email, modify 三个字段, 可以通过调用 maple-header:update-xxx 手动更新指定的字段, 如果需要增加新的或者覆盖原字段,可以使用
1(add-to-list 'maple-header:auto-update-alist '(author . custom-update-author)) 2 3(defun custom-update-author(&optional current-line) 4 (interactive) 5 (maple-header:update-action 6 ".*\\(Author:\\)\\(.*\\)" 7 user-full-name current-line))
使用 maple-header:update 会更新所有字段(包括:filename,email,modify,author), 如果只需要更新部分,需要修改为
1(setq maple-header:auto-update-alist 2 '((filename . maple-header:update-filename) 3 (modify . maple-header:update-modify)))
