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.24 - (show annotations) (download)
Fri Feb 13 11:35:12 2004 UTC (20 years, 2 months ago) by jet
Branch: MAIN
Changes since 1.23: +2 -2 lines
2004-02-13  Masatake YAMATO  <jet@gyve.org>

	* kogiku.el (kogiku-read-file-name-internal): delete-if
	の引数が一つだったtypoを修正。

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-enable-once nil
77 "*If non-nil, kogiku effects a one time when call with a prefix argument.
78 If nil, kogiku toggle with a prefix argument."
79 :group 'kogiku
80 :type 'boolean)
81
82 (defcustom kogiku-mode-change-key "\M-k"
83 "*Key of change `kogiku-enable-once'."
84 :group 'kogiku
85 :type 'sexp)
86
87 (defcustom kogiku-take-over-targets '(read-file-name-internal
88 ffap-read-file-or-url-internal)
89 "*絨???????c????`minibuffer-completion-table'??┏蕁???
90 minibuffer???九勝??腱祉?c????????/span>`minibuffer-completion-table'???ゃ??
91 `kogiku-take-over-targets'???膣??с?????違??絨????????茖?絎????????茵???????"
92 :group 'kogiku
93 :type '(repeat symbol))
94
95 (defcustom kogiku-minibuffer-prompt-string "kogiku"
96 "*Kogiku indicator in minibuffer."
97 :group 'kogiku
98 :type 'string)
99
100 (defcustom kogiku-minibuffer-indicator-strings '("ON" "off" "ONESHOT")
101 "*Indicators in minibuffer prpmpt."
102 :group 'kogiku
103 :type '(list (string :tag "Toggle-mode ON")
104 (string :tag "Toggle-mode off")
105 (string :tag "Oneshot-mode")))
106
107 (defface kogiku-indicator-face
108 '((((class color) (type tty)) (:foreground "blue" :bold t))
109 (((class color) (background light)) (:foreground "dark blue" :bold t))
110 (((class color) (background dark)) (:foreground "cyan" :bold t))
111 (t (:bold t)))
112 "*Face of kogiku indicator."
113 :group 'kogiku)
114
115 (defvar kogiku-original-functions nil)
116 (defvar kogiku-original-completion-tables nil)
117
118 (defvar kogiku-completion-key "\t")
119 (defvar kogiku-minibuffer-prompt-overlay nil)
120 (defvar kogiku-mode-change-original-functions nil)
121
122 (defvar kogiku-emacs21-p (fboundp 'field-beginning))
123
124 (defun kogiku-complete (&optional arg)
125 (interactive "P")
126 (if kogiku-enable-once
127 (kogiku-oneshot-complete arg)
128 (kogiku-complete-with-toggle arg)))
129
130 (defun kogiku-oneshot-complete (fire)
131 (let ((minibuffer-completion-table minibuffer-completion-table))
132 (when fire
133 (setq minibuffer-completion-table 'kogiku-read-file-name-internal))
134 (prog1
135 (funcall (car kogiku-original-functions))
136 (kogiku-minibuffer-prompt))))
137
138 (defun kogiku-complete-with-toggle (switch)
139 (cond
140 ((and switch
141 (eq minibuffer-completion-table 'kogiku-read-file-name-internal))
142 (setq minibuffer-completion-table (car kogiku-original-completion-tables))
143 (kogiku-minibuffer-prompt))
144 (switch
145 (setq minibuffer-completion-table 'kogiku-read-file-name-internal)
146 (kogiku-minibuffer-prompt)))
147 (prog1
148 (funcall (car kogiku-original-functions))
149 (kogiku-minibuffer-prompt)))
150
151 (defvar kogiku-minibuffer-prompt-map nil
152 "kogiku prompt map for mode change.")
153
154 (let ((map (make-sparse-keymap)))
155 (define-key map [mouse-2] 'kogiku-mode-change-at-mouse)
156 (setq kogiku-minibuffer-prompt-map map))
157
158 (defun kogiku-minibuffer-prompt ()
159 (let* ((mode (cond
160 (kogiku-enable-once
161 (nth 2 kogiku-minibuffer-indicator-strings))
162 ((eq minibuffer-completion-table 'kogiku-read-file-name-internal)
163 (nth 0 kogiku-minibuffer-indicator-strings))
164 (t
165 (nth 1 kogiku-minibuffer-indicator-strings))))
166 (indicator
167 (if kogiku-emacs21-p
168 (format "[%s:%s] " kogiku-minibuffer-prompt-string mode)
169 (format "%s(%s): " kogiku-minibuffer-prompt-string mode)))
170 (max (if kogiku-emacs21-p (1+ (point-min)) (point-max))))
171 (when kogiku-emacs21-p
172 (add-text-properties 0 (1- (length indicator))
173 '(face kogiku-indicator-face)
174 indicator)
175 (add-text-properties 0 (length indicator)
176 `(local-map ,kogiku-minibuffer-prompt-map)
177 indicator))
178 (if kogiku-minibuffer-prompt-overlay
179 (move-overlay kogiku-minibuffer-prompt-overlay
180 (point-min) max)
181 (setq kogiku-minibuffer-prompt-overlay
182 (make-overlay (point-min) max)))
183 (overlay-put kogiku-minibuffer-prompt-overlay
184 'before-string indicator)
185 (overlay-put kogiku-minibuffer-prompt-overlay 'evaporate t)
186 indicator))
187
188 (if (fboundp 'compare-strings)
189 (defalias 'kogiku-compare-strings 'compare-strings)
190 (defun kogiku-compare-strings (string1 start1 end1 string2 start2 end2)
191 "Convenience `compare-strings' for XEmacs."
192 (let* ((str1 (substring string1 start1 end1))
193 (str2 (substring string2 start2 end2))
194 (len (min (length str1) (length str2)))
195 (i 0))
196 (if (string= str1 str2)
197 t
198 (setq i (catch 'ignore
199 (while (< i len)
200 (when (not (eq (aref str1 i) (aref str2 i)))
201 (throw 'ignore i))
202 (setq i (1+ i)))
203 i))
204 (1+ i)))))
205
206 (defun kogiku-try-completion-regexp (regexp all-list)
207 (if (= (length regexp) 0)
208 ""
209 (substring
210 (car all-list)
211 0
212 (apply 'min
213 (mapcar
214 (lambda (a)
215 (apply 'min
216 (mapcar
217 (lambda (b)
218 (- (abs
219 (kogiku-compare-strings a 0 nil
220 b 0 nil))
221 1))
222 (delete a all-list))))
223 all-list)))))
224
225 (defun kogiku-migemo-get-pattern (string)
226 (let ((migemo-pattern-alist migemo-pattern-alist)
227 (migemo-white-space-regexp " *"))
228 (let ((case-fold-search nil))
229 (while (string-match "[^a-zA-Z]\\([a-z]+\\)" string)
230 (setq string
231 (replace-match (capitalize (match-string 1 string)) nil nil string 1))))
232 (migemo-get-pattern string)))
233
234 (defun kogiku-file-name-completion (string dir &optional all)
235 (let* ((expanded-string (expand-file-name string dir))
236 (files (directory-files dir))
237 (pattern
238 (if (string-match "/$" expanded-string)
239 ""
240 (concat "^\\("
241 (cond
242 ((string-match "\\cj$" string)
243 string)
244 ((string-match "^\\(\\(\\Cj*\\)?\\cj+\\)\\(\\Cj+\\)$" string)
245 (concat (match-string 1 string)
246 "\\("
247 (kogiku-migemo-get-pattern (match-string 3 string))
248 "\\)"))
249 (t
250 (kogiku-migemo-get-pattern (file-name-nondirectory string))))
251 "\\)")))
252 (candidates (delete nil
253 (mapcar
254 (lambda (f)
255 (if (string-match pattern f)
256 (if (file-directory-p (expand-file-name f dir))
257 ;; ???c????????? / ?х??????????/span>
258 (file-name-as-directory f)
259 f)))
260 files)))
261 (count (length candidates))
262 common)
263 (if all
264 candidates
265 (cond
266 ((eq count 0) nil)
267 ((eq count 1) (let ((candidate (car candidates)))
268 (if (file-directory-p candidate)
269 (file-name-as-directory candidate)
270 candidate)))
271 (t
272 (if (null (delete nil (mapcar (lambda (candidate)
273 (string-match "\\cj" candidate))
274 candidates)))
275 ;; ????????茖????ユ??????????с???????翫??????絽檎????
276 ;; `try-completion' ???若???с?障????????
277 (try-completion string (mapcar 'list candidates))
278 (setq common (kogiku-try-completion-regexp pattern candidates))
279 (if (eq (length common) 0) string common)))))))
280
281 (defun kogiku-file-name-all-completions (string dir)
282 (kogiku-file-name-completion string dir t))
283
284 (defun kogiku-double-dollars (input)
285 (let ((ret ""))
286 (while (string-match "\\$" input)
287 (setq ret (concat ret
288 (substring input 0 (match-beginning 0))
289 "$$"))
290 (setq input (substring input (match-end 0))))
291 (concat ret input)))
292
293 (defun kogiku-read-file-name-internal (string dir action)
294 (block nil
295 (unless (boundp 'read-file-name-predicate)
296 (setq read-file-name-predicate nil))
297 (let ((realdir dir)
298 (name string)
299 (orig-string)
300 (changed 0)
301 (val)
302 (specdir))
303 (if (eq 0 (length string))
304 (if (eq action 'lambda)
305 (return nil))
306 (setq orig-string string)
307 (setq string (substitute-in-file-name string))
308 (setq changed (null (string-equal string orig-string)))
309 (setq name (file-name-nondirectory string))
310 (setq val (file-name-directory string))
311 (if (not (null val))
312 (setq realdir (expand-file-name val realdir))))
313 (cond
314 ((null action)
315 (setq specdir (file-name-directory string))
316 (setq val (kogiku-file-name-completion name realdir))
317 (return (if (not (stringp val))
318 (if changed
319 (kogiku-double-dollars string)
320 val)
321 (if (not (null specdir))
322 (setq val (concat specdir val)))
323 (kogiku-double-dollars val))))
324 ((eq action t)
325 (let ((all (kogiku-file-name-all-completions name realdir)))
326 (unless (or (null read-file-name-predicate)
327 (eq read-file-name-predicate 'file-exists-p))
328 (delete-if (lambda (x)
329 (not (funcall read-file-name-predicate x)))
330 all))
331 (return all)))
332 ((eq action 'lambda)
333 (return (if read-file-name-predicate
334 (funcall read-file-name-predicate string)
335 (file-exists-p string))))))))
336
337 (defun kogiku-mode-change-at-mouse (event)
338 (interactive "e")
339 (save-window-excursion
340 (save-excursion
341 (set-buffer (window-buffer (posn-window (event-end event))))
342 (kogiku-mode-change))))
343
344 (defun kogiku-mode-change ()
345 (interactive)
346 ;; Cyclic: on-off, off->oneshot, oneshot->on
347 (let ((nextmode (cond
348 (kogiku-enable-once 'on)
349 ((eq minibuffer-completion-table
350 'kogiku-read-file-name-internal) 'off)
351 (t 'oneshot))))
352 (cond
353 ((eq nextmode 'on)
354 (setq kogiku-enable-once nil)
355 (setq minibuffer-completion-table 'kogiku-read-file-name-internal))
356 ((eq nextmode 'off)
357 (setq kogiku-enable-once nil)
358 (setq minibuffer-completion-table (car kogiku-original-completion-tables)))
359 (t ;; oneshot
360 (setq kogiku-enable-once t)
361 (setq minibuffer-completion-table (car kogiku-original-completion-tables))))
362 (kogiku-minibuffer-prompt)))
363
364 (defun kogiku-install-key ()
365 (when (memq minibuffer-completion-table kogiku-take-over-targets)
366 (push minibuffer-completion-table kogiku-original-completion-tables)
367 (push (lookup-key (current-local-map) kogiku-completion-key)
368 kogiku-original-functions)
369 (define-key (current-local-map) kogiku-completion-key 'kogiku-complete)
370 (push (lookup-key (current-local-map) kogiku-mode-change-key)
371 kogiku-mode-change-original-functions)
372 (define-key (current-local-map) kogiku-mode-change-key 'kogiku-mode-change)
373 (kogiku-minibuffer-prompt)))
374
375 (add-hook 'minibuffer-setup-hook 'kogiku-install-key)
376
377 (defun kogiku-uninstall-key ()
378 (when (and (or (eq minibuffer-completion-table 'kogiku-read-file-name-internal)
379 (memq minibuffer-completion-table kogiku-take-over-targets))
380 (eq (lookup-key (current-local-map) kogiku-completion-key)
381 'kogiku-complete))
382 (pop kogiku-original-completion-tables)
383 (define-key (current-local-map) kogiku-completion-key (pop kogiku-original-functions))
384 (when (eq (lookup-key (current-local-map) kogiku-mode-change-key)
385 'kogiku-mode-change)
386 (define-key (current-local-map) kogiku-mode-change-key
387 (pop kogiku-mode-change-original-functions)))))
388
389 (add-hook 'minibuffer-exit-hook 'kogiku-uninstall-key)
390
391 (provide 'kogiku)
392 ;; kogiku.el ends here

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