1 ;;; conf-mode.el --- Simple major mode for editing conf/ini/properties files
3 ;; Copyright (C) 2004 by Daniel Pfeiffer <occitan@esperanto.org>
4 ;; Keywords: conf ini windows java
6 ;; This file is part of GNU Emacs.
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING. If not, write to the
20 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, USA.
25 ;; This mode is designed to edit many similar varieties of Conf/Ini files and
26 ;; Java properties. It started out from Aurélien Tisné's ini-mode.
27 ;; `conf-space-keywords' were inspired by Robert Fitzgerald's any-ini-mode.
37 "Configuration files."
41 (defcustom conf-assignment-column
24
42 "Align assignments to this column by default with \\[conf-align-assignments].
43 If this number is negative, the `=' comes before the whitespace. Use 0 to
44 not align (only setting space according to `conf-assignment-space')."
48 (defcustom conf-javaprop-assignment-column
32
49 "Value for `conf-assignment-column' in Java properties buffers."
53 (defcustom conf-colon-assignment-column
(- (abs conf-assignment-column
))
54 "Value for `conf-assignment-column' in Java properties buffers."
58 (defcustom conf-assignment-space t
59 "Put at least one space around assignments when aligning."
63 (defcustom conf-colon-assignment-space nil
64 "Value for `conf-assignment-space' in colon style Conf mode buffers."
70 (let ((map (make-sparse-keymap)))
71 (define-key map
"\C-c\C-u" 'conf-unix-mode
)
72 (define-key map
"\C-c\C-w" 'conf-windows-mode
)
73 (define-key map
"\C-c\C-j" 'conf-javaprop-mode
)
74 (define-key map
"\C-c\C-s" 'conf-space-mode
)
75 (define-key map
"\C-c " 'conf-space-mode
)
76 (define-key map
"\C-c\C-c" 'conf-colon-mode
)
77 (define-key map
"\C-c:" 'conf-colon-mode
)
78 (define-key map
"\C-c\C-x" 'conf-xdefaults-mode
)
79 (define-key map
"\C-c\C-q" 'conf-quote-normal
)
80 (define-key map
"\C-c\"" 'conf-quote-normal
)
81 (define-key map
"\C-c'" 'conf-quote-normal
)
82 (define-key map
"\C-c\C-a" 'conf-align-assignments
)
84 "Local keymap for conf-mode buffers.")
86 (defvar conf-mode-syntax-table
87 (let ((table (make-syntax-table)))
88 (modify-syntax-entry ?
= "." table
)
89 (modify-syntax-entry ?_
"_" table
)
90 (modify-syntax-entry ?-
"_" table
)
91 (modify-syntax-entry ?.
"_" table
)
92 (modify-syntax-entry ?
\' "\"" table
)
93 ; (modify-syntax-entry ?: "_" table)
94 (modify-syntax-entry ?\
; "<" table)
95 (modify-syntax-entry ?
\n ">" table
)
96 (modify-syntax-entry ?
\r ">" table
)
98 "Syntax table in use in Windows style conf-mode buffers.")
100 (defvar conf-unix-mode-syntax-table
101 (let ((table (make-syntax-table conf-mode-syntax-table
)))
102 (modify-syntax-entry ?\
# "<" table
)
104 (modify-syntax-entry ?\
; "." table)
106 "Syntax table in use in Unix style conf-mode buffers.")
108 (defvar conf-javaprop-mode-syntax-table
109 (let ((table (make-syntax-table conf-unix-mode-syntax-table
)))
110 (modify-syntax-entry ?
/ ". 124" table
)
111 (modify-syntax-entry ?
* ". 23b" table
)
113 "Syntax table in use in Java prperties buffers.")
115 (defvar conf-xdefaults-mode-syntax-table
116 (let ((table (make-syntax-table conf-mode-syntax-table
)))
117 (modify-syntax-entry ?
! "<" table
)
119 (modify-syntax-entry ?\
; "." table)
121 "Syntax table in use in Xdefaults style conf-mode buffers.")
124 (defvar conf-font-lock-keywords
125 `(;; [section] (do this first because it may look like a parameter)
126 ("^[ \t]*\\[\\(.+\\)\\]" 1 'font-lock-type-face
)
127 ;; var=val or var[index]=val
128 ("^[ \t]*\\(.+?\\)\\(?:\\[\\(.*?\\)\\]\\)?[ \t]*="
129 (1 'font-lock-variable-name-face
)
130 (2 'font-lock-constant-face nil t
))
131 ;; section { ... } (do this last because some assign ...{...)
132 ("^[ \t]*\\([^=:\n]+?\\)[ \t\n]*{[^{}]*?$" 1 'font-lock-type-face prepend
))
133 "Keywords to hilight in Conf mode")
135 (defvar conf-javaprop-font-lock-keywords
137 ("^[ \t]*\\(.+?\\)\\(?:\\.\\([0-9]+\\)\\(?:\\.\\(.+?\\)\\(?:\\.\\([0-9]+\\)\\(?:\\.\\(.+?\\)\\(?:\\.\\([0-9]+\\)\\(\\..+?\\)?\\)?\\)?\\)?\\)?\\)?\\([:= \t]\\|$\\)"
138 (1 'font-lock-variable-name-face
)
139 (2 'font-lock-constant-face nil t
)
140 (3 'font-lock-variable-name-face nil t
)
141 (4 'font-lock-constant-face nil t
)
142 (5 'font-lock-variable-name-face nil t
)
143 (6 'font-lock-constant-face nil t
)
144 (7 'font-lock-variable-name-face nil t
)))
145 "Keywords to hilight in Conf Java Properties mode")
147 (defvar conf-space-keywords-alist
148 '(("\\`/etc/gpm/" .
"key\\|name\\|foreground\\|background\\|border\\|head")
149 ("\\`/etc/magic\\'" .
"[^ \t]+[ \t]+\\(?:[bl]?e?\\(?:short\\|long\\)\\|byte\\|string\\)[^ \t]*")
150 ("/mod\\(?:ules\\|probe\\)\\.conf" .
"alias\\|in\\(?:clude\\|stall\\)\\|options\\|remove")
151 ("/manpath\\.config" .
"MAN\\(?:DATORY_MANPATH\\|PATH_MAP\\|DB_MAP\\)")
152 ("/sensors\\.conf" .
"chip\\|bus\\|label\\|compute\\|set\\|ignore")
153 ("/sane\\(\\.d\\)?/" .
"option\\|device\\|port\\|usb\\|sc\\(?:si\\|anner\\)")
154 ("/resmgr\\.conf" .
"class\\|add\\|allow\\|deny")
155 ("/dictionary\\.lst\\'" .
"DICT\\|HYPH\\|THES")
156 ("/tuxracer/options" .
"set"))
157 "File name based settings for `conf-space-keywords'.")
159 (defvar conf-space-keywords nil
160 "Regexps for functions that may come before a space assignment.
161 This allows constructs such as
163 This variable is best set in the file local variables, or through
164 `conf-space-keywords-alist'.")
166 (defvar conf-space-font-lock-keywords
167 `(;; [section] (do this first because it may look like a parameter)
168 ("^[ \t]*\\[\\(.+\\)\\]" 1 'font-lock-type-face
)
169 ;; section { ... } (do this first because it looks like a parameter)
170 ("^[ \t]*\\(.+?\\)[ \t\n]*{[^{}]*?$" 1 'font-lock-type-face
)
172 (eval if conf-space-keywords
173 (list (concat "^[ \t]*\\(" conf-space-keywords
"\\)[ \t]+\\([^\000- ]+\\)")
174 '(1 'font-lock-keyword-face
)
175 '(2 'font-lock-variable-name-face
))
176 '("^[ \t]*\\([^\000- ]+\\)" 1 'font-lock-variable-name-face
)))
177 "Keywords to hilight in Conf Space mode")
179 (defvar conf-colon-font-lock-keywords
180 `(;; [section] (do this first because it may look like a parameter)
181 ("^[ \t]*\\[\\(.+\\)\\]" 1 'font-lock-type-face
)
183 ("^[ \t]*\\(.+?\\)[ \t]*:"
184 (1 'font-lock-variable-name-face
))
185 ;; section { ... } (do this last because some assign ...{...)
186 ("^[ \t]*\\([^:\n]+\\)[ \t\n]*{[^{}]*?$" 1 'font-lock-type-face prepend
))
187 "Keywords to hilight in Conf Colon mode")
189 (defvar conf-assignment-sign ?
=
190 "What sign is used for assignments.")
192 (defvar conf-assignment-regexp
".+?\\([ \t]*=[ \t]*\\)"
193 "Regexp to recognize assignments.
194 It is anchored after the first sexp on a line. There must a
195 grouping for the assignment sign, including leading and trailing
199 ;; If anybody can figure out how to get the same effect by configuring
200 ;; `align', I'd be glad to hear.
201 (defun conf-align-assignments (&optional arg
)
204 (prefix-numeric-value arg
)
205 conf-assignment-column
))
207 (goto-char (point-min))
209 (let ((cs (comment-beginning))) ; go before comment if within
210 (if cs
(goto-char cs
)))
211 (while (forward-comment 9)) ; max-int?
212 (when (and (not (eobp))
213 (looking-at conf-assignment-regexp
))
214 (goto-char (match-beginning 1))
215 (delete-region (point) (match-end 1))
216 (if conf-assignment-sign
219 (indent-to-column arg
)
220 (or (not conf-assignment-space
) (memq (char-before (point)) '(? ?
\t)) (insert ?
))
221 (insert conf-assignment-sign
(if (and conf-assignment-space
(not (eolp))) ?\
"")))
222 (insert (if conf-assignment-space ?\
"") conf-assignment-sign
)
224 (indent-to-column (- arg
))
225 (or (not conf-assignment-space
) (memq (char-before (point)) '(? ?
\t)) (insert ?
))))
227 (if (>= (current-column) (abs arg
))
229 (indent-to-column (abs arg
))))))
233 (defun conf-quote-normal ()
234 "Set the syntax of \" and ' to punctuation.
235 This only affects the current buffer. Some conf files use quotes
236 to delimit strings, while others allow quotes as simple parts of
237 the assigned value. In those files font locking will be wrong,
238 and you can correct it with this command. (Some files even do
239 both, i.e. quotes delimit strings, except when they are
240 unbalanced, but hey...)"
242 (let ((table (copy-syntax-table (syntax-table))))
243 (modify-syntax-entry ?
\" "." table
)
244 (modify-syntax-entry ?
\' "." table
)
245 (set-syntax-table table
)
246 (and (boundp 'font-lock-mode
)
248 (font-lock-fontify-buffer))))
251 (defun conf-outline-level ()
255 (while (setq pt
(scan-lists pt -
1 1)
257 (scan-error depth
))))
262 (defun conf-mode (&optional comment syntax-table name
)
263 "Mode for Unix and Windows Conf files and Java properties.
264 Most conf files know only three kinds of constructs: parameter
265 assignments optionally grouped into sections and comments. Yet
266 there is a great range of variation in the exact syntax of conf
267 files. See below for various wrapper commands that set up the
268 details for some of the most widespread variants.
270 This mode sets up font locking, outline, imenu and it provides
271 alignment support through `conf-align-assignments'. If strings
272 come out wrong, try `conf-quote-normal'.
274 Some files allow continuation lines, either with a backslash at
275 the end of line, or by indenting the next line (further). These
276 constructs cannot currently be recognized.
278 Because of this great variety of nuances, which are often not
279 even clearly specified, please don't expect it to get every file
280 quite right. Patches that clearly identify some special case,
281 without breaking the general ones, are welcome.
283 If instead you start this mode with the generic `conf-mode'
284 command, it will parse the buffer. It will generally well
285 identify the first four cases listed below. If the buffer
286 doesn't have enough contents to decide, this is identical to
287 `conf-windows-mode' on Windows, elsewhere to `conf-unix-mode'. See
288 also `conf-space-mode', `conf-colon-mode', `conf-javaprop-mode' and
289 `conf-xdefaults-mode'.
295 (let ((unix 0) (win 0) (equal 0) (colon 0) (space 0) (jp 0))
297 (goto-char (point-min))
299 (skip-chars-forward " \t\f")
300 (cond ((eq (char-after) ?\
#) (setq unix
(1+ unix
)))
301 ((eq (char-after) ?\
;) (setq win (1+ win)))
302 ((eq (char-after) ?\
[)) ; nop
304 ((eq (char-after) ?
})) ; nop
305 ;; recognize at most double spaces within names
306 ((looking-at "[^ \t\n=:]+\\(?: ?[^ \t\n=:]+\\)*[ \t]*[=:]")
307 (if (eq (char-before (match-end 0)) ?
=)
308 (setq equal
(1+ equal
))
309 (setq colon
(1+ colon
))))
310 ((looking-at "/[/*]") (setq jp
(1+ jp
)))
311 ((looking-at ".*{")) ; nop
312 ((setq space
(1+ space
))))
314 (if (> jp
(max unix win
3))
316 (if (> colon
(max equal space
))
318 (if (> space
(max equal colon
))
321 (and (= win unix
) (eq system-type
'windows-nt
)))
323 (conf-unix-mode))))))
324 (kill-all-local-variables)
325 (use-local-map conf-mode-map
)
327 (setq major-mode
'conf-mode
329 (set (make-local-variable 'comment-start
) comment
)
330 (set (make-local-variable 'comment-start-skip
)
331 (concat comment-start
"+\\s *"))
332 (set (make-local-variable 'comment-use-syntax
) t
)
333 (set (make-local-variable 'parse-sexp-ignore-comments
) t
)
334 (set (make-local-variable 'outline-regexp
)
335 "[ \t]*\\(?:\\[\\|.+[ \t\n]*{\\)")
336 (set (make-local-variable 'outline-heading-end-regexp
)
338 (set (make-local-variable 'outline-level
)
340 (set-syntax-table syntax-table
)
341 (setq imenu-generic-expression
342 '(("Parameters" "^[ \t]*\\(.+?\\)[ \t]*=" 1)
344 (nil "^[ \t]*\\[[ \t]*\\(.+\\)[ \t]*\\]" 1)
346 (nil "^[ \t]*\\([^=:\n]+\\)[ \t\n]*{" 1)))
348 (run-mode-hooks 'conf-mode-hook
)))
351 (defun conf-unix-mode ()
352 "Conf Mode starter for Unix style Conf files.
353 Comments start with `#'.
354 For details see `conf-mode'. Example:
356 # Conf mode font-locks this right on Unix and with C-c C-u
364 (conf-mode "#" conf-unix-mode-syntax-table
"Conf[Unix]"))
367 (defun conf-windows-mode ()
368 "Conf Mode starter for Windows style Conf files.
369 Comments start with `;'.
370 For details see `conf-mode'. Example:
372 ; Conf mode font-locks this right on Windows and with C-c C-w
374 \[ExtShellFolderViews]
375 Default={5984FFE0-28D4-11CF-AE66-08002B2E1262}
376 {5984FFE0-28D4-11CF-AE66-08002B2E1262}={5984FFE0-28D4-11CF-AE66-08002B2E1262}
378 \[{5984FFE0-28D4-11CF-AE66-08002B2E1262}]
379 PersistMoniker=file://Folder.htt"
381 (conf-mode ";" conf-mode-syntax-table
"Conf[WinIni]"))
383 ;; Here are a few more or less widespread styles. There are others, so
384 ;; obscure, they are not covered. E.g. RFC 2614 allows both Unix and Windows
385 ;; comments. Or the donkey has (* Pascal comments *) -- roll your own starter
389 (defun conf-javaprop-mode ()
390 "Conf Mode starter for Java properties files.
391 Comments start with `#' but are also recognized with `//' or
392 between `/*' and `*/'.
393 For details see `conf-mode'. Example:
395 # Conf mode font-locks this right with C-c C-j (Java properties)
396 // another kind of comment
406 (conf-mode "#" conf-javaprop-mode-syntax-table
"Conf[JavaProp]")
407 (set (make-local-variable 'conf-assignment-column
)
408 conf-javaprop-assignment-column
)
409 (set (make-local-variable 'conf-assignment-regexp
)
410 ".+?\\([ \t]*[=: \t][ \t]*\\|$\\)")
411 (set (make-local-variable 'conf-font-lock-keywords
)
412 conf-javaprop-font-lock-keywords
)
413 (setq comment-start-skip
"\\(?:#+\\|/[/*]+\\)\\s *")
414 (setq imenu-generic-expression
415 '(("Parameters" "^[ \t]*\\(.+?\\)[=: \t]" 1))))
418 (defun conf-space-mode (&optional keywords
)
419 "Conf Mode starter for space separated conf files.
420 \"Assignments\" are with ` '. Keywords before the parameters are
421 recognized according to `conf-space-keywords'. Interactively
422 with a prefix ARG of `0' no keywords will be recognized. With
423 any other prefix arg you will be prompted for a regexp to match
424 the keywords. Programmatically you can pass such a regexp as
425 KEYWORDS, or any non-nil non-string for no keywords.
427 For details see `conf-mode'. Example:
429 # Conf mode font-locks this right with C-c C-s (space separated)
431 image/jpeg jpeg jpg jpe
435 # Or with keywords (from a recognized file name):
437 # Standard multimedia devices
438 add /dev/audio desktop
439 add /dev/mixer desktop"
441 (list (if current-prefix-arg
442 (if (> (prefix-numeric-value current-prefix-arg
) 0)
443 (read-string "Regexp to match keywords: ")
446 (setq mode-name
"Conf[Space]")
447 (set (make-local-variable 'conf-assignment-sign
)
449 (set (make-local-variable 'conf-font-lock-keywords
)
450 conf-space-font-lock-keywords
)
451 ;; This doesn't seem right, but the next two depend on conf-space-keywords
452 ;; being set, while after-change-major-mode-hook might set up imenu, needing
453 ;; the following result:
454 (hack-local-variables-prop-line)
455 (hack-local-variables)
457 (set (make-local-variable 'conf-space-keywords
)
458 (if (stringp keywords
) keywords
))
459 (or conf-space-keywords
460 (not buffer-file-name
)
461 (set (make-local-variable 'conf-space-keywords
)
462 (assoc-default buffer-file-name conf-space-keywords-alist
464 (set (make-local-variable 'conf-assignment-regexp
)
465 (if conf-space-keywords
466 (concat "\\(?:" conf-space-keywords
"\\)[ \t]+.+?\\([ \t]+\\|$\\)")
467 ".+?\\([ \t]+\\|$\\)"))
468 (setq imenu-generic-expression
469 `(,@(cdr imenu-generic-expression
)
471 ,(if conf-space-keywords
472 (concat "^[ \t]*\\(?:" conf-space-keywords
473 "\\)[ \t]+\\([^ \t\n]+\\)\\(?:[ \t]\\|$\\)")
474 "^[ \t]*\\([^ \t\n[]+\\)\\(?:[ \t]\\|$\\)")
478 (defun conf-colon-mode (&optional comment syntax-table name
)
479 "Conf Mode starter for Colon files.
480 \"Assignments\" are with `:'.
481 For details see `conf-mode'. Example:
483 # Conf mode font-locks this right with C-c C-c (colon)
485 <Multi_key> <exclam> <exclam> : \"\\241\" exclamdown
486 <Multi_key> <c> <slash> : \"\\242\" cent"
489 (conf-mode comment syntax-table name
)
491 (setq mode-name
"Conf[Colon]"))
492 (set (make-local-variable 'conf-assignment-space
)
493 conf-colon-assignment-space
)
494 (set (make-local-variable 'conf-assignment-column
)
495 conf-colon-assignment-column
)
496 (set (make-local-variable 'conf-assignment-sign
)
498 (set (make-local-variable 'conf-assignment-regexp
)
499 ".+?\\([ \t]*:[ \t]*\\)")
500 (set (make-local-variable 'conf-font-lock-keywords
)
501 conf-colon-font-lock-keywords
)
502 (setq imenu-generic-expression
503 `(("Parameters" "^[ \t]*\\(.+?\\)[ \t]*:" 1)
504 ,@(cdr imenu-generic-expression
))))
507 (defun conf-xdefaults-mode ()
508 "Conf Mode starter for Xdefaults files.
509 Comments start with `!' and \"assignments\" are with `:'.
510 For details see `conf-mode'. Example:
512 ! Conf mode font-locks this right with C-c C-x (.Xdefaults)
517 (conf-colon-mode "!" conf-xdefaults-mode-syntax-table
"Conf[Xdefaults]"))
521 (if (boundp 'font-lock-defaults-alist
)
523 'font-lock-defaults-alist
525 (list 'conf-font-lock-keywords nil t nil nil
))))
530 ;; arch-tag: 0a3805b2-0371-4d3a-8498-8897116b2356
531 ;;; conf-mode.el ends here