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.12 - (show annotations) (download)
Tue Feb 10 04:45:08 2004 UTC (20 years, 2 months ago) by jet
Branch: MAIN
Changes since 1.11: +48 -43 lines
2004-02-10  Masatake YAMATO  <jet@gyve.org>

	* kogiku.el (kogiku-completion-key): 通常の変数にした。
	(kogiku-enable-once): 新しいオプション。

	minibuffer-local-completion-mapとminibuffer-local-must-match-map
	の2系統あった変数、関数を統合した。
	(kogiku-original-function-for-minibuffer-local-completion-map):
	(kogiku-original-function-for-minibuffer-local-must-match-map):
	(kogiku-must-match-complete):
	(kogiku-completion-complete): 削除。
	(kogiku-original-function): 新しい変数。
	kogiku-original-function-for-*を統合。
	(kogiku-complete): 新しい関数。
	(kogiku-oneshot-complete): 新しい関数。
	(kogiku-complete-with-toggle): 新しい関数。

	(kogiku-install-key): 新しい関数。minibuffer-setup-hookに差し込む。
	(kogiku-uninstall-key): 新しい関数。minibuffer-exit-hookに差し込む。

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 ;; 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-minibuffer-prompt-string "kogiku"
77 "*Kogiku indicator in minibuffer."
78 :group 'kogiku
79 :type 'string)
80
81 (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
87 (defvar kogiku-original-function nil)
88 (defvar kogiku-completion-key "\t")
89
90 (defun kogiku-complete (&optional arg)
91 (interactive "P")
92 (if kogiku-enable-once
93 (kogiku-oneshot-complete arg)
94 (kogiku-complete-with-toggle arg)))
95
96 (defun kogiku-oneshot-complete (fire)
97 (let ((minibuffer-completion-table minibuffer-completion-table))
98 (when fire
99 (setq minibuffer-completion-table 'kogiku-read-file-name-internal))
100 (funcall kogiku-original-function)))
101
102 (defun kogiku-complete-with-toggle (switch)
103 (cond
104 ((and switch
105 (eq minibuffer-completion-table 'read-file-name-internal))
106 (setq minibuffer-completion-table 'kogiku-read-file-name-internal)
107 (kogiku-minibuffer-prompt-e21))
108 ((and switch
109 (eq minibuffer-completion-table 'kogiku-read-file-name-internal))
110 (setq minibuffer-completion-table 'read-file-name-internal)
111 (kogiku-minibuffer-prompt-e21)))
112 (funcall kogiku-original-function)
113 (kogiku-minibuffer-prompt-not-e21))
114
115 (defun kogiku-minibuffer-prompt-e21 ()
116 (when (fboundp 'field-beginning)
117 (let ((prompt (buffer-substring (point-min) (field-beginning)))
118 (buffer-read-only nil)
119 (inhibit-read-only t)
120 (props (text-properties-at (point-min))))
121 (save-excursion
122 (when (string-match ":[^:]*" prompt)
123 (setq prompt (concat (substring prompt 0 (match-beginning 0))
124 (format " (%s)" kogiku-minibuffer-prompt-string)
125 (substring prompt (match-beginning 0))))
126 (set-text-properties 0 (length prompt) props prompt)
127 (delete-region (point-min) (field-beginning))
128 (goto-char (point-min))
129 (insert prompt))))))
130
131 (defvar kogiku-minibuffer-prompt-overlay nil)
132
133 (defun kogiku-minibuffer-prompt-not-e21 ()
134 (unless (fboundp 'field-beginning)
135 (when (eq minibuffer-completion-table 'kogiku-read-file-name-internal)
136 (if kogiku-minibuffer-prompt-overlay
137 (move-overlay kogiku-minibuffer-prompt-overlay
138 (point-min) (1+ (point-min)))
139 (setq kogiku-minibuffer-prompt-overlay
140 (make-overlay (point-min) (1+ (point-min)))))
141 (overlay-put kogiku-minibuffer-prompt-overlay
142 'before-string
143 (format "%s: " kogiku-minibuffer-prompt-string))
144 (overlay-put kogiku-minibuffer-prompt-overlay 'evaporate t))))
145
146 (if (fboundp 'compare-strings)
147 (defalias 'kogiku-compare-strings 'compare-strings)
148 (defun kogiku-compare-strings (string1 start1 end1 string2 start2 end2)
149 "Convenience `compare-strings' for XEmacs."
150 (let* ((str1 (substring string1 start1 end1))
151 (str2 (substring string2 start2 end2))
152 (len (min (length str1) (length str2)))
153 (i 0))
154 (if (string= str1 str2)
155 t
156 (setq i (catch 'ignore
157 (while (< i len)
158 (when (not (eq (aref str1 i) (aref str2 i)))
159 (throw 'ignore i))
160 (setq i (1+ i)))
161 i))
162 (1+ i)))))
163
164 (defun kogiku-try-completion-regexp (regexp all-list)
165 (if (= (length regexp) 0)
166 ""
167 (let (common)
168 (setq common
169 (substring
170 (car all-list)
171 0
172 (apply 'min
173 (mapcar
174 (lambda (a)
175 (apply 'min
176 (mapcar
177 (lambda (b)
178 (- (abs
179 (kogiku-compare-strings a 0 nil
180 b 0 nil))
181 1))
182 (delete a all-list))))
183 all-list))))
184 common)))
185
186 (defun kogiku-migemo-get-pattern (string)
187 (let ((migemo-pattern-alist migemo-pattern-alist)
188 (migemo-white-space-regexp " *"))
189 (let ((case-fold-search nil))
190 (while (string-match "[^a-zA-Z]\\([a-z]+\\)" string)
191 (setq string
192 (replace-match (capitalize (match-string 1 string)) nil nil string 1))))
193 (migemo-get-pattern string)))
194
195 (defun kogiku-file-name-completion (string dir &optional all)
196 (let* ((expanded-string (expand-file-name string dir))
197 (files (directory-files dir))
198 (pattern
199 (if (string-match "/$" expanded-string)
200 ""
201 (concat "^\\("
202 (cond
203 ((string-match "\\cj$" string)
204 string)
205 ((string-match "^\\(\\(\\Cj*\\)?\\cj+\\)\\(\\Cj+\\)$" string)
206 (concat (match-string 1 string)
207 "\\("
208 (kogiku-migemo-get-pattern (match-string 3 string))
209 "\\)"))
210 (t
211 (kogiku-migemo-get-pattern (file-name-nondirectory string))))
212 "\\)")))
213 (candidates (delete nil
214 (mapcar
215 (lambda (f)
216 (if (string-match pattern f)
217 (if (file-directory-p (expand-file-name f dir))
218 ;; ???c????????? / ?х??????????/span>
219 (file-name-as-directory f)
220 f)))
221 files)))
222 (count (length candidates))
223 common)
224 (if all
225 candidates
226 (cond
227 ((eq count 0) nil)
228 ((eq count 1) (let ((candidate (car candidates)))
229 (if (file-directory-p candidate)
230 (file-name-as-directory candidate)
231 candidate)))
232 (t
233 (if (null (delete nil (mapcar (lambda (candidate)
234 (string-match "\\cj" candidate))
235 candidates)))
236 ;; ????????茖????ユ??????????с???????翫??????絽檎????
237 ;; `try-completion' ???若???с?障????????
238 (try-completion string (mapcar 'list candidates))
239 (setq common (kogiku-try-completion-regexp pattern candidates))
240 (if (eq (length common) 0) string common)))))))
241
242 (defun kogiku-file-name-all-completions (string dir)
243 (kogiku-file-name-completion string dir t))
244
245 (defun kogiku-double-dollars (input)
246 (let ((ret ""))
247 (while (string-match "\\$" input)
248 (setq ret (concat ret
249 (substring input 0 (match-beginning 0))
250 "$$"))
251 (setq input (substring input (match-end 0))))
252 (concat ret input)))
253
254 (defun kogiku-read-file-name-internal (string dir action)
255 (block nil
256 (unless (boundp 'read-file-name-predicate)
257 (setq read-file-name-predicate nil))
258 (let ((realdir dir)
259 (name string)
260 (orig-string)
261 (changed 0)
262 (val)
263 (specdir))
264 (if (eq 0 (length string))
265 (if (eq action 'lambda)
266 (return nil))
267 (setq orig-string string)
268 (setq string (substitute-in-file-name string))
269 (setq changed (null (string-equal string orig-string)))
270 (setq name (file-name-nondirectory string))
271 (setq val (file-name-directory string))
272 (if (not (null val))
273 (setq realdir (expand-file-name val realdir))))
274 (cond
275 ((null action)
276 (setq specdir (file-name-directory string))
277 (setq val (kogiku-file-name-completion name realdir))
278 (return (if (not (stringp val))
279 (if changed
280 (kogiku-double-dollars string)
281 val)
282 (if (not (null specdir))
283 (concat specdir val)
284 (kogiku-double-dollars val)
285 ))))
286 ((eq action t)
287 (let ((all (kogiku-file-name-all-completions name realdir)))
288 (unless (or (null read-file-name-predicate)
289 (eq read-file-name-predicate 'file-exists-p))
290 (delete-if (lambda (x)
291 (not (funcall read-file-name-predicate x))
292 all)))
293 (return all)))
294 ((eq action 'lambda)
295 (return (if read-file-name-predicate
296 (funcall read-file-name-predicate string)
297 (file-exists-p string))))))))
298
299 (defun kogiku-install-key ()
300 (when (eq minibuffer-completion-table 'read-file-name-internal)
301 (setq kogiku-original-function
302 (let ((tmp (lookup-key (current-local-map) kogiku-completion-key)))
303 (cond
304 ((null tmp)
305 'minibuffer-complete)
306 ((eq tmp 'kogiku-complete)
307 (and kogiku-original-function 'minibuffer-complete))
308 (t
309 tmp))))
310 (define-key (current-local-map) kogiku-completion-key 'kogiku-complete)))
311 (add-hook 'minibuffer-setup-hook 'kogiku-install-key)
312
313 (defun kogiku-uninstall-key ()
314 (when (and (eq minibuffer-completion-table 'read-file-name-internal)
315 (eq (lookup-key (current-local-map) kogiku-completion-key)
316 'kogiku-complete)))
317 (define-key (current-local-map) kogiku-completion-key kogiku-original-function))
318 (add-hook 'minibuffer-exit-hook 'kogiku-uninstall-key)
319
320 (provide 'kogiku)
321 ;; kogiku.el ends here

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