* lisp/emacs-lisp/seq.el: Documentation improvements
[emacs.git] / lisp / erc / erc-pcomplete.el
blobf1e912c3d7c899dfae3535b345db02b66bd59ca6
1 ;;; erc-pcomplete.el --- Provides programmable completion for ERC
3 ;; Copyright (C) 2002-2004, 2006-2015 Free Software Foundation, Inc.
5 ;; Author: Sacha Chua <sacha@free.net.ph>
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Keywords: comm, convenience
8 ;; URL: http://www.emacswiki.org/cgi-bin/wiki.pl?ErcCompletion
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
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.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
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.
31 ;; To use, (require 'erc-auto) or (require 'erc-pcomplete), then
32 ;; (erc-pcomplete-mode 1)
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'.
38 ;; See CREDITS for other contributors.
40 ;;; Code:
42 (require 'pcomplete)
43 (require 'erc)
44 (require 'erc-compat)
45 (require 'time-date)
47 (defgroup erc-pcomplete nil
48 "Programmable completion for ERC"
49 :group 'erc)
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)
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)
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)))
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)))))))
81 (defun erc-pcomplete ()
82 "Complete the nick before point."
83 (interactive)
84 (when (> (point) (erc-beg-of-input-line))
85 (setq this-command 'pcomplete)
86 (call-interactively 'pcomplete)
87 t))
89 ;;; Setup function
91 (defun pcomplete-erc-setup ()
92 "Setup `erc-mode' to use pcomplete."
93 (set (make-local-variable 'pcomplete-ignore-case)
95 (set (make-local-variable 'pcomplete-use-paring)
96 nil)
97 (set (make-local-variable 'pcomplete-parse-arguments-function)
98 'pcomplete-erc-parse-arguments)
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)))))
106 ;;; Programmable completion logic
108 (defun pcomplete/erc-mode/complete-command ()
109 (pcomplete-here
110 (append
111 (pcomplete-erc-commands)
112 (pcomplete-erc-nicks erc-pcomplete-nick-postfix t))))
114 (defvar erc-pcomplete-ctcp-commands
115 '("ACTION" "CLIENTINFO" "ECHO" "FINGER" "PING" "TIME" "USERINFO" "VERSION"))
117 (defun pcomplete/erc-mode/CTCP ()
118 (pcomplete-here (pcomplete-erc-nicks))
119 (pcomplete-here erc-pcomplete-ctcp-commands))
121 (defun pcomplete/erc-mode/CLEARTOPIC ()
122 (pcomplete-here (pcomplete-erc-channels)))
124 (defun pcomplete/erc-mode/DEOP ()
125 (while (pcomplete-here (pcomplete-erc-ops))))
127 (defun pcomplete/erc-mode/DESCRIBE ()
128 (pcomplete-here (pcomplete-erc-nicks)))
130 (defun pcomplete/erc-mode/IDLE ()
131 (while (pcomplete-here (pcomplete-erc-nicks))))
133 (defun pcomplete/erc-mode/KICK ()
134 (pcomplete-here (pcomplete-erc-channels))
135 (pcomplete-here (pcomplete-erc-nicks)))
137 (defun pcomplete/erc-mode/LOAD ()
138 (pcomplete-here (pcomplete-entries)))
140 (defun pcomplete/erc-mode/MODE ()
141 (pcomplete-here (pcomplete-erc-channels))
142 (while (pcomplete-here (pcomplete-erc-nicks))))
144 (defun pcomplete/erc-mode/ME ()
145 (while (pcomplete-here (pcomplete-erc-nicks))))
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))))
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))))
157 (defun pcomplete/erc-mode/NAMES ()
158 (while (pcomplete-here (pcomplete-erc-channels))))
160 (defalias 'pcomplete/erc-mode/NOTICE 'pcomplete/erc-mode/MSG)
162 (defun pcomplete/erc-mode/OP ()
163 (while (pcomplete-here (pcomplete-erc-not-ops))))
165 (defun pcomplete/erc-mode/PART ()
166 (pcomplete-here (pcomplete-erc-channels)))
168 (defalias 'pcomplete/erc-mode/LEAVE 'pcomplete/erc-mode/PART)
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)))
176 (defun pcomplete/erc-mode/SOUND ()
177 (while (pcomplete-here (pcomplete-entries))))
179 (defun pcomplete/erc-mode/TOPIC ()
180 (pcomplete-here (pcomplete-erc-channels)))
182 (defun pcomplete/erc-mode/WHOIS ()
183 (while (pcomplete-here (pcomplete-erc-nicks))))
185 (defun pcomplete/erc-mode/UNIGNORE ()
186 (pcomplete-here (erc-with-server-buffer erc-ignore-list)))
188 ;;; Functions that provide possible completions.
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]+"))))
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))
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))
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)))
236 (defun pcomplete-erc-all-nicks (&optional postfix)
237 "Returns a list of all nicks on the current server."
238 (let (nicks)
239 (erc-with-server-buffer
240 (maphash (lambda (nick user)
241 (setq nicks (cons (concat nick postfix) nicks)))
242 erc-server-users))
243 nicks))
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)))
250 ;;; Functions for parsing
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"))
258 (defun pcomplete-erc-parse-arguments ()
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)))
279 (provide 'erc-pcomplete)
281 ;;; erc-pcomplete.el ends here
283 ;; Local Variables:
284 ;; indent-tabs-mode: nil
285 ;; End: