comparison lisp/erc/erc-complete.el @ 68451:fc745b05e928

Revision: emacs@sv.gnu.org/emacs--devo--0--patch-22 Creator: Michael Olson <mwolson@gnu.org> Install ERC.
author Miles Bader <miles@gnu.org>
date Sun, 29 Jan 2006 13:08:58 +0000
parents
children 86c478cccc57
comparison
equal deleted inserted replaced
68450:a3ba4ef5d590 68451:fc745b05e928
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.
43 See `erc-try-complete-nick' for more technical info.
44 This 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.
55 See `erc-complete-nick' for information on how to activate this.
56
57 pals: Use `erc-pals'.
58 all: All channel members.
59
60 You may also provide your own function that returns a list of completions.
61 One example is `erc-nick-completion-exclude-myself',
62 or 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.
74 Case will be automatically corrected when non-nil.
75 For 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
83 This function returns a list of all the members in the channel, except
84 your own nick. This way if you're named foo and someone is called foobar,
85 typing \"f o TAB\" will directly give you foobar. Use this with
86 `erc-nick-completion'."
87 (delete
88 (erc-current-nick)
89 (mapcar (function car) (erc-get-channel-user-list))))
90
91 (defcustom erc-nick-completion-postfix ": "
92 "*When `erc-complete' is used in the first word after the prompt,
93 add 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.
106 This is a function to put on `hippie-expand-try-functions-list'.
107 Then use \\[hippie-expand] to expand nicks.
108 The 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
113 (mapcar (function car)
114 (erc-get-channel-user-list))
115 (erc-command-list))))
116 ((functionp erc-nick-completion)
117 (try-complete-erc-nick old (funcall erc-nick-completion)))
118 (t
119 (try-complete-erc-nick old erc-nick-completion))))
120
121 (defvar try-complete-erc-nick-window-configuration nil
122 "The window configuration for `try-complete-erc-nick'.
123 When called the first time, a window config is stored here,
124 and when completion is done, the window config is restored
125 from here. See `try-complete-erc-nick-restore' and
126 `try-complete-erc-nick'.")
127
128 (defun try-complete-erc-nick-restore ()
129 "Restore window configuration."
130 (if (not try-complete-erc-nick-window-configuration)
131 (when (get-buffer "*Completions*")
132 (delete-windows-on "*Completions*"))
133 (set-window-configuration
134 try-complete-erc-nick-window-configuration)
135 (setq try-complete-erc-nick-window-configuration nil)))
136
137 (defun try-complete-erc-nick (old completions)
138 "Try to complete current word depending on `erc-try-complete-nick'.
139 The argument OLD has to be nil the first call of this function, and t
140 for subsequent calls (for further possible completions of the same
141 string). It returns t if a new completion is found, nil otherwise. The
142 second argument COMPLETIONS is a list of completions to use. Actually,
143 it is only used when OLD is nil. It will be copied to `he-expand-list'
144 on the first call. After that, it is no longer used.
145 Window configurations are stored in
146 `try-complete-erc-nick-window-configuration'."
147 (let (expansion
148 final
149 (alist (if (consp (car completions))
150 completions
151 (mapcar (lambda (s)
152 (if (and (erc-complete-at-prompt)
153 (and (not (= (length s) 0))
154 (not (eq (elt s 0) ?/))))
155 (list (concat s erc-nick-completion-postfix))
156 (list (concat s " "))))
157 completions))) ; make alist if required
158 (completion-ignore-case erc-nick-completion-ignore-case))
159 (he-init-string (he-dabbrev-beg) (point))
160 ;; If there is a string to complete, complete it using alist.
161 ;; expansion is the possible expansion, or t. If expansion is t
162 ;; or if expansion is the "real" thing, we are finished (final is
163 ;; t). Take care -- expansion can also be nil!
164 (unless (string= he-search-string "")
165 (setq expansion (try-completion he-search-string alist)
166 final (or (eq t expansion)
167 (and expansion
168 (eq t (try-completion expansion alist))))))
169 (cond ((not expansion)
170 ;; There is no expansion at all.
171 (try-complete-erc-nick-restore)
172 (he-reset-string)
173 nil)
174 ((eq t expansion)
175 ;; The user already has the correct expansion.
176 (try-complete-erc-nick-restore)
177 (he-reset-string)
178 t)
179 ((and old (string= expansion he-search-string))
180 ;; This is the second time around and nothing changed,
181 ;; ie. the user tried to expand something incomplete
182 ;; without making a choice -- hitting TAB twice, for
183 ;; example.
184 (try-complete-erc-nick-restore)
185 (he-reset-string)
186 nil)
187 (final
188 ;; The user has found the correct expansion.
189 (try-complete-erc-nick-restore)
190 (he-substitute-string expansion)
191 t)
192 (t
193 ;; We found something but we are not finished. Show a
194 ;; completions buffer. Substitute what we found and return
195 ;; t.
196 (setq try-complete-erc-nick-window-configuration
197 (current-window-configuration))
198 (with-output-to-temp-buffer "*Completions*"
199 (display-completion-list (all-completions he-search-string alist)))
200 (he-substitute-string expansion)
201 t))))
202
203 (defun erc-at-beginning-of-line-p (point &optional bol-func)
204 (save-excursion
205 (funcall (or bol-func
206 'erc-bol))
207 (equal point (point))))
208
209 (defun erc-complete-at-prompt ()
210 "Returns t if point is directly after `erc-prompt' when doing completion."
211 (erc-at-beginning-of-line-p (he-dabbrev-beg)))
212
213 (provide 'erc-complete)
214
215 ;;; erc-complete.el ends here
216 ;;
217 ;; Local Variables:
218 ;; indent-tabs-mode: t
219 ;; tab-width: 8
220 ;; End:
221
222 ;; arch-tag: 3be13ee8-8fdb-41ab-83c2-6582c757b91e