1 ;;; erc-hecomplete.el --- Provides Nick name completion for ERC
3 ;; Copyright (C) 2001, 2002, 2004, 2006, 2007 Free Software Foundation, Inc.
5 ;; Author: Alex Schroeder <alex@gnu.org>
6 ;; URL: http://www.emacswiki.org/cgi-bin/wiki.pl?ErcCompletion
8 ;; This file is part of GNU Emacs.
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 3, or (at your option)
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.
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.
27 ;; This file is considered obsolete. It is recommended to use
28 ;; completion from erc-pcomplete instead.
30 ;; This file is based on hippie-expand, while the new file is based on
36 (require 'erc-match
); for erc-pals
37 (require 'hippie-exp
); for the hippie expand stuff
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
)))
45 (defun erc-hecomplete ()
46 "Complete nick at point.
47 See `erc-try-complete-nick' for more technical info.
48 This function is obsolete, use `erc-pcomplete' instead."
50 (let ((hippie-expand-try-functions-list '(erc-try-complete-nick)))
53 (defgroup erc-hecomplete nil
54 "Nick completion. It is recommended to use erc-pcomplete instead."
57 (defcustom erc-nick-completion
'all
58 "Determine how the list of nicks is determined during nick completion.
59 See `erc-complete-nick' for information on how to activate this.
62 all: All channel members.
64 You may also provide your own function that returns a list of completions.
65 One example is `erc-nick-completion-exclude-myself',
66 or 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"))
74 :group
'erc-hecomplete
)
76 (defcustom erc-nick-completion-ignore-case t
77 "*Non-nil means don't consider case significant in nick completion.
78 Case will be automatically corrected when non-nil.
79 For instance if you type \"dely TAB\" the word completes and changes to
81 :group
'erc-hecomplete
84 (defun erc-nick-completion-exclude-myself ()
85 "Get a list of all the channel members except you.
87 This function returns a list of all the members in the channel, except
88 your own nick. This way if you're named foo and someone is called foobar,
89 typing \"f o TAB\" will directly give you foobar. Use this with
90 `erc-nick-completion'."
93 (erc-get-channel-nickname-list)))
95 (defcustom erc-nick-completion-postfix
": "
96 "*When `erc-complete' is used in the first word after the prompt,
97 add this string when a unique expansion was found."
98 :group
'erc-hecomplete
101 (defun erc-command-list ()
102 "Returns a list of strings of the defined user commands."
103 (let ((case-fold-search nil
))
105 (concat "/" (downcase (substring (symbol-name x
) 8))))
106 (apropos-internal "erc-cmd-[A-Z]+"))))
108 (defun erc-try-complete-nick (old)
109 "Complete nick at point.
110 This is a function to put on `hippie-expand-try-functions-list'.
111 Then use \\[hippie-expand] to expand nicks.
112 The 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
)))
122 (try-complete-erc-nick old erc-nick-completion
))))
124 (defvar try-complete-erc-nick-window-configuration nil
125 "The window configuration for `try-complete-erc-nick'.
126 When called the first time, a window config is stored here,
127 and when completion is done, the window config is restored
128 from here. See `try-complete-erc-nick-restore' and
129 `try-complete-erc-nick'.")
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
)))
140 (defun try-complete-erc-nick (old completions
)
141 "Try to complete current word depending on `erc-try-complete-nick'.
142 The argument OLD has to be nil the first call of this function, and t
143 for subsequent calls (for further possible completions of the same
144 string). It returns t if a new completion is found, nil otherwise. The
145 second argument COMPLETIONS is a list of completions to use. Actually,
146 it is only used when OLD is nil. It will be copied to `he-expand-list'
147 on the first call. After that, it is no longer used.
148 Window configurations are stored in
149 `try-complete-erc-nick-window-configuration'."
152 (alist (if (consp (car completions
))
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
)
171 (eq t
(try-completion expansion alist
))))))
172 (cond ((not expansion
)
173 ;; There is no expansion at all.
174 (try-complete-erc-nick-restore)
178 ;; The user already has the correct expansion.
179 (try-complete-erc-nick-restore)
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
187 (try-complete-erc-nick-restore)
191 ;; The user has found the correct expansion.
192 (try-complete-erc-nick-restore)
193 (he-substitute-string expansion
)
196 ;; We found something but we are not finished. Show a
197 ;; completions buffer. Substitute what we found and return
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
)
206 (defun erc-at-beginning-of-line-p (point &optional bol-func
)
208 (funcall (or bol-func
210 (equal point
(point))))
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)))
216 (provide 'erc-hecomplete
)
218 ;;; erc-hecomplete.el ends here
221 ;; indent-tabs-mode: t
225 ;; arch-tag: 3be13ee8-8fdb-41ab-83c2-6582c757b91e