aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Monnier2024-05-04 12:06:16 -0400
committerStefan Monnier2024-05-04 12:06:16 -0400
commitea752667cd8a146e79e5646bbedc76be08c50141 (patch)
tree18d2298cc96970b2d08edfa3f5e19d3f22ab8053
parentfd859fbea2e9d13e76db1c5295d9ddd1c5955d83 (diff)
downloademacs-ea752667cd8a146e79e5646bbedc76be08c50141.tar.gz
emacs-ea752667cd8a146e79e5646bbedc76be08c50141.zip
(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.
-rw-r--r--lisp/ruler-mode.el70
1 files changed, 35 insertions, 35 deletions
diff --git a/lisp/ruler-mode.el b/lisp/ruler-mode.el
index b9c32a40a07..c7e85b04cfd 100644
--- a/lisp/ruler-mode.el
+++ b/lisp/ruler-mode.el
@@ -350,7 +350,7 @@ nothing is dragged.")
350 350
351(defun ruler-mode-text-scaled-width (width) 351(defun ruler-mode-text-scaled-width (width)
352 "Compute scaled text width according to current font scaling. 352 "Compute scaled text width according to current font scaling.
353Convert a width of char units into a text-scaled char width units, 353Convert a WIDTH of char units into a text-scaled char width units,
354for example `window-hscroll'." 354for example `window-hscroll'."
355 (/ (* width (frame-char-width)) (default-font-width))) 355 (/ (* width (frame-char-width)) (default-font-width)))
356 356
@@ -528,7 +528,7 @@ START-EVENT is the mouse click event."
528(defvar ruler-mode-header-line-format-old nil 528(defvar ruler-mode-header-line-format-old nil
529 "Hold previous value of `header-line-format'.") 529 "Hold previous value of `header-line-format'.")
530 530
531(defvar ruler-mode-ruler-function 'ruler-mode-ruler 531(defvar ruler-mode-ruler-function #'ruler-mode-ruler
532 "Function to call to return ruler header line format. 532 "Function to call to return ruler header line format.
533This variable is expected to be made buffer-local by modes.") 533This variable is expected to be made buffer-local by modes.")
534 534
@@ -563,7 +563,7 @@ format first."
563 (ruler--save-header-line-format)) 563 (ruler--save-header-line-format))
564 (setq ruler-mode enable))) 564 (setq ruler-mode enable)))
565 (if ruler-mode 565 (if ruler-mode
566 (add-hook 'post-command-hook 'force-mode-line-update nil t) 566 (add-hook 'post-command-hook #'force-mode-line-update nil t)
567 ;; When `ruler-mode' is off restore previous header line format if 567 ;; When `ruler-mode' is off restore previous header line format if
568 ;; the current one is the ruler header line format. 568 ;; the current one is the ruler header line format.
569 (when (eq header-line-format ruler-mode-header-line-format) 569 (when (eq header-line-format ruler-mode-header-line-format)
@@ -571,7 +571,7 @@ format first."
571 (when (local-variable-p 'ruler-mode-header-line-format-old) 571 (when (local-variable-p 'ruler-mode-header-line-format-old)
572 (setq header-line-format ruler-mode-header-line-format-old) 572 (setq header-line-format ruler-mode-header-line-format-old)
573 (kill-local-variable 'ruler-mode-header-line-format-old))) 573 (kill-local-variable 'ruler-mode-header-line-format-old)))
574 (remove-hook 'post-command-hook 'force-mode-line-update t))) 574 (remove-hook 'post-command-hook #'force-mode-line-update t)))
575 575
576;; Add ruler-mode to the minor mode menu in the mode line 576;; Add ruler-mode to the minor mode menu in the mode line
577(define-key mode-line-mode-menu [ruler-mode] 577(define-key mode-line-mode-menu [ruler-mode]
@@ -625,7 +625,7 @@ mouse-2: unset goal column"
625(defsubst ruler-mode-space (width &rest props) 625(defsubst ruler-mode-space (width &rest props)
626 "Return a single space string of WIDTH times the normal character width. 626 "Return a single space string of WIDTH times the normal character width.
627Optional argument PROPS specifies other text properties to apply." 627Optional argument PROPS specifies other text properties to apply."
628 (apply 'propertize " " 'display (list 'space :width width) props)) 628 (apply #'propertize " " 'display (list 'space :width width) props))
629 629
630(defun ruler-mode-ruler () 630(defun ruler-mode-ruler ()
631 "Compute and return a header line ruler." 631 "Compute and return a header line ruler."
@@ -665,25 +665,25 @@ Optional argument PROPS specifies other text properties to apply."
665 'face 'ruler-mode-pad)) 665 'face 'ruler-mode-pad))
666 ;; Remember the scrollbar vertical type. 666 ;; Remember the scrollbar vertical type.
667 (sbvt (car (window-current-scroll-bars))) 667 (sbvt (car (window-current-scroll-bars)))
668 ;; Create an "clean" ruler. 668 ;; Create a "clean" ruler.
669 (ruler 669 (ruler
670 ;; Make the part of header-line corresponding to the 670 ;; Make the part of header-line corresponding to the
671 ;; line-number display be blank, not filled with 671 ;; line-number display be blank, not filled with
672 ;; ruler-mode-basic-graduation-char. 672 ;; ruler-mode-basic-graduation-char.
673 (if display-line-numbers 673 (if (> i 0)
674 (let ((lndw (round (line-number-display-width 'columns)))) 674 (vconcat (make-vector i ?\s)
675 (vconcat (make-vector lndw ?\s) 675 (make-vector (- w i)
676 (make-vector (- w lndw) 676 ruler-mode-basic-graduation-char))
677 ruler-mode-basic-graduation-char)))
678 (make-vector w ruler-mode-basic-graduation-char))) 677 (make-vector w ruler-mode-basic-graduation-char)))
679 (ruler-wide-props 678 (ruler-wide-props
680 `((face . ruler-mode-default) 679 `( face ruler-mode-default
681 (local-map . ruler-mode-map) 680 ;; This is redundant with the minor mode map.
682 (help-echo . ,(cond (ruler-mode-show-tab-stops 681 ;;local-map ruler-mode-map
683 ruler-mode-ruler-help-echo-when-tab-stops) 682 help-echo ,(cond (ruler-mode-show-tab-stops
684 (goal-column 683 ruler-mode-ruler-help-echo-when-tab-stops)
685 ruler-mode-ruler-help-echo-when-goal-column) 684 (goal-column
686 (ruler-mode-ruler-help-echo))))) 685 ruler-mode-ruler-help-echo-when-goal-column)
686 (ruler-mode-ruler-help-echo))))
687 (props nil) 687 (props nil)
688 k c) 688 k c)
689 ;; Setup the active area. 689 ;; Setup the active area.
@@ -695,7 +695,7 @@ Optional argument PROPS specifies other text properties to apply."
695 (setq c (number-to-string (/ j 10)) 695 (setq c (number-to-string (/ j 10))
696 m (length c) 696 m (length c)
697 k i) 697 k i)
698 (push (list i (1+ i) 'face 'ruler-mode-column-number) props) 698 (push `(,i ,(1+ i) face ruler-mode-column-number) props)
699 (while (and (> m 0) (>= k 0)) 699 (while (and (> m 0) (>= k 0))
700 (aset ruler k (aref c (setq m (1- m)))) 700 (aset ruler k (aref c (setq m (1- m))))
701 (setq k (1- k)))) 701 (setq k (1- k))))
@@ -707,43 +707,43 @@ Optional argument PROPS specifies other text properties to apply."
707 ;; Show the `current-column' marker. 707 ;; Show the `current-column' marker.
708 ((= j (current-column)) 708 ((= j (current-column))
709 (aset ruler i ruler-mode-current-column-char) 709 (aset ruler i ruler-mode-current-column-char)
710 (push (list i (1+ i) 'face 'ruler-mode-current-column) props)) 710 (push `(,i ,(1+ i) face ruler-mode-current-column) props))
711 ;; Show the `goal-column' marker. 711 ;; Show the `goal-column' marker.
712 ((and goal-column (= j goal-column)) 712 ((and goal-column (= j goal-column))
713 (aset ruler i ruler-mode-goal-column-char) 713 (aset ruler i ruler-mode-goal-column-char)
714 (push (list i (1+ i) 'face 'ruler-mode-goal-column) props) 714 (push `(,i ,(1+ i)
715 (push (list i (1+ i) 'mouse-face 'mode-line-highlight) props) 715 help-echo ,ruler-mode-goal-column-help-echo
716 (push (list i (1+ i) 'help-echo ruler-mode-goal-column-help-echo) 716 face ruler-mode-goal-column
717 mouse-face mode-line-highlight)
717 props)) 718 props))
718 ;; Show the `comment-column' marker. 719 ;; Show the `comment-column' marker.
719 ((= j comment-column) 720 ((= j comment-column)
720 (aset ruler i ruler-mode-comment-column-char) 721 (aset ruler i ruler-mode-comment-column-char)
721 (push (list i (1+ i) 'face 'ruler-mode-comment-column) 722 (push `(,i ,(1+ i)
722 props) 723 help-echo ,ruler-mode-comment-column-help-echo
723 (push (list i (1+ i) 'mouse-face 'mode-line-highlight) 724 face ruler-mode-comment-column
724 props) 725 mouse-face mode-line-highlight)
725 (push (list i (1+ i) 'help-echo ruler-mode-comment-column-help-echo)
726 props)) 726 props))
727 ;; Show the `fill-column' marker. 727 ;; Show the `fill-column' marker.
728 ((= j fill-column) 728 ((= j fill-column)
729 (aset ruler i ruler-mode-fill-column-char) 729 (aset ruler i ruler-mode-fill-column-char)
730 (push (list i (1+ i) 'face 'ruler-mode-fill-column) props) 730 (push `(,i ,(1+ i)
731 (push (list i (1+ i) 'mouse-face 'mode-line-highlight) props) 731 help-echo ,ruler-mode-fill-column-help-echo
732 (push (list i (1+ i) 'help-echo ruler-mode-fill-column-help-echo) 732 face ruler-mode-fill-column
733 mouse-face mode-line-highlight)
733 props)) 734 props))
734 ;; Show the `tab-stop-list' markers. 735 ;; Show the `tab-stop-list' markers.
735 ((and ruler-mode-show-tab-stops (= j (indent-next-tab-stop (1- j)))) 736 ((and ruler-mode-show-tab-stops (= j (indent-next-tab-stop (1- j))))
736 (aset ruler i ruler-mode-tab-stop-char) 737 (aset ruler i ruler-mode-tab-stop-char)
737 (push (list i (1+ i) 'face 'ruler-mode-tab-stop) props))) 738 (push `(,i ,(1+ i) face ruler-mode-tab-stop) props)))
738 (setq i (1+ i) 739 (setq i (1+ i)
739 j (1+ j))) 740 j (1+ j)))
740 741
741 (let ((ruler-str (concat ruler)) 742 (let ((ruler-str (concat ruler))
742 (len (length ruler))) 743 (len (length ruler)))
743 (dolist (c ruler-wide-props) 744 (add-text-properties 0 len ruler-wide-props ruler-str)
744 (put-text-property 0 len (car c) (cdr c) ruler-str))
745 (dolist (p (nreverse props)) 745 (dolist (p (nreverse props))
746 (put-text-property (nth 0 p) (nth 1 p) (nth 2 p) (nth 3 p) ruler-str)) 746 (add-text-properties (nth 0 p) (nth 1 p) (nthcdr 2 p) ruler-str))
747 747
748 ;; Return the ruler propertized string. Using list here, 748 ;; Return the ruler propertized string. Using list here,
749 ;; instead of concat visually separate the different areas. 749 ;; instead of concat visually separate the different areas.