emacs自动添加文件头


想要在emacs中添加文件头有好几种方法:

  • 直接手打
  • 使用模板yasnippet
  • 使用header2
  • 使用内置的package auto-insert

header2可能是最方便的一种自动添加文件头的方式,但我之前使用过,想要自定义自己想要的内容很难,而且白白浪费header2的功能

今天主要说一说emacs内置里auto-insert,使用它在创建文件时自动添加文件头信息

基础使用

直接(auto-insert-mode) 并且设置

(setq auto-insert-query nil) ;; 不提醒

自定义文件头

(setq auto-insert-alist
      '(((python-mode . "Python program") nil
         "#!/usr/bin/env python\n"
         "# -*- coding: utf-8 -*-\n"
         "# **************************************************************************\n"
         "# Copyright © " (substring (current-time-string) -4) " " (user-full-name) "\n"
         "# File Name: " (file-name-nondirectory buffer-file-name) "\n"
         "# Author: " (user-full-name)"\n"
         "# Email: " user-mail-address "\n"
         "# Created: " (format-time-string "%Y-%m-%d %T (%Z)" (current-time)) "\n"
         "# Last Update: " (format-time-string "%Y-%m-%d %T (%Z)" (current-time)) "\n"
         "#          By: \n"
         "# Description: \n"
         "# **************************************************************************\n")
        ((c-mode . "C program") nil
         "/**************************************************************************\n"
         " Copyright © " (substring (current-time-string) -4) " " (user-full-name) "\n"
         " File Name: " (file-name-nondirectory buffer-file-name) "\n"
         " Author: " (user-full-name)"\n"
         " Email: " user-mail-address "\n"
         " Created: " (format-time-string "%Y-%m-%d %T (%Z)" (current-time)) "\n"
         " Last Update: \n"
         "           By: \n"
         " Description: \n"
         " **************************************************************************/\n"
         "#include<stdio.h>\n"
         "#include<string.h>\n")
        ((sh-mode . "Shell script") nil
         "#!/bin/bash\n"
         "# **************************************************************************\n"
         "# Copyright © " (substring (current-time-string) -4) " " (user-full-name) "\n"
         "# File Name: " (file-name-nondirectory buffer-file-name) "\n"
         "# Author: " (user-full-name)"\n"
         "# Email: " user-mail-address "\n"
         "# Created: " (format-time-string "%Y-%m-%d %T (%Z)" (current-time)) "\n"
         "# Last Update: \n"
         "#          By: \n"
         "# Description: \n"
         "# **************************************************************************/\n")))

提取文件头公共信息

之前的模板有很多重复的内容,写一个函数把公共的信息提取出来

  • 基础信息
    (defun maple//insert-string()
      (concat
       (make-string 80 ?*)
       "\n"
       "Copyright © " (substring (current-time-string) -4) " " (user-full-name) "\n"
       "File Name: " (file-name-nondirectory buffer-file-name) "\n"
       "Author: " (user-full-name)"\n"
       "Email: " user-mail-address "\n"
       "Created: " (format-time-string "%Y-%m-%d %T (%Z)" (current-time)) "\n"
       "Last Update: \n"
       "         By: \n"
       "Description: \n"
       (make-string 80 ?*)))
    
  • 插入注释信息

    <2018-03-16 五>更新:

    (defun maple/insert-string(&optional prefix)
      (replace-regexp-in-string
       "^" (or prefix comment-start)
       (maple//insert-string)))
    

    原代码:

    (defun maple/insert-string(&optional prefix)
      (or prefix (setq prefix comment-start))
      (mapconcat
       (lambda (x) (concat prefix x))
       (split-string (maple//insert-string) "\n") "\n"))
    
  • 更改模板
    (setq auto-insert-alist
          '(((ruby-mode . "Ruby program") nil
             "#!/usr/bin/env ruby\n"
             "# -*- encoding: utf-8 -*-\n"
             (maple/insert-string) "\n")
            ((python-mode . "Python program") nil
             "#!/usr/bin/env python\n"
             "# -*- coding: utf-8 -*-\n"
             (maple/insert-string) "\n")
            ((c-mode . "C program") nil
             "/*"
             (string-trim-left (maple/insert-string " ")) "*/\n"
             "#include<stdio.h>\n"
             "#include<string.h>\n")
            ((sh-mode . "Shell script") nil
             "#!/bin/bash\n"
             (maple/insert-string) "\n")
            ((go-mode . "Go program") nil
             "/*"
             (string-trim-left (maple/insert-string " ")) "*/\n")))
    

相关的配置可以在我的github上找到

Author: honmaple

NO CONTENTS

lin.jiang

风落花语风落天,花落风雨花落田.