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.26 - (show annotations) (download)
Fri Feb 13 13:29:12 2004 UTC (20 years, 2 months ago) by shirai
Branch: MAIN
Changes since 1.25: +16 -8 lines
(kogiku-install-key): kogiku が二重に実行されたときの
ガード。

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

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