1 ;;; erc-pcomplete.el --- Provides programmable completion for ERC
3 ;; Copyright (C) 2002-2004, 2006-2011 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 (pcomplete-completions-at-point)))
78 (defun erc-pcomplete ()
79 "Complete the nick before point."
81 (when (> (point) (erc-beg-of-input-line))
82 (let ((last-command (if (eq last-command
'erc-complete-word
)
85 (call-interactively 'pcomplete
))
90 (defun pcomplete-erc-setup ()
91 "Setup `erc-mode' to use pcomplete."
92 (set (make-local-variable 'pcomplete-ignore-case
)
94 (set (make-local-variable 'pcomplete-use-paring
)
96 (set (make-local-variable 'pcomplete-suffix-list
)
98 (set (make-local-variable 'pcomplete-parse-arguments-function
)
99 'pcomplete-parse-erc-arguments
)
100 (set (make-local-variable 'pcomplete-command-completion-function
)
101 'pcomplete
/erc-mode
/complete-command
)
102 (set (make-local-variable 'pcomplete-command-name-function
)
103 'pcomplete-erc-command-name
)
104 (set (make-local-variable 'pcomplete-default-completion-function
)
105 (lambda () (pcomplete-here (pcomplete-erc-nicks)))))
107 ;;; Programmable completion logic
109 (defun pcomplete/erc-mode
/complete-command
()
112 (pcomplete-erc-commands)
113 (pcomplete-erc-nicks erc-pcomplete-nick-postfix t
))))
115 (defvar erc-pcomplete-ctcp-commands
116 '("ACTION" "CLIENTINFO" "ECHO" "FINGER" "PING" "TIME" "USERINFO" "VERSION"))
118 (defun pcomplete/erc-mode
/CTCP
()
119 (pcomplete-here (pcomplete-erc-nicks))
120 (pcomplete-here erc-pcomplete-ctcp-commands
))
122 (defun pcomplete/erc-mode
/CLEARTOPIC
()
123 (pcomplete-here (pcomplete-erc-channels)))
125 (defun pcomplete/erc-mode
/DEOP
()
126 (while (pcomplete-here (pcomplete-erc-ops))))
128 (defun pcomplete/erc-mode
/DESCRIBE
()
129 (pcomplete-here (pcomplete-erc-nicks)))
131 (defun pcomplete/erc-mode
/IDLE
()
132 (while (pcomplete-here (pcomplete-erc-nicks))))
134 (defun pcomplete/erc-mode
/KICK
()
135 (pcomplete-here (pcomplete-erc-channels))
136 (pcomplete-here (pcomplete-erc-nicks)))
138 (defun pcomplete/erc-mode
/LOAD
()
139 (pcomplete-here (pcomplete-entries)))
141 (defun pcomplete/erc-mode
/MODE
()
142 (pcomplete-here (pcomplete-erc-channels))
143 (while (pcomplete-here (pcomplete-erc-nicks))))
145 (defun pcomplete/erc-mode
/ME
()
146 (while (pcomplete-here (pcomplete-erc-nicks))))
148 (defun pcomplete/erc-mode
/SAY
()
149 (pcomplete-here (pcomplete-erc-nicks))
150 (pcomplete-here (pcomplete-erc-nicks))
151 (while (pcomplete-here (pcomplete-erc-nicks))))
153 (defun pcomplete/erc-mode
/MSG
()
154 (pcomplete-here (append (pcomplete-erc-all-nicks)
155 (pcomplete-erc-channels)))
156 (while (pcomplete-here (pcomplete-erc-nicks))))
158 (defun pcomplete/erc-mode
/NAMES
()
159 (while (pcomplete-here (pcomplete-erc-channels))))
161 (defalias 'pcomplete
/erc-mode
/NOTICE
'pcomplete
/erc-mode
/MSG
)
163 (defun pcomplete/erc-mode
/OP
()
164 (while (pcomplete-here (pcomplete-erc-not-ops))))
166 (defun pcomplete/erc-mode
/PART
()
167 (pcomplete-here (pcomplete-erc-channels)))
169 (defalias 'pcomplete
/erc-mode
/LEAVE
'pcomplete
/erc-mode
/PART
)
171 (defun pcomplete/erc-mode
/QUERY
()
172 (pcomplete-here (append (pcomplete-erc-all-nicks)
173 (pcomplete-erc-channels)))
174 (while (pcomplete-here (pcomplete-erc-nicks)))
177 (defun pcomplete/erc-mode
/SOUND
()
178 (while (pcomplete-here (pcomplete-entries))))
180 (defun pcomplete/erc-mode
/TOPIC
()
181 (pcomplete-here (pcomplete-erc-channels)))
183 (defun pcomplete/erc-mode
/WHOIS
()
184 (while (pcomplete-here (pcomplete-erc-nicks))))
186 (defun pcomplete/erc-mode
/UNIGNORE
()
187 (pcomplete-here (erc-with-server-buffer erc-ignore-list
)))
189 ;;; Functions that provide possible completions.
191 (defun pcomplete-erc-commands ()
192 "Returns a list of strings of the defined user commands."
193 (let ((case-fold-search nil
))
195 (concat "/" (downcase (substring (symbol-name x
) 8))))
196 (apropos-internal "erc-cmd-[A-Z]+"))))
198 (defun pcomplete-erc-ops ()
199 "Returns a list of nicks with ops."
201 (maphash (lambda (nick cdata
)
203 (erc-channel-user-op (cdr cdata
)))
204 (setq ops
(cons nick ops
))))
208 (defun pcomplete-erc-not-ops ()
209 "Returns a list of nicks without ops."
211 (maphash (lambda (nick cdata
)
213 (not (erc-channel-user-op (cdr cdata
))))
214 (setq not-ops
(cons nick not-ops
))))
219 (defun pcomplete-erc-nicks (&optional postfix ignore-self
)
220 "Returns a list of nicks in the current channel.
221 Optional argument POSTFIX is something to append to the nickname.
222 If optional argument IGNORE-SELF is non-nil, don't return the current nick."
223 (let ((users (if erc-pcomplete-order-nickname-completions
224 (erc-sort-channel-users-by-activity
225 (erc-get-channel-user-list))
226 (erc-get-channel-user-list)))
229 (unless (and ignore-self
230 (string= (erc-server-user-nickname (car user
))
232 (setq nicks
(cons (concat (erc-server-user-nickname (car user
))
237 (defun pcomplete-erc-all-nicks (&optional postfix
)
238 "Returns a list of all nicks on the current server."
240 (erc-with-server-buffer
241 (maphash (lambda (nick user
)
242 (setq nicks
(cons (concat nick postfix
) nicks
)))
246 (defun pcomplete-erc-channels ()
247 "Returns a list of channels associated with the current server."
248 (mapcar (lambda (buf) (with-current-buffer buf
(erc-default-target)))
249 (erc-channel-list erc-server-process
)))
251 ;;; Functions for parsing
253 (defun pcomplete-erc-command-name ()
254 "Returns the command name of the first argument."
255 (if (eq (elt (pcomplete-arg 'first
) 0) ?
/)
256 (upcase (substring (pcomplete-arg 'first
) 1))
259 (defun pcomplete-parse-erc-arguments ()
260 "Returns a list of parsed whitespace-separated arguments.
261 These are the words from the beginning of the line after the prompt
262 up to where point is right now."
263 (let* ((start erc-input-marker
)
267 (if (< (skip-chars-backward " \t\n" start
) 0)
269 beginnings
(list end
)))
271 (while (< (skip-chars-backward "^ \t\n" start
) 0)
272 (setq beginnings
(cons (point) beginnings
)
273 args
(cons (buffer-substring-no-properties
276 (skip-chars-backward " \t\n" start
)
278 (cons args beginnings
)))
280 (provide 'erc-pcomplete
)
282 ;;; erc-pcomplete.el ends here
285 ;; indent-tabs-mode: nil