Fix up comment convention on the arch-tag lines.
[bpt/emacs.git] / lisp / talk.el
CommitLineData
60370d40 1;;; talk.el --- allow several users to talk to each other through Emacs
5abe8993 2
c90f2757 3;; Copyright (C) 1995, 2001, 2002, 2003, 2004, 2005,
409cc4a3 4;; 2006, 2007, 2008 Free Software Foundation, Inc.
b578f267 5
30764597 6;; Maintainer: FSF
5abe8993
RS
7;; Keywords: comm, frames
8
9;; This file is part of GNU Emacs.
10
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
b4aa6026 13;; the Free Software Foundation; either version 3, or (at your option)
5abe8993
RS
14;; any later version.
15
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.
20
21;; You should have received a copy of the GNU General Public License
b578f267 22;; along with GNU Emacs; see the file COPYING. If not, write to the
086add15
LK
23;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24;; Boston, MA 02110-1301, USA.
5abe8993
RS
25
26;;; Commentary:
27
b578f267
EN
28;; This is a multi-user talk package that runs in Emacs.
29;; Use talk-connect to bring a new person into the conversation.
5abe8993
RS
30
31;;; Code:
32
33(defvar talk-display-alist nil
34 "Alist of displays on which Emacs talk is now running.
35Each element has the form (DISPLAY FRAME BUFFER).")
36
37;;;###autoload
38(defun talk-connect (display)
39 "Connect to display DISPLAY for the Emacs talk group."
40 (interactive "sTalk to display: ")
41 ;; Make sure we have an entry for the current display.
42 (let ((mydisp (cdr (assq 'display (frame-parameters (selected-frame))))))
43 (talk-add-display mydisp))
44 ;; Make sure we have an entry for the specified display.
45 (talk-add-display display)
46 ;; Add the new buffers to all talk frames.
47 (talk-update-buffers))
48
17086732
KL
49;;;###autoload
50(defun talk ()
51 "Connect to the Emacs talk group from the current X display or tty frame."
52 (interactive)
b6660415 53 (let ((type (frame-live-p (selected-frame)))
6ed8eeff 54 (display (frame-terminal (selected-frame))))
b6660415
KL
55 (cond
56 ((eq type t)
57 (talk-add-display (selected-frame)))
58 ((eq type 'x)
6ed8eeff 59 (talk-add-display (frame-terminal (selected-frame))))
b6660415
KL
60 (t
61 (error "Unknown frame type"))))
17086732
KL
62 (talk-update-buffers))
63
b6660415
KL
64(defun talk-add-display (frame)
65 (let* ((display (if (frame-live-p frame)
6ed8eeff 66 (frame-terminal frame)
b6660415
KL
67 frame))
68 (elt (assoc display talk-display-alist))
6ed8eeff 69 (name (concat "*talk-" (terminal-name display) "*"))
17086732 70 buffer)
b6660415
KL
71 (unless (frame-live-p frame)
72 (setq frame (make-frame-on-display display (list (cons 'name name)))))
73 (if (and elt (frame-live-p (nth 1 elt)))
74 (setq frame (nth 1 elt)))
17086732
KL
75 (if (not (and elt (buffer-name (get-buffer (setq buffer (nth 2 elt))))))
76 (setq buffer (get-buffer-create name)))
b6660415 77 (add-to-list 'delete-frame-functions 'talk-handle-delete-frame)
17086732 78 (setq talk-display-alist
b6660415 79 (cons (list display frame buffer) (delq elt talk-display-alist)))))
17086732 80
b6660415
KL
81(defun talk-handle-delete-frame (frame)
82 (dolist (d talk-display-alist)
83 (when (eq (nth 1 d) frame)
84 (setq talk-display-alist (delq d talk-display-alist))
85 (talk-update-buffers))))
17086732 86
5abe8993
RS
87(defun talk-disconnect ()
88 "Disconnect this display from the Emacs talk group."
c2bef985 89 (interactive)
5abe8993
RS
90 (let* ((mydisp (cdr (assq 'display (frame-parameters (selected-frame)))))
91 (elt (assoc mydisp talk-display-alist)))
92 (delete-frame (nth 1 elt))
93 (kill-buffer (nth 2 elt))
94 (setq talk-display-alist (delq elt talk-display-alist))
95 (talk-update-buffers)))
96
97(defun talk-update-buffers ()
98 "Update all the talk frames so that each shows all the talk buffers."
99 (let ((tail talk-display-alist))
100 (while tail
101 (let ((frame (nth 1 (car tail)))
102 (this-buffer (nth 2 (car tail)))
103 (buffers
104 (mapcar (function (lambda (elt) (nth 2 elt)))
105 talk-display-alist)))
106 ;; Put this display's own talk buffer
107 ;; at the front of the list.
108 (setq buffers (cons this-buffer (delq this-buffer buffers)))
109 (talk-split-up-frame frame buffers))
110 (setq tail (cdr tail)))))
111
112(defun talk-split-up-frame (frame buffers)
113 "Split FRAME into equal-sized windows displaying the buffers in BUFFERS.
114Select the first of these windows, displaying the first of the buffers."
115 (let ((lines-per-buffer (/ (frame-height frame) (length buffers)))
116 (old-frame (selected-frame)))
117 (unwind-protect
118 (progn
119 (select-frame frame)
120 (select-window (frame-first-window frame))
121 (delete-other-windows)
122 (while (progn
123 (switch-to-buffer (car buffers))
124 (setq buffers (cdr buffers)))
125 (split-window-vertically lines-per-buffer)
126 (other-window 1))
127 (select-window (frame-first-window frame)))
128 (select-frame old-frame))))
129
896546cd
RS
130(provide 'talk)
131
cbee283d 132;; arch-tag: 7ab0ad88-1788-4886-a44c-ae685e6f8a1a
5abe8993 133;;; talk.el ends here