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

Contents of /climacs/gui.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.31 - (hide annotations)
Wed Dec 29 08:02:45 2004 UTC (9 years, 3 months ago) by ejohnson
Branch: MAIN
Changes since 1.30: +18 -38 lines
factored out kr generic functions in gui.lisp for define-commands
1 ejohnson 1.27 ;;; -*- 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 rstrandh 1.28 (needs-saving :initform nil :accessor needs-saving)))
33 ejohnson 1.27
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 rstrandh 1.28 (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 ejohnson 1.27 (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 rstrandh 1.28 :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 ejohnson 1.27 (: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 rstrandh 1.28 (defmethod redisplay-frame-panes :after ((frame climacs) &rest args)
83     (declare (ignore args))
84     (clear-modify (buffer (win frame))))
85    
86 ejohnson 1.27 (defun climacs ()
87     "Starts up a climacs session"
88     (let ((frame (make-application-frame 'climacs)))
89     (run-frame-top-level frame)))
90    
91 rstrandh 1.30 (defun display-message (format-string &rest format-args)
92     (apply #'format *standard-input* format-string format-args))
93    
94 ejohnson 1.27 (defun display-info (frame pane)
95     (let* ((win (win frame))
96     (buf (buffer win))
97     (name-info (format nil " ~a ~a"
98 rstrandh 1.28 (if (needs-saving buf) "**" "--")
99 ejohnson 1.27 (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 rstrandh 1.28 (let ((buffer (buffer (win frame))))
156     (when (modified-p buffer)
157     (setf (needs-saving buffer) t)))
158 ejohnson 1.27 (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 rstrandh 1.28 (insert-object (point (win *application-frame*)) *current-gesture*))
167 ejohnson 1.27
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 rstrandh 1.28 (delete-range (point (win *application-frame*))))
182 ejohnson 1.27
183     (define-command com-backward-delete-object ()
184 rstrandh 1.28 (delete-range (point (win *application-frame*)) -1))
185 ejohnson 1.27
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 rstrandh 1.28 (open-line (point (win *application-frame*))))
194 ejohnson 1.27
195     (define-command com-kill-line ()
196 rstrandh 1.28 (kill-line (point (win *application-frame*))))
197 ejohnson 1.27
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 rstrandh 1.28 (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 ejohnson 1.27 (beginning-of-buffer point))))
309    
310     (define-command com-save-buffer ()
311 rstrandh 1.30 (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 ejohnson 1.27
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 rstrandh 1.28 (name buffer) (pathname-filename filename)
333 rstrandh 1.30 (needs-saving buffer) nil)
334     (display-message "Wrote: ~a" (filename buffer))))
335 ejohnson 1.27
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 ejohnson 1.31 ;; Copies an element from a kill-ring to a buffer at the given offset
353 ejohnson 1.27 (define-command com-copy-in ()
354 ejohnson 1.31 (insert-sequence (point (win *application-frame*)) (kr-copy *kill-ring*)))
355 ejohnson 1.27
356 ejohnson 1.31 ;; Cuts an element from a kill-ring out to a buffer at a given offset
357 ejohnson 1.27 (define-command com-cut-in ()
358 ejohnson 1.31 (insert-sequence (point (win *application-frame*)) (kr-pop *kill-ring*)))
359 ejohnson 1.27
360 ejohnson 1.31 ;; Destructively cut a given buffer region into the kill-ring
361 ejohnson 1.27 (define-command com-cut-out ()
362     (with-slots (buffer point mark)(win *application-frame*)
363 ejohnson 1.31 (if (< (offset point) (offset mark))
364     ((lambda (b o1 o2)
365     (kr-push *kill-ring* (buffer-sequence b o1 o2))
366     (delete-buffer-range b o1 (- o2 o1)))
367     buffer (offset point) (offset mark))
368     ((lambda (b o1 o2)
369     (kr-push *kill-ring* (buffer-sequence b o2 o1))
370     (delete-buffer-range b o1 (- o2 o1)))
371     buffer (offset mark) (offset point)))))
372    
373 ejohnson 1.27
374 ejohnson 1.31 ;; Non destructively copies in buffer region to the kill ring
375 ejohnson 1.27 (define-command com-copy-out ()
376     (with-slots (buffer point mark)(win *application-frame*)
377     (let ((off1 (offset point))
378     (off2 (offset mark)))
379     (if (< off1 off2)
380 ejohnson 1.31 (kr-push *kill-ring* (buffer-sequence buffer off1 off2))
381     (kr-push *kill-ring* (buffer-sequence buffer off2 off1))))))
382 ejohnson 1.27
383     ;; Needs adjustment to be like emacs M-y
384     (define-command com-kr-rotate ()
385     (kr-rotate *kill-ring* -1))
386    
387     ;; Not bound to a key yet
388     (define-command com-kr-resize ()
389     (let ((size (accept 'fixnum :prompt "New kill ring size: ")))
390     (kr-resize *kill-ring* size)))
391    
392     ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
393     ;;;
394     ;;; Global command table
395    
396     (make-command-table 'global-climacs-table :errorp nil)
397    
398     (defun global-set-key (gesture command)
399     (add-command-to-command-table command 'global-climacs-table
400     :keystroke gesture :errorp nil))
401    
402     (loop for code from (char-code #\space) to (char-code #\~)
403     do (global-set-key (code-char code) 'com-self-insert))
404    
405     (global-set-key #\newline 'com-self-insert)
406     (global-set-key #\tab 'com-self-insert)
407     (global-set-key '(#\f :control) 'com-forward-object)
408     (global-set-key '(#\b :control) 'com-backward-object)
409     (global-set-key '(#\a :control) 'com-beginning-of-line)
410     (global-set-key '(#\e :control) 'com-end-of-line)
411     (global-set-key '(#\d :control) 'com-delete-object)
412     (global-set-key '(#\p :control) 'com-previous-line)
413     (global-set-key '(#\n :control) 'com-next-line)
414     (global-set-key '(#\o :control) 'com-open-line)
415     (global-set-key '(#\k :control) 'com-kill-line)
416     (global-set-key '(#\Space :control) 'com-set-mark)
417     (global-set-key '(#\y :control) 'com-copy-in)
418     (global-set-key '(#\w :control) 'com-cut-out)
419     (global-set-key '(#\f :meta) 'com-forward-word)
420     (global-set-key '(#\b :meta) 'com-backward-word)
421     (global-set-key '(#\x :meta) 'com-extended-command)
422     (global-set-key '(#\y :meta) 'com-kr-rotate) ;currently rotates only
423     (global-set-key '(#\w :meta) 'com-copy-out)
424     (global-set-key '(#\< :shift :meta) 'com-beginning-of-buffer)
425     (global-set-key '(#\> :shift :meta) 'com-end-of-buffer)
426     (global-set-key '(#\u :meta) 'com-browse-url)
427    
428     (global-set-key '(:up) 'com-previous-line)
429     (global-set-key '(:down) 'com-next-line)
430     (global-set-key '(:left) 'com-backward-object)
431     (global-set-key '(:right) 'com-forward-object)
432     (global-set-key '(:left :control) 'com-backward-word)
433     (global-set-key '(:right :control) 'com-forward-word)
434     (global-set-key '(:home) 'com-beginning-of-line)
435     (global-set-key '(:end) 'com-end-of-line)
436     (global-set-key '(:home :control) 'com-beginning-of-buffer)
437     (global-set-key '(:end :control) 'com-end-of-buffer)
438     (global-set-key #\Rubout 'com-delete-object)
439     (global-set-key #\Backspace 'com-backward-delete-object)
440    
441     ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
442     ;;;
443     ;;; C-x command table
444    
445     (make-command-table 'c-x-climacs-table :errorp nil)
446    
447     (add-menu-item-to-command-table 'global-climacs-table "C-x"
448     :menu 'c-x-climacs-table
449     :keystroke '(#\x :control))
450    
451     (defun c-x-set-key (gesture command)
452     (add-command-to-command-table command 'c-x-climacs-table
453     :keystroke gesture :errorp nil))
454    
455     (c-x-set-key '(#\c :control) 'com-quit)
456     (c-x-set-key '(#\f :control) 'com-find-file)
457     (c-x-set-key '(#\s :control) 'com-save-buffer)
458     (c-x-set-key '(#\w :control) 'com-write-buffer)

  ViewVC Help
Powered by ViewVC 1.1.5