]> git.eshelyaron.com Git - emacs.git/commitdiff
Add new function mode-line-window-selected-p
authorLars Ingebrigtsen <larsi@gnus.org>
Sat, 5 Feb 2022 21:46:08 +0000 (22:46 +0100)
committerLars Ingebrigtsen <larsi@gnus.org>
Sat, 5 Feb 2022 21:46:08 +0000 (22:46 +0100)
* doc/lispref/modes.texi (Mode Line Basics): Mention it (bug#53629).
* lisp/bindings.el (mode-line-window-selected-p): New function
from martin rudalics <rudalics@gmx.at>.

doc/lispref/modes.texi
etc/NEWS
lisp/bindings.el

index e2b39836e663e86565a81b0280f504c43baa26ab..2ef7de066f46040e7eca64b0284eedd14427143c 100644 (file)
@@ -1964,8 +1964,26 @@ This function also forces an update of the menu bar and frame title.
 @end defun
 
   The selected window's mode line is usually displayed in a different
-color using the face @code{mode-line}.  Other windows' mode lines appear
-in the face @code{mode-line-inactive} instead.  @xref{Faces}.
+color using the face @code{mode-line-active}.  Other windows' mode
+lines appear in the face @code{mode-line-inactive} instead.
+@xref{Faces}.
+
+@defun mode-line-window-selected-p
+If you want to have more extensive differences between the mode lines
+in selected and non-selected windows, you can use this predicate in an
+@code{:eval} construct.  For instance, if you want to display the
+buffer name in bold in selected windows, but in italics in the other
+windows, you can say something like:
+
+@lisp
+(setq-default
+ mode-line-buffer-identification
+ '(:eval (propertize "%12b"
+                    'face (if (mode-line-window-selected-p)
+                              'bold
+                            'italic))))
+@end lisp
+@end defun
 
 @vindex mode-line-compact
   Some modes put a lot of data in the mode line, pushing elements at
index ff9f22b6b00634a366c50d1b62b6a65e1aa014ec..e90cf19c16b4071c8b5bd1c4caf978a4ece5f368 100644 (file)
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1035,6 +1035,12 @@ functions.
 \f
 * Lisp Changes in Emacs 29.1
 
++++
+** New utility predicate 'mode-line-window-selected-p'.
+This is meant to be used from ':eval' mode line constructs to create
+different mode line looks for selected and unselected windows.
+
++++
 ** New variable 'messages-buffer-name'.
 This variable (defaulting to "*Messages*") allows packages to override
 where messages are logged.
index 86c0ea1a908e94de10bfbf07be1e64865b49e17e..26b17035ef9c52fdab5fcd74632296e81c22ca98 100644 (file)
@@ -654,6 +654,18 @@ By default, this shows the information specified by `global-mode-string'.")
   (with-selected-window (posn-window (event-start event))
     (previous-buffer)))
 
+(defun mode-line-window-selected-p ()
+  "Return non-nil if we're updating the mode line for the selected window.
+This function is meant to be called in `:eval' mode line
+constructs to allow altering the look of the mode line depending
+on whether the mode line belongs to the currently selected window
+or not."
+  (let ((window (selected-window)))
+    (or (eq window (old-selected-window))
+       (and (minibuffer-window-active-p (minibuffer-window))
+            (with-selected-window (minibuffer-window)
+              (eq window (minibuffer-selected-window)))))))
+
 (defmacro bound-and-true-p (var)
   "Return the value of symbol VAR if it is bound, else nil.
 Note that if `lexical-binding' is in effect, this function isn't