1 ;;; ez.el --- Emacs Z-machine
3 ;; Copyright (C) 2021 Tim Vaughan
5 ;; Author: Tim Vaughan <timv@ughan.xyz>
6 ;; Created: 13 Oct 2021
9 ;; Homepage: http://thelambdalab.xyz/ez
10 ;; Package-Requires: ((emacs "26"))
12 ;; This file is not part of GNU Emacs.
14 ;; This program is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; This program is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with this file. If not, see <http://www.gnu.org/licenses/>.
33 (defun ez-decode-signed-bits (n nbits)
34 (if (= 0 (lsh n (- 1 nbits)))
38 (defun ez-decode-signed-byte (b)
39 (ez-decode-signed-bits b 8))
41 (defun ez-decode-signed-word (w)
42 (ez-decode-signed-bits w 16))
44 (defun ez-decode-signed-operand (operand operand-type)
45 (if (eq operand-type 'b)
46 (ez-decode-signed-byte operand)
47 (ez-decode-signed-word operand)))
49 (defun binformat (n &optional s)
53 (new-s (concat (number-to-string (mod n 2)) s)))
56 (binformat d new-s))))
61 "Memory of z-machine.")
63 (defvar ez-version nil)
65 (defvar ez-start-pc nil)
66 (defvar ez-himem-base nil)
67 (defvar ez-dict-addr nil)
68 (defvar ez-objtab-addr nil)
69 (defvar ez-abbrevtab-addr nil)
70 (defvar ez-globalvartab-addr nil)
72 (defun ez-mem-ref-byte (addr)
73 (aref ez-memory addr))
75 (defun ez-mem-set-byte (addr val)
76 (aset ez-memory addr val))
78 (defun ez-mem-ref-word (addr)
79 (+ (* 256 (aref ez-memory addr))
80 (aref ez-memory (+ addr 1))))
82 (defun ez-mem-set-word (addr val)
83 (let ((byte-high (/ val 256))
84 (byte-low (mod val 256)))
85 (aset ez-memory addr byte-high)
86 (aset ez-memory (+ addr 1) byte-low)))
88 (defun ez-parse-header ()
89 (setq ez-version (ez-mem-ref-byte #x0))
90 (setq ez-himem-addr (ez-mem-ref-word #x4))
91 (setq ez-start-pc (ez-mem-ref-word #x6))
92 (setq ez-dict-base (ez-mem-ref-word #x8))
93 (setq ez-objtab-addr (ez-mem-ref-word #xA))
94 (setq ez-globalvartab-addr (ez-mem-ref-word #xC))
95 (setq ez-abbrevtab-addr (ez-mem-ref-word #x18)))
97 (defun ez-load-file (filename)
98 "Load story file into memory."
100 (insert-file-contents-literally filename)
101 (setq ez-memory (encode-coding-string (buffer-string) 'raw-text)))
106 (defun ez-get-global-var (gvar)
108 (error "Invalid global variable %d" gvar))
109 (ez-mem-ref-word (+ (* 2 gvar) ez-globalvartab-addr)))
111 (defun ez-set-global-var (gvar val)
112 (ez-mem-set-word (+ (* 2 gvar) ez-globalvartab-addr) val))
116 (defvar ez-property-defaults nil)
118 (defun ez-load-property-defaults ()
119 (setq ez-property-defaults (make-vector 31 0))
121 (aset ez-property-defaults i (aref ez-memory (+ ez-objtab-addr (* 2 i))))))
123 (defun ez-get-obj-addr (obj-id)
124 (+ ez-objtab-addr (* 2 31) (* 9 (- obj-id 1))))
126 (defun ez-get-obj-parent (obj-id)
127 (let ((addr (ez-get-obj-addr obj-id)))
128 (ez-mem-ref-byte (+ addr 4))))
130 (defun ez-get-obj-sibling (obj-id)
131 (let ((addr (ez-get-obj-addr obj-id)))
132 (ez-mem-ref-byte (+ addr 5))))
134 (defun ez-get-obj-child (obj-id)
135 (let ((addr (ez-get-obj-addr obj-id)))
136 (ez-mem-ref-byte (+ addr 6))))
138 (defun ez-get-obj-plist-addr (obj-id)
139 (let ((addr (ez-get-obj-addr obj-id)))
140 (ez-mem-ref-word (+ addr 7))))
142 (defun ez-get-obj-name (obj-id)
143 (let ((plist-addr (ez-get-obj-plist-addr obj-id)))
144 (if (> (ez-mem-ref-byte plist-addr) 0)
145 (ez-get-zstring (+ 1 (ez-get-obj-plist-addr obj-id)))
148 (defun ez-get-obj (obj-id)
149 (let ((addr (+ ez-objtab-addr
151 (* 9 (- obj-id 1)))))
154 (ez-mem-ref-byte (+ addr 4))
155 (ez-mem-ref-byte (+ addr 5))
156 (ez-mem-ref-byte (+ addr 6))
157 (ez-mem-ref-word (+ addr 7))
158 (let ((plist-addr (ez-mem-ref-word (+ addr 7))))
159 (if (> (ez-mem-ref-byte plist-addr) 0)
160 (ez-get-zstring (+ 1 plist-addr))
163 (defun ez-get-prop-default (prop)
164 (ez-mem-ref-word (+ ez-objtab-addr (* 2 (+ 1 prop)))))
166 (defun ez-get-prop-len (prop-size-byte)
167 (lsh prop-size-byte -5))
169 (defun ez-get-prop-num (prop-size-byte)
170 (logand #b00011111 prop-size-byte))
172 (defun ez-get-obj-prop-addr (obj prop)
173 (let* ((plist-addr (ez-get-obj-plist-addr obj))
174 (prop-addr (+ plist-addr 1 (* 2 (ez-mem-ref-byte plist-addr))))
175 (size-byte (ez-mem-ref-byte prop-addr)))
176 (while (not (or (= size-byte 0)
177 (= prop (ez-get-prop-num size-byte))))
178 (setq prop-addr (+ prop-addr (ez-get-prop-len size-byte) 2)
179 size-byte (ez-mem-ref-byte prop-addr)))
182 (defun ez-get-obj-prop (obj prop)
183 (let* ((prop-addr (ez-get-obj-prop-addr obj prop))
184 (size-byte (ez-mem-ref-byte prop-addr)))
186 (ez-get-prop-default prop)
187 (let ((prop-len (ez-get-prop-len size-byte))
188 (data-addr (+ prop-addr 1)))
191 (ez-mem-ref-byte data-addr))
193 (ez-mem-ref-word data-addr)))))))
195 (defun ez-set-obj-prop (obj prop value)
196 (let* ((prop-addr (ez-get-obj-prop-addr obj prop))
197 (size-byte (ez-mem-ref-byte prop-addr)))
200 (error "Tried to set non-existant property"))
201 ((= (ez-get-prop-len size-byte) 1)
202 (ez-mem-set-byte (+ prop-addr 1) value))
204 (ez-mem-set-word (+ prop-addr 1) value)))))
206 (defun ez-get-obj-attr (obj attr)
207 (let* ((byte-num (/ attr 8))
208 (bit-num (mod attr 8))
209 (bit-mask (lsh 1 (- 7 bit-num))))
210 (if (> (logand bit-mask (ez-mem-ref-byte (+ (ez-get-obj-addr obj) byte-num))) 0)
214 (defun ez-set-obj-attr (obj attr val)
215 (let* ((byte-num (/ attr 8))
216 (bit-num (mod attr 8))
217 (bit-mask (lsh 1 (- 7 bit-num)))
218 (byte-addr (+ (ez-get-obj-addr obj) byte-num))
219 (byte (ez-mem-ref-byte byte-addr))
220 (current-set (> (logand bit-mask byte) 0)))
221 (if (or (and current-set (= val 0))
222 (and (not current-set) (> val 0)))
223 (ez-mem-set-byte byte-addr (logxor byte bit-mask)))))
227 (defvar ez-zstring-alphabets
230 (list " abcdefghijklmnopqrstuvwxyz"
231 " ABCDEFGHIJKLMNOPQRSTUVWXYZ"
232 " \n0123456789.,!?_#'\"/\\-:()")
233 "Alphabets used by V3")
235 (defun ez-parse-zstring-word (word)
237 (logand (lsh word -10) #b11111)
238 (logand (lsh word -5) #b11111)
239 (logand word #b11111)))
241 (defun ez-get-zstring-chars (base-addr)
242 (let ((addr base-addr)
247 (let ((components (ez-parse-zstring-word (ez-mem-ref-word addr))))
248 (setq chars (append chars (cdr components)))
249 (setq addr (+ addr 2))
250 (setq word-count (+ word-count 1))
251 (when (= (car components) 1)
252 (setq not-done nil))))
253 (cons word-count chars)))
255 (defun ez-get-zstring (base-addr)
256 (let* ((word-count-and-chars (ez-get-zstring-chars base-addr))
257 (word-count (car word-count-and-chars))
258 (chars (cdr word-count-and-chars))
267 (* 2 (ez-mem-ref-word (+ ez-abbrevtab-addr
268 (* 2 (+ (* (- abbrev-char 1) 32) char)))))))
269 (setq s (concat s (cdr (ez-get-zstring abbrev-addr)))))
270 (setq abbrev-char nil))
271 ((memq char '(1 2 3)) ;Abbreviation
272 (setq abbrev-char char))
274 (setq cur (mod (+ cur 1) 3)))
276 (setq cur (mod (+ 3 (- cur 1)) 3)))
278 (setq s (concat s (substring (elt ez-zstring-alphabets cur)
281 (cons word-count s)))
286 (defvar ez-call-stack nil)
288 (defun ez-make-call-stack-frame (pc &optional call-method)
294 (defun ez-add-call-stack-frame (pc &optional call-method)
295 (push (ez-make-call-stack-frame pc call-method) ez-call-stack))
297 (defun ez-pop-call-stack-frame ()
300 (defun ez-routine-stack ()
301 (elt (car ez-call-stack) 1))
303 (defun ez-routine-stack-push (val)
304 (let ((frame (car ez-call-stack)))
305 (setf (elt frame 1) (cons val (elt frame 1)))))
307 (defun ez-routine-stack-pop ()
308 (let* ((frame (car ez-call-stack))
309 (rs-head (car (elt frame 1))))
310 (setf (elt frame 1) (cdr (elt frame 1)))
313 (defun ez-get-local-var (lvar)
314 (let ((frame (car ez-call-stack)))
315 (aref (elt frame 2) (- lvar 1))))
317 (defun ez-set-local-var (lvar val)
318 (let ((frame (car ez-call-stack)))
319 (aset (elt frame 2) (- lvar 1) val)))
322 (caar ez-call-stack))
324 (defun ez-set-pc (new-pc)
325 (setf (car (car ez-call-stack)) new-pc))
327 (defun ez-increment-pc (inc)
328 (ez-set-pc (+ (ez-get-pc) inc)))
330 (defun ez-read-pc-byte-and-inc ()
331 (let ((res (ez-mem-ref-byte (ez-get-pc))))
335 (defun ez-read-pc-word-and-inc ()
336 (let ((res (ez-mem-ref-word (ez-get-pc))))
340 ;; Instruction execution
342 (defun ez-get-var (var)
345 (ez-routine-stack-pop))
347 (ez-get-local-var var))
349 (ez-get-global-var (- var 16)))))
351 (defun ez-set-var (var val)
354 (ez-routine-stack-push val))
356 (ez-set-local-var var val))
358 (ez-set-global-var (- var 16) val))))
360 (defun ez-read-pc-var-and-inc ()
361 (ez-get-var (ez-read-pc-byte-and-inc)))
363 (defun ez-execute-instr ()
364 (let ((instr-pc (ez-get-pc))
365 (opbyte (ez-read-pc-byte-and-inc))
366 (optype) (opcode) (operands))
368 ((<= #x0 opbyte #x1f)
371 operands (list (ez-read-pc-byte-and-inc)
372 (ez-read-pc-byte-and-inc))
373 operand-types '(b b)))
374 ((<= #x20 opbyte #x3F)
376 opcode (- opbyte #x20)
377 operands (list (ez-read-pc-byte-and-inc)
378 (ez-read-pc-var-and-inc))
379 operand-types '(b w)))
380 ((<= #x40 opbyte #x5F)
382 opcode (- opbyte #x40)
383 operands (list (ez-read-pc-var-and-inc)
384 (ez-read-pc-byte-and-inc))
385 operand-types '(w b)))
386 ((<= #x60 opbyte #x7F)
388 opcode (- opbyte #x60)
389 operands (list (ez-read-pc-var-and-inc)
390 (ez-read-pc-var-and-inc))
391 operand-types '(w w)))
392 ((<= #x80 opbyte #x8F)
394 opcode (- opbyte #x80)
395 operands (list (ez-read-pc-word-and-inc))
397 ((<= #x90 opbyte #x9F)
399 opcode (- opbyte #x90)
400 operands (list (ez-read-pc-byte-and-inc))
402 ((<= #xA0 opbyte #xAF)
404 opcode (- opbyte #xa0)
405 operands (list (ez-read-pc-var-and-inc))
407 ((<= #xB0 opbyte #xBF)
409 opcode (- opbyte #xb0)
412 ((<= #xC0 opbyte #xDF)
414 opcode (- opbyte #xc0))
415 (let ((operands-and-types (ez-read-var-operands-and-inc)))
416 (setq operands (car operands-and-types)
417 operand-types (cdr operands-and-types))))
418 ((<= #xE0 opbyte #xFF)
420 opcode (- opbyte #xe0))
421 (let ((operands-and-types (ez-read-var-operands-and-inc)))
422 (setq operands (car operands-and-types)
423 operand-types (cdr operands-and-types)))))
424 (let ((table-row (assoc (list optype opcode) ez-op-table)))
426 (error "Unsupported op PC:%x Optype:%s Opcode:%x Operands:%s Operand-types:%s"
427 instr-pc optype opcode operands operand-types))
428 (let ((mnemonic (elt table-row 1)))
429 (message "PC:%x Optype:%s Opcode:%x Mnemonic:%s Operands:%s Operand-types:%s"
430 instr-pc optype opcode mnemonic operands operand-types))
431 (funcall (elt table-row 2) operands operand-types))))
433 (defun ez-read-var-operands-and-inc ()
434 (let* ((type-byte (ez-read-pc-byte-and-inc))
435 (types (let ((type1 (lsh type-byte -6)))
439 (let ((type2 (lsh (logand #b110000 type-byte) -4)))
443 (let ((type3 (lsh (logand #b1100 type-byte) -2)))
447 (let ((type4 (logand #b11)))
450 (list type4))))))))))))))
455 ((= type 0) (ez-read-pc-word-and-inc))
456 ((= type 1) (ez-read-pc-byte-and-inc))
457 ((= type 2) (ez-read-pc-var-and-inc))))
468 (defun ez-do-branch (branch)
469 (let* ((branch-byte (ez-read-pc-byte-and-inc))
470 (invert (= 0 (logand branch-byte #b10000000)))
471 (single-byte (> (logand branch-byte #b01000000) 0))
474 (logand branch-byte #b00111111)
475 (let ((pos (= (logand branch-byte #b00100000) 0))
476 (val (+ (* 256 (logand branch-byte #b00011111))
477 (ez-read-pc-byte-and-inc))))
481 (if (or (and branch (not invert))
482 (and (not branch) invert))
489 (ez-set-pc (+ (ez-get-pc) offset -2)))))))
494 '(((0op #x00) rtrue ez-op-rtrue)
495 ((0op #x01) rfalse ez-op-rfalse)
496 ((1op #x00) jz ez-op-jz)
497 ((1op #x05) inc ez-op-inc)
498 ((1op #x06) dec ez-op-dec)
499 ((1op #x0B) ret ez-op-ret)
500 ((1op #x0C) jump ez-op-jump)
501 ((2op #x05) inc_jg ez-op-inc-jg)
502 ((2op #x04) dec_jg ez-op-dec-jg)
503 ((2op #x0D) store ez-op-store)
504 ((1op #x0E) load ez-op-load)
505 ((var #x01) storew ez-op-storew)
506 ((2op #x0F) loadw ez-op-loadw)
507 ((var #x02) storeb ez-op-storeb)
508 ((2op #x10) loadb ez-op-loadb)
509 ((2op #x01) je ez-op-je)
510 ((2op #x08) or ez-op-or)
511 ((2op #x09) and ez-op-and)
512 ((2op #x14) add ez-op-add)
513 ((2op #x15) sub ez-op-sub)
514 ((2op #x16) mul ez-op-mul)
515 ((2op #x17) div ez-op-div)
516 ((2op #x18) mod ez-op-mod)
517 ((var #x00) call_fv ez-op-callf)
518 ((1op #x01) get_sibling ez-op-get-sibling)
519 ((1op #x02) get_child ez-op-get-child)
520 ((1op #x03) get_parent ez-op-get-parent)
521 ((2op #x0A) test_attr ez-op-test-attr)
522 ((var #x03) put_prop ez-op-put-prop)
523 ((0op #x02) print ez-op-print)
524 ((0op #x0B) new_line ez-op-new-line)
525 ((var #x06) print_num ez-op-print-num)
526 ((var #x05) print_char ez-op-print-char)))
528 (defun ez-op-ret (operands &optional operand-types)
529 (let ((retval (car operands)))
530 (ez-pop-call-stack-frame)
531 (ez-set-var (ez-read-pc-byte-and-inc) retval))
534 (defun ez-op-rtrue (&optional operands operand-types)
538 (defun ez-op-rfalse (&optional operands operand-types)
542 (defun ez-op-jz (operands operand-types)
543 (ez-do-branch (= (car operands) 0))
546 (defun ez-op-je (operands operand-types)
547 (ez-do-branch (memq (car operands) (cdr operands)))
550 (defun ez-op-jg (operands operand-types)
551 (let ((s1 (ez-decode-signed-operand (car operands) (car operand-types)))
552 (s2 (ez-decode-signed-operand (cadr operands) (cadr operand-types))))
553 (ez-do-branch (> s1 s2)))
556 (defun ez-op-jl (operands operand-types)
557 (let ((s1 (ez-decode-signed-operand (car operands) (car operand-types)))
558 (s2 (ez-decode-signed-operand (cadr operands) (cadr operand-types))))
559 (ez-do-branch (< s1 s2)))
562 (defun ez-op-inc-jg (operands operand-types)
563 (let ((var (car operands)))
564 (ez-op-inc (list var))
565 (ez-op-jg (cons (ez-get-var var) (cdr operands)) (cons 'w (cdr operand-types))))
568 (defun ez-op-dec-jl (operands operand-types)
569 (let ((var (car operands)))
570 (ez-op-dec (list var))
571 (ez-op-jl (cons (ez-get-var var) (cdr operands)) (cons 'w (cdr operand-types))))
575 (defun ez-op-jump (operands operand-types)
576 (let ((offset (if (eq (car operand-types) 'b)
577 (ez-decode-signed-byte (car operands))
578 (ez-decode-signed-word (car operands)))))
579 (ez-set-pc (+ (ez-get-pc) offset -2)))
582 (defun ez-op-inc (operands &optional operand-types)
583 (let ((var (car operands)))
584 (ez-set-var var (mod (+ 1 (ez-get-var var)) #x10000)))
587 (defun ez-op-dec (operands &optional operand-types)
588 (let ((var (car operands)))
589 (ez-set-var var (mod (+ (ez-get-var var) 1) #x10000)))
592 (defun ez-op-store (operands operand-types)
593 (let ((var (car operands))
598 (defun ez-op-load (operands operand-types)
599 (let ((var (car operands)))
600 (ez-set-var (ez-read-pc-byte-and-inc) (ez-get-var var)))
603 (defun ez-op-storew (operands operand-types)
604 (let ((baddr (car operands))
606 (a (caddr operands)))
607 (ez-mem-set-word (+ baddr (* 2 n)) a))
610 (defun ez-op-loadw (operands operand-types)
611 (let ((baddr (car operands))
613 (ez-set-var (ez-read-pc-byte-and-inc) (ez-mem-ref-word (+ baddr (* 2 n)))))
616 (defun ez-op-storeb (operands operand-types)
617 (let ((baddr (car operands))
619 (a (caddr operands)))
620 (ez-mem-set-byte (+ baddr n) a))
623 (defun ez-op-loadb (operands operand-types)
624 (let ((baddr (car operands))
626 (ez-set-var (ez-read-pc-byte-and-inc) (ez-mem-ref-byte (+ baddr n))))
629 (defun ez-op-and (operands operand-types)
630 (let ((a (car operands))
632 (ez-set-var (ez-read-pc-byte-and-inc) (logand a b)))
635 (defun ez-op-or (operands operand-types)
636 (let ((a (car operands))
638 (ez-set-var (ez-read-pc-byte-and-inc) (logior a b)))
641 (defun ez-op-add (operands operand-types)
642 (let ((a (car operands))
644 (ez-set-var (ez-read-pc-byte-and-inc) (mod (+ a b) #x10000)))
647 (defun ez-op-sub (operands operand-types)
648 (let ((a (car operands))
650 (ez-set-var (ez-read-pc-byte-and-inc) (mod (+ (- a b) #x10000) #x10000)))
653 (defun ez-op-mul (a b)
654 (let ((a (car operands))
656 (ez-set-var (ez-read-pc-byte-and-inc) (mod (* a b) #x10000)))
659 (defun ez-op-div (a b)
660 (error "Not implemented"))
662 (defun ez-op-mod (a b)
663 (error "Not implemented"))
665 (defun ez-op-callf (operands operand-types)
666 (let* ((raddr (car operands))
667 (call-operands (cdr operands))
669 (L (ez-mem-ref-byte r))
670 (n (length call-operands))
671 (new-pc (+ r 1 (* L 2))))
672 (ez-add-call-stack-frame new-pc)
675 (ez-set-local-var (+ i 1) (elt call-operands i))
676 (ez-set-local-var (+ i 1) (ez-mem-ref-word (+ r 1 (* 2 i)))))))
679 (defun ez-op-test-attr (operands operand-types)
680 (let ((obj (car operands))
681 (attr (cadr operands)))
682 (ez-do-branch (= 1 (ez-get-obj-attr obj attr)))
685 (defun ez-op-get-sibling (operands operand-types)
686 (let ((sib (ez-get-obj-sibling (car operands))))
687 (ez-set-var (ez-read-pc-byte-and-inc) sib)
688 (ez-do-branch (> sib 0))
691 (defun ez-op-get-child (operands operand-types)
692 (let ((child (ez-get-obj-child (car operands))))
693 (ez-set-var (ez-read-pc-byte-and-inc) child)
694 (ez-do-branch (> child 0))
697 (defun ez-op-get-parent (operands operand-types)
698 (let ((parent (ez-get-obj-parent (car operands))))
699 (ez-set-var (ez-read-pc-byte-and-inc) parent)
700 (ez-do-branch (> parent 0))
703 (defun ez-op-put-prop (operands operand-types)
704 (let* ((obj (car operands))
705 (prop (cadr operands))
706 (a (caddr operands)))
707 (ez-set-obj-prop obj prop a)
710 (defun ez-op-print (operands operand-types)
711 (let* ((word-count-and-string (ez-get-zstring (ez-get-pc)))
712 (word-count (car word-count-and-string))
713 (string (cdr word-count-and-string)))
715 (ez-increment-pc (* 2 word-count)))
718 (defun ez-op-new-line (operands operand-types)
722 (defun ez-op-print-num (operands operand-types)
723 (let ((s (ez-decode-signed-operand (car operands) (car operand-types))))
724 (ez-print (number-to-string s)))
727 (defun ez-op-print-char (operands operand-types)
728 (let ((c (car operands)))
729 (ez-print (string c)))
735 (defun ez-run (filename)
736 (ez-load-file filename)
738 (setq ez-call-stack (list (ez-make-call-stack-frame ez-start-pc)))
740 (while (ez-execute-instr)))
746 (defun ez-render-prompt ()
747 (with-current-buffer "*ez*"
748 (let ((update-point (= ez-input-marker (point)))
749 (update-window-points (mapcar (lambda (w)
750 (list (= (window-point w) ez-input-marker)
752 (get-buffer-window-list nil nil t))))
754 (set-marker-insertion-type ez-prompt-marker nil)
755 (set-marker-insertion-type ez-input-marker t)
756 (let ((inhibit-read-only t))
757 (delete-region ez-prompt-marker ez-input-marker)
758 (goto-char ez-prompt-marker)
761 (propertize " " ; Need this to be separate to mark it as rear-nonsticky
764 (set-marker-insertion-type ez-input-marker nil))
765 (goto-char ez-input-marker))))
767 (defvar ez-prompt-marker nil
768 "Marker for prompt position in buffer.")
770 (defvar ez-input-marker nil
771 "Marker for prompt position in buffer.")
773 (defun ez-setup-buffer ()
774 (with-current-buffer (get-buffer-create "*ez*")
775 (let ((inhibit-read-only t))
776 (delete-region (point-min) (point-max)))
777 (setq-local scroll-conservatively 1)
778 (if (markerp ez-prompt-marker)
779 (set-marker ez-prompt-marker (point-max))
780 (setq ez-prompt-marker (point-max-marker)))
781 (if (markerp ez-input-marker)
782 (set-marker ez-input-marker (point-max))
783 (setq ez-input-marker (point-max-marker)))
784 (goto-char (point-max))
788 (defun ez-print (string)
789 (with-current-buffer "*ez*"
791 (goto-char ez-prompt-marker)
792 (insert-before-markers string))))
797 (let ((map (make-sparse-keymap)))
798 (define-key map (kbd "RET") 'ez-enter)
801 (define-derived-mode ez-mode text-mode "ez"
802 "Major mode for EZ.")
804 (when (fboundp 'evil-set-initial-state)
805 (evil-set-initial-state 'ez-mode 'insert))
808 (interactive "fEnter name of z3 story file: ")
809 (if (get-buffer "*ez*")
810 (switch-to-buffer "*ez*")
811 (switch-to-buffer "*ez*")
819 (switch-to-buffer "*ez*")