(gnus-score-customize): Don't use `format' on `error' arguments.
[bpt/emacs.git] / lisp / rfn-eshadow.el
CommitLineData
519f7ff6
MB
1;;; rfn-eshadow.el --- Highlight `shadowed' part of read-file-name input text
2;;
17230c90 3;; Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
519f7ff6
MB
4;;
5;; Author: Miles Bader <miles@gnu.org>
476cf013 6;; Keywords: convenience minibuffer
519f7ff6
MB
7
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software; you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
12;; the Free Software Foundation; either version 2, or (at your option)
13;; any later version.
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
21;; along with GNU Emacs; see the file COPYING. If not, write to the
22;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23;; Boston, MA 02111-1307, USA.
24
25;;; Commentary:
26;;
476cf013 27;; Defines the mode `file-name-shadow-mode'.
519f7ff6
MB
28;;
29;; The `read-file-name' function passes its result through
30;; `substitute-in-file-name', so any part of the string preceding
31;; multiple slashes (or a drive indicator on MS-DOS/MS-Windows) is
32;; ignored.
33;;
476cf013 34;; If `file-name-shadow-mode' is active, any part of the
519f7ff6 35;; minibuffer text that would be ignored because of this is given the
476cf013 36;; properties in `file-name-shadow-properties', which may
519f7ff6
MB
37;; be used to make the ignored text invisible, dim, etc.
38;;
39
40;;; Code:
41
42\f
43;;; Customization
44
476cf013 45(defconst file-name-shadow-properties-custom-type
519f7ff6
MB
46 '(list
47 (checklist :inline t
48 (const :tag "Invisible"
49 :doc "Make shadowed part of filename invisible"
50 :format "%t%n%h"
51 :inline t
52 (invisible t intangible t))
53 (list :inline t
54 :format "%v"
55 :tag "Face"
56 :doc "Display shadowed part of filename using a different face"
57 (const :format "" face)
476cf013 58 (face :value file-name-shadow))
519f7ff6
MB
59 (list :inline t
60 :format "%t: %v%h"
61 :tag "Brackets"
62 ;; Note the 4 leading spaces in the doc string;
63 ;; this is hack to get around the fact that the
64 ;; newline after the second string widget comes
65 ;; from the string widget, and doesn't indent
66 ;; correctly. We could use a :size attribute to
67 ;; make the second string widget not have a
68 ;; terminating newline, but this makes it impossible
69 ;; to enter trailing whitespace, and it's desirable
70 ;; that it be possible.
71 :doc " Surround shadowed part of filename with brackets"
72 (const :format "" before-string)
73 (string :format "%v" :size 4 :value "{")
74 (const :format "" after-string)
75 ;; see above about why the 2nd string doesn't use :size
76 (string :format " and: %v" :value "} "))
77 (list :inline t
78 :format "%t: %v%n%h"
79 :tag "String"
80 :doc "Display a string instead of the shadowed part of filename"
81 (const :format "" display)
82 (string :format "%v" :size 15 :value "<...ignored...>"))
83 (const :tag "Avoid"
84 :doc "Try to keep cursor out of shadowed part of filename"
85 :format "%t%n%h"
86 :inline t
87 (field shadow)))
88 (repeat :inline t
89 :tag "Other Properties"
90 (list :inline t
91 :format "%v"
92 (symbol :tag "Property")
93 (sexp :tag "Value")))))
94
18a90ec7 95;;;###autoload
476cf013
MB
96(defcustom file-name-shadow-properties
97 '(face file-name-shadow field shadow)
519f7ff6 98 "Properties given to the `shadowed' part of a filename in the minibuffer.
476cf013 99Only used when `file-name-shadow-mode' is active.
519f7ff6 100If emacs is not running under a window system,
476cf013
MB
101`file-name-shadow-tty-properties' is used instead."
102 :type file-name-shadow-properties-custom-type
519f7ff6
MB
103 :group 'minibuffer)
104
18a90ec7 105;;;###autoload
476cf013 106(defcustom file-name-shadow-tty-properties
519f7ff6
MB
107 '(before-string "{" after-string "} " field shadow)
108 "Properties given to the `shadowed' part of a filename in the minibuffer.
476cf013 109Only used when `file-name-shadow-mode' is active and emacs
519f7ff6 110is not running under a window-system; if emacs is running under a window
476cf013
MB
111system, `file-name-shadow-properties' is used instead."
112 :type file-name-shadow-properties-custom-type
519f7ff6
MB
113 :group 'minibuffer)
114
476cf013 115(defface file-name-shadow
c3423c97 116 '((t :inherit shadow))
476cf013 117 "Face used by `file-name-shadow-mode' for the shadow."
519f7ff6
MB
118 :group 'minibuffer)
119
120\f
121;;; Internal variables
122
123;; Regexp to locate dividing point between shadow and real pathname
124(defconst rfn-eshadow-regexp
125 (cond ((memq system-type '(ms-dos windows-nt))
126 ;; This horrible regexp considers the following patterns as
127 ;; starting an absolute pathname, when following a `/' or an `\':
128 ;; L: / // ~ $ \\ \\\\
f4be0a12 129 "\\(.*[^/]+/+?\\|/*?\\|\\)\\(~\\|$[^$]\\|$\\'\\|[][\\^a-z]:\\|//?\\([^][\\^a-z/$~]\\|[^/$~][^:]\\|[^/$~]?\\'\\)\\)")
519f7ff6
MB
130 (t
131 ;; default is for unix-style filenames
f4be0a12 132 "\\(.*/\\)\\([/~]\\|$[^$]\\|$\\'\\)"))
519f7ff6
MB
133 "Regular expression used to match shadowed filenames.
134There should be at least one regexp group; the end of the first one
135is used as the end of the shadowed portion of the filename.")
136
137;; A list of minibuffers to which we've added a post-command-hook.
138(defvar rfn-eshadow-frobbed-minibufs nil)
139
140;; An overlay covering the shadowed part of the filename (local to the
141;; minibuffer).
142(defvar rfn-eshadow-overlay)
143(make-variable-buffer-local 'rfn-eshadow-overlay)
144
145\f
146;;; Hook functions
147
148;; This function goes on minibuffer-setup-hook
149(defun rfn-eshadow-setup-minibuffer ()
476cf013 150 "Set up a minibuffer for `file-name-shadow-mode'.
519f7ff6
MB
151The prompt and initial input should already have been inserted."
152 (when minibuffer-completing-file-name
153 (setq rfn-eshadow-overlay
154 (make-overlay (minibuffer-prompt-end) (minibuffer-prompt-end)))
155 ;; Give rfn-eshadow-overlay the user's props.
156 (let ((props
157 (if window-system
476cf013
MB
158 file-name-shadow-properties
159 file-name-shadow-tty-properties)))
519f7ff6
MB
160 (while props
161 (overlay-put rfn-eshadow-overlay (pop props) (pop props))))
162 ;; Turn on overlay evaporation so that we don't have to worry about
163 ;; odd effects when the overlay sits empty at the beginning of the
164 ;; minibuffer.
165 (overlay-put rfn-eshadow-overlay 'evaporate t)
166 ;; Add our post-command hook, and make sure can remove it later.
167 (add-to-list 'rfn-eshadow-frobbed-minibufs (current-buffer))
168 (add-hook 'post-command-hook #'rfn-eshadow-update-overlay nil t)))
169
170;; post-command-hook to update overlay
171(defun rfn-eshadow-update-overlay ()
172 "Update `rfn-eshadow-overlay' to cover shadowed part of minibuffer input.
173This is intended to be used as a minibuffer post-command-hook for
476cf013 174`file-name-shadow-mode'; the minibuffer should have already
519f7ff6
MB
175been set up by `rfn-eshadow-setup-minibuffer'."
176 ;; This is not really a correct implementation; it won't always do the
177 ;; right thing in the presence of environment variables that
178 ;; substitute-in-file-name would expand; currently it just assumes any
a01b7831 179 ;; environment variable contains an absolute filename.
519f7ff6 180 (save-excursion
17230c90
MB
181 (let ((inhibit-point-motion-hooks t))
182 (goto-char (minibuffer-prompt-end))
183 ;; Update the overlay (which will evaporate if it's empty).
184 (move-overlay rfn-eshadow-overlay
185 (point)
186 (if (looking-at rfn-eshadow-regexp)
187 (match-end 1)
188 (point))))))
519f7ff6
MB
189
190\f
191;;; Note this definition must be at the end of the file, because
192;;; `define-minor-mode' actually calls the mode-function if the
193;;; associated variable is non-nil, which requires that all needed
194;;; functions be already defined. [This is arguably a bug in d-m-m]
195;;;###autoload
476cf013 196(define-minor-mode file-name-shadow-mode
1ced831d
MB
197 "Toggle File-Name Shadow mode.
198When active, any part of a filename being read in the minibuffer
199that would be ignored (because the result is passed through
200`substitute-in-file-name') is given the properties in
476cf013 201`file-name-shadow-properties', which can be used to make
b2c8e6ab 202that portion dim, invisible, or otherwise less visually noticeable.
519f7ff6
MB
203
204With prefix argument ARG, turn on if positive, otherwise off.
205Returns non-nil if the new state is enabled."
206 :global t
207 :group 'minibuffer
476cf013 208 (if file-name-shadow-mode
519f7ff6
MB
209 ;; Enable the mode
210 (add-hook 'minibuffer-setup-hook 'rfn-eshadow-setup-minibuffer)
211 ;; Disable the mode
212 (remove-hook 'minibuffer-setup-hook 'rfn-eshadow-setup-minibuffer)
213 ;; Remove our entry from any post-command-hook variable's it's still in
214 (dolist (minibuf rfn-eshadow-frobbed-minibufs)
215 (with-current-buffer minibuf
216 (remove-hook 'post-command-hook #'rfn-eshadow-update-overlay t)))
217 (setq rfn-eshadow-frobbed-minibufs nil)))
218
219
220(provide 'rfn-eshadow)
221
ab5796a9 222;;; arch-tag: dcf70a52-0115-4ec2-b1e3-4f8d3541a888
519f7ff6 223;;; rfn-eshadow.el ends here