Emacs NYC talk
[arxana.git] / elisp / simple-ui.el
blobb3ed29a4bf09bf864fd23f4db4ce384122e1dc9a
2 (defvar simple-ui-buff (list nil (make-list 7 nil) nil))
3 ;; This variable stores three items: the buffer in which
4 ;; the nema is to be edited, markers inside that buffer
5 ;; which delimit the text which the user supplies, and
6 ;; the number of the nema being edited.
8 (defun simple-ui-init ()
9 "Initialize the buffer for editing nemata."
10 (interactive)
11 (let ((buff (get-buffer-create "nema-edit"))
12 (marx nil)
13 (places nil))
14 (cl-flet ((add-rubric (pre mid post)
15 (insert
16 (concat
17 (propertize pre
18 'face 'bold
19 'read-only "Rubric."
20 'front-sticky nil
21 'rear-nonsticky nil)
22 (propertize mid
23 'face 'bold
24 'read-only "Rubric."
25 'front-sticky t
26 'rear-nonsticky nil)
27 (propertize post
28 'face 'bold
29 'read-only "Rubric."
30 'front-sticky t
31 'rear-nonsticky t)))))
32 ;; Since the buffer may contain read-only
33 ;; text, we must inhibit the read-only
34 ;; property in order to erase such text.
35 (with-current-buffer buff
36 (let ((inhibit-read-only t))
37 (erase-buffer))
38 (dotimes (n 7)
39 (push (make-marker) marx))
40 (add-rubric "" "Editing nema" ":")
41 (push (- (point) 1) places)
42 (push (+ (point) 1) places)
43 (add-rubric "\n" "Source" ":")
44 (push (- (point) 1) places)
45 (insert " ")
46 (push (+ (point) 1) places)
47 (add-rubric "\n" "Sink" ":")
48 (push (- (point) 1) places)
49 (insert " ")
50 (push (+ (point) 1) places)
51 (add-rubric "\n" "Content" ":")
52 (push (- (point) 1) places)
53 (insert " ")
54 (mapply 'set-marker `(,marx ,(reverse places)))
55 (setq simple-ui-buff (list buff marx nil)))))
56 simple-ui-buff)
58 (defun simple-ui-load (nema)
59 "Load a nema into the editing buffer."
60 (interactive "xNema uid:")
61 (if (uid-p nema)
62 (cl-flet (
63 ;; Helper function to figure out character
64 ;; positions between which text goes.
65 (spot (n)
66 (if (< n 7)
67 (+ (marker-position
68 (nth n (nth 1 simple-ui-buff)))
69 (- 1 (* (% n 2) 2)))
70 (point-max))))
71 (with-current-buffer (nth 0 simple-ui-buff)
72 ;; Replace nema
73 (delete-region (spot 0) (spot 1))
74 (goto-char (spot 0))
75 (insert (prin1-to-string nema))
76 ;; Replace source.
77 (delete-region (spot 2) (spot 3))
78 (goto-char (spot 2))
79 (insert (prin1-to-string (get-source nema)))
80 ;; Replce sink.
81 (delete-region (spot 4) (spot 5))
82 (goto-char (spot 4))
83 (insert (prin1-to-string (get-sink nema)))
84 ;; Replace content
85 (delete-region (spot 6) (spot 7))
86 (goto-char (spot 6))
87 (insert (prin1-to-string (get-content nema)))
88 (setcar (cddr simple-ui-buff) nema))
89 nema)
90 'huh))
92 (defun simple-ui-new ()
93 "Make a new nema and edit it."
94 (interactive)
95 (simple-ui-load
96 (add-nema 0 0 nil)))
99 (defun simple-ui-save ()
100 "Save the nema in the editing buffer."
101 (interactive)
102 (if (uid-p (nth 2 simple-ui-buff))
103 (cl-flet (
104 ;; Helper function to figure out character
105 ;; positions between which text goes.
106 (spot (n)
107 (if (< n 7)
108 (+ (marker-position
109 (nth n (nth 1 simple-ui-buff)))
110 (- 1 (* (% n 2) 2)))
111 (point-max))))
112 (with-current-buffer (nth 0 simple-ui-buff)
113 (update-source
114 (nth 2 simple-ui-buff)
115 (car (read-from-string
116 (buffer-substring (spot 2) (spot 3)))))
117 (update-sink
118 (nth 2 simple-ui-buff)
119 (car (read-from-string
120 (buffer-substring (spot 4) (spot 5)))))
121 (update-content
122 (nth 2 simple-ui-buff)
123 (car (read-from-string
124 (buffer-substring (spot 6) (spot 7))))))
125 (nth 2 simple-ui-buff))
126 nil))