2

I've recently been using the delight package to override mode line strings and control whether or not they're displayed, and it's great, but I'd like also to control the order in which minor mode strings appear on the modeline. For example making sure the projectile mode line string always appears as the second string.

The order in which the mode line strings appears to correspond to their locations in minor-mode-alist (?). Is there a way to control the ordering which doesn't require sorting the values of this list?

2 Answers 2

2

Order of minor mode lighters (before) minor-mode-alist (before)

Order of minor mode lighters (after) minor-mode-alist (before)

(add-hook
 'after-load-functions
 (prog1
     (defun re-arrange-minor-mode-alist (&rest _)
       (cl-loop with modes =
                '(
                  ;; order in which you want the minor mode lighters to
                  ;; appear
                  projectile-mode
                  hs-minor-mode
                  outline-minor-mode
                  ;; To get the current order of entries, and what to plug in
                  ;; here do `M-x pp-eval-expression RET (mapcar #'car minor-mode-alist)'
                  )
                for mode in (nreverse modes)
                for mode-line-entry = (assq mode minor-mode-alist)
                when mode-line-entry do
                (assq-delete-all mode minor-mode-alist)
                (add-to-list 'minor-mode-alist mode-line-entry)))
   (re-arrange-minor-mode-alist)))
3
  • Thanks, I think (re-arrange-minor-mode-alist) needs to be at the end of the prog1 body, but otherwise great job! Sep 23, 2022 at 7:43
  • I have updated the snippet now.
    – user31220
    Sep 23, 2022 at 7:49
  • Great solution! One small adjustment: to operate correctly each time a new file is loaded then I think (nreverse modes) should be (reverse modes). The former is destructive. With nreverse the "modes" list is modified the first time (re-arrange-minor-mode-alist) is evaluated, and subsequent evaluations (each time a new file is opened) only iterate over a single mode element as this is all that's left in that list. (The symptom is that when opening new files any newly enabled minor modes won't be automatically reordered as expected.)
    – projectgus
    Jan 11 at 6:01
1

The order in which the mode line strings appears to correspond to their locations in minor-mode-alist (?).

Yes, and in a very direct sense -- minor-mode-alist is literally the mode line component responsible for rendering those values.

Is there a way to control the ordering which doesn't require sorting the values of this list?

On account of the above, no -- not unless you generate some alternative way of rendering that part of the mode line.

You could use after-load-functions to re-order minor-mode-alist every time any library is loaded (which should do the trick).

Alternatively...

For example making sure the projectile mode line string always appears as the second string.

...use delight.el to hide projectile entirely, but firstly copy that value into a separate mode line construct which you put directly in front of minor-mode-alist (i.e. between the major mode and the minor modes).

That's not entirely trivial, as all of those things are encapsulated in the value of mode-line-modes, but see https://emacs.stackexchange.com/a/3927/454 for an example of manipulating that.

I would recommend the re-ordering approach as the simpler option, though.

1
  • Thanks, I went with your first suggestion using after-load-functions using the method described in whitetrillium's answer Sep 23, 2022 at 7:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.