skelrc: Add a debugging utility.
[skel] / skelrc
... / ...
CommitLineData
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
14Whitespace 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
38If the PRIORITY (defaults to 1) is strictly greater than
39`skelrc-forced-major-mode' then the NEW-MODE takes precedence. A `skelrc'
40file which wants to delegate settings to another file should therefore force
41its 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-prefix-lines (prefix lines)
66 "Return LINES, each with PREFIX prepended to the beginning.
67
68If the LINES end with a newline character, there is not considered to be a
69final empty line. When prepended to an empty line, trailing whitespace in
70the PREFIX is removed."
71 (with-temp-buffer
72 (insert lines)
73 (goto-char (point-min))
74 (while (< (point) (point-max))
75 (insert prefix)
76 (and (looking-at "\n")
77 (delete-horizontal-space t))
78 (forward-line))
79 (buffer-string)))
80
81(defun skelrc-component ()
82 "Return a suitable a `This file is part of ...' line."
83 (if (assq 'full-title skel-alist)
84"[[cont-comment]] This file is part of [[full-title]].
85\[[cont-comment]]\n"
86 ""))
87
88(setq skelrc-gpl (skelrc-prefix-lines "[[cont-comment]] "
89"[[Program-name]] is free software; you can redistribute it and/or modify
90it under the terms of the GNU General Public License as published by
91the Free Software Foundation; either version 2 of the License, or
92\(at your option) any later version.
93
94\[[Program-name]] is distributed in the hope that it will be useful,
95but WITHOUT ANY WARRANTY; without even the implied warranty of
96MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
97GNU General Public License for more details.
98
99You should have received a copy of the GNU General Public License
100along with [[program-name]]; if not, write to the Free Software Foundation,
101Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA."))
102
103(setq skelrc-wide-gpl (skelrc-prefix-lines "[[cont-comment]] "
104;;Version for wide program names
105"[[Program-name]] is free software; you can redistribute it and/or modify
106it under the terms of the GNU General Public License as published by
107the Free Software Foundation; either version 2 of the License, or
108\(at your option) any later version.
109
110\[[Program-name]] is distributed in the hope that it will be useful,
111but WITHOUT ANY WARRANTY; without even the implied warranty of
112MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
113GNU General Public License for more details.
114
115You should have received a copy of the GNU General Public License
116along with [[program-name]]; if not, write to the Free
117Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
118MA 02111-1307, USA."))
119
120(setq skelrc-lgpl (skelrc-prefix-lines "[[cont-comment]] "
121"[[Library-name]] is free software; you can redistribute it and/or modify
122it under the terms of the GNU Library General Public License as
123published by the Free Software Foundation; either version 2 of the
124License, or (at your option) any later version.
125
126\[[Library-name]] is distributed in the hope that it will be useful,
127but WITHOUT ANY WARRANTY; without even the implied warranty of
128MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
129GNU Library General Public License for more details.
130
131You should have received a copy of the GNU Library General Public
132License along with [[library-name]]; if not, write to the Free
133Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
134MA 02111-1307, USA."))
135
136(setq skelrc-agpl (skelrc-prefix-lines "[[cont-comment]] "
137"[[Program-name]] is free software; you can redistribute it and/or modify
138it under the terms of the GNU Affero General Public License as
139published by the Free Software Foundation; either version 3 of the
140License, or (at your option) any later version.
141
142\[[Program-name]] is distributed in the hope that it will be useful,
143but WITHOUT ANY WARRANTY; without even the implied warranty of
144MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
145GNU Affero General Public License for more details.
146
147You should have received a copy of the GNU Affero General Public
148License along with [[program-name]]; if not, see
149<http://www.gnu.org/licenses/>."))
150
151(setq skelrc-bsd (skelrc-prefix-lines "[[cont-comment]] "
152"Copyright (c) [[year]] [[author]]
153All rights reserved.
154
155Redistribution and use in source and binary forms, with or without
156modification, are permitted provided that the following conditions are
157met:
158
1591. Redistributions of source code must retain the above copyright
160 notice, this list of conditions and the following disclaimer.
161
1622, Redistributions in binary form must reproduce the above copyright
163 notice, this list of conditions and the following disclaimer in the
164 documentation and/or other materials provided with the distribution.
165
1663. The name of the authors may not be used to endorse or promote
167 products derived from this software without specific prior written
168 permission.
169
170THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
171WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
172MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
173NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
174INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
175\(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
176SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
177HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
178STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
179ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
180POSSIBILITY OF SUCH DAMAGE.
181
182Instead of accepting the above terms, you may redistribute and/or modify
183this software under the terms of either the GNU General Public License,
184or the GNU Library General Public License, published by the Free
185Software Foundation; either version 2 of the License, or (at your
186option) any later version."))
187
188(defun skel-basename ()
189 (file-name-sans-extension (file-name-nondirectory buffer-file-name)))
190
191(setq skel-alist
192 (append
193 `((first-line . "[[new-comment]] -*-[[emacs-mode]]-*-")
194 (emacs-mode . (skelrc-decode-major-mode))
195 (year . (substring (current-time-string) 20 24))
196 (header . "[[licence]][[preamble]]")
197 (basename . (skel-basename))
198 (licence . ,(concat "[[licence-banner]]\n"
199 "[[cont-comment]]\n"
200 "[[component]][[licence-text]]"
201 "[[block-end]]\n\n"))
202 (licence-banner . (skelrc-banner "Licensing notice" t))
203 (component . (skelrc-component))
204 (licence-text . "[[gpl]]")
205 (Program-name . (or (skelrc-assq 'Program skel-alist)
206 (skelrc-assq 'program skel-alist)
207 "This program"))
208 (program-name . (or (skelrc-assq 'program skel-alist)
209 "this program"))
210 (Library-name . (or (skelrc-assq 'Library skel-alist)
211 (skelrc-assq 'library skel-alist)
212 (skelrc-assq 'Program skel-alist)
213 (skelrc-assq 'program skel-alist)
214 "This library"))
215 (library-name . (or (skelrc-assq 'library skel-alist)
216 (skelrc-assq 'program skel-alist)
217 "this library"))
218 (gpl . skelrc-gpl)
219 (wide-gpl . skelrc-wide-gpl)
220 (lgpl . skelrc-lgpl)
221 (agpl . skelrc-agpl)
222 (bsd . skelrc-bsd)
223 (revisions . ,(concat "[[revision-banner]]\n"
224 "[[cont-comment]]\n"
225 "[[cont-comment]] $Log$"
226 "[[block-end]]\n\n"))
227 (revision-banner . (skelrc-banner "Revision history" t))
228 (preamble . "")
229 (trailer . "[[tag-line]]\n[[postamble]]")
230 (postamble . "")
231 (tag-line . (skelrc-banner "That's all, folks"))
232 (block-start . (skel-lookup 'new-comment))
233 (block-banner-knob . "")
234 (block-banner-end . "")
235 (block-end . ""))
236 skel-alist))