# HG changeset patch # User Alan Mackenzie # Date 1211632319 0 # Node ID 52c723879a4b958ad81684748ba9790896b6b301 # Parent 2b36423768cae644475a11fc0e5ab2c9cc312cfd * progmodes/cc-mode.el (c-postprocess-file-styles): Throw an error if c-file-style is set to a non-string (c-neutralize-CPP-line): Surround by `save-excursion'. (c-neutralize-syntax-in-CPP): Optimize for speed. diff -r 2b36423768ca -r 52c723879a4b lisp/progmodes/cc-mode.el --- a/lisp/progmodes/cc-mode.el Sat May 24 12:17:18 2008 +0000 +++ b/lisp/progmodes/cc-mode.el Sat May 24 12:31:59 2008 +0000 @@ -710,8 +710,11 @@ (when c-buffer-is-cc-mode (if (or c-file-style c-file-offsets) (c-make-styles-buffer-local t)) - (and c-file-style - (c-set-style c-file-style)) + (when c-file-style + (or (stringp c-file-style) + (error "c-file-style is not a string")) + (c-set-style c-file-style)) + (and c-file-offsets (mapcar (lambda (langentry) @@ -789,28 +792,30 @@ (setq c-old-EOM (point))) (defun c-neutralize-CPP-line (beg end) - ;; BEG and END bound a preprocessor line. Put a "punctuation" syntax-table - ;; property on syntactically obtrusive characters, ones which would interact - ;; syntactically with stuff outside the CPP line. + ;; BEG and END bound a region, typically a preprocessor line. Put a + ;; "punctuation" syntax-table property on syntactically obtrusive + ;; characters, ones which would interact syntactically with stuff outside + ;; this region. ;; ;; These are unmatched string delimiters, or unmatched ;; parens/brackets/braces. An unclosed comment is regarded as valid, NOT ;; obtrusive. - (let (s) - (while - (progn - (setq s (parse-partial-sexp beg end -1)) - (cond - ((< (nth 0 s) 0) ; found an unmated ),},] - (c-put-char-property (1- (point)) 'syntax-table '(1)) - t) - ((nth 3 s) ; In a string - (c-put-char-property (nth 8 s) 'syntax-table '(1)) - t) - ((> (nth 0 s) 0) ; In a (,{,[ - (c-put-char-property (nth 1 s) 'syntax-table '(1)) - t) - (t nil)))))) + (save-excursion + (let (s) + (while + (progn + (setq s (parse-partial-sexp beg end -1)) + (cond + ((< (nth 0 s) 0) ; found an unmated ),},] + (c-put-char-property (1- (point)) 'syntax-table '(1)) + t) + ((nth 3 s) ; In a string + (c-put-char-property (nth 8 s) 'syntax-table '(1)) + t) + ((> (nth 0 s) 0) ; In a (,{,[ + (c-put-char-property (nth 1 s) 'syntax-table '(1)) + t) + (t nil))))))) (defun c-neutralize-syntax-in-CPP (begg endd old-len) ;; "Neutralize" every preprocessor line wholly or partially in the changed @@ -830,27 +835,43 @@ ;; ;; This function is the C/C++/ObjC value of `c-before-font-lock-function'. ;; - ;; This function might do invisible changes. + ;; Note: SPEED _MATTERS_ IN THIS FUNCTION!!! + ;; + ;; This function might make hidden buffer changes. (c-save-buffer-state (limits mbeg+1 beg end) - ;; First calculate the region, possibly to be extended. - (setq beg (min begg c-old-BOM)) + ;; First determine the region, (beg end), which may need "neutralizing". + ;; This may not start inside a string, comment, or macro. + (goto-char c-old-BOM) ; already set to old start of macro or begg. + (setq beg + (if (setq limits (c-literal-limits)) + (cdr limits) ; go forward out of any string or comment. + (point))) + (goto-char endd) - (when (c-beginning-of-macro) - (c-end-of-macro)) + (if (setq limits (c-literal-limits)) + (goto-char (car limits))) ; go backward out of any string or comment. + (if (c-beginning-of-macro) + (c-end-of-macro)) (setq end (max (+ (- c-old-EOM old-len) (- endd begg)) (point))) + ;; Clear all old punctuation properties (c-clear-char-property-with-value beg end 'syntax-table '(1)) (goto-char beg) - (while (and (< (point) end) - (search-forward-regexp c-anchored-cpp-prefix end t)) - ;; If we've found a "#" inside a string/comment, ignore it. - (if (setq limits (c-literal-limits)) - (goto-char (cdr limits)) - (setq mbeg+1 (point)) - (c-end-of-macro) ; Do we need to go forward 1 char here? No! - (c-neutralize-CPP-line mbeg+1 (point)))))) + (let ((pps-position beg) pps-state) + (while (and (< (point) end) + (search-forward-regexp c-anchored-cpp-prefix end t)) + ;; If we've found a "#" inside a string/comment, ignore it. + (setq pps-state + (parse-partial-sexp pps-position (point) nil nil pps-state) + pps-position (point)) + (unless (or (nth 3 pps-state) ; in a string? + (nth 4 pps-state)) ; in a comment? + (setq mbeg+1 (point)) + (c-end-of-macro) ; Do we need to go forward 1 char here? No! + (c-neutralize-CPP-line mbeg+1 (point)) + (setq pps-position (point))))))) ; no need to update pps-state. (defun c-before-change (beg end) ;; Function to be put on `before-change-function'. Primarily, this calls