Skip to content

Commit e2a15c8

Browse files
committed
* lisp/textmodes/conf-mode.el (conf-mode): Use define-derived-mode
(conf--guess-mode): Extract from conf-mode. (defcustoms): Remove redundant `:group` args. (conf-mode, conf-mode-initialize, conf-javaprop-mode) (conf-space-mode, conf-space-keywords, conf-space-mode-internal) (conf-colon-mode): Use `setq-local`.
1 parent f9685f9 commit e2a15c8

File tree

1 file changed

+75
-98
lines changed

1 file changed

+75
-98
lines changed

lisp/textmodes/conf-mode.el

Lines changed: 75 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -44,28 +44,23 @@
4444
"Align assignments to this column by default with \\[conf-align-assignments].
4545
If this number is negative, the `=' comes before the whitespace. Use 0 to
4646
not align (only setting space according to `conf-assignment-space')."
47-
:type 'integer
48-
:group 'conf)
47+
:type 'integer)
4948

5049
(defcustom conf-javaprop-assignment-column 32
5150
"Value for `conf-assignment-column' in Java properties buffers."
52-
:type 'integer
53-
:group 'conf)
51+
:type 'integer)
5452

5553
(defcustom conf-colon-assignment-column (- (abs conf-assignment-column))
5654
"Value for `conf-assignment-column' in Java properties buffers."
57-
:type 'integer
58-
:group 'conf)
55+
:type 'integer)
5956

6057
(defcustom conf-assignment-space t
6158
"Put at least one space around assignments when aligning."
62-
:type 'boolean
63-
:group 'conf)
59+
:type 'boolean)
6460

6561
(defcustom conf-colon-assignment-space nil
6662
"Value for `conf-assignment-space' in colon style Conf mode buffers."
67-
:type 'boolean
68-
:group 'conf)
63+
:type 'boolean)
6964

7065
(defvar conf-mode-map
7166
(let ((map (make-sparse-keymap))
@@ -349,9 +344,37 @@ unbalanced, but hey...)"
349344
(scan-error depth))))
350345

351346

347+
(defun conf--guess-mode ()
348+
"Try to guess sub-mode of `conf-mode' based on buffer content."
349+
(let ((unix 0) (win 0) (equal 0) (colon 0) (space 0) (jp 0))
350+
(save-excursion
351+
(goto-char (point-min))
352+
(while (not (eobp))
353+
(skip-chars-forward " \t\f")
354+
(cond ((eq (char-after) ?\#) (setq unix (1+ unix)))
355+
((eq (char-after) ?\;) (setq win (1+ win)))
356+
((eq (char-after) ?\[)) ; nop
357+
((eolp)) ; nop
358+
((eq (char-after) ?})) ; nop
359+
;; recognize at most double spaces within names
360+
((looking-at "[^ \t\n=:]+\\(?: ?[^ \t\n=:]+\\)*[ \t]*[=:]")
361+
(if (eq (char-before (match-end 0)) ?=)
362+
(setq equal (1+ equal))
363+
(setq colon (1+ colon))))
364+
((looking-at "/[/*]") (setq jp (1+ jp)))
365+
((looking-at ".*{")) ; nop
366+
((setq space (1+ space))))
367+
(forward-line)))
368+
(cond
369+
((> jp (max unix win 3)) #'conf-javaprop-mode)
370+
((> colon (max equal space)) #'conf-colon-mode)
371+
((> space (max equal colon)) #'conf-space-mode)
372+
((or (> win unix) (and (= win unix) (eq system-type 'windows-nt)))
373+
#'conf-windows-mode)
374+
(t #'conf-unix-mode))))
352375

353376
;;;###autoload
354-
(defun conf-mode ()
377+
(define-derived-mode conf-mode nil "Conf[?]"
355378
"Mode for Unix and Windows Conf files and Java properties.
356379
Most conf files know only three kinds of constructs: parameter
357380
assignments optionally grouped into sections and comments. Yet
@@ -382,75 +405,37 @@ See also `conf-space-mode', `conf-colon-mode', `conf-javaprop-mode',
382405
383406
\\{conf-mode-map}"
384407

385-
(interactive)
386408
;; `conf-mode' plays two roles: it's the parent of several sub-modes
387409
;; but it's also the function that chooses between those submodes.
388410
;; To tell the difference between those two cases where the function
389411
;; might be called, we check `delay-mode-hooks'.
390412
;; (adopted from tex-mode.el)
391413
(if (not delay-mode-hooks)
392-
;; try to guess sub-mode of conf-mode based on buffer content
393-
(let ((unix 0) (win 0) (equal 0) (colon 0) (space 0) (jp 0))
394-
(save-excursion
395-
(goto-char (point-min))
396-
(while (not (eobp))
397-
(skip-chars-forward " \t\f")
398-
(cond ((eq (char-after) ?\#) (setq unix (1+ unix)))
399-
((eq (char-after) ?\;) (setq win (1+ win)))
400-
((eq (char-after) ?\[)) ; nop
401-
((eolp)) ; nop
402-
((eq (char-after) ?})) ; nop
403-
;; recognize at most double spaces within names
404-
((looking-at "[^ \t\n=:]+\\(?: ?[^ \t\n=:]+\\)*[ \t]*[=:]")
405-
(if (eq (char-before (match-end 0)) ?=)
406-
(setq equal (1+ equal))
407-
(setq colon (1+ colon))))
408-
((looking-at "/[/*]") (setq jp (1+ jp)))
409-
((looking-at ".*{")) ; nop
410-
((setq space (1+ space))))
411-
(forward-line)))
412-
(cond
413-
((> jp (max unix win 3)) (conf-javaprop-mode))
414-
((> colon (max equal space)) (conf-colon-mode))
415-
((> space (max equal colon)) (conf-space-mode))
416-
((or (> win unix) (and (= win unix) (eq system-type 'windows-nt)))
417-
(conf-windows-mode))
418-
(t (conf-unix-mode))))
419-
420-
(kill-all-local-variables)
421-
(use-local-map conf-mode-map)
422-
(setq major-mode 'conf-mode
423-
mode-name "Conf[?]")
424-
(set (make-local-variable 'font-lock-defaults)
425-
'(conf-font-lock-keywords nil t nil nil))
414+
(funcall (conf--guess-mode))
415+
416+
(setq-local font-lock-defaults '(conf-font-lock-keywords nil t nil nil))
426417
;; Let newcomment.el decide this for itself.
427-
;; (set (make-local-variable 'comment-use-syntax) t)
428-
(set (make-local-variable 'parse-sexp-ignore-comments) t)
429-
(set (make-local-variable 'outline-regexp)
430-
"[ \t]*\\(?:\\[\\|.+[ \t\n]*{\\)")
431-
(set (make-local-variable 'outline-heading-end-regexp)
432-
"[\n}]")
433-
(set (make-local-variable 'outline-level)
434-
'conf-outline-level)
435-
(set-syntax-table conf-mode-syntax-table)
436-
(setq imenu-generic-expression
437-
'(("Parameters" "^[ \t]*\\(.+?\\)[ \t]*=" 1)
438-
;; [section]
439-
(nil "^[ \t]*\\[[ \t]*\\(.+\\)[ \t]*\\]" 1)
440-
;; section { ... }
441-
(nil "^[ \t]*\\([^=:{} \t\n][^=:{}\n]+\\)[ \t\n]*{" 1)))
442-
(run-mode-hooks 'conf-mode-hook)))
418+
;; (setq-local comment-use-syntax t)
419+
(setq-local parse-sexp-ignore-comments t)
420+
(setq-local outline-regexp "[ \t]*\\(?:\\[\\|.+[ \t\n]*{\\)")
421+
(setq-local outline-heading-end-regexp "[\n}]")
422+
(setq-local outline-level #'conf-outline-level)
423+
(setq-local imenu-generic-expression
424+
'(("Parameters" "^[ \t]*\\(.+?\\)[ \t]*=" 1)
425+
;; [section]
426+
(nil "^[ \t]*\\[[ \t]*\\(.+\\)[ \t]*\\]" 1)
427+
;; section { ... }
428+
(nil "^[ \t]*\\([^=:{} \t\n][^=:{}\n]+\\)[ \t\n]*{" 1)))))
443429

444430
(defun conf-mode-initialize (comment &optional font-lock)
445431
"Initializations for sub-modes of `conf-mode'.
446432
COMMENT initializes `comment-start' and `comment-start-skip'.
447433
The optional arg FONT-LOCK is the value for FONT-LOCK-KEYWORDS."
448-
(set (make-local-variable 'comment-start) comment)
449-
(set (make-local-variable 'comment-start-skip)
450-
(concat (regexp-quote comment-start) "+\\s *"))
434+
(setq-local comment-start comment)
435+
(setq-local comment-start-skip
436+
(concat (regexp-quote comment-start) "+\\s *"))
451437
(if font-lock
452-
(set (make-local-variable 'font-lock-defaults)
453-
`(,font-lock nil t nil nil))))
438+
(setq-local font-lock-defaults `(,font-lock nil t nil nil))))
454439

455440
;;;###autoload
456441
(define-derived-mode conf-unix-mode conf-mode "Conf[Unix]"
@@ -497,13 +482,11 @@ x.1 =
497482
x.2.y.1.z.1 =
498483
x.2.y.1.z.2.zz ="
499484
(conf-mode-initialize "#" 'conf-javaprop-font-lock-keywords)
500-
(set (make-local-variable 'conf-assignment-column)
501-
conf-javaprop-assignment-column)
502-
(set (make-local-variable 'conf-assignment-regexp)
503-
".+?\\([ \t]*[=: \t][ \t]*\\|$\\)")
504-
(setq comment-start-skip "\\(?:#+\\|/[/*]+\\)\\s *")
505-
(setq imenu-generic-expression
506-
'(("Parameters" "^[ \t]*\\(.+?\\)[=: \t]" 1))))
485+
(setq-local conf-assignment-column conf-javaprop-assignment-column)
486+
(setq-local conf-assignment-regexp ".+?\\([ \t]*[=: \t][ \t]*\\|$\\)")
487+
(setq-local comment-start-skip "\\(?:#+\\|/[/*]+\\)\\s *")
488+
(setq-local imenu-generic-expression
489+
'(("Parameters" "^[ \t]*\\(.+?\\)[=: \t]" 1))))
507490

508491
;;;###autoload
509492
(define-derived-mode conf-space-mode conf-unix-mode "Conf[Space]"
@@ -529,20 +512,18 @@ class desktop
529512
add /dev/audio desktop
530513
add /dev/mixer desktop"
531514
(conf-mode-initialize "#" 'conf-space-font-lock-keywords)
532-
(make-local-variable 'conf-assignment-sign)
533-
(setq conf-assignment-sign nil)
534-
(make-local-variable 'conf-space-keywords)
515+
(setq-local conf-assignment-sign nil)
535516
(cond (buffer-file-name
536517
;; We set conf-space-keywords directly, but a value which is
537518
;; in the local variables list or interactively specified
538519
;; (see the function conf-space-keywords) takes precedence.
539-
(setq conf-space-keywords
540-
(assoc-default buffer-file-name conf-space-keywords-alist
541-
'string-match))))
520+
(setq-local conf-space-keywords
521+
(assoc-default buffer-file-name conf-space-keywords-alist
522+
#'string-match))))
542523
(conf-space-mode-internal)
543524
;; In case the local variables list specifies conf-space-keywords,
544525
;; recompute other things from that afterward.
545-
(add-hook 'hack-local-variables-hook 'conf-space-mode-internal nil t))
526+
(add-hook 'hack-local-variables-hook #'conf-space-mode-internal nil t))
546527

547528
;;;###autoload
548529
(defun conf-space-keywords (keywords)
@@ -553,16 +534,16 @@ See `conf-space-mode'."
553534
(conf-space-mode))
554535
(if (string-equal keywords "")
555536
(setq keywords nil))
556-
(setq conf-space-keywords keywords)
537+
(setq-local conf-space-keywords keywords)
557538
(conf-space-mode-internal)
558539
(run-mode-hooks))
559540

560541
(defun conf-space-mode-internal ()
561-
(make-local-variable 'conf-assignment-regexp)
562-
(setq conf-assignment-regexp
563-
(if conf-space-keywords
564-
(concat "\\(?:" conf-space-keywords "\\)[ \t]+.+?\\([ \t]+\\|$\\)")
565-
".+?\\([ \t]+\\|$\\)"))
542+
(setq-local conf-assignment-regexp
543+
(if conf-space-keywords
544+
(concat "\\(?:" conf-space-keywords
545+
"\\)[ \t]+.+?\\([ \t]+\\|$\\)")
546+
".+?\\([ \t]+\\|$\\)"))
566547
;; If Font Lock is already enabled, reenable it with new
567548
;; conf-assignment-regexp.
568549
(when (and font-lock-mode
@@ -596,17 +577,13 @@ For details see `conf-mode'. Example:
596577
<Multi_key> <exclam> <exclam> : \"\\241\" exclamdown
597578
<Multi_key> <c> <slash> : \"\\242\" cent"
598579
(conf-mode-initialize "#" 'conf-colon-font-lock-keywords)
599-
(set (make-local-variable 'conf-assignment-space)
600-
conf-colon-assignment-space)
601-
(set (make-local-variable 'conf-assignment-column)
602-
conf-colon-assignment-column)
603-
(set (make-local-variable 'conf-assignment-sign)
604-
?:)
605-
(set (make-local-variable 'conf-assignment-regexp)
606-
".+?\\([ \t]*:[ \t]*\\)")
607-
(setq imenu-generic-expression
608-
`(("Parameters" "^[ \t]*\\(.+?\\)[ \t]*:" 1)
609-
,@(cdr imenu-generic-expression))))
580+
(setq-local conf-assignment-space conf-colon-assignment-space)
581+
(setq-local conf-assignment-column conf-colon-assignment-column)
582+
(setq-local conf-assignment-sign ?:)
583+
(setq-local conf-assignment-regexp ".+?\\([ \t]*:[ \t]*\\)")
584+
(setq-local imenu-generic-expression
585+
`(("Parameters" "^[ \t]*\\(.+?\\)[ \t]*:" 1)
586+
,@(cdr imenu-generic-expression))))
610587

611588
;;;###autoload
612589
(define-derived-mode conf-ppd-mode conf-colon-mode "Conf[PPD]"

0 commit comments

Comments
 (0)