1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
|
;;; erc-speedbar.el --- Speedbar support for ERC
;; Copyright (C) 2001-2004, 2006-2012 Free Software Foundation, Inc.
;; Author: Mario Lang <mlang@delysid.org>
;; Contributor: Eric M. Ludlam <eric@siege-engine.com>
;; This file is part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; This module provides integration of ERC into the Speedbar.
;;; TODO / ideas:
;; * Write intelligent update function:
;; update-channel, update-nick, remove-nick-from-channel, ...
;; * Use indicator-strings for op/voice
;; * Extract/convert face notes field from bbdb if available and show
;; it using sb-image.el
;;
;;; Code:
(require 'erc)
(require 'speedbar)
(condition-case nil (require 'dframe) (error nil))
(eval-when-compile (require 'cl))
;;; Customization:
(defgroup erc-speedbar nil
"Integration of ERC in the Speedbar"
:group 'erc)
(defcustom erc-speedbar-sort-users-type 'activity
"How channel nicknames are sorted.
'activity - Sort users by channel activity
'alphabetical - Sort users alphabetically
nil - Do not sort users"
:group 'erc-speedbar
:type '(choice (const :tag "Sort users by channel activity" activity)
(const :tag "Sort users alphabetically" alphabetical)
(const :tag "Do not sort users" nil)))
(defvar erc-speedbar-key-map nil
"Keymap used when in erc display mode.")
(defun erc-install-speedbar-variables ()
"Install those variables used by speedbar to enhance ERC."
(if erc-speedbar-key-map
nil
(setq erc-speedbar-key-map (speedbar-make-specialized-keymap))
;; Basic tree features
(define-key erc-speedbar-key-map "e" 'speedbar-edit-line)
(define-key erc-speedbar-key-map "\C-m" 'speedbar-edit-line)
(define-key erc-speedbar-key-map "+" 'speedbar-expand-line)
(define-key erc-speedbar-key-map "=" 'speedbar-expand-line)
(define-key erc-speedbar-key-map "-" 'speedbar-contract-line))
(speedbar-add-expansion-list '("ERC" erc-speedbar-menu-items
erc-speedbar-key-map
erc-speedbar-server-buttons))
(speedbar-add-mode-functions-list
'("ERC" (speedbar-item-info . erc-speedbar-item-info))))
(defvar erc-speedbar-menu-items
'(["Goto buffer" speedbar-edit-line t]
["Expand Node" speedbar-expand-line
(save-excursion (beginning-of-line)
(looking-at "[0-9]+: *.\\+. "))]
["Contract Node" speedbar-contract-line
(save-excursion (beginning-of-line)
(looking-at "[0-9]+: *.-. "))])
"Additional menu-items to add to speedbar frame.")
;; Make sure our special speedbar major mode is loaded
(if (featurep 'speedbar)
(erc-install-speedbar-variables)
(add-hook 'speedbar-load-hook 'erc-install-speedbar-variables))
;;; ERC hierarchy display method
;;;###autoload
(defun erc-speedbar-browser ()
"Initialize speedbar to display an ERC browser.
This will add a speedbar major display mode."
(interactive)
(require 'speedbar)
;; Make sure that speedbar is active
(speedbar-frame-mode 1)
;; Now, throw us into Info mode on speedbar.
(speedbar-change-initial-expansion-list "ERC")
(speedbar-get-focus))
(defun erc-speedbar-buttons (buffer)
"Create buttons for speedbar in BUFFER."
(erase-buffer)
(let (serverp chanp queryp)
(with-current-buffer buffer
(setq serverp (erc-server-buffer-p))
(setq chanp (erc-channel-p (erc-default-target)))
(setq queryp (erc-query-buffer-p)))
(cond (serverp
(erc-speedbar-channel-buttons nil 0 buffer))
(chanp
(erc-speedbar-insert-target buffer 0)
(forward-line -1)
(erc-speedbar-expand-channel "+" buffer 0))
(queryp
(erc-speedbar-insert-target buffer 0))
(t (ignore)))))
(defun erc-speedbar-server-buttons (directory depth)
"Insert the initial list of servers you are connected to."
(let ((servers (erc-buffer-list
(lambda ()
(eq (current-buffer)
(process-buffer erc-server-process))))))
(when servers
(speedbar-with-writable
(dolist (server servers)
(speedbar-make-tag-line
'bracket ?+ 'erc-speedbar-expand-server server
(buffer-name server) 'erc-speedbar-goto-buffer server nil
depth))
t))))
(defun erc-speedbar-expand-server (text server indent)
(cond ((string-match "+" text)
(speedbar-change-expand-button-char ?-)
(if (speedbar-with-writable
(save-excursion
(end-of-line) (forward-char 1)
(erc-speedbar-channel-buttons nil (1+ indent) server)))
(speedbar-change-expand-button-char ?-)
(speedbar-change-expand-button-char ??)))
((string-match "-" text) ;we have to contract this node
(speedbar-change-expand-button-char ?+)
(speedbar-delete-subblock indent))
(t (error "Ooops... not sure what to do")))
(speedbar-center-buffer-smartly))
(defun erc-speedbar-channel-buttons (directory depth server-buffer)
(when (get-buffer server-buffer)
(let* ((proc (with-current-buffer server-buffer erc-server-process))
(targets (erc-buffer-list
(lambda ()
(not (eq (process-buffer erc-server-process)
(current-buffer))))
proc)))
(when targets
(speedbar-with-writable
(dolist (target targets)
(erc-speedbar-insert-target target depth))
t)))))
(defun erc-speedbar-insert-target (buffer depth)
(if (with-current-buffer buffer
(erc-channel-p (erc-default-target)))
(speedbar-make-tag-line
'bracket ?+ 'erc-speedbar-expand-channel buffer
(buffer-name buffer) 'erc-speedbar-goto-buffer buffer nil
depth)
;; Query target
(speedbar-make-tag-line
nil nil nil nil
(buffer-name buffer) 'erc-speedbar-goto-buffer buffer nil
depth)))
(defun erc-speedbar-expand-channel (text channel indent)
"For the line matching TEXT, in CHANNEL, expand or contract a line.
INDENT is the current indentation level."
(cond
((string-match "+" text)
(speedbar-change-expand-button-char ?-)
(speedbar-with-writable
(save-excursion
(end-of-line) (forward-char 1)
(let ((modes (with-current-buffer channel
(concat (apply 'concat
erc-channel-modes)
(cond
((and erc-channel-user-limit
erc-channel-key)
(if erc-show-channel-key-p
(format "lk %.0f %s"
erc-channel-user-limit
erc-channel-key)
(format "kl %.0f" erc-channel-user-limit)))
(erc-channel-user-limit
;; Emacs has no bignums
(format "l %.0f" erc-channel-user-limit))
(erc-channel-key
(if erc-show-channel-key-p
(format "k %s" erc-channel-key)
"k"))
(t "")))))
(topic (erc-controls-interpret
(with-current-buffer channel erc-channel-topic))))
(speedbar-make-tag-line
'angle ?i nil nil
(concat "Modes: +" modes) nil nil nil
(1+ indent))
(unless (string= topic "")
(speedbar-make-tag-line
'angle ?i nil nil
(concat "Topic: " topic) nil nil nil
(1+ indent)))
(let ((names (cond ((eq erc-speedbar-sort-users-type 'alphabetical)
(erc-sort-channel-users-alphabetically
(with-current-buffer channel
(erc-get-channel-user-list))))
((eq erc-speedbar-sort-users-type 'activity)
(erc-sort-channel-users-by-activity
(with-current-buffer channel
(erc-get-channel-user-list))))
(t (with-current-buffer channel
(erc-get-channel-user-list))))))
(when names
(speedbar-with-writable
(dolist (entry names)
(erc-speedbar-insert-user entry ?+ (1+ indent))))))))))
((string-match "-" text)
(speedbar-change-expand-button-char ?+)
(speedbar-delete-subblock indent))
(t (error "Ooops... not sure what to do")))
(speedbar-center-buffer-smartly))
(defun erc-speedbar-insert-user (entry exp-char indent)
"Insert one user based on the channel member list ENTRY.
EXP-CHAR is the expansion character to use.
INDENT is the current indentation level."
(let* ((user (car entry))
(cuser (cdr entry))
(nick (erc-server-user-nickname user))
(host (erc-server-user-host user))
(info (erc-server-user-info user))
(login (erc-server-user-login user))
(name (erc-server-user-full-name user))
(voice (and cuser (erc-channel-user-voice cuser)))
(op (and cuser (erc-channel-user-op cuser)))
(nick-str (concat (if op "@" "") (if voice "+" "") nick))
(finger (concat login (when (or login host) "@") host))
(sbtoken (list finger name info)))
(if (or login host name info) ; we want to be expandable
(speedbar-make-tag-line
'bracket ?+ 'erc-speedbar-expand-user sbtoken
nick-str nil sbtoken nil
indent)
(when (equal exp-char ?-)
(forward-line -1)
(erc-speedbar-expand-user "+" (list finger name info) indent))
(speedbar-make-tag-line
'statictag ?? nil nil
nick-str nil nil nil
indent))))
(defun erc-speedbar-update-channel (buffer)
"Update the speedbar information about a ERC buffer. The update
is only done when the channel is actually expanded already."
;; This is only a rude hack and doesn't care about multiserver usage
;; yet, consider this a brain storming, better ideas?
(with-current-buffer speedbar-buffer
(save-excursion
(goto-char (point-min))
(when (re-search-forward (concat "^1: *.+. *"
(regexp-quote (buffer-name buffer)))
nil t)
(beginning-of-line)
(speedbar-delete-subblock 1)
(erc-speedbar-expand-channel "+" buffer 1)))))
(defun erc-speedbar-expand-user (text token indent)
(cond ((string-match "+" text)
(speedbar-change-expand-button-char ?-)
(speedbar-with-writable
(save-excursion
(end-of-line) (forward-char 1)
(let ((finger (nth 0 token))
(name (nth 1 token))
(info (nth 2 token)))
(when finger
(speedbar-make-tag-line
nil nil nil nil
finger nil nil nil
(1+ indent)))
(when name
(speedbar-make-tag-line
nil nil nil nil
name nil nil nil
(1+ indent)))
(when info
(speedbar-make-tag-line
nil nil nil nil
info nil nil nil
(1+ indent)))))))
((string-match "-" text)
(speedbar-change-expand-button-char ?+)
(speedbar-delete-subblock indent))
(t (error "Ooops... not sure what to do")))
(speedbar-center-buffer-smartly))
(defun erc-speedbar-goto-buffer (text buffer indent)
"When user clicks on TEXT, goto an ERC buffer.
The INDENT level is ignored."
(if (featurep 'dframe)
(progn
(dframe-select-attached-frame speedbar-frame)
(let ((bwin (get-buffer-window buffer 0)))
(if bwin
(progn
(select-window bwin)
(raise-frame (window-frame bwin)))
(if dframe-power-click
(let ((pop-up-frames t))
(select-window (display-buffer buffer)))
(dframe-select-attached-frame speedbar-frame)
(switch-to-buffer buffer)))))
(let ((bwin (get-buffer-window buffer 0)))
(if bwin
(progn
(select-window bwin)
(raise-frame (window-frame bwin)))
(if speedbar-power-click
(let ((pop-up-frames t)) (select-window (display-buffer buffer)))
(dframe-select-attached-frame speedbar-frame)
(switch-to-buffer buffer))))))
(defun erc-speedbar-line-text ()
"Return the text for the item on the current line."
(beginning-of-line)
(when (re-search-forward "[]>] " nil t)
(buffer-substring-no-properties (point) (point-at-eol))))
(defun erc-speedbar-item-info ()
"Display information about the current buffer on the current line."
(let ((data (speedbar-line-token))
(txt (erc-speedbar-line-text)))
(cond ((and data (listp data))
(message "%s: %s" txt (car data)))
((bufferp data)
(message "Channel: %s" txt))
(t
(message "%s" txt)))))
(provide 'erc-speedbar)
;;; erc-speedbar.el ends here
;;
;; Local Variables:
;; indent-tabs-mode: t
;; tab-width: 8
;; End:
|