1;;; erc-pcomplete.el --- Provides programmable completion for ERC 2 3;; Copyright (C) 2002, 2003, 2004, 2006, 2007 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(defgroup erc-pcomplete nil 50 "Programmable completion for ERC" 51 :group 'erc) 52 53(defcustom erc-pcomplete-nick-postfix ": " 54 "*When `pcomplete' is used in the first word after the prompt, 55add this string to nicks completed." 56 :group 'erc-pcomplete 57 :type 'string) 58 59(defcustom erc-pcomplete-order-nickname-completions t 60 "If t, channel nickname completions will be ordered such that 61the most recent speakers are listed first." 62 :group 'erc-pcomplete 63 :type 'boolean) 64 65;;;###autoload (autoload 'erc-completion-mode "erc-pcomplete" nil t) 66(define-erc-module pcomplete Completion 67 "In ERC Completion mode, the TAB key does completion whenever possible." 68 ((add-hook 'erc-mode-hook 'pcomplete-erc-setup) 69 (add-hook 'erc-complete-functions 'erc-pcomplete) 70 (erc-buffer-list #'pcomplete-erc-setup)) 71 ((remove-hook 'erc-mode-hook 'pcomplete-erc-setup) 72 (remove-hook 'erc-complete-functions 'erc-pcomplete))) 73 74(defun erc-pcomplete () 75 "Complete the nick before point." 76 (interactive) 77 (when (> (point) (erc-beg-of-input-line)) 78 (let ((last-command (if (eq last-command 'erc-complete-word) 79 'pcomplete 80 last-command))) 81 (call-interactively 'pcomplete)) 82 t)) 83 84;;; Setup function 85 86(defun pcomplete-erc-setup () 87 "Setup `erc-mode' to use pcomplete." 88 (set (make-local-variable 'pcomplete-ignore-case) 89 t) 90 (set (make-local-variable 'pcomplete-use-paring) 91 nil) 92 (set (make-local-variable 'pcomplete-suffix-list) 93 '(? ?:)) 94 (set (make-local-variable 'pcomplete-parse-arguments-function) 95 'pcomplete-parse-erc-arguments) 96 (set (make-local-variable 'pcomplete-command-completion-function) 97 'pcomplete/erc-mode/complete-command) 98 (set (make-local-variable 'pcomplete-command-name-function) 99 'pcomplete-erc-command-name) 100 (set (make-local-variable 'pcomplete-default-completion-function) 101 (lambda () (pcomplete-here (pcomplete-erc-nicks))))) 102 103;;; Programmable completion logic 104 105(defun pcomplete/erc-mode/complete-command () 106 (pcomplete-here 107 (append 108 (pcomplete-erc-commands) 109 (pcomplete-erc-nicks erc-pcomplete-nick-postfix t)))) 110 111(defvar erc-pcomplete-ctcp-commands 112 '("ACTION" "CLIENTINFO" "ECHO" "FINGER" "PING" "TIME" "USERINFO" "VERSION")) 113 114(defun pcomplete/erc-mode/CTCP () 115 (pcomplete-here (pcomplete-erc-nicks)) 116 (pcomplete-here erc-pcomplete-ctcp-commands)) 117 118(defun pcomplete/erc-mode/CLEARTOPIC () 119 (pcomplete-here (pcomplete-erc-channels))) 120 121(defun pcomplete/erc-mode/DEOP () 122 (while (pcomplete-here (pcomplete-erc-ops)))) 123 124(defun pcomplete/erc-mode/DESCRIBE () 125 (pcomplete-here (pcomplete-erc-nicks))) 126 127(defun pcomplete/erc-mode/IDLE () 128 (while (pcomplete-here (pcomplete-erc-nicks)))) 129 130(defun pcomplete/erc-mode/KICK () 131 (pcomplete-here (pcomplete-erc-channels)) 132 (pcomplete-here (pcomplete-erc-nicks))) 133 134(defun pcomplete/erc-mode/LOAD () 135 (pcomplete-here (pcomplete-entries))) 136 137(defun pcomplete/erc-mode/MODE () 138 (pcomplete-here (pcomplete-erc-channels)) 139 (while (pcomplete-here (pcomplete-erc-nicks)))) 140 141(defun pcomplete/erc-mode/ME () 142 (while (pcomplete-here (pcomplete-erc-nicks)))) 143 144(defun pcomplete/erc-mode/SAY () 145 (pcomplete-here (pcomplete-erc-nicks)) 146 (pcomplete-here (pcomplete-erc-nicks)) 147 (while (pcomplete-here (pcomplete-erc-nicks)))) 148 149(defun pcomplete/erc-mode/MSG () 150 (pcomplete-here (append (pcomplete-erc-all-nicks) 151 (pcomplete-erc-channels))) 152 (while (pcomplete-here (pcomplete-erc-nicks)))) 153 154(defun pcomplete/erc-mode/NAMES () 155 (while (pcomplete-here (pcomplete-erc-channels)))) 156 157(defalias 'pcomplete/erc-mode/NOTICE 'pcomplete/erc-mode/MSG) 158 159(defun pcomplete/erc-mode/OP () 160 (while (pcomplete-here (pcomplete-erc-not-ops)))) 161 162(defun pcomplete/erc-mode/PART () 163 (pcomplete-here (pcomplete-erc-channels))) 164 165(defalias 'pcomplete/erc-mode/LEAVE 'pcomplete/erc-mode/PART) 166 167(defun pcomplete/erc-mode/QUERY () 168 (pcomplete-here (append (pcomplete-erc-all-nicks) 169 (pcomplete-erc-channels))) 170 (while (pcomplete-here (pcomplete-erc-nicks))) 171 ) 172 173(defun pcomplete/erc-mode/SOUND () 174 (while (pcomplete-here (pcomplete-entries)))) 175 176(defun pcomplete/erc-mode/TOPIC () 177 (pcomplete-here (pcomplete-erc-channels))) 178 179(defun pcomplete/erc-mode/WHOIS () 180 (while (pcomplete-here (pcomplete-erc-nicks)))) 181 182(defun pcomplete/erc-mode/UNIGNORE () 183 (pcomplete-here (erc-with-server-buffer erc-ignore-list))) 184 185;;; Functions that provide possible completions. 186 187(defun pcomplete-erc-commands () 188 "Returns a list of strings of the defined user commands." 189 (let ((case-fold-search nil)) 190 (mapcar (lambda (x) 191 (concat "/" (downcase (substring (symbol-name x) 8)))) 192 (apropos-internal "erc-cmd-[A-Z]+")))) 193 194(defun pcomplete-erc-ops () 195 "Returns a list of nicks with ops." 196 (let (ops) 197 (maphash (lambda (nick cdata) 198 (if (and (cdr cdata) 199 (erc-channel-user-op (cdr cdata))) 200 (setq ops (cons nick ops)))) 201 erc-channel-users) 202 ops)) 203 204(defun pcomplete-erc-not-ops () 205 "Returns a list of nicks without ops." 206 (let (not-ops) 207 (maphash (lambda (nick cdata) 208 (if (and (cdr cdata) 209 (not (erc-channel-user-op (cdr cdata)))) 210 (setq not-ops (cons nick not-ops)))) 211 erc-channel-users) 212 not-ops)) 213 214 215(defun pcomplete-erc-nicks (&optional postfix ignore-self) 216 "Returns a list of nicks in the current channel. 217Optional argument POSTFIX is something to append to the nickname. 218If optional argument IGNORE-SELF is non-nil, don't return the current nick." 219 (let ((users (if erc-pcomplete-order-nickname-completions 220 (erc-sort-channel-users-by-activity 221 (erc-get-channel-user-list)) 222 (erc-get-channel-user-list))) 223 (nicks nil)) 224 (dolist (user users) 225 (unless (and ignore-self 226 (string= (erc-server-user-nickname (car user)) 227 (erc-current-nick))) 228 (setq nicks (cons (concat (erc-server-user-nickname (car user)) 229 postfix) 230 nicks)))) 231 (nreverse nicks))) 232 233(defun pcomplete-erc-all-nicks (&optional postfix) 234 "Returns a list of all nicks on the current server." 235 (let (nicks) 236 (erc-with-server-buffer 237 (maphash (lambda (nick user) 238 (setq nicks (cons (concat nick postfix) nicks))) 239 erc-server-users)) 240 nicks)) 241 242(defun pcomplete-erc-channels () 243 "Returns a list of channels associated with the current server." 244 (mapcar (lambda (buf) (with-current-buffer buf (erc-default-target))) 245 (erc-channel-list erc-server-process))) 246 247;;; Functions for parsing 248 249(defun pcomplete-erc-command-name () 250 "Returns the command name of the first argument." 251 (if (eq (elt (pcomplete-arg 'first) 0) ?/) 252 (upcase (substring (pcomplete-arg 'first) 1)) 253 "SAY")) 254 255(defun pcomplete-parse-erc-arguments () 256 "Returns a list of parsed whitespace-separated arguments. 257These are the words from the beginning of the line after the prompt 258up to where point is right now." 259 (let* ((start erc-input-marker) 260 (end (point)) 261 args beginnings) 262 (save-excursion 263 (if (< (skip-chars-backward " \t\n" start) 0) 264 (setq args '("") 265 beginnings (list end))) 266 (setq end (point)) 267 (while (< (skip-chars-backward "^ \t\n" start) 0) 268 (setq beginnings (cons (point) beginnings) 269 args (cons (buffer-substring-no-properties 270 (point) end) 271 args)) 272 (skip-chars-backward " \t\n" start) 273 (setq end (point)))) 274 (cons args beginnings))) 275 276(provide 'erc-pcomplete) 277 278;;; erc-pcomplete.el ends here 279;; 280;; Local Variables: 281;; indent-tabs-mode: nil 282;; End: 283 284;; arch-tag: 32a7703b-be87-45a4-82f3-9eed5a628911 285