1 (defvar TEMPLATE-DIR
"/home/ggmon/emacs/NEWTEMPLATES/")
2 (defun get-from-user (prompt)
3 ;; get the value from user
4 (completing-read (concat prompt
":")' ()))
7 (defvar my-common-replacement-alist
8 ;;common replacement list
9 '(("%filename%" .
(lambda() (file-name-nondirectory (buffer-file-name))))
10 ("%creator%" . user-full-name
)
11 ("%author%" . user-full-name
)
12 ("%date%". current-time-string
)
16 (defun template-replace-holders ()
17 ;; replace all the templates
18 (goto-char (point-min))
19 (let ((the-list (append my-common-replacement-alist
'())))
21 (if (search-forward (car (car the-list
)) (point-max) t
)
23 (goto-char (point-min))
24 (replace-string (car (car the-list
))
25 (funcall (cdr (car the-list
))) nil
)
26 (setq the-list
(cdr the-list
)))
27 (setq the-list
(cdr the-list
))))))
30 (defun replace-potential-holders()
31 ;; find and replace all the templates from the template
32 (goto-char (point-min))
33 (let (start end potential-holder
)
34 (while (search-forward-regexp "%.*%" (point-max) t
)
37 (setq start
(search-backward-regexp "%.*%" (point-min) t
))
38 (goto-char (point-min))
39 (setq potential-holder
(buffer-substring start end
))
40 (replace-holder potential-holder
)))))
43 (defun replace-holder (potential-holder)
44 (replace-string potential-holder
(get-from-user (substring potential-holder
1 -
1))))
50 (file (concat TEMPLATE-DIR
51 (completing-read "from:"
52 (directory-files TEMPLATE-DIR
)))) match
)
55 (concat file
"/" (completing-read "what:"(mapcar (lambda (str) (list (substring str
0 -
4)))
56 (directory-files (concat file
"/" ) nil
"\\.tpl$"))) ".tpl" ))
57 (template-replace-holders )
58 (replace-potential-holders))))
61 (defun write-template-old()
66 (kill-ring-save (mark) (point))
69 (completing-read "To:"
70 (directory-files TEMPLATE-DIR
)) "/"
71 (completing-read "what: " '()) ".tpl" ))
75 (kill-buffer (current-buffer))
76 (error "Template by the same name already exists.")
79 (mark-inactive ; condition
80 (message "%s" (error-message-string err
)))))
84 (defun write-template()
89 (kill-ring-save (mark) (point))
90 (setq where
(completing-read "To:" (directory-files TEMPLATE-DIR
)))
91 (setq what
(completing-read "what:" '()))
92 (if (not (file-directory-p (concat TEMPLATE-DIR where
)))
93 (make-directory (concat TEMPLATE-DIR where
)))
95 (concat TEMPLATE-DIR where
"/" what
".tpl"))
99 (kill-buffer (current-buffer))
100 (error "Template by the same name already exists.")
104 (message "%s" (error-message-string err
)))))