1 ;; Copyright William F. Schelter. 1994
2 ;; Licensed by GNU public license.
4 ;; You should copy isp-complete.el to the emacs/lisp directory.
6 ;; Some commands and macros for dealing with lisp
7 ;; M-X run : run gcl or another lisp
8 ;; m-c-x ; evaluate defun in the other window or in the last lisp which you were using.
9 ;; m-c-x ; with a numeric arg : compile the current defun in the other window
10 ;; m-c-d ; disassemble in other window.
11 ;; M-x macroexpand-next : macro expand the next sexp in other window.
12 ;; C-h d Find documentation on symbol where the cursor is.
13 ;; C-h / Find documentation on all strings containing a given string.
14 ;; M-p complete the current input by looking back through the buffer to see what was last typed
15 ;; using this prompt and this beginning. Useful in shell, in lisp, in gdb,...
18 (setq lisp-mode-hook
'remote-lisp
)
20 (autoload 'lisp-complete
"lisp-complete" nil t
)
21 (autoload 'smart-complete
"smart-complete" nil t
)
23 ;(global-set-key "\ep" 'lisp-complete)
24 (global-set-key "\ep" 'smart-complete
)
26 (defun remote-lisp (&rest l
)
27 (and (boundp 'lisp-mode-map
)
30 (define-key lisp-mode-map
"\e\C-d" 'lisp-send-disassemble
)
31 (define-key lisp-mode-map
"\e\C-x" 'lisp-send-defun-compile
)
32 (make-local-variable 'lisp-package
)
33 (setq lisp-package nil
)
34 (and (boundp 'remote-lisp-hook
) (funcall remote-lisp-hook
))
38 (defvar search-back-for-lisp-package-p nil
)
40 ;; look at the beginning of buffer to try to find an in package statement
41 (defun get-buffer-package ()
43 "Returns what it thinks is the lisp package for the current buffer.
44 It caches this information in the local variable `lisp-package'. It
45 obtains the information from searching for the first in-package from
46 the beginning of the file. Since in common lisp, there is only
47 supposed to be one such statement, it should be able to determine
48 this. By setting lisp-package to t, you may disable its search. This
49 will also disable the automatic inclusion of an in-package statement
50 in the tmp-lisp-file, used for sending forms to the current
53 (cond ((eq lisp-package t
) nil
)
54 (search-back-for-lisp-package-p
56 (cond ((re-search-backward "^[ \t]*(in-package " nil t
)
57 (goto-char (match-end 0))
58 (read (current-buffer))))))
59 (lisp-package lisp-package
)
65 (goto-char (point-min))
67 (if (and (setq success
(search-forward "(in-package " 1000 t
))
70 (looking-at "[ \t]*;"))))
71 (setq found
(read (current-buffer))))
72 (if (>= (point) 980) (setq found t
))
73 (or success
(setq found t
))
79 "Run an inferior Lisp process, input and output via buffer *lisp*."
80 (interactive "sEnter name of file to run: ")
82 ;; in emacs 19 uncomment:
84 (setq lisp-mode-hook
'remote-lisp
)
85 (switch-to-buffer (make-sshell (concat arg
"-lisp") arg nil
"-i"))
86 (make-local-variable 'shell-prompt-pattern
)
87 (setq sshell-prompt-pattern
"^[^#%)>]*[#%)>]+ *")
88 (cond ((or (string-match "maxima" arg
) (string-match "affine" arg
)
89 (save-excursion (sleep-for 2)
90 (re-search-backward "maxima"
91 (max 1 (- (point) 300))
93 (require 'maxima-mode
)
94 (inferior-maxima-mode)
95 (goto-char (point-max))
98 (if (boundp 'inferior-lisp-mode
)
100 (funcall lisp-mode-hook
))
103 (defun lisp-send-disassemble (arg)
106 ( lisp-send-defun-compile
"disassemble-h")
107 ( lisp-send-defun-compile
"disassemble"))
110 (defvar time-to-throw-away nil
)
111 (defvar telnet-new-line
"")
113 (defun lisp-send-defun-compile (arg)
115 "Send the current defun (or other form) to the lisp-process. If there
116 is a numeric arg, the form (compile function-name) is also sent. The
117 value of lisp-process will be the process of the other exposed window (if
118 there is one) or else the global value of lisp-process. If the
119 ...received message is not received, probably either the reading of
120 the form caused an error. If the process does not have telnet in
121 its name, then we write a tmp file and load it.
122 If :sdebug is in *features*, then si::nload is used instead of
123 ordinary load, in order to record line information for debugging.
125 The value of `lisp-package' if non nil, will be used in putting an
126 in-package statement at the front of the tmp file to be loaded.
127 `lisp-package' is determined automatically on a per file basis,
128 by get-buffer-package.
133 (let* ((proc (or (get-buffer-process (current-buffer)) lisp-process
))
135 (this-lisp-process proc
)
136 (lisp-buffer (process-buffer this-lisp-process
))
141 (let ((end (dot)) (buffer (current-buffer))
142 (proc (get-process this-lisp-process
)))
143 (setq lisp-process proc
)
146 (cond ((and arg
(looking-at "(def")) (setq def t
))
148 (cond (def (forward-char 2)(forward-sexp 1)
149 (setq fun
(read buffer
))
150 (setq fun
(prin1-to-string fun
))
152 "For the lisp-process %s: %s"
153 (prin1-to-string this-lisp-process
) fun
)))))
154 (cond ((equal (char-after (1- end
)) ?
\n)
155 (setq end
(1- end
)) ))
157 (my-send-region this-lisp-process beg end
)
161 (send-string this-lisp-process
162 (concat ";;end of form" "\n" telnet-new-line
))
164 (if (numberp arg
) (setq arg
"compile"))
165 (send-string this-lisp-process
(concat "(" arg
"'" fun
")"
167 (and time-to-throw-away
168 (string-match "telnet"(buffer-name (process-buffer proc
)))
169 (dump-output proc time-to-throw-away
))
170 (cond (nil (get-buffer-window lisp-buffer
)
171 (select-window (get-buffer-window lisp-buffer
))
172 (goto-char (point-max)))
175 (fset 'lisp-eval-defun
(symbol-function 'lisp-send-defun-compile
))
177 (defvar telnet-new-line
"")
178 (defvar tmp-lisp-file
(concat "/tmp/" (user-login-name) ".lsp"))
180 (defun get-buffer-clear (name)
181 (let ((cb (current-buffer))
182 (buf (get-buffer-create name
)))
188 (defmacro my-with-output-to-temp-buffer
(name &rest body
)
191 (list (list 'standard-output
(list 'get-buffer-clear name
))))
195 (defun my-send-region (proc beg end
)
196 (cond ((or (string-match "telnet" (process-name proc
)))
197 (send-region proc beg end
))
199 (let ((package (get-buffer-package)))
201 (my-with-output-to-temp-buffer "*tmp-gcl*"
202 (if (and package
(not (eq package t
)))
203 (prin1 (list 'in-package package
)))
206 (let ((na (buffer-file-name (current-buffer))))
207 (if na
(expand-file-name na
)
208 (buffer-name (current-buffer))))
210 (princ (- (count-lines (point-min) (+ beg
5)) 1))
212 (set-buffer "*tmp-gcl*")
213 (write-region (point-min) (point-max) tmp-lisp-file nil nil
)))
214 (write-region beg end tmp-lisp-file t nil
)
215 (message "sending ..")
218 (concat "(lisp::let ((*load-verbose* nil)) (#+sdebug si::nload #-sdebug load \""
220 "\")#+gcl(setq si::*no-prompt* t)(values))\n ")
222 (message (format "PACKAGE: %s ..done"
223 (if (or (not package
) (eq package t
))
230 (defun dump-output (proc seconds
)
231 "dump output for PROCESS for SECONDS or to \";;end of form\""
232 (let ((prev-filter (process-filter proc
)) (already-waited 0))
233 (unwind-protect (progn (set-process-filter proc
'dump-filter
)
234 (while (< already-waited seconds
)
235 (sleep-for 1)(setq already-waited
236 (1+ already-waited
))))
237 (set-process-filter proc prev-filter
))))
241 (defun dump-filter (proc string
)
242 ; (setq she (cons string she))
243 (let ((ind (string-match ";;end of form" string
)))
244 (cond (ind (setq string
(substring
249 (message "... received.")
250 (setq already-waited
1000)
251 (set-process-filter proc prev-filter
)
252 (cond (prev-filter (funcall prev-filter proc string
))
257 ;;(process-filter (get-process "lisp"))
258 (defun macroexpand-next ()
259 "macroexpand current form"
264 (message "sending macro")
266 (let* ((current-lisp-process
267 (or (get-buffer-process (current-buffer))
268 (prog2 (other-window 1)
269 (get-buffer-process (current-buffer))
271 (send-string current-lisp-process
"(macroexpand '")
272 (send-region current-lisp-process beg
(point) )
273 (send-string current-lisp-process
")\n")))))
275 (defun delete-comment-char (arg)
276 (while (and (> arg
0) (looking-at comment-start
)) (delete-char 1)
277 (setq arg
(1- arg
))))
279 (defun mark-long-comment ()
283 (while(and (not (eobp))
284 (or (looking-at comment-start
)
285 ;(looking-at "[ ]*\n")
290 (while(and (not (bobp))
291 (or (looking-at comment-start
)
292 ;(looking-at "[ ]*\n")
295 (or (bobp )(forward-line 1))))
298 (defun fill-long-comment ()
301 (let ((beg (min (dot) (mark)))
302 (end (max (dot) (mark))) (n 0)m
)
303 (narrow-to-region beg end
)
304 (goto-char (point-min))
305 (while (looking-at ";")
307 (setq n
(- (point) beg
))
308 (goto-char (point-min))
312 (cond ((looking-at ";")
314 (cond ((looking-at " ")(delete-char 1)(setq m
0)))
318 (fill-region (dot-min) (dot-max))
319 (goto-char (point-min))
321 (cond ((looking-at "\n")
325 (goto-char (point-min))
326 (set-mark (point-max))
329 (defun comment-region (arg)
330 "Comments the region, with a numeric arg deletes up to arg comment
331 characters from the beginning of each line in the region. The region stays,
332 so a second comment-region adds another comment character"
337 (comment-region1 beg end arg
))))
339 (defun comment-region1 (beg end arg
)
343 (setq end beg beg oth
))))
344 (narrow-to-region beg end
)
349 (delete-comment-char arg
))
350 (t (insert-string comment-start
)))
351 (if (< end
(dot)) (setq ok nil
)
352 (if (search-forward "\n" end t
) nil
(setq ok nil
))) )
355 (defun trace-expression ()
362 (let* ((proc (get-buffer-process (current-buffer)))
363 (current-lisp-process (or proc lisp-process
)))
365 (message "Tracing: %s" (buffer-substring (point) end
))
366 (send-string current-lisp-process
"(trace ")
367 (send-region current-lisp-process
(point) end
)
368 (send-string current-lisp-process
")\n")))))