1 ;;; liece-minibuf.el --- Minibuffer custom completion.
2 ;; Copyright (C) 1998-2000 Daiki Ueno
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
7 ;; Keywords: minibuffer, completion
9 ;; This file is part of Liece.
11 ;; This program is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; This program is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
32 (require 'liece-compat)
36 (defvar liece-minibuffer-map nil)
37 (defvar liece-minibuffer-complete-function nil)
39 (autoload 'completing-read-multiple "crm")
40 (defvar crm-separator)
42 (unless liece-minibuffer-map
43 (setq liece-minibuffer-map
44 (let ((map (make-sparse-keymap)))
45 (set-keymap-parent map minibuffer-local-map)
46 (define-key map " " nil)
47 (define-key map "\t" 'liece-minibuffer-complete)
48 (define-key map "\r" 'exit-minibuffer)
49 (define-key map "\n" 'exit-minibuffer)
52 (defun liece-minibuffer-complete ()
54 (if (and liece-minibuffer-complete-function
55 (fboundp liece-minibuffer-complete-function))
56 (funcall liece-minibuffer-complete-function)))
58 (defun liece-minibuffer-parse-modes ()
60 (let (preceding-char (state 'flag) type)
64 (setq preceding-char (char-before))
66 ((and (eq state 'flag) (memq preceding-char '(+ ?-)))
69 ((and (eq state 'mode) (eq preceding-char ? ))
71 ((and (eq state 'mode) (memq preceding-char '(?o ?v)))
72 (setq type (nconc type (list 'nick preceding-char
73 (char-before (1- (point)))))))
74 ((and (eq state 'mode) (eq preceding-char ?b))
75 (setq type (nconc type (list 'ban (char-before (1- (point)))))))))
78 (defun liece-minibuffer-prepare-candidate ()
79 (let ((point (point)))
80 (skip-syntax-backward "^ ")
81 (prog1 (buffer-substring (point) point)
84 (defun liece-minibuffer-delete-candidate ()
85 (let ((point (point)))
86 (skip-syntax-backward "^ ")
87 (delete-region (point) point)))
89 (defun liece-minibuffer-finalize-completion (completion pattern all)
93 (temp-minibuffer-message (_ "[No match]")))
94 ((not (string= pattern completion))
95 (liece-minibuffer-delete-candidate)
98 (with-output-to-temp-buffer "*Completions*"
99 (funcall completion-display-completion-list-function
100 (sort all (function (lambda (x y)
103 (or (car-safe y) y))))))))))
105 (defun liece-minibuffer-complete-channel-modes ()
106 (let* ((preceding-char (char-before)) completion candidate all
109 liece-supported-channel-mode-alist ""))
110 (nicks (liece-channel-get-nicks))
112 (context (liece-minibuffer-parse-modes))
113 (state (car context)) (type (cdr context)))
115 ((memq state '(flag mode))
116 (temp-minibuffer-message
117 (format (_ "[Modes are: %s]") modes)))
118 ((and (eq state 'arg) (memq 'ban type))
120 (setq uahs (list-to-alist (liece-channel-get-bans)))
124 (list (concat nick "!"
125 (liece-nick-get-user-at-host nick)))))
127 (setq candidate (liece-minibuffer-prepare-candidate)
128 completion (try-completion candidate uahs)
129 all (all-completions candidate uahs)))
130 ((and (eq state 'arg) (memq 'nick type))
131 (let* ((masks (cond ((memq ?o type) (liece-channel-get-operators))
132 ((memq ?v type) (liece-channel-get-voices))))
139 (string-list-member-ignore-case item ',masks)))
141 (setq nicks (mapcar (function list) nicks)
142 candidate (liece-minibuffer-prepare-candidate)
143 completion (try-completion candidate nicks)
144 all (all-completions candidate nicks)))))
145 (liece-minibuffer-finalize-completion completion candidate all)))
147 (defun liece-minibuffer-complete-user-modes ()
148 (temp-minibuffer-message
150 (_ "[Modes are: %s]")
151 (mapconcat (function car) liece-supported-user-mode-alist ""))))
153 (defun liece-minibuffer-completing-read
154 (prompt table &optional predicate require-match initial-input history default)
157 (format "%s(default %s) " prompt default)
159 table predicate require-match initial-input history default))
161 (defvar liece-minibuffer-completion-separator ","
162 "Separator used for separating strings in `liece-minibuffer-completing-read-multiple'.
163 It should be regular expression which doesn't match word-continuent characters.")
165 (defvar liece-minibuffer-completion-table nil)
167 (defun liece-minibuffer-completing-read-multiple-1 (string predicate flag)
168 "Function used by `liece-minibuffer-completing-read-multiple'.
169 The value of STRING is the string to be completed.
171 The value of PREDICATE is a function to filter possible matches, or
174 The value of FLAG is used to specify the type of completion operation.
175 A value of nil specifies `try-completion'. A value of t specifies
176 `all-completions'. A value of lambda specifes a test for an exact match.
178 For more information on STRING, PREDICATE, and FLAG, see the Elisp
179 Reference sections on 'Programmed Completion' and 'Basic Completion
182 (split-string string liece-minibuffer-completion-separator))
184 (copy-sequence liece-minibuffer-completion-table))
186 ;; Remove a partially matched word construct if it exists.
188 (concat liece-minibuffer-completion-separator "$")
190 (setq except (butlast except)))
192 (concat ".*" liece-minibuffer-completion-separator)
194 (setq lead (substring string 0 (match-end 0))
195 string (substring string (match-end 0))))
197 (setq table (remassoc (car except) table)
198 except (cdr except)))
201 (setq string (try-completion string table predicate))
203 (concat lead string)))
204 (if (eq flag 'lambda)
205 (eq t (try-completion string table predicate))
207 (all-completions string table predicate))))))
209 (defun liece-minibuffer-completing-read-multiple
210 (prompt table &optional predicate require-match initial-input
211 history default multiple-candidate)
212 "Execute `completing-read' consequently.
214 See the documentation for `completing-read' for details on the arguments:
215 PROMPT, TABLE, PREDICATE, REQUIRE-MATCH, INITIAL-INPUT, HISTORY, DEFAULT."
217 (format "%s(punctuate by \"%s\") "
218 prompt liece-minibuffer-completion-separator)))
219 (if multiple-candidate
221 liece-minibuffer-completion-separator))
222 (completing-read-multiple
223 prompt table predicate require-match initial-input
225 (let ((liece-minibuffer-completion-table
229 prompt #'liece-minibuffer-completing-read-multiple-1
230 predicate require-match initial-input history default)
231 liece-minibuffer-completion-separator)))))
233 (provide 'liece-minibuf)
235 ;;; liece-minibuf.el ends here