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

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

Make sure that the newline typed to "Confirm Listener Input" replaces
the selection, if any.

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