Develop and Download Open Source Software

Browse CVS Repository

Annotation of /kogiku/kogiku/kogiku.el

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


Revision 1.15 - (hide annotations) (download)
Tue Feb 10 11:47:59 2004 UTC (20 years, 2 months ago) by shirai
Branch: MAIN
Changes since 1.14: +70 -11 lines
(kogiku-mode-change-key): toggle-mode と oneshot-mode
を切り替えるキー指定。
(kogiku-indicator-face, kogiku-change-face): indicator と mode
change 表示の Face。
(kogiku-mode-change-original-functions): mode chage 用のオリジナル
関数の待避変数。
(kogiku-minibuffer-prompt-e21): `kogiku-indicator-face' を使うよう
に変更。
(kogiku-mode-change): toggle/oneshot の mode change 関数。
(kogiku-install-key, kogiku-uninstall-key): mode change 用の設定追
加。

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

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