From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Po Lu via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#54481: 29.0.50; [PATCH] Rewrite hl-line Date: Wed, 23 Mar 2022 16:48:21 +0800 Message-ID: <87czidcawa.fsf@yahoo.com> References: <877d8o8vuk.fsf@dick> <87y213mjf7.fsf@gnus.org> <87tubqkf3g.fsf@yahoo.com> Reply-To: Po Lu Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="39784"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.91 (gnu/linux) Cc: 54481@debbugs.gnu.org, dick To: Lars Ingebrigtsen Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Wed Mar 23 09:51:25 2022 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nWwiB-000ABW-69 for geb-bug-gnu-emacs@m.gmane-mx.org; Wed, 23 Mar 2022 09:51:23 +0100 Original-Received: from localhost ([::1]:52600 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nWwiA-00005D-7L for geb-bug-gnu-emacs@m.gmane-mx.org; Wed, 23 Mar 2022 04:51:22 -0400 Original-Received: from eggs.gnu.org ([209.51.188.92]:33566) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nWwfu-0006lz-2Z for bug-gnu-emacs@gnu.org; Wed, 23 Mar 2022 04:49:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:48821) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nWwft-0004yz-ON for bug-gnu-emacs@gnu.org; Wed, 23 Mar 2022 04:49:01 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1nWwft-0004Jf-N4 for bug-gnu-emacs@gnu.org; Wed, 23 Mar 2022 04:49:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Po Lu Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Wed, 23 Mar 2022 08:49:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 54481 X-GNU-PR-Package: emacs Original-Received: via spool by 54481-submit@debbugs.gnu.org id=B54481.164802532216557 (code B ref 54481); Wed, 23 Mar 2022 08:49:01 +0000 Original-Received: (at 54481) by debbugs.gnu.org; 23 Mar 2022 08:48:42 +0000 Original-Received: from localhost ([127.0.0.1]:42718 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nWwfZ-0004Iz-GN for submit@debbugs.gnu.org; Wed, 23 Mar 2022 04:48:42 -0400 Original-Received: from sonic306-20.consmr.mail.ne1.yahoo.com ([66.163.189.82]:39432) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nWwfW-0004Ii-KJ for 54481@debbugs.gnu.org; Wed, 23 Mar 2022 04:48:40 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1648025311; bh=i9LFlcFcg73aDffnAjvxwWhY5BG/RI3MFwqJ81R7Jl0=; h=From:To:Cc:Subject:References:Date:In-Reply-To:From:Subject:Reply-To; b=Eiywfm/sJn9t5rF7OZvHed6JRAnNAbqWJT34LcZjQ+d9an3ByO+5ZN6Y3x6VBearc/CrAv5aa/IbxOzcz2qFcIqcBvgkSLC5jvRnvjeVI478J1VEEe1rxQ9GNKUys6J1Sm9oHoemT64vRVerTBXtDPIgtnm4TYg7qhk9yJI2N7epIihXlLPA66vGJvlXu3NUobhfiJbW5qMRX5GVbVdEqw1hRAn/WZdaMK+2a9pIayVEWPDAYYVEmRzCA7cT4QZkOQ0BuzvA22m/w4yigYOGaXljGP/UC+RpVbjR7Ww/JSczSQZykXBxeXEUyde8pwn2B51eEy/ZRA0jdehdQtDk6A== X-SONIC-DKIM-SIGN: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1648025311; bh=CjJdlVapfdMyXEiSxG/W2ytDc599aa/KFassNdMQs4+=; h=X-Sonic-MF:From:To:Subject:Date:From:Subject; b=dr8UMBUIV6TnX1R3iZ5XX8CjlbK+pksKOKAtxsBTZQqXnKznccOwoYnRsLUMA/K8+D16tLhpGAMTohpPP06rTvuF7pUZO4RuW6qRI87c95EyEaUnph8BXkvmtYPtDiTxNj/jc4OgzUeBKjqywu9zlahaQb0KyV8J46XvSd4tinE+fu8DArRe/z3kCDFpaWmd8uxWXUJGlFKCIrlj//l08PYiTkkP6BwN3lZhCHLtqu7+jHqKa6ylPA9V/COkK0k2m/a4VjMy15iuII4sWkfB/g9NoNYbzOw0uvwKc5l18ekkyh6mU3PmYAS59hKlkdU4DOQcJR26ukaZBoXuqTIEZA== X-YMail-OSG: S6MRKTEVM1mrwQxfZoKpqNVp8LnGEAubTQGRCaVVC9f5LlThX_pU9QVPiCGQX5_ 2oqVgvbGQaHPvEok4J_Z5GH49CAB0VNo9PnGefJYJFc4tT_4pDl69UY5txZSUeyhUriH1G_.XrIa HJsf7nczGTKBg.erzuXuwd52YV20fxi_oWEXovE8d6elP1UR927IUD3U14KUitrUTDjElLidDIBP 4HwQelzBldvdL8uIyVjKLsiFGGErexv8RzIFpVM1J7Ip1Zsyx.2xFfQU1Yd66S4qAT425iqgw4XA euOsIztbb9FduTThZ.Vjze3A.48mFTPl_abkJm.l6icJn__GMgDAJTc2lroBlX._z1dxt1J_e.Yq thLwbBlVpaNpXrEvsTNedUHgL48pf6ql4bgYr3T1H8oecj2qCzBkBSGSTunrwprhvuxSQlXkBQsd TUstw6SNTKYXnSyPFWkhJQ_VEGJPfqWY9nMyJGDR7j0Nl29otEOsG9shVVTIfQpmI5Ft4MKf5e_4 4s3amn2iM.wmGLXYPL.OxhiA.N7MMKB0mhExO015JpUsoKvpfP1Ye5isHDgfFrSMZzQW_P.yDK9E HUkIVb8HUq84FxsJGSCWK8xLhnX8qUfSXRCWk0KL4llT.Jd3B1zHKhAPrbNMCUgmTOLsPd5L2GK7 3PmFBWPb8FqvM.Wcl8SIOZY69sxqn5LaFp6FnMTzKmfFYbrE0S.fDWN6rgXVPuw9_YLsZM79AQ27 XzHVdizhEn1tNqtTNptIjd8mldC2TQHx1.MhA1Yk3INxbOKi_IgzhJ1JkPchLhaFyGj3rNm9Xybo sV96xpif1IAyUeUPoim806zg9iOy1540m62cOFSvG7 X-Sonic-MF: Original-Received: from sonic.gate.mail.ne1.yahoo.com by sonic306.consmr.mail.ne1.yahoo.com with HTTP; Wed, 23 Mar 2022 08:48:31 +0000 Original-Received: by kubenode518.mail-prod1.omega.sg3.yahoo.com (VZM Hermes SMTP Server) with ESMTPA ID 995bbe2a5419412c53b82d4d706f6605; Wed, 23 Mar 2022 08:48:25 +0000 (UTC) In-Reply-To: <87tubqkf3g.fsf@yahoo.com> (Po Lu's message of "Tue, 22 Mar 2022 08:25:55 +0800") X-Mailer: WebService/1.1.19894 mail.backend.jedi.jws.acl:role.jedi.acl.token.atz.jws.hermes.yahoo X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:228792 Archived-At: Po Lu writes: > Now, ironically, that patch introduced at least two incompatible changes > which are likely to affect users: `hl-line' is no longer a face, and > `hl-line-face' (a variable) has been replaced by the face. > > Of course, with no updates to the version field, NEWS, or the > documentation. I tried to fix it, but I might've missed something. And that was before I realized there's no way to set stickyness separately for the global mode and minor mode anymore. Not to mention the plethora of unwarranted (and undocumented) obsoletions, especially that of `hl-line-range-function', which is supposed to be used by packages to customize the position of the highlight overlay. So how about this to get us back to where we were, with the one real improvement of `hl-line-overlay-priority' being made a defcustom? diff --git a/lisp/hl-line.el b/lisp/hl-line.el index f1c2e1ebf2..20b3f4160f 100644 --- a/lisp/hl-line.el +++ b/lisp/hl-line.el @@ -24,40 +24,58 @@ ;;; Commentary: -;; Proper scuttling of unsticky overlays relies on `post-command-hook` -;; being called on a buffer switch and the stationarity of -;; `hl-line--buffer` across switches. One could easily imagine -;; programatically defeating unsticky overlays by bypassing -;; `post-command-hook`. +;; Provides a local minor mode (toggled by M-x hl-line-mode) and +;; a global minor mode (toggled by M-x global-hl-line-mode) to +;; highlight, on a suitable terminal, the line on which point is. The +;; global mode highlights the current line in the selected window only +;; (except when the minibuffer window is selected). This was +;; implemented to satisfy a request for a feature of Lesser Editors. +;; The local mode is sticky: it highlights the line about the buffer's +;; point even if the buffer's window is not selected. Caveat: the +;; buffer's point might be different from the point of a non-selected +;; window. Set the variable `hl-line-sticky-flag' to nil to make the +;; local mode behave like the global mode. -;;; Code: +;; You probably don't really want to use the global mode; if the +;; cursor is difficult to spot, try changing its color, relying on +;; `blink-cursor-mode' or both. The hookery used might affect +;; response noticeably on a slow machine. The local mode may be +;; useful in non-editing buffers such as Gnus or PCL-CVS though. + +;; An overlay is used. In the non-sticky cases, this overlay is +;; active only on the selected window. A hook is added to +;; `post-command-hook' to activate the overlay and move it to the line +;; about point. + +;; You could make variable `global-hl-line-mode' buffer-local and set +;; it to nil to avoid highlighting specific buffers, when the global +;; mode is used. -(make-obsolete-variable 'hl-line-overlay 'hl-line--overlay "29.1") -(make-obsolete-variable 'global-hl-line-overlay nil "29.1") -(make-obsolete-variable 'global-hl-line-overlays nil "29.1") -(make-obsolete-variable 'global-hl-line-sticky-flag nil "29.1") -(make-obsolete-variable 'hl-line-overlay-buffer 'hl-line--buffer "29.1") -(make-obsolete-variable 'hl-line-range-function nil "29.1") +;; By default the whole line is highlighted. The range of highlighting +;; can be changed by defining an appropriate function as the +;; buffer-local value of `hl-line-range-function'. -(defvar-local hl-line--overlay nil - "The prevailing highlighting overlay per buffer.") +;;; Code: + +(defvar-local hl-line-overlay nil + "Overlay used by Hl-Line mode to highlight the current line.") -(defvar hl-line--buffer nil - "Used to track last buffer.") +(defvar-local global-hl-line-overlay nil + "Overlay used by Global-Hl-Line mode to highlight the current line.") -;; 1. define-minor-mode creates buffer-local hl-line--overlay -;; 2. overlay wiped by kill-all-local-variables -;; 3. post-command-hook dupes overlay -;; Solution: prevent step 2. -(put 'hl-line--overlay 'permanent-local t) +(defvar global-hl-line-overlays nil + "Overlays used by Global-Hl-Line mode in various buffers. +Global-Hl-Line keeps displaying one overlay in each buffer +when `global-hl-line-sticky-flag' is non-nil.") (defgroup hl-line nil "Highlight the current line." :version "21.1" :group 'convenience) -(defface hl-line '((t :inherit highlight :extend t)) - "Default face for highlighting the current line in hl-line-mode." +(defface hl-line + '((t :inherit highlight :extend t)) + "Default face for highlighting the current line in Hl-Line mode." :version "22.1" :group 'hl-line) @@ -69,77 +87,204 @@ hl-line-face (set symbol value) (dolist (buffer (buffer-list)) (with-current-buffer buffer - (when hl-line--overlay - (overlay-put hl-line--overlay 'face hl-line-face)))))) + (when (overlayp hl-line-overlay) + (overlay-put hl-line-overlay 'face hl-line-face)))) + (when (overlayp global-hl-line-overlay) + (overlay-put global-hl-line-overlay 'face hl-line-face)))) (defcustom hl-line-sticky-flag t - "Non-nil to preserve highlighting overlay when focus leaves window." + "Non-nil means the HL-Line mode highlight appears in all windows. +Otherwise Hl-Line mode will highlight only in the selected +window. Setting this variable takes effect the next time you use +the command `hl-line-mode' to turn Hl-Line mode on. + +This variable has no effect in Global Highlight Line mode. +For that, use `global-hl-line-sticky-flag'." :type 'boolean :version "22.1" - :group 'hl-line - :initialize #'custom-initialize-default - :set (lambda (symbol value) - (set-default symbol value) - (unless value - (let ((selected (window-buffer (selected-window)))) - (dolist (buffer (buffer-list)) - (unless (eq buffer selected) - (with-current-buffer buffer - (hl-line-unhighlight)))))))) + :group 'hl-line) + +(defcustom global-hl-line-sticky-flag nil + "Non-nil means the Global HL-Line mode highlight appears in all windows. +Otherwise Global Hl-Line mode will highlight only in the selected +window. Setting this variable takes effect the next time you use +the command `global-hl-line-mode' to turn Global Hl-Line mode on." + :type 'boolean + :version "24.1" + :group 'hl-line) + +(defvar hl-line-range-function nil + "If non-nil, function to call to return highlight range. +The function of no args should return a cons cell; its car value +is the beginning position of highlight and its cdr value is the +end position of highlight in the buffer. +It should return nil if there's no region to be highlighted. + +This variable is expected to be made buffer-local by modes.") + +(defvar hl-line-overlay-buffer nil + "Most recently visited buffer in which Hl-Line mode is enabled.") (defcustom hl-line-overlay-priority -50 "Priority used on the overlay used by hl-line." :type 'integer - :version "22.1" - :group 'hl-line) - -(defcustom hl-line-highlight-hook nil - "After hook for `hl-line-highlight'. -Currently used in calendar/todo-mode." - :type 'hook + :version "28.1" :group 'hl-line) ;;;###autoload (define-minor-mode hl-line-mode - "Toggle highlighting of the current line." + "Toggle highlighting of the current line (Hl-Line mode). + +Hl-Line mode is a buffer-local minor mode. If +`hl-line-sticky-flag' is non-nil, Hl-Line mode highlights the +line about the buffer's point in all windows. Caveat: the +buffer's point might be different from the point of a +non-selected window. Hl-Line mode uses the function +`hl-line-highlight' on `post-command-hook' in this case. + +When `hl-line-sticky-flag' is nil, Hl-Line mode highlights the +line about point in the selected window only." :group 'hl-line (if hl-line-mode (progn - (hl-line-highlight) + ;; In case `kill-all-local-variables' is called. (add-hook 'change-major-mode-hook #'hl-line-unhighlight nil t) + (hl-line-highlight) + (setq hl-line-overlay-buffer (current-buffer)) (add-hook 'post-command-hook #'hl-line-highlight nil t)) (remove-hook 'post-command-hook #'hl-line-highlight t) - (remove-hook 'change-major-mode-hook #'hl-line-unhighlight t) + (hl-line-unhighlight) + (remove-hook 'change-major-mode-hook #'hl-line-unhighlight t))) + +(defun hl-line-make-overlay () + (let ((ol (make-overlay (point) (point)))) + (overlay-put ol 'priority hl-line-overlay-priority) ;(bug#16192) + (overlay-put ol 'face hl-line-face) + ol)) + +(defun hl-line-highlight () + "Activate the Hl-Line overlay on the current line." + (if hl-line-mode ; Might be changed outside the mode function. + (progn + (unless (overlayp hl-line-overlay) + (setq hl-line-overlay (hl-line-make-overlay))) ; To be moved. + (overlay-put hl-line-overlay + 'window (unless hl-line-sticky-flag (selected-window))) + (hl-line-move hl-line-overlay) + (hl-line-maybe-unhighlight)) (hl-line-unhighlight))) (defun hl-line-unhighlight () - (when hl-line--overlay - (delete-overlay hl-line--overlay) - (setq hl-line--overlay nil))) + "Deactivate the Hl-Line overlay on the current line." + (when (overlayp hl-line-overlay) + (delete-overlay hl-line-overlay) + (setq hl-line-overlay nil))) -(defun hl-line-highlight () - (unless (minibufferp) - (unless hl-line--overlay - (setq hl-line--overlay - (let ((ol (make-overlay (point) (point)))) - (prog1 ol - (overlay-put ol 'priority hl-line-overlay-priority) - (overlay-put ol 'face hl-line-face))))) - (move-overlay hl-line--overlay - (line-beginning-position) - (line-beginning-position 2)) - (when (and (not (eq hl-line--buffer (current-buffer))) +(defun hl-line-maybe-unhighlight () + "Maybe deactivate the Hl-Line overlay on the current line. +Specifically, when `hl-line-sticky-flag' is nil deactivate all +such overlays in all buffers except the current one." + (let ((hlob hl-line-overlay-buffer) + (curbuf (current-buffer))) + (when (and (buffer-live-p hlob) (not hl-line-sticky-flag) - (buffer-live-p hl-line--buffer)) - (with-current-buffer hl-line--buffer + (not (eq curbuf hlob)) + (not (minibufferp))) + (with-current-buffer hlob (hl-line-unhighlight))) - (setq hl-line--buffer (current-buffer)) - (run-hooks 'hl-line-highlight-hook))) + (when (and (overlayp hl-line-overlay) + (eq (overlay-buffer hl-line-overlay) curbuf)) + (setq hl-line-overlay-buffer curbuf)))) + +;;;###autoload +(define-minor-mode global-hl-line-mode + "Toggle line highlighting in all buffers (Global Hl-Line mode). + +If `global-hl-line-sticky-flag' is non-nil, Global Hl-Line mode +highlights the line about the current buffer's point in all live +windows. + +Global-Hl-Line mode uses the function `global-hl-line-highlight' +on `post-command-hook'." + :global t + :group 'hl-line + (if global-hl-line-mode + (progn + ;; In case `kill-all-local-variables' is called. + (add-hook 'change-major-mode-hook #'global-hl-line-unhighlight) + (global-hl-line-highlight-all) + (add-hook 'post-command-hook #'global-hl-line-highlight)) + (global-hl-line-unhighlight-all) + (remove-hook 'post-command-hook #'global-hl-line-highlight) + (remove-hook 'change-major-mode-hook #'global-hl-line-unhighlight))) + +(defun global-hl-line-highlight () + "Highlight the current line in the current window." + (when global-hl-line-mode ; Might be changed outside the mode function. + (unless (window-minibuffer-p) + (unless (overlayp global-hl-line-overlay) + (setq global-hl-line-overlay (hl-line-make-overlay))) ; To be moved. + (unless (member global-hl-line-overlay global-hl-line-overlays) + (push global-hl-line-overlay global-hl-line-overlays)) + (overlay-put global-hl-line-overlay 'window + (unless global-hl-line-sticky-flag + (selected-window))) + (hl-line-move global-hl-line-overlay) + (global-hl-line-maybe-unhighlight)))) + +(defun global-hl-line-highlight-all () + "Highlight the current line in all live windows." + (walk-windows (lambda (w) + (with-current-buffer (window-buffer w) + (global-hl-line-highlight))) + nil t)) + +(defun global-hl-line-unhighlight () + "Deactivate the Global-Hl-Line overlay on the current line." + (when (overlayp global-hl-line-overlay) + (delete-overlay global-hl-line-overlay) + (setq global-hl-line-overlay nil))) -(defun hl-line-turn-on () - (unless (minibufferp) - (let (inhibit-quit) - (hl-line-mode 1)))) +(defun global-hl-line-maybe-unhighlight () + "Maybe deactivate the Global-Hl-Line overlay on the current line. +Specifically, when `global-hl-line-sticky-flag' is nil deactivate +all such overlays in all buffers except the current one." + (mapc (lambda (ov) + (let ((ovb (overlay-buffer ov))) + (when (and (not global-hl-line-sticky-flag) + (bufferp ovb) + (not (eq ovb (current-buffer))) + (not (minibufferp))) + (with-current-buffer ovb + (global-hl-line-unhighlight))))) + global-hl-line-overlays)) + +(defun global-hl-line-unhighlight-all () + "Deactivate all Global-Hl-Line overlays." + (mapc (lambda (ov) + (let ((ovb (overlay-buffer ov))) + (when (bufferp ovb) + (with-current-buffer ovb + (global-hl-line-unhighlight))))) + global-hl-line-overlays) + (setq global-hl-line-overlays nil)) + +(defun hl-line-move (overlay) + "Move the Hl-Line overlay. +If `hl-line-range-function' is non-nil, move the OVERLAY to the position +where the function returns. If `hl-line-range-function' is nil, fill +the line including the point by OVERLAY." + (let (tmp b e) + (if hl-line-range-function + (setq tmp (funcall hl-line-range-function) + b (car tmp) + e (cdr tmp)) + (setq tmp t + b (line-beginning-position) + e (line-beginning-position 2))) + (if tmp + (move-overlay overlay b e) + (move-overlay overlay 1 1)))) (defun hl-line-unload-function () "Unload the Hl-Line library." @@ -151,12 +296,6 @@ hl-line-unload-function ;; continue standard unloading nil) -;;;###autoload -(define-globalized-minor-mode global-hl-line-mode - hl-line-mode hl-line-turn-on - :group 'hl-line - :version "29.1") - (provide 'hl-line) ;;; hl-line.el ends here