1 ;;; conf-mode.el --- Simple major mode for editing conf/ini/properties files
3 ;; Copyright (C) 2004, 2005 Free Software Foundation, Inc.
5 ;; Author: Daniel Pfeiffer <occitan@esperanto.org>
6 ;; Keywords: conf ini windows java
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
27 ;; This mode is designed to edit many similar varieties of Conf/Ini files and
28 ;; Java properties. It started out from Aurélien Tisné's ini-mode.
29 ;; `conf-space-keywords' were inspired by Robert Fitzgerald's any-ini-mode.
36 (defvar outline-heading-end-regexp
)
41 "Configuration files."
45 (defcustom conf-assignment-column
24
46 "Align assignments to this column by default with \\[conf-align-assignments].
47 If this number is negative, the `=' comes before the whitespace. Use 0 to
48 not align (only setting space according to `conf-assignment-space')."
52 (defcustom conf-javaprop-assignment-column
32
53 "Value for `conf-assignment-column' in Java properties buffers."
57 (defcustom conf-colon-assignment-column
(- (abs conf-assignment-column
))
58 "Value for `conf-assignment-column' in Java properties buffers."
62 (defcustom conf-assignment-space t
63 "Put at least one space around assignments when aligning."
67 (defcustom conf-colon-assignment-space nil
68 "Value for `conf-assignment-space' in colon style Conf mode buffers."
74 (let ((map (make-sparse-keymap)))
75 (define-key map
"\C-c\C-u" 'conf-unix-mode
)
76 (define-key map
"\C-c\C-w" 'conf-windows-mode
)
77 (define-key map
"\C-c\C-j" 'conf-javaprop-mode
)
78 (define-key map
"\C-c\C-s" 'conf-space-mode
)
79 (define-key map
"\C-c " 'conf-space-mode
)
80 (define-key map
"\C-c\C-c" 'conf-colon-mode
)
81 (define-key map
"\C-c:" 'conf-colon-mode
)
82 (define-key map
"\C-c\C-x" 'conf-xdefaults-mode
)
83 (define-key map
"\C-c\C-p" 'conf-ppd-mode
)
84 (define-key map
"\C-c\C-q" 'conf-quote-normal
)
85 (define-key map
"\C-c\"" 'conf-quote-normal
)
86 (define-key map
"\C-c'" 'conf-quote-normal
)
87 (define-key map
"\C-c\C-a" 'conf-align-assignments
)
89 "Local keymap for conf-mode buffers.")
91 (defvar conf-mode-syntax-table
92 (let ((table (make-syntax-table)))
93 (modify-syntax-entry ?
= "." table
)
94 (modify-syntax-entry ?_
"_" table
)
95 (modify-syntax-entry ?-
"_" table
)
96 (modify-syntax-entry ?.
"_" table
)
97 (modify-syntax-entry ?
\' "\"" table
)
98 (modify-syntax-entry ?\
; "<" table)
99 (modify-syntax-entry ?
\n ">" table
)
100 (modify-syntax-entry ?
\r ">" table
)
102 "Syntax table in use in Windows style conf-mode buffers.")
104 (defvar conf-unix-mode-syntax-table
105 (let ((table (make-syntax-table conf-mode-syntax-table
)))
106 (modify-syntax-entry ?\
# "<" table
)
108 (modify-syntax-entry ?\
; "." table)
110 "Syntax table in use in Unix style conf-mode buffers.")
112 (defvar conf-javaprop-mode-syntax-table
113 (let ((table (make-syntax-table conf-unix-mode-syntax-table
)))
114 (modify-syntax-entry ?
/ ". 124" table
)
115 (modify-syntax-entry ?
* ". 23b" table
)
117 "Syntax table in use in Java prperties buffers.")
119 (defvar conf-ppd-mode-syntax-table
120 (let ((table (make-syntax-table conf-mode-syntax-table
)))
121 (modify-syntax-entry ?
* ". 1" table
)
122 (modify-syntax-entry ?%
". 2" table
)
124 (modify-syntax-entry ?
\' "." table
)
125 (modify-syntax-entry ?\
; "." table)
127 "Syntax table in use in PPD conf-mode buffers.")
129 (defvar conf-xdefaults-mode-syntax-table
130 (let ((table (make-syntax-table conf-mode-syntax-table
)))
131 (modify-syntax-entry ?
! "<" table
)
133 (modify-syntax-entry ?\
; "." table)
135 "Syntax table in use in Xdefaults style conf-mode buffers.")
138 (defvar conf-font-lock-keywords
139 `(;; [section] (do this first because it may look like a parameter)
140 ("^[ \t]*\\[\\(.+\\)\\]" 1 'font-lock-type-face
)
141 ;; var=val or var[index]=val
142 ("^[ \t]*\\(.+?\\)\\(?:\\[\\(.*?\\)\\]\\)?[ \t]*="
143 (1 'font-lock-variable-name-face
)
144 (2 'font-lock-constant-face nil t
))
145 ;; section { ... } (do this last because some assign ...{...)
146 ("^[ \t]*\\([^=:\n]+?\\)[ \t\n]*{[^{}]*?$" 1 'font-lock-type-face prepend
))
147 "Keywords to hilight in Conf mode")
149 (defvar conf-javaprop-font-lock-keywords
151 ("^[ \t]*\\(.+?\\)\\(?:\\.\\([0-9]+\\)\\(?:\\.\\(.+?\\)\\(?:\\.\\([0-9]+\\)\\(?:\\.\\(.+?\\)\\(?:\\.\\([0-9]+\\)\\(\\..+?\\)?\\)?\\)?\\)?\\)?\\)?\\([:= \t]\\|$\\)"
152 (1 'font-lock-variable-name-face
)
153 (2 'font-lock-constant-face nil t
)
154 (3 'font-lock-variable-name-face nil t
)
155 (4 'font-lock-constant-face nil t
)
156 (5 'font-lock-variable-name-face nil t
)
157 (6 'font-lock-constant-face nil t
)
158 (7 'font-lock-variable-name-face nil t
)))
159 "Keywords to hilight in Conf Java Properties mode")
161 (defvar conf-space-keywords-alist
162 '(("\\`/etc/gpm/" .
"key\\|name\\|foreground\\|background\\|border\\|head")
163 ("\\`/etc/magic\\'" .
"[^ \t]+[ \t]+\\(?:[bl]?e?\\(?:short\\|long\\)\\|byte\\|string\\)[^ \t]*")
164 ("/mod\\(?:ules\\|probe\\)\\.conf" .
"alias\\|in\\(?:clude\\|stall\\)\\|options\\|remove")
165 ("/manpath\\.config" .
"MAN\\(?:DATORY_MANPATH\\|PATH_MAP\\|DB_MAP\\)")
166 ("/sensors\\.conf" .
"chip\\|bus\\|label\\|compute\\|set\\|ignore")
167 ("/sane\\(\\.d\\)?/" .
"option\\|device\\|port\\|usb\\|sc\\(?:si\\|anner\\)")
168 ("/resmgr\\.conf" .
"class\\|add\\|allow\\|deny")
169 ("/dictionary\\.lst\\'" .
"DICT\\|HYPH\\|THES")
170 ("/tuxracer/options" .
"set"))
171 "File name based settings for `conf-space-keywords'.")
173 (defvar conf-space-keywords nil
174 "Regexps for functions that may come before a space assignment.
175 This allows constructs such as
177 This variable is best set in the file local variables, or through
178 `conf-space-keywords-alist'.")
180 (defvar conf-space-font-lock-keywords
181 `(;; [section] (do this first because it may look like a parameter)
182 ("^[ \t]*\\[\\(.+\\)\\]" 1 'font-lock-type-face
)
183 ;; section { ... } (do this first because it looks like a parameter)
184 ("^[ \t]*\\(.+?\\)[ \t\n]*{[^{}]*?$" 1 'font-lock-type-face
)
186 (eval if conf-space-keywords
187 (list (concat "^[ \t]*\\(" conf-space-keywords
"\\)[ \t]+\\([^\000- ]+\\)")
188 '(1 'font-lock-keyword-face
)
189 '(2 'font-lock-variable-name-face
))
190 '("^[ \t]*\\([^\000- ]+\\)" 1 'font-lock-variable-name-face
)))
191 "Keywords to hilight in Conf Space mode")
193 (defvar conf-colon-font-lock-keywords
194 `(;; [section] (do this first because it may look like a parameter)
195 ("^[ \t]*\\[\\(.+\\)\\]" 1 'font-lock-type-face
)
197 ("^[ \t]*\\(.+?\\)[ \t]*:"
198 (1 'font-lock-variable-name-face
))
199 ;; section { ... } (do this last because some assign ...{...)
200 ("^[ \t]*\\([^:\n]+\\)[ \t\n]*{[^{}]*?$" 1 'font-lock-type-face prepend
))
201 "Keywords to hilight in Conf Colon mode")
203 (defvar conf-assignment-sign ?
=
204 "What sign is used for assignments.")
206 (defvar conf-assignment-regexp
".+?\\([ \t]*=[ \t]*\\)"
207 "Regexp to recognize assignments.
208 It is anchored after the first sexp on a line. There must a
209 grouping for the assignment sign, including leading and trailing
213 ;; If anybody can figure out how to get the same effect by configuring
214 ;; `align', I'd be glad to hear.
215 (defun conf-align-assignments (&optional arg
)
218 (prefix-numeric-value arg
)
219 conf-assignment-column
))
221 (goto-char (point-min))
223 (let ((cs (comment-beginning))) ; go before comment if within
224 (if cs
(goto-char cs
)))
225 (while (forward-comment 9)) ; max-int?
226 (when (and (not (eobp))
227 (looking-at conf-assignment-regexp
))
228 (goto-char (match-beginning 1))
229 (delete-region (point) (match-end 1))
230 (if conf-assignment-sign
233 (indent-to-column arg
)
234 (or (not conf-assignment-space
) (memq (char-before (point)) '(?\s ?
\t)) (insert ?\s
))
235 (insert conf-assignment-sign
(if (and conf-assignment-space
(not (eolp))) ?\s
"")))
236 (insert (if conf-assignment-space ?\s
"") conf-assignment-sign
)
238 (indent-to-column (- arg
))
239 (or (not conf-assignment-space
) (memq (char-before (point)) '(?\s ?
\t)) (insert ?\s
))))
241 (if (>= (current-column) (abs arg
))
243 (indent-to-column (abs arg
))))))
247 (defun conf-quote-normal (arg)
248 "Set the syntax of ' and \" to punctuation.
249 With prefix arg, only do it for ' if 1, or only for \" if 2.
250 This only affects the current buffer. Some conf files use quotes
251 to delimit strings, while others allow quotes as simple parts of
252 the assigned value. In those files font locking will be wrong,
253 and you can correct it with this command. (Some files even do
254 both, i.e. quotes delimit strings, except when they are
255 unbalanced, but hey...)"
257 (let ((table (copy-syntax-table (syntax-table))))
258 (if (or (not arg
) (= (prefix-numeric-value arg
) 1)) (modify-syntax-entry ?
\' "." table
))
259 (if (or (not arg
) (= (prefix-numeric-value arg
) 2)) (modify-syntax-entry ?
\" "." table
))
260 (set-syntax-table table
)
261 (and (boundp 'font-lock-mode
)
263 (font-lock-fontify-buffer))))
266 (defun conf-outline-level ()
270 (while (setq pt
(scan-lists pt -
1 1)
272 (scan-error depth
))))
277 (defun conf-mode (&optional comment syntax-table name
)
278 "Mode for Unix and Windows Conf files and Java properties.
279 Most conf files know only three kinds of constructs: parameter
280 assignments optionally grouped into sections and comments. Yet
281 there is a great range of variation in the exact syntax of conf
282 files. See below for various wrapper commands that set up the
283 details for some of the most widespread variants.
285 This mode sets up font locking, outline, imenu and it provides
286 alignment support through `conf-align-assignments'. If strings
287 come out wrong, try `conf-quote-normal'.
289 Some files allow continuation lines, either with a backslash at
290 the end of line, or by indenting the next line (further). These
291 constructs cannot currently be recognized.
293 Because of this great variety of nuances, which are often not
294 even clearly specified, please don't expect it to get every file
295 quite right. Patches that clearly identify some special case,
296 without breaking the general ones, are welcome.
298 If instead you start this mode with the generic `conf-mode'
299 command, it will parse the buffer. It will generally well
300 identify the first four cases listed below. If the buffer
301 doesn't have enough contents to decide, this is identical to
302 `conf-windows-mode' on Windows, elsewhere to `conf-unix-mode'.
303 See also `conf-space-mode', `conf-colon-mode', `conf-javaprop-mode',
304 `conf-ppd-mode' and `conf-xdefaults-mode'.
310 (let ((unix 0) (win 0) (equal 0) (colon 0) (space 0) (jp 0))
312 (goto-char (point-min))
314 (skip-chars-forward " \t\f")
315 (cond ((eq (char-after) ?\
#) (setq unix
(1+ unix
)))
316 ((eq (char-after) ?\
;) (setq win (1+ win)))
317 ((eq (char-after) ?\
[)) ; nop
319 ((eq (char-after) ?
})) ; nop
320 ;; recognize at most double spaces within names
321 ((looking-at "[^ \t\n=:]+\\(?: ?[^ \t\n=:]+\\)*[ \t]*[=:]")
322 (if (eq (char-before (match-end 0)) ?
=)
323 (setq equal
(1+ equal
))
324 (setq colon
(1+ colon
))))
325 ((looking-at "/[/*]") (setq jp
(1+ jp
)))
326 ((looking-at ".*{")) ; nop
327 ((setq space
(1+ space
))))
329 (if (> jp
(max unix win
3))
331 (if (> colon
(max equal space
))
333 (if (> space
(max equal colon
))
336 (and (= win unix
) (eq system-type
'windows-nt
)))
338 (conf-unix-mode))))))
339 (kill-all-local-variables)
340 (use-local-map conf-mode-map
)
342 (setq major-mode
'conf-mode
344 (set (make-local-variable 'comment-start
) comment
)
345 (set (make-local-variable 'comment-start-skip
)
346 (concat (regexp-quote comment-start
) "+\\s *"))
347 (set (make-local-variable 'comment-use-syntax
) t
)
348 (set (make-local-variable 'parse-sexp-ignore-comments
) t
)
349 (set (make-local-variable 'outline-regexp
)
350 "[ \t]*\\(?:\\[\\|.+[ \t\n]*{\\)")
351 (set (make-local-variable 'outline-heading-end-regexp
)
353 (set (make-local-variable 'outline-level
)
355 (set-syntax-table syntax-table
)
356 (setq imenu-generic-expression
357 '(("Parameters" "^[ \t]*\\(.+?\\)[ \t]*=" 1)
359 (nil "^[ \t]*\\[[ \t]*\\(.+\\)[ \t]*\\]" 1)
361 (nil "^[ \t]*\\([^=:{} \t\n][^=:{}\n]+\\)[ \t\n]*{" 1)))
363 (run-mode-hooks 'conf-mode-hook
)))
366 (defun conf-unix-mode ()
367 "Conf Mode starter for Unix style Conf files.
368 Comments start with `#'.
369 For details see `conf-mode'. Example:
371 # Conf mode font-locks this right on Unix and with C-c C-u
379 (conf-mode "#" conf-unix-mode-syntax-table
"Conf[Unix]"))
382 (defun conf-windows-mode ()
383 "Conf Mode starter for Windows style Conf files.
384 Comments start with `;'.
385 For details see `conf-mode'. Example:
387 ; Conf mode font-locks this right on Windows and with C-c C-w
389 \[ExtShellFolderViews]
390 Default={5984FFE0-28D4-11CF-AE66-08002B2E1262}
391 {5984FFE0-28D4-11CF-AE66-08002B2E1262}={5984FFE0-28D4-11CF-AE66-08002B2E1262}
393 \[{5984FFE0-28D4-11CF-AE66-08002B2E1262}]
394 PersistMoniker=file://Folder.htt"
396 (conf-mode ";" conf-mode-syntax-table
"Conf[WinIni]"))
398 ;; Here are a few more or less widespread styles. There are others, so
399 ;; obscure, they are not covered. E.g. RFC 2614 allows both Unix and Windows
400 ;; comments. Or the donkey has (* Pascal comments *) -- roll your own starter
404 (defun conf-javaprop-mode ()
405 "Conf Mode starter for Java properties files.
406 Comments start with `#' but are also recognized with `//' or
407 between `/*' and `*/'.
408 For details see `conf-mode'. Example:
410 # Conf mode font-locks this right with C-c C-j (Java properties)
411 // another kind of comment
421 (conf-mode "#" conf-javaprop-mode-syntax-table
"Conf[JavaProp]")
422 (set (make-local-variable 'conf-assignment-column
)
423 conf-javaprop-assignment-column
)
424 (set (make-local-variable 'conf-assignment-regexp
)
425 ".+?\\([ \t]*[=: \t][ \t]*\\|$\\)")
426 (set (make-local-variable 'conf-font-lock-keywords
)
427 conf-javaprop-font-lock-keywords
)
428 (setq comment-start-skip
"\\(?:#+\\|/[/*]+\\)\\s *")
429 (setq imenu-generic-expression
430 '(("Parameters" "^[ \t]*\\(.+?\\)[=: \t]" 1))))
433 (defun conf-space-mode (&optional keywords
)
434 "Conf Mode starter for space separated conf files.
435 \"Assignments\" are with ` '. Keywords before the parameters are
436 recognized according to `conf-space-keywords'. Interactively
437 with a prefix ARG of `0' no keywords will be recognized. With
438 any other prefix arg you will be prompted for a regexp to match
439 the keywords. Programmatically you can pass such a regexp as
440 KEYWORDS, or any non-nil non-string for no keywords.
442 For details see `conf-mode'. Example:
444 # Conf mode font-locks this right with C-c C-s (space separated)
446 image/jpeg jpeg jpg jpe
450 # Or with keywords (from a recognized file name):
452 # Standard multimedia devices
453 add /dev/audio desktop
454 add /dev/mixer desktop"
456 (list (if current-prefix-arg
457 (if (> (prefix-numeric-value current-prefix-arg
) 0)
458 (read-string "Regexp to match keywords: ")
461 (setq mode-name
"Conf[Space]")
462 (set (make-local-variable 'conf-assignment-sign
)
464 (set (make-local-variable 'conf-font-lock-keywords
)
465 conf-space-font-lock-keywords
)
466 ;; This doesn't seem right, but the next two depend on conf-space-keywords
467 ;; being set, while after-change-major-mode-hook might set up imenu, needing
468 ;; the following result:
469 (hack-local-variables-prop-line)
470 (hack-local-variables)
472 (set (make-local-variable 'conf-space-keywords
)
473 (if (stringp keywords
) keywords
))
474 (or conf-space-keywords
475 (not buffer-file-name
)
476 (set (make-local-variable 'conf-space-keywords
)
477 (assoc-default buffer-file-name conf-space-keywords-alist
479 (set (make-local-variable 'conf-assignment-regexp
)
480 (if conf-space-keywords
481 (concat "\\(?:" conf-space-keywords
"\\)[ \t]+.+?\\([ \t]+\\|$\\)")
482 ".+?\\([ \t]+\\|$\\)"))
483 (setq imenu-generic-expression
484 `(,@(cdr imenu-generic-expression
)
486 ,(if conf-space-keywords
487 (concat "^[ \t]*\\(?:" conf-space-keywords
488 "\\)[ \t]+\\([^ \t\n]+\\)\\(?:[ \t]\\|$\\)")
489 "^[ \t]*\\([^ \t\n[]+\\)\\(?:[ \t]\\|$\\)")
493 (defun conf-colon-mode (&optional comment syntax-table name
)
494 "Conf Mode starter for Colon files.
495 \"Assignments\" are with `:'.
496 For details see `conf-mode'. Example:
498 # Conf mode font-locks this right with C-c C-c (colon)
500 <Multi_key> <exclam> <exclam> : \"\\241\" exclamdown
501 <Multi_key> <c> <slash> : \"\\242\" cent"
504 (conf-mode comment syntax-table name
)
506 (setq mode-name
"Conf[Colon]"))
507 (set (make-local-variable 'conf-assignment-space
)
508 conf-colon-assignment-space
)
509 (set (make-local-variable 'conf-assignment-column
)
510 conf-colon-assignment-column
)
511 (set (make-local-variable 'conf-assignment-sign
)
513 (set (make-local-variable 'conf-assignment-regexp
)
514 ".+?\\([ \t]*:[ \t]*\\)")
515 (set (make-local-variable 'conf-font-lock-keywords
)
516 conf-colon-font-lock-keywords
)
517 (setq imenu-generic-expression
518 `(("Parameters" "^[ \t]*\\(.+?\\)[ \t]*:" 1)
519 ,@(cdr imenu-generic-expression
))))
522 (defun conf-ppd-mode ()
523 "Conf Mode starter for Adobe/CUPS PPD files.
524 Comments start with `*%' and \"assignments\" are with `:'.
525 For details see `conf-mode'. Example:
527 *% Conf mode font-locks this right with C-c C-p (PPD)
529 *DefaultTransfer: Null
530 *Transfer Null.Inverse: \"{ 1 exch sub }\""
532 (conf-colon-mode "*%" conf-ppd-mode-syntax-table
"Conf[PPD]")
533 ;; no sections, they match within PostScript code
534 (setq imenu-generic-expression
(list (car imenu-generic-expression
))))
537 (defun conf-xdefaults-mode ()
538 "Conf Mode starter for Xdefaults files.
539 Comments start with `!' and \"assignments\" are with `:'.
540 For details see `conf-mode'. Example:
542 ! Conf mode font-locks this right with C-c C-x (.Xdefaults)
547 (conf-colon-mode "!" conf-xdefaults-mode-syntax-table
"Conf[Xdefaults]"))
551 (if (boundp 'font-lock-defaults-alist
)
553 'font-lock-defaults-alist
555 (list 'conf-font-lock-keywords nil t nil nil
))))
560 ;; arch-tag: 0a3805b2-0371-4d3a-8498-8897116b2356
561 ;;; conf-mode.el ends here