erc-pcomplete.el 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. ;;; erc-pcomplete.el --- Provides programmable completion for ERC
  2. ;; Copyright (C) 2002-2004, 2006-2017 Free Software Foundation, Inc.
  3. ;; Author: Sacha Chua <sacha@free.net.ph>
  4. ;; Maintainer: emacs-devel@gnu.org
  5. ;; Keywords: comm, convenience
  6. ;; URL: http://www.emacswiki.org/cgi-bin/wiki.pl?ErcCompletion
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; This file replaces erc-complete.el. It provides nick completion
  20. ;; for ERC based on pcomplete. If you do not have pcomplete, you may
  21. ;; try to use erc-complete.el.
  22. ;;
  23. ;; To use, (require 'erc-auto) or (require 'erc-pcomplete), then
  24. ;; (erc-pcomplete-mode 1)
  25. ;;
  26. ;; If you want nickname completions ordered such that the most recent
  27. ;; speakers are listed first, set
  28. ;; `erc-pcomplete-order-nickname-completions' to t.
  29. ;;
  30. ;; See CREDITS for other contributors.
  31. ;;
  32. ;;; Code:
  33. (require 'pcomplete)
  34. (require 'erc)
  35. (require 'erc-compat)
  36. (require 'time-date)
  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. (declare (obsolete completion-at-point "25.1"))
  69. (interactive)
  70. (when (> (point) (erc-beg-of-input-line))
  71. (let ((completion-at-point-functions '(erc-pcompletions-at-point)))
  72. (completion-at-point))))
  73. ;;; Setup function
  74. (defun pcomplete-erc-setup ()
  75. "Setup `erc-mode' to use pcomplete."
  76. (set (make-local-variable 'pcomplete-ignore-case)
  77. t)
  78. (set (make-local-variable 'pcomplete-use-paring)
  79. nil)
  80. (set (make-local-variable 'pcomplete-parse-arguments-function)
  81. 'pcomplete-erc-parse-arguments)
  82. (set (make-local-variable 'pcomplete-command-completion-function)
  83. 'pcomplete/erc-mode/complete-command)
  84. (set (make-local-variable 'pcomplete-command-name-function)
  85. 'pcomplete-erc-command-name)
  86. (set (make-local-variable 'pcomplete-default-completion-function)
  87. (lambda () (pcomplete-here (pcomplete-erc-nicks)))))
  88. ;;; Programmable completion logic
  89. (defun pcomplete/erc-mode/complete-command ()
  90. (pcomplete-here
  91. (append
  92. (pcomplete-erc-commands)
  93. (pcomplete-erc-nicks erc-pcomplete-nick-postfix t))))
  94. (defvar erc-pcomplete-ctcp-commands
  95. '("ACTION" "CLIENTINFO" "ECHO" "FINGER" "PING" "TIME" "USERINFO" "VERSION"))
  96. (defun pcomplete/erc-mode/CTCP ()
  97. (pcomplete-here (pcomplete-erc-nicks))
  98. (pcomplete-here erc-pcomplete-ctcp-commands))
  99. (defun pcomplete/erc-mode/CLEARTOPIC ()
  100. (pcomplete-here (pcomplete-erc-channels)))
  101. (defun pcomplete/erc-mode/DEOP ()
  102. (while (pcomplete-here (pcomplete-erc-ops))))
  103. (defun pcomplete/erc-mode/DESCRIBE ()
  104. (pcomplete-here (pcomplete-erc-nicks)))
  105. (defun pcomplete/erc-mode/IDLE ()
  106. (while (pcomplete-here (pcomplete-erc-nicks))))
  107. (defun pcomplete/erc-mode/KICK ()
  108. (pcomplete-here (pcomplete-erc-channels))
  109. (pcomplete-here (pcomplete-erc-nicks)))
  110. (defun pcomplete/erc-mode/LOAD ()
  111. (pcomplete-here (pcomplete-entries)))
  112. (defun pcomplete/erc-mode/MODE ()
  113. (pcomplete-here (pcomplete-erc-channels))
  114. (while (pcomplete-here (pcomplete-erc-nicks))))
  115. (defun pcomplete/erc-mode/ME ()
  116. (while (pcomplete-here (pcomplete-erc-nicks))))
  117. (defun pcomplete/erc-mode/SAY ()
  118. (pcomplete-here (pcomplete-erc-nicks))
  119. (pcomplete-here (pcomplete-erc-nicks))
  120. (while (pcomplete-here (pcomplete-erc-nicks))))
  121. (defun pcomplete/erc-mode/MSG ()
  122. (pcomplete-here (append (pcomplete-erc-all-nicks)
  123. (pcomplete-erc-channels)))
  124. (while (pcomplete-here (pcomplete-erc-nicks))))
  125. (defun pcomplete/erc-mode/NAMES ()
  126. (while (pcomplete-here (pcomplete-erc-channels))))
  127. (defalias 'pcomplete/erc-mode/NOTICE 'pcomplete/erc-mode/MSG)
  128. (defun pcomplete/erc-mode/OP ()
  129. (while (pcomplete-here (pcomplete-erc-not-ops))))
  130. (defun pcomplete/erc-mode/PART ()
  131. (pcomplete-here (pcomplete-erc-channels)))
  132. (defalias 'pcomplete/erc-mode/LEAVE 'pcomplete/erc-mode/PART)
  133. (defun pcomplete/erc-mode/QUERY ()
  134. (pcomplete-here (append (pcomplete-erc-all-nicks)
  135. (pcomplete-erc-channels)))
  136. (while (pcomplete-here (pcomplete-erc-nicks)))
  137. )
  138. (defun pcomplete/erc-mode/SOUND ()
  139. (while (pcomplete-here (pcomplete-entries))))
  140. (defun pcomplete/erc-mode/TOPIC ()
  141. (pcomplete-here (pcomplete-erc-channels)))
  142. (defun pcomplete/erc-mode/WHOIS ()
  143. (while (pcomplete-here (pcomplete-erc-nicks))))
  144. (defun pcomplete/erc-mode/UNIGNORE ()
  145. (pcomplete-here (erc-with-server-buffer erc-ignore-list)))
  146. ;;; Functions that provide possible completions.
  147. (defun pcomplete-erc-commands ()
  148. "Returns a list of strings of the defined user commands."
  149. (let ((case-fold-search nil))
  150. (mapcar (lambda (x)
  151. (concat "/" (downcase (substring (symbol-name x) 8))))
  152. (apropos-internal "erc-cmd-[A-Z]+"))))
  153. (defun pcomplete-erc-ops ()
  154. "Returns a list of nicks with ops."
  155. (let (ops)
  156. (maphash (lambda (nick cdata)
  157. (if (and (cdr cdata)
  158. (erc-channel-user-op (cdr cdata)))
  159. (setq ops (cons nick ops))))
  160. erc-channel-users)
  161. ops))
  162. (defun pcomplete-erc-not-ops ()
  163. "Returns a list of nicks without ops."
  164. (let (not-ops)
  165. (maphash (lambda (nick cdata)
  166. (if (and (cdr cdata)
  167. (not (erc-channel-user-op (cdr cdata))))
  168. (setq not-ops (cons nick not-ops))))
  169. erc-channel-users)
  170. not-ops))
  171. (defun pcomplete-erc-nicks (&optional postfix ignore-self)
  172. "Returns a list of nicks in the current channel.
  173. Optional argument POSTFIX is something to append to the nickname.
  174. If optional argument IGNORE-SELF is non-nil, don't return the current nick."
  175. (let ((users (if erc-pcomplete-order-nickname-completions
  176. (erc-sort-channel-users-by-activity
  177. (erc-get-channel-user-list))
  178. (erc-get-channel-user-list)))
  179. (nicks nil))
  180. (dolist (user users)
  181. (unless (or (not user)
  182. (and ignore-self
  183. (string= (erc-server-user-nickname (car user))
  184. (erc-current-nick))))
  185. (setq nicks (cons (concat (erc-server-user-nickname (car user))
  186. postfix)
  187. nicks))))
  188. (nreverse nicks)))
  189. (defun pcomplete-erc-all-nicks (&optional postfix)
  190. "Returns a list of all nicks on the current server."
  191. (let (nicks)
  192. (erc-with-server-buffer
  193. (maphash (lambda (_nick user)
  194. (setq nicks (cons
  195. (concat (erc-server-user-nickname user) postfix)
  196. nicks)))
  197. erc-server-users))
  198. nicks))
  199. (defun pcomplete-erc-channels ()
  200. "Returns a list of channels associated with the current server."
  201. (mapcar (lambda (buf) (with-current-buffer buf (erc-default-target)))
  202. (erc-channel-list erc-server-process)))
  203. ;;; Functions for parsing
  204. (defun pcomplete-erc-command-name ()
  205. "Returns the command name of the first argument."
  206. (if (eq (elt (pcomplete-arg 'first) 0) ?/)
  207. (upcase (substring (pcomplete-arg 'first) 1))
  208. "SAY"))
  209. (defun pcomplete-erc-parse-arguments ()
  210. "Returns a list of parsed whitespace-separated arguments.
  211. These are the words from the beginning of the line after the prompt
  212. up to where point is right now."
  213. (let* ((start erc-input-marker)
  214. (end (point))
  215. args beginnings)
  216. (save-excursion
  217. (if (< (skip-chars-backward " \t\n" start) 0)
  218. (setq args '("")
  219. beginnings (list end)))
  220. (setq end (point))
  221. (while (< (skip-chars-backward "^ \t\n" start) 0)
  222. (setq beginnings (cons (point) beginnings)
  223. args (cons (buffer-substring-no-properties
  224. (point) end)
  225. args))
  226. (skip-chars-backward " \t\n" start)
  227. (setq end (point))))
  228. (cons args beginnings)))
  229. (provide 'erc-pcomplete)
  230. ;;; erc-pcomplete.el ends here
  231. ;;
  232. ;; Local Variables:
  233. ;; indent-tabs-mode: nil
  234. ;; End: