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