]> git.eshelyaron.com Git - emacs.git/commitdiff
Support mode line constructs for 'mode-name' in c-mode (bug#2034)
authorPhil Sainty <psainty@orcon.net.nz>
Mon, 2 Jul 2018 11:58:34 +0000 (23:58 +1200)
committerPhil Sainty <psainty@orcon.net.nz>
Sat, 7 Jul 2018 13:20:10 +0000 (01:20 +1200)
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
etc/NEWS
lisp/progmodes/cc-cmds.el
lisp/progmodes/cc-engine.el
lisp/progmodes/cc-mode.el
lisp/progmodes/cc-styles.el
lisp/progmodes/cc-vars.el

index 5a229c1cd6f0d431ac8ee81ece426e6b2fe3dc35..8ffe11caaba841a18ebec7939251044f5277b09c 100644 (file)
@@ -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:
 
index c92ee6e68044e5b6eb644ae882a36ff8cc1bebf8..76f776ded57412cdf8c367b5e117f72e6d953c42 100644 (file)
--- 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'.
 \f
 ** Message
 
index 31cf0b115969e462d0ae6deda878f569c9bc0d73..69217fd185975413e3516c8cb275a93016eeeb8a 100644 (file)
@@ -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))
 
 \f
index 3961ea647cf5d7ea1e493fd133ff2a8082767d51..338bf33c35f8945d679dbd4a9adf802216f88a56 100644 (file)
 (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
index f09ca04cffffed292a1724f1f8b9980753df33ba..73b3b461869a14a502dff78d66e60956b1a0da06 100644 (file)
@@ -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)))
 
 \f
 ;; 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
index a1f3261a24ab4242043720cbb96439ab959d0e0d..1f5258b46d3216154de35c01610d9a1adc40aef2 100644 (file)
@@ -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))))
index 047511406d9005aa44f69280b79aecf4d5f4a36c..8dee5e2d20f3870cb55364ef28e328932f993d42 100644 (file)
@@ -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)
 \f
 ;; Non-customizable variables, still part of the interface to CC Mode
 (defvar c-macro-with-semi-re nil