1 ;;; gds.el -- frontend for Guile development in Emacs
3 ;;;; Copyright (C) 2003 Free Software Foundation, Inc.
5 ;;;; This library is free software; you can redistribute it and/or
6 ;;;; modify it under the terms of the GNU Lesser General Public
7 ;;;; License as published by the Free Software Foundation; either
8 ;;;; version 2.1 of the License, or (at your option) any later
11 ;;;; This library is distributed in the hope that it will be useful,
12 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ;;;; Lesser General Public License for more details.
16 ;;;; You should have received a copy of the GNU Lesser General Public
17 ;;;; License along with this library; if not, write to the Free
18 ;;;; Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
29 ;;;; Customization group setup.
32 "Customization options for Guile Emacs frontend."
36 ;;;; Communication with the (emacs gds-server) subprocess.
38 ;; The subprocess object.
39 (defvar gds-process nil
)
41 ;; Subprocess output goes into the `*GDS Process*' buffer, and
42 ;; is then read from there one form at a time. `gds-read-cursor' is
43 ;; the buffer position of the start of the next unread form.
44 (defvar gds-read-cursor nil
)
47 "Start (or restart, if already running) the GDS subprocess."
49 (if gds-process
(gds-shutdown))
50 (with-current-buffer (get-buffer-create "*GDS Process*")
53 (let ((process-connection-type nil
)) ; use a pipe
60 "(begin (use-modules (emacs gds-server)) (run-server))"))))
61 (setq gds-read-cursor
(point-min))
62 (set-process-filter gds-process
(function gds-filter
))
63 (set-process-sentinel gds-process
(function gds-sentinel
))
64 (set-process-coding-system gds-process
'latin-1-unix
))
66 ;; Shutdown the subprocess and cleanup all associated data.
67 (defun gds-shutdown ()
68 "Shut down the GDS subprocess."
74 ;; Kill the subprocess.
75 (process-kill-without-query gds-process
)
78 (kill-process gds-process
)
79 (accept-process-output gds-process
0 200))
81 (setq gds-process nil
))
83 ;; Subprocess output filter: inserts normally into the process buffer,
84 ;; then tries to reread the output one form at a time and delegates
85 ;; processing of each form to `gds-handle-input'.
86 (defun gds-filter (proc string
)
87 (with-current-buffer (process-buffer proc
)
89 (goto-char (process-mark proc
))
90 (insert-before-markers string
))
91 (goto-char gds-read-cursor
)
92 (while (let ((form (condition-case nil
93 (read (current-buffer))
97 (gds-handle-input form
)))
99 (setq gds-read-cursor
(point)))))
101 ;; Subprocess sentinel: do nothing. (Currently just here to avoid
102 ;; inserting un-`read'able process status messages into the process
104 (defun gds-sentinel (proc event
)
107 ;; Send input to the subprocess.
108 (defun gds-send (string)
109 (process-send-string gds-process string
))
112 ;;;; Multiple application scheduling.
114 ;; Here is how we schedule the display of multiple clients that are
115 ;; competing for user attention.
117 ;; - `gds-waiting' holds a list of clients that want attention but
118 ;; haven't yet got it. A client is added to this list for two
119 ;; reasons. (1) When it is blocked waiting for user input.
120 ;; (2) When it first connects to GDS, even if not blocked.
122 ;; - `gds-focus-client' holds the client, if any, that currently has
123 ;; the user's attention. A client can be given the focus if
124 ;; `gds-focus-client' is nil at the time that the client wants
125 ;; attention, or if another client relinquishes it. A client can
126 ;; relinquish the focus in two ways. (1) If the client application
127 ;; says that it is no longer blocked, and a small time passes without
128 ;; it becoming blocked again. (2) If the user explicitly `quits'
130 (defvar gds-focus-client nil
)
131 (defvar gds-waiting nil
)
133 (defun gds-request-focus (client)
134 (cond ((eq client gds-focus-client
)
135 ;; CLIENT already has the focus. Display its buffer.
136 (gds-display-buffers))
138 ;; Another client has the focus. Add CLIENT to `gds-waiting'.
139 (or (memq client gds-waiting
)
140 (setq gds-waiting
(append gds-waiting
(list client
)))))
142 ;; Give focus to CLIENT and display its buffer.
143 (setq gds-focus-client client
)
144 (gds-display-buffers))))
146 ;; Explicitly give up focus.
149 (if (or (car gds-waiting
)
150 (not (gds-client-blocked))
152 "Client is blocked and no others are waiting. Still quit? "))
154 (bury-buffer (current-buffer))
155 ;; Pass on the focus.
156 (setq gds-focus-client
(car gds-waiting
)
157 gds-waiting
(cdr gds-waiting
))
158 ;; If this client is blocked, add it back into the waiting list.
159 (if (gds-client-blocked)
160 (gds-request-focus gds-client
))
161 ;; If there is a new focus client, request display for it.
163 (gds-request-focus gds-focus-client
)))))
166 ;;;; GDS protocol dispatch.
168 ;; General dispatch function called by the subprocess filter.
169 (defun gds-handle-input (form)
170 (let ((client (car form
)))
172 (let* ((proc (cadr form
))
174 (buf (gds-client-buffer client proc args
)))
175 (if buf
(gds-handle-client-input buf client proc args
))))))
177 (defun gds-handle-client-input (buf client proc args
)
178 (with-current-buffer buf
179 (with-current-buffer gds-transcript
180 (goto-char (point-max))
181 (let ((inhibit-read-only t
))
182 (insert (format "<%S %S %S>" client proc args
) "\n")))
183 (cond (;; (name ...) - Client name.
185 (setq gds-pid
(cadr args
))
186 (gds-promote-view 'interaction
)
187 (gds-request-focus client
))
189 (;; (current-module ...) - Current module.
190 (eq proc
'current-module
)
191 (setq gds-current-module
(car args
)))
193 (;; (stack ...) - Stack at an error or breakpoint.
195 (setq gds-stack args
)
196 (gds-promote-view 'stack
))
198 (;; (modules ...) - Application's loaded modules.
201 (or (assoc (car args
) gds-modules
)
202 (setq gds-modules
(cons (list (car args
)) gds-modules
)))
203 (setq args
(cdr args
))))
205 (;; (output ...) - Last printed output.
207 (setq gds-output
(car args
))
208 (gds-add-view 'messages
))
210 (;; (status ...) - Application status indication.
212 (setq gds-status
(car args
))
213 (if (eq gds-status
'running
)
214 (gds-delete-view 'browser
)
215 (gds-add-view 'browser
))
216 (if (eq gds-status
'waiting-for-input
)
218 (gds-promote-view 'stack
)
220 (gds-request-focus client
))
222 (gds-delete-view 'stack
)
223 (gds-update-buffers-in-a-while)))
225 (;; (module MODULE ...) - The specified module's bindings.
227 (let ((minfo (assoc (car args
) gds-modules
)))
229 (setcdr (cdr minfo
) (cdr args
)))))
231 (;; (closed) - Client has gone away.
233 (setq gds-status
'closed
)
236 (delq (assq client gds-buffers
) gds-buffers
))
237 (if (eq client gds-focus-client
)
240 (;; (eval-results ...) - Results of evaluation.
241 (eq proc
'eval-results
)
242 (gds-display-results client args
))
244 ((eq proc
'completion-result
)
245 (setq gds-completion-results
(or (car args
) t
)))
250 ;;;; Per-client buffer state.
252 ;; This section contains code that is specific to each Guile client's
253 ;; buffer but independent of any particular `view'.
255 ;; Alist mapping each client port number to corresponding buffer.
256 (defvar gds-buffers nil
)
258 (define-derived-mode gds-mode
261 "Major mode for interacting with a Guile client application.")
263 (defvar gds-client nil
264 "GDS client's port number.")
265 (make-variable-buffer-local 'gds-client
)
267 (defvar gds-status nil
268 "GDS client's latest status, one of the following symbols.
269 `running' - Application is running.
270 `waiting-for-input' - Application is blocked waiting for instruction
272 `ready-for-input' - Application is not blocked but can also accept
273 asynchronous instructions from the frontend.")
274 (make-variable-buffer-local 'gds-status
)
276 (defvar gds-transcript nil
277 "Transcript buffer for this GDS client.")
278 (make-variable-buffer-local 'gds-transcript
)
280 ;; Return client buffer for specified client and protocol input.
281 (defun gds-client-buffer (client proc args
)
283 ;; Introduction from client - create a new buffer.
284 (with-current-buffer (generate-new-buffer (car args
))
286 (setq gds-client client
)
289 (expand-file-name (concat "~/.gds-transcript-" (car args
)))))
290 (with-current-buffer gds-transcript
291 (goto-char (point-max))
292 (insert "\nTranscript:\n"))
294 (cons (cons client
(current-buffer))
297 ;; Otherwise there should be an existing buffer that we can
299 (let ((existing (assq client gds-buffers
)))
300 (if (buffer-live-p (cdr existing
))
302 (setq gds-buffers
(delq existing gds-buffers
))
303 (gds-client-buffer client
'name
'("(GDS buffer killed)"))))))
305 (defun gds-client-blocked ()
306 (eq gds-status
'waiting-for-input
))
308 (defvar gds-delayed-update-timer nil
)
310 (defvar gds-delayed-update-buffers nil
)
312 (defun gds-update-delayed-update-buffers ()
313 (while gds-delayed-update-buffers
314 (with-current-buffer (car gds-delayed-update-buffers
)
315 (setq gds-delayed-update-buffers
316 (cdr gds-delayed-update-buffers
))
317 (gds-update-buffers))))
319 (defun gds-update-buffers ()
320 (if (timerp gds-delayed-update-timer
)
321 (cancel-timer gds-delayed-update-timer
))
322 (setq gds-delayed-update-timer nil
)
323 (let ((view (car gds-views
))
324 (inhibit-read-only t
))
325 (cond ((eq view
'stack
)
327 ((eq view
'interaction
)
328 (gds-insert-interaction))
330 (gds-insert-modules))
332 (gds-insert-messages))
334 (error "Bad GDS view %S" view
)))
337 (force-mode-line-update t
)))
339 (defun gds-update-buffers-in-a-while ()
340 (or (memq (current-buffer) gds-delayed-update-buffers
)
341 (setq gds-delayed-update-buffers
342 (cons (current-buffer) gds-delayed-update-buffers
)))
343 (if (timerp gds-delayed-update-timer
)
345 (setq gds-delayed-update-timer
346 (run-at-time 0.5 nil
(function gds-update-delayed-update-buffers
)))))
348 (defun gds-display-buffers ()
350 (let ((gds-focus-buffer (cdr (assq gds-focus-client gds-buffers
))))
351 ;; If there's already a window showing the buffer, use it.
352 (let ((window (get-buffer-window gds-focus-buffer t
)))
355 (make-frame-visible (window-frame window
))
356 (select-frame (window-frame window
))
357 (select-window window
))
358 ;(select-window (display-buffer gds-focus-buffer))
359 (display-buffer gds-focus-buffer
)))
360 ;; If there is an associated source buffer, display it as well.
361 (if (and (eq (car gds-views
) 'stack
)
362 gds-frame-source-overlay
363 (> (overlay-end gds-frame-source-overlay
) 0))
364 (let ((window (display-buffer
365 (overlay-buffer gds-frame-source-overlay
))))
366 (set-window-point window
367 (overlay-start gds-frame-source-overlay
)))))))
370 ;;;; Management of `views'.
372 ;; The idea here is to keep the buffer describing a Guile client
373 ;; relatively uncluttered by only showing one kind of information
374 ;; about that client at a time. Menu items and key sequences are
375 ;; provided to switch easily between the available views.
377 (defvar gds-views nil
378 "List of available views for a GDS client. Each element is one of
379 the following symbols.
380 `interaction' - Interaction with running client.
381 `stack' - Call stack view.
382 `browser' - Modules and bindings browser view.
383 `breakpoints' - List of set breakpoints.
384 `messages' - Non-GDS-protocol output from the debugger.")
385 (make-variable-buffer-local 'gds-views
)
387 (defun gds-promote-view (view)
388 (setq gds-views
(cons view
(delq view gds-views
))))
390 (defun gds-switch-to-view (view)
391 (or (memq view gds-views
)
392 (error "View %S is not available" view
))
393 (gds-promote-view view
)
394 (gds-update-buffers))
396 (defun gds-add-view (view)
397 (or (memq view gds-views
)
398 (setq gds-views
(append gds-views
(list view
)))))
400 (defun gds-delete-view (view)
401 (setq gds-views
(delq view gds-views
)))
404 ;;;; `Interaction' view.
406 ;; This view provides interaction with a normally running Guile
407 ;; client, in other words one that is not stopped in the debugger but
408 ;; is still available to take input from GDS (usually via a thread for
409 ;; that purpose). The view supports evaluation, help requests,
410 ;; control of `debug-on-exception' function, and methods for breaking
411 ;; into the running code.
413 (defvar gds-current-module
"()"
414 "GDS client's current module.")
415 (make-variable-buffer-local 'gds-current-module
)
418 "GDS client's process ID.")
419 (make-variable-buffer-local 'gds-pid
)
421 (defvar gds-debug-exceptions nil
422 "Whether to debug exceptions.")
423 (make-variable-buffer-local 'gds-debug-exceptions
)
425 (defvar gds-exception-keys
"signal misc-error"
426 "The exception keys for which to debug a GDS client.")
427 (make-variable-buffer-local 'gds-exception-keys
)
429 (defun gds-insert-interaction ()
431 ;; Insert stuff for interacting with a running (non-blocked) Guile
433 (widget-insert (buffer-name)
435 (cdr (assq gds-status
436 '((running .
"running (cannot accept input)")
437 (waiting-for-input .
"waiting for input")
438 (ready-for-input .
"running")
439 (closed .
"closed"))))
443 (widget-create 'push-button
444 :notify
(function gds-sigint
)
447 (widget-create 'push-button
448 :notify
(function gds-async-break
)
451 (widget-create 'checkbox
452 :notify
(function gds-toggle-debug-exceptions
)
453 gds-debug-exceptions
)
454 (widget-insert " Debug exception keys: ")
455 (widget-create 'editable-field
456 :notify
(function gds-set-exception-keys
)
458 (widget-insert "\n"))
460 (defun gds-sigint (w &rest ignore
)
462 (signal-process gds-pid
2))
464 (defun gds-async-break (w &rest ignore
)
466 (gds-send (format "(%S async-break)\n" gds-focus-client
)))
468 (defun gds-toggle-debug-exceptions (w &rest ignore
)
470 (setq gds-debug-exceptions
(widget-value w
))
471 (gds-eval-expression (concat "(use-modules (ice-9 debugger))"
476 (defun gds-set-exception-keys (w &rest ignore
)
478 (setq gds-exception-keys
(widget-value w
)))
480 (defun gds-view-interaction ()
482 (gds-switch-to-view 'interaction
))
487 ;; This view shows the Guile call stack after the application has hit
488 ;; an error, or when it is stopped in the debugger.
490 (defvar gds-stack nil
491 "GDS client's stack when last stopped.")
492 (make-variable-buffer-local 'gds-stack
)
494 (defun gds-insert-stack ()
496 (let ((frames (car gds-stack
))
497 (index (cadr gds-stack
))
498 (flags (caddr gds-stack
))
500 (cond ((memq 'application flags
)
501 (widget-insert "Calling procedure:\n"))
502 ((memq 'evaluation flags
)
503 (widget-insert "Evaluating expression:\n"))
504 ((memq 'return flags
)
505 (widget-insert "Return value: "
506 (cadr (memq 'return flags
))
509 (widget-insert "Stack: " (prin1-to-string flags
) "\n")))
511 (gds-show-selected-frame (caddr (nth index frames
)))
513 (setq frame
(car frames
)
516 items
(cons (list 'item
517 (let ((s (cadr frame
)))
518 (put-text-property 0 1 'index i s
)
521 (setq items
(nreverse items
))
522 (apply (function widget-create
)
524 :value
(cadr (nth index items
))
525 :notify
(function gds-select-stack-frame
)
528 (goto-char (point-min))))
530 (defun gds-select-stack-frame (widget &rest ignored
)
531 (let* ((s (widget-value widget
))
532 (ind (memq 'index
(text-properties-at 0 s
))))
533 (gds-send (format "(%S debugger-command frame %d)\n"
537 ;; Overlay used to highlight the source expression corresponding to
538 ;; the selected frame.
539 (defvar gds-frame-source-overlay nil
)
541 (defun gds-show-selected-frame (source)
542 ;; Highlight the frame source, if possible.
544 (file-readable-p (car source
)))
545 (with-current-buffer (find-file-noselect (car source
))
546 (if gds-frame-source-overlay
548 (setq gds-frame-source-overlay
(make-overlay 0 0))
549 (overlay-put gds-frame-source-overlay
'face
'highlight
))
550 ;; Move to source line. Note that Guile line numbering is
551 ;; 0-based, while Emacs numbering is 1-based.
554 (goto-line (+ (cadr source
) 1))
555 (move-to-column (caddr source
))
556 (move-overlay gds-frame-source-overlay
558 (if (not (looking-at ")"))
559 (save-excursion (forward-sexp 1) (point))
560 ;; It seems that the source coordinates for
561 ;; backquoted expressions are at the end of
562 ;; the sexp rather than the beginning...
563 (save-excursion (forward-char 1)
564 (backward-sexp 1) (point)))
566 (if gds-frame-source-overlay
567 (move-overlay gds-frame-source-overlay
0 0))))
569 (defun gds-view-stack ()
571 (gds-switch-to-view 'stack
))
574 ;;;; `Breakpoints' view.
576 ;; This view shows a list of breakpoints.
578 (defun gds-view-breakpoints ()
580 (gds-switch-to-view 'breakpoints
))
585 ;; This view shows a list of modules and module bindings.
587 (defcustom gds-module-filter
'(t (guile nil
) (ice-9 nil
) (oop nil
))
588 "Specification of which Guile modules the debugger should display.
589 This is a list with structure (DEFAULT EXCEPTION EXCEPTION...), where
590 DEFAULT is `t' or `nil' and each EXCEPTION has the structure (SYMBOL
591 DEFAULT EXCEPTION EXCEPTION...).
593 A Guile module name `(x y z)' is matched against this filter as
594 follows. If one of the top level EXCEPTIONs has SYMBOL `x', continue
595 by matching the rest of the module name, in this case `(y z)', against
596 that SYMBOL's DEFAULT and next level EXCEPTION list. Otherwise, if
597 the current DEFAULT is `t' display the module, and if the current
598 DEFAULT is `nil', don't display it.
600 This variable is usually set to exclude Guile system modules that are
601 not of primary interest when debugging application code."
605 (defun gds-show-module-p (name)
606 ;; Determine whether to display the NAMEd module by matching NAME
607 ;; against `gds-module-filter'.
608 (let ((default (car gds-module-filter
))
609 (exceptions (cdr gds-module-filter
)))
610 (let ((exception (assq (car name
) exceptions
)))
612 (let ((gds-module-filter (cdr exception
)))
613 (gds-show-module-p (cdr name
)))
616 (defvar gds-modules nil
617 "GDS client's module information.
618 Alist mapping module names to their symbols and related information.
621 (((guile) t sym1 sym2 ...)
623 ((ice-9 debug) nil sym3 sym4)
626 The `t' or `nil' after the module name indicates whether the module is
627 displayed in expanded form (that is, showing the bindings in that
628 module). The syms are actually all strings because some Guile symbols
629 are not readable by Emacs.")
630 (make-variable-buffer-local 'gds-modules
)
632 (defun gds-insert-modules ()
633 (let ((p (if (eq (window-buffer (selected-window)) (current-buffer))
636 (modules gds-modules
))
638 (insert "Modules:\n")
640 (let ((minfo (car modules
)))
641 (if (gds-show-module-p (car minfo
))
642 (let ((w (widget-create 'push-button
643 :notify
(function gds-module-notify
)
647 (widget-put w
:module
(cons gds-client
(car minfo
)))
648 (widget-insert " " (prin1-to-string (car minfo
)) "\n")
650 (let ((syms (cddr minfo
)))
652 (widget-insert " > " (car syms
) "\n")
653 (setq syms
(cdr syms
))))))))
654 (setq modules
(cdr modules
)))
658 (defun gds-module-notify (w &rest ignore
)
659 (let* ((module (widget-get w
:module
))
660 (client (car module
))
662 (minfo (assoc name gds-modules
)))
664 ;; Just toggle expansion state.
666 (setcar (cdr minfo
) (not (cadr minfo
)))
667 (gds-update-buffers))
668 ;; Set flag to indicate module expanded.
669 (setcdr minfo
(list t
))
670 ;; Get symlist from Guile.
671 (gds-send (format "(%S query-module %S)\n" client name
)))))
673 (defun gds-query-modules ()
675 (gds-send (format "(%S query-modules)\n" gds-focus-client
)))
677 (defun gds-view-browser ()
679 (or gds-modules
(gds-query-modules))
680 (gds-switch-to-view 'browser
))
683 ;;;; `Messages' view.
685 ;; This view shows recent non-GDS-protocol messages output from the
686 ;; (ice-9 debugger) code.
688 (defvar gds-output nil
689 "GDS client's recent output (printed).")
690 (make-variable-buffer-local 'gds-output
)
692 (defun gds-insert-messages ()
694 ;; Insert recent non-protocol output from (ice-9 debugger).
696 (goto-char (point-min)))
698 (defun gds-view-messages ()
700 (gds-switch-to-view 'messages
))
703 ;;;; Debugger commands.
705 ;; Typically but not necessarily used from the `stack' view.
709 (gds-send (format "(%S debugger-command continue)\n" gds-focus-client
)))
713 (gds-send (format "(%S debugger-command next 1)\n" gds-focus-client
)))
715 (defun gds-evaluate (expr)
716 (interactive "sEvaluate (in this stack frame): ")
717 (gds-send (format "(%S debugger-command evaluate %s)\n"
719 (prin1-to-string expr
))))
721 (defun gds-step-in ()
723 (gds-send (format "(%S debugger-command step 1)\n" gds-focus-client
)))
725 (defun gds-step-out ()
727 (gds-send (format "(%S debugger-command finish)\n" gds-focus-client
)))
729 (defun gds-trace-finish ()
731 (gds-send (format "(%S debugger-command trace-finish)\n"
734 (defun gds-frame-info ()
736 (gds-send (format "(%S debugger-command info-frame)\n" gds-focus-client
)))
738 (defun gds-frame-args ()
740 (gds-send (format "(%S debugger-command info-args)\n" gds-focus-client
)))
743 ;;;; Setting breakpoints.
745 (defun gds-set-breakpoint ()
747 (cond ((gds-in-source-buffer)
748 (gds-set-source-breakpoint))
750 (gds-set-stack-breakpoint))
752 (gds-set-module-breakpoint))
754 (error "No way to set a breakpoint from here"))))
756 (defun gds-in-source-buffer ()
757 ;; Not yet worked out what will be available in Scheme source
761 (defun gds-in-stack ()
763 (and (re-search-backward "^\\(Stack\\|Modules\\):" nil t
)
764 (looking-at "Stack"))))
766 (defun gds-in-modules ()
768 (and (re-search-backward "^\\(Stack\\|Modules\\):" nil t
)
769 (looking-at "Modules"))))
771 (defun gds-set-module-breakpoint ()
772 (let ((sym (save-excursion
774 (and (looking-at " > \\([^ \n\t]+\\)")
776 (module (save-excursion
777 (and (re-search-backward "^\\[[+---]\\] \\(([^)]+)\\)" nil t
)
780 (error "Couldn't find procedure name on current line"))
782 (error "Couldn't find module name for current line"))
785 (format "Behaviour for breakpoint at %s:%s (default debug-here): "
795 (gds-send (format "(%S set-breakpoint %s %s %s)\n"
802 ;;;; Evaluating code.
804 ;; The following commands send code for evaluation through the GDS TCP
805 ;; connection, receive the result and any output generated through the
806 ;; same connection, and display the result and output to the user.
808 ;; Where there are multiple Guile applications known to GDS, GDS by
809 ;; default sends code to the one that holds the debugging focus,
810 ;; i.e. `gds-focus-client'. Where no application has the focus,
811 ;; or the command is invoked with `C-u', GDS asks the user which
812 ;; application is intended.
814 (defun gds-read-client ()
815 (let* ((def (if gds-focus-client
816 (cdr (assq gds-focus-client gds-names
))))
818 (concat "Application for eval (default "
821 "Application for eval: "))
823 (completing-read prompt
824 (mapcar (function list
)
825 (mapcar (function cdr
) gds-names
))
828 (let (client (names gds-names
))
829 (while (and names
(not client
))
830 (if (string-equal (cdar names
) name
)
831 (setq client
(caar names
)))
832 (setq names
(cdr names
)))
835 (defun gds-choose-client (client)
836 (or ;; If client is an integer, it is the port number of the
838 (if (integerp client
) client
)
839 ;; Any other non-nil value indicates invocation with a prefix
840 ;; arg, which forces asking the user which application is
842 (if client
(gds-read-client))
843 ;; If ask not forced, and there is a client with the focus,
844 ;; default to that one.
846 ;; If there are no clients at this point, and we are allowed to
847 ;; autostart a captive Guile, do so.
848 (and (null gds-buffers
)
849 gds-autostart-captive
851 (gds-start-captive t
)
852 (while (null gds-buffers
)
853 (accept-process-output (get-buffer-process gds-captive
)
856 ;; If there is only one known client, use that one.
857 (if (and (car gds-buffers
)
858 (null (cdr gds-buffers
)))
860 ;; Last resort - ask the user.
863 (error "No application chosen.")))
865 (defun gds-module-name (start end
)
866 "Determine and return the name of the module that governs the
867 specified region. The module name is returned as a list of symbols."
868 (interactive "r") ; why not?
872 (while (and (not module-name
)
873 (beginning-of-defun-raw 1))
874 (if (looking-at "(define-module ")
877 (goto-char (match-end 0))
878 (read (current-buffer))))))
881 (defun gds-port-name (start end
)
882 "Return port name for the specified region of the current buffer.
883 The name will be used by Guile as the port name when evaluating that
885 (or (buffer-file-name)
886 (concat "Emacs buffer: " (buffer-name))))
888 (defun gds-eval-region (start end
&optional client
)
889 "Evaluate the current region."
891 (setq client
(gds-choose-client client
))
892 (let ((module (gds-module-name start end
))
893 (port-name (gds-port-name start end
))
897 (setq column
(current-column)) ; 0-based
899 (setq line
(count-lines (point-min) (point)))) ; 0-based
900 (gds-send (format "(%S eval %s %S %d %d %S)\n"
902 (if module
(prin1-to-string module
) "#f")
903 port-name line column
904 (buffer-substring-no-properties start end
)))))
906 (defun gds-eval-expression (expr &optional client
)
907 "Evaluate the supplied EXPR (a string)."
908 (interactive "sEvaluate expression: \nP")
909 (setq client
(gds-choose-client client
))
910 (gds-send (format "(%S eval #f \"Emacs expression\" 0 0 %S)\n"
913 (defun gds-eval-defun (&optional client
)
914 "Evaluate the defun (top-level form) at point."
920 (gds-eval-region (point) end client
))))
922 (defun gds-eval-last-sexp (&optional client
)
923 "Evaluate the sexp before point."
925 (gds-eval-region (save-excursion (backward-sexp) (point)) (point) client
))
930 ;; Help is implemented as a special case of evaluation, where we
931 ;; arrange for the evaluation result to be a known symbol that is
932 ;; unlikely to crop up otherwise. When the evaluation result is this
933 ;; symbol, we only display the output from the evaluation.
935 (defvar gds-help-symbol
'%-gds-help-%
936 "Symbol used by GDS to identify an evaluation response as help.")
938 (defun gds-help-symbol (sym &optional client
)
939 "Get help for SYM (a Scheme symbol)."
941 (let ((sym (thing-at-point 'symbol
))
942 (enable-recursive-minibuffers t
)
944 (setq val
(read-from-minibuffer
946 (format "Describe Guile symbol (default %s): " sym
)
947 "Describe Guile symbol: ")))
948 (list (if (zerop (length val
)) sym val
)
949 current-prefix-arg
)))
950 (gds-eval-expression (format "(begin (help %s) '%S)" sym gds-help-symbol
)
953 (defun gds-apropos (regex &optional client
)
954 "List Guile symbols matching REGEX."
956 (let ((sym (thing-at-point 'symbol
))
957 (enable-recursive-minibuffers t
)
959 (setq val
(read-from-minibuffer
961 (format "Guile apropos (regexp, default \"%s\"): " sym
)
962 "Guile apropos (regexp): ")))
963 (list (if (zerop (length val
)) sym val
)
964 current-prefix-arg
)))
965 (gds-eval-expression (format "(begin (apropos %S) '%S)" regex gds-help-symbol
)
968 (defvar gds-completion-results nil
)
970 (defun gds-complete-symbol (&optional client
)
971 "Complete the Guile symbol before point. Returns `t' if anything
972 interesting happened, `nil' if not."
974 (let* ((chars (- (point) (save-excursion
975 (while (let ((syntax (char-syntax (char-before (point)))))
976 (or (eq syntax ?w
) (eq syntax ?_
)))
981 (setq client
(gds-choose-client client
))
982 (setq gds-completion-results nil
)
983 (gds-send (format "(%S complete %s)\n" client
985 (buffer-substring-no-properties (- (point) chars
)
987 (while (null gds-completion-results
)
988 (accept-process-output gds-process
0 200))
989 (cond ((eq gds-completion-results t
)
991 ((stringp gds-completion-results
)
992 (if (<= (length gds-completion-results
) chars
)
994 (insert (substring gds-completion-results chars
))
995 (message "Sole completion")
997 ((= (length gds-completion-results
) 1)
998 (if (<= (length (car gds-completion-results
)) chars
)
1000 (insert (substring (car gds-completion-results
) chars
))
1003 (with-output-to-temp-buffer "*Completions*"
1004 (display-completion-list gds-completion-results
))
1008 ;;;; Display of evaluation and help results.
1010 (defun gds-display-results (client results
)
1011 (let ((helpp (and (= (length results
) 2)
1012 (= (length (cadr results
)) 1)
1013 (string-equal (caadr results
)
1014 (prin1-to-string gds-help-symbol
)))))
1015 (let ((buf (get-buffer-create (if helpp
1017 "*Guile Results*"))))
1023 (insert (car results
))
1026 (mapcar (function (lambda (value)
1027 (insert " => " value
"\n")))
1030 (setq results
(cddr results
)))
1031 (goto-char (point-min))
1032 (if (and helpp
(looking-at "Evaluating in "))
1033 (delete-region (point) (progn (forward-line 1) (point)))))
1035 (run-hooks 'temp-buffer-show-hook
)
1039 ;;;; Loading (evaluating) a whole Scheme file.
1041 (defcustom gds-source-modes
'(scheme-mode)
1042 "*Used to determine if a buffer contains Scheme source code.
1043 If it's loaded into a buffer that is in one of these major modes, it's
1044 considered a scheme source file by `gds-load-file'."
1045 :type
'(repeat function
)
1048 (defvar gds-prev-load-dir
/file nil
1049 "Holds the last (directory . file) pair passed to `gds-load-file'.
1050 Used for determining the default for the next `gds-load-file'.")
1052 (defun gds-load-file (file-name &optional client
)
1053 "Load a Scheme file into the inferior Scheme process."
1054 (interactive (list (car (comint-get-source "Load Scheme file: "
1055 gds-prev-load-dir
/file
1056 gds-source-modes t
))
1057 ; T because LOAD needs an
1059 current-prefix-arg
))
1060 (comint-check-source file-name
) ; Check to see if buffer needs saved.
1061 (setq gds-prev-load-dir
/file
(cons (file-name-directory file-name
)
1062 (file-name-nondirectory file-name
)))
1063 (setq client
(gds-choose-client client
))
1064 (gds-send (format "(%S load %S)\n" client file-name
)))
1067 ;;;; Scheme mode keymap items.
1069 (define-key scheme-mode-map
"\M-\C-x" 'gds-eval-defun
);gnu convention
1070 (define-key scheme-mode-map
"\C-x\C-e" 'gds-eval-last-sexp
);gnu convention
1071 (define-key scheme-mode-map
"\C-c\C-e" 'gds-eval-expression
)
1072 (define-key scheme-mode-map
"\C-c\C-r" 'gds-eval-region
)
1073 (define-key scheme-mode-map
"\C-c\C-l" 'gds-load-file
)
1074 (define-key scheme-mode-map
"\C-hg" 'gds-help-symbol
)
1075 (define-key scheme-mode-map
"\C-h\C-g" 'gds-apropos
)
1076 (define-key scheme-mode-map
"\e\t" 'gds-complete-symbol
)
1079 ;;;; GDS (Guile Interaction) mode keymap and menu items.
1081 (set-keymap-parent gds-mode-map widget-keymap
)
1083 (define-key gds-mode-map
"M" (function gds-query-modules
))
1085 (define-key gds-mode-map
"g" (function gds-go
))
1086 (define-key gds-mode-map
"q" (function gds-quit
))
1087 (define-key gds-mode-map
" " (function gds-next
))
1088 (define-key gds-mode-map
"e" (function gds-evaluate
))
1089 (define-key gds-mode-map
"i" (function gds-step-in
))
1090 (define-key gds-mode-map
"o" (function gds-step-out
))
1091 (define-key gds-mode-map
"t" (function gds-trace-finish
))
1092 (define-key gds-mode-map
"I" (function gds-frame-info
))
1093 (define-key gds-mode-map
"A" (function gds-frame-args
))
1095 (define-key gds-mode-map
"b" (function gds-set-breakpoint
))
1097 (define-key gds-mode-map
"vi" (function gds-view-interaction
))
1098 (define-key gds-mode-map
"vs" (function gds-view-stack
))
1099 (define-key gds-mode-map
"vb" (function gds-view-breakpoints
))
1100 (define-key gds-mode-map
"vB" (function gds-view-browser
))
1101 (define-key gds-mode-map
"vm" (function gds-view-messages
))
1103 (defvar gds-view-menu nil
1107 (setq gds-view-menu
(make-sparse-keymap "View"))
1108 (define-key gds-view-menu
[messages]
1109 '(menu-item "Messages" gds-view-messages
1110 :enable (memq 'messages gds-views)))
1111 (define-key gds-view-menu [browser]
1112 '(menu-item "Browser" gds-view-browser
1113 :enable (memq 'browser gds-views)))
1114 (define-key gds-view-menu [breakpoints]
1115 '(menu-item "Breakpoints" gds-view-breakpoints
1116 :enable (memq 'breakpoints gds-views)))
1117 (define-key gds-view-menu [stack]
1118 '(menu-item "Stack" gds-view-stack
1119 :enable (memq 'stack gds-views)))
1120 (define-key gds-view-menu [interaction]
1121 '(menu-item "Interaction" gds-view-interaction
1122 :enable (memq 'interaction gds-views))))
1124 (defvar gds-debug-menu nil
1125 "GDS debugging menu.")
1128 (setq gds-debug-menu (make-sparse-keymap "Debug"))
1129 (define-key gds-debug-menu [go]
1130 '(menu-item "Go" gds-go))
1131 (define-key gds-debug-menu [trace-finish]
1132 '(menu-item "Trace This Frame" gds-trace-finish))
1133 (define-key gds-debug-menu [step-out]
1134 '(menu-item "Finish This Frame" gds-step-out))
1135 (define-key gds-debug-menu [next]
1136 '(menu-item "Next" gds-next))
1137 (define-key gds-debug-menu [step-in]
1138 '(menu-item "Single Step" gds-step-in))
1139 (define-key gds-debug-menu [eval]
1140 '(menu-item "Eval In This Frame..." gds-evaluate)))
1142 (defvar gds-eval-menu nil
1143 "GDS evaluation menu.")
1146 (setq gds-eval-menu (make-sparse-keymap "Evaluate"))
1147 (define-key gds-eval-menu [load-file]
1148 '(menu-item "Load Scheme File" gds-load-file))
1149 (define-key gds-eval-menu [defun]
1150 '(menu-item "Defun At Point" gds-eval-defun))
1151 (define-key gds-eval-menu [region]
1152 '(menu-item "Region" gds-eval-region))
1153 (define-key gds-eval-menu [last-sexp]
1154 '(menu-item "Sexp Before Point" gds-eval-last-sexp))
1155 (define-key gds-eval-menu [expr]
1156 '(menu-item "Expression..." gds-eval-expression)))
1158 (defvar gds-help-menu nil
1162 (setq gds-help-menu (make-sparse-keymap "Help"))
1163 (define-key gds-help-menu [apropos]
1164 '(menu-item "Apropos..." gds-apropos))
1165 (define-key gds-help-menu [sym]
1166 '(menu-item "Symbol..." gds-help-symbol)))
1168 (defvar gds-advanced-menu nil
1169 "Menu of rarely needed GDS operations.")
1170 (if gds-advanced-menu
1172 (setq gds-advanced-menu (make-sparse-keymap "Advanced"))
1173 (define-key gds-advanced-menu [run-captive]
1174 '(menu-item "Run Captive Guile" gds-start-captive
1175 :enable (not (comint-check-proc gds-captive))))
1176 (define-key gds-advanced-menu [restart-gds]
1177 '(menu-item "Restart IDE" gds-start :enable gds-process))
1178 (define-key gds-advanced-menu [kill-gds]
1179 '(menu-item "Shutdown IDE" gds-shutdown :enable gds-process))
1180 (define-key gds-advanced-menu [start-gds]
1181 '(menu-item "Start IDE" gds-start :enable (not gds-process))))
1183 (defvar gds-menu nil
1184 "Global menu for GDS commands.")
1187 (setq gds-menu (make-sparse-keymap "Guile"))
1188 (define-key gds-menu [advanced]
1189 (cons "Advanced" gds-advanced-menu))
1190 (define-key gds-menu [separator-1]
1192 (define-key gds-menu [view]
1193 `(menu-item "View" ,gds-view-menu :enable gds-views))
1194 (define-key gds-menu [debug]
1195 `(menu-item "Debug" ,gds-debug-menu :enable (and gds-focus-client
1196 (gds-client-blocked))))
1197 (define-key gds-menu [eval]
1198 `(menu-item "Evaluate" ,gds-eval-menu :enable (or gds-buffers
1199 gds-autostart-captive)))
1200 (define-key gds-menu [help]
1201 `(menu-item "Help" ,gds-help-menu :enable (or gds-buffers
1202 gds-autostart-captive)))
1203 (setq menu-bar-final-items
1204 (cons 'guile menu-bar-final-items))
1205 (define-key scheme-mode-map [menu-bar guile]
1206 (cons "Guile" gds-menu)))
1209 ;;;; Autostarting the GDS server.
1211 (defcustom gds-autostart-server t
1212 "Whether to automatically start the GDS server when `gds.el' is loaded."
1216 (if (and gds-autostart-server
1221 ;;;; `Captive' Guile - a Guile process that is started when needed to
1222 ;;;; provide help, completion, evaluations etc.
1224 (defcustom gds-autostart-captive t
1225 "Whether to automatically start a `captive' Guile process when needed."
1229 (defvar gds-captive nil
1230 "Buffer of captive Guile.")
1232 (defun gds-start-captive (&optional restart)
1235 (comint-check-proc gds-captive))
1237 (if (comint-check-proc gds-captive)
1239 (let ((process-connection-type nil))
1240 (setq gds-captive (make-comint "captive-guile"
1244 (let ((proc (get-buffer-process gds-captive)))
1245 (comint-send-string proc "(set! %load-path (cons \"/home/neil/Guile/cvs/guile-core\" %load-path))\n")
1246 (comint-send-string proc "(debug-enable 'backtrace)\n")
1247 (comint-send-string proc "(use-modules (emacs gds-client))\n")
1248 (comint-send-string proc "(gds-connect \"Captive Guile\" #f)\n"))))
1250 (defun gds-kill-captive ()
1252 (let ((proc (get-buffer-process gds-captive)))
1253 (process-kill-without-query proc)
1257 (accept-process-output gds-process 0 200))
1265 ;;; gds.el ends here.