插入代码模板
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了插入代码模板相关的知识,希望对你有一定的参考价值。
An example solution for the 'insert-code-template exercise listed here: http://stackoverflow.com/questions/41522/tips-for-learning-elisp/59589#59589
(defvar insert-code-template-templates '(((c-mode c++-mode) ".h$" "/* copyright 2001 */ #ifndef __SOMETHING_H #define __SOMETHING_H #endif /* # __SOMETHING_H */ ") ((c-mode c++-mode) nil "int main(int argc, char **argv) { } ") ((cperl-mode perl-mode) nil "#!/usr/bin/perl -w use strict; ")) "A list of triples, used for inserting code. A triplet is composed of a symbol for the major mode (or a list of symbols), a regular expression to match against the buffer's file name, and the text to insert when both the major mode and regular expression match.") (defun insert-code-template () "insert a code template, based on major mode when called interactively, always do insertion otherwise, only do so when the buffer is empty" (interactive) (let ((l insert-code-template-templates)) (when (or (called-interactively-p) (eq (point-min) (point-max))) (while l (let* ((elt (car l)) (modes (if (listp (car elt)) (car elt) (list (car elt)))) (re (cadr elt)) (template (caddr elt))) (when (and (member major-mode modes) (or (null re) (string-match re (buffer-file-name)))) (insert template) (setq l nil))) (setq l (cdr l)))))) (add-hook 'find-file-hook 'insert-code-template)
以上是关于插入代码模板的主要内容,如果未能解决你的问题,请参考以下文章