skelrc: New function to insert flowed paragraphs.
[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-prefix-and-reflow (prefix text)
99 "Return LINES, reflowed to `fill-column', with PREFIX at the start."
100 (let ((expanded-prefix (skelrc-expand-text prefix))
101 (expanded-text (skelrc-expand-text text))
102 (col (string-to-number (skel-lookup 'fill-column))))
103 (with-temp-buffer
104 (let* ((fill-prefix expanded-prefix)
105 (paragraph-start (concat (skelrc-strip-trailing-whitespace
106 (regexp-quote expanded-prefix))
107 "[ \t\f]*$"))
108 (paragraph-separate paragraph-start)
109 (fill-column col))
110
111 (insert (skelrc-prefix-lines expanded-prefix expanded-text))
112
113 (goto-char (point-min))
114 (while (< (point) (point-max))
115 (fill-paragraph)
116 (forward-paragraph))
117
118 (buffer-string)))))
119
120 (defun skelrc-component ()
121 "Return a suitable a `This file is part of ...' line."
122 (if (assq 'full-title skel-alist)
123 "[[cont-comment]] This file is part of [[full-title]].
124 \[[cont-comment]]\n"
125 ""))
126
127 (setq skelrc-gpl (skelrc-prefix-lines "[[cont-comment]] "
128 "[[Program-name]] is free software; you can redistribute it and/or modify
129 it under the terms of the GNU General Public License as published by
130 the Free Software Foundation; either version 2 of the License, or
131 \(at your option) any later version.
132
133 \[[Program-name]] is distributed in the hope that it will be useful,
134 but WITHOUT ANY WARRANTY; without even the implied warranty of
135 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
136 GNU General Public License for more details.
137
138 You should have received a copy of the GNU General Public License
139 along with [[program-name]]; if not, write to the Free Software Foundation,
140 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA."))
141
142 (setq skelrc-wide-gpl (skelrc-prefix-lines "[[cont-comment]] "
143 ;;Version for wide program names
144 "[[Program-name]] is free software; you can redistribute it and/or modify
145 it under the terms of the GNU General Public License as published by
146 the Free Software Foundation; either version 2 of the License, or
147 \(at your option) any later version.
148
149 \[[Program-name]] is distributed in the hope that it will be useful,
150 but WITHOUT ANY WARRANTY; without even the implied warranty of
151 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
152 GNU General Public License for more details.
153
154 You should have received a copy of the GNU General Public License
155 along with [[program-name]]; if not, write to the Free
156 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
157 MA 02111-1307, USA."))
158
159 (setq skelrc-lgpl (skelrc-prefix-lines "[[cont-comment]] "
160 "[[Library-name]] is free software; you can redistribute it and/or modify
161 it under the terms of the GNU Library General Public License as
162 published by the Free Software Foundation; either version 2 of the
163 License, or (at your option) any later version.
164
165 \[[Library-name]] is distributed in the hope that it will be useful,
166 but WITHOUT ANY WARRANTY; without even the implied warranty of
167 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
168 GNU Library General Public License for more details.
169
170 You should have received a copy of the GNU Library General Public
171 License along with [[library-name]]; if not, write to the Free
172 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
173 MA 02111-1307, USA."))
174
175 (setq skelrc-agpl (skelrc-prefix-lines "[[cont-comment]] "
176 "[[Program-name]] is free software; you can redistribute it and/or modify
177 it under the terms of the GNU Affero General Public License as
178 published by the Free Software Foundation; either version 3 of the
179 License, or (at your option) any later version.
180
181 \[[Program-name]] is distributed in the hope that it will be useful,
182 but WITHOUT ANY WARRANTY; without even the implied warranty of
183 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
184 GNU Affero General Public License for more details.
185
186 You should have received a copy of the GNU Affero General Public
187 License along with [[program-name]]; if not, see
188 <http://www.gnu.org/licenses/>."))
189
190 (setq skelrc-bsd (skelrc-prefix-lines "[[cont-comment]] "
191 "Copyright (c) [[year]] [[author]]
192 All rights reserved.
193
194 Redistribution and use in source and binary forms, with or without
195 modification, are permitted provided that the following conditions are
196 met:
197
198 1. Redistributions of source code must retain the above copyright
199 notice, this list of conditions and the following disclaimer.
200
201 2, Redistributions in binary form must reproduce the above copyright
202 notice, this list of conditions and the following disclaimer in the
203 documentation and/or other materials provided with the distribution.
204
205 3. The name of the authors may not be used to endorse or promote
206 products derived from this software without specific prior written
207 permission.
208
209 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
210 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
211 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
212 NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
213 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
214 \(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
215 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
216 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
217 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
218 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
219 POSSIBILITY OF SUCH DAMAGE.
220
221 Instead of accepting the above terms, you may redistribute and/or modify
222 this software under the terms of either the GNU General Public License,
223 or the GNU Library General Public License, published by the Free
224 Software Foundation; either version 2 of the License, or (at your
225 option) any later version."))
226
227 (defun skel-basename ()
228 (file-name-sans-extension (file-name-nondirectory buffer-file-name)))
229
230 (setq skel-alist
231 (append
232 `((first-line . "[[new-comment]] -*-[[emacs-mode]]-*-")
233 (emacs-mode . (skelrc-decode-major-mode))
234 (year . (substring (current-time-string) 20 24))
235 (header . "[[licence]][[preamble]]")
236 (basename . (skel-basename))
237 (licence . ,(concat "[[licence-banner]]\n"
238 "[[cont-comment]]\n"
239 "[[component]][[licence-text]]"
240 "[[block-end]]\n\n"))
241 (licence-banner . (skelrc-banner "Licensing notice" t))
242 (component . (skelrc-component))
243 (licence-text . "[[gpl]]")
244 (Program-name . (or (skelrc-assq 'Program skel-alist)
245 (skelrc-assq 'program skel-alist)
246 "This program"))
247 (program-name . (or (skelrc-assq 'program skel-alist)
248 "this program"))
249 (Library-name . (or (skelrc-assq 'Library skel-alist)
250 (skelrc-assq 'library skel-alist)
251 (skelrc-assq 'Program skel-alist)
252 (skelrc-assq 'program skel-alist)
253 "This library"))
254 (library-name . (or (skelrc-assq 'library skel-alist)
255 (skelrc-assq 'program skel-alist)
256 "this library"))
257 (gpl . skelrc-gpl)
258 (wide-gpl . skelrc-wide-gpl)
259 (lgpl . skelrc-lgpl)
260 (agpl . skelrc-agpl)
261 (bsd . skelrc-bsd)
262 (revisions . ,(concat "[[revision-banner]]\n"
263 "[[cont-comment]]\n"
264 "[[cont-comment]] $Log$"
265 "[[block-end]]\n\n"))
266 (revision-banner . (skelrc-banner "Revision history" t))
267 (preamble . "")
268 (trailer . "[[tag-line]]\n[[postamble]]")
269 (postamble . "")
270 (tag-line . (skelrc-banner "That's all, folks"))
271 (block-start . (skel-lookup 'new-comment))
272 (block-banner-knob . "")
273 (block-banner-end . "")
274 (block-end . ""))
275 skel-alist))