1 ;;; erc-list.el --- Provide a faster channel listing mechanism
3 ;; Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
4 ;; Copyright (C) 2004 Brian Palmer
6 ;; Author: Mario Lang <mlang@lexx.delysid.org>
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 2, or (at your option)
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; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
28 ;; This file provides a simple derived mode for viewing Channel lists.
29 ;; It also serves as a demonstration of how the new server hook facility
35 (require 'erc-networks
)
37 (unless (fboundp 'make-overlay
)
39 (eval-when-compile (require 'cl
))
41 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
42 ;; User customizable variables.
43 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
45 (defgroup erc-list nil
46 "Display IRC channels in another window when using /LIST"
49 (defcustom erc-chanlist-progress-message t
50 "*Show progress message while accumulating channel list."
54 (defcustom erc-no-list-networks nil
55 "*A list of network names on which the /LIST command refuses to work."
57 :type
'(repeat string
))
59 (defcustom erc-chanlist-frame-parameters nil
60 "*If nil, the channel list is displayed in a new window; if non-nil,
61 this variable holds the frame parameters used to make a frame to
62 display the channel list."
66 (defcustom erc-chanlist-hide-modeline nil
67 "*If nil, the channel list buffer has a modeline, otherwise the modeline is hidden."
71 (defface erc-chanlist-header-face
'((t (:bold t
)))
72 "Face used for the headers in erc's channel list."
75 (defface erc-chanlist-odd-line-face
'((t (:inverse-video t
)))
76 "Face used for the odd lines in erc's channel list."
79 (defface erc-chanlist-even-line-face
'((t (:inverse-video nil
)))
80 "Face used for the even lines in erc's channel list."
83 (defface erc-chanlist-highlight
'((t (:foreground
"red")))
84 "Face used to highlight the current line in the channel list."
87 ;; This should perhaps be a defface that inherits values from the highlight face
88 ;; but xemacs does not support inheritance
89 (defcustom erc-chanlist-highlight-face
'erc-chanlist-highlight
90 "Face used for highlighting the current line in a list."
95 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
96 ;; All variables below this line are for internal use only.
97 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
99 (defvar erc-chanlist-channel-line-regexp
"^\\([#&\\*][^ \t\n]*\\)\\s-+[0-9]+"
100 "Regexp that matches a channel line in the channel list buffer.")
102 (defvar erc-chanlist-buffer nil
)
103 (make-variable-buffer-local 'erc-chanlist-buffer
)
105 (defvar erc-chanlist-last-time
0
106 "A time value used to throttle the progress indicator.")
108 (defvar erc-chanlist-frame nil
109 "The frame displaying the most recent channel list buffer.")
111 (defvar erc-chanlist-sort-state
'channel
112 "The sort mode of the channel list buffer. Either 'channel or 'users.")
113 (make-variable-buffer-local 'erc-chanlist-sort-state
)
115 (defvar erc-chanlist-highlight-overlay nil
116 "The overlay used for erc chanlist highlighting")
117 (make-variable-buffer-local 'erc-chanlist-highlight-overlay
)
119 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
120 ;; Define erc-chanlist-mode.
121 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
123 (defcustom erc-chanlist-mode-hook nil
124 "Hook run by erc-chanlist-mode."
128 (define-derived-mode erc-chanlist-mode fundamental-mode
"ERC Channel List"
129 "Mode for viewing a channel list of a particular server.
131 \\{erc-chanlist-mode-map}"
132 (local-set-key "\C-c\C-j" 'erc-join-channel
)
133 (local-set-key "j" 'erc-chanlist-join-channel
)
134 (local-set-key "n" 'next-line
)
135 (local-set-key "p" 'previous-line
)
136 (local-set-key "q" 'erc-chanlist-quit
)
137 (local-set-key "s" 'erc-chanlist-toggle-sort-state
)
138 (local-set-key "t" 'toggle-truncate-lines
)
139 (setq erc-chanlist-sort-state
'channel
)
140 (setq truncate-lines t
)
141 (add-hook 'post-command-hook
'erc-chanlist-post-command-hook
'append
'local
))
144 ;;;###autoload (autoload 'erc-list-mode "erc-list")
145 (define-erc-module list nil
146 "List channels nicely in a separate buffer."
147 ((defalias 'erc-cmd-LIST
'erc-list-channels
))
148 ((defalias 'erc-cmd-LIST
'erc-list-channels-simple
)))
150 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
152 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
155 (defun erc-list-channels (&rest channel
)
156 "Display a buffer containing a list of channels on the current server.
157 Optional argument CHANNEL specifies a single channel to list (instead of every
160 (remove "" (split-string
161 (read-from-minibuffer "List channels (RET for all): ") " ")))
162 (if (and (null channel
)
163 (erc-member-ignore-case (erc-network-name) erc-no-list-networks
))
164 (erc-display-line "ERC is configured not to allow the /LIST command on this network!"
166 (erc-display-line (erc-make-notice (concat "Listing channel"
169 "s. This may take a while."))))
170 (erc-chanlist channel
))
173 (defun erc-list-channels-simple (&optional line
)
174 "Send the LIST command to the current server with optional channels LINE."
175 (when (string-match "^\\s-*\\(.*\\)$" line
)
176 (let ((channels (match-string 1 line
)))
177 (erc-log (format "cmd: LIST: %s" channels
))
179 (if (string= channels
"")
181 (concat "LIST :" channels
))))
183 (put 'erc-list-channels-simple
'do-not-parse-args t
)
186 (defun erc-chanlist (&optional channels
)
187 "Show a channel listing of the current server in a special mode.
188 Please note that this function only works with IRC servers which conform
189 to RFC and send the LIST header (#321) at start of list transmission."
191 (with-current-buffer (erc-server-buffer)
192 (erc-once-with-server-event
195 (add-hook 'erc-server-322-functions
'erc-chanlist-322 nil t
)
197 (erc-once-with-server-event
200 (remove-hook 'erc-server-322-functions
'erc-chanlist-322 t
)
201 (let ((buf erc-chanlist-buffer
))
202 (if (not (buffer-live-p buf
))
203 (error "`erc-chanlist-buffer' does not refer to a live buffer"))
206 (buffer-disable-undo)
207 (let (buffer-read-only
209 (sort-lines nil
(point-min) (point-max))
210 (setq erc-chanlist-sort-state
'channel
)
212 (let ((sum (count-lines (point-min) (point-max))))
213 (goto-char (point-min))
214 (insert (substitute-command-keys
215 (concat "'\\[erc-chanlist-toggle-sort-state]' toggle sort mode.\n"
216 "'\\[erc-chanlist-quit]' kill this buffer.\n"
217 "'\\[toggle-truncate-lines]' toggle line truncation.\n"
218 "'\\[erc-chanlist-join-channel]' join the channel listed on the current line.\n\n")))
219 (insert (format "%d channels (sorted by %s).\n\n"
220 sum
(if (eq erc-chanlist-sort-state
'channel
)
222 "number of users"))))
224 (insert (format "%-25s%5s %s\n------------------------ ----- ----------------------------\n"
229 ;; Display the channel list buffer.
230 (if erc-chanlist-frame-parameters
232 (if (or (null erc-chanlist-frame
)
233 (not (frame-live-p erc-chanlist-frame
)))
234 (setq erc-chanlist-frame
235 (make-frame `((name .
,(format "Channels on %s"
237 ,@erc-chanlist-frame-parameters
))))
238 (select-frame erc-chanlist-frame
)
239 (switch-to-buffer buf
)
240 (erc-prettify-channel-list))
242 (erc-prettify-channel-list))))
243 (goto-char (point-min))
244 (search-forward-regexp "^------" nil t
)
246 (erc-chanlist-highlight-line)
250 (setq erc-chanlist-buffer
(get-buffer-create
251 (format "*Channels on %s*"
252 (erc-response.sender parsed
))))
253 (with-current-buffer erc-chanlist-buffer
254 (setq buffer-read-only nil
)
257 (setq erc-server-process proc
)
258 (if erc-chanlist-hide-modeline
259 (setq mode-line-format nil
))
260 (setq buffer-read-only t
))
263 ;; Now that we've setup our callbacks, pull the trigger.
265 (message "Collecting channel list for server %s" erc-session-server
))
266 (erc-server-send (if (null channels
)
269 (mapconcat #'identity channels
","))))))
271 (defun erc-chanlist-322 (proc parsed
)
272 "Process an IRC 322 message.
274 The message carries information about one channel for the LIST
276 (multiple-value-bind (channel num-users
)
277 (cdr (erc-response.command-args parsed
))
278 (let ((topic (erc-response.contents parsed
)))
279 (with-current-buffer erc-chanlist-buffer
281 (goto-char (point-max))
282 (let (buffer-read-only)
283 (insert (format "%-26s%4s %s\n" (erc-controls-strip channel
)
285 (erc-controls-strip topic
))))
287 ;; Maybe display a progress indicator in the minibuffer.
288 (when (and erc-chanlist-progress-message
290 erc-chanlist-last-time
(erc-current-time))
292 (setq erc-chanlist-last-time
(erc-current-time))
293 (message "Accumulating channel list ... %c"
294 (aref [?
/ ?| ?
\\ ?- ?
! ?O ?o
] (random 7))))
296 ;; Return success to prevent other hook functions from being run.
299 (defun erc-chanlist-post-command-hook ()
300 "Keep the current line highlighted."
304 (if (looking-at erc-chanlist-channel-line-regexp
)
305 (erc-chanlist-highlight-line)
306 (erc-chanlist-dehighlight-line)))))
308 (defun erc-chanlist-highlight-line ()
309 "Highlight the current line."
310 (unless erc-chanlist-highlight-overlay
311 (setq erc-chanlist-highlight-overlay
312 (make-overlay (point-min) (point-min)))
313 ;; Detach it from the buffer.
314 (delete-overlay erc-chanlist-highlight-overlay
)
315 (overlay-put erc-chanlist-highlight-overlay
316 'face erc-chanlist-highlight-face
)
317 ;; Expressly put it at a higher priority than the text
318 ;; properties used for faces later on. Gnu emacs promises that
319 ;; right now overlays are higher priority than text properties,
320 ;; but why take chances?
321 (overlay-put erc-chanlist-highlight-overlay
'priority
1))
322 (move-overlay erc-chanlist-highlight-overlay
(point) (1+ (point-at-eol))))
324 (defun erc-chanlist-dehighlight-line ()
325 "Remove the line highlighting."
326 (delete-overlay erc-chanlist-highlight-overlay
))
328 (defun erc-prettify-channel-list ()
329 "Make the channel list buffer look pretty.
330 When this function runs, the current buffer must be the channel
331 list buffer, or it does nothing."
332 (if (eq major-mode
'erc-chanlist-mode
)
334 (let ((inhibit-read-only t
))
335 (goto-char (point-min))
336 (when (search-forward-regexp "^-------" nil t
)
338 (point-min) (1+ (point-at-eol)) '(face erc-chanlist-header-face
))
343 (point) (1+ (point-at-eol)) '(face erc-chanlist-odd-line-face
))
347 (point) (1+ (point-at-eol)) '(face erc-chanlist-even-line-face
)))
348 (forward-line 1))))))
350 (defun erc-chanlist-toggle-sort-state ()
351 "Toggle the channel list buffer sorting method.
352 Either sort by channel names or by number of users in each channel."
354 (let ((inhibit-read-only t
)
357 (goto-char (point-min))
358 (search-forward-regexp "^-----" nil t
)
361 (if (eq erc-chanlist-sort-state
'channel
)
363 (sort-numeric-fields 2 (point) (point-max))
364 (reverse-region (point) (point-max))
365 (setq erc-chanlist-sort-state
'users
))
366 (sort-lines nil
(point) (point-max))
367 (setq erc-chanlist-sort-state
'channel
))
369 (goto-char (point-min))
370 (if (search-forward-regexp "^[0-9]+ channels (sorted by \\(.*\\)).$"
372 (replace-match (if (eq erc-chanlist-sort-state
'channel
)
377 (goto-char (point-min))
378 (search-forward-regexp "^-----" nil t
)
382 (erc-prettify-channel-list)))))
384 (defun erc-chanlist-quit ()
386 Kill the channel list buffer, window, and frame (if there's a frame
387 devoted to the channel list)."
389 (kill-buffer (current-buffer))
390 (if (eq (selected-frame) erc-chanlist-frame
)
394 (defun erc-chanlist-join-channel ()
395 "Join the channel listed on the current line of the channel list buffer.
396 Private channels, which are shown as asterisks (*), are ignored."
400 (when (looking-at erc-chanlist-channel-line-regexp
)
401 (let ((channel-name (match-string 1)))
402 (when (and (stringp channel-name
)
403 (not (string= channel-name
"*")))
404 (run-at-time 0.5 nil
'erc-join-channel channel-name
))))))
408 ;;; erc-list.el ends here
411 ;; indent-tabs-mode: t
415 ;; arch-tag: 4a13196a-a61b-465a-9926-044dfbc7e5ff