-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsxhkd-mode.el
132 lines (104 loc) · 4.38 KB
/
sxhkd-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
;;; sxhkd-mode.el --- A mode for editing sxhkdrc -*- lexical-binding: t; -*-
;; Copyright (C) 2019
;; Author: xFA25E
;; Keywords: extensions
;; This program 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 program 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 program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; A major mode for editing sxhkdrc
;; Set up like this:
;; (add-to-list 'auto-mode-alist `(,(rx "sxhkdrc" string-end) . sxhkd-mode))
;; This mode also can reload your config after save.
;; Customize `sxhkd-mode-reload-config' for this
;;; Code:
(require 'array)
(defgroup sxhkd-mode nil
"Mode for editing sxhkd configuration files."
:group 'data)
(defcustom sxhkd-mode-reload-config 'ask
"Should `sxhkd-mode' reload config after save?
Can be t, nil or ask"
:type '(radio (const :tag "Always" t)
(const :tag "Never" nil)
(const :tag "Ask" ask))
:group 'sxhkd-mode)
(defcustom sxhkd-mode-indentation-length 2
"Indentation length."
:type 'integer
:group 'sxhkd-mode)
(defvar sxhkd-mode-keywords
'("super" "hyper" "meta" "alt" "control" "ctrl" "shift" "mode_switch" "lock"
"mod1" "mod2" "mod3" "mod4" "mod5" "any" "button1" "button2" "button3"
"button4" "button5" "button6" "button7" "button8" "button9" "button10"
"button11" "button12" "button13" "button14" "button15" "button16" "button17"
"button18" "button19" "button20" "button21" "button22" "button23"
"button24")
"Sxhkd keywords.")
(defvar sxhkd-mode-font-lock-defaults
`(;; comments
(,(rx bol "#" (*? nonl) eol) . font-lock-comment-face)
;; synchronous commands
(,(rx bol (+ space) (group ";")) . (1 font-lock-variable-name-face))
;; end backslash
(,(rx bol (not (in "#")) (*? nonl) (group "\\") eol)
. (1 font-lock-constant-face))
;; brackets
(,(rx (or "{" "}")) . font-lock-type-face)
;; keywords
(,(regexp-opt sxhkd-mode-keywords) . font-lock-keyword-face)
;; disallowed comments
(,(rx bol (not (in space "#")) (*? nonl) (group "#" (*? nonl)) eol)
. (1 font-lock-warning-face)))
"`sxhkd-mode' font lock defaults.")
;; TODO: use emacs built-in way to find sxhkd pid for signal-process
(defun sxhkd-mode-reload-config ()
"Reload sxhkd config."
(when (if (eq sxhkd-mode-reload-config 'ask)
(yes-or-no-p "Reload config?")
sxhkd-mode-reload-config)
(call-process "pkill" nil 0 nil "-USR1" "--exact" "sxhkd")))
(defun sxhkd-mode-completion-at-point ()
"This is the function used for the hook `completion-at-point-functions'."
(interactive)
(let* ((bounds (bounds-of-thing-at-point 'symbol))
(start (car bounds))
(end (cdr bounds)))
(list start end sxhkd-mode-keywords)))
(defun sxhkd-mode-indent-line ()
"Indentation function for `sxhkd-mode'."
(indent-line-to (sxhkd-mode-current-indentation-length)))
(defun sxhkd-mode-current-indentation-length ()
"Determine indentation length of current line."
(save-excursion
(if (zerop (current-line))
0
;; go up, until end of comment or beginning of buffer
(beginning-of-line 0)
(while (and (looking-at-p (rx "#")) (not (bobp)))
(beginning-of-line 0))
;; previous line is comment or empty line
(if (looking-at-p (rx (or "#" (and bol eol))))
0
(end-of-line)
(forward-char (- sxhkd-mode-indentation-length))
(if (looking-at-p (rx "\\")) 0 sxhkd-mode-indentation-length)))))
;;;###autoload
(define-derived-mode sxhkd-mode fundamental-mode "Sxhkd"
"A major mode for editing sxhkdrc."
(setq-local font-lock-defaults '(sxhkd-mode-font-lock-defaults))
(setq-local comment-start "# ")
(setq-local comment-end "")
(setq-local indent-line-function #'sxhkd-mode-indent-line)
(add-hook 'after-save-hook #'sxhkd-mode-reload-config nil t)
(add-hook 'completion-at-point-functions
#'sxhkd-mode-completion-at-point nil t))
(provide 'sxhkd-mode)
;;; sxhkd-mode.el ends here