Revision: emacs@sv.gnu.org/emacs--devo--0--patch-22
[bpt/emacs.git] / lisp / erc / erc-pcomplete.el
CommitLineData
597993cf
MB
1;;; erc-pcomplete.el --- Provides programmable completion for ERC
2
3;; Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
4
5;; Author: Sacha Chua <sacha@free.net.ph>
6;; Keywords: comm, convenience
7;; URL: http://www.emacswiki.org/cgi-bin/wiki.pl?ErcCompletion
8
9;; This file is part of GNU Emacs.
10
11;; GNU Emacs 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)
14;; any later version.
15
16;; GNU Emacs 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.
20
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., 51 Franklin Street, Fifth Floor,
24;; Boston, MA 02110-1301, USA.
25
26;;; Commentary:
27
28;; This file replaces erc-complete.el. It provides nick completion
29;; for ERC based on pcomplete. If you do not have pcomplete, you may
30;; try to use erc-complete.el.
31;;
32;; To use, (require 'erc-auto) or (require 'erc-pcomplete), then
33;; (erc-pcomplete-mode 1)
34;;
35;; If you want nickname completions ordered such that the most recent
36;; speakers are listed first, set
37;; `erc-pcomplete-order-nickname-completions' to `t'.
38;;
39;; See CREDITS for other contributors.
40;;
41;;; Code:
42
43(require 'pcomplete)
44(require 'erc)
45(require 'erc-compat)
46(require 'time-date)
47(eval-when-compile (require 'cl))
48
49(defgroup erc-pcomplete nil
50 "Programmable completion for ERC"
51 :group 'erc)
52
53(defcustom erc-pcomplete-nick-postfix ": "
54 "*When `pcomplete' is used in the first word after the prompt,
55add this string to nicks completed."
56 :group 'erc-pcomplete
57 :type 'string)
58
59(defcustom erc-pcomplete-order-nickname-completions t
60 "If t, channel nickname completions will be ordered such that
61the most recent speakers are listed first."
62 :group 'erc-pcomplete
63 :type 'boolean)
64
65;;;###autoload (autoload 'erc-completion-mode "erc-pcomplete" nil t)
66(define-erc-module pcomplete Completion
67 "In ERC Completion mode, the TAB key does completion whenever possible."
68 ((add-hook 'erc-mode-hook 'pcomplete-erc-setup)
69 (add-hook 'erc-complete-functions 'erc-pcomplete)
70 (erc-buffer-list #'pcomplete-erc-setup))
71 ((remove-hook 'erc-mode-hook 'pcomplete-erc-setup)
72 (remove-hook 'erc-complete-functions 'erc-pcomplete)))
73
74(defun erc-pcomplete ()
75 "Complete the nick before point."
76 (interactive)
77 (when (> (point) (erc-beg-of-input-line))
78 (let ((last-command (if (eq last-command 'erc-complete-word)
79 'pcomplete
80 last-command)))
81 (call-interactively 'pcomplete))
82 t))
83
84;;; Setup function
85
86(defun pcomplete-erc-setup ()
87 "Setup `erc-mode' to use pcomplete."
88 (set (make-local-variable 'pcomplete-ignore-case)
89 t)
90 (set (make-local-variable 'pcomplete-use-paring)
91 nil)
92 (set (make-local-variable 'pcomplete-suffix-list)
93 '(? ?:))
94 (set (make-local-variable 'pcomplete-parse-arguments-function)
95 'pcomplete-parse-erc-arguments)
96 (set (make-local-variable 'pcomplete-command-completion-function)
97 'pcomplete/erc-mode/complete-command)
98 (set (make-local-variable 'pcomplete-command-name-function)
99 'pcomplete-erc-command-name)
100 (set (make-local-variable 'pcomplete-default-completion-function)
101 (lambda () (pcomplete-here (pcomplete-erc-nicks)))))
102
103;;; Programmable completion logic
104
105(defun pcomplete/erc-mode/complete-command ()
106 (pcomplete-here
107 (append
108 (pcomplete-erc-commands)
109 (pcomplete-erc-nicks erc-pcomplete-nick-postfix))))
110
111(defvar erc-pcomplete-ctcp-commands
112 '("ACTION" "CLIENTINFO" "ECHO" "FINGER" "PING" "TIME" "USERINFO" "VERSION"))
113
114(defun pcomplete/erc-mode/CTCP ()
115 (pcomplete-here (pcomplete-erc-nicks))
116 (pcomplete-here erc-pcomplete-ctcp-commands))
117
118(defun pcomplete/erc-mode/CLEARTOPIC ()
119 (pcomplete-here (pcomplete-erc-channels)))
120
121(defun pcomplete/erc-mode/DEOP ()
122 (while (pcomplete-here (pcomplete-erc-ops))))
123
124(defun pcomplete/erc-mode/DESCRIBE ()
125 (pcomplete-here (pcomplete-erc-nicks)))
126
127(defun pcomplete/erc-mode/IDLE ()
128 (while (pcomplete-here (pcomplete-erc-nicks))))
129
130(defun pcomplete/erc-mode/KICK ()
131 (pcomplete-here (pcomplete-erc-channels))
132 (pcomplete-here (pcomplete-erc-nicks)))
133
134(defun pcomplete/erc-mode/LOAD ()
135 (pcomplete-here (pcomplete-entries)))
136
137(defun pcomplete/erc-mode/MODE ()
138 (pcomplete-here (pcomplete-erc-channels))
139 (while (pcomplete-here (pcomplete-erc-nicks))))
140
141(defun pcomplete/erc-mode/ME ()
142 (while (pcomplete-here (pcomplete-erc-nicks))))
143
144(defun pcomplete/erc-mode/SAY ()
145 (pcomplete-here (pcomplete-erc-nicks))
146 (pcomplete-here (pcomplete-erc-nicks))
147 (while (pcomplete-here (pcomplete-erc-nicks))))
148
149(defun pcomplete/erc-mode/MSG ()
150 (pcomplete-here (append (pcomplete-erc-all-nicks)
151 (pcomplete-erc-channels)))
152 (while (pcomplete-here (pcomplete-erc-nicks))))
153
154(defun pcomplete/erc-mode/NAMES ()
155 (while (pcomplete-here (pcomplete-erc-channels))))
156
157(defalias 'pcomplete/erc-mode/NOTICE 'pcomplete/erc-mode/MSG)
158
159(defun pcomplete/erc-mode/OP ()
160 (while (pcomplete-here (pcomplete-erc-not-ops))))
161
162(defun pcomplete/erc-mode/PART ()
163 (pcomplete-here (pcomplete-erc-channels)))
164
165(defalias 'pcomplete/erc-mode/LEAVE 'pcomplete/erc-mode/PART)
166
167(defun pcomplete/erc-mode/QUERY ()
168 (pcomplete-here (append (pcomplete-erc-all-nicks)
169 (pcomplete-erc-channels)))
170 (while (pcomplete-here (pcomplete-erc-nicks)))
171 )
172
173(defun pcomplete/erc-mode/SOUND ()
174 (while (pcomplete-here (pcomplete-entries))))
175
176(defun pcomplete/erc-mode/TOPIC ()
177 (pcomplete-here (pcomplete-erc-channels)))
178
179(defun pcomplete/erc-mode/WHOIS ()
180 (while (pcomplete-here (pcomplete-erc-nicks))))
181
182(defun pcomplete/erc-mode/UNIGNORE ()
183 (pcomplete-here (with-current-buffer (erc-server-buffer) erc-ignore-list)))
184
185;;; Functions that provide possible completions.
186
187(defun pcomplete-erc-commands ()
188 "Returns a list of strings of the defined user commands."
189 (let ((case-fold-search nil))
190 (mapcar (lambda (x)
191 (concat "/" (downcase (substring (symbol-name x) 8))))
192 (apropos-internal "erc-cmd-[A-Z]+"))))
193
194(defun pcomplete-erc-ops ()
195 "Returns a list of nicks with ops."
196 (let (ops)
197 (maphash (lambda (nick cdata)
198 (if (and (cdr cdata)
199 (erc-channel-user-op (cdr cdata)))
200 (setq ops (cons nick ops))))
201 erc-channel-users)
202 ops))
203
204(defun pcomplete-erc-not-ops ()
205 "Returns a list of nicks without ops."
206 (let (not-ops)
207 (maphash (lambda (nick cdata)
208 (if (and (cdr cdata)
209 (not (erc-channel-user-op (cdr cdata))))
210 (setq not-ops (cons nick not-ops))))
211 erc-channel-users)
212 not-ops))
213
214
215(defun pcomplete-erc-nicks (&optional postfix)
216 "Returns a list of nicks in the current channel."
217 (let ((users (erc-get-channel-user-list)))
218 (if erc-pcomplete-order-nickname-completions
219 (setq users (erc-sort-channel-users-by-activity users)))
220 (mapcar (lambda (x)
221 (concat (erc-server-user-nickname (car x)) postfix))
222 users)))
223
224(defun pcomplete-erc-all-nicks (&optional postfix)
225 "Returns a list of all nicks on the current server."
226 (let (nicks)
227 (with-current-buffer (process-buffer erc-server-process)
228 (maphash (lambda (nick user)
229 (setq nicks (cons (concat nick postfix) nicks)))
230 erc-server-users))
231 nicks))
232
233(defun pcomplete-erc-channels ()
234 "Returns a list of channels associated with the current server."
235 (mapcar (lambda (buf) (with-current-buffer buf (erc-default-target)))
236 (erc-channel-list erc-server-process)))
237
238;;; Functions for parsing
239
240(defun pcomplete-erc-command-name ()
241 "Returns the command name of the first argument."
242 (if (eq (elt (pcomplete-arg 'first) 0) ?/)
243 (upcase (substring (pcomplete-arg 'first) 1))
244 "SAY"))
245
246(defun pcomplete-parse-erc-arguments ()
247 "Returns a list of parsed whitespace-separated arguments.
248These are the words from the beginning of the line after the prompt
249up to where point is right now."
250 (let* ((start erc-input-marker)
251 (end (point))
252 args beginnings)
253 (save-excursion
254 (if (< (skip-chars-backward " \t\n" start) 0)
255 (setq args '("")
256 beginnings (list end)))
257 (setq end (point))
258 (while (< (skip-chars-backward "^ \t\n" start) 0)
259 (setq beginnings (cons (point) beginnings)
260 args (cons (buffer-substring-no-properties
261 (point) end)
262 args))
263 (skip-chars-backward " \t\n" start)
264 (setq end (point))))
265 (cons args beginnings)))
266
267(provide 'erc-pcomplete)
268
269;;; erc-pcomplete.el ends here
270;;
271;; Local Variables:
272;; indent-tabs-mode: nil
273;; End:
274
275;; arch-tag: 32a7703b-be87-45a4-82f3-9eed5a628911