conf-mode.el 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. ;;; conf-mode.el --- Simple major mode for editing conf/ini/properties files
  2. ;; Copyright (C) 2004-2017 Free Software Foundation, Inc.
  3. ;; Author: Daniel Pfeiffer <occitan@esperanto.org>
  4. ;; Keywords: conf ini windows java
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;
  18. ;; This mode is designed to edit many similar varieties of Conf/Ini files and
  19. ;; Java properties. It started out from Aurélien Tisné's ini-mode.
  20. ;; `conf-space-keywords' were inspired by Robert Fitzgerald's any-ini-mode.
  21. ;;; Code:
  22. (require 'newcomment)
  23. (defvar outline-heading-end-regexp)
  24. ;; Variables:
  25. (defgroup conf nil
  26. "Configuration files."
  27. :group 'data
  28. :version "22.1")
  29. (defcustom conf-assignment-column 24
  30. "Align assignments to this column by default with \\[conf-align-assignments].
  31. If this number is negative, the `=' comes before the whitespace. Use 0 to
  32. not align (only setting space according to `conf-assignment-space')."
  33. :type 'integer
  34. :group 'conf)
  35. (defcustom conf-javaprop-assignment-column 32
  36. "Value for `conf-assignment-column' in Java properties buffers."
  37. :type 'integer
  38. :group 'conf)
  39. (defcustom conf-colon-assignment-column (- (abs conf-assignment-column))
  40. "Value for `conf-assignment-column' in Java properties buffers."
  41. :type 'integer
  42. :group 'conf)
  43. (defcustom conf-assignment-space t
  44. "Put at least one space around assignments when aligning."
  45. :type 'boolean
  46. :group 'conf)
  47. (defcustom conf-colon-assignment-space nil
  48. "Value for `conf-assignment-space' in colon style Conf mode buffers."
  49. :type 'boolean
  50. :group 'conf)
  51. (defvar conf-mode-map
  52. (let ((map (make-sparse-keymap))
  53. (menu-map (make-sparse-keymap)))
  54. (define-key map "\C-c\C-u" 'conf-unix-mode)
  55. (define-key map "\C-c\C-w" 'conf-windows-mode)
  56. (define-key map "\C-c\C-j" 'conf-javaprop-mode)
  57. (define-key map "\C-c\C-s" 'conf-space-keywords)
  58. (define-key map "\C-c " 'conf-space-keywords)
  59. (define-key map "\C-c\C-c" 'conf-colon-mode)
  60. (define-key map "\C-c:" 'conf-colon-mode)
  61. (define-key map "\C-c\C-x" 'conf-xdefaults-mode)
  62. (define-key map "\C-c\C-p" 'conf-ppd-mode)
  63. (define-key map "\C-c\C-q" 'conf-quote-normal)
  64. (define-key map "\C-c\"" 'conf-quote-normal)
  65. (define-key map "\C-c'" 'conf-quote-normal)
  66. (define-key map "\C-c\C-a" 'conf-align-assignments)
  67. (define-key map [menu-bar sh-script] (cons "Conf" menu-map))
  68. (define-key menu-map [conf-windows-mode]
  69. '(menu-item "Windows mode"
  70. conf-windows-mode
  71. :help "Conf Mode starter for Windows style Conf files"
  72. :button (:radio . (eq major-mode 'conf-windows-mode))))
  73. (define-key menu-map [conf-javaprop-mode]
  74. '(menu-item "Java properties mode"
  75. conf-javaprop-mode
  76. :help "Conf Mode starter for Java properties files"
  77. :button (:radio . (eq major-mode 'conf-javaprop-mode))))
  78. (define-key menu-map [conf-space-keywords]
  79. '(menu-item "Space keywords mode..."
  80. conf-space-keywords
  81. :help "Enter Conf Space mode using regexp KEYWORDS to match the keywords"
  82. :button (:radio . (eq major-mode 'conf-space-keywords))))
  83. (define-key menu-map [conf-ppd-mode]
  84. '(menu-item "PPD mode"
  85. conf-ppd-mode
  86. :help "Conf Mode starter for Adobe/CUPS PPD files"
  87. :button (:radio . (eq major-mode 'conf-ppd-mode))))
  88. (define-key menu-map [conf-colon-mode]
  89. '(menu-item "Colon mode"
  90. conf-colon-mode
  91. :help "Conf Mode starter for Colon files"
  92. :button (:radio . (eq major-mode 'conf-colon-mode))))
  93. (define-key menu-map [conf-unix-mode]
  94. '(menu-item "Unix mode"
  95. conf-unix-mode
  96. :help "Conf Mode starter for Unix style Conf files"
  97. :button (:radio . (eq major-mode 'conf-unix-mode))))
  98. (define-key menu-map [conf-xdefaults-mode]
  99. '(menu-item "Xdefaults mode"
  100. conf-xdefaults-mode
  101. :help "Conf Mode starter for Xdefaults files"
  102. :button (:radio . (eq major-mode 'conf-xdefaults-mode))))
  103. (define-key menu-map [c-s0] '("--"))
  104. (define-key menu-map [conf-quote-normal]
  105. '(menu-item "Set quote syntax normal" conf-quote-normal
  106. :help "Set the syntax of \\=' and \" to punctuation"))
  107. (define-key menu-map [conf-align-assignments]
  108. '(menu-item "Align assignments" conf-align-assignments
  109. :help "Align assignments"))
  110. map)
  111. "Local keymap for `conf-mode' buffers.")
  112. (defvar conf-mode-syntax-table
  113. (let ((table (make-syntax-table)))
  114. (modify-syntax-entry ?= "." table)
  115. (modify-syntax-entry ?_ "_" table)
  116. (modify-syntax-entry ?- "_" table)
  117. (modify-syntax-entry ?. "_" table)
  118. (modify-syntax-entry ?\' "\"" table)
  119. (modify-syntax-entry ?\; "<" table)
  120. (modify-syntax-entry ?\n ">" table)
  121. (modify-syntax-entry ?\r ">" table)
  122. table)
  123. "Syntax table in use in Windows style `conf-mode' buffers.")
  124. (defvar conf-unix-mode-syntax-table
  125. (let ((table (make-syntax-table conf-mode-syntax-table)))
  126. (modify-syntax-entry ?\# "<" table)
  127. ;; override
  128. (modify-syntax-entry ?\; "." table)
  129. table)
  130. "Syntax table in use in Unix style `conf-mode' buffers.")
  131. (defvar conf-javaprop-mode-syntax-table
  132. (let ((table (make-syntax-table conf-unix-mode-syntax-table)))
  133. (modify-syntax-entry ?/ ". 124" table)
  134. (modify-syntax-entry ?* ". 23b" table)
  135. table)
  136. "Syntax table in use in Java properties buffers.")
  137. (defvar conf-ppd-mode-syntax-table
  138. (let ((table (make-syntax-table conf-mode-syntax-table)))
  139. (modify-syntax-entry ?* ". 1" table)
  140. (modify-syntax-entry ?% ". 2" table)
  141. ;; override
  142. (modify-syntax-entry ?\' "." table)
  143. (modify-syntax-entry ?\; "." table)
  144. table)
  145. "Syntax table in use in PPD `conf-mode' buffers.")
  146. (defvar conf-xdefaults-mode-syntax-table
  147. (let ((table (make-syntax-table conf-mode-syntax-table)))
  148. (modify-syntax-entry ?! "<" table)
  149. ;; override
  150. (modify-syntax-entry ?\; "." table)
  151. table)
  152. "Syntax table in use in Xdefaults style `conf-mode' buffers.")
  153. (defvar conf-toml-mode-syntax-table
  154. (let ((table (make-syntax-table conf-mode-syntax-table)))
  155. (modify-syntax-entry ?\" "\"" table)
  156. (modify-syntax-entry ?' "\"" table)
  157. (modify-syntax-entry ?\\ "\\" table)
  158. (modify-syntax-entry ?# "<" table)
  159. ;; override
  160. (modify-syntax-entry ?\; "." table)
  161. table)
  162. "Syntax table in use in TOML style `conf-mode' buffers.")
  163. (defvar conf-font-lock-keywords
  164. '(;; [section] (do this first because it may look like a parameter)
  165. ("^[ \t]*\\[\\(.+\\)\\]" 1 'font-lock-type-face)
  166. ;; var=val or var[index]=val
  167. ("^[ \t]*\\(.+?\\)\\(?:\\[\\(.*?\\)\\]\\)?[ \t]*="
  168. (1 'font-lock-variable-name-face)
  169. (2 'font-lock-constant-face nil t))
  170. ;; section { ... } (do this last because some assign ...{...)
  171. ("^[ \t]*\\([^=:\n]+?\\)[ \t\n]*{[^{}]*?$" 1 'font-lock-type-face prepend))
  172. "Keywords to highlight in Conf mode.")
  173. (defvar conf-javaprop-font-lock-keywords
  174. '(;; var=val
  175. ("^[ \t]*\\(.+?\\)\\(?:\\.\\([0-9]+\\)\\(?:\\.\\(.+?\\)\\(?:\\.\\([0-9]+\\)\\(?:\\.\\(.+?\\)\\(?:\\.\\([0-9]+\\)\\(\\..+?\\)?\\)?\\)?\\)?\\)?\\)?\\([:= \t]\\|$\\)"
  176. (1 'font-lock-variable-name-face)
  177. (2 'font-lock-constant-face nil t)
  178. (3 'font-lock-variable-name-face nil t)
  179. (4 'font-lock-constant-face nil t)
  180. (5 'font-lock-variable-name-face nil t)
  181. (6 'font-lock-constant-face nil t)
  182. (7 'font-lock-variable-name-face nil t)))
  183. "Keywords to highlight in Conf Java Properties mode.")
  184. (defvar conf-space-keywords-alist
  185. '(("\\`/etc/gpm/" . "key\\|name\\|foreground\\|background\\|border\\|head")
  186. ("\\`/etc/magic\\'" . "[^ \t]+[ \t]+\\(?:[bl]?e?\\(?:short\\|long\\)\\|byte\\|string\\)[^ \t]*")
  187. ("/mod\\(?:ules\\|probe\\)\\.conf" . "alias\\|in\\(?:clude\\|stall\\)\\|options\\|remove")
  188. ("/manpath\\.config" . "MAN\\(?:DATORY_MANPATH\\|PATH_MAP\\|DB_MAP\\)")
  189. ("/sensors\\.conf" . "chip\\|bus\\|label\\|compute\\|set\\|ignore")
  190. ("/sane\\(\\.d\\)?/" . "option\\|device\\|port\\|usb\\|sc\\(?:si\\|anner\\)")
  191. ("/resmgr\\.conf" . "class\\|add\\|allow\\|deny")
  192. ("/dictionary\\.lst\\'" . "DICT\\|HYPH\\|THES")
  193. ("/tuxracer/options" . "set"))
  194. "File-name-based settings for the variable `conf-space-keywords'.")
  195. (defvar conf-space-keywords nil
  196. "Regexps for functions that may come before a space assignment.
  197. This allows constructs such as
  198. keyword var value
  199. This variable is best set in the file local variables, or through
  200. `conf-space-keywords-alist'.")
  201. (put 'conf-space-keywords 'safe-local-variable 'stringp)
  202. (defvar conf-space-font-lock-keywords
  203. `(;; [section] (do this first because it may look like a parameter)
  204. ("^[ \t]*\\[\\(.+\\)\\]" 1 'font-lock-type-face)
  205. ;; section { ... } (do this first because it looks like a parameter)
  206. ("^[ \t]*\\(.+?\\)[ \t\n]*{[^{}]*?$" 1 'font-lock-type-face)
  207. ;; var val
  208. (eval if conf-space-keywords
  209. (list (concat "^[ \t]*\\(" conf-space-keywords "\\)[ \t]+\\([^\000- ]+\\)")
  210. '(1 'font-lock-keyword-face)
  211. '(2 'font-lock-variable-name-face))
  212. '("^[ \t]*\\([^\000- ]+\\)" 1 'font-lock-variable-name-face)))
  213. "Keywords to highlight in Conf Space mode.")
  214. (defvar conf-colon-font-lock-keywords
  215. `(;; [section] (do this first because it may look like a parameter)
  216. ("^[ \t]*\\[\\(.+\\)\\]" 1 'font-lock-type-face)
  217. ;; var: val
  218. ("^[ \t]*\\(.+?\\)[ \t]*:"
  219. (1 'font-lock-variable-name-face))
  220. ;; section { ... } (do this last because some assign ...{...)
  221. ("^[ \t]*\\([^:\n]+\\)[ \t\n]*{[^{}]*?$" 1 'font-lock-type-face prepend))
  222. "Keywords to highlight in Conf Colon mode.")
  223. (defvar conf-toml-font-lock-keywords
  224. '(;; [section] (do this first because it may look like a parameter)
  225. (conf-toml-recognize-section 0 'font-lock-type-face prepend)
  226. ;; var=val or var[index]=val
  227. ("^\\s-*\\(.+?\\)\\(?:\\[\\(.*?\\)\\]\\)?\\s-*="
  228. (1 'font-lock-variable-name-face)
  229. (2 'font-lock-constant-face nil t))
  230. ("\\_<false\\|true\\_>" 0 'font-lock-keyword-face))
  231. "Keywords to highlight in Conf TOML mode.")
  232. (defvar conf-desktop-font-lock-keywords
  233. `(,@conf-font-lock-keywords
  234. ("\\_<false\\|true\\_>" 0 'font-lock-constant-face)
  235. ("\\_<%[uUfFick%]\\_>" 0 'font-lock-constant-face))
  236. "Keywords to highlight in Conf Desktop mode.")
  237. (defvar conf-assignment-sign ?=
  238. "Sign used for assignments (char or string).")
  239. (defvar conf-assignment-regexp ".+?\\([ \t]*=[ \t]*\\)"
  240. "Regexp to recognize assignments.
  241. It is anchored after the first sexp on a line. There must be a
  242. grouping for the assignment sign, including leading and trailing
  243. whitespace.")
  244. ;; If anybody can figure out how to get the same effect by configuring
  245. ;; `align', I'd be glad to hear.
  246. (defun conf-align-assignments (&optional arg)
  247. (interactive "P")
  248. "Align the assignments in the buffer or active region.
  249. In Transient Mark mode, if the mark is active, operate on the
  250. contents of the region. Otherwise, operate on the whole buffer."
  251. (setq arg (if arg
  252. (prefix-numeric-value arg)
  253. conf-assignment-column))
  254. (save-excursion
  255. (save-restriction
  256. (when (use-region-p)
  257. (narrow-to-region (region-beginning) (region-end)))
  258. (goto-char (point-min))
  259. (while (not (eobp))
  260. (let ((cs (comment-beginning))) ; go before comment if within
  261. (if cs (goto-char cs)))
  262. (while (forward-comment 9)) ; max-int?
  263. (when (and (not (eobp))
  264. (looking-at conf-assignment-regexp))
  265. (goto-char (match-beginning 1))
  266. (delete-region (point) (match-end 1))
  267. (if conf-assignment-sign
  268. (if (>= arg 0)
  269. (progn
  270. (indent-to-column arg)
  271. (or (not conf-assignment-space)
  272. (memq (char-before (point)) '(?\s ?\t)) (insert ?\s))
  273. (insert conf-assignment-sign
  274. (if (and conf-assignment-space (not (eolp))) ?\s "")))
  275. (insert (if conf-assignment-space ?\s "") conf-assignment-sign)
  276. (unless (eolp)
  277. (indent-to-column (- arg))
  278. (or (not conf-assignment-space)
  279. (memq (char-before (point)) '(?\s ?\t)) (insert ?\s))))
  280. (unless (eolp)
  281. (if (>= (current-column) (abs arg))
  282. (insert ?\s)
  283. (indent-to-column (abs arg))))))
  284. (forward-line)))))
  285. (defun conf-quote-normal (arg)
  286. "Set the syntax of \\=' and \" to punctuation.
  287. With prefix arg, only do it for \\=' if 1, or only for \" if 2.
  288. This only affects the current buffer. Some conf files use quotes
  289. to delimit strings, while others allow quotes as simple parts of
  290. the assigned value. In those files font locking will be wrong,
  291. and you can correct it with this command. (Some files even do
  292. both, i.e. quotes delimit strings, except when they are
  293. unbalanced, but hey...)"
  294. (interactive "P")
  295. (let ((table (copy-syntax-table (syntax-table))))
  296. (when (or (not arg) (= (prefix-numeric-value arg) 1))
  297. (modify-syntax-entry ?\' "." table))
  298. (when (or (not arg) (= (prefix-numeric-value arg) 2))
  299. (modify-syntax-entry ?\" "." table))
  300. (set-syntax-table table)
  301. (font-lock-flush)))
  302. (defun conf-outline-level ()
  303. (let ((depth 0)
  304. (pt (match-end 0)))
  305. (condition-case nil
  306. (while (setq pt (scan-lists pt -1 1)
  307. depth (1+ depth)))
  308. (scan-error depth))))
  309. ;;;###autoload
  310. (defun conf-mode ()
  311. "Mode for Unix and Windows Conf files and Java properties.
  312. Most conf files know only three kinds of constructs: parameter
  313. assignments optionally grouped into sections and comments. Yet
  314. there is a great range of variation in the exact syntax of conf
  315. files. See below for various wrapper commands that set up the
  316. details for some of the most widespread variants.
  317. This mode sets up font locking, outline, imenu and it provides
  318. alignment support through `conf-align-assignments'. If strings
  319. come out wrong, try `conf-quote-normal'.
  320. Some files allow continuation lines, either with a backslash at
  321. the end of line, or by indenting the next line (further). These
  322. constructs cannot currently be recognized.
  323. Because of this great variety of nuances, which are often not
  324. even clearly specified, please don't expect it to get every file
  325. quite right. Patches that clearly identify some special case,
  326. without breaking the general ones, are welcome.
  327. If instead you start this mode with the generic `conf-mode'
  328. command, it will parse the buffer. It will generally well
  329. identify the first four cases listed below. If the buffer
  330. doesn't have enough contents to decide, this is identical to
  331. `conf-windows-mode' on Windows, elsewhere to `conf-unix-mode'.
  332. See also `conf-space-mode', `conf-colon-mode', `conf-javaprop-mode',
  333. `conf-ppd-mode' and `conf-xdefaults-mode'.
  334. \\{conf-mode-map}"
  335. (interactive)
  336. ;; `conf-mode' plays two roles: it's the parent of several sub-modes
  337. ;; but it's also the function that chooses between those submodes.
  338. ;; To tell the difference between those two cases where the function
  339. ;; might be called, we check `delay-mode-hooks'.
  340. ;; (adopted from tex-mode.el)
  341. (if (not delay-mode-hooks)
  342. ;; try to guess sub-mode of conf-mode based on buffer content
  343. (let ((unix 0) (win 0) (equal 0) (colon 0) (space 0) (jp 0))
  344. (save-excursion
  345. (goto-char (point-min))
  346. (while (not (eobp))
  347. (skip-chars-forward " \t\f")
  348. (cond ((eq (char-after) ?\#) (setq unix (1+ unix)))
  349. ((eq (char-after) ?\;) (setq win (1+ win)))
  350. ((eq (char-after) ?\[)) ; nop
  351. ((eolp)) ; nop
  352. ((eq (char-after) ?})) ; nop
  353. ;; recognize at most double spaces within names
  354. ((looking-at "[^ \t\n=:]+\\(?: ?[^ \t\n=:]+\\)*[ \t]*[=:]")
  355. (if (eq (char-before (match-end 0)) ?=)
  356. (setq equal (1+ equal))
  357. (setq colon (1+ colon))))
  358. ((looking-at "/[/*]") (setq jp (1+ jp)))
  359. ((looking-at ".*{")) ; nop
  360. ((setq space (1+ space))))
  361. (forward-line)))
  362. (cond
  363. ((> jp (max unix win 3)) (conf-javaprop-mode))
  364. ((> colon (max equal space)) (conf-colon-mode))
  365. ((> space (max equal colon)) (conf-space-mode))
  366. ((or (> win unix) (and (= win unix) (eq system-type 'windows-nt)))
  367. (conf-windows-mode))
  368. (t (conf-unix-mode))))
  369. (kill-all-local-variables)
  370. (use-local-map conf-mode-map)
  371. (setq major-mode 'conf-mode
  372. mode-name "Conf[?]")
  373. (set (make-local-variable 'font-lock-defaults)
  374. '(conf-font-lock-keywords nil t nil nil))
  375. ;; Let newcomment.el decide this for itself.
  376. ;; (set (make-local-variable 'comment-use-syntax) t)
  377. (set (make-local-variable 'parse-sexp-ignore-comments) t)
  378. (set (make-local-variable 'outline-regexp)
  379. "[ \t]*\\(?:\\[\\|.+[ \t\n]*{\\)")
  380. (set (make-local-variable 'outline-heading-end-regexp)
  381. "[\n}]")
  382. (set (make-local-variable 'outline-level)
  383. 'conf-outline-level)
  384. (set-syntax-table conf-mode-syntax-table)
  385. (setq imenu-generic-expression
  386. '(("Parameters" "^[ \t]*\\(.+?\\)[ \t]*=" 1)
  387. ;; [section]
  388. (nil "^[ \t]*\\[[ \t]*\\(.+\\)[ \t]*\\]" 1)
  389. ;; section { ... }
  390. (nil "^[ \t]*\\([^=:{} \t\n][^=:{}\n]+\\)[ \t\n]*{" 1)))
  391. (run-mode-hooks 'conf-mode-hook)))
  392. (defun conf-mode-initialize (comment &optional font-lock)
  393. "Initializations for sub-modes of conf-mode.
  394. COMMENT initializes `comment-start' and `comment-start-skip'.
  395. The optional arg FONT-LOCK is the value for FONT-LOCK-KEYWORDS."
  396. (set (make-local-variable 'comment-start) comment)
  397. (set (make-local-variable 'comment-start-skip)
  398. (concat (regexp-quote comment-start) "+\\s *"))
  399. (if font-lock
  400. (set (make-local-variable 'font-lock-defaults)
  401. `(,font-lock nil t nil nil))))
  402. ;;;###autoload
  403. (define-derived-mode conf-unix-mode conf-mode "Conf[Unix]"
  404. "Conf Mode starter for Unix style Conf files.
  405. Comments start with `#'. For details see `conf-mode'."
  406. (conf-mode-initialize "#"))
  407. ;;;###autoload
  408. (define-derived-mode conf-windows-mode conf-mode "Conf[WinIni]"
  409. "Conf Mode starter for Windows style Conf files.
  410. Comments start with `;'.
  411. For details see `conf-mode'. Example:
  412. ; Conf mode font-locks this right on Windows and with \\[conf-windows-mode]
  413. [ExtShellFolderViews]
  414. Default={5984FFE0-28D4-11CF-AE66-08002B2E1262}
  415. {5984FFE0-28D4-11CF-AE66-08002B2E1262}={5984FFE0-28D4-11CF-AE66-08002B2E1262}
  416. [{5984FFE0-28D4-11CF-AE66-08002B2E1262}]
  417. PersistMoniker=file://Folder.htt"
  418. (conf-mode-initialize ";"))
  419. ;; Here are a few more or less widespread styles. There are others, so
  420. ;; obscure, they are not covered. E.g. RFC 2614 allows both Unix and Windows
  421. ;; comments. Or the donkey has (* Pascal comments *) -- roll your own starter
  422. ;; if you need it.
  423. ;;;###autoload
  424. (define-derived-mode conf-javaprop-mode conf-mode "Conf[JavaProp]"
  425. "Conf Mode starter for Java properties files.
  426. Comments start with `#' but are also recognized with `//' or
  427. between `/*' and `*/'.
  428. For details see `conf-mode'. Example:
  429. # Conf mode font-locks this right with \\[conf-javaprop-mode] (Java properties)
  430. // another kind of comment
  431. /* yet another */
  432. name:value
  433. name=value
  434. name value
  435. x.1 =
  436. x.2.y.1.z.1 =
  437. x.2.y.1.z.2.zz ="
  438. (conf-mode-initialize "#" 'conf-javaprop-font-lock-keywords)
  439. (set (make-local-variable 'conf-assignment-column)
  440. conf-javaprop-assignment-column)
  441. (set (make-local-variable 'conf-assignment-regexp)
  442. ".+?\\([ \t]*[=: \t][ \t]*\\|$\\)")
  443. (setq comment-start-skip "\\(?:#+\\|/[/*]+\\)\\s *")
  444. (setq imenu-generic-expression
  445. '(("Parameters" "^[ \t]*\\(.+?\\)[=: \t]" 1))))
  446. ;;;###autoload
  447. (define-derived-mode conf-space-mode conf-unix-mode "Conf[Space]"
  448. "Conf Mode starter for space separated conf files.
  449. \"Assignments\" are with ` '. Keywords before the parameters are
  450. recognized according to the variable `conf-space-keywords-alist'.
  451. Alternatively, you can specify a value for the file local variable
  452. `conf-space-keywords'.
  453. Use the function `conf-space-keywords' if you want to specify keywords
  454. in an interactive fashion instead.
  455. For details see `conf-mode'. Example:
  456. # Conf mode font-locks this right with \\[conf-space-mode] (space separated)
  457. image/jpeg jpeg jpg jpe
  458. image/png png
  459. image/tiff tiff tif
  460. # Or with keywords (from a recognized file name):
  461. class desktop
  462. # Standard multimedia devices
  463. add /dev/audio desktop
  464. add /dev/mixer desktop"
  465. (conf-mode-initialize "#" 'conf-space-font-lock-keywords)
  466. (make-local-variable 'conf-assignment-sign)
  467. (setq conf-assignment-sign nil)
  468. (make-local-variable 'conf-space-keywords)
  469. (cond (buffer-file-name
  470. ;; We set conf-space-keywords directly, but a value which is
  471. ;; in the local variables list or interactively specified
  472. ;; (see the function conf-space-keywords) takes precedence.
  473. (setq conf-space-keywords
  474. (assoc-default buffer-file-name conf-space-keywords-alist
  475. 'string-match))))
  476. (conf-space-mode-internal)
  477. ;; In case the local variables list specifies conf-space-keywords,
  478. ;; recompute other things from that afterward.
  479. (add-hook 'hack-local-variables-hook 'conf-space-mode-internal nil t))
  480. ;;;###autoload
  481. (defun conf-space-keywords (keywords)
  482. "Enter Conf Space mode using regexp KEYWORDS to match the keywords.
  483. See `conf-space-mode'."
  484. (interactive "sConf Space keyword regexp: ")
  485. (delay-mode-hooks
  486. (conf-space-mode))
  487. (if (string-equal keywords "")
  488. (setq keywords nil))
  489. (setq conf-space-keywords keywords)
  490. (conf-space-mode-internal)
  491. (run-mode-hooks))
  492. (defun conf-space-mode-internal ()
  493. (make-local-variable 'conf-assignment-regexp)
  494. (setq conf-assignment-regexp
  495. (if conf-space-keywords
  496. (concat "\\(?:" conf-space-keywords "\\)[ \t]+.+?\\([ \t]+\\|$\\)")
  497. ".+?\\([ \t]+\\|$\\)"))
  498. ;; If Font Lock is already enabled, reenable it with new
  499. ;; conf-assignment-regexp.
  500. (when (and font-lock-mode
  501. (boundp 'font-lock-keywords)) ;see `normal-mode'
  502. (font-lock-add-keywords nil nil)
  503. (font-lock-mode 1))
  504. ;; Copy so that we don't destroy shared structure.
  505. (setq imenu-generic-expression (copy-sequence imenu-generic-expression))
  506. ;; Get rid of any existing Parameters element.
  507. (setq imenu-generic-expression
  508. (delq (assoc "Parameters" imenu-generic-expression)
  509. imenu-generic-expression))
  510. ;; Add a new one based on conf-space-keywords.
  511. (setq imenu-generic-expression
  512. (cons `("Parameters"
  513. ,(if conf-space-keywords
  514. (concat "^[ \t]*\\(?:" conf-space-keywords
  515. "\\)[ \t]+\\([^ \t\n]+\\)\\(?:[ \t]\\|$\\)")
  516. "^[ \t]*\\([^ \t\n[]+\\)\\(?:[ \t]\\|$\\)")
  517. 1)
  518. imenu-generic-expression)))
  519. ;;;###autoload
  520. (define-derived-mode conf-colon-mode conf-unix-mode "Conf[Colon]"
  521. "Conf Mode starter for Colon files.
  522. \"Assignments\" are with `:'.
  523. For details see `conf-mode'. Example:
  524. # Conf mode font-locks this right with \\[conf-colon-mode] (colon)
  525. <Multi_key> <exclam> <exclam> : \"\\241\" exclamdown
  526. <Multi_key> <c> <slash> : \"\\242\" cent"
  527. (conf-mode-initialize "#" 'conf-colon-font-lock-keywords)
  528. (set (make-local-variable 'conf-assignment-space)
  529. conf-colon-assignment-space)
  530. (set (make-local-variable 'conf-assignment-column)
  531. conf-colon-assignment-column)
  532. (set (make-local-variable 'conf-assignment-sign)
  533. ?:)
  534. (set (make-local-variable 'conf-assignment-regexp)
  535. ".+?\\([ \t]*:[ \t]*\\)")
  536. (setq imenu-generic-expression
  537. `(("Parameters" "^[ \t]*\\(.+?\\)[ \t]*:" 1)
  538. ,@(cdr imenu-generic-expression))))
  539. ;;;###autoload
  540. (define-derived-mode conf-ppd-mode conf-colon-mode "Conf[PPD]"
  541. "Conf Mode starter for Adobe/CUPS PPD files.
  542. Comments start with `*%' and \"assignments\" are with `:'.
  543. For details see `conf-mode'. Example:
  544. *% Conf mode font-locks this right with \\[conf-ppd-mode] (PPD)
  545. *DefaultTransfer: Null
  546. *Transfer Null.Inverse: \"{ 1 exch sub }\""
  547. (conf-mode-initialize "*%")
  548. ;; no sections, they match within PostScript code
  549. (setq imenu-generic-expression (list (car imenu-generic-expression))))
  550. ;;;###autoload
  551. (define-derived-mode conf-xdefaults-mode conf-colon-mode "Conf[Xdefaults]"
  552. "Conf Mode starter for Xdefaults files.
  553. Comments start with `!' and \"assignments\" are with `:'.
  554. For details see `conf-mode'. Example:
  555. ! Conf mode font-locks this right with \\[conf-xdefaults-mode] (.Xdefaults)
  556. *background: gray99
  557. *foreground: black"
  558. (conf-mode-initialize "!"))
  559. (defun conf-toml-recognize-section (limit)
  560. "Font-lock helper function for conf-toml-mode.
  561. Handles recognizing TOML section names, like [section],
  562. \[[section]], or [something.\"else\".section]."
  563. (save-excursion
  564. ;; Skip any number of "[" to handle things like [[section]].
  565. (when (re-search-forward "^\\s-*\\[+" limit t)
  566. (let ((start (point)))
  567. (backward-char)
  568. (let ((end (min limit
  569. (condition-case nil
  570. (progn
  571. (forward-list)
  572. (1- (point)))
  573. (scan-error
  574. (end-of-line)
  575. (point))))))
  576. ;; If there is a comma in the text, then we assume this is
  577. ;; an array and not a section. (This could be refined to
  578. ;; look only for unquoted commas if necessary.)
  579. (save-excursion
  580. (goto-char start)
  581. (unless (search-forward "," end t)
  582. (set-match-data (list start end))
  583. t)))))))
  584. ;;;###autoload
  585. (define-derived-mode conf-toml-mode conf-mode "Conf[TOML]"
  586. "Conf Mode starter for TOML files.
  587. Comments start with `#' and \"assignments\" are with `='.
  588. For details see `conf-mode'. Example:
  589. # Conf mode font-locks this right with \\[conf-toml-mode]
  590. \[entry]
  591. value = \"some string\""
  592. (conf-mode-initialize "#" 'conf-toml-font-lock-keywords)
  593. (setq-local conf-assignment-column 0)
  594. (setq-local conf-assignment-sign ?=))
  595. ;;;###autoload
  596. (define-derived-mode conf-desktop-mode conf-unix-mode "Conf[Desktop]"
  597. "Conf Mode started for freedesktop.org Desktop files.
  598. Comments start with `#' and \"assignments\" are with `='.
  599. For details see `conf-mode'.
  600. # Conf mode font-locks this correctly with \\[conf-desktop-mode]
  601. [Desktop Entry]
  602. Name=GNU Image Manipulation Program
  603. Name[oc]=Editor d'imatge GIMP
  604. Exec=gimp-2.8 %U
  605. Terminal=false"
  606. (conf-mode-initialize "#" 'conf-desktop-font-lock-keywords)
  607. (conf-quote-normal nil))
  608. (provide 'conf-mode)
  609. ;;; conf-mode.el ends here