From 6a4477d057e8e35698c34193e928089290b16a58 Mon Sep 17 00:00:00 2001 From: Phil Sainty Date: Mon, 2 Jul 2018 23:58:34 +1200 Subject: [PATCH] Support mode line constructs for 'mode-name' in c-mode (bug#2034) Also make the inclusion of minor mode flags in 'mode-name' optional. * lisp/progmodes/cc-vars.el (c-modeline-display-flags): New user option. * lisp/progmodes/cc-cmds.el (c-modeline-flags): New variable. (c--modeline-major-mode): New internal buffer-local variable. (c-update-modeline): Use mode line constructs, rather than string concatenation, to optionally include minor mode flags in 'mode-name'. * lisp/progmodes/cc-cmds.el (c-toggle-auto-newline) (c-toggle-hungry-state, c-toggle-auto-hungry-state) (c-toggle-electric-state, c-toggle-comment-style): * lisp/progmodes/cc-mode.el (c-electric-indent-mode-hook) (c-electric-indent-local-mode-hook): Remove redundant calls to 'c-update-modeline'. It is no longer necessary to call this function every time one of the minor mode states changes. The remaining calls are in 'c-basic-common-init' (which is called via 'c-common-init' by all the major modes defined in cc-mode.el), and in the :after-hook of those modes (which ensures that 'mode-name' is still processed for a derived mode that doesn't call 'c-common-init' itself). * lisp/progmodes/cc-mode.el (c-submit-bug-report): * lisp/progmodes/cc-styles.el (c-set-style): Format 'mode-name'. * doc/misc/cc-mode.texi: Document 'c-modeline-display-flags'. * etc/NEWS: Mention new user option and behaviors. * lisp/progmodes/cc-engine.el: * lisp/progmodes/cc-mode.el: Remove commented remnants of 'c-submode-indicators'. This code was commented out in commit cb694ab73063cc92342daf96d009cdc6d086bc0b in which the indicators were moved directly into 'mode-name' (to prevent lighter text for other minor modes from appearing inbetween). These indicators are now replaced by 'c-modeline-flags'. --- doc/misc/cc-mode.texi | 25 ++++++++------ etc/NEWS | 12 +++++++ lisp/progmodes/cc-cmds.el | 68 +++++++++++++++++++++---------------- lisp/progmodes/cc-engine.el | 4 --- lisp/progmodes/cc-mode.el | 14 +++----- lisp/progmodes/cc-styles.el | 2 +- lisp/progmodes/cc-vars.el | 10 ++++++ 7 files changed, 81 insertions(+), 54 deletions(-) diff --git a/doc/misc/cc-mode.texi b/doc/misc/cc-mode.texi index 5a229c1cd6f..8ffe11caaba 100644 --- a/doc/misc/cc-mode.texi +++ b/doc/misc/cc-mode.texi @@ -1179,16 +1179,21 @@ you initialize a buffer, the comment style is set to the default for the major mode, electric mode and syntactic-indentation mode are enabled, but the other three modes are disabled. -@ccmode{} displays the current state of the first five of these minor -modes on the mode line by appending characters to the major mode's -name: @samp{/} or @samp{*} to indicate the comment style (respectively -line or block), and one letter for each of the other minor modes which -is enabled - @samp{l} for electric mode, @samp{a} for auto-newline -mode, @samp{h} for hungry delete mode, and @samp{w} for subword mode. -If the comment style was block and all the other modes were enabled, -you'd see @samp{C/*lahw}@footnote{The @samp{C} would be replaced with -the name of the language in question for the other languages @ccmode{} -supports.}. +@ccmode{}, by default, displays the current state of the first five of +these minor modes on the mode line by appending characters to the +major mode's name: @samp{/} or @samp{*} to indicate the comment style +(respectively line or block), and one letter for each of the other +minor modes which is enabled - @samp{l} for electric mode, @samp{a} +for auto-newline mode, @samp{h} for hungry delete mode, and @samp{w} +for subword mode. If the comment style was block and all the other +modes were enabled, you'd see @samp{C/*lahw}@footnote{The @samp{C} +would be replaced with the name of the language in question for the +other languages @ccmode{} supports.}. + +@vindex c-modeline-display-flags +@vindex modeline-display-flags @r{(c-)} +If you do not wish these minor mode states to be displayed, set the +user option @code{c-modeline-display-flags} to @code{nil}. Here are the commands to toggle these modes: diff --git a/etc/NEWS b/etc/NEWS index c92ee6e6804..76f776ded57 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -525,6 +525,18 @@ It was obsolete since Emacs 22.1, replaced by customize. Use of built-in libgnutls based functionality (described in the Emacs GnuTLS manual) is recommended instead. +** CC mode + ++++ +*** The user option 'c-modeline-display-flags' allows hiding the +sequence of characters indicating the states of various minor modes, +which by default are appended to 'mode-name' in the mode line (and +elsewhere). + +--- +*** 'mode-name' is no longer required to be a string. The function +'c-update-modeline' now uses mode line constructs to append the minor +mode flags to 'mode-name'. ** Message diff --git a/lisp/progmodes/cc-cmds.el b/lisp/progmodes/cc-cmds.el index 31cf0b11596..69217fd1859 100644 --- a/lisp/progmodes/cc-cmds.el +++ b/lisp/progmodes/cc-cmds.el @@ -258,31 +258,46 @@ With universal argument, inserts the analysis as a comment on that line." (defvar c-block-comment-flag nil) (make-variable-buffer-local 'c-block-comment-flag) +(defvar c-modeline-flags + '(c-modeline-display-flags + ("/" + (c-block-comment-flag "*" "/") + (c-electric-flag ("l" (c-auto-newline "a"))) + (c-hungry-delete-key "h") + ;; FIXME: subword-mode already comes with its own lighter! + (c-subword-mode "w"))) + "A mode line construct to indicate the enabled minor modes. + +See Info node `(ccmode) Minor Modes'. + +The flags are hidden when `c-modeline-display-flags' is nil. + +This construct is added to `mode-name' by `c-update-modeline'.") + +(defvar-local c--modeline-major-mode nil + "The last major mode processed by `c-update-modeline'. + +Internal use only. + +If the value of `major-mode' is not a match for this buffer- +local value, then a new mode has been invoked (e.g. a derived +mode of the previous value seen). That in turn means that +`mode-name' has been reset by the new major mode body, and it +needs to be processed.") + (defun c-update-modeline () - (let ((fmt (format "/%s%s%s%s%s" - (if c-block-comment-flag "*" "/") - (if c-electric-flag "l" "") - (if (and c-electric-flag c-auto-newline) - "a" "") - (if c-hungry-delete-key "h" "") - (if (and - ;; (cc-)subword might not be loaded. - (boundp 'c-subword-mode) - (symbol-value 'c-subword-mode)) - ;; FIXME: subword-mode already comes with its - ;; own lighter! - "w" - ""))) - ;; FIXME: Derived modes might want to use something else - ;; than a string for `mode-name'. - (bare-mode-name (if (string-match "\\(^[^/]*\\)/" mode-name) - (match-string 1 mode-name) - mode-name))) - (setq mode-name - (if (> (length fmt) 1) - (concat bare-mode-name fmt) - bare-mode-name)) - (force-mode-line-update))) + "Add `c-modeline-flags' to `mode-name', if not already done. + +This is called from `c-basic-common-init' and should also be +invoked via the :after-hook of the major mode. The hook ensures +that this function is still called for derived modes which don't +call `c-basic-common-init'." + (unless (eq major-mode c--modeline-major-mode) + (setq mode-name (list mode-name 'c-modeline-flags)) + (unless (stringp (car mode-name)) + (push "" mode-name)) + (setq c--modeline-major-mode major-mode)) + (force-mode-line-update)) (defun c-toggle-syntactic-indentation (&optional arg) "Toggle syntactic indentation. @@ -322,7 +337,6 @@ after special characters such as brace, comma, semi-colon, and colon." (setq c-auto-newline (c-calculate-state arg (and c-auto-newline c-electric-flag))) (if c-auto-newline (setq c-electric-flag t)) - (c-update-modeline) (c-keep-region-active)) (defalias 'c-toggle-auto-state 'c-toggle-auto-newline) @@ -339,7 +353,6 @@ the mode line after the mode name) the delete key gobbles all preceding whitespace in one fell swoop." (interactive "P") (setq c-hungry-delete-key (c-calculate-state arg c-hungry-delete-key)) - (c-update-modeline) (c-keep-region-active)) (defun c-toggle-auto-hungry-state (&optional arg) @@ -352,7 +365,6 @@ See `c-toggle-auto-newline' and `c-toggle-hungry-state' for details." (interactive "P") (setq c-auto-newline (c-calculate-state arg c-auto-newline)) (setq c-hungry-delete-key (c-calculate-state arg c-hungry-delete-key)) - (c-update-modeline) (c-keep-region-active)) (defun c-toggle-electric-state (&optional arg) @@ -362,7 +374,6 @@ positive, turns it off when negative, and just toggles it when zero or left out." (interactive "P") (setq c-electric-flag (c-calculate-state arg c-electric-flag)) - (c-update-modeline) (when (fboundp 'electric-indent-local-mode) ; Emacs 24.4 or later. (electric-indent-local-mode (if c-electric-flag 1 0))) (c-keep-region-active)) @@ -390,7 +401,6 @@ This action does nothing when the mode only has one comment style." (if c-block-comment-flag (concat " " c-block-comment-ender) "")) - (c-update-modeline) (c-keep-region-active)) diff --git a/lisp/progmodes/cc-engine.el b/lisp/progmodes/cc-engine.el index 3961ea647cf..338bf33c35f 100644 --- a/lisp/progmodes/cc-engine.el +++ b/lisp/progmodes/cc-engine.el @@ -185,10 +185,6 @@ (defvar c-auto-newline nil) (make-variable-buffer-local 'c-auto-newline) -;; Included in the mode line to indicate the active submodes. -;; (defvar c-submode-indicators nil) -;; (make-variable-buffer-local 'c-submode-indicators) - (defun c-calculate-state (arg prevstate) ;; Calculate the new state of PREVSTATE, t or nil, based on arg. If ;; arg is nil or zero, toggle the state. If arg is negative, turn diff --git a/lisp/progmodes/cc-mode.el b/lisp/progmodes/cc-mode.el index f09ca04cfff..73b3b461869 100644 --- a/lisp/progmodes/cc-mode.el +++ b/lisp/progmodes/cc-mode.el @@ -637,11 +637,7 @@ that requires a literal mode spec at compile time." (when (fboundp 'electric-indent-local-mode) (setq c-electric-flag electric-indent-mode)) -;; ;; Put submode indicators onto minor-mode-alist, but only once. -;; (or (assq 'c-submode-indicators minor-mode-alist) -;; (setq minor-mode-alist -;; (cons '(c-submode-indicators c-submode-indicators) -;; minor-mode-alist))) + ;; Add minor mode flags to `mode-name'. (c-update-modeline) ;; Install the functions that ensure that various internal caches @@ -2029,16 +2025,14 @@ This function is called from `c-common-init', once per mode initialization." (with-current-buffer buf (when c-buffer-is-cc-mode ;; Don't use `c-toggle-electric-state' here due to recursion. - (setq c-electric-flag electric-indent-mode) - (c-update-modeline)))) + (setq c-electric-flag electric-indent-mode)))) (buffer-list))) (defun c-electric-indent-local-mode-hook () ;; Emacs has en/disabled `electric-indent-local-mode' for this buffer. ;; Propagate this through to this buffer's value of `c-electric-flag' (when c-buffer-is-cc-mode - (setq c-electric-flag electric-indent-mode) - (c-update-modeline))) + (setq c-electric-flag electric-indent-mode))) ;; Support for C @@ -2521,7 +2515,7 @@ Key bindings: t (message "") nil) (reporter-submit-bug-report c-mode-help-address - (concat "CC Mode " c-version " (" mode-name ")") + (concat "CC Mode " c-version " (" (format-mode-line mode-name) ")") (let ((vars (append c-style-variables '(c-buffer-is-cc-mode diff --git a/lisp/progmodes/cc-styles.el b/lisp/progmodes/cc-styles.el index a1f3261a24a..1f5258b46d3 100644 --- a/lisp/progmodes/cc-styles.el +++ b/lisp/progmodes/cc-styles.el @@ -365,7 +365,7 @@ a null operation." (interactive (list (let ((completion-ignore-case t) (prompt (format "Which %s indentation style? " - mode-name))) + (format-mode-line mode-name)))) (completing-read prompt c-style-alist nil t nil 'c-set-style-history c-indentation-style)))) diff --git a/lisp/progmodes/cc-vars.el b/lisp/progmodes/cc-vars.el index 047511406d9..8dee5e2d20f 100644 --- a/lisp/progmodes/cc-vars.el +++ b/lisp/progmodes/cc-vars.el @@ -1695,6 +1695,16 @@ c-noise-macro-with-parens-names is invalid: %s" c-noise-macro-with-parens-names) (copy-sequence c-noise-macro-names)) (t (error "c-make-noise-macro-regexps: \ c-noise-macro-names is invalid: %s" c-noise-macro-names))))) + +(defcustom c-modeline-display-flags t + "If non-nil, `mode-name' includes indicators for certain minor modes. + +See Info node `(ccmode) Minor Modes'. + +These flags are defined by `c-modeline-flags'." + :version "27.1" + :type 'boolean + :group 'c) ;; Non-customizable variables, still part of the interface to CC Mode (defvar c-macro-with-semi-re nil -- 2.39.5