Develop and Download Open Source Software

Browse CVS Repository

Contents of /kogiku/kogiku/kogiku.el

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.14 - (show annotations) (download)
Tue Feb 10 07:30:55 2004 UTC (20 years, 2 months ago) by jet
Branch: MAIN
Changes since 1.13: +23 -26 lines
2004-02-10  Masatake YAMATO  <jet@gyve.org>

	再帰的にminibufferに入った場合をサポート。
	* kogiku.el (kogiku-original-function):削除。
	(kogiku-original-functions): 新しい変数。
	(kogiku-install-key, kogiku-uninstall-key):
	minibuffer-completion-tableに設定されていた変数をリストで保存。

	minibuffer-completion-tableがread-file-name-internal以外の
	場合をサポート。
	(kogiku-original-completion-table): 新しい変数。
	(kogiku-take-over-targets): 新しい変数。

1 ;;; kogiku.el - reading file and directory names with migemo service
2
3 ;; Copyright (C) 2004 Masatake YAMATO
4
5 ;; Author: Masatake YAMATO <jet@gyve.org>
6
7 ;; This file is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 2, or (at your option)
10 ;; any later version.
11
12 ;; This file is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
16
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to
19 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 ;; Boston, MA 02111-1307, USA.
21
22 ;;; Commentary:
23 ;; ?ユ??????<?ゃ???????ュ????migemo??篏帥?c??亜?????????????違?????с????
24
25 ;; (篁???戎?c???????????с??????)篁ュ??windows??eadow??篏帥?c??????????????
26 ;; ?????鴻????????(絎?????????茹?)?????吟?<?障???????????<?ゃ????find-file??
27 ;; ????????????篁???羲√?紊?????綽?荀???????)???<?ゃ????????絎?????絽吾???√????/span>
28 ;; ???????????障??????"desukutoxtupu"?????泣???????????????鴻???????????後???
29 ;; ????return]?с???c????絎????????????с???障??????
30
31 ;; ?????鴻??????????????絎??????????違??"desktop"???? "de"[tab]?????ф?????/span>
32 ;; ???違?????????????障??????鐚?migemo??篏帥???違??????絎??障???????????с??????
33 ;; ?с????"desktop"??"de"??migemo?ф?ユ????????????絮???羝??帥?????帥?若?潟??
34 ;; ?上???????c???????????????????????<?ゃ???????????у???????違?????<?ゃ????
35 ;; ????絎?("desktop"???????鴻???????????????????????茖?絎???茖???????
36 ;; ("de"???????鴻????????????????????????????????с???障????
37
38 ;; kogiku.el??igemo.el????NU Emacs 21???絖????????障????migemo.el??/span>
39 ;; ??篏????障??腆肴????????????kogiku.el?????若?????鴻?????c?????c??????
40 ;; ???c??????????舟??.emacs? (require 'kogiku)???吾????????macs????
41 ;; 莎桁?????障????migemo? http://migemo.namazu.rog ?????ユ???с???障????
42
43 ;;; Acknowledgments:
44
45 ;; Some code used in `kogiku-file-name-completion'
46 ;; and `kogiku-try-completion-regexp' are copied from
47 ;; http://www.bookshelf.jp/cgi-bin/goto.cgi?file=meadow&node=kogiku
48 ;; (MeadowMemo maintained by Akihisa Matsushita <akihisa@mail.ne.jp>).
49
50 ;; `kogiku-read-file-name-internal' are
51 ;; translated from emacs/src/fileio.c::read-file-name-internal
52 ;; in elisp.
53
54 ;;; Version:
55 ;; 2.0.2.
56
57 ;;; History:
58 ;; 2.0.2
59 ;; -- inlucde shirai's patch
60 ;; -- introduce custom
61 ;;
62 ;; 2.0.1 -- update `Acknowledgments'.
63 ;;
64 ;; 2.0 -- rewrite.
65
66 ;;; Code:
67
68 (eval-when-compile
69 (require 'cl)
70 (require 'migemo))
71
72 (defgroup kogiku nil
73 "reading file and directory name with migemo service"
74 :group 'convenience)
75
76 (defcustom kogiku-minibuffer-prompt-string "kogiku"
77 "*Kogiku indicator in minibuffer."
78 :group 'kogiku
79 :type 'string)
80
81 (defcustom kogiku-enable-once nil
82 "*If non-nil, kogiku effects a one time when call with a prefix argument.
83 If nil, kogiku toggle with a prefix argument."
84 :group 'kogiku
85 :type 'boolean)
86
87 (defvar kogiku-take-over-targets
88 '(read-file-name-internal
89 ffap-read-file-or-url-internal))
90
91 (defvar kogiku-original-functions nil)
92 (defvar kogiku-original-completion-table nil)
93
94 (defvar kogiku-completion-key "\t")
95 (defvar kogiku-minibuffer-prompt-overlay nil)
96
97 (defun kogiku-complete (&optional arg)
98 (interactive "P")
99 (if kogiku-enable-once
100 (kogiku-oneshot-complete arg)
101 (kogiku-complete-with-toggle arg)))
102
103 (defun kogiku-oneshot-complete (fire)
104 (let ((minibuffer-completion-table minibuffer-completion-table))
105 (when fire
106 (setq minibuffer-completion-table 'kogiku-read-file-name-internal))
107 (funcall (car kogiku-original-functions))))
108
109 (defun kogiku-complete-with-toggle (switch)
110 (when switch
111 (cond
112 ((eq minibuffer-completion-table 'kogiku-read-file-name-internal)
113 (setq minibuffer-completion-table kogiku-original-completion-table))
114 (t
115 (setq minibuffer-completion-table 'kogiku-read-file-name-internal)))
116 (kogiku-minibuffer-prompt-e21))
117 (funcall (car kogiku-original-functions))
118 (kogiku-minibuffer-prompt-not-e21))
119
120 (defun kogiku-minibuffer-prompt-e21 ()
121 (when (fboundp 'field-beginning)
122 (let ((prompt (buffer-substring (point-min) (field-beginning)))
123 (buffer-read-only nil)
124 (inhibit-read-only t)
125 (props (text-properties-at (point-min)))
126 (indicator (format " [%s]" kogiku-minibuffer-prompt-string)))
127 (save-excursion
128 (if (eq minibuffer-completion-table 'kogiku-read-file-name-internal)
129 (when (string-match ":[^:]*" prompt)
130 (setq prompt (concat (substring prompt 0 (match-beginning 0))
131 indicator
132 (substring prompt (match-beginning 0))))
133 (set-text-properties 0 (length prompt) props prompt)
134 (delete-region (point-min) (field-beginning))
135 (goto-char (point-min))
136 (insert prompt))
137 (when (string-match (regexp-quote indicator) prompt)
138 (setq prompt (replace-match "" nil nil prompt))
139 (set-text-properties 0 (length prompt) props prompt)
140 (delete-region (point-min) (field-beginning))
141 (goto-char (point-min))
142 (insert prompt)))))))
143
144 (defun kogiku-minibuffer-prompt-not-e21 ()
145 (unless (fboundp 'field-beginning)
146 (if (eq minibuffer-completion-table 'kogiku-read-file-name-internal)
147 (let ((max (min (point-max) (1+ (point-min)))))
148 (if kogiku-minibuffer-prompt-overlay
149 (move-overlay kogiku-minibuffer-prompt-overlay
150 (point-min) max)
151 (setq kogiku-minibuffer-prompt-overlay
152 (make-overlay (point-min) max)))
153 (overlay-put kogiku-minibuffer-prompt-overlay
154 'before-string
155 (format "%s: " kogiku-minibuffer-prompt-string))
156 (overlay-put kogiku-minibuffer-prompt-overlay 'evaporate t))
157 (when kogiku-minibuffer-prompt-overlay
158 (delete-overlay kogiku-minibuffer-prompt-overlay)))))
159
160 (if (fboundp 'compare-strings)
161 (defalias 'kogiku-compare-strings 'compare-strings)
162 (defun kogiku-compare-strings (string1 start1 end1 string2 start2 end2)
163 "Convenience `compare-strings' for XEmacs."
164 (let* ((str1 (substring string1 start1 end1))
165 (str2 (substring string2 start2 end2))
166 (len (min (length str1) (length str2)))
167 (i 0))
168 (if (string= str1 str2)
169 t
170 (setq i (catch 'ignore
171 (while (< i len)
172 (when (not (eq (aref str1 i) (aref str2 i)))
173 (throw 'ignore i))
174 (setq i (1+ i)))
175 i))
176 (1+ i)))))
177
178 (defun kogiku-try-completion-regexp (regexp all-list)
179 (if (= (length regexp) 0)
180 ""
181 (let (common)
182 (setq common
183 (substring
184 (car all-list)
185 0
186 (apply 'min
187 (mapcar
188 (lambda (a)
189 (apply 'min
190 (mapcar
191 (lambda (b)
192 (- (abs
193 (kogiku-compare-strings a 0 nil
194 b 0 nil))
195 1))
196 (delete a all-list))))
197 all-list))))
198 common)))
199
200 (defun kogiku-migemo-get-pattern (string)
201 (let ((migemo-pattern-alist migemo-pattern-alist)
202 (migemo-white-space-regexp " *"))
203 (let ((case-fold-search nil))
204 (while (string-match "[^a-zA-Z]\\([a-z]+\\)" string)
205 (setq string
206 (replace-match (capitalize (match-string 1 string)) nil nil string 1))))
207 (migemo-get-pattern string)))
208
209 (defun kogiku-file-name-completion (string dir &optional all)
210 (let* ((expanded-string (expand-file-name string dir))
211 (files (directory-files dir))
212 (pattern
213 (if (string-match "/$" expanded-string)
214 ""
215 (concat "^\\("
216 (cond
217 ((string-match "\\cj$" string)
218 string)
219 ((string-match "^\\(\\(\\Cj*\\)?\\cj+\\)\\(\\Cj+\\)$" string)
220 (concat (match-string 1 string)
221 "\\("
222 (kogiku-migemo-get-pattern (match-string 3 string))
223 "\\)"))
224 (t
225 (kogiku-migemo-get-pattern (file-name-nondirectory string))))
226 "\\)")))
227 (candidates (delete nil
228 (mapcar
229 (lambda (f)
230 (if (string-match pattern f)
231 (if (file-directory-p (expand-file-name f dir))
232 ;; ???c????????? / ?х??????????/span>
233 (file-name-as-directory f)
234 f)))
235 files)))
236 (count (length candidates))
237 common)
238 (if all
239 candidates
240 (cond
241 ((eq count 0) nil)
242 ((eq count 1) (let ((candidate (car candidates)))
243 (if (file-directory-p candidate)
244 (file-name-as-directory candidate)
245 candidate)))
246 (t
247 (if (null (delete nil (mapcar (lambda (candidate)
248 (string-match "\\cj" candidate))
249 candidates)))
250 ;; ????????茖????ユ??????????с???????翫??????絽檎????
251 ;; `try-completion' ???若???с?障????????
252 (try-completion string (mapcar 'list candidates))
253 (setq common (kogiku-try-completion-regexp pattern candidates))
254 (if (eq (length common) 0) string common)))))))
255
256 (defun kogiku-file-name-all-completions (string dir)
257 (kogiku-file-name-completion string dir t))
258
259 (defun kogiku-double-dollars (input)
260 (let ((ret ""))
261 (while (string-match "\\$" input)
262 (setq ret (concat ret
263 (substring input 0 (match-beginning 0))
264 "$$"))
265 (setq input (substring input (match-end 0))))
266 (concat ret input)))
267
268 (defun kogiku-read-file-name-internal (string dir action)
269 (block nil
270 (unless (boundp 'read-file-name-predicate)
271 (setq read-file-name-predicate nil))
272 (let ((realdir dir)
273 (name string)
274 (orig-string)
275 (changed 0)
276 (val)
277 (specdir))
278 (if (eq 0 (length string))
279 (if (eq action 'lambda)
280 (return nil))
281 (setq orig-string string)
282 (setq string (substitute-in-file-name string))
283 (setq changed (null (string-equal string orig-string)))
284 (setq name (file-name-nondirectory string))
285 (setq val (file-name-directory string))
286 (if (not (null val))
287 (setq realdir (expand-file-name val realdir))))
288 (cond
289 ((null action)
290 (setq specdir (file-name-directory string))
291 (setq val (kogiku-file-name-completion name realdir))
292 (return (if (not (stringp val))
293 (if changed
294 (kogiku-double-dollars string)
295 val)
296 (if (not (null specdir))
297 (setq val (concat specdir val)))
298 (kogiku-double-dollars val))))
299 ((eq action t)
300 (let ((all (kogiku-file-name-all-completions name realdir)))
301 (unless (or (null read-file-name-predicate)
302 (eq read-file-name-predicate 'file-exists-p))
303 (delete-if (lambda (x)
304 (not (funcall read-file-name-predicate x))
305 all)))
306 (return all)))
307 ((eq action 'lambda)
308 (return (if read-file-name-predicate
309 (funcall read-file-name-predicate string)
310 (file-exists-p string))))))))
311
312 (defun kogiku-install-key ()
313 (when (member minibuffer-completion-table kogiku-take-over-targets)
314 (setq kogiku-original-completion-table minibuffer-completion-table)
315 (push (lookup-key (current-local-map) kogiku-completion-key)
316 kogiku-original-functions)
317 (define-key (current-local-map) kogiku-completion-key 'kogiku-complete)))
318
319 (add-hook 'minibuffer-setup-hook 'kogiku-install-key)
320
321 (defun kogiku-uninstall-key ()
322 (when (and (or (eq minibuffer-completion-table 'kogiku-read-file-name-internal)
323 (memq minibuffer-completion-table kogiku-take-over-targets))
324 (eq (lookup-key (current-local-map) kogiku-completion-key)
325 'kogiku-complete))
326 (define-key (current-local-map) kogiku-completion-key (pop kogiku-original-functions))))
327 (add-hook 'minibuffer-exit-hook 'kogiku-uninstall-key)
328
329 (provide 'kogiku)
330 ;; kogiku.el ends here

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26