如何实现文件作者信息注释?

各位,有没有自动添加自定义文件头和函数注释的方案?如下:通过快捷键自动插入,并且更新最后修改时间。

# -*- coding: utf-8 -*-

# **********************************************************
# * Author        : xxxxxx
# * Email         : [email protected]
# * Create time   : 2018-04-22 19:26
# * Last modified : 2018-04-25 14:37
# * Filename      : collect-sys-msg.py
# * Description   :
# **********************************************************

doxymacs可以实现大部分功能, 但是不能更新最后修改时间.

这些注释也只是普通的文字,应该不难想到怎么解决,比如自己定义一个命令:

(defun your-insert-python-comment-section ()
  "Insert comments according to URL `https://emacs-china.org/t/topic/5689'."
  (interactive)
  (insert
   (format
    "\
# -*- coding: utf-8 -*-

# **********************************************************
# * Author        : %s
# * Email         : %s
# * Create time   : %s
# * Last modified : %s
# * Filename      : %s
# * Description   :
# **********************************************************"
    user-full-name
    user-mail-address
    (format-time-string "%Y-%m-%d %H:%M")
    (format-time-string "%Y-%m-%d %H:%M")
    (file-name-nondirectory buffer-file-name))))

还有像 auto-insert 和 YASnippet 之类的专门解决这类问题的工具。另外假如使用 Python 3 的话,不应该再重复指定 UTF-8。

这个也得你自己选择,自己实现也不会很困难(写一个命令或者函数更新修改时间,还可以考虑加到 before-save-hook 上实现「自动」更新),也有 time-stamp.el 现成的工具。

假如我有这个需求的话,我第一个想到的解决方法是:

(defun your-update-last-modified ()
  (interactive)
  (save-excursion
    (goto-char (point-min))
    (when (re-search-forward
           (concat "^" (regexp-quote "# * Last modified :"))
           nil 'no-error)
      (delete-region (point) (line-end-position))
      (insert (format-time-string " %Y-%m-%d %H:%M")))))

(add-hook 'before-save-hook 'your-update-last-modified)
1 个赞

启用

(add-hook 'before-save-hook 'time-stamp)

在文件注释区域添加:

Time-stamp: <2018-04-25 17:01:44 gxy>

好用,谢谢!!

很早之前就有这样的需求,可以使用emacs内置的auto-inserttime-stamp

emacs自动添加文件头

emacs自动更新文件头

1 个赞