1 ;;; erc-pcomplete.el --- Provides programmable completion for ERC
3 ;; Copyright (C) 2002, 2003, 2004, 2006, 2007, 2008, 2009, 2010 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-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
)))
72 (defun erc-pcomplete ()
73 "Complete the nick before point."
75 (when (> (point) (erc-beg-of-input-line))
76 (let ((last-command (if (eq last-command
'erc-complete-word
)
79 (call-interactively 'pcomplete
))
84 (defun pcomplete-erc-setup ()
85 "Setup `erc-mode' to use pcomplete."
86 (set (make-local-variable 'pcomplete-ignore-case
)
88 (set (make-local-variable 'pcomplete-use-paring
)
90 (set (make-local-variable 'pcomplete-suffix-list
)
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)))))
101 ;;; Programmable completion logic
103 (defun pcomplete/erc-mode
/complete-command
()
106 (pcomplete-erc-commands)
107 (pcomplete-erc-nicks erc-pcomplete-nick-postfix t
))))
109 (defvar erc-pcomplete-ctcp-commands
110 '("ACTION" "CLIENTINFO" "ECHO" "FINGER" "PING" "TIME" "USERINFO" "VERSION"))
112 (defun pcomplete/erc-mode
/CTCP
()
113 (pcomplete-here (pcomplete-erc-nicks))
114 (pcomplete-here erc-pcomplete-ctcp-commands
))
116 (defun pcomplete/erc-mode
/CLEARTOPIC
()
117 (pcomplete-here (pcomplete-erc-channels)))
119 (defun pcomplete/erc-mode
/DEOP
()
120 (while (pcomplete-here (pcomplete-erc-ops))))
122 (defun pcomplete/erc-mode
/DESCRIBE
()
123 (pcomplete-here (pcomplete-erc-nicks)))
125 (defun pcomplete/erc-mode
/IDLE
()
126 (while (pcomplete-here (pcomplete-erc-nicks))))
128 (defun pcomplete/erc-mode
/KICK
()
129 (pcomplete-here (pcomplete-erc-channels))
130 (pcomplete-here (pcomplete-erc-nicks)))
132 (defun pcomplete/erc-mode
/LOAD
()
133 (pcomplete-here (pcomplete-entries)))
135 (defun pcomplete/erc-mode
/MODE
()
136 (pcomplete-here (pcomplete-erc-channels))
137 (while (pcomplete-here (pcomplete-erc-nicks))))
139 (defun pcomplete/erc-mode
/ME
()
140 (while (pcomplete-here (pcomplete-erc-nicks))))
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))))
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))))
152 (defun pcomplete/erc-mode
/NAMES
()
153 (while (pcomplete-here (pcomplete-erc-channels))))
155 (defalias 'pcomplete
/erc-mode
/NOTICE
'pcomplete
/erc-mode
/MSG
)
157 (defun pcomplete/erc-mode
/OP
()
158 (while (pcomplete-here (pcomplete-erc-not-ops))))
160 (defun pcomplete/erc-mode
/PART
()
161 (pcomplete-here (pcomplete-erc-channels)))
163 (defalias 'pcomplete
/erc-mode
/LEAVE
'pcomplete
/erc-mode
/PART
)
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)))
171 (defun pcomplete/erc-mode
/SOUND
()
172 (while (pcomplete-here (pcomplete-entries))))
174 (defun pcomplete/erc-mode
/TOPIC
()
175 (pcomplete-here (pcomplete-erc-channels)))
177 (defun pcomplete/erc-mode
/WHOIS
()
178 (while (pcomplete-here (pcomplete-erc-nicks))))
180 (defun pcomplete/erc-mode
/UNIGNORE
()
181 (pcomplete-here (erc-with-server-buffer erc-ignore-list
)))
183 ;;; Functions that provide possible completions.
185 (defun pcomplete-erc-commands ()
186 "Returns a list of strings of the defined user commands."
187 (let ((case-fold-search nil
))
189 (concat "/" (downcase (substring (symbol-name x
) 8))))
190 (apropos-internal "erc-cmd-[A-Z]+"))))
192 (defun pcomplete-erc-ops ()
193 "Returns a list of nicks with ops."
195 (maphash (lambda (nick cdata
)
197 (erc-channel-user-op (cdr cdata
)))
198 (setq ops
(cons nick ops
))))
202 (defun pcomplete-erc-not-ops ()
203 "Returns a list of nicks without ops."
205 (maphash (lambda (nick cdata
)
207 (not (erc-channel-user-op (cdr cdata
))))
208 (setq not-ops
(cons nick not-ops
))))
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)))
223 (unless (and ignore-self
224 (string= (erc-server-user-nickname (car user
))
226 (setq nicks
(cons (concat (erc-server-user-nickname (car user
))
231 (defun pcomplete-erc-all-nicks (&optional postfix
)
232 "Returns a list of all nicks on the current server."
234 (erc-with-server-buffer
235 (maphash (lambda (nick user
)
236 (setq nicks
(cons (concat nick postfix
) nicks
)))
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
)))
245 ;;; Functions for parsing
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))
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
)
261 (if (< (skip-chars-backward " \t\n" start
) 0)
263 beginnings
(list end
)))
265 (while (< (skip-chars-backward "^ \t\n" start
) 0)
266 (setq beginnings
(cons (point) beginnings
)
267 args
(cons (buffer-substring-no-properties
270 (skip-chars-backward " \t\n" start
)
272 (cons args beginnings
)))
274 (provide 'erc-pcomplete
)
276 ;;; erc-pcomplete.el ends here
279 ;; indent-tabs-mode: nil
282 ;; arch-tag: 32a7703b-be87-45a4-82f3-9eed5a628911