Mercurial > emacs
annotate lisp/generic.el @ 21974:42b308d86213
(report-emacs-bug): After reinserting
the signature, leave point before it.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Thu, 07 May 1998 06:31:31 +0000 |
parents | 42fa0da05721 |
children | f53740d7d40d |
rev | line source |
---|---|
21013
adb28ace7f33
Provide generic, not generic-mode.
Richard M. Stallman <rms@gnu.org>
parents:
20459
diff
changeset
|
1 ;;; generic.el --- Defining simple major modes with comment and font-lock. |
18254 | 2 ;; |
3 ;; Copyright (C) 1997 Free Software Foundation, Inc. | |
4 ;; | |
21182 | 5 ;; Author: Peter Breton <pbreton@cs.umb.edu> |
18254 | 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 ;; Purpose: | |
27 | |
28 ;; Meta-mode to create simple major modes | |
29 ;; with basic comment and font-lock support | |
30 | |
31 ;;; Commentary: | |
32 | |
33 ;; INTRODUCTION: | |
34 | |
35 ;; Generic-mode is a meta-mode which can be used to define small modes | |
21014 | 36 ;; which provide basic comment and font-lock support. These modes are |
18254 | 37 ;; intended for the many configuration files and such which are too small |
38 ;; for a "real" mode, but still have a regular syntax, comment characters | |
39 ;; and the like. | |
40 ;; | |
41 ;; Each generic mode can define the following: | |
42 ;; | |
21014 | 43 ;; * List of comment-characters. The entries in this list should be |
18254 | 44 ;; either a character, a one or two character string or a cons pair. |
45 ;; If the entry is a character or a one-character string | |
46 ;; LIMITATIONS: Emacs does not support comment strings of more than | |
47 ;; two characters in length. | |
48 ;; | |
21014 | 49 ;; * List of keywords to font-lock. Each keyword should be a string. |
18254 | 50 ;; If you have additional keywords which should be highlighted in a face |
21182 | 51 ;; different from `font-lock-keyword-face', you can use the convenience |
52 ;; function `generic-make-keywords-list' (which see), and add the | |
18254 | 53 ;; result to the following list: |
54 ;; | |
21014 | 55 ;; * Additional expressions to font-lock. This should be a list of |
18254 | 56 ;; expressions, each of which should be of the same form |
21182 | 57 ;; as those in `font-lock-defaults-alist'. |
18254 | 58 ;; |
59 ;; * List of regular expressions to be placed in auto-mode-alist. | |
60 ;; | |
61 ;; * List of functions to call to do some additional setup | |
62 ;; | |
63 ;; This should pretty much cover basic functionality; if you need much | |
64 ;; more than this, or you find yourself writing extensive customizations, | |
65 ;; perhaps you should be writing a major mode instead! | |
66 ;; | |
67 ;; LOCAL VARIABLES: | |
68 ;; | |
69 ;; To put a file into generic mode using local variables, use a line | |
70 ;; like this in a Local Variables block: | |
71 ;; | |
72 ;; mode: default-generic | |
73 ;; | |
74 ;; Do NOT use "mode: generic"! | |
75 ;; See also "AUTOMATICALLY ENTERING GENERIC MODE" below. | |
76 ;; | |
77 ;; DEFINING NEW GENERIC MODES: | |
78 ;; | |
21182 | 79 ;; Use the `define-generic-mode' function to define new modes. |
18254 | 80 ;; For example: |
81 ;; | |
21182 | 82 ;; (require 'generic) |
18254 | 83 ;; (define-generic-mode 'foo-generic-mode |
84 ;; (list ?% ) | |
85 ;; (list "keyword") | |
86 ;; nil | |
87 ;; (list "\.FOO") | |
88 ;; (list 'foo-setup-function)) | |
89 ;; | |
21182 | 90 ;; defines a new generic-mode `foo-generic-mode', which has '%' as a |
91 ;; comment character, and "keyword" as a keyword. When files which end in | |
18254 | 92 ;; '.FOO' are loaded, Emacs will go into foo-generic-mode and call |
21182 | 93 ;; foo-setup-function. You can also use the function `foo-generic-mode' |
18254 | 94 ;; (which is interactive) to put a buffer into foo-generic-mode. |
95 ;; | |
96 ;; AUTOMATICALLY ENTERING GENERIC MODE: | |
97 ;; | |
98 ;; Generic-mode provides a hook which automatically puts a | |
99 ;; file into default-generic-mode if the first few lines of a file in | |
21182 | 100 ;; fundamental mode start with a hash comment character. To disable |
101 ;; this functionality, set the variable `generic-use-find-file-hook' | |
102 ;; to nil BEFORE loading generic-mode. See the variables | |
103 ;; `generic-lines-to-scan' and `generic-find-file-regexp' for customization | |
18254 | 104 ;; options. |
105 ;; | |
106 ;; GOTCHAS: | |
107 ;; | |
21014 | 108 ;; Be careful that your font-lock definitions are correct. Getting them |
18254 | 109 ;; wrong can cause emacs to continually attempt to fontify! This problem |
110 ;; is not specific to generic-mode. | |
111 ;; | |
112 | |
21057 | 113 ;; Credit for suggestions, brainstorming, help with debugging: |
18254 | 114 ;; ACorreir@pervasive-sw.com (Alfred Correira) |
115 | |
116 ;;; Code: | |
117 | |
118 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
21077
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
119 ;; Internal Variables |
18254 | 120 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
121 | |
122 (make-variable-buffer-local | |
123 (defvar generic-font-lock-defaults nil | |
124 "Global defaults for font-lock in a generic mode.")) | |
125 | |
126 (make-variable-buffer-local | |
127 (defvar generic-mode-name 'default-generic-mode | |
128 "The name of the generic mode. | |
129 This is the car of one of the items in `generic-mode-alist'. | |
130 This variable is buffer-local.")) | |
131 | |
132 (make-variable-buffer-local | |
133 (defvar generic-comment-list nil | |
134 "List of comment characters for a generic mode.")) | |
135 | |
136 (make-variable-buffer-local | |
137 (defvar generic-keywords-list nil | |
138 "List of keywords for a generic mode.")) | |
139 | |
140 (make-variable-buffer-local | |
141 (defvar generic-font-lock-expressions nil | |
142 "List of font-lock expressions for a generic mode.")) | |
143 | |
144 (make-variable-buffer-local | |
145 (defvar generic-mode-function-list nil | |
146 "List of customization functions to call for a generic mode.")) | |
147 | |
148 (make-variable-buffer-local | |
149 (defvar generic-mode-syntax-table nil | |
150 "Syntax table for use in a generic mode.")) | |
151 | |
152 (defvar generic-mode-alist nil | |
153 "An association list for generic-mode. | |
154 Each entry in the list looks like this: | |
155 | |
156 NAME COMMENT-LIST KEYWORD-LIST FONT-LOCK-LIST AUTO-MODE-LIST FUNCTION-LIST. | |
157 | |
158 Do not add entries to this list directly; use `define-generic-mode' | |
159 instead (which see).") | |
160 | |
21077
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
161 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
162 ;; Customization Variables |
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
163 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
164 |
21879
14316c9ecdac
(generic): Added defgroup declaration.
Richard M. Stallman <rms@gnu.org>
parents:
21182
diff
changeset
|
165 (defgroup generic nil |
14316c9ecdac
(generic): Added defgroup declaration.
Richard M. Stallman <rms@gnu.org>
parents:
21182
diff
changeset
|
166 "Define simple major modes with comment and font-lock support." |
14316c9ecdac
(generic): Added defgroup declaration.
Richard M. Stallman <rms@gnu.org>
parents:
21182
diff
changeset
|
167 :prefix "generic-" |
14316c9ecdac
(generic): Added defgroup declaration.
Richard M. Stallman <rms@gnu.org>
parents:
21182
diff
changeset
|
168 :group 'extensions) |
14316c9ecdac
(generic): Added defgroup declaration.
Richard M. Stallman <rms@gnu.org>
parents:
21182
diff
changeset
|
169 |
21077
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
170 (defcustom generic-use-find-file-hook t |
18254 | 171 "*If non-nil, add a hook to enter default-generic-mode automatically |
172 if the first few lines of a file in fundamental mode start with a hash | |
21077
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
173 comment character." |
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
174 :group 'generic |
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
175 :type 'boolean |
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
176 ) |
18254 | 177 |
21077
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
178 (defcustom generic-lines-to-scan 3 |
18254 | 179 "*Number of lines that `generic-mode-find-file-hook' looks at |
180 when deciding whether to enter generic-mode automatically. | |
21077
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
181 This variable should be set to a small positive number." |
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
182 :group 'generic |
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
183 :type 'integer |
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
184 ) |
18254 | 185 |
21077
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
186 (defcustom generic-find-file-regexp "#.*\n\\(.*\n\\)?" |
18254 | 187 "*Regular expression used by `generic-mode-find-file-hook' |
188 to determine if files in fundamental mode should be put into | |
21077
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
189 `default-generic-mode' instead." |
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
190 :group 'generic |
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
191 :type 'regexp |
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
192 ) |
18254 | 193 |
194 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
195 ;; Inline functions | |
196 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
197 | |
198 (defsubst generic-read-type () | |
199 (completing-read | |
200 "Generic Type: " | |
201 (mapcar | |
202 '(lambda (elt) (list (symbol-name (car elt)))) | |
203 generic-mode-alist) nil t)) | |
204 | |
205 ;; Basic sanity checks. It does *not* check whether the elements of the lists | |
206 ;; are of the correct type. | |
207 (defsubst generic-mode-sanity-check (name comment-list keyword-list | |
208 font-lock-list auto-mode-list | |
209 function-list &optional description) | |
21077
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
210 (and (not (symbolp name)) |
18254 | 211 (error "%s is not a symbol" (princ name))) |
212 | |
213 (mapcar '(lambda (elt) | |
214 (if (not (listp elt)) | |
215 (error "%s is not a list" (princ elt)))) | |
216 (list comment-list keyword-list font-lock-list | |
217 auto-mode-list function-list)) | |
218 | |
21077
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
219 (and (not (or (null description) (stringp description))) |
18254 | 220 (error "Description must be a string or nil")) |
221 ) | |
222 | |
223 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
224 ;; Functions | |
225 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
226 | |
227 ;;;### autoload | |
228 (defun define-generic-mode (name comment-list keyword-list font-lock-list | |
229 auto-mode-list function-list | |
230 &optional description) | |
231 "Create a new generic mode with NAME. | |
232 NAME should be a symbol; its string representation is used as the function | |
233 name. If DESCRIPTION is provided, it is used as the docstring for the new | |
234 function. | |
235 | |
236 COMMENT-LIST is a list, whose entries are either a single character, | |
237 a one or two character string or a cons pair. If the entry is a character | |
238 or a one-character string, it is added to the mode's syntax table with | |
239 comment-start syntax. If the entry is a cons pair, the elements of the | |
240 pair are considered to be comment-start and comment-end respectively. | |
241 Note that Emacs has limitations regarding comment characters. | |
242 | |
243 KEYWORD-LIST is a list of keywords to highlight with `font-lock-keyword-face'. | |
244 Each keyword should be a string. | |
245 | |
246 FONT-LOCK-LIST is a list of additional expressions to highlight. Each entry | |
247 in the list should have the same form as an entry in `font-lock-defaults-alist' | |
248 | |
249 AUTO-MODE-LIST is a list of regular expressions to add to auto-mode-alist. | |
250 These regexps are added to auto-mode-alist as soon as `define-generic-mode' | |
21014 | 251 is called; any old regexps with the same name are removed. |
18254 | 252 |
253 FUNCTION-LIST is a list of functions to call to do some additional setup. | |
254 | |
21014 | 255 See the file generic-x.el for some examples of `define-generic-mode'." |
18254 | 256 |
257 ;; Basic sanity check | |
258 (generic-mode-sanity-check name | |
259 comment-list keyword-list font-lock-list | |
260 auto-mode-list function-list description) | |
261 | |
262 ;; Remove any old entry | |
263 (setq generic-mode-alist | |
264 (delq (assq name generic-mode-alist) | |
265 generic-mode-alist)) | |
266 | |
267 ;; Add a new entry | |
268 (setq generic-mode-alist | |
269 (append | |
270 (list | |
271 (list | |
272 name comment-list keyword-list font-lock-list | |
273 auto-mode-list function-list | |
274 )) | |
275 generic-mode-alist)) | |
276 | |
277 ;; Add it to auto-mode-alist | |
278 (generic-add-to-auto-mode name auto-mode-list t) | |
279 | |
280 ;; Define a function for it | |
281 (generic-create-generic-function name description) | |
282 ) | |
283 | |
284 (defun generic-add-to-auto-mode (mode auto-mode-list | |
285 &optional remove-old prepend) | |
286 "Add the entries for mode to `auto-mode-alist'. | |
287 If remove-old is non-nil, removes old entries first. If prepend is | |
288 non-nil, prepends entries to auto-mode-alist; otherwise, appends them." | |
289 | |
290 (if (not (listp auto-mode-list)) | |
291 (error "%s is not a list" (princ auto-mode-list))) | |
292 | |
293 (let ((new-mode (intern (symbol-name mode)))) | |
21077
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
294 (and remove-old |
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
295 (let ((auto-mode-entry)) |
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
296 (while (setq auto-mode-entry (rassq new-mode auto-mode-alist)) |
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
297 (setq auto-mode-alist |
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
298 (delq auto-mode-entry |
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
299 auto-mode-alist))))) |
18254 | 300 |
301 (mapcar '(lambda (entry) | |
302 (generic-add-auto-mode-entry new-mode entry prepend)) | |
303 auto-mode-list))) | |
304 | |
305 (defun generic-add-auto-mode-entry (name entry &optional prepend) | |
306 "Add a new entry to the end of auto-mode-alist. | |
307 If prepend is non-nil, add the entry to the front of the list." | |
308 (let ((new-entry (list (cons entry name)))) | |
309 (setq auto-mode-alist | |
310 (if prepend | |
311 (append new-entry auto-mode-alist) | |
312 (append auto-mode-alist new-entry))))) | |
313 | |
314 (defun generic-create-generic-function (name &optional description) | |
315 "Create a generic mode function with NAME. | |
316 If DESCRIPTION is provided, it is used as the docstring." | |
317 (let ((symname (symbol-name name))) | |
318 (fset (intern symname) | |
319 (list 'lambda nil | |
320 (or description | |
321 (concat "Generic mode for type " symname)) | |
322 (list 'interactive) | |
323 (list 'generic-mode-with-type (list 'quote name)))))) | |
324 | |
325 (defun generic-mode-with-type (&optional mode) | |
326 "Go into the generic-mode MODE." | |
327 (let* ((type (or mode generic-mode-name)) | |
328 (generic-mode-list (assoc type generic-mode-alist)) | |
329 ) | |
330 | |
21077
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
331 (and (not generic-mode-list) |
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
332 (error "Can't find generic-mode information for type %s" |
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
333 (princ generic-mode-name))) |
18254 | 334 |
335 ;; Put this after the point where we read generic-mode-name! | |
336 (kill-all-local-variables) | |
337 | |
338 (setq | |
339 generic-mode-name type | |
340 generic-comment-list (nth 1 generic-mode-list) | |
341 generic-keywords-list (nth 2 generic-mode-list) | |
342 generic-font-lock-expressions (nth 3 generic-mode-list) | |
343 generic-mode-function-list (nth 5 generic-mode-list) | |
344 major-mode 'generic-mode | |
345 mode-name (symbol-name type) | |
346 ) | |
347 | |
348 (generic-mode-set-comments generic-comment-list) | |
349 | |
350 ;; Font-lock functionality | |
351 ;; Font-lock-defaults are always set even if there are no keywords | |
352 ;; or font-lock expressions, so comments can be highlighted. | |
353 (setq generic-font-lock-defaults nil) | |
354 (generic-mode-set-font-lock generic-keywords-list | |
355 generic-font-lock-expressions) | |
356 (make-local-variable 'font-lock-defaults) | |
357 (setq font-lock-defaults (list 'generic-font-lock-defaults nil)) | |
358 | |
359 ;; Call a list of functions | |
21077
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
360 (and generic-mode-function-list |
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
361 (mapcar 'funcall generic-mode-function-list)) |
18254 | 362 ) |
363 ) | |
364 | |
365 ;;;###autoload | |
366 (defun generic-mode (type) | |
367 "A mode to do basic comment and font-lock functionality | |
368 for files which are too small to warrant their own mode, but have | |
369 comment characters, keywords, and the like. | |
370 | |
371 To define a generic-mode, use the function `define-generic-mode'. | |
21014 | 372 Some generic modes are defined in `generic-x.el'." |
18254 | 373 (interactive |
374 (list (generic-read-type))) | |
375 (generic-mode-with-type (intern type))) | |
376 | |
377 ;;; Comment Functionality | |
378 (defun generic-mode-set-comments (comment-list) | |
379 "Set up comment functionality for generic mode." | |
380 (if (null comment-list) | |
381 nil | |
382 (let ((generic-mode-syntax-table (make-syntax-table))) | |
383 (make-local-variable 'comment-start) | |
384 (make-local-variable 'comment-start-skip) | |
385 (make-local-variable 'comment-end) | |
386 (mapcar 'generic-mode-set-a-comment comment-list) | |
387 (set-syntax-table generic-mode-syntax-table)))) | |
388 | |
389 (defun generic-mode-set-a-comment (comment) | |
390 (and (char-or-string-p comment) | |
391 (if (stringp comment) | |
392 (cond | |
393 ((eq (length comment) 1) | |
394 (generic-mode-set-comment-char | |
395 (string-to-char comment))) | |
396 ((eq (length comment) 2) | |
397 (generic-mode-set-comment-string comment)) | |
398 (t | |
399 (error "Character string %s must be one or two characters long" | |
400 comment)) | |
401 ) | |
402 (generic-mode-set-comment-char comment))) | |
21077
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
403 (and (consp comment) |
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
404 (generic-mode-set-comment-pair comment))) |
18254 | 405 |
406 (defun generic-mode-set-comment-char (comment-char) | |
407 "Set the given character as a comment character for generic mode." | |
408 (if (not comment-char) | |
409 nil | |
410 (setq | |
411 comment-end "" | |
412 comment-start (char-to-string comment-char) | |
413 comment-start-skip (concat comment-start "+ *") | |
414 ) | |
415 | |
416 (modify-syntax-entry comment-char "<" | |
417 generic-mode-syntax-table) | |
418 (modify-syntax-entry ?\n ">" | |
419 generic-mode-syntax-table))) | |
420 | |
421 (defun generic-mode-set-comment-string (comment-string) | |
422 "Set the given string as a comment string for generic mode." | |
423 (if (not comment-string) | |
424 nil | |
425 (setq | |
426 comment-end "" | |
427 comment-start comment-string | |
428 comment-start-skip (concat comment-start " *") | |
429 ) | |
430 | |
431 (let ((first (elt comment-string 0)) | |
432 (second (elt comment-string 1))) | |
433 ;; C++ style comments | |
434 (if (char-equal first second) | |
435 (progn | |
436 (modify-syntax-entry first "<12b" | |
437 generic-mode-syntax-table) | |
438 (modify-syntax-entry ?\n ">b" | |
439 generic-mode-syntax-table))) | |
440 ;; Some other two character string | |
441 (modify-syntax-entry first "<1" | |
442 generic-mode-syntax-table) | |
443 (modify-syntax-entry second "<2" | |
444 generic-mode-syntax-table) | |
445 (modify-syntax-entry ?\n ">" | |
446 generic-mode-syntax-table)))) | |
447 | |
448 (defun generic-mode-set-comment-pair (comment-pair) | |
449 "Set the given comment pair as a comment start and end for generic mode." | |
450 (let ((generic-comment-start (car comment-pair)) | |
451 (generic-comment-end (cdr comment-pair)) | |
452 ) | |
453 (setq | |
454 comment-end generic-comment-end | |
455 comment-start generic-comment-start | |
456 comment-start-skip (concat generic-comment-start " *") | |
457 ) | |
458 | |
459 ;; Sanity checks | |
21077
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
460 (and (not (and (stringp generic-comment-start) |
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
461 (stringp generic-comment-end))) |
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
462 (error "Elements of cons pair must be strings")) |
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
463 (and (not (and (equal (length generic-comment-start) 2) |
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
464 (equal (length generic-comment-end) 2))) |
18254 | 465 (error "Start and end must be exactly two characters long")) |
466 | |
467 (let ((first (elt generic-comment-start 0)) | |
468 (second (elt generic-comment-start 1)) | |
469 (third (elt generic-comment-end 0)) | |
470 (fourth (elt generic-comment-end 1)) | |
471 ) | |
472 | |
473 (modify-syntax-entry first ". 1" generic-mode-syntax-table) | |
474 (modify-syntax-entry second ". 2" generic-mode-syntax-table) | |
475 | |
476 (modify-syntax-entry | |
477 third | |
478 (concat | |
479 "." | |
480 (cond | |
481 ((char-equal first third) " 13") | |
482 ((char-equal second third) " 23") | |
483 (t " 3")) | |
484 ) | |
485 generic-mode-syntax-table) | |
486 | |
487 (modify-syntax-entry | |
488 fourth | |
489 (concat | |
490 "." | |
491 (cond | |
492 ((char-equal first fourth) " 14") | |
493 ((char-equal second fourth) " 24") | |
494 (t " 4")) | |
495 ) | |
496 generic-mode-syntax-table) | |
497 ))) | |
498 | |
499 (defun generic-mode-set-font-lock (keywords font-lock-expressions) | |
500 "Set up font-lock functionality for generic mode." | |
501 (let ((generic-font-lock-expressions)) | |
502 ;; Keywords | |
21077
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
503 (and keywords |
18254 | 504 (setq |
505 generic-font-lock-expressions | |
506 (append | |
21879
14316c9ecdac
(generic): Added defgroup declaration.
Richard M. Stallman <rms@gnu.org>
parents:
21182
diff
changeset
|
507 (list (let ((regexp (regexp-opt keywords))) |
14316c9ecdac
(generic): Added defgroup declaration.
Richard M. Stallman <rms@gnu.org>
parents:
21182
diff
changeset
|
508 (list (concat "\\<\\(" regexp "\\)\\>") |
14316c9ecdac
(generic): Added defgroup declaration.
Richard M. Stallman <rms@gnu.org>
parents:
21182
diff
changeset
|
509 1 |
14316c9ecdac
(generic): Added defgroup declaration.
Richard M. Stallman <rms@gnu.org>
parents:
21182
diff
changeset
|
510 'font-lock-keyword-face))) |
18254 | 511 generic-font-lock-expressions))) |
512 ;; Other font-lock expressions | |
21077
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
513 (and font-lock-expressions |
18254 | 514 (setq generic-font-lock-expressions |
515 (append | |
516 font-lock-expressions | |
517 generic-font-lock-expressions))) | |
21077
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
518 (and (or font-lock-expressions keywords) |
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
519 (setq generic-font-lock-defaults generic-font-lock-expressions)) |
18254 | 520 )) |
521 | |
522 ;; Support for [KEYWORD] constructs found in INF, INI and Samba files | |
523 (defun generic-bracket-support () | |
524 (setq imenu-generic-expression | |
20459 | 525 '((nil "^\\[\\(.*\\)\\]" 1)) |
526 imenu-case-fold-search t)) | |
18254 | 527 |
528 ;; This generic mode is always defined | |
529 (define-generic-mode 'default-generic-mode (list ?#) nil nil nil nil) | |
530 | |
531 ;; A more general solution would allow us to enter generic-mode for | |
532 ;; *any* comment character, but would require us to synthesize a new | |
533 ;; generic-mode on the fly. I think this gives us most of what we | |
534 ;; want. | |
535 (defun generic-mode-find-file-hook () | |
536 "Hook to enter default-generic-mode automatically | |
537 if the first few lines of a file in fundamental-mode start with a hash | |
538 comment character. This hook will be installed if the variable | |
539 `generic-use-find-file-hook' is non-nil. The variable `generic-lines-to-scan' | |
540 determines the number of lines to look at." | |
541 (if (not (eq major-mode 'fundamental-mode)) | |
542 nil | |
21077
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
543 (and (or (> 1 generic-lines-to-scan) |
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
544 (< 50 generic-lines-to-scan)) |
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
545 (error "Variable `generic-lines-to-scan' should be set to a small" |
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
546 " positive number")) |
18254 | 547 (let ((comment-regexp "") |
548 (count 0) | |
549 ) | |
550 (while (< count generic-lines-to-scan) | |
551 (setq comment-regexp (concat comment-regexp | |
552 generic-find-file-regexp)) | |
553 (setq count (1+ count))) | |
554 (save-excursion | |
555 (goto-char (point-min)) | |
21077
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
556 (and (looking-at comment-regexp) |
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
557 (generic-mode-with-type 'default-generic-mode)))))) |
18254 | 558 |
559 (defun generic-mode-ini-file-find-file-hook () | |
560 "Hook to enter default-generic-mode automatically | |
561 if the first few lines of a file in fundamental-mode look like an INI file. | |
562 This hook is NOT installed by default." | |
21077
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
563 (and (eq major-mode 'fundamental-mode) |
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
564 (save-excursion |
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
565 (goto-char (point-min)) |
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
566 (and (looking-at "^\\s-*\\[.*\\]") |
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
567 (generic-mode-with-type 'ini-generic-mode))))) |
18254 | 568 |
569 (and generic-use-find-file-hook | |
570 (add-hook 'find-file-hooks 'generic-mode-find-file-hook)) | |
571 | |
572 (defun generic-make-keywords-list (keywords-list face &optional prefix suffix) | |
573 "Return a regular expression matching the specified keywords. | |
574 The regexp is highlighted with FACE." | |
21077
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
575 (and (not (listp keywords-list)) |
4b5c8a2ce0b2
(generic-mode-ini-file-find-file-hook): Use and-s instead of if-s.
Richard M. Stallman <rms@gnu.org>
parents:
21057
diff
changeset
|
576 (error "Keywords argument must be a list of strings")) |
21879
14316c9ecdac
(generic): Added defgroup declaration.
Richard M. Stallman <rms@gnu.org>
parents:
21182
diff
changeset
|
577 (list (concat (or prefix "") |
14316c9ecdac
(generic): Added defgroup declaration.
Richard M. Stallman <rms@gnu.org>
parents:
21182
diff
changeset
|
578 "\\<\\(" |
14316c9ecdac
(generic): Added defgroup declaration.
Richard M. Stallman <rms@gnu.org>
parents:
21182
diff
changeset
|
579 ;; Use an optimized regexp. |
14316c9ecdac
(generic): Added defgroup declaration.
Richard M. Stallman <rms@gnu.org>
parents:
21182
diff
changeset
|
580 (regexp-opt keywords-list t) |
14316c9ecdac
(generic): Added defgroup declaration.
Richard M. Stallman <rms@gnu.org>
parents:
21182
diff
changeset
|
581 "\\)\\>" |
14316c9ecdac
(generic): Added defgroup declaration.
Richard M. Stallman <rms@gnu.org>
parents:
21182
diff
changeset
|
582 (or suffix "")) |
14316c9ecdac
(generic): Added defgroup declaration.
Richard M. Stallman <rms@gnu.org>
parents:
21182
diff
changeset
|
583 1 |
21928
42fa0da05721
(generic-make-keywords-list): Delete spurious paren.
Richard M. Stallman <rms@gnu.org>
parents:
21879
diff
changeset
|
584 face)) |
18254 | 585 |
21013
adb28ace7f33
Provide generic, not generic-mode.
Richard M. Stallman <rms@gnu.org>
parents:
20459
diff
changeset
|
586 (provide 'generic) |
18254 | 587 |
21879
14316c9ecdac
(generic): Added defgroup declaration.
Richard M. Stallman <rms@gnu.org>
parents:
21182
diff
changeset
|
588 ;;; generic.el ends here |