skelrc: Shiny new function to return an expanded string.
[skel] / skelrc
1 ;;; -*-emacs-lisp-*-
2
3 (defun skelrc-dribble (msg &rest args)
4 "Write MSG (a `format'-style string, with ARGS) to the debug buffer."
5 (let ((buffer (get-buffer "*skel-debug*")))
6 (and buffer
7 (with-current-buffer buffer
8 (goto-char (point-max))
9 (insert (apply #'format msg args) "\n")))))
10
11 (defun skelrc-strip-trailing-whitespace (string)
12 "Return STRING, but with trailing whitespace removed.
13
14 Whitespace characters are those with space syntax."
15 (let ((i (1- (length string))))
16 (while (and (>= i 0) (= (char-syntax (aref string i)) ? ))
17 (setq i (1- i)))
18 (substring string 0 (1+ i))))
19
20 (defun skelrc-banner (title &optional block)
21 "Return a comment banner with the given TITLE, and maybe a BLOCK of text."
22 (let* ((start (skel-lookup 'block-start))
23 (end (skel-lookup (if block 'block-banner-knob 'block-banner-end)))
24 (barlen (- 77 (length (concat start end " ----- " title)))))
25 (skelrc-strip-trailing-whitespace (concat start
26 "----- "
27 title
28 " "
29 (make-string barlen ?-)
30 end))))
31
32 (defvar skelrc-forced-major-mode 0
33 "The priority of the currently forced major mode")
34
35 (defun skelrc-force-mode (new-mode &optional priority)
36 "Force the use of major mode NEW-MODE.
37
38 If the PRIORITY (defaults to 1) is strictly greater than
39 `skelrc-forced-major-mode' then the NEW-MODE takes precedence. A `skelrc'
40 file which wants to delegate settings to another file should therefore force
41 its chosen major-mode before calling `skel-include'."
42 (or priority (setq priority 1))
43 (if (> priority skelrc-forced-major-mode)
44 (progn
45 (or (eq new-mode major-mode)
46 (let ((old-skel-alist skel-alist))
47 (funcall new-mode)
48 (make-variable-buffer-local 'skel-alist)
49 (setq skel-alist old-skel-alist)))
50 (make-variable-buffer-local 'skelrc-forced-major-mode)
51 (setq skelrc-forced-major-mode priority))))
52
53 (defun skelrc-decode-major-mode ()
54 "Return the mode dropping to put in a local-variables line."
55 (let* ((name (symbol-name major-mode))
56 (endind (string-match "-mode$" name)))
57 (if endind (substring name 0 endind)
58 (name))))
59
60 (defun skelrc-assq (key alist)
61 "Pick out the value associated with KEY in ALIST (rather than the cons)."
62 (let ((val (assq key alist)))
63 (and val (cdr val))))
64
65 (defun skelrc-expand-text (text)
66 "Return the result of expanding TEXT in the current context.
67
68 The context is extended with any new associations formed during
69 the expansion."
70 (let* ((alist skel-alist)
71 (expanded (with-temp-buffer
72 (let ((skel-alist alist))
73 (insert text)
74 (goto-char (point-min))
75 (skelrc-dribble "before: alist = `%s'" skel-alist)
76 (skel-do-fill-in)
77 (setq alist skel-alist)
78 (buffer-string)))))
79 (setq skel-alist alist)
80 expanded))
81
82 (defun skelrc-prefix-lines (prefix lines)
83 "Return LINES, each with PREFIX prepended to the beginning.
84
85 If the LINES end with a newline character, there is not considered to be a
86 final empty line. When prepended to an empty line, trailing whitespace in
87 the PREFIX is removed."
88 (with-temp-buffer
89 (insert lines)
90 (goto-char (point-min))
91 (while (< (point) (point-max))
92 (insert prefix)
93 (and (looking-at "\n")
94 (delete-horizontal-space t))
95 (forward-line))
96 (buffer-string)))
97
98 (defun skelrc-component ()
99 "Return a suitable a `This file is part of ...' line."
100 (if (assq 'full-title skel-alist)
101 "[[cont-comment]] This file is part of [[full-title]].
102 \[[cont-comment]]\n"
103 ""))
104
105 (setq skelrc-gpl (skelrc-prefix-lines "[[cont-comment]] "
106 "[[Program-name]] is free software; you can redistribute it and/or modify
107 it under the terms of the GNU General Public License as published by
108 the Free Software Foundation; either version 2 of the License, or
109 \(at your option) any later version.
110
111 \[[Program-name]] is distributed in the hope that it will be useful,
112 but WITHOUT ANY WARRANTY; without even the implied warranty of
113 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
114 GNU General Public License for more details.
115
116 You should have received a copy of the GNU General Public License
117 along with [[program-name]]; if not, write to the Free Software Foundation,
118 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA."))
119
120 (setq skelrc-wide-gpl (skelrc-prefix-lines "[[cont-comment]] "
121 ;;Version for wide program names
122 "[[Program-name]] is free software; you can redistribute it and/or modify
123 it under the terms of the GNU General Public License as published by
124 the Free Software Foundation; either version 2 of the License, or
125 \(at your option) any later version.
126
127 \[[Program-name]] is distributed in the hope that it will be useful,
128 but WITHOUT ANY WARRANTY; without even the implied warranty of
129 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
130 GNU General Public License for more details.
131
132 You should have received a copy of the GNU General Public License
133 along with [[program-name]]; if not, write to the Free
134 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
135 MA 02111-1307, USA."))
136
137 (setq skelrc-lgpl (skelrc-prefix-lines "[[cont-comment]] "
138 "[[Library-name]] is free software; you can redistribute it and/or modify
139 it under the terms of the GNU Library General Public License as
140 published by the Free Software Foundation; either version 2 of the
141 License, or (at your option) any later version.
142
143 \[[Library-name]] is distributed in the hope that it will be useful,
144 but WITHOUT ANY WARRANTY; without even the implied warranty of
145 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
146 GNU Library General Public License for more details.
147
148 You should have received a copy of the GNU Library General Public
149 License along with [[library-name]]; if not, write to the Free
150 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
151 MA 02111-1307, USA."))
152
153 (setq skelrc-agpl (skelrc-prefix-lines "[[cont-comment]] "
154 "[[Program-name]] is free software; you can redistribute it and/or modify
155 it under the terms of the GNU Affero General Public License as
156 published by the Free Software Foundation; either version 3 of the
157 License, or (at your option) any later version.
158
159 \[[Program-name]] is distributed in the hope that it will be useful,
160 but WITHOUT ANY WARRANTY; without even the implied warranty of
161 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
162 GNU Affero General Public License for more details.
163
164 You should have received a copy of the GNU Affero General Public
165 License along with [[program-name]]; if not, see
166 <http://www.gnu.org/licenses/>."))
167
168 (setq skelrc-bsd (skelrc-prefix-lines "[[cont-comment]] "
169 "Copyright (c) [[year]] [[author]]
170 All rights reserved.
171
172 Redistribution and use in source and binary forms, with or without
173 modification, are permitted provided that the following conditions are
174 met:
175
176 1. Redistributions of source code must retain the above copyright
177 notice, this list of conditions and the following disclaimer.
178
179 2, Redistributions in binary form must reproduce the above copyright
180 notice, this list of conditions and the following disclaimer in the
181 documentation and/or other materials provided with the distribution.
182
183 3. The name of the authors may not be used to endorse or promote
184 products derived from this software without specific prior written
185 permission.
186
187 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
188 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
189 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
190 NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
191 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
192 \(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
193 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
194 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
195 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
196 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
197 POSSIBILITY OF SUCH DAMAGE.
198
199 Instead of accepting the above terms, you may redistribute and/or modify
200 this software under the terms of either the GNU General Public License,
201 or the GNU Library General Public License, published by the Free
202 Software Foundation; either version 2 of the License, or (at your
203 option) any later version."))
204
205 (defun skel-basename ()
206 (file-name-sans-extension (file-name-nondirectory buffer-file-name)))
207
208 (setq skel-alist
209 (append
210 `((first-line . "[[new-comment]] -*-[[emacs-mode]]-*-")
211 (emacs-mode . (skelrc-decode-major-mode))
212 (year . (substring (current-time-string) 20 24))
213 (header . "[[licence]][[preamble]]")
214 (basename . (skel-basename))
215 (licence . ,(concat "[[licence-banner]]\n"
216 "[[cont-comment]]\n"
217 "[[component]][[licence-text]]"
218 "[[block-end]]\n\n"))
219 (licence-banner . (skelrc-banner "Licensing notice" t))
220 (component . (skelrc-component))
221 (licence-text . "[[gpl]]")
222 (Program-name . (or (skelrc-assq 'Program skel-alist)
223 (skelrc-assq 'program skel-alist)
224 "This program"))
225 (program-name . (or (skelrc-assq 'program skel-alist)
226 "this program"))
227 (Library-name . (or (skelrc-assq 'Library skel-alist)
228 (skelrc-assq 'library skel-alist)
229 (skelrc-assq 'Program skel-alist)
230 (skelrc-assq 'program skel-alist)
231 "This library"))
232 (library-name . (or (skelrc-assq 'library skel-alist)
233 (skelrc-assq 'program skel-alist)
234 "this library"))
235 (gpl . skelrc-gpl)
236 (wide-gpl . skelrc-wide-gpl)
237 (lgpl . skelrc-lgpl)
238 (agpl . skelrc-agpl)
239 (bsd . skelrc-bsd)
240 (revisions . ,(concat "[[revision-banner]]\n"
241 "[[cont-comment]]\n"
242 "[[cont-comment]] $Log$"
243 "[[block-end]]\n\n"))
244 (revision-banner . (skelrc-banner "Revision history" t))
245 (preamble . "")
246 (trailer . "[[tag-line]]\n[[postamble]]")
247 (postamble . "")
248 (tag-line . (skelrc-banner "That's all, folks"))
249 (block-start . (skel-lookup 'new-comment))
250 (block-banner-knob . "")
251 (block-banner-end . "")
252 (block-end . ""))
253 skel-alist))