source: tags/pre_1_0_pre_hash_modifications/ccl/hemlock/src/lispbuf.lisp @ 2475

Last change on this file since 2475 was 2475, checked in by anonymous, 14 years ago

This commit was manufactured by cvs2svn to create tag
'pre_1_0_pre_hash_modifications'.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 30.0 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#+CMU (ext:file-comment
8  "$Header$")
9;;;
10;;; **********************************************************************
11;;;
12;;; Stuff to do a little lisp hacking in the editor's Lisp environment.
13;;;
14
15(in-package :hemlock)
16
17
18(defmacro in-lisp (&body body)
19  "Evaluates body inside HANDLE-LISP-ERRORS.  *package* is bound to the package
20   named by \"Current Package\" if it is non-nil."
21  (let ((name (gensym)) (package (gensym)))
22    `(handle-lisp-errors
23      (let* ((,name (value current-package))
24             (,package (and ,name (find-package ,name))))
25        (progv (if ,package '(*package*)) (if ,package (list ,package))
26          ,@body)))))
27
28
29(define-file-option "Package" (buffer value)
30  (defhvar "Current Package"
31    "The package used for evaluation of Lisp in this buffer."
32    :buffer buffer
33    :value
34    (let* ((eof (list nil))
35           (thing (read-from-string value nil eof)))
36      (when (eq thing eof) (error "Bad package file option value."))
37      (cond
38       ((stringp thing)
39        thing)
40       ((symbolp thing)
41        (symbol-name thing))
42       ((characterp thing)
43        (string thing))
44       (t
45        (message
46         "Ignoring \"package\" file option -- cannot convert to a string."))))
47    :hooks (list 'package-name-change-hook)))
48
49
50;;;; Eval Mode Interaction.
51
52(declaim (special * ** *** - + ++ +++ / // ///))
53
54
55(defun get-prompt ()
56  #+cmu (locally (declare (special ext:*prompt*))
57          (if (functionp ext:*prompt*)
58              (funcall ext:*prompt*)
59              ext:*prompt*))
60  #+sbcl (with-output-to-string (out)
61           (funcall sb-int:*repl-prompt-fun* out))
62  #-(or cmu sbcl) "* ")
63
64
65(defun show-prompt (&optional (stream *standard-output*))
66  #-sbcl (princ (get-prompt) stream)
67  #+sbcl (funcall sb-int:*repl-prompt-fun* stream))
68
69
70(defun setup-eval-mode (buffer)
71  (let ((point (buffer-point buffer)))
72    (setf (buffer-minor-mode buffer "Eval") t)
73    (setf (buffer-minor-mode buffer "Editor") t)
74    (setf (buffer-major-mode buffer) "Lisp")
75    (buffer-end point)
76    (defhvar "Current Package"
77      "This variable holds the name of the package currently used for Lisp
78       evaluation and compilation.  If it is Nil, the value of *Package* is used
79       instead."
80      :value nil
81      :buffer buffer)
82    (unless (hemlock-bound-p 'buffer-input-mark :buffer buffer)
83      (defhvar "Buffer Input Mark"
84        "Mark used for Eval Mode input."
85        :buffer buffer
86        :value (copy-mark point :right-inserting))
87      (defhvar "Eval Output Stream"
88        "Output stream used for Eval Mode output in this buffer."
89        :buffer buffer
90        :value (make-hemlock-output-stream point))
91      (defhvar "Interactive History"
92        "A ring of the regions input to an interactive mode (Eval or Typescript)."
93        :buffer buffer
94        :value (make-ring (value interactive-history-length)))
95      (defhvar "Interactive Pointer"
96        "Pointer into \"Interactive History\"."
97        :buffer buffer
98        :value 0)
99      (defhvar "Searching Interactive Pointer"
100        "Pointer into \"Interactive History\"."
101        :buffer buffer
102        :value 0))
103    (let ((*standard-output*
104           (variable-value 'eval-output-stream :buffer buffer)))
105      (fresh-line)
106      (show-prompt))
107    (move-mark (variable-value 'buffer-input-mark :buffer buffer) point)))
108
109(defmode "Eval" :major-p nil :setup-function #'setup-eval-mode)
110
111(defun eval-mode-lisp-mode-hook (buffer on)
112  "Turn on Lisp mode when we go into Eval Mode."
113  (when on
114    (setf (buffer-major-mode buffer) "Lisp")))
115;;;
116(add-hook eval-mode-hook 'eval-mode-lisp-mode-hook)
117
118(defhvar "Editor Definition Info"
119  "When this is non-nil, the editor Lisp is used to determine definition
120   editing information; otherwise, the slave Lisp is used."
121  :value t
122  :mode "Eval")
123
124
125(defvar *selected-eval-buffer* nil)
126
127(defcommand "Select Eval Buffer" (p)
128  "Goto buffer in \"Eval\" mode, creating one if necessary."
129  "Goto buffer in \"Eval\" mode, creating one if necessary."
130  (declare (ignore p))
131  (unless *selected-eval-buffer*
132    (when (getstring "Eval" *buffer-names*)
133      (editor-error "There is already a buffer named \"Eval\"!"))
134    (setf *selected-eval-buffer*
135          (make-buffer "Eval"
136                       :delete-hook
137                       (list #'(lambda (buf)
138                                 (declare (ignore buf))
139                                 (setf *selected-eval-buffer* nil)))))
140    (setf (buffer-minor-mode *selected-eval-buffer* "Eval") t))
141  (change-to-buffer *selected-eval-buffer*))
142
143
144(defvar lispbuf-eof '(nil))
145
146(defhvar "Unwedge Interactive Input Confirm"
147  "When set (the default), trying to confirm interactive input when the
148   point is not after the input mark causes Hemlock to ask the user if he
149   needs to be unwedged.  When not set, an editor error is signaled
150   informing the user that the point is before the input mark."
151  :value t)
152
153(defun unwedge-eval-buffer ()
154  (abort-eval-input-command nil))
155
156(defhvar "Unwedge Interactive Input Fun"
157  "Function to call when input is confirmed, but the point is not past the
158   input mark."
159  :value #'unwedge-eval-buffer
160  :mode "Eval")
161
162(defhvar "Unwedge Interactive Input String"
163  "String to add to \"Point not past input mark.  \" explaining what will
164   happen if the the user chooses to be unwedged."
165  :value "Prompt again at the end of the buffer? "
166  :mode "Eval")
167
168(defcommand "Confirm Eval Input" (p)
169  "Evaluate Eval Mode input between point and last prompt."
170  "Evaluate Eval Mode input between point and last prompt."
171  (declare (ignore p))
172  (let ((input-region (get-interactive-input)))
173    (when input-region
174      (let* ((output (value eval-output-stream))
175             (*standard-output* output)
176             (*error-output* output)
177             (*trace-output* output))
178        (fresh-line)
179        (in-lisp
180         ;; Copy the region to keep the output and input streams from interacting
181         ;; since input-region is made of permanent marks into the buffer.
182         (with-input-from-region (stream (copy-region input-region))
183           (loop
184             (let ((form (read stream nil lispbuf-eof)))
185               (when (eq form lispbuf-eof)
186                 ;; Move the buffer's input mark to the end of the buffer.
187                 (move-mark (region-start input-region)
188                            (region-end input-region))
189                 (return))
190               (setq +++ ++ ++ + + - - form)
191               (let ((this-eval (multiple-value-list (eval form))))
192                 (fresh-line)
193                 (dolist (x this-eval) (prin1 x) (terpri))
194                 (show-prompt)
195                 (setq /// // // / / this-eval)
196                 (setq *** ** ** * * (car this-eval)))))))))))
197
198(defcommand "Abort Eval Input" (p)
199  "Move to the end of the buffer and prompt."
200  "Move to the end of the buffer and prompt."
201  (declare (ignore p))
202  (let ((point (current-point)))
203    (buffer-end point)
204    (insert-character point #\newline)
205    (insert-string point "Aborted.")
206    (insert-character point #\newline)
207    (insert-string point (get-prompt))
208    (move-mark (value buffer-input-mark) point)))
209
210
211
212;;;; General interactive commands used in eval and typescript buffers.
213
214(defun get-interactive-input ()
215  "Tries to return a region.  When the point is not past the input mark, and
216   the user has \"Unwedge Interactive Input Confirm\" set, the buffer is
217   optionally fixed up, and nil is returned.  Otherwise, an editor error is
218   signalled.  When a region is returned, the start is the current buffer's
219   input mark, and the end is the current point moved to the end of the buffer."
220  (let ((point (current-point))
221        (mark (value buffer-input-mark)))
222    (cond
223     ((mark>= point mark)
224      (buffer-end point)
225      (let* ((input-region (region mark point))
226             (string (region-to-string input-region))
227             (ring (value interactive-history)))
228        (when (and (or (zerop (ring-length ring))
229                       (string/= string (region-to-string (ring-ref ring 0))))
230                   (> (length string) (value minimum-interactive-input-length)))
231          (ring-push (copy-region input-region) ring))
232        input-region))
233     ((value unwedge-interactive-input-confirm)
234      (beep)
235      (when (prompt-for-y-or-n
236             :prompt (concatenate 'simple-string
237                                  "Point not past input mark.  "
238                                  (value unwedge-interactive-input-string))
239             :must-exist t :default t :default-string "yes")
240        (funcall (value unwedge-interactive-input-fun))
241        (message "Unwedged."))
242      nil)
243     (t
244      (editor-error "Point not past input mark.")))))
245
246(defhvar "Interactive History Length"
247  "This is the length used for the history ring in interactive buffers.
248   It must be set before turning on the mode."
249  :value 10)
250
251(defhvar "Minimum Interactive Input Length"
252  "When the number of characters in an interactive buffer exceeds this value,
253   it is pushed onto the interactive history, otherwise it is lost forever."
254  :value 2)
255
256
257(defvar *previous-input-search-string* "ignore")
258
259(defvar *previous-input-search-pattern*
260  ;; Give it a bogus string since you can't give it the empty string.
261  (new-search-pattern :string-insensitive :forward "ignore"))
262
263(defun get-previous-input-search-pattern (string)
264  (if (string= *previous-input-search-string* string)
265      *previous-input-search-pattern*
266      (new-search-pattern :string-insensitive :forward 
267                          (setf *previous-input-search-string* string)
268                          *previous-input-search-pattern*)))
269
270(defcommand "Search Previous Interactive Input" (p)
271  "Search backward through the interactive history using the current input as
272   a search string.  Consecutive invocations repeat the previous search."
273  "Search backward through the interactive history using the current input as
274   a search string.  Consecutive invocations repeat the previous search."
275  (declare (ignore p))
276  (let* ((mark (value buffer-input-mark))
277         (ring (value interactive-history))
278         (point (current-point))
279         (just-invoked (eq (last-command-type) :searching-interactive-input)))
280    (when (mark<= point mark)
281      (editor-error "Point not past input mark."))
282    (when (zerop (ring-length ring))
283      (editor-error "No previous input in this buffer."))
284    (unless just-invoked
285      (get-previous-input-search-pattern (region-to-string (region mark point))))
286    (let ((found-it (find-previous-input ring just-invoked)))
287      (unless found-it 
288        (editor-error "Couldn't find ~a." *previous-input-search-string*))
289      (delete-region (region mark point))
290      (insert-region point (ring-ref ring found-it))
291      (setf (value searching-interactive-pointer) found-it))
292  (setf (last-command-type) :searching-interactive-input)))
293
294(defun find-previous-input (ring againp)
295  (let ((ring-length (ring-length ring))
296        (base (if againp
297                  (+ (value searching-interactive-pointer) 1)
298                  0)))
299      (loop
300        (when (= base ring-length)
301          (if againp
302              (setf base 0)
303              (return nil)))
304        (with-mark ((m (region-start (ring-ref ring base))))
305          (when (find-pattern m *previous-input-search-pattern*)
306            (return base)))
307        (incf base))))
308
309(defcommand "Previous Interactive Input" (p)
310  "Insert the previous input in an interactive mode (Eval or Typescript).
311   If repeated, keep rotating the history.  With prefix argument, rotate
312   that many times."
313  "Pop the *interactive-history* at the point."
314  (let* ((point (current-point))
315         (mark (value buffer-input-mark))
316         (ring (value interactive-history))
317         (length (ring-length ring))
318         (p (or p 1)))
319    (when (or (mark< point mark) (zerop length)) (editor-error))
320    (cond
321     ((eq (last-command-type) :interactive-history)
322      (let ((base (mod (+ (value interactive-pointer) p) length)))
323        (delete-region (region mark point))
324        (insert-region point (ring-ref ring base))
325        (setf (value interactive-pointer) base)))
326     (t
327      (let ((base (mod (if (minusp p) p (1- p)) length))
328            (region (delete-and-save-region (region mark point))))
329        (insert-region point (ring-ref ring base))
330        (when (mark/= (region-start region) (region-end region))
331          (ring-push region ring)
332          (incf base))
333        (setf (value interactive-pointer) base)))))
334  (setf (last-command-type) :interactive-history))
335
336(defcommand "Next Interactive Input" (p)
337  "Rotate the interactive history backwards.  The region is left around the
338   inserted text.  With prefix argument, rotate that many times."
339  "Call previous-interactive-input-command with negated arg."
340  (previous-interactive-input-command (- (or p 1))))
341
342(defcommand "Kill Interactive Input" (p)
343  "Kill any input to an interactive mode (Eval or Typescript)."
344  "Kill any input to an interactive mode (Eval or Typescript)."
345  (declare (ignore p))
346  (let ((point (buffer-point (current-buffer)))
347        (mark (value buffer-input-mark)))
348    (when (mark< point mark) (editor-error))
349    (kill-region (region mark point) :kill-backward)))
350
351(defcommand "Interactive Beginning of Line" (p)
352  "If on line with current prompt, go to after it, otherwise do what
353  \"Beginning of Line\" always does."
354  "Go to after prompt when on prompt line."
355  (let ((mark (value buffer-input-mark))
356        (point (current-point)))
357    (if (and (same-line-p point mark) (or (not p) (= p 1)))
358        (move-mark point mark)
359        (beginning-of-line-command p))))
360
361(defcommand "Reenter Interactive Input" (p)
362  "Copies the form to the left of point to be after the interactive buffer's
363   input mark.  When the current region is active, it is copied instead."
364  "Copies the form to the left of point to be after the interactive buffer's
365   input mark.  When the current region is active, it is copied instead."
366  (declare (ignore p))
367  (unless (hemlock-bound-p 'buffer-input-mark)
368    (editor-error "Not in an interactive buffer."))
369  (let ((point (current-point)))
370    (let ((region (if (region-active-p)
371                      ;; Copy this, so moving point doesn't affect the region.
372                      (copy-region (current-region))
373                      (with-mark ((start point)
374                                  (end point))
375                        (pre-command-parse-check start)
376                        (unless (form-offset start -1)
377                          (editor-error "Not after complete form."))
378                        (region (copy-mark start) (copy-mark end))))))
379      (buffer-end point)
380      (push-buffer-mark (copy-mark point))
381      (insert-region point region)
382      (setf (last-command-type) :ephemerally-active))))
383
384
385
386;;; Other stuff.
387
388(defmode "Editor")
389
390(defcommand "Editor Mode" (p)
391  "Turn on \"Editor\" mode in the current buffer.  If it is already on, turn it
392  off.  When in editor mode, most lisp compilation and evaluation commands
393  manipulate the editor process instead of the current eval server."
394  "Toggle \"Editor\" mode in the current buffer."
395  (declare (ignore p))
396  (setf (buffer-minor-mode (current-buffer) "Editor")
397        (not (buffer-minor-mode (current-buffer) "Editor"))))
398
399(define-file-option "Editor" (buffer value)
400  (declare (ignore value))
401  (setf (buffer-minor-mode buffer "Editor") t))
402
403(defhvar "Editor Definition Info"
404  "When this is non-nil, the editor Lisp is used to determine definition
405   editing information; otherwise, the slave Lisp is used."
406  :value t
407  :mode "Editor")
408
409(defcommand "Editor Compile Defun" (p)
410  "Compiles the current or next top-level form in the editor Lisp.
411   First the form is evaluated, then the result of this evaluation
412   is passed to compile.  If the current region is active, this
413   compiles the region."
414  "Evaluates the current or next top-level form in the editor Lisp."
415  (declare (ignore p))
416  (if (region-active-p)
417      (editor-compile-region (current-region))
418      (editor-compile-region (defun-region (current-point)) t)))
419
420(defcommand "Editor Compile Region" (p)
421  "Compiles lisp forms between the point and the mark in the editor Lisp."
422  "Compiles lisp forms between the point and the mark in the editor Lisp."
423  (declare (ignore p))
424  (editor-compile-region (current-region)))
425
426(defun defun-region (mark)
427  "This returns a region around the current or next defun with respect to mark.
428   Mark is not used to form the region.  If there is no appropriate top level
429   form, this signals an editor-error.  This calls PRE-COMMAND-PARSE-CHECK."
430  (with-mark ((start mark)
431              (end mark))
432    (pre-command-parse-check start)
433    (cond ((not (mark-top-level-form start end))
434           (editor-error "No current or next top level form."))
435          (t (region start end)))))
436
437(defun editor-compile-region (region &optional quiet)
438  (unless quiet (message "Compiling region ..."))
439  (in-lisp
440   (with-input-from-region (stream region)
441     (with-pop-up-display (*error-output* :height 19)
442       ;; JDz: We don't record source locations and what not, but this
443       ;; is portable.  CMUCL specific implementation removed because
444       ;; it does not work on HEMLOCK-REGION-STREAM (but it can be
445       ;; added back later if CMUCL starts using user-extensible
446       ;; streams internally.)
447       (funcall (compile nil `(lambda ()
448                                ,@(loop for form = (read stream nil stream)
449                                        until (eq form stream)
450                                        collect form))))))))
451
452
453(defcommand "Editor Evaluate Defun" (p)
454  "Evaluates the current or next top-level form in the editor Lisp.
455   If the current region is active, this evaluates the region."
456  "Evaluates the current or next top-level form in the editor Lisp."
457  (declare (ignore p))
458  (if (region-active-p)
459      (editor-evaluate-region-command nil)
460      (with-input-from-region (stream (defun-region (current-point)))
461        (clear-echo-area)
462        (in-lisp
463         (message "Editor Evaluation returned ~S"
464                  (eval (read stream)))))))
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  (with-input-from-region (stream (current-region))
471    (clear-echo-area)
472    (write-string "Evaluating region in the editor ..." *echo-area-stream*)
473    (finish-output *echo-area-stream*)
474    (in-lisp
475     (do ((object (read stream nil lispbuf-eof) 
476                  (read stream nil lispbuf-eof)))
477         ((eq object lispbuf-eof))
478       (eval object)))
479    (message "Evaluation complete.")))
480           
481(defcommand "Editor Re-evaluate Defvar" (p)
482  "Evaluate the current or next top-level form if it is a DEFVAR.  Treat the
483   form as if the variable is not bound.  This occurs in the editor Lisp."
484  "Evaluate the current or next top-level form if it is a DEFVAR.  Treat the
485   form as if the variable is not bound.  This occurs in the editor Lisp."
486  (declare (ignore p))
487  (with-input-from-region (stream (defun-region (current-point)))
488    (clear-echo-area)
489    (in-lisp
490     (let ((form (read stream)))
491       (unless (eq (car form) 'defvar) (editor-error "Not a DEFVAR."))
492       (makunbound (cadr form))
493       (message "Evaluation returned ~S" (eval form))))))
494
495(defcommand "Editor Macroexpand Expression" (p)
496  "Show the macroexpansion of the current expression in the null environment.
497   With an argument, use MACROEXPAND instead of MACROEXPAND-1."
498  "Show the macroexpansion of the current expression in the null environment.
499   With an argument, use MACROEXPAND instead of MACROEXPAND-1."
500  (let ((point (buffer-point (current-buffer))))
501    (with-mark ((start point))
502      (pre-command-parse-check start)
503      (with-mark ((end start))
504        (unless (form-offset end 1) (editor-error))
505        (in-lisp
506         (with-pop-up-display (rts)
507           (write-string (with-input-from-region (s (region start end))
508                           (prin1-to-string (funcall (if p
509                                                         'macroexpand
510                                                         'macroexpand-1)
511                                                     (read s))))
512                         rts)))))))
513
514(defcommand "Editor Evaluate Expression" (p)
515  "Prompt for an expression to evaluate in the editor Lisp."
516  "Prompt for an expression to evaluate in the editor Lisp."
517  (declare (ignore p))
518  (in-lisp
519   (multiple-value-call #'message "=> ~@{~#[~;~S~:;~S, ~]~}"
520     (eval (prompt-for-expression
521            :prompt "Editor Eval: "
522            :help "Expression to evaluate")))))
523
524(defcommand "Editor Evaluate Buffer" (p)
525  "Evaluates the text in the current buffer in the editor Lisp."
526  "Evaluates the text in the current buffer redirecting *Standard-Output* to
527   the echo area.  This occurs in the editor Lisp.  The prefix argument is
528   ignored."
529  (declare (ignore p))
530  (clear-echo-area)
531  (write-string "Evaluating buffer in the editor ..." *echo-area-stream*)
532  (finish-output *echo-area-stream*)
533  (with-input-from-region (stream (buffer-region (current-buffer)))
534    (let ((*standard-output* *echo-area-stream*))
535      (in-lisp
536       (do ((object (read stream nil lispbuf-eof) 
537                    (read stream nil lispbuf-eof)))
538           ((eq object lispbuf-eof))
539         (eval object))))
540    (message "Evaluation complete.")))
541
542
543
544;;; With-Output-To-Window  --  Internal
545;;;
546;;;
547(defmacro with-output-to-window ((stream name) &body forms)
548  "With-Output-To-Window (Stream Name) {Form}*
549  Bind Stream to a stream that writes into the buffer named Name a la
550  With-Output-To-Mark.  The buffer is created if it does not exist already
551  and a window is created to display the buffer if it is not displayed.
552  For the duration of the evaluation this window is made the current window."
553  (let ((nam (gensym)) (buffer (gensym)) (point (gensym)) 
554        (window (gensym)) (old-window (gensym)))
555    `(let* ((,nam ,name)
556            (,buffer (or (getstring ,nam *buffer-names*) (make-buffer ,nam)))
557            (,point (buffer-end (buffer-point ,buffer)))
558            (,window (or (car (buffer-windows ,buffer)) (make-window ,point)))
559            (,old-window (current-window)))
560       (unwind-protect
561         (progn (setf (current-window) ,window)
562                (buffer-end ,point)
563                (with-output-to-mark (,stream ,point) ,@forms))
564         (setf (current-window) ,old-window)))))
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    (with-output-to-window (*error-output* "Compiler Warnings")
576      (in-lisp (compile-file (namestring pn) #+cmu :error-file #+cmu nil)))))
577
578
579(defun older-or-non-existent-fasl-p (pathname &optional definitely)
580  (let ((obj-pn (probe-file (compile-file-pathname pathname))))
581    (or definitely
582        (not obj-pn)
583        (< (file-write-date obj-pn) (file-write-date pathname)))))
584
585
586(defcommand "Editor Compile Buffer File" (p)
587  "Compile the file in the current buffer in the editor Lisp if its associated
588   binary file (of type .fasl) is older than the source or doesn't exist.  When
589   the binary file is up to date, the user is asked if the source should be
590   compiled anyway.  When the prefix argument is supplied, compile the file
591   without checking the binary file.  When \"Compile Buffer File Confirm\" is
592   set, this command will ask for confirmation when it otherwise would not."
593  "Compile the file in the current buffer in the editor Lisp if the fasl file
594   isn't up to date.  When p, always do it."
595  (let* ((buf (current-buffer))
596         (pn (buffer-pathname buf)))
597    (unless pn (editor-error "Buffer has no associated pathname."))
598    (cond ((buffer-modified buf)
599           (when (or (not (value compile-buffer-file-confirm))
600                     (prompt-for-y-or-n
601                      :default t :default-string "Y"
602                      :prompt (list "Save and compile file ~A? "
603                                    (namestring pn))))
604             (write-buffer-file buf pn)
605             (with-output-to-window (*error-output* "Compiler Warnings")
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             (with-output-to-window (*error-output* "Compiler Warnings")
613               (in-lisp (compile-file (namestring pn) #+cmu :error-file #+cmu nil)))))
614          (t (when (or p
615                       (prompt-for-y-or-n
616                        :default t :default-string "Y"
617                        :prompt
618                        "Fasl file up to date, compile source anyway? "))
619               (with-output-to-window (*error-output* "Compiler Warnings")
620                 (in-lisp (compile-file (namestring pn) #+cmu :error-file #+cmu nil))))))))
621
622(defcommand "Editor Compile Group" (p)
623  "Compile each file in the current group which needs it in the editor Lisp.
624   If a file has type LISP and there is a curresponding file with type
625   FASL which has been written less recently (or it doesn't exit), then
626   the file is compiled, with error output directed to the \"Compiler Warnings\"
627   buffer.  If a prefix argument is provided, then all the files are compiled.
628   All modified files are saved beforehand."
629  "Do a Compile-File in each file in the current group that seems to need it
630   in the editor Lisp."
631  (save-all-files-command ())
632  (unless *active-file-group* (editor-error "No active file group."))
633  (dolist (file *active-file-group*)
634    (when (string-equal (pathname-type file) "lisp")
635      (let ((tn (probe-file file)))
636        (cond ((not tn)
637               (message "File ~A not found." (namestring file)))
638              ((older-or-non-existent-fasl-p tn p)
639               (with-output-to-window (*error-output* "Compiler Warnings")
640                 (in-lisp (compile-file (namestring tn) #+cmu :error-file #+cmu nil)))))))))
641
642(defcommand "List Compile Group" (p)
643  "List any files that would be compiled by \"Compile Group\".  All Modified
644   files are saved before checking to generate a consistent list."
645  "Do a Compile-File in each file in the current group that seems to need it."
646  (declare (ignore p))
647  (save-all-files-command ())
648  (unless *active-file-group* (editor-error "No active file group."))
649  (with-pop-up-display (s)
650    (write-line "\"Compile Group\" would compile the following files:" s)
651    (force-output s)
652    (dolist (file *active-file-group*)
653      (when (string-equal (pathname-type file) "lisp")
654        (let ((tn (probe-file file)))
655          (cond ((not tn)
656                 (format s "File ~A not found.~%" (namestring file)))
657                ((older-or-non-existent-fasl-p tn)
658                 (write-line (namestring tn) s)))
659          (force-output s))))))
660
661(defhvar "Load Pathname Defaults"
662  "The default pathname used by the load command.")
663
664(defcommand "Editor Load File" (p)
665  "Prompt for a file to load into Editor Lisp."
666  "Prompt for a file to load into the Editor Lisp."
667  (declare (ignore p))
668  (let ((name (truename (prompt-for-file
669                         :default
670                         (or (value load-pathname-defaults)
671                             (buffer-default-pathname (current-buffer)))
672                         :prompt "Editor file to load: "
673                         :help "The name of the file to load"))))
674    (setv load-pathname-defaults name)
675    (in-lisp (load name))))
676
677
678
679;;;; Lisp documentation stuff.
680
681;;; FUNCTION-TO-DESCRIBE is used in "Editor Describe Function Call" and
682;;; "Describe Function Call".
683;;;
684(defmacro function-to-describe (var error-name)
685  `(cond ((not (symbolp ,var))
686          (,error-name "~S is not a symbol." ,var))
687         ((macro-function ,var))
688         ((fboundp ,var)
689          (if (listp (symbol-function ,var))
690              ,var
691              (symbol-function ,var)))
692         (t
693          (,error-name "~S is not a function." ,var))))
694
695(defcommand "Editor Describe Function Call" (p)
696  "Describe the most recently typed function name in the editor Lisp."
697  "Describe the most recently typed function name in the editor Lisp."
698  (declare (ignore p))
699  (with-mark ((mark1 (current-point))
700              (mark2 (current-point)))
701    (pre-command-parse-check mark1)
702    (unless (backward-up-list mark1) (editor-error))
703    (form-offset (move-mark mark2 (mark-after mark1)) 1)
704    (with-input-from-region (s (region mark1 mark2))
705      (in-lisp
706       (let* ((sym (read s))
707              (fun (function-to-describe sym editor-error)))
708         (with-pop-up-display (*standard-output*)
709           (editor-describe-function fun sym)))))))
710
711
712(defcommand "Editor Describe Symbol" (p)
713  "Describe the previous s-expression if it is a symbol in the editor Lisp."
714  "Describe the previous s-expression if it is a symbol in the editor Lisp."
715  (declare (ignore p))
716  (with-mark ((mark1 (current-point))
717              (mark2 (current-point)))
718    (mark-symbol mark1 mark2)
719    (with-input-from-region (s (region mark1 mark2))
720      (in-lisp
721       (let ((thing (read s)))
722         (if (symbolp thing)
723             (with-pop-up-display (*standard-output*)
724               (describe thing))
725             (if (and (consp thing)
726                      (or (eq (car thing) 'quote)
727                          (eq (car thing) 'function))
728                      (symbolp (cadr thing)))
729                 (with-pop-up-display (*standard-output*)
730                   (describe (cadr thing)))
731                 (editor-error "~S is not a symbol, or 'symbol, or #'symbol."
732                               thing))))))))
733
734;;; MARK-SYMBOL moves mark1 and mark2 around the previous or current symbol.
735;;; However, if the marks are immediately before the first constituent char
736;;; of the symbol name, we use the next symbol since the marks probably
737;;; correspond to the point, and Hemlock's cursor display makes it look like
738;;; the point is within the symbol name.  This also tries to ignore :prefix
739;;; characters such as quotes, commas, etc.
740;;;
741(defun mark-symbol (mark1 mark2)
742  (pre-command-parse-check mark1)
743  (with-mark ((tmark1 mark1)
744              (tmark2 mark1))
745    (cond ((and (form-offset tmark1 1)
746                (form-offset (move-mark tmark2 tmark1) -1)
747                (or (mark= mark1 tmark2)
748                    (and (find-attribute tmark2 :lisp-syntax
749                                         #'(lambda (x) (not (eq x :prefix))))
750                         (mark= mark1 tmark2))))
751           (form-offset mark2 1))
752          (t
753           (form-offset mark1 -1)
754           (find-attribute mark1 :lisp-syntax
755                           #'(lambda (x) (not (eq x :prefix))))
756           (form-offset (move-mark mark2 mark1) 1)))))
757
758
759(defcommand "Editor Describe" (p)
760  "Call Describe on a Lisp object.
761  Prompt for an expression which is evaluated to yield the object."
762  "Prompt for an object to describe."
763  (declare (ignore p))
764  (in-lisp
765   (let* ((exp (prompt-for-expression
766                :prompt "Object: "
767                :help "Expression to evaluate to get object to describe."))
768          (obj (eval exp)))
769     (with-pop-up-display (*standard-output*)
770       (describe obj)))))
771
772(defcommand "Filter Region" (p)
773  "Apply a Lisp function to each line of the region.
774  An expression is prompted for which should evaluate to a Lisp function
775  from a string to a string.  The function must neither modify its argument
776  nor modify the return value after it is returned."
777  "Call prompt for a function, then call Filter-Region with it and the region."
778  (declare (ignore p))
779  (let* ((exp (prompt-for-expression
780               :prompt "Function: "
781               :help "Expression to evaluate to get function to use as filter."))
782         (fun (in-lisp (eval exp)))
783         (region (current-region)))
784    (let* ((start (copy-mark (region-start region) :left-inserting))
785           (end (copy-mark (region-end region) :left-inserting))
786           (region (region start end))
787           (undo-region (copy-region region)))
788      (filter-region fun region)
789      (make-region-undo :twiddle "Filter Region" region undo-region))))
Note: See TracBrowser for help on using the repository browser.