aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlan Mackenzie2016-05-29 12:22:17 +0000
committerAlan Mackenzie2016-05-29 12:22:17 +0000
commit80dcc2370927a18fd033c928416e7d57e7addd89 (patch)
tree02e0dc47faa90a328dabf3ac762fdb83a78f8fe8
parentb90e8904092eb7aa5b4cb3ede822d10422b70976 (diff)
downloademacs-80dcc2370927a18fd033c928416e7d57e7addd89.tar.gz
emacs-80dcc2370927a18fd033c928416e7d57e7addd89.zip
Rationalize the use of c-new-BEG and c-new-END in CC Mode.
Remove the now redundant c-old-BOM and c-old-EOM. * lisp/progmodes/cc-engine.el (c-macro-cache-syntactic): Change and simplify meaning. (c-macro-cache-no-comment): New variable. (c-invalidate-macro-cache, c-beginning-of-macro, c-end-of-macro): incorporate the new c-macro-cache-no-comment. (c-syntactic-end-of-macro): Make better use of c-macro-cache-syntactic. (c-no-comment-end-of-macro): New function. * lisp/progmodes/cc-langs.el (c-before-font-lock-functions): Add c-extend-font-lock-region-for-macros to C/C++/ObjC value. * lisp/progmodes/cc-mode.el (c-old-BOM, c-old-EOM): Remove. (c-extend-region-for-CPP): Put results in c-new-BEG/END rather than c-old-BOM/EOM. (c-extend-font-lock-region-for-macros): Simplify meaning, no longer returning a cons for the new region, since the function is now called as an after-change function. No longer adjust c-new-END for the length of inserted/deleted text. Move the size restrictions on macros to here from c-neutralize-syntax-in-and-mark-CPP. (c-neutralize-syntax-in-and-mark-CPP): No longer adjust c-new-BEG/END here. Use c-no-comment-end-of-macro rather than c-syntactic-end-of-macro to find the upper boundary to "neutralize" syntactically obtrusive characters. (c-change-expand-fl-region): Don't set c-new-END to next BOL when already at one.
-rw-r--r--lisp/progmodes/cc-engine.el48
-rw-r--r--lisp/progmodes/cc-langs.el9
-rw-r--r--lisp/progmodes/cc-mode.el103
3 files changed, 85 insertions, 75 deletions
diff --git a/lisp/progmodes/cc-engine.el b/lisp/progmodes/cc-engine.el
index b9f25eeaee4..4d6a1203c25 100644
--- a/lisp/progmodes/cc-engine.el
+++ b/lisp/progmodes/cc-engine.el
@@ -229,8 +229,12 @@
229;; The starting position from where we determined `c-macro-cache'. 229;; The starting position from where we determined `c-macro-cache'.
230(defvar c-macro-cache-syntactic nil) 230(defvar c-macro-cache-syntactic nil)
231(make-variable-buffer-local 'c-macro-cache-syntactic) 231(make-variable-buffer-local 'c-macro-cache-syntactic)
232;; non-nil iff `c-macro-cache' has both elements set AND the cdr is at a 232;; Either nil, or the syntactic end of the macro currently represented by
233;; syntactic end of macro, not merely an apparent one. 233;; `c-macro-cache'.
234(defvar c-macro-cache-no-comment nil)
235(make-variable-buffer-local 'c-macro-cache-no-comment)
236;; Either nil, or the last character of the macro currently represented by
237;; `c-macro-cache' which isn't in a comment. */
234 238
235(defun c-invalidate-macro-cache (beg end) 239(defun c-invalidate-macro-cache (beg end)
236 ;; Called from a before-change function. If the change region is before or 240 ;; Called from a before-change function. If the change region is before or
@@ -242,12 +246,14 @@
242 ((< beg (car c-macro-cache)) 246 ((< beg (car c-macro-cache))
243 (setq c-macro-cache nil 247 (setq c-macro-cache nil
244 c-macro-cache-start-pos nil 248 c-macro-cache-start-pos nil
245 c-macro-cache-syntactic nil)) 249 c-macro-cache-syntactic nil
250 c-macro-cache-no-comment nil))
246 ((and (cdr c-macro-cache) 251 ((and (cdr c-macro-cache)
247 (< beg (cdr c-macro-cache))) 252 (< beg (cdr c-macro-cache)))
248 (setcdr c-macro-cache nil) 253 (setcdr c-macro-cache nil)
249 (setq c-macro-cache-start-pos beg 254 (setq c-macro-cache-start-pos beg
250 c-macro-cache-syntactic nil)))) 255 c-macro-cache-syntactic nil
256 c-macro-cache-no-comment nil))))
251 257
252(defun c-macro-is-genuine-p () 258(defun c-macro-is-genuine-p ()
253 ;; Check that the ostensible CPP construct at point is a real one. In 259 ;; Check that the ostensible CPP construct at point is a real one. In
@@ -288,7 +294,8 @@ comment at the start of cc-engine.el for more info."
288 t)) 294 t))
289 (setq c-macro-cache nil 295 (setq c-macro-cache nil
290 c-macro-cache-start-pos nil 296 c-macro-cache-start-pos nil
291 c-macro-cache-syntactic nil) 297 c-macro-cache-syntactic nil
298 c-macro-cache-no-comment nil)
292 299
293 (save-restriction 300 (save-restriction
294 (if lim (narrow-to-region lim (point-max))) 301 (if lim (narrow-to-region lim (point-max)))
@@ -323,7 +330,8 @@ comment at the start of cc-engine.el for more info."
323 (>= (point) (car c-macro-cache))) 330 (>= (point) (car c-macro-cache)))
324 (setq c-macro-cache nil 331 (setq c-macro-cache nil
325 c-macro-cache-start-pos nil 332 c-macro-cache-start-pos nil
326 c-macro-cache-syntactic nil)) 333 c-macro-cache-syntactic nil
334 c-macro-cache-no-comment nil))
327 (while (progn 335 (while (progn
328 (end-of-line) 336 (end-of-line)
329 (when (and (eq (char-before) ?\\) 337 (when (and (eq (char-before) ?\\)
@@ -347,14 +355,38 @@ comment at the start of cc-engine.el for more info."
347 (let* ((here (point)) 355 (let* ((here (point))
348 (there (progn (c-end-of-macro) (point))) 356 (there (progn (c-end-of-macro) (point)))
349 s) 357 s)
350 (unless c-macro-cache-syntactic 358 (if c-macro-cache-syntactic
359 (goto-char c-macro-cache-syntactic)
351 (setq s (parse-partial-sexp here there)) 360 (setq s (parse-partial-sexp here there))
352 (while (and (or (nth 3 s) ; in a string 361 (while (and (or (nth 3 s) ; in a string
353 (nth 4 s)) ; in a comment (maybe at end of line comment) 362 (nth 4 s)) ; in a comment (maybe at end of line comment)
354 (> there here)) ; No infinite loops, please. 363 (> there here)) ; No infinite loops, please.
355 (setq there (1- (nth 8 s))) 364 (setq there (1- (nth 8 s)))
356 (setq s (parse-partial-sexp here there))) 365 (setq s (parse-partial-sexp here there)))
357 (setq c-macro-cache-syntactic (car c-macro-cache))) 366 (setq c-macro-cache-syntactic (point)))
367 (point)))
368
369(defun c-no-comment-end-of-macro ()
370 ;; Go to the end of a CPP directive, or a pos just before which isn't in a
371 ;; comment. For this purpose, open strings are ignored.
372 ;;
373 ;; This function must only be called from the beginning of a CPP construct.
374 ;;
375 ;; Note that this function might do hidden buffer changes. See the comment
376 ;; at the start of cc-engine.el for more info.
377 (let* ((here (point))
378 (there (progn (c-end-of-macro) (point)))
379 s)
380 (if c-macro-cache-no-comment
381 (goto-char c-macro-cache-no-comment)
382 (setq s (parse-partial-sexp here there))
383 (while (and (nth 3 s) ; in a string
384 (> there here)) ; No infinite loops, please.
385 (setq here (1+ (nth 8 s)))
386 (setq s (parse-partial-sexp here there)))
387 (when (nth 4 s)
388 (goto-char (1- (nth 8 s))))
389 (setq c-macro-cache-no-comment (point)))
358 (point))) 390 (point)))
359 391
360(defun c-forward-over-cpp-define-id () 392(defun c-forward-over-cpp-define-id ()
diff --git a/lisp/progmodes/cc-langs.el b/lisp/progmodes/cc-langs.el
index 10fed6d77fc..6f4d1f16857 100644
--- a/lisp/progmodes/cc-langs.el
+++ b/lisp/progmodes/cc-langs.el
@@ -476,7 +476,8 @@ so that all identifiers are recognized as words.")
476 c++ '(c-extend-region-for-CPP 476 c++ '(c-extend-region-for-CPP
477 c-before-change-check-<>-operators 477 c-before-change-check-<>-operators
478 c-invalidate-macro-cache) 478 c-invalidate-macro-cache)
479 (c objc) '(c-extend-region-for-CPP c-invalidate-macro-cache) 479 (c objc) '(c-extend-region-for-CPP
480 c-invalidate-macro-cache)
480 ;; java 'c-before-change-check-<>-operators 481 ;; java 'c-before-change-check-<>-operators
481 awk 'c-awk-record-region-clear-NL) 482 awk 'c-awk-record-region-clear-NL)
482(c-lang-defvar c-get-state-before-change-functions 483(c-lang-defvar c-get-state-before-change-functions
@@ -505,9 +506,11 @@ parameters \(point-min) and \(point-max).")
505 ;; For documentation see the following c-lang-defvar of the same name. 506 ;; For documentation see the following c-lang-defvar of the same name.
506 ;; The value here may be a list of functions or a single function. 507 ;; The value here may be a list of functions or a single function.
507 t 'c-change-expand-fl-region 508 t 'c-change-expand-fl-region
508 (c objc) '(c-neutralize-syntax-in-and-mark-CPP 509 (c objc) '(c-extend-font-lock-region-for-macros
510 c-neutralize-syntax-in-and-mark-CPP
509 c-change-expand-fl-region) 511 c-change-expand-fl-region)
510 c++ '(c-neutralize-syntax-in-and-mark-CPP 512 c++ '(c-extend-font-lock-region-for-macros
513 c-neutralize-syntax-in-and-mark-CPP
511 c-restore-<>-properties 514 c-restore-<>-properties
512 c-change-expand-fl-region) 515 c-change-expand-fl-region)
513 java '(c-restore-<>-properties 516 java '(c-restore-<>-properties
diff --git a/lisp/progmodes/cc-mode.el b/lisp/progmodes/cc-mode.el
index de903b80ade..9ab04808af6 100644
--- a/lisp/progmodes/cc-mode.el
+++ b/lisp/progmodes/cc-mode.el
@@ -865,14 +865,6 @@ Note that the style variables are always made local to the buffer."
865 865
866;;; Change hooks, linking with Font Lock and electric-indent-mode. 866;;; Change hooks, linking with Font Lock and electric-indent-mode.
867 867
868;; Buffer local variables recording Beginning/End-of-Macro position before a
869;; change, when a macro straddles, respectively, the BEG or END (or both) of
870;; the change region. Otherwise these have the values BEG/END.
871(defvar c-old-BOM 0)
872(make-variable-buffer-local 'c-old-BOM)
873(defvar c-old-EOM 0)
874(make-variable-buffer-local 'c-old-EOM)
875
876(defun c-called-from-text-property-change-p () 868(defun c-called-from-text-property-change-p ()
877 ;; Is the primitive which invoked `before-change-functions' or 869 ;; Is the primitive which invoked `before-change-functions' or
878 ;; `after-change-functions' one which merely changes text properties? This 870 ;; `after-change-functions' one which merely changes text properties? This
@@ -886,8 +878,8 @@ Note that the style variables are always made local to the buffer."
886 '(put-text-property remove-list-of-text-properties))) 878 '(put-text-property remove-list-of-text-properties)))
887 879
888(defun c-extend-region-for-CPP (beg end) 880(defun c-extend-region-for-CPP (beg end)
889 ;; Set c-old-BOM or c-old-EOM respectively to BEG, END, each extended to the 881 ;; Adjust `c-new-BEG', `c-new-END' respectively to the beginning and end of
890 ;; beginning/end of any preprocessor construct they may be in. 882 ;; any preprocessor construct they may be in.
891 ;; 883 ;;
892 ;; Point is undefined both before and after this function call; the buffer 884 ;; Point is undefined both before and after this function call; the buffer
893 ;; has already been widened, and match-data saved. The return value is 885 ;; has already been widened, and match-data saved. The return value is
@@ -896,45 +888,33 @@ Note that the style variables are always made local to the buffer."
896 ;; This function is in the C/C++/ObjC values of 888 ;; This function is in the C/C++/ObjC values of
897 ;; `c-get-state-before-change-functions' and is called exclusively as a 889 ;; `c-get-state-before-change-functions' and is called exclusively as a
898 ;; before change function. 890 ;; before change function.
899 (goto-char beg) 891 (goto-char c-new-BEG)
900 (c-beginning-of-macro) 892 (c-beginning-of-macro)
901 (setq c-old-BOM (point)) 893 (setq c-new-BEG (point))
902 894
903 (goto-char end) 895 (goto-char c-new-END)
904 (when (c-beginning-of-macro) 896 (when (c-beginning-of-macro)
905 (c-end-of-macro) 897 (c-end-of-macro)
906 (or (eobp) (forward-char))) ; Over the terminating NL which may be marked 898 (or (eobp) (forward-char))) ; Over the terminating NL which may be marked
907 ; with a c-cpp-delimiter category property 899 ; with a c-cpp-delimiter category property
908 (setq c-old-EOM (point))) 900 (setq c-new-END (point)))
909 901
910(defun c-extend-font-lock-region-for-macros (begg endd &optional old-len) 902(defun c-extend-font-lock-region-for-macros (begg endd old-len)
911 ;; Extend the region (BEGG ENDD) to cover all (possibly changed) 903 ;; Extend the region (c-new-BEG c-new-END) to cover all (possibly changed)
912 ;; preprocessor macros; return the cons (new-BEG . new-END). OLD-LEN should 904 ;; preprocessor macros; The return value has no significance.
913 ;; be either the old length parameter when called from an
914 ;; after-change-function, or nil otherwise. This defun uses the variables
915 ;; c-old-BOM, c-new-BOM.
916 ;; 905 ;;
917 ;; Point is undefined on both entry and exit to this function. The buffer 906 ;; Point is undefined on both entry and exit to this function. The buffer
918 ;; will have been widened on entry. 907 ;; will have been widened on entry.
919 (let (limits new-beg new-end) 908 ;;
920 (goto-char c-old-BOM) ; already set to old start of macro or begg. 909 ;; This function is in the C/C++/ObjC value of `c-before-font-lock-functions'.
921 (setq new-beg 910 (goto-char endd)
922 (min begg 911 (if (c-beginning-of-macro)
923 (if (setq limits (c-state-literal-at (point))) 912 (c-end-of-macro))
924 (cdr limits) ; go forward out of any string or comment. 913 (setq c-new-END (max endd c-new-END (point)))
925 (point)))) 914 ;; Determine the region, (c-new-BEG c-new-END), which will get font
926 915 ;; locked. This restricts the region should there be long macros.
927 (goto-char endd) 916 (setq c-new-BEG (max c-new-BEG (c-determine-limit 500 begg))
928 (if (setq limits (c-state-literal-at (point))) 917 c-new-END (min c-new-END (c-determine-+ve-limit 500 endd))))
929 (goto-char (car limits))) ; go backward out of any string or comment.
930 (if (c-beginning-of-macro)
931 (c-end-of-macro))
932 (setq new-end (max endd
933 (if old-len
934 (+ (- c-old-EOM old-len) (- endd begg))
935 c-old-EOM)
936 (point)))
937 (cons new-beg new-end)))
938 918
939(defun c-neutralize-CPP-line (beg end) 919(defun c-neutralize-CPP-line (beg end)
940 ;; BEG and END bound a region, typically a preprocessor line. Put a 920 ;; BEG and END bound a region, typically a preprocessor line. Put a
@@ -963,19 +943,14 @@ Note that the style variables are always made local to the buffer."
963 (t nil))))))) 943 (t nil)))))))
964 944
965(defun c-neutralize-syntax-in-and-mark-CPP (begg endd old-len) 945(defun c-neutralize-syntax-in-and-mark-CPP (begg endd old-len)
966 ;; (i) Extend the font lock region to cover all changed preprocessor 946 ;; (i) "Neutralize" every preprocessor line wholly or partially in the
967 ;; regions; it does this by setting the variables `c-new-BEG' and 947 ;; changed region. "Restore" lines which were CPP lines before the change
968 ;; `c-new-END' to the new boundaries. 948 ;; and are no longer so.
969 ;;
970 ;; (ii) "Neutralize" every preprocessor line wholly or partially in the
971 ;; extended changed region. "Restore" lines which were CPP lines before the
972 ;; change and are no longer so; these can be located from the Buffer local
973 ;; variables `c-old-BOM' and `c-old-EOM'.
974 ;; 949 ;;
975 ;; (iii) Mark every CPP construct by placing a `category' property value 950 ;; (ii) Mark each CPP construct by placing a `category' property value
976 ;; `c-cpp-delimiter' at its start and end. The marked characters are the 951 ;; `c-cpp-delimiter' at its start and end. The marked characters are the
977 ;; opening # and usually the terminating EOL, but sometimes the character 952 ;; opening # and usually the terminating EOL, but sometimes the character
978 ;; before a comment/string delimiter. 953 ;; before a comment delimiter.
979 ;; 954 ;;
980 ;; That is, set syntax-table properties on characters that would otherwise 955 ;; That is, set syntax-table properties on characters that would otherwise
981 ;; interact syntactically with those outside the CPP line(s). 956 ;; interact syntactically with those outside the CPP line(s).
@@ -992,15 +967,8 @@ Note that the style variables are always made local to the buffer."
992 ;; Note: SPEED _MATTERS_ IN THIS FUNCTION!!! 967 ;; Note: SPEED _MATTERS_ IN THIS FUNCTION!!!
993 ;; 968 ;;
994 ;; This function might make hidden buffer changes. 969 ;; This function might make hidden buffer changes.
995 (c-save-buffer-state (new-bounds) 970 (c-save-buffer-state (limits )
996 ;; First determine the region, (c-new-BEG c-new-END), which will get font 971 ;; Clear 'syntax-table properties "punctuation":
997 ;; locked. It might need "neutralizing". This region may not start
998 ;; inside a string, comment, or macro.
999 (setq new-bounds (c-extend-font-lock-region-for-macros
1000 c-new-BEG c-new-END old-len))
1001 (setq c-new-BEG (max (car new-bounds) (c-determine-limit 500 begg))
1002 c-new-END (min (cdr new-bounds) (c-determine-+ve-limit 500 endd)))
1003 ;; Clear all old relevant properties.
1004 (c-clear-char-property-with-value c-new-BEG c-new-END 'syntax-table '(1)) 972 (c-clear-char-property-with-value c-new-BEG c-new-END 'syntax-table '(1))
1005 973
1006 ;; CPP "comment" markers: 974 ;; CPP "comment" markers:
@@ -1011,6 +979,8 @@ Note that the style variables are always made local to the buffer."
1011 979
1012 ;; Add needed properties to each CPP construct in the region. 980 ;; Add needed properties to each CPP construct in the region.
1013 (goto-char c-new-BEG) 981 (goto-char c-new-BEG)
982 (if (setq limits (c-literal-limits)) ; Go past any literal.
983 (goto-char (cdr limits)))
1014 (skip-chars-backward " \t") 984 (skip-chars-backward " \t")
1015 (let ((pps-position (point)) pps-state mbeg) 985 (let ((pps-position (point)) pps-state mbeg)
1016 (while (and (< (point) c-new-END) 986 (while (and (< (point) c-new-END)
@@ -1030,7 +1000,7 @@ Note that the style variables are always made local to the buffer."
1030 (nth 4 pps-state)))) ; in a comment? 1000 (nth 4 pps-state)))) ; in a comment?
1031 (goto-char (match-beginning 1)) 1001 (goto-char (match-beginning 1))
1032 (setq mbeg (point)) 1002 (setq mbeg (point))
1033 (if (> (c-syntactic-end-of-macro) mbeg) 1003 (if (> (c-no-comment-end-of-macro) mbeg)
1034 (progn 1004 (progn
1035 (c-neutralize-CPP-line mbeg (point)) ; "punctuation" properties 1005 (c-neutralize-CPP-line mbeg (point)) ; "punctuation" properties
1036 (if (eval-when-compile 1006 (if (eval-when-compile
@@ -1256,10 +1226,15 @@ Note that the style variables are always made local to the buffer."
1256 ;; 1226 ;;
1257 ;; This is called from an after-change-function, but the parameters BEG END 1227 ;; This is called from an after-change-function, but the parameters BEG END
1258 ;; and OLD-LEN are not used. 1228 ;; and OLD-LEN are not used.
1259 (if font-lock-mode 1229 (if font-lock-mode
1260 (setq c-new-BEG 1230 (setq c-new-BEG
1261 (or (c-fl-decl-start c-new-BEG) (c-point 'bol c-new-BEG)) 1231 (or (c-fl-decl-start c-new-BEG) (c-point 'bol c-new-BEG))
1262 c-new-END (c-point 'bonl c-new-END)))) 1232 c-new-END
1233 (save-excursion
1234 (goto-char c-new-END)
1235 (if (bolp)
1236 (point)
1237 (c-point 'bonl c-new-END))))))
1263 1238
1264(defun c-context-expand-fl-region (beg end) 1239(defun c-context-expand-fl-region (beg end)
1265 ;; Return a cons (NEW-BEG . NEW-END), where NEW-BEG is the beginning of a 1240 ;; Return a cons (NEW-BEG . NEW-END), where NEW-BEG is the beginning of a