erc-list.el 7.3 KB

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