erc-hecomplete.el 7.8 KB

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