Source

emacsd / init-auto-insert.el

Full commit
;;; auto-insert stuff
(add-hook 'find-file-hooks 'auto-insert)
(setq auto-insert-directory (concat (getenv "HOME") "/.emacs.d/insert/"))
(setq auto-insert 'other)
(setq auto-insert-query t)


(setq auto-insert-alist
      '(("\\.sh$"  . ["insert . sh" alexott/auto-update-defaults])
        ("\\.el$"  . ["insert.el" alexott/auto-update-defaults])
        ("\\.pl$"  . ["insert.pl" alexott/auto-update-defaults])
        ("\\.ncl$" . ["insert.ncl" alexott/auto-update-defaults])
        ("\\.py$"  . ["insert.py" alexott/auto-update-defaults])
        ("\\.py3$" . ["insert.py3" alexott/auto-update-defaults])
        ))


(defun alexott/auto-replace-header-name ()
  (save-excursion
    (while (search-forward "###" nil t)
      (save-restriction
        (narrow-to-region (match-beginning 0) (match-end 0))
        (replace-match (upcase (file-name-nondirectory buffer-file-name)))
        (subst-char-in-region (point-min) (point-max) ?. ?_)
        (subst-char-in-region (point-min) (point-max) ?- ?_)
        ))
    )
  )

(defun alexott/auto-replace-file-name ()
  (save-excursion
    ;; Replace @@@ 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)
        ))
    )
  )

(defun alexott/auto-replace-file-name-no-ext ()
  (save-excursion
    ;; Replace @@@ with file name
    (while (search-forward "(>>FILE_NO_EXT<<)" 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)
        ))
    )
  )

(defun alexott/insert-today ()
  "Insert today's date into buffer"
  (interactive)
  (insert (format-time-string "%A, %B %e %Y" (current-time))))

(defun alexott/auto-replace-date-time ()
  (save-excursion
    ;; replace DDDD with today's date
    (while (search-forward "(>>DATE<<)" nil t)
      (save-restriction
        (narrow-to-region (match-beginning 0) (match-end 0))
        (replace-match "" t)
        (alexott/insert-today)
        ))))

(defun alexott/auto-update-header-file ()
  (alexott/auto-replace-header-name)
  (alexott/auto-replace-file-name))

(defun alexott/auto-update-c-source-file ()
  (save-excursion
    ;; Replace HHHH with file name sans suffix
    (while (search-forward "HHHH" nil t)
      (save-restriction
        (narrow-to-region (match-beginning 0) (match-end 0))
        (replace-match (concat (file-name-sans-extension (file-name-nondirectory buffer-file-name)) ".h") t))))
  (alexott/auto-replace-file-name)
  (alexott/auto-replace-date-time))

(defun alexott/auto-update-defaults ()
  (alexott/auto-replace-file-name)
  (alexott/auto-replace-file-name-no-ext)
  (alexott/auto-replace-date-time))