;; (C) Copyright Collin J. Doering 2014 ;; ;; This program is free software: you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by ;; the Free Software Foundation, either version 3 of the License, or ;; (at your option) any later version. ;; ;; This program is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU General Public License for more details. ;; ;; You should have received a copy of the GNU General Public License ;; along with this program. If not, see . ;; File: insert-templates.el ;; Author: Collin J. Doering ;; Date: Oct 22, 2014 ;; ;; Considering phasing this out in-place of yasnippet ;; ;; Enable autoinsert feature to automagically insert (require 'autoinsert) ;; Built-in (auto-insert-mode) ;;; Adds hook to find-files-hook (setq auto-insert-directory "~/.emacs.d/templates/") ;;; Or use custom, *NOTE* Trailing slash important (setq auto-insert-query nil) ;;; If you don't want to be prompted before insertion (setq autoinsert-tpl-author "Collin J. Doering") (setq autoinsert-tpl-email "collin.doering@rekahsoft.ca") ;; auto-insert options template and auto-completion (add-hook 'find-file-hooks 'auto-insert) (setq auto-insert-directory (concat (getenv "HOME") "/.emacs.d/templates/")) (setq auto-insert-alist '(("\\.c$" . ["c-template.c" auto-update-generic-template]) ("\\.cc\\|cpp$" . ["cpp-template.cc" auto-update-generic-template]) ("\\.php$" . ["php-template.php" auto-update-generic-template]) ("\\.rb$" . ["ruby-template.rb" auto-update-generic-template]) ("\\.lua$" . ["lua-template.lua" auto-update-generic-template]) ("\\.erl$" . ["erlang-template.erl" auto-update-generic-template]) ("\\.sh$" . ["shell-template.sh" auto-update-generic-template]) ("\\.rkt$" . ["racket-template.rkt" auto-update-generic-template]) ("\\.scm$" . ["scheme-template.scm" auto-update-generic-template]) ("\\.clj$" . ["clojure-template.clj" auto-update-generic-template]) ("\\.lisp$" . ["lisp-template.lisp" auto-update-generic-template]) ("\\.el$" . ["emacs-lisp-template.el" auto-update-generic-template]) ("\\.hs$" . ["haskell-template.hs" auto-update-generic-template]) ("\\.ml$" . ["ocaml-template.ml" auto-update-generic-template]) ("\\.sml$" . ["sml-template.sml" auto-update-generic-template]) ("\\.py$" . ["python-template.py" auto-update-generic-template]) ("\\.java$" . ["java-template.java" auto-update-generic-template]) ("\\.scala$" . ["scala-template.scala" auto-update-generic-template]) ("\\.htm\\|html$" . ["html-template.html" auto-update-generic-template]) ("\\.js$" . ["java-script-template.js" auto-update-generic-template]) ("\\.css$" . ["css-template.css" auto-update-generic-template]) ("\\.scss$" . ["scss-template.scss" auto-update-generic-template]) ("\\.sass$" . ["sass-template.sass" auto-update-generic-template]) ("\\.haml$" . ["haml-template.haml" auto-update-generic-template]) ("\\.markdown$" . ["markdown-template.markdown" auto-update-generic-template]) ("\\.tex$" . ["latex-template.tex" auto-update-generic-template]))) (setq auto-insert 'other) ;; TODO: remove interactive-ness from auto-update-generic-template as it's not needed ;; and there only as a workaround. Python and PHP templates are not filled for ;; some unknown reason. (defun auto-update-generic-template () (interactive) (save-excursion ;; Replace @!@FILENAME@!@ with file name sans suffix (while (search-forward "@!@FILENAME@!@" nil t) (save-restriction (narrow-to-region (match-beginning 0) (match-end 0)) (replace-match (file-name-sans-extension (file-name-nondirectory buffer-file-name)) t)))) (save-excursion ;; Replace @!@FILE@!@ with file name (while (search-forward "@!@FILE@!@" nil t) (save-restriction (narrow-to-region (match-beginning 0) (match-end 0)) (replace-match (file-name-nondirectory buffer-file-name) t)))) (save-excursion ;; replace @!@DATE@!@ with today's date (while (search-forward "@!@DATE@!@" nil t) (save-restriction (narrow-to-region (match-beginning 0) (match-end 0)) (replace-match "") (insert-date)))) (save-excursion ;; Replace @!@YEAR@!@ with the current year (while (search-forward "@!@YEAR@!@" nil t) (save-restriction (narrow-to-region (match-beginning 0) (match-end 0)) (replace-match (format-time-string "%Y" (current-time)))))) (save-excursion ;; Replace @!@AUTHOR@!@ with the current year (while (search-forward "@!@AUTHOR@!@" nil t) (save-restriction (narrow-to-region (match-beginning 0) (match-end 0)) (replace-match "") (insert-author)))) (save-excursion ;; Replace @!@EMAIL@!@ with the current year (while (search-forward "@!@EMAIL@!@" nil t) (save-restriction (narrow-to-region (match-beginning 0) (match-end 0)) (replace-match "") (insert-author-email))))) ;; Insert current date at cursor in the currently active buffer (defun insert-date () "Insert today's date into buffer" (interactive) (insert (format-time-string "%b %e, %Y" (current-time)))) (defun insert-author () "Insert author name at point" (interactive) (insert autoinsert-tpl-author)) (defun insert-author-email () "Insert author email at point" (interactive) (insert autoinsert-tpl-email))