misc-commands.lisp 28.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
;;; -*- Mode: Lisp; Package: CLIMACS-GUI -*-

;;;  (c) copyright 2004-2005 by
;;;           Robert Strandh (strandh@labri.fr)
;;;  (c) copyright 2004-2005 by
;;;           Elliott Johnson (ejohnson@fasl.info)
;;;  (c) copyright 2005 by
;;;           Matthieu Villeneuve (matthieu.villeneuve@free.fr)
;;;  (c) copyright 2005 by
;;;           Aleksandar Bakic (a_bakic@yahoo.com)

;;; This library is free software; you can redistribute it and/or
;;; modify it under the terms of the GNU Library General Public
;;; License as published by the Free Software Foundation; either
;;; version 2 of the License, or (at your option) any later version.
;;;
;;; This library is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; Library General Public License for more details.
;;;
;;; You should have received a copy of the GNU Library General Public
;;; License along with this library; if not, write to the
;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;;; Boston, MA  02111-1307  USA.

;;; miscellaneous commands for the Climacs editor. 

29
(in-package :climacs-commands)
30
31

(define-command (com-overwrite-mode :name t :command-table editing-table) ()
32
  "Toggle overwrite mode for the current mode.
David Murray's avatar
David Murray committed
33
34
35
36
When overwrite is on, an object entered on the keyboard 
will replace the object after the point. 
When overwrite is off (the default), objects are inserted at point. 
In both cases point is positioned after the new object."
37
38
39
40
41
42
43
44
  (with-slots (overwrite-mode) (current-window)
    (setf overwrite-mode (not overwrite-mode))))

(set-key 'com-overwrite-mode
	 'editing-table
	 '((:insert)))

(define-command (com-not-modified :name t :command-table buffer-table) ()
45
  "Clear the modified flag for the current buffer.
David Murray's avatar
David Murray committed
46
47
48
The modified flag is automatically set when the contents 
of the buffer are changed. This flag is consulted, for instance, 
when deciding whether to prompt you to save the buffer before killing it."
49
50
51
52
53
54
  (setf (needs-saving (buffer (current-window))) nil))

(set-key 'com-not-modified
	 'buffer-table
	 '((#\~ :meta :shift)))

55
56
57
58
59
(defun set-fill-column (column)
  (if (> column 1)
      (setf (auto-fill-column (current-window)) column)
      (progn (beep) (display-message "Set Fill Column requires an explicit argument."))))

60
61
(define-command (com-set-fill-column :name t :command-table fill-table)
    ((column 'integer :prompt "Column Number:"))
62
  "Set the fill column to the specified value.
David Murray's avatar
David Murray committed
63
64
65
66
You must supply a numeric argument. The fill column is 
the column beyond which automatic line-wrapping will occur. 

The default fill column is 70."
67
68
69
70
71
72
73
  (set-fill-column column))

(set-key `(com-set-fill-column ,*numeric-argument-marker*)
	 'fill-table
	 '((#\x :control) (#\f)))

(define-command (com-zap-to-object :name t :command-table deletion-table) ()
David Murray's avatar
David Murray committed
74
  "Prompt for an object and kill to the next occurence of that object after point.
75
Characters can be entered in #\ format."
76
77
78
79
80
81
82
83
84
85
86
  (let* ((item (handler-case (accept 't :prompt "Zap to Object")
		(error () (progn (beep)
				 (display-message "Not a valid object")
				 (return-from com-zap-to-object nil)))))
	 (current-point (point (current-window)))
	 (item-mark (clone-mark current-point))
	 (current-offset (offset current-point)))
    (search-forward item-mark (vector item))
    (delete-range current-point (- (offset item-mark) current-offset))))

(define-command (com-zap-to-character :name t :command-table deletion-table) ()
David Murray's avatar
David Murray committed
87
88
89
90
  "Prompt for a character and kill to the next occurence of that character after point.
FIXME: Accepts a string (that is, zero or more characters) 
terminated by a #\NEWLINE. If a zero length string signals an error. 
If a string of length >1, uses the first character of the string."
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
  (let* ((item-string (handler-case (accept 'string :prompt "Zap to Character") ; Figure out how to get #\d and d.  (or 'string 'character)?
		(error () (progn (beep)
				 (display-message "Not a valid string. ")
				 (return-from com-zap-to-character nil)))))
       (item (subseq item-string 0 1))
       (current-point (point (current-window)))
       (item-mark (clone-mark current-point))

       (current-offset (offset current-point)))
  (if (> (length item-string) 1)
      (display-message "Using just the first character"))
  (search-forward item-mark item)
  (delete-range current-point (- (offset item-mark) current-offset))))

(set-key 'com-zap-to-character
	 'deletion-table
	 '((#\z :meta)))

(define-command (com-open-line :name t :command-table editing-table)
    ((numarg 'integer :prompt "How many lines?"))
111
112
  "Insert a #\Newline and leave point before it.
With a numeric argument greater than 1, insert that many #\Newlines."
113
114
115
116
117
118
  (open-line (point (current-window)) numarg))

(set-key `(com-open-line ,*numeric-argument-marker*)
	 'editing-table
	 '((#\o :control)))

119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
(defmacro define-mark-unit-command (unit command-table &key
                                    move-point
                                    noun
                                    plural)
  "Define a COM-MARK-<UNIT> for `unit' command and put it in
  `command-table'."
  (labels ((symbol (&rest strings)
             (intern (apply #'concat strings)))
           (concat (&rest strings)
             (apply #'concatenate 'STRING (mapcar #'string strings))))
    (let ((forward (symbol "FORWARD-" unit))
          (backward (symbol "BACKWARD-" unit))
          (noun (or noun (string-downcase unit)))
          (plural (or plural (concat (string-downcase unit) "s"))))
      `(define-command (,(symbol "COM-MARK-" unit)
                         :name t
                         :command-table ,command-table)
           ((count 'integer :prompt ,(concat "Number of " plural)))
           ,(if (not (null move-point))
                (concat "Place point and mark around the current " noun ".
Put point at the beginning of the current " noun ", and mark at the end. 
With a positive numeric argument, put mark that many " plural " forward. 
With a negative numeric argument, put point at the end of the current 
" noun " and mark that many " plural " backward. 
Successive invocations extend the selection.")
                (concat "Place mark at the next " noun " end.
David Murray's avatar
David Murray committed
145
With a positive numeric argument, place mark at the end of 
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
that many " plural " forward. With a negative numeric argument, 
place mark at the beginning of that many " plural " backward. 

Successive invocations extend the selection."))
         (let* ((pane (current-window))
                (point (point pane))
                (mark (mark pane)))
           (unless (eq (previous-command pane) 'com-mark-word)
             (setf (offset mark) (offset point))
             ,(when (not (null move-point))
                    `(if (plusp count)
                         (,backward point (syntax (buffer pane)))
                         (,forward point (syntax (buffer pane))))))
           (,forward mark (syntax (buffer pane)) count))))))

(define-mark-unit-command word marking-table)
(define-mark-unit-command expression marking-table)
(define-mark-unit-command paragraph marking-table :move-point t)
(define-mark-unit-command definition marking-table :move-point t)
165
166
167
168
169

(set-key `(com-mark-word ,*numeric-argument-marker*)
	 'marking-table
	 '((#\@ :meta :shift)))

170
171
172
173
174
175
176
(set-key `(com-mark-paragraph ,*numeric-argument-marker*)
	 'marking-table
	 '((#\h :meta)))

(set-key 'com-mark-definition
	 'marking-table
	 '((#\h :control :meta)))
177
178

(define-command (com-upcase-region :name t :command-table case-table) ()
179
  "Convert the region to upper case."
180
181
182
183
  (let ((cw (current-window)))
    (upcase-region (mark cw) (point cw))))

(define-command (com-downcase-region :name t :command-table case-table) ()
184
  "Convert the region to lower case."
185
186
187
188
  (let ((cw (current-window)))
    (downcase-region (mark cw) (point cw))))

(define-command (com-capitalize-region :name t :command-table case-table) ()
189
  "Capitalize each word in the region."
190
191
192
193
  (let ((cw (current-window)))
    (capitalize-region (mark cw) (point cw))))

(define-command (com-upcase-word :name t :command-table case-table) ()
194
195
  "Convert the characters from point until the next word end to upper case.
Leave point at the word end."
196
197
  (upcase-word (point (current-window))
               (syntax (buffer (current-window)))))
198
199
200
201
202
203

(set-key 'com-upcase-word
	 'case-table
	 '((#\u :meta)))

(define-command (com-downcase-word :name t :command-table case-table) ()
204
205
  "Convert the characters from point until the next word end to lower case.
Leave point at the word end."
Troels Henriksen's avatar
Troels Henriksen committed
206
207
  (downcase-word (point (current-window))
                 (syntax (buffer (current-window)))))
208
209
210
211
212
213

(set-key 'com-downcase-word
	 'case-table
	 '((#\l :meta)))

(define-command (com-capitalize-word :name t :command-table case-table) ()
214
  "Capitalize the next word.
David Murray's avatar
David Murray committed
215
216
217
218
219
220
If point is in a word, convert the next character to 
upper case and the remaining letters in the word to lower case. 
If point is before the start of a word, convert the first character 
of that word to upper case and the rest of the letters to lower case. 

Leave point at the word end."
Troels Henriksen's avatar
Troels Henriksen committed
221
222
  (capitalize-word (point (current-window))
                   (syntax (buffer (current-window)))))
223
224
225
226
227
228

(set-key 'com-capitalize-word
	 'case-table
	 '((#\c :meta)))

(define-command (com-tabify-region :name t :command-table editing-table) ()
229
230
  "Replace runs of spaces with tabs in region where possible.
Uses TAB-SPACE-COUNT of the STREAM-DEFAULT-VIEW of the pane."
231
232
233
234
235
  (let ((pane (current-window)))
    (tabify-region
     (mark pane) (point pane) (tab-space-count (stream-default-view pane)))))

(define-command (com-untabify-region :name t :command-table editing-table) ()
236
237
  "Replace tabs with equivalent runs of spaces in the region.
Uses TAB-SPACE-COUNT of the STREAM-DEFAULT-VIEW of the pane."
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
  (let ((pane (current-window)))
    (untabify-region
     (mark pane) (point pane) (tab-space-count (stream-default-view pane)))))

(define-command (com-indent-line :name t :command-table indent-table) ()
  (let* ((pane (current-window))
         (point (point pane)))
    (indent-current-line pane point)))

(set-key 'com-indent-line
	 'indent-table
	 '((#\Tab)))

(set-key 'com-indent-line
	 'indent-table
	 '((#\i :control)))

(define-command (com-newline-and-indent :name t :command-table indent-table) ()
256
  "Inserts a newline and indents the new line."
257
258
259
  (let* ((pane (current-window))
	 (point (point pane)))
    (insert-object point #\Newline)
260
261
    (update-syntax (current-buffer *application-frame*)
                   (syntax (current-buffer *application-frame*)))
262
263
264
265
266
267
    (indent-current-line pane point)))

(set-key 'com-newline-and-indent
	 'indent-table
	 '((#\j :control)))

268
269
270
271
272
273
274
275
(define-command (com-indent-region :name t :command-table indent-table) ()
  "Indent every line of the current region as specified by the
syntax for the buffer."
  (let* ((pane (current-window))
         (point (point pane))
         (mark (mark pane)))
    (indent-region pane point mark)))

276
(define-command (com-delete-indentation :name t :command-table indent-table) ()
277
  "Join current line to previous non-blank line.
David Murray's avatar
David Murray committed
278
279
280
281
282
Leaves a single space between the last non-whitespace object 
of the previous line and the first non-whitespace object of 
the current line, and point after that space. If there is no 
previous non-blank line, deletes all whitespace at the 
beginning of the buffer at leaves point there."
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
  (delete-indentation (point (current-window))))

(set-key 'com-delete-indentation
	 'indent-table
	 '((#\^ :shift :meta)))

(define-command (com-auto-fill-mode :name t :command-table fill-table) ()
  (let ((pane (current-window)))
    (setf (auto-fill-mode pane) (not (auto-fill-mode pane)))))

(define-command (com-fill-paragraph :name t :command-table fill-table) ()
  (let* ((pane (current-window))
         (buffer (buffer pane))
         (syntax (syntax buffer))
	 (point (point pane))
         (begin-mark (clone-mark point))
         (end-mark (clone-mark point)))
    (unless (eql (object-before begin-mark) #\Newline)
      (backward-paragraph begin-mark syntax))
    (unless (eql (object-after end-mark) #\Newline)
      (forward-paragraph end-mark syntax))
    (do-buffer-region (object offset buffer
                       (offset begin-mark) (offset end-mark))
      (when (eql object #\Newline)
        (setf object #\Space)))
    (let ((point-backup (clone-mark point)))
      (setf (offset point) (offset end-mark))
      (possibly-fill-line)
      (setf (offset point) (offset point-backup)))))

(set-key 'com-fill-paragraph
	 'fill-table
	 '((#\q :meta)))

(define-command (com-beginning-of-buffer :name t :command-table movement-table) ()
318
  "Move point to the beginning of the buffer."
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
  (beginning-of-buffer (point (current-window))))

(set-key 'com-beginning-of-buffer
	 'movement-table
	 '((#\< :shift :meta)))

(set-key 'com-beginning-of-buffer
	 'movement-table
	 '((:home :control)))

(define-command (com-page-down :name t :command-table movement-table) ()
  (let ((pane (current-window)))
    (page-down pane)))

(set-key 'com-page-down
	 'movement-table
	 '((#\v :control)))

(set-key 'com-page-down
	 'movement-table
	 '((:next)))

(define-command (com-page-up :name t :command-table movement-table) ()
  (let ((pane (current-window)))
    (page-up pane)))

(set-key 'com-page-up
	 'movement-table
	 '((#\v :meta)))

(set-key 'com-page-up
	 'movement-table
	 '((:prior)))

(define-command (com-end-of-buffer :name t :command-table movement-table) ()
354
  "Move point to the end of the buffer."
355
356
357
358
359
360
361
362
363
364
365
  (end-of-buffer (point (current-window))))

(set-key 'com-end-of-buffer
	 'movement-table
	 '((#\> :shift :meta)))

(set-key 'com-end-of-buffer
	 'movement-table
	 '((:end :control)))

(define-command (com-mark-whole-buffer :name t :command-table marking-table) ()
366
  "Place point at the beginning and mark at the end of the buffer."
367
368
369
370
371
372
373
374
  (beginning-of-buffer (point (current-window)))
  (end-of-buffer (mark (current-window))))

(set-key 'com-mark-whole-buffer
	 'marking-table
	 '((#\x :control) (#\h)))

(define-command (com-back-to-indentation :name t :command-table movement-table) ()
375
376
  "Move point to the first non-whitespace object on the current line.
If there is no non-whitespace object, leaves point at the end of the line."
377
378
  (back-to-indentation (point (current-window))
                       (syntax (buffer (current-window)))))
379
380
381
382
383
384
385
386

(set-key 'com-back-to-indentation
	 'movement-table
	 '((#\m :meta)))

(define-command (com-delete-horizontal-space :name t :command-table deletion-table)
    ((backward-only-p
      'boolean :prompt "Delete backwards only?"))
387
388
  "Delete whitespace around point.
With a numeric argument, only delete whitespace before point."
389
390
391
  (delete-horizontal-space (point (current-window))
                           (syntax (buffer (current-window)))
                           backward-only-p))
392
393
394
395
396
397
398

(set-key `(com-delete-horizontal-space ,*numeric-argument-p*)
	 'deletion-table
	 '((#\\ :meta)))

(define-command (com-just-one-space :name t :command-table deletion-table)
    ((count 'integer :prompt "Number of spaces"))
399
400
  "Delete whitespace around point, leaving a single space.
With a positive numeric argument, leave that many spaces.
David Murray's avatar
David Murray committed
401

402
FIXME: should distinguish between types of whitespace."
403
404
  (just-n-spaces (point (current-window))
                 count))
405
406
407
408
409

(set-key `(com-just-one-space ,*numeric-argument-marker*)
	 'deletion-table
	 '((#\Space :meta)))

410
411
(define-command (com-goto-position :name t :command-table movement-table) 
    ((position 'integer :prompt "Goto Position"))
412
  "Prompts for an integer, and sets the offset of point to that integer."
413
414
  (goto-position
   (point (current-window))
415
   position))  
416

417
418
(define-command (com-goto-line :name t :command-table movement-table) 
    ((line-number 'integer :prompt "Goto Line"))
419
  "Prompts for a line number, and sets point to the beginning of that line.
David Murray's avatar
David Murray committed
420
421
422
423
The first line of the buffer is 1. Giving a number <1 leaves 
point at the beginning of the buffer. Giving a line number 
larger than the number of the last line in the buffer leaves 
point at the beginning of the last line of the buffer."
424
425
426
427
428
429
430
431
432
  (goto-line (point (current-window)) line-number))

(define-command (com-browse-url :name t :command-table base-table) 
    ((url 'url :prompt "Browse URL"))
  (declare (ignorable url))
  #+ (and sbcl darwin)
     (sb-ext:run-program "/usr/bin/open" `(,url) :wait nil)
  #+ (and openmcl darwin)
     (ccl:run-program "/usr/bin/open" `(,url) :wait nil))
433
434

(define-command (com-set-mark :name t :command-table marking-table) ()
435
  "Set mark to the current position of point."
436
437
438
439
440
441
442
443
  (let ((pane (current-window)))
    (setf (mark pane) (clone-mark (point pane)))))

(set-key 'com-set-mark
	 'marking-table
	 '((#\Space :control)))

(define-command (com-exchange-point-and-mark :name t :command-table marking-table) ()
444
  "Exchange the positions of point and mark."
445
446
447
448
449
450
451
452
  (let ((pane (current-window)))
    (psetf (offset (mark pane)) (offset (point pane))
	   (offset (point pane)) (offset (mark pane)))))

(set-key 'com-exchange-point-and-mark
	 'marking-table
	 '((#\x :control) (#\x :control)))

453
454
455
(define-command (com-set-syntax :name t :command-table buffer-table) 
    ((syntax 'syntax
      :prompt "Name of syntax"))
456
  "Prompts for a syntax to set for the current buffer.
457
   Setting a syntax will cause the buffer to be reparsed using the new syntax."
458
  (set-syntax (current-buffer *application-frame*) syntax))
459
460
461
462
463
464

;;;;;;;;;;;;;;;;;;;;
;; Kill ring commands

;; Copies an element from a kill-ring to a buffer at the given offset
(define-command (com-yank :name t :command-table editing-table) ()
465
  "Insert the objects most recently added to the kill ring at point."
466
  (handler-case (insert-sequence (point (current-window)) (kill-ring-yank *kill-ring*))
467
    (empty-kill-ring ()
468
      (display-message "Kill ring is empty"))))
469
470
471
472
473
474
475

(set-key 'com-yank
	 'editing-table
	 '((#\y :control)))

;; Destructively cut a given buffer region into the kill-ring
(define-command (com-kill-region :name t :command-table editing-table) ()
476
477
  "Kill the objects between point and mark.
That is, push them onto the kill ring, and delete them from the buffer."
478
479
480
481
482
483
484
485
486
487
488
  (let ((pane (current-window)))
    (kill-ring-standard-push
     *kill-ring* (region-to-sequence (mark pane) (point pane)))
    (delete-region (mark pane) (point pane))))

(set-key 'com-kill-region
	 'editing-table
	 '((#\w :control)))

;; Non destructively copies buffer region to the kill ring
(define-command (com-copy-region :name t :command-table marking-table) ()
489
  "Copy the objects between point and mark to the kill ring."
490
491
492
493
494
495
496
497
  (let ((pane (current-window)))
    (kill-ring-standard-push *kill-ring* (region-to-sequence (point pane) (mark pane)))))

(set-key 'com-copy-region
	 'marking-table
	 '((#\w :meta)))

(define-command (com-rotate-yank :name t :command-table editing-table) ()
498
  "Replace the immediately previously yanked objects with others.
David Murray's avatar
David Murray committed
499
500
501
Must be given immediately following a Yank or Rotate Yank command. 
The replacement objects are those before the previously yanked 
objects in the kill ring."
502
503
504
505
506
507
508
509
510
  (handler-case (let* ((pane (current-window))
                       (point (point pane))
                       (last-yank (kill-ring-yank *kill-ring*)))
                  (if (eq (previous-command pane)
                          'com-rotate-yank)
                      (progn
                        (delete-range point (* -1 (length last-yank)))
                        (rotate-yank-position *kill-ring*)))
                  (insert-sequence point (kill-ring-yank *kill-ring*)))
511
    (empty-kill-ring ()
512
      (display-message "Kill ring is empty"))))
513
514
515
516
517

(set-key 'com-rotate-yank
	 'editing-table
	 '((#\y :meta)))

518
519
(define-command (com-resize-kill-ring :name t :command-table editing-table) 
    ((size 'integer :prompt "New kill ring size"))
520
521
  "Prompt for a new size for the kill ring.
The default is 5. A number less than 5 will be replaced by 5."
522
     (setf (kill-ring-max-size *kill-ring*) size))
523
524

(define-command (com-append-next-kill :name t :command-table editing-table) ()
525
  "Set the kill ring to append the next kill to the previous one."
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
  (setf (append-next-p *kill-ring*) t))

(set-key 'com-append-next-kill
	 'editing-table
	 '((#\w :control :meta)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; 
;;; Undo/redo

(define-command (com-undo :name t :command-table editing-table) ()
  (handler-case (undo (undo-tree (buffer (current-window))))
    (no-more-undo () (beep) (display-message "No more undo")))
  (full-redisplay (current-window)))

(set-key 'com-undo
	 'editing-table
	 '((#\_ :shift :control)))

(set-key 'com-undo
	 'editing-table
	 '((#\x :control) (#\u)))

(define-command (com-redo :name t :command-table editing-table) ()
  (handler-case (redo (undo-tree (buffer (current-window))))
    (no-more-undo () (beep) (display-message "No more redo")))
  (full-redisplay (current-window)))

(set-key 'com-redo
	 'editing-table
	 '((#\_ :shift :meta)))

(set-key 'com-redo
	 'editing-table
	 '((#\x :control) (#\r :control)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; 
;;; Dynamic abbrevs

(define-command (com-dabbrev-expand :name t :command-table editing-table) ()
567
  "Expand word before point dynamically.
David Murray's avatar
David Murray committed
568
569
570
Search from point (first backward to the beginning of the buffer, 
then forward) for words for which the word before point is a prefix, 
inserting each in turn at point as an expansion."
571
  (let* ((window (current-window))
572
573
	 (point (point window))
         (syntax (syntax (buffer window))))
574
575
576
    (with-accessors ((original-prefix original-prefix)
                     (prefix-start-offset prefix-start-offset)
                     (dabbrev-expansion-mark dabbrev-expansion-mark)) window
577
578
579
       (flet ((move () (cond ((beginning-of-buffer-p dabbrev-expansion-mark)
			      (setf (offset dabbrev-expansion-mark)
				    (offset point))
580
			      (forward-word dabbrev-expansion-mark syntax))
581
582
583
584
585
586
587
588
			     ((mark< dabbrev-expansion-mark point)
			      (backward-object dabbrev-expansion-mark))
			     (t (forward-object dabbrev-expansion-mark)))))
	 (unless (or (beginning-of-buffer-p point)
		     (not (constituentp (object-before point))))
	   (unless (and (eq (previous-command window) 'com-dabbrev-expand)
			(not (null prefix-start-offset)))
	     (setf dabbrev-expansion-mark (clone-mark point))
589
	     (backward-word dabbrev-expansion-mark syntax)
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
	     (setf prefix-start-offset (offset dabbrev-expansion-mark))
	     (setf original-prefix (region-to-sequence prefix-start-offset point))
	     (move))
	   (loop until (or (end-of-buffer-p dabbrev-expansion-mark)
			   (and (or (beginning-of-buffer-p dabbrev-expansion-mark)
				    (not (constituentp (object-before dabbrev-expansion-mark))))
				(looking-at dabbrev-expansion-mark original-prefix)))
		 do (move))
	   (if (end-of-buffer-p dabbrev-expansion-mark)
	       (progn (delete-region prefix-start-offset point)
		      (insert-sequence point original-prefix)
		      (setf prefix-start-offset nil))
	       (progn (delete-region prefix-start-offset point)
		      (insert-sequence point
				       (let ((offset (offset dabbrev-expansion-mark)))
605
					 (prog2 (forward-word dabbrev-expansion-mark syntax)
606
607
608
609
610
611
612
613
614
615
616
						(region-to-sequence offset dabbrev-expansion-mark)
						(setf (offset dabbrev-expansion-mark) offset))))
		      (move))))))))

(set-key 'com-dabbrev-expand
	 'editing-table
	 '((#\/ :meta)))

(define-command (com-mark-page :name t :command-table marking-table)
    ((count 'integer :prompt "Move how many pages")
     (numargp 'boolean :prompt "Move to another page?"))
617
  "Place point and mark around the current page.
David Murray's avatar
David Murray committed
618
619
620
621
622
623
With a numeric argument, move point that many 
pages forward (backward if negative) before marking the 
surrounding page. When no page delimeters are found, 
leave point at the beginning and mark at the end of the buffer. 

A page is delimited by the sequence #\Newline #\Page."
624
  (let* ((pane (current-window))
625
626
	 (syntax (syntax (buffer pane)))
         (point (point pane))
627
628
629
	 (mark (mark pane)))
    (cond ((and numargp (/= 0 count))
	   (if (plusp count)
630
631
632
	       (forward-page point syntax count)
	       (backward-page point syntax (1+ count))))
	  (t (backward-page point syntax count)))
633
    (setf (offset mark) (offset point))
634
    (forward-page mark syntax 1)))
635
636
637
638
639
640

(set-key `(com-mark-page ,*numeric-argument-marker* ,*numeric-argument-p*)
	 'marking-table
	 '((#\x :control) (#\p :control)))

(define-command (com-count-lines-page :name t :command-table info-table) ()
641
  "Print the number of lines in the current page.
642
Also prints the number of lines before and after point (as '(b + a)')."
643
  (let* ((pane (current-window))
644
         (syntax (syntax (buffer pane)))
645
646
647
	 (point (point pane))
	 (start (clone-mark point))
	 (end (clone-mark point)))
648
649
    (backward-page start syntax)
    (forward-page end syntax)
650
651
652
    (let ((total (number-of-lines-in-region start end))
	  (before (number-of-lines-in-region start point))
	  (after (number-of-lines-in-region point end)))
653
      (display-message "Page has ~A lines (~A + ~A)" (1+ total) before after))))
654
655
656
657
658
659

(set-key 'com-count-lines-page
	 'info-table
	 '((#\x :control) (#\l)))

(define-command (com-count-lines-region :name t :command-table info-table) ()
660
  "Print the number of lines in the region.
661
Also prints the number of objects (as 'o character[s]')."
662
663
664
665
666
  (let*  ((pane (current-window))
	  (point (point pane))
	  (mark (mark pane))
	  (lines (number-of-lines-in-region point mark))
	  (chars (abs (- (offset point) (offset mark)))))
667
    (display-message "Region has ~D line~:P, ~D character~:P." (1+ lines) chars)))
668
669
670
671
672
673

(set-key 'com-count-lines-region
	 'info-table
	 '((#\= :meta)))

(define-command (com-what-cursor-position :name t :command-table info-table) ()
674
  "Print information about point.
David Murray's avatar
David Murray committed
675
676
677
678
Gives the character after point (name and octal, decimal and hexidecimal charcode), 
the offset of point, the total objects in the buffer, 
and the percentage of the buffers objects before point.

679
FIXME: gives no information at end of buffer."
680
681
682
683
684
  (let* ((pane (current-window))
	 (point (point pane))
	 (buffer (buffer pane))
	 (offset (offset point))
	 (size (size buffer))
685
	 (char (or (end-of-buffer-p point) (object-after point)))
686
	 (column (column-number point)))
687
688
689
690
691
692
    (display-message "Char: ~:[none~*~;~:*~:C (#o~O ~:*~D ~:*#x~X)~] point=~D of ~D (~D%) column ~D"
		     (and (characterp char) char)
		     (and (characterp char) (char-code char))
		     offset size
		     (if size (round (* 100 (/ offset size))) 100)
		     column)))
693
694
695
696
697
698

(set-key 'com-what-cursor-position
	 'info-table
	 '((#\x :control) (#\=)))

(define-command (com-eval-expression :name t :command-table base-table)
699
700
    ((exp 'expression :prompt "Eval")
     (insertp 'boolean :prompt "Insert?"))
701
  "Prompt for and evaluate a lisp expression.
David Murray's avatar
David Murray committed
702
703
With a numeric argument inserts the result at point as a string; 
otherwise prints the result."
704
  (let* ((*package* (find-package :climacs-gui))
705
706
         (values (multiple-value-list
		  (handler-case (eval exp)
707
708
709
710
711
712
713
714
		    (error (condition) (progn (beep)
					      (display-message "~a" condition)
					      (return-from com-eval-expression nil))))))
	 (result (format nil "~:[; No values~;~:*~{~S~^,~}~]" values)))
    (if insertp
	(insert-sequence (point (current-window)) result)
	(display-message result))))

715
(set-key `(com-eval-expression ,*unsupplied-argument-marker* ,*numeric-argument-p*)
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
	 'base-table
	 '((#\: :shift :meta)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; 
;;; Commenting

;;; figure out how to make commands without key bindings accept numeric arguments. 
(define-command (com-comment-region :name t :command-table comment-table) ()
  (let* ((pane (current-window))
	 (point (point pane))
	 (mark (mark pane))
	 (syntax (syntax (buffer pane))))
    (comment-region syntax point mark)))

;; (defparameter *insert-pair-alist*
;; 	      '((#\( #\)) (#\[ #\]) (#\{ #\}) (#\< #\>) (#\" #\") (#\' #\') (#\` #\')))

(defun insert-parentheses (mark syntax count)
  (insert-pair mark syntax count #\( #\)))

(define-command (com-insert-parentheses :name t :command-table editing-table)
    ((count 'integer :prompt "Number of expressions")
     (wrap-p 'boolean :prompt "Wrap expressions?"))
740
  "Insert a pair of parentheses, leaving point in between.
David Murray's avatar
David Murray committed
741
742
743
With a numeric argument, enclose that many expressions 
forward (backward if negative).

744
FIXME: no it doesn't."
745
746
747
748
749
750
751
752
753
754
  (let* ((pane (current-window))
	 (point (point pane))
	 (syntax (syntax (buffer pane))))
    (unless wrap-p (setf count 0))
    (insert-parentheses point syntax count)))

(set-key `(com-insert-parentheses ,*numeric-argument-marker* ,*numeric-argument-p*)
	 'editing-table
	 '((#\( :meta)))

755
756
757
(define-command (com-visible-region :name t :command-table marking-table) ()
  "Toggle the visibility of the region in the current pane."
  (setf (region-visible-p (current-window)) (not (region-visible-p (current-window)))))