Mercurial > emacs
annotate lisp/emacs-lisp/generic.el @ 62207:50fda5cdcc5d
(Arguments): Fix punctuation.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Tue, 10 May 2005 07:26:25 +0000 |
parents | f3e1b2ceffd2 |
children | a0af6e05c03a 08185296b491 |
rev | line source |
---|---|
61521 | 1 ;;; generic.el --- defining simple major modes with comment and font-lock |
2 ;; | |
3 ;; Copyright (C) 1997, 1999, 2004, 2005 Free Software Foundation, Inc. | |
4 ;; | |
5 ;; Author: Peter Breton <pbreton@cs.umb.edu> | |
6 ;; Created: Fri Sep 27 1996 | |
7 ;; Keywords: generic, comment, font-lock | |
8 | |
9 ;; This file is part of GNU Emacs. | |
10 | |
11 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
12 ;; it under the terms of the GNU General Public License as published by | |
13 ;; the Free Software Foundation; either version 2, or (at your option) | |
14 ;; any later version. | |
15 | |
16 ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 ;; GNU General Public License for more details. | |
20 | |
21 ;; You should have received a copy of the GNU General Public License | |
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
24 ;; Boston, MA 02111-1307, USA. | |
25 | |
26 ;;; Commentary: | |
27 | |
28 ;; INTRODUCTION: | |
29 ;; | |
30 ;; The macro `define-generic-mode' can be used to define small modes | |
31 ;; which provide basic comment and font-lock support. These modes are | |
32 ;; intended for the many configuration files and such which are too | |
33 ;; small for a "real" mode, but still have a regular syntax, comment | |
34 ;; characters and the like. | |
35 ;; | |
36 ;; Each generic mode can define the following: | |
37 ;; | |
61931 | 38 ;; * List of comment-characters. The elements of this list should be |
39 ;; either a character, a one or two character string, or a cons | |
40 ;; cell. If the entry is a character or a string, it is added to | |
41 ;; the mode's syntax table with "comment starter" syntax. If the | |
42 ;; entry is a cons cell, the `car' and `cdr' of the pair are | |
43 ;; considered the "comment starter" and "comment ender" | |
44 ;; respectively. (The latter should be nil if you want comments to | |
45 ;; end at the end of the line.) Emacs does not support comment | |
46 ;; strings of more than two characters in length. | |
61521 | 47 ;; |
48 ;; * List of keywords to font-lock. Each keyword should be a string. | |
49 ;; If you have additional keywords which should be highlighted in a | |
50 ;; face different from `font-lock-keyword-face', you can use the | |
51 ;; convenience function `generic-make-keywords-list' (which see), | |
52 ;; and add the result to the following list: | |
53 ;; | |
54 ;; * Additional expressions to font-lock. This should be a list of | |
55 ;; expressions, each of which should be of the same form as those in | |
56 ;; `font-lock-keywords'. | |
57 ;; | |
58 ;; * List of regular expressions to be placed in auto-mode-alist. | |
59 ;; | |
60 ;; * List of functions to call to do some additional setup | |
61 ;; | |
62 ;; This should pretty much cover basic functionality; if you need much | |
63 ;; more than this, or you find yourself writing extensive customizations, | |
64 ;; perhaps you should be writing a major mode instead! | |
65 ;; | |
66 ;; EXAMPLE: | |
67 ;; | |
68 ;; You can use `define-generic-mode' like this: | |
69 ;; | |
70 ;; (define-generic-mode 'foo-generic-mode | |
71 ;; (list ?%) | |
72 ;; (list "keyword") | |
73 ;; nil | |
74 ;; (list "\\.FOO\\'") | |
75 ;; (list 'foo-setup-function)) | |
76 ;; | |
77 ;; to define a new generic-mode `foo-generic-mode', which has '%' as a | |
78 ;; comment character, and "keyword" as a keyword. When files which | |
79 ;; end in '.FOO' are loaded, Emacs will go into foo-generic-mode and | |
80 ;; call foo-setup-function. You can also use the function | |
81 ;; `foo-generic-mode' (which is interactive) to put a buffer into | |
82 ;; foo-generic-mode. | |
83 ;; | |
84 ;; GOTCHAS: | |
85 ;; | |
86 ;; Be careful that your font-lock definitions are correct. Getting | |
87 ;; them wrong can cause Emacs to continually attempt to fontify! This | |
88 ;; problem is not specific to generic-mode. | |
89 | |
90 ;; Credit for suggestions, brainstorming, help with debugging: | |
91 ;; ACorreir@pervasive-sw.com (Alfred Correira) | |
92 ;; Extensive cleanup by: | |
93 ;; Stefan Monnier (monnier+gnu/emacs@flint.cs.yale.edu) | |
94 | |
95 ;;; Code: | |
96 | |
97 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
98 ;; Internal Variables | |
99 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
100 | |
101 (defvar generic-font-lock-keywords nil | |
102 "Keywords for `font-lock-defaults' in a generic mode.") | |
103 (make-variable-buffer-local 'generic-font-lock-keywords) | |
104 (defvaralias 'generic-font-lock-defaults 'generic-font-lock-keywords) | |
105 (make-obsolete-variable 'generic-font-lock-defaults 'generic-font-lock-keywords "22.1") | |
106 | |
107 ;;;###autoload | |
108 (defvar generic-mode-list nil | |
109 "A list of mode names for `generic-mode'. | |
110 Do not add entries to this list directly; use `define-generic-mode' | |
111 instead (which see).") | |
112 | |
113 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
114 ;; Functions | |
115 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
116 | |
117 ;;;###autoload | |
118 (defmacro define-generic-mode (mode comment-list keyword-list | |
119 font-lock-list auto-mode-list | |
120 function-list &optional docstring | |
121 &rest custom-keyword-args) | |
122 "Create a new generic mode MODE. | |
123 | |
61931 | 124 MODE is the name of the command for the generic mode; don't quote |
125 it. The optional DOCSTRING is the documentation for the mode | |
126 command. If you do not supply it, `define-generic-mode' uses a | |
127 default documentation string instead. | |
61521 | 128 |
61931 | 129 COMMENT-LIST is a list in which each element is either a |
130 character, a string of one or two characters, or a cons cell. A | |
131 character or a string is set up in the mode's syntax table as a | |
132 \"comment starter\". If the entry is a cons cell, the `car' is | |
133 set up as a \"comment starter\" and the `cdr' as a \"comment | |
134 ender\". (Use nil for the latter if you want comments to end at | |
135 the end of the line.) Note that the syntax table has limitations | |
136 about what comment starters and enders are actually possible. | |
61521 | 137 |
138 KEYWORD-LIST is a list of keywords to highlight with | |
139 `font-lock-keyword-face'. Each keyword should be a string. | |
140 | |
141 FONT-LOCK-LIST is a list of additional expressions to highlight. | |
61931 | 142 Each element of this list should have the same form as an element |
143 of `font-lock-keywords'. | |
61521 | 144 |
145 AUTO-MODE-LIST is a list of regular expressions to add to | |
61931 | 146 `auto-mode-alist'. These regular expressions are added when |
147 Emacs runs the macro expansion. | |
61521 | 148 |
149 FUNCTION-LIST is a list of functions to call to do some | |
61916
9096ff17bfdf
(define-generic-mode): Fix docstring.
Lute Kamstra <lute@gnu.org>
parents:
61904
diff
changeset
|
150 additional setup. The mode command calls these functions just |
9096ff17bfdf
(define-generic-mode): Fix docstring.
Lute Kamstra <lute@gnu.org>
parents:
61904
diff
changeset
|
151 before it runs the mode hook. |
61521 | 152 |
61931 | 153 The optional CUSTOM-KEYWORD-ARGS are pairs of keywords and values |
154 to include in the generated `defcustom' form for the mode hook | |
155 variable `MODE-hook'. The default value for the `:group' keyword | |
156 is MODE with the final \"-mode\" (if any) removed. (Don't use | |
157 this default group name unless you have written a `defgroup' to | |
158 define that group properly.) You can specify keyword arguments | |
159 without specifying a docstring. | |
61521 | 160 |
161 See the file generic-x.el for some examples of `define-generic-mode'." | |
162 (declare (debug (sexp def-form def-form def-form form def-form | |
163 [&optional stringp] &rest [keywordp form])) | |
164 (indent 1)) | |
165 | |
166 ;; Backward compatibility. | |
167 (when (eq (car-safe mode) 'quote) | |
168 (setq mode (eval mode))) | |
169 | |
170 (when (and docstring (not (stringp docstring))) | |
171 ;; DOCSTRING is not a string so we assume that it's actually the | |
172 ;; first keyword of CUSTOM-KEYWORD-ARGS. | |
173 (push docstring custom-keyword-args) | |
174 (setq docstring nil)) | |
175 | |
176 (let* ((name (symbol-name mode)) | |
177 (pretty-name (capitalize (replace-regexp-in-string | |
178 "-mode\\'" "" name))) | |
179 (mode-hook (intern (concat name "-hook")))) | |
180 | |
181 (unless (plist-get custom-keyword-args :group) | |
182 (setq custom-keyword-args | |
61931 | 183 (plist-put custom-keyword-args |
61521 | 184 :group `',(intern (replace-regexp-in-string |
185 "-mode\\'" "" name))))) | |
186 | |
187 `(progn | |
188 ;; Add a new entry. | |
189 (add-to-list 'generic-mode-list ,name) | |
190 | |
191 ;; Add it to auto-mode-alist | |
192 (dolist (re ,auto-mode-list) | |
193 (add-to-list 'auto-mode-alist (cons re ',mode))) | |
194 | |
195 (defcustom ,mode-hook nil | |
196 ,(concat "Hook run when entering " pretty-name " mode.") | |
197 :type 'hook | |
198 ,@custom-keyword-args) | |
199 | |
200 (defun ,mode () | |
201 ,(or docstring | |
202 (concat pretty-name " mode.\n" | |
203 "This a generic mode defined with `define-generic-mode'.")) | |
204 (interactive) | |
205 (generic-mode-internal ',mode ,comment-list ,keyword-list | |
206 ,font-lock-list ,function-list))))) | |
207 | |
208 ;;;###autoload | |
209 (defun generic-mode-internal (mode comment-list keyword-list | |
210 font-lock-list function-list) | |
211 "Go into the generic mode MODE." | |
212 (let* ((name (symbol-name mode)) | |
213 (pretty-name (capitalize (replace-regexp-in-string | |
214 "-mode\\'" "" name))) | |
215 (mode-hook (intern (concat name "-hook")))) | |
216 | |
217 (kill-all-local-variables) | |
218 | |
219 (setq major-mode mode | |
220 mode-name pretty-name) | |
221 | |
222 (generic-mode-set-comments comment-list) | |
223 | |
224 ;; Font-lock functionality. | |
225 ;; Font-lock-defaults is always set even if there are no keywords | |
226 ;; or font-lock expressions, so comments can be highlighted. | |
227 (setq generic-font-lock-keywords font-lock-list) | |
228 (when keyword-list | |
229 (push (concat "\\_<" (regexp-opt keyword-list t) "\\_>") | |
230 generic-font-lock-keywords)) | |
61904
114b2859d567
(generic-mode-internal): Simplify font-lock-defaults.
Lute Kamstra <lute@gnu.org>
parents:
61521
diff
changeset
|
231 (setq font-lock-defaults '(generic-font-lock-keywords)) |
61521 | 232 |
233 ;; Call a list of functions | |
234 (mapcar 'funcall function-list) | |
235 | |
236 (run-mode-hooks mode-hook))) | |
237 | |
238 ;;;###autoload | |
239 (defun generic-mode (mode) | |
240 "Enter generic mode MODE. | |
241 | |
242 Generic modes provide basic comment and font-lock functionality | |
243 for \"generic\" files. (Files which are too small to warrant their | |
244 own mode, but have comment characters, keywords, and the like.) | |
245 | |
246 To define a generic-mode, use the function `define-generic-mode'. | |
247 Some generic modes are defined in `generic-x.el'." | |
248 (interactive | |
249 (list (completing-read "Generic mode: " generic-mode-list nil t))) | |
250 (funcall (intern mode))) | |
251 | |
252 ;;; Comment Functionality | |
253 (defun generic-mode-set-comments (comment-list) | |
254 "Set up comment functionality for generic mode." | |
255 (let ((st (make-syntax-table)) | |
256 (chars nil) | |
257 (comstyles)) | |
258 (make-local-variable 'comment-start) | |
259 (make-local-variable 'comment-start-skip) | |
260 (make-local-variable 'comment-end) | |
261 | |
262 ;; Go through all the comments | |
263 (dolist (start comment-list) | |
264 (let (end (comstyle "")) | |
265 ;; Normalize | |
266 (when (consp start) | |
267 (setq end (cdr start)) | |
268 (setq start (car start))) | |
269 (when (char-valid-p start) (setq start (char-to-string start))) | |
270 (cond | |
271 ((char-valid-p end) (setq end (char-to-string end))) | |
272 ((zerop (length end)) (setq end "\n"))) | |
273 | |
274 ;; Setup the vars for `comment-region' | |
275 (if comment-start | |
276 ;; We have already setup a comment-style, so use style b | |
277 (progn | |
278 (setq comstyle "b") | |
279 (setq comment-start-skip | |
280 (concat comment-start-skip "\\|" (regexp-quote start) "+\\s-*"))) | |
281 ;; First comment-style | |
282 (setq comment-start start) | |
283 (setq comment-end (if (string-equal end "\n") "" end)) | |
284 (setq comment-start-skip (concat (regexp-quote start) "+\\s-*"))) | |
285 | |
286 ;; Reuse comstyles if necessary | |
287 (setq comstyle | |
288 (or (cdr (assoc start comstyles)) | |
289 (cdr (assoc end comstyles)) | |
290 comstyle)) | |
291 (push (cons start comstyle) comstyles) | |
292 (push (cons end comstyle) comstyles) | |
293 | |
294 ;; Setup the syntax table | |
295 (if (= (length start) 1) | |
296 (modify-syntax-entry (string-to-char start) | |
297 (concat "< " comstyle) st) | |
298 (let ((c0 (elt start 0)) (c1 (elt start 1))) | |
299 ;; Store the relevant info but don't update yet | |
300 (push (cons c0 (concat (cdr (assoc c0 chars)) "1")) chars) | |
301 (push (cons c1 (concat (cdr (assoc c1 chars)) | |
302 (concat "2" comstyle))) chars))) | |
303 (if (= (length end) 1) | |
304 (modify-syntax-entry (string-to-char end) | |
305 (concat ">" comstyle) st) | |
306 (let ((c0 (elt end 0)) (c1 (elt end 1))) | |
307 ;; Store the relevant info but don't update yet | |
308 (push (cons c0 (concat (cdr (assoc c0 chars)) | |
309 (concat "3" comstyle))) chars) | |
310 (push (cons c1 (concat (cdr (assoc c1 chars)) "4")) chars))))) | |
311 | |
312 ;; Process the chars that were part of a 2-char comment marker | |
313 (dolist (cs (nreverse chars)) | |
314 (modify-syntax-entry (car cs) | |
315 (concat (char-to-string (char-syntax (car cs))) | |
316 " " (cdr cs)) | |
317 st)) | |
318 (set-syntax-table st))) | |
319 | |
320 (defun generic-bracket-support () | |
321 "Imenu support for [KEYWORD] constructs found in INF, INI and Samba files." | |
322 (setq imenu-generic-expression | |
323 '((nil "^\\[\\(.*\\)\\]" 1)) | |
324 imenu-case-fold-search t)) | |
325 | |
326 ;;;###autoload | |
327 (defun generic-make-keywords-list (keyword-list face &optional prefix suffix) | |
328 "Return a `font-lock-keywords' construct that highlights KEYWORD-LIST. | |
329 KEYWORD-LIST is a list of keyword strings that should be | |
330 highlighted with face FACE. This function calculates a regular | |
331 expression that matches these keywords and concatenates it with | |
332 PREFIX and SUFFIX. Then it returns a construct based on this | |
333 regular expression that can be used as an element of | |
334 `font-lock-keywords'." | |
335 (unless (listp keyword-list) | |
336 (error "Keywords argument must be a list of strings")) | |
337 (list (concat prefix "\\_<" | |
338 ;; Use an optimized regexp. | |
339 (regexp-opt keyword-list t) | |
340 "\\_>" suffix) | |
341 1 | |
342 face)) | |
343 | |
344 (provide 'generic) | |
345 | |
346 ;; arch-tag: 239c1fc4-1303-48d9-9ac0-657d655669ea | |
347 ;;; generic.el ends here |