insert-code-template


/ Published in: Emacs Lisp
Save to your folder(s)

An example solution for the 'insert-code-template exercise listed here: http://stackoverflow.com/questions/41522/tips-for-learning-elisp/59589#59589


Copy this code and paste it in your HTML
  1. (defvar insert-code-template-templates
  2. '(((c-mode c++-mode) ".h$" "/* copyright 2001 */\n#ifndef __SOMETHING_H\n#define __SOMETHING_H\n\n#endif /* # __SOMETHING_H */\n")
  3. ((c-mode c++-mode) nil "int\nmain(int argc, char **argv)\n{\n\n}\n")
  4. ((cperl-mode perl-mode) nil "#!/usr/bin/perl -w\n\nuse strict;\n"))
  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)

URL: http://stackoverflow.com/questions/41522/tips-for-learning-elisp/59589#59589

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.