1
0
mirror of https://git.savannah.gnu.org/git/emacs.git synced 2024-11-21 06:55:39 +00:00

(ruler-mode-ruler): Minor optimization

* lisp/ruler-mode.el (ruler-mode-ruler): Don't compute
`line-number-display-width` redundantly.  Use `add-text-properties`.
Remove redundant `local-map` property.
This commit is contained in:
Stefan Monnier 2024-05-04 12:06:16 -04:00
parent fd859fbea2
commit ea752667cd

View File

@ -350,7 +350,7 @@ nothing is dragged.")
(defun ruler-mode-text-scaled-width (width) (defun ruler-mode-text-scaled-width (width)
"Compute scaled text width according to current font scaling. "Compute scaled text width according to current font scaling.
Convert a width of char units into a text-scaled char width units, Convert a WIDTH of char units into a text-scaled char width units,
for example `window-hscroll'." for example `window-hscroll'."
(/ (* width (frame-char-width)) (default-font-width))) (/ (* width (frame-char-width)) (default-font-width)))
@ -528,7 +528,7 @@ START-EVENT is the mouse click event."
(defvar ruler-mode-header-line-format-old nil (defvar ruler-mode-header-line-format-old nil
"Hold previous value of `header-line-format'.") "Hold previous value of `header-line-format'.")
(defvar ruler-mode-ruler-function 'ruler-mode-ruler (defvar ruler-mode-ruler-function #'ruler-mode-ruler
"Function to call to return ruler header line format. "Function to call to return ruler header line format.
This variable is expected to be made buffer-local by modes.") This variable is expected to be made buffer-local by modes.")
@ -563,7 +563,7 @@ format first."
(ruler--save-header-line-format)) (ruler--save-header-line-format))
(setq ruler-mode enable))) (setq ruler-mode enable)))
(if ruler-mode (if ruler-mode
(add-hook 'post-command-hook 'force-mode-line-update nil t) (add-hook 'post-command-hook #'force-mode-line-update nil t)
;; When `ruler-mode' is off restore previous header line format if ;; When `ruler-mode' is off restore previous header line format if
;; the current one is the ruler header line format. ;; the current one is the ruler header line format.
(when (eq header-line-format ruler-mode-header-line-format) (when (eq header-line-format ruler-mode-header-line-format)
@ -571,7 +571,7 @@ format first."
(when (local-variable-p 'ruler-mode-header-line-format-old) (when (local-variable-p 'ruler-mode-header-line-format-old)
(setq header-line-format ruler-mode-header-line-format-old) (setq header-line-format ruler-mode-header-line-format-old)
(kill-local-variable 'ruler-mode-header-line-format-old))) (kill-local-variable 'ruler-mode-header-line-format-old)))
(remove-hook 'post-command-hook 'force-mode-line-update t))) (remove-hook 'post-command-hook #'force-mode-line-update t)))
;; Add ruler-mode to the minor mode menu in the mode line ;; Add ruler-mode to the minor mode menu in the mode line
(define-key mode-line-mode-menu [ruler-mode] (define-key mode-line-mode-menu [ruler-mode]
@ -625,7 +625,7 @@ mouse-2: unset goal column"
(defsubst ruler-mode-space (width &rest props) (defsubst ruler-mode-space (width &rest props)
"Return a single space string of WIDTH times the normal character width. "Return a single space string of WIDTH times the normal character width.
Optional argument PROPS specifies other text properties to apply." Optional argument PROPS specifies other text properties to apply."
(apply 'propertize " " 'display (list 'space :width width) props)) (apply #'propertize " " 'display (list 'space :width width) props))
(defun ruler-mode-ruler () (defun ruler-mode-ruler ()
"Compute and return a header line ruler." "Compute and return a header line ruler."
@ -665,25 +665,25 @@ Optional argument PROPS specifies other text properties to apply."
'face 'ruler-mode-pad)) 'face 'ruler-mode-pad))
;; Remember the scrollbar vertical type. ;; Remember the scrollbar vertical type.
(sbvt (car (window-current-scroll-bars))) (sbvt (car (window-current-scroll-bars)))
;; Create an "clean" ruler. ;; Create a "clean" ruler.
(ruler (ruler
;; Make the part of header-line corresponding to the ;; Make the part of header-line corresponding to the
;; line-number display be blank, not filled with ;; line-number display be blank, not filled with
;; ruler-mode-basic-graduation-char. ;; ruler-mode-basic-graduation-char.
(if display-line-numbers (if (> i 0)
(let ((lndw (round (line-number-display-width 'columns)))) (vconcat (make-vector i ?\s)
(vconcat (make-vector lndw ?\s) (make-vector (- w i)
(make-vector (- w lndw) ruler-mode-basic-graduation-char))
ruler-mode-basic-graduation-char)))
(make-vector w ruler-mode-basic-graduation-char))) (make-vector w ruler-mode-basic-graduation-char)))
(ruler-wide-props (ruler-wide-props
`((face . ruler-mode-default) `( face ruler-mode-default
(local-map . ruler-mode-map) ;; This is redundant with the minor mode map.
(help-echo . ,(cond (ruler-mode-show-tab-stops ;;local-map ruler-mode-map
ruler-mode-ruler-help-echo-when-tab-stops) help-echo ,(cond (ruler-mode-show-tab-stops
(goal-column ruler-mode-ruler-help-echo-when-tab-stops)
ruler-mode-ruler-help-echo-when-goal-column) (goal-column
(ruler-mode-ruler-help-echo))))) ruler-mode-ruler-help-echo-when-goal-column)
(ruler-mode-ruler-help-echo))))
(props nil) (props nil)
k c) k c)
;; Setup the active area. ;; Setup the active area.
@ -695,7 +695,7 @@ Optional argument PROPS specifies other text properties to apply."
(setq c (number-to-string (/ j 10)) (setq c (number-to-string (/ j 10))
m (length c) m (length c)
k i) k i)
(push (list i (1+ i) 'face 'ruler-mode-column-number) props) (push `(,i ,(1+ i) face ruler-mode-column-number) props)
(while (and (> m 0) (>= k 0)) (while (and (> m 0) (>= k 0))
(aset ruler k (aref c (setq m (1- m)))) (aset ruler k (aref c (setq m (1- m))))
(setq k (1- k)))) (setq k (1- k))))
@ -707,43 +707,43 @@ Optional argument PROPS specifies other text properties to apply."
;; Show the `current-column' marker. ;; Show the `current-column' marker.
((= j (current-column)) ((= j (current-column))
(aset ruler i ruler-mode-current-column-char) (aset ruler i ruler-mode-current-column-char)
(push (list i (1+ i) 'face 'ruler-mode-current-column) props)) (push `(,i ,(1+ i) face ruler-mode-current-column) props))
;; Show the `goal-column' marker. ;; Show the `goal-column' marker.
((and goal-column (= j goal-column)) ((and goal-column (= j goal-column))
(aset ruler i ruler-mode-goal-column-char) (aset ruler i ruler-mode-goal-column-char)
(push (list i (1+ i) 'face 'ruler-mode-goal-column) props) (push `(,i ,(1+ i)
(push (list i (1+ i) 'mouse-face 'mode-line-highlight) props) help-echo ,ruler-mode-goal-column-help-echo
(push (list i (1+ i) 'help-echo ruler-mode-goal-column-help-echo) face ruler-mode-goal-column
mouse-face mode-line-highlight)
props)) props))
;; Show the `comment-column' marker. ;; Show the `comment-column' marker.
((= j comment-column) ((= j comment-column)
(aset ruler i ruler-mode-comment-column-char) (aset ruler i ruler-mode-comment-column-char)
(push (list i (1+ i) 'face 'ruler-mode-comment-column) (push `(,i ,(1+ i)
props) help-echo ,ruler-mode-comment-column-help-echo
(push (list i (1+ i) 'mouse-face 'mode-line-highlight) face ruler-mode-comment-column
props) mouse-face mode-line-highlight)
(push (list i (1+ i) 'help-echo ruler-mode-comment-column-help-echo)
props)) props))
;; Show the `fill-column' marker. ;; Show the `fill-column' marker.
((= j fill-column) ((= j fill-column)
(aset ruler i ruler-mode-fill-column-char) (aset ruler i ruler-mode-fill-column-char)
(push (list i (1+ i) 'face 'ruler-mode-fill-column) props) (push `(,i ,(1+ i)
(push (list i (1+ i) 'mouse-face 'mode-line-highlight) props) help-echo ,ruler-mode-fill-column-help-echo
(push (list i (1+ i) 'help-echo ruler-mode-fill-column-help-echo) face ruler-mode-fill-column
mouse-face mode-line-highlight)
props)) props))
;; Show the `tab-stop-list' markers. ;; Show the `tab-stop-list' markers.
((and ruler-mode-show-tab-stops (= j (indent-next-tab-stop (1- j)))) ((and ruler-mode-show-tab-stops (= j (indent-next-tab-stop (1- j))))
(aset ruler i ruler-mode-tab-stop-char) (aset ruler i ruler-mode-tab-stop-char)
(push (list i (1+ i) 'face 'ruler-mode-tab-stop) props))) (push `(,i ,(1+ i) face ruler-mode-tab-stop) props)))
(setq i (1+ i) (setq i (1+ i)
j (1+ j))) j (1+ j)))
(let ((ruler-str (concat ruler)) (let ((ruler-str (concat ruler))
(len (length ruler))) (len (length ruler)))
(dolist (c ruler-wide-props) (add-text-properties 0 len ruler-wide-props ruler-str)
(put-text-property 0 len (car c) (cdr c) ruler-str))
(dolist (p (nreverse props)) (dolist (p (nreverse props))
(put-text-property (nth 0 p) (nth 1 p) (nth 2 p) (nth 3 p) ruler-str)) (add-text-properties (nth 0 p) (nth 1 p) (nthcdr 2 p) ruler-str))
;; Return the ruler propertized string. Using list here, ;; Return the ruler propertized string. Using list here,
;; instead of concat visually separate the different areas. ;; instead of concat visually separate the different areas.