| 1 | ;;; -*-emacs-lisp-*- |
| 2 | ;;; |
| 3 | ;;; $Id: skel.el.in,v 1.5 2004/04/08 01:36:28 mdw Exp $ |
| 4 | ;;; |
| 5 | ;;; Filling in skeletons |
| 6 | ;;; |
| 7 | ;;; (c) 1998 Mark Wooding |
| 8 | ;;; |
| 9 | |
| 10 | ;;;----- Licensing notice --------------------------------------------------- |
| 11 | ;;; |
| 12 | ;;; This program is free software; you can redistribute it and/or modify |
| 13 | ;;; it under the terms of the GNU General Public License as published by |
| 14 | ;;; the Free Software Foundation; either version 2 of the License, or |
| 15 | ;;; (at your option) any later version. |
| 16 | ;;; |
| 17 | ;;; This program is distributed in the hope that it will be useful, |
| 18 | ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | ;;; GNU General Public License for more details. |
| 21 | ;;; |
| 22 | ;;; You should have received a copy of the GNU General Public License |
| 23 | ;;; along with this program; if not, write to the Free Software Foundation, |
| 24 | ;;; Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | |
| 26 | ;;;----- Variables (largely tweakable) -------------------------------------- |
| 27 | |
| 28 | (defvar skel-directory-list '(".skel" "") |
| 29 | "*List of directory names which contain skeleton files.") |
| 30 | |
| 31 | (defvar skel-skeleton-path '("/etc/skel" "@skeldir@" "~/skel" "~/src/skel") |
| 32 | "*List of directories to search for skeleton data anyway.") |
| 33 | |
| 34 | (defvar skel-skelrc '(".skelrc" "skelrc") |
| 35 | "*File containing skeleton substitution data, looked up using the standard |
| 36 | skeleton search mathod.") |
| 37 | |
| 38 | (defvar skel-alist '() |
| 39 | "Alist of values to substitute into a skeleton. It is filled in by skelrc |
| 40 | files and user interaction. |
| 41 | |
| 42 | The alist's keys are symbols, interned from the placeholder strings in the |
| 43 | skeleton file. The values are forms to be evaluated. In the simplest case, |
| 44 | the form is a string added as a result of user interaction; however, it could |
| 45 | just as easily be a function call or something similarly complicated.") |
| 46 | |
| 47 | ;;;----- Finding skeleton files --------------------------------------------- |
| 48 | |
| 49 | (defun skel-do-join (acc jfun ll s) |
| 50 | "Recursive guts of skel-join." |
| 51 | (if ll |
| 52 | (if (car ll) |
| 53 | (skel-do-join (skel-do-join acc jfun (cdr ll) |
| 54 | (funcall jfun s (car (car ll)))) |
| 55 | jfun (cons (cdr (car ll)) (cdr ll)) s) |
| 56 | acc) |
| 57 | (cons s acc))) |
| 58 | |
| 59 | (defun skel-join (jfun base &rest ll) |
| 60 | "Return a list built from joining elements from the given lists in order, |
| 61 | left to right. JFUN is a function of two arguments which can join items |
| 62 | together. BASE is the initial item." |
| 63 | (nreverse (skel-do-join nil jfun ll base))) |
| 64 | |
| 65 | (defun skel-do-parents (dir acc) |
| 66 | "Tail recursive guts of skel-parents" |
| 67 | (setq acc (cons dir acc)) |
| 68 | (setq dir (substring dir 0 (string-match "/[^/]*/?$" dir))) |
| 69 | (if (string= dir "") |
| 70 | (cons "/" acc) |
| 71 | (skel-do-parents dir acc))) |
| 72 | |
| 73 | (defun skel-parents (dir) |
| 74 | "Returns a list of DIR, DIR's parent directory, etc., all the way up to the |
| 75 | root." |
| 76 | (setq dir (expand-file-name dir)) |
| 77 | (nreverse (skel-do-parents dir nil))) |
| 78 | |
| 79 | (defun skel-do-find (l all acc) |
| 80 | (if l |
| 81 | (let ((n (car l))) |
| 82 | (if (and (file-readable-p n) (file-regular-p n)) |
| 83 | (if all |
| 84 | (skel-do-find (cdr l) all (cons (abbreviate-file-name n) acc)) |
| 85 | (abbreviate-file-name n)) |
| 86 | (skel-do-find (cdr l) all acc))) |
| 87 | acc)) |
| 88 | |
| 89 | (defun skel-find-skeleton (name &optional all acc) |
| 90 | "Searches for skeleton files. NAME is the name of the file to find, or |
| 91 | a list of possible names. |
| 92 | |
| 93 | If ALL is nil, or omitted, return only the first matching filename |
| 94 | encountered. Otherwise, return a list of all matching names, most `global' |
| 95 | first. ACC is a base list to which the matching filenames are prepended." |
| 96 | |
| 97 | ;; --- Build one big list of all the possible names --- |
| 98 | |
| 99 | (let ((l (skel-join #'(lambda (x y) (if (string= y "") |
| 100 | x |
| 101 | (expand-file-name y x))) |
| 102 | nil |
| 103 | (append (skel-parents default-directory) |
| 104 | skel-skeleton-path) |
| 105 | skel-directory-list |
| 106 | (if (consp name) name (cons name nil))))) |
| 107 | |
| 108 | ;; --- Now filter out any which aren't interesting --- |
| 109 | |
| 110 | (skel-do-find l all acc))) |
| 111 | |
| 112 | ;;;----- Processing file skeletons ------------------------------------------ |
| 113 | |
| 114 | (defun skel-include (file) |
| 115 | "Includes the skeleton rc FILE." |
| 116 | (let ((rc (skel-find-skeleton file t))) |
| 117 | (while rc |
| 118 | (load (car rc) nil t t) |
| 119 | (setq rc (cdr rc))))) |
| 120 | |
| 121 | (defun skel-lookup (name) |
| 122 | |
| 123 | "Reads the value of symbol NAME in skel-alist. If there is no currrent |
| 124 | value, the user is prompted for one." |
| 125 | |
| 126 | ;; --- Resolve NAME into a symbol --- |
| 127 | |
| 128 | (if (stringp name) |
| 129 | (setq name (intern name))) |
| 130 | |
| 131 | ;; --- Look up the value --- |
| 132 | ;; |
| 133 | ;; Add it to the list if we've not seen it before. Protect ourselves |
| 134 | ;; against functions which do regexp matching. |
| 135 | |
| 136 | (let ((pair (assq name skel-alist)) |
| 137 | value) |
| 138 | (if pair |
| 139 | (save-match-data (setq value (eval (cdr pair)))) |
| 140 | (setq value (read-string (format "Value for %s: " name))) |
| 141 | (setq skel-alist (cons (cons name value) skel-alist))) |
| 142 | value)) |
| 143 | |
| 144 | (defun skel-do-fill-in () |
| 145 | "Does the actual donkey-work of filling in a file. For each fill-in area |
| 146 | in the current buffer, the function looks to see if the item in question has |
| 147 | been entered into ALIST: if so, it is replaced automatically; otherwise the |
| 148 | user is promted to enter a string to substitute into the buffer at this |
| 149 | point." |
| 150 | (if (re-search-forward "\\[\\[\\([^]]*\\)\\]\\]" nil t) |
| 151 | (progn |
| 152 | (replace-match (skel-lookup (match-string 1)) t t nil) |
| 153 | (goto-char (match-beginning 0)) |
| 154 | (skel-do-fill-in)))) |
| 155 | |
| 156 | ;;;----- Creating new files from skeletons ---------------------------------- |
| 157 | |
| 158 | (defun skel-do-create-file (name switch &optional skel) |
| 159 | |
| 160 | "Does the main work of creating a file based on a skeleton. The SWITCH |
| 161 | argument is called to display the buffer." |
| 162 | |
| 163 | ;; --- Some local variables --- |
| 164 | ;; |
| 165 | ;; This is a little bit of a hack, but do I look like someone who cares? |
| 166 | |
| 167 | (let (ext) |
| 168 | |
| 169 | ;; --- Find out if the file's there already --- |
| 170 | |
| 171 | (if (file-exists-p name) |
| 172 | (or (yes-or-no-p |
| 173 | (format "File %s already exists. Overwrite? " name)) |
| 174 | (error "Aborted!"))) |
| 175 | |
| 176 | ;; --- Fiddle with the filename --- |
| 177 | |
| 178 | (cond ((stringp skel) (let ((extind (string-match "\.[^.]*$" skel))) |
| 179 | (setq ext (and extind (substring skel extind))))) |
| 180 | (skel (progn |
| 181 | (setq ext (read-string "Extension: ")) |
| 182 | (or (string= ext "") (setq ext (concat "." ext))))) |
| 183 | (t (let ((extind (string-match "\.[^.]*$" name))) |
| 184 | (setq ext (and extind (substring name extind)))))) |
| 185 | (setq skel (concat "skeleton" (or ext ""))) |
| 186 | |
| 187 | ;; --- Find the skeleton filename --- |
| 188 | |
| 189 | (setq skel (or (skel-find-skeleton skel) |
| 190 | (skel-find-skeleton "skeleton") |
| 191 | (error "Couldn't find skeleton file %s" skel))) |
| 192 | |
| 193 | ;; --- Visit the file and destroy its contents --- |
| 194 | |
| 195 | (funcall switch (find-file-noselect name)) |
| 196 | (kill-region (point-min) (point-max)) |
| 197 | (insert-file skel) |
| 198 | |
| 199 | ;; --- Mangle the skeleton data in the file --- |
| 200 | |
| 201 | (make-local-variable 'skel-alist) |
| 202 | (setq skel-alist '()) |
| 203 | |
| 204 | ;; --- Read the default values to insert --- |
| 205 | |
| 206 | (let ((rc (append |
| 207 | (skel-find-skeleton skel-skelrc t) |
| 208 | (and ext |
| 209 | (skel-find-skeleton |
| 210 | (if (listp skel-skelrc) |
| 211 | (mapcar #'(lambda (x) (concat x ext)) skel-skelrc) |
| 212 | (concat skel-skelrc ext)) |
| 213 | t))))) |
| 214 | (while rc |
| 215 | (load (car rc) nil t t) |
| 216 | (setq rc (cdr rc)))) |
| 217 | |
| 218 | ;; --- Now do substitution --- |
| 219 | |
| 220 | (skel-do-fill-in) |
| 221 | (not-modified))) |
| 222 | |
| 223 | ;;;----- User commands ------------------------------------------------------ |
| 224 | |
| 225 | (defun skel-create-file (name &optional skel) |
| 226 | "Creates a new file called NAME and visits it. If SKEL is non-`nil', it is |
| 227 | the name of a skeleton file to insert and substitute. Otherwise the skeleton |
| 228 | file's name is derived from NAME by taking NAME's extension and appending it |
| 229 | to `skel'." |
| 230 | (interactive "FSkeleton create file: \nP") |
| 231 | (skel-do-create-file name 'switch-to-buffer skel)) |
| 232 | |
| 233 | (defun skel-create-file-other-window (name &optional skel) |
| 234 | "Like skel-create-file, but in another window." |
| 235 | (interactive "FSkeleton create file in other window: \nP") |
| 236 | (skel-do-create-file name 'switch-to-buffer-other-window skel)) |
| 237 | |
| 238 | (defun skel-create-file-other-frame (name &optional skel) |
| 239 | "Like skel-create-file, but in another frame." |
| 240 | (interactive "FSkeleton create file in other frame: \nP") |
| 241 | (skel-do-create-file name 'switch-to-buffer-other-frame skel)) |
| 242 | |
| 243 | ;;;----- Is that all there is? ---------------------------------------------- |
| 244 | |
| 245 | (provide 'skel) |