-
Notifications
You must be signed in to change notification settings - Fork 9
/
electric-spacing-cc-mode.el
188 lines (163 loc) · 6.29 KB
/
electric-spacing-cc-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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
;;; electric-spacing-cc-mode.el --- c-buffer-is tunings
;; Copyright (C) 2019 Free Software Foundation, Inc.
;; Author: William Xu <[email protected]>
;; 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, 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 EMMS; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Code:
(require 'electric-spacing)
(defun electric-spacing-cc-mode-include-line ()
"Return t if we are on the line of including header files."
(save-excursion
(re-search-backward "#include < ?" (line-beginning-position) t 1)))
(defun electric-spacing-cc-mode-hook ()
"Unset c-electric-* keymaps."
(dolist (op electric-spacing-operators)
(local-unset-key (char-to-string op))))
(add-hook 'c-mode-common-hook 'electric-spacing-cc-mode-hook)
(defun electric-spacing-cc-mode-: ()
(cond ((or (looking-back " +: *") (looking-back": *" )) ; namespace
(replace-match "::"))
((save-excursion
(re-search-backward "struct\\|class" (line-beginning-position) t 1))
(electric-spacing-insert ":"))
((save-excursion ; a ? b : c
(re-search-backward "\\?" (line-beginning-position) t 1))
(electric-spacing-insert ":"))
((save-excursion ; for (const auto& el : v)
(re-search-backward "for" (line-beginning-position) t 1))
(electric-spacing-insert ":"))
(t
;; Do not delete spaces after, as it will affect code completion.
(insert ":")))
(indent-according-to-mode))
(defun electric-spacing-cc-mode-* ()
"See `electric-spacing-insert'."
;; ,----
;; | a * b;
;; | char *a;
;; | char **b;
;; | (*a)->func();
;; | *p++;
;; | *a = *b;
;; | printf("%d", *ip);
;; | func(*p);
;; `----
(cond ((or (looking-back (concat (electric-spacing-c-types) " *") nil)
(looking-back (concat "auto *") nil))
(electric-spacing-insert "*" 'before))
((looking-back "\\* *" nil)
(electric-spacing-insert "*" 'middle))
((looking-back "^[ (]*" nil)
(electric-spacing-insert "*" 'middle)
(indent-according-to-mode))
((looking-back "( *" nil)
(electric-spacing-insert "*" 'middle))
((looking-back ", *" nil)
(electric-spacing-insert "*" 'before))
((looking-back "= *" nil)
(electric-spacing-insert "*" 'before))
(t
(electric-spacing-insert "*"))))
(defun electric-spacing-cc-mode-& ()
"See `electric-spacing-insert'."
;; ,----[ cases ]
;; | char &a = b; // FIXME
;; | int c = a & b;
;; | a && b;
;; `----
(cond ((or (looking-back (concat (electric-spacing-c-types) " *") nil) ; void foo(const int& a);
(looking-back (concat "string *") nil)
(looking-back (concat "auto *") nil))
(electric-spacing-insert "&" 'after))
((looking-back "= *" nil) ; char* a = &b;
(electric-spacing-insert "&" 'before))
((looking-back "( *" nil) ; func(&i)
(electric-spacing-insert "&" 'middle))
((looking-back ", *" nil) ; scanf ("%d", &i);
(electric-spacing-insert "&" 'before))
(t
(electric-spacing-insert "&"))))
(defun electric-spacing-cc-mode-< ()
"See `electric-spacing-insert'."
(cond ((looking-back "operator<?" nil)
(insert "<"))
(t
(electric-spacing-insert "<"))))
(defun electric-spacing-cc-mode-> ()
"See `electric-spacing-insert'."
(cond ((looking-back " - " nil)
(delete-char -3)
(insert "->"))
((electric-spacing-cc-mode-include-line)
(save-excursion (replace-match "#include <"))
(insert ">"))
((save-excursion (re-search-backward " < " (line-beginning-position) t 1))
(save-excursion (replace-match "<"))
(electric-spacing-insert ">" 'after))
(t
(electric-spacing-insert ">"))))
(defun electric-spacing-cc-mode-+ ()
"See `electric-spacing-insert'."
(cond ((looking-back "\\+ *" nil)
(when (looking-back "[a-zA-Z0-9_] +\\+ *" nil)
(save-excursion
(backward-char 2)
(delete-horizontal-space)))
(electric-spacing-insert "+" 'middle)
(indent-according-to-mode))
(t
(electric-spacing-insert "+"))))
(defun electric-spacing-cc-mode-- ()
"See `electric-spacing-insert'."
(cond ((looking-back "\\- *" nil)
(electric-spacing-insert "-" 'middle))
((looking-back (concat electric-spacing-operators-regexp " *") nil)
(insert "-"))
(t
(electric-spacing-insert "-"))))
(defun electric-spacing-cc-mode-? ()
"See `electric-spacing-insert'."
(electric-spacing-insert "?"))
(defun electric-spacing-cc-mode-% ()
"See `electric-spacing-insert'."
;; ,----
;; | a % b;
;; | printf("%d %d\n", a % b);
;; `----
(if (and (looking-back "\".*" nil)
(not (looking-back "\",.*" nil)))
(insert "%")
(electric-spacing-insert "%")))
(defun electric-spacing-cc-mode-. ()
(electric-spacing-insert "." 'middle))
(defun electric-spacing-cc-mode-/ ()
(if (electric-spacing-cc-mode-include-line)
(insert "/")
(electric-spacing-insert "/")))
(defun electric-spacing-cc-mode-! ()
"See `electric-spacing-insert'."
(if (looking-back "(" nil)
(electric-spacing-insert "!" 'middle)
(electric-spacing-insert "!" 'before)))
(defun electric-spacing-cc-mode-\; ()
"See `electric-spacing-insert'."
(electric-spacing-insert ";" 'middle)
(indent-according-to-mode))
(defun electric-spacing-cc-mode-= ()
"See `electric-spacing-insert'."
(if (looking-back "operator[ =]*")
(electric-spacing-insert "=" 'middle)
(electric-spacing-insert "="))
(indent-according-to-mode))
(provide 'electric-spacing-cc-mode)
;;; electric-spacing-cc-mode.el ends here