Revision: emacs@sv.gnu.org/emacs--devo--0--patch-46
[bpt/emacs.git] / lisp / erc / erc-complete.el
CommitLineData
597993cf
MB
1;;; erc-complete.el --- Provides Nick name completion for ERC
2
3;; Copyright (C) 2001, 2002, 2004 Free Software Foundation, Inc.
4
5;; Author: Alex Schroeder <alex@gnu.org>
6;; URL: http://www.emacswiki.org/cgi-bin/wiki.pl?ErcCompletion
7
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software; you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
12;; the Free Software Foundation; either version 2, or (at your option)
13;; any later version.
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
21;; along with GNU Emacs; see the file COPYING. If not, write to the
22;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23;; Boston, MA 02110-1301, USA.
24
25;;; Commentary:
26
27;; This file is obsolete. Use completion from erc-pcomplete instead.
28;; This file is based on hippie-expand, while the new file is based on
29;; pcomplete. There is no autoload cookie in this file. If you want
30;; to use the code in this file, add the following to your ~/.emacs:
31
32;; (autoload 'erc-complete "erc-complete" "Complete nick at point." t)
33
34;;; Code:
35
36(require 'erc)
37(require 'erc-match); for erc-pals
38(require 'hippie-exp); for the hippie expand stuff
39
40;;;###autoload
41(defun erc-complete ()
42 "Complete nick at point.
43See `erc-try-complete-nick' for more technical info.
44This function is obsolete, use `erc-pcomplete' instead."
45 (interactive)
46 (let ((hippie-expand-try-functions-list '(erc-try-complete-nick)))
47 (hippie-expand nil)))
48
49(defgroup erc-old-complete nil
50 "Nick completion. Obsolete, use erc-pcomplete instead."
51 :group 'erc)
52
53(defcustom erc-nick-completion 'all
54 "Determine how the list of nicks is determined during nick completion.
55See `erc-complete-nick' for information on how to activate this.
56
57pals: Use `erc-pals'.
58all: All channel members.
59
60You may also provide your own function that returns a list of completions.
61One example is `erc-nick-completion-exclude-myself',
62or you may use an arbitrary lisp expression."
63 :type '(choice (const :tag "List of pals" pals)
64 (const :tag "All channel members" all)
65 (const :tag "All channel members except yourself"
66 erc-nick-completion-exclude-myself)
67 (repeat :tag "List" (string :tag "Nick"))
68 function
69 sexp)
70 :group 'erc-old-complete)
71
72(defcustom erc-nick-completion-ignore-case t
73 "*Non-nil means don't consider case significant in nick completion.
74Case will be automatically corrected when non-nil.
75For instance if you type \"dely TAB\" the word completes and changes to
76\"delYsid\"."
77 :group 'erc-old-complete
78 :type 'boolean)
79
80(defun erc-nick-completion-exclude-myself ()
81 "Get a list of all the channel members except you.
82
83This function returns a list of all the members in the channel, except
84your own nick. This way if you're named foo and someone is called foobar,
85typing \"f o TAB\" will directly give you foobar. Use this with
86`erc-nick-completion'."
8508e990 87 (remove
597993cf 88 (erc-current-nick)
8508e990 89 (erc-get-channel-nickname-list)))
597993cf
MB
90
91(defcustom erc-nick-completion-postfix ": "
92 "*When `erc-complete' is used in the first word after the prompt,
93add this string when a unique expansion was found."
94 :group 'erc-old-complete
95 :type 'string)
96
97(defun erc-command-list ()
98 "Returns a list of strings of the defined user commands."
99 (let ((case-fold-search nil))
100 (mapcar (lambda (x)
101 (concat "/" (downcase (substring (symbol-name x) 8))))
102 (apropos-internal "erc-cmd-[A-Z]+"))))
103
104(defun erc-try-complete-nick (old)
105 "Complete nick at point.
106This is a function to put on `hippie-expand-try-functions-list'.
107Then use \\[hippie-expand] to expand nicks.
108The type of completion depends on `erc-nick-completion'."
109 (cond ((eq erc-nick-completion 'pals)
110 (try-complete-erc-nick old erc-pals))
111 ((eq erc-nick-completion 'all)
112 (try-complete-erc-nick old (append
8508e990 113 (erc-get-channel-nickname-list)
597993cf
MB
114 (erc-command-list))))
115 ((functionp erc-nick-completion)
116 (try-complete-erc-nick old (funcall erc-nick-completion)))
117 (t
118 (try-complete-erc-nick old erc-nick-completion))))
119
120(defvar try-complete-erc-nick-window-configuration nil
121 "The window configuration for `try-complete-erc-nick'.
122When called the first time, a window config is stored here,
123and when completion is done, the window config is restored
124from here. See `try-complete-erc-nick-restore' and
125`try-complete-erc-nick'.")
126
127(defun try-complete-erc-nick-restore ()
128 "Restore window configuration."
129 (if (not try-complete-erc-nick-window-configuration)
130 (when (get-buffer "*Completions*")
131 (delete-windows-on "*Completions*"))
132 (set-window-configuration
133 try-complete-erc-nick-window-configuration)
134 (setq try-complete-erc-nick-window-configuration nil)))
135
136(defun try-complete-erc-nick (old completions)
137 "Try to complete current word depending on `erc-try-complete-nick'.
138The argument OLD has to be nil the first call of this function, and t
139for subsequent calls (for further possible completions of the same
140string). It returns t if a new completion is found, nil otherwise. The
141second argument COMPLETIONS is a list of completions to use. Actually,
142it is only used when OLD is nil. It will be copied to `he-expand-list'
143on the first call. After that, it is no longer used.
144Window configurations are stored in
145`try-complete-erc-nick-window-configuration'."
146 (let (expansion
147 final
148 (alist (if (consp (car completions))
149 completions
150 (mapcar (lambda (s)
151 (if (and (erc-complete-at-prompt)
152 (and (not (= (length s) 0))
153 (not (eq (elt s 0) ?/))))
154 (list (concat s erc-nick-completion-postfix))
155 (list (concat s " "))))
156 completions))) ; make alist if required
157 (completion-ignore-case erc-nick-completion-ignore-case))
158 (he-init-string (he-dabbrev-beg) (point))
159 ;; If there is a string to complete, complete it using alist.
160 ;; expansion is the possible expansion, or t. If expansion is t
161 ;; or if expansion is the "real" thing, we are finished (final is
162 ;; t). Take care -- expansion can also be nil!
163 (unless (string= he-search-string "")
164 (setq expansion (try-completion he-search-string alist)
165 final (or (eq t expansion)
166 (and expansion
167 (eq t (try-completion expansion alist))))))
168 (cond ((not expansion)
169 ;; There is no expansion at all.
170 (try-complete-erc-nick-restore)
171 (he-reset-string)
172 nil)
173 ((eq t expansion)
174 ;; The user already has the correct expansion.
175 (try-complete-erc-nick-restore)
176 (he-reset-string)
177 t)
178 ((and old (string= expansion he-search-string))
179 ;; This is the second time around and nothing changed,
180 ;; ie. the user tried to expand something incomplete
181 ;; without making a choice -- hitting TAB twice, for
182 ;; example.
183 (try-complete-erc-nick-restore)
184 (he-reset-string)
185 nil)
186 (final
187 ;; The user has found the correct expansion.
188 (try-complete-erc-nick-restore)
189 (he-substitute-string expansion)
190 t)
191 (t
192 ;; We found something but we are not finished. Show a
193 ;; completions buffer. Substitute what we found and return
194 ;; t.
195 (setq try-complete-erc-nick-window-configuration
196 (current-window-configuration))
197 (with-output-to-temp-buffer "*Completions*"
198 (display-completion-list (all-completions he-search-string alist)))
199 (he-substitute-string expansion)
200 t))))
201
202(defun erc-at-beginning-of-line-p (point &optional bol-func)
203 (save-excursion
204 (funcall (or bol-func
205 'erc-bol))
206 (equal point (point))))
207
208(defun erc-complete-at-prompt ()
209 "Returns t if point is directly after `erc-prompt' when doing completion."
210 (erc-at-beginning-of-line-p (he-dabbrev-beg)))
211
212(provide 'erc-complete)
213
214;;; erc-complete.el ends here
215;;
216;; Local Variables:
217;; indent-tabs-mode: t
218;; tab-width: 8
219;; End:
220
221;; arch-tag: 3be13ee8-8fdb-41ab-83c2-6582c757b91e