1 ;;; -*- Mode: Emacs-Lisp -*-
4 ;;; ILISP send and support.
6 ;;; This file is part of ILISP.
7 ;;; Please refer to the file COPYING for copyrights and licensing
9 ;;; Please refer to the file ACKNOWLEGDEMENTS for an (incomplete) list
10 ;;; of present and past contributors.
12 ;;; $Id: ilisp-snd.el,v 1.6 2009-08-13 16:47:39 aidan Exp $
15 ;;;%% Package / Symbol support
17 ;;;---------------------------------------------------------------------------
18 ;;; Package hacks by Martin Atzmueller
20 ;;; 19990824 Marco Antoniotti
22 ;; Have compiled 21.4 code also work on XEmacs binaries with real support
23 ;; for multiple values, by avoiding runtime calls to #'values:
24 (eval-when-compile (when (eq 'list (symbol-function 'values))
25 (define-compiler-macro values (&rest args)
28 (defvar *ILISP-default-package* "COMMON-LISP-USER")
30 (defun ilisp-add-set-package-hook () ; Was: add-set-package-hook
32 (add-hook 'lisp-mode-hook
34 ;; ilisp-buffer must exist and be ready
35 (if (and (boundp 'ilisp-buffer)
36 (not (equal ilisp-buffer "*nil*"))
37 (not (ilisp-value 'ilisp-initializing t))
38 (ilisp-value 'ilisp-status) " :ready")
40 ;; and check the Package when in ILISP
41 (lisp-buffer-package)))))
43 ;;; ilisp-check-package-advanced --
44 ;;; treat DEFPACKAGE before IN-PACKAGE.
46 (defun ilisp-check-package-advanced (hash-defpackage-forms-list
47 hash-in-package-forms-list)
48 "Advanced check for packages in buffer.
49 It hanldes the special case of read-time conditionals - i.e. hash plus
50 or minus forms - as well as normal IN-PACKAGE or DEFPACKAGE forms."
52 (apply #'concat (nconc hash-defpackage-forms-list
53 hash-in-package-forms-list)))
55 (format (ilisp-value 'ilisp-package-command)
56 (format (ilisp-value 'ilisp-block-command) string)))
58 ;;; (string &optional message status and-go handler)
61 string "Finding Buffer package with hash-forms" 'pkg nil t)))
62 ;; there may have been an error: fix it.
63 (if (string-match (ilisp-value 'ilisp-error-regexp t) the-package)
65 (comint-send (ilisp-process) (ilisp-value 'comint-fix-error) t t 'fix "Fixing erroneous package-situation in inferior-lisp." t nil)
71 (npic-regexp (ilisp-value 'ilisp-no-package-in-core-regexp t)))
72 (if (and npic-regexp (string-match npic-regexp package))
74 (message (format "Buffer package not found. Using fallback-package: %s"
75 (ilisp-value 'ilisp-fallback-package)))
76 (values (ilisp-value 'ilisp-fallback-package) t))
78 (message "Buffer package: %s" package)
79 (values package nil)))))
82 (defun lisp-find-hash-form () ; Was: find-hash-form.
83 "Tries to find either an hash-form, e.g. #{+|-}-form, or a regular
84 in-package or defpackage form."
85 (let ((hash-form-regexp (ilisp-value 'ilisp-hash-form-regexp))
86 (in-package-regexp (ilisp-value 'ilisp-in-package-command-string t))
87 (defpackage-regexp (ilisp-value 'ilisp-defpackage-command-string t)))
88 (ignore-errors (backward-char))
89 (and (re-search-forward hash-form-regexp)
90 (let ((found (buffer-substring
92 (match-end 0) ; this is due to problems with
93 (backward-char) ; the #{+|-}-regexp.
94 (backward-sexp) ; now we are in front of the exp
99 (string-match in-package-regexp found)
100 (setq in-package-found-p t))
101 (string-match defpackage-regexp found))
103 (buffer-substring (point) (progn (forward-sexp) (point))))
108 (let ((post-form (buffer-substring (point)
113 ;; This is for the case '#{+|-}({'and'|'or'} expr*)'
114 (unless (string-match "^\\(#+[^(].\\)\\|\\(#-[^(].\\)"
116 (forward-sexp))) ; after the #{+|-}-form
117 (forward-sexp) ; after the *real* Lisp-exp
121 ;;; lisp-buffer-package-internal -- This function does not work well
122 ;;; for Scheme yet. After all the module system of Scheme is a system
123 ;;; of very high entropy.
125 ;;; 19990824 Marco Antoniotti
127 (defun lisp-buffer-package-internal ()
128 "Returns the package of the buffer.
129 If SEARCH-FROM-START is T then will search from the beginning of the
130 buffer, otherwise will search backwards from current point. This
131 function also tries to correctly handle read-time
132 conditionals and the relative order of DEFPACKAGE and IN-PACKAGE for
135 (setq mode-line-process 'ilisp-status)
136 (let* ((lisp-buffer-package t)
138 (hash-form-regexp (ilisp-value 'ilisp-hash-form-regexp t))
139 (in-package-regexp (ilisp-value 'ilisp-in-package-command-string t))
140 (defpackage-regexp (ilisp-value 'ilisp-defpackage-command-string t))
141 (hash-in-package-forms-list nil)
142 (hash-defpackage-forms-list nil)
143 (in-package-found-p nil)
145 (should-not-cache-p nil))
146 (if (not hash-form-regexp)
149 (goto-char (point-min))
153 (ignore-errors (lisp-find-hash-form)))
156 (string-match hash-form-regexp hash-expr)
157 (substring hash-expr (match-beginning 0)))))
159 (cond ((string-match "(in-package\\s-*)" hash-expr)
160 (setq should-not-cache-p t)
163 (when (and sub-expr (string-match in-package-regexp sub-expr))
164 (setq in-package-found-p t)
165 (push hash-expr hash-in-package-forms-list))
166 (when (and sub-expr (string-match defpackage-regexp sub-expr))
167 (push hash-expr hash-defpackage-forms-list))
170 (multiple-value-bind (package package-not-in-core-p)
171 (ilisp-check-package-advanced
172 (nreverse hash-defpackage-forms-list)
173 (nreverse hash-in-package-forms-list))
174 (let ((should-not-cache-p (or should-not-cache-p package-not-in-core-p)))
175 ;;; RED? (when (ilisp-value 'comint-errorp t)
176 ;;; (lisp-display-output package)
177 ;;; (error "No package"))
180 ;; There was a bug here, used to have the second *
181 ;; outside of the parens.
182 ;; CMUCL needs just that WITHIN the double-quotes
183 ;; the old regexp is (string-match "[ \n\t:\"]*\\([^
184 ;; \n\t\"]*\\)" package))
185 (string-match "\\([\"].[^\"]*[\"]\\)" package))
189 (1+ (match-beginning 1)) (1- (match-end 1)))))
190 ;; => without double-quotes
192 (values package should-not-cache-p)))))))
195 (defun set-package-lisp-always ()
196 "Set inferior LISP to a named package.
197 The package is set whether the buffer has a package or not!"
199 (let* ((default *ILISP-default-package*)
202 (format "Package [%s]: " (lisp-buffer-package)) ""))
203 (package (and (equal name "") default name)))
206 (ilisp-send (format (ilisp-value 'ilisp-in-package-command) package)
207 (format "Set %s's package to %s"
208 (buffer-name (ilisp-buffer))
211 (error "No package"))))
213 ;;; Martin Atzmueller code ends here.
214 ;;; --------------------------------------------------------------------------
217 (defun lisp-buffer-package ()
218 "Return the package for this buffer.
219 The package name is a string. If there is none, return NIL. This
220 caches the package unless 'ILISP-DONT-CACHE-PACKAGE' is non-nil, so
221 calling this more than once is cheap."
222 (cond ((and (not (eq buffer-package 'not-yet-computed))
223 (null lisp-dont-cache-package))
225 (ilisp-completion-package ilisp-completion-package)
226 ((or lisp-buffer-package
227 (memq major-mode ilisp-modes)
228 (not (memq major-mode lisp-source-modes)))
231 (make-local-variable 'buffer-package)
232 (make-local-variable 'buffer-mode-name)
233 (multiple-value-bind (package should-not-cache-p)
234 (lisp-buffer-package-internal)
235 (setq buffer-package (if (or should-not-cache-p lisp-dont-cache-package)
236 'not-yet-computed package))
237 ;; Display package in mode line
240 (concat (or buffer-mode-name
241 (setq buffer-mode-name mode-name))
247 (defun package-lisp ()
248 "Show current inferior LISP package."
250 (message "Inferior LISP package is %s"
251 (ilisp-send (ilisp-value 'ilisp-package-name-command)
252 "Finding inferior LISP package" 'pkg)))
254 ;;; set-package-lisp --
255 ;;; Left the original version, instead of M. Atzmueller's.
257 ;;; 19990824 Marco Antoniotti
258 (defun set-package-lisp (package)
259 "Set inferior LISP to package of buffer or a named package with prefix."
261 (let ((default (lisp-buffer-package)))
262 (if (or current-prefix-arg (null default))
265 (format "Package [%s]: " (lisp-buffer-package)) "")))
266 (list (if (equal name "") default name)))
269 (ilisp-send (format (ilisp-value 'ilisp-in-package-command) package)
270 (format "Set %s's package to %s"
271 (buffer-name (ilisp-buffer))
274 (error "No package")))
277 (defun set-buffer-package-lisp (package)
278 "Reset the current package of the current buffer.
279 With prefix specify manually."
280 (interactive (if current-prefix-arg
281 (list (read-from-minibuffer "Package: " ))
284 (setq buffer-package package
285 mode-name (concat (or buffer-mode-name mode-name) ":" package))
286 (setq buffer-package 'not-yet-computed)
287 (lisp-buffer-package)))
290 ;;;%Interface functions
292 (defun lisp-string-to-symbol (string)
293 "Convert STRING to a symbol, (package delimiter symbol).
294 'package' is either package:symbol or from the current buffer."
295 (let* ((start (if (ilisp-value 'ilisp-package-separator-regexp t)
296 (string-match (ilisp-value 'ilisp-package-separator-regexp t)
298 (end (if start (match-end 0))))
303 (substring string 0 start))
304 (substring string start end)
305 (substring string end))
306 (let ((package (lisp-buffer-package)))
307 (lisp-symbol package (if package "::") string)))))
310 (defun lisp-symbol-to-string (symbol)
311 "Convert SYMBOL to a string."
312 (apply 'concat symbol))
315 (defun lisp-buffer-symbol (symbol)
316 "Return SYMBOL as a string qualified for the current buffer."
317 (let ((symbol-name (lisp-symbol-name symbol))
318 (pkg (lisp-symbol-package symbol))
319 (delimiter (lisp-symbol-delimiter symbol)))
320 (cond ((string= pkg (lisp-buffer-package)) symbol-name)
321 ((string= pkg "") (concat ":" symbol-name))
322 (pkg (concat pkg delimiter symbol-name))
326 (defun lisp-previous-symbol (&optional stay)
327 "Return the immediately preceding symbol encoding.
328 The result is encoded as ((package delimiter symbol) function-p start end).
329 If STAY is T, the end of the symbol will be point."
331 (if (or (and (memq major-mode ilisp-modes)
332 (= (point) (process-mark (get-buffer-process
335 (skip-chars-backward " \t\n")
336 (or (bobp) (memq (char-after (1- (point))) '(?\) ?\")))))
338 (let* ((delimiters (ilisp-value 'ilisp-symbol-delimiters))
340 (if (not stay) (skip-chars-forward delimiters))
343 (skip-chars-backward delimiters)
345 (prefix (if (not (bobp)) (1- start)))
348 (or (eq (char-after prefix) ?\()
349 (and (eq (char-after prefix) ?')
351 (eq (char-after (1- prefix)) ?#)))
352 (not (looking-at "[^: \t\n]*:*\\*[^ \t\n]")))))
353 (cons (lisp-string-to-symbol (buffer-substring start end))
354 (list function-p start end))))))
358 (defun lisp-function-name ()
359 "Return the previous function symbol.
360 This is either after a #' or at the start of the current sexp. If there
361 is no current sexp, return NIL."
363 (let ((symbol (lisp-previous-symbol)))
364 (if (car (cdr symbol))
367 (if (and (memq major-mode ilisp-modes)
370 (get-buffer-process (current-buffer)))))
374 (lisp-string-to-symbol
375 (buffer-substring (point)
376 (progn (forward-sexp 1) (point)))))
380 (defun lisp-defun-name ()
381 "Return the name of the current defun."
384 (lisp-string-to-symbol (lisp-def-name t))))
387 ;;;%% ILISP initializations
389 (defun ilisp-initialized ()
390 "Return T if the current inferior LISP has been initialized."
391 (memq (buffer-name (ilisp-buffer)) ilisp-initialized))
394 (defun ilisp-load-init (dialect file)
395 "Add FILE to the files to be loaded into the inferior LISP.
396 The file(s) are 'init' files to be loaded when dialect is initialized.
397 If FILE is NIL, the entry will be removed."
398 (let ((old (assoc dialect ilisp-load-inits)))
402 (setq ilisp-load-inits (nconc ilisp-load-inits
403 (list (cons dialect file)))))
405 (setq ilisp-load-inits (delq old ilisp-load-inits))))))
408 (defun ilisp-binary (init var)
409 "Initialize VAR to the result of INIT if VAR is NIL."
410 (if (not (ilisp-value var t))
411 (let ((binary (ilisp-value init t)))
414 (ilisp-process) binary
416 (` (lambda (error wait message output last)
418 (not (string-match "\"[^\"]*\"" output)))
420 (lisp-display-output output)
421 (abort-commands-lisp "No binary"))
424 (1+ (match-beginning 0))
425 (1- (match-end 0))))))))))))
428 (defun ilisp-done-init ()
429 "Make sure that initialization is done, and, if not, dispatch another check."
431 (comint-send-code (get-buffer-process (current-buffer))
433 (when ilisp-initializing
434 (unless comint-errorp
435 (message "Finished initializing %s" (car ilisp-dialect)))
436 (setq ilisp-initializing nil
438 (cons (buffer-name (current-buffer)) ilisp-initialized)))))
442 (defun comint-send-code-init-function ()
443 "Stuff that is executed for initialization in the ilisp process buffer."
444 (let ((files ilisp-load-inits)
448 (when (not ilisp-init-binary-extension)
449 (setq ilisp-init-binary-extension
450 ilisp-binary-extension))
455 (expand-file-name (cdr file)
459 (lisp-file-extension (cdr file)
460 ilisp-binary-extension)
462 (if (file-newer-than-file-p binary source)
465 (ilisp-load-or-send (file-name-hack load-file))))
466 (comint-send-code (ilisp-process)
470 (setq ilisp-initializing nil)
471 (abort-commands-lisp)))))
473 (defun ilisp-init-internal (&optional sync)
474 "Send all of the stuff necessary to initialize."
478 (comint-sync (ilisp-process)
479 "\"Start sync\"" "[ \t\n]*\"Start sync\""
480 "\"End sync\"" "\"End sync\""))
481 (ilisp-binary 'ilisp-binary-command 'ilisp-binary-extension)
482 (ilisp-binary 'ilisp-init-binary-command
483 'ilisp-init-binary-extension)
485 ;; This gets executed in the process buffer
488 (function comint-send-code-init-function))
489 (set-ilisp-value 'ilisp-initializing t)) ; progn
491 (unless (ilisp-value 'ilisp-initializing t)
492 (abort-commands-lisp))))
495 (defun ilisp-init (&optional waitp forcep sync)
496 "Initialize the current inferior LISP.
497 If necessary load the files in 'ilisp-load-inits'. Optional WAITP
498 waits for initialization to finish. When called interactively, force
499 reinitialization. With a prefix, get the binary extensions again."
501 (list (if current-prefix-arg
503 (set-ilisp-value 'ilisp-init-binary-extension nil)
504 (set-ilisp-value 'ilisp-binary-extension nil)
507 (when (or forcep (not (ilisp-initialized)))
508 (message "Started initializing ILISP")
509 (unless ilisp-*directory*
510 (setq ilisp-*directory* (or (ilisp-directory "ilisp.elc" load-path)
511 (ilisp-directory "ilisp.el" load-path))))
512 (unless (ilisp-value 'ilisp-initializing t)
513 (ilisp-init-internal sync))
515 (while (ilisp-value 'ilisp-initializing t)
516 (accept-process-output)
520 (defun ilisp-init-and-sync ()
521 "Synchronize with the inferior LISP and then initialize."
522 (ilisp-init nil nil t))
525 (defun call-defun-lisp (arg)
526 "Put a call of the current defun in the inferior LISP and go there.
527 If it is a \(def* name form, look up reasonable forms of name in the
528 input history unless called with prefix ARG. If not found, use \(name
529 or *name* as the call. If is not a def* form, put the whole form in
532 (if (save-excursion (lisp-defun-begin) (looking-at "(def"))
533 (let* ((symbol (lisp-defun-name))
534 (name (lisp-symbol-name symbol))
535 (package (if (lisp-symbol-package symbol)
537 (lisp-symbol-package symbol) ":+\\)?")))
538 (variablep (string-match "^\\*" name))
539 (setfp (string-match "(setf \\([^\)]+\\)" name))
544 (substring name (match-beginning 1) (match-end 1)))
545 (lisp-match-ring (if (not arg)
546 (concat "(setf[ \t\n]*("
547 package name "[ \t\n]"))
548 (concat "(setf (" name)))
549 (variablep (lisp-match-ring (if (not arg)
550 (concat package name))
553 (let ((fun (concat "(" name)))
554 (setq name (regexp-quote name))
556 (if (not arg) (concat "(" package name "[ \t\n\)]"))
559 (lisp-match-ring (concat "(" package
564 (buffer-substring (lisp-defun-begin)
565 (lisp-end-defun-text t)))))
571 (defun ilisp-send (string &optional message status and-go handler)
572 "Send STRING to the ILISP buffer.
573 Also print MESSAGE set STATUS and return the result if AND-GO is NIL,
574 otherwise switch to ilisp if and-go is T and show message and results.
575 If AND-GO is 'dispatch, then the command will be executed without
576 waiting for results. If AND-GO is 'call, then a call will be
577 generated. If this is the first time an ilisp command has been
578 executed, the lisp will also be initialized from the files in
579 ilisp-load-inits. If there is an error, comint-errorp will be T and
580 it will be handled by HANDLER."
582 (let ((process (ilisp-process))
583 (dispatch (eq and-go 'dispatch)))
585 (message "%s" (if dispatch
586 (concat "Started " message)
588 ;; No completion table
589 (setq ilisp-original nil)
591 (cond ((memq and-go '(t call))
592 (comint-send process string nil nil status message handler)
593 (if (eq and-go 'call)
594 (call-defun-lisp nil)
595 (switch-to-lisp t t))
598 (let* ((save (ilisp-value 'ilisp-save-command t))
602 (if save (format save string) string)
603 ;; Interrupt without waiting
604 t (unless dispatch 'wait) status message handler)))
608 (ilisp-value 'ilisp-restore-command t)
609 ;; Martin Atzmueller 2000-01-22
610 ;; this was necessary to have it work in Emacs 20.3 smoothly
611 ;; old one: t nil 'restore "Restore" t t
613 ;; t (unless dispatch 'wait) 'restore "Restore" t t))
614 t (unless dispatch 'wait) 'restore "Restore" t t))
616 (while (not (cdr result))
617 ;; (sit-for 0) ; 19990912 Pretty useless.
618 (accept-process-output))
619 (comint-remove-whitespace (car result))))))))
623 (defun ilisp-load-or-send (file)
624 "Try to load FILE into the inferior LISP.
625 If the file is not accessible in the inferior LISP as determined by
626 ilisp-load-or-send-command, then visit the file and send the file over
627 the process interface."
630 (format (ilisp-value 'ilisp-load-or-send-command)
633 (ilisp-value 'ilisp-init-binary-extension t))
635 (set-ilisp-value 'ilisp-load-files
636 (nconc (ilisp-value 'ilisp-load-files t) (list file)))
638 (ilisp-process) command t nil 'load
639 (format "Loading %s" file)
641 (lambda (error wait message output last)
642 (let* ((file (first (last ilisp-load-files)))
643 (process (get-buffer-process (current-buffer)))
644 (case-fold-search t))
646 (string-match "nil" (car (lisp-last-line output))))
647 (let* ((old-buffer (get-file-buffer file))
648 (buffer (find-file-noselect file))
649 (string (save-excursion
652 (unless old-buffer (kill-buffer buffer))
653 (if (string= "" string)
654 (abort-commands-lisp (format "Can't find file %s" file))
657 (format ilisp-block-command string)
658 t nil 'send (format "Sending %s" file)
659 (function (lambda (error wait message output last)
662 (comint-display-error output)
664 (format "Error sending %s"
665 (first (last ilisp-load-files)))
667 (setq ilisp-load-files
668 (delq (first (last ilisp-load-files))
669 ilisp-load-files))))))))
670 (when error (ilisp-handler error wait message output last))
671 (setq ilisp-load-files (delq file ilisp-load-files)))))))))
673 ;;; end of file -- ilisp-snd.el --