Mercurial > emacs
annotate lisp/generic.el @ 59778:f378841c3724
*** empty log message ***
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Sat, 29 Jan 2005 17:16:47 +0000 |
parents | 275bfc016770 |
children | b392b963c10b f2ebccfa87d4 |
rev | line source |
---|---|
38436
b174db545cfd
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
37058
diff
changeset
|
1 ;;; generic.el --- defining simple major modes with comment and font-lock |
18254 | 2 ;; |
55555
7cb75f6a290a
(define-generic-mode): Remove redundant arglist info.
Juanma Barranquero <lekktu@gmail.com>
parents:
53401
diff
changeset
|
3 ;; Copyright (C) 1997, 1999, 2004 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: |
32120 | 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 |
53401
3cf88b19f762
(define-generic-mode): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
57 ;; as those in `font-lock-keywords'. |
32120 | 58 ;; |
18254 | 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. | |
32120 | 76 ;; |
18254 | 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 | |
32120 | 87 ;; (list "\\.FOO\\'") |
18254 | 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. |
32120 | 105 ;; |
18254 | 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. | |
32120 | 111 ;; |
18254 | 112 |
21057 | 113 ;; Credit for suggestions, brainstorming, help with debugging: |
18254 | 114 ;; ACorreir@pervasive-sw.com (Alfred Correira) |
32120 | 115 ;; Extensive cleanup by: |
116 ;; Stefan Monnier (monnier+gnu/emacs@flint.cs.yale.edu) | |
117 ;; | |
118 ;;; Code: | |
18254 | 119 |
32120 | 120 (eval-when-compile |
121 (require 'cl)) | |
18254 | 122 |
123 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
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
|
124 ;; Internal Variables |
18254 | 125 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
126 | |
127 (defvar generic-font-lock-defaults nil | |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
128 "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
|
129 (make-variable-buffer-local 'generic-font-lock-defaults) |
18254 | 130 |
32120 | 131 (defvar generic-mode-list nil |
132 "A list of mode names for `generic-mode'. | |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
133 Do not add entries to this list directly; use `define-generic-mode' |
18254 | 134 instead (which see).") |
135 | |
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
|
136 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
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
|
137 ;; 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
|
138 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
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
|
139 |
21879
14316c9ecdac
(generic): Added defgroup declaration.
Richard M. Stallman <rms@gnu.org>
parents:
21182
diff
changeset
|
140 (defgroup generic nil |
14316c9ecdac
(generic): Added defgroup declaration.
Richard M. Stallman <rms@gnu.org>
parents:
21182
diff
changeset
|
141 "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
|
142 :prefix "generic-" |
14316c9ecdac
(generic): Added defgroup declaration.
Richard M. Stallman <rms@gnu.org>
parents:
21182
diff
changeset
|
143 :group 'extensions) |
14316c9ecdac
(generic): Added defgroup declaration.
Richard M. Stallman <rms@gnu.org>
parents:
21182
diff
changeset
|
144 |
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
|
145 (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
|
146 "*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
|
147 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
|
148 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
|
149 :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
|
150 :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
|
151 ) |
18254 | 152 |
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
|
153 (defcustom generic-lines-to-scan 3 |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
154 "*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
|
155 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
|
156 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
|
157 :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
|
158 :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
|
159 ) |
18254 | 160 |
32120 | 161 (defcustom generic-find-file-regexp "^#" |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
162 "*Regular expression used by `generic-mode-find-file-hook'. |
36124
5eb6597319ec
(generic-find-file-regexp): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
34814
diff
changeset
|
163 Files in fundamental mode whose first few lines contain a match for |
5eb6597319ec
(generic-find-file-regexp): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
34814
diff
changeset
|
164 this regexp, should be put into `default-generic-mode' instead. |
5eb6597319ec
(generic-find-file-regexp): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
34814
diff
changeset
|
165 The number of lines tested for the matches is specified by the value |
5eb6597319ec
(generic-find-file-regexp): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
34814
diff
changeset
|
166 of the variable `generic-lines-to-scan', which see." |
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
|
167 :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
|
168 :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
|
169 ) |
18254 | 170 |
36124
5eb6597319ec
(generic-find-file-regexp): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
34814
diff
changeset
|
171 (defcustom generic-ignore-files-regexp "[Tt][Aa][Gg][Ss]\\'" |
5eb6597319ec
(generic-find-file-regexp): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
34814
diff
changeset
|
172 "*Regular expression used by `generic-mode-find-file-hook'. |
5eb6597319ec
(generic-find-file-regexp): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
34814
diff
changeset
|
173 Files whose names match this regular expression should not be put |
5eb6597319ec
(generic-find-file-regexp): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
34814
diff
changeset
|
174 into `default-generic-mode', even if they have lines which match the |
5eb6597319ec
(generic-find-file-regexp): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
34814
diff
changeset
|
175 regexp in `generic-find-file-regexp'. If the value is nil, |
5eb6597319ec
(generic-find-file-regexp): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
34814
diff
changeset
|
176 `generic-mode-find-file-hook' does not check the file names." |
5eb6597319ec
(generic-find-file-regexp): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
34814
diff
changeset
|
177 :group 'generic |
5eb6597319ec
(generic-find-file-regexp): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
34814
diff
changeset
|
178 :type '(choice (const :tag "Don't check file names" nil) regexp) |
5eb6597319ec
(generic-find-file-regexp): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
34814
diff
changeset
|
179 ) |
5eb6597319ec
(generic-find-file-regexp): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
34814
diff
changeset
|
180 |
18254 | 181 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
182 ;; Functions | |
183 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
184 | |
22347
f53740d7d40d
*** empty log message ***
Dan Nicolaescu <done@ece.arizona.edu>
parents:
21928
diff
changeset
|
185 ;;;###autoload |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
186 (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
|
187 auto-mode-list function-list |
18254 | 188 &optional description) |
189 "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
|
190 |
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
191 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
|
192 name. If DESCRIPTION is provided, it is used as the docstring for the new |
18254 | 193 function. |
194 | |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
195 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
|
196 a one or two character string or a cons pair. If the entry is a character |
18254 | 197 or a one-character string, it is added to the mode's syntax table with |
32120 | 198 `comment-start' syntax. If the entry is a cons pair, the elements of the |
199 pair are considered to be `comment-start' and `comment-end' respectively. | |
58587
275bfc016770
(define-generic-mode): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
58473
diff
changeset
|
200 \(The latter should be nil if you want comments to end at end of line.) |
18254 | 201 Note that Emacs has limitations regarding comment characters. |
202 | |
203 KEYWORD-LIST is a list of keywords to highlight with `font-lock-keyword-face'. | |
204 Each keyword should be a string. | |
205 | |
206 FONT-LOCK-LIST is a list of additional expressions to highlight. Each entry | |
53401
3cf88b19f762
(define-generic-mode): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
207 in the list should have the same form as an entry in `font-lock-keywords'. |
18254 | 208 |
32120 | 209 AUTO-MODE-LIST is a list of regular expressions to add to `auto-mode-alist'. |
210 These regexps are added to `auto-mode-alist' as soon as `define-generic-mode' | |
21014 | 211 is called; any old regexps with the same name are removed. |
18254 | 212 |
213 FUNCTION-LIST is a list of functions to call to do some additional setup. | |
214 | |
21014 | 215 See the file generic-x.el for some examples of `define-generic-mode'." |
18254 | 216 |
217 ;; Add a new entry | |
32120 | 218 (unless (assq name generic-mode-list) |
34814
ce9ed5ad8fa2
(generic-read-type): Undo last change, inline into
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
34793
diff
changeset
|
219 (push (list (symbol-name name)) generic-mode-list)) |
18254 | 220 |
221 ;; Add it to auto-mode-alist | |
32120 | 222 (dolist (re auto-mode-list) |
223 (add-to-list 'auto-mode-alist (cons re name))) | |
224 | |
225 ;; Define a function for it using `defalias' (not `fset') to make | |
226 ;; the mode appear on load-history. | |
227 (defalias name | |
228 `(lambda nil | |
229 ,(or description (concat "Generic mode for type " (symbol-name name))) | |
230 (interactive) | |
231 (generic-mode-internal ',name ',comment-list ',keyword-list | |
232 ',font-lock-list ',function-list))) | |
18254 | 233 ) |
234 | |
32120 | 235 (defun generic-mode-internal (mode comments keywords font-lock-list funs) |
18254 | 236 "Go into the generic-mode MODE." |
32120 | 237 (let* ((generic-mode-hooks (intern (concat (symbol-name mode) "-hook"))) |
238 (modename (symbol-name mode)) | |
239 (name (if (string-match "-mode\\'" modename) | |
240 (substring modename 0 (match-beginning 0)) | |
241 modename)) | |
18254 | 242 ) |
243 | |
244 ;; Put this after the point where we read generic-mode-name! | |
245 (kill-all-local-variables) | |
246 | |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
247 (setq |
32120 | 248 major-mode mode |
249 mode-name (capitalize name) | |
18254 | 250 ) |
251 | |
32120 | 252 (generic-mode-set-comments comments) |
18254 | 253 |
254 ;; Font-lock functionality | |
255 ;; Font-lock-defaults are always set even if there are no keywords | |
256 ;; or font-lock expressions, so comments can be highlighted. | |
257 (setq generic-font-lock-defaults nil) | |
32120 | 258 (generic-mode-set-font-lock keywords font-lock-list) |
18254 | 259 (make-local-variable 'font-lock-defaults) |
260 (setq font-lock-defaults (list 'generic-font-lock-defaults nil)) | |
261 | |
262 ;; Call a list of functions | |
32120 | 263 (mapcar 'funcall funs) |
23392
d86ad410d285
(generic-mode-with-type): Added hooks for generic-modes.
Karl Heuer <kwzh@gnu.org>
parents:
22402
diff
changeset
|
264 |
d86ad410d285
(generic-mode-with-type): Added hooks for generic-modes.
Karl Heuer <kwzh@gnu.org>
parents:
22402
diff
changeset
|
265 (run-hooks generic-mode-hooks) |
18254 | 266 ) |
267 ) | |
268 | |
269 ;;;###autoload | |
270 (defun generic-mode (type) | |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
271 "Basic comment and font-lock functionality for `generic' files. |
32120 | 272 \(Files which are too small to warrant their own mode, but have |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
273 comment characters, keywords, and the like.) |
18254 | 274 |
275 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
|
276 Some generic modes are defined in `generic-x.el'." |
18254 | 277 (interactive |
34814
ce9ed5ad8fa2
(generic-read-type): Undo last change, inline into
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
34793
diff
changeset
|
278 (list (completing-read "Generic Type: " generic-mode-list nil t))) |
32120 | 279 (funcall (intern type))) |
18254 | 280 |
281 ;;; Comment Functionality | |
282 (defun generic-mode-set-comments (comment-list) | |
283 "Set up comment functionality for generic mode." | |
32120 | 284 (let ((st (make-syntax-table)) |
285 (chars nil) | |
286 (comstyles)) | |
287 (make-local-variable 'comment-start) | |
288 (make-local-variable 'comment-start-skip) | |
289 (make-local-variable 'comment-end) | |
18254 | 290 |
32120 | 291 ;; Go through all the comments |
292 (dolist (start comment-list) | |
58473
9aacf8e5ae58
(generic-mode-set-comments): Accept an empty comment-end.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
57113
diff
changeset
|
293 (let ((end nil) (comstyle "")) |
32120 | 294 ;; Normalize |
295 (when (consp start) | |
296 (setq end (or (cdr start) end)) | |
297 (setq start (car start))) | |
298 (when (char-valid-p start) (setq start (char-to-string start))) | |
58473
9aacf8e5ae58
(generic-mode-set-comments): Accept an empty comment-end.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
57113
diff
changeset
|
299 (cond |
9aacf8e5ae58
(generic-mode-set-comments): Accept an empty comment-end.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
57113
diff
changeset
|
300 ((char-valid-p end) (setq end (char-to-string end))) |
9aacf8e5ae58
(generic-mode-set-comments): Accept an empty comment-end.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
57113
diff
changeset
|
301 ((zerop (length end)) (setq end "\n"))) |
18254 | 302 |
32120 | 303 ;; Setup the vars for `comment-region' |
304 (if comment-start | |
305 ;; We have already setup a comment-style, so use style b | |
306 (progn | |
307 (setq comstyle "b") | |
308 (setq comment-start-skip | |
309 (concat comment-start-skip "\\|" (regexp-quote start) "+\\s-*"))) | |
310 ;; First comment-style | |
311 (setq comment-start start) | |
37058
a2e5f00c1254
(generic-mode-set-comments): Use "" rather than nil for comment-end.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
36124
diff
changeset
|
312 (setq comment-end (if (string-equal end "\n") "" end)) |
32120 | 313 (setq comment-start-skip (concat (regexp-quote start) "+\\s-*"))) |
18254 | 314 |
32120 | 315 ;; Reuse comstyles if necessary |
316 (setq comstyle | |
317 (or (cdr (assoc start comstyles)) | |
318 (cdr (assoc end comstyles)) | |
319 comstyle)) | |
320 (push (cons start comstyle) comstyles) | |
321 (push (cons end comstyle) comstyles) | |
18254 | 322 |
32120 | 323 ;; Setup the syntax table |
324 (if (= (length start) 1) | |
325 (modify-syntax-entry (string-to-char start) | |
326 (concat "< " comstyle) st) | |
327 (let ((c0 (elt start 0)) (c1 (elt start 1))) | |
328 ;; Store the relevant info but don't update yet | |
329 (push (cons c0 (concat (cdr (assoc c0 chars)) "1")) chars) | |
330 (push (cons c1 (concat (cdr (assoc c1 chars)) | |
331 (concat "2" comstyle))) chars))) | |
332 (if (= (length end) 1) | |
333 (modify-syntax-entry (string-to-char end) | |
334 (concat ">" comstyle) st) | |
335 (let ((c0 (elt end 0)) (c1 (elt end 1))) | |
336 ;; Store the relevant info but don't update yet | |
337 (push (cons c0 (concat (cdr (assoc c0 chars)) | |
338 (concat "3" comstyle))) chars) | |
339 (push (cons c1 (concat (cdr (assoc c1 chars)) "4")) chars))))) | |
18254 | 340 |
32120 | 341 ;; Process the chars that were part of a 2-char comment marker |
342 (dolist (cs (nreverse chars)) | |
343 (modify-syntax-entry (car cs) | |
344 (concat (char-to-string (char-syntax (car cs))) | |
345 " " (cdr cs)) | |
346 st)) | |
347 (set-syntax-table st))) | |
18254 | 348 |
349 (defun generic-mode-set-font-lock (keywords font-lock-expressions) | |
350 "Set up font-lock functionality for generic mode." | |
32120 | 351 (setq generic-font-lock-defaults |
352 (append | |
353 (when keywords | |
354 (list (generic-make-keywords-list keywords font-lock-keyword-face))) | |
355 font-lock-expressions))) | |
18254 | 356 |
357 ;; Support for [KEYWORD] constructs found in INF, INI and Samba files | |
358 (defun generic-bracket-support () | |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
359 (setq imenu-generic-expression |
20459 | 360 '((nil "^\\[\\(.*\\)\\]" 1)) |
361 imenu-case-fold-search t)) | |
18254 | 362 |
363 ;; This generic mode is always defined | |
364 (define-generic-mode 'default-generic-mode (list ?#) nil nil nil nil) | |
365 | |
366 ;; A more general solution would allow us to enter generic-mode for | |
367 ;; *any* comment character, but would require us to synthesize a new | |
368 ;; generic-mode on the fly. I think this gives us most of what we | |
369 ;; want. | |
370 (defun generic-mode-find-file-hook () | |
32120 | 371 "Hook function to enter `default-generic-mode' automatically. |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
372 Done if the first few lines of a file in `fundamental-mode' start with |
36124
5eb6597319ec
(generic-find-file-regexp): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
34814
diff
changeset
|
373 a match for the regexp in `generic-find-file-regexp', unless the |
5eb6597319ec
(generic-find-file-regexp): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
34814
diff
changeset
|
374 file's name matches the regexp which is the value of the variable |
5eb6597319ec
(generic-find-file-regexp): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
34814
diff
changeset
|
375 `generic-ignore-files-regexp'. |
5eb6597319ec
(generic-find-file-regexp): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
34814
diff
changeset
|
376 This hook will be installed if the variable |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
377 `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
|
378 `generic-lines-to-scan' determines the number of lines to look at." |
36124
5eb6597319ec
(generic-find-file-regexp): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
34814
diff
changeset
|
379 (when (and (eq major-mode 'fundamental-mode) |
5eb6597319ec
(generic-find-file-regexp): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
34814
diff
changeset
|
380 (or (null generic-ignore-files-regexp) |
5eb6597319ec
(generic-find-file-regexp): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
34814
diff
changeset
|
381 (not (string-match |
5eb6597319ec
(generic-find-file-regexp): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
34814
diff
changeset
|
382 generic-ignore-files-regexp |
5eb6597319ec
(generic-find-file-regexp): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
34814
diff
changeset
|
383 (file-name-sans-versions buffer-file-name))))) |
32120 | 384 (save-excursion |
385 (goto-char (point-min)) | |
386 (when (re-search-forward generic-find-file-regexp | |
387 (save-excursion | |
388 (forward-line generic-lines-to-scan) | |
389 (point)) t) | |
18254 | 390 (goto-char (point-min)) |
32120 | 391 (default-generic-mode))))) |
18254 | 392 |
393 (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
|
394 "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
|
395 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
|
396 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
|
397 (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
|
398 (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
|
399 (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
|
400 (and (looking-at "^\\s-*\\[.*\\]") |
32120 | 401 (ini-generic-mode))))) |
18254 | 402 |
403 (and generic-use-find-file-hook | |
404 (add-hook 'find-file-hooks 'generic-mode-find-file-hook)) | |
405 | |
406 (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
|
407 "Return a regular expression matching the specified KEYWORDS-LIST. |
18254 | 408 The regexp is highlighted with FACE." |
32120 | 409 (unless (listp keywords-list) |
410 (error "Keywords argument must be a list of strings")) | |
57113
5bd3a1e1dfe5
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-549
Miles Bader <miles@gnu.org>
parents:
55555
diff
changeset
|
411 (list (concat prefix "\\_<" |
21879
14316c9ecdac
(generic): Added defgroup declaration.
Richard M. Stallman <rms@gnu.org>
parents:
21182
diff
changeset
|
412 ;; Use an optimized regexp. |
14316c9ecdac
(generic): Added defgroup declaration.
Richard M. Stallman <rms@gnu.org>
parents:
21182
diff
changeset
|
413 (regexp-opt keywords-list t) |
57113
5bd3a1e1dfe5
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-549
Miles Bader <miles@gnu.org>
parents:
55555
diff
changeset
|
414 "\\_>" suffix) |
21879
14316c9ecdac
(generic): Added defgroup declaration.
Richard M. Stallman <rms@gnu.org>
parents:
21182
diff
changeset
|
415 1 |
21928
42fa0da05721
(generic-make-keywords-list): Delete spurious paren.
Richard M. Stallman <rms@gnu.org>
parents:
21879
diff
changeset
|
416 face)) |
18254 | 417 |
21013
adb28ace7f33
Provide generic, not generic-mode.
Richard M. Stallman <rms@gnu.org>
parents:
20459
diff
changeset
|
418 (provide 'generic) |
18254 | 419 |
58473
9aacf8e5ae58
(generic-mode-set-comments): Accept an empty comment-end.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
57113
diff
changeset
|
420 ;; arch-tag: 239c1fc4-1303-48d9-9ac0-657d655669ea |
24022
218b7edb9baf
Re-write `(make-variable-buffer-local (defvar ...'
Dave Love <fx@gnu.org>
parents:
23392
diff
changeset
|
421 ;;; generic.el ends here |