| 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 |
(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 |
(defcustom kogiku-take-over-targets '(read-file-name-internal |
| 109 |
ffap-read-file-or-url-internal) |
| 110 |
"絨???????c????`minibuffer-completion-table'??┏蕁??? |
| 111 |
minibuffer???九勝??腱祉?c????????/span>`minibuffer-completion-table'???ゃ?? |
| 112 |
`kogiku-take-over-targets'???膣??с?????違??絨????????茖?絎????????茵???????" |
| 113 |
:group 'kogiku |
| 114 |
:type '(repeat symbol)) |
| 115 |
|
| 116 |
(defvar kogiku-original-functions nil) |
| 117 |
(defvar kogiku-original-completion-table nil) |
| 118 |
|
| 119 |
(defvar kogiku-completion-key "\t") |
| 120 |
(defvar kogiku-minibuffer-prompt-overlay nil) |
| 121 |
(defvar kogiku-mode-change-original-functions nil) |
| 122 |
|
| 123 |
(defun kogiku-complete (&optional arg) |
| 124 |
(interactive "P") |
| 125 |
(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 |
(funcall (car kogiku-original-functions)))) |
| 134 |
|
| 135 |
(defun kogiku-complete-with-toggle (switch) |
| 136 |
(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 |
(prog1 |
| 143 |
(funcall (car kogiku-original-functions)) |
| 144 |
(kogiku-minibuffer-prompt))) |
| 145 |
|
| 146 |
(defun kogiku-minibuffer-prompt () |
| 147 |
(if (fboundp 'field-beginning) |
| 148 |
;; Emacs 21 |
| 149 |
(let ((indicator (format "[%s]" kogiku-minibuffer-prompt-string)) |
| 150 |
(props (text-properties-at (point-min))) |
| 151 |
(buffer-read-only nil) |
| 152 |
(inhibit-read-only t)) |
| 153 |
(save-excursion |
| 154 |
(goto-char (point-min)) |
| 155 |
(cond |
| 156 |
((and (not (eq minibuffer-completion-table 'kogiku-read-file-name-internal)) |
| 157 |
(looking-at (concat "^" (regexp-quote indicator) " "))) |
| 158 |
(delete-region (point-min) (match-end 0))) |
| 159 |
((and (eq minibuffer-completion-table 'kogiku-read-file-name-internal) |
| 160 |
(not (looking-at (concat "^" (regexp-quote indicator) " ")))) |
| 161 |
(setq indicator (concat indicator " ")) |
| 162 |
(insert indicator) |
| 163 |
(set-text-properties (point-min) (field-beginning) props) |
| 164 |
(add-text-properties (point-min) (+ -1 (point-min) (length indicator)) |
| 165 |
'(face kogiku-indicator-face)))))) |
| 166 |
;; Others |
| 167 |
(if (eq minibuffer-completion-table 'kogiku-read-file-name-internal) |
| 168 |
(let ((max (min (point-max) (1+ (point-min))))) |
| 169 |
(if kogiku-minibuffer-prompt-overlay |
| 170 |
(move-overlay kogiku-minibuffer-prompt-overlay |
| 171 |
(point-min) max) |
| 172 |
(setq kogiku-minibuffer-prompt-overlay |
| 173 |
(make-overlay (point-min) max))) |
| 174 |
(overlay-put kogiku-minibuffer-prompt-overlay |
| 175 |
'before-string |
| 176 |
(format "%s: " kogiku-minibuffer-prompt-string)) |
| 177 |
(overlay-put kogiku-minibuffer-prompt-overlay 'evaporate t)) |
| 178 |
(when kogiku-minibuffer-prompt-overlay |
| 179 |
(delete-overlay kogiku-minibuffer-prompt-overlay))))) |
| 180 |
|
| 181 |
(if (fboundp 'compare-strings) |
| 182 |
(defalias 'kogiku-compare-strings 'compare-strings) |
| 183 |
(defun kogiku-compare-strings (string1 start1 end1 string2 start2 end2) |
| 184 |
"Convenience `compare-strings' for XEmacs." |
| 185 |
(let* ((str1 (substring string1 start1 end1)) |
| 186 |
(str2 (substring string2 start2 end2)) |
| 187 |
(len (min (length str1) (length str2))) |
| 188 |
(i 0)) |
| 189 |
(if (string= str1 str2) |
| 190 |
t |
| 191 |
(setq i (catch 'ignore |
| 192 |
(while (< i len) |
| 193 |
(when (not (eq (aref str1 i) (aref str2 i))) |
| 194 |
(throw 'ignore i)) |
| 195 |
(setq i (1+ i))) |
| 196 |
i)) |
| 197 |
(1+ i))))) |
| 198 |
|
| 199 |
(defun kogiku-try-completion-regexp (regexp all-list) |
| 200 |
(if (= (length regexp) 0) |
| 201 |
"" |
| 202 |
(substring |
| 203 |
(car all-list) |
| 204 |
0 |
| 205 |
(apply 'min |
| 206 |
(mapcar |
| 207 |
(lambda (a) |
| 208 |
(apply 'min |
| 209 |
(mapcar |
| 210 |
(lambda (b) |
| 211 |
(- (abs |
| 212 |
(kogiku-compare-strings a 0 nil |
| 213 |
b 0 nil)) |
| 214 |
1)) |
| 215 |
(delete a all-list)))) |
| 216 |
all-list))))) |
| 217 |
|
| 218 |
(defun kogiku-migemo-get-pattern (string) |
| 219 |
(let ((migemo-pattern-alist migemo-pattern-alist) |
| 220 |
(migemo-white-space-regexp " *")) |
| 221 |
(let ((case-fold-search nil)) |
| 222 |
(while (string-match "[^a-zA-Z]\\([a-z]+\\)" string) |
| 223 |
(setq string |
| 224 |
(replace-match (capitalize (match-string 1 string)) nil nil string 1)))) |
| 225 |
(migemo-get-pattern string))) |
| 226 |
|
| 227 |
(defun kogiku-file-name-completion (string dir &optional all) |
| 228 |
(let* ((expanded-string (expand-file-name string dir)) |
| 229 |
(files (directory-files dir)) |
| 230 |
(pattern |
| 231 |
(if (string-match "/$" expanded-string) |
| 232 |
"" |
| 233 |
(concat "^\\(" |
| 234 |
(cond |
| 235 |
((string-match "\\cj$" string) |
| 236 |
string) |
| 237 |
((string-match "^\\(\\(\\Cj*\\)?\\cj+\\)\\(\\Cj+\\)$" string) |
| 238 |
(concat (match-string 1 string) |
| 239 |
"\\(" |
| 240 |
(kogiku-migemo-get-pattern (match-string 3 string)) |
| 241 |
"\\)")) |
| 242 |
(t |
| 243 |
(kogiku-migemo-get-pattern (file-name-nondirectory string)))) |
| 244 |
"\\)"))) |
| 245 |
(candidates (delete nil |
| 246 |
(mapcar |
| 247 |
(lambda (f) |
| 248 |
(if (string-match pattern f) |
| 249 |
(if (file-directory-p (expand-file-name f dir)) |
| 250 |
;; ???c????????? / ?х??????????/span> |
| 251 |
(file-name-as-directory f) |
| 252 |
f))) |
| 253 |
files))) |
| 254 |
(count (length candidates)) |
| 255 |
common) |
| 256 |
(if all |
| 257 |
candidates |
| 258 |
(cond |
| 259 |
((eq count 0) nil) |
| 260 |
((eq count 1) (let ((candidate (car candidates))) |
| 261 |
(if (file-directory-p candidate) |
| 262 |
(file-name-as-directory candidate) |
| 263 |
candidate))) |
| 264 |
(t |
| 265 |
(if (null (delete nil (mapcar (lambda (candidate) |
| 266 |
(string-match "\\cj" candidate)) |
| 267 |
candidates))) |
| 268 |
;; ????????茖????ユ??????????с???????翫??????絽檎???? |
| 269 |
;; `try-completion' ???若???с?障???????? |
| 270 |
(try-completion string (mapcar 'list candidates)) |
| 271 |
(setq common (kogiku-try-completion-regexp pattern candidates)) |
| 272 |
(if (eq (length common) 0) string common))))))) |
| 273 |
|
| 274 |
(defun kogiku-file-name-all-completions (string dir) |
| 275 |
(kogiku-file-name-completion string dir t)) |
| 276 |
|
| 277 |
(defun kogiku-double-dollars (input) |
| 278 |
(let ((ret "")) |
| 279 |
(while (string-match "\\$" input) |
| 280 |
(setq ret (concat ret |
| 281 |
(substring input 0 (match-beginning 0)) |
| 282 |
"$$")) |
| 283 |
(setq input (substring input (match-end 0)))) |
| 284 |
(concat ret input))) |
| 285 |
|
| 286 |
(defun kogiku-read-file-name-internal (string dir action) |
| 287 |
(block nil |
| 288 |
(unless (boundp 'read-file-name-predicate) |
| 289 |
(setq read-file-name-predicate nil)) |
| 290 |
(let ((realdir dir) |
| 291 |
(name string) |
| 292 |
(orig-string) |
| 293 |
(changed 0) |
| 294 |
(val) |
| 295 |
(specdir)) |
| 296 |
(if (eq 0 (length string)) |
| 297 |
(if (eq action 'lambda) |
| 298 |
(return nil)) |
| 299 |
(setq orig-string string) |
| 300 |
(setq string (substitute-in-file-name string)) |
| 301 |
(setq changed (null (string-equal string orig-string))) |
| 302 |
(setq name (file-name-nondirectory string)) |
| 303 |
(setq val (file-name-directory string)) |
| 304 |
(if (not (null val)) |
| 305 |
(setq realdir (expand-file-name val realdir)))) |
| 306 |
(cond |
| 307 |
((null action) |
| 308 |
(setq specdir (file-name-directory string)) |
| 309 |
(setq val (kogiku-file-name-completion name realdir)) |
| 310 |
(return (if (not (stringp val)) |
| 311 |
(if changed |
| 312 |
(kogiku-double-dollars string) |
| 313 |
val) |
| 314 |
(if (not (null specdir)) |
| 315 |
(setq val (concat specdir val))) |
| 316 |
(kogiku-double-dollars val)))) |
| 317 |
((eq action t) |
| 318 |
(let ((all (kogiku-file-name-all-completions name realdir))) |
| 319 |
(unless (or (null read-file-name-predicate) |
| 320 |
(eq read-file-name-predicate 'file-exists-p)) |
| 321 |
(delete-if (lambda (x) |
| 322 |
(not (funcall read-file-name-predicate x)) |
| 323 |
all))) |
| 324 |
(return all))) |
| 325 |
((eq action 'lambda) |
| 326 |
(return (if read-file-name-predicate |
| 327 |
(funcall read-file-name-predicate string) |
| 328 |
(file-exists-p string)))))))) |
| 329 |
|
| 330 |
(defun kogiku-mode-change () |
| 331 |
(interactive) |
| 332 |
(setq kogiku-enable-once (not kogiku-enable-once)) |
| 333 |
(when (and kogiku-enable-once |
| 334 |
(eq minibuffer-completion-table 'kogiku-read-file-name-internal)) |
| 335 |
(setq minibuffer-completion-table 'read-file-name-internal)) |
| 336 |
(when (and (not kogiku-enable-once) |
| 337 |
(eq minibuffer-completion-table 'read-file-name-internal)) |
| 338 |
(setq minibuffer-completion-table 'kogiku-read-file-name-internal)) |
| 339 |
(kogiku-minibuffer-prompt) |
| 340 |
(let ((msg (format "[%s%s-mode]" |
| 341 |
(if kogiku-enable-once |
| 342 |
(concat kogiku-minibuffer-prompt-string " ") "") |
| 343 |
(if kogiku-enable-once "ONESHOT" "TOGGLE"))) |
| 344 |
(max (point-max)) |
| 345 |
(inhibit-quit t)) |
| 346 |
(put-text-property 0 (length msg) 'face 'kogiku-change-face msg) |
| 347 |
(save-excursion |
| 348 |
(goto-char max) |
| 349 |
(insert " " msg)) |
| 350 |
(sit-for 5) |
| 351 |
(save-excursion |
| 352 |
(delete-region max (point-max))))) |
| 353 |
|
| 354 |
(defun kogiku-install-key () |
| 355 |
(when (memq minibuffer-completion-table kogiku-take-over-targets) |
| 356 |
(setq kogiku-original-completion-table minibuffer-completion-table) |
| 357 |
(push (lookup-key (current-local-map) kogiku-completion-key) |
| 358 |
kogiku-original-functions) |
| 359 |
(define-key (current-local-map) kogiku-completion-key 'kogiku-complete) |
| 360 |
(push (lookup-key (current-local-map) kogiku-mode-change-key) |
| 361 |
kogiku-mode-change-original-functions) |
| 362 |
(define-key (current-local-map) kogiku-mode-change-key 'kogiku-mode-change))) |
| 363 |
|
| 364 |
(add-hook 'minibuffer-setup-hook 'kogiku-install-key) |
| 365 |
|
| 366 |
(defun kogiku-uninstall-key () |
| 367 |
(when (and (or (eq minibuffer-completion-table 'kogiku-read-file-name-internal) |
| 368 |
(memq minibuffer-completion-table kogiku-take-over-targets)) |
| 369 |
(eq (lookup-key (current-local-map) kogiku-completion-key) |
| 370 |
'kogiku-complete)) |
| 371 |
(define-key (current-local-map) kogiku-completion-key (pop kogiku-original-functions)) |
| 372 |
(when (eq (lookup-key (current-local-map) kogiku-mode-change-key) |
| 373 |
'kogiku-mode-change) |
| 374 |
(define-key (current-local-map) kogiku-mode-change-key |
| 375 |
(pop kogiku-mode-change-original-functions))))) |
| 376 |
|
| 377 |
(add-hook 'minibuffer-exit-hook 'kogiku-uninstall-key) |
| 378 |
|
| 379 |
(provide 'kogiku) |
| 380 |
;; kogiku.el ends here |