a390fcfe84da85749c570b11f605e1fbe7ae1d1d
[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 (or (let ((pcomplete-default-completion-function #'ignore))
77 (pcomplete-completions-at-point))
78 (let ((c (pcomplete-completions-at-point)))
79 (if c (nconc c '(:exclusive no)))))))
80
81 (defun erc-pcomplete ()
82 "Complete the nick before point."
83 (interactive)
84 (when (> (point) (erc-beg-of-input-line))
85 (let ((last-command (if (eq last-command 'erc-complete-word)
86 'pcomplete
87 last-command)))
88 (call-interactively 'pcomplete))
89 t))
90
91 ;;; Setup function
92
93 (defun pcomplete-erc-setup ()
94 "Setup `erc-mode' to use pcomplete."
95 (set (make-local-variable 'pcomplete-ignore-case)
96 t)
97 (set (make-local-variable 'pcomplete-use-paring)
98 nil)
99 (set (make-local-variable 'pcomplete-parse-arguments-function)
100 'pcomplete-erc-parse-arguments)
101 (set (make-local-variable 'pcomplete-command-completion-function)
102 'pcomplete/erc-mode/complete-command)
103 (set (make-local-variable 'pcomplete-command-name-function)
104 'pcomplete-erc-command-name)
105 (set (make-local-variable 'pcomplete-default-completion-function)
106 (lambda () (pcomplete-here (pcomplete-erc-nicks)))))
107
108 ;;; Programmable completion logic
109
110 (defun pcomplete/erc-mode/complete-command ()
111 (pcomplete-here
112 (append
113 (pcomplete-erc-commands)
114 (pcomplete-erc-nicks erc-pcomplete-nick-postfix t))))
115
116 (defvar erc-pcomplete-ctcp-commands
117 '("ACTION" "CLIENTINFO" "ECHO" "FINGER" "PING" "TIME" "USERINFO" "VERSION"))
118
119 (defun pcomplete/erc-mode/CTCP ()
120 (pcomplete-here (pcomplete-erc-nicks))
121 (pcomplete-here erc-pcomplete-ctcp-commands))
122
123 (defun pcomplete/erc-mode/CLEARTOPIC ()
124 (pcomplete-here (pcomplete-erc-channels)))
125
126 (defun pcomplete/erc-mode/DEOP ()
127 (while (pcomplete-here (pcomplete-erc-ops))))
128
129 (defun pcomplete/erc-mode/DESCRIBE ()
130 (pcomplete-here (pcomplete-erc-nicks)))
131
132 (defun pcomplete/erc-mode/IDLE ()
133 (while (pcomplete-here (pcomplete-erc-nicks))))
134
135 (defun pcomplete/erc-mode/KICK ()
136 (pcomplete-here (pcomplete-erc-channels))
137 (pcomplete-here (pcomplete-erc-nicks)))
138
139 (defun pcomplete/erc-mode/LOAD ()
140 (pcomplete-here (pcomplete-entries)))
141
142 (defun pcomplete/erc-mode/MODE ()
143 (pcomplete-here (pcomplete-erc-channels))
144 (while (pcomplete-here (pcomplete-erc-nicks))))
145
146 (defun pcomplete/erc-mode/ME ()
147 (while (pcomplete-here (pcomplete-erc-nicks))))
148
149 (defun pcomplete/erc-mode/SAY ()
150 (pcomplete-here (pcomplete-erc-nicks))
151 (pcomplete-here (pcomplete-erc-nicks))
152 (while (pcomplete-here (pcomplete-erc-nicks))))
153
154 (defun pcomplete/erc-mode/MSG ()
155 (pcomplete-here (append (pcomplete-erc-all-nicks)
156 (pcomplete-erc-channels)))
157 (while (pcomplete-here (pcomplete-erc-nicks))))
158
159 (defun pcomplete/erc-mode/NAMES ()
160 (while (pcomplete-here (pcomplete-erc-channels))))
161
162 (defalias 'pcomplete/erc-mode/NOTICE 'pcomplete/erc-mode/MSG)
163
164 (defun pcomplete/erc-mode/OP ()
165 (while (pcomplete-here (pcomplete-erc-not-ops))))
166
167 (defun pcomplete/erc-mode/PART ()
168 (pcomplete-here (pcomplete-erc-channels)))
169
170 (defalias 'pcomplete/erc-mode/LEAVE 'pcomplete/erc-mode/PART)
171
172 (defun pcomplete/erc-mode/QUERY ()
173 (pcomplete-here (append (pcomplete-erc-all-nicks)
174 (pcomplete-erc-channels)))
175 (while (pcomplete-here (pcomplete-erc-nicks)))
176 )
177
178 (defun pcomplete/erc-mode/SOUND ()
179 (while (pcomplete-here (pcomplete-entries))))
180
181 (defun pcomplete/erc-mode/TOPIC ()
182 (pcomplete-here (pcomplete-erc-channels)))
183
184 (defun pcomplete/erc-mode/WHOIS ()
185 (while (pcomplete-here (pcomplete-erc-nicks))))
186
187 (defun pcomplete/erc-mode/UNIGNORE ()
188 (pcomplete-here (erc-with-server-buffer erc-ignore-list)))
189
190 ;;; Functions that provide possible completions.
191
192 (defun pcomplete-erc-commands ()
193 "Returns a list of strings of the defined user commands."
194 (let ((case-fold-search nil))
195 (mapcar (lambda (x)
196 (concat "/" (downcase (substring (symbol-name x) 8))))
197 (apropos-internal "erc-cmd-[A-Z]+"))))
198
199 (defun pcomplete-erc-ops ()
200 "Returns a list of nicks with ops."
201 (let (ops)
202 (maphash (lambda (nick cdata)
203 (if (and (cdr cdata)
204 (erc-channel-user-op (cdr cdata)))
205 (setq ops (cons nick ops))))
206 erc-channel-users)
207 ops))
208
209 (defun pcomplete-erc-not-ops ()
210 "Returns a list of nicks without ops."
211 (let (not-ops)
212 (maphash (lambda (nick cdata)
213 (if (and (cdr cdata)
214 (not (erc-channel-user-op (cdr cdata))))
215 (setq not-ops (cons nick not-ops))))
216 erc-channel-users)
217 not-ops))
218
219
220 (defun pcomplete-erc-nicks (&optional postfix ignore-self)
221 "Returns a list of nicks in the current channel.
222 Optional argument POSTFIX is something to append to the nickname.
223 If optional argument IGNORE-SELF is non-nil, don't return the current nick."
224 (let ((users (if erc-pcomplete-order-nickname-completions
225 (erc-sort-channel-users-by-activity
226 (erc-get-channel-user-list))
227 (erc-get-channel-user-list)))
228 (nicks nil))
229 (dolist (user users)
230 (unless (and ignore-self
231 (string= (erc-server-user-nickname (car user))
232 (erc-current-nick)))
233 (setq nicks (cons (concat (erc-server-user-nickname (car user))
234 postfix)
235 nicks))))
236 (nreverse nicks)))
237
238 (defun pcomplete-erc-all-nicks (&optional postfix)
239 "Returns a list of all nicks on the current server."
240 (let (nicks)
241 (erc-with-server-buffer
242 (maphash (lambda (nick user)
243 (setq nicks (cons (concat nick postfix) nicks)))
244 erc-server-users))
245 nicks))
246
247 (defun pcomplete-erc-channels ()
248 "Returns a list of channels associated with the current server."
249 (mapcar (lambda (buf) (with-current-buffer buf (erc-default-target)))
250 (erc-channel-list erc-server-process)))
251
252 ;;; Functions for parsing
253
254 (defun pcomplete-erc-command-name ()
255 "Returns the command name of the first argument."
256 (if (eq (elt (pcomplete-arg 'first) 0) ?/)
257 (upcase (substring (pcomplete-arg 'first) 1))
258 "SAY"))
259
260 (defun pcomplete-erc-parse-arguments ()
261 "Returns a list of parsed whitespace-separated arguments.
262 These are the words from the beginning of the line after the prompt
263 up to where point is right now."
264 (let* ((start erc-input-marker)
265 (end (point))
266 args beginnings)
267 (save-excursion
268 (if (< (skip-chars-backward " \t\n" start) 0)
269 (setq args '("")
270 beginnings (list end)))
271 (setq end (point))
272 (while (< (skip-chars-backward "^ \t\n" start) 0)
273 (setq beginnings (cons (point) beginnings)
274 args (cons (buffer-substring-no-properties
275 (point) end)
276 args))
277 (skip-chars-backward " \t\n" start)
278 (setq end (point))))
279 (cons args beginnings)))
280
281 (provide 'erc-pcomplete)
282
283 ;;; erc-pcomplete.el ends here
284 ;;
285 ;; Local Variables:
286 ;; indent-tabs-mode: nil
287 ;; End:
288