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