插入代码模板

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
  1. (defvar insert-code-template-templates
  2. '(((c-mode c++-mode) ".h$" "/* copyright 2001 */ #ifndef __SOMETHING_H #define __SOMETHING_H #endif /* # __SOMETHING_H */ ")
  3. ((c-mode c++-mode) nil "int main(int argc, char **argv) { } ")
  4. ((cperl-mode perl-mode) nil "#!/usr/bin/perl -w use strict; "))
  5. "A list of triples, used for inserting code.
  6. A triplet is composed of a symbol for the major mode (or a list of symbols),
  7. a regular expression to match against the buffer's file name,
  8. and the text to insert when both the major mode and regular expression match.")
  9.  
  10. (defun insert-code-template ()
  11. "insert a code template, based on major mode
  12. when called interactively, always do insertion
  13. otherwise, only do so when the buffer is empty"
  14. (interactive)
  15. (let ((l insert-code-template-templates))
  16. (when (or (called-interactively-p)
  17. (eq (point-min) (point-max)))
  18. (while l
  19. (let* ((elt (car l))
  20. (modes (if (listp (car elt)) (car elt) (list (car elt))))
  21. (re (cadr elt))
  22. (template (caddr elt)))
  23. (when (and (member major-mode modes)
  24. (or (null re)
  25. (string-match re (buffer-file-name))))
  26. (insert template)
  27. (setq l nil)))
  28. (setq l (cdr l))))))
  29.  
  30. (add-hook 'find-file-hook 'insert-code-template)

以上是关于插入代码模板的主要内容,如果未能解决你的问题,请参考以下文章

VSCode自定义代码片段(vue主模板)

VSCode自定义代码片段2——.vue文件的模板

KDoc:插入代码片段

Eclipse 中的通用代码片段或模板

调用模板化成员函数:帮助我理解另一个 *** 帖子中的代码片段

代码片段使用复杂的 JavaScript 在 UIWebView 中插入 HTML?