From 0b160d16b7c2bf833c5f5596c5cf8471c3f7e4ba Mon Sep 17 00:00:00 2001 From: Stefan Monnier Date: Mon, 5 Apr 2021 13:28:07 -0400 Subject: [PATCH 1/4] * lisp/progmodes/cc-*.el: Prepare for lexical-binding Minor changes that were found while trying to use lexical-binding. * lisp/progmodes/cc-cmds.el (c-where-wrt-brace-construct): Remove unused vars `decl-result` and `brace-decl-p`. (c-electric-lt-gt, c-forward-over-illiterals): Make comment style obey outline-minor-mode's conventions. (c-defun-name-and-limits): Silence a warning about `where` being unused. * lisp/progmodes/cc-engine.el (c-cache-to-parse-ps-state): Remove always-nil vars `last` and `intermediate`. * lisp/progmodes/cc-fonts.el (c-make-syntactic-matcher): Clarify that regexp has to be a string rather than an expression returning a string. (c-font-lock-c++-using): Remove unused var `after-name`. (c-font-lock-c++-using): Tweak layout to avoid an arg being easy to misread. * lisp/progmodes/cc-styles.el (c-set-offset): Mark arg as knowingly unused. * lisp/progmodes/cc-mode.el (c-mode-base-map): Skip fill-paragraph-or-region in Emacs. Protect against disappearance of obsolete `c-subword-mode`. --- lisp/progmodes/cc-cmds.el | 20 ++++++++++++-------- lisp/progmodes/cc-defs.el | 1 + lisp/progmodes/cc-engine.el | 12 ++++++------ lisp/progmodes/cc-fonts.el | 13 +++++++++---- lisp/progmodes/cc-mode.el | 10 +++++++--- lisp/progmodes/cc-styles.el | 2 +- 6 files changed, 36 insertions(+), 22 deletions(-) diff --git a/lisp/progmodes/cc-cmds.el b/lisp/progmodes/cc-cmds.el index 1754436d13..f7f2b10b7c 100644 --- a/lisp/progmodes/cc-cmds.el +++ b/lisp/progmodes/cc-cmds.el @@ -1220,9 +1220,9 @@ c-electric-lt-gt (self-insert-command (prefix-numeric-value arg))) (setq final-pos (point)) -;;;; 2010-01-31: There used to be code here to put a syntax-table text -;;;; property on the new < or > and its mate (if any) when they are template -;;;; parens. This is now done in an after-change function. +;;;; 2010-01-31: There used to be code here to put a syntax-table text +;;;; property on the new < or > and its mate (if any) when they are template +;;;; parens. This is now done in an after-change function. (when (and (not arg) (not literal)) ;; Have we got a delimiter on a #include directive? @@ -1640,7 +1640,7 @@ c-where-wrt-brace-construct ;; This function might do hidden buffer changes. (save-excursion (let* (knr-start knr-res - decl-result brace-decl-p + ;; decl-result brace-decl-p (start (point)) (paren-state (c-parse-state)) (least-enclosing (c-least-enclosing-brace paren-state))) @@ -2308,6 +2308,7 @@ c-defun-name-and-limits (orig-point-min (point-min)) (orig-point-max (point-max)) lim name limits where) + (ignore where) ;Assigned within `c-while-widening-to-decl-block'. (setq lim (c-widen-to-enclosing-decl-scope paren-state orig-point-min orig-point-max)) (and lim (setq lim (1- lim))) @@ -2927,10 +2928,13 @@ c-forward-over-illiterals (c-looking-at-special-brace-list))) (or allow-early-stop (/= here last)) (save-excursion ; Is this a check that we're NOT at top level? -;;;; NO! This seems to check that (i) EITHER we're at the top level; OR (ii) The next enclosing -;;;; level of bracketing is a '{'. HMM. Doesn't seem to make sense. -;;;; 2003/8/8 This might have something to do with the GCC extension "Statement Expressions", e.g. -;;;; while ({stmt1 ; stmt2 ; exp ;}). This form excludes such Statement Expressions. +;;;; NO! This seems to check that (i) EITHER we're at the top level; +;;;; OR (ii) The next enclosing level of bracketing is a '{'. HMM. +;;;; Doesn't seem to make sense. +;;;; 2003/8/8 This might have something to do with the GCC extension +;;;; "Statement Expressions", e.g. +;;;; while ({stmt1 ; stmt2 ; exp ;}). +;;;; This form excludes such Statement Expressions. (or (not (c-safe (up-list -1) t)) (= (char-after) ?{)))) (goto-char last) diff --git a/lisp/progmodes/cc-defs.el b/lisp/progmodes/cc-defs.el index 536e676626..bfad72b7f2 100644 --- a/lisp/progmodes/cc-defs.el +++ b/lisp/progmodes/cc-defs.el @@ -993,6 +993,7 @@ c-with-syntax-table ;; way to execute code. ;; Maintainers' note: If TABLE is `c++-template-syntax-table', DON'T call ;; any forms inside this that call `c-parse-state'. !!!! + ;; FIXME: Use `with-syntax-table' (introduced in Emacs-21.1)? `(let ((c-with-syntax-table-orig-table (syntax-table))) (unwind-protect (progn diff --git a/lisp/progmodes/cc-engine.el b/lisp/progmodes/cc-engine.el index cc9833a434..8634f7c63f 100644 --- a/lisp/progmodes/cc-engine.el +++ b/lisp/progmodes/cc-engine.el @@ -2717,9 +2717,9 @@ c-cache-to-parse-ps-state ;; two char construct (such as a comment opener or an escaped character).) (if (and (consp elt) (>= (length elt) 3)) ;; Inside a string or comment - (let ((depth 0) (containing nil) (last nil) + (let ((depth 0) (containing nil) ;; (last nil) in-string in-comment - (min-depth 0) com-style com-str-start (intermediate nil) + (min-depth 0) com-style com-str-start ;; (intermediate nil) (char-1 (nth 3 elt)) ; first char of poss. 2-char construct (pos (car elt)) (type (cadr elt))) @@ -2736,14 +2736,14 @@ c-cache-to-parse-ps-state (1- pos) pos)) (if (memq 'pps-extended-state c-emacs-features) - (list depth containing last + (list depth containing nil ;; last in-string in-comment nil min-depth com-style com-str-start - intermediate nil) - (list depth containing last + nil nil) ;; intermediate + (list depth containing nil ;; last in-string in-comment nil min-depth com-style com-str-start - intermediate))) + ))) ;; intermediate ;; Not in a string or comment. (if (memq 'pps-extended-state c-emacs-features) diff --git a/lisp/progmodes/cc-fonts.el b/lisp/progmodes/cc-fonts.el index 4e283764ce..57e5dccfd7 100644 --- a/lisp/progmodes/cc-fonts.el +++ b/lisp/progmodes/cc-fonts.el @@ -279,9 +279,11 @@ c-annotation-face ;; generated functions will. (They are however used in places ;; covered by the font-lock context.) (byte-compile + ;; Starting with Emacs-24, we could skip this `byte-compile' and just + ;; use a closure. `(lambda (limit) (let (res) - (while (and (setq res (re-search-forward ,regexp limit t)) + (while (and (setq res (re-search-forward ',regexp limit t)) (progn (goto-char (match-beginning 0)) (or (c-skip-comments-and-strings limit) @@ -861,6 +863,8 @@ c-basic-matchers-before ;; search from the end of the first highlighted ;; submatch (something that causes problems in other ;; places). + ;; Starting with Emacs-24, we could skip this + ;; `byte-compile' and just use a closure. `(lambda (limit) (while (re-search-forward ,(concat "\\(\\<" ; 1 @@ -2287,7 +2291,7 @@ c-font-lock-c++-using ;; font-lock-keyword-face. It always returns NIL to inhibit this and ;; prevent a repeat invocation. See elisp/lispref page "Search-based ;; fontification". - (let (pos after-name) + (let (pos) ;; after-name (while (c-syntactic-re-search-forward c-using-key limit 'end) (while ; Do one declarator of a comma separated list, each time around. (progn @@ -2295,7 +2299,7 @@ c-font-lock-c++-using (setq pos (point)) ; token after "using". (when (and (c-on-identifier) (c-forward-name)) - (setq after-name (point)) + ;; (setq after-name (point)) (cond ((eq (char-after) ?=) ; using foo = ; (goto-char pos) @@ -2305,7 +2309,8 @@ c-font-lock-c++-using (c-go-up-list-backward) (eq (char-after) ?{) (eq (car (c-beginning-of-decl-1 - (c-determine-limit 1000))) 'same) + (c-determine-limit 1000))) + 'same) (looking-at c-colon-type-list-re))) ;; Inherited protected member: leave unfontified ) diff --git a/lisp/progmodes/cc-mode.el b/lisp/progmodes/cc-mode.el index cfb23d0d45..f1f8f19cee 100644 --- a/lisp/progmodes/cc-mode.el +++ b/lisp/progmodes/cc-mode.el @@ -370,8 +370,9 @@ c-bind-special-erase-keys c-mode-base-map global-map) ;; In XEmacs the default fill function is called ;; fill-paragraph-or-region. - (substitute-key-definition 'fill-paragraph-or-region 'c-fill-paragraph - c-mode-base-map global-map) + (when (featurep 'xemacs) + (substitute-key-definition #'fill-paragraph-or-region #'c-fill-paragraph + c-mode-base-map global-map)) ;; We bind the forward deletion key and (implicitly) C-d to ;; `c-electric-delete-forward', and the backward deletion key to @@ -443,7 +444,8 @@ c-bind-special-erase-keys ;; conflicts with OOBR ;;(define-key c-mode-base-map "\C-c\C-v" 'c-version) ;; (define-key c-mode-base-map "\C-c\C-y" 'c-toggle-hungry-state) Commented out by ACM, 2005-11-22. - (define-key c-mode-base-map "\C-c\C-w" 'c-subword-mode) + (define-key c-mode-base-map "\C-c\C-w" + (if (fboundp 'c-subword-mode) #'c-subword-mode #'subword-mode)) (define-key c-mode-base-map "\C-c\C-k" 'c-toggle-comment-style) (define-key c-mode-base-map "\C-c\C-z" 'c-display-defun-name)) @@ -2055,6 +2057,7 @@ c-before-change (buffer-substring-no-properties beg end))))))) (if c-get-state-before-change-functions + ;; Use `run-hook-with-args'? (mapc (lambda (fn) (funcall fn beg end)) c-get-state-before-change-functions)) @@ -2157,6 +2160,7 @@ c-after-change (setq c-in-after-change-fontification t) (save-excursion + ;; Use `run-hook-with-args'? (mapc (lambda (fn) (funcall fn beg end old-len)) c-before-font-lock-functions))) diff --git a/lisp/progmodes/cc-styles.el b/lisp/progmodes/cc-styles.el index 29cbe54c3b..f2ebaa48f3 100644 --- a/lisp/progmodes/cc-styles.el +++ b/lisp/progmodes/cc-styles.el @@ -464,7 +464,7 @@ c-read-offset offset)) ;;;###autoload -(defun c-set-offset (symbol offset &optional ignored) +(defun c-set-offset (symbol offset &optional _ignored) "Change the value of a syntactic element symbol in `c-offsets-alist'. SYMBOL is the syntactic element symbol to change and OFFSET is the new offset for that syntactic element. The optional argument is not used -- 2.30.2