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