source: trunk/source/cocoa-ide/hemlock/src/listener.lisp @ 11894

Last change on this file since 11894 was 11894, checked in by rme, 11 years ago

Editor Evaluate Region: if no region is active, and point is not
before an open paren or after a close paren, try to evaluate the
thing that point is before/in/after.

It can choke if point is within or after something like #\c or
#&NSFontAttributeName (although if point is before such a thing, it
works).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 27.6 KB
Line 
1;;; -*- Log: hemlock.log; Package: Hemlock -*-
2;;;
3;;; **********************************************************************
4;;; This code was written as part of the CMU Common Lisp project at
5;;; Carnegie Mellon University, and has been placed in the public domain.
6;;;
7;;;
8;;; **********************************************************************
9;;;
10;;; Listener mode, dervived (sort of) from Hemlock's "Eval" mode.
11;;;
12
13(in-package :hemlock)
14
15
16(defmacro in-lisp (&body body)
17  "Evaluates body inside HANDLE-LISP-ERRORS.  *package* is bound to the package
18   named by \"Current Package\" if it is non-nil."
19  (let ((name (gensym)) (package (gensym)))
20    `(handle-lisp-errors
21      (let* ((,name (variable-value 'current-package :buffer (current-buffer)))
22             (,package (and ,name (find-package ,name))))
23        (progv (if ,package '(*package*)) (if ,package (list ,package))
24          ,@body)))))
25
26
27(defun package-name-change-hook (name kind where new-value)
28  (declare (ignore name new-value))
29  (if (eq kind :buffer)
30    (hi::note-modeline-change where)))
31
32(define-file-option "Package" (buffer value)
33  (defhvar "Current Package"
34    "The package used for evaluation of Lisp in this buffer."
35    :buffer buffer
36    :value
37    (let ((thing (handler-case (read-from-string value t)
38                   (error () (editor-error "Bad package file option value")))))
39      (cond
40       ((or (stringp thing) (symbolp thing))
41        (string thing))
42       ((and (consp thing) ;; e.g. Package: (foo :use bar)
43             (or (stringp (car thing)) (symbolp (car thing))))
44        (string (car thing)))
45       (t
46        (message "Ignoring \"package:\" file option ~a" thing)
47        nil)))
48    :hooks (list 'package-name-change-hook)))
49
50
51;;;; Listener Mode Interaction.
52
53
54
55(defun setup-listener-mode (buffer)
56  (let ((point (buffer-point buffer)))
57    (setf (buffer-minor-mode buffer "Listener") t)
58    (setf (buffer-minor-mode buffer "Editor") t)
59    (setf (buffer-major-mode buffer) "Lisp")
60    (buffer-end point)
61    (defhvar "Current Package"
62      "This variable holds the name of the package currently used for Lisp
63       evaluation and compilation.  If it is Nil, the value of *Package* is used
64       instead."
65      :value nil
66      :buffer buffer)
67    (unless (hemlock-bound-p 'buffer-input-mark :buffer buffer)
68      (defhvar "Buffer Input Mark"
69        "Mark used for Listener Mode input."
70        :buffer buffer
71        :value (copy-mark point :right-inserting))
72      (defhvar "Buffer Output Mark"
73        "Mark used for Listener Mode output."
74        :buffer buffer
75        :value (copy-mark point :left-inserting))
76      (defhvar "Interactive History"
77        "A ring of the regions input to an interactive mode (Eval or Typescript)."
78        :buffer buffer
79        :value (make-ring (value interactive-history-length)))
80      (defhvar "Interactive Pointer"
81        "Pointer into \"Interactive History\"."
82        :buffer buffer
83        :value 0)
84      (defhvar "Searching Interactive Pointer"
85        "Pointer into \"Interactive History\"."
86        :buffer buffer
87        :value 0)
88      (defhvar "Input Regions"
89        "Input region history list."
90        :buffer buffer
91        :value nil)
92      (defhvar "Current Input Font Region"
93          "Current font region, for listener input"
94        :buffer buffer
95        :value nil)
96      (defhvar "Current Output Font Region"
97          "Current font region, for listener output"
98        :buffer buffer
99        :value nil)
100      )
101    (let* ((input-mark (variable-value 'buffer-input-mark :buffer buffer)))
102      (when (hemlock-ext:read-only-listener-p)
103        (setf (hi::buffer-protected-region buffer)
104              (region (buffer-start-mark buffer) input-mark)))
105      (move-mark input-mark point)
106      (append-font-regions buffer))))
107
108(defmode "Listener" :major-p nil :setup-function #'setup-listener-mode)
109
110(declaim (special hi::*listener-input-style* hi::*listener-output-style*))
111
112(defun append-font-regions (buffer)
113  (let* ((end (region-end (buffer-region buffer))))
114    (setf (variable-value 'current-output-font-region :buffer buffer)
115          (hi::new-font-region buffer end end hi::*listener-output-style*))
116    (let* ((input (hi::new-font-region buffer end end hi::*listener-input-style*)))
117      (hi::activate-buffer-font-region buffer input)
118      (setf (variable-value 'current-input-font-region :buffer buffer) input))))
119
120(defun append-buffer-output (buffer string)
121  (let* ((output-region (variable-value 'current-output-font-region
122                                        :buffer buffer))
123         (output-end (region-end output-region)))
124    (hi::with-active-font-region (buffer output-region)
125      (with-mark ((output-mark output-end :left-inserting))
126        ;(setf (mark-charpos output-mark) 0)
127        (insert-string output-mark string))
128      (move-mark (variable-value 'buffer-input-mark :buffer buffer)
129                 output-end))))
130
131
132
133(defparameter *listener-modeline-fields*
134  (list (modeline-field :package)
135        (modeline-field :modes)
136        (modeline-field :process-info)))
137 
138(defun listener-mode-lisp-mode-hook (buffer on)
139  "Turn on Lisp mode when we go into Listener Mode."
140  (when on
141    (setf (buffer-major-mode buffer) "Lisp")))
142;;;
143(add-hook listener-mode-hook 'listener-mode-lisp-mode-hook)
144
145
146
147
148
149(defvar lispbuf-eof '(nil))
150
151(defun balanced-expressions-in-region (region)
152  "Return true if there's at least one syntactically well-formed S-expression
153between the region's start and end, and if there are no ill-formed expressions in that region."
154  ;; It helps to know that END-MARK immediately follows a #\newline.
155  (let* ((start-mark (region-start region))
156         (end-mark (region-end region))
157         (end-line (mark-line end-mark))
158         (end-charpos (mark-charpos end-mark)))
159    (with-mark ((m start-mark))
160      (pre-command-parse-check m)
161      (when (form-offset m 1)
162        (let* ((skip-whitespace t))
163          (loop
164            (let* ((current-line (mark-line m))
165                   (current-charpos (mark-charpos m)))
166              (when (and (eq current-line end-line)
167                         (eql current-charpos end-charpos))
168                (return t))
169              (if skip-whitespace
170                (progn
171                  (scan-char m :whitespace nil)
172                  (setq skip-whitespace nil))
173                (progn
174                  (pre-command-parse-check m)
175                  (unless (form-offset m 1)
176                    (return nil))
177                  (setq skip-whitespace t))))))))))
178               
179           
180 
181(defcommand "Confirm Listener Input" (p)
182  "Evaluate Listener Mode input between point and last prompt."
183  "Evaluate Listener Mode input between point and last prompt."
184  (declare (ignore p))
185  (let* ((input-region (get-interactive-input))
186         (r (if input-region
187              (region (copy-mark (region-start input-region))
188                      (copy-mark (region-end input-region) :right-inserting)))))
189
190    (when input-region
191      (insert-character (current-point) #\NewLine)
192      (when (balanced-expressions-in-region input-region)
193        (let* ((string (region-to-string input-region))               )
194          (push (cons r nil) (value input-regions))
195          (move-mark (value buffer-input-mark) (current-point))
196          (append-font-regions (current-buffer))
197          (hemlock-ext:send-string-to-listener (current-buffer) string))))))
198
199(defparameter *pop-string* ":POP
200" "what you have to type to exit a break loop")
201
202(defcommand "POP or Delete Forward" (p)
203  "Send :POP if input-mark is at buffer's end, else delete forward character."
204  "Send :POP if input-mark is at buffer's end, else delete forward character."
205  (let* ((input-mark (value buffer-input-mark))
206         (point (current-point-for-deletion)))
207    (when point
208      (if (and (null (next-character point))
209               (null (next-character input-mark)))
210        (hemlock-ext:send-string-to-listener (current-buffer) *pop-string*)
211        (delete-next-character-command p)))))
212
213
214;;;; General interactive commands used in eval and typescript buffers.
215
216(defhvar "Interactive History Length"
217  "This is the length used for the history ring in interactive buffers.
218   It must be set before turning on the mode."
219  :value 10)
220
221(defun input-region-containing-mark (m history-list)
222  (dolist (pair history-list)
223    (let* ((actual (car pair))
224           (start (region-start actual))
225           (end (region-end actual)))
226      (when (and (mark>= m start)
227                 (mark<= m end))        ; sic: inclusive
228        (return (or (cdr pair) (setf (cdr pair) (copy-region actual))))))))
229
230
231(defun get-interactive-input ()
232  "Tries to return a region.  When the point is not past the input mark, and
233   the user has \"Unwedge Interactive Input Confirm\" set, the buffer is
234   optionally fixed up, and nil is returned.  Otherwise, an editor error is
235   signalled.  When a region is returned, the start is the current buffer's
236   input mark, and the end is the current point moved to the end of the buffer."
237  (let ((point (current-point))
238        (mark (value buffer-input-mark)))
239    (cond
240     ((mark>= point mark)
241      (buffer-end point)
242      (let* ((input-region (region mark point))
243             (string (region-to-string input-region))
244             (ring (value interactive-history)))
245        (when (and (or (zerop (ring-length ring))
246                       (string/= string (region-to-string (ring-ref ring 0))))
247                   (> (length string) (value minimum-interactive-input-length)))
248          (ring-push (copy-region input-region) ring))
249        input-region))
250     (t
251      (let* ((region (input-region-containing-mark point (value input-regions ))))
252        (buffer-end point)
253        (if region
254          (progn
255            (delete-region (region mark point))
256            (insert-region point region))
257          (beep))
258        nil)))))
259
260
261(defhvar "Minimum Interactive Input Length"
262  "When the number of characters in an interactive buffer exceeds this value,
263   it is pushed onto the interactive history, otherwise it is lost forever."
264  :value 2)
265
266
267(defvar *previous-input-search-string* "ignore")
268
269(defvar *previous-input-search-pattern*
270  ;; Give it a bogus string since you can't give it the empty string.
271  (new-search-pattern :string-insensitive :forward "ignore"))
272
273(defun get-previous-input-search-pattern (string)
274  (if (string= *previous-input-search-string* string)
275      *previous-input-search-pattern*
276      (new-search-pattern :string-insensitive :forward 
277                          (setf *previous-input-search-string* string)
278                          *previous-input-search-pattern*)))
279
280(defcommand "Search Previous Interactive Input" (p)
281  "Search backward through the interactive history using the current input as
282   a search string.  Consecutive invocations repeat the previous search."
283  "Search backward through the interactive history using the current input as
284   a search string.  Consecutive invocations repeat the previous search."
285  (declare (ignore p))
286  (let* ((mark (value buffer-input-mark))
287         (ring (value interactive-history))
288         (point (current-point))
289         (just-invoked (eq (last-command-type) :searching-interactive-input)))
290    (when (mark<= point mark)
291      (editor-error "Point not past input mark."))
292    (when (zerop (ring-length ring))
293      (editor-error "No previous input in this buffer."))
294    (unless just-invoked
295      (get-previous-input-search-pattern (region-to-string (region mark point))))
296    (let ((found-it (find-previous-input ring just-invoked)))
297      (unless found-it 
298        (editor-error "Couldn't find ~a." *previous-input-search-string*))
299      (delete-region (region mark point))
300      (insert-region point (ring-ref ring found-it))
301      (setf (value searching-interactive-pointer) found-it))
302  (setf (last-command-type) :searching-interactive-input)))
303
304(defun find-previous-input (ring againp)
305  (let ((ring-length (ring-length ring))
306        (base (if againp
307                  (+ (value searching-interactive-pointer) 1)
308                  0)))
309      (loop
310        (when (= base ring-length)
311          (if againp
312              (setf base 0)
313              (return nil)))
314        (with-mark ((m (region-start (ring-ref ring base))))
315          (when (find-pattern m *previous-input-search-pattern*)
316            (return base)))
317        (incf base))))
318
319(defcommand "Previous Interactive Input" (p)
320  "Insert the previous input in an interactive mode (Listener or Typescript).
321   If repeated, keep rotating the history.  With prefix argument, rotate
322   that many times."
323  "Pop the *interactive-history* at the point."
324  (let* ((point (current-point))
325         (mark (value buffer-input-mark))
326         (ring (value interactive-history))
327         (length (ring-length ring))
328         (p (or p 1)))
329    (when (or (mark< point mark) (zerop length)) (editor-error "Can't get command history"))
330    (cond
331     ((eq (last-command-type) :interactive-history)
332      (let ((base (mod (+ (value interactive-pointer) p) length)))
333        (delete-region (region mark point))
334        (insert-region point (ring-ref ring base))
335        (setf (value interactive-pointer) base)))
336     (t
337      (let ((base (mod (if (minusp p) p (1- p)) length))
338            (region (delete-and-save-region (region mark point))))
339        (insert-region point (ring-ref ring base))
340        (when (mark/= (region-start region) (region-end region))
341          (ring-push region ring)
342          (incf base))
343        (setf (value interactive-pointer) base)))))
344  (setf (last-command-type) :interactive-history))
345
346(defcommand "Next Interactive Input" (p)
347  "Rotate the interactive history backwards.  The region is left around the
348   inserted text.  With prefix argument, rotate that many times."
349  "Call previous-interactive-input-command with negated arg."
350  (previous-interactive-input-command (- (or p 1))))
351
352(defcommand "Kill Interactive Input" (p)
353  "Kill any input to an interactive mode (Listener or Typescript)."
354  "Kill any input to an interactive mode (Listener or Typescript)."
355  (declare (ignore p))
356  (let ((point (buffer-point (current-buffer)))
357        (mark (value buffer-input-mark)))
358    (when (mark< point mark) (editor-error))
359    (kill-region (region mark point) :kill-backward)))
360
361(defcommand "Interactive Beginning of Line" (p)
362  "If on line with current prompt, go to after it, otherwise do what
363  \"Beginning of Line\" always does."
364  "Go to after prompt when on prompt line."
365  (let ((mark (value buffer-input-mark))
366        (point (current-point)))
367    (if (and (same-line-p point mark) (or (not p) (= p 1)))
368        (move-mark point mark)
369        (beginning-of-line-command p))))
370
371(defcommand "Reenter Interactive Input" (p)
372  "Copies the form to the left of point to be after the interactive buffer's
373   input mark.  When the current region is active, it is copied instead."
374  "Copies the form to the left of point to be after the interactive buffer's
375   input mark.  When the current region is active, it is copied instead."
376  (declare (ignore p))
377  (unless (hemlock-bound-p 'buffer-input-mark)
378    (editor-error "Not in an interactive buffer."))
379  (let ((point (current-point)))
380    (let ((region (if (region-active-p)
381                      ;; Copy this, so moving point doesn't affect the region.
382                      (copy-region (current-region))
383                      (with-mark ((start point)
384                                  (end point))
385                        (pre-command-parse-check start)
386                        (unless (form-offset start -1)
387                          (editor-error "Not after complete form."))
388                        (region (copy-mark start) (copy-mark end))))))
389      (buffer-end point)
390      (push-new-buffer-mark point)
391      (insert-region point region)
392      (setf (last-command-type) :ephemerally-active))))
393
394
395
396;;; Other stuff.
397
398(defmode "Editor" :hidden t)
399
400(defcommand "Editor Mode" (p)
401  "Turn on \"Editor\" mode in the current buffer.  If it is already on, turn it
402  off.  When in editor mode, most lisp compilation and evaluation commands
403  manipulate the editor process instead of the current eval server."
404  "Toggle \"Editor\" mode in the current buffer."
405  (declare (ignore p))
406  (setf (buffer-minor-mode (current-buffer) "Editor")
407        (not (buffer-minor-mode (current-buffer) "Editor"))))
408
409(define-file-option "Editor" (buffer value)
410  (declare (ignore value))
411  (setf (buffer-minor-mode buffer "Editor") t))
412
413
414
415(defcommand "Editor Compile Defun" (p)
416  "Compiles the current or next top-level form in the editor Lisp.
417   First the form is evaluated, then the result of this evaluation
418   is passed to compile.  If the current region is active, this
419   compiles the region."
420  "Evaluates the current or next top-level form in the editor Lisp."
421  (declare (ignore p))
422  (if (region-active-p)
423      (editor-compile-region (current-region))
424      (editor-compile-region (defun-region (current-point)) t)))
425
426(defcommand "Editor Compile Region" (p)
427  "Compiles lisp forms between the point and the mark in the editor Lisp."
428  "Compiles lisp forms between the point and the mark in the editor Lisp."
429  (declare (ignore p))
430  (editor-compile-region (current-region)))
431
432(defun defun-region (mark)
433  "This returns a region around the current or next defun with respect to mark.
434   Mark is not used to form the region.  If there is no appropriate top level
435   form, this signals an editor-error.  This calls PRE-COMMAND-PARSE-CHECK."
436  (with-mark ((start mark)
437              (end mark))
438    (pre-command-parse-check start)
439    (cond ((not (mark-top-level-form start end))
440           (editor-error "No current or next top level form."))
441          (t (region start end)))))
442
443(defun eval-region (region
444                    &key
445                    (package (variable-value 'current-package :buffer (current-buffer)))
446                    (path (buffer-pathname (current-buffer))))
447  (evaluate-input-selection
448   (list package path (region-to-string region))))
449       
450                                       
451
452(defun editor-compile-region (region &optional quiet)
453  (unless quiet (message "Compiling region ..."))
454  (eval-region region))
455
456
457(defcommand "Editor Evaluate Defun" (p)
458  "Evaluates the current or next top-level form in the editor Lisp.
459   If the current region is active, this evaluates the region."
460  "Evaluates the current or next top-level form in the editor Lisp."
461  (declare (ignore p))
462  (if (region-active-p)
463    (editor-evaluate-region-command nil)
464    (eval-region (defun-region (current-point)))))
465
466(defcommand "Editor Evaluate Region" (p)
467  "Evaluates lisp forms between the point and the mark in the editor Lisp."
468  "Evaluates lisp forms between the point and the mark in the editor Lisp."
469  (declare (ignore p))
470  (if (region-active-p)
471    (eval-region (current-region))
472    (let* ((point (current-point)))
473      (pre-command-parse-check point)
474      (when (valid-spot point nil)      ; not in the middle of a comment
475        (cond ((eql (next-character point) #\()
476               (with-mark ((m point))
477                 (if (form-offset m 1)
478                   (eval-region (region point m)))))
479              ((eql (previous-character point) #\))
480               (with-mark ((m point))
481                 (if (form-offset m -1)
482                   (eval-region (region m point)))))
483              (t
484               (with-mark ((start point)
485                           (end point))
486                 (when (mark-symbol start end)
487                   (eval-region (region start end))))))))))
488
489(defcommand "Editor Re-evaluate Defvar" (p)
490  "Evaluate the current or next top-level form if it is a DEFVAR.  Treat the
491   form as if the variable is not bound.  This occurs in the editor Lisp."
492  "Evaluate the current or next top-level form if it is a DEFVAR.  Treat the
493   form as if the variable is not bound.  This occurs in the editor Lisp."
494  (declare (ignore p))
495  (with-input-from-region (stream (defun-region (current-point)))
496    (clear-echo-area)
497    (in-lisp
498     (let ((form (read stream)))
499       (unless (eq (car form) 'defvar) (editor-error "Not a DEFVAR."))
500       (makunbound (cadr form))
501       (message "Evaluation returned ~S" (eval form))))))
502
503(defun macroexpand-expression (expander)
504  (let* ((point (buffer-point (current-buffer)))
505         (region (if (region-active-p)
506                   (current-region)
507                   (with-mark ((start point))
508                     (pre-command-parse-check start)
509                     (with-mark ((end start))
510                       (unless (form-offset end 1) (editor-error))
511                       (region start end)))))
512         (expr (with-input-from-region (s region)
513                 (read s))))
514    (let* ((*print-pretty* t)
515           (expansion (funcall expander expr)))
516      (format t "~&~s~&" expansion))))
517
518(defcommand "Editor Macroexpand-1 Expression" (p)
519  "Show the macroexpansion of the current expression in the null environment.
520   With an argument, use MACROEXPAND instead of MACROEXPAND-1."
521  "Show the macroexpansion of the current expression in the null environment.
522   With an argument, use MACROEXPAND instead of MACROEXPAND-1."
523  (macroexpand-expression (if p 'macroexpand 'macroexpand-1)))
524
525(defcommand "Editor Macroexpand Expression" (p)
526  "Show the macroexpansion of the current expression in the null environment.
527   With an argument, use MACROEXPAND-1 instead of MACROEXPAND."
528  "Show the macroexpansion of the current expression in the null environment.
529   With an argument, use MACROEXPAND-1 instead of MACROEXPAND."
530  (macroexpand-expression (if p 'macroexpand-1 'macroexpand)))
531
532
533(defcommand "Editor Evaluate Expression" (p)
534  "Prompt for an expression to evaluate in the editor Lisp."
535  "Prompt for an expression to evaluate in the editor Lisp."
536  (declare (ignore p))
537  (in-lisp
538   (multiple-value-call #'message "=> ~@{~#[~;~S~:;~S, ~]~}"
539     (eval (prompt-for-expression
540            :prompt "Editor Eval: "
541            :help "Expression to evaluate")))))
542
543(defcommand "Editor Evaluate Buffer" (p)
544  "Evaluates the text in the current buffer in the editor Lisp."
545  (declare (ignore p))
546  (message "Evaluating buffer in the editor ...")
547  (with-input-from-region (stream (buffer-region (current-buffer)))
548    (in-lisp
549     (do ((object (read stream nil lispbuf-eof) 
550                  (read stream nil lispbuf-eof)))
551         ((eq object lispbuf-eof))
552       (eval object)))
553    (message "Evaluation complete.")))
554
555
556
557(defcommand "Editor Compile File" (p)
558  "Prompts for file to compile in the editor Lisp.  Does not compare source
559   and binary write dates.  Does not check any buffer for that file for
560   whether the buffer needs to be saved."
561  "Prompts for file to compile."
562  (declare (ignore p))
563  (let ((pn (prompt-for-file :default
564                             (buffer-default-pathname (current-buffer))
565                             :prompt "File to compile: ")))
566    (in-lisp (compile-file (namestring pn) #+cmu :error-file #+cmu nil))))
567
568
569(defun older-or-non-existent-fasl-p (pathname &optional definitely)
570  (let ((obj-pn (probe-file (compile-file-pathname pathname))))
571    (or definitely
572        (not obj-pn)
573        (< (file-write-date obj-pn) (file-write-date pathname)))))
574
575
576(defcommand "Editor Compile Buffer File" (p)
577  "Compile the file in the current buffer in the editor Lisp if its associated
578   binary file (of type .fasl) is older than the source or doesn't exist.  When
579   the binary file is up to date, the user is asked if the source should be
580   compiled anyway.  When the prefix argument is supplied, compile the file
581   without checking the binary file.  When \"Compile Buffer File Confirm\" is
582   set, this command will ask for confirmation when it otherwise would not."
583  "Compile the file in the current buffer in the editor Lisp if the fasl file
584   isn't up to date.  When p, always do it."
585  (let* ((buf (current-buffer))
586         (pn (buffer-pathname buf)))
587    (unless pn (editor-error "Buffer has no associated pathname."))
588    (cond ((buffer-modified buf)
589           (when (or (not (value compile-buffer-file-confirm))
590                     (prompt-for-y-or-n
591                      :default t :default-string "Y"
592                      :prompt (list "Save and compile file ~A? "
593                                    (namestring pn))))
594             (write-buffer-file buf pn)
595             (in-lisp (compile-file (namestring pn) #+cmu :error-file #+cmu nil))))
596          ((older-or-non-existent-fasl-p pn p)
597           (when (or (not (value compile-buffer-file-confirm))
598                     (prompt-for-y-or-n
599                      :default t :default-string "Y"
600                      :prompt (list "Compile file ~A? " (namestring pn))))
601             (in-lisp (compile-file (namestring pn) #+cmu :error-file #+cmu nil))))
602          (t (when (or p
603                       (prompt-for-y-or-n
604                        :default t :default-string "Y"
605                        :prompt
606                        "Fasl file up to date, compile source anyway? "))
607               (in-lisp (compile-file (namestring pn) #+cmu :error-file #+cmu nil)))))))
608
609
610
611
612
613
614
615;;;; Lisp documentation stuff.
616
617;;; FUNCTION-TO-DESCRIBE is used in "Editor Describe Function Call" and
618;;; "Describe Function Call".
619;;;
620(defmacro function-to-describe (var error-name)
621  `(cond ((not (symbolp ,var))
622          (,error-name "~S is not a symbol." ,var))
623         ((special-operator-p ,var) ,var)
624         ((macro-function ,var))
625         ((fboundp ,var))
626         (t
627          (,error-name "~S is not a function." ,var))))
628
629(defcommand "Editor Describe Function Call" (p)
630  "Describe the most recently typed function name in the editor Lisp."
631  "Describe the most recently typed function name in the editor Lisp."
632  (declare (ignore p))
633  (with-mark ((mark1 (current-point))
634              (mark2 (current-point)))
635    (pre-command-parse-check mark1)
636    (unless (backward-up-list mark1) (editor-error))
637    (form-offset (move-mark mark2 (mark-after mark1)) 1)
638    (with-input-from-region (s (region mark1 mark2))
639      (in-lisp
640       (let* ((sym (read s))
641              (fun (function-to-describe sym editor-error)))
642         (with-pop-up-display (*standard-output* :title (format nil "~s" sym))
643           (editor-describe-function fun sym)))))))
644
645
646(defcommand "Editor Describe Symbol" (p)
647  "Describe the previous s-expression if it is a symbol in the editor Lisp."
648  "Describe the previous s-expression if it is a symbol in the editor Lisp."
649  (declare (ignore p))
650  (with-mark ((mark1 (current-point))
651              (mark2 (current-point)))
652    (mark-symbol mark1 mark2)
653    (with-input-from-region (s (region mark1 mark2))
654      (let ((thing (read s)))
655        (if (symbolp thing)
656          (with-pop-up-display (*standard-output* :title (format nil "~s" thing))
657            (describe thing))
658          (if (and (consp thing)
659                   (or (eq (car thing) 'quote)
660                       (eq (car thing) 'function))
661                   (symbolp (cadr thing)))
662            (with-pop-up-display (*standard-output* :title (format nil "~s" (cadr thing)))
663              (describe (cadr thing)))
664            (editor-error "~S is not a symbol, or 'symbol, or #'symbol."
665                          thing)))))))
666
667;;; MARK-SYMBOL moves mark1 and mark2 around the previous or current symbol.
668;;; However, if the marks are immediately before the first constituent char
669;;; of the symbol name, we use the next symbol since the marks probably
670;;; correspond to the point, and Hemlock's cursor display makes it look like
671;;; the point is within the symbol name.  This also tries to ignore :prefix
672;;; characters such as quotes, commas, etc.
673;;;
674(defun mark-symbol (mark1 mark2)
675  (pre-command-parse-check mark1)
676  (with-mark ((tmark1 mark1)
677              (tmark2 mark1))
678    (cond ((and (form-offset tmark1 1)
679                (form-offset (move-mark tmark2 tmark1) -1)
680                (or (mark= mark1 tmark2)
681                    (and (find-attribute tmark2 :lisp-syntax
682                                         #'(lambda (x) (not (eq x :prefix))))
683                         (mark= mark1 tmark2))))
684           (form-offset mark2 1))
685          (t
686           (form-offset mark1 -1)
687           (find-attribute mark1 :lisp-syntax
688                           #'(lambda (x) (not (eq x :prefix))))
689           (form-offset (move-mark mark2 mark1) 1)))))
690
691
692(defcommand "Editor Describe" (p)
693  "Call Describe on a Lisp object.
694  Prompt for an expression which is evaluated to yield the object."
695  "Prompt for an object to describe."
696  (declare (ignore p))
697  (in-lisp
698   (let* ((exp (prompt-for-expression
699                :prompt "Object: "
700                :help "Expression to evaluate to get object to describe."))
701          (obj (eval exp)))
702     (with-pop-up-display (*standard-output* :title (format nil "~s" exp))
703       (describe obj)))))
704
705(defcommand "Filter Region" (p)
706  "Apply a Lisp function to each line of the region.
707  An expression is prompted for which should evaluate to a Lisp function
708  from a string to a string.  The function must neither modify its argument
709  nor modify the return value after it is returned."
710  "Call prompt for a function, then call Filter-Region with it and the region."
711  (declare (ignore p))
712  (let* ((exp (prompt-for-expression
713               :prompt "Function: "
714               :help "Expression to evaluate to get function to use as filter."))
715         (fun (in-lisp (eval exp)))
716         (region (current-region)))
717    (let* ((start (copy-mark (region-start region) :left-inserting))
718           (end (copy-mark (region-end region) :left-inserting))
719           (region (region start end))
720           (undo-region (copy-region region)))
721      (filter-region fun region)
722      (make-region-undo :twiddle "Filter Region" region undo-region))))
Note: See TracBrowser for help on using the repository browser.