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
159 (format "%s(default %s) " prompt default)
161 table predicate require-match initial-input history)))
162 (if (and default (equal result ""))
166 (defvar liece-minibuffer-completion-separator ","
167 "Separator used for separating strings in `liece-minibuffer-completing-read-multiple'.
168 It should be regular expression which doesn't match word-continuent characters.")
170 (defvar liece-minibuffer-completion-table nil)
172 (defun liece-minibuffer-completing-read-multiple-1 (string predicate flag)
173 "Function used by `liece-minibuffer-completing-read-multiple'.
174 The value of STRING is the string to be completed.
176 The value of PREDICATE is a function to filter possible matches, or
179 The value of FLAG is used to specify the type of completion operation.
180 A value of nil specifies `try-completion'. A value of t specifies
181 `all-completions'. A value of lambda specifes a test for an exact match.
183 For more information on STRING, PREDICATE, and FLAG, see the Elisp
184 Reference sections on 'Programmed Completion' and 'Basic Completion
187 (split-string string liece-minibuffer-completion-separator))
189 (copy-sequence liece-minibuffer-completion-table))
191 ;; Remove a partially matched word construct if it exists.
193 (concat liece-minibuffer-completion-separator "$")
195 (setq except (butlast except)))
197 (concat ".*" liece-minibuffer-completion-separator)
199 (setq lead (substring string 0 (match-end 0))
200 string (substring string (match-end 0))))
202 (setq table (remassoc (car except) table)
203 except (cdr except)))
206 (setq string (try-completion string table predicate))
208 (concat lead string)))
209 (if (eq flag 'lambda)
210 (eq t (try-completion string table predicate))
212 (all-completions string table predicate))))))
214 (defun liece-minibuffer-completing-read-multiple
215 (prompt table &optional predicate require-match initial-input
216 history default multiple-candidate)
217 "Execute `completing-read' consequently.
219 See the documentation for `completing-read' for details on the arguments:
220 PROMPT, TABLE, PREDICATE, REQUIRE-MATCH, INITIAL-INPUT, HISTORY, DEFAULT."
222 (format "%s(punctuate by \"%s\") "
223 prompt liece-minibuffer-completion-separator)))
224 (if multiple-candidate
226 liece-minibuffer-completion-separator))
227 (completing-read-multiple
228 prompt table predicate require-match initial-input
230 (let ((liece-minibuffer-completion-table
233 (liece-minibuffer-completing-read
234 prompt #'liece-minibuffer-completing-read-multiple-1
235 predicate require-match initial-input history default)
236 liece-minibuffer-completion-separator)))))
238 (provide 'liece-minibuf)
240 ;;; liece-minibuf.el ends here