/[climacs]/climacs/gui.lisp
ViewVC logotype

Contents of /climacs/gui.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.41 - (show annotations)
Sat Jan 1 12:55:01 2005 UTC (9 years, 3 months ago) by rstrandh
Branch: MAIN
Changes since 1.40: +1 -1 lines
Committed fix for bug found by Christophe Rhodes.  Thanks.
1 ;;; -*- Mode: Lisp; Package: CLIMACS-GUI -*-
2
3 ;;; (c) copyright 2004 by
4 ;;; Robert Strandh (strandh@labri.fr)
5 ;;; (c) copyright 2004 by
6 ;;; Elliott Johnson (ejohnson@fasl.info)
7
8 ;;; This library is free software; you can redistribute it and/or
9 ;;; modify it under the terms of the GNU Library General Public
10 ;;; License as published by the Free Software Foundation; either
11 ;;; version 2 of the License, or (at your option) any later version.
12 ;;;
13 ;;; This library is distributed in the hope that it will be useful,
14 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 ;;; Library General Public License for more details.
17 ;;;
18 ;;; You should have received a copy of the GNU Library General Public
19 ;;; License along with this library; if not, write to the
20 ;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ;;; Boston, MA 02111-1307 USA.
22
23 ;;; GUI for the Climacs editor.
24
25 (in-package :climacs-gui)
26
27 (defclass filename-mixin ()
28 ((filename :initform nil :accessor filename)))
29
30 (defclass climacs-buffer (standard-buffer abbrev-mixin filename-mixin name-mixin)
31 ((needs-saving :initform nil :accessor needs-saving))
32 (:default-initargs :name "*scratch*"))
33
34
35 (defclass climacs-pane (application-pane)
36 ((buffer :initform (make-instance 'climacs-buffer) :accessor buffer)
37 (point :initform nil :initarg :point :reader point)
38 (syntax :initarg :syntax :accessor syntax)
39 (mark :initform nil :initarg :mark :reader mark)))
40
41 (defmethod initialize-instance :after ((pane climacs-pane) &rest args)
42 (declare (ignore args))
43 (with-slots (buffer point syntax mark) pane
44 (when (null point)
45 (setf point (make-instance 'standard-right-sticky-mark
46 :buffer buffer)))
47 (when (null mark)
48 (setf mark (make-instance 'standard-right-sticky-mark
49 :buffer buffer)))
50 (setf syntax (make-instance 'texinfo-syntax :pane pane))))
51
52 (defclass minibuffer-pane (application-pane) ())
53
54 (defmethod stream-accept :before ((pane minibuffer-pane) type &rest args)
55 (declare (ignore type args))
56 (window-clear pane))
57
58 (define-application-frame climacs ()
59 ((win :reader win))
60 (:panes
61 (win (make-pane 'climacs-pane
62 :width 900 :height 400
63 :name 'win
64 :incremental-redisplay t
65 :display-function 'display-win))
66 (info :application
67 :width 900 :height 20 :max-height 20
68 :name 'info :background +light-gray+
69 :scroll-bars nil
70 :incremental-redisplay t
71 :display-function 'display-info)
72 (int (make-pane 'minibuffer-pane
73 :width 900 :height 20 :max-height 20 :min-height 20
74 :scroll-bars nil)))
75 (:layouts
76 (default
77 (vertically (:scroll-bars nil)
78 (scrolling (:width 900 :height 400) win)
79 info
80 int)))
81 (:top-level (climacs-top-level)))
82
83 (defmethod redisplay-frame-panes :after ((frame climacs) &rest args)
84 (declare (ignore args))
85 (clear-modify (buffer (win frame))))
86
87 (defun climacs ()
88 "Starts up a climacs session"
89 (let ((frame (make-application-frame 'climacs)))
90 (run-frame-top-level frame)))
91
92 (defun display-message (format-string &rest format-args)
93 (apply #'format *standard-input* format-string format-args))
94
95 (defun display-info (frame pane)
96 (let* ((win (win frame))
97 (buf (buffer win))
98 (name-info (format nil " ~a ~a Syntax: ~a"
99 (if (needs-saving buf) "**" "--")
100 (name buf)
101 (name (syntax win)))))
102 (princ name-info pane)))
103
104 (defun display-win (frame pane)
105 "The display function used by the climacs application frame."
106 (declare (ignore frame))
107 (redisplay-pane pane))
108
109 (defun find-gestures (gestures start-table)
110 (loop with table = (find-command-table start-table)
111 for (gesture . rest) on gestures
112 for item = (find-keystroke-item gesture table :errorp nil)
113 while item
114 do (if (eq (command-menu-item-type item) :command)
115 (return (if (null rest) item nil))
116 (setf table (command-menu-item-value item)))
117 finally (return item)))
118
119 (defvar *kill-ring* (initialize-kill-ring 7))
120 (defparameter *current-gesture* nil)
121
122 (defun meta-digit (gesture)
123 (position gesture
124 '((#\0 :meta) (#\1 :meta) (#\2 :meta) (#\3 :meta) (#\4 :meta)
125 (#\5 :meta) (#\6 :meta) (#\7 :meta) (#\8 :meta) (#\9 :meta))
126 :test #'event-matches-gesture-name-p))
127
128 (defun read-numeric-argument (&key (stream *standard-input*))
129 (let ((gesture (read-gesture :stream stream)))
130 (cond ((event-matches-gesture-name-p gesture '(#\u :control))
131 (let ((numarg 4))
132 (loop for gesture = (read-gesture :stream stream)
133 while (event-matches-gesture-name-p gesture '(#\u :control))
134 do (setf numarg (* 4 numarg))
135 finally (unread-gesture gesture :stream stream))
136 (let ((gesture (read-gesture :stream stream)))
137 (cond ((and (characterp gesture)
138 (digit-char-p gesture 10))
139 (setf numarg (- (char-code gesture) (char-code #\0)))
140 (loop for gesture = (read-gesture :stream stream)
141 while (and (characterp gesture)
142 (digit-char-p gesture 10))
143 do (setf gesture (+ (* 10 numarg)
144 (- (char-code gesture) (char-code #\0))))
145 finally (unread-gesture gesture :stream stream)
146 (return (values numarg t))))
147 (t
148 (values numarg t))))))
149 ((meta-digit gesture)
150 (let ((numarg (meta-digit gesture)))
151 (loop for gesture = (read-gesture :stream stream)
152 while (meta-digit gesture)
153 do (setf numarg (+ (* 10 numarg) (meta-digit gesture)))
154 finally (unread-gesture gesture :stream stream)
155 (return (values numarg t)))))
156 (t (unread-gesture gesture :stream stream)
157 (values 1 nil)))))
158
159 (defun climacs-top-level (frame &key
160 command-parser command-unparser
161 partial-command-parser prompt)
162 (declare (ignore command-parser command-unparser partial-command-parser prompt))
163 (setf (slot-value frame 'win) (find-pane-named frame 'win))
164 (let ((*standard-output* (find-pane-named frame 'win))
165 (*standard-input* (find-pane-named frame 'int))
166 (*print-pretty* nil)
167 (*abort-gestures* nil))
168 (redisplay-frame-panes frame :force-p t)
169 (loop with gestures = '()
170 with numarg = 1 ; FIXME (read-numeric-argument :stream *standard-input*)
171 do (setf *current-gesture* (read-gesture :stream *standard-input*))
172 (when (or (characterp *current-gesture*)
173 (and (typep *current-gesture* 'keyboard-event)
174 (or (keyboard-event-character *current-gesture*)
175 (not (member (keyboard-event-key-name
176 *current-gesture*)
177 '(:control-left :control-right
178 :shift-left :shift-right
179 :meta-left :meta-right
180 :super-left :super-right
181 :hyper-left :hyper-right
182 :shift-lock :caps-lock))))))
183 (setf gestures (nconc gestures (list *current-gesture*)))
184 (let ((item (find-gestures gestures 'global-climacs-table)))
185 (cond ((not item)
186 (beep) (setf gestures '()))
187 ((eq (command-menu-item-type item) :command)
188 (let ((command (command-menu-item-value item)))
189 (unless (consp command)
190 (setf command (list command)))
191 (setf command (substitute-numeric-argument-marker command numarg))
192 (handler-case
193 (execute-frame-command frame command)
194 (error (condition)
195 (beep)
196 (format *error-output* "~a~%" condition)))
197 (setf gestures '())))
198 (t nil))))
199 (let ((buffer (buffer (win frame))))
200 (when (modified-p buffer)
201 (setf (needs-saving buffer) t)))
202 (redisplay-frame-panes frame))))
203
204 (defmacro define-named-command (command-name args &body body)
205 `(define-climacs-command ,(if (listp command-name) `(,@command-name :name t) `(,command-name :name t)) ,args ,@body))
206
207 (define-named-command (com-quit) ()
208 (frame-exit *application-frame*))
209
210 (define-command com-self-insert ()
211 (unless (constituentp *current-gesture*)
212 (possibly-expand-abbrev (point (win *application-frame*))))
213 (insert-object (point (win *application-frame*)) *current-gesture*))
214
215 (define-named-command com-backward-object ()
216 (decf (offset (point (win *application-frame*)))))
217
218 (define-named-command com-forward-object ()
219 (incf (offset (point (win *application-frame*)))))
220
221 (define-named-command com-beginning-of-line ()
222 (beginning-of-line (point (win *application-frame*))))
223
224 (define-named-command com-end-of-line ()
225 (end-of-line (point (win *application-frame*))))
226
227 (define-named-command com-delete-object ()
228 (delete-range (point (win *application-frame*))))
229
230 (define-named-command com-backward-delete-object ()
231 (delete-range (point (win *application-frame*)) -1))
232
233 (define-named-command com-previous-line ()
234 (previous-line (point (win *application-frame*))))
235
236 (define-named-command com-next-line ()
237 (next-line (point (win *application-frame*))))
238
239 (define-named-command com-open-line ()
240 (open-line (point (win *application-frame*))))
241
242 (define-named-command com-kill-line ()
243 (kill-line (point (win *application-frame*))))
244
245 (define-named-command com-forward-word ()
246 (forward-word (point (win *application-frame*))))
247
248 (define-named-command com-backward-word ()
249 (backward-word (point (win *application-frame*))))
250
251 (define-named-command com-delete-word ()
252 (delete-word (point (win *application-frame*))))
253
254 (define-named-command com-backward-delete-word ()
255 (backward-delete-word (point (win *application-frame*))))
256
257 (define-named-command com-toggle-layout ()
258 (setf (frame-current-layout *application-frame*)
259 (if (eq (frame-current-layout *application-frame*) 'default)
260 'with-interactor
261 'default)))
262
263 (define-command com-extended-command ()
264 (let ((item (accept 'command :prompt "Extended Command")))
265 (execute-frame-command *application-frame* item)))
266
267 (eval-when (:compile-toplevel :load-toplevel)
268 (define-presentation-type completable-pathname ()
269 :inherit-from 'pathname))
270
271 (defun filename-completer (so-far mode)
272 (flet ((remove-trail (s)
273 (subseq s 0 (let ((pos (position #\/ s :from-end t)))
274 (if pos (1+ pos) 0)))))
275 (let* ((directory-prefix
276 (if (and (plusp (length so-far)) (eql (aref so-far 0) #\/))
277 ""
278 (namestring #+sbcl (car (directory ".")) #+cmu (ext:default-directory))))
279 (full-so-far (concatenate 'string directory-prefix so-far))
280 (pathnames
281 (loop with length = (length full-so-far)
282 for path in (directory (concatenate 'string
283 (remove-trail so-far)
284 "*.*"))
285 when (let ((mismatch (mismatch (namestring path) full-so-far)))
286 (or (null mismatch) (= mismatch length)))
287 collect path))
288 (strings (mapcar #'namestring pathnames))
289 (first-string (car strings))
290 (length-common-prefix nil)
291 (completed-string nil)
292 (full-completed-string nil))
293 (unless (null pathnames)
294 (setf length-common-prefix
295 (loop with length = (length first-string)
296 for string in (cdr strings)
297 do (setf length (min length (or (mismatch string first-string) length)))
298 finally (return length))))
299 (unless (null pathnames)
300 (setf completed-string
301 (subseq first-string (length directory-prefix)
302 (if (null (cdr pathnames)) nil length-common-prefix)))
303 (setf full-completed-string
304 (concatenate 'string directory-prefix completed-string)))
305 (case mode
306 ((:complete-limited :complete-maximal)
307 (cond ((null pathnames)
308 (values so-far nil nil 0 nil))
309 ((null (cdr pathnames))
310 (values completed-string t (car pathnames) 1 nil))
311 (t
312 (values completed-string nil nil (length pathnames) nil))))
313 (:complete
314 (cond ((null pathnames)
315 (values so-far t so-far 1 nil))
316 ((null (cdr pathnames))
317 (values completed-string t (car pathnames) 1 nil))
318 ((find full-completed-string strings :test #'string-equal)
319 (let ((pos (position full-completed-string strings :test #'string-equal)))
320 (values completed-string
321 t (elt pathnames pos) (length pathnames) nil)))
322 (t
323 (values completed-string nil nil (length pathnames) nil))))
324 (:possibilities
325 (values nil nil nil (length pathnames)
326 (loop with length = (length directory-prefix)
327 for name in pathnames
328 collect (list (subseq (namestring name) length nil)
329 name))))))))
330
331 (define-presentation-method accept
332 ((type completable-pathname) stream (view textual-view) &key)
333 (multiple-value-bind (pathname success string)
334 (complete-input stream
335 #'filename-completer
336 :partial-completers '(#\Space)
337 :allow-any-input t)
338 (declare (ignore success))
339 (or pathname string)))
340
341 (defun pathname-filename (pathname)
342 (if (null (pathname-type pathname))
343 (pathname-name pathname)
344 (concatenate 'string (pathname-name pathname)
345 "." (pathname-type pathname))))
346
347 (define-named-command com-find-file ()
348 (let ((filename (accept 'completable-pathname
349 :prompt "Find File")))
350 (with-slots (buffer point syntax) (win *application-frame*)
351 (setf buffer (make-instance 'climacs-buffer)
352 point (make-instance 'standard-right-sticky-mark :buffer buffer)
353 syntax (make-instance 'texinfo-syntax :pane (win *application-frame*)))
354 (with-open-file (stream filename :direction :input :if-does-not-exist :create)
355 (input-from-stream stream buffer 0))
356 (setf (filename buffer) filename
357 (name buffer) (pathname-filename filename)
358 (needs-saving buffer) nil)
359 (beginning-of-buffer point)
360 ;; this one is needed so that the buffer modification protocol
361 ;; resets the low and high marks after redisplay
362 (redisplay-frame-panes *application-frame*))))
363
364 (define-named-command com-save-buffer ()
365 (let* ((buffer (buffer (win *application-frame*)))
366 (filename (or (filename buffer)
367 (accept 'completable-pathname
368 :prompt "Save Buffer to File"))))
369 (if (or (null (filename buffer))
370 (needs-saving buffer))
371 (progn (with-open-file (stream filename :direction :output :if-exists :supersede)
372 (output-to-stream stream buffer 0 (size buffer)))
373 (setf (filename buffer) filename
374 (name buffer) (pathname-filename filename))
375 (display-message "Wrote: ~a" (filename buffer)))
376 (display-message "No changes need to be saved from ~a" (name buffer)))
377 (setf (needs-saving buffer) nil)))
378
379 (define-named-command com-write-buffer ()
380 (let ((filename (accept 'completable-pathname
381 :prompt "Write Buffer to File"))
382 (buffer (buffer (win *application-frame*))))
383 (with-open-file (stream filename :direction :output :if-exists :supersede)
384 (output-to-stream stream buffer 0 (size buffer)))
385 (setf (filename buffer) filename
386 (name buffer) (pathname-filename filename)
387 (needs-saving buffer) nil)
388 (display-message "Wrote: ~a" (filename buffer))))
389
390 (define-named-command com-beginning-of-buffer ()
391 (beginning-of-buffer (point (win *application-frame*))))
392
393 (define-named-command com-page-down ()
394 (let ((pane (win *application-frame*)))
395 (page-down pane (syntax pane))))
396
397 (define-named-command com-page-up ()
398 (let ((pane (win *application-frame*)))
399 (page-up pane (syntax pane))))
400
401 (define-named-command com-end-of-buffer ()
402 (end-of-buffer (point (win *application-frame*))))
403
404 (define-named-command com-back-to-indentation ()
405 (let ((point (point (win *application-frame*))))
406 (beginning-of-line point)
407 (loop until (end-of-line-p point)
408 while (whitespacep (object-after point))
409 do (incf (offset point)))))
410
411 (define-named-command com-goto-position ()
412 (setf (offset (point (win *application-frame*)))
413 (accept 'integer :prompt "Goto Position")))
414
415 (define-named-command com-goto-line ()
416 (loop with mark = (make-instance 'standard-right-sticky-mark
417 :buffer (buffer (win *application-frame*)))
418 do (end-of-line mark)
419 until (end-of-buffer-p mark)
420 repeat (accept 'integer :prompt "Goto Line")
421 do (incf (offset mark))
422 (end-of-line mark)
423 finally (beginning-of-line mark)
424 (setf (offset (point (win *application-frame*)))
425 (offset mark))))
426
427 (define-named-command com-browse-url ()
428 (accept 'url :prompt "Browse URL"))
429
430 (define-named-command com-set-mark ()
431 (with-slots (point mark) (win *application-frame*)
432 (setf mark (clone-mark point))))
433
434 (define-named-command com-set-syntax ()
435 (setf (syntax (win *application-frame*))
436 (make-instance (accept 'syntax :prompt "Set Syntax")
437 :pane (win *application-frame*))))
438
439 ;;;;;;;;;;;;;;;;;;;;
440 ;; Kill ring commands
441
442 ;; Copies an element from a kill-ring to a buffer at the given offset
443 (define-named-command com-copy-in ()
444 (insert-sequence (point (win *application-frame*)) (kr-copy *kill-ring*)))
445
446 ;; Cuts an element from a kill-ring out to a buffer at a given offset
447 (define-named-command com-cut-in ()
448 (insert-sequence (point (win *application-frame*)) (kr-pop *kill-ring*)))
449
450 ;; Destructively cut a given buffer region into the kill-ring
451 (define-named-command com-cut-out ()
452 (with-slots (buffer point mark)(win *application-frame*)
453 (if (< (offset point) (offset mark))
454 ((lambda (b o1 o2)
455 (kr-push *kill-ring* (buffer-sequence b o1 o2))
456 (delete-buffer-range b o1 (- o2 o1)))
457 buffer (offset point) (offset mark))
458 ((lambda (b o1 o2)
459 (kr-push *kill-ring* (buffer-sequence b o2 o1))
460 (delete-buffer-range b o1 (- o2 o1)))
461 buffer (offset mark) (offset point)))))
462
463
464 ;; Non destructively copies in buffer region to the kill ring
465 (define-named-command com-copy-out ()
466 (with-slots (buffer point mark)(win *application-frame*)
467 (let ((off1 (offset point))
468 (off2 (offset mark)))
469 (if (< off1 off2)
470 (kr-push *kill-ring* (buffer-sequence buffer off1 off2))
471 (kr-push *kill-ring* (buffer-sequence buffer off2 off1))))))
472
473 ;; Needs adjustment to be like emacs M-y
474 (define-named-command com-kr-rotate ()
475 (kr-rotate *kill-ring* -1))
476
477 ;; Not bound to a key yet
478 (define-named-command com-kr-resize ()
479 (let ((size (accept 'fixnum :prompt "New kill ring size: ")))
480 (kr-resize *kill-ring* size)))
481
482 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
483 ;;;
484 ;;; Global command table
485
486 (make-command-table 'global-climacs-table :errorp nil)
487
488 (defun global-set-key (gesture command)
489 (add-command-to-command-table command 'global-climacs-table
490 :keystroke gesture :errorp nil))
491
492 (loop for code from (char-code #\space) to (char-code #\~)
493 do (global-set-key (code-char code) 'com-self-insert))
494
495 (global-set-key #\newline 'com-self-insert)
496 (global-set-key #\tab 'com-self-insert)
497 (global-set-key '(#\f :control) 'com-forward-object)
498 (global-set-key '(#\b :control) 'com-backward-object)
499 (global-set-key '(#\a :control) 'com-beginning-of-line)
500 (global-set-key '(#\e :control) 'com-end-of-line)
501 (global-set-key '(#\d :control) 'com-delete-object)
502 (global-set-key '(#\p :control) 'com-previous-line)
503 (global-set-key '(#\n :control) 'com-next-line)
504 (global-set-key '(#\o :control) 'com-open-line)
505 (global-set-key '(#\k :control) 'com-kill-line)
506 (global-set-key '(#\Space :control) 'com-set-mark)
507 (global-set-key '(#\y :control) 'com-copy-in)
508 (global-set-key '(#\w :control) 'com-cut-out)
509 (global-set-key '(#\f :meta) 'com-forward-word)
510 (global-set-key '(#\b :meta) 'com-backward-word)
511 (global-set-key '(#\x :meta) 'com-extended-command)
512 (global-set-key '(#\y :meta) 'com-kr-rotate) ;currently rotates only
513 (global-set-key '(#\w :meta) 'com-copy-out)
514 (global-set-key '(#\v :control) 'com-page-down)
515 (global-set-key '(#\v :meta) 'com-page-up)
516 (global-set-key '(#\< :shift :meta) 'com-beginning-of-buffer)
517 (global-set-key '(#\> :shift :meta) 'com-end-of-buffer)
518 (global-set-key '(#\u :meta) 'com-browse-url)
519 (global-set-key '(#\m :meta) 'com-back-to-indentation)
520 (global-set-key '(#\d :meta) 'com-delete-word)
521 (global-set-key '(#\Backspace :meta) 'com-backward-delete-word)
522
523 (global-set-key '(:up) 'com-previous-line)
524 (global-set-key '(:down) 'com-next-line)
525 (global-set-key '(:left) 'com-backward-object)
526 (global-set-key '(:right) 'com-forward-object)
527 (global-set-key '(:left :control) 'com-backward-word)
528 (global-set-key '(:right :control) 'com-forward-word)
529 (global-set-key '(:home) 'com-beginning-of-line)
530 (global-set-key '(:end) 'com-end-of-line)
531 (global-set-key '(:home :control) 'com-beginning-of-buffer)
532 (global-set-key '(:end :control) 'com-end-of-buffer)
533 (global-set-key #\Rubout 'com-delete-object)
534 (global-set-key #\Backspace 'com-backward-delete-object)
535
536 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
537 ;;;
538 ;;; C-x command table
539
540 (make-command-table 'c-x-climacs-table :errorp nil)
541
542 (add-menu-item-to-command-table 'global-climacs-table "C-x"
543 :menu 'c-x-climacs-table
544 :keystroke '(#\x :control))
545
546 (defun c-x-set-key (gesture command)
547 (add-command-to-command-table command 'c-x-climacs-table
548 :keystroke gesture :errorp nil))
549
550 (c-x-set-key '(#\c :control) 'com-quit)
551 (c-x-set-key '(#\f :control) 'com-find-file)
552 (c-x-set-key '(#\s :control) 'com-save-buffer)
553 (c-x-set-key '(#\w :control) 'com-write-buffer)

  ViewVC Help
Powered by ViewVC 1.1.5