initial commit
[emacs-init.git] / auto-install / col-highlight.el
1 ;;; col-highlight.el --- Highlight the current column.
2 ;; 
3 ;; Filename: col-highlight.el
4 ;; Description: Highlight the current column.
5 ;; Author: Drew Adams
6 ;; Maintainer: Drew Adams
7 ;; Copyright (C) 2006-2011, Drew Adams, all rights reserved.
8 ;; Created: Fri Sep 08 11:06:35 2006
9 ;; Version: 22.0
10 ;; Last-Updated: Mon Jan  3 19:55:33 2011 (-0800)
11 ;;           By: dradams
12 ;;     Update #: 320
13 ;; URL: http://www.emacswiki.org/cgi-bin/wiki/col-highlight.el
14 ;; Keywords: faces, frames, emulation, highlight, cursor, accessibility
15 ;; Compatibility: GNU Emacs: 22.x, 23.x
16 ;; 
17 ;; Features that might be required by this library:
18 ;;
19 ;;   `vline'.
20 ;;
21 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
22 ;; 
23 ;;; Commentary:
24 ;; 
25 ;;  This library highlights the current column.  When you move the
26 ;;  cursor, the highlighting follows (tracks the cursor), as long as
27 ;;  the highlighting stays on.
28 ;;
29 ;;  Command `column-highlight-mode' toggles this highlighting on and
30 ;;  off.
31 ;;
32 ;;  If you use `column-highlight-mode' twice in succession (I bind it
33 ;;  to `C-+'), you can flash the highlighting to show you the current
34 ;;  column temporarily.  An alternative way to flash-highlight is to
35 ;;  use command `flash-column-highlight' (once).  It shows the
36 ;;  highlighting for just a second or two (see option
37 ;;  `col-highlight-period').
38 ;;
39 ;;  You can also have current-column highlighting come on
40 ;;  automatically, when Emacs is idle.  Command
41 ;;  `toggle-highlight-column-when-idle' toggles this mode.  Command
42 ;;  `col-highlight-set-interval' changes the number of idle seconds to
43 ;;  wait before highlighting.
44 ;;
45 ;;
46 ;;  To use this file, you must also have library `vline.el'.
47 ;;  Put this in your Emacs init file (~/.emacs):
48 ;;
49 ;;    (require 'col-highlight) ; Load this file (and `vline')
50 ;;
51 ;;  If you want to turn on continual current-column highlighting by
52 ;;  default, then add this to your init file:
53 ;;
54 ;;    (column-highlight-mode 1)
55 ;;
56 ;;  If you want to turn on automatic idle highlighting of the current
57 ;;  column, then add this to your init file:
58 ;;
59 ;;    (toggle-highlight-column-when-idle 1)
60 ;;
61 ;;  If you want to use a different wait interval, before idle
62 ;;  highlighting begins, then set it in your init file using
63 ;;  `col-highlight-set-interval':
64 ;;
65 ;;    (col-highlight-set-interval 6) ; Wait 6 idle secs.
66 ;;
67 ;;  Note that `column-highlight-mode' is intentionally a global minor
68 ;;  mode.  If you want a local minor mode, so that highlighting
69 ;;  affects only a particular buffer, you can use `vline-mode' (in
70 ;;  `vline.el').
71 ;;
72 ;;
73 ;;  See also:
74 ;;
75 ;;  * Library `hl-line+.el', which offers the same functionality, but
76 ;;    for the current line instead of the current column.
77 ;;
78 ;;  * Library `crosshairs.el', which combines the features of
79 ;;    `col-highlight.el' and `hl-line+.el', providing a crosshair
80 ;;    highlighting effect.  It requires `col-highlight.el' and
81 ;;    `hl-line+.el'.
82 ;;
83 ;;  * Library `cursor-chg.el' or library `oneonone.el', to change the
84 ;;    cursor type when Emacs is idle.
85 ;;
86 ;;  User options defined here:
87 ;;
88 ;;    `col-highlight-period', `column-highlight-mode',
89 ;;    `col-highlight-vline-face-flag'.
90 ;;
91 ;;  Faces defined here:
92 ;;
93 ;;    `col-highlight'.
94 ;;
95 ;;  Commands defined here:
96 ;;
97 ;;    `col-highlight-flash', `col-highlight-set-interval',
98 ;;    `col-highlight-toggle-when-idle', `column-highlight-mode',
99 ;;    `flash-column-highlight', `toggle-highlight-column-when-idle'.
100 ;;
101 ;;  Non-interactive functions defined here:
102 ;;
103 ;;    `col-highlight-highlight', `col-highlight-unhighlight'.
104 ;;
105 ;;  Internal variables defined here:
106 ;;
107 ;;    `col-highlight-face', `col-highlight-idle-interval',
108 ;;    `col-highlight-idle-timer', `col-highlight-when-idle-p'.
109 ;;
110 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
111 ;; 
112 ;;; Change log:
113 ;;
114 ;; 2011/01/03 dadams
115 ;;     Added autoload cookies for defgroup, defcustom, defface, and commands.
116 ;; 2008/09/03 dadams
117 ;;     col-highlight-highlight: Bind vline-current-window-only to t.
118 ;; 2008/08/08 dadams
119 ;;     col-highlight-(un)highlight: Added optional arg.
120 ;; 2008/01/21 dadams
121 ;;     Use vline.el instead of column-marker.el.
122 ;;     Added: group column-highlight, option col-highlight-vline-face-flag.
123 ;;     col-highlight-toggle-when-idle: col-highlight-unhighlight when turn off.
124 ;;     col-highlight-flash: Use col-highlight-highlight, not mode.
125 ;;     col-highlight-(un)highlight: Respect col-highlight-vline-face-flag.
126 ;;                                  Don't highlight minibuffer.
127 ;;     Renamed: face col-highlight-face to col-highlight.
128 ;;     Removed semi-support for Emacs 20.
129 ;; 2006/09/08 dadams
130 ;;     Created.
131 ;; 
132 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
133 ;; 
134 ;; This program is free software; you can redistribute it and/or
135 ;; modify it under the terms of the GNU General Public License as
136 ;; published by the Free Software Foundation; either version 3, or
137 ;; (at your option) any later version.
138 ;; 
139 ;; This program is distributed in the hope that it will be useful,
140 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
141 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
142 ;; General Public License for more details.
143 ;; 
144 ;; You should have received a copy of the GNU General Public License
145 ;; along with this program; see the file COPYING.  If not, write to
146 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
147 ;; Floor, Boston, MA 02110-1301, USA.
148 ;; 
149 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
150 ;; 
151 ;;; Code:
152
153 (require 'vline)
154
155 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
156
157
158 ;;;###autoload
159 (defgroup column-highlight nil
160   "Highlight the current column."
161   :prefix "col-highlight-"
162   :group 'editing :group 'cursor :group 'hl-line :group 'frames
163   :link `(url-link :tag "Send Bug Report"
164           ,(concat "mailto:" "drew.adams" "@" "oracle" ".com?subject=\
165 col-highlight.el bug: \
166 &body=Describe bug here, starting with `emacs -q'.  \
167 Don't forget to mention your Emacs and library versions."))
168   :link '(url-link :tag "Other Libraries by Drew"
169           "http://www.emacswiki.org/cgi-bin/wiki/DrewsElispLibraries")
170   :link '(url-link :tag "Download"
171           "http://www.emacswiki.org/cgi-bin/wiki/col-highlight.el"))
172
173 ;;;###autoload
174 (defcustom col-highlight-vline-face-flag t
175   "*Non-nil means `column-highlight-mode' uses `col-highlight-face'.
176 nil means that it uses `vline-face'."
177   :type 'boolean :group 'column-highlight)
178
179 ;;;###autoload
180 (defcustom col-highlight-period 1
181   "*Number of seconds to highlight the current column."
182   :type 'integer :group 'column-highlight)
183
184 ;;;###autoload
185 (defface col-highlight '((t (:background "SlateGray3")))
186   "*Face for current-column highlighting by `column-highlight-mode'.
187 Not used if `col-highlight-vline-face-flag' is nil."
188   :group 'column-highlight :group 'faces)
189
190 (defvar col-highlight-face 'col-highlight
191   "Face used for highlighting current column.
192 Do NOT change this.")
193
194 (defvar col-highlight-idle-interval 5
195   "Number of seconds to wait before highlighting current column.
196 Do NOT change this yourself to change the wait period; instead, use
197 `\\[col-highlight-set-interval]'.")
198
199 (defvar col-highlight-when-idle-p nil
200   "Non-nil means highlight the current column whenever Emacs is idle.
201 Do NOT change this yourself; instead, use
202 `\\[toggle-highlight-column-when-idle]'.")
203
204 (defvar col-highlight-idle-timer
205   (progn                              ; Cancel to prevent duplication.
206     (when (boundp 'col-highlight-idle-timer)
207       (cancel-timer col-highlight-idle-timer))
208     (run-with-idle-timer col-highlight-idle-interval t 'col-highlight-highlight))
209   "Timer used to highlight current column whenever Emacs is idle.")
210
211 ;; Turn it off, by default.
212 ;; You must use `toggle-highlight-column-when-idle' to turn it on.
213 (cancel-timer col-highlight-idle-timer)
214
215 ;;;###autoload
216 (define-minor-mode column-highlight-mode
217     "Toggle highlighting the current column.
218 With ARG, turn column highlighting on if and only if ARG is positive.
219
220 Column-Highlight mode uses the functions
221 `col-highlight-unhighlight' and `col-highlight-highlight'
222 on `pre-command-hook' and `post-command-hook'."
223   :init-value nil :global t :group 'column-highlight
224   :link `(url-link :tag "Send Bug Report"
225                    ,(concat "mailto:" "drew.adams" "@" "oracle" ".com?subject=\
226 col-highlight.el bug: \
227 &body=Describe bug here, starting with `emacs -q'.  \
228 Don't forget to mention your Emacs and library versions."))
229   :link '(url-link :tag "Other Libraries by Drew"
230           "http://www.emacswiki.org/cgi-bin/wiki/DrewsElispLibraries")
231   :link '(url-link :tag
232           "Download" "http://www.emacswiki.org/cgi-bin/wiki/col-highlight.el")
233   :link '(url-link :tag "Description"
234           "http://www.emacswiki.org/cgi-bin/wiki/ChangingCursorDynamically")
235   :link '(emacs-commentary-link :tag "Commentary" "col-highlight")
236   (cond (column-highlight-mode
237          (add-hook 'pre-command-hook #'col-highlight-unhighlight)
238          (add-hook 'post-command-hook #'col-highlight-highlight))
239         (t
240          (col-highlight-unhighlight)
241          (remove-hook 'pre-command-hook #'col-highlight-unhighlight)
242          (remove-hook 'post-command-hook #'col-highlight-highlight))))
243
244 ;;;###autoload
245 (defalias 'toggle-highlight-column-when-idle 'col-highlight-toggle-when-idle)
246 ;;;###autoload
247 (defun col-highlight-toggle-when-idle (&optional arg)
248   "Turn on or off highlighting the current column when Emacs is idle.
249 With prefix argument, turn on if ARG > 0; else turn off."
250   (interactive "P")
251   (setq col-highlight-when-idle-p
252         (if arg (> (prefix-numeric-value arg) 0) (not col-highlight-when-idle-p)))
253   (cond (col-highlight-when-idle-p
254          (timer-activate-when-idle col-highlight-idle-timer)
255          (add-hook 'pre-command-hook #'col-highlight-unhighlight)
256          (message "Turned ON highlighting current column when Emacs is idle."))
257         (t
258          (cancel-timer col-highlight-idle-timer)
259          (col-highlight-unhighlight)
260          (remove-hook 'pre-command-hook #'col-highlight-unhighlight)
261          (message "Turned OFF highlighting current column when Emacs is idle."))))
262
263 ;;;###autoload
264 (defun col-highlight-set-interval (secs)
265   "Set wait until highlight current column when Emacs is idle.
266 Whenever Emacs is idle for this many seconds, the current column
267 will be highlighted in the face that is the value of variable
268 `col-highlight-face'.
269
270 To turn on or off automatically highlighting the current column
271 when Emacs is idle, use `\\[toggle-highlight-column-when-idle]."
272   (interactive
273    "nSeconds to idle, before highlighting current column: ")
274   (timer-set-idle-time col-highlight-idle-timer
275                        (setq col-highlight-idle-interval secs) t))
276
277
278 ;;;###autoload
279 (defalias 'flash-column-highlight 'col-highlight-flash)
280 ;;;###autoload
281 (defun col-highlight-flash (&optional arg)
282   "Highlight the current column for `col-highlight-period' seconds.
283 With a prefix argument, highlight for that many seconds."
284   (interactive)
285   (col-highlight-highlight)
286   (let ((column-period col-highlight-period))
287     (when current-prefix-arg
288       (setq column-period (prefix-numeric-value current-prefix-arg)))
289     (run-at-time column-period nil #'col-highlight-unhighlight)))
290
291 (defun col-highlight-highlight (&optional minibuffer-also-p)
292   "Highlight current column."
293   (unless (and (minibufferp) (not minibuffer-also-p))
294     (let ((vline-current-window-only t))
295       (if col-highlight-vline-face-flag
296           (let ((vline-style 'face)
297                 (vline-face col-highlight-face))
298             (vline-show))
299         (vline-show)))))
300
301 (defun col-highlight-unhighlight (&optional minibuffer-also-p)
302   "Turn off highlighting of current column.
303 If optional arg MINIBUFFER-ALSO-P is nil, then do nothing if the
304 current buffer is the minibuffer. "
305   (unless (and (minibufferp) (not minibuffer-also-p))
306     (if col-highlight-vline-face-flag
307         (let ((vline-style 'face)
308               (vline-face col-highlight-face))
309           (vline-clear))
310       (vline-clear))))
311
312 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
313
314 (provide 'col-highlight)
315
316 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
317 ;;; col-highlight.el ends here