Mercurial > emacs
annotate lisp/generic.el @ 31723:8162eb7a8a98
*** empty log message ***
author | Gerd Moellmann <gerd@gnu.org> |
---|---|
date | Tue, 19 Sep 2000 15:00:29 +0000 |
parents | 218b7edb9baf |
children | c443d63bec69 |
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 ;; |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
3 ;; Copyright (C) 1997, 1999 Free Software Foundation, Inc. |
18254 | 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 (defvar generic-font-lock-defaults nil | |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
123 "Global defaults for font-lock in a generic mode.") |
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
124 (make-variable-buffer-local 'generic-font-lock-defaults) |
18254 | 125 |
126 (defvar generic-mode-name 'default-generic-mode | |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
127 "The name of the generic mode. |
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
128 This is the car of one of the items in `generic-mode-alist'. |
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
129 This variable is buffer-local.") |
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
130 (make-variable-buffer-local 'generic-mode-name) |
18254 | 131 |
132 (defvar generic-comment-list nil | |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
133 "List of comment characters for a generic mode.") |
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
134 (make-variable-buffer-local 'generic-comment-list) |
18254 | 135 |
136 (defvar generic-keywords-list nil | |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
137 "List of keywords for a generic mode.") |
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
138 (make-variable-buffer-local 'generic-keywords-list) |
18254 | 139 |
140 (defvar generic-font-lock-expressions nil | |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
141 "List of font-lock expressions for a generic mode.") |
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
142 (make-variable-buffer-local 'generic-font-lock-expressions) |
18254 | 143 |
144 (defvar generic-mode-function-list nil | |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
145 "List of customization functions to call for a generic mode.") |
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
146 (make-variable-buffer-local 'generic-mode-function-list) |
18254 | 147 |
148 (defvar generic-mode-syntax-table nil | |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
149 "Syntax table for use in a generic mode.") |
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
150 (make-variable-buffer-local 'generic-mode-syntax-table) |
18254 | 151 |
152 (defvar generic-mode-alist nil | |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
153 "An association list for `generic-mode'. |
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
154 Each entry in the list looks like this: |
18254 | 155 |
156 NAME COMMENT-LIST KEYWORD-LIST FONT-LOCK-LIST AUTO-MODE-LIST FUNCTION-LIST. | |
157 | |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
158 Do not add entries to this list directly; use `define-generic-mode' |
18254 | 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 |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
171 "*If non-nil, add a hook to enter default-generic-mode automatically. |
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
172 This is done if the first few lines of a file in fundamental mode start |
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
173 with a hash comment character." |
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
|
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 |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
179 "*Number of lines that `generic-mode-find-file-hook' looks at. |
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
180 Relevant 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\\)?" |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
187 "*Regular expression used by `generic-mode-find-file-hook'. |
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
188 Used 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. | |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
207 (defsubst generic-mode-sanity-check (name comment-list keyword-list |
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
208 font-lock-list auto-mode-list |
18254 | 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 | |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
213 (mapcar '(lambda (elt) |
18254 | 214 (if (not (listp elt)) |
215 (error "%s is not a list" (princ elt)))) | |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
216 (list comment-list keyword-list font-lock-list |
18254 | 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 | |
22347
f53740d7d40d
*** empty log message ***
Dan Nicolaescu <done@ece.arizona.edu>
parents:
21928
diff
changeset
|
227 ;;;###autoload |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
228 (defun define-generic-mode (name comment-list keyword-list font-lock-list |
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
229 auto-mode-list function-list |
18254 | 230 &optional description) |
231 "Create a new generic mode with NAME. | |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
232 |
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
233 Args: (NAME COMMENT-LIST KEYWORD-LIST FONT-LOCK-LIST AUTO-MODE-LIST |
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
234 FUNCTION-LIST &optional DESCRIPTION) |
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
235 |
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
236 NAME should be a symbol; its string representation is used as the function |
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
237 name. If DESCRIPTION is provided, it is used as the docstring for the new |
18254 | 238 function. |
239 | |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
240 COMMENT-LIST is a list, whose entries are either a single character, |
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
241 a one or two character string or a cons pair. If the entry is a character |
18254 | 242 or a one-character string, it is added to the mode's syntax table with |
243 comment-start syntax. If the entry is a cons pair, the elements of the | |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
244 pair are considered to be comment-start and comment-end respectively. |
18254 | 245 Note that Emacs has limitations regarding comment characters. |
246 | |
247 KEYWORD-LIST is a list of keywords to highlight with `font-lock-keyword-face'. | |
248 Each keyword should be a string. | |
249 | |
250 FONT-LOCK-LIST is a list of additional expressions to highlight. Each entry | |
251 in the list should have the same form as an entry in `font-lock-defaults-alist' | |
252 | |
253 AUTO-MODE-LIST is a list of regular expressions to add to auto-mode-alist. | |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
254 These regexps are added to auto-mode-alist as soon as `define-generic-mode' |
21014 | 255 is called; any old regexps with the same name are removed. |
18254 | 256 |
257 FUNCTION-LIST is a list of functions to call to do some additional setup. | |
258 | |
21014 | 259 See the file generic-x.el for some examples of `define-generic-mode'." |
18254 | 260 |
261 ;; Basic sanity check | |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
262 (generic-mode-sanity-check name |
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
263 comment-list keyword-list font-lock-list |
18254 | 264 auto-mode-list function-list description) |
265 | |
266 ;; Remove any old entry | |
267 (setq generic-mode-alist | |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
268 (delq (assq name generic-mode-alist) |
18254 | 269 generic-mode-alist)) |
270 | |
271 ;; Add a new entry | |
272 (setq generic-mode-alist | |
273 (append | |
274 (list | |
275 (list | |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
276 name comment-list keyword-list font-lock-list |
18254 | 277 auto-mode-list function-list |
278 )) | |
279 generic-mode-alist)) | |
280 | |
281 ;; Add it to auto-mode-alist | |
282 (generic-add-to-auto-mode name auto-mode-list t) | |
283 | |
284 ;; Define a function for it | |
285 (generic-create-generic-function name description) | |
286 ) | |
287 | |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
288 (defun generic-add-to-auto-mode (mode auto-mode-list |
18254 | 289 &optional remove-old prepend) |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
290 "Add the entries for MODE to `auto-mode-alist', supplied as AUTO-MODE-ALIST. |
18254 | 291 If remove-old is non-nil, removes old entries first. If prepend is |
292 non-nil, prepends entries to auto-mode-alist; otherwise, appends them." | |
293 | |
294 (if (not (listp auto-mode-list)) | |
295 (error "%s is not a list" (princ auto-mode-list))) | |
296 | |
297 (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
|
298 (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
|
299 (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
|
300 (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
|
301 (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
|
302 (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
|
303 auto-mode-alist))))) |
18254 | 304 |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
305 (mapcar '(lambda (entry) |
18254 | 306 (generic-add-auto-mode-entry new-mode entry prepend)) |
307 auto-mode-list))) | |
308 | |
309 (defun generic-add-auto-mode-entry (name entry &optional prepend) | |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
310 "Add a new NAME regexp with ENTRY to the end of `auto-mode-alist'. |
18254 | 311 If prepend is non-nil, add the entry to the front of the list." |
312 (let ((new-entry (list (cons entry name)))) | |
313 (setq auto-mode-alist | |
314 (if prepend | |
315 (append new-entry auto-mode-alist) | |
316 (append auto-mode-alist new-entry))))) | |
317 | |
318 (defun generic-create-generic-function (name &optional description) | |
319 "Create a generic mode function with NAME. | |
320 If DESCRIPTION is provided, it is used as the docstring." | |
321 (let ((symname (symbol-name name))) | |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
322 ;; Use `defalias', not `fset' to make the mode appear on |
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
323 ;; load-history. |
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
324 (defalias (intern symname) |
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
325 (list 'lambda nil |
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
326 (or description |
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
327 (concat "Generic mode for type " symname)) |
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
328 (list 'interactive) |
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
329 (list 'generic-mode-with-type (list 'quote name)))))) |
18254 | 330 |
331 (defun generic-mode-with-type (&optional mode) | |
332 "Go into the generic-mode MODE." | |
333 (let* ((type (or mode generic-mode-name)) | |
334 (generic-mode-list (assoc type generic-mode-alist)) | |
23392
d86ad410d285
(generic-mode-with-type): Added hooks for generic-modes.
Karl Heuer <kwzh@gnu.org>
parents:
22402
diff
changeset
|
335 (generic-mode-hooks (intern (concat (symbol-name type) "-hooks"))) |
18254 | 336 ) |
337 | |
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
|
338 (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
|
339 (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
|
340 (princ generic-mode-name))) |
18254 | 341 |
342 ;; Put this after the point where we read generic-mode-name! | |
343 (kill-all-local-variables) | |
344 | |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
345 (setq |
18254 | 346 generic-mode-name type |
347 generic-comment-list (nth 1 generic-mode-list) | |
348 generic-keywords-list (nth 2 generic-mode-list) | |
349 generic-font-lock-expressions (nth 3 generic-mode-list) | |
350 generic-mode-function-list (nth 5 generic-mode-list) | |
22402
bb03a020d02c
*** empty log message ***
Dan Nicolaescu <done@ece.arizona.edu>
parents:
22347
diff
changeset
|
351 major-mode type |
18254 | 352 mode-name (symbol-name type) |
353 ) | |
354 | |
355 (generic-mode-set-comments generic-comment-list) | |
356 | |
357 ;; Font-lock functionality | |
358 ;; Font-lock-defaults are always set even if there are no keywords | |
359 ;; or font-lock expressions, so comments can be highlighted. | |
360 (setq generic-font-lock-defaults nil) | |
361 (generic-mode-set-font-lock generic-keywords-list | |
362 generic-font-lock-expressions) | |
363 (make-local-variable 'font-lock-defaults) | |
364 (setq font-lock-defaults (list 'generic-font-lock-defaults nil)) | |
365 | |
366 ;; 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
|
367 (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
|
368 (mapcar 'funcall generic-mode-function-list)) |
23392
d86ad410d285
(generic-mode-with-type): Added hooks for generic-modes.
Karl Heuer <kwzh@gnu.org>
parents:
22402
diff
changeset
|
369 |
d86ad410d285
(generic-mode-with-type): Added hooks for generic-modes.
Karl Heuer <kwzh@gnu.org>
parents:
22402
diff
changeset
|
370 (run-hooks generic-mode-hooks) |
18254 | 371 ) |
372 ) | |
373 | |
374 ;;;###autoload | |
375 (defun generic-mode (type) | |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
376 "Basic comment and font-lock functionality for `generic' files. |
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
377 (Files which are too small to warrant their own mode, but have |
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
378 comment characters, keywords, and the like.) |
18254 | 379 |
380 To define a generic-mode, use the function `define-generic-mode'. | |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
381 Some generic modes are defined in `generic-x.el'." |
18254 | 382 (interactive |
383 (list (generic-read-type))) | |
384 (generic-mode-with-type (intern type))) | |
385 | |
386 ;;; Comment Functionality | |
387 (defun generic-mode-set-comments (comment-list) | |
388 "Set up comment functionality for generic mode." | |
389 (if (null comment-list) | |
390 nil | |
391 (let ((generic-mode-syntax-table (make-syntax-table))) | |
392 (make-local-variable 'comment-start) | |
393 (make-local-variable 'comment-start-skip) | |
394 (make-local-variable 'comment-end) | |
395 (mapcar 'generic-mode-set-a-comment comment-list) | |
396 (set-syntax-table generic-mode-syntax-table)))) | |
397 | |
398 (defun generic-mode-set-a-comment (comment) | |
399 (and (char-or-string-p comment) | |
400 (if (stringp comment) | |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
401 (cond |
18254 | 402 ((eq (length comment) 1) |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
403 (generic-mode-set-comment-char |
18254 | 404 (string-to-char comment))) |
405 ((eq (length comment) 2) | |
406 (generic-mode-set-comment-string comment)) | |
407 (t | |
408 (error "Character string %s must be one or two characters long" | |
409 comment)) | |
410 ) | |
411 (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
|
412 (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
|
413 (generic-mode-set-comment-pair comment))) |
18254 | 414 |
415 (defun generic-mode-set-comment-char (comment-char) | |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
416 "Set COMMENT-CHAR as a comment character for generic mode." |
18254 | 417 (if (not comment-char) |
418 nil | |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
419 (setq |
18254 | 420 comment-end "" |
421 comment-start (char-to-string comment-char) | |
422 comment-start-skip (concat comment-start "+ *") | |
423 ) | |
424 | |
425 (modify-syntax-entry comment-char "<" | |
426 generic-mode-syntax-table) | |
427 (modify-syntax-entry ?\n ">" | |
428 generic-mode-syntax-table))) | |
429 | |
430 (defun generic-mode-set-comment-string (comment-string) | |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
431 "Set COMMENT-STRING as a comment string for generic mode." |
18254 | 432 (if (not comment-string) |
433 nil | |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
434 (setq |
18254 | 435 comment-end "" |
436 comment-start comment-string | |
437 comment-start-skip (concat comment-start " *") | |
438 ) | |
439 | |
440 (let ((first (elt comment-string 0)) | |
441 (second (elt comment-string 1))) | |
442 ;; C++ style comments | |
443 (if (char-equal first second) | |
444 (progn | |
445 (modify-syntax-entry first "<12b" | |
446 generic-mode-syntax-table) | |
447 (modify-syntax-entry ?\n ">b" | |
448 generic-mode-syntax-table))) | |
449 ;; Some other two character string | |
450 (modify-syntax-entry first "<1" | |
451 generic-mode-syntax-table) | |
452 (modify-syntax-entry second "<2" | |
453 generic-mode-syntax-table) | |
454 (modify-syntax-entry ?\n ">" | |
455 generic-mode-syntax-table)))) | |
456 | |
457 (defun generic-mode-set-comment-pair (comment-pair) | |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
458 "Set COMMENT-PAIR as a comment start and end for generic mode." |
18254 | 459 (let ((generic-comment-start (car comment-pair)) |
460 (generic-comment-end (cdr comment-pair)) | |
461 ) | |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
462 (setq |
18254 | 463 comment-end generic-comment-end |
464 comment-start generic-comment-start | |
465 comment-start-skip (concat generic-comment-start " *") | |
466 ) | |
467 | |
468 ;; 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
|
469 (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
|
470 (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
|
471 (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
|
472 (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
|
473 (equal (length generic-comment-end) 2))) |
18254 | 474 (error "Start and end must be exactly two characters long")) |
475 | |
476 (let ((first (elt generic-comment-start 0)) | |
477 (second (elt generic-comment-start 1)) | |
478 (third (elt generic-comment-end 0)) | |
479 (fourth (elt generic-comment-end 1)) | |
480 ) | |
481 | |
482 (modify-syntax-entry first ". 1" generic-mode-syntax-table) | |
483 (modify-syntax-entry second ". 2" generic-mode-syntax-table) | |
484 | |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
485 (modify-syntax-entry |
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
486 third |
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
487 (concat |
18254 | 488 "." |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
489 (cond |
18254 | 490 ((char-equal first third) " 13") |
491 ((char-equal second third) " 23") | |
492 (t " 3")) | |
493 ) | |
494 generic-mode-syntax-table) | |
495 | |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
496 (modify-syntax-entry |
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
497 fourth |
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
498 (concat |
18254 | 499 "." |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
500 (cond |
18254 | 501 ((char-equal first fourth) " 14") |
502 ((char-equal second fourth) " 24") | |
503 (t " 4")) | |
504 ) | |
505 generic-mode-syntax-table) | |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
506 ))) |
18254 | 507 |
508 (defun generic-mode-set-font-lock (keywords font-lock-expressions) | |
509 "Set up font-lock functionality for generic mode." | |
510 (let ((generic-font-lock-expressions)) | |
511 ;; 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
|
512 (and keywords |
18254 | 513 (setq |
514 generic-font-lock-expressions | |
515 (append | |
21879
14316c9ecdac
(generic): Added defgroup declaration.
Richard M. Stallman <rms@gnu.org>
parents:
21182
diff
changeset
|
516 (list (let ((regexp (regexp-opt keywords))) |
14316c9ecdac
(generic): Added defgroup declaration.
Richard M. Stallman <rms@gnu.org>
parents:
21182
diff
changeset
|
517 (list (concat "\\<\\(" regexp "\\)\\>") |
14316c9ecdac
(generic): Added defgroup declaration.
Richard M. Stallman <rms@gnu.org>
parents:
21182
diff
changeset
|
518 1 |
14316c9ecdac
(generic): Added defgroup declaration.
Richard M. Stallman <rms@gnu.org>
parents:
21182
diff
changeset
|
519 'font-lock-keyword-face))) |
18254 | 520 generic-font-lock-expressions))) |
521 ;; 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
|
522 (and font-lock-expressions |
18254 | 523 (setq generic-font-lock-expressions |
524 (append | |
525 font-lock-expressions | |
526 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
|
527 (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
|
528 (setq generic-font-lock-defaults generic-font-lock-expressions)) |
18254 | 529 )) |
530 | |
531 ;; Support for [KEYWORD] constructs found in INF, INI and Samba files | |
532 (defun generic-bracket-support () | |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
533 (setq imenu-generic-expression |
20459 | 534 '((nil "^\\[\\(.*\\)\\]" 1)) |
535 imenu-case-fold-search t)) | |
18254 | 536 |
537 ;; This generic mode is always defined | |
538 (define-generic-mode 'default-generic-mode (list ?#) nil nil nil nil) | |
539 | |
540 ;; A more general solution would allow us to enter generic-mode for | |
541 ;; *any* comment character, but would require us to synthesize a new | |
542 ;; generic-mode on the fly. I think this gives us most of what we | |
543 ;; want. | |
544 (defun generic-mode-find-file-hook () | |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
545 "Hook function to enter default-generic-mode automatically. |
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
546 Done if the first few lines of a file in `fundamental-mode' start with |
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
547 a hash comment character. This hook will be installed if the variable |
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
548 `generic-use-find-file-hook' is non-nil. The variable |
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
549 `generic-lines-to-scan' determines the number of lines to look at." |
18254 | 550 (if (not (eq major-mode 'fundamental-mode)) |
551 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
|
552 (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
|
553 (< 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
|
554 (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
|
555 " positive number")) |
18254 | 556 (let ((comment-regexp "") |
557 (count 0) | |
558 ) | |
559 (while (< count generic-lines-to-scan) | |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
560 (setq comment-regexp (concat comment-regexp |
18254 | 561 generic-find-file-regexp)) |
562 (setq count (1+ count))) | |
563 (save-excursion | |
564 (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
|
565 (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
|
566 (generic-mode-with-type 'default-generic-mode)))))) |
18254 | 567 |
568 (defun generic-mode-ini-file-find-file-hook () | |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
569 "Hook function to enter default-generic-mode automatically for INI files. |
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
570 Done if the first few lines of a file in `fundamental-mode' look like an |
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
571 INI file. 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
|
572 (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
|
573 (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
|
574 (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
|
575 (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
|
576 (generic-mode-with-type 'ini-generic-mode))))) |
18254 | 577 |
578 (and generic-use-find-file-hook | |
579 (add-hook 'find-file-hooks 'generic-mode-find-file-hook)) | |
580 | |
581 (defun generic-make-keywords-list (keywords-list face &optional prefix suffix) | |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
582 "Return a regular expression matching the specified KEYWORDS-LIST. |
18254 | 583 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
|
584 (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
|
585 (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
|
586 (list (concat (or prefix "") |
14316c9ecdac
(generic): Added defgroup declaration.
Richard M. Stallman <rms@gnu.org>
parents:
21182
diff
changeset
|
587 "\\<\\(" |
14316c9ecdac
(generic): Added defgroup declaration.
Richard M. Stallman <rms@gnu.org>
parents:
21182
diff
changeset
|
588 ;; Use an optimized regexp. |
14316c9ecdac
(generic): Added defgroup declaration.
Richard M. Stallman <rms@gnu.org>
parents:
21182
diff
changeset
|
589 (regexp-opt keywords-list t) |
14316c9ecdac
(generic): Added defgroup declaration.
Richard M. Stallman <rms@gnu.org>
parents:
21182
diff
changeset
|
590 "\\)\\>" |
14316c9ecdac
(generic): Added defgroup declaration.
Richard M. Stallman <rms@gnu.org>
parents:
21182
diff
changeset
|
591 (or suffix "")) |
14316c9ecdac
(generic): Added defgroup declaration.
Richard M. Stallman <rms@gnu.org>
parents:
21182
diff
changeset
|
592 1 |
21928
42fa0da05721
(generic-make-keywords-list): Delete spurious paren.
Richard M. Stallman <rms@gnu.org>
parents:
21879
diff
changeset
|
593 face)) |
18254 | 594 |
21013
adb28ace7f33
Provide generic, not generic-mode.
Richard M. Stallman <rms@gnu.org>
parents:
20459
diff
changeset
|
595 (provide 'generic) |
18254 | 596 |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
597 ;;; generic.el ends here |