1 ;;; generic.el --- Defining simple major modes with comment and font-lock.
3 ;; Copyright (C) 1997, 1999 Free Software Foundation, Inc.
5 ;; Author: Peter Breton <pbreton@cs.umb.edu>
6 ;; Created: Fri Sep 27 1996
7 ;; Keywords: generic, comment, font-lock
9 ;; This file is part of GNU Emacs.
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)
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.
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.
28 ;; Meta-mode to create simple major modes
29 ;; with basic comment and font-lock support
35 ;; Generic-mode is a meta-mode which can be used to define small modes
36 ;; which provide basic comment and font-lock support. These modes are
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
41 ;; Each generic mode can define the following:
43 ;; * List of comment-characters. The entries in this list should be
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.
49 ;; * List of keywords to font-lock. Each keyword should be a string.
50 ;; If you have additional keywords which should be highlighted in a face
51 ;; different from `font-lock-keyword-face', you can use the convenience
52 ;; function `generic-make-keywords-list' (which see), and add the
53 ;; result to the following list:
55 ;; * Additional expressions to font-lock. This should be a list of
56 ;; expressions, each of which should be of the same form
57 ;; as those in `font-lock-defaults-alist'.
59 ;; * List of regular expressions to be placed in auto-mode-alist.
61 ;; * List of functions to call to do some additional setup
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!
69 ;; To put a file into generic mode using local variables, use a line
70 ;; like this in a Local Variables block:
72 ;; mode: default-generic
74 ;; Do NOT use "mode: generic"!
75 ;; See also "AUTOMATICALLY ENTERING GENERIC MODE" below.
77 ;; DEFINING NEW GENERIC MODES:
79 ;; Use the `define-generic-mode' function to define new modes.
83 ;; (define-generic-mode 'foo-generic-mode
88 ;; (list 'foo-setup-function))
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
92 ;; '.FOO' are loaded, Emacs will go into foo-generic-mode and call
93 ;; foo-setup-function. You can also use the function `foo-generic-mode'
94 ;; (which is interactive) to put a buffer into foo-generic-mode.
96 ;; AUTOMATICALLY ENTERING GENERIC MODE:
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
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
108 ;; Be careful that your font-lock definitions are correct. Getting them
109 ;; wrong can cause emacs to continually attempt to fontify! This problem
110 ;; is not specific to generic-mode.
113 ;; Credit for suggestions, brainstorming, help with debugging:
114 ;; ACorreir@pervasive-sw.com (Alfred Correira)
115 ;; Extensive cleanup by:
116 ;; Stefan Monnier (monnier+gnu/emacs@flint.cs.yale.edu)
123 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
124 ;; Internal Variables
125 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
127 (defvar generic-font-lock-defaults nil
128 "Global defaults for font-lock in a generic mode.")
129 (make-variable-buffer-local 'generic-font-lock-defaults
)
131 (defvar generic-mode-list nil
132 "A list of mode names for `generic-mode'.
133 Do not add entries to this list directly; use `define-generic-mode'
134 instead (which see).")
136 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
137 ;; Customization Variables
138 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
140 (defgroup generic nil
141 "Define simple major modes with comment and font-lock support."
145 (defcustom generic-use-find-file-hook t
146 "*If non-nil, add a hook to enter default-generic-mode automatically.
147 This is done if the first few lines of a file in fundamental mode start
148 with a hash comment character."
153 (defcustom generic-lines-to-scan
3
154 "*Number of lines that `generic-mode-find-file-hook' looks at.
155 Relevant when deciding whether to enter `generic-mode' automatically.
156 This variable should be set to a small positive number."
161 (defcustom generic-find-file-regexp
"^#"
162 "*Regular expression used by `generic-mode-find-file-hook'.
163 Used to determine if files in fundamental mode should be put into
164 `default-generic-mode' instead."
169 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
171 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
174 (defun define-generic-mode (name comment-list keyword-list font-lock-list
175 auto-mode-list function-list
176 &optional description
)
177 "Create a new generic mode with NAME.
179 Args: (NAME COMMENT-LIST KEYWORD-LIST FONT-LOCK-LIST AUTO-MODE-LIST
180 FUNCTION-LIST &optional DESCRIPTION)
182 NAME should be a symbol; its string representation is used as the function
183 name. If DESCRIPTION is provided, it is used as the docstring for the new
186 COMMENT-LIST is a list, whose entries are either a single character,
187 a one or two character string or a cons pair. If the entry is a character
188 or a one-character string, it is added to the mode's syntax table with
189 `comment-start' syntax. If the entry is a cons pair, the elements of the
190 pair are considered to be `comment-start' and `comment-end' respectively.
191 Note that Emacs has limitations regarding comment characters.
193 KEYWORD-LIST is a list of keywords to highlight with `font-lock-keyword-face'.
194 Each keyword should be a string.
196 FONT-LOCK-LIST is a list of additional expressions to highlight. Each entry
197 in the list should have the same form as an entry in `font-lock-defaults-alist'
199 AUTO-MODE-LIST is a list of regular expressions to add to `auto-mode-alist'.
200 These regexps are added to `auto-mode-alist' as soon as `define-generic-mode'
201 is called; any old regexps with the same name are removed.
203 FUNCTION-LIST is a list of functions to call to do some additional setup.
205 See the file generic-x.el for some examples of `define-generic-mode'."
208 (unless (assq name generic-mode-list
)
209 (push (list (symbol-name name
)) generic-mode-list
))
211 ;; Add it to auto-mode-alist
212 (dolist (re auto-mode-list
)
213 (add-to-list 'auto-mode-alist
(cons re name
)))
215 ;; Define a function for it using `defalias' (not `fset') to make
216 ;; the mode appear on load-history.
219 ,(or description
(concat "Generic mode for type " (symbol-name name
)))
221 (generic-mode-internal ',name
',comment-list
',keyword-list
222 ',font-lock-list
',function-list
)))
225 (defun generic-mode-internal (mode comments keywords font-lock-list funs
)
226 "Go into the generic-mode MODE."
227 (let* ((generic-mode-hooks (intern (concat (symbol-name mode
) "-hook")))
228 (modename (symbol-name mode
))
229 (name (if (string-match "-mode\\'" modename
)
230 (substring modename
0 (match-beginning 0))
234 ;; Put this after the point where we read generic-mode-name!
235 (kill-all-local-variables)
239 mode-name
(capitalize name
)
242 (generic-mode-set-comments comments
)
244 ;; Font-lock functionality
245 ;; Font-lock-defaults are always set even if there are no keywords
246 ;; or font-lock expressions, so comments can be highlighted.
247 (setq generic-font-lock-defaults nil
)
248 (generic-mode-set-font-lock keywords font-lock-list
)
249 (make-local-variable 'font-lock-defaults
)
250 (setq font-lock-defaults
(list 'generic-font-lock-defaults nil
))
252 ;; Call a list of functions
253 (mapcar 'funcall funs
)
255 (run-hooks generic-mode-hooks
)
260 (defun generic-mode (type)
261 "Basic comment and font-lock functionality for `generic' files.
262 \(Files which are too small to warrant their own mode, but have
263 comment characters, keywords, and the like.)
265 To define a generic-mode, use the function `define-generic-mode'.
266 Some generic modes are defined in `generic-x.el'."
268 (list (completing-read "Generic Type: " generic-mode-list nil t
)))
269 (funcall (intern type
)))
271 ;;; Comment Functionality
272 (defun generic-mode-set-comments (comment-list)
273 "Set up comment functionality for generic mode."
274 (let ((st (make-syntax-table))
277 (make-local-variable 'comment-start
)
278 (make-local-variable 'comment-start-skip
)
279 (make-local-variable 'comment-end
)
281 ;; Go through all the comments
282 (dolist (start comment-list
)
283 (let ((end ?
\n) (comstyle ""))
286 (setq end
(or (cdr start
) end
))
287 (setq start
(car start
)))
288 (when (char-valid-p start
) (setq start
(char-to-string start
)))
289 (when (char-valid-p end
) (setq end
(char-to-string end
)))
291 ;; Setup the vars for `comment-region'
293 ;; We have already setup a comment-style, so use style b
296 (setq comment-start-skip
297 (concat comment-start-skip
"\\|" (regexp-quote start
) "+\\s-*")))
298 ;; First comment-style
299 (setq comment-start start
)
300 (setq comment-end
(unless (string-equal end
"\n") end
))
301 (setq comment-start-skip
(concat (regexp-quote start
) "+\\s-*")))
303 ;; Reuse comstyles if necessary
305 (or (cdr (assoc start comstyles
))
306 (cdr (assoc end comstyles
))
308 (push (cons start comstyle
) comstyles
)
309 (push (cons end comstyle
) comstyles
)
311 ;; Setup the syntax table
312 (if (= (length start
) 1)
313 (modify-syntax-entry (string-to-char start
)
314 (concat "< " comstyle
) st
)
315 (let ((c0 (elt start
0)) (c1 (elt start
1)))
316 ;; Store the relevant info but don't update yet
317 (push (cons c0
(concat (cdr (assoc c0 chars
)) "1")) chars
)
318 (push (cons c1
(concat (cdr (assoc c1 chars
))
319 (concat "2" comstyle
))) chars
)))
320 (if (= (length end
) 1)
321 (modify-syntax-entry (string-to-char end
)
322 (concat ">" comstyle
) st
)
323 (let ((c0 (elt end
0)) (c1 (elt end
1)))
324 ;; Store the relevant info but don't update yet
325 (push (cons c0
(concat (cdr (assoc c0 chars
))
326 (concat "3" comstyle
))) chars
)
327 (push (cons c1
(concat (cdr (assoc c1 chars
)) "4")) chars
)))))
329 ;; Process the chars that were part of a 2-char comment marker
330 (dolist (cs (nreverse chars
))
331 (modify-syntax-entry (car cs
)
332 (concat (char-to-string (char-syntax (car cs
)))
335 (set-syntax-table st
)))
337 (defun generic-mode-set-font-lock (keywords font-lock-expressions
)
338 "Set up font-lock functionality for generic mode."
339 (setq generic-font-lock-defaults
342 (list (generic-make-keywords-list keywords font-lock-keyword-face
)))
343 font-lock-expressions
)))
345 ;; Support for [KEYWORD] constructs found in INF, INI and Samba files
346 (defun generic-bracket-support ()
347 (setq imenu-generic-expression
348 '((nil "^\\[\\(.*\\)\\]" 1))
349 imenu-case-fold-search t
))
351 ;; This generic mode is always defined
352 (define-generic-mode 'default-generic-mode
(list ?
#) nil nil nil nil
)
354 ;; A more general solution would allow us to enter generic-mode for
355 ;; *any* comment character, but would require us to synthesize a new
356 ;; generic-mode on the fly. I think this gives us most of what we
358 (defun generic-mode-find-file-hook ()
359 "Hook function to enter `default-generic-mode' automatically.
360 Done if the first few lines of a file in `fundamental-mode' start with
361 a hash comment character. This hook will be installed if the variable
362 `generic-use-find-file-hook' is non-nil. The variable
363 `generic-lines-to-scan' determines the number of lines to look at."
364 (when (eq major-mode
'fundamental-mode
)
366 (goto-char (point-min))
367 (when (re-search-forward generic-find-file-regexp
369 (forward-line generic-lines-to-scan
)
371 (goto-char (point-min))
372 (default-generic-mode)))))
374 (defun generic-mode-ini-file-find-file-hook ()
375 "Hook function to enter default-generic-mode automatically for INI files.
376 Done if the first few lines of a file in `fundamental-mode' look like an
377 INI file. This hook is NOT installed by default."
378 (and (eq major-mode
'fundamental-mode
)
380 (goto-char (point-min))
381 (and (looking-at "^\\s-*\\[.*\\]")
382 (ini-generic-mode)))))
384 (and generic-use-find-file-hook
385 (add-hook 'find-file-hooks
'generic-mode-find-file-hook
))
387 (defun generic-make-keywords-list (keywords-list face
&optional prefix suffix
)
388 "Return a regular expression matching the specified KEYWORDS-LIST.
389 The regexp is highlighted with FACE."
390 (unless (listp keywords-list
)
391 (error "Keywords argument must be a list of strings"))
392 (list (concat prefix
"\\<"
393 ;; Use an optimized regexp.
394 (regexp-opt keywords-list t
)
401 ;;; generic.el ends here