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