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 (require 'liece-channel)
37 (require 'liece-inlines))
39 (defvar liece-minibuffer-map nil)
40 (defvar liece-minibuffer-complete-function nil)
42 (autoload 'completing-read-multiple "crm")
43 (defvar crm-separator)
45 (unless liece-minibuffer-map
46 (setq liece-minibuffer-map
47 (let ((map (make-sparse-keymap)))
48 (set-keymap-parent map minibuffer-local-map)
49 (define-key map " " nil)
50 (define-key map "\t" 'liece-minibuffer-complete)
51 (define-key map "\r" 'exit-minibuffer)
52 (define-key map "\n" 'exit-minibuffer)
55 (defun liece-minibuffer-complete ()
57 (if (and liece-minibuffer-complete-function
58 (fboundp liece-minibuffer-complete-function))
59 (funcall liece-minibuffer-complete-function)))
61 (defun liece-minibuffer-parse-modes ()
63 (let (preceding-char (state 'flag) type)
67 (setq preceding-char (char-before))
69 ((and (eq state 'flag) (memq preceding-char '(+ ?-)))
72 ((and (eq state 'mode) (eq preceding-char ? ))
74 ((and (eq state 'mode) (memq preceding-char '(?o ?v)))
75 (setq type (nconc type (list 'nick preceding-char
76 (char-before (1- (point)))))))
77 ((and (eq state 'mode) (eq preceding-char ?b))
78 (setq type (nconc type (list 'ban (char-before (1- (point)))))))))
81 (defun liece-minibuffer-prepare-candidate ()
82 (let ((point (point)))
83 (skip-syntax-backward "^ ")
84 (prog1 (buffer-substring (point) point)
87 (defun liece-minibuffer-delete-candidate ()
88 (let ((point (point)))
89 (skip-syntax-backward "^ ")
90 (delete-region (point) point)))
92 (defun liece-minibuffer-finalize-completion (completion pattern all)
96 (temp-minibuffer-message (_ "[No match]")))
97 ((not (string= pattern completion))
98 (liece-minibuffer-delete-candidate)
101 (with-output-to-temp-buffer "*Completions*"
102 (funcall completion-display-completion-list-function
103 (sort all (function (lambda (x y)
106 (or (car-safe y) y))))))))))
108 (defun liece-minibuffer-complete-channel-modes ()
109 (let* ((preceding-char (char-before)) completion candidate all
112 liece-supported-channel-mode-alist ""))
113 (nicks (liece-channel-get-nicks))
115 (context (liece-minibuffer-parse-modes))
116 (state (car context)) (type (cdr context)))
118 ((memq state '(flag mode))
119 (temp-minibuffer-message
120 (format (_ "[Modes are: %s]") modes)))
121 ((and (eq state 'arg) (memq 'ban type))
123 (setq uahs (list-to-alist (liece-channel-get-bans)))
127 (list (concat nick "!"
128 (liece-nick-get-user-at-host nick)))))
130 (setq candidate (liece-minibuffer-prepare-candidate)
131 completion (try-completion candidate uahs)
132 all (all-completions candidate uahs)))
133 ((and (eq state 'arg) (memq 'nick type))
134 (let* ((masks (cond ((memq ?o type) (liece-channel-get-operators))
135 ((memq ?v type) (liece-channel-get-voices))))
142 (string-list-member-ignore-case item ',masks)))
144 (setq nicks (mapcar (function list) nicks)
145 candidate (liece-minibuffer-prepare-candidate)
146 completion (try-completion candidate nicks)
147 all (all-completions candidate nicks)))))
148 (liece-minibuffer-finalize-completion completion candidate all)))
150 (defun liece-minibuffer-complete-user-modes ()
151 (temp-minibuffer-message
153 (_ "[Modes are: %s]")
154 (mapconcat (function car) liece-supported-user-mode-alist ""))))
156 (defun liece-minibuffer-completing-read
157 (prompt table &optional predicate require-match initial-input history
162 (format "%s(default %s) " prompt default)
164 table predicate require-match initial-input history)))
165 (if (and default (equal result ""))
169 (defvar liece-minibuffer-completion-separator ","
170 "Separator used for separating strings in `liece-minibuffer-completing-read-multiple'.
171 It should be regular expression which doesn't match word-continuent characters.")
173 (defvar liece-minibuffer-completion-table nil)
175 (defun liece-minibuffer-completing-read-multiple-1 (string predicate flag)
176 "Function used by `liece-minibuffer-completing-read-multiple'.
177 The value of STRING is the string to be completed.
179 The value of PREDICATE is a function to filter possible matches, or
182 The value of FLAG is used to specify the type of completion operation.
183 A value of nil specifies `try-completion'. A value of t specifies
184 `all-completions'. A value of lambda specifes a test for an exact match.
186 For more information on STRING, PREDICATE, and FLAG, see the Elisp
187 Reference sections on 'Programmed Completion' and 'Basic Completion
190 (split-string string liece-minibuffer-completion-separator))
192 (copy-sequence liece-minibuffer-completion-table))
194 ;; Remove a partially matched word construct if it exists.
196 (concat liece-minibuffer-completion-separator "$")
198 (setq except (butlast except)))
200 (concat ".*" liece-minibuffer-completion-separator)
202 (setq lead (substring string 0 (match-end 0))
203 string (substring string (match-end 0))))
205 (setq table (remassoc (car except) table)
206 except (cdr except)))
209 (setq string (try-completion string table predicate))
211 (concat lead string)))
212 (if (eq flag 'lambda)
213 (eq t (try-completion string table predicate))
215 (all-completions string table predicate))))))
217 (defun liece-minibuffer-completing-read-multiple
218 (prompt table &optional predicate require-match initial-input
219 history default multiple-candidate)
220 "Execute `completing-read' consequently.
222 See the documentation for `completing-read' for details on the arguments:
223 PROMPT, TABLE, PREDICATE, REQUIRE-MATCH, INITIAL-INPUT, HISTORY, DEFAULT."
225 (format "%s(punctuate by \"%s\") "
226 prompt liece-minibuffer-completion-separator)))
227 (if multiple-candidate
229 liece-minibuffer-completion-separator))
230 (completing-read-multiple
231 prompt table predicate require-match initial-input
233 (let ((liece-minibuffer-completion-table
236 (liece-minibuffer-completing-read
237 prompt #'liece-minibuffer-completing-read-multiple-1
238 predicate require-match initial-input history default)
239 liece-minibuffer-completion-separator)))))
241 (provide 'liece-minibuf)
243 ;;; liece-minibuf.el ends here