-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
gitconfig-mode.el
138 lines (121 loc) · 5.1 KB
/
gitconfig-mode.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
;;; gitconfig-mode.el --- Major mode for editing .gitconfig files -*- lexical-binding:t -*-
;; Copyright (c) 2012-2013 Sebastian Wiesner
;; Copyright (C) 2012-2024 The Magit Project Contributors
;; Author: Sebastian Wiesner <lunaryorn@gmail.com>
;; Maintainer: Jonas Bernoulli <emacs.git-modes@jonas.bernoulli.dev>
;; Homepage: https://github.com/magit/git-modes
;; Keywords: convenience vc git
;; SPDX-License-Identifier: GPL-3.0-or-later
;; This file is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published
;; by the Free Software Foundation, either version 3 of the License,
;; or (at your option) any later version.
;;
;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this file. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; A major mode for editing .gitconfig files.
;;; Code:
(require 'compat)
(require 'conf-mode)
(require 'rx)
(defun gitconfig-line-indented-p ()
"Return t if the current line is indented correctly."
(save-excursion
(beginning-of-line)
(or (looking-at (rx line-start "["
symbol-start
(minimal-match (zero-or-more not-newline))
symbol-end "]"))
(looking-at (concat (rx line-start)
(gitconfig-indentation-string)
(rx symbol-start (or (syntax word)
(syntax symbol)))))
(looking-at (rx (zero-or-one "\t") (or "#" ";"))))))
(defun gitconfig-point-in-indentation-p ()
"Return if the point is in the indentation of the current line."
(save-excursion
(let ((pos (point)))
(back-to-indentation)
(<= pos (point)))))
(defun gitconfig-indent-line ()
"Indent the current line."
(interactive)
(if (gitconfig-line-indented-p)
(when (gitconfig-point-in-indentation-p)
(back-to-indentation))
(let ((old-point (point-marker))
(was-in-indent (gitconfig-point-in-indentation-p)))
(beginning-of-line)
(delete-horizontal-space)
(unless (equal (char-after) ?\[)
(insert (gitconfig-indentation-string)))
(if was-in-indent
(back-to-indentation)
(goto-char (marker-position old-point)))
(set-marker old-point nil))))
(defun gitconfig-indentation-string ()
(if indent-tabs-mode "\t" (make-string tab-width ?\ )))
(defvar gitconfig-mode-syntax-table
(let ((table (make-syntax-table conf-unix-mode-syntax-table)))
;; ; is a comment in .gitconfig
(modify-syntax-entry ?\; "<" table)
;; ' is not used for string quoting
(modify-syntax-entry ?\' "." table)
table)
"Syntax table to use in .gitconfig buffers.")
(defvar gitconfig-mode-font-lock-keywords
`(
;; Highlight section and subsection gitconfig headers, and override
;; syntactic fontification in these.
(,(rx line-start (zero-or-more (syntax whitespace))
"[" symbol-start
(group (one-or-more (or (syntax word) (syntax symbol))))
symbol-end
(optional (one-or-more (syntax whitespace))
(group (syntax string-quote)
(minimal-match (one-or-more not-newline))
(syntax string-quote)))
"]" (zero-or-more not-newline) line-end)
(1 'font-lock-type-face t nil)
(2 'font-lock-function-name-face t t))
(,(rx line-start (zero-or-more (syntax whitespace)) symbol-start
(group alphanumeric
(zero-or-more (or (syntax word) (syntax symbol))))
symbol-end (zero-or-more (syntax whitespace))
(optional "=" (zero-or-more not-newline)) line-end)
(1 'font-lock-variable-name-face))
;; Highlight booleans and numbers
(,(rx "="
(zero-or-more (syntax whitespace)) word-start
(group (or "yes" "no" "true" "false" "on" "off"))
word-end (zero-or-more (syntax whitespace)) line-end)
(1 'font-lock-keyword-face))
(,(rx "="
(zero-or-more (syntax whitespace)) word-start
(group (one-or-more digit))
word-end (zero-or-more (syntax whitespace)) line-end)
(1 'font-lock-constant-face))))
;;;###autoload
(define-derived-mode gitconfig-mode conf-unix-mode "Gitconfig"
"A major mode for editing .gitconfig files."
;; .gitconfig is indented with tabs only
(conf-mode-initialize "#" gitconfig-mode-font-lock-keywords)
(setq indent-tabs-mode t)
(setq-local indent-line-function 'gitconfig-indent-line))
;;;###autoload
(dolist (pattern '("/\\.gitconfig\\'" "/\\.git/config\\'"
"/modules/.*/config\\'" "/git/config\\'"
"/\\.gitmodules\\'" "/etc/gitconfig\\'"))
(add-to-list 'auto-mode-alist (cons pattern 'gitconfig-mode)))
;;; _
(provide 'gitconfig-mode)
;; Local Variables:
;; indent-tabs-mode: nil
;; End:
;;; gitconfig-mode.el ends here