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

Contents of /climacs/gui.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.42 - (show annotations)
Sat Jan 1 13:25:19 2005 UTC (9 years, 3 months ago) by rstrandh
Branch: MAIN
Changes since 1.41: +12 -0 lines
implemented transpose-objects (C-t) according to a suggestion by
Christophe Rhodes.  Applied some code factoring to his initial
suggestion.
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-transpose-objects ()
234 (let* ((point (point (win *application-frame*))))
235 (unless (beginning-of-buffer-p point)
236 (when (end-of-line-p point)
237 (decf (offset point)))
238 (let ((object (object-after point)))
239 (delete-range point)
240 (decf (offset point))
241 (insert-object point object)
242 (incf (offset point))))))
243
244 (define-named-command com-previous-line ()
245 (previous-line (point (win *application-frame*))))
246
247 (define-named-command com-next-line ()
248 (next-line (point (win *application-frame*))))
249
250 (define-named-command com-open-line ()
251 (open-line (point (win *application-frame*))))
252
253 (define-named-command com-kill-line ()
254 (kill-line (point (win *application-frame*))))
255
256 (define-named-command com-forward-word ()
257 (forward-word (point (win *application-frame*))))
258
259 (define-named-command com-backward-word ()
260 (backward-word (point (win *application-frame*))))
261
262 (define-named-command com-delete-word ()
263 (delete-word (point (win *application-frame*))))
264
265 (define-named-command com-backward-delete-word ()
266 (backward-delete-word (point (win *application-frame*))))
267
268 (define-named-command com-toggle-layout ()
269 (setf (frame-current-layout *application-frame*)
270 (if (eq (frame-current-layout *application-frame*) 'default)
271 'with-interactor
272 'default)))
273
274 (define-command com-extended-command ()
275 (let ((item (accept 'command :prompt "Extended Command")))
276 (execute-frame-command *application-frame* item)))
277
278 (eval-when (:compile-toplevel :load-toplevel)
279 (define-presentation-type completable-pathname ()
280 :inherit-from 'pathname))
281
282 (defun filename-completer (so-far mode)
283 (flet ((remove-trail (s)
284 (subseq s 0 (let ((pos (position #\/ s :from-end t)))
285 (if pos (1+ pos) 0)))))
286 (let* ((directory-prefix
287 (if (and (plusp (length so-far)) (eql (aref so-far 0) #\/))
288 ""
289 (namestring #+sbcl (car (directory ".")) #+cmu (ext:default-directory))))
290 (full-so-far (concatenate 'string directory-prefix so-far))
291 (pathnames
292 (loop with length = (length full-so-far)
293 for path in (directory (concatenate 'string
294 (remove-trail so-far)
295 "*.*"))
296 when (let ((mismatch (mismatch (namestring path) full-so-far)))
297 (or (null mismatch) (= mismatch length)))
298 collect path))
299 (strings (mapcar #'namestring pathnames))
300 (first-string (car strings))
301 (length-common-prefix nil)
302 (completed-string nil)
303 (full-completed-string nil))
304 (unless (null pathnames)
305 (setf length-common-prefix
306 (loop with length = (length first-string)
307 for string in (cdr strings)
308 do (setf length (min length (or (mismatch string first-string) length)))
309 finally (return length))))
310 (unless (null pathnames)
311 (setf completed-string
312 (subseq first-string (length directory-prefix)
313 (if (null (cdr pathnames)) nil length-common-prefix)))
314 (setf full-completed-string
315 (concatenate 'string directory-prefix completed-string)))
316 (case mode
317 ((:complete-limited :complete-maximal)
318 (cond ((null pathnames)
319 (values so-far nil nil 0 nil))
320 ((null (cdr pathnames))
321 (values completed-string t (car pathnames) 1 nil))
322 (t
323 (values completed-string nil nil (length pathnames) nil))))
324 (:complete
325 (cond ((null pathnames)
326 (values so-far t so-far 1 nil))
327 ((null (cdr pathnames))
328 (values completed-string t (car pathnames) 1 nil))
329 ((find full-completed-string strings :test #'string-equal)
330 (let ((pos (position full-completed-string strings :test #'string-equal)))
331 (values completed-string
332 t (elt pathnames pos) (length pathnames) nil)))
333 (t
334 (values completed-string nil nil (length pathnames) nil))))
335 (:possibilities
336 (values nil nil nil (length pathnames)
337 (loop with length = (length directory-prefix)
338 for name in pathnames
339 collect (list (subseq (namestring name) length nil)
340 name))))))))
341
342 (define-presentation-method accept
343 ((type completable-pathname) stream (view textual-view) &key)
344 (multiple-value-bind (pathname success string)
345 (complete-input stream
346 #'filename-completer
347 :partial-completers '(#\Space)
348 :allow-any-input t)
349 (declare (ignore success))
350 (or pathname string)))
351
352 (defun pathname-filename (pathname)
353 (if (null (pathname-type pathname))
354 (pathname-name pathname)
355 (concatenate 'string (pathname-name pathname)
356 "." (pathname-type pathname))))
357
358 (define-named-command com-find-file ()
359 (let ((filename (accept 'completable-pathname
360 :prompt "Find File")))
361 (with-slots (buffer point syntax) (win *application-frame*)
362 (setf buffer (make-instance 'climacs-buffer)
363 point (make-instance 'standard-right-sticky-mark :buffer buffer)
364 syntax (make-instance 'texinfo-syntax :pane (win *application-frame*)))
365 (with-open-file (stream filename :direction :input :if-does-not-exist :create)
366 (input-from-stream stream buffer 0))
367 (setf (filename buffer) filename
368 (name buffer) (pathname-filename filename)
369 (needs-saving buffer) nil)
370 (beginning-of-buffer point)
371 ;; this one is needed so that the buffer modification protocol
372 ;; resets the low and high marks after redisplay
373 (redisplay-frame-panes *application-frame*))))
374
375 (define-named-command com-save-buffer ()
376 (let* ((buffer (buffer (win *application-frame*)))
377 (filename (or (filename buffer)
378 (accept 'completable-pathname
379 :prompt "Save Buffer to File"))))
380 (if (or (null (filename buffer))
381 (needs-saving buffer))
382 (progn (with-open-file (stream filename :direction :output :if-exists :supersede)
383 (output-to-stream stream buffer 0 (size buffer)))
384 (setf (filename buffer) filename
385 (name buffer) (pathname-filename filename))
386 (display-message "Wrote: ~a" (filename buffer)))
387 (display-message "No changes need to be saved from ~a" (name buffer)))
388 (setf (needs-saving buffer) nil)))
389
390 (define-named-command com-write-buffer ()
391 (let ((filename (accept 'completable-pathname
392 :prompt "Write Buffer to File"))
393 (buffer (buffer (win *application-frame*))))
394 (with-open-file (stream filename :direction :output :if-exists :supersede)
395 (output-to-stream stream buffer 0 (size buffer)))
396 (setf (filename buffer) filename
397 (name buffer) (pathname-filename filename)
398 (needs-saving buffer) nil)
399 (display-message "Wrote: ~a" (filename buffer))))
400
401 (define-named-command com-beginning-of-buffer ()
402 (beginning-of-buffer (point (win *application-frame*))))
403
404 (define-named-command com-page-down ()
405 (let ((pane (win *application-frame*)))
406 (page-down pane (syntax pane))))
407
408 (define-named-command com-page-up ()
409 (let ((pane (win *application-frame*)))
410 (page-up pane (syntax pane))))
411
412 (define-named-command com-end-of-buffer ()
413 (end-of-buffer (point (win *application-frame*))))
414
415 (define-named-command com-back-to-indentation ()
416 (let ((point (point (win *application-frame*))))
417 (beginning-of-line point)
418 (loop until (end-of-line-p point)
419 while (whitespacep (object-after point))
420 do (incf (offset point)))))
421
422 (define-named-command com-goto-position ()
423 (setf (offset (point (win *application-frame*)))
424 (accept 'integer :prompt "Goto Position")))
425
426 (define-named-command com-goto-line ()
427 (loop with mark = (make-instance 'standard-right-sticky-mark
428 :buffer (buffer (win *application-frame*)))
429 do (end-of-line mark)
430 until (end-of-buffer-p mark)
431 repeat (accept 'integer :prompt "Goto Line")
432 do (incf (offset mark))
433 (end-of-line mark)
434 finally (beginning-of-line mark)
435 (setf (offset (point (win *application-frame*)))
436 (offset mark))))
437
438 (define-named-command com-browse-url ()
439 (accept 'url :prompt "Browse URL"))
440
441 (define-named-command com-set-mark ()
442 (with-slots (point mark) (win *application-frame*)
443 (setf mark (clone-mark point))))
444
445 (define-named-command com-set-syntax ()
446 (setf (syntax (win *application-frame*))
447 (make-instance (accept 'syntax :prompt "Set Syntax")
448 :pane (win *application-frame*))))
449
450 ;;;;;;;;;;;;;;;;;;;;
451 ;; Kill ring commands
452
453 ;; Copies an element from a kill-ring to a buffer at the given offset
454 (define-named-command com-copy-in ()
455 (insert-sequence (point (win *application-frame*)) (kr-copy *kill-ring*)))
456
457 ;; Cuts an element from a kill-ring out to a buffer at a given offset
458 (define-named-command com-cut-in ()
459 (insert-sequence (point (win *application-frame*)) (kr-pop *kill-ring*)))
460
461 ;; Destructively cut a given buffer region into the kill-ring
462 (define-named-command com-cut-out ()
463 (with-slots (buffer point mark)(win *application-frame*)
464 (if (< (offset point) (offset mark))
465 ((lambda (b o1 o2)
466 (kr-push *kill-ring* (buffer-sequence b o1 o2))
467 (delete-buffer-range b o1 (- o2 o1)))
468 buffer (offset point) (offset mark))
469 ((lambda (b o1 o2)
470 (kr-push *kill-ring* (buffer-sequence b o2 o1))
471 (delete-buffer-range b o1 (- o2 o1)))
472 buffer (offset mark) (offset point)))))
473
474
475 ;; Non destructively copies in buffer region to the kill ring
476 (define-named-command com-copy-out ()
477 (with-slots (buffer point mark)(win *application-frame*)
478 (let ((off1 (offset point))
479 (off2 (offset mark)))
480 (if (< off1 off2)
481 (kr-push *kill-ring* (buffer-sequence buffer off1 off2))
482 (kr-push *kill-ring* (buffer-sequence buffer off2 off1))))))
483
484 ;; Needs adjustment to be like emacs M-y
485 (define-named-command com-kr-rotate ()
486 (kr-rotate *kill-ring* -1))
487
488 ;; Not bound to a key yet
489 (define-named-command com-kr-resize ()
490 (let ((size (accept 'fixnum :prompt "New kill ring size: ")))
491 (kr-resize *kill-ring* size)))
492
493 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
494 ;;;
495 ;;; Global command table
496
497 (make-command-table 'global-climacs-table :errorp nil)
498
499 (defun global-set-key (gesture command)
500 (add-command-to-command-table command 'global-climacs-table
501 :keystroke gesture :errorp nil))
502
503 (loop for code from (char-code #\space) to (char-code #\~)
504 do (global-set-key (code-char code) 'com-self-insert))
505
506 (global-set-key #\newline 'com-self-insert)
507 (global-set-key #\tab 'com-self-insert)
508 (global-set-key '(#\f :control) 'com-forward-object)
509 (global-set-key '(#\b :control) 'com-backward-object)
510 (global-set-key '(#\a :control) 'com-beginning-of-line)
511 (global-set-key '(#\e :control) 'com-end-of-line)
512 (global-set-key '(#\d :control) 'com-delete-object)
513 (global-set-key '(#\p :control) 'com-previous-line)
514 (global-set-key '(#\n :control) 'com-next-line)
515 (global-set-key '(#\o :control) 'com-open-line)
516 (global-set-key '(#\k :control) 'com-kill-line)
517 (global-set-key '(#\t :control) 'com-transpose-objects)
518 (global-set-key '(#\Space :control) 'com-set-mark)
519 (global-set-key '(#\y :control) 'com-copy-in)
520 (global-set-key '(#\w :control) 'com-cut-out)
521 (global-set-key '(#\f :meta) 'com-forward-word)
522 (global-set-key '(#\b :meta) 'com-backward-word)
523 (global-set-key '(#\x :meta) 'com-extended-command)
524 (global-set-key '(#\y :meta) 'com-kr-rotate) ;currently rotates only
525 (global-set-key '(#\w :meta) 'com-copy-out)
526 (global-set-key '(#\v :control) 'com-page-down)
527 (global-set-key '(#\v :meta) 'com-page-up)
528 (global-set-key '(#\< :shift :meta) 'com-beginning-of-buffer)
529 (global-set-key '(#\> :shift :meta) 'com-end-of-buffer)
530 (global-set-key '(#\u :meta) 'com-browse-url)
531 (global-set-key '(#\m :meta) 'com-back-to-indentation)
532 (global-set-key '(#\d :meta) 'com-delete-word)
533 (global-set-key '(#\Backspace :meta) 'com-backward-delete-word)
534
535 (global-set-key '(:up) 'com-previous-line)
536 (global-set-key '(:down) 'com-next-line)
537 (global-set-key '(:left) 'com-backward-object)
538 (global-set-key '(:right) 'com-forward-object)
539 (global-set-key '(:left :control) 'com-backward-word)
540 (global-set-key '(:right :control) 'com-forward-word)
541 (global-set-key '(:home) 'com-beginning-of-line)
542 (global-set-key '(:end) 'com-end-of-line)
543 (global-set-key '(:home :control) 'com-beginning-of-buffer)
544 (global-set-key '(:end :control) 'com-end-of-buffer)
545 (global-set-key #\Rubout 'com-delete-object)
546 (global-set-key #\Backspace 'com-backward-delete-object)
547
548 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
549 ;;;
550 ;;; C-x command table
551
552 (make-command-table 'c-x-climacs-table :errorp nil)
553
554 (add-menu-item-to-command-table 'global-climacs-table "C-x"
555 :menu 'c-x-climacs-table
556 :keystroke '(#\x :control))
557
558 (defun c-x-set-key (gesture command)
559 (add-command-to-command-table command 'c-x-climacs-table
560 :keystroke gesture :errorp nil))
561
562 (c-x-set-key '(#\c :control) 'com-quit)
563 (c-x-set-key '(#\f :control) 'com-find-file)
564 (c-x-set-key '(#\s :control) 'com-save-buffer)
565 (c-x-set-key '(#\w :control) 'com-write-buffer)

  ViewVC Help
Powered by ViewVC 1.1.5