-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvs-electric-spacing.el
105 lines (85 loc) · 3.62 KB
/
vs-electric-spacing.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
;;; vs-electric-spacing.el --- Add spacing around operators like Visual Studio -*- lexical-binding: t; -*-
;; Copyright (C) 2022-2025 Shen, Jen-Chieh
;; Author: Shen, Jen-Chieh <[email protected]>
;; Maintainer: Shen, Jen-Chieh <[email protected]>
;; URL: https://github.com/jcs-elpa/vs-electric-spacing
;; Version: 0.1.0
;; Package-Requires: ((emacs "26.1"))
;; Keywords: convenience electric vs
;; This file is not part of GNU Emacs.
;; 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:
;;
;; Add spacing around operators like Visual Studio.
;;
;;; Code:
(require 'elec-pair)
(defgroup vs-electric-spacing nil
"Add spacing around operators like Visual Studio."
:prefix "vs-electric-spacing-"
:group 'electricity
:link '(url-link :tag "Repository" "https://github.com/jcs-elpa/vs-electric-spacing"))
(defun vs-electric-spacing--char-before (&optional pos)
"Like `char-before' but return a string at POS."
(ignore-errors (string (char-before pos))))
(defun vs-electric-spacing--char-after (&optional pos)
"Like `char-after' but return a string at POS."
(ignore-errors (string (char-after pos))))
(defun vs-electric-spacing--post-self-insert (&rest _)
"Hook function for `post-self-insert-hook'."
(when-let* ((b-1 (vs-electric-spacing--char-before (1- (point))))
(b-0 (vs-electric-spacing--char-before))
(a-0 (vs-electric-spacing--char-after))
(a-1 (vs-electric-spacing--char-after (1+ (point)))))
(pcase b-0
("{"
;; Insert a space before {
(unless (string= b-1 " ")
(save-excursion
(forward-char -1)
(insert " ")))
;; Insert a space between { }
(when (string= a-0 "}") (insert " "))
;; Insert a space after }
(when (string= a-1 "}")
(save-excursion
(forward-char 1)
(insert " "))))
(";"
;; Insert a space after ; (if needed)
(when (and (not (eolp))
(string= b-1 " ")
(not (string= a-0 " ")))
(insert " ")
(forward-char -1))))))
(defun vs-electric-spacing--enable ()
"Enable `vs-electric-spacing-mode'."
;; this require `electric-pair-mode' to be enabled
(unless electric-pair-mode (electric-pair-mode 1))
(add-hook 'post-self-insert-hook #'vs-electric-spacing--post-self-insert nil t))
(defun vs-electric-spacing--disable ()
"Disable `vs-electric-spacing-mode'."
(remove-hook 'post-self-insert-hook #'vs-electric-spacing--post-self-insert t))
;;;###autoload
(define-minor-mode vs-electric-spacing-mode
"Minor mode `vs-electric-spacing-mode'."
:group 'vs-electric-spacing
(if vs-electric-spacing-mode (vs-electric-spacing--enable) (vs-electric-spacing--disable)))
(defun vs-electric-spacing--turn-on ()
"Turn on the `vs-electric-spacing-mode'."
(vs-electric-spacing-mode 1))
;;;###autoload
(define-globalized-minor-mode global-vs-electric-spacing-mode
vs-electric-spacing-mode vs-electric-spacing--turn-on
:require 'vs-electric-spacing)
(provide 'vs-electric-spacing)
;;; vs-electric-spacing.el ends here