aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlan Mackenzie2019-01-22 14:37:54 +0000
committerAlan Mackenzie2019-01-22 14:37:54 +0000
commit9eaf5e052a802a7b0560f983f2bb786e13ce2488 (patch)
treee89f066d7a0621e703df5b80eef9491278b2f612
parentf97b734ffb2b70f3d94e46453a236b46e0704901 (diff)
downloademacs-9eaf5e052a802a7b0560f983f2bb786e13ce2488.tar.gz
emacs-9eaf5e052a802a7b0560f983f2bb786e13ce2488.zip
Extend electric-pair-mode actions to < and >, and also to ( and ) in literals
* lisp/progmodes/cc-cmds.el (c-electric-lt-gt): Actuate electric-pair-mode if a < or > is typed in a context where this is meaningful (#include, or template). (c-electric-paren): Allow electric-pair-mode activity in a comment or string. * lisp/progmodes/cc-defs.el (c-make-keywords-re): Fix a bug where lists of source symbols could get overwritten when parameter adorn is set to 'appendable. * list/progmodes/cc-langs.el (c-cpp-include-key): New lang const and var.
-rw-r--r--lisp/progmodes/cc-cmds.el111
-rw-r--r--lisp/progmodes/cc-defs.el3
-rw-r--r--lisp/progmodes/cc-langs.el13
3 files changed, 86 insertions, 41 deletions
diff --git a/lisp/progmodes/cc-cmds.el b/lisp/progmodes/cc-cmds.el
index 78677fefadb..c0a688195ca 100644
--- a/lisp/progmodes/cc-cmds.el
+++ b/lisp/progmodes/cc-cmds.el
@@ -1167,7 +1167,9 @@ finishes a C++ style stream operator in C++ mode. Exceptions are when a
1167numeric argument is supplied, or the point is inside a literal." 1167numeric argument is supplied, or the point is inside a literal."
1168 1168
1169 (interactive "*P") 1169 (interactive "*P")
1170 (let ((c-echo-syntactic-information-p nil) 1170 (let ((literal (c-save-buffer-state () (c-in-literal)))
1171 template-delim include-delim
1172 (c-echo-syntactic-information-p nil)
1171 final-pos found-delim case-fold-search) 1173 final-pos found-delim case-fold-search)
1172 1174
1173 (let (post-self-insert-hook) ; Disable random functionality. 1175 (let (post-self-insert-hook) ; Disable random functionality.
@@ -1178,39 +1180,63 @@ numeric argument is supplied, or the point is inside a literal."
1178;;;; property on the new < or > and its mate (if any) when they are template 1180;;;; property on the new < or > and its mate (if any) when they are template
1179;;;; parens. This is now done in an after-change function. 1181;;;; parens. This is now done in an after-change function.
1180 1182
1181 ;; Indent the line if appropriate. 1183 (when (and (not arg) (not literal))
1182 (when (and c-electric-flag c-syntactic-indentation c-recognize-<>-arglists) 1184 ;; Have we got a delimiter on a #include directive?
1183 (setq found-delim 1185 (beginning-of-line)
1184 (if (eq (c-last-command-char) ?<) 1186 (setq include-delim
1185 ;; If a <, basically see if it's got "template" before it ..... 1187 (and
1186 (or (and (progn 1188 (looking-at c-cpp-include-key)
1187 (backward-char) 1189 (if (eq (c-last-command-char) ?<)
1188 (= (point) 1190 (eq (match-end 0) (1- final-pos))
1189 (progn (c-beginning-of-current-token) (point)))) 1191 (goto-char (1- final-pos))
1190 (progn 1192 (skip-chars-backward "^<>" (c-point 'bol))
1191 (c-backward-token-2) 1193 (eq (char-before) ?<))))
1192 (looking-at c-opt-<>-sexp-key))) 1194 (goto-char final-pos)
1193 ;; ..... or is a C++ << operator. 1195
1196 ;; Indent the line if appropriate.
1197 (when (and c-electric-flag c-syntactic-indentation c-recognize-<>-arglists)
1198 (setq found-delim
1199 (if (eq (c-last-command-char) ?<)
1200 ;; If a <, basically see if it's got "template" before it .....
1201 (or (and (progn
1202 (backward-char)
1203 (= (point)
1204 (progn (c-beginning-of-current-token) (point))))
1205 (progn
1206 (c-backward-token-2)
1207 (looking-at c-opt-<>-sexp-key))
1208 (setq template-delim t))
1209 ;; ..... or is a C++ << operator.
1210 (and (c-major-mode-is 'c++-mode)
1211 (progn
1212 (goto-char (1- final-pos))
1213 (c-beginning-of-current-token)
1214 (looking-at "<<"))
1215 (>= (match-end 0) final-pos)))
1216
1217 ;; It's a >. Either a template/generic terminator ...
1218 (or (and (c-get-char-property (1- final-pos) 'syntax-table)
1219 (setq template-delim t))
1220 ;; or a C++ >> operator.
1194 (and (c-major-mode-is 'c++-mode) 1221 (and (c-major-mode-is 'c++-mode)
1195 (progn 1222 (progn
1196 (goto-char (1- final-pos)) 1223 (goto-char (1- final-pos))
1197 (c-beginning-of-current-token) 1224 (c-beginning-of-current-token)
1198 (looking-at "<<")) 1225 (looking-at ">>"))
1199 (>= (match-end 0) final-pos))) 1226 (>= (match-end 0) final-pos)))))
1227 (goto-char final-pos)
1200 1228
1201 ;; It's a >. Either a template/generic terminator ... 1229 (when found-delim
1202 (or (c-get-char-property (1- final-pos) 'syntax-table) 1230 (indent-according-to-mode)))
1203 ;; or a C++ >> operator. 1231
1204 (and (c-major-mode-is 'c++-mode) 1232 ;; On the off chance that < and > are configured as pairs in
1205 (progn 1233 ;; electric-pair-mode.
1206 (goto-char (1- final-pos)) 1234 (when (and (boundp 'electric-pair-mode) electric-pair-mode
1207 (c-beginning-of-current-token) 1235 (or template-delim include-delim))
1208 (looking-at ">>")) 1236 (let (post-self-insert-hook)
1209 (>= (match-end 0) final-pos)))))) 1237 (electric-pair-post-self-insert-function))))
1210 1238
1211 (goto-char final-pos)
1212 (when found-delim 1239 (when found-delim
1213 (indent-according-to-mode)
1214 (when (and (eq (char-before) ?>) 1240 (when (and (eq (char-before) ?>)
1215 (not executing-kbd-macro) 1241 (not executing-kbd-macro)
1216 blink-paren-function) 1242 blink-paren-function)
@@ -1241,7 +1267,7 @@ newline cleanups are done if appropriate; see the variable `c-cleanup-list'."
1241 (self-insert-command (prefix-numeric-value arg))) 1267 (self-insert-command (prefix-numeric-value arg)))
1242 1268
1243 (if (and (not arg) (not literal)) 1269 (if (and (not arg) (not literal))
1244 (let* ( ;; We want to inhibit blinking the paren since this will 1270 (let* (;; We want to inhibit blinking the paren since this will
1245 ;; be most disruptive. We'll blink it ourselves 1271 ;; be most disruptive. We'll blink it ourselves
1246 ;; afterwards. 1272 ;; afterwards.
1247 (old-blink-paren blink-paren-function) 1273 (old-blink-paren blink-paren-function)
@@ -1317,21 +1343,26 @@ newline cleanups are done if appropriate; see the variable `c-cleanup-list'."
1317 (insert ?\ ))) 1343 (insert ?\ )))
1318 1344
1319 ;; compact-empty-funcall clean-up? 1345 ;; compact-empty-funcall clean-up?
1320 ((c-save-buffer-state () 1346 ((c-save-buffer-state ()
1321 (and (memq 'compact-empty-funcall c-cleanup-list) 1347 (and (memq 'compact-empty-funcall c-cleanup-list)
1322 (eq (c-last-command-char) ?\)) 1348 (eq (c-last-command-char) ?\))
1323 (save-excursion 1349 (save-excursion
1324 (c-safe (backward-char 2)) 1350 (c-safe (backward-char 2))
1325 (when (looking-at "()") 1351 (when (looking-at "()")
1326 (setq end (point)) 1352 (setq end (point))
1327 (skip-chars-backward " \t") 1353 (skip-chars-backward " \t")
1328 (setq beg (point)) 1354 (setq beg (point))
1329 (c-on-identifier))))) 1355 (c-on-identifier)))))
1330 (delete-region beg end)))) 1356 (delete-region beg end))))
1331 (and (eq last-input-event ?\)) 1357 (and (eq last-input-event ?\))
1332 (not executing-kbd-macro) 1358 (not executing-kbd-macro)
1333 old-blink-paren 1359 old-blink-paren
1334 (funcall old-blink-paren)))))) 1360 (funcall old-blink-paren)))
1361
1362 ;; Apply `electric-pair-mode' stuff inside a string or comment.
1363 (when (and (boundp 'electric-pair-mode) electric-pair-mode)
1364 (let (post-self-insert-hook)
1365 (electric-pair-post-self-insert-function))))))
1335 1366
1336(defun c-electric-continued-statement () 1367(defun c-electric-continued-statement ()
1337 "Reindent the current line if appropriate. 1368 "Reindent the current line if appropriate.
diff --git a/lisp/progmodes/cc-defs.el b/lisp/progmodes/cc-defs.el
index 1cb823495a3..f8b1b45de77 100644
--- a/lisp/progmodes/cc-defs.el
+++ b/lisp/progmodes/cc-defs.el
@@ -1769,7 +1769,8 @@ when it's needed. The default is the current language taken from
1769 t)) 1769 t))
1770 (setq pos (cdr pos))) 1770 (setq pos (cdr pos)))
1771 found)) 1771 found))
1772 (setq pos list) 1772 (setq pos (copy-tree list)
1773 )
1773 (while pos 1774 (while pos
1774 (if (string-match "\\w\\'" (car pos)) 1775 (if (string-match "\\w\\'" (car pos))
1775 (setcar pos (concat (car pos) unique))) 1776 (setcar pos (concat (car pos) unique)))
diff --git a/lisp/progmodes/cc-langs.el b/lisp/progmodes/cc-langs.el
index 4bd4914a2d7..53342713b4a 100644
--- a/lisp/progmodes/cc-langs.el
+++ b/lisp/progmodes/cc-langs.el
@@ -927,6 +927,19 @@ file name in angle brackets or quotes."
927 '("include")) 927 '("include"))
928 objc '("include" "import")) 928 objc '("include" "import"))
929 929
930(c-lang-defconst c-cpp-include-key
931 ;; Matches an include directive anchored at BOL including any trailing
932 ;; whitespace, e.g. " # include "
933 t (if (and (c-lang-const c-anchored-cpp-prefix)
934 (c-lang-const c-cpp-include-directives))
935 (concat
936 (c-lang-const c-anchored-cpp-prefix)
937 (c-make-keywords-re 'appendable
938 (c-lang-const c-cpp-include-directives))
939 "[ \t]*")
940 "a\\`")) ; Doesn't match anything
941(c-lang-defvar c-cpp-include-key (c-lang-const c-cpp-include-key))
942
930(c-lang-defconst c-opt-cpp-macro-define 943(c-lang-defconst c-opt-cpp-macro-define
931 "Cpp directive (without the prefix) that is followed by a macro 944 "Cpp directive (without the prefix) that is followed by a macro
932definition, or nil if the language doesn't have any." 945definition, or nil if the language doesn't have any."