-
Notifications
You must be signed in to change notification settings - Fork 0
/
mouseedit.scm
428 lines (385 loc) · 14.8 KB
/
mouseedit.scm
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
;;; ezd - easy drawing for X11 displays.
;;;
;;; The procedures in this module implement a mouse based editor for use inside
;;; a TEXT-DRAWING. In order to test the utility of attributes, all
;;; communication with the text drawing is via attributes.
;* Copyright 1990-1993 Digital Equipment Corporation
;* All Rights Reserved
;*
;* Permission to use, copy, and modify this software and its documentation is
;* hereby granted only under the following terms and conditions. Both the
;* above copyright notice and this permission notice must appear in all copies
;* of the software, derivative works or modified versions, and any portions
;* thereof, and both notices must appear in supporting documentation.
;*
;* Users of this software agree to the terms and conditions set forth herein,
;* and hereby grant back to Digital a non-exclusive, unrestricted, royalty-free
;* right and license under any changes, enhancements or extensions made to the
;* core functions of the software, including but not limited to those affording
;* compatibility with other hardware or software environments, but excluding
;* applications which incorporate this software. Users further agree to use
;* their best efforts to return to Digital any such changes, enhancements or
;* extensions that they make and inform Digital of noteworthy uses of this
;* software. Correspondence should be provided to Digital at:
;*
;* Director of Licensing
;* Western Research Laboratory
;* Digital Equipment Corporation
;* 250 University Avenue
;* Palo Alto, California 94301
;*
;* This software may be distributed (but not offered for sale or transferred
;* for compensation) to third parties, provided such third parties agree to
;* abide by the terms and conditions of this notice.
;*
;* THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
;* WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
;* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
;* CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
;* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
;* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
;* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
;* SOFTWARE.
;;; When a TEXT-DRAWING is created, the following procedure is called to
;;; install mouse based editting.
(define (mouse-edit-init drawing object options)
(let* ((read-only (memq 'read-only options))
(current-window #f)
(first-line #f)
(last-line #f)
(slider #f)
(button1 #f)
(button1down-line #f)
(button1down-char #f)
(cursor-line 0)
(cursor-char 0)
(selection #f)
(begin-line 0)
(begin-char 0)
(end-line 0)
(end-char 0)
(undo-cursor #f)
(undo-text #f))
;;; Change cursor on drawing entry and exit. Find out viewed lines
;;; and slider name on entry.
(define (enter)
(when (not current-window)
(set! current-window *user-event-window*)
(let ((view (get-attribute drawing object
`(view ,current-window))))
(set! first-line (car view))
(set! last-line (cadr view))
(set! slider (caddr view)))
(set! button1 #f)
(ezd `(save-cursor ,current-window)
`(set-cursor ,current-window XC_XTERM))))
(define (exit)
(when (and current-window
(or (not (eq? (car *user-event-misc*)
current-window))
(not (eq? (cadr *user-event-misc*) drawing))))
(ezd `(restore-cursor ,current-window))
(set! current-window #f)))
;;; Covert the mouse position in the current event to the cursor
;;; position stored here. Note that the screen cursor is not
;;; updated at this time.
(define (mouse->cursor)
(let* ((line-char-text (get-attribute drawing object
`(xy->line-char-text
,*user-event-x*
,*user-event-y*)))
(line (car line-char-text))
(char (cadr line-char-text))
(text (caddr line-char-text)))
(set! cursor-line line)
(set! cursor-char
(if (and (positive? char)
(eq? char (string-length text))
(eq? (string-ref text (- char 1))
#\newline))
(- char 1)
char))))
;;; Query the drawing for the current cursor position. If the
;;; cursor is not visibile in the current window, then scroll it to
;;; make it visible.
(define (read-cursor)
(let ((line-char (get-attribute drawing object 'cursor)))
(set! cursor-line (car line-char))
(set! cursor-char (cadr line-char))
(if (not (<= first-line cursor-line last-line))
(let ((newfirst (if (< cursor-line first-line)
cursor-line
(+ first-line
(- cursor-line last-line)))))
(set! last-line
(+ last-line (- newfirst first-line)))
(set! first-line newfirst)
(if slider
(set-attributes slider 'slider
`(value ,newfirst)))
(set-attributes drawing object
'(mouse-edit)
`(scroll ,current-window ,newfirst))))))
;;; Mouse button 1 going down sets the cursor and clears any
;;; current selection.
(define (button1down)
(mouse->cursor)
(set! button1down-line cursor-line)
(set! button1down-char cursor-char)
(set! selection #f)
(set! button1 #t)
(set-attributes drawing object
'(mouse-edit)
`(cursor ,cursor-line ,cursor-char) '(highlight)))
;;; Motion with mouse button 1 down causes the cursor to move and
;;; starts/extends the current selection. Completion of the
;;; selection causes the selection to be copied to the X cut buffer
;;; when in READ-ONLY mode.
(define (motion-button1up)
(if button1
(let ((event *user-event-xevent*))
(mouse->cursor)
(set! button1 *mouse-button1*)
(extend-selection cursor-line cursor-char)
(if (and read-only (not button1))
(cut/copy #f (xevent-xbutton-time event))))))
;;; The current selection is extended by the following function. The
;;; cursor is placed at the end of the selection. If the selection
;;; turns out to be null when the button comes up, it disappears.
(define (extend-selection line char)
(if (lc<? button1down-line button1down-char line char)
(let* ((lc (inc-line-char drawing object line char #f
-1))
(new-line (car lc))
(new-char (cadr lc)))
(set! begin-line button1down-line)
(set! begin-char button1down-char)
(set! end-line new-line)
(set! end-char new-char))
(let* ((lc (inc-line-char drawing object button1down-line
button1down-char #f -1))
(new-line (car lc))
(new-char (cadr lc)))
(set! begin-line line)
(set! begin-char char)
(set! end-line new-line)
(set! end-char new-char)))
(if (lc=? line char button1down-line button1down-char)
(begin (set! selection #f)
(set! cursor-line line)
(set! cursor-char char)
(set-attributes drawing object
'(mouse-edit)
`(cursor ,line ,char) '(highlight)))
(let ((lc (inc-line-char drawing object end-line end-char
#f 1)))
(set! cursor-line (car lc))
(set! cursor-char (cadr lc))
(set! selection #t)
(set-attributes drawing object
'(mouse-edit)
`(cursor ,cursor-line ,cursor-char)
`(highlight ,begin-line ,begin-char
,end-line ,end-char)))))
;;; Keyboard input is handled here.
(define (keypress)
(let* ((key (car *user-event-misc*))
(keysym (cadr *user-event-misc*))
(ascii-code (if (equal? key "") 0
(char->integer (string-ref key 0))))
(key-state (xevent-xkey-state *user-event-xevent*))
(time (xevent-xkey-time *user-event-xevent*)))
(cond (read-only (ezd '(bell)))
((or (eq? ascii-code 8) ;;; control-h
(eq? ascii-code 127));;; backspace
(if selection
(delete-selection)
(delete-before-cursor)))
((eq? ascii-code 13) ;;; return
(delete-selection)
(set-attributes drawing object
'(mouse-edit)
`(insert ,cursor-line ,cursor-char
,(list->string '(#\newline))))
(read-cursor)
(unless (zero? cursor-char)
(set! cursor-line (+ cursor-line 1))
(set! cursor-char 0)
(set-attributes drawing object
'(mouse-edit)
`(cursor ,cursor-line ,cursor-char))
(read-cursor)))
((not (zero? (bit-and key-state MOD1MASK)))
(cond ((equal? key "z") (undo))
((equal? key "x") (cut/copy #t time))
((equal? key "c") (cut/copy #f time))
((equal? key "v") (paste))
(else (ezd '(bell)))))
((or (and (string<=? " " key)
(string<=? key "~"))
(eq? ascii-code 9)) ;;; tab
(delete-selection)
(set-attributes drawing object
'(mouse-edit)
`(insert ,cursor-line ,cursor-char ,key))
(read-cursor))
((<= XK_LEFT keysym XK_DOWN)
(cursor-motion keysym))
((and (not (<= XK_SHIFT_L keysym XK_HYPER_R))
(not (= keysym XK_MULTI_KEY)))
(ezd '(bell))))))
;;; Delete the currently selected text.
(define (delete-selection)
(when selection
(set! selection #f)
(set! undo-cursor (list begin-line begin-char))
(set! undo-text (selection->string))
(set-attributes drawing object
'(mouse-edit)
'(highlight)
`(delete ,begin-line ,begin-char
,end-line ,end-char))
(read-cursor)))
;;; Delete the character behind the cursor.
(define (delete-before-cursor)
(when (lc>? cursor-line cursor-char 0 0)
(if (zero? cursor-char) (cursor-motion XK_LEFT))
(cursor-motion XK_LEFT)
(set-attributes drawing object
'(mouse-edit)
`(delete ,cursor-line ,cursor-char
,cursor-line ,cursor-char))
(read-cursor)
(if (zero? cursor-char) (cursor-motion XK_LEFT))))
;;; Undo the last edit command.
(define (undo)
(when undo-cursor
(set-attributes drawing object
'(mouse-edit)
`(cursor ,@undo-cursor)
`(insert ,@undo-cursor ,undo-text)
'(highlight))
(set! undo-cursor #f)
(set! undo-text #f)))
;;; Return a string containing the current selection.
(define (selection->string)
(define cut-buffer #f)
(let loop ((i begin-line) (len 0))
(if (<= i end-line)
(let* ((whole-line (get-attribute drawing object
`(text-line ,i)))
(line (if (or (< begin-line i end-line)
(eq? whole-line ""))
whole-line
(substring whole-line
(if (eq? i begin-line)
begin-char
0)
(if (eq? i end-line)
(min (+ end-char 1)
(string-length
whole-line))
(string-length
whole-line)))))
(line-len (string-length line)))
(case (and (< i end-line)
(positive? line-len)
(string-ref line (- line-len 1)))
((#f #\tab #\space #\newline)
(loop (+ i 1) (+ line-len len)))
((#\.)
(loop (+ i 1) (+ line-len len 2)))
(else
(loop (+ i 1) (+ line-len len 1))))
(do ((j 0 (+ j 1)))
((= j line-len))
(string-set! cut-buffer (+ j len)
(string-ref line j))))
(set! cut-buffer (make-string len #\space))))
cut-buffer)
;;; Cut or copy the current selection to the X selection.
(define (cut/copy cut time)
(let ((cut-buffer (selection->string)))
(xsetselectionowner *dpy* XA_PRIMARY NONE time)
(xstorebytes *dpy*
(make-locative cut-buffer)
(string-length cut-buffer))
(if cut (delete-selection))))
;;; Paste the current X selection into the document.
(define (paste)
(let* ((ptr-cnt (make-s32vector 1 0))
(ptr (xfetchbytes *dpy* (make-locative ptr-cnt)))
(cnt (s32vector-ref ptr-cnt 0))
(buffer (make-string cnt)))
(delete-selection)
(move-memory! ptr buffer)
(if ptr (xfree ptr))
(set-attributes drawing object
'(mouse-edit)
`(insert ,cursor-line ,cursor-char ,buffer))
(read-cursor)))
;;; Handle a cursor character.
(define (cursor-motion keysym)
(let ((line-char (inc-line-char drawing object
cursor-line cursor-char
(cond ((eq? keysym XK_UP) -1)
((eq? keysym XK_DOWN) 1)
(else #f))
(cond ((eq? keysym XK_LEFT) -1)
((eq? keysym XK_RIGHT) 1)
(else #f)))))
(set! cursor-line (car line-char))
(set! cursor-char (cadr line-char))
(set! selection #f)
(set-attributes drawing object
'(mouse-edit)
`(cursor ,cursor-line ,cursor-char)
'(highlight))
(read-cursor)))
;;; Get current cursor position and highlight information from
;;; the drawing.
(let ((cursor-line-char (get-attribute drawing object 'cursor))
(l-c-l-c (get-attribute drawing object 'highlight)))
(set! cursor-line (car cursor-line-char))
(set! cursor-char (cadr cursor-line-char))
(when (not (negative? (cadr l-c-l-c)))
(set! selection #t)
(set! begin-line (car l-c-l-c))
(set! begin-char (cadr l-c-l-c))
(set! end-line (caddr l-c-l-c))
(set! end-char (cadddr l-c-l-c))))
;;; Install event handlers.
(ezd '(save-drawing)
`(set-drawing ,drawing)
`(when * enter ,enter)
`(when * exit ,exit)
`(when * button1down ,button1down)
`(when * motion ,motion-button1up)
`(when * button1up ,motion-button1up)
`(when * keypress ,keypress)
'(restore-drawing))))
;;; Booleans for comparing line/character positions.
(define (lc=? l0 c0 l1 c1) (and (eq? l0 l1) (eq? c0 c1)))
(define (lc<? l0 c0 l1 c1) (or (< l0 l1) (and (eq? l0 l1) (< c0 c1))))
(define (lc>? l0 c0 l1 c1) (or (> l0 l1) (and (eq? l0 l1) (> c0 c1))))
(define (lc<=? l0 c0 l1 c1) (not (lc>? l0 c0 l1 c1)))
(define (lc>=? l0 c0 l1 c1) (not (lc<? l0 c0 l1 c1)))
;;; Procedure to move a line-character position either a number of lines or a
;;; number of characters. A list consisting of the new line and character
;;; positions is returned.
(define (inc-line-char drawing object line char delta-line delta-char)
(if delta-line
(let* ((line (min (get-attribute drawing object 'lines)
(max 0 (+ line delta-line))))
(text (get-attribute drawing object `(text-line ,line)))
(text-len (string-length text))
(char (min char text-len)))
(list line char))
(let* ((text (get-attribute drawing object `(text-line ,line)))
(text-len (string-length text))
(char (+ delta-char char)))
(cond ((negative? char)
(inc-line-char drawing object line 1000000 -1 #f))
((<= char text-len)
(list line char))
(else (inc-line-char drawing object line 0 1 #f))))))