Commit | Line | Data |
---|---|---|
597993cf MB |
1 | ;;; erc-pcomplete.el --- Provides programmable completion for ERC |
2 | ||
ba318903 | 3 | ;; Copyright (C) 2002-2004, 2006-2014 Free Software Foundation, Inc. |
597993cf MB |
4 | |
5 | ;; Author: Sacha Chua <sacha@free.net.ph> | |
34dc21db | 6 | ;; Maintainer: emacs-devel@gnu.org |
597993cf MB |
7 | ;; Keywords: comm, convenience |
8 | ;; URL: http://www.emacswiki.org/cgi-bin/wiki.pl?ErcCompletion | |
9 | ||
10 | ;; This file is part of GNU Emacs. | |
11 | ||
4ee57b2a | 12 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
597993cf | 13 | ;; it under the terms of the GNU General Public License as published by |
4ee57b2a GM |
14 | ;; the Free Software Foundation, either version 3 of the License, or |
15 | ;; (at your option) any later version. | |
597993cf MB |
16 | |
17 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ;; GNU General Public License for more details. | |
21 | ||
22 | ;; You should have received a copy of the GNU General Public License | |
4ee57b2a | 23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
597993cf MB |
24 | |
25 | ;;; Commentary: | |
26 | ||
27 | ;; This file replaces erc-complete.el. It provides nick completion | |
28 | ;; for ERC based on pcomplete. If you do not have pcomplete, you may | |
29 | ;; try to use erc-complete.el. | |
30 | ;; | |
31 | ;; To use, (require 'erc-auto) or (require 'erc-pcomplete), then | |
32 | ;; (erc-pcomplete-mode 1) | |
33 | ;; | |
34 | ;; If you want nickname completions ordered such that the most recent | |
35 | ;; speakers are listed first, set | |
36 | ;; `erc-pcomplete-order-nickname-completions' to `t'. | |
37 | ;; | |
38 | ;; See CREDITS for other contributors. | |
39 | ;; | |
40 | ;;; Code: | |
41 | ||
42 | (require 'pcomplete) | |
43 | (require 'erc) | |
44 | (require 'erc-compat) | |
45 | (require 'time-date) | |
597993cf MB |
46 | |
47 | (defgroup erc-pcomplete nil | |
48 | "Programmable completion for ERC" | |
49 | :group 'erc) | |
50 | ||
38b3645a | 51 | (defcustom erc-pcomplete-nick-postfix ":" |
fb7ada5f | 52 | "When `pcomplete' is used in the first word after the prompt, |
597993cf MB |
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) | |
d4aa710a | 67 | (add-hook 'erc-complete-functions 'erc-pcompletions-at-point) |
597993cf MB |
68 | (erc-buffer-list #'pcomplete-erc-setup)) |
69 | ((remove-hook 'erc-mode-hook 'pcomplete-erc-setup) | |
d4aa710a SM |
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)) | |
0ff8e1ba SM |
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))))))) | |
597993cf MB |
80 | |
81 | (defun erc-pcomplete () | |
82 | "Complete the nick before point." | |
83 | (interactive) | |
84 | (when (> (point) (erc-beg-of-input-line)) | |
7f851ea4 SM |
85 | (setq this-command 'pcomplete) |
86 | (call-interactively 'pcomplete) | |
597993cf MB |
87 | t)) |
88 | ||
89 | ;;; Setup function | |
90 | ||
91 | (defun pcomplete-erc-setup () | |
92 | "Setup `erc-mode' to use pcomplete." | |
93 | (set (make-local-variable 'pcomplete-ignore-case) | |
94 | t) | |
95 | (set (make-local-variable 'pcomplete-use-paring) | |
96 | nil) | |
597993cf | 97 | (set (make-local-variable 'pcomplete-parse-arguments-function) |
0ff8e1ba | 98 | 'pcomplete-erc-parse-arguments) |
597993cf MB |
99 | (set (make-local-variable 'pcomplete-command-completion-function) |
100 | 'pcomplete/erc-mode/complete-command) | |
101 | (set (make-local-variable 'pcomplete-command-name-function) | |
102 | 'pcomplete-erc-command-name) | |
103 | (set (make-local-variable 'pcomplete-default-completion-function) | |
104 | (lambda () (pcomplete-here (pcomplete-erc-nicks))))) | |
105 | ||
106 | ;;; Programmable completion logic | |
107 | ||
108 | (defun pcomplete/erc-mode/complete-command () | |
109 | (pcomplete-here | |
110 | (append | |
111 | (pcomplete-erc-commands) | |
0b6bb130 | 112 | (pcomplete-erc-nicks erc-pcomplete-nick-postfix t)))) |
597993cf MB |
113 | |
114 | (defvar erc-pcomplete-ctcp-commands | |
115 | '("ACTION" "CLIENTINFO" "ECHO" "FINGER" "PING" "TIME" "USERINFO" "VERSION")) | |
116 | ||
117 | (defun pcomplete/erc-mode/CTCP () | |
118 | (pcomplete-here (pcomplete-erc-nicks)) | |
119 | (pcomplete-here erc-pcomplete-ctcp-commands)) | |
120 | ||
121 | (defun pcomplete/erc-mode/CLEARTOPIC () | |
122 | (pcomplete-here (pcomplete-erc-channels))) | |
123 | ||
124 | (defun pcomplete/erc-mode/DEOP () | |
125 | (while (pcomplete-here (pcomplete-erc-ops)))) | |
126 | ||
127 | (defun pcomplete/erc-mode/DESCRIBE () | |
128 | (pcomplete-here (pcomplete-erc-nicks))) | |
129 | ||
130 | (defun pcomplete/erc-mode/IDLE () | |
131 | (while (pcomplete-here (pcomplete-erc-nicks)))) | |
132 | ||
133 | (defun pcomplete/erc-mode/KICK () | |
134 | (pcomplete-here (pcomplete-erc-channels)) | |
135 | (pcomplete-here (pcomplete-erc-nicks))) | |
136 | ||
137 | (defun pcomplete/erc-mode/LOAD () | |
138 | (pcomplete-here (pcomplete-entries))) | |
139 | ||
140 | (defun pcomplete/erc-mode/MODE () | |
141 | (pcomplete-here (pcomplete-erc-channels)) | |
142 | (while (pcomplete-here (pcomplete-erc-nicks)))) | |
143 | ||
144 | (defun pcomplete/erc-mode/ME () | |
145 | (while (pcomplete-here (pcomplete-erc-nicks)))) | |
146 | ||
147 | (defun pcomplete/erc-mode/SAY () | |
148 | (pcomplete-here (pcomplete-erc-nicks)) | |
149 | (pcomplete-here (pcomplete-erc-nicks)) | |
150 | (while (pcomplete-here (pcomplete-erc-nicks)))) | |
151 | ||
152 | (defun pcomplete/erc-mode/MSG () | |
153 | (pcomplete-here (append (pcomplete-erc-all-nicks) | |
154 | (pcomplete-erc-channels))) | |
155 | (while (pcomplete-here (pcomplete-erc-nicks)))) | |
156 | ||
157 | (defun pcomplete/erc-mode/NAMES () | |
158 | (while (pcomplete-here (pcomplete-erc-channels)))) | |
159 | ||
160 | (defalias 'pcomplete/erc-mode/NOTICE 'pcomplete/erc-mode/MSG) | |
161 | ||
162 | (defun pcomplete/erc-mode/OP () | |
163 | (while (pcomplete-here (pcomplete-erc-not-ops)))) | |
164 | ||
165 | (defun pcomplete/erc-mode/PART () | |
166 | (pcomplete-here (pcomplete-erc-channels))) | |
167 | ||
168 | (defalias 'pcomplete/erc-mode/LEAVE 'pcomplete/erc-mode/PART) | |
169 | ||
170 | (defun pcomplete/erc-mode/QUERY () | |
171 | (pcomplete-here (append (pcomplete-erc-all-nicks) | |
172 | (pcomplete-erc-channels))) | |
173 | (while (pcomplete-here (pcomplete-erc-nicks))) | |
174 | ) | |
175 | ||
176 | (defun pcomplete/erc-mode/SOUND () | |
177 | (while (pcomplete-here (pcomplete-entries)))) | |
178 | ||
179 | (defun pcomplete/erc-mode/TOPIC () | |
180 | (pcomplete-here (pcomplete-erc-channels))) | |
181 | ||
182 | (defun pcomplete/erc-mode/WHOIS () | |
183 | (while (pcomplete-here (pcomplete-erc-nicks)))) | |
184 | ||
185 | (defun pcomplete/erc-mode/UNIGNORE () | |
ff59d266 | 186 | (pcomplete-here (erc-with-server-buffer erc-ignore-list))) |
597993cf MB |
187 | |
188 | ;;; Functions that provide possible completions. | |
189 | ||
190 | (defun pcomplete-erc-commands () | |
191 | "Returns a list of strings of the defined user commands." | |
192 | (let ((case-fold-search nil)) | |
193 | (mapcar (lambda (x) | |
194 | (concat "/" (downcase (substring (symbol-name x) 8)))) | |
195 | (apropos-internal "erc-cmd-[A-Z]+")))) | |
196 | ||
197 | (defun pcomplete-erc-ops () | |
198 | "Returns a list of nicks with ops." | |
199 | (let (ops) | |
200 | (maphash (lambda (nick cdata) | |
201 | (if (and (cdr cdata) | |
202 | (erc-channel-user-op (cdr cdata))) | |
203 | (setq ops (cons nick ops)))) | |
204 | erc-channel-users) | |
205 | ops)) | |
206 | ||
207 | (defun pcomplete-erc-not-ops () | |
208 | "Returns a list of nicks without ops." | |
209 | (let (not-ops) | |
210 | (maphash (lambda (nick cdata) | |
211 | (if (and (cdr cdata) | |
212 | (not (erc-channel-user-op (cdr cdata)))) | |
213 | (setq not-ops (cons nick not-ops)))) | |
214 | erc-channel-users) | |
215 | not-ops)) | |
216 | ||
217 | ||
0b6bb130 MB |
218 | (defun pcomplete-erc-nicks (&optional postfix ignore-self) |
219 | "Returns a list of nicks in the current channel. | |
220 | Optional argument POSTFIX is something to append to the nickname. | |
221 | If optional argument IGNORE-SELF is non-nil, don't return the current nick." | |
222 | (let ((users (if erc-pcomplete-order-nickname-completions | |
223 | (erc-sort-channel-users-by-activity | |
224 | (erc-get-channel-user-list)) | |
225 | (erc-get-channel-user-list))) | |
226 | (nicks nil)) | |
227 | (dolist (user users) | |
228 | (unless (and ignore-self | |
229 | (string= (erc-server-user-nickname (car user)) | |
230 | (erc-current-nick))) | |
231 | (setq nicks (cons (concat (erc-server-user-nickname (car user)) | |
232 | postfix) | |
233 | nicks)))) | |
234 | (nreverse nicks))) | |
597993cf MB |
235 | |
236 | (defun pcomplete-erc-all-nicks (&optional postfix) | |
237 | "Returns a list of all nicks on the current server." | |
238 | (let (nicks) | |
ff59d266 | 239 | (erc-with-server-buffer |
597993cf MB |
240 | (maphash (lambda (nick user) |
241 | (setq nicks (cons (concat nick postfix) nicks))) | |
242 | erc-server-users)) | |
243 | nicks)) | |
244 | ||
245 | (defun pcomplete-erc-channels () | |
246 | "Returns a list of channels associated with the current server." | |
247 | (mapcar (lambda (buf) (with-current-buffer buf (erc-default-target))) | |
248 | (erc-channel-list erc-server-process))) | |
249 | ||
250 | ;;; Functions for parsing | |
251 | ||
252 | (defun pcomplete-erc-command-name () | |
253 | "Returns the command name of the first argument." | |
254 | (if (eq (elt (pcomplete-arg 'first) 0) ?/) | |
255 | (upcase (substring (pcomplete-arg 'first) 1)) | |
256 | "SAY")) | |
257 | ||
0ff8e1ba | 258 | (defun pcomplete-erc-parse-arguments () |
597993cf MB |
259 | "Returns a list of parsed whitespace-separated arguments. |
260 | These are the words from the beginning of the line after the prompt | |
261 | up to where point is right now." | |
262 | (let* ((start erc-input-marker) | |
263 | (end (point)) | |
264 | args beginnings) | |
265 | (save-excursion | |
266 | (if (< (skip-chars-backward " \t\n" start) 0) | |
267 | (setq args '("") | |
268 | beginnings (list end))) | |
269 | (setq end (point)) | |
270 | (while (< (skip-chars-backward "^ \t\n" start) 0) | |
271 | (setq beginnings (cons (point) beginnings) | |
272 | args (cons (buffer-substring-no-properties | |
273 | (point) end) | |
274 | args)) | |
275 | (skip-chars-backward " \t\n" start) | |
276 | (setq end (point)))) | |
277 | (cons args beginnings))) | |
278 | ||
279 | (provide 'erc-pcomplete) | |
280 | ||
281 | ;;; erc-pcomplete.el ends here | |
282 | ;; | |
283 | ;; Local Variables: | |
284 | ;; indent-tabs-mode: nil | |
285 | ;; End: | |
286 |