Fix bug #2496 with column counting in presence of display tables.
[emacs.git] / lisp / erc / erc-list.el
blobb8eb5a4aa1997e5b57c2bf66c33d9e0c31e9f1f0
1 ;;; erc-list.el --- /list support for ERC
3 ;; Copyright (C) 2008-2011 Free Software Foundation, Inc.
5 ;; Author: Tom Tromey <tromey@redhat.com>
6 ;; Version: 0.1
7 ;; Keywords: comm
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
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.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; This file provides nice support for /list in ERC.
28 ;;; Code:
30 (require 'erc)
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)
38 ;; Width of the number-of-users column.
39 (defconst erc-list-topic-column (+ erc-list-nusers-column 10))
41 ;; The list buffer. This is buffer local in the server buffer.
42 (defvar erc-list-buffer nil)
44 ;; The argument to the last "/list". This is buffer local in the
45 ;; server buffer.
46 (defvar erc-list-last-argument nil)
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)
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)))
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))
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"))))
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)))))
95 (defun erc-list-kill ()
96 "Kill the current ERC list buffer."
97 (interactive)
98 (kill-buffer (current-buffer)))
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)))
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))))))
120 (defvar erc-list-menu-mode-map
121 (let ((map (make-keymap)))
122 (set-keymap-parent map special-mode-map)
123 (define-key map "k" 'erc-list-kill)
124 (define-key map "j" 'erc-list-join)
125 (define-key map "g" 'erc-list-revert)
126 (define-key map "n" 'next-line)
127 (define-key map "p" 'previous-line)
128 map)
129 "Local keymap for `erc-list-mode' buffers.")
131 (defvar erc-list-menu-sort-button-map
132 (let ((map (make-sparse-keymap)))
133 (define-key map [header-line mouse-1] 'erc-list-menu-sort-by-column)
134 (define-key map [follow-link] 'mouse-face)
135 map)
136 "Local keymap for ERC list menu mode sorting buttons.")
138 ;; Helper function that makes a buttonized column header.
139 (defun erc-list-button (title column)
140 (erc-propertize title
141 'column-number column
142 'help-echo "mouse-1: sort by column"
143 'mouse-face 'highlight
144 'keymap erc-list-menu-sort-button-map))
146 (define-derived-mode erc-list-menu-mode special-mode "ERC-List"
147 "Major mode for editing a list of irc channels."
148 (setq header-line-format
149 (concat
150 (erc-propertize " "
151 'display '(space :align-to 0)
152 'face 'fixed-pitch)
153 (erc-list-make-string (erc-list-button "Channel" 1)
154 (erc-list-button "# Users" 2)
155 "Topic")))
156 (setq truncate-lines t))
158 (put 'erc-list-menu-mode 'mode-class 'special)
160 ;; Handle a "322" response. This response tells us about a single
161 ;; channel.
162 (defun erc-list-handle-322 (proc parsed)
163 (let* ((args (cdr (erc-response.command-args parsed)))
164 (channel (car args))
165 (nusers (car (cdr args)))
166 (topic (erc-response.contents parsed)))
167 (when (buffer-live-p erc-list-buffer)
168 (with-current-buffer erc-list-buffer
169 (erc-list-insert-item channel nusers topic))))
170 ;; Don't let another hook run.
173 ;; Helper function to install our 322 handler and make our buffer.
174 (defun erc-list-install-322-handler (server-buffer)
175 (with-current-buffer server-buffer
176 ;; Arrange for 322 responses to insert into our buffer.
177 (add-hook 'erc-server-322-functions 'erc-list-handle-322 t t)
178 ;; Arrange for 323 (end of list) to end this.
179 (erc-once-with-server-event
181 '(progn
182 (remove-hook 'erc-server-322-functions 'erc-list-handle-322 t)))
183 ;; Find the list buffer, empty it, and display it.
184 (set (make-local-variable 'erc-list-buffer)
185 (get-buffer-create (concat "*Channels of "
186 erc-server-announced-name
187 "*")))
188 (with-current-buffer erc-list-buffer
189 (erc-list-menu-mode)
190 (setq buffer-read-only nil)
191 (erase-buffer)
192 (set (make-local-variable 'erc-list-server-buffer) server-buffer)
193 (setq buffer-read-only t))
194 (pop-to-buffer erc-list-buffer))
197 ;; The main entry point.
198 (defun erc-cmd-LIST (&optional line)
199 "Show a listing of channels on the current server in a separate window.
201 If LINE is specified, include it with the /LIST command. It
202 should usually be one or more channels, separated by commas.
204 Please note that this function only works with IRC servers which conform
205 to RFC and send the LIST header (#321) at start of list transmission."
206 (erc-with-server-buffer
207 (set (make-local-variable 'erc-list-last-argument) line)
208 (erc-once-with-server-event
210 (list 'progn
211 (list 'erc-list-install-322-handler (current-buffer)))))
212 (erc-server-send (concat "LIST :" (or (and line (substring line 1))
213 ""))))
214 (put 'erc-cmd-LIST 'do-not-parse-args t)
216 ;;; erc-list.el ends here
218 ;; Local Variables:
219 ;; indent-tabs-mode: t
220 ;; tab-width: 8
221 ;; End: