erc-pcomplete.el 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. ;;; erc-pcomplete.el --- Provides programmable completion for ERC
  2. ;; Copyright (C) 2002-2004, 2006-2012 Free Software Foundation, Inc.
  3. ;; Author: Sacha Chua <sacha@free.net.ph>
  4. ;; Keywords: comm, convenience
  5. ;; URL: http://www.emacswiki.org/cgi-bin/wiki.pl?ErcCompletion
  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 replaces erc-complete.el. It provides nick completion
  19. ;; for ERC based on pcomplete. If you do not have pcomplete, you may
  20. ;; try to use erc-complete.el.
  21. ;;
  22. ;; To use, (require 'erc-auto) or (require 'erc-pcomplete), then
  23. ;; (erc-pcomplete-mode 1)
  24. ;;
  25. ;; If you want nickname completions ordered such that the most recent
  26. ;; speakers are listed first, set
  27. ;; `erc-pcomplete-order-nickname-completions' to `t'.
  28. ;;
  29. ;; See CREDITS for other contributors.
  30. ;;
  31. ;;; Code:
  32. (require 'pcomplete)
  33. (require 'erc)
  34. (require 'erc-compat)
  35. (require 'time-date)
  36. (eval-when-compile (require 'cl))
  37. (defgroup erc-pcomplete nil
  38. "Programmable completion for ERC"
  39. :group 'erc)
  40. (defcustom erc-pcomplete-nick-postfix ":"
  41. "*When `pcomplete' is used in the first word after the prompt,
  42. add this string to nicks completed."
  43. :group 'erc-pcomplete
  44. :type 'string)
  45. (defcustom erc-pcomplete-order-nickname-completions t
  46. "If t, channel nickname completions will be ordered such that
  47. the most recent speakers are listed first."
  48. :group 'erc-pcomplete
  49. :type 'boolean)
  50. ;;;###autoload (autoload 'erc-completion-mode "erc-pcomplete" nil t)
  51. (define-erc-module pcomplete Completion
  52. "In ERC Completion mode, the TAB key does completion whenever possible."
  53. ((add-hook 'erc-mode-hook 'pcomplete-erc-setup)
  54. (add-hook 'erc-complete-functions 'erc-pcompletions-at-point)
  55. (erc-buffer-list #'pcomplete-erc-setup))
  56. ((remove-hook 'erc-mode-hook 'pcomplete-erc-setup)
  57. (remove-hook 'erc-complete-functions 'erc-pcompletions-at-point)))
  58. (defun erc-pcompletions-at-point ()
  59. "ERC completion data from pcomplete.
  60. for use on `completion-at-point-function'."
  61. (when (> (point) (erc-beg-of-input-line))
  62. (or (let ((pcomplete-default-completion-function #'ignore))
  63. (pcomplete-completions-at-point))
  64. (let ((c (pcomplete-completions-at-point)))
  65. (if c (nconc c '(:exclusive no)))))))
  66. (defun erc-pcomplete ()
  67. "Complete the nick before point."
  68. (interactive)
  69. (when (> (point) (erc-beg-of-input-line))
  70. (let ((last-command (if (eq last-command 'erc-complete-word)
  71. 'pcomplete
  72. last-command)))
  73. (call-interactively 'pcomplete))
  74. t))
  75. ;;; Setup function
  76. (defun pcomplete-erc-setup ()
  77. "Setup `erc-mode' to use pcomplete."
  78. (set (make-local-variable 'pcomplete-ignore-case)
  79. t)
  80. (set (make-local-variable 'pcomplete-use-paring)
  81. nil)
  82. (set (make-local-variable 'pcomplete-parse-arguments-function)
  83. 'pcomplete-erc-parse-arguments)
  84. (set (make-local-variable 'pcomplete-command-completion-function)
  85. 'pcomplete/erc-mode/complete-command)
  86. (set (make-local-variable 'pcomplete-command-name-function)
  87. 'pcomplete-erc-command-name)
  88. (set (make-local-variable 'pcomplete-default-completion-function)
  89. (lambda () (pcomplete-here (pcomplete-erc-nicks)))))
  90. ;;; Programmable completion logic
  91. (defun pcomplete/erc-mode/complete-command ()
  92. (pcomplete-here
  93. (append
  94. (pcomplete-erc-commands)
  95. (pcomplete-erc-nicks erc-pcomplete-nick-postfix t))))
  96. (defvar erc-pcomplete-ctcp-commands
  97. '("ACTION" "CLIENTINFO" "ECHO" "FINGER" "PING" "TIME" "USERINFO" "VERSION"))
  98. (defun pcomplete/erc-mode/CTCP ()
  99. (pcomplete-here (pcomplete-erc-nicks))
  100. (pcomplete-here erc-pcomplete-ctcp-commands))
  101. (defun pcomplete/erc-mode/CLEARTOPIC ()
  102. (pcomplete-here (pcomplete-erc-channels)))
  103. (defun pcomplete/erc-mode/DEOP ()
  104. (while (pcomplete-here (pcomplete-erc-ops))))
  105. (defun pcomplete/erc-mode/DESCRIBE ()
  106. (pcomplete-here (pcomplete-erc-nicks)))
  107. (defun pcomplete/erc-mode/IDLE ()
  108. (while (pcomplete-here (pcomplete-erc-nicks))))
  109. (defun pcomplete/erc-mode/KICK ()
  110. (pcomplete-here (pcomplete-erc-channels))
  111. (pcomplete-here (pcomplete-erc-nicks)))
  112. (defun pcomplete/erc-mode/LOAD ()
  113. (pcomplete-here (pcomplete-entries)))
  114. (defun pcomplete/erc-mode/MODE ()
  115. (pcomplete-here (pcomplete-erc-channels))
  116. (while (pcomplete-here (pcomplete-erc-nicks))))
  117. (defun pcomplete/erc-mode/ME ()
  118. (while (pcomplete-here (pcomplete-erc-nicks))))
  119. (defun pcomplete/erc-mode/SAY ()
  120. (pcomplete-here (pcomplete-erc-nicks))
  121. (pcomplete-here (pcomplete-erc-nicks))
  122. (while (pcomplete-here (pcomplete-erc-nicks))))
  123. (defun pcomplete/erc-mode/MSG ()
  124. (pcomplete-here (append (pcomplete-erc-all-nicks)
  125. (pcomplete-erc-channels)))
  126. (while (pcomplete-here (pcomplete-erc-nicks))))
  127. (defun pcomplete/erc-mode/NAMES ()
  128. (while (pcomplete-here (pcomplete-erc-channels))))
  129. (defalias 'pcomplete/erc-mode/NOTICE 'pcomplete/erc-mode/MSG)
  130. (defun pcomplete/erc-mode/OP ()
  131. (while (pcomplete-here (pcomplete-erc-not-ops))))
  132. (defun pcomplete/erc-mode/PART ()
  133. (pcomplete-here (pcomplete-erc-channels)))
  134. (defalias 'pcomplete/erc-mode/LEAVE 'pcomplete/erc-mode/PART)
  135. (defun pcomplete/erc-mode/QUERY ()
  136. (pcomplete-here (append (pcomplete-erc-all-nicks)
  137. (pcomplete-erc-channels)))
  138. (while (pcomplete-here (pcomplete-erc-nicks)))
  139. )
  140. (defun pcomplete/erc-mode/SOUND ()
  141. (while (pcomplete-here (pcomplete-entries))))
  142. (defun pcomplete/erc-mode/TOPIC ()
  143. (pcomplete-here (pcomplete-erc-channels)))
  144. (defun pcomplete/erc-mode/WHOIS ()
  145. (while (pcomplete-here (pcomplete-erc-nicks))))
  146. (defun pcomplete/erc-mode/UNIGNORE ()
  147. (pcomplete-here (erc-with-server-buffer erc-ignore-list)))
  148. ;;; Functions that provide possible completions.
  149. (defun pcomplete-erc-commands ()
  150. "Returns a list of strings of the defined user commands."
  151. (let ((case-fold-search nil))
  152. (mapcar (lambda (x)
  153. (concat "/" (downcase (substring (symbol-name x) 8))))
  154. (apropos-internal "erc-cmd-[A-Z]+"))))
  155. (defun pcomplete-erc-ops ()
  156. "Returns a list of nicks with ops."
  157. (let (ops)
  158. (maphash (lambda (nick cdata)
  159. (if (and (cdr cdata)
  160. (erc-channel-user-op (cdr cdata)))
  161. (setq ops (cons nick ops))))
  162. erc-channel-users)
  163. ops))
  164. (defun pcomplete-erc-not-ops ()
  165. "Returns a list of nicks without ops."
  166. (let (not-ops)
  167. (maphash (lambda (nick cdata)
  168. (if (and (cdr cdata)
  169. (not (erc-channel-user-op (cdr cdata))))
  170. (setq not-ops (cons nick not-ops))))
  171. erc-channel-users)
  172. not-ops))
  173. (defun pcomplete-erc-nicks (&optional postfix ignore-self)
  174. "Returns a list of nicks in the current channel.
  175. Optional argument POSTFIX is something to append to the nickname.
  176. If optional argument IGNORE-SELF is non-nil, don't return the current nick."
  177. (let ((users (if erc-pcomplete-order-nickname-completions
  178. (erc-sort-channel-users-by-activity
  179. (erc-get-channel-user-list))
  180. (erc-get-channel-user-list)))
  181. (nicks nil))
  182. (dolist (user users)
  183. (unless (and ignore-self
  184. (string= (erc-server-user-nickname (car user))
  185. (erc-current-nick)))
  186. (setq nicks (cons (concat (erc-server-user-nickname (car user))
  187. postfix)
  188. nicks))))
  189. (nreverse nicks)))
  190. (defun pcomplete-erc-all-nicks (&optional postfix)
  191. "Returns a list of all nicks on the current server."
  192. (let (nicks)
  193. (erc-with-server-buffer
  194. (maphash (lambda (nick user)
  195. (setq nicks (cons (concat nick postfix) nicks)))
  196. erc-server-users))
  197. nicks))
  198. (defun pcomplete-erc-channels ()
  199. "Returns a list of channels associated with the current server."
  200. (mapcar (lambda (buf) (with-current-buffer buf (erc-default-target)))
  201. (erc-channel-list erc-server-process)))
  202. ;;; Functions for parsing
  203. (defun pcomplete-erc-command-name ()
  204. "Returns the command name of the first argument."
  205. (if (eq (elt (pcomplete-arg 'first) 0) ?/)
  206. (upcase (substring (pcomplete-arg 'first) 1))
  207. "SAY"))
  208. (defun pcomplete-erc-parse-arguments ()
  209. "Returns a list of parsed whitespace-separated arguments.
  210. These are the words from the beginning of the line after the prompt
  211. up to where point is right now."
  212. (let* ((start erc-input-marker)
  213. (end (point))
  214. args beginnings)
  215. (save-excursion
  216. (if (< (skip-chars-backward " \t\n" start) 0)
  217. (setq args '("")
  218. beginnings (list end)))
  219. (setq end (point))
  220. (while (< (skip-chars-backward "^ \t\n" start) 0)
  221. (setq beginnings (cons (point) beginnings)
  222. args (cons (buffer-substring-no-properties
  223. (point) end)
  224. args))
  225. (skip-chars-backward " \t\n" start)
  226. (setq end (point))))
  227. (cons args beginnings)))
  228. (provide 'erc-pcomplete)
  229. ;;; erc-pcomplete.el ends here
  230. ;;
  231. ;; Local Variables:
  232. ;; indent-tabs-mode: nil
  233. ;; End: