-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmu.el
288 lines (236 loc) · 8.69 KB
/
mu.el
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
;;; mu.el --- play on a MUSH or MUD within Emacs
;; Copyright (C) 2001, 2004 Alex Schroeder <alex@gnu.org>
;; Emacs Lisp Archive Entry
;; Filename: mu.el
;; Version: 1.0.1
;; Keywords: comm, games
;; Author: Alex Schroeder <alex@gnu.org>
;; Maintainer: Alex Schroeder <alex@gnu.org>
;; Description: Play in a MUSH or MUD within Emacs.
;; Compatibility: Emacs20
;; This file is not part of GNU Emacs.
;; This 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 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:
;; This is is a MUSH or MUD client. Instead of using telnet or
;; standalone client to connect to your favorite MUSH or MUD, you can
;; use mu.el to play within Emacs.
;; I used to play using tinymud.el, but decided to rewrite it based on
;; comint-mode. :)
;; Before playing, customize `mu-worlds'. Then use `mu-open' to open a
;; connection to one of the worlds. This will automaticall create a mu
;; connection buffer and a mu buffer. You can type commands in either
;; buffer and send them to the host with RET. The output will be in the
;; mu connection buffer.
;; If you load ansi-color.el, you should be able to get ANSI colors.
;;; Code:
(require 'comint)
(load "ansi-color" t)
(defgroup mu nil
"A MUSH or MUD client."
:group 'processes)
(defcustom mu-worlds nil
"List of worlds you play in.
You need to define the worlds you play in before you can get
started. In most worlds, you can start playing using a guest account.
Each element WORLD of the list has the following form:
\[NAME HOST PORT CHARACTER PASSWORD]
NAME identifies the connection, HOST and PORT specify the network
connection, CHARACTER and PASSWORD are used to connect automatically.
Note that this will be saved in your `custom-file' -- including your
passwords! If you don't want that, specify nil as your password."
:type '(repeat
(vector :tag "World"
(string :tag "Name")
(string :tag "Host")
(integer :tag "Port")
(string :tag "Char" :value "guest")
(string :tag "Pwd" :value "guest")))
:group 'mu)
;; Accessing the fields
(defsubst mu-world-name (world)
"Return the name for WORLD as a string."
(concat (aref world 3) "@" (aref world 0)))
(defsubst mu-world-network (world)
"Return the network details for WORLD as a cons cell (HOST . PORT)."
(cons (aref world 1) (aref world 2)))
(defsubst mu-world-character (world)
"Return the character for WORLD as a string."
(aref world 3))
(defsubst mu-world-password (world)
"Return the password for WORLD as a string."
(aref world 4))
;;; Modes
(defvar mu-input-mode-map
(let ((map (make-sparse-keymap)))
(if (functionp 'set-keymap-parent)
(set-keymap-parent map text-mode-map); Emacs
(set-keymap-parents map (list text-mode-map))); XEmacs
(if (functionp 'set-keymap-name)
(set-keymap-name map 'mu-input-mode-map)); XEmacs
(define-key map (kbd "<RET>") 'mu-send)
map)
"Mode map used for `mu-input-mode'.
Based on `text-mode-map'.")
(defvar mu-connection nil
"Local variable for the connection.")
(defun mu-input-mode (&optional conn)
"Major mode to type commands for the mu connection.
This is called a mu input buffer.
Use \\[mu-open] to open a connection.
Use \\[mu-choose-connection] to choose a connection.
Use \\[mu-send] to send commands to the current connection.
This function will run `mu-input-mode-hook' at the end.
\\{mu-input-mode-map}"
(interactive)
(setq conn (or conn mu-connection (mu-get-connection)))
(kill-all-local-variables)
(setq major-mode 'mu-input-mode)
(setq mode-name "MU* Input")
(use-local-map mu-input-mode-map)
;; Make each buffer in mu-input-mode remember the current connection.
(set (make-local-variable 'mu-connection) conn)
;; Run hook
(run-hooks 'mu-input-mode-hook))
(defvar mu-connection-mode-map
(let ((map (make-sparse-keymap)))
(if (functionp 'set-keymap-parent)
(set-keymap-parent map comint-mode-map); Emacs
(set-keymap-parents map (list comint-mode-map))); XEmacs
(if (functionp 'set-keymap-name)
(set-keymap-name map 'mu-connection-mode-map)); XEmacs
;; (define-key map (kbd "C-j") 'mu-accumulate-and-indent)
map)
"Mode map used for `mu-connection-mode'.
Based on `comint-mode-map'.")
(defvar mu-name nil
"Local variable for the connection name.")
(defun mu-connection-mode (name)
"Major mode for a mu connection.
Use \\[comint-send-input] to send commands.
Use \\[mu-open] to open other connections.
Use \\[mu-input-buffer] to create a mu input buffer.
This function will run `mu-connection-mode-hook' at the end.
\\{mu-connection-mode-map}"
(comint-mode)
(setq major-mode 'mu-connection-mode)
(setq mode-name "MU* Conn")
(use-local-map mu-connection-mode-map)
(set (make-local-variable 'mu-name) name)
(setq fill-column 80)
(add-to-list 'comint-output-filter-functions 'mu-fill)
;; User stuff.
(run-hooks 'mu-connection-mode-hook))
(put 'mu-connection-mode 'mode-class 'special)
;;; Opening connections
(defvar mu-world-history nil
"History for `mu-get-world'.")
(defun mu-get-world ()
"Let the user choose a world from `mu-worlds'.
The return value is a cons cell, the car is the name of the connection,
the cdr holds the connection defails from `mu-worlds'."
(let ((world-completions
(mapcar (lambda (w)
(cons (mu-world-name w) w))
mu-worlds)))
(if world-completions
(cdr (assoc (completing-read "World: " world-completions
nil t nil mu-world-history)
world-completions))
(customize-option 'mu-worlds)
nil)))
(defun mu-open (world)
"Create a new mu connection."
(interactive (list (mu-get-world)))
(when world
(message "Opening connection...")
(let ((buf (make-comint (mu-world-name world) (mu-world-network world))))
(pop-to-buffer buf)
(when (mu-world-password world)
(mu-login world))
(mu-connection-mode (mu-world-name world))
(mu-input-buffer buf)
(message "Opening connection...done"))))
(defun mu-reconnect (world)
"Renew the connection in a mu output buffer."
(interactive (list (mu-get-world)))
(open-network-stream (mu-world-name world) (current-buffer)
(car (mu-world-network world))
(cdr (mu-world-network world))))
(defun mu-login (world)
"Login for WORLD in the current buffer.
This just sends the login string and hopes for the best."
(process-send-string
(current-buffer)
(format "\nconnect %s %s\n"
(mu-world-character world)
(mu-world-password world))))
;; Creating mu mode buffers
(defun mu-input-buffer (buf)
"Create a mu input buffer for connection BUF.
The current buffer must be a mu connection."
(interactive (list (mu-get-connection)))
(set-buffer buf)
(pop-to-buffer (get-buffer-create
(concat "*Input for " mu-name "*")))
(mu-input-mode buf))
(defvar mu-connection-history nil
"History for `mu-get-connection'.")
(defun mu-get-connection ()
"Let the user choose a connection from all buffers.
Only buffers with `mu-name' set are eligible.
Note that `default-value' of `mu-name' must be nil for this to work."
(let ((buffers (buffer-list))
buf conns)
(while buffers
(setq buf (car buffers)
buffers (cdr buffers))
(set-buffer buf)
(when mu-name
(setq conns (cons (cons mu-name buf) conns))))
(cdr (assoc (completing-read "Connection: " conns
nil t nil mu-connection-history)
conns))))
;; Sending stuff
(defun mu-send ()
"Send current line to the current connection.
The current connection is stored in `mu-connection'."
(interactive)
(unless mu-connection
(error "No connection"))
(let ((pos (point)))
(save-excursion
(beginning-of-line)
(process-send-string
mu-connection
(concat (buffer-substring-no-properties (point) pos) "\n"))))
(when (looking-at "\\'")
(newline)))
;; Receiving stuff
(defun mu-fill (str)
"Fill text received from the host.
This fills each line between `comint-last-output-start' and the buffer's
`process-mark'."
(save-excursion
(let ((pos (point-marker)))
(goto-char comint-last-output-start)
(while (< (point) pos)
(let (start)
(beginning-of-line)
(setq start (point))
(forward-line)
(fill-region start (point) nil t))))))
(provide 'mu)
;;; mu.el ends here