Merge from trunk.
[emacs.git] / lisp / obsolete / erc-hecomplete.el
blob67f51d690b2baed8b46673c103015b24b9a8472a
1 ;;; erc-hecomplete.el --- Provides Nick name completion for ERC
3 ;; Copyright (C) 2001-2002, 2004, 2006-2011 Free Software Foundation, Inc.
5 ;; Author: Alex Schroeder <alex@gnu.org>
6 ;; URL: http://www.emacswiki.org/cgi-bin/wiki.pl?ErcCompletion
7 ;; Obsolete-since: 24.1
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 3 of the License, or
14 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; This file is considered obsolete. It is recommended to use
27 ;; completion from erc-pcomplete instead.
29 ;; This file is based on hippie-expand, while the new file is based on
30 ;; pcomplete.
32 ;;; Code:
34 (require 'erc)
35 (require 'erc-match); for erc-pals
36 (require 'hippie-exp); for the hippie expand stuff
38 ;;;###autoload (autoload 'erc-hecomplete-mode "erc-hecomplete" nil t)
39 (define-erc-module hecomplete nil
40 "Complete nick at point."
41 ((add-hook 'erc-complete-functions 'erc-hecomplete))
42 ((remove-hook 'erc-complete-functions 'erc-hecomplete)))
44 (defun erc-hecomplete ()
45 "Complete nick at point.
46 See `erc-try-complete-nick' for more technical info.
47 This function is obsolete, use `erc-pcomplete' instead."
48 (interactive)
49 (let ((hippie-expand-try-functions-list '(erc-try-complete-nick)))
50 (hippie-expand nil)))
52 (defgroup erc-hecomplete nil
53 "Nick completion. It is recommended to use erc-pcomplete instead."
54 :group 'erc)
56 (defcustom erc-nick-completion 'all
57 "Determine how the list of nicks is determined during nick completion.
58 See `erc-complete-nick' for information on how to activate this.
60 pals: Use `erc-pals'.
61 all: All channel members.
63 You may also provide your own function that returns a list of completions.
64 One example is `erc-nick-completion-exclude-myself',
65 or you may use an arbitrary lisp expression."
66 :type '(choice (const :tag "List of pals" pals)
67 (const :tag "All channel members" all)
68 (const :tag "All channel members except yourself"
69 erc-nick-completion-exclude-myself)
70 (repeat :tag "List" (string :tag "Nick"))
71 function
72 sexp)
73 :group 'erc-hecomplete)
75 (defcustom erc-nick-completion-ignore-case t
76 "*Non-nil means don't consider case significant in nick completion.
77 Case will be automatically corrected when non-nil.
78 For instance if you type \"dely TAB\" the word completes and changes to
79 \"delYsid\"."
80 :group 'erc-hecomplete
81 :type 'boolean)
83 (defun erc-nick-completion-exclude-myself ()
84 "Get a list of all the channel members except you.
86 This function returns a list of all the members in the channel, except
87 your own nick. This way if you're named foo and someone is called foobar,
88 typing \"f o TAB\" will directly give you foobar. Use this with
89 `erc-nick-completion'."
90 (remove
91 (erc-current-nick)
92 (erc-get-channel-nickname-list)))
94 (defcustom erc-nick-completion-postfix ": "
95 "*When `erc-complete' is used in the first word after the prompt,
96 add this string when a unique expansion was found."
97 :group 'erc-hecomplete
98 :type 'string)
100 (defun erc-command-list ()
101 "Returns a list of strings of the defined user commands."
102 (let ((case-fold-search nil))
103 (mapcar (lambda (x)
104 (concat "/" (downcase (substring (symbol-name x) 8))))
105 (apropos-internal "erc-cmd-[A-Z]+"))))
107 (defun erc-try-complete-nick (old)
108 "Complete nick at point.
109 This is a function to put on `hippie-expand-try-functions-list'.
110 Then use \\[hippie-expand] to expand nicks.
111 The type of completion depends on `erc-nick-completion'."
112 (try-complete-erc-nick old (cond ((eq erc-nick-completion 'pals) erc-pals)
113 ((eq erc-nick-completion 'all)
114 (append
115 (erc-get-channel-nickname-list)
116 (erc-command-list)))
117 ((functionp erc-nick-completion)
118 (funcall erc-nick-completion))
119 (t erc-nick-completion))))
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'.")
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)))
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)
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)
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))))
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))))
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)))
213 (provide 'erc-hecomplete)
215 ;;; erc-hecomplete.el ends here
217 ;; Local Variables:
218 ;; indent-tabs-mode: t
219 ;; tab-width: 8
220 ;; End: