Mercurial > emacs
annotate lisp/emacs-lisp/generic.el @ 80462:837d54fb9fc9
(dired-dnd-handle-local-file): Obey dired-backup-overwrite for copy,
move, and link operations.
author | Chong Yidong <cyd@stupidchicken.com> |
---|---|
date | Sun, 06 Apr 2008 21:01:54 +0000 |
parents | 78ee6fae0e41 |
children | 107ccd98fa12 |
rev | line source |
---|---|
61521 | 1 ;;; generic.el --- defining simple major modes with comment and font-lock |
2 ;; | |
74466 | 3 ;; Copyright (C) 1997, 1999, 2001, 2002, 2003, 2004, |
79704 | 4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc. |
61521 | 5 ;; |
6 ;; Author: Peter Breton <pbreton@cs.umb.edu> | |
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 | |
78217
935157c0b596
Switch license to GPLv3 or later.
Glenn Morris <rgm@gnu.org>
parents:
75346
diff
changeset
|
14 ;; the Free Software Foundation; either version 3, or (at your option) |
61521 | 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 | |
64085 | 24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
25 ;; Boston, MA 02110-1301, USA. | |
61521 | 26 |
27 ;;; Commentary: | |
28 | |
29 ;; INTRODUCTION: | |
30 ;; | |
31 ;; The macro `define-generic-mode' can be used to define small modes | |
32 ;; which provide basic comment and font-lock support. These modes are | |
33 ;; intended for the many configuration files and such which are too | |
34 ;; small for a "real" mode, but still have a regular syntax, comment | |
35 ;; characters and the like. | |
36 ;; | |
37 ;; Each generic mode can define the following: | |
38 ;; | |
61931 | 39 ;; * List of comment-characters. The elements of this list should be |
40 ;; either a character, a one or two character string, or a cons | |
41 ;; cell. If the entry is a character or a string, it is added to | |
42 ;; the mode's syntax table with "comment starter" syntax. If the | |
43 ;; entry is a cons cell, the `car' and `cdr' of the pair are | |
44 ;; considered the "comment starter" and "comment ender" | |
45 ;; respectively. (The latter should be nil if you want comments to | |
46 ;; end at the end of the line.) Emacs does not support comment | |
47 ;; strings of more than two characters in length. | |
61521 | 48 ;; |
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 | |
51 ;; face different from `font-lock-keyword-face', you can use the | |
52 ;; convenience function `generic-make-keywords-list' (which see), | |
53 ;; and add the result to the following list: | |
54 ;; | |
55 ;; * Additional expressions to font-lock. This should be a list of | |
56 ;; expressions, each of which should be of the same form as those in | |
57 ;; `font-lock-keywords'. | |
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 ;; EXAMPLE: | |
68 ;; | |
69 ;; You can use `define-generic-mode' like this: | |
70 ;; | |
71 ;; (define-generic-mode 'foo-generic-mode | |
72 ;; (list ?%) | |
73 ;; (list "keyword") | |
74 ;; nil | |
75 ;; (list "\\.FOO\\'") | |
76 ;; (list 'foo-setup-function)) | |
77 ;; | |
78 ;; to define a new generic-mode `foo-generic-mode', which has '%' as a | |
79 ;; comment character, and "keyword" as a keyword. When files which | |
80 ;; end in '.FOO' are loaded, Emacs will go into foo-generic-mode and | |
81 ;; call foo-setup-function. You can also use the function | |
82 ;; `foo-generic-mode' (which is interactive) to put a buffer into | |
83 ;; foo-generic-mode. | |
84 ;; | |
85 ;; GOTCHAS: | |
86 ;; | |
87 ;; Be careful that your font-lock definitions are correct. Getting | |
88 ;; them wrong can cause Emacs to continually attempt to fontify! This | |
89 ;; problem is not specific to generic-mode. | |
90 | |
91 ;; Credit for suggestions, brainstorming, help with debugging: | |
92 ;; ACorreir@pervasive-sw.com (Alfred Correira) | |
93 ;; Extensive cleanup by: | |
94 ;; Stefan Monnier (monnier+gnu/emacs@flint.cs.yale.edu) | |
95 | |
96 ;;; Code: | |
97 | |
98 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
99 ;; Internal Variables | |
100 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
101 | |
102 (defvar generic-font-lock-keywords nil | |
103 "Keywords for `font-lock-defaults' in a generic mode.") | |
104 (make-variable-buffer-local 'generic-font-lock-keywords) | |
64908
45df3993d950
(generic-font-lock-defaults): Declare with define-obsolete-variable-alias.
Lute Kamstra <lute@gnu.org>
parents:
64751
diff
changeset
|
105 (define-obsolete-variable-alias 'generic-font-lock-defaults 'generic-font-lock-keywords "22.1") |
61521 | 106 |
107 ;;;###autoload | |
108 (defvar generic-mode-list nil | |
109 "A list of mode names for `generic-mode'. | |
110 Do not add entries to this list directly; use `define-generic-mode' | |
111 instead (which see).") | |
112 | |
113 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
114 ;; Functions | |
115 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
116 | |
117 ;;;###autoload | |
118 (defmacro define-generic-mode (mode comment-list keyword-list | |
119 font-lock-list auto-mode-list | |
62260
a0af6e05c03a
(define-generic-mode): Don't generate a defcustom for the mode hook
Lute Kamstra <lute@gnu.org>
parents:
61931
diff
changeset
|
120 function-list &optional docstring) |
61521 | 121 "Create a new generic mode MODE. |
122 | |
62260
a0af6e05c03a
(define-generic-mode): Don't generate a defcustom for the mode hook
Lute Kamstra <lute@gnu.org>
parents:
61931
diff
changeset
|
123 MODE is the name of the command for the generic mode; don't quote it. |
a0af6e05c03a
(define-generic-mode): Don't generate a defcustom for the mode hook
Lute Kamstra <lute@gnu.org>
parents:
61931
diff
changeset
|
124 The optional DOCSTRING is the documentation for the mode command. If |
a0af6e05c03a
(define-generic-mode): Don't generate a defcustom for the mode hook
Lute Kamstra <lute@gnu.org>
parents:
61931
diff
changeset
|
125 you do not supply it, `define-generic-mode' uses a default |
a0af6e05c03a
(define-generic-mode): Don't generate a defcustom for the mode hook
Lute Kamstra <lute@gnu.org>
parents:
61931
diff
changeset
|
126 documentation string instead. |
61521 | 127 |
62260
a0af6e05c03a
(define-generic-mode): Don't generate a defcustom for the mode hook
Lute Kamstra <lute@gnu.org>
parents:
61931
diff
changeset
|
128 COMMENT-LIST is a list in which each element is either a character, a |
a0af6e05c03a
(define-generic-mode): Don't generate a defcustom for the mode hook
Lute Kamstra <lute@gnu.org>
parents:
61931
diff
changeset
|
129 string of one or two characters, or a cons cell. A character or a |
a0af6e05c03a
(define-generic-mode): Don't generate a defcustom for the mode hook
Lute Kamstra <lute@gnu.org>
parents:
61931
diff
changeset
|
130 string is set up in the mode's syntax table as a \"comment starter\". |
a0af6e05c03a
(define-generic-mode): Don't generate a defcustom for the mode hook
Lute Kamstra <lute@gnu.org>
parents:
61931
diff
changeset
|
131 If the entry is a cons cell, the `car' is set up as a \"comment |
a0af6e05c03a
(define-generic-mode): Don't generate a defcustom for the mode hook
Lute Kamstra <lute@gnu.org>
parents:
61931
diff
changeset
|
132 starter\" and the `cdr' as a \"comment ender\". (Use nil for the |
a0af6e05c03a
(define-generic-mode): Don't generate a defcustom for the mode hook
Lute Kamstra <lute@gnu.org>
parents:
61931
diff
changeset
|
133 latter if you want comments to end at the end of the line.) Note that |
a0af6e05c03a
(define-generic-mode): Don't generate a defcustom for the mode hook
Lute Kamstra <lute@gnu.org>
parents:
61931
diff
changeset
|
134 the syntax table has limitations about what comment starters and |
a0af6e05c03a
(define-generic-mode): Don't generate a defcustom for the mode hook
Lute Kamstra <lute@gnu.org>
parents:
61931
diff
changeset
|
135 enders are actually possible. |
61521 | 136 |
137 KEYWORD-LIST is a list of keywords to highlight with | |
138 `font-lock-keyword-face'. Each keyword should be a string. | |
139 | |
62260
a0af6e05c03a
(define-generic-mode): Don't generate a defcustom for the mode hook
Lute Kamstra <lute@gnu.org>
parents:
61931
diff
changeset
|
140 FONT-LOCK-LIST is a list of additional expressions to highlight. Each |
a0af6e05c03a
(define-generic-mode): Don't generate a defcustom for the mode hook
Lute Kamstra <lute@gnu.org>
parents:
61931
diff
changeset
|
141 element of this list should have the same form as an element of |
a0af6e05c03a
(define-generic-mode): Don't generate a defcustom for the mode hook
Lute Kamstra <lute@gnu.org>
parents:
61931
diff
changeset
|
142 `font-lock-keywords'. |
61521 | 143 |
144 AUTO-MODE-LIST is a list of regular expressions to add to | |
62260
a0af6e05c03a
(define-generic-mode): Don't generate a defcustom for the mode hook
Lute Kamstra <lute@gnu.org>
parents:
61931
diff
changeset
|
145 `auto-mode-alist'. These regular expressions are added when Emacs |
a0af6e05c03a
(define-generic-mode): Don't generate a defcustom for the mode hook
Lute Kamstra <lute@gnu.org>
parents:
61931
diff
changeset
|
146 runs the macro expansion. |
61521 | 147 |
62260
a0af6e05c03a
(define-generic-mode): Don't generate a defcustom for the mode hook
Lute Kamstra <lute@gnu.org>
parents:
61931
diff
changeset
|
148 FUNCTION-LIST is a list of functions to call to do some additional |
a0af6e05c03a
(define-generic-mode): Don't generate a defcustom for the mode hook
Lute Kamstra <lute@gnu.org>
parents:
61931
diff
changeset
|
149 setup. The mode command calls these functions just before it runs the |
a0af6e05c03a
(define-generic-mode): Don't generate a defcustom for the mode hook
Lute Kamstra <lute@gnu.org>
parents:
61931
diff
changeset
|
150 mode hook `MODE-hook'. |
61521 | 151 |
152 See the file generic-x.el for some examples of `define-generic-mode'." | |
153 (declare (debug (sexp def-form def-form def-form form def-form | |
154 [&optional stringp] &rest [keywordp form])) | |
155 (indent 1)) | |
156 | |
157 ;; Backward compatibility. | |
158 (when (eq (car-safe mode) 'quote) | |
159 (setq mode (eval mode))) | |
160 | |
161 (let* ((name (symbol-name mode)) | |
162 (pretty-name (capitalize (replace-regexp-in-string | |
62260
a0af6e05c03a
(define-generic-mode): Don't generate a defcustom for the mode hook
Lute Kamstra <lute@gnu.org>
parents:
61931
diff
changeset
|
163 "-mode\\'" "" name)))) |
61521 | 164 |
165 `(progn | |
166 ;; Add a new entry. | |
167 (add-to-list 'generic-mode-list ,name) | |
168 | |
169 ;; Add it to auto-mode-alist | |
170 (dolist (re ,auto-mode-list) | |
171 (add-to-list 'auto-mode-alist (cons re ',mode))) | |
172 | |
173 (defun ,mode () | |
174 ,(or docstring | |
175 (concat pretty-name " mode.\n" | |
62260
a0af6e05c03a
(define-generic-mode): Don't generate a defcustom for the mode hook
Lute Kamstra <lute@gnu.org>
parents:
61931
diff
changeset
|
176 "This a generic mode defined with `define-generic-mode'.\n" |
a0af6e05c03a
(define-generic-mode): Don't generate a defcustom for the mode hook
Lute Kamstra <lute@gnu.org>
parents:
61931
diff
changeset
|
177 "It runs `" name "-hook' as the last thing it does.")) |
61521 | 178 (interactive) |
179 (generic-mode-internal ',mode ,comment-list ,keyword-list | |
180 ,font-lock-list ,function-list))))) | |
181 | |
182 ;;;###autoload | |
183 (defun generic-mode-internal (mode comment-list keyword-list | |
184 font-lock-list function-list) | |
185 "Go into the generic mode MODE." | |
186 (let* ((name (symbol-name mode)) | |
187 (pretty-name (capitalize (replace-regexp-in-string | |
188 "-mode\\'" "" name))) | |
189 (mode-hook (intern (concat name "-hook")))) | |
190 | |
191 (kill-all-local-variables) | |
192 | |
193 (setq major-mode mode | |
194 mode-name pretty-name) | |
195 | |
196 (generic-mode-set-comments comment-list) | |
197 | |
198 ;; Font-lock functionality. | |
199 ;; Font-lock-defaults is always set even if there are no keywords | |
200 ;; or font-lock expressions, so comments can be highlighted. | |
201 (setq generic-font-lock-keywords font-lock-list) | |
202 (when keyword-list | |
203 (push (concat "\\_<" (regexp-opt keyword-list t) "\\_>") | |
204 generic-font-lock-keywords)) | |
61904
114b2859d567
(generic-mode-internal): Simplify font-lock-defaults.
Lute Kamstra <lute@gnu.org>
parents:
61521
diff
changeset
|
205 (setq font-lock-defaults '(generic-font-lock-keywords)) |
61521 | 206 |
207 ;; Call a list of functions | |
208 (mapcar 'funcall function-list) | |
209 | |
210 (run-mode-hooks mode-hook))) | |
211 | |
212 ;;;###autoload | |
213 (defun generic-mode (mode) | |
214 "Enter generic mode MODE. | |
215 | |
216 Generic modes provide basic comment and font-lock functionality | |
217 for \"generic\" files. (Files which are too small to warrant their | |
218 own mode, but have comment characters, keywords, and the like.) | |
219 | |
220 To define a generic-mode, use the function `define-generic-mode'. | |
221 Some generic modes are defined in `generic-x.el'." | |
222 (interactive | |
223 (list (completing-read "Generic mode: " generic-mode-list nil t))) | |
224 (funcall (intern mode))) | |
225 | |
226 ;;; Comment Functionality | |
227 (defun generic-mode-set-comments (comment-list) | |
228 "Set up comment functionality for generic mode." | |
229 (let ((st (make-syntax-table)) | |
230 (chars nil) | |
231 (comstyles)) | |
232 (make-local-variable 'comment-start) | |
233 (make-local-variable 'comment-start-skip) | |
234 (make-local-variable 'comment-end) | |
235 | |
236 ;; Go through all the comments | |
237 (dolist (start comment-list) | |
238 (let (end (comstyle "")) | |
239 ;; Normalize | |
240 (when (consp start) | |
241 (setq end (cdr start)) | |
242 (setq start (car start))) | |
243 (when (char-valid-p start) (setq start (char-to-string start))) | |
244 (cond | |
245 ((char-valid-p end) (setq end (char-to-string end))) | |
246 ((zerop (length end)) (setq end "\n"))) | |
247 | |
248 ;; Setup the vars for `comment-region' | |
249 (if comment-start | |
250 ;; We have already setup a comment-style, so use style b | |
251 (progn | |
252 (setq comstyle "b") | |
253 (setq comment-start-skip | |
254 (concat comment-start-skip "\\|" (regexp-quote start) "+\\s-*"))) | |
255 ;; First comment-style | |
256 (setq comment-start start) | |
257 (setq comment-end (if (string-equal end "\n") "" end)) | |
258 (setq comment-start-skip (concat (regexp-quote start) "+\\s-*"))) | |
259 | |
260 ;; Reuse comstyles if necessary | |
261 (setq comstyle | |
262 (or (cdr (assoc start comstyles)) | |
263 (cdr (assoc end comstyles)) | |
264 comstyle)) | |
265 (push (cons start comstyle) comstyles) | |
266 (push (cons end comstyle) comstyles) | |
267 | |
268 ;; Setup the syntax table | |
269 (if (= (length start) 1) | |
270 (modify-syntax-entry (string-to-char start) | |
271 (concat "< " comstyle) st) | |
272 (let ((c0 (elt start 0)) (c1 (elt start 1))) | |
273 ;; Store the relevant info but don't update yet | |
274 (push (cons c0 (concat (cdr (assoc c0 chars)) "1")) chars) | |
275 (push (cons c1 (concat (cdr (assoc c1 chars)) | |
276 (concat "2" comstyle))) chars))) | |
277 (if (= (length end) 1) | |
278 (modify-syntax-entry (string-to-char end) | |
279 (concat ">" comstyle) st) | |
280 (let ((c0 (elt end 0)) (c1 (elt end 1))) | |
281 ;; Store the relevant info but don't update yet | |
282 (push (cons c0 (concat (cdr (assoc c0 chars)) | |
283 (concat "3" comstyle))) chars) | |
284 (push (cons c1 (concat (cdr (assoc c1 chars)) "4")) chars))))) | |
285 | |
286 ;; Process the chars that were part of a 2-char comment marker | |
287 (dolist (cs (nreverse chars)) | |
288 (modify-syntax-entry (car cs) | |
289 (concat (char-to-string (char-syntax (car cs))) | |
290 " " (cdr cs)) | |
291 st)) | |
292 (set-syntax-table st))) | |
293 | |
294 (defun generic-bracket-support () | |
295 "Imenu support for [KEYWORD] constructs found in INF, INI and Samba files." | |
296 (setq imenu-generic-expression | |
297 '((nil "^\\[\\(.*\\)\\]" 1)) | |
298 imenu-case-fold-search t)) | |
299 | |
300 ;;;###autoload | |
301 (defun generic-make-keywords-list (keyword-list face &optional prefix suffix) | |
302 "Return a `font-lock-keywords' construct that highlights KEYWORD-LIST. | |
303 KEYWORD-LIST is a list of keyword strings that should be | |
304 highlighted with face FACE. This function calculates a regular | |
305 expression that matches these keywords and concatenates it with | |
306 PREFIX and SUFFIX. Then it returns a construct based on this | |
307 regular expression that can be used as an element of | |
308 `font-lock-keywords'." | |
309 (unless (listp keyword-list) | |
310 (error "Keywords argument must be a list of strings")) | |
311 (list (concat prefix "\\_<" | |
312 ;; Use an optimized regexp. | |
313 (regexp-opt keyword-list t) | |
314 "\\_>" suffix) | |
315 1 | |
316 face)) | |
317 | |
318 (provide 'generic) | |
319 | |
320 ;; arch-tag: 239c1fc4-1303-48d9-9ac0-657d655669ea | |
321 ;;; generic.el ends here |