summaryrefslogtreecommitdiff
path: root/emacs.d/lisp/erc-tab.el
blob: 50f5db72cd9ebc90b316a8452f649eccd6029896 (plain)
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
;;; erc-tab.el --- Provide a tab-style interface to erc buffers.

;; Copyright (C) 2003  David Edmondson <dme@dme.org>

;; Author: David Edmondson <dme@dme.org>
;; Keywords: tabs, faces
;; URL: http://www.emacswiki.org/cgi-bin/wiki.pl?ErcTabs

;; This file 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 2, or (at your option)
;; any later version.

;; This file 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; see the file COPYING.  If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.

;;; Commentary:

;; Provides tabs in the header-line to access different erc buffers.
;; This only works if your emacs supports a header line (emacs 21
;; does).  Add to your ~/.emacs:

;; (require 'erc-tab)
;; (erc-tab-mode 1)

;; The ideas and implementation here are taken from emacs-w3m.

;; Todo:
;;
;; * change the colour of the text in a tab to reflect activity and
;;   the type of activity (a combination of erc-track and xchat
;;   behaviour).  DONE: not as complex as erc-track, but useful for
;;   discussion.

(require 'erc)
; anything else?

;;; Code:

(defconst erc-tab-version "$Revision$"
  "ERC tab mode revision")

(defgroup erc-tab nil
  "Provide a tab interface to ERC buffers.")

(defcustom erc-tab-topic-format
  '(" " nick " " channel-modes " " topic)
  "Format of the topic in the header-line in erc-mode.
Only used in Emacs 21. This variable is processed using
`erc-prepare-mode-line-format'."
  :group 'erc-tab
  :type 'sexp)

(defcustom erc-tab-max-width 16
  "Maximum width of any one channel tab."
  :group 'erc-tab
  :type 'number)

; this removes server buffers for me
(defcustom erc-tab-interesting-buffers-regexp "^[^:]*$"
  "Regular expression used to look for interesting buffers.  Buffers whose
names match this expression will have a tab created in erc buffers."
  :group 'erc-tab
  :type 'regexp)

(defface erc-tab-unselected-face
  '((((type x w32 mac) (class color))
     :background "Gray50" :foreground "Gray20"
     :underline "Gray85" :box (:line-width -1 :style released-button))
    (((class color))
     (:background "cyan" :foreground "black" :underline "blue")))
  "*Face to fontify unselected tabs."
  :group 'erc-tab)

; TODO: use `:inherit' from `erc-tab-unselected-face' and then
; change the foreground.
(defface erc-tab-unselected-active-face
  '((((type x w32 mac) (class color))
     :background "Gray50" :foreground "blue"
     :underline "Gray85" :box (:line-width -1 :style released-button))
    (((class color))
     (:background "cyan" :foreground "yellow" :underline "blue")))
  "*Face to fontify unselected tabs."
  :group 'erc-tab)

(defface erc-tab-selected-face
  '((((type x w32 mac) (class color))
     :background "Gray85" :foreground "black"
     :underline "Gray85" :box (:line-width -1 :style released-button))
    (((class color))
     (:background "blue" :foreground "black" :underline "blue"))
    (t (:underline t)))
  "*Face to fontify selected tab."
  :group 'erc-tab)

(defface erc-tab-topic-face
  '((((type x w32 mac) (class color))
     :foreground "Grey85"
     :underline "Gray85" :box (:line-width -1 :style released-button))
    (((class color))
     (:background "cyan" :foreground "black" :underline "blue"))
    (t (:underline t)))
  "*Face to fontify the tab gutter, which contains the topic."
  :group 'erc-tab)

;;;

(defun erc-tab-buffer-list ()
  "Return all erc buffers."
  (mapcar
   'buffer-name
   (erc-buffer-list)))

(defun erc-tab-all-visible-buffer-names ()
  "Return all visible channel or query buffer names."
  (mapcar
   'buffer-name
   (erc-buffer-filter
    (lambda () (get-buffer-window (current-buffer) 'visible)))))

(defun erc-tab-all-interesting-buffer-names ()
  "Return all interesting erc buffer names."
  (mapcar
   'buffer-name
   (erc-buffer-filter
    (lambda ()
      (string-match erc-tab-interesting-buffers-regexp
		    (buffer-name (current-buffer)))))))

(defun erc-tab-make-keymap (buffer)
  (let ((map (make-sparse-keymap))
	(fn `(lambda (e) (interactive "e")
	       (select-window (car (event-start e)))
	       (switch-to-buffer ,buffer))))
    (define-key map [header-line down-mouse-1] 'ignore)
    (define-key map [header-line drag-mouse-1] fn)
    (define-key map [header-line mouse-1] fn)
    map))

(defun erc-tab-update ()
  "Update all tabs, as necessary."
  ; buffers which are visible don't belong in erc-tab-modified-buffers
  (mapcar (lambda (b)
	    (setq erc-tab-modified-buffers
		  (remove b erc-tab-modified-buffers)))
	  (erc-tab-all-visible-buffer-names))
  
  (mapcar 'erc-tab-update-buffer (erc-tab-buffer-list)))

(defun erc-tab-update-buffer (buffer)
  "Update the tabs in erc buffer `buffer'."
  (save-excursion
    (set-buffer buffer)
    (setq header-line-format
	  (append
	   (mapcar
	    (lambda (b)
	      (propertize
	       (concat " " (truncate-string-to-width
			    b (- erc-tab-max-width 2)) " ")
	       'face (cond
		      ((eq b buffer)
		       'erc-tab-selected-face)
		      ((member b erc-tab-modified-buffers)
		       'erc-tab-unselected-active-face)
		      (t
		       'erc-tab-unselected-face))
	       'local-map (erc-tab-make-keymap b)))
	    (erc-sort-strings (erc-tab-all-interesting-buffer-names)))
	   (list
	    (let* ((topic-text
		    (mapconcat 'identity
			       (erc-prepare-mode-line-format
				erc-tab-topic-format) ""))
		   (help-text
		    (with-temp-buffer
		      (insert topic-text)
		      (fill-region (point-min) (point-max))
		      (buffer-string))))
	      (propertize topic-text
			  'help-echo help-text
			  'face 'erc-tab-topic-face)))))))

(defvar erc-tab-modified-buffers nil)

(defun erc-tab-buffer-updated ()
  "Hook for `erc-insert-post-hook'."
  (let ((this-channel (erc-default-target)))
    (if (and (not (get-buffer-window (current-buffer) 'visible))
	     (not (memq this-channel erc-tab-modified-buffers)))
	(setq erc-tab-modified-buffers
	      (cons this-channel erc-tab-modified-buffers)))))

(defun erc-tab-JOIN (proc parsed)
  (erc-tab-update)
  nil)

; TODO: only really needs to update the buffer that changed.
(defun erc-tab-TOPIC (proc parsed)
  (erc-tab-update)
  nil)

(defun erc-tab-remove ()
  "Unset the header line for all erc buffers."
  (save-excursion
    (mapcar
     (lambda (b) (set-buffer b) (setq header-line-format nil))
     (erc-tab-buffer-list))))

;;; Module;

(setq erc-tab-server-hooks '("JOIN" "TOPIC"))

;;;###autoload (autoload 'erc-tab-mode "erc-tab" nil t)
(define-erc-module tab nil
  "This mode provides a tab interface to ERC buffers."

  ; enable:
  ((if (featurep 'xemacs)
       (defadvice switch-to-buffer (after erc-update (&rest args) activate)
         (erc-tab-update))
     (add-hook 'window-configuration-change-hook
               'erc-tab-update))
   (mapcar
    (lambda (message)
      (add-hook (intern (concat "erc-server-" message "-hook"))
		(intern (concat "erc-tab-" message))
		t))
    erc-tab-server-hooks)

   (add-hook 'erc-kill-server-hook 'erc-tab-update t)
   (add-hook 'erc-kill-channel-hook 'erc-tab-update t)
   (add-hook 'erc-kill-buffer-hook 'erc-tab-update t)

   (add-hook 'erc-insert-post-hook 'erc-tab-buffer-updated)

   ; stop the normal header-line updates
   (remove-hook 'erc-update-mode-line-hooks 'erc-update-header-line)

   (erc-tab-update))

  ; disable:
  ; re-enable normal header-line updates
  ((add-hook 'erc-update-mode-line-hooks 'erc-update-header-line)

   (remove-hook 'erc-insert-post-hook 'erc-tab-buffer-updated)

   (remove-hook 'erc-kill-buffer-hook 'erc-tab-update)
   (remove-hook 'erc-kill-channel-hook 'erc-tab-update)
   (remove-hook 'erc-kill-server-hook 'erc-tab-update)

   (mapcar
    (lambda (message)
      (remove-hook (intern (concat "erc-server-" message "-hook"))
		   (intern (concat "erc-tab-" message))))
    (reverse erc-tab-server-hooks))

   (if (featurep 'xemacs)
       (ad-disable-advice 'switch-to-buffer 'after 'erc-update)
     (remove-hook 'window-configuration-change-hook
                  'erc-tab-update))
   (erc-tab-remove)))