erc-list.el 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. ;;; erc-list.el --- /list support for ERC -*- lexical-binding:t -*-
  2. ;; Copyright (C) 2008-2017 Free Software Foundation, Inc.
  3. ;; Author: Tom Tromey <tromey@redhat.com>
  4. ;; Maintainer: emacs-devel@gnu.org
  5. ;; Old-Version: 0.1
  6. ;; Keywords: comm
  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 provides nice support for /list in ERC.
  20. ;;; Code:
  21. (require 'erc)
  22. (defgroup erc-list nil
  23. "Support for the /list command."
  24. :group 'erc)
  25. ;; This is implicitly the width of the channel name column. Pick
  26. ;; something small enough that the topic has a chance of being
  27. ;; readable, but long enough that most channel names won't make for
  28. ;; strange formatting.
  29. (defconst erc-list-nusers-column 25)
  30. ;; Width of the number-of-users column.
  31. (defconst erc-list-topic-column (+ erc-list-nusers-column 10))
  32. ;; The list buffer. This is buffer local in the server buffer.
  33. (defvar erc-list-buffer nil)
  34. ;; The argument to the last "/list". This is buffer local in the
  35. ;; server buffer.
  36. (defvar erc-list-last-argument nil)
  37. ;; The server buffer corresponding to the list buffer. This is buffer
  38. ;; local in the list buffer.
  39. (defvar erc-list-server-buffer nil)
  40. ;; Define module:
  41. ;;;###autoload (autoload 'erc-list-mode "erc-list")
  42. (define-erc-module list nil
  43. "List channels nicely in a separate buffer."
  44. ((remove-hook 'erc-server-321-functions 'erc-server-321-message)
  45. (remove-hook 'erc-server-322-functions 'erc-server-322-message))
  46. ((erc-with-all-buffers-of-server nil
  47. #'erc-open-server-buffer-p
  48. (remove-hook 'erc-server-322-functions 'erc-list-handle-322 t))
  49. (add-hook 'erc-server-321-functions 'erc-server-321-message t)
  50. (add-hook 'erc-server-322-functions 'erc-server-322-message t)))
  51. ;; Format a record for display.
  52. (defun erc-list-make-string (channel users topic)
  53. (concat
  54. channel
  55. (erc-propertize " "
  56. 'display (list 'space :align-to erc-list-nusers-column)
  57. 'face 'fixed-pitch)
  58. users
  59. (erc-propertize " "
  60. 'display (list 'space :align-to erc-list-topic-column)
  61. 'face 'fixed-pitch)
  62. topic))
  63. ;; Insert a record into the list buffer.
  64. (defun erc-list-insert-item (channel users topic)
  65. (save-excursion
  66. (let ((buffer-read-only nil))
  67. (goto-char (point-max))
  68. (insert (erc-list-make-string channel users topic) "\n"))))
  69. (defun erc-list-join ()
  70. "Join the irc channel named on this line."
  71. (interactive)
  72. (unless (eobp)
  73. (beginning-of-line)
  74. (unless (looking-at "\\([&#+!][^ \n]+\\)")
  75. (error "Not looking at channel name?"))
  76. (let ((chan (match-string 1)))
  77. (with-current-buffer erc-list-server-buffer
  78. (erc-join-channel chan)))))
  79. (defun erc-list-kill ()
  80. "Kill the current ERC list buffer."
  81. (interactive)
  82. (kill-buffer (current-buffer)))
  83. (defun erc-list-revert ()
  84. "Refresh the list of channels."
  85. (interactive)
  86. (with-current-buffer erc-list-server-buffer
  87. (erc-cmd-LIST erc-list-last-argument)))
  88. (defun erc-list-menu-sort-by-column (&optional e)
  89. "Sort the channel list by the column clicked on."
  90. (interactive (list last-input-event))
  91. (if e (mouse-select-window e))
  92. (let* ((pos (event-start e))
  93. (obj (posn-object pos))
  94. (col (if obj
  95. (get-text-property (cdr obj) 'column-number (car obj))
  96. (get-text-property (posn-point pos) 'column-number))))
  97. (let ((buffer-read-only nil))
  98. (if (= col 1)
  99. (sort-fields col (point-min) (point-max))
  100. (sort-numeric-fields col (point-min) (point-max))))))
  101. (defvar erc-list-menu-mode-map
  102. (let ((map (make-keymap)))
  103. (set-keymap-parent map special-mode-map)
  104. (define-key map "k" 'erc-list-kill)
  105. (define-key map "j" 'erc-list-join)
  106. (define-key map "g" 'erc-list-revert)
  107. (define-key map "n" 'next-line)
  108. (define-key map "p" 'previous-line)
  109. map)
  110. "Local keymap for `erc-list-mode' buffers.")
  111. (defvar erc-list-menu-sort-button-map
  112. (let ((map (make-sparse-keymap)))
  113. (define-key map [header-line mouse-1] 'erc-list-menu-sort-by-column)
  114. (define-key map [follow-link] 'mouse-face)
  115. map)
  116. "Local keymap for ERC list menu mode sorting buttons.")
  117. ;; Helper function that makes a buttonized column header.
  118. (defun erc-list-button (title column)
  119. (erc-propertize title
  120. 'column-number column
  121. 'help-echo "mouse-1: sort by column"
  122. 'mouse-face 'header-line-highlight
  123. 'keymap erc-list-menu-sort-button-map))
  124. (define-derived-mode erc-list-menu-mode special-mode "ERC-List"
  125. "Major mode for editing a list of irc channels."
  126. (setq header-line-format
  127. (concat
  128. (erc-propertize " "
  129. 'display '(space :align-to 0)
  130. 'face 'fixed-pitch)
  131. (erc-list-make-string (erc-list-button "Channel" 1)
  132. (erc-list-button "# Users" 2)
  133. "Topic")))
  134. (setq truncate-lines t))
  135. (put 'erc-list-menu-mode 'mode-class 'special)
  136. ;; Handle a "322" response. This response tells us about a single
  137. ;; channel.
  138. ;; Called via erc-once-with-server-event with two arguments.
  139. (defun erc-list-handle-322 (_proc parsed)
  140. (let* ((args (cdr (erc-response.command-args parsed)))
  141. (channel (car args))
  142. (nusers (car (cdr args)))
  143. (topic (erc-response.contents parsed)))
  144. (when (buffer-live-p erc-list-buffer)
  145. (with-current-buffer erc-list-buffer
  146. (erc-list-insert-item channel nusers topic))))
  147. ;; Don't let another hook run.
  148. t)
  149. ;; Helper function to install our 322 handler and make our buffer.
  150. (defun erc-list-install-322-handler (server-buffer)
  151. (with-current-buffer server-buffer
  152. ;; Arrange for 322 responses to insert into our buffer.
  153. (add-hook 'erc-server-322-functions 'erc-list-handle-322 t t)
  154. ;; Arrange for 323 (end of list) to end this.
  155. (erc-once-with-server-event
  156. 323
  157. (lambda (_proc _parsed)
  158. (remove-hook 'erc-server-322-functions 'erc-list-handle-322 t)))
  159. ;; Find the list buffer, empty it, and display it.
  160. (set (make-local-variable 'erc-list-buffer)
  161. (get-buffer-create (concat "*Channels of "
  162. erc-server-announced-name
  163. "*")))
  164. (with-current-buffer erc-list-buffer
  165. (erc-list-menu-mode)
  166. (setq buffer-read-only nil)
  167. (erase-buffer)
  168. (set (make-local-variable 'erc-list-server-buffer) server-buffer)
  169. (setq buffer-read-only t))
  170. (pop-to-buffer erc-list-buffer))
  171. t)
  172. ;; The main entry point.
  173. (defun erc-cmd-LIST (&optional line)
  174. "Show a listing of channels on the current server in a separate window.
  175. If LINE is specified, include it with the /LIST command. It
  176. should usually be one or more channels, separated by commas.
  177. Please note that this function only works with IRC servers which conform
  178. to RFC and send the LIST header (#321) at start of list transmission."
  179. (erc-with-server-buffer
  180. (set (make-local-variable 'erc-list-last-argument) line)
  181. (erc-once-with-server-event
  182. 321
  183. (let ((buf (current-buffer)))
  184. (lambda (_proc _parsed)
  185. (erc-list-install-322-handler buf)))))
  186. (erc-server-send (concat "LIST :" (or (and line (substring line 1))
  187. ""))))
  188. (put 'erc-cmd-LIST 'do-not-parse-args t)
  189. (provide 'erc-list)
  190. ;;; erc-list.el ends here
  191. ;;
  192. ;; Local Variables:
  193. ;; indent-tabs-mode: t
  194. ;; tab-width: 8
  195. ;; End: