*** empty log message ***
[bpt/emacs.git] / lisp / rfn-eshadow.el
CommitLineData
519f7ff6
MB
1;;; rfn-eshadow.el --- Highlight `shadowed' part of read-file-name input text
2;;
0d30b337
TTN
3;; Copyright (C) 2000, 2001, 2002, 2003, 2004,
4;; 2005 Free Software Foundation, Inc.
519f7ff6
MB
5;;
6;; Author: Miles Bader <miles@gnu.org>
476cf013 7;; Keywords: convenience minibuffer
519f7ff6
MB
8
9;; This file is part of GNU Emacs.
10
11;; GNU Emacs is free software; you can redistribute it and/or modify
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
16;; GNU Emacs is distributed in the hope that it will be useful,
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
086add15
LK
23;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24;; Boston, MA 02110-1301, USA.
519f7ff6
MB
25
26;;; Commentary:
27;;
476cf013 28;; Defines the mode `file-name-shadow-mode'.
519f7ff6
MB
29;;
30;; The `read-file-name' function passes its result through
31;; `substitute-in-file-name', so any part of the string preceding
32;; multiple slashes (or a drive indicator on MS-DOS/MS-Windows) is
33;; ignored.
34;;
476cf013 35;; If `file-name-shadow-mode' is active, any part of the
519f7ff6 36;; minibuffer text that would be ignored because of this is given the
476cf013 37;; properties in `file-name-shadow-properties', which may
519f7ff6
MB
38;; be used to make the ignored text invisible, dim, etc.
39;;
40
41;;; Code:
42
43\f
44;;; Customization
45
476cf013 46(defconst file-name-shadow-properties-custom-type
519f7ff6
MB
47 '(list
48 (checklist :inline t
49 (const :tag "Invisible"
50 :doc "Make shadowed part of filename invisible"
51 :format "%t%n%h"
52 :inline t
53 (invisible t intangible t))
54 (list :inline t
55 :format "%v"
56 :tag "Face"
57 :doc "Display shadowed part of filename using a different face"
58 (const :format "" face)
476cf013 59 (face :value file-name-shadow))
519f7ff6
MB
60 (list :inline t
61 :format "%t: %v%h"
62 :tag "Brackets"
63 ;; Note the 4 leading spaces in the doc string;
64 ;; this is hack to get around the fact that the
65 ;; newline after the second string widget comes
66 ;; from the string widget, and doesn't indent
67 ;; correctly. We could use a :size attribute to
68 ;; make the second string widget not have a
69 ;; terminating newline, but this makes it impossible
70 ;; to enter trailing whitespace, and it's desirable
71 ;; that it be possible.
72 :doc " Surround shadowed part of filename with brackets"
73 (const :format "" before-string)
74 (string :format "%v" :size 4 :value "{")
75 (const :format "" after-string)
76 ;; see above about why the 2nd string doesn't use :size
77 (string :format " and: %v" :value "} "))
78 (list :inline t
79 :format "%t: %v%n%h"
80 :tag "String"
81 :doc "Display a string instead of the shadowed part of filename"
82 (const :format "" display)
83 (string :format "%v" :size 15 :value "<...ignored...>"))
84 (const :tag "Avoid"
85 :doc "Try to keep cursor out of shadowed part of filename"
86 :format "%t%n%h"
87 :inline t
88 (field shadow)))
89 (repeat :inline t
90 :tag "Other Properties"
91 (list :inline t
92 :format "%v"
93 (symbol :tag "Property")
94 (sexp :tag "Value")))))
95
18a90ec7 96;;;###autoload
476cf013
MB
97(defcustom file-name-shadow-properties
98 '(face file-name-shadow field shadow)
519f7ff6 99 "Properties given to the `shadowed' part of a filename in the minibuffer.
476cf013 100Only used when `file-name-shadow-mode' is active.
2fe3d6ec 101If Emacs is not running under a window system,
476cf013
MB
102`file-name-shadow-tty-properties' is used instead."
103 :type file-name-shadow-properties-custom-type
519f7ff6
MB
104 :group 'minibuffer)
105
18a90ec7 106;;;###autoload
476cf013 107(defcustom file-name-shadow-tty-properties
519f7ff6
MB
108 '(before-string "{" after-string "} " field shadow)
109 "Properties given to the `shadowed' part of a filename in the minibuffer.
476cf013 110Only used when `file-name-shadow-mode' is active and emacs
519f7ff6 111is not running under a window-system; if emacs is running under a window
476cf013
MB
112system, `file-name-shadow-properties' is used instead."
113 :type file-name-shadow-properties-custom-type
519f7ff6
MB
114 :group 'minibuffer)
115
476cf013 116(defface file-name-shadow
c3423c97 117 '((t :inherit shadow))
476cf013 118 "Face used by `file-name-shadow-mode' for the shadow."
519f7ff6
MB
119 :group 'minibuffer)
120
121\f
122;;; Internal variables
123
519f7ff6
MB
124;; A list of minibuffers to which we've added a post-command-hook.
125(defvar rfn-eshadow-frobbed-minibufs nil)
126
127;; An overlay covering the shadowed part of the filename (local to the
128;; minibuffer).
129(defvar rfn-eshadow-overlay)
130(make-variable-buffer-local 'rfn-eshadow-overlay)
131
132\f
133;;; Hook functions
134
135;; This function goes on minibuffer-setup-hook
136(defun rfn-eshadow-setup-minibuffer ()
476cf013 137 "Set up a minibuffer for `file-name-shadow-mode'.
519f7ff6
MB
138The prompt and initial input should already have been inserted."
139 (when minibuffer-completing-file-name
140 (setq rfn-eshadow-overlay
141 (make-overlay (minibuffer-prompt-end) (minibuffer-prompt-end)))
142 ;; Give rfn-eshadow-overlay the user's props.
143 (let ((props
144 (if window-system
476cf013
MB
145 file-name-shadow-properties
146 file-name-shadow-tty-properties)))
519f7ff6
MB
147 (while props
148 (overlay-put rfn-eshadow-overlay (pop props) (pop props))))
149 ;; Turn on overlay evaporation so that we don't have to worry about
150 ;; odd effects when the overlay sits empty at the beginning of the
151 ;; minibuffer.
152 (overlay-put rfn-eshadow-overlay 'evaporate t)
153 ;; Add our post-command hook, and make sure can remove it later.
154 (add-to-list 'rfn-eshadow-frobbed-minibufs (current-buffer))
155 (add-hook 'post-command-hook #'rfn-eshadow-update-overlay nil t)))
156
2fe3d6ec
SM
157(defsubst rfn-eshadow-sifn-equal (goal pos)
158 (equal goal (condition-case nil
159 (substitute-in-file-name
160 (buffer-substring-no-properties pos (point-max)))
161 ;; `substitute-in-file-name' can fail on partial input.
162 (error nil))))
163
519f7ff6
MB
164;; post-command-hook to update overlay
165(defun rfn-eshadow-update-overlay ()
166 "Update `rfn-eshadow-overlay' to cover shadowed part of minibuffer input.
2fe3d6ec 167This is intended to be used as a minibuffer `post-command-hook' for
476cf013 168`file-name-shadow-mode'; the minibuffer should have already
519f7ff6 169been set up by `rfn-eshadow-setup-minibuffer'."
2fe3d6ec
SM
170 (condition-case nil
171 (let ((goal (substitute-in-file-name (minibuffer-contents)))
172 (mid (overlay-end rfn-eshadow-overlay))
173 (start (minibuffer-prompt-end))
174 (end (point-max)))
175 (unless
176 ;; Catch the common case where the shadow does not need to move.
177 (and mid
178 (or (eq mid end)
179 (not (rfn-eshadow-sifn-equal goal (1+ mid))))
180 (or (eq mid start)
181 (rfn-eshadow-sifn-equal goal mid)))
182 ;; Binary search for the greatest position still equivalent to
183 ;; the whole.
184 (while (or (< (1+ start) end)
185 (if (and (< (1+ end) (point-max))
186 (rfn-eshadow-sifn-equal goal (1+ end)))
187 ;; (SIFN end) != goal, but (SIFN (1+end)) == goal,
188 ;; We've reached a discontinuity: this can happen
189 ;; e.g. if `end' point to "/:...".
190 (setq start (1+ end) end (point-max))))
191 (setq mid (/ (+ start end) 2))
192 (if (rfn-eshadow-sifn-equal goal mid)
193 (setq start mid)
194 (setq end mid)))
195 (move-overlay rfn-eshadow-overlay (minibuffer-prompt-end) start)))
196 ;; `substitute-in-file-name' can fail on partial input.
197 (error nil)))
519f7ff6 198\f
519f7ff6 199;;;###autoload
476cf013 200(define-minor-mode file-name-shadow-mode
1ced831d
MB
201 "Toggle File-Name Shadow mode.
202When active, any part of a filename being read in the minibuffer
203that would be ignored (because the result is passed through
204`substitute-in-file-name') is given the properties in
476cf013 205`file-name-shadow-properties', which can be used to make
b2c8e6ab 206that portion dim, invisible, or otherwise less visually noticeable.
519f7ff6
MB
207
208With prefix argument ARG, turn on if positive, otherwise off.
209Returns non-nil if the new state is enabled."
210 :global t
da80e0da 211 :init-value t
519f7ff6 212 :group 'minibuffer
476cf013 213 (if file-name-shadow-mode
519f7ff6
MB
214 ;; Enable the mode
215 (add-hook 'minibuffer-setup-hook 'rfn-eshadow-setup-minibuffer)
216 ;; Disable the mode
217 (remove-hook 'minibuffer-setup-hook 'rfn-eshadow-setup-minibuffer)
218 ;; Remove our entry from any post-command-hook variable's it's still in
219 (dolist (minibuf rfn-eshadow-frobbed-minibufs)
220 (with-current-buffer minibuf
221 (remove-hook 'post-command-hook #'rfn-eshadow-update-overlay t)))
222 (setq rfn-eshadow-frobbed-minibufs nil)))
223
224
225(provide 'rfn-eshadow)
226
2fe3d6ec 227;; arch-tag: dcf70a52-0115-4ec2-b1e3-4f8d3541a888
519f7ff6 228;;; rfn-eshadow.el ends here