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