3d7c45281aaaa05a4faa6a31de5095b0215bbaa9
[bpt/emacs.git] / lisp / talk.el
1 ;;; talk.el --- allow several users to talk to each other through Emacs
2
3 ;; Copyright (C) 1995 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6 ;; Keywords: comm, frames
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; This is a multi-user talk package that runs in Emacs.
28 ;; Use talk-connect to bring a new person into the conversation.
29
30 ;;; Code:
31
32 (defvar talk-display-alist nil
33 "Alist of displays on which Emacs talk is now running.
34 Each element has the form (DISPLAY FRAME BUFFER).")
35
36 ;;;###autoload
37 (defun talk-connect (display)
38 "Connect to display DISPLAY for the Emacs talk group."
39 (interactive "sTalk to display: ")
40 ;; Make sure we have an entry for the current display.
41 (let ((mydisp (cdr (assq 'display (frame-parameters (selected-frame))))))
42 (talk-add-display mydisp))
43 ;; Make sure we have an entry for the specified display.
44 (talk-add-display display)
45 ;; Add the new buffers to all talk frames.
46 (talk-update-buffers))
47
48 ;;;###autoload
49 (defun talk ()
50 "Connect to the Emacs talk group from the current X display or tty frame."
51 (interactive)
52 (let ((type (frame-live-p (selected-frame))))
53 (case type
54 ;; Termcap frame
55 ((t) (talk-add-tty-frame (selected-frame)))
56 ;; X frame
57 ((x) (talk-add-display (frame-parameter (selected-frame) 'display)))
58 (t (error "Could not determine frame type"))))
59 (talk-update-buffers))
60
61 (defun talk-add-display (display)
62 (let* ((elt (assoc display talk-display-alist))
63 (name (concat "*talk-" display "*"))
64 buffer frame)
65 (if (not (and elt (frame-live-p (setq frame (nth 1 elt)))))
66 (setq frame (make-frame-on-display display (list (cons 'name name)))))
67 (if (not (and elt (buffer-name (get-buffer (setq buffer (nth 2 elt))))))
68 (setq buffer (get-buffer-create name)))
69 (setq talk-display-alist
70 (cons (list display frame buffer) (delq elt talk-display-alist)))))
71
72 (defun talk-add-tty-frame (frame)
73 (let* ((elt (assoc (frame-tty-name frame) talk-display-alist))
74 (name (concat "*talk-" (frame-tty-name frame) "*"))
75 buffer)
76 (if (not (and elt (buffer-name (get-buffer (setq buffer (nth 2 elt))))))
77 (setq buffer (get-buffer-create name)))
78 (add-to-list 'delete-tty-after-functions 'talk-handle-delete-tty)
79 (setq talk-display-alist
80 (cons (list (frame-tty-name frame) frame buffer) (delq elt talk-display-alist)))))
81
82 (defun talk-handle-delete-tty (tty)
83 (let (elt (assoc tty talk-display-alist))
84 (setq talk-display-alist (delq elt talk-display-alist))
85 (talk-update-buffers)))
86
87 (defun talk-disconnect ()
88 "Disconnect this display from the Emacs talk group."
89 (interactive)
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.
114 Select 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
130 (provide 'talk)
131
132 ;;; arch-tag: 7ab0ad88-1788-4886-a44c-ae685e6f8a1a
133 ;;; talk.el ends here