How can I display the max line of a file in the bar at the bottom? So, how could it say "L1/L1300" instead of just "Top L1", if the respective happens to have 1300 lines?
1 Answer
count-lines
(also used by line-number-at-pos
) is increasingly less efficient as the buffer grows in size/length, whereas (format-mode-line "%l")
does not have those limitations -- however, there are other limitations. See the related thread entitled "A faster method to obtain line-number-at-pos
in large buffers": A faster method to obtain `line-number-at-pos` in large buffers
Performing anything remotely significant in the mode-line can cause a slow down (in part) because the mode-line for more than one visible window may be updated during redisplay. The following example updates the total line numbers in only the selected-window (i.e., active window). See the related thread entitled "Show something in active mode line instead of all mode lines": Show something in active mode line instead of all mode lines
The mode-line for the inactive window uses the previously recorded value for the total number of lines if said value exists, otherwise calculate said value.
Add additional hooks as needed to call ml-update-all
.
(defvar ml-selected-window nil)
(defvar ml-total-lines nil
"Previously recorded total lines in a buffer -- used for inactive windows.")
(make-variable-buffer-local 'ml-total-lines)
(defun ml-record-selected-window ()
(setq ml-selected-window (selected-window)))
(defun ml-update-all ()
(force-mode-line-update t))
(add-hook 'post-command-hook 'ml-record-selected-window)
(add-hook 'buffer-list-update-hook 'ml-update-all)
(setq-default mode-line-format '(
" %l | "
(:eval
(let ((win (selected-window)))
(with-current-buffer (window-buffer win)
(if (or (eq ml-selected-window win) (null ml-total-lines))
(save-excursion
(goto-char (point-max))
(setq ml-total-lines (format-mode-line "%l")))
ml-total-lines))))))
-
The
(save-excursion ...)
expression clocks in at 0.5ms on my laptop. So this should be sufficient performance wise. Leaves the question of how to elegantly incorporate this into themode-line-format
.– politzaAug 31, 2016 at 23:08 -
@politza -- my preference would be to create a minimal working example that is as efficient as possible, without opening a "Pandora's box" by creating a complex
mode-line-format
or getting into the realm of unlimited possibilities with custom variations from the default -- e.g., thepowerline
library.– lawlistSep 1, 2016 at 5:48 -
I was simply hinting at the fact, that computing the value is relatively easy compared to getting it actually displayed in place of the default line-number. Which is, of course, not your fault.– politzaSep 1, 2016 at 6:54