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

Last change on this file since 12321 was 12321, checked in by gb, 10 years ago

"Confirm Listener Input" - in the top listener input stream is in
"read-line" mode, send the input region without worrying about whether
or not it appears to contain complete lisp expressions.

[This is intended to allow things like:

? (read-line)
((((
"(((("
NIL
?
. There may be race conditions here.]

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