third commit with a revised write-template
[myelisptry.git] / newsnippet.el
blob9e06e6f352d7daabf985ce02f34903adcc8813ea
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 '())))
20 (while the-list
21 (if (search-forward (car (car the-list)) (point-max) t)
22 (progn
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)
35 (progn
36 (setq end (point))
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))))
47 (defun get-template()
48 (interactive)
49 (let (beg end
50 (file (concat TEMPLATE-DIR
51 (completing-read "from:"
52 (directory-files TEMPLATE-DIR)))) match)
53 (progn
54 (insert-file-contents
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()
62 ;;deprecated
63 (interactive)
64 (condition-case err
65 (progn
66 (kill-ring-save (mark) (point))
67 (find-file
68 (concat TEMPLATE-DIR
69 (completing-read "To:"
70 (directory-files TEMPLATE-DIR )) "/"
71 (completing-read "what: " '()) ".tpl" ))
72 (if (= (point-max) 1)
73 (yank)
74 (progn
75 (kill-buffer (current-buffer))
76 (error "Template by the same name already exists.")
78 (save-buffer))
79 (mark-inactive ; condition
80 (message "%s" (error-message-string err)))))
84 (defun write-template()
85 (interactive)
86 (condition-case err
87 (let (where what)
88 (progn
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)))
94 (find-file
95 (concat TEMPLATE-DIR where "/" what ".tpl"))
96 (if (= (point-max) 1)
97 (yank)
98 (progn
99 (kill-buffer (current-buffer))
100 (error "Template by the same name already exists.")
102 (save-buffer)))
103 (mark-inactive
104 (message "%s" (error-message-string err)))))