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