Commit | Line | Data |
---|---|---|
798330fe DL |
1 | ;;; hl-line.el --- highlight the current line |
2 | ||
c5967e91 | 3 | ;; Copyright (C) 1998, 2000 Free Software Foundation, Inc. |
798330fe DL |
4 | |
5 | ;; Author: Dave Love <fx@gnu.org> | |
6 | ;; Created: 1998-09-13 | |
3a850efa | 7 | ;; Keywords: faces, frames, emulation |
798330fe | 8 | |
643c911e DL |
9 | ;; This file is part of GNU Emacs. |
10 | ||
11 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
798330fe DL |
12 | ;; it under the terms of the GNU General Public License as published by |
13 | ;; the Free Software Foundation; either version 2, or (at your option) | |
14 | ;; any later version. | |
15 | ||
643c911e | 16 | ;; GNU Emacs is distributed in the hope that it will be useful, |
798330fe DL |
17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | ;; GNU General Public License for more details. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
22 | ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
23 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
24 | ;; Boston, MA 02111-1307, USA. | |
25 | ||
26 | ;;; Commentary: | |
27 | ||
28 | ;; Provides a global minor mode (toggled by M-x hl-line-mode) to | |
c5967e91 DL |
29 | ;; highlight, on a suitable terminal, the line in the current window |
30 | ;; on which point is (except in a minibuffer window). Done to satisfy | |
31 | ;; a request for a feature of Lesser Editors. | |
798330fe DL |
32 | |
33 | ;; You probably don't really want this; if the cursor is difficult to | |
3a850efa | 34 | ;; spot, try changing its colour, relying on `blink-cursor-mode' or |
6b567d50 | 35 | ;; both. The hookery used might affect response noticeably on a slow |
c5967e91 | 36 | ;; machine. |
798330fe DL |
37 | |
38 | ;; An overlay is used, active only on the selected window. Hooks are | |
39 | ;; added to `pre-command-hook' and `post-command-hook' to activate and | |
40 | ;; deactivate (by deleting) the overlay. `hl-line-unhighlight', on | |
41 | ;; `pre-command-hook', deactivates it unconditionally in case the | |
42 | ;; command changes the selected window. (It does so rather than | |
43 | ;; keeping track of changes in the selected window). | |
44 | ;; `hl-line-highlight', on `post-command-hook', activates it again | |
45 | ;; across the window width. | |
46 | ||
7013279c DL |
47 | ;; You could make variable `hl-line-mode' buffer-local to avoid |
48 | ;; highlighting specific buffers. | |
49 | ||
798330fe DL |
50 | ;;; Code: |
51 | ||
52 | (defgroup hl-line nil | |
53 | "Highliight the current line." | |
eee06d26 | 54 | :version "21.1" |
798330fe DL |
55 | :group 'editing) |
56 | ||
798330fe DL |
57 | (defcustom hl-line-face 'highlight |
58 | "Face with which to highlight the current line." | |
59 | :type 'face | |
60 | :group 'hl-line) | |
61 | ||
62 | (defvar hl-line-overlay nil) | |
798330fe DL |
63 | |
64 | (defun hl-line-highlight () | |
65 | "Active the Hl-Line overlay on the current line in the current window. | |
66 | \(Unless it's a minibuffer window.)" | |
7013279c DL |
67 | (when hl-line-mode ; Could be made buffer-local. |
68 | (unless (window-minibuffer-p (selected-window)) ; silly in minibuffer | |
e6f6401a | 69 | (unless hl-line-overlay |
7013279c DL |
70 | (setq hl-line-overlay (make-overlay 1 1)) ; to be moved |
71 | (overlay-put hl-line-overlay 'face hl-line-face)) | |
72 | (overlay-put hl-line-overlay 'window (selected-window)) | |
73 | (move-overlay hl-line-overlay | |
e6f6401a DL |
74 | (line-beginning-position) (1+ (line-end-position)) |
75 | (current-buffer))))) | |
798330fe DL |
76 | |
77 | (defun hl-line-unhighlight () | |
78 | "Deactivate the Hl-Line overlay on the current line in the current window." | |
79 | (if hl-line-overlay | |
80 | (delete-overlay hl-line-overlay))) | |
81 | ||
ceafc2fa | 82 | ;;;###autoload |
3a850efa | 83 | (define-minor-mode hl-line-mode |
c5967e91 | 84 | "Global minor mode to highlight the line about point in the current window. |
798330fe | 85 | With ARG, turn Hl-Line mode on if ARG is positive, off otherwise. |
798330fe DL |
86 | Uses functions `hl-line-unhighlight' and `hl-line-highlight' on |
87 | `pre-command-hook' and `post-command-hook'." | |
d3d02e65 | 88 | nil nil nil :global t |
3a850efa DL |
89 | |
90 | (if hl-line-mode | |
91 | (progn | |
92 | (add-hook 'pre-command-hook #'hl-line-unhighlight) | |
93 | (add-hook 'post-command-hook #'hl-line-highlight)) | |
94 | (hl-line-unhighlight) | |
95 | (remove-hook 'pre-command-hook #'hl-line-unhighlight) | |
96 | (remove-hook 'post-command-hook #'hl-line-highlight))) | |
798330fe DL |
97 | |
98 | (provide 'hl-line) | |
99 | ||
100 | ;;; hl-line.el ends here |