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.27 - (show annotations) (download)
Mon Feb 16 10:17:50 2004 UTC (20 years, 1 month ago) by shirai
Branch: MAIN
Changes since 1.26: +163 -85 lines
kogiku の状態?Eoneshot(Fire) と ON の二状態とした。
kogiku を使うとう?E靴い?發靴?E覆ぞ?屬?Eindicator で教え?E茲Δ
した。とりあえず advocate と呼ぶことにした。
* kogiku.el (kogiku-enable-once): t ?Edefault にした。
(kogiku-minibuffer-indicator-strings): 要素を二つに変更。
(kogiku-use-advocate): 新しいオプション。non-nil な?Eadvocate ?E態を通知す?E
(kogiku-indicator-advocate-face): advocate 用 face。
(kogiku-minibuffer-prompt-advocate-files): 新規内部用変数。
(kogiku-migemo-pattern-alist): Ditto.
(kogiku-migemo-pattern-alist-length): Ditto.
(kogiku-complete): `kogiku-oneshot-complete' と
`kogiku-complete-with-toggle' を廃止して、?E弔砲泙箸瓩拭
(kogiku-field-beginning): Emacs 間の?E磴魑杣?垢?E愎堯
(kogiku-migemo-get-pattern): 簡易キャッシュ機能追加。
(kogiku-mode-change): 二状態の対応。
(kogiku-minibuffer-prompt): advocate 対応。minibuffer 中で
post-command-hook で動くように変更。
(kogiku-minibuffer-prompt-advocate): advocate 検出関数。
(kogiku-install-key): advocate 対応。
(kogiku-uninstall-key): Ditto.

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.org ?????ユ???с???障????
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 t
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" "Fire")
102 "*Indicators in minibuffer prpmpt."
103 :group 'kogiku
104 :type '(list (string :tag "Kogiku-mode")
105 (string :tag "Oneshot-mode")))
106
107 (defcustom kogiku-use-advocate t
108 "*Use kogiku `advocate' mode."
109 :group 'kogiku
110 :type 'boolean)
111
112 (defface kogiku-indicator-face
113 '((((class color) (type tty)) (:foreground "blue" :bold t))
114 (((class color) (background light)) (:foreground "dark blue" :bold t))
115 (((class color) (background dark)) (:foreground "cyan" :bold t))
116 (t (:bold t)))
117 "*Face of kogiku indicator."
118 :group 'kogiku)
119
120 (defface kogiku-indicator-advocate-face
121 '((((class color) (type tty)) (:foreground "red" :bold t))
122 (((class color) (background light))
123 (:foreground "firebrick" :background "pink" :bold t))
124 (((class color) (background dark))
125 (:foreground "pink" :background "firebrick" :bold t))
126 (t (:inverse-video t :bold t)))
127 "*Face of kogiku indicator."
128 :group 'kogiku)
129
130 (defvar kogiku-original-functions nil)
131 (defvar kogiku-original-completion-tables nil)
132
133 (defvar kogiku-completion-key "\t")
134 (defvar kogiku-minibuffer-prompt-overlay nil)
135 (defvar kogiku-mode-change-original-functions nil)
136
137 (defvar kogiku-emacs21-p (fboundp 'field-beginning))
138 (defvar kogiku-minibuffer-prompt-advocate-files nil)
139 (defvar kogiku-migemo-pattern-alist nil)
140 (defvar kogiku-migemo-pattern-alist-length 128)
141
142 (defvar kogiku-minibuffer-prompt-map nil
143 "kogiku prompt map for mode change.")
144
145 (let ((map (make-sparse-keymap)))
146 (define-key map [mouse-2] 'kogiku-mode-change-at-mouse)
147 (setq kogiku-minibuffer-prompt-map map))
148
149 (if kogiku-emacs21-p
150 (defalias 'kogiku-field-beginning 'field-beginning)
151 (defalias 'kogiku-field-beginning 'point-min))
152
153 (defun kogiku-complete (&optional arg)
154 (interactive "P")
155 (let ((minibuffer-completion-table minibuffer-completion-table))
156 (when (or (and kogiku-enable-once arg)
157 (not (or kogiku-enable-once arg)))
158 (setq minibuffer-completion-table 'kogiku-read-file-name-internal))
159 (funcall (car kogiku-original-functions))))
160
161 (if (fboundp 'compare-strings)
162 (defalias 'kogiku-compare-strings 'compare-strings)
163 (defun kogiku-compare-strings (string1 start1 end1 string2 start2 end2)
164 "Convenience `compare-strings' for XEmacs."
165 (let* ((str1 (substring string1 start1 end1))
166 (str2 (substring string2 start2 end2))
167 (len (min (length str1) (length str2)))
168 (i 0))
169 (if (string= str1 str2)
170 t
171 (setq i (catch 'ignore
172 (while (< i len)
173 (when (not (eq (aref str1 i) (aref str2 i)))
174 (throw 'ignore i))
175 (setq i (1+ i)))
176 i))
177 (1+ i)))))
178
179 (defun kogiku-try-completion-regexp (regexp all-list)
180 (if (= (length regexp) 0)
181 ""
182 (substring
183 (car all-list)
184 0
185 (apply 'min
186 (mapcar
187 (lambda (a)
188 (apply 'min
189 (mapcar
190 (lambda (b)
191 (- (abs
192 (kogiku-compare-strings a 0 nil
193 b 0 nil))
194 1))
195 (delete a all-list))))
196 all-list)))))
197
198 (defun kogiku-migemo-get-pattern (string)
199 (let ((migemo-pattern-alist migemo-pattern-alist)
200 (migemo-white-space-regexp " *")
201 pattern)
202 (let ((case-fold-search nil))
203 (while (string-match "[^a-zA-Z]\\([a-z]+\\)" string)
204 (setq string
205 (replace-match (capitalize (match-string 1 string)) nil nil string 1))))
206 (if (setq pattern (assoc string kogiku-migemo-pattern-alist))
207 (prog1
208 (cdr pattern)
209 (setq kogiku-migemo-pattern-alist
210 (cons pattern
211 (delete pattern kogiku-migemo-pattern-alist))))
212 (prog1
213 (setq pattern (migemo-get-pattern string))
214 (setq kogiku-migemo-pattern-alist
215 (cons (cons string pattern) kogiku-migemo-pattern-alist))
216 (when (> (length kogiku-migemo-pattern-alist)
217 kogiku-migemo-pattern-alist-length)
218 (setcdr
219 (nthcdr (1- kogiku-migemo-pattern-alist-length) kogiku-migemo-pattern-alist)
220 nil))))))
221
222 (defun kogiku-file-name-completion (string dir &optional all)
223 (let* ((expanded-string (expand-file-name string dir))
224 (files (directory-files dir))
225 (pattern
226 (if (string-match "/$" expanded-string)
227 ""
228 (concat "^\\("
229 (cond
230 ((string-match "\\cj$" string)
231 string)
232 ((string-match "^\\(\\(\\Cj*\\)?\\cj+\\)\\(\\Cj+\\)$" string)
233 (concat (match-string 1 string)
234 "\\("
235 (kogiku-migemo-get-pattern (match-string 3 string))
236 "\\)"))
237 (t
238 (kogiku-migemo-get-pattern (file-name-nondirectory string))))
239 "\\)")))
240 (candidates (delete nil
241 (mapcar
242 (lambda (f)
243 (if (string-match pattern f)
244 (if (file-directory-p (expand-file-name f dir))
245 ;; ???c????????? / ?х??????????/span>
246 (file-name-as-directory f)
247 f)))
248 files)))
249 (count (length candidates))
250 common)
251 (if all
252 candidates
253 (cond
254 ((eq count 0) nil)
255 ((eq count 1) (let ((candidate (car candidates)))
256 (if (file-directory-p candidate)
257 (file-name-as-directory candidate)
258 candidate)))
259 (t
260 (if (null (delete nil (mapcar (lambda (candidate)
261 (string-match "\\cj" candidate))
262 candidates)))
263 ;; ????????茖????ユ??????????с???????翫??????絽檎????
264 ;; `try-completion' ???若???с?障????????
265 (try-completion string (mapcar 'list candidates))
266 (setq common (kogiku-try-completion-regexp pattern candidates))
267 (if (eq (length common) 0) string common)))))))
268
269 (defun kogiku-file-name-all-completions (string dir)
270 (kogiku-file-name-completion string dir t))
271
272 (defun kogiku-double-dollars (input)
273 (let ((ret ""))
274 (while (string-match "\\$" input)
275 (setq ret (concat ret
276 (substring input 0 (match-beginning 0))
277 "$$"))
278 (setq input (substring input (match-end 0))))
279 (concat ret input)))
280
281 (defun kogiku-read-file-name-internal (string dir action)
282 (block nil
283 (unless (boundp 'read-file-name-predicate)
284 (setq read-file-name-predicate nil))
285 (let ((realdir dir)
286 (name string)
287 (orig-string)
288 (changed 0)
289 (val)
290 (specdir))
291 (if (eq 0 (length string))
292 (if (eq action 'lambda)
293 (return nil))
294 (setq orig-string string)
295 (setq string (substitute-in-file-name string))
296 (setq changed (null (string-equal string orig-string)))
297 (setq name (file-name-nondirectory string))
298 (setq val (file-name-directory string))
299 (if (not (null val))
300 (setq realdir (expand-file-name val realdir))))
301 (cond
302 ((null action)
303 (setq specdir (file-name-directory string))
304 (setq val (kogiku-file-name-completion name realdir))
305 (return (if (not (stringp val))
306 (if changed
307 (kogiku-double-dollars string)
308 val)
309 (if (not (null specdir))
310 (setq val (concat specdir val)))
311 (kogiku-double-dollars val))))
312 ((eq action t)
313 (let ((all (kogiku-file-name-all-completions name realdir)))
314 (unless (or (null read-file-name-predicate)
315 (eq read-file-name-predicate 'file-exists-p))
316 (delete-if (lambda (x)
317 (not (funcall read-file-name-predicate x)))
318 all))
319 (return all)))
320 ((eq action 'lambda)
321 (return (if read-file-name-predicate
322 (funcall read-file-name-predicate string)
323 (file-exists-p string))))))))
324
325 (defun kogiku-mode-change-at-mouse (event)
326 (interactive "e")
327 (save-window-excursion
328 (save-excursion
329 (set-buffer (window-buffer (posn-window (event-end event))))
330 (kogiku-mode-change))))
331
332 (defun kogiku-mode-change ()
333 (interactive)
334 (setq kogiku-enable-once (not kogiku-enable-once)))
335
336 (defun kogiku-minibuffer-prompt ()
337 (when (and (window-minibuffer-p (selected-window))
338 (not (input-pending-p)))
339 (let* ((advocate (and kogiku-use-advocate
340 (kogiku-minibuffer-prompt-advocate)))
341 (mode (if kogiku-enable-once
342 (nth 1 kogiku-minibuffer-indicator-strings)
343 (nth 0 kogiku-minibuffer-indicator-strings)))
344 (indicator
345 (cond
346 ((and kogiku-emacs21-p advocate)
347 (format "<%s:%s> " kogiku-minibuffer-prompt-string mode))
348 (kogiku-emacs21-p
349 (format "[%s:%s] " kogiku-minibuffer-prompt-string mode))
350 (advocate
351 (format "%s<%s>: " kogiku-minibuffer-prompt-string mode))
352 (t
353 (format "%s(%s): " kogiku-minibuffer-prompt-string mode))))
354 (max (if kogiku-emacs21-p (1+ (point-min)) (point-max)))
355 (force (and (not kogiku-emacs21-p) (eq (point-min) max))))
356 (when force
357 (insert " ")
358 (goto-char (point-min))
359 (setq max (point-max)))
360 (when kogiku-emacs21-p
361 (add-text-properties 0 (1- (length indicator))
362 `(face ,(if advocate
363 'kogiku-indicator-advocate-face
364 'kogiku-indicator-face))
365 indicator)
366 (add-text-properties 0 (length indicator)
367 `(local-map
368 ,kogiku-minibuffer-prompt-map
369 mouse-face
370 highlight)
371 indicator))
372 (if kogiku-minibuffer-prompt-overlay
373 (move-overlay kogiku-minibuffer-prompt-overlay
374 (point-min) max)
375 (setq kogiku-minibuffer-prompt-overlay
376 (make-overlay (point-min) max)))
377 (overlay-put kogiku-minibuffer-prompt-overlay
378 'before-string indicator)
379 (overlay-put kogiku-minibuffer-prompt-overlay 'evaporate t)
380 (when force
381 (let ((inhibit-quit t))
382 (sit-for 60)
383 (delete-region (point-min) (point-max))))
384 indicator)))
385
386 (defun kogiku-minibuffer-prompt-advocate ()
387 (when (and (window-minibuffer-p (selected-window))
388 (not (input-pending-p)))
389 (let* ((full (buffer-substring-no-properties
390 (kogiku-field-beginning) (point-max)))
391 (dir (or (file-name-directory full) default-directory))
392 (file (file-name-nondirectory full))
393 (files (cdr (assoc dir kogiku-minibuffer-prompt-advocate-files)))
394 (count 0)
395 (kcount 0)
396 (case-fold-search completion-ignore-case)
397 fileregex tmpfiles host)
398 (catch 'advocate
399 (unless (and (not files)
400 (or (not (eq (point) (point-max)))
401 (and (string-match "^\\(/[^/]+:\\)\\|\\(//[^/]+/[^/]+\\)" dir)
402 (setq host (concat "^" (regexp-quote (match-string 0 dir))))
403 (not (string-match host default-directory)))))
404 (unless (or (input-pending-p)
405 (not (and (file-exists-p dir) (file-directory-p dir))))
406 (unless files
407 (setq files (directory-files dir nil nil 'nosort))
408 (setq kogiku-minibuffer-prompt-advocate-files
409 (cons (cons dir files) kogiku-minibuffer-prompt-advocate-files)))
410 (if (or (and (string= file "") (setq fileregex "^\\cj"))
411 (and (string-match "\\cj$" file)
412 (setq fileregex (concat "^" (regexp-quote file) "\\cj"))))
413 (while (and files (not (input-pending-p)))
414 (when (string-match fileregex (car files))
415 (throw 'advocate t))
416 (setq files (cdr files)))
417 (unless (input-pending-p)
418 (setq tmpfiles files)
419 (setq fileregex (concat "^" (regexp-quote file) "\\Cj"
420 "\\|^" (regexp-quote file) "$"))
421 (while (and tmpfiles (not (input-pending-p)))
422 (when (string-match fileregex (car tmpfiles))
423 (setq count (1+ count)))
424 (setq tmpfiles (cdr tmpfiles)))
425 (unless (input-pending-p)
426 (setq fileregex
427 (concat "^\\("
428 (if (string-match "^\\(\\(\\Cj*\\)?\\cj+\\)\\(\\Cj+\\)$" file)
429 (concat (match-string 1 file)
430 "\\("
431 (kogiku-migemo-get-pattern (match-string 3 file))
432 "\\)")
433 (kogiku-migemo-get-pattern file))
434 "\\)"))
435 (while (and (<= kcount count)
436 files (not (input-pending-p)))
437 (when (string-match fileregex (car files))
438 (setq kcount (1+ kcount)))
439 (setq files (cdr files)))
440 (> kcount count))))))))))
441
442 (defun kogiku-install-key ()
443 (when (memq minibuffer-completion-table kogiku-take-over-targets)
444 (let ((table (car kogiku-original-completion-tables))
445 (func (lookup-key (current-local-map) kogiku-completion-key))
446 (cfunc (lookup-key (current-local-map) kogiku-mode-change-key)))
447 (if (eq func 'kogiku-complete)
448 (progn
449 (push table kogiku-original-completion-tables)
450 (push (car kogiku-original-functions)
451 kogiku-original-functions)
452 (push (car kogiku-mode-change-original-functions)
453 kogiku-mode-change-original-functions))
454 (push minibuffer-completion-table kogiku-original-completion-tables)
455 (push func kogiku-original-functions)
456 (push cfunc kogiku-mode-change-original-functions))
457 (define-key (current-local-map) kogiku-completion-key 'kogiku-complete)
458 (define-key (current-local-map) kogiku-mode-change-key 'kogiku-mode-change)
459 (setq kogiku-minibuffer-prompt-advocate-files nil)
460 (add-hook 'post-command-hook 'kogiku-minibuffer-prompt)
461 (kogiku-minibuffer-prompt))))
462
463 (add-hook 'minibuffer-setup-hook 'kogiku-install-key)
464
465 (defun kogiku-uninstall-key ()
466 (when (and (or (eq minibuffer-completion-table 'kogiku-read-file-name-internal)
467 (memq minibuffer-completion-table kogiku-take-over-targets))
468 (eq (lookup-key (current-local-map) kogiku-completion-key)
469 'kogiku-complete))
470 (pop kogiku-original-completion-tables)
471 (define-key (current-local-map) kogiku-completion-key (pop kogiku-original-functions))
472 (when (eq (lookup-key (current-local-map) kogiku-mode-change-key)
473 'kogiku-mode-change)
474 (define-key (current-local-map) kogiku-mode-change-key
475 (pop kogiku-mode-change-original-functions))))
476 (setq kogiku-minibuffer-prompt-advocate-files nil)
477 (remove-hook 'post-command-hook 'kogiku-minibuffer-prompt))
478
479 (add-hook 'minibuffer-exit-hook 'kogiku-uninstall-key)
480
481 (provide 'kogiku)
482 ;; kogiku.el ends here

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