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

Contents of /climacs/gui.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.30 - (show annotations)
Wed Dec 29 07:26:02 2004 UTC (9 years, 3 months ago) by rstrandh
Branch: MAIN
Changes since 1.29: +18 -10 lines
Implemented a suggestion from Lawrence Mitchell to avoid saving
a buffer that has not need to be saved.
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)
31 ((name :initform "*scratch*" :accessor name)
32 (needs-saving :initform nil :accessor needs-saving)))
33
34 (defclass climacs-pane (application-pane)
35 ((buffer :initform (make-instance 'climacs-buffer) :accessor buffer)
36 (point :initform nil :initarg :point :reader point)
37 (syntax :initarg :syntax :accessor syntax)
38 (mark :initform nil :initarg :mark :reader mark)))
39
40 (defmethod initialize-instance :after ((pane climacs-pane) &rest args)
41 (declare (ignore args))
42 (with-slots (buffer point syntax mark) pane
43 (when (null point)
44 (setf point (make-instance 'standard-right-sticky-mark
45 :buffer buffer)))
46 (when (null mark)
47 (setf mark (make-instance 'standard-right-sticky-mark
48 :buffer buffer)))
49 (setf syntax (make-instance 'texinfo-syntax :pane pane))))
50
51 (defclass minibuffer-pane (application-pane) ())
52
53 (defmethod stream-accept :before ((pane minibuffer-pane) type &rest args)
54 (declare (ignore type args))
55 (window-clear pane))
56
57 (define-application-frame climacs ()
58 ((win :reader win))
59 (:panes
60 (win (make-pane 'climacs-pane
61 :width 900 :height 400
62 :name 'win
63 :incremental-redisplay t
64 :display-function 'display-win))
65 (info :application
66 :width 900 :height 20 :max-height 20
67 :name 'info :background +light-gray+
68 :scroll-bars nil
69 :incremental-redisplay t
70 :display-function 'display-info)
71 (int (make-pane 'minibuffer-pane
72 :width 900 :height 20 :max-height 20 :min-height 20
73 :scroll-bars nil)))
74 (:layouts
75 (default
76 (vertically (:scroll-bars nil)
77 (scrolling (:width 900 :height 400) win)
78 info
79 int)))
80 (:top-level (climacs-top-level)))
81
82 (defmethod redisplay-frame-panes :after ((frame climacs) &rest args)
83 (declare (ignore args))
84 (clear-modify (buffer (win frame))))
85
86 (defun climacs ()
87 "Starts up a climacs session"
88 (let ((frame (make-application-frame 'climacs)))
89 (run-frame-top-level frame)))
90
91 (defun display-message (format-string &rest format-args)
92 (apply #'format *standard-input* format-string format-args))
93
94 (defun display-info (frame pane)
95 (let* ((win (win frame))
96 (buf (buffer win))
97 (name-info (format nil " ~a ~a"
98 (if (needs-saving buf) "**" "--")
99 (name buf))))
100 (princ name-info pane)))
101
102 (defun display-win (frame pane)
103 "The display function used by the climacs application frame."
104 (declare (ignore frame))
105 (redisplay-pane pane))
106
107 (defun find-gestures (gestures start-table)
108 (loop with table = (find-command-table start-table)
109 for (gesture . rest) on gestures
110 for item = (find-keystroke-item gesture table :errorp nil)
111 while item
112 do (if (eq (command-menu-item-type item) :command)
113 (return (if (null rest) item nil))
114 (setf table (command-menu-item-value item)))
115 finally (return item)))
116
117 (defvar *kill-ring* (initialize-kill-ring 7))
118 (defparameter *current-gesture* nil)
119
120 (defun climacs-top-level (frame &key
121 command-parser command-unparser
122 partial-command-parser prompt)
123 (declare (ignore command-parser command-unparser partial-command-parser prompt))
124 (setf (slot-value frame 'win) (find-pane-named frame 'win))
125 (let ((*standard-output* (find-pane-named frame 'win))
126 (*standard-input* (find-pane-named frame 'int))
127 (*print-pretty* nil)
128 (*abort-gestures* nil))
129 (redisplay-frame-panes frame :force-p t)
130 (loop with gestures = '()
131 do (setf *current-gesture* (read-gesture :stream *standard-input*))
132 (when (or (characterp *current-gesture*)
133 (and (typep *current-gesture* 'keyboard-event)
134 (or (keyboard-event-character *current-gesture*)
135 (not (member (keyboard-event-key-name
136 *current-gesture*)
137 '(:control-left :control-right
138 :shift-left :shift-right
139 :meta-left :meta-right
140 :super-left :super-right
141 :hyper-left :hyper-right
142 :shift-lock :caps-lock))))))
143 (setf gestures (nconc gestures (list *current-gesture*)))
144 (let ((item (find-gestures gestures 'global-climacs-table)))
145 (cond ((not item)
146 (beep) (setf gestures '()))
147 ((eq (command-menu-item-type item) :command)
148 (handler-case
149 (funcall (command-menu-item-value item))
150 (error (condition)
151 (beep)
152 (format *error-output* "~a~%" condition)))
153 (setf gestures '()))
154 (t nil))))
155 (let ((buffer (buffer (win frame))))
156 (when (modified-p buffer)
157 (setf (needs-saving buffer) t)))
158 (redisplay-frame-panes frame))))
159
160 (define-command (com-quit :name "Quit" :command-table climacs) ()
161 (frame-exit *application-frame*))
162
163 (define-command com-self-insert ()
164 (unless (constituentp *current-gesture*)
165 (possibly-expand-abbrev (point (win *application-frame*))))
166 (insert-object (point (win *application-frame*)) *current-gesture*))
167
168 (define-command com-backward-object ()
169 (decf (offset (point (win *application-frame*)))))
170
171 (define-command com-forward-object ()
172 (incf (offset (point (win *application-frame*)))))
173
174 (define-command com-beginning-of-line ()
175 (beginning-of-line (point (win *application-frame*))))
176
177 (define-command com-end-of-line ()
178 (end-of-line (point (win *application-frame*))))
179
180 (define-command com-delete-object ()
181 (delete-range (point (win *application-frame*))))
182
183 (define-command com-backward-delete-object ()
184 (delete-range (point (win *application-frame*)) -1))
185
186 (define-command com-previous-line ()
187 (previous-line (point (win *application-frame*))))
188
189 (define-command com-next-line ()
190 (next-line (point (win *application-frame*))))
191
192 (define-command com-open-line ()
193 (open-line (point (win *application-frame*))))
194
195 (define-command com-kill-line ()
196 (kill-line (point (win *application-frame*))))
197
198 (define-command com-forward-word ()
199 (forward-word (point (win *application-frame*))))
200
201 (define-command com-backward-word ()
202 (backward-word (point (win *application-frame*))))
203
204 (define-command com-toggle-layout ()
205 (setf (frame-current-layout *application-frame*)
206 (if (eq (frame-current-layout *application-frame*) 'default)
207 'with-interactor
208 'default)))
209
210 (define-command com-extended-command ()
211 (let ((item (accept 'command :prompt "Extended Command")))
212 (execute-frame-command *application-frame* item)))
213
214 (define-presentation-type completable-pathname ()
215 :inherit-from 'pathname)
216
217 (defun filename-completer (so-far mode)
218 (flet ((remove-trail (s)
219 (subseq s 0 (let ((pos (position #\/ s :from-end t)))
220 (if pos (1+ pos) 0)))))
221 (let* ((directory-prefix
222 (if (and (plusp (length so-far)) (eql (aref so-far 0) #\/))
223 ""
224 (namestring #+sbcl (car (directory ".")) #+cmu (ext:default-directory))))
225 (full-so-far (concatenate 'string directory-prefix so-far))
226 (pathnames
227 (loop with length = (length full-so-far)
228 for path in (directory (concatenate 'string
229 (remove-trail so-far)
230 "*.*"))
231 when (let ((mismatch (mismatch (namestring path) full-so-far)))
232 (or (null mismatch) (= mismatch length)))
233 collect path))
234 (strings (mapcar #'namestring pathnames))
235 (first-string (car strings))
236 (length-common-prefix nil)
237 (completed-string nil)
238 (full-completed-string nil))
239 (unless (null pathnames)
240 (setf length-common-prefix
241 (loop with length = (length first-string)
242 for string in (cdr strings)
243 do (setf length (min length (or (mismatch string first-string) length)))
244 finally (return length))))
245 (unless (null pathnames)
246 (setf completed-string
247 (subseq first-string (length directory-prefix)
248 (if (null (cdr pathnames)) nil length-common-prefix)))
249 (setf full-completed-string
250 (concatenate 'string directory-prefix completed-string)))
251 (case mode
252 ((:complete-limited :complete-maximal)
253 (cond ((null pathnames)
254 (values so-far nil nil 0 nil))
255 ((null (cdr pathnames))
256 (values completed-string t (car pathnames) 1 nil))
257 (t
258 (values completed-string nil nil (length pathnames) nil))))
259 (:complete
260 (cond ((null pathnames)
261 (values so-far t so-far 1 nil))
262 ((null (cdr pathnames))
263 (values completed-string t (car pathnames) 1 nil))
264 ((find full-completed-string strings :test #'string-equal)
265 (let ((pos (position full-completed-string strings :test #'string-equal)))
266 (values completed-string
267 t (elt pathnames pos) (length pathnames) nil)))
268 (t
269 (values completed-string nil nil (length pathnames) nil))))
270 (:possibilities
271 (values nil nil nil (length pathnames)
272 (loop with length = (length directory-prefix)
273 for name in pathnames
274 collect (list (subseq (namestring name) length nil)
275 name))))))))
276
277 (define-presentation-method accept
278 ((type completable-pathname) stream (view textual-view) &key)
279 (multiple-value-bind (pathname success string)
280 (complete-input stream
281 #'filename-completer
282 :partial-completers '(#\Space)
283 :allow-any-input t)
284 (declare (ignore success))
285 (or pathname string)))
286
287 (defun pathname-filename (pathname)
288 (if (null (pathname-type pathname))
289 (pathname-name pathname)
290 (concatenate 'string (pathname-name pathname)
291 "." (pathname-type pathname))))
292
293 (define-command (com-find-file :name "Find File" :command-table climacs) ()
294 (let ((filename (accept 'completable-pathname
295 :prompt "Find File")))
296 (with-slots (buffer point syntax) (win *application-frame*)
297 (setf buffer (make-instance 'climacs-buffer)
298 point (make-instance 'standard-right-sticky-mark :buffer buffer)
299 syntax (make-instance 'texinfo-syntax :pane (win *application-frame*)))
300 (with-open-file (stream filename :direction :input :if-does-not-exist :create)
301 (input-from-stream stream buffer 0))
302 (setf (filename buffer) filename
303 (name buffer) (pathname-filename filename)
304 (needs-saving buffer) nil)
305 ;; this one is needed so that the buffer modification protocol
306 ;; resets the low and high marks after redisplay
307 (redisplay-frame-panes *application-frame*)
308 (beginning-of-buffer point))))
309
310 (define-command com-save-buffer ()
311 (let* ((buffer (buffer (win *application-frame*)))
312 (filename (or (filename buffer)
313 (accept 'completable-pathname
314 :prompt "Save Buffer to File"))))
315 (if (or (null (filename buffer))
316 (needs-saving buffer))
317 (progn (with-open-file (stream filename :direction :output :if-exists :supersede)
318 (output-to-stream stream buffer 0 (size buffer)))
319 (setf (filename buffer) filename
320 (name buffer) (pathname-filename filename))
321 (display-message "Wrote: ~a" (filename buffer)))
322 (display-message "No changes need to be saved from ~a" (name buffer)))
323 (setf (needs-saving buffer) nil)))
324
325 (define-command com-write-buffer ()
326 (let ((filename (accept 'completable-pathname
327 :prompt "Write Buffer to File"))
328 (buffer (buffer (win *application-frame*))))
329 (with-open-file (stream filename :direction :output :if-exists :supersede)
330 (output-to-stream stream buffer 0 (size buffer)))
331 (setf (filename buffer) filename
332 (name buffer) (pathname-filename filename)
333 (needs-saving buffer) nil)
334 (display-message "Wrote: ~a" (filename buffer))))
335
336 (define-command com-beginning-of-buffer ()
337 (beginning-of-buffer (point (win *application-frame*))))
338
339 (define-command com-end-of-buffer ()
340 (end-of-buffer (point (win *application-frame*))))
341
342 (define-command com-browse-url ()
343 (accept 'url :prompt "Browse URL"))
344
345 (define-command com-set-mark ()
346 (with-slots (point mark) (win *application-frame*)
347 (setf mark (clone-mark point))))
348
349 ;;;;;;;;;;;;;;;;;;;;
350 ;; Kill ring commands
351
352 ;; The naming may sound odd here, but think of electronic wireing:
353 ;; outputs to inputs and inputs to outputs. Copying into a buffer
354 ;; first requires coping out of the kill ring.
355
356 (defgeneric kr-copy-in (buffer kr offset1 offset2)
357 (:documentation "Non destructively copies in buffer region to the kill ring"))
358
359 (defmethod kr-copy-in ((buffer standard-buffer) (kr kill-ring) offset1 offset2)
360 (kr-push kr (buffer-sequence buffer offset1 offset2)))
361
362 (defgeneric kr-cut-in (buffer kr offset1 offset2)
363 (:documentation "Destructively cut a given buffer region into the kill-ring"))
364
365 (defmethod kr-cut-in ((buffer standard-buffer) (kr kill-ring) offset1 offset2)
366 (kr-copy-in buffer kr offset1 offset2)
367 (climacs-buffer::delete-buffer-range buffer offset1 (- offset2 offset1)))
368
369 (defgeneric kr-copy-out (mark kr)
370 (:documentation "Copies an element from a kill-ring to a buffer at the given offset"))
371
372 (defmethod kr-copy-out ((mark standard-right-sticky-mark)(kr kill-ring))
373 (insert-sequence mark (kr-copy kr)))
374
375 (defgeneric kr-cut-out (mark kr)
376 (:documentation "Cuts an element from a kill-ring out to a buffer at a given offset"))
377
378 (defmethod kr-cut-out ((mark standard-right-sticky-mark) (kr kill-ring))
379 (insert-sequence mark (kr-pop kr)))
380
381 (define-command com-copy-in ()
382 (kr-copy-out (point (win *application-frame*)) *kill-ring*))
383
384 (define-command com-cut-in ()
385 (kr-cut-out (point (win *application-frame*)) *kill-ring*))
386
387 (define-command com-cut-out ()
388 (with-slots (buffer point mark)(win *application-frame*)
389 (let ((off1 (offset point))
390 (off2 (offset mark)))
391 (if (< off1 off2)
392 (kr-cut-in buffer *kill-ring* off1 off2)
393 (kr-cut-in buffer *kill-ring* off2 off1)))))
394
395 (define-command com-copy-out ()
396 (with-slots (buffer point mark)(win *application-frame*)
397 (let ((off1 (offset point))
398 (off2 (offset mark)))
399 (if (< off1 off2)
400 (kr-copy-in buffer *kill-ring* off1 off2)
401 (kr-copy-in buffer *kill-ring* off2 off1)))))
402
403 ;; Needs adjustment to be like emacs M-y
404 (define-command com-kr-rotate ()
405 (kr-rotate *kill-ring* -1))
406
407 ;; Not bound to a key yet
408 (define-command com-kr-resize ()
409 (let ((size (accept 'fixnum :prompt "New kill ring size: ")))
410 (kr-resize *kill-ring* size)))
411
412 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
413 ;;;
414 ;;; Global command table
415
416 (make-command-table 'global-climacs-table :errorp nil)
417
418 (defun global-set-key (gesture command)
419 (add-command-to-command-table command 'global-climacs-table
420 :keystroke gesture :errorp nil))
421
422 (loop for code from (char-code #\space) to (char-code #\~)
423 do (global-set-key (code-char code) 'com-self-insert))
424
425 (global-set-key #\newline 'com-self-insert)
426 (global-set-key #\tab 'com-self-insert)
427 (global-set-key '(#\f :control) 'com-forward-object)
428 (global-set-key '(#\b :control) 'com-backward-object)
429 (global-set-key '(#\a :control) 'com-beginning-of-line)
430 (global-set-key '(#\e :control) 'com-end-of-line)
431 (global-set-key '(#\d :control) 'com-delete-object)
432 (global-set-key '(#\p :control) 'com-previous-line)
433 (global-set-key '(#\n :control) 'com-next-line)
434 (global-set-key '(#\o :control) 'com-open-line)
435 (global-set-key '(#\k :control) 'com-kill-line)
436 (global-set-key '(#\Space :control) 'com-set-mark)
437 (global-set-key '(#\y :control) 'com-copy-in)
438 (global-set-key '(#\w :control) 'com-cut-out)
439 (global-set-key '(#\f :meta) 'com-forward-word)
440 (global-set-key '(#\b :meta) 'com-backward-word)
441 (global-set-key '(#\x :meta) 'com-extended-command)
442 (global-set-key '(#\y :meta) 'com-kr-rotate) ;currently rotates only
443 (global-set-key '(#\w :meta) 'com-copy-out)
444 (global-set-key '(#\< :shift :meta) 'com-beginning-of-buffer)
445 (global-set-key '(#\> :shift :meta) 'com-end-of-buffer)
446 (global-set-key '(#\u :meta) 'com-browse-url)
447
448 (global-set-key '(:up) 'com-previous-line)
449 (global-set-key '(:down) 'com-next-line)
450 (global-set-key '(:left) 'com-backward-object)
451 (global-set-key '(:right) 'com-forward-object)
452 (global-set-key '(:left :control) 'com-backward-word)
453 (global-set-key '(:right :control) 'com-forward-word)
454 (global-set-key '(:home) 'com-beginning-of-line)
455 (global-set-key '(:end) 'com-end-of-line)
456 (global-set-key '(:home :control) 'com-beginning-of-buffer)
457 (global-set-key '(:end :control) 'com-end-of-buffer)
458 (global-set-key #\Rubout 'com-delete-object)
459 (global-set-key #\Backspace 'com-backward-delete-object)
460
461 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
462 ;;;
463 ;;; C-x command table
464
465 (make-command-table 'c-x-climacs-table :errorp nil)
466
467 (add-menu-item-to-command-table 'global-climacs-table "C-x"
468 :menu 'c-x-climacs-table
469 :keystroke '(#\x :control))
470
471 (defun c-x-set-key (gesture command)
472 (add-command-to-command-table command 'c-x-climacs-table
473 :keystroke gesture :errorp nil))
474
475 (c-x-set-key '(#\c :control) 'com-quit)
476 (c-x-set-key '(#\f :control) 'com-find-file)
477 (c-x-set-key '(#\s :control) 'com-save-buffer)
478 (c-x-set-key '(#\w :control) 'com-write-buffer)

  ViewVC Help
Powered by ViewVC 1.1.5