1 ;;; erc-pcomplete.el --- Provides programmable completion for ERC
3 ;; Copyright (C) 2002, 2003, 2004 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 2, or (at your option)
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; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
28 ;; This file replaces erc-complete.el. It provides nick completion
29 ;; for ERC based on pcomplete. If you do not have pcomplete, you may
30 ;; try to use erc-complete.el.
32 ;; To use, (require 'erc-auto) or (require 'erc-pcomplete), then
33 ;; (erc-pcomplete-mode 1)
35 ;; If you want nickname completions ordered such that the most recent
36 ;; speakers are listed first, set
37 ;; `erc-pcomplete-order-nickname-completions' to `t'.
39 ;; See CREDITS for other contributors.
47 (eval-when-compile (require 'cl))
49 (defconst erc-pcomplete-version "$Revision: 1.32.2.1 $"
50 "ERC pcomplete mode revision")
52 (defgroup erc-pcomplete nil
53 "Programmable completion for ERC"
56 (defcustom erc-pcomplete-nick-postfix ": "
57 "*When `pcomplete' is used in the first word after the prompt,
58 add this string to nicks completed."
62 (defcustom erc-pcomplete-order-nickname-completions t
63 "If t, channel nickname completions will be ordered such that
64 the most recent speakers are listed first."
68 ;;;###autoload (autoload 'erc-completion-mode "erc-pcomplete" nil t)
69 (define-erc-module pcomplete Completion
70 "In ERC Completion mode, the TAB key does completion whenever possible."
71 ((add-hook 'erc-mode-hook 'pcomplete-erc-setup)
72 (add-hook 'erc-complete-functions 'erc-pcomplete)
73 (erc-buffer-list #'pcomplete-erc-setup))
74 ((remove-hook 'erc-mode-hook 'pcomplete-erc-setup)
75 (remove-hook 'erc-complete-functions 'erc-pcomplete)))
77 (defun erc-pcomplete ()
78 "Complete the nick before point."
80 (when (> (point) (erc-beg-of-input-line))
81 (let ((last-command (if (eq last-command 'erc-complete-word)
84 (call-interactively 'pcomplete))
89 (defun pcomplete-erc-setup ()
90 "Setup `erc-mode' to use pcomplete."
91 (set (make-local-variable 'pcomplete-ignore-case)
93 (set (make-local-variable 'pcomplete-use-paring)
95 (set (make-local-variable 'pcomplete-suffix-list)
97 (set (make-local-variable 'pcomplete-parse-arguments-function)
98 'pcomplete-parse-erc-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 ()
111 (pcomplete-erc-commands)
112 (pcomplete-erc-nicks erc-pcomplete-nick-postfix))))
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 (with-current-buffer (erc-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))
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."
200 (maphash (lambda (nick cdata)
202 (erc-channel-user-op (cdr cdata)))
203 (setq ops (cons nick ops))))
207 (defun pcomplete-erc-not-ops ()
208 "Returns a list of nicks without ops."
210 (maphash (lambda (nick cdata)
212 (not (erc-channel-user-op (cdr cdata))))
213 (setq not-ops (cons nick not-ops))))
218 (defun pcomplete-erc-nicks (&optional postfix)
219 "Returns a list of nicks in the current channel."
220 (let ((users (erc-get-channel-user-list)))
221 (if erc-pcomplete-order-nickname-completions
222 (setq users (erc-sort-channel-users-by-activity users)))
224 (concat (erc-server-user-nickname (car x)) postfix))
227 (defun pcomplete-erc-all-nicks (&optional postfix)
228 "Returns a list of all nicks on the current server."
230 (with-current-buffer (process-buffer erc-server-process)
231 (maphash (lambda (nick user)
232 (setq nicks (cons (concat nick postfix) nicks)))
236 (defun pcomplete-erc-channels ()
237 "Returns a list of channels associated with the current server."
238 (mapcar (lambda (buf) (with-current-buffer buf (erc-default-target)))
239 (erc-channel-list erc-server-process)))
241 ;;; Functions for parsing
243 (defun pcomplete-erc-command-name ()
244 "Returns the command name of the first argument."
245 (if (eq (elt (pcomplete-arg 'first) 0) ?/)
246 (upcase (substring (pcomplete-arg 'first) 1))
249 (defun pcomplete-parse-erc-arguments ()
250 "Returns a list of parsed whitespace-separated arguments.
251 These are the words from the beginning of the line after the prompt
252 up to where point is right now."
253 (let* ((start erc-input-marker)
257 (if (< (skip-chars-backward " \t\n" start) 0)
259 beginnings (list end)))
261 (while (< (skip-chars-backward "^ \t\n" start) 0)
262 (setq beginnings (cons (point) beginnings)
263 args (cons (buffer-substring-no-properties
266 (skip-chars-backward " \t\n" start)
268 (cons args beginnings)))
270 (provide 'erc-pcomplete)
272 ;;; erc-pcomplete.el ends here
275 ;; indent-tabs-mode: nil
278 ;; arch-tag: 32a7703b-be87-45a4-82f3-9eed5a628911