1 ;;; erc-pcomplete.el --- Provides programmable completion for ERC
3 ;; Copyright (C) 2002-2004, 2006-2012 Free Software Foundation, Inc.
5 ;; Author: Sacha Chua <sacha@free.net.ph>
6 ;; Keywords: comm, convenience
7 ;; URL: http://www.emacswiki.org/cgi-bin/wiki.pl?ErcCompletion
9 ;; This file is part of GNU Emacs.
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.
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.
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/>.
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.
30 ;; To use, (require 'erc-auto) or (require 'erc-pcomplete), then
31 ;; (erc-pcomplete-mode 1)
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'.
37 ;; See CREDITS for other contributors.
45 (eval-when-compile (require 'cl
))
47 (defgroup erc-pcomplete nil
48 "Programmable completion for 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."
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."
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."
84 (when (> (point) (erc-beg-of-input-line))
85 (let ((last-command (if (eq last-command
'erc-complete-word
)
88 (call-interactively 'pcomplete
))
93 (defun pcomplete-erc-setup ()
94 "Setup `erc-mode' to use pcomplete."
95 (set (make-local-variable 'pcomplete-ignore-case
)
97 (set (make-local-variable 'pcomplete-use-paring
)
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)))))
108 ;;; Programmable completion logic
110 (defun pcomplete/erc-mode
/complete-command
()
113 (pcomplete-erc-commands)
114 (pcomplete-erc-nicks erc-pcomplete-nick-postfix t
))))
116 (defvar erc-pcomplete-ctcp-commands
117 '("ACTION" "CLIENTINFO" "ECHO" "FINGER" "PING" "TIME" "USERINFO" "VERSION"))
119 (defun pcomplete/erc-mode
/CTCP
()
120 (pcomplete-here (pcomplete-erc-nicks))
121 (pcomplete-here erc-pcomplete-ctcp-commands
))
123 (defun pcomplete/erc-mode
/CLEARTOPIC
()
124 (pcomplete-here (pcomplete-erc-channels)))
126 (defun pcomplete/erc-mode
/DEOP
()
127 (while (pcomplete-here (pcomplete-erc-ops))))
129 (defun pcomplete/erc-mode
/DESCRIBE
()
130 (pcomplete-here (pcomplete-erc-nicks)))
132 (defun pcomplete/erc-mode
/IDLE
()
133 (while (pcomplete-here (pcomplete-erc-nicks))))
135 (defun pcomplete/erc-mode
/KICK
()
136 (pcomplete-here (pcomplete-erc-channels))
137 (pcomplete-here (pcomplete-erc-nicks)))
139 (defun pcomplete/erc-mode
/LOAD
()
140 (pcomplete-here (pcomplete-entries)))
142 (defun pcomplete/erc-mode
/MODE
()
143 (pcomplete-here (pcomplete-erc-channels))
144 (while (pcomplete-here (pcomplete-erc-nicks))))
146 (defun pcomplete/erc-mode
/ME
()
147 (while (pcomplete-here (pcomplete-erc-nicks))))
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))))
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))))
159 (defun pcomplete/erc-mode
/NAMES
()
160 (while (pcomplete-here (pcomplete-erc-channels))))
162 (defalias 'pcomplete
/erc-mode
/NOTICE
'pcomplete
/erc-mode
/MSG
)
164 (defun pcomplete/erc-mode
/OP
()
165 (while (pcomplete-here (pcomplete-erc-not-ops))))
167 (defun pcomplete/erc-mode
/PART
()
168 (pcomplete-here (pcomplete-erc-channels)))
170 (defalias 'pcomplete
/erc-mode
/LEAVE
'pcomplete
/erc-mode
/PART
)
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)))
178 (defun pcomplete/erc-mode
/SOUND
()
179 (while (pcomplete-here (pcomplete-entries))))
181 (defun pcomplete/erc-mode
/TOPIC
()
182 (pcomplete-here (pcomplete-erc-channels)))
184 (defun pcomplete/erc-mode
/WHOIS
()
185 (while (pcomplete-here (pcomplete-erc-nicks))))
187 (defun pcomplete/erc-mode
/UNIGNORE
()
188 (pcomplete-here (erc-with-server-buffer erc-ignore-list
)))
190 ;;; Functions that provide possible completions.
192 (defun pcomplete-erc-commands ()
193 "Returns a list of strings of the defined user commands."
194 (let ((case-fold-search nil
))
196 (concat "/" (downcase (substring (symbol-name x
) 8))))
197 (apropos-internal "erc-cmd-[A-Z]+"))))
199 (defun pcomplete-erc-ops ()
200 "Returns a list of nicks with ops."
202 (maphash (lambda (nick cdata
)
204 (erc-channel-user-op (cdr cdata
)))
205 (setq ops
(cons nick ops
))))
209 (defun pcomplete-erc-not-ops ()
210 "Returns a list of nicks without ops."
212 (maphash (lambda (nick cdata
)
214 (not (erc-channel-user-op (cdr cdata
))))
215 (setq not-ops
(cons nick not-ops
))))
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)))
230 (unless (and ignore-self
231 (string= (erc-server-user-nickname (car user
))
233 (setq nicks
(cons (concat (erc-server-user-nickname (car user
))
238 (defun pcomplete-erc-all-nicks (&optional postfix
)
239 "Returns a list of all nicks on the current server."
241 (erc-with-server-buffer
242 (maphash (lambda (nick user
)
243 (setq nicks
(cons (concat nick postfix
) nicks
)))
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
)))
252 ;;; Functions for parsing
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))
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
)
268 (if (< (skip-chars-backward " \t\n" start
) 0)
270 beginnings
(list end
)))
272 (while (< (skip-chars-backward "^ \t\n" start
) 0)
273 (setq beginnings
(cons (point) beginnings
)
274 args
(cons (buffer-substring-no-properties
277 (skip-chars-backward " \t\n" start
)
279 (cons args beginnings
)))
281 (provide 'erc-pcomplete
)
283 ;;; erc-pcomplete.el ends here
286 ;; indent-tabs-mode: nil