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.8 - (hide annotations) (download)
Mon Feb 9 10:14:25 2004 UTC (20 years, 2 months ago) by shirai
Branch: MAIN
Changes since 1.7: +7 -1 lines
*** empty log message ***

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を使って簡単にするプログラムです。
24    
25     ;; (今は使っていないのですが、)以前windowsでmeadowを使っていたときに、
26     ;; 「デスクトップ(実際には半角)」にぶちまけてあるファイルをfind-fileし
27     ;; ようとすると(仮名漢字変換が必要なため)ファイル名の指定が非常に面倒で
28     ;; あると感じました。"desukutoxtupu"と打鍵してそれを「デスクトップ」へ変換
29     ;; して[return]でやっと指定することができました。
30    
31     ;; 「デスクトップ」の指定をたとえば、"desktop"とか "de"[tab]とかで済めば
32     ;; すばらしいと思いませんか?migemoを使えばこれを実現することができそう
33     ;; です。"desktop"や"de"をmigemoで日本語に展開し、展開済みのパターンを
34     ;; 現在のディレクトリにある全てのファイルの名前と照合すれば、ファイル名
35     ;; を指定("desktop"と「デスクトップ」の一致)したり、補完候補を生成
36     ;; ("de"と「デスクトップ」の一致)したりすることができます。
37    
38     ;; kogiku.elはmigemo.el及びGNU Emacs 21に依存しています。migemo.elの
39     ;; 動作をまず確認して下さい。kogiku.elをロードパスが通って通っている
40     ;; ディレクトリに置き.emacsに (require 'kogiku)と書き加えてemacsを再
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-completion-key "\t"
77     ""
78     :group 'kogiku
79     :type 'sexp
80     :options '("\t" "\e\t")
81     :set (lambda (symbol value)
82     (set-default symbol value)
83     (setq kogiku-original-function-for-minibuffer-local-completion-map
84     (let ((tmp (lookup-key minibuffer-local-completion-map value)))
85     (if (or (null tmp) (eq tmp 'kogiku-completion-complete))
86     'minibuffer-complete
87     tmp))
88     kogiku-original-function-for-minibuffer-local-must-match-map
89     (let ((tmp (lookup-key minibuffer-local-must-match-map value)))
90     (if (or (null tmp) (eq tmp 'kogiku-must-match-complete))
91     'minibuffer-complete
92     tmp)))
93     (define-key
94     minibuffer-local-completion-map
95     value
96     'kogiku-completion-complete)
97     (define-key
98     minibuffer-local-must-match-map
99     value
100     'kogiku-must-match-complete)))
101    
102     (defvar kogiku-original-function-for-minibuffer-local-completion-map
103     nil)
104    
105     (defvar kogiku-original-function-for-minibuffer-local-must-match-map
106     nil)
107    
108 jet 1.5 (defun kogiku-completion-complete (&optional arg)
109     (interactive "P")
110     (if (and
111     arg
112     (eq minibuffer-completion-table 'read-file-name-internal))
113 jet 1.1 (setq minibuffer-completion-table
114     'kogiku-read-file-name-internal))
115     (funcall
116     kogiku-original-function-for-minibuffer-local-completion-map))
117    
118 jet 1.5 (defun kogiku-must-match-complete (&optional arg)
119     (interactive "P")
120     (if (and
121     arg
122     (eq minibuffer-completion-table 'read-file-name-internal))
123 jet 1.1 (setq minibuffer-completion-table
124     'kogiku-read-file-name-internal))
125     (funcall
126     kogiku-original-function-for-minibuffer-local-must-match-map))
127    
128     (if (fboundp 'compare-strings)
129     (defalias 'kogiku-compare-strings 'compare-strings)
130     (defun kogiku-compare-strings (string1 start1 end1 string2 start2 end2)
131     "Convenience `compare-strings' for XEmacs."
132     (let* ((str1 (substring string1 start1 end1))
133     (str2 (substring string2 start2 end2))
134     (len (min (length str1) (length str2)))
135     (i 0))
136     (if (string= str1 str2)
137     t
138     (setq i (catch 'ignore
139     (while (< i len)
140     (when (not (eq (aref str1 i) (aref str2 i)))
141     (throw 'ignore i))
142     (setq i (1+ i)))
143     i))
144     (1+ i)))))
145    
146     (defun kogiku-try-completion-regexp (regexp all-list)
147     (if (= (length regexp) 0)
148     ""
149     (let (common)
150     (setq common
151     (substring
152     (car all-list)
153     0
154     (apply 'min
155     (mapcar
156     (lambda (a)
157     (apply 'min
158     (mapcar
159     (lambda (b)
160     (- (abs
161     (kogiku-compare-strings a 0 nil
162     b 0 nil))
163     1))
164     (delete a all-list))))
165     all-list))))
166     common)))
167    
168     (defun kogiku-migemo-get-pattern (string)
169 shirai 1.2 (let ((migemo-pattern-alist migemo-pattern-alist)
170     (migemo-after-conv-function nil)
171     (migemo-white-space-regexp " *"))
172 jet 1.1 (let ((case-fold-search nil))
173     (while (string-match "[^a-zA-Z]\\([a-z]+\\)" string)
174     (setq string
175 shirai 1.7 (replace-match (capitalize (match-string 1 string)) nil nil string 1))))
176 jet 1.1 (migemo-get-pattern string)))
177    
178     (defun kogiku-file-name-completion (string dir &optional all)
179     (let* ((expanded-string (expand-file-name string dir))
180     (files (directory-files dir))
181     (pattern
182     (if (string-match "/$" expanded-string)
183     ""
184     (concat "^\\("
185     (cond
186     ((string-match "\\cj$" string)
187     string)
188 shirai 1.6 ((string-match "^\\(\\(\\Cj*\\)?\\cj+\\)\\(\\Cj+\\)$" string)
189 jet 1.1 (concat (match-string 1 string)
190     "\\("
191 shirai 1.6 (kogiku-migemo-get-pattern (match-string 3 string))
192 jet 1.1 "\\)"))
193     (t
194     (kogiku-migemo-get-pattern (file-name-nondirectory string))))
195     "\\)")))
196 jet 1.3 (candidates (delete nil
197 jet 1.1 (mapcar
198     (lambda (f)
199     (if (string-match pattern f)
200     (if (file-directory-p (expand-file-name f dir))
201     ;; ディレクトリは / で終わるように
202     (file-name-as-directory f)
203     f)))
204     files)))
205 jet 1.3 (count (length candidates))
206 jet 1.1 common)
207     (if all
208 jet 1.3 candidates
209 jet 1.1 (cond
210     ((eq count 0) nil)
211 jet 1.3 ((eq count 1) (let ((candidate (car candidates)))
212 jet 1.1 (if (file-directory-p candidate)
213     (file-name-as-directory candidate)
214     candidate)))
215     (t
216 jet 1.3 (if (null (delete nil (mapcar (lambda (candidate)
217     (string-match "\\cj" candidate))
218     candidates)))
219     ;; 全ての候補が日本語を含んでいない場合、通常通り
220     ;; `try-completion' を呼んでまかせる。
221 shirai 1.4 (try-completion string (mapcar 'list candidates))
222 jet 1.3 (setq common (kogiku-try-completion-regexp pattern candidates))
223     (if (eq (length common) 0) string common)))))))
224 jet 1.1
225     (defun kogiku-file-name-all-completions (string dir)
226     (kogiku-file-name-completion string dir t))
227    
228 jet 1.3 (defun kogiku-double-dollars (input)
229 shirai 1.8 (let ((ret ""))
230     (while (string-match "\\$" input)
231     (setq ret (concat ret
232     (substring input 0 (match-beginning 0))
233     "$$"))
234     (setq input (substring input (match-end 0))))
235     (concat ret input)))
236 jet 1.3
237 jet 1.1 (defun kogiku-read-file-name-internal (string dir action)
238     (block nil
239     (unless (boundp 'read-file-name-predicate)
240     (setq read-file-name-predicate nil))
241     (let ((realdir dir)
242     (name string)
243     (orig-string)
244     (changed 0)
245     (val)
246     (specdir))
247     (if (eq 0 (length string))
248     (if (eq action 'lambda)
249     (return nil))
250     (setq orig-string string)
251     (setq string (substitute-in-file-name string))
252     (setq changed (null (string-equal string orig-string)))
253     (setq name (file-name-nondirectory string))
254     (setq val (file-name-directory string))
255     (if (not (null val))
256     (setq realdir (expand-file-name val realdir))))
257     (cond
258     ((null action)
259     (setq specdir (file-name-directory string))
260     (setq val (kogiku-file-name-completion name realdir))
261     (return (if (not (stringp val))
262     (if changed
263 jet 1.3 (kogiku-double-dollars string)
264 jet 1.1 val)
265     (if (not (null specdir))
266     (concat specdir val)
267 jet 1.3 (kogiku-double-dollars val)
268 jet 1.1 ))))
269     ((eq action t)
270     (let ((all (kogiku-file-name-all-completions name realdir)))
271     (unless (or (null read-file-name-predicate)
272     (eq read-file-name-predicate 'file-exists-p))
273     (delete-if (lambda (x)
274     (not (funcall read-file-name-predicate x))
275     all)))
276     (return all)))
277     ((eq action 'lambda)
278     (return (if read-file-name-predicate
279     (funcall read-file-name-predicate string)
280     (file-exists-p string))))))))
281    
282     (provide 'kogiku)
283     ;; kogiku.el ends here

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