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

Contents of /climacs/gui.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.133 - (show annotations)
Fri May 6 16:56:32 2005 UTC (8 years, 11 months ago) by crhodes
Branch: MAIN
Changes since 1.132: +39 -32 lines
rearrange the toplevel loop a little
1 ;;; -*- Mode: Lisp; Package: CLIMACS-GUI -*-
2
3 ;;; (c) copyright 2004-2005 by
4 ;;; Robert Strandh (strandh@labri.fr)
5 ;;; (c) copyright 2004-2005 by
6 ;;; Elliott Johnson (ejohnson@fasl.info)
7 ;;; (c) copyright 2005 by
8 ;;; Matthieu Villeneuve (matthieu.villeneuve@free.fr)
9 ;;; (c) copyright 2005 by
10 ;;; Aleksandar Bakic (a_bakic@yahoo.com)
11
12 ;;; This library is free software; you can redistribute it and/or
13 ;;; modify it under the terms of the GNU Library General Public
14 ;;; License as published by the Free Software Foundation; either
15 ;;; version 2 of the License, or (at your option) any later version.
16 ;;;
17 ;;; This library is distributed in the hope that it will be useful,
18 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 ;;; Library General Public License for more details.
21 ;;;
22 ;;; You should have received a copy of the GNU Library General Public
23 ;;; License along with this library; if not, write to the
24 ;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;;; Boston, MA 02111-1307 USA.
26
27 ;;; GUI for the Climacs editor.
28
29 (in-package :climacs-gui)
30
31 (defclass extended-pane (climacs-pane)
32 (;; allows a certain number of commands to have some minimal memory
33 (previous-command :initform nil :accessor previous-command)
34 ;; for next-line and previous-line commands
35 (goal-column :initform nil)
36 ;; for dynamic abbrev expansion
37 (original-prefix :initform nil)
38 (prefix-start-offset :initform nil)
39 (dabbrev-expansion-mark :initform nil)
40 (overwrite-mode :initform nil)))
41
42 (defclass info-pane (application-pane)
43 ((climacs-pane :initarg :climacs-pane)))
44
45 (defclass minibuffer-pane (application-pane) ())
46
47 (defmethod stream-accept :before ((pane minibuffer-pane) type &rest args)
48 (declare (ignore type args))
49 (window-clear pane))
50
51 (define-application-frame climacs ()
52 ((windows :accessor windows)
53 (buffers :initform '() :accessor buffers)
54 (recordingp :initform nil :accessor recordingp)
55 (executingp :initform nil :accessor executingp)
56 (recorded-keys :initform '() :accessor recorded-keys)
57 (remaining-keys :initform '() :accessor remaining-keys))
58 (:panes
59 (win (let* ((extended-pane
60 (make-pane 'extended-pane
61 :width 900 :height 400
62 :name 'bla
63 :end-of-line-action :scroll
64 :incremental-redisplay t
65 :display-function 'display-win))
66 (info-pane
67 (make-pane 'info-pane
68 :climacs-pane extended-pane
69 :width 900 :height 20 :max-height 20 :min-height 20
70 ::background +gray85+
71 :scroll-bars nil
72 :borders nil
73 :incremental-redisplay t
74 :display-function 'display-info)))
75 (vertically ()
76 (scrolling ()
77 extended-pane)
78 info-pane)))
79 (int (make-pane 'minibuffer-pane
80 :width 900 :height 20 :max-height 20 :min-height 20
81 :display-function 'display-minibuffer
82 :scroll-bars nil)))
83 (:layouts
84 (default
85 (vertically (:scroll-bars nil)
86 win
87 int)))
88 (:top-level (climacs-top-level)))
89
90 (defparameter *message* nil)
91
92 (defun display-message (format-string &rest format-args)
93 (setf *message*
94 (apply #'format nil format-string format-args)))
95
96 (defun display-minibuffer (frame pane)
97 (declare (ignore frame))
98 (unless (null *message*)
99 (princ *message* pane)
100 (setf *message* nil)))
101
102 (defmacro current-window () ; shouldn't this be an inlined function? --amb
103 `(car (windows *application-frame*)))
104
105 (defmethod execute-frame-command :around ((frame climacs) command)
106 (declare (ignore command))
107 (with-undo ((buffer (current-window)))
108 (call-next-method)))
109
110 (defmethod redisplay-frame-panes :around ((frame climacs) &rest args)
111 (declare (ignore args))
112 (let ((buffers (remove-duplicates (mapcar #'buffer (windows frame)))))
113 (loop for buffer in buffers
114 do (update-syntax buffer (syntax buffer)))
115 (call-next-method)
116 (loop for buffer in buffers
117 do (clear-modify buffer))))
118
119 (defun climacs ()
120 "Starts up a climacs session"
121 (let ((frame (make-application-frame 'climacs)))
122 (run-frame-top-level frame)))
123
124 (defun display-info (frame pane)
125 (declare (ignore frame))
126 (with-slots (climacs-pane) pane
127 (let* ((buf (buffer climacs-pane))
128 (name-info (format nil " ~a ~a Syntax: ~a~a~a~a ~a"
129 (if (needs-saving buf) "**" "--")
130 (name buf)
131 (name (syntax buf))
132 (if (slot-value climacs-pane 'overwrite-mode)
133 " Ovwrt"
134 "")
135 (if (auto-fill-mode climacs-pane)
136 " Fill"
137 "")
138 (if (isearch-mode climacs-pane)
139 " Isearch"
140 "")
141 (if (recordingp *application-frame*)
142 "Def"
143 ""))))
144 (princ name-info pane))))
145
146 (defun display-win (frame pane)
147 "The display function used by the climacs application frame."
148 (declare (ignore frame))
149 (redisplay-pane pane (eq pane (current-window))))
150
151 (defmethod handle-repaint :before ((pane extended-pane) region)
152 (declare (ignore region))
153 (redisplay-frame-panes *application-frame*))
154
155 (defun find-gestures (gestures start-table)
156 (loop with table = (find-command-table start-table)
157 for (gesture . rest) on gestures
158 for item = (find-keystroke-item gesture table :errorp nil)
159 while item
160 do (if (eq (command-menu-item-type item) :command)
161 (return (if (null rest) item nil))
162 (setf table (command-menu-item-value item)))
163 finally (return item)))
164
165 (defvar *kill-ring* (make-instance 'kill-ring :max-size 7))
166 (defparameter *current-gesture* nil)
167
168 (defun meta-digit (gesture)
169 (position gesture
170 '((#\0 :meta) (#\1 :meta) (#\2 :meta) (#\3 :meta) (#\4 :meta)
171 (#\5 :meta) (#\6 :meta) (#\7 :meta) (#\8 :meta) (#\9 :meta))
172 :test #'event-matches-gesture-name-p))
173
174 (defun climacs-read-gesture ()
175 (unless (null (remaining-keys *application-frame*))
176 (return-from climacs-read-gesture
177 (pop (remaining-keys *application-frame*))))
178 (loop for gesture = (read-gesture :stream *standard-input*)
179 until (or (characterp gesture)
180 (and (typep gesture 'keyboard-event)
181 (or (keyboard-event-character gesture)
182 (not (member (keyboard-event-key-name
183 gesture)
184 '(:control-left :control-right
185 :shift-left :shift-right
186 :meta-left :meta-right
187 :super-left :super-right
188 :hyper-left :hyper-right
189 :shift-lock :caps-lock
190 :alt-left :alt-right))))))
191 finally (progn (when (recordingp *application-frame*)
192 (push gesture (recorded-keys *application-frame*)))
193 (return gesture))))
194
195 (defun climacs-unread-gesture (gesture stream)
196 (cond ((recordingp *application-frame*)
197 (pop (recorded-keys *application-frame*))
198 (unread-gesture gesture :stream stream))
199 ((executingp *application-frame*)
200 (push gesture (remaining-keys *application-frame*)))
201 (t
202 (unread-gesture gesture :stream stream))))
203
204 (defun read-numeric-argument (&key (stream *standard-input*))
205 (let ((gesture (climacs-read-gesture)))
206 (cond ((event-matches-gesture-name-p gesture '(:keyboard #\u 512)) ; FIXME
207 (let ((numarg 4))
208 (loop for gesture = (climacs-read-gesture)
209 while (event-matches-gesture-name-p gesture '(:keyboard #\u 512)) ; FIXME
210 do (setf numarg (* 4 numarg))
211 finally (climacs-unread-gesture gesture stream))
212 (let ((gesture (climacs-read-gesture)))
213 (cond ((and (characterp gesture)
214 (digit-char-p gesture 10))
215 (setf numarg (- (char-code gesture) (char-code #\0)))
216 (loop for gesture = (climacs-read-gesture)
217 while (and (characterp gesture)
218 (digit-char-p gesture 10))
219 do (setf numarg (+ (* 10 numarg)
220 (- (char-code gesture) (char-code #\0))))
221 finally (climacs-unread-gesture gesture stream)
222 (return (values numarg t))))
223 (t
224 (climacs-unread-gesture gesture stream)
225 (values numarg t))))))
226 ((meta-digit gesture)
227 (let ((numarg (meta-digit gesture)))
228 (loop for gesture = (climacs-read-gesture)
229 while (meta-digit gesture)
230 do (setf numarg (+ (* 10 numarg) (meta-digit gesture)))
231 finally (climacs-unread-gesture gesture stream)
232 (return (values numarg t)))))
233 (t (climacs-unread-gesture gesture stream)
234 (values 1 nil)))))
235
236 ;;; we know the vbox pane has a scroller pane and an info
237 ;;; pane in it. The scroller pane has a viewport in it,
238 ;;; and the viewport contains the climacs-pane as its only child.
239 (defun find-climacs-pane (vbox)
240 (first (sheet-children
241 (find-if-not (lambda (pane) (typep pane 'scroll-bar-pane))
242 (sheet-children
243 (find-if (lambda (pane) (typep pane 'scroller-pane))
244 (sheet-children vbox)))))))
245
246 (defvar *numeric-argument-p* (list nil))
247
248 (defun substitute-numeric-argument-p (command numargp)
249 (substitute numargp *numeric-argument-p* command :test #'eq))
250
251 (defun climacs-top-level (frame &key
252 command-parser command-unparser
253 partial-command-parser prompt)
254 (declare (ignore command-parser command-unparser partial-command-parser prompt))
255 (with-slots (windows) frame
256 (setf windows (list (find-climacs-pane (find-pane-named frame 'win))))
257 (push (buffer (car windows)) (buffers frame))
258 (let ((*standard-output* (car windows))
259 (*standard-input* (find-pane-named frame 'int))
260 (*print-pretty* nil)
261 (*abort-gestures* '((:keyboard #\g 512))))
262 (redisplay-frame-panes frame :force-p t)
263 (flet ((do-command (command)
264 (handler-case
265 (execute-frame-command frame command)
266 (offset-before-beginning ()
267 (beep) (display-message "Beginning of buffer"))
268 (offset-after-end ()
269 (beep) (display-message "End of buffer"))
270 (motion-before-beginning ()
271 (beep) (display-message "Beginning of buffer"))
272 (motion-after-end ()
273 (beep) (display-message "End of buffer")))
274 (setf (previous-command *standard-output*)
275 (if (consp command)
276 (car command)
277 command)))
278 (update-climacs ()
279 (let ((buffer (buffer (current-window))))
280 (when (modified-p buffer)
281 (setf (needs-saving buffer) t)))
282 (when (null (remaining-keys *application-frame*))
283 (setf (executingp *application-frame*) nil)
284 (redisplay-frame-panes frame))))
285 (flet ((process-gestures ()
286 (loop
287 for gestures = '()
288 do (multiple-value-bind (numarg numargp)
289 (read-numeric-argument :stream *standard-input*)
290 (loop
291 (setf *current-gesture* (climacs-read-gesture))
292 (setf gestures
293 (nconc gestures (list *current-gesture*)))
294 (let ((item (find-gestures gestures 'global-climacs-table)))
295 (cond
296 ((not item)
297 (beep) (return))
298 ((eq (command-menu-item-type item) :command)
299 (let ((command (command-menu-item-value item)))
300 (unless (consp command)
301 (setf command (list command)))
302 (setf command (substitute-numeric-argument-marker command numarg))
303 (setf command (substitute-numeric-argument-p command numargp))
304 (do-command command)
305 (return)))
306 (t nil)))))
307 do (update-climacs))))
308 (loop
309 for maybe-error = t
310 do (restart-case
311 (progn
312 (handler-case
313 (with-input-context
314 ('(command :command-table 'global-climacs-table))
315 (object)
316 (process-gestures)
317 (t
318 (do-command object)
319 (setq maybe-error nil)))
320 (abort-gesture () (display-message "Quit")))
321 (when maybe-error
322 (beep))
323 (update-climacs))
324 (return-to-climacs () nil))))))))
325
326 (defmacro simple-command-loop (command-table loop-condition end-clauses)
327 (let ((gesture (gensym))
328 (item (gensym))
329 (command (gensym)))
330 `(progn
331 (redisplay-frame-panes *application-frame*)
332 (loop while ,loop-condition
333 as ,gesture = (climacs-read-gesture)
334 as ,item = (find-gestures (list ,gesture) ,command-table)
335 do (cond ((and ,item (eq (command-menu-item-type ,item) :command))
336 (setf *current-gesture* ,gesture)
337 (let ((,command (command-menu-item-value ,item)))
338 (unless (consp ,command)
339 (setf ,command (list ,command)))
340 (handler-case
341 (execute-frame-command *application-frame*
342 ,command)
343 (offset-before-beginning ()
344 (beep) (display-message "Beginning of buffer"))
345 (offset-after-end ()
346 (beep) (display-message "End of buffer"))
347 (motion-before-beginning ()
348 (beep) (display-message "Beginning of buffer"))
349 (motion-after-end ()
350 (beep) (display-message "End of buffer")))))
351 (t
352 (unread-gesture ,gesture)
353 ,@end-clauses))
354 (redisplay-frame-panes *application-frame*)))))
355
356 (defmacro define-named-command (command-name args &body body)
357 `(define-climacs-command ,(if (listp command-name)
358 `(,@command-name :name t)
359 `(,command-name :name t)) ,args ,@body))
360
361 (define-named-command com-toggle-overwrite-mode ()
362 (with-slots (overwrite-mode) (current-window)
363 (setf overwrite-mode (not overwrite-mode))))
364
365 (defun possibly-fill-line ()
366 (let* ((pane (current-window))
367 (buffer (buffer pane)))
368 (when (auto-fill-mode pane)
369 (let* ((fill-column (auto-fill-column pane))
370 (point (point pane))
371 (offset (offset point))
372 (tab-width (tab-space-count (stream-default-view pane)))
373 (syntax (syntax buffer)))
374 (when (>= (buffer-display-column buffer offset tab-width)
375 (1- fill-column))
376 (fill-line point
377 (lambda (mark)
378 (syntax-line-indentation mark tab-width syntax))
379 fill-column
380 tab-width))))))
381
382 (defun insert-character (char)
383 (let* ((win (current-window))
384 (point (point win)))
385 (unless (constituentp char)
386 (possibly-expand-abbrev point))
387 (when (whitespacep char)
388 (possibly-fill-line))
389 (if (and (slot-value win 'overwrite-mode) (not (end-of-line-p point)))
390 (progn
391 (delete-range point)
392 (insert-object point char))
393 (insert-object point char))))
394
395 (define-command com-self-insert ()
396 (insert-character *current-gesture*))
397
398 (define-named-command com-beginning-of-line ()
399 (beginning-of-line (point (current-window))))
400
401 (define-named-command com-end-of-line ()
402 (end-of-line (point (current-window))))
403
404 (define-named-command com-delete-object ((count 'integer :prompt "Number of Objects"))
405 (delete-range (point (current-window)) count))
406
407 (define-named-command com-backward-delete-object ((count 'integer :prompt "Number of Objects"))
408 (delete-range (point (current-window)) (- count)))
409
410 (define-named-command com-transpose-objects ()
411 (let* ((point (point (current-window))))
412 (unless (beginning-of-buffer-p point)
413 (when (end-of-line-p point)
414 (backward-object point))
415 (let ((object (object-after point)))
416 (delete-range point)
417 (backward-object point)
418 (insert-object point object)
419 (forward-object point)))))
420
421 (define-named-command com-backward-object ((count 'integer :prompt "Number of Objects"))
422 (backward-object (point (current-window)) count))
423
424 (define-named-command com-forward-object ((count 'integer :prompt "Number of Objects"))
425 (forward-object (point (current-window)) count))
426
427 (define-named-command com-transpose-words ()
428 (let* ((point (point (current-window))))
429 (let (bw1 bw2 ew1 ew2)
430 (backward-word point)
431 (setf bw1 (offset point))
432 (forward-word point)
433 (setf ew1 (offset point))
434 (forward-word point)
435 (when (= (offset point) ew1)
436 ;; this is emacs' message in the minibuffer
437 (error "Don't have two things to transpose"))
438 (setf ew2 (offset point))
439 (backward-word point)
440 (setf bw2 (offset point))
441 (let ((w2 (buffer-sequence (buffer point) bw2 ew2))
442 (w1 (buffer-sequence (buffer point) bw1 ew1)))
443 (delete-word point)
444 (insert-sequence point w1)
445 (backward-word point)
446 (backward-word point)
447 (delete-word point)
448 (insert-sequence point w2)
449 (forward-word point)))))
450
451 (define-named-command com-transpose-lines ()
452 (let ((point (point (current-window))))
453 (beginning-of-line point)
454 (unless (beginning-of-buffer-p point)
455 (previous-line point))
456 (let* ((bol (offset point))
457 (eol (progn (end-of-line point)
458 (offset point)))
459 (line (buffer-sequence (buffer point) bol eol)))
460 (delete-region bol point)
461 ;; Remove newline at end of line as well.
462 (unless (end-of-buffer-p point)
463 (delete-range point))
464 ;; If the current line is at the end of the buffer, we want to
465 ;; be able to insert past it, so we need to get an extra line
466 ;; at the end.
467 (end-of-line point)
468 (when (end-of-buffer-p point)
469 (insert-object point #\Newline))
470 (next-line point 0)
471 (insert-sequence point line)
472 (insert-object point #\Newline))))
473
474 (define-named-command com-previous-line ((numarg 'integer :prompt "How many lines?"))
475 (let* ((win (current-window))
476 (point (point win)))
477 (unless (or (eq (previous-command win) 'com-previous-line)
478 (eq (previous-command win) 'com-next-line))
479 (setf (slot-value win 'goal-column) (column-number point)))
480 (previous-line point (slot-value win 'goal-column) numarg)))
481
482 (define-named-command com-next-line ((numarg 'integer :prompt "How many lines?"))
483 (let* ((win (current-window))
484 (point (point win)))
485 (unless (or (eq (previous-command win) 'com-previous-line)
486 (eq (previous-command win) 'com-next-line))
487 (setf (slot-value win 'goal-column) (column-number point)))
488 (next-line point (slot-value win 'goal-column) numarg)))
489
490 (define-named-command com-open-line ((numarg 'integer :prompt "How many lines?"))
491 (open-line (point (current-window)) numarg))
492
493 (define-named-command com-kill-line ((numarg 'integer :prompt "Kill how many lines?")
494 (numargp 'boolean :prompt "Kill entire lines?"))
495 (let* ((pane (current-window))
496 (point (point pane))
497 (mark (offset point)))
498 (cond ((or numargp (> numarg 1))
499 (loop repeat numarg
500 until (end-of-buffer-p point)
501 do (end-of-line point)
502 until (end-of-buffer-p point)
503 do (forward-object point)))
504 (t
505 (cond ((end-of-buffer-p point) nil)
506 ((end-of-line-p point)(forward-object point))
507 (t (end-of-line point)))))
508 (unless (mark= point mark)
509 (if (eq (previous-command pane) 'com-kill-line)
510 (kill-ring-concatenating-push *kill-ring*
511 (region-to-sequence mark point))
512 (kill-ring-standard-push *kill-ring*
513 (region-to-sequence mark point)))
514 (delete-region mark point))))
515
516 (define-named-command com-forward-word ((count 'integer :prompt "Number of words"))
517 (forward-word (point (current-window)) count))
518
519 (define-named-command com-backward-word ((count 'integer :prompt "Number of words"))
520 (backward-word (point (current-window)) count))
521
522 (define-named-command com-delete-word ((count 'integer :prompt "Number of words"))
523 (delete-word (point (current-window)) count))
524
525 (define-named-command com-backward-delete-word ((count 'integer :prompt "Number of words"))
526 (backward-delete-word (point (current-window)) count))
527
528 (define-named-command com-upcase-region ()
529 (let ((cw (current-window)))
530 (upcase-region (mark cw) (point cw))))
531
532 (define-named-command com-downcase-region ()
533 (let ((cw (current-window)))
534 (downcase-region (mark cw) (point cw))))
535
536 (define-named-command com-capitalize-region ()
537 (let ((cw (current-window)))
538 (capitalize-region (mark cw) (point cw))))
539
540 (define-named-command com-upcase-word ()
541 (upcase-word (point (current-window))))
542
543 (define-named-command com-downcase-word ()
544 (downcase-word (point (current-window))))
545
546 (define-named-command com-capitalize-word ()
547 (capitalize-word (point (current-window))))
548
549 (define-named-command com-tabify-region ()
550 (let ((pane (current-window)))
551 (tabify-region
552 (mark pane) (point pane) (tab-space-count (stream-default-view pane)))))
553
554 (define-named-command com-untabify-region ()
555 (let ((pane (current-window)))
556 (untabify-region
557 (mark pane) (point pane) (tab-space-count (stream-default-view pane)))))
558
559 (defun indent-current-line (pane point)
560 (let* ((buffer (buffer pane))
561 (view (stream-default-view pane))
562 (tab-space-count (tab-space-count view))
563 (indentation (syntax-line-indentation point
564 tab-space-count
565 (syntax buffer))))
566 (indent-line point indentation (and (indent-tabs-mode buffer)
567 tab-space-count))))
568
569 (define-named-command com-indent-line ()
570 (let* ((pane (current-window))
571 (point (point pane)))
572 (indent-current-line pane point)))
573
574 (define-named-command com-newline-and-indent ()
575 (let* ((pane (current-window))
576 (point (point pane)))
577 (insert-object point #\Newline)
578 (indent-current-line pane point)))
579
580 (define-named-command com-delete-indentation ()
581 (delete-indentation (point (current-window))))
582
583 (define-named-command com-auto-fill-mode ()
584 (let ((pane (current-window)))
585 (setf (auto-fill-mode pane) (not (auto-fill-mode pane)))))
586
587 (define-named-command com-fill-paragraph ()
588 (let* ((pane (current-window))
589 (buffer (buffer pane))
590 (syntax (syntax buffer))
591 (point (point pane))
592 (begin-mark (clone-mark point))
593 (end-mark (clone-mark point)))
594 (unless (eql (object-before begin-mark) #\Newline)
595 (beginning-of-paragraph begin-mark syntax))
596 (unless (eql (object-after end-mark) #\Newline)
597 (end-of-paragraph end-mark syntax))
598 (do-buffer-region (object offset buffer
599 (offset begin-mark) (offset end-mark))
600 (when (eql object #\Newline)
601 (setf object #\Space)))
602 (let ((point-backup (clone-mark point)))
603 (setf (offset point) (offset end-mark))
604 (possibly-fill-line)
605 (setf (offset point) (offset point-backup)))))
606
607 (define-command com-extended-command ()
608 (let ((item (handler-case (accept 'command :prompt "Extended Command")
609 (error () (progn (beep)
610 (display-message "No such command")
611 (return-from com-extended-command nil))))))
612 (execute-frame-command *application-frame* item)))
613
614 (eval-when (:compile-toplevel :load-toplevel)
615 (define-presentation-type completable-pathname ()
616 :inherit-from 'pathname))
617
618 (defun filename-completer (so-far mode)
619 (flet ((remove-trail (s)
620 (subseq s 0 (let ((pos (position #\/ s :from-end t)))
621 (if pos (1+ pos) 0)))))
622 (let* ((directory-prefix
623 (if (and (plusp (length so-far)) (eql (aref so-far 0) #\/))
624 ""
625 (namestring #+sbcl *default-pathname-defaults*
626 #+cmu (ext:default-directory)
627 #-(or sbcl cmu) *default-pathname-defaults*)))
628 (full-so-far (concatenate 'string directory-prefix so-far))
629 (pathnames
630 (loop with length = (length full-so-far)
631 and wildcard = (concatenate 'string (remove-trail so-far) "*.*")
632 for path in
633 #+(or sbcl cmu lispworks) (directory wildcard)
634 #+openmcl (directory wildcard :directories t)
635 #+allegro (directory wildcard :directories-are-files nil)
636 #+cormanlisp (nconc (directory wildcard)
637 (cl::directory-subdirs dirname))
638 #-(or sbcl cmu lispworks openmcl allegro cormanlisp)
639 (directory wildcard)
640 when (let ((mismatch (mismatch (namestring path) full-so-far)))
641 (or (null mismatch) (= mismatch length)))
642 collect path))
643 (strings (mapcar #'namestring pathnames))
644 (first-string (car strings))
645 (length-common-prefix nil)
646 (completed-string nil)
647 (full-completed-string nil))
648 (unless (null pathnames)
649 (setf length-common-prefix
650 (loop with length = (length first-string)
651 for string in (cdr strings)
652 do (setf length (min length (or (mismatch string first-string) length)))
653 finally (return length))))
654 (unless (null pathnames)
655 (setf completed-string
656 (subseq first-string (length directory-prefix)
657 (if (null (cdr pathnames)) nil length-common-prefix)))
658 (setf full-completed-string
659 (concatenate 'string directory-prefix completed-string)))
660 (case mode
661 ((:complete-limited :complete-maximal)
662 (cond ((null pathnames)
663 (values so-far nil nil 0 nil))
664 ((null (cdr pathnames))
665 (values completed-string t (car pathnames) 1 nil))
666 (t
667 (values completed-string nil nil (length pathnames) nil))))
668 (:complete
669 (cond ((null pathnames)
670 (values so-far t so-far 1 nil))
671 ((null (cdr pathnames))
672 (values completed-string t (car pathnames) 1 nil))
673 ((find full-completed-string strings :test #'string-equal)
674 (let ((pos (position full-completed-string strings :test #'string-equal)))
675 (values completed-string
676 t (elt pathnames pos) (length pathnames) nil)))
677 (t
678 (values completed-string nil nil (length pathnames) nil))))
679 (:possibilities
680 (values nil nil nil (length pathnames)
681 (loop with length = (length directory-prefix)
682 for name in pathnames
683 collect (list (subseq (namestring name) length nil)
684 name))))))))
685
686 (define-presentation-method accept
687 ((type completable-pathname) stream (view textual-view) &key)
688 (multiple-value-bind (pathname success string)
689 (complete-input stream
690 #'filename-completer
691 :partial-completers '(#\Space)
692 :allow-any-input t)
693 (declare (ignore success))
694 (or pathname string)))
695
696 (defun pathname-filename (pathname)
697 (if (null (pathname-type pathname))
698 (pathname-name pathname)
699 (concatenate 'string (pathname-name pathname)
700 "." (pathname-type pathname))))
701
702 (define-named-command com-find-file ()
703 (let ((filename (accept 'completable-pathname
704 :prompt "Find File"))
705 (buffer (make-instance 'climacs-buffer))
706 (pane (current-window)))
707 (push buffer (buffers *application-frame*))
708 (setf (buffer (current-window)) buffer)
709 (setf (syntax buffer) (make-instance
710 'basic-syntax :buffer (buffer (point pane))))
711 ;; Don't want to create the file if it doesn't exist.
712 (when (probe-file filename)
713 (with-open-file (stream filename :direction :input)
714 (input-from-stream stream buffer 0)))
715 (setf (filename buffer) filename
716 (name buffer) (pathname-filename filename)
717 (needs-saving buffer) nil)
718 (beginning-of-buffer (point pane))
719 ;; this one is needed so that the buffer modification protocol
720 ;; resets the low and high marks after redisplay
721 (redisplay-frame-panes *application-frame*)))
722
723 (defun save-buffer (buffer)
724 (let ((filename (or (filename buffer)
725 (accept 'completable-pathname
726 :prompt "Save Buffer to File"))))
727 (with-open-file (stream filename :direction :output :if-exists :supersede)
728 (output-to-stream stream buffer 0 (size buffer)))
729 (setf (filename buffer) filename
730 (name buffer) (pathname-filename filename))
731 (display-message "Wrote: ~a" (filename buffer))
732 (setf (needs-saving buffer) nil)))
733
734 (define-named-command com-save-buffer ()
735 (let ((buffer (buffer (current-window))))
736 (if (or (null (filename buffer))
737 (needs-saving buffer))
738 (save-buffer buffer)
739 (display-message "No changes need to be saved from ~a" (name buffer)))))
740
741 (define-named-command (com-quit) ()
742 (loop for buffer in (buffers *application-frame*)
743 when (and (needs-saving buffer)
744 (handler-case (accept 'boolean
745 :prompt (format nil "Save buffer: ~a ?" (name buffer)))
746 (error () (progn (beep)
747 (display-message "Invalid answer")
748 (return-from com-quit nil)))))
749 do (save-buffer buffer))
750 (when (or (notany #'needs-saving
751 (buffers *application-frame*))
752 (handler-case (accept 'boolean :prompt "Modified buffers exist. Quit anyway?")
753 (error () (progn (beep)
754 (display-message "Invalid answer")
755 (return-from com-quit nil)))))
756 (frame-exit *application-frame*)))
757
758 (define-named-command com-write-buffer ()
759 (let ((filename (accept 'completable-pathname
760 :prompt "Write Buffer to File"))
761 (buffer (buffer (current-window))))
762 (with-open-file (stream filename :direction :output :if-exists :supersede)
763 (output-to-stream stream buffer 0 (size buffer)))
764 (setf (filename buffer) filename
765 (name buffer) (pathname-filename filename)
766 (needs-saving buffer) nil)
767 (display-message "Wrote: ~a" (filename buffer))))
768
769 (define-presentation-method accept
770 ((type buffer) stream (view textual-view) &key)
771 (multiple-value-bind (object success string)
772 (complete-input stream
773 (lambda (so-far action)
774 (complete-from-possibilities
775 so-far (buffers *application-frame*) '() :action action
776 :name-key #'name
777 :value-key #'identity))
778 :partial-completers '(#\Space)
779 :allow-any-input t)
780 (declare (ignore success))
781 (or object
782 (car (push (make-instance 'climacs-buffer :name string)
783 (buffers *application-frame*))))))
784
785 (define-named-command com-switch-to-buffer ()
786 (let ((buffer (accept 'buffer
787 :prompt "Switch to buffer"))
788 (pane (current-window)))
789 (setf (buffer pane) buffer)
790 (full-redisplay pane)))
791
792 (define-named-command com-kill-buffer ()
793 (with-slots (buffers) *application-frame*
794 (let ((buffer (buffer (current-window))))
795 (when (and (needs-saving buffer)
796 (handler-case (accept 'boolean :prompt "Save buffer first?")
797 (error () (progn (beep)
798 (display-message "Invalid answer")
799 (return-from com-kill-buffer nil)))))
800 (com-save-buffer))
801 (setf buffers (remove buffer buffers))
802 ;; Always need one buffer.
803 (when (null buffers)
804 (push (make-instance 'climacs-buffer :name "*scratch*")
805 buffers))
806 (setf (buffer (current-window)) (car buffers)))))
807
808 (define-named-command com-full-redisplay ()
809 (full-redisplay (current-window)))
810
811 (define-named-command com-load-file ()
812 (let ((filename (accept 'completable-pathname
813 :prompt "Load File")))
814 (load filename)))
815
816 (define-named-command com-beginning-of-buffer ()
817 (beginning-of-buffer (point (current-window))))
818
819 (define-named-command com-page-down ()
820 (let ((pane (current-window)))
821 (page-down pane)))
822
823 (define-named-command com-page-up ()
824 (let ((pane (current-window)))
825 (page-up pane)))
826
827 (define-named-command com-end-of-buffer ()
828 (end-of-buffer (point (current-window))))
829
830 (define-named-command com-back-to-indentation ()
831 (let ((point (point (current-window))))
832 (beginning-of-line point)
833 (loop until (end-of-line-p point)
834 while (whitespacep (object-after point))
835 do (incf (offset point)))))
836
837 (define-named-command com-goto-position ()
838 (setf (offset (point (current-window)))
839 (handler-case (accept 'integer :prompt "Goto Position")
840 (error () (progn (beep)
841 (display-message "Not a valid position")
842 (return-from com-goto-position nil))))))
843
844 (define-named-command com-goto-line ()
845 (loop with mark = (let ((m (clone-mark
846 (low-mark (buffer (current-window)))
847 :right)))
848 (beginning-of-buffer m)
849 m)
850 do (end-of-line mark)
851 until (end-of-buffer-p mark)
852 repeat (handler-case (accept 'integer :prompt "Goto Line")
853 (error () (progn (beep)
854 (display-message "Not a valid line number")
855 (return-from com-goto-line nil))))
856 do (incf (offset mark))
857 (end-of-line mark)
858 finally (beginning-of-line mark)
859 (setf (offset (point (current-window)))
860 (offset mark))))
861
862 (define-named-command com-browse-url ()
863 (accept 'url :prompt "Browse URL"))
864
865 (define-named-command com-set-mark ()
866 (let ((pane (current-window)))
867 (setf (mark pane) (clone-mark (point pane)))))
868
869 (define-named-command com-exchange-point-and-mark ()
870 (let ((pane (current-window)))
871 (psetf (offset (mark pane)) (offset (point pane))
872 (offset (point pane)) (offset (mark pane)))))
873
874 (define-named-command com-set-syntax ()
875 (let* ((pane (current-window))
876 (buffer (buffer pane)))
877 (setf (syntax buffer)
878 (make-instance (or (accept 'syntax :prompt "Set Syntax")
879 (progn (beep)
880 (display-message "No such syntax")
881 (return-from com-set-syntax nil)))
882 :buffer (buffer (point pane))))
883 (setf (offset (low-mark buffer)) 0
884 (offset (high-mark buffer)) (size buffer))))
885
886 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
887 ;;;
888 ;;; Keyboard macros
889
890 (define-named-command com-start-kbd-macro ()
891 (setf (recordingp *application-frame*) t)
892 (setf (recorded-keys *application-frame*) '()))
893
894 (define-named-command com-end-kbd-macro ()
895 (setf (recordingp *application-frame*) nil)
896 (setf (recorded-keys *application-frame*)
897 ;; this won't work if the command was invoked in any old way
898 (reverse (cddr (recorded-keys *application-frame*)))))
899
900 (define-named-command com-call-last-kbd-macro ()
901 (setf (remaining-keys *application-frame*)
902 (recorded-keys *application-frame*))
903 (setf (executingp *application-frame*) t))
904
905 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
906 ;;;
907 ;;; Commands for splitting windows
908
909 (defun replace-constellation (constellation additional-constellation vertical-p)
910 (let* ((parent (sheet-parent constellation))
911 (children (sheet-children parent))
912 (first (first children))
913 (second (second children))
914 (third (third children))
915 (adjust (make-pane 'clim-extensions:box-adjuster-gadget)))
916 (assert (member constellation children))
917 (sheet-disown-child parent constellation)
918 (let ((new (if vertical-p
919 (vertically ()
920 constellation adjust additional-constellation)
921 (horizontally ()
922 constellation adjust additional-constellation))))
923 (sheet-adopt-child parent new)
924 (reorder-sheets parent
925 (if (eq constellation first)
926 (if third
927 (list new second third)
928 (list new second))
929 (if third
930 (list first second new)
931 (list first new)))))))
932
933 (defun parent3 (sheet)
934 (sheet-parent (sheet-parent (sheet-parent sheet))))
935
936 (defun make-pane-constellation ()
937 "make a vbox containing a scroller pane as its first child and an
938 info pane as its second child. The scroller pane contains a viewport
939 which contains an extended pane. Return the vbox and the extended pane
940 as two values"
941 (let* ((extended-pane
942 (make-pane 'extended-pane
943 :width 900 :height 400
944 :name 'win
945 :end-of-line-action :scroll
946 :incremental-redisplay t
947 :display-function 'display-win))
948 (vbox
949 (vertically ()
950 (scrolling () extended-pane)
951 (make-pane 'info-pane
952 :climacs-pane extended-pane
953 :width 900 :height 20
954 :max-height 20 :min-height 20
955 ::background +gray85+
956 :scroll-bars nil
957 :borders nil
958 :incremental-redisplay t
959 :display-function 'display-info))))
960 (values vbox extended-pane)))
961
962 (define-named-command com-split-window-vertically ()
963 (with-look-and-feel-realization
964 ((frame-manager *application-frame*) *application-frame*)
965 (multiple-value-bind (vbox new-pane) (make-pane-constellation)
966 (let* ((current-window (current-window))
967 (constellation-root (parent3 current-window)))
968 (setf (buffer new-pane) (buffer current-window)
969 (auto-fill-mode new-pane) (auto-fill-mode current-window)
970 (auto-fill-column new-pane) (auto-fill-column current-window))
971 (push new-pane (windows *application-frame*))
972 (replace-constellation constellation-root vbox t)
973 (full-redisplay current-window)
974 (full-redisplay new-pane)))))
975
976 (define-named-command com-split-window-horizontally ()
977 (with-look-and-feel-realization
978 ((frame-manager *application-frame*) *application-frame*)
979 (multiple-value-bind (vbox new-pane) (make-pane-constellation)
980 (let* ((current-window (current-window))
981 (constellation-root (parent3 current-window)))
982 (setf (buffer new-pane) (buffer current-window)
983 (auto-fill-mode new-pane) (auto-fill-mode current-window)
984 (auto-fill-column new-pane) (auto-fill-column current-window))
985 (push new-pane (windows *application-frame*))
986 (replace-constellation constellation-root vbox nil)
987 (full-redisplay current-window)
988 (full-redisplay new-pane)))))
989
990 (define-named-command com-other-window ()
991 (setf (windows *application-frame*)
992 (append (cdr (windows *application-frame*))
993 (list (car (windows *application-frame*))))))
994
995 (define-named-command com-single-window ()
996 (loop until (null (cdr (windows *application-frame*)))
997 do (rotatef (car (windows *application-frame*))
998 (cadr (windows *application-frame*)))
999 (com-delete-window)))
1000
1001 (define-named-command com-delete-window ()
1002 (unless (null (cdr (windows *application-frame*)))
1003 (let* ((constellation (parent3 (current-window)))
1004 (box (sheet-parent constellation))
1005 (box-children (sheet-children box))
1006 (other (if (eq constellation (first box-children))
1007 (third box-children)
1008 (first box-children)))
1009 (parent (sheet-parent box))
1010 (children (sheet-children parent))
1011 (first (first children))
1012 (second (second children))
1013 (third (third children)))
1014 (pop (windows *application-frame*))
1015 (sheet-disown-child box other)
1016 (sheet-disown-child parent box)
1017 (sheet-adopt-child parent other)
1018 (reorder-sheets parent (if (eq box first)
1019 (if third
1020 (list other second third)
1021 (list other second))
1022 (if third
1023 (list first second other)
1024 (list first other)))))))
1025
1026 ;;;;;;;;;;;;;;;;;;;;
1027 ;; Kill ring commands
1028
1029 ;; Copies an element from a kill-ring to a buffer at the given offset
1030 (define-named-command com-yank ()
1031 (insert-sequence (point (current-window)) (kill-ring-yank *kill-ring*)))
1032
1033 ;; Destructively cut a given buffer region into the kill-ring
1034 (define-named-command com-cut-out ()
1035 (let ((pane (current-window)))
1036 (kill-ring-standard-push
1037 *kill-ring* (region-to-sequence (mark pane) (point pane)))
1038 (delete-region (mark pane) (point pane))))
1039
1040 ;; Non destructively copies in buffer region to the kill ring
1041 (define-named-command com-copy-out ()
1042 (let ((pane (current-window)))
1043 (kill-ring-standard-push *kill-ring* (region-to-sequence (point pane) (mark pane)))))
1044
1045 (define-named-command com-rotate-yank ()
1046 (let* ((pane (current-window))
1047 (point (point pane))
1048 (last-yank (kill-ring-yank *kill-ring*)))
1049 (if (eq (previous-command pane)
1050 'com-rotate-yank)
1051 (progn
1052 (delete-range point (* -1 (length last-yank)))
1053 (rotate-yank-position *kill-ring*)))
1054 (insert-sequence point (kill-ring-yank *kill-ring*))))
1055
1056 (define-named-command com-resize-kill-ring ()
1057 (let ((size (handler-case (accept 'integer :prompt "New kill ring size")
1058 (error () (progn (beep)
1059 (display-message "Not a valid kill ring size")
1060 (return-from com-resize-kill-ring nil))))))
1061 (setf (kill-ring-max-size *kill-ring*) size)))
1062
1063 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1064 ;;;
1065 ;;; Incremental search
1066
1067 (defun isearch-command-loop (pane forwardp)
1068 (let ((point (point pane)))
1069 (unless (endp (isearch-states pane))
1070 (setf (isearch-previous-string pane)
1071 (search-string (first (isearch-states pane)))))
1072 (setf (isearch-mode pane) t)
1073 (setf (isearch-states pane)
1074 (list (make-instance 'isearch-state
1075 :search-string ""
1076 :search-mark (clone-mark point)
1077 :search-forward-p forwardp
1078 :search-success-p t)))
1079 (simple-command-loop 'isearch-climacs-table
1080 (isearch-mode pane)
1081 ((setf (isearch-mode pane) nil)))))
1082
1083 (defun isearch-from-mark (pane mark string forwardp)
1084 (flet ((object-equal (x y)
1085 (if (characterp x)
1086 (and (characterp y) (char-equal x y))
1087 (eql x y))))
1088 (let* ((point (point pane))
1089 (mark2 (clone-mark mark))
1090 (success (funcall (if forwardp #'search-forward #'search-backward)
1091 mark2
1092 string
1093 :test #'object-equal)))
1094 (when success
1095 (setf (offset point) (offset mark2)
1096 (offset mark) (if forwardp
1097 (- (offset mark2) (length string))
1098 (+ (offset mark2) (length string)))))
1099 (display-message "~:[Failing ~;~]Isearch~:[ backward~;~]: ~A"
1100 success forwardp string)
1101 (push (make-instance 'isearch-state
1102 :search-string string
1103 :search-mark mark
1104 :search-forward-p forwardp
1105 :search-success-p success)
1106 (isearch-states pane))
1107 (unless success
1108 (beep)))))
1109
1110 (define-named-command com-isearch-mode-forward ()
1111 (display-message "Isearch: ")
1112 (isearch-command-loop (current-window) t))
1113
1114 (define-named-command com-isearch-mode-backward ()
1115 (display-message "Isearch backward: ")
1116 (isearch-command-loop (current-window) nil))
1117
1118 (define-named-command com-isearch-append-char ()
1119 (let* ((pane (current-window))
1120 (states (isearch-states pane))
1121 (string (concatenate 'string
1122 (search-string (first states))
1123 (string *current-gesture*)))
1124 (mark (clone-mark (search-mark (first states))))
1125 (forwardp (search-forward-p (first states))))
1126 (unless forwardp
1127 (incf (offset mark)))
1128 (isearch-from-mark pane mark string forwardp)))
1129
1130 (define-named-command com-isearch-delete-char ()
1131 (let* ((pane (current-window)))
1132 (cond ((null (second (isearch-states pane)))
1133 (display-message "Isearch: ")
1134 (beep))
1135 (t
1136 (pop (isearch-states pane))
1137 (loop until (endp (rest (isearch-states pane)))
1138 until (search-success-p (first (isearch-states pane)))
1139 do (pop (isearch-states pane)))
1140 (let ((state (first (isearch-states pane))))
1141 (setf (offset (point pane))
1142 (if (search-forward-p state)
1143 (+ (offset (search-mark state))
1144 (length (search-string state)))
1145 (- (offset (search-mark state))
1146 (length (search-string state)))))
1147 (display-message "Isearch~:[ backward~;~]: ~A"
1148 (search-forward-p state)
1149 (search-string state)))))))
1150
1151 (define-named-command com-isearch-forward ()
1152 (let* ((pane (current-window))
1153 (point (point pane))
1154 (states (isearch-states pane))
1155 (string (if (null (second states))
1156 (isearch-previous-string pane)
1157 (search-string (first states))))
1158 (mark (clone-mark point)))
1159 (isearch-from-mark pane mark string t)))
1160
1161 (define-named-command com-isearch-backward ()
1162 (let* ((pane (current-window))
1163 (point (point pane))
1164 (states (isearch-states pane))
1165 (string (if (null (second states))
1166 (isearch-previous-string pane)
1167 (search-string (first states))))
1168 (mark (clone-mark point)))
1169 (isearch-from-mark pane mark string nil)))
1170
1171 (define-named-command com-isearch-exit ()
1172 (setf (isearch-mode (current-window)) nil))
1173
1174 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1175 ;;;
1176 ;;; Query replace
1177
1178 (defun query-replace-find-next-match (mark string)
1179 (flet ((object-equal (x y)
1180 (and (characterp x)
1181 (characterp y)
1182 (char-equal x y))))
1183 (let ((offset-before (offset mark)))
1184 (search-forward mark string :test #'object-equal)
1185 (/= (offset mark) offset-before))))
1186
1187 (define-named-command com-query-replace ()
1188 (let* ((string1 (handler-case (accept 'string :prompt "Query replace")
1189 (error () (progn (beep)
1190 (display-message "Empty string")
1191 (return-from com-query-replace nil)))))
1192 (string2 (handler-case (accept 'string
1193 :prompt (format nil "Query replace ~A with"
1194 string1))
1195 (error () (progn (beep)
1196 (display-message "Empty string")
1197 (return-from com-query-replace nil)))))
1198 (pane (current-window))
1199 (point (point pane)))
1200 (when (query-replace-find-next-match point string1)
1201 (setf (query-replace-state pane) (make-instance 'query-replace-state
1202 :string1 string1
1203 :string2 string2)
1204 (query-replace-mode pane) t)
1205 (simple-command-loop 'query-replace-climacs-table
1206 (query-replace-mode pane)
1207 ((setf (query-replace-mode pane) nil))))))
1208
1209 (define-named-command com-query-replace-replace ()
1210 (let* ((pane (current-window))
1211 (point (point pane))
1212 (buffer (buffer pane))
1213 (state (query-replace-state pane))
1214 (string1-length (length (string1 state))))
1215 (backward-object point string1-length)
1216 (let* ((offset1 (offset point))
1217 (offset2 (+ offset1 string1-length))
1218 (region-case (buffer-region-case buffer offset1 offset2)))
1219 (delete-range point string1-length)
1220 (insert-sequence point (string2 state))
1221 (setf offset2 (+ offset1 (length (string2 state))))
1222 (finish-output *error-output*)
1223 (case region-case
1224 (:upper-case (upcase-buffer-region buffer offset1 offset2))
1225 (:lower-case (downcase-buffer-region buffer offset1 offset2))
1226 (:capitalized (capitalize-buffer-region buffer offset1 offset2))))
1227 (unless (query-replace-find-next-match point (string1 state))
1228 (setf (query-replace-mode pane) nil))))
1229
1230 (define-named-command com-query-replace-skip ()
1231 (let* ((pane (current-window))
1232 (point (point pane))
1233 (state (query-replace-state pane)))
1234 (unless (query-replace-find-next-match point (string1 state))
1235 (setf (query-replace-mode pane) nil))))
1236
1237 (define-named-command com-query-replace-exit ()
1238 (setf (query-replace-mode (current-window)) nil))
1239
1240 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1241 ;;;
1242 ;;; Undo/redo
1243
1244 (define-named-command com-undo ()
1245 (undo (undo-tree (buffer (current-window))))
1246 (full-redisplay (current-window)))
1247
1248 (define-named-command com-redo ()
1249 (redo (undo-tree (buffer (current-window))))
1250 (full-redisplay (current-window)))
1251
1252 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1253 ;;;
1254 ;;; Dynamic abbrevs
1255
1256 (define-named-command com-dabbrev-expand ()
1257 (let* ((win (current-window))
1258 (point (point win)))
1259 (with-slots (original-prefix prefix-start-offset dabbrev-expansion-mark) win
1260 (flet ((move () (cond ((beginning-of-buffer-p dabbrev-expansion-mark)
1261 (setf (offset dabbrev-expansion-mark)
1262 (offset point))
1263 (forward-word dabbrev-expansion-mark))
1264 ((mark< dabbrev-expansion-mark point)
1265 (backward-object dabbrev-expansion-mark))
1266 (t (forward-object dabbrev-expansion-mark)))))
1267 (unless (or (beginning-of-buffer-p point)
1268 (not (constituentp (object-before point))))
1269 (unless (and (eq (previous-command win) 'com-dabbrev-expand)
1270 (not (null prefix-start-offset)))
1271 (setf dabbrev-expansion-mark (clone-mark point))
1272 (backward-word dabbrev-expansion-mark)
1273 (setf prefix-start-offset (offset dabbrev-expansion-mark))
1274 (setf original-prefix (region-to-sequence prefix-start-offset point))
1275 (move))
1276 (loop until (or (end-of-buffer-p dabbrev-expansion-mark)
1277 (and (or (beginning-of-buffer-p dabbrev-expansion-mark)
1278 (not (constituentp (object-before dabbrev-expansion-mark))))
1279 (looking-at dabbrev-expansion-mark original-prefix)))
1280 do (move))
1281 (if (end-of-buffer-p dabbrev-expansion-mark)
1282 (progn (delete-region prefix-start-offset point)
1283 (insert-sequence point original-prefix)
1284 (setf prefix-start-offset nil))
1285 (progn (delete-region prefix-start-offset point)
1286 (insert-sequence point
1287 (let ((offset (offset dabbrev-expansion-mark)))
1288 (prog2 (forward-word dabbrev-expansion-mark)
1289 (region-to-sequence offset dabbrev-expansion-mark)
1290 (setf (offset dabbrev-expansion-mark) offset))))
1291 (move))))))))
1292
1293 (define-named-command com-beginning-of-paragraph ()
1294 (let* ((pane (current-window))
1295 (point (point pane))
1296 (syntax (syntax (buffer pane))))
1297 (beginning-of-paragraph point syntax)))
1298
1299 (define-named-command com-end-of-paragraph ()
1300 (let* ((pane (current-window))
1301 (point (point pane))
1302 (syntax (syntax (buffer pane))))
1303 (end-of-paragraph point syntax)))
1304
1305 (define-named-command com-eval-expression ((insertp 'boolean :prompt "Insert?"))
1306 (let* ((*package* (find-package :climacs-gui))
1307 (string (handler-case (accept 'string :prompt "Eval")
1308 (error () (progn (beep)
1309 (display-message "Empty string")
1310 (return-from com-eval-expression nil)))))
1311 (result (format nil "~a"
1312 (handler-case (eval (read-from-string string))
1313 (error (condition) (progn (beep)
1314 (display-message "~a" condition)
1315 (return-from com-eval-expression nil)))))))
1316 (if insertp
1317 (insert-sequence (point (current-window)) result)
1318 (display-message result))))
1319
1320 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1321 ;;;
1322 ;;; Global and dead-escape command tables
1323
1324 (make-command-table 'global-climacs-table :errorp nil)
1325
1326 (make-command-table 'dead-escape-climacs-table :errorp nil)
1327
1328 (add-menu-item-to-command-table 'global-climacs-table "dead-escape"
1329 :menu 'dead-escape-climacs-table
1330 :keystroke '(:escape))
1331
1332 (defun dead-escape-set-key (gesture command)
1333 (add-command-to-command-table command 'dead-escape-climacs-table
1334 :keystroke gesture :errorp nil))
1335
1336 (defun global-set-key (gesture command)
1337 (add-command-to-command-table command 'global-climacs-table
1338 :keystroke gesture :errorp nil)
1339 (when (and
1340 (listp gesture)
1341 (find :meta gesture))
1342 (dead-escape-set-key (remove :meta gesture) command)))
1343
1344 (loop for code from (char-code #\Space) to (char-code #\~)
1345 do (global-set-key (code-char code) 'com-self-insert))
1346
1347 (global-set-key #\Newline 'com-self-insert)
1348 (global-set-key #\Tab 'com-indent-line)
1349 (global-set-key '(#\: :shift :meta) `(com-eval-expression ,*numeric-argument-p*))
1350 (global-set-key '(#\j :control) 'com-newline-and-indent)
1351 (global-set-key '(#\f :control) `(com-forward-object ,*numeric-argument-marker*))
1352 (global-set-key '(#\b :control) `(com-backward-object ,*numeric-argument-marker*))
1353 (global-set-key '(#\a :control) 'com-beginning-of-line)
1354 (global-set-key '(#\e :control) 'com-end-of-line)
1355 (global-set-key '(#\d :control) `(com-delete-object ,*numeric-argument-marker*))
1356 (global-set-key '(#\p :control) `(com-previous-line ,*numeric-argument-marker*))
1357 (global-set-key '(#\l :control) 'com-full-redisplay)
1358 (global-set-key '(#\n :control) `(com-next-line ,*numeric-argument-marker*))
1359 (global-set-key '(#\o :control) `(com-open-line ,*numeric-argument-marker*))
1360 (global-set-key '(#\k :control) `(com-kill-line ,*numeric-argument-marker* ,*numeric-argument-p*))
1361 (global-set-key '(#\t :control) 'com-transpose-objects)
1362 (global-set-key '(#\Space :control) 'com-set-mark)
1363 (global-set-key '(#\y :control) 'com-yank)
1364 (global-set-key '(#\w :control) 'com-cut-out)
1365 (global-set-key '(#\f :meta) `(com-forward-word ,*numeric-argument-marker*))
1366 (global-set-key '(#\b :meta) `(com-backward-word ,*numeric-argument-marker*))
1367 (global-set-key '(#\t :meta) 'com-transpose-words)
1368 (global-set-key '(#\u :meta) 'com-upcase-word)
1369 (global-set-key '(#\l :meta) 'com-downcase-word)
1370 (global-set-key '(#\c :meta) 'com-capitalize-word)
1371 (global-set-key '(#\x :meta) 'com-extended-command)
1372 (global-set-key '(#\y :meta) 'com-rotate-yank)
1373 (global-set-key '(#\w :meta) 'com-copy-out)
1374 (global-set-key '(#\v :control) 'com-page-down)
1375 (global-set-key '(#\v :meta) 'com-page-up)
1376 (global-set-key '(#\< :shift :meta) 'com-beginning-of-buffer)
1377 (global-set-key '(#\> :shift :meta) 'com-end-of-buffer)
1378 (global-set-key '(#\m :meta) 'com-back-to-indentation)
1379 (global-set-key '(#\^ :shift :meta) 'com-delete-indentation)
1380 (global-set-key '(#\q :meta) 'com-fill-paragraph)
1381 (global-set-key '(#\d :meta) `(com-delete-word ,*numeric-argument-marker*))
1382 (global-set-key '(#\Backspace :meta) `(com-backward-delete-word ,*numeric-argument-marker*))
1383 (global-set-key '(#\/ :meta) 'com-dabbrev-expand)
1384 (global-set-key '(#\a :control :meta) 'com-beginning-of-paragraph)
1385 (global-set-key '(#\e :control :meta) 'com-end-of-paragraph)
1386 (global-set-key '(#\s :control) 'com-isearch-mode-forward)
1387 (global-set-key '(#\r :control) 'com-isearch-mode-backward)
1388 (global-set-key '(#\% :shift :meta) 'com-query-replace)
1389
1390 (global-set-key '(:up) `(com-previous-line ,*numeric-argument-marker*))
1391 (global-set-key '(:down) `(com-next-line ,*numeric-argument-marker*))
1392 (global-set-key '(:left) `(com-backward-object ,*numeric-argument-marker*))
1393 (global-set-key '(:right) `(com-forward-object ,*numeric-argument-marker*))
1394 (global-set-key '(:left :control) `(com-backward-word ,*numeric-argument-marker*))
1395 (global-set-key '(:right :control) `(com-forward-word ,*numeric-argument-marker*))
1396 (global-set-key '(:home) 'com-beginning-of-line)
1397 (global-set-key '(:end) 'com-end-of-line)
1398 (global-set-key '(:prior) 'com-page-up)
1399 (global-set-key '(:next) 'com-page-down)
1400 (global-set-key '(:home :control) 'com-beginning-of-buffer)
1401 (global-set-key '(:end :control) 'com-end-of-buffer)
1402 (global-set-key #\Rubout `(com-delete-object ,*numeric-argument-marker*))
1403 (global-set-key #\Backspace `(com-backward-delete-object ,*numeric-argument-marker*))
1404
1405 (global-set-key '(:insert) 'com-toggle-overwrite-mode)
1406
1407 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1408 ;;;
1409 ;;; C-x command table
1410
1411 (make-command-table 'c-x-climacs-table :errorp nil)
1412
1413 (add-menu-item-to-command-table 'global-climacs-table "C-x"
1414 :menu 'c-x-climacs-table
1415 :keystroke '(#\x :control))
1416
1417 (defun c-x-set-key (gesture command)
1418 (add-command-to-command-table command 'c-x-climacs-table
1419 :keystroke gesture :errorp nil))
1420
1421 (c-x-set-key '(#\0) 'com-delete-window)
1422 (c-x-set-key '(#\1) 'com-single-window)
1423 (c-x-set-key '(#\2) 'com-split-window-vertically)
1424 (c-x-set-key '(#\3) 'com-split-window-horizontally)
1425 (c-x-set-key '(#\() 'com-start-kbd-macro)
1426 (c-x-set-key '(#\)) 'com-end-kbd-macro)
1427 (c-x-set-key '(#\b) 'com-switch-to-buffer)
1428 (c-x-set-key '(#\e) 'com-call-last-kbd-macro)
1429 (c-x-set-key '(#\c :control) 'com-quit)
1430 (c-x-set-key '(#\f :control) 'com-find-file)
1431 (c-x-set-key '(#\k) 'com-kill-buffer)
1432 (c-x-set-key '(#\l :control) 'com-load-file)
1433 (c-x-set-key '(#\o) 'com-other-window)
1434 (c-x-set-key '(#\r) 'com-redo)
1435 (c-x-set-key '(#\u) 'com-undo)
1436 (c-x-set-key '(#\s :control) 'com-save-buffer)
1437 (c-x-set-key '(#\t :control) 'com-transpose-lines)
1438 (c-x-set-key '(#\w :control) 'com-write-buffer)
1439 (c-x-set-key '(#\x :control) 'com-exchange-point-and-mark)
1440
1441 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1442 ;;;
1443 ;;; Some Unicode stuff
1444
1445 (define-named-command com-insert-charcode ((code 'integer :prompt "Code point"))
1446 (insert-object (point (current-window)) (code-char code)))
1447
1448 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1449 ;;;
1450 ;;; Dead-acute command table
1451
1452 (make-command-table 'dead-acute-climacs-table :errorp nil)
1453
1454 (add-menu-item-to-command-table 'global-climacs-table "dead-acute"
1455 :menu 'dead-acute-climacs-table
1456 :keystroke '(:dead--acute))
1457
1458 (defun dead-acute-set-key (gesture command)
1459 (add-command-to-command-table command 'dead-acute-climacs-table
1460 :keystroke gesture :errorp nil))
1461
1462 (dead-acute-set-key '(#\A) '(com-insert-charcode 193))
1463 (dead-acute-set-key '(#\E) '(com-insert-charcode 201))
1464 (dead-acute-set-key '(#\I) '(com-insert-charcode 205))
1465 (dead-acute-set-key '(#\O) '(com-insert-charcode 211))
1466 (dead-acute-set-key '(#\U) '(com-insert-charcode 218))
1467 (dead-acute-set-key '(#\Y) '(com-insert-charcode 221))
1468 (dead-acute-set-key '(#\a) '(com-insert-charcode 225))
1469 (dead-acute-set-key '(#\e) '(com-insert-charcode 233))
1470 (dead-acute-set-key '(#\i) '(com-insert-charcode 237))
1471 (dead-acute-set-key '(#\o) '(com-insert-charcode 243))
1472 (dead-acute-set-key '(#\u) '(com-insert-charcode 250))
1473 (dead-acute-set-key '(#\y) '(com-insert-charcode 253))
1474 (dead-acute-set-key '(#\C) '(com-insert-charcode 199))
1475 (dead-acute-set-key '(#\c) '(com-insert-charcode 231))
1476 (dead-acute-set-key '(#\x) '(com-insert-charcode 215))
1477 (dead-acute-set-key '(#\-) '(com-insert-charcode 247))
1478 (dead-acute-set-key '(#\T) '(com-insert-charcode 222))
1479 (dead-acute-set-key '(#\t) '(com-insert-charcode 254))
1480 (dead-acute-set-key '(#\s) '(com-insert-charcode 223))
1481 (dead-acute-set-key '(#\Space) '(com-insert-charcode 39))
1482
1483 (make-command-table 'dead-acute-dead-accute-climacs-table :errorp nil)
1484
1485 (add-menu-item-to-command-table 'dead-acute-climacs-table "dead-acute-dead-accute"
1486 :menu 'dead-acute-dead-accute-climacs-table
1487 :keystroke '(:dead--acute))
1488
1489 (defun dead-acute-dead-accute-set-key (gesture command)
1490 (add-command-to-command-table command 'dead-acute-dead-accute-climacs-table
1491 :keystroke gesture :errorp nil))
1492
1493 (dead-acute-dead-accute-set-key '(#\A) '(com-insert-charcode 197))
1494 (dead-acute-dead-accute-set-key '(#\a) '(com-insert-charcode 229))
1495 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1496 ;;;
1497 ;;; Dead-grave command table
1498
1499 (make-command-table 'dead-grave-climacs-table :errorp nil)
1500
1501 (add-menu-item-to-command-table 'global-climacs-table "dead-grave"
1502 :menu 'dead-grave-climacs-table
1503 :keystroke '(:dead--grave))
1504
1505 (defun dead-grave-set-key (gesture command)
1506 (add-command-to-command-table command 'dead-grave-climacs-table
1507 :keystroke gesture :errorp nil))
1508
1509 (dead-grave-set-key '(#\A) '(com-insert-charcode 192))
1510 (dead-grave-set-key '(#\E) '(com-insert-charcode 200))
1511 (dead-grave-set-key '(#\I) '(com-insert-charcode 204))
1512 (dead-grave-set-key '(#\O) '(com-insert-charcode 210))
1513 (dead-grave-set-key '(#\U) '(com-insert-charcode 217))
1514 (dead-grave-set-key '(#\a) '(com-insert-charcode 224))
1515 (dead-grave-set-key '(#\e) '(com-insert-charcode 232))
1516 (dead-grave-set-key '(#\i) '(com-insert-charcode 236))
1517 (dead-grave-set-key '(#\o) '(com-insert-charcode 242))
1518 (dead-grave-set-key '(#\u) '(com-insert-charcode 249))
1519 (dead-grave-set-key '(#\Space) '(com-insert-charcode 96))
1520
1521 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1522 ;;;
1523 ;;; Dead-diaeresis command table
1524
1525 (make-command-table 'dead-diaeresis-climacs-table :errorp nil)
1526
1527 (add-menu-item-to-command-table 'global-climacs-table "dead-diaeresis"
1528 :menu 'dead-diaeresis-climacs-table
1529 :keystroke '(:dead--diaeresis :shift))
1530
1531 (defun dead-diaeresis-set-key (gesture command)
1532 (add-command-to-command-table command 'dead-diaeresis-climacs-table
1533 :keystroke gesture :errorp nil))
1534
1535 (dead-diaeresis-set-key '(#\A) '(com-insert-charcode 196))
1536 (dead-diaeresis-set-key '(#\E) '(com-insert-charcode 203))
1537 (dead-diaeresis-set-key '(#\I) '(com-insert-charcode 207))
1538 (dead-diaeresis-set-key '(#\O) '(com-insert-charcode 214))
1539 (dead-diaeresis-set-key '(#\U) '(com-insert-charcode 220))
1540 (dead-diaeresis-set-key '(#\a) '(com-insert-charcode 228))
1541 (dead-diaeresis-set-key '(#\e) '(com-insert-charcode 235))
1542 (dead-diaeresis-set-key '(#\i) '(com-insert-charcode 239))
1543 (dead-diaeresis-set-key '(#\o) '(com-insert-charcode 246))
1544 (dead-diaeresis-set-key '(#\u) '(com-insert-charcode 252))
1545 (dead-diaeresis-set-key '(#\y) '(com-insert-charcode 255))
1546 (dead-diaeresis-set-key '(#\Space) '(com-insert-charcode 34))
1547
1548 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1549 ;;;
1550 ;;; Dead-tilde command table
1551
1552 (make-command-table 'dead-tilde-climacs-table :errorp nil)
1553
1554 (add-menu-item-to-command-table 'global-climacs-table "dead-tilde"
1555 :menu 'dead-tilde-climacs-table
1556 :keystroke '(:dead--tilde :shift))
1557
1558 (defun dead-tilde-set-key (gesture command)
1559 (add-command-to-command-table command 'dead-tilde-climacs-table
1560 :keystroke gesture :errorp nil))
1561
1562 (dead-tilde-set-key '(#\A) '(com-insert-charcode 195))
1563 (dead-tilde-set-key '(#\N) '(com-insert-charcode 209))
1564 (dead-tilde-set-key '(#\a) '(com-insert-charcode 227))
1565 (dead-tilde-set-key '(#\n) '(com-insert-charcode 241))
1566 (dead-tilde-set-key '(#\E) '(com-insert-charcode 198))
1567 (dead-tilde-set-key '(#\e) '(com-insert-charcode 230))
1568 (dead-tilde-set-key '(#\D) '(com-insert-charcode 208))
1569 (dead-tilde-set-key '(#\d) '(com-insert-charcode 240))
1570 (dead-tilde-set-key '(#\O) '(com-insert-charcode 216))
1571 (dead-tilde-set-key '(#\o) '(com-insert-charcode 248))
1572 (dead-tilde-set-key '(#\Space) '(com-insert-charcode 126))
1573
1574 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1575 ;;;
1576 ;;; Dead-circumflex command table
1577
1578 (make-command-table 'dead-circumflex-climacs-table :errorp nil)
1579
1580 (add-menu-item-to-command-table 'global-climacs-table "dead-circumflex"
1581 :menu 'dead-circumflex-climacs-table
1582 :keystroke '(:dead--circumflex :shift))
1583
1584 (defun dead-circumflex-set-key (gesture command)
1585 (add-command-to-command-table command 'dead-circumflex-climacs-table
1586 :keystroke gesture :errorp nil))
1587
1588 (dead-circumflex-set-key '(#\A) '(com-insert-charcode 194))
1589 (dead-circumflex-set-key '(#\E) '(com-insert-charcode 202))
1590 (dead-circumflex-set-key '(#\I) '(com-insert-charcode 206))
1591 (dead-circumflex-set-key '(#\O) '(com-insert-charcode 212))
1592 (dead-circumflex-set-key '(#\U) '(com-insert-charcode 219))
1593 (dead-circumflex-set-key '(#\a) '(com-insert-charcode 226))
1594 (dead-circumflex-set-key '(#\e) '(com-insert-charcode 234))
1595 (dead-circumflex-set-key '(#\i) '(com-insert-charcode 238))
1596 (dead-circumflex-set-key '(#\o) '(com-insert-charcode 244))
1597 (dead-circumflex-set-key '(#\u) '(com-insert-charcode 251))
1598 (dead-circumflex-set-key '(#\Space) '(com-insert-charcode 94))
1599
1600 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1601 ;;;
1602 ;;; Isearch command table
1603
1604 (make-command-table 'isearch-climacs-table :errorp nil)
1605
1606 (defun isearch-set-key (gesture command)
1607 (add-command-to-command-table command 'isearch-climacs-table
1608 :keystroke gesture :errorp nil))
1609
1610 (loop for code from (char-code #\Space) to (char-code #\~)
1611 do (isearch-set-key (code-char code) 'com-isearch-append-char))
1612
1613 (isearch-set-key '(#\Newline) 'com-isearch-exit)
1614 (isearch-set-key '(#\Backspace) 'com-isearch-delete-char)
1615 (isearch-set-key '(#\s :control) 'com-isearch-forward)
1616 (isearch-set-key '(#\r :control) 'com-isearch-backward)
1617
1618 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1619 ;;;
1620 ;;; Query replace command table
1621
1622 (make-command-table 'query-replace-climacs-table :errorp nil)
1623
1624 (defun query-replace-set-key (gesture command)
1625 (add-command-to-command-table command 'query-replace-climacs-table
1626 :keystroke gesture :errorp nil))
1627
1628 (query-replace-set-key '(#\Newline) 'com-query-replace-exit)
1629 (query-replace-set-key '(#\Space) 'com-query-replace-replace)
1630 (query-replace-set-key '(#\Backspace) 'com-query-replace-skip)
1631 (query-replace-set-key '(#\Rubout) 'com-query-replace-skip)
1632 (query-replace-set-key '(#\q) 'com-query-replace-exit)
1633 (query-replace-set-key '(#\y) 'com-query-replace-replace)
1634 (query-replace-set-key '(#\n) 'com-query-replace-skip)

  ViewVC Help
Powered by ViewVC 1.1.5