Initial Commit
[packages] / xemacs-packages / erc / erc-pcomplete.el
1 ;;; erc-pcomplete.el --- Provides programmable completion for ERC
2
3 ;; Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
4
5 ;; Author: Sacha Chua <sacha@free.net.ph>
6 ;; Keywords: comm, convenience
7 ;; URL: http://www.emacswiki.org/cgi-bin/wiki.pl?ErcCompletion
8
9 ;; This file is part of GNU Emacs.
10
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)
14 ;; any later version.
15
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.
20
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.
25
26 ;;; Commentary:
27
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.
31 ;;
32 ;; To use, (require 'erc-auto) or (require 'erc-pcomplete), then
33 ;;   (erc-pcomplete-mode 1)
34 ;;
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'.
38 ;;
39 ;; See CREDITS for other contributors.
40 ;;
41 ;;; Code:
42
43 (require 'pcomplete)
44 (require 'erc)
45 (require 'erc-compat)
46 (require 'time-date)
47 (eval-when-compile (require 'cl))
48
49 (defconst erc-pcomplete-version "$Revision: 1.32.2.1 $"
50   "ERC pcomplete mode revision")
51
52 (defgroup erc-pcomplete nil
53   "Programmable completion for ERC"
54   :group 'erc)
55
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."
59   :group 'erc-pcomplete
60   :type 'string)
61
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."
65   :group 'erc-pcomplete
66   :type 'boolean)
67
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)))
76
77 (defun erc-pcomplete ()
78   "Complete the nick before point."
79   (interactive)
80   (when (> (point) (erc-beg-of-input-line))
81     (let ((last-command (if (eq last-command 'erc-complete-word)
82                             'pcomplete
83                           last-command)))
84       (call-interactively 'pcomplete))
85     t))
86
87 ;;; Setup function
88
89 (defun pcomplete-erc-setup ()
90   "Setup `erc-mode' to use pcomplete."
91   (set (make-local-variable 'pcomplete-ignore-case)
92        t)
93   (set (make-local-variable 'pcomplete-use-paring)
94        nil)
95   (set (make-local-variable 'pcomplete-suffix-list)
96        '(?  ?:))
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)))))
105
106 ;;; Programmable completion logic
107
108 (defun pcomplete/erc-mode/complete-command ()
109   (pcomplete-here
110    (append
111     (pcomplete-erc-commands)
112     (pcomplete-erc-nicks erc-pcomplete-nick-postfix))))
113
114 (defvar erc-pcomplete-ctcp-commands
115   '("ACTION" "CLIENTINFO" "ECHO" "FINGER" "PING" "TIME" "USERINFO" "VERSION"))
116
117 (defun pcomplete/erc-mode/CTCP ()
118   (pcomplete-here (pcomplete-erc-nicks))
119   (pcomplete-here erc-pcomplete-ctcp-commands))
120
121 (defun pcomplete/erc-mode/CLEARTOPIC ()
122   (pcomplete-here (pcomplete-erc-channels)))
123
124 (defun pcomplete/erc-mode/DEOP ()
125   (while (pcomplete-here (pcomplete-erc-ops))))
126
127 (defun pcomplete/erc-mode/DESCRIBE ()
128   (pcomplete-here (pcomplete-erc-nicks)))
129
130 (defun pcomplete/erc-mode/IDLE ()
131   (while (pcomplete-here (pcomplete-erc-nicks))))
132
133 (defun pcomplete/erc-mode/KICK ()
134   (pcomplete-here (pcomplete-erc-channels))
135   (pcomplete-here (pcomplete-erc-nicks)))
136
137 (defun pcomplete/erc-mode/LOAD ()
138   (pcomplete-here (pcomplete-entries)))
139
140 (defun pcomplete/erc-mode/MODE ()
141   (pcomplete-here (pcomplete-erc-channels))
142   (while (pcomplete-here (pcomplete-erc-nicks))))
143
144 (defun pcomplete/erc-mode/ME ()
145   (while (pcomplete-here (pcomplete-erc-nicks))))
146
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))))
151
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))))
156
157 (defun pcomplete/erc-mode/NAMES ()
158   (while (pcomplete-here (pcomplete-erc-channels))))
159
160 (defalias 'pcomplete/erc-mode/NOTICE 'pcomplete/erc-mode/MSG)
161
162 (defun pcomplete/erc-mode/OP ()
163   (while (pcomplete-here (pcomplete-erc-not-ops))))
164
165 (defun pcomplete/erc-mode/PART ()
166   (pcomplete-here (pcomplete-erc-channels)))
167
168 (defalias 'pcomplete/erc-mode/LEAVE 'pcomplete/erc-mode/PART)
169
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)))
174   )
175
176 (defun pcomplete/erc-mode/SOUND ()
177   (while (pcomplete-here (pcomplete-entries))))
178
179 (defun pcomplete/erc-mode/TOPIC ()
180   (pcomplete-here (pcomplete-erc-channels)))
181
182 (defun pcomplete/erc-mode/WHOIS ()
183   (while (pcomplete-here (pcomplete-erc-nicks))))
184
185 (defun pcomplete/erc-mode/UNIGNORE ()
186   (pcomplete-here (with-current-buffer (erc-server-buffer) erc-ignore-list)))
187
188 ;;; Functions that provide possible completions.
189
190 (defun pcomplete-erc-commands ()
191   "Returns a list of strings of the defined user commands."
192   (let ((case-fold-search nil))
193     (mapcar (lambda (x)
194               (concat "/" (downcase (substring (symbol-name x) 8))))
195             (apropos-internal "erc-cmd-[A-Z]+"))))
196
197 (defun pcomplete-erc-ops ()
198   "Returns a list of nicks with ops."
199   (let (ops)
200     (maphash (lambda (nick cdata)
201                (if (and (cdr cdata)
202                         (erc-channel-user-op (cdr cdata)))
203                    (setq ops (cons nick ops))))
204              erc-channel-users)
205     ops))
206
207 (defun pcomplete-erc-not-ops ()
208   "Returns a list of nicks without ops."
209   (let (not-ops)
210     (maphash (lambda (nick cdata)
211                (if (and (cdr cdata)
212                         (not (erc-channel-user-op (cdr cdata))))
213                    (setq not-ops (cons nick not-ops))))
214              erc-channel-users)
215     not-ops))
216
217
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)))
223   (mapcar (lambda (x)
224               (concat (erc-server-user-nickname (car x)) postfix))
225             users)))
226
227 (defun pcomplete-erc-all-nicks (&optional postfix)
228   "Returns a list of all nicks on the current server."
229   (let (nicks)
230     (with-current-buffer (process-buffer erc-server-process)
231       (maphash (lambda (nick user)
232                  (setq nicks (cons (concat nick postfix) nicks)))
233                erc-server-users))
234       nicks))
235
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)))
240
241 ;;; Functions for parsing
242
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))
247     "SAY"))
248
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)
254          (end (point))
255          args beginnings)
256     (save-excursion
257       (if (< (skip-chars-backward " \t\n" start) 0)
258           (setq args '("")
259                 beginnings (list end)))
260       (setq end (point))
261       (while (< (skip-chars-backward "^ \t\n" start) 0)
262         (setq beginnings (cons (point) beginnings)
263               args (cons (buffer-substring-no-properties
264                           (point) end)
265                          args))
266         (skip-chars-backward " \t\n" start)
267         (setq end (point))))
268     (cons args beginnings)))
269
270 (provide 'erc-pcomplete)
271
272 ;;; erc-pcomplete.el ends here
273 ;;
274 ;; Local Variables:
275 ;; indent-tabs-mode: nil
276 ;; End:
277
278 ;; arch-tag: 32a7703b-be87-45a4-82f3-9eed5a628911