talk.el 4.21 KB
Newer Older
1
;;; talk.el --- allow several users to talk to each other through Emacs
Richard M. Stallman's avatar
Richard M. Stallman committed
2

Paul Eggert's avatar
Paul Eggert committed
3
;; Copyright (C) 1995, 2001-2019 Free Software Foundation, Inc.
Erik Naggum's avatar
Erik Naggum committed
4

5
;; Maintainer: emacs-devel@gnu.org
Richard M. Stallman's avatar
Richard M. Stallman committed
6 7 8 9
;; Keywords: comm, frames

;; This file is part of GNU Emacs.

10
;; GNU Emacs is free software: you can redistribute it and/or modify
Richard M. Stallman's avatar
Richard M. Stallman committed
11
;; it under the terms of the GNU General Public License as published by
12 13
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
Richard M. Stallman's avatar
Richard M. Stallman committed
14 15 16 17 18 19 20

;; 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
21
;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
Richard M. Stallman's avatar
Richard M. Stallman committed
22 23 24

;;; Commentary:

Erik Naggum's avatar
Erik Naggum committed
25 26
;; This is a multi-user talk package that runs in Emacs.
;; Use talk-connect to bring a new person into the conversation.
Richard M. Stallman's avatar
Richard M. Stallman committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

;;; Code:

(defvar talk-display-alist nil
  "Alist of displays on which Emacs talk is now running.
Each element has the form (DISPLAY FRAME BUFFER).")

;;;###autoload
(defun talk-connect (display)
  "Connect to display DISPLAY for the Emacs talk group."
  (interactive "sTalk to display: ")
  ;; Make sure we have an entry for the current display.
  (let ((mydisp (cdr (assq 'display (frame-parameters (selected-frame))))))
    (talk-add-display mydisp))
  ;; Make sure we have an entry for the specified display.
  (talk-add-display display)
  ;; Add the new buffers to all talk frames.
  (talk-update-buffers))

46 47 48 49
;;;###autoload
(defun talk ()
  "Connect to the Emacs talk group from the current X display or tty frame."
  (interactive)
50
  (let ((type (frame-live-p (selected-frame))))
51
    (if (or (eq type t) (eq type 'x))
52
	(talk-add-display
53
	 (terminal-name (frame-terminal)))
54
      (error "Unknown frame type")))
55 56
  (talk-update-buffers))

57 58 59 60
(defun talk-add-display (display)
  (let* ((elt (assoc display talk-display-alist))
	 (name (concat "*talk-" display "*"))
	 frame buffer)
61
    (if (and elt (frame-live-p (nth 1 elt)))
62 63
	(setq frame (nth 1 elt))
      (setq frame (make-frame-on-display display (list (cons 'name name)))))
64 65
    (if (not (and elt (buffer-name (get-buffer (setq buffer (nth 2 elt))))))
	(setq buffer (get-buffer-create name)))
66
    (add-to-list 'delete-frame-functions 'talk-handle-delete-frame)
67
    (setq talk-display-alist
68
	  (cons (list display frame buffer) (delq elt talk-display-alist)))))
69

70 71 72 73 74
(defun talk-handle-delete-frame (frame)
  (dolist (d talk-display-alist)
    (when (eq (nth 1 d) frame)
      (setq talk-display-alist (delq d talk-display-alist))
      (talk-update-buffers))))
75

Richard M. Stallman's avatar
Richard M. Stallman committed
76 77
(defun talk-disconnect ()
  "Disconnect this display from the Emacs talk group."
78
  (interactive)
Richard M. Stallman's avatar
Richard M. Stallman committed
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
  (let* ((mydisp (cdr (assq 'display (frame-parameters (selected-frame)))))
	 (elt (assoc mydisp talk-display-alist)))
    (delete-frame (nth 1 elt))
    (kill-buffer (nth 2 elt))
    (setq talk-display-alist (delq elt talk-display-alist))
    (talk-update-buffers)))

(defun talk-update-buffers ()
  "Update all the talk frames so that each shows all the talk buffers."
  (let ((tail talk-display-alist))
    (while tail
      (let ((frame (nth 1 (car tail)))
	    (this-buffer (nth 2 (car tail)))
	    (buffers
	     (mapcar (function (lambda (elt) (nth 2 elt)))
		     talk-display-alist)))
	;; Put this display's own talk buffer
	;; at the front of the list.
	(setq buffers (cons this-buffer (delq this-buffer buffers)))
	(talk-split-up-frame frame buffers))
      (setq tail (cdr tail)))))

(defun talk-split-up-frame (frame buffers)
  "Split FRAME into equal-sized windows displaying the buffers in BUFFERS.
Select the first of these windows, displaying the first of the buffers."
  (let ((lines-per-buffer (/ (frame-height frame) (length buffers)))
	(old-frame (selected-frame)))
    (unwind-protect
	(progn
	  (select-frame frame)
	  (select-window (frame-first-window frame))
	  (delete-other-windows)
	  (while (progn
		   (switch-to-buffer (car buffers))
		   (setq buffers (cdr buffers)))
114
	    (split-window-below lines-per-buffer)
Richard M. Stallman's avatar
Richard M. Stallman committed
115 116 117 118
	    (other-window 1))
	  (select-window (frame-first-window frame)))
      (select-frame old-frame))))

Richard M. Stallman's avatar
Richard M. Stallman committed
119 120
(provide 'talk)

Richard M. Stallman's avatar
Richard M. Stallman committed
121
;;; talk.el ends here