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.19 - (hide annotations) (download)
Tue Feb 10 13:31:36 2004 UTC (20 years, 2 months ago) by jet
Branch: MAIN
Changes since 1.18: +7 -3 lines
2004-02-10  Masatake YAMATO  <jet@gyve.org>

	* kogiku.el (kogiku-take-over-targets): ・ォ・ケ・ソ・゙・、・コイス。」

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     ;; 譌・譛ャ隱槭ヵ繧。繧、繝ォ蜷阪?蜈・蜉帙rmigemo繧剃スソ縺」縺ヲ邁。蜊倥↓縺吶k繝励Ο繧ー繝ゥ繝?縺ァ縺吶??/span>
24    
25     ;; (莉翫?菴ソ縺」縺ヲ縺?↑縺??縺ァ縺吶′縲?莉・蜑購indows縺ァmeadow繧剃スソ縺」縺ヲ縺?◆縺ィ縺阪↓縲?/span>
26     ;; 縲後ョ繧ケ繧ッ繝医ャ繝?螳滄圀縺ォ縺ッ蜊願ァ?縲阪↓縺カ縺。縺セ縺代※縺ゅk繝輔ぃ繧、繝ォ繧断ind-file縺?/span>
27     ;; 繧医≧縺ィ縺吶k縺ィ(莉ョ蜷肴シ「蟄怜、画鋤縺悟ソ?ヲ√↑縺溘a)繝輔ぃ繧、繝ォ蜷阪?謖?ョ壹′髱槫クク縺ォ髱「蛟偵〒
28     ;; 縺ゅk縺ィ諢溘§縺セ縺励◆縲?quot;desukutoxtupu"縺ィ謇馴嵯縺励※縺昴l繧偵?後ョ繧ケ繧ッ繝医ャ繝励?阪∈螟画鋤
29     ;; 縺励※[return]縺ァ繧?▲縺ィ謖?ョ壹☆繧九%縺ィ縺後〒縺阪∪縺励◆縲?/span>
30    
31     ;; 縲後ョ繧ケ繧ッ繝医ャ繝励?阪?謖?ョ壹r縺溘→縺医?縲?quot;desktop"縺ィ縺 "de"[tab]縺ィ縺九〒貂医a縺ー
32     ;; 縺吶?繧峨@縺?→諤昴>縺セ縺帙s縺具シ殞igemo繧剃スソ縺医?縺薙l繧貞ョ溽樟縺吶k縺薙→縺後〒縺阪◎縺?/span>
33     ;; 縺ァ縺吶??quot;desktop"繧?quot;de"繧知igemo縺ァ譌・譛ャ隱槭↓螻暮幕縺励?∝ア暮幕貂医∩縺ョ繝代ち繝シ繝ウ繧?/span>
34     ;; 迴セ蝨ィ縺ョ繝?ぅ繝ャ繧ッ繝医Μ縺ォ縺ゅk蜈ィ縺ヲ縺ョ繝輔ぃ繧、繝ォ縺ョ蜷榊燕縺ィ辣ァ蜷医☆繧後?縲√ヵ繧。繧、繝ォ蜷?/span>
35     ;; 繧呈欠螳?"desktop"縺ィ縲後ョ繧ケ繧ッ繝医ャ繝励?阪?荳?閾エ)縺励◆繧翫?∬」懷ョ悟?呵」懊r逕滓?
36     ;; ("de"縺ィ縲後ョ繧ケ繧ッ繝医ャ繝励?阪?荳?閾エ)縺励◆繧翫☆繧九%縺ィ縺後〒縺阪∪縺吶??/span>
37    
38     ;; kogiku.el縺ッmigemo.el蜿翫?GNU Emacs 21縺ォ萓晏ュ倥@縺ヲ縺?∪縺吶?Nigemo.el縺ョ
39     ;; 蜍穂ス懊r縺セ縺夂「コ隱阪@縺ヲ荳九&縺??Logiku.el繧偵Ο繝シ繝峨ヱ繧ケ縺碁?壹▲縺ヲ騾壹▲縺ヲ縺?k
40     ;; 繝?ぅ繝ャ繧ッ繝医Μ縺ォ鄂ョ縺?emacs縺ォ (require 'kogiku)縺ィ譖ク縺榊刈縺医※emacs繧貞?
41     ;; 襍キ蜍輔@縺セ縺吶?Nigemo縺ッ http://migemo.namazu.rog 縺九i蜈・謇九〒縺阪∪縺吶??/span>
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 jet 1.19 (defcustom kogiku-take-over-targets '(read-file-name-internal
109     ffap-read-file-or-url-internal)
110     "蟆剰所縺後?縺」縺ィ繧?/span>`minibuffer-completion-table'縺ョ遞ョ鬘槭??/span>
111     minibuffer縺ォ蛻カ蠕。縺檎ァサ縺」縺溘→縺阪↓`minibuffer-completion-table'縺ョ蛟、縺?/span>
112     `kogiku-take-over-targets'縺ョ隕∫エ?縺ァ縺ゅl縺ー縲∝ー剰所縺ォ繧医k陬懷ョ後?貅門y繧定。後↑縺???quot;
113     :group 'kogiku
114     :type '(repeat symbol))
115 jet 1.14
116     (defvar kogiku-original-functions nil)
117     (defvar kogiku-original-completion-table nil)
118    
119 jet 1.12 (defvar kogiku-completion-key "\t")
120 shirai 1.13 (defvar kogiku-minibuffer-prompt-overlay nil)
121 shirai 1.15 (defvar kogiku-mode-change-original-functions nil)
122 jet 1.1
123 jet 1.12 (defun kogiku-complete (&optional arg)
124 jet 1.5 (interactive "P")
125 jet 1.12 (if kogiku-enable-once
126     (kogiku-oneshot-complete arg)
127     (kogiku-complete-with-toggle arg)))
128    
129     (defun kogiku-oneshot-complete (fire)
130     (let ((minibuffer-completion-table minibuffer-completion-table))
131     (when fire
132     (setq minibuffer-completion-table 'kogiku-read-file-name-internal))
133 jet 1.14 (funcall (car kogiku-original-functions))))
134 jet 1.12
135     (defun kogiku-complete-with-toggle (switch)
136 shirai 1.17 (cond
137     ((and switch
138     (eq minibuffer-completion-table 'kogiku-read-file-name-internal))
139     (setq minibuffer-completion-table kogiku-original-completion-table))
140     (switch
141     (setq minibuffer-completion-table 'kogiku-read-file-name-internal)))
142 jet 1.14 (funcall (car kogiku-original-functions))
143 shirai 1.17 (kogiku-minibuffer-prompt))
144 shirai 1.10
145 shirai 1.17 (defun kogiku-minibuffer-prompt ()
146     (if (fboundp 'field-beginning)
147     ;; Emacs 21
148     (let ((prompt (buffer-substring (point-min) (field-beginning)))
149     (props (text-properties-at (point-min)))
150 shirai 1.18 (indicator (format " [%s]" kogiku-minibuffer-prompt-string))
151 shirai 1.17 (buffer-read-only nil)
152     (inhibit-read-only t)
153     replace)
154     (save-excursion
155 shirai 1.16 (when (string-match (regexp-quote indicator) prompt)
156 shirai 1.17 (setq replace t)
157 shirai 1.13 (setq prompt (replace-match "" nil nil prompt))
158 shirai 1.17 (set-text-properties 0 (length prompt) props prompt))
159     (when (and (eq minibuffer-completion-table
160     'kogiku-read-file-name-internal)
161     (string-match ":[^:]*" prompt))
162     (setq replace t)
163     (set-text-properties 0 (length indicator) props indicator)
164     (add-text-properties 0 (length indicator)
165     '(face kogiku-indicator-face) indicator)
166     (setq prompt (concat (substring prompt 0 (match-beginning 0))
167     indicator
168     (substring prompt (match-beginning 0)))))
169     (when replace
170 shirai 1.13 (delete-region (point-min) (field-beginning))
171     (goto-char (point-min))
172 shirai 1.17 (insert prompt))))
173     ;; Others
174 shirai 1.13 (if (eq minibuffer-completion-table 'kogiku-read-file-name-internal)
175     (let ((max (min (point-max) (1+ (point-min)))))
176     (if kogiku-minibuffer-prompt-overlay
177     (move-overlay kogiku-minibuffer-prompt-overlay
178     (point-min) max)
179     (setq kogiku-minibuffer-prompt-overlay
180     (make-overlay (point-min) max)))
181     (overlay-put kogiku-minibuffer-prompt-overlay
182     'before-string
183     (format "%s: " kogiku-minibuffer-prompt-string))
184     (overlay-put kogiku-minibuffer-prompt-overlay 'evaporate t))
185     (when kogiku-minibuffer-prompt-overlay
186     (delete-overlay kogiku-minibuffer-prompt-overlay)))))
187    
188 jet 1.1 (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 shirai 1.13 (if (= (length regexp) 0)
208 jet 1.1 ""
209     (let (common)
210     (setq common
211     (substring
212 shirai 1.13 (car all-list)
213 jet 1.1 0
214 shirai 1.13 (apply 'min
215 jet 1.1 (mapcar
216     (lambda (a)
217 shirai 1.13 (apply 'min
218     (mapcar
219 jet 1.1 (lambda (b)
220 shirai 1.13 (- (abs
221 jet 1.1 (kogiku-compare-strings a 0 nil
222     b 0 nil))
223     1))
224     (delete a all-list))))
225     all-list))))
226     common)))
227    
228     (defun kogiku-migemo-get-pattern (string)
229 shirai 1.2 (let ((migemo-pattern-alist migemo-pattern-alist)
230     (migemo-white-space-regexp " *"))
231 jet 1.1 (let ((case-fold-search nil))
232     (while (string-match "[^a-zA-Z]\\([a-z]+\\)" string)
233     (setq string
234 shirai 1.7 (replace-match (capitalize (match-string 1 string)) nil nil string 1))))
235 jet 1.1 (migemo-get-pattern string)))
236    
237     (defun kogiku-file-name-completion (string dir &optional all)
238     (let* ((expanded-string (expand-file-name string dir))
239     (files (directory-files dir))
240     (pattern
241     (if (string-match "/$" expanded-string)
242     ""
243     (concat "^\\("
244     (cond
245     ((string-match "\\cj$" string)
246     string)
247 shirai 1.6 ((string-match "^\\(\\(\\Cj*\\)?\\cj+\\)\\(\\Cj+\\)$" string)
248 jet 1.1 (concat (match-string 1 string)
249     "\\("
250 shirai 1.6 (kogiku-migemo-get-pattern (match-string 3 string))
251 jet 1.1 "\\)"))
252     (t
253     (kogiku-migemo-get-pattern (file-name-nondirectory string))))
254     "\\)")))
255 jet 1.3 (candidates (delete nil
256 jet 1.1 (mapcar
257     (lambda (f)
258     (if (string-match pattern f)
259     (if (file-directory-p (expand-file-name f dir))
260     ;; 繝?ぅ繝ャ繧ッ繝医Μ縺ッ / 縺ァ邨ゅo繧九h縺?↓
261     (file-name-as-directory f)
262     f)))
263     files)))
264 jet 1.3 (count (length candidates))
265 jet 1.1 common)
266     (if all
267 jet 1.3 candidates
268 jet 1.1 (cond
269     ((eq count 0) nil)
270 jet 1.3 ((eq count 1) (let ((candidate (car candidates)))
271 jet 1.1 (if (file-directory-p candidate)
272     (file-name-as-directory candidate)
273     candidate)))
274     (t
275 jet 1.3 (if (null (delete nil (mapcar (lambda (candidate)
276     (string-match "\\cj" candidate))
277     candidates)))
278     ;; 蜈ィ縺ヲ縺ョ蛟呵」懊′譌・譛ャ隱槭r蜷ォ繧薙〒縺?↑縺??エ蜷医???壼クク騾壹j
279     ;; `try-completion' 繧貞他繧薙〒縺セ縺九○繧九??/span>
280 shirai 1.4 (try-completion string (mapcar 'list candidates))
281 jet 1.3 (setq common (kogiku-try-completion-regexp pattern candidates))
282     (if (eq (length common) 0) string common)))))))
283 jet 1.1
284     (defun kogiku-file-name-all-completions (string dir)
285     (kogiku-file-name-completion string dir t))
286    
287 jet 1.3 (defun kogiku-double-dollars (input)
288 shirai 1.8 (let ((ret ""))
289     (while (string-match "\\$" input)
290     (setq ret (concat ret
291     (substring input 0 (match-beginning 0))
292     "$$"))
293     (setq input (substring input (match-end 0))))
294     (concat ret input)))
295 jet 1.3
296 jet 1.1 (defun kogiku-read-file-name-internal (string dir action)
297     (block nil
298     (unless (boundp 'read-file-name-predicate)
299     (setq read-file-name-predicate nil))
300     (let ((realdir dir)
301     (name string)
302     (orig-string)
303     (changed 0)
304     (val)
305     (specdir))
306     (if (eq 0 (length string))
307     (if (eq action 'lambda)
308     (return nil))
309     (setq orig-string string)
310     (setq string (substitute-in-file-name string))
311     (setq changed (null (string-equal string orig-string)))
312     (setq name (file-name-nondirectory string))
313     (setq val (file-name-directory string))
314     (if (not (null val))
315     (setq realdir (expand-file-name val realdir))))
316     (cond
317     ((null action)
318     (setq specdir (file-name-directory string))
319     (setq val (kogiku-file-name-completion name realdir))
320     (return (if (not (stringp val))
321 shirai 1.13 (if changed
322 jet 1.3 (kogiku-double-dollars string)
323 jet 1.1 val)
324 shirai 1.13 (if (not (null specdir))
325     (setq val (concat specdir val)))
326     (kogiku-double-dollars val))))
327 jet 1.1 ((eq action t)
328     (let ((all (kogiku-file-name-all-completions name realdir)))
329     (unless (or (null read-file-name-predicate)
330     (eq read-file-name-predicate 'file-exists-p))
331 shirai 1.13 (delete-if (lambda (x)
332 jet 1.1 (not (funcall read-file-name-predicate x))
333     all)))
334     (return all)))
335     ((eq action 'lambda)
336     (return (if read-file-name-predicate
337     (funcall read-file-name-predicate string)
338     (file-exists-p string))))))))
339 jet 1.12
340 shirai 1.15 (defun kogiku-mode-change ()
341     (interactive)
342     (setq kogiku-enable-once (not kogiku-enable-once))
343     (when (and kogiku-enable-once
344     (eq minibuffer-completion-table 'kogiku-read-file-name-internal))
345     (setq minibuffer-completion-table 'read-file-name-internal))
346     (when (and (not kogiku-enable-once)
347     (eq minibuffer-completion-table 'read-file-name-internal))
348     (setq minibuffer-completion-table 'kogiku-read-file-name-internal))
349 shirai 1.17 (kogiku-minibuffer-prompt)
350 shirai 1.15 (let ((msg (format "[%s%s-mode]"
351     (if kogiku-enable-once
352     (concat kogiku-minibuffer-prompt-string " ") "")
353     (if kogiku-enable-once "ONESHOT" "TOGGLE")))
354     (max (point-max))
355     (inhibit-quit t))
356     (put-text-property 0 (length msg) 'face 'kogiku-change-face msg)
357     (save-excursion
358     (goto-char max)
359     (insert " " msg))
360     (sit-for 5)
361     (save-excursion
362     (delete-region max (point-max)))))
363    
364 jet 1.12 (defun kogiku-install-key ()
365 shirai 1.15 (when (memq minibuffer-completion-table kogiku-take-over-targets)
366 jet 1.14 (setq kogiku-original-completion-table minibuffer-completion-table)
367     (push (lookup-key (current-local-map) kogiku-completion-key)
368     kogiku-original-functions)
369 shirai 1.15 (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 shirai 1.13
374 jet 1.12 (add-hook 'minibuffer-setup-hook 'kogiku-install-key)
375    
376     (defun kogiku-uninstall-key ()
377 jet 1.14 (when (and (or (eq minibuffer-completion-table 'kogiku-read-file-name-internal)
378     (memq minibuffer-completion-table kogiku-take-over-targets))
379 jet 1.12 (eq (lookup-key (current-local-map) kogiku-completion-key)
380 shirai 1.13 'kogiku-complete))
381 shirai 1.15 (define-key (current-local-map) kogiku-completion-key (pop kogiku-original-functions))
382     (when (eq (lookup-key (current-local-map) kogiku-mode-change-key)
383     'kogiku-mode-change)
384     (define-key (current-local-map) kogiku-mode-change-key
385     (pop kogiku-mode-change-original-functions)))))
386    
387     (add-hook 'minibuffer-exit-hook 'kogiku-uninstall-key)
388 jet 1.1
389     (provide 'kogiku)
390     ;; kogiku.el ends here

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