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