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