(edebug-emacs-19-specific): Renamed from edebug-fsf19-specific.
[bpt/emacs.git] / lisp / gud.el
1 ;;; gud.el --- Grand Unified Debugger mode for gdb, sdb, dbx, or xdb
2 ;;; under Emacs
3
4 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
5 ;; Maintainer: FSF
6 ;; Version: 1.3
7 ;; Keywords: unix, tools
8
9 ;; Copyright (C) 1992, 1993 Free Software Foundation, Inc.
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to
25 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26
27 ;;; Commentary:
28
29 ;; The ancestral gdb.el was by W. Schelter <wfs@rascal.ics.utexas.edu>
30 ;; It was later rewritten by rms. Some ideas were due to Masanobu.
31 ;; Grand Unification (sdb/dbx support) by Eric S. Raymond <esr@thyrsus.com>
32 ;; The overloading code was then rewritten by Barry Warsaw <bwarsaw@cen.com>,
33 ;; who also hacked the mode to use comint.el. Shane Hartman <shane@spr.com>
34 ;; added support for xdb (HPUX debugger). Rick Sladkey <jrs@world.std.com>
35 ;; wrote the GDB command completion code.
36
37 ;;; Code:
38
39 (require 'comint)
40 (require 'etags)
41
42 ;; ======================================================================
43 ;; GUD commands must be visible in C buffers visited by GUD
44
45 (defvar gud-key-prefix "\C-x\C-a"
46 "Prefix of all GUD commands valid in C buffers.")
47
48 (global-set-key (concat gud-key-prefix "\C-l") 'gud-refresh)
49 (global-set-key "\C-x " 'gud-break) ;; backward compatibility hack
50
51 ;; ======================================================================
52 ;; the overloading mechanism
53
54 (defun gud-overload-functions (gud-overload-alist)
55 "Overload functions defined in GUD-OVERLOAD-ALIST.
56 This association list has elements of the form
57 (ORIGINAL-FUNCTION-NAME OVERLOAD-FUNCTION)"
58 (mapcar
59 (function (lambda (p) (fset (car p) (symbol-function (cdr p)))))
60 gud-overload-alist))
61
62 (defun gud-massage-args (file args)
63 (error "GUD not properly entered."))
64
65 (defun gud-marker-filter (str)
66 (error "GUD not properly entered."))
67
68 (defun gud-find-file (f)
69 (error "GUD not properly entered."))
70 \f
71 ;; ======================================================================
72 ;; command definition
73
74 ;; This macro is used below to define some basic debugger interface commands.
75 ;; Of course you may use `gud-def' with any other debugger command, including
76 ;; user defined ones.
77
78 ;; A macro call like (gud-def FUNC NAME KEY DOC) expands to a form
79 ;; which defines FUNC to send the command NAME to the debugger, gives
80 ;; it the docstring DOC, and binds that function to KEY in the GUD
81 ;; major mode. The function is also bound in the global keymap with the
82 ;; GUD prefix.
83
84 (defmacro gud-def (func cmd key &optional doc)
85 "Define FUNC to be a command sending STR and bound to KEY, with
86 optional doc string DOC. Certain %-escapes in the string arguments
87 are interpreted specially if present. These are:
88
89 %f name (without directory) of current source file.
90 %d directory of current source file.
91 %l number of current source line
92 %e text of the C lvalue or function-call expression surrounding point.
93 %a text of the hexadecimal address surrounding point
94 %p prefix argument to the command (if any) as a number
95
96 The `current' source file is the file of the current buffer (if
97 we're in a C file) or the source file current at the last break or
98 step (if we're in the GUD buffer).
99 The `current' line is that of the current buffer (if we're in a
100 source file) or the source line number at the last break or step (if
101 we're in the GUD buffer)."
102 (list 'progn
103 (list 'defun func '(arg)
104 (or doc "")
105 '(interactive "p")
106 (list 'gud-call cmd 'arg))
107 (if key
108 (list 'define-key
109 '(current-local-map)
110 (concat "\C-c" key)
111 (list 'quote func)))
112 (if key
113 (list 'global-set-key
114 (list 'concat 'gud-key-prefix key)
115 (list 'quote func)))))
116
117 ;; Where gud-display-frame should put the debugging arrow. This is
118 ;; set by the marker-filter, which scans the debugger's output for
119 ;; indications of the current program counter.
120 (defvar gud-last-frame nil)
121
122 ;; Used by gud-refresh, which should cause gud-display-frame to redisplay
123 ;; the last frame, even if it's been called before and gud-last-frame has
124 ;; been set to nil.
125 (defvar gud-last-last-frame nil)
126
127 ;; All debugger-specific information is collected here.
128 ;; Here's how it works, in case you ever need to add a debugger to the mode.
129 ;;
130 ;; Each entry must define the following at startup:
131 ;;
132 ;;<name>
133 ;; comint-prompt-regexp
134 ;; gud-<name>-massage-args
135 ;; gud-<name>-marker-filter
136 ;; gud-<name>-find-file
137 ;;
138 ;; The job of the massage-args method is to modify the given list of
139 ;; debugger arguments before running the debugger.
140 ;;
141 ;; The job of the marker-filter method is to detect file/line markers in
142 ;; strings and set the global gud-last-frame to indicate what display
143 ;; action (if any) should be triggered by the marker. Note that only
144 ;; whatever the method *returns* is displayed in the buffer; thus, you
145 ;; can filter the debugger's output, interpreting some and passing on
146 ;; the rest.
147 ;;
148 ;; The job of the find-file method is to visit and return the buffer indicated
149 ;; by the car of gud-tag-frame. This may be a file name, a tag name, or
150 ;; something else.
151 \f
152 ;; ======================================================================
153 ;; gdb functions
154
155 ;;; History of argument lists passed to gdb.
156 (defvar gud-gdb-history nil)
157
158 (defun gud-gdb-massage-args (file args)
159 (cons "-fullname" (cons file args)))
160
161 ;; There's no guarantee that Emacs will hand the filter the entire
162 ;; marker at once; it could be broken up across several strings. We
163 ;; might even receive a big chunk with several markers in it. If we
164 ;; receive a chunk of text which looks like it might contain the
165 ;; beginning of a marker, we save it here between calls to the
166 ;; filter.
167 (defvar gud-gdb-marker-acc "")
168
169 (defun gud-gdb-marker-filter (string)
170 (save-match-data
171 (setq gud-gdb-marker-acc (concat gud-gdb-marker-acc string))
172 (let ((output ""))
173
174 ;; Process all the complete markers in this chunk.
175 (while (string-match "^\032\032\\([^:\n]*\\):\\([0-9]*\\):.*\n"
176 gud-gdb-marker-acc)
177 (setq
178
179 ;; Extract the frame position from the marker.
180 gud-last-frame
181 (cons (substring gud-gdb-marker-acc (match-beginning 1) (match-end 1))
182 (string-to-int (substring gud-gdb-marker-acc
183 (match-beginning 2)
184 (match-end 2))))
185
186 ;; Append any text before the marker to the output we're going
187 ;; to return - we don't include the marker in this text.
188 output (concat output
189 (substring gud-gdb-marker-acc 0 (match-beginning 0)))
190
191 ;; Set the accumulator to the remaining text.
192 gud-gdb-marker-acc (substring gud-gdb-marker-acc (match-end 0))))
193
194 ;; Does the remaining text look like it might end with the
195 ;; beginning of another marker? If it does, then keep it in
196 ;; gud-gdb-marker-acc until we receive the rest of it. Since we
197 ;; know the full marker regexp above failed, it's pretty simple to
198 ;; test for marker starts.
199 (if (string-match "^\032.*\\'" gud-gdb-marker-acc)
200 (progn
201 ;; Everything before the potential marker start can be output.
202 (setq output (concat output (substring gud-gdb-marker-acc
203 0 (match-beginning 0))))
204
205 ;; Everything after, we save, to combine with later input.
206 (setq gud-gdb-marker-acc
207 (substring gud-gdb-marker-acc (match-beginning 0))))
208
209 (setq output (concat output gud-gdb-marker-acc)
210 gud-gdb-marker-acc ""))
211
212 output)))
213
214 (defun gud-gdb-find-file (f)
215 (find-file-noselect f))
216
217 (defvar gdb-minibuffer-local-map nil
218 "Keymap for minibuffer prompting of gdb startup command.")
219 (if gdb-minibuffer-local-map
220 ()
221 (setq gdb-minibuffer-local-map (copy-keymap minibuffer-local-map))
222 (define-key
223 gdb-minibuffer-local-map "\C-i" 'comint-dynamic-complete-filename))
224
225 ;;;###autoload
226 (defun gdb (command-line)
227 "Run gdb on program FILE in buffer *gud-FILE*.
228 The directory containing FILE becomes the initial working directory
229 and source-file directory for your debugger."
230 (interactive
231 (list (read-from-minibuffer "Run gdb (like this): "
232 (if (consp gud-gdb-history)
233 (car gud-gdb-history)
234 "gdb ")
235 gdb-minibuffer-local-map nil
236 '(gud-gdb-history . 1))))
237 (gud-overload-functions '((gud-massage-args . gud-gdb-massage-args)
238 (gud-marker-filter . gud-gdb-marker-filter)
239 (gud-find-file . gud-gdb-find-file)
240 ))
241
242 (gud-common-init command-line)
243
244 (gud-def gud-break "break %f:%l" "\C-b" "Set breakpoint at current line.")
245 (gud-def gud-tbreak "tbreak %f:%l" "\C-t" "Set breakpoint at current line.")
246 (gud-def gud-remove "clear %l" "\C-d" "Remove breakpoint at current line")
247 (gud-def gud-step "step %p" "\C-s" "Step one source line with display.")
248 (gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
249 (gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
250 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
251 (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
252 (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
253 (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
254 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
255
256 (local-set-key "\C-i" 'gud-gdb-complete-command)
257 (setq comint-prompt-regexp "^(.*gdb[+]?) *")
258 (run-hooks 'gdb-mode-hook)
259 )
260
261 ;; One of the nice features of GDB is its impressive support for
262 ;; context-sensitive command completion. We preserve that feature
263 ;; in the GUD buffer by using a GDB command designed just for Emacs.
264
265 ;; The completion process filter indicates when it is finished.
266 (defvar gud-gdb-complete-in-progress)
267
268 ;; Since output may arrive in fragments we accumulate partials strings here.
269 (defvar gud-gdb-complete-string)
270
271 ;; We need to know how much of the completion to chop off.
272 (defvar gud-gdb-complete-break)
273
274 ;; The completion list is constructed by the process filter.
275 (defvar gud-gdb-complete-list)
276
277 (defvar gud-comint-buffer nil)
278
279 (defun gud-gdb-complete-command ()
280 "Perform completion on the GDB command preceding point.
281 This is implemented using the GDB `complete' command which isn't
282 available with older versions of GDB."
283 (interactive)
284 (let* ((end (point))
285 (command (save-excursion
286 (beginning-of-line)
287 (and (looking-at comint-prompt-regexp)
288 (goto-char (match-end 0)))
289 (buffer-substring (point) end)))
290 command-word)
291 ;; Find the word break. This match will always succeed.
292 (string-match "\\(\\`\\| \\)\\([^ ]*\\)\\'" command)
293 (setq gud-gdb-complete-break (match-beginning 2)
294 command-word (substring command gud-gdb-complete-break))
295 (unwind-protect
296 (progn
297 ;; Temporarily install our filter function.
298 (gud-overload-functions
299 '((gud-marker-filter . gud-gdb-complete-filter)))
300 ;; Issue the command to GDB.
301 (gud-basic-call (concat "complete " command))
302 (setq gud-gdb-complete-in-progress t
303 gud-gdb-complete-string nil
304 gud-gdb-complete-list nil)
305 ;; Slurp the output.
306 (while gud-gdb-complete-in-progress
307 (accept-process-output (get-buffer-process gud-comint-buffer))))
308 ;; Restore the old filter function.
309 (gud-overload-functions '((gud-marker-filter . gud-gdb-marker-filter))))
310 ;; Protect against old versions of GDB.
311 (and gud-gdb-complete-list
312 (string-match "^Undefined command: \"complete\""
313 (car gud-gdb-complete-list))
314 (error "This version of GDB doesn't support the `complete' command."))
315 ;; Sort the list like readline.
316 (setq gud-gdb-complete-list
317 (sort gud-gdb-complete-list (function string-lessp)))
318 ;; Remove duplicates.
319 (let ((first gud-gdb-complete-list)
320 (second (cdr gud-gdb-complete-list)))
321 (while second
322 (if (string-equal (car first) (car second))
323 (setcdr first (setq second (cdr second)))
324 (setq first second
325 second (cdr second)))))
326 ;; Let comint handle the rest.
327 (comint-dynamic-simple-complete command-word gud-gdb-complete-list)))
328
329 ;; The completion process filter is installed temporarily to slurp the
330 ;; output of GDB up to the next prompt and build the completion list.
331 (defun gud-gdb-complete-filter (string)
332 (setq string (concat gud-gdb-complete-string string))
333 (while (string-match "\n" string)
334 (setq gud-gdb-complete-list
335 (cons (substring string gud-gdb-complete-break (match-beginning 0))
336 gud-gdb-complete-list))
337 (setq string (substring string (match-end 0))))
338 (if (string-match comint-prompt-regexp string)
339 (progn
340 (setq gud-gdb-complete-in-progress nil)
341 string)
342 (progn
343 (setq gud-gdb-complete-string string)
344 "")))
345
346 \f
347 ;; ======================================================================
348 ;; sdb functions
349
350 ;;; History of argument lists passed to sdb.
351 (defvar gud-sdb-history nil)
352
353 (defvar gud-sdb-needs-tags (not (file-exists-p "/var"))
354 "If nil, we're on a System V Release 4 and don't need the tags hack.")
355
356 (defvar gud-sdb-lastfile nil)
357
358 (defun gud-sdb-massage-args (file args)
359 (cons file args))
360
361 (defun gud-sdb-marker-filter (string)
362 (cond
363 ;; System V Release 3.2 uses this format
364 ((string-match "\\(^0x\\w* in \\|^\\|\n\\)\\([^:\n]*\\):\\([0-9]*\\):.*\n"
365 string)
366 (setq gud-last-frame
367 (cons
368 (substring string (match-beginning 2) (match-end 2))
369 (string-to-int
370 (substring string (match-beginning 3) (match-end 3))))))
371 ;; System V Release 4.0
372 ((string-match "^\\(BREAKPOINT\\|STEPPED\\) process [0-9]+ function [^ ]+ in \\(.+\\)\n"
373 string)
374 (setq gud-sdb-lastfile
375 (substring string (match-beginning 2) (match-end 2))))
376 ((and gud-sdb-lastfile (string-match "^\\([0-9]+\\):" string))
377 (setq gud-last-frame
378 (cons
379 gud-sdb-lastfile
380 (string-to-int
381 (substring string (match-beginning 1) (match-end 1))))))
382 (t
383 (setq gud-sdb-lastfile nil)))
384 string)
385
386 (defun gud-sdb-find-file (f)
387 (if gud-sdb-needs-tags
388 (find-tag-noselect f)
389 (find-file-noselect f)))
390
391 ;;;###autoload
392 (defun sdb (command-line)
393 "Run sdb on program FILE in buffer *gud-FILE*.
394 The directory containing FILE becomes the initial working directory
395 and source-file directory for your debugger."
396 (interactive
397 (list (read-from-minibuffer "Run sdb (like this): "
398 (if (consp gud-sdb-history)
399 (car gud-sdb-history)
400 "sdb ")
401 nil nil
402 '(gud-sdb-history . 1))))
403 (if (and gud-sdb-needs-tags
404 (not (and (boundp 'tags-file-name) (file-exists-p tags-file-name))))
405 (error "The sdb support requires a valid tags table to work."))
406 (gud-overload-functions '((gud-massage-args . gud-sdb-massage-args)
407 (gud-marker-filter . gud-sdb-marker-filter)
408 (gud-find-file . gud-sdb-find-file)
409 ))
410
411 (gud-common-init command-line)
412
413 (gud-def gud-break "%l b" "\C-b" "Set breakpoint at current line.")
414 (gud-def gud-tbreak "%l c" "\C-t" "Set temporary breakpoint at current line.")
415 (gud-def gud-remove "%l d" "\C-d" "Remove breakpoint at current line")
416 (gud-def gud-step "s %p" "\C-s" "Step one source line with display.")
417 (gud-def gud-stepi "i %p" "\C-i" "Step one instruction with display.")
418 (gud-def gud-next "S %p" "\C-n" "Step one line (skip functions).")
419 (gud-def gud-cont "c" "\C-r" "Continue with display.")
420 (gud-def gud-print "%e/" "\C-p" "Evaluate C expression at point.")
421
422 (setq comint-prompt-regexp "\\(^\\|\n\\)\\*")
423 (run-hooks 'sdb-mode-hook)
424 )
425 \f
426 ;; ======================================================================
427 ;; dbx functions
428
429 ;;; History of argument lists passed to dbx.
430 (defvar gud-dbx-history nil)
431
432 (defun gud-dbx-massage-args (file args)
433 (cons file args))
434
435 (defun gud-dbx-marker-filter (string)
436 (if (or (string-match
437 "stopped in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
438 string)
439 (string-match
440 "signal .* in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
441 string))
442 (setq gud-last-frame
443 (cons
444 (substring string (match-beginning 2) (match-end 2))
445 (string-to-int
446 (substring string (match-beginning 1) (match-end 1))))))
447 string)
448
449 ;; Functions for dbx on Mips/Ultrix.
450 ;; This is very similar to the code for gdb. The trick is to start dbx
451 ;; with the (undocumented) option `-emacs'.
452
453 ;; Are we running on a Mips system under Ultrix?
454 (defvar gud-dbx-mips-p (file-exists-p "/usr/include/mips"))
455
456 (defun gud-mipsdbx-massage-args (file args)
457 (cons "-emacs" (cons file args)))
458
459 ;; There's no guarantee that Emacs will hand the filter the entire
460 ;; marker at once; it could be broken up across several strings. We
461 ;; might even receive a big chunk with several markers in it. If we
462 ;; receive a chunk of text which looks like it might contain the
463 ;; beginning of a marker, we save it here between calls to the
464 ;; filter.
465 (defvar gud-mipsdbx-marker-acc "")
466
467 (defun gud-mipsdbx-marker-filter (string)
468 (save-match-data
469 (setq gud-mipsdbx-marker-acc (concat gud-mipsdbx-marker-acc string))
470 (let ((output ""))
471
472 ;; Process all the complete markers in this chunk.
473 (while (string-match
474 "^[] [0-9]*\032\032\\([^:\n]*\\):\\([0-9]*\\):.*\n"
475 gud-mipsdbx-marker-acc)
476 (setq
477
478 ;; Extract the frame position from the marker.
479 gud-last-frame (cons
480 (substring gud-mipsdbx-marker-acc
481 (match-beginning 1) (match-end 1))
482 (string-to-int
483 (substring gud-mipsdbx-marker-acc
484 (match-beginning 2) (match-end 2))))
485
486 ;; Append any text before the marker to the output we're going
487 ;; to return - we don't include the marker in this text.
488 output (concat output (substring gud-mipsdbx-marker-acc
489 0 (match-beginning 0)))
490
491 ;; Set the accumulator to the remaining text.
492 gud-mipsdbx-marker-acc (substring gud-mipsdbx-marker-acc
493 (match-end 0))))
494
495 ;; Does the remaining text look like it might end with the
496 ;; beginning of another marker? If it does, then keep it in
497 ;; gud-mipsdbx-marker-acc until we receive the rest of it.
498 ;; Since we know the full marker regexp above failed, it's pretty
499 ;; simple to test for marker starts.
500 (if (string-match "^[] [0-9]*\032.*\\'" gud-mipsdbx-marker-acc)
501 (setq
502 ;; Everything before the potential marker start can be output.
503 output (concat output (substring gud-mipsdbx-marker-acc
504 0 (match-beginning 0)))
505 ;; Everything after, we save, to combine with later input.
506 gud-mipsdbx-marker-acc (substring gud-mipsdbx-marker-acc
507 (match-beginning 0)))
508 (setq output (concat output gud-mipsdbx-marker-acc)
509 gud-mipsdbx-marker-acc ""))
510
511 output)))
512
513 (defun gud-dbx-find-file (f)
514 (find-file-noselect f))
515
516 ;;;###autoload
517 (defun dbx (command-line)
518 "Run dbx on program FILE in buffer *gud-FILE*.
519 The directory containing FILE becomes the initial working directory
520 and source-file directory for your debugger."
521 (interactive
522 (list (read-from-minibuffer "Run dbx (like this): "
523 (if (consp gud-dbx-history)
524 (car gud-dbx-history)
525 "dbx ")
526 nil nil
527 '(gud-dbx-history . 1))))
528
529 (gud-overload-functions
530 (cond
531 (gud-dbx-mips-p
532 '((gud-massage-args . gud-mipsdbx-massage-args)
533 (gud-marker-filter . gud-mipsdbx-marker-filter)
534 (gud-find-file . gud-dbx-find-file)))
535 (t
536 '((gud-massage-args . gud-dbx-massage-args)
537 (gud-marker-filter . gud-dbx-marker-filter)
538 (gud-find-file . gud-dbx-find-file)))))
539
540 (gud-common-init command-line)
541
542 (cond
543 (gud-dbx-mips-p
544 (gud-def gud-break "stop at \"%f\":%l"
545 "\C-b" "Set breakpoint at current line.")
546 (gud-def gud-finish "return" "\C-f" "Finish executing current function."))
547 (t
548 (gud-def gud-break "file \"%d%f\"\nstop at %l"
549 "\C-b" "Set breakpoint at current line.")))
550
551 (gud-def gud-remove "clear %l" "\C-d" "Remove breakpoint at current line")
552 (gud-def gud-step "step %p" "\C-s" "Step one line with display.")
553 (gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
554 (gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
555 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
556 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
557 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
558 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
559
560 (setq comint-prompt-regexp "^[^)\n]*dbx) *")
561 (run-hooks 'dbx-mode-hook)
562 )
563 \f
564 ;; ======================================================================
565 ;; xdb (HP PARISC debugger) functions
566
567 ;;; History of argument lists passed to xdb.
568 (defvar gud-xdb-history nil)
569
570 (defvar gud-xdb-directories nil
571 "*A list of directories that xdb should search for source code.
572 If nil, only source files in the program directory
573 will be known to xdb.
574
575 The file names should be absolute, or relative to the directory
576 containing the executable being debugged.")
577
578 (defun gud-xdb-massage-args (file args)
579 (nconc (let ((directories gud-xdb-directories)
580 (result nil))
581 (while directories
582 (setq result (cons (car directories) (cons "-d" result)))
583 (setq directories (cdr directories)))
584 (nreverse (cons file result)))
585 args))
586
587 (defun gud-xdb-file-name (f)
588 "Transform a relative pathname to a full pathname in xdb mode"
589 (let ((result nil))
590 (if (file-exists-p f)
591 (setq result (expand-file-name f))
592 (let ((directories gud-xdb-directories))
593 (while directories
594 (let ((path (concat (car directories) "/" f)))
595 (if (file-exists-p path)
596 (setq result (expand-file-name path)
597 directories nil)))
598 (setq directories (cdr directories)))))
599 result))
600
601 ;; xdb does not print the lines all at once, so we have to accumulate them
602 (defvar gud-xdb-accumulation "")
603
604 (defun gud-xdb-marker-filter (string)
605 (let (result)
606 (if (or (string-match comint-prompt-regexp string)
607 (string-match ".*\012" string))
608 (setq result (concat gud-xdb-accumulation string)
609 gud-xdb-accumulation "")
610 (setq gud-xdb-accumulation (concat gud-xdb-accumulation string)))
611 (if result
612 (if (or (string-match "\\([^\n \t:]+\\): [^:]+: \\([0-9]+\\):" result)
613 (string-match "[^: \t]+:[ \t]+\\([^:]+\\): [^:]+: \\([0-9]+\\):"
614 result))
615 (let ((line (string-to-int
616 (substring result (match-beginning 2) (match-end 2))))
617 (file (gud-xdb-file-name
618 (substring result (match-beginning 1) (match-end 1)))))
619 (if file
620 (setq gud-last-frame (cons file line))))))
621 (or result "")))
622
623 (defun gud-xdb-find-file (f)
624 (let ((realf (gud-xdb-file-name f)))
625 (if realf (find-file-noselect realf))))
626
627 ;;;###autoload
628 (defun xdb (command-line)
629 "Run xdb on program FILE in buffer *gud-FILE*.
630 The directory containing FILE becomes the initial working directory
631 and source-file directory for your debugger.
632
633 You can set the variable 'gud-xdb-directories' to a list of program source
634 directories if your program contains sources from more than one directory."
635 (interactive
636 (list (read-from-minibuffer "Run xdb (like this): "
637 (if (consp gud-xdb-history)
638 (car gud-xdb-history)
639 "xdb ")
640 nil nil
641 '(gud-xdb-history . 1))))
642 (gud-overload-functions '((gud-massage-args . gud-xdb-massage-args)
643 (gud-marker-filter . gud-xdb-marker-filter)
644 (gud-find-file . gud-xdb-find-file)))
645
646 (gud-common-init command-line)
647
648 (gud-def gud-break "b %f:%l" "\C-b" "Set breakpoint at current line.")
649 (gud-def gud-tbreak "b %f:%l\\t" "\C-t"
650 "Set temporary breakpoint at current line.")
651 (gud-def gud-remove "db" "\C-d" "Remove breakpoint at current line")
652 (gud-def gud-step "s %p" "\C-s" "Step one line with display.")
653 (gud-def gud-next "S %p" "\C-n" "Step one line (skip functions).")
654 (gud-def gud-cont "c" "\C-r" "Continue with display.")
655 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
656 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
657 (gud-def gud-finish "bu\\t" "\C-f" "Finish executing current function.")
658 (gud-def gud-print "p %e" "\C-p" "Evaluate C expression at point.")
659
660 (setq comint-prompt-regexp "^>")
661 (make-local-variable 'gud-xdb-accumulation)
662 (setq gud-xdb-accumulation "")
663 (run-hooks 'xdb-mode-hook))
664 \f
665 ;; ======================================================================
666 ;; perldb functions
667
668 ;;; History of argument lists passed to perldb.
669 (defvar gud-perldb-history nil)
670
671 (defun gud-perldb-massage-args (file args)
672 (cons "-d" (cons file (cons "-emacs" args))))
673
674 ;; There's no guarantee that Emacs will hand the filter the entire
675 ;; marker at once; it could be broken up across several strings. We
676 ;; might even receive a big chunk with several markers in it. If we
677 ;; receive a chunk of text which looks like it might contain the
678 ;; beginning of a marker, we save it here between calls to the
679 ;; filter.
680 (defvar gud-perldb-marker-acc "")
681
682 (defun gud-perldb-marker-filter (string)
683 (save-match-data
684 (setq gud-perldb-marker-acc (concat gud-perldb-marker-acc string))
685 (let ((output ""))
686
687 ;; Process all the complete markers in this chunk.
688 (while (string-match "^\032\032\\([^:\n]*\\):\\([0-9]*\\):.*\n"
689 gud-perldb-marker-acc)
690 (setq
691
692 ;; Extract the frame position from the marker.
693 gud-last-frame
694 (cons (substring gud-perldb-marker-acc (match-beginning 1) (match-end 1))
695 (string-to-int (substring gud-perldb-marker-acc
696 (match-beginning 2)
697 (match-end 2))))
698
699 ;; Append any text before the marker to the output we're going
700 ;; to return - we don't include the marker in this text.
701 output (concat output
702 (substring gud-perldb-marker-acc 0 (match-beginning 0)))
703
704 ;; Set the accumulator to the remaining text.
705 gud-perldb-marker-acc (substring gud-perldb-marker-acc (match-end 0))))
706
707 ;; Does the remaining text look like it might end with the
708 ;; beginning of another marker? If it does, then keep it in
709 ;; gud-perldb-marker-acc until we receive the rest of it. Since we
710 ;; know the full marker regexp above failed, it's pretty simple to
711 ;; test for marker starts.
712 (if (string-match "^\032.*\\'" gud-perldb-marker-acc)
713 (progn
714 ;; Everything before the potential marker start can be output.
715 (setq output (concat output (substring gud-perldb-marker-acc
716 0 (match-beginning 0))))
717
718 ;; Everything after, we save, to combine with later input.
719 (setq gud-perldb-marker-acc
720 (substring gud-perldb-marker-acc (match-beginning 0))))
721
722 (setq output (concat output gud-perldb-marker-acc)
723 gud-perldb-marker-acc ""))
724
725 output)))
726
727 (defun gud-perldb-find-file (f)
728 (find-file-noselect f))
729
730 ;;;###autoload
731 (defun perldb (command-line)
732 "Run perldb on program FILE in buffer *gud-FILE*.
733 The directory containing FILE becomes the initial working directory
734 and source-file directory for your debugger."
735 (interactive
736 (list (read-from-minibuffer "Run perldb (like this): "
737 (if (consp gud-perldb-history)
738 (car gud-perldb-history)
739 "perl ")
740 nil nil
741 '(gud-perldb-history . 1))))
742 (gud-overload-functions '((gud-massage-args . gud-perldb-massage-args)
743 (gud-marker-filter . gud-perldb-marker-filter)
744 (gud-find-file . gud-perldb-find-file)
745 ))
746
747 (gud-common-init command-line)
748
749 (gud-def gud-break "b %l" "\C-b" "Set breakpoint at current line.")
750 (gud-def gud-remove "d %l" "\C-d" "Remove breakpoint at current line")
751 (gud-def gud-step "s" "\C-s" "Step one source line with display.")
752 (gud-def gud-next "n" "\C-n" "Step one line (skip functions).")
753 (gud-def gud-cont "c" "\C-r" "Continue with display.")
754 ; (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
755 ; (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
756 ; (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
757 (gud-def gud-print "%e" "\C-p" "Evaluate perl expression at point.")
758
759 (setq comint-prompt-regexp "^ DB<[0-9]+> ")
760 (run-hooks 'perldb-mode-hook)
761 )
762
763 ;;
764 ;; End of debugger-specific information
765 ;;
766
767 \f
768 ;;; When we send a command to the debugger via gud-call, it's annoying
769 ;;; to see the command and the new prompt inserted into the debugger's
770 ;;; buffer; we have other ways of knowing the command has completed.
771 ;;;
772 ;;; If the buffer looks like this:
773 ;;; --------------------
774 ;;; (gdb) set args foo bar
775 ;;; (gdb) -!-
776 ;;; --------------------
777 ;;; (the -!- marks the location of point), and we type `C-x SPC' in a
778 ;;; source file to set a breakpoint, we want the buffer to end up like
779 ;;; this:
780 ;;; --------------------
781 ;;; (gdb) set args foo bar
782 ;;; Breakpoint 1 at 0x92: file make-docfile.c, line 49.
783 ;;; (gdb) -!-
784 ;;; --------------------
785 ;;; Essentially, the old prompt is deleted, and the command's output
786 ;;; and the new prompt take its place.
787 ;;;
788 ;;; Not echoing the command is easy enough; you send it directly using
789 ;;; process-send-string, and it never enters the buffer. However,
790 ;;; getting rid of the old prompt is trickier; you don't want to do it
791 ;;; when you send the command, since that will result in an annoying
792 ;;; flicker as the prompt is deleted, redisplay occurs while Emacs
793 ;;; waits for a response from the debugger, and the new prompt is
794 ;;; inserted. Instead, we'll wait until we actually get some output
795 ;;; from the subprocess before we delete the prompt. If the command
796 ;;; produced no output other than a new prompt, that prompt will most
797 ;;; likely be in the first chunk of output received, so we will delete
798 ;;; the prompt and then replace it with an identical one. If the
799 ;;; command produces output, the prompt is moving anyway, so the
800 ;;; flicker won't be annoying.
801 ;;;
802 ;;; So - when we want to delete the prompt upon receipt of the next
803 ;;; chunk of debugger output, we position gud-delete-prompt-marker at
804 ;;; the start of the prompt; the process filter will notice this, and
805 ;;; delete all text between it and the process output marker. If
806 ;;; gud-delete-prompt-marker points nowhere, we leave the current
807 ;;; prompt alone.
808 (defvar gud-delete-prompt-marker nil)
809
810 \f
811 (defun gud-mode ()
812 "Major mode for interacting with an inferior debugger process.
813
814 You start it up with one of the commands M-x gdb, M-x sdb, M-x dbx,
815 or M-x xdb. Each entry point finishes by executing a hook; `gdb-mode-hook',
816 `sdb-mode-hook', `dbx-mode-hook' or `xdb-mode-hook' respectively.
817
818 After startup, the following commands are available in both the GUD
819 interaction buffer and any source buffer GUD visits due to a breakpoint stop
820 or step operation:
821
822 \\[gud-break] sets a breakpoint at the current file and line. In the
823 GUD buffer, the current file and line are those of the last breakpoint or
824 step. In a source buffer, they are the buffer's file and current line.
825
826 \\[gud-remove] removes breakpoints on the current file and line.
827
828 \\[gud-refresh] displays in the source window the last line referred to
829 in the gud buffer.
830
831 \\[gud-step], \\[gud-next], and \\[gud-stepi] do a step-one-line,
832 step-one-line (not entering function calls), and step-one-instruction
833 and then update the source window with the current file and position.
834 \\[gud-cont] continues execution.
835
836 \\[gud-print] tries to find the largest C lvalue or function-call expression
837 around point, and sends it to the debugger for value display.
838
839 The above commands are common to all supported debuggers except xdb which
840 does not support stepping instructions.
841
842 Under gdb, sdb and xdb, \\[gud-tbreak] behaves exactly like \\[gud-break],
843 except that the breakpoint is temporary; that is, it is removed when
844 execution stops on it.
845
846 Under gdb, dbx, and xdb, \\[gud-up] pops up through an enclosing stack
847 frame. \\[gud-down] drops back down through one.
848
849 If you are using gdb or xdb, \\[gud-finish] runs execution to the return from
850 the current function and stops.
851
852 All the keystrokes above are accessible in the GUD buffer
853 with the prefix C-c, and in all buffers through the prefix C-x C-a.
854
855 All pre-defined functions for which the concept make sense repeat
856 themselves the appropriate number of times if you give a prefix
857 argument.
858
859 You may use the `gud-def' macro in the initialization hook to define other
860 commands.
861
862 Other commands for interacting with the debugger process are inherited from
863 comint mode, which see."
864 (interactive)
865 (comint-mode)
866 (setq major-mode 'gud-mode)
867 (setq mode-name "Debugger")
868 (setq mode-line-process '(": %s"))
869 (use-local-map (copy-keymap comint-mode-map))
870 (define-key (current-local-map) "\C-c\C-l" 'gud-refresh)
871 (make-local-variable 'gud-last-frame)
872 (setq gud-last-frame nil)
873 (make-local-variable 'comint-prompt-regexp)
874 (make-local-variable 'gud-delete-prompt-marker)
875 (setq gud-delete-prompt-marker (make-marker))
876 (run-hooks 'gud-mode-hook)
877 )
878
879 ;; Chop STRING into words separated by SPC or TAB and return a list of them.
880 (defun gud-chop-words (string)
881 (let ((i 0) (beg 0)
882 (len (length string))
883 (words nil))
884 (while (< i len)
885 (if (memq (aref string i) '(?\t ? ))
886 (progn
887 (setq words (cons (substring string beg i) words)
888 beg (1+ i))
889 (while (and (< beg len) (memq (aref string beg) '(?\t ? )))
890 (setq beg (1+ beg)))
891 (setq i (1+ beg)))
892 (setq i (1+ i))))
893 (if (< beg len)
894 (setq words (cons (substring string beg) words)))
895 (nreverse words)))
896
897 ;; Perform initializations common to all debuggers.
898 (defun gud-common-init (command-line)
899 (let* ((words (gud-chop-words command-line))
900 (program (car words))
901 (file-word (let ((w (cdr words)))
902 (while (and w (= ?- (aref (car w) 0)))
903 (setq w (cdr w)))
904 (car w)))
905 (args (delq file-word (cdr words)))
906 (file (and file-word
907 (expand-file-name (substitute-in-file-name file-word))))
908 (filepart (and file-word (file-name-nondirectory file))))
909 (switch-to-buffer (concat "*gud-" filepart "*"))
910 (and file-word (setq default-directory (file-name-directory file)))
911 (or (bolp) (newline))
912 (insert "Current directory is " default-directory "\n")
913 (apply 'make-comint (concat "gud-" filepart) program nil
914 (if file-word (gud-massage-args file args))))
915 (gud-mode)
916 (set-process-filter (get-buffer-process (current-buffer)) 'gud-filter)
917 (set-process-sentinel (get-buffer-process (current-buffer)) 'gud-sentinel)
918 (gud-set-buffer)
919 )
920
921 (defun gud-set-buffer ()
922 (cond ((eq major-mode 'gud-mode)
923 (setq gud-comint-buffer (current-buffer)))))
924
925 ;; These functions are responsible for inserting output from your debugger
926 ;; into the buffer. The hard work is done by the method that is
927 ;; the value of gud-marker-filter.
928
929 ;; Rather than duplicating all the work of comint-output-filter, perhaps
930 ;; gud-filter should be implemented by adding appropriate hooks to
931 ;; comint-output-filter. Would somebody like to volunteer to do that?
932 (defun gud-filter (proc string)
933 ;; Here's where the actual buffer insertion is done
934 (let ((inhibit-quit t))
935 (save-excursion
936 (set-buffer (process-buffer proc))
937 (let (moving output-after-point)
938 (save-excursion
939 (goto-char (process-mark proc))
940 ;; If we have been so requested, delete the debugger prompt.
941 (if (marker-buffer gud-delete-prompt-marker)
942 (progn
943 (delete-region (point) gud-delete-prompt-marker)
944 (set-marker gud-delete-prompt-marker nil)))
945 (setq string (gud-marker-filter string))
946 (insert-before-markers string)
947 (and comint-last-input-end
948 (marker-buffer comint-last-input-end)
949 (= (point) comint-last-input-end)
950 (set-marker comint-last-input-end
951 (- comint-last-input-end (length string))))
952 (setq moving (= (point) (process-mark proc)))
953 (setq output-after-point (< (point) (process-mark proc)))
954 ;; Check for a filename-and-line number.
955 ;; Don't display the specified file
956 ;; unless (1) point is at or after the position where output appears
957 ;; and (2) this buffer is on the screen.
958 (if (and gud-last-frame
959 (not output-after-point)
960 (get-buffer-window (current-buffer)))
961 (gud-display-frame)))
962 (if moving (goto-char (process-mark proc)))))))
963
964 (defun gud-sentinel (proc msg)
965 (cond ((null (buffer-name (process-buffer proc)))
966 ;; buffer killed
967 ;; Stop displaying an arrow in a source file.
968 (setq overlay-arrow-position nil)
969 (set-process-buffer proc nil))
970 ((memq (process-status proc) '(signal exit))
971 ;; Stop displaying an arrow in a source file.
972 (setq overlay-arrow-position nil)
973 ;; Fix the mode line.
974 (setq mode-line-process
975 (concat ": "
976 (symbol-name (process-status proc))))
977 (let* ((obuf (current-buffer)))
978 ;; save-excursion isn't the right thing if
979 ;; process-buffer is current-buffer
980 (unwind-protect
981 (progn
982 ;; Write something in *compilation* and hack its mode line,
983 (set-buffer (process-buffer proc))
984 ;; Force mode line redisplay soon
985 (set-buffer-modified-p (buffer-modified-p))
986 (if (eobp)
987 (insert ?\n mode-name " " msg)
988 (save-excursion
989 (goto-char (point-max))
990 (insert ?\n mode-name " " msg)))
991 ;; If buffer and mode line will show that the process
992 ;; is dead, we can delete it now. Otherwise it
993 ;; will stay around until M-x list-processes.
994 (delete-process proc))
995 ;; Restore old buffer, but don't restore old point
996 ;; if obuf is the gud buffer.
997 (set-buffer obuf))))))
998
999 (defun gud-display-frame ()
1000 "Find and obey the last filename-and-line marker from the debugger.
1001 Obeying it means displaying in another window the specified file and line."
1002 (interactive)
1003 (if gud-last-frame
1004 (progn
1005 (gud-set-buffer)
1006 (gud-display-line (car gud-last-frame) (cdr gud-last-frame))
1007 (setq gud-last-last-frame gud-last-frame
1008 gud-last-frame nil))))
1009
1010 ;; Make sure the file named TRUE-FILE is in a buffer that appears on the screen
1011 ;; and that its line LINE is visible.
1012 ;; Put the overlay-arrow on the line LINE in that buffer.
1013 ;; Most of the trickiness in here comes from wanting to preserve the current
1014 ;; region-restriction if that's possible. We use an explicit display-buffer
1015 ;; to get around the fact that this is called inside a save-excursion.
1016
1017 (defun gud-display-line (true-file line)
1018 (let* ((buffer (gud-find-file true-file))
1019 (window (display-buffer buffer))
1020 (pos))
1021 ;;; (if (equal buffer (current-buffer))
1022 ;;; nil
1023 ;;; (setq buffer-read-only nil))
1024 (save-excursion
1025 ;;; (setq buffer-read-only t)
1026 (set-buffer buffer)
1027 (save-restriction
1028 (widen)
1029 (goto-line line)
1030 (setq pos (point))
1031 (setq overlay-arrow-string "=>")
1032 (or overlay-arrow-position
1033 (setq overlay-arrow-position (make-marker)))
1034 (set-marker overlay-arrow-position (point) (current-buffer)))
1035 (cond ((or (< pos (point-min)) (> pos (point-max)))
1036 (widen)
1037 (goto-char pos))))
1038 (set-window-point window overlay-arrow-position)))
1039
1040 ;;; The gud-call function must do the right thing whether its invoking
1041 ;;; keystroke is from the GUD buffer itself (via major-mode binding)
1042 ;;; or a C buffer. In the former case, we want to supply data from
1043 ;;; gud-last-frame. Here's how we do it:
1044
1045 (defun gud-format-command (str arg)
1046 (let ((insource (not (eq (current-buffer) gud-comint-buffer)))
1047 (frame (or gud-last-frame gud-last-last-frame))
1048 result)
1049 (while (and str (string-match "\\([^%]*\\)%\\([adeflp]\\)" str))
1050 (let ((key (string-to-char (substring str (match-beginning 2))))
1051 subst)
1052 (cond
1053 ((eq key ?f)
1054 (setq subst (file-name-nondirectory (if insource
1055 (buffer-file-name)
1056 (car frame)))))
1057 ((eq key ?d)
1058 (setq subst (file-name-directory (if insource
1059 (buffer-file-name)
1060 (car frame)))))
1061 ((eq key ?l)
1062 (setq subst (if insource
1063 (save-excursion
1064 (beginning-of-line)
1065 (save-restriction (widen)
1066 (1+ (count-lines 1 (point)))))
1067 (cdr frame))))
1068 ((eq key ?e)
1069 (setq subst (find-c-expr)))
1070 ((eq key ?a)
1071 (setq subst (gud-read-address)))
1072 ((eq key ?p)
1073 (setq subst (if arg (int-to-string arg) ""))))
1074 (setq result (concat result
1075 (substring str (match-beginning 1) (match-end 1))
1076 subst)))
1077 (setq str (substring str (match-end 2))))
1078 ;; There might be text left in STR when the loop ends.
1079 (concat result str)))
1080
1081 (defun gud-read-address ()
1082 "Return a string containing the core-address found in the buffer at point."
1083 (save-excursion
1084 (let ((pt (point)) found begin)
1085 (setq found (if (search-backward "0x" (- pt 7) t) (point)))
1086 (cond
1087 (found (forward-char 2)
1088 (buffer-substring found
1089 (progn (re-search-forward "[^0-9a-f]")
1090 (forward-char -1)
1091 (point))))
1092 (t (setq begin (progn (re-search-backward "[^0-9]")
1093 (forward-char 1)
1094 (point)))
1095 (forward-char 1)
1096 (re-search-forward "[^0-9]")
1097 (forward-char -1)
1098 (buffer-substring begin (point)))))))
1099
1100 (defun gud-call (fmt &optional arg)
1101 (let ((msg (gud-format-command fmt arg)))
1102 (message "Command: %s" msg)
1103 (sit-for 0)
1104 (gud-basic-call msg)))
1105
1106 (defun gud-basic-call (command)
1107 "Invoke the debugger COMMAND displaying source in other window."
1108 (interactive)
1109 (gud-set-buffer)
1110 (let ((command (concat command "\n"))
1111 (proc (get-buffer-process gud-comint-buffer)))
1112
1113 ;; Arrange for the current prompt to get deleted.
1114 (save-excursion
1115 (set-buffer gud-comint-buffer)
1116 (goto-char (process-mark proc))
1117 (beginning-of-line)
1118 (if (looking-at comint-prompt-regexp)
1119 (set-marker gud-delete-prompt-marker (point))))
1120 (process-send-string proc command)))
1121
1122 (defun gud-refresh (&optional arg)
1123 "Fix up a possibly garbled display, and redraw the arrow."
1124 (interactive "P")
1125 (recenter arg)
1126 (or gud-last-frame (setq gud-last-frame gud-last-last-frame))
1127 (gud-display-frame))
1128 \f
1129 ;;; Code for parsing expressions out of C code. The single entry point is
1130 ;;; find-c-expr, which tries to return an lvalue expression from around point.
1131 ;;;
1132 ;;; The rest of this file is a hacked version of gdbsrc.el by
1133 ;;; Debby Ayers <ayers@asc.slb.com>,
1134 ;;; Rich Schaefer <schaefer@asc.slb.com> Schlumberger, Austin, Tx.
1135
1136 (defun find-c-expr ()
1137 "Returns the C expr that surrounds point."
1138 (interactive)
1139 (save-excursion
1140 (let ((p) (expr) (test-expr))
1141 (setq p (point))
1142 (setq expr (expr-cur))
1143 (setq test-expr (expr-prev))
1144 (while (expr-compound test-expr expr)
1145 (setq expr (cons (car test-expr) (cdr expr)))
1146 (goto-char (car expr))
1147 (setq test-expr (expr-prev)))
1148 (goto-char p)
1149 (setq test-expr (expr-next))
1150 (while (expr-compound expr test-expr)
1151 (setq expr (cons (car expr) (cdr test-expr)))
1152 (setq test-expr (expr-next))
1153 )
1154 (buffer-substring (car expr) (cdr expr)))))
1155
1156 (defun expr-cur ()
1157 "Returns the expr that point is in; point is set to beginning of expr.
1158 The expr is represented as a cons cell, where the car specifies the point in
1159 the current buffer that marks the beginning of the expr and the cdr specifies
1160 the character after the end of the expr."
1161 (let ((p (point)) (begin) (end))
1162 (expr-backward-sexp)
1163 (setq begin (point))
1164 (expr-forward-sexp)
1165 (setq end (point))
1166 (if (>= p end)
1167 (progn
1168 (setq begin p)
1169 (goto-char p)
1170 (expr-forward-sexp)
1171 (setq end (point))
1172 )
1173 )
1174 (goto-char begin)
1175 (cons begin end)))
1176
1177 (defun expr-backward-sexp ()
1178 "Version of `backward-sexp' that catches errors."
1179 (condition-case nil
1180 (backward-sexp)
1181 (error t)))
1182
1183 (defun expr-forward-sexp ()
1184 "Version of `forward-sexp' that catches errors."
1185 (condition-case nil
1186 (forward-sexp)
1187 (error t)))
1188
1189 (defun expr-prev ()
1190 "Returns the previous expr, point is set to beginning of that expr.
1191 The expr is represented as a cons cell, where the car specifies the point in
1192 the current buffer that marks the beginning of the expr and the cdr specifies
1193 the character after the end of the expr"
1194 (let ((begin) (end))
1195 (expr-backward-sexp)
1196 (setq begin (point))
1197 (expr-forward-sexp)
1198 (setq end (point))
1199 (goto-char begin)
1200 (cons begin end)))
1201
1202 (defun expr-next ()
1203 "Returns the following expr, point is set to beginning of that expr.
1204 The expr is represented as a cons cell, where the car specifies the point in
1205 the current buffer that marks the beginning of the expr and the cdr specifies
1206 the character after the end of the expr."
1207 (let ((begin) (end))
1208 (expr-forward-sexp)
1209 (expr-forward-sexp)
1210 (setq end (point))
1211 (expr-backward-sexp)
1212 (setq begin (point))
1213 (cons begin end)))
1214
1215 (defun expr-compound-sep (span-start span-end)
1216 "Returns '.' for '->' & '.', returns ' ' for white space,
1217 returns '?' for other punctuation."
1218 (let ((result ? )
1219 (syntax))
1220 (while (< span-start span-end)
1221 (setq syntax (char-syntax (char-after span-start)))
1222 (cond
1223 ((= syntax ? ) t)
1224 ((= syntax ?.) (setq syntax (char-after span-start))
1225 (cond
1226 ((= syntax ?.) (setq result ?.))
1227 ((and (= syntax ?-) (= (char-after (+ span-start 1)) ?>))
1228 (setq result ?.)
1229 (setq span-start (+ span-start 1)))
1230 (t (setq span-start span-end)
1231 (setq result ??)))))
1232 (setq span-start (+ span-start 1)))
1233 result))
1234
1235 (defun expr-compound (first second)
1236 "Non-nil if concatenating FIRST and SECOND makes a single C token.
1237 The two exprs are represented as a cons cells, where the car
1238 specifies the point in the current buffer that marks the beginning of the
1239 expr and the cdr specifies the character after the end of the expr.
1240 Link exprs of the form:
1241 Expr -> Expr
1242 Expr . Expr
1243 Expr (Expr)
1244 Expr [Expr]
1245 (Expr) Expr
1246 [Expr] Expr"
1247 (let ((span-start (cdr first))
1248 (span-end (car second))
1249 (syntax))
1250 (setq syntax (expr-compound-sep span-start span-end))
1251 (cond
1252 ((= (car first) (car second)) nil)
1253 ((= (cdr first) (cdr second)) nil)
1254 ((= syntax ?.) t)
1255 ((= syntax ? )
1256 (setq span-start (char-after (- span-start 1)))
1257 (setq span-end (char-after span-end))
1258 (cond
1259 ((= span-start ?) ) t )
1260 ((= span-start ?] ) t )
1261 ((= span-end ?( ) t )
1262 ((= span-end ?[ ) t )
1263 (t nil))
1264 )
1265 (t nil))))
1266
1267 (provide 'gud)
1268
1269 ;;; gud.el ends here