Auto-commit of loaddefs files.
[emacs.git] / lisp / erc / erc-pcomplete.el
blob212ad50a6396698bc3fb60e7596fae1267220f2e
1 ;;; erc-pcomplete.el --- Provides programmable completion for ERC
3 ;; Copyright (C) 2002-2004, 2006-2013 Free Software Foundation, Inc.
5 ;; Author: Sacha Chua <sacha@free.net.ph>
6 ;; Maintainer: FSF
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)
46 (eval-when-compile (require 'cl))
48 (defgroup erc-pcomplete nil
49 "Programmable completion for ERC"
50 :group 'erc)
52 (defcustom erc-pcomplete-nick-postfix ":"
53 "When `pcomplete' is used in the first word after the prompt,
54 add this string to nicks completed."
55 :group 'erc-pcomplete
56 :type 'string)
58 (defcustom erc-pcomplete-order-nickname-completions t
59 "If t, channel nickname completions will be ordered such that
60 the most recent speakers are listed first."
61 :group 'erc-pcomplete
62 :type 'boolean)
64 ;;;###autoload (autoload 'erc-completion-mode "erc-pcomplete" nil t)
65 (define-erc-module pcomplete Completion
66 "In ERC Completion mode, the TAB key does completion whenever possible."
67 ((add-hook 'erc-mode-hook 'pcomplete-erc-setup)
68 (add-hook 'erc-complete-functions 'erc-pcompletions-at-point)
69 (erc-buffer-list #'pcomplete-erc-setup))
70 ((remove-hook 'erc-mode-hook 'pcomplete-erc-setup)
71 (remove-hook 'erc-complete-functions 'erc-pcompletions-at-point)))
73 (defun erc-pcompletions-at-point ()
74 "ERC completion data from pcomplete.
75 for use on `completion-at-point-function'."
76 (when (> (point) (erc-beg-of-input-line))
77 (or (let ((pcomplete-default-completion-function #'ignore))
78 (pcomplete-completions-at-point))
79 (let ((c (pcomplete-completions-at-point)))
80 (if c (nconc c '(:exclusive no)))))))
82 (defun erc-pcomplete ()
83 "Complete the nick before point."
84 (interactive)
85 (when (> (point) (erc-beg-of-input-line))
86 (let ((last-command (if (eq last-command 'erc-complete-word)
87 'pcomplete
88 last-command)))
89 (call-interactively 'pcomplete))
90 t))
92 ;;; Setup function
94 (defun pcomplete-erc-setup ()
95 "Setup `erc-mode' to use pcomplete."
96 (set (make-local-variable 'pcomplete-ignore-case)
98 (set (make-local-variable 'pcomplete-use-paring)
99 nil)
100 (set (make-local-variable 'pcomplete-parse-arguments-function)
101 'pcomplete-erc-parse-arguments)
102 (set (make-local-variable 'pcomplete-command-completion-function)
103 'pcomplete/erc-mode/complete-command)
104 (set (make-local-variable 'pcomplete-command-name-function)
105 'pcomplete-erc-command-name)
106 (set (make-local-variable 'pcomplete-default-completion-function)
107 (lambda () (pcomplete-here (pcomplete-erc-nicks)))))
109 ;;; Programmable completion logic
111 (defun pcomplete/erc-mode/complete-command ()
112 (pcomplete-here
113 (append
114 (pcomplete-erc-commands)
115 (pcomplete-erc-nicks erc-pcomplete-nick-postfix t))))
117 (defvar erc-pcomplete-ctcp-commands
118 '("ACTION" "CLIENTINFO" "ECHO" "FINGER" "PING" "TIME" "USERINFO" "VERSION"))
120 (defun pcomplete/erc-mode/CTCP ()
121 (pcomplete-here (pcomplete-erc-nicks))
122 (pcomplete-here erc-pcomplete-ctcp-commands))
124 (defun pcomplete/erc-mode/CLEARTOPIC ()
125 (pcomplete-here (pcomplete-erc-channels)))
127 (defun pcomplete/erc-mode/DEOP ()
128 (while (pcomplete-here (pcomplete-erc-ops))))
130 (defun pcomplete/erc-mode/DESCRIBE ()
131 (pcomplete-here (pcomplete-erc-nicks)))
133 (defun pcomplete/erc-mode/IDLE ()
134 (while (pcomplete-here (pcomplete-erc-nicks))))
136 (defun pcomplete/erc-mode/KICK ()
137 (pcomplete-here (pcomplete-erc-channels))
138 (pcomplete-here (pcomplete-erc-nicks)))
140 (defun pcomplete/erc-mode/LOAD ()
141 (pcomplete-here (pcomplete-entries)))
143 (defun pcomplete/erc-mode/MODE ()
144 (pcomplete-here (pcomplete-erc-channels))
145 (while (pcomplete-here (pcomplete-erc-nicks))))
147 (defun pcomplete/erc-mode/ME ()
148 (while (pcomplete-here (pcomplete-erc-nicks))))
150 (defun pcomplete/erc-mode/SAY ()
151 (pcomplete-here (pcomplete-erc-nicks))
152 (pcomplete-here (pcomplete-erc-nicks))
153 (while (pcomplete-here (pcomplete-erc-nicks))))
155 (defun pcomplete/erc-mode/MSG ()
156 (pcomplete-here (append (pcomplete-erc-all-nicks)
157 (pcomplete-erc-channels)))
158 (while (pcomplete-here (pcomplete-erc-nicks))))
160 (defun pcomplete/erc-mode/NAMES ()
161 (while (pcomplete-here (pcomplete-erc-channels))))
163 (defalias 'pcomplete/erc-mode/NOTICE 'pcomplete/erc-mode/MSG)
165 (defun pcomplete/erc-mode/OP ()
166 (while (pcomplete-here (pcomplete-erc-not-ops))))
168 (defun pcomplete/erc-mode/PART ()
169 (pcomplete-here (pcomplete-erc-channels)))
171 (defalias 'pcomplete/erc-mode/LEAVE 'pcomplete/erc-mode/PART)
173 (defun pcomplete/erc-mode/QUERY ()
174 (pcomplete-here (append (pcomplete-erc-all-nicks)
175 (pcomplete-erc-channels)))
176 (while (pcomplete-here (pcomplete-erc-nicks)))
179 (defun pcomplete/erc-mode/SOUND ()
180 (while (pcomplete-here (pcomplete-entries))))
182 (defun pcomplete/erc-mode/TOPIC ()
183 (pcomplete-here (pcomplete-erc-channels)))
185 (defun pcomplete/erc-mode/WHOIS ()
186 (while (pcomplete-here (pcomplete-erc-nicks))))
188 (defun pcomplete/erc-mode/UNIGNORE ()
189 (pcomplete-here (erc-with-server-buffer erc-ignore-list)))
191 ;;; Functions that provide possible completions.
193 (defun pcomplete-erc-commands ()
194 "Returns a list of strings of the defined user commands."
195 (let ((case-fold-search nil))
196 (mapcar (lambda (x)
197 (concat "/" (downcase (substring (symbol-name x) 8))))
198 (apropos-internal "erc-cmd-[A-Z]+"))))
200 (defun pcomplete-erc-ops ()
201 "Returns a list of nicks with ops."
202 (let (ops)
203 (maphash (lambda (nick cdata)
204 (if (and (cdr cdata)
205 (erc-channel-user-op (cdr cdata)))
206 (setq ops (cons nick ops))))
207 erc-channel-users)
208 ops))
210 (defun pcomplete-erc-not-ops ()
211 "Returns a list of nicks without ops."
212 (let (not-ops)
213 (maphash (lambda (nick cdata)
214 (if (and (cdr cdata)
215 (not (erc-channel-user-op (cdr cdata))))
216 (setq not-ops (cons nick not-ops))))
217 erc-channel-users)
218 not-ops))
221 (defun pcomplete-erc-nicks (&optional postfix ignore-self)
222 "Returns a list of nicks in the current channel.
223 Optional argument POSTFIX is something to append to the nickname.
224 If optional argument IGNORE-SELF is non-nil, don't return the current nick."
225 (let ((users (if erc-pcomplete-order-nickname-completions
226 (erc-sort-channel-users-by-activity
227 (erc-get-channel-user-list))
228 (erc-get-channel-user-list)))
229 (nicks nil))
230 (dolist (user users)
231 (unless (and ignore-self
232 (string= (erc-server-user-nickname (car user))
233 (erc-current-nick)))
234 (setq nicks (cons (concat (erc-server-user-nickname (car user))
235 postfix)
236 nicks))))
237 (nreverse nicks)))
239 (defun pcomplete-erc-all-nicks (&optional postfix)
240 "Returns a list of all nicks on the current server."
241 (let (nicks)
242 (erc-with-server-buffer
243 (maphash (lambda (nick user)
244 (setq nicks (cons (concat nick postfix) nicks)))
245 erc-server-users))
246 nicks))
248 (defun pcomplete-erc-channels ()
249 "Returns a list of channels associated with the current server."
250 (mapcar (lambda (buf) (with-current-buffer buf (erc-default-target)))
251 (erc-channel-list erc-server-process)))
253 ;;; Functions for parsing
255 (defun pcomplete-erc-command-name ()
256 "Returns the command name of the first argument."
257 (if (eq (elt (pcomplete-arg 'first) 0) ?/)
258 (upcase (substring (pcomplete-arg 'first) 1))
259 "SAY"))
261 (defun pcomplete-erc-parse-arguments ()
262 "Returns a list of parsed whitespace-separated arguments.
263 These are the words from the beginning of the line after the prompt
264 up to where point is right now."
265 (let* ((start erc-input-marker)
266 (end (point))
267 args beginnings)
268 (save-excursion
269 (if (< (skip-chars-backward " \t\n" start) 0)
270 (setq args '("")
271 beginnings (list end)))
272 (setq end (point))
273 (while (< (skip-chars-backward "^ \t\n" start) 0)
274 (setq beginnings (cons (point) beginnings)
275 args (cons (buffer-substring-no-properties
276 (point) end)
277 args))
278 (skip-chars-backward " \t\n" start)
279 (setq end (point))))
280 (cons args beginnings)))
282 (provide 'erc-pcomplete)
284 ;;; erc-pcomplete.el ends here
286 ;; Local Variables:
287 ;; indent-tabs-mode: nil
288 ;; End: