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