Add 2012 to FSF copyright years for Emacs files
[bpt/emacs.git] / lisp / progmodes / idlw-shell.el
1 ;; idlw-shell.el --- run IDL as an inferior process of Emacs.
2
3 ;; Copyright (C) 1999-2012 Free Software Foundation, Inc.
4
5 ;; Authors: J.D. Smith <jdsmith@as.arizona.edu>
6 ;; Carsten Dominik <dominik@astro.uva.nl>
7 ;; Chris Chase <chase@att.com>
8 ;; Maintainer: J.D. Smith <jdsmith@as.arizona.edu>
9 ;; Version: 6.1.22
10 ;; Keywords: processes
11 ;; Package: idlwave
12
13 ;; This file is part of GNU Emacs.
14
15 ;; GNU Emacs is free software: you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation, either version 3 of the License, or
18 ;; (at your option) any later version.
19
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
24
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27
28 ;;; Commentary:
29 ;;
30 ;; This mode is for IDL version 5 or later. It should work on
31 ;; Emacs>20.3 or XEmacs>20.4.
32 ;;
33 ;; Runs IDL as an inferior process of Emacs, much like the Emacs
34 ;; `shell' or `telnet' commands. Provides command history and
35 ;; searching. Provides debugging commands available in buffers
36 ;; visiting IDL procedure files, e.g., breakpoint setting, stepping,
37 ;; execution until a certain line, printing expressions under point,
38 ;; visual line pointer for current execution line, etc.
39 ;;
40 ;; Documentation should be available online with `M-x idlwave-info'.
41 ;;
42 ;; New versions of IDLWAVE, documentation, and more information
43 ;; available from:
44 ;; http://idlwave.org
45 ;;
46 ;; INSTALLATION:
47 ;; =============
48 ;;
49 ;; Follow the instructions in the INSTALL file of the distribution.
50 ;; In short, put this file on your load path and add the following
51 ;; lines to your .emacs file:
52 ;;
53 ;; (autoload 'idlwave-shell "idlw-shell" "IDLWAVE Shell" t)
54 ;;
55 ;;
56 ;; SOURCE
57 ;; ======
58 ;;
59 ;; The newest version of this file can be found on the maintainers
60 ;; web site.
61 ;;
62 ;; http://idlwave.org
63 ;;
64 ;; DOCUMENTATION
65 ;; =============
66 ;;
67 ;; IDLWAVE is documented online in info format.
68 ;; A printable version of the documentation is available from the
69 ;; maintainers webpage (see under SOURCE)
70 ;;
71 ;;
72 ;; KNOWN PROBLEMS
73 ;; ==============
74 ;;
75 ;; Under XEmacs the Debug menu in the shell does not display the
76 ;; keybindings in the prefix map. There bindings are available anyway - so
77 ;; it is a bug in XEmacs.
78 ;; The Debug menu in source buffers *does* display the bindings correctly.
79 ;;
80 ;;
81 ;; CUSTOMIZATION VARIABLES
82 ;; =======================
83 ;;
84 ;; IDLWAVE has customize support - so if you want to learn about
85 ;; the variables which control the behavior of the mode, use
86 ;; `M-x idlwave-customize'.
87 ;;
88 ;;--------------------------------------------------------------------------
89 ;;
90 \f
91 ;;; Code:
92
93 (require 'comint)
94 (require 'idlwave)
95
96 (eval-when-compile (require 'cl))
97
98 (defvar idlwave-shell-have-new-custom nil)
99
100 ;;; Customizations: idlwave-shell group
101
102 ;; General/Misc. customizations
103 (defgroup idlwave-shell-general-setup nil
104 "General setup of the Shell interaction for IDLWAVE/Shell."
105 :prefix "idlwave-shell"
106 :group 'idlwave)
107
108 (defcustom idlwave-shell-prompt-pattern "^\r? ?IDL> "
109 "*Regexp to match IDL prompt at beginning of a line.
110 For example, \"^\r?IDL> \" or \"^\r?WAVE> \".
111 The \"^\r?\" is needed, to indicate the beginning of the line, with
112 optional return character (which IDL seems to output randomly).
113 This variable is used to initialize `comint-prompt-regexp' in the
114 process buffer.
115
116 This is a fine thing to set in your `.emacs' file."
117 :group 'idlwave-shell-general-setup
118 :type 'regexp)
119
120 (defcustom idlwave-shell-process-name "idl"
121 "*Name to be associated with the IDL process. The buffer for the
122 process output is made by surrounding this name with `*'s."
123 :group 'idlwave-shell-general-setup
124 :type 'string)
125
126 ;; (defcustom idlwave-shell-automatic-start...) See idlwave.el
127
128 (defcustom idlwave-shell-use-dedicated-window nil
129 "*Non-nil means, never replace the shell frame with another buffer."
130 :group 'idlwave-shell-general-setup
131 :type 'boolean)
132
133 (defcustom idlwave-shell-use-dedicated-frame nil
134 "*Non-nil means, IDLWAVE should use a special frame to display shell buffer."
135 :group 'idlwave-shell-general-setup
136 :type 'boolean)
137
138 (defcustom idlwave-shell-frame-parameters
139 '((height . 30) (unsplittable . nil))
140 "The frame parameters for a dedicated idlwave-shell frame.
141 See also `idlwave-shell-use-dedicated-frame'.
142 The default makes the frame splittable, so that completion works correctly."
143 :group 'idlwave-shell-general-setup
144 :type '(repeat
145 (cons symbol sexp)))
146
147 (defcustom idlwave-shell-raise-frame t
148 "*Non-nil means, `idlwave-shell' raises the frame showing the shell window."
149 :group 'idlwave-shell-general-setup
150 :type 'boolean)
151
152 (defcustom idlwave-shell-arrows-do-history t
153 "*Non-nil means UP and DOWN arrows move through command history.
154 This variable can have 3 values:
155 nil Arrows just move the cursor
156 t Arrows force the cursor back to the current command line and
157 walk the history
158 'cmdline When the cursor is in the current command line, arrows walk the
159 history. Everywhere else in the buffer, arrows move the cursor."
160 :group 'idlwave-shell-general-setup
161 :type '(choice
162 (const :tag "never" nil)
163 (const :tag "everywhere" t)
164 (const :tag "in command line only" cmdline)))
165
166 ;; FIXME: add comint-input-ring-size?
167
168 (defcustom idlwave-shell-use-toolbar t
169 "*Non-nil means, use the debugging toolbar in all IDL related buffers.
170 Starting the shell will then add the toolbar to all idlwave-mode buffers.
171 Exiting the shell will removed everywhere.
172 Available on XEmacs and on Emacs 21.x or later.
173 At any time you can toggle the display of the toolbar with
174 `C-c C-d C-t' (`idlwave-shell-toggle-toolbar')."
175 :group 'idlwave-shell-general-setup
176 :type 'boolean)
177
178 (defcustom idlwave-shell-temp-pro-prefix "/tmp/idltemp"
179 "*The prefix for temporary IDL files used when compiling regions.
180 It should be an absolute pathname.
181 The full temporary file name is obtained by using `make-temp-file'
182 so that the name will be unique among multiple Emacs processes."
183 :group 'idlwave-shell-general-setup
184 :type 'string)
185
186 (defvar idlwave-shell-fix-inserted-breaks nil
187 "*OBSOLETE VARIABLE, is no longer used.
188
189 The documentation of this variable used to be:
190 If non-nil then run `idlwave-shell-remove-breaks' to clean up IDL messages.")
191
192 (defcustom idlwave-shell-prefix-key "\C-c\C-d"
193 "*The prefix key for the debugging map `idlwave-shell-mode-prefix-map'.
194 This variable must already be set when idlwave-shell.el is loaded.
195 Setting it in the mode-hook is too late."
196 :group 'idlwave-shell-general-setup
197 :type 'string)
198
199 (defcustom idlwave-shell-activate-prefix-keybindings t
200 "Non-nil means, the debug commands will be bound to the prefix key.
201 The prefix key itself is given in the option `idlwave-shell-prefix-key'.
202 So by default setting a breakpoint will be on C-c C-d C-b."
203 :group 'idlwave-shell-general-setup
204 :type 'boolean)
205
206 (defcustom idlwave-shell-automatic-electric-debug 'breakpoint
207 "Enter the electric-debug minor mode automatically.
208 This occurs at a breakpoint or any other halt. The mode is exited
209 upon return to the main level. Can be set to 'breakpoint to enter
210 electric debug mode only when breakpoints are tripped."
211 :group 'idlwave-shell-general-setup
212 :type '(choice
213 (const :tag "never" nil)
214 (const :tag "always" t)
215 (const :tag "for breakpoints only" breakpoint)))
216
217 (defcustom idlwave-shell-electric-zap-to-file t
218 "When entering electric debug mode, select the window displaying the
219 file at which point is stopped. This takes point away from the shell
220 window, but is useful for stepping, etc."
221 :group 'idlwave-shell-general-setup
222 :type 'boolean)
223
224 ;; (defcustom idlwave-shell-debug-modifiers... See idlwave.el
225
226 (defvar idlwave-shell-activate-alt-keybindings nil
227 "Obsolete variable. See `idlwave-shell-debug-modifiers'.")
228
229 (defcustom idlwave-shell-use-truename nil
230 "*Non-nil means, use use `file-truename' when looking for buffers.
231 If this variable is non-nil, Emacs will use the function `file-truename' to
232 resolve symbolic links in the file paths printed by e.g., STOP commands.
233 This means, unvisited files will be loaded under their truename.
234 However, when a file is already visited under a different name, IDLWAVE will
235 reuse that buffer.
236 This option was once introduced in order to avoid multiple buffers visiting
237 the same file. However, IDLWAVE no longer makes this mistake, so it is safe
238 to set this option to nil."
239 :group 'idlwave-shell-general-setup
240 :type 'boolean)
241
242 (defcustom idlwave-shell-file-name-chars "~/A-Za-z0-9+:_.$#%={}\\- "
243 "The characters allowed in file names, as a string.
244 Used for file name completion. Must not contain `'', `,' and `\"'
245 because these are used as separators by IDL."
246 :group 'idlwave-shell-general-setup
247 :type 'string)
248
249 (defcustom idlwave-shell-mode-hook '()
250 "*Hook for customizing `idlwave-shell-mode'."
251 :group 'idlwave-shell-general-setup
252 :type 'hook)
253
254 (defcustom idlwave-shell-graphics-window-size '(500 400)
255 "Size of IDL graphics windows popped up by special IDLWAVE command.
256 The command is `C-c C-d C-f' and accepts as a prefix the window nr.
257 A command like `WINDOW,N,xsize=XX,ysize=YY' is sent to IDL."
258 :group 'idlwave-shell-general-setup
259 :type '(list
260 (integer :tag "x size")
261 (integer :tag "y size")))
262
263
264 ;; Commands Sent to Shell... etc.
265 (defgroup idlwave-shell-command-setup nil
266 "Setup for command parameters of the Shell interaction for IDLWAVE."
267 :prefix "idlwave-shell"
268 :group 'idlwave)
269
270 (defcustom idlwave-shell-initial-commands "!more=0 & defsysv,'!ERROR_STATE',EXISTS=__e & if __e then begin & !ERROR_STATE.MSG_PREFIX=\"% \" & delvar,__e & endif"
271 "Initial commands, separated by newlines, to send to IDL.
272 This string is sent to the IDL process by `idlwave-shell-mode' which is
273 invoked by `idlwave-shell'."
274 :group 'idlwave-shell-command-setup
275 :type 'string)
276
277 (defcustom idlwave-shell-save-command-history t
278 "Non-nil means preserve command history between sessions.
279 The file `idlwave-shell-command-history-file' is used to save and restore
280 the history."
281 :group 'idlwave-shell-command-setup
282 :type 'boolean)
283
284 (defcustom idlwave-shell-command-history-file "idlwhist"
285 "The file in which the command history of the idlwave shell is saved.
286 In order to change the size of the history, see the variable
287 `comint-input-ring-size'.
288 The history is only saved if the variable `idlwave-shell-save-command-history'
289 is non-nil."
290 :group 'idlwave-shell-command-setup
291 :type 'file)
292
293 (defcustom idlwave-shell-show-commands
294 '(run misc breakpoint)
295 "*A list of command types to show output from in the shell.
296 Possibilities are 'run, 'debug, 'breakpoint, and 'misc. Unselected
297 types are not displayed in the shell. The type 'everything causes all
298 the copious shell traffic to be displayed."
299 :group 'idlwave-shell-command-setup
300 :type '(choice
301 (const everything)
302 (set :tag "Checklist" :greedy t
303 (const :tag "All .run and .compile commands" run)
304 (const :tag "All breakpoint commands" breakpoint)
305 (const :tag "All debug and stepping commands" debug)
306 (const :tag "Close, window, retall, etc. commands" misc))))
307
308 (defcustom idlwave-shell-max-print-length 200
309 "Maximum number of array elements to print when examining."
310 :group 'idlwave-shell-command-setup
311 :type 'integer)
312
313 (defcustom idlwave-shell-examine-alist
314 `(("Print" . ,(concat "idlwave_print_safe,___,"
315 (number-to-string
316 idlwave-shell-max-print-length)))
317 ("Help" . "help,___")
318 ("Structure Help" . "help,___,/STRUCTURE")
319 ("Dimensions" . "print,size(___,/DIMENSIONS)")
320 ("Type" . "print,size(___,/TNAME)")
321 ("N_Elements" . "print,n_elements(___)")
322 ("All Size Info" . "help,(__IWsz__=size(___,/STRUCTURE)),/STRUCTURE & print,__IWsz__.DIMENSIONS")
323 ("Ptr Valid" . "print,ptr_valid(___)")
324 ("Arg Present" . "print,arg_present(___)")
325 ("Widget Valid" . "print,widget_info(___,/VALID)")
326 ("Widget Geometry" . "help,widget_info(___,/GEOMETRY)"))
327 "Alist of special examine commands for popup selection.
328 The keys are used in the selection popup created by
329 `idlwave-shell-examine-select', and the corresponding value is sent as
330 a command to the shell, with special sequence `___' replaced by the
331 expression being examined."
332 :group 'idlwave-shell-command-setup
333 :type '(repeat
334 (cons
335 (string :tag "Label ")
336 (string :tag "Command"))))
337
338 (defvar idlwave-shell-print-expression-function nil
339 "*OBSOLETE VARIABLE, is no longer used.")
340
341 (defcustom idlwave-shell-separate-examine-output t
342 "*Non-nil means, put output of examine commands in their own buffer."
343 :group 'idlwave-shell-command-setup
344 :type 'boolean)
345
346 (defcustom idlwave-shell-comint-settings
347 '((comint-scroll-to-bottom-on-input . t)
348 (comint-scroll-to-bottom-on-output . t)
349 (comint-scroll-show-maximum-output . nil)
350 (comint-prompt-read-only . t))
351
352 "Alist of special settings for the comint variables in the IDLWAVE Shell.
353 Each entry is a cons cell with the name of a variable and a value.
354 The function `idlwave-shell-mode' will make local variables out of each entry.
355 Changes to this variable will only be active when the shell buffer is
356 newly created."
357 :group 'idlwave-shell-command-setup
358 :type '(repeat
359 (cons variable sexp)))
360
361 (defcustom idlwave-shell-query-for-class t
362 "*Non-nil means query the shell for object class on object completions."
363 :group 'idlwave-shell-command-setup
364 :type 'boolean)
365
366 (defcustom idlwave-shell-use-input-mode-magic nil
367 "*Non-nil means, IDLWAVE should check for input mode spells in output.
368 The spells are strings printed by your IDL program and matched
369 by the regular expressions in `idlwave-shell-input-mode-spells'.
370 When these expressions match, IDLWAVE switches to character input mode and
371 back, respectively. See `idlwave-shell-input-mode-spells' for details."
372 :group 'idlwave-shell-command-setup
373 :type 'boolean)
374
375 (defcustom idlwave-shell-input-mode-spells
376 '("^<onechar>$" "^<chars>$" "^</chars>$")
377 "The three regular expressions which match the magic spells for input modes.
378
379 When the first regexp matches in the output stream of IDL, IDLWAVE
380 prompts for a single character and sends it immediately to IDL, similar
381 to the command \\[idlwave-shell-send-char].
382
383 When the second regexp matches, IDLWAVE switches to a blocking
384 single-character input mode. This is the same mode which can be entered
385 manually with \\[idlwave-shell-char-mode-loop].
386 This input mode exits when the third regexp matches in the output,
387 or when the IDL prompt is encountered.
388
389 The variable `idlwave-shell-use-input-mode-magic' must be non-nil to enable
390 scanning for these expressions. If the IDL program produces lots of
391 output, shell operation may be slowed down.
392
393 This mechanism is useful for correct interaction with the IDL function
394 GET_KBRD, because in normal operation IDLWAVE only sends \\n terminated
395 strings. Here is some example code which makes use of the default spells.
396
397 print,'<chars>' ; Make IDLWAVE switch to character mode
398 REPEAT BEGIN
399 A = GET_KBRD(1)
400 PRINT, BYTE(A)
401 ENDREP UNTIL A EQ 'q'
402 print,'</chars>' ; Make IDLWAVE switch back to line mode
403
404 print,'Quit the program, y or n?'
405 print,'<onechar>' ; Ask IDLWAVE to send one character
406 answer = GET_KBRD(1)
407
408 Since the IDLWAVE shell defines the system variable `!IDLWAVE_VERSION',
409 you could actually check if you are running under Emacs before printing
410 the magic strings. Here is a procedure which uses this.
411
412 Usage:
413 ======
414 idlwave_char_input ; Make IDLWAVE send one character
415 idlwave_char_input,/on ; Start the loop to send characters
416 idlwave_char_input,/off ; End the loop to send characters
417
418
419 pro idlwave_char_input,on=on,off=off
420 ;; Test if we are running under Emacs
421 defsysv,'!idlwave_version',exists=running_emacs
422 if running_emacs then begin
423 if keyword_set(on) then print,'<chars>' $
424 else if keyword_set(off) then print,'</chars>' $
425 else print,'<onechar>'
426 endif
427 end"
428 :group 'idlwave-shell-command-setup
429 :type '(list
430 (regexp :tag "One-char regexp")
431 (regexp :tag "Char-mode regexp")
432 (regexp :tag "Line-mode regexp")))
433
434 (defcustom idlwave-shell-breakpoint-popup-menu t
435 "*If non-nil, provide a menu on mouse-3 on breakpoint lines, and
436 popup help text on the line."
437 :group 'idlwave-shell-command-setup
438 :type 'boolean)
439
440 (defcustom idlwave-shell-reset-no-prompt nil
441 "If non-nil, skip the yes/no prompt when resetting the IDL session."
442 :group 'idlwave-shell-command-setup
443 :type 'boolean)
444
445 ;; Breakpoint Overlays etc
446 (defgroup idlwave-shell-highlighting-and-faces nil
447 "Highlighting and faces used by the IDLWAVE Shell mode."
448 :prefix "idlwave-shell"
449 :group 'idlwave)
450
451 (defcustom idlwave-shell-mark-stop-line t
452 "*Non-nil means, mark the source code line where IDL is currently stopped.
453 Value decides about the method which is used to mark the line. Valid values
454 are:
455
456 nil Do not mark the line
457 'arrow Use the overlay arrow
458 'face Use `idlwave-shell-stop-line-face' to highlight the line.
459 t Use what IDLWAVE thinks is best. Will be a face where possible,
460 otherwise the overlay arrow.
461 The overlay-arrow has the disadvantage to hide the first chars of a line.
462 Since many people do not have the main block of IDL programs indented,
463 a face highlighting may be better.
464 In Emacs 21, the overlay arrow is displayed in a special area and never
465 hides any code, so setting this to 'arrow on Emacs 21 sounds like a good idea."
466 :group 'idlwave-shell-highlighting-and-faces
467 :type '(choice
468 (const :tag "No marking" nil)
469 (const :tag "Use overlay arrow" arrow)
470 (const :tag "Highlight with face" face)
471 (const :tag "Face or arrow." t)))
472
473 (defcustom idlwave-shell-overlay-arrow ">"
474 "*The overlay arrow to display at source lines where execution halts.
475 We use a single character by default, since the main block of IDL procedures
476 often has no indentation. Where possible, IDLWAVE will use overlays to
477 display the stop-lines. The arrow is only used on character-based terminals.
478 See also `idlwave-shell-use-overlay-arrow'."
479 :group 'idlwave-shell-highlighting-and-faces
480 :type 'string)
481
482 (defcustom idlwave-shell-stop-line-face 'highlight
483 "*The face for `idlwave-shell-stop-line-overlay'.
484 Allows you to choose the font, color and other properties for
485 line where IDL is stopped. See also `idlwave-shell-mark-stop-line'."
486 :group 'idlwave-shell-highlighting-and-faces
487 :type 'symbol)
488
489 (defcustom idlwave-shell-electric-stop-color "Violet"
490 "*The color for the default face or overlay arrow when stopped."
491 :group 'idlwave-shell-highlighting-and-faces
492 :type 'string)
493
494 (defcustom idlwave-shell-electric-stop-line-face
495 (prog1
496 (copy-face 'modeline 'idlwave-shell-electric-stop-line)
497 (set-face-background 'idlwave-shell-electric-stop-line
498 idlwave-shell-electric-stop-color)
499 (condition-case nil
500 (set-face-foreground 'idlwave-shell-electric-stop-line nil)
501 (error nil)))
502 "*The face for `idlwave-shell-stop-line-overlay' when in electric debug mode.
503 Allows you to choose the font, color and other properties for the line
504 where IDL is stopped, when in Electric Debug Mode."
505 :group 'idlwave-shell-highlighting-and-faces
506 :type 'symbol)
507
508 (defcustom idlwave-shell-mark-breakpoints t
509 "*Non-nil means, mark breakpoints in the source files.
510 Valid values are:
511 nil Do not mark breakpoints.
512 'face Highlight line with `idlwave-shell-breakpoint-face'.
513 'glyph Red dot at the beginning of line. If the display does not
514 support glyphs, will use 'face instead.
515 t Glyph when possible, otherwise face (same effect as 'glyph)."
516 :group 'idlwave-shell-highlighting-and-faces
517 :type '(choice
518 (const :tag "No marking" nil)
519 (const :tag "Highlight with face" face)
520 (const :tag "Display glyph (red dot)" glyph)
521 (const :tag "Glyph or face." t)))
522
523 (defvar idlwave-shell-use-breakpoint-glyph t
524 "Obsolete variable. See `idlwave-shell-mark-breakpoints'.")
525
526 (defcustom idlwave-shell-breakpoint-face 'idlwave-shell-bp
527 "*The face for breakpoint lines in the source code.
528 Allows you to choose the font, color and other properties for
529 lines which have a breakpoint. See also `idlwave-shell-mark-breakpoints'."
530 :group 'idlwave-shell-highlighting-and-faces
531 :type 'symbol)
532
533 (if (not idlwave-shell-have-new-custom)
534 ;; Just copy the underline face to be on the safe side.
535 (copy-face 'underline 'idlwave-shell-bp)
536 ;; We have the new customize - use it to define a customizable face
537 (defface idlwave-shell-bp
538 '((((class color)) (:foreground "Black" :background "Pink"))
539 (t (:underline t)))
540 "Face for highlighting lines with breakpoints."
541 :group 'idlwave-shell-highlighting-and-faces))
542
543 (defcustom idlwave-shell-disabled-breakpoint-face
544 'idlwave-shell-disabled-bp
545 "*The face for disabled breakpoint lines in the source code.
546 Allows you to choose the font, color and other properties for
547 lines which have a breakpoint. See also `idlwave-shell-mark-breakpoints'."
548 :group 'idlwave-shell-highlighting-and-faces
549 :type 'symbol)
550
551 (if (not idlwave-shell-have-new-custom)
552 ;; Just copy the underline face to be on the safe side.
553 (copy-face 'underline 'idlwave-shell-disabled-bp)
554 ;; We have the new customize - use it to define a customizable face
555 (defface idlwave-shell-disabled-bp
556 '((((class color)) (:foreground "Black" :background "gray"))
557 (t (:underline t)))
558 "Face for highlighting lines with breakpoints."
559 :group 'idlwave-shell-highlighting-and-faces))
560
561
562 (defcustom idlwave-shell-expression-face 'secondary-selection
563 "*The face for `idlwave-shell-expression-overlay'.
564 Allows you to choose the font, color and other properties for
565 the expression printed by IDL."
566 :group 'idlwave-shell-highlighting-and-faces
567 :type 'symbol)
568
569 (defcustom idlwave-shell-output-face 'secondary-selection
570 "*The face for `idlwave-shell-output-overlay'.
571 Allows you to choose the font, color and other properties for
572 the expression output by IDL."
573 :group 'idlwave-shell-highlighting-and-faces
574 :type 'symbol)
575
576 ;;; End user customization variables
577
578 ;;; External variables
579 (defvar comint-last-input-start)
580 (defvar comint-last-input-end)
581
582 ;; Other variables
583 (defvar idlwave-shell-temp-pro-file nil
584 "Absolute pathname for temporary IDL file for compiling regions")
585
586 (defvar idlwave-shell-temp-rinfo-save-file nil
587 "Absolute pathname for temporary IDL file save file for routine_info.
588 This is used to speed up the reloading of the routine info procedure
589 before use by the shell.")
590
591 (defun idlwave-shell-temp-file (type)
592 "Return a temp file, creating it if necessary.
593
594 TYPE is either 'pro' or 'rinfo', and `idlwave-shell-temp-pro-file' or
595 `idlwave-shell-temp-rinfo-save-file' is set (respectively)."
596 (cond
597 ((eq type 'rinfo)
598 (or idlwave-shell-temp-rinfo-save-file
599 (setq idlwave-shell-temp-rinfo-save-file
600 (idlwave-shell-make-temp-file idlwave-shell-temp-pro-prefix))))
601 ((eq type 'pro)
602 (or idlwave-shell-temp-pro-file
603 (setq idlwave-shell-temp-pro-file
604 (idlwave-shell-make-temp-file idlwave-shell-temp-pro-prefix))))
605 (t (error "Wrong argument (idlwave-shell-temp-file): %s"
606 (symbol-name type)))))
607
608
609 (defun idlwave-shell-make-temp-file (prefix)
610 "Create a temporary file."
611 ; Hard coded make-temp-file for Emacs<21
612 (if (fboundp 'make-temp-file)
613 (make-temp-file prefix)
614 (let (file
615 (temp-file-dir (if (boundp 'temporary-file-directory)
616 temporary-file-directory
617 "/tmp")))
618 (while (condition-case ()
619 (progn
620 (setq file
621 (make-temp-name
622 (expand-file-name prefix temp-file-dir)))
623 (if (featurep 'xemacs)
624 (write-region "" nil file nil 'silent nil)
625 (write-region "" nil file nil 'silent nil 'excl))
626 nil)
627 (file-already-exists t))
628 ;; the file was somehow created by someone else between
629 ;; `make-temp-name' and `write-region', let's try again.
630 nil)
631 file)))
632
633
634 (defvar idlwave-shell-dirstack-query "cd,current=___cur & print,___cur"
635 "Command used by `idlwave-shell-resync-dirs' to query IDL for
636 the directory stack.")
637
638 (defvar idlwave-shell-path-query "print,'PATH:<'+transpose(expand_path(!PATH,/ARRAY))+'>' & print,'SYSDIR:<'+!dir+'>'"
639
640 "The command which gets !PATH and !DIR info from the shell.")
641
642 (defvar idlwave-shell-mode-line-info nil
643 "Additional info displayed in the mode line.")
644
645 (defvar idlwave-shell-default-directory nil
646 "The default directory in the idlwave-shell buffer, of outside use.")
647
648 (defvar idlwave-shell-last-save-and-action-file nil
649 "The last file which was compiled with `idlwave-shell-save-and-...'.")
650
651 ;; Highlighting uses overlays. When necessary, require the emulation.
652 (if (not (fboundp 'make-overlay))
653 (condition-case nil
654 (require 'overlay)
655 (error nil)))
656
657 (defvar idlwave-shell-stop-line-overlay nil
658 "The overlay for where IDL is currently stopped.")
659 (defvar idlwave-shell-is-stopped nil)
660 (defvar idlwave-shell-expression-overlay nil
661 "The overlay for the examined expression.")
662 (defvar idlwave-shell-output-overlay nil
663 "The overlay for the last IDL output.")
664
665 ;; If these were already overlays, delete them. This probably means that we
666 ;; are reloading this file.
667 (if (overlayp idlwave-shell-stop-line-overlay)
668 (delete-overlay idlwave-shell-stop-line-overlay))
669 (if (overlayp idlwave-shell-expression-overlay)
670 (delete-overlay idlwave-shell-expression-overlay))
671 (if (overlayp idlwave-shell-output-overlay)
672 (delete-overlay idlwave-shell-output-overlay))
673
674 ;; Set to nil initially
675 (setq idlwave-shell-stop-line-overlay nil
676 idlwave-shell-expression-overlay nil
677 idlwave-shell-output-overlay nil)
678
679 ;; Define the shell stop overlay. When left nil, the arrow will be used.
680 (cond
681 ((or (null idlwave-shell-mark-stop-line)
682 (eq idlwave-shell-mark-stop-line 'arrow))
683 ;; Leave the overlay nil
684 nil)
685
686 ((eq idlwave-shell-mark-stop-line 'face)
687 ;; Try to use a face. If not possible, arrow will be used anyway
688 ;; So who can display faces?
689 (when (or (featurep 'xemacs) ; XEmacs can do also ttys
690 (fboundp 'tty-defined-colors) ; Emacs 21 as well
691 window-system) ; Window systems always
692 (progn
693 (setq idlwave-shell-stop-line-overlay (make-overlay 1 1))
694 (overlay-put idlwave-shell-stop-line-overlay
695 'face idlwave-shell-stop-line-face))))
696
697 (t
698 ;; IDLWAVE may decide. Will use a face on window systems, arrow elsewhere
699 (if window-system
700 (progn
701 (setq idlwave-shell-stop-line-overlay (make-overlay 1 1))
702 (overlay-put idlwave-shell-stop-line-overlay
703 'face idlwave-shell-stop-line-face)))))
704
705 ;; Now the expression and output overlays
706 (setq idlwave-shell-expression-overlay (make-overlay 1 1))
707 (overlay-put idlwave-shell-expression-overlay
708 'face idlwave-shell-expression-face)
709 (overlay-put idlwave-shell-expression-overlay
710 'priority 1)
711 (setq idlwave-shell-output-overlay (make-overlay 1 1))
712 (overlay-put idlwave-shell-output-overlay
713 'face idlwave-shell-output-face)
714
715 (copy-face idlwave-shell-stop-line-face
716 'idlwave-shell-pending-stop)
717 (copy-face idlwave-shell-electric-stop-line-face
718 'idlwave-shell-pending-electric-stop)
719 (set-face-background 'idlwave-shell-pending-stop "gray70")
720 (set-face-background 'idlwave-shell-pending-electric-stop "gray70")
721
722
723
724 (defvar idlwave-shell-bp-query "help,/breakpoints"
725 "Command to obtain list of breakpoints.")
726
727 (defvar idlwave-shell-command-output nil
728 "String for accumulating current command output.")
729
730 (defvar idlwave-shell-post-command-hook nil
731 "Lisp list expression or function to run when an IDL command is finished.
732 The current command is finished when the IDL prompt is displayed.
733 This is evaluated if it is a list or called with funcall.")
734
735 (defvar idlwave-shell-sentinel-hook nil
736 "Hook run when the IDL process exits.")
737
738 (defvar idlwave-shell-hide-output nil
739 "If non-nil the process output is not inserted into the output buffer.")
740
741 (defvar idlwave-shell-show-if-error nil
742 "If non-nil the process output is inserted into the output buffer if
743 it contains an error message, even if hide-output is non-nil.")
744
745 (defvar idlwave-shell-accumulation nil
746 "Accumulate last line of output.")
747
748 (defvar idlwave-shell-command-line-to-execute nil)
749 (defvar idlwave-shell-cleanup-hook nil
750 "List of functions to do cleanup when the shell exits.")
751
752 (defvar idlwave-shell-pending-commands nil
753 "List of commands to be sent to IDL.
754 Each element of the list is list of \(CMD PCMD HIDE\), where CMD is a
755 string to be sent to IDL and PCMD is a post-command to be placed on
756 `idlwave-shell-post-command-hook'. If HIDE is non-nil, hide the output
757 from command CMD. PCMD and HIDE are optional.")
758
759 (defun idlwave-shell-buffer ()
760 "Name of buffer associated with IDL process.
761 The name of the buffer is made by surrounding `idlwave-shell-process-name'
762 with `*'s."
763 (concat "*" idlwave-shell-process-name "*"))
764
765 (defvar idlwave-shell-ready nil
766 "If non-nil can send next command to IDL process.")
767
768 ;;; The following are the types of messages we attempt to catch to
769 ;;; resync our idea of where IDL execution currently is.
770 ;;;
771
772 (defvar idlwave-shell-halt-frame nil
773 "The frame associated with halt/breakpoint messages.")
774
775 (defvar idlwave-shell-step-frame nil
776 "The frame associated with step messages.")
777
778 (defvar idlwave-shell-trace-frame nil
779 "The frame associated with trace messages.")
780
781 (defconst idlwave-shell-halt-messages
782 '("^% Interrupted at:"
783 "^% Stepped to:"
784 "^% Skipped to:"
785 "^% Stop encountered:"
786 )
787 "*A list of regular expressions matching IDL messages.
788 These are the messages containing file and line information where
789 IDL is currently stopped.")
790
791
792 (defconst idlwave-shell-halt-messages-re
793 (mapconcat 'identity idlwave-shell-halt-messages "\\|")
794 "The regular expression computed from `idlwave-shell-halt-messages'.")
795
796 (defconst idlwave-shell-trace-message-re
797 "^% At " ;; First line of a trace message
798 "*A regular expression matching IDL trace messages. These are the
799 messages containing file and line information of a current
800 traceback.")
801
802 (defconst idlwave-shell-step-messages
803 '("^% Stepped to:"
804 )
805 "*A list of regular expressions matching stepped execution messages.
806 These are IDL messages containing file and line information where
807 IDL has currently stepped.")
808
809 (defvar idlwave-shell-break-message "^% Breakpoint at:"
810 "*Regular expression matching an IDL breakpoint message line.")
811
812 (defconst idlwave-shell-electric-debug-help
813 " ==> IDLWAVE Electric Debug Mode Help <==
814
815 Break Point Setting and Clearing:
816 b Set breakpoint ([C-u b] for conditional, [C-n b] nth hit, etc.).
817 d Clear nearby breakpoint.
818 a Clear all breakpoints.
819 i Set breakpoint in routine named here.
820 j Set breakpoint at beginning of containing routine.
821 \\ Toggle breakpoint disable
822 ] Go to next breakpoint in file.
823 [ Go to previous breakpoint in file.
824
825 Stepping, Continuing, and the Stack:
826 s or SPACE Step, into function calls.
827 n Step, over function calls.
828 k Skip one statement.
829 m Continue to end of function.
830 o Continue past end of function.
831 u Continue to end of block.
832 h Continue to line at cursor position.
833 r Continue execution to next breakpoint, if any.
834 + or = Show higher level in calling stack.
835 - or _ Show lower level in calling stack.
836
837 Examining Expressions (with prefix for examining the region):
838 p Print expression near point or in region ([C-u p]).
839 ? Help on expression near point or in region ([C-u ?]).
840 x Examine expression near point or in region ([C-u x]) with
841 letter completion of the examine type.
842 e Prompt for an expression to print.
843
844 Miscellaneous:
845 q Quit - end debugging session and return to the Shell's main level.
846 v Turn Electric Debugging Mode off (C-c C-d C-v to return).
847 t Print a calling-level traceback in the shell.
848 z Reset IDL.
849 C-? Show this help menu.")
850
851 (defvar idlwave-shell-bp-alist)
852 ;(defvar idlwave-shell-post-command-output)
853 (defvar idlwave-shell-sources-alist)
854 (defvar idlwave-shell-menu-def)
855 (defvar idlwave-shell-mode-menu)
856 (defvar idlwave-shell-initial-commands)
857 (defvar idlwave-shell-syntax-error)
858 (defvar idlwave-shell-other-error)
859 (defvar idlwave-shell-error-buffer)
860 (defvar idlwave-shell-error-last)
861 (defvar idlwave-shell-bp-buffer)
862 (defvar idlwave-shell-sources-query)
863 (defvar idlwave-shell-mode-map)
864 (defvar idlwave-shell-calling-stack-index)
865 (defvar idlwave-shell-only-prompt-pattern nil)
866 (defvar tool-bar-map)
867
868 (define-derived-mode idlwave-shell-mode comint-mode "IDL-Shell"
869 "Major mode for interacting with an inferior IDL process.
870
871 1. Shell Interaction
872 -----------------
873 RET after the end of the process' output sends the text from the
874 end of process to the end of the current line. RET before end of
875 process output copies the current line (except for the prompt) to
876 the end of the buffer.
877
878 Command history, searching of previous commands, command line
879 editing are available via the comint-mode key bindings, by default
880 mostly on the key `C-c'. Command history is also available with
881 the arrow keys UP and DOWN.
882
883 2. Completion
884 ----------
885 TAB and M-TAB do completion of IDL routines, classes and keywords -
886 similar to M-TAB in `idlwave-mode'. In executive commands and
887 strings, it completes file names. Abbreviations are also expanded
888 like in `idlwave-mode'.
889
890 3. Routine Info
891 ------------
892 `\\[idlwave-routine-info]' displays information about an IDL routine near point,
893 just like in `idlwave-mode'. The module used is the one at point or
894 the one whose argument list is being edited.
895 To update IDLWAVE's knowledge about compiled or edited modules, use
896 \\[idlwave-update-routine-info].
897 \\[idlwave-find-module] find the source of a module.
898 \\[idlwave-resolve] tells IDL to compile an unresolved module.
899 \\[idlwave-context-help] shows the online help on the item at
900 point, if online help has been installed.
901
902
903 4. Debugging
904 ---------
905 A complete set of commands for compiling and debugging IDL programs
906 is available from the menu. Also keybindings starting with a
907 `C-c C-d' prefix are available for most commands in the *idl* buffer
908 and also in source buffers. The best place to learn about the
909 keybindings is again the menu.
910
911 On Emacs versions where this is possible, a debugging toolbar is
912 installed.
913
914 When IDL is halted in the middle of a procedure, the corresponding
915 line of that procedure file is displayed with an overlay in another
916 window. Breakpoints are also highlighted in the source.
917
918 \\[idlwave-shell-resync-dirs] queries IDL in order to change Emacs current directory
919 to correspond to the IDL process current directory.
920
921 5. Expression Examination
922 ----------------------
923
924 Expressions near point can be examined with print,
925 \\[idlwave-shell-print] or \\[idlwave-shell-mouse-print] with the
926 mouse, help, \\[idlwave-shell-help-expression] or
927 \\[idlwave-shell-mouse-help] with the mouse, or with a
928 configurable set of custom examine commands using
929 \\[idlwave-shell-examine-select]. The mouse examine commands can
930 also work by click and drag, to select an expression for
931 examination.
932
933 6. Hooks
934 -----
935 Turning on `idlwave-shell-mode' runs `comint-mode-hook' and
936 `idlwave-shell-mode-hook' (in that order).
937
938 7. Documentation and Customization
939 -------------------------------
940 Info documentation for this package is available. Use \\[idlwave-info]
941 to display (complain to your sysadmin if that does not work).
942 For PostScript and HTML versions of the documentation, check IDLWAVE's
943 homepage at URL `http://idlwave.org'.
944 IDLWAVE has customize support - see the group `idlwave'.
945
946 8. Keybindings
947 -----------
948 \\{idlwave-shell-mode-map}"
949 :abbrev-table idlwave-mode-abbrev-table
950 (idlwave-setup) ; Make sure config files and paths, etc. are available.
951 (unless (file-name-absolute-p idlwave-shell-command-history-file)
952 (setq idlwave-shell-command-history-file
953 (expand-file-name idlwave-shell-command-history-file
954 idlwave-config-directory)))
955
956 (setq comint-prompt-regexp idlwave-shell-prompt-pattern)
957 (setq comint-process-echoes t)
958
959 ;; Can not use history expansion because "!" is used for system variables.
960 (setq comint-input-autoexpand nil)
961 ;; (setq comint-input-ring-size 64)
962
963 (set (make-local-variable 'completion-ignore-case) t)
964 (set (make-local-variable 'comint-completion-addsuffix) '("/" . ""))
965 (setq comint-input-ignoredups t)
966 (setq idlwave-shell-mode-line-info nil)
967 (setq mode-line-format
968 '(""
969 mode-line-modified
970 mode-line-buffer-identification
971 " "
972 global-mode-string
973 " %[("
974 mode-name
975 mode-line-process
976 minor-mode-alist
977 "%n"
978 ")%]-"
979 idlwave-shell-mode-line-info
980 "---"
981 (line-number-mode "L%l--")
982 (column-number-mode "C%c--")
983 (-3 . "%p")
984 "-%-"))
985 ;; (make-local-variable 'idlwave-shell-bp-alist)
986 (setq idlwave-shell-halt-frame nil
987 idlwave-shell-trace-frame nil
988 idlwave-shell-command-output nil
989 idlwave-shell-step-frame nil)
990 (idlwave-shell-display-line nil)
991 (setq idlwave-shell-calling-stack-index 0)
992 (setq idlwave-shell-only-prompt-pattern
993 (concat "\\`[ \t\n]*"
994 (substring idlwave-shell-prompt-pattern 1)
995 "[ \t\n]*\\'"))
996
997 (when idlwave-shell-query-for-class
998 (add-to-list (make-local-variable 'idlwave-determine-class-special)
999 'idlwave-shell-get-object-class)
1000 (setq idlwave-store-inquired-class t))
1001
1002 ;; Make sure comint-last-input-end does not go to beginning of
1003 ;; buffer (in case there were other processes already in this buffer).
1004 (set-marker comint-last-input-end (point))
1005 (setq idlwave-idlwave_routine_info-compiled nil)
1006 (setq idlwave-shell-ready nil)
1007 (setq idlwave-shell-bp-alist nil)
1008 (idlwave-shell-update-bp-overlays) ; Throw away old overlays
1009 (setq idlwave-shell-post-command-hook nil ;clean up any old stuff
1010 idlwave-shell-sources-alist nil)
1011 (setq idlwave-shell-default-directory default-directory)
1012 (setq idlwave-shell-hide-output nil)
1013
1014 ;; NB: `make-local-hook' needed for older/alternative Emacs compatibility
1015 ;; (make-local-hook 'kill-buffer-hook)
1016 (add-hook 'kill-buffer-hook 'idlwave-shell-kill-shell-buffer-confirm
1017 nil 'local)
1018 (add-hook 'kill-buffer-hook 'idlwave-shell-delete-temp-files nil 'local)
1019 (add-hook 'kill-emacs-hook 'idlwave-shell-delete-temp-files)
1020 (easy-menu-add idlwave-shell-mode-menu idlwave-shell-mode-map)
1021
1022 ;; Set the optional comint variables
1023 (when idlwave-shell-comint-settings
1024 (let ((list idlwave-shell-comint-settings) entry)
1025 (while (setq entry (pop list))
1026 (set (make-local-variable (car entry)) (cdr entry)))))
1027
1028
1029 (unless (memq 'comint-carriage-motion
1030 (default-value 'comint-output-filter-functions))
1031 ;; Strip those pesky ctrl-m's.
1032 (add-hook 'comint-output-filter-functions
1033 (lambda (string)
1034 (when (string-match "\r" string)
1035 (let ((pmark (process-mark (get-buffer-process
1036 (current-buffer)))))
1037 (save-excursion
1038 ;; bare CR -> delete preceding line
1039 (goto-char comint-last-output-start)
1040 (while (search-forward "\r" pmark t)
1041 (delete-region (point) (line-beginning-position)))))))
1042 'append 'local)
1043 (add-hook 'comint-output-filter-functions 'comint-strip-ctrl-m nil 'local))
1044
1045 ;; Python-mode, bundled with many Emacs installs, quite cavalierly
1046 ;; adds this function to the global default hook. It interferes
1047 ;; with overlay-arrows.
1048 (remove-hook 'comint-output-filter-functions 'py-pdbtrack-track-stack-file)
1049
1050 ;; IDLWAVE syntax, and turn on abbreviations
1051 (set (make-local-variable 'comment-start) ";")
1052 (setq abbrev-mode t)
1053
1054 ;; NB: `make-local-hook' needed for older/alternative Emacs compatibility
1055 ;; make-local-hook 'post-command-hook)
1056 (add-hook 'post-command-hook 'idlwave-command-hook nil t)
1057
1058 ;; Read the command history?
1059 (when (and idlwave-shell-save-command-history
1060 (stringp idlwave-shell-command-history-file))
1061 (set (make-local-variable 'comint-input-ring-file-name)
1062 idlwave-shell-command-history-file)
1063 (if (file-regular-p idlwave-shell-command-history-file)
1064 (comint-read-input-ring)))
1065
1066 ;; Turn off the non-debug toolbar buttons (open,save,etc.)
1067 (set (make-local-variable 'tool-bar-map) nil)
1068
1069 (idlwave-shell-send-command idlwave-shell-initial-commands nil 'hide)
1070 ;; Turn off IDL's ^d interpreting, and define a system
1071 ;; variable which knows the version of IDLWAVE
1072 (idlwave-shell-send-command
1073 (format "defsysv,'!idlwave_version','%s',1" idlwave-mode-version)
1074 nil 'hide)
1075 ;; Read the paths, and save if they changed
1076 (idlwave-shell-send-command idlwave-shell-path-query
1077 'idlwave-shell-get-path-info
1078 'hide))
1079
1080 (defvar idlwave-system-directory)
1081 (defun idlwave-shell-get-path-info (&optional no-write)
1082 "Get the path lists, writing to file unless NO-WRITE is set."
1083 (let* ((rpl (idlwave-shell-path-filter))
1084 (sysdir (car rpl))
1085 (dirs (cdr rpl))
1086 (old-path-alist idlwave-path-alist)
1087 (old-sys-dir idlwave-system-directory)
1088 path-changed sysdir-changed)
1089 (when sysdir
1090 (setq idlwave-system-directory sysdir)
1091 (if (setq sysdir-changed
1092 (not (string= idlwave-system-directory old-sys-dir)))
1093 (put 'idlwave-system-directory 'from-shell t)))
1094 ;; Preserve any existing flags
1095 (setq idlwave-path-alist
1096 (mapcar (lambda (x)
1097 (let ((old-entry (assoc x old-path-alist)))
1098 (if old-entry
1099 (cons x (cdr old-entry))
1100 (list x))))
1101 dirs))
1102 (if (setq path-changed (not (equal idlwave-path-alist old-path-alist)))
1103 (put 'idlwave-path-alist 'from-shell t))
1104 (if idlwave-path-alist
1105 (if (and (not no-write)
1106 idlwave-auto-write-paths
1107 (or sysdir-changed path-changed)
1108 (not idlwave-library-path))
1109 (idlwave-write-paths))
1110 ;; Fall back
1111 (setq idlwave-path-alist old-path-alist))))
1112
1113 (if (not (fboundp 'idl-shell))
1114 (fset 'idl-shell 'idlwave-shell))
1115
1116 (defvar idlwave-shell-idl-wframe nil
1117 "Frame for displaying the IDL shell window.")
1118 (defvar idlwave-shell-display-wframe nil
1119 "Frame for displaying the IDL source files.")
1120
1121 (defvar idlwave-shell-calling-stack-index 0)
1122 (defvar idlwave-shell-calling-stack-routine nil)
1123
1124 (defun idlwave-shell-source-frame ()
1125 "Return the frame to be used for source display."
1126 (if idlwave-shell-use-dedicated-frame
1127 ;; We want separate frames for source and shell
1128 (if (frame-live-p idlwave-shell-display-wframe)
1129 ;; The frame exists, so we use it.
1130 idlwave-shell-display-wframe
1131 ;; The frame does not exist. We use the current frame.
1132 ;; However, if the current is the shell frame, we make a new frame,
1133 ;; or recycle the first existing visible frame
1134 (setq idlwave-shell-display-wframe
1135 (if (eq (selected-frame) idlwave-shell-idl-wframe)
1136 (or
1137 (let ((flist (visible-frame-list))
1138 (frame (selected-frame)))
1139 (catch 'exit
1140 (while flist
1141 (if (not (eq (car flist)
1142 idlwave-shell-idl-wframe))
1143 (throw 'exit (car flist))
1144 (setq flist (cdr flist))))))
1145 (make-frame))
1146 (selected-frame))))))
1147
1148 (defun idlwave-shell-shell-frame ()
1149 "Return the frame to be used for the shell buffer."
1150 (if idlwave-shell-use-dedicated-frame
1151 ;; We want a dedicated frame
1152 (if (frame-live-p idlwave-shell-idl-wframe)
1153 ;; It does exist, so we use it.
1154 idlwave-shell-idl-wframe
1155 ;; It does not exist. Check if we have a source frame.
1156 (if (not (frame-live-p idlwave-shell-display-wframe))
1157 ;; We do not have a source frame, so we use this one.
1158 (setq idlwave-shell-display-wframe (selected-frame)))
1159 ;; Return a new frame
1160 (setq idlwave-shell-idl-wframe
1161 (make-frame idlwave-shell-frame-parameters)))))
1162
1163 ;;;###autoload
1164 (defun idlwave-shell (&optional arg quick)
1165 "Run an inferior IDL, with I/O through buffer `(idlwave-shell-buffer)'.
1166 If buffer exists but shell process is not running, start new IDL.
1167 If buffer exists and shell process is running, just switch to the buffer.
1168
1169 When called with a prefix ARG, or when `idlwave-shell-use-dedicated-frame'
1170 is non-nil, the shell buffer and the source buffers will be in
1171 separate frames.
1172
1173 The command to run comes from variable `idlwave-shell-explicit-file-name',
1174 with options taken from `idlwave-shell-command-line-options'.
1175
1176 The buffer is put in `idlwave-shell-mode', providing commands for sending
1177 input and controlling the IDL job. See help on `idlwave-shell-mode'.
1178 See also the variable `idlwave-shell-prompt-pattern'.
1179
1180 \(Type \\[describe-mode] in the shell buffer for a list of commands.)"
1181 (interactive "P")
1182 (if (eq arg 'quick)
1183 (progn
1184 (let ((idlwave-shell-use-dedicated-frame nil))
1185 (idlwave-shell nil)
1186 (delete-other-windows))
1187 (and idlwave-shell-use-dedicated-frame
1188 (setq idlwave-shell-idl-wframe (selected-frame)))
1189 (add-hook 'idlwave-shell-sentinel-hook
1190 'save-buffers-kill-emacs t))
1191
1192 ;; A non-nil arg means, we want a dedicated frame. This will last
1193 ;; for the current editing session.
1194 (if arg (setq idlwave-shell-use-dedicated-frame t))
1195 (if (equal arg '(16)) (setq idlwave-shell-use-dedicated-frame nil))
1196
1197 ;; Check if the process still exists. If not, create it.
1198 (unless (comint-check-proc (idlwave-shell-buffer))
1199 (let* ((prg (or idlwave-shell-explicit-file-name "idl"))
1200 (buf (apply 'make-comint
1201 idlwave-shell-process-name prg nil
1202 (if (stringp idlwave-shell-command-line-options)
1203 (idlwave-split-string
1204 idlwave-shell-command-line-options)
1205 idlwave-shell-command-line-options)))
1206 (process (get-buffer-process buf)))
1207 (setq idlwave-idlwave_routine_info-compiled nil)
1208 (set-process-filter process 'idlwave-shell-filter)
1209 (set-process-sentinel process 'idlwave-shell-sentinel)
1210 (set-buffer buf)
1211 (idlwave-shell-mode)))
1212 (let ((window (idlwave-display-buffer (idlwave-shell-buffer) nil
1213 (idlwave-shell-shell-frame)))
1214 (current-window (selected-window)))
1215 (select-window window)
1216 (goto-char (point-max))
1217 (if idlwave-shell-use-dedicated-window
1218 (set-window-dedicated-p window t))
1219 (select-window current-window)
1220 (if idlwave-shell-ready
1221 (raise-frame (window-frame window)))
1222 (if (eq (selected-frame) (window-frame window))
1223 (select-window window))))
1224 ;; Save the paths at the end, if they are from the Shell and new.
1225 (add-hook 'idlwave-shell-sentinel-hook
1226 (lambda ()
1227 (if (and
1228 idlwave-auto-write-paths
1229 idlwave-path-alist
1230 (not idlwave-library-path)
1231 (get 'idlwave-path-alist 'from-shell))
1232 (idlwave-write-paths)))))
1233
1234 (defun idlwave-shell-recenter-shell-window (&optional arg)
1235 "Run `idlwave-shell', but make sure the current window stays selected."
1236 (interactive "P")
1237 (let ((window (selected-window)))
1238 (idlwave-shell arg)
1239 (select-window window)))
1240
1241 (defun idlwave-shell-hide-p (type &optional list)
1242 "Whether to hide this type of command.
1243 Return either nil or 'hide."
1244 (let ((list (or list idlwave-shell-show-commands)))
1245 (if (listp list)
1246 (if (not (memq type list)) 'hide))))
1247
1248 (defun idlwave-shell-add-or-remove-show (type)
1249 "Add or remove a show command from the list."
1250 (if (listp idlwave-shell-show-commands)
1251 (setq idlwave-shell-show-commands
1252 (if (memq type idlwave-shell-show-commands)
1253 (delq type idlwave-shell-show-commands)
1254 (add-to-list'idlwave-shell-show-commands type)))
1255 (setq idlwave-shell-show-commands (list type))))
1256
1257
1258 (defun idlwave-shell-send-command (&optional cmd pcmd hide preempt
1259 show-if-error)
1260 "Send a command to IDL process.
1261
1262 \(CMD PCMD HIDE\) are placed at the end of `idlwave-shell-pending-commands'.
1263 If IDL is ready the first command in `idlwave-shell-pending-commands',
1264 CMD, is sent to the IDL process.
1265
1266 If optional second argument PCMD is non-nil it will be placed on
1267 `idlwave-shell-post-command-hook' when CMD is executed.
1268
1269 If the optional third argument HIDE is non-nil, then hide output from
1270 CMD, unless it is the symbol 'mostly, in which case only output
1271 beginning with \"%\" is hidden, and all other output (i.e., the
1272 results of a PRINT command), is shown. This helps with, e.g.,
1273 stepping through code with output.
1274
1275 If optional fourth argument PREEMPT is non-nil CMD is put at front of
1276 `idlwave-shell-pending-commands'. If PREEMPT is 'wait, wait for all
1277 output to complete and the next prompt to arrive before returning
1278 \(useful if you need an answer now\). IDL is considered ready if the
1279 prompt is present and if `idlwave-shell-ready' is non-nil.
1280
1281 If SHOW-IF-ERROR is non-nil, show the output if it contains an error
1282 message, independent of what HIDE is set to."
1283
1284 ; (setq hide nil) ; FIXME: turn this on for debugging only
1285 ; (if (null cmd)
1286 ; (progn
1287 ; (message "SENDING Pending commands: %s"
1288 ; (prin1-to-string idlwave-shell-pending-commands)))
1289 ; (message "SENDING %s|||%s" cmd pcmd))
1290 (if (and (symbolp idlwave-shell-show-commands)
1291 (eq idlwave-shell-show-commands 'everything))
1292 (setq hide nil))
1293 (let ((save-buffer (current-buffer))
1294 buf proc)
1295 ;; Get or make the buffer and its process
1296 (if (or (not (setq buf (get-buffer (idlwave-shell-buffer))))
1297 (not (setq proc (get-buffer-process buf))))
1298 (if (not idlwave-shell-automatic-start)
1299 (error "%s"
1300 (substitute-command-keys
1301 "You need to first start an IDL shell with \\[idlwave-shell]"))
1302 (idlwave-shell-recenter-shell-window)
1303 (setq buf (get-buffer (idlwave-shell-buffer)))
1304 (if (or (not (setq buf (get-buffer (idlwave-shell-buffer))))
1305 (not (setq proc (get-buffer-process buf))))
1306 ;; Still nothing
1307 (error "Problem with autostarting IDL shell"))))
1308 (when (or cmd idlwave-shell-pending-commands)
1309 (set-buffer buf)
1310 ;; To make this easy, always push CMD onto pending commands
1311 (if cmd
1312 (setq idlwave-shell-pending-commands
1313 (if preempt
1314 ;; Put at front.
1315 (append (list (list cmd pcmd hide show-if-error))
1316 idlwave-shell-pending-commands)
1317 ;; Put at end.
1318 (append idlwave-shell-pending-commands
1319 (list (list cmd pcmd hide show-if-error))))))
1320 ;; Check if IDL ready
1321 (let ((save-point (point-marker)))
1322 (goto-char (process-mark proc))
1323 (if (and idlwave-shell-ready
1324 ;; Check for IDL prompt
1325 (prog2
1326 (forward-line 0)
1327 ;; (beginning-of-line) ; Changed for Emacs 21
1328 (looking-at idlwave-shell-prompt-pattern)
1329 (goto-char (process-mark proc))))
1330 ;; IDL ready for command, execute it
1331 (let* ((lcmd (car idlwave-shell-pending-commands))
1332 (cmd (car lcmd))
1333 (pcmd (nth 1 lcmd))
1334 (hide (nth 2 lcmd))
1335 (show-if-error (nth 3 lcmd)))
1336 ;; If this is an executive command, reset the stack pointer
1337 (if (eq (string-to-char cmd) ?.)
1338 (setq idlwave-shell-calling-stack-index 0))
1339 ;; Set post-command
1340 (setq idlwave-shell-post-command-hook pcmd)
1341 ;; Output hiding
1342 (setq idlwave-shell-hide-output hide)
1343 ;;Showing errors
1344 (setq idlwave-shell-show-if-error show-if-error)
1345 ;; Pop command
1346 (setq idlwave-shell-pending-commands
1347 (cdr idlwave-shell-pending-commands))
1348 ;; Send command for execution
1349 (set-marker comint-last-input-start (point))
1350 (set-marker comint-last-input-end (point))
1351 (comint-simple-send proc cmd)
1352 (setq idlwave-shell-ready nil)
1353 (if (equal preempt 'wait) ; Get all the output at once
1354 (while (not idlwave-shell-ready)
1355 (when (not (accept-process-output proc 6)) ; long wait
1356 (setq idlwave-shell-pending-commands nil)
1357 (error "Process timed out"))))))
1358 (goto-char save-point))
1359 (set-buffer save-buffer))))
1360
1361 (defun idlwave-shell-send-char (c &optional error)
1362 "Send one character to the shell, without a newline."
1363 (interactive "cChar to send to IDL: \np")
1364 (let ((errf (if error 'error 'message))
1365 buf proc)
1366 (if (or (not (setq buf (get-buffer (idlwave-shell-buffer))))
1367 (not (setq proc (get-buffer-process buf))))
1368 (funcall errf "Shell is not running"))
1369 (if (equal c ?\C-g)
1370 (funcall errf "Abort")
1371 (comint-send-string proc (char-to-string c)))))
1372
1373 (defvar idlwave-shell-char-mode-active)
1374 (defun idlwave-shell-input-mode-magic (string)
1375 "Check STRING for magic words and toggle character input mode.
1376 See also the variable `idlwave-shell-input-mode-spells'."
1377 (cond
1378 ((string-match (car idlwave-shell-input-mode-spells) string)
1379 (call-interactively 'idlwave-shell-send-char))
1380 ((and (boundp 'idlwave-shell-char-mode-active)
1381 (string-match (nth 2 idlwave-shell-input-mode-spells) string))
1382 (setq idlwave-shell-char-mode-active 'exit))
1383 ((string-match (nth 1 idlwave-shell-input-mode-spells) string)
1384 ;; Set a timer which will soon start the character loop
1385 (if (fboundp 'start-itimer)
1386 (start-itimer "IDLWAVE Char Mode" 'idlwave-shell-char-mode-loop 0.5
1387 nil nil t 'no-error)
1388 (run-at-time 0.5 nil 'idlwave-shell-char-mode-loop 'no-error)))))
1389
1390 (defvar keyboard-quit)
1391 (defun idlwave-shell-char-mode-loop (&optional no-error)
1392 "Enter a loop which accepts single characters and sends them to IDL.
1393 Characters are sent one by one, without newlines. The loop is blocking
1394 and intercepts all input events to Emacs. You can use this command
1395 to interact with the IDL command GET_KBRD.
1396 The loop can be aborted by typing `C-g'. The loop also exits automatically
1397 when the IDL prompt gets displayed again after the current IDL command."
1398 (interactive)
1399
1400 ;; First check if there is a shell waiting for input
1401 (let ((idlwave-shell-char-mode-active t)
1402 (errf (if no-error 'message 'error))
1403 buf proc c)
1404 (if (or (not (setq buf (get-buffer (idlwave-shell-buffer))))
1405 (not (setq proc (get-buffer-process buf))))
1406 (funcall errf "Shell is not running"))
1407 (if idlwave-shell-ready
1408 (funcall errf "No IDL program seems to be waiting for input"))
1409
1410 ;; OK, start the loop
1411 (message "Character mode on: Sending single chars (`C-g' to exit)")
1412 (message
1413 (catch 'exit
1414 (while t
1415 ;; Wait for input
1416 ;; FIXME: Is it too dangerous to inhibit quit here?
1417 (let ((inhibit-quit t))
1418 ;; We wait and check frequently if we should abort
1419 (while (sit-for 0.3)
1420 (and idlwave-shell-ready
1421 (throw 'exit "Character mode off (prompt displayed)"))
1422 (and (eq idlwave-shell-char-mode-active 'exit)
1423 (throw 'exit "Character mode off (closing spell incantation)")))
1424 ;; Interpret input as a character - ignore non-char input
1425 (condition-case nil
1426 (setq c (read-char))
1427 (error (ding) (throw 'exit "Character mode off")))
1428 (cond
1429 ((null c) ; Non-char event: ignore
1430 (ding))
1431 ((equal c ?\C-g) ; Abort the loop
1432 (setq keyboard-quit nil)
1433 (ding)
1434 (throw 'exit "Character mode off (keyboard quit)"))
1435 (t ; Send the character and continue the loop
1436 (comint-send-string proc (char-to-string c))))
1437 (and (eq idlwave-shell-char-mode-active 'exit)
1438 (throw 'exit "Single char loop exited"))))))))
1439
1440 (defun idlwave-shell-move-or-history (up &optional arg)
1441 "When in last line of process buffer, do `comint-previous-input'.
1442 Otherwise just move the line. Move down unless UP is non-nil."
1443 (let* ((proc-pos (marker-position
1444 (process-mark (get-buffer-process (current-buffer)))))
1445 (arg (or arg 1))
1446 (arg (if up arg (- arg))))
1447 (if (eq t idlwave-shell-arrows-do-history) (goto-char proc-pos))
1448 (if (and idlwave-shell-arrows-do-history
1449 (>= (1+ (point-at-eol)) proc-pos))
1450 (comint-previous-input arg)
1451 (forward-line (- arg)))))
1452
1453 (defun idlwave-shell-up-or-history (&optional arg)
1454 "When in last line of process buffer, move to previous input.
1455 Otherwise just go up one line."
1456 (interactive "p")
1457 (idlwave-shell-move-or-history t arg))
1458
1459 (defun idlwave-shell-down-or-history (&optional arg)
1460 "When in last line of process buffer, move to next input.
1461 Otherwise just go down one line."
1462 (interactive "p")
1463 (idlwave-shell-move-or-history nil arg))
1464
1465 ;; Newer versions of comint.el changed the name of comint-filter to
1466 ;; comint-output-filter.
1467 (defun idlwave-shell-comint-filter (process string) nil)
1468 (if (fboundp 'comint-output-filter)
1469 (fset 'idlwave-shell-comint-filter (symbol-function 'comint-output-filter))
1470 (fset 'idlwave-shell-comint-filter (symbol-function 'comint-filter)))
1471
1472 (defun idlwave-shell-is-running ()
1473 "Return t if the shell process is running."
1474 (eq (process-status idlwave-shell-process-name) 'run))
1475
1476 (defun idlwave-shell-filter-hidden-output (output)
1477 "Filter hidden output, leaving the good stuff.
1478
1479 Remove everything to the first newline, and all lines with % in front
1480 of them, with optional follow-on lines starting with two spaces. This
1481 works well enough, since any print output typically arrives before
1482 error messages, etc."
1483 (setq output (substring output (string-match "\n" output)))
1484 (while (string-match "\\(\n\\|\\`\\)%.*\\(\n .*\\)*" output)
1485 (setq output (replace-match "" nil t output)))
1486 (unless
1487 (string-match idlwave-shell-only-prompt-pattern output)
1488 output))
1489
1490 (defvar idlwave-shell-hidden-output-buffer " *idlwave-shell-hidden-output*"
1491 "Buffer containing hidden output from IDL commands.")
1492 (defvar idlwave-shell-current-state nil)
1493
1494 (defun idlwave-shell-filter (proc string)
1495 "Watch for IDL prompt and filter incoming text.
1496 When the IDL prompt is received executes `idlwave-shell-post-command-hook'
1497 and then calls `idlwave-shell-send-command' for any pending commands."
1498 ;; We no longer do the cleanup here - this is done by the process sentinel
1499 (if (eq (process-status idlwave-shell-process-name) 'run)
1500 ;; OK, process is still running, so we can use it.
1501 (let ((data (match-data)) p full-output)
1502 (unwind-protect
1503 (progn
1504 ;; Ring the bell if necessary
1505 (while (setq p (string-match "\C-G" string))
1506 (ding)
1507 (aset string p ?\C-j ))
1508 (if idlwave-shell-hide-output
1509 (save-excursion
1510 (while (setq p (string-match "\C-M" string))
1511 (aset string p ?\ ))
1512 (set-buffer
1513 (get-buffer-create idlwave-shell-hidden-output-buffer))
1514 (goto-char (point-max))
1515 (insert string))
1516 (idlwave-shell-comint-filter proc string))
1517 ;; Watch for magic - need to accumulate the current line
1518 ;; since it may not be sent all at once.
1519 (if (string-match "\n" string)
1520 (progn
1521 (if idlwave-shell-use-input-mode-magic
1522 (idlwave-shell-input-mode-magic
1523 (concat idlwave-shell-accumulation string)))
1524 (setq idlwave-shell-accumulation
1525 (substring string
1526 (progn (string-match "\\(.*[\n\r]+\\)*"
1527 string)
1528 (match-end 0)))))
1529 (setq idlwave-shell-accumulation
1530 (concat idlwave-shell-accumulation string)))
1531
1532
1533 ;; ;; Test/Debug code
1534 ;;(with-current-buffer
1535 ;; (get-buffer-create "*idlwave-shell-output*")
1536 ;; (goto-char (point-max))
1537 ;; (insert "\nReceived STRING\n===>\n" string "\n<====\n"))
1538
1539 ;; Check for prompt in current accumulating output
1540 (when (setq idlwave-shell-ready
1541 (string-match idlwave-shell-prompt-pattern
1542 idlwave-shell-accumulation))
1543 ;; Gather the command output
1544 (if idlwave-shell-hide-output
1545 (with-current-buffer idlwave-shell-hidden-output-buffer
1546 (setq full-output (buffer-string))
1547 (goto-char (point-max))
1548 (re-search-backward idlwave-shell-prompt-pattern nil t)
1549 (goto-char (match-end 0))
1550 (setq idlwave-shell-command-output
1551 (buffer-substring-no-properties
1552 (point-min) (point)))
1553 (delete-region (point-min) (point)))
1554 (setq idlwave-shell-command-output
1555 (with-current-buffer (process-buffer proc)
1556 (buffer-substring-no-properties
1557 (save-excursion
1558 (goto-char (process-mark proc))
1559 (forward-line 0) ; Emacs 21 (beginning-of-line nil)
1560 (point))
1561 comint-last-input-end))))
1562
1563 ;; Scan for state and do post commands - bracket
1564 ;; them with idlwave-shell-ready=nil since they may
1565 ;; call idlwave-shell-send-command themselves.
1566 (let ((idlwave-shell-ready nil))
1567 (idlwave-shell-scan-for-state)
1568 ;; Show the output in the shell if it contains an error
1569 (if idlwave-shell-hide-output
1570 (if (and idlwave-shell-show-if-error
1571 (eq idlwave-shell-current-state 'error))
1572 (idlwave-shell-comint-filter proc full-output)
1573 ;; If it's only *mostly* hidden, filter % lines,
1574 ;; and show anything that remains
1575 (if (eq idlwave-shell-hide-output 'mostly)
1576 (let ((filtered
1577 (idlwave-shell-filter-hidden-output
1578 full-output)))
1579 (if filtered
1580 (idlwave-shell-comint-filter
1581 proc filtered))))))
1582
1583 ;; Call the post-command hook
1584 (if (listp idlwave-shell-post-command-hook)
1585 (progn
1586 ;;(message "Calling list")
1587 ;;(prin1 idlwave-shell-post-command-hook)
1588 (eval idlwave-shell-post-command-hook))
1589 ;;(message "Calling command function")
1590 (funcall idlwave-shell-post-command-hook))
1591
1592 ;; Reset to default state for next command.
1593 ;; Also we do not want to find this prompt again.
1594 (setq idlwave-shell-accumulation nil
1595 idlwave-shell-command-output nil
1596 idlwave-shell-post-command-hook nil
1597 idlwave-shell-hide-output nil
1598 idlwave-shell-show-if-error nil))
1599 ;; Done with post command. Do pending command if
1600 ;; any.
1601 (idlwave-shell-send-command)))
1602 (store-match-data data)))))
1603
1604 (defun idlwave-shell-sentinel (process event)
1605 "The sentinel function for the IDLWAVE shell process."
1606 (let* ((buf (idlwave-shell-buffer))
1607 (win (get-buffer-window buf)))
1608 (when (get-buffer buf)
1609 (with-current-buffer (idlwave-shell-buffer)
1610 (goto-char (point-max))
1611 (insert (format "\n\n Process %s %s" process event))
1612 (if (and idlwave-shell-save-command-history
1613 (stringp idlwave-shell-command-history-file))
1614 (condition-case nil
1615 (comint-write-input-ring)
1616 (error nil)))))
1617
1618 (when (and (> (length (frame-list)) 1)
1619 (frame-live-p idlwave-shell-idl-wframe))
1620 (delete-frame idlwave-shell-idl-wframe)
1621 (setq idlwave-shell-idl-wframe nil
1622 idlwave-shell-display-wframe nil))
1623 (when (and (window-live-p win)
1624 (not (one-window-p 'nomini)))
1625 (delete-window win))
1626 (idlwave-shell-cleanup)
1627 ;; Run the hook, if possible in the shell buffer.
1628 (if (get-buffer buf)
1629 (with-current-buffer buf
1630 (run-hooks 'idlwave-shell-sentinel-hook))
1631 (run-hooks 'idlwave-shell-sentinel-hook))))
1632
1633 (defvar idlwave-shell-error-buffer " *idlwave-shell-errors*"
1634 "Buffer containing syntax errors from IDL compilations.")
1635
1636 ;; FIXME: the following two variables do not currently allow line breaks
1637 ;; in module and file names. I am not sure if it will be necessary to
1638 ;; change this. Currently it seems to work the way it is.
1639 (defvar idlwave-shell-syntax-error
1640 "^% Syntax error.\\s-*\n\\s-*At:\\s-*\\(.*\\),\\s-*Line\\s-*\\(.*\\)"
1641 "A regular expression to match an IDL syntax error.
1642 The first pair matches the file name, the second pair matches the line
1643 number.")
1644
1645 (defvar idlwave-shell-other-error
1646 "^% .*\n\\s-*At:\\s-*\\(.*\\),\\s-*Line\\s-*\\(.*\\)"
1647 "A regular expression to match any IDL error.")
1648
1649 (defvar idlwave-shell-halting-error
1650 "^% .*\n\\([^%].*\n\\)*% Execution halted at:\\(\\s-*\\S-+\\s-*[0-9]+\\s-*.*\\)\n"
1651 "A regular expression to match errors which halt execution.")
1652
1653 (defvar idlwave-shell-cant-continue-error
1654 "^% Can't continue from this point.\n"
1655 "A regular expression to match errors stepping errors.")
1656
1657 (defvar idlwave-shell-file-line-message
1658 (concat
1659 "\\(" ; program name group (1)
1660 "\\$MAIN\\$\\|" ; main level routine
1661 "\\<[a-zA-Z][a-zA-Z0-9_$:]*" ; start with a letter followed by [..]
1662 "\\([ \t]*\n[ \t]*[a-zA-Z0-9_$:]+\\)*"; continuation lines program name (2)
1663 "\\)" ; end program name group (1)
1664 "[ \t\n]+" ; white space
1665 "\\(" ; line number group (3)
1666 "[0-9]+" ; the line number (the fix point)
1667 "\\([ \t]*\n[ \t]*[0-9]+\\)*" ; continuation lines number (4)
1668 "\\)" ; end line number group (3)
1669 "[ \t\n]+" ; white space
1670 "\\(" ; file name group (5)
1671 "[^ \t\n]+" ; file names can contain any non-white
1672 "\\([ \t]*\n[ \t]*[^ \t\n]+\\)*" ; continuation lines file name (6)
1673 "\\)" ; end line number group (5)
1674 )
1675 "*A regular expression to parse out the file name and line number.
1676 The 1st group should match the subroutine name.
1677 The 3rd group is the line number.
1678 The 5th group is the file name.
1679 All parts may contain linebreaks surrounded by spaces. This is important
1680 in IDL5 which inserts random linebreaks in long module and file names.")
1681
1682 (defvar idlwave-shell-electric-debug-mode) ; defined by easy-mmode
1683
1684 (defun idlwave-shell-scan-for-state ()
1685 "Scan for state info.
1686 Looks for messages in output from last IDL command indicating where
1687 IDL has stopped. The types of messages we are interested in are
1688 execution halted, stepped, breakpoint, interrupted at and trace
1689 messages. For breakpoint messages process any attached count or
1690 command parameters. Update the stop line if a message is found.
1691 The variable `idlwave-shell-current-state' is set to 'error, 'halt,
1692 or 'breakpoint, which describes the status, or nil for none of
1693 the above."
1694 (let (trace)
1695 (cond
1696 ;; Make sure we have output
1697 ((not idlwave-shell-command-output))
1698
1699 ;; First Priority: Syntax and other errors
1700 ((or
1701 (string-match idlwave-shell-syntax-error
1702 idlwave-shell-command-output)
1703 (string-match idlwave-shell-other-error
1704 idlwave-shell-command-output))
1705 (with-current-buffer
1706 (get-buffer-create idlwave-shell-error-buffer)
1707 (erase-buffer)
1708 (insert idlwave-shell-command-output)
1709 (goto-char (point-min))
1710 (setq idlwave-shell-error-last (point)))
1711 (setq idlwave-shell-current-state 'error)
1712 (idlwave-shell-goto-next-error))
1713
1714 ;; Second Priority: Halting errors
1715 ((string-match idlwave-shell-halting-error
1716 idlwave-shell-command-output)
1717 ;; Grab the file and line state info.
1718 (setq idlwave-shell-calling-stack-index 0)
1719 (setq idlwave-shell-halt-frame
1720 (idlwave-shell-parse-line
1721 (substring idlwave-shell-command-output
1722 (match-beginning 2)))
1723 idlwave-shell-current-state 'error)
1724 (idlwave-shell-display-line (idlwave-shell-pc-frame)))
1725
1726 ;; Third Priority: Various types of innocuous HALT and
1727 ;; TRACEBACK messages.
1728 ((or (setq trace (string-match idlwave-shell-trace-message-re
1729 idlwave-shell-command-output))
1730 (string-match idlwave-shell-halt-messages-re
1731 idlwave-shell-command-output))
1732 ;; Grab the file and line state info.
1733 (setq idlwave-shell-calling-stack-index 0)
1734 (setq idlwave-shell-halt-frame
1735 (idlwave-shell-parse-line
1736 (substring idlwave-shell-command-output (match-end 0))))
1737 (setq idlwave-shell-current-state 'halt)
1738 ;; Don't debug trace messages
1739 (idlwave-shell-display-line
1740 (idlwave-shell-pc-frame) nil
1741 (if trace 'disable
1742 (if idlwave-shell-electric-debug-mode 'force))))
1743
1744 ;; Fourth Priority: Breakpoints
1745 ((string-match idlwave-shell-break-message
1746 idlwave-shell-command-output)
1747 (setq idlwave-shell-calling-stack-index 0)
1748 (setq idlwave-shell-halt-frame
1749 (idlwave-shell-parse-line
1750 (substring idlwave-shell-command-output (match-end 0))))
1751 ;; We used to count hits on breakpoints
1752 ;; this is no longer supported since IDL breakpoints
1753 ;; have learned counting.
1754 ;; Do breakpoint command processing
1755 (let ((bp (assoc
1756 (list
1757 (nth 0 idlwave-shell-halt-frame)
1758 (nth 1 idlwave-shell-halt-frame))
1759 idlwave-shell-bp-alist)))
1760 ;(message "Scanning with %s" bp)
1761 (if bp
1762 (let ((cmd (idlwave-shell-bp-get bp 'cmd)))
1763 (if cmd ;; Execute any breakpoint command
1764 (if (listp cmd) (eval cmd) (funcall cmd))))
1765 ;; A breakpoint that we did not know about - perhaps it was
1766 ;; set by the user... Let's update our list.
1767 (idlwave-shell-bp-query)))
1768 (setq idlwave-shell-current-state 'breakpoint)
1769 (idlwave-shell-display-line (idlwave-shell-pc-frame)))
1770
1771 ;; Last Priority: Can't Step errors
1772 ((string-match idlwave-shell-cant-continue-error
1773 idlwave-shell-command-output)
1774 (setq idlwave-shell-current-state 'breakpoint))
1775
1776 ;; Otherwise, no particular state
1777 (t (setq idlwave-shell-current-state nil)))))
1778
1779
1780 (defun idlwave-shell-parse-line (string &optional skip-main)
1781 "Parse IDL message for the subroutine, file name and line number."
1782 ;We need to work hard here to remove the stupid line breaks inserted by
1783 ;IDL5. These line breaks can be right in the middle of procedure
1784 ;or file names.
1785 ;It is very difficult to come up with a robust solution. This one seems
1786 ;to be pretty good though.
1787 ;
1788 ;Here is in what ways it improves over the previous solution:
1789 ;
1790 ;1. The procedure name can be split and will be restored.
1791 ;2. The number can be split. I have never seen this, but who knows.
1792 ;3. We do not require the `.pro' extension for files.
1793 ;
1794 ;This function can still break when the file name ends on an end line
1795 ;and the message line contains an additional line with garbage. Then
1796 ;the first part of that garbage will be added to the file name.
1797 ;However, the function checks the existence of the files with and
1798 ;without this last part - thus the function only breaks if file name
1799 ;plus garbage match an existing regular file. This is hopefully very
1800 ;unlikely.
1801 ;
1802 ;If optional arg SKIP-MAIN is non-nil, don't parse $MAIN$ routine stop
1803 ;statements.
1804
1805 (let (number procedure file)
1806 (when (and (not (if skip-main (string-match ":\\s-*\\$MAIN" string)))
1807 (string-match idlwave-shell-file-line-message string))
1808 (setq procedure (match-string 1 string)
1809 number (match-string 3 string)
1810 file (match-string 5 string))
1811
1812 ;; Repair the strings
1813 (setq procedure (idlwave-shell-repair-string procedure))
1814 (setq number (idlwave-shell-repair-string number))
1815 (setq file (idlwave-shell-repair-file-name file))
1816
1817 ;; If we have a file, return the frame list
1818 (if file
1819 (list (idlwave-shell-file-name file)
1820 (string-to-number number)
1821 procedure)
1822 ;; No success finding a file
1823 nil))))
1824
1825 (defun idlwave-shell-repair-string (string)
1826 "Repair a string by taking out all linebreaks. This is destructive!"
1827 (while (string-match "[ \t]*\n[ \t]*" string)
1828 (setq string (replace-match "" t t string)))
1829 string)
1830
1831 (defun idlwave-shell-repair-file-name (file)
1832 "Repair a file name string by taking out all linebreaks.
1833 The last line of STRING may be garbage - we check which one makes a valid
1834 file name."
1835 (let ((file1 "") (file2 "") (start 0))
1836 ;; We scan no further than to the next "^%" line
1837 (if (string-match "^%" file)
1838 (setq file (substring file 0 (match-beginning 0))))
1839 ;; Take out the line breaks
1840 (while (string-match "[ \t]*\n[ \t]*" file start)
1841 (setq file1 (concat file1 (substring file start (match-beginning 0)))
1842 start (match-end 0)))
1843 (setq file2 (concat file1 (substring file start)))
1844 (cond
1845 ((file-regular-p file2) file2)
1846 ((file-regular-p file1) file1)
1847 ;; If we cannot verify the existence of the file, we return the shorter
1848 ;; name. The idea behind this is that this may be a relative file name
1849 ;; and our idea about the current working directory may be wrong.
1850 ;; If it is a relative file name, it hopefully is short.
1851 ((not (string= "" file1)) file1)
1852 ((not (string= "" file2)) file2)
1853 (t nil))))
1854
1855 (defun idlwave-shell-cleanup ()
1856 "Do necessary cleanup for a terminated IDL process."
1857 (setq idlwave-shell-step-frame nil
1858 idlwave-shell-halt-frame nil
1859 idlwave-shell-pending-commands nil
1860 idlwave-shell-command-line-to-execute nil
1861 idlwave-shell-bp-alist nil
1862 idlwave-shell-calling-stack-index 0
1863 idlwave-idlwave_routine_info-compiled nil)
1864 (idlwave-shell-delete-temp-files)
1865 (idlwave-shell-display-line nil)
1866 (idlwave-shell-update-bp-overlays) ; kill old overlays
1867 (idlwave-shell-kill-buffer idlwave-shell-hidden-output-buffer)
1868 (idlwave-shell-kill-buffer idlwave-shell-bp-buffer)
1869 (idlwave-shell-kill-buffer idlwave-shell-error-buffer)
1870 ;; (idlwave-shell-kill-buffer (idlwave-shell-buffer))
1871 (and (get-buffer (idlwave-shell-buffer))
1872 (bury-buffer (get-buffer (idlwave-shell-buffer))))
1873 (run-hooks 'idlwave-shell-cleanup-hook))
1874
1875 (defun idlwave-shell-kill-buffer (buf)
1876 "Kill buffer BUF if it exists."
1877 (if (setq buf (get-buffer buf))
1878 (kill-buffer buf)))
1879
1880 (defun idlwave-shell-kill-shell-buffer-confirm ()
1881 (when (idlwave-shell-is-running)
1882 (ding)
1883 (unless (y-or-n-p "IDL shell is running. Are you sure you want to kill the buffer? ")
1884 (error "Abort"))
1885 (message "Killing buffer *idl* and the associated process")))
1886
1887 (defun idlwave-shell-window (n)
1888 "Issue a `window,N' command to IDL, with special window size.
1889 The size is given by `idlwave-shell-graphics-window-size'."
1890 (interactive "P")
1891 (let ((n (if n (prefix-numeric-value n) 0)))
1892 (idlwave-shell-send-command
1893 (apply 'format "window,%d,xs=%d,ys=%d"
1894 n idlwave-shell-graphics-window-size)
1895 nil (idlwave-shell-hide-p 'misc) nil t)))
1896
1897 (defun idlwave-shell-resync-dirs ()
1898 "Resync the buffer's idea of the current directory.
1899 This command queries IDL with the command bound to
1900 `idlwave-shell-dirstack-query', reads the output for the new
1901 directory."
1902 (interactive)
1903 (idlwave-shell-send-command idlwave-shell-dirstack-query
1904 'idlwave-shell-filter-directory
1905 'hide 'wait))
1906
1907 (defun idlwave-shell-retall (&optional arg)
1908 "Return from the entire calling stack.
1909 Also get rid of widget events in the queue."
1910 (interactive "P")
1911 (save-selected-window
1912 ;;if (widget_info(/MANAGED))[0] gt 0 then for i=0,n_elements(widget_info(/MANAGED))-1 do widget_control,(widget_info(/MANAGED))[i],/clear_events &
1913 (idlwave-shell-send-command "retall" nil
1914 (if (idlwave-shell-hide-p 'misc) 'mostly)
1915 nil t)
1916 (idlwave-shell-display-line nil)))
1917
1918 (defun idlwave-shell-closeall (&optional arg)
1919 "Close all open files."
1920 (interactive "P")
1921 (idlwave-shell-send-command "close,/all" nil
1922 (idlwave-shell-hide-p 'misc) nil t))
1923
1924 (defun idlwave-shell-quit (&optional arg)
1925 "Exit the IDL process after confirmation.
1926 With prefix ARG, exit without confirmation."
1927 (interactive "P")
1928 (if (not (idlwave-shell-is-running))
1929 (error "Shell is not running")
1930 (if (or arg (y-or-n-p "Exit the IDLWAVE Shell? "))
1931 (condition-case nil
1932 (idlwave-shell-send-command "exit")
1933 (error nil)))))
1934
1935 (defun idlwave-shell-reset (&optional hidden)
1936 "Reset IDL. Return to main level and destroy the leftover variables.
1937 This issues the following commands:
1938 RETALL
1939 WIDGET_CONTROL,/RESET
1940 CLOSE, /ALL
1941 HEAP_GC, /VERBOSE"
1942 ;; OBJ_DESTROY, OBJ_VALID() FIXME: should this be added?
1943 (interactive "P")
1944 (when (or idlwave-shell-reset-no-prompt
1945 (yes-or-no-p "Really Reset IDL and discard current session? "))
1946 (message "Resetting IDL")
1947 (setq idlwave-shell-calling-stack-index 0)
1948 ;; Give widget exit handlers a chance
1949 (idlwave-shell-send-command "retall" nil hidden)
1950 (idlwave-shell-send-command "widget_control,/reset" nil hidden)
1951 (idlwave-shell-send-command "close,/all" nil hidden)
1952 ;; (idlwave-shell-send-command "obj_destroy, obj_valid()" nil hidden)
1953 (idlwave-shell-send-command "heap_gc,/verbose" nil hidden)
1954 (idlwave-shell-display-line nil)))
1955
1956 (defun idlwave-shell-path-filter ()
1957 ;; Convert the output of the path query into a list of directories
1958 (let ((path-string idlwave-shell-command-output)
1959 (case-fold-search t)
1960 (start 0)
1961 dirs sysdir)
1962 (while (string-match "^PATH:[ \t]*<\\(.*\\)>[ \t]*\n" path-string start)
1963 (push (match-string 1 path-string) dirs)
1964 (setq start (match-end 0)))
1965 (setq dirs (mapcar 'file-name-as-directory dirs))
1966 (if (string-match "^SYSDIR:[ \t]*<\\(.*\\)>[ \t]*\n" path-string)
1967 (setq sysdir (file-name-as-directory
1968 (match-string 1 path-string))))
1969 (cons sysdir (nreverse dirs))))
1970
1971 (defun idlwave-shell-routine-info-filter ()
1972 "Function which parses the special output from idlwave_routine_info.pro."
1973 (let ((text idlwave-shell-command-output)
1974 (start 0)
1975 sep sep-re file type spec specs name cs key keys class entry)
1976 ;; (message "GOT: %s" text) ;??????????????????????
1977 ;; Initialize variables
1978 (setq idlwave-compiled-routines nil
1979 idlwave-unresolved-routines nil)
1980 ;; Cut out the correct part of the output.
1981 (if (string-match
1982 "^>>>BEGIN OF IDLWAVE ROUTINE INFO (\"\\(.+\\)\" IS THE SEPARATOR.*"
1983 text)
1984 (setq sep (match-string 1 text)
1985 sep-re (concat (regexp-quote sep) " *")
1986 text (substring text (match-end 0)))
1987 ;; Set dummy values and kill the text
1988 (setq sep "@" sep-re "@ *" text "")
1989 (if idlwave-idlwave_routine_info-compiled
1990 (message
1991 "Routine Info warning: No match for BEGIN line in \n>>>\n%s\n<<<\n"
1992 idlwave-shell-command-output)))
1993 (if (string-match "^>>>END OF IDLWAVE ROUTINE INFO.*" text)
1994 (setq text (substring text 0 (match-beginning 0)))
1995 (if idlwave-idlwave_routine_info-compiled
1996 (message
1997 "Routine Info warning: No match for END line in \n>>>\n%s\n<<<\n"
1998 idlwave-shell-command-output)))
1999 ;; Match the output lines
2000 (while (string-match "^IDLWAVE-\\(PRO\\|FUN\\): \\(.*\\)" text start)
2001 (setq start (match-end 0))
2002 (setq type (match-string 1 text)
2003 spec (match-string 2 text)
2004 specs (idlwave-split-string spec sep-re)
2005 name (nth 0 specs)
2006 class (if (equal (nth 1 specs) "") nil (nth 1 specs))
2007 file (nth 2 specs)
2008 cs (nth 3 specs)
2009 key (nth 4 specs)
2010 keys (if (and (stringp key)
2011 (not (string-match "\\` *\\'" key)))
2012 (mapcar 'list
2013 (delete "" (idlwave-split-string key " +")))))
2014 (setq name (idlwave-sintern-routine-or-method name class t)
2015 class (idlwave-sintern-class class t)
2016 file (if (equal file "") nil file)
2017 keys (mapcar (lambda (x)
2018 (list (idlwave-sintern-keyword (car x) t))) keys))
2019
2020 ;; In the following ignore routines already defined in buffers,
2021 ;; assuming that if the buffer stuff differs, it is a "new"
2022 ;; version, not yet compiled, and should take precedence.
2023 ;; We could do the same for the library to avoid duplicates -
2024 ;; but I think frequently a user might have several versions of
2025 ;; the same function in different programs, and in this case the
2026 ;; compiled one will be the best guess of all versions.
2027 ;; Therefore, we leave duplicates of library routines in.
2028 (cond ((string= name "$MAIN$")) ; ignore this one
2029 ((and (string= type "PRO")
2030 ;; FIXME: is it OK to make the buffer routines dominate?
2031 (or t (null file)
2032 (not (idlwave-rinfo-assq name 'pro class
2033 idlwave-buffer-routines)))
2034 ;; FIXME: is it OK to make the library routines dominate?
2035 ;;(not (idlwave-rinfo-assq name 'pro class
2036 ;; idlwave-library-routines))
2037 )
2038 (setq entry (list name 'pro class
2039 (cons 'compiled
2040 (if file
2041 (list
2042 (file-name-nondirectory file)
2043 (idlwave-sintern-dir
2044 (file-name-directory file)))))
2045 cs (cons nil keys)))
2046 (if file
2047 (push entry idlwave-compiled-routines)
2048 (push entry idlwave-unresolved-routines)))
2049
2050 ((and (string= type "FUN")
2051 ;; FIXME: is it OK to make the buffer routines dominate?
2052 (or t (not file)
2053 (not (idlwave-rinfo-assq name 'fun class
2054 idlwave-buffer-routines)))
2055 ;; FIXME: is it OK to make the library routines dominate?
2056 ;; (not (idlwave-rinfo-assq name 'fun class
2057 ;; idlwave-library-routines))
2058 )
2059 (setq entry (list name 'fun class
2060 (cons 'compiled
2061 (if file
2062 (list
2063 (file-name-nondirectory file)
2064 (idlwave-sintern-dir
2065 (file-name-directory file)))))
2066 cs (cons nil keys)))
2067 (if file
2068 (push entry idlwave-compiled-routines)
2069 (push entry idlwave-unresolved-routines))))))
2070 ;; Reverse the definitions so that they are alphabetically sorted.
2071 (setq idlwave-compiled-routines (nreverse idlwave-compiled-routines)
2072 idlwave-unresolved-routines (nreverse idlwave-unresolved-routines)))
2073
2074 (defun idlwave-shell-filter-directory ()
2075 "Get the current directory from `idlwave-shell-command-output'.
2076 Change the default directory for the process buffer to concur."
2077 (with-current-buffer (idlwave-shell-buffer)
2078 (if (string-match ",___cur[\n\r ]+\\([^\n\r]+\\)[\n\r]"
2079 idlwave-shell-command-output)
2080 (let ((dir (substring idlwave-shell-command-output
2081 (match-beginning 1) (match-end 1))))
2082 ;; (message "Setting Emacs working dir to %s" dir)
2083 (setq idlwave-shell-default-directory dir)
2084 (setq default-directory (file-name-as-directory dir))))))
2085
2086 (defvar idlwave-shell-get-object-class nil)
2087 (defun idlwave-shell-get-object-class (apos)
2088 "Query the shell for the class of the object before point."
2089 (let ((bos (save-excursion (idlwave-start-of-substatement 'pre) (point)))
2090 (bol (save-excursion (forward-line 0) (point)))
2091 expression)
2092 (save-excursion
2093 (goto-char apos)
2094 (setq expression (buffer-substring
2095 (catch 'exit
2096 (while t
2097 (if (not (re-search-backward
2098 "[^][.A-Za-z0-9_() ]" bos t))
2099 (throw 'exit bos)) ;ran into bos
2100 (if (not (idlwave-is-pointer-dereference bol))
2101 (throw 'exit (1+ (point))))))
2102 apos)))
2103 (when (not (string= expression ""))
2104 (setq idlwave-shell-get-object-class nil)
2105 (idlwave-shell-send-command
2106 (concat "if obj_valid(" expression ") then print,obj_class("
2107 expression ")")
2108 'idlwave-shell-parse-object-class
2109 'hide 'wait)
2110 ;; If we don't know anything about the class, update shell routines
2111 (if (and idlwave-shell-get-object-class
2112 (not (assoc-string idlwave-shell-get-object-class
2113 (idlwave-class-alist) t)))
2114 (idlwave-shell-maybe-update-routine-info))
2115 idlwave-shell-get-object-class)))
2116
2117 (defun idlwave-shell-parse-object-class ()
2118 "Parse the output of the obj_class command."
2119 (let ((match "obj_class([^\n\r]+[\n\r ]"))
2120 (if (string-match (concat match "\\([A-Za-z_0-9]+\\) *[\n\r]\\("
2121 idlwave-shell-prompt-pattern "\\)")
2122 idlwave-shell-command-output)
2123 (setq idlwave-shell-get-object-class
2124 (match-string 1 idlwave-shell-command-output)))))
2125
2126 (defvar idlwave-sint-sysvars nil)
2127 (idlwave-new-sintern-type 'execcomm)
2128
2129 (defun idlwave-shell-complete (&optional arg)
2130 "Do completion in the idlwave-shell buffer.
2131 Calls `idlwave-shell-complete-filename' after some executive commands or
2132 in strings. Otherwise, calls `idlwave-complete' to complete modules and
2133 keywords."
2134 (interactive "P")
2135 (let (exec-cmd)
2136 (cond
2137 ((and
2138 (setq exec-cmd (idlwave-shell-executive-command))
2139 (cdr exec-cmd)
2140 (member (upcase (cdr exec-cmd))
2141 '(".R" ".RU" ".RUN" ".RN" ".RNE" ".RNEW"
2142 ".COM" ".COMP" ".COMPI" ".COMPIL" ".COMPILE")))
2143 ;; We are in a command line with an executive command
2144 (idlwave-shell-complete-filename))
2145
2146 ((car-safe exec-cmd)
2147 (setq idlwave-completion-help-info
2148 '(idlwave-shell-complete-execcomm-help))
2149 (idlwave-complete-in-buffer 'execcomm 'execcomm
2150 idlwave-executive-commands-alist nil
2151 "Select an executive command"
2152 "system variable"))
2153
2154 ((idlwave-shell-batch-command)
2155 (idlwave-shell-complete-filename))
2156
2157 ((idlwave-shell-shell-command)
2158 (idlwave-shell-complete-filename))
2159
2160 ((and (idlwave-shell-filename-string)
2161 (save-excursion
2162 (beginning-of-line)
2163 (let ((case-fold-search t))
2164 (not (looking-at ".*obj_new")))))
2165 (idlwave-shell-complete-filename))
2166
2167 (t
2168 ;; Default completion of modules and keywords
2169 (idlwave-complete arg)))))
2170
2171 ;; Get rid of opaque dynamic variable passing of idlw-help-link?
2172 (defvar idlw-help-link) ; dynamic variable from idlwave-do-mouse-completion-help
2173 (defun idlwave-shell-complete-execcomm-help (mode word)
2174 (let ((word (or (nth 1 idlwave-completion-help-info) word))
2175 (entry (assoc-string word idlwave-executive-commands-alist t)))
2176 (cond
2177 ((eq mode 'test)
2178 (and (stringp word) entry (cdr entry)))
2179 ((eq mode 'set)
2180 (if entry (setq idlw-help-link (cdr entry)))) ; setting dynamic variable!
2181 (t (error "This should not happen")))))
2182
2183 (defun idlwave-shell-complete-filename (&optional arg)
2184 "Complete a file name at point if after a file name.
2185 We assume that we are after a file name when completing one of the
2186 args of an executive .run, .rnew or .compile."
2187 ;; CWD might have changed, resync, to set default directory
2188 (idlwave-shell-resync-dirs)
2189 (let ((comint-file-name-chars idlwave-shell-file-name-chars))
2190 (comint-filename-completion)))
2191
2192 (defun idlwave-shell-executive-command ()
2193 "Return the name of the current executive command, if any."
2194 (save-excursion
2195 (idlwave-beginning-of-statement)
2196 (cons (looking-at "[ \t]*\\.")
2197 (if (looking-at "[ \t]*[.]\\([^ \t\n\r]+\\)[ \t]")
2198 (match-string 1)))))
2199
2200 (defun idlwave-shell-filename-string ()
2201 "Return t if in a string and after what could be a file name."
2202 (let ((limit (point-at-bol)))
2203 (save-excursion
2204 ;; Skip backwards over file name chars
2205 (skip-chars-backward idlwave-shell-file-name-chars limit)
2206 ;; Check of the next char is a string delimiter
2207 (memq (preceding-char) '(?\' ?\")))))
2208
2209 (defun idlwave-shell-batch-command ()
2210 "Return t if we're in a batch command statement like @foo"
2211 (let ((limit (point-at-bol)))
2212 (save-excursion
2213 ;; Skip backwards over filename
2214 (skip-chars-backward idlwave-shell-file-name-chars limit)
2215 (skip-chars-backward " \t" limit)
2216 (and (eq (preceding-char) ?@) (not (idlwave-in-quote))))))
2217
2218 (defun idlwave-shell-shell-command ()
2219 "Return t if we're in a shell command statement like $ls"
2220 (save-excursion
2221 (idlwave-beginning-of-statement)
2222 (looking-at "\\$")))
2223
2224 ;; Debugging Commands ------------------------------------------------------
2225
2226 (defun idlwave-shell-redisplay (&optional hide)
2227 "Try to resync the display with where execution has stopped.
2228 Issues a \"help,/trace\" command followed by a call to
2229 `idlwave-shell-display-line'. Also updates the breakpoint
2230 overlays."
2231 (interactive)
2232 (setq idlwave-shell-calling-stack-index 0)
2233 (idlwave-shell-send-command
2234 "help,/trace"
2235 '(idlwave-shell-display-line
2236 (idlwave-shell-pc-frame))
2237 hide)
2238 (idlwave-shell-bp-query))
2239
2240 (defun idlwave-shell-display-level-in-calling-stack (&optional hide)
2241 (idlwave-shell-send-command
2242 "help,/trace"
2243 `(progn
2244 ;; scanning for the state will reset the stack level - restore it
2245 (setq idlwave-shell-calling-stack-index
2246 ,idlwave-shell-calling-stack-index)
2247 ;; parse the stack and visit the selected frame
2248 (idlwave-shell-parse-stack-and-display))
2249 hide))
2250
2251 (defun idlwave-shell-parse-stack-and-display ()
2252 (let* ((lines (delete "" (idlwave-split-string
2253 idlwave-shell-command-output "^%")))
2254 (stack (delq nil (mapcar 'idlwave-shell-parse-line lines)))
2255 (nmax (1- (length stack)))
2256 (nmin 0) message)
2257 (cond
2258 ((< nmax nmin)
2259 (setq idlwave-shell-calling-stack-index 0)
2260 (ding)
2261 (message "Problem with calling stack"))
2262 ((> idlwave-shell-calling-stack-index nmax)
2263 (ding)
2264 (setq idlwave-shell-calling-stack-index nmax
2265 message (format "%d is the highest calling stack level - can't go further up"
2266 (- nmax))))
2267 ((< idlwave-shell-calling-stack-index nmin)
2268 (ding)
2269 (setq idlwave-shell-calling-stack-index nmin
2270 message (format "%d is the current calling stack level - can't go further down"
2271 (- nmin)))))
2272 (setq idlwave-shell-calling-stack-routine
2273 (nth 2 (nth idlwave-shell-calling-stack-index stack)))
2274
2275 ;; force edebug for this frame if we're in that mode already
2276 (idlwave-shell-display-line
2277 (nth idlwave-shell-calling-stack-index stack) nil
2278 (if idlwave-shell-electric-debug-mode 'force))
2279 (message "%s" (or message
2280 (format "In routine %s (stack level %d)"
2281 idlwave-shell-calling-stack-routine
2282 (- idlwave-shell-calling-stack-index))))))
2283
2284 (defun idlwave-shell-stack-up ()
2285 "Display the source code one step up the calling stack."
2286 (interactive)
2287 (incf idlwave-shell-calling-stack-index)
2288 (idlwave-shell-display-level-in-calling-stack 'hide))
2289 (defun idlwave-shell-stack-down ()
2290 "Display the source code one step down the calling stack."
2291 (interactive)
2292 (decf idlwave-shell-calling-stack-index)
2293 (idlwave-shell-display-level-in-calling-stack 'hide))
2294
2295 (defun idlwave-shell-goto-frame (&optional frame)
2296 "Set buffer to FRAME with point at the frame line.
2297 If the optional argument FRAME is nil then `idlwave-shell-pc-frame'
2298 is used. Does nothing if the resulting frame is nil."
2299 (if frame ()
2300 (setq frame (idlwave-shell-pc-frame)))
2301 (cond
2302 (frame
2303 (set-buffer (idlwave-find-file-noselect (car frame) 'shell))
2304 (widen)
2305 (goto-char (point-min))
2306 (forward-line (1- (nth 1 frame))))))
2307
2308 (defun idlwave-shell-pc-frame ()
2309 "Return the frame for IDL execution."
2310 (and idlwave-shell-halt-frame
2311 (list (nth 0 idlwave-shell-halt-frame)
2312 (nth 1 idlwave-shell-halt-frame)
2313 (nth 2 idlwave-shell-halt-frame))))
2314
2315 (defun idlwave-shell-valid-frame (frame)
2316 "Check that frame is for an existing file."
2317 (file-readable-p (car frame)))
2318
2319 (defun idlwave-shell-stop-line-pending ()
2320 ;; Temporarily change the color of the stop line overlay
2321 (if idlwave-shell-stop-line-overlay
2322 (overlay-put idlwave-shell-stop-line-overlay 'face
2323 (if idlwave-shell-electric-debug-mode
2324 'idlwave-shell-pending-electric-stop
2325 'idlwave-shell-pending-stop))))
2326
2327 (defvar idlwave-shell-suppress-electric-debug nil)
2328 (defun idlwave-shell-display-line (frame &optional col debug)
2329 "Display frame file in other window with overlay arrow.
2330
2331 FRAME is a list of file name, line number, and subroutine name. If
2332 FRAME is nil then remove overlay. If COL is set, move point to that
2333 column in the line. If DEBUG is non-nil, enable the electric debug
2334 mode. If it is 'disable, do not enable no matter what the setting of
2335 `idlwave-shell-automatic-electric-debug'. If it is 'force, enable no
2336 matter what the settings of that variable."
2337 (if (not frame)
2338 ;; remove stop-line overlay from old position
2339 (progn
2340 (setq overlay-arrow-string nil)
2341 (setq idlwave-shell-mode-line-info nil)
2342 (setq idlwave-shell-is-stopped nil)
2343 (if idlwave-shell-stop-line-overlay
2344 (delete-overlay idlwave-shell-stop-line-overlay))
2345 ;; turn off electric debug everywhere, if it's on
2346 (idlwave-shell-electric-debug-all-off))
2347 (if (not (idlwave-shell-valid-frame frame))
2348 ;; fixme: errors are dangerous in shell filters. but i think i
2349 ;; have never encountered this one.
2350 (error "invalid frame - unable to access file: %s" (car frame))
2351 ;;
2352 ;; buffer : the buffer to display a line in.
2353 ;; select-shell: current buffer is the shell.
2354 ;;
2355 (setq idlwave-shell-mode-line-info
2356 (if (nth 2 frame)
2357 (format "[%d:%s]"
2358 (- idlwave-shell-calling-stack-index)
2359 (nth 2 frame))))
2360 (let* ((buffer (idlwave-find-file-noselect (car frame) 'shell))
2361 (select-shell (equal (buffer-name) (idlwave-shell-buffer)))
2362 window pos electric)
2363
2364 ;; first make sure the shell window is visible
2365 (idlwave-display-buffer (idlwave-shell-buffer)
2366 nil (idlwave-shell-shell-frame))
2367
2368 ;; now display the buffer and remember which window it is.
2369 (setq window (idlwave-display-buffer buffer
2370 nil (idlwave-shell-source-frame)))
2371
2372 ;; enter the buffer and mark the line
2373 (with-current-buffer buffer
2374 (save-restriction
2375 (widen)
2376 (goto-char (point-min))
2377 (forward-line (1- (nth 1 frame)))
2378 (setq pos (point))
2379 (setq idlwave-shell-is-stopped t)
2380
2381 (if idlwave-shell-stop-line-overlay
2382 (progn
2383 ;; restore face and move overlay
2384 (overlay-put idlwave-shell-stop-line-overlay 'face
2385 (if idlwave-shell-electric-debug-mode
2386 idlwave-shell-electric-stop-line-face
2387 idlwave-shell-stop-line-face))
2388 (move-overlay idlwave-shell-stop-line-overlay
2389 (point) (point-at-eol)
2390 (current-buffer)))
2391 ;; use the arrow instead, but only if marking is wanted.
2392 (if idlwave-shell-mark-stop-line
2393 (setq overlay-arrow-string idlwave-shell-overlay-arrow))
2394 (or overlay-arrow-position ; create the marker if necessary
2395 (setq overlay-arrow-position (make-marker)))
2396 (set-marker overlay-arrow-position (point) buffer)))
2397
2398 ;; if the point is outside the restriction, widen the buffer.
2399 (if (or (< pos (point-min)) (> pos (point-max)))
2400 (progn
2401 (widen)
2402 (goto-char pos)))
2403
2404 ;; if we have the column of the error, move the cursor there.
2405 (if col (move-to-column col))
2406 (setq pos (point))
2407
2408 ;; enter electric debug mode, if not prohibited and not in
2409 ;; it already
2410 (when (and (not idlwave-shell-electric-debug-mode)
2411 (or (eq debug 'force)
2412 (and
2413 (not (eq debug 'disable)) ;; explicitly disabled
2414 (or
2415 (eq idlwave-shell-automatic-electric-debug t)
2416 (and
2417 (eq idlwave-shell-automatic-electric-debug
2418 'breakpoint)
2419 (not (eq idlwave-shell-current-state 'error))))
2420 (not idlwave-shell-suppress-electric-debug))))
2421 (idlwave-shell-electric-debug-mode t))
2422 (setq electric idlwave-shell-electric-debug-mode))
2423
2424 ;; Make sure pos is really displayed in the window.
2425 (set-window-point window pos)
2426
2427 ;; If we came from the shell, go back there. Otherwise select
2428 ;; the window where the error/halt is displayed.
2429 (if (or (and idlwave-shell-electric-zap-to-file electric)
2430 (and (equal (buffer-name) (idlwave-shell-buffer))
2431 (not select-shell)))
2432 (select-window window))))))
2433
2434
2435 (defun idlwave-shell-step (arg)
2436 "Step one source line.
2437 If given prefix argument ARG, step ARG source lines."
2438 (interactive "p")
2439 (or (not arg) (< arg 1)
2440 (setq arg 1))
2441 (idlwave-shell-stop-line-pending)
2442 (idlwave-shell-send-command
2443 (concat ".s " (if (integerp arg) (int-to-string arg) arg))
2444 nil (if (idlwave-shell-hide-p 'debug) 'mostly) nil t))
2445
2446 (defun idlwave-shell-stepover (arg)
2447 "Stepover one source line.
2448 If given prefix argument ARG, step ARG source lines.
2449 Uses IDL's stepover executive command which does not enter called functions."
2450 (interactive "p")
2451 (or (not arg) (< arg 1)
2452 (setq arg 1))
2453 (idlwave-shell-stop-line-pending)
2454 (idlwave-shell-send-command
2455 (concat ".so " (if (integerp arg) (int-to-string arg) arg))
2456 nil (if (idlwave-shell-hide-p 'debug) 'mostly) nil t))
2457
2458 (defun idlwave-shell-break-here (&optional count cmd condition disabled
2459 no-show)
2460 "Set breakpoint at current line.
2461
2462 If COUNT is nil then an ordinary breakpoint is set. We treat a COUNT
2463 of 1 as a temporary breakpoint using the ONCE keyword. Counts greater
2464 than 1 use the IDL AFTER=count keyword to break only after reaching
2465 the statement COUNT times.
2466
2467 Optional argument CMD is a list or function to evaluate upon reaching
2468 the breakpoint. CONDITION is a break condition, and DISABLED, if
2469 non-nil disables the breakpoint."
2470 (interactive "P")
2471 (when (listp count)
2472 (if (equal (car count) 4)
2473 (setq condition (read-string "Break Condition: ")))
2474 (setq count nil))
2475 (idlwave-shell-set-bp
2476 ;; Create breakpoint
2477 (idlwave-shell-bp (idlwave-shell-current-frame)
2478 (list count cmd condition disabled)
2479 (idlwave-shell-current-module))
2480 no-show))
2481
2482 (defun idlwave-shell-set-bp-check (bp)
2483 "Check for failure to set breakpoint.
2484 This is run on `idlwave-shell-post-command-hook'.
2485 Offers to recompile the procedure if we failed. This usually fixes
2486 the problem with not being able to set the breakpoint."
2487 ;; Scan for message
2488 (if idlwave-shell-command-output
2489 (cond
2490 ((string-match "% BREAKPOINT: *Unable to find code"
2491 idlwave-shell-command-output)
2492 ;; Offer to recompile
2493 (if (progn
2494 (beep)
2495 (y-or-n-p
2496 (concat "Okay to recompile file "
2497 (idlwave-shell-bp-get bp 'file) "?")))
2498 ;; Recompile
2499 (progn
2500 ;; Clean up before retrying
2501 (idlwave-shell-command-failure)
2502 (idlwave-shell-send-command
2503 (concat ".run \"" (idlwave-shell-bp-get bp 'file) "\"") nil
2504 (if (idlwave-shell-hide-p 'run) 'mostly) nil t)
2505 ;; Try setting breakpoint again
2506 (idlwave-shell-set-bp bp))
2507 (beep)
2508 (message "Unable to set breakpoint.")
2509 (idlwave-shell-command-failure))
2510 nil)
2511
2512 ((string-match "% Syntax error" idlwave-shell-command-output)
2513 (message "Syntax error in condition.")
2514 (idlwave-shell-command-failure)
2515 nil)
2516
2517 (t 'okay))))
2518
2519 (defun idlwave-shell-command-failure ()
2520 "Do any necessary clean up when an IDL command fails.
2521 Call this from a function attached to `idlwave-shell-post-command-hook'
2522 that detects the failure of a command.
2523 For example, this is called from `idlwave-shell-set-bp-check' when a
2524 breakpoint can not be set."
2525 ;; Clear pending commands
2526 (setq idlwave-shell-pending-commands nil))
2527
2528 (defun idlwave-shell-cont (&optional no-show)
2529 "Continue executing."
2530 (interactive)
2531 (idlwave-shell-stop-line-pending)
2532 (idlwave-shell-send-command ".c" (unless no-show
2533 '(idlwave-shell-redisplay 'hide))
2534 (if (idlwave-shell-hide-p 'debug) 'mostly)
2535 nil t))
2536
2537 (defun idlwave-shell-go ()
2538 "Run .GO. This starts the main program of the last compiled file."
2539 (interactive)
2540 (idlwave-shell-stop-line-pending)
2541 (idlwave-shell-send-command ".go" '(idlwave-shell-redisplay 'hide)
2542 (if (idlwave-shell-hide-p 'debug) 'mostly)
2543 nil t))
2544
2545 (defun idlwave-shell-return ()
2546 "Run .RETURN (continue to next return, but stay in subprogram)."
2547 (interactive)
2548 (idlwave-shell-stop-line-pending)
2549 (idlwave-shell-send-command ".return" '(idlwave-shell-redisplay 'hide)
2550 (if (idlwave-shell-hide-p 'debug) 'mostly)
2551 nil t))
2552
2553 (defun idlwave-shell-skip ()
2554 "Run .SKIP (skip one line, then step)."
2555 (interactive)
2556 (idlwave-shell-stop-line-pending)
2557 (idlwave-shell-send-command ".skip" '(idlwave-shell-redisplay 'hide)
2558 (if (idlwave-shell-hide-p 'debug) 'mostly)
2559 nil t))
2560
2561 (defun idlwave-shell-clear-bp (bp &optional no-query)
2562 "Clear breakpoint BP.
2563 Clears in IDL and in `idlwave-shell-bp-alist'."
2564 (let ((index (idlwave-shell-bp-get bp)))
2565 (if index
2566 (progn
2567 (idlwave-shell-send-command
2568 (concat "breakpoint,/clear," (int-to-string index))
2569 nil (idlwave-shell-hide-p 'breakpoint) nil t)
2570 (unless no-query (idlwave-shell-bp-query))))))
2571
2572 (defun idlwave-shell-current-frame ()
2573 "Return a list containing the current file name and line point is in.
2574 If in the IDL shell buffer, returns `idlwave-shell-pc-frame'."
2575 (if (eq (current-buffer) (get-buffer (idlwave-shell-buffer)))
2576 ;; In IDL shell
2577 (idlwave-shell-pc-frame)
2578 ;; In source
2579 (list (idlwave-shell-file-name (buffer-file-name))
2580 (save-restriction
2581 (widen)
2582 (1+ (count-lines 1 (point-at-bol)))))))
2583
2584 (defun idlwave-shell-current-module ()
2585 "Return the name of the module for the current file.
2586 Returns nil if unable to obtain a module name."
2587 (if (eq (current-buffer) (get-buffer (idlwave-shell-buffer)))
2588 ;; In IDL shell
2589 (nth 2 idlwave-shell-halt-frame)
2590 ;; In pro file
2591 (save-restriction
2592 (widen)
2593 (save-excursion
2594 (if (idlwave-prev-index-position)
2595 (let* ((module (idlwave-what-module))
2596 (name (idlwave-make-full-name (nth 2 module) (car module)))
2597 (type (nth 1 module)))
2598 (list (upcase name) type)))))))
2599
2600 (defun idlwave-shell-clear-current-bp ()
2601 "Remove breakpoint at current line.
2602 This command can be called from the shell buffer if IDL is currently
2603 stopped at a breakpoint."
2604 (interactive)
2605 (let ((bp (idlwave-shell-find-current-bp)))
2606 (if bp (idlwave-shell-clear-bp bp))))
2607
2608
2609 (defun idlwave-shell-toggle-enable-current-bp (&optional bp force
2610 no-update)
2611 "Disable or enable current breakpoint or a breakpoint passed in BP.
2612 If FORCE is 'disable or 'enable, for that condition instead of
2613 toggling. If NO-UPDATE is non-nil, don't update the breakpoint
2614 list after toggling."
2615 (interactive)
2616 (let* ((bp (or bp (idlwave-shell-find-current-bp)))
2617 (disabled (idlwave-shell-bp-get bp 'disabled)))
2618 (cond ((eq force 'disable) (setq disabled nil))
2619 ((eq force 'enable) (setq disabled t)))
2620 (when bp
2621 (setf (nth 3 (cdr (cdr bp))) (not disabled))
2622 (idlwave-shell-send-command
2623 (concat "breakpoint,"
2624 (if disabled "/enable," "/disable,")
2625 (int-to-string (idlwave-shell-bp-get bp)))
2626 nil (idlwave-shell-hide-p 'breakpoint) nil t)
2627 (unless no-update (idlwave-shell-bp-query)))))
2628
2629 (defun idlwave-shell-enable-all-bp (&optional enable no-update bpl)
2630 "Disable all breakpoints we know about which need disabling.
2631 If ENABLE is non-nil, enable them instead."
2632 (let ((bpl (or bpl idlwave-shell-bp-alist)) disabled modified)
2633 (while bpl
2634 (setq disabled (idlwave-shell-bp-get (car bpl) 'disabled))
2635 (when (idlwave-xor (not disabled) (eq enable 'enable))
2636 (idlwave-shell-toggle-enable-current-bp
2637 (car bpl) (if (eq enable 'enable) 'enable 'disable) no-update)
2638 (push (car bpl) modified))
2639 (setq bpl (cdr bpl)))
2640 (unless no-update (idlwave-shell-bp-query))
2641 modified))
2642
2643 (defun idlwave-shell-to-here ()
2644 "Set a breakpoint with count 1 then continue."
2645 (interactive)
2646 ;; temporarily disable all other breakpoints
2647 (let ((disabled (idlwave-shell-enable-all-bp 'disable 'no-update)))
2648 (idlwave-shell-break-here 1 nil nil nil 'no-show)
2649 (idlwave-shell-cont 'no-show)
2650 (idlwave-shell-enable-all-bp 'enable 'no-update disabled))
2651 (idlwave-shell-redisplay)) ; sync up everything at the end
2652
2653 (defun idlwave-shell-break-this-module (&optional arg)
2654 (interactive "P")
2655 (save-excursion
2656 (idlwave-beginning-of-subprogram)
2657 (idlwave-shell-break-here arg)))
2658
2659 (defun idlwave-shell-break-in ()
2660 "Look for a module name near point and set a break point for it.
2661 The command looks for an identifier near point and sets a breakpoint
2662 for the first line of the corresponding module. If MODULE is `t', set
2663 in the current routine."
2664 (interactive)
2665 (let* ((module (idlwave-fix-module-if-obj_new (idlwave-what-module)))
2666 (type (nth 1 module))
2667 (name (car module))
2668 (class (nth 2 module)))
2669 (if module
2670 (progn
2671 (setq module (idlwave-make-full-name class name))
2672 (idlwave-shell-module-source-query module type)
2673 (idlwave-shell-set-bp-in-module name type class))
2674 (error "No identifier at point"))))
2675
2676
2677 (defun idlwave-shell-set-bp-in-module (name type class)
2678 "Set breakpoint in module.
2679 Assumes that `idlwave-shell-sources-alist' contains an entry for that module."
2680 (let* ((module (idlwave-make-full-name class name))
2681 (source-file
2682 (car-safe (cdr-safe
2683 (or
2684 (assoc (upcase module)
2685 idlwave-shell-sources-alist)
2686 (nth 3 (idlwave-best-rinfo-assoc name type class
2687 (idlwave-routines)))))))
2688 buf)
2689 (if (or (not source-file)
2690 (not (file-regular-p source-file))
2691 (not (setq buf
2692 (or (idlwave-get-buffer-visiting source-file)
2693 (find-file-noselect source-file)))))
2694 (progn
2695 (message "The source file for module %s is probably not compiled"
2696 module)
2697 (beep))
2698 (with-current-buffer buf
2699 (save-excursion
2700 (goto-char (point-min))
2701 (let ((case-fold-search t))
2702 (if (re-search-forward
2703 (concat "^[ \t]*\\(pro\\|function\\)[ \t]+"
2704 (downcase module)
2705 "[ \t\n,]") nil t)
2706 (progn
2707 (goto-char (match-beginning 1))
2708 (message "Setting breakpoint for module %s" module)
2709 (idlwave-shell-break-here))
2710 (message "Cannot find module %s in file %s" module source-file)
2711 (beep))))))))
2712
2713 (defun idlwave-shell-up ()
2714 "Run to end of current block.
2715 Sets a breakpoint with count 1 at end of block, then continues."
2716 (interactive)
2717 (if (idlwave-shell-pc-frame)
2718 (save-excursion
2719 (idlwave-shell-goto-frame)
2720 ;; find end of subprogram
2721 (let ((eos (save-excursion
2722 (idlwave-beginning-of-subprogram)
2723 (idlwave-forward-block)
2724 (point))))
2725 (idlwave-backward-up-block -1)
2726 ;; move beyond end block line - IDL will not break there.
2727 ;; That is, you can put a breakpoint there but when IDL does
2728 ;; break it will report that it is at the next line.
2729 (idlwave-next-statement)
2730 (idlwave-end-of-statement)
2731 ;; Make sure we are not beyond subprogram
2732 (if (< (point) eos)
2733 ;; okay
2734 ()
2735 ;; Move back inside subprogram
2736 (goto-char eos)
2737 (idlwave-previous-statement))
2738 (idlwave-shell-to-here)))))
2739
2740 (defun idlwave-shell-out ()
2741 "Attempt to run until this procedure exits.
2742 Runs to the last statement and then steps 1 statement. Use the .out command."
2743 (interactive)
2744 (idlwave-shell-send-command ".o" nil
2745 (if (idlwave-shell-hide-p 'debug) 'mostly)
2746 nil t))
2747
2748 (defun idlwave-shell-goto-previous-bp ()
2749 "Move to the previous breakpoint in the buffer."
2750 (interactive)
2751 (idlwave-shell-move-to-bp -1))
2752 (defun idlwave-shell-goto-next-bp ()
2753 "Move to the next breakpoint in the buffer."
2754 (interactive)
2755 (idlwave-shell-move-to-bp 1))
2756
2757 (defun idlwave-shell-move-to-bp (dir)
2758 "Move to the next or previous breakpoint, depending on direction DIR."
2759 (let* ((frame (idlwave-shell-current-frame))
2760 (file (car frame))
2761 (orig-bp-line (nth 1 frame))
2762 (bp-alist idlwave-shell-bp-alist)
2763 (orig-func (if (> dir 0) '> '<))
2764 (closer-func (if (> dir 0) '< '>))
2765 bp got-bp bp-line cur-line)
2766 (while (setq bp (pop bp-alist))
2767 (when (string= file (car (car bp)))
2768 (setq got-bp 1)
2769 (setq cur-line (nth 1 (car bp)))
2770 (if (and
2771 (funcall orig-func cur-line orig-bp-line)
2772 (or (not bp-line) (funcall closer-func cur-line bp-line)))
2773 (setq bp-line cur-line))))
2774 (unless bp-line (error "No further breakpoints"))
2775 (goto-char (point-min))
2776 (forward-line (1- bp-line))))
2777
2778 ;; Examine Commands ------------------------------------------------------
2779
2780 (defun idlwave-shell-help-expression (arg)
2781 "Print help on current expression. See `idlwave-shell-print'."
2782 (interactive "P")
2783 (idlwave-shell-print arg 'help))
2784
2785 (defmacro idlwave-shell-mouse-examine (help &optional ev)
2786 "Create a function for generic examination of expressions."
2787 `(lambda (event)
2788 "Expansion function for expression examination."
2789 (interactive "e")
2790 (let* ((drag-track (fboundp 'mouse-drag-track))
2791 (transient-mark-mode t)
2792 (zmacs-regions t)
2793 (tracker (if (featurep 'xemacs)
2794 (if (fboundp
2795 'default-mouse-track-event-is-with-button)
2796 'idlwave-xemacs-hack-mouse-track
2797 'mouse-track)
2798 ;; Emacs 22 no longer completes the drag with
2799 ;; mouse-drag-region, without an additional
2800 ;; event. mouse-drag-track does so.
2801 (if drag-track 'mouse-drag-track 'mouse-drag-region))))
2802 (funcall tracker event)
2803 (idlwave-shell-print (if (idlwave-region-active-p) '(4) nil)
2804 ,help ,ev))))
2805
2806 ;; Begin terrible hack section -- XEmacs tests for button2 explicitly
2807 ;; on drag events, calling drag-n-drop code if detected. Ughhh...
2808 (defun idlwave-default-mouse-track-event-is-with-button (event n)
2809 t)
2810
2811 (defun idlwave-xemacs-hack-mouse-track (event)
2812 (if (featurep 'xemacs)
2813 (let ((oldfunc (symbol-function
2814 'default-mouse-track-event-is-with-button)))
2815 (unwind-protect
2816 (progn
2817 (fset 'default-mouse-track-event-is-with-button
2818 'idlwave-default-mouse-track-event-is-with-button)
2819 (mouse-track event))
2820 (fset 'default-mouse-track-event-is-with-button oldfunc)))))
2821 ;;; End terrible hack section
2822
2823 (defun idlwave-shell-mouse-print (event)
2824 "Print value of variable at the mouse position, with `print'."
2825 (interactive "e")
2826 (funcall (idlwave-shell-mouse-examine nil) event))
2827
2828 (defun idlwave-shell-mouse-help (event)
2829 "Print value of variable at the mouse position, with `help'."
2830 (interactive "e")
2831 (funcall (idlwave-shell-mouse-examine 'help) event))
2832
2833 (defun idlwave-shell-examine-select (event)
2834 "Pop-up a list to select from for examining the expression."
2835 (interactive "e")
2836 (funcall (idlwave-shell-mouse-examine nil event) event))
2837
2838 (defmacro idlwave-shell-examine (help)
2839 "Create a function for key-driven expression examination."
2840 `(lambda ()
2841 (interactive)
2842 (idlwave-shell-print nil ,help)))
2843
2844 (defvar idlwave-shell-examine-label nil
2845 "Label to include with examine text if in a separate buffer.")
2846 (defvar idlwave-shell-examine-completion-list nil)
2847
2848 (defun idlwave-shell-print (arg &optional help ev complete-help-type)
2849 "Print current expression.
2850
2851 With HELP non-nil, show help on expression. If HELP is a string,
2852 the expression will be put in place of ___, e.g.:
2853
2854 print,size(___,/DIMENSIONS)
2855
2856 HELP can also be a cons cell ( NAME . STRING ) in which case NAME will
2857 be used to label the help print-out.
2858
2859 Otherwise, print is called on the expression.
2860
2861 An expression is an identifier plus 1 pair of matched parentheses
2862 directly following the identifier - an array or function call.
2863 Alternatively, an expression is the contents of any matched
2864 parentheses when the open parenthesis is not directly preceded by an
2865 identifier. If point is at the beginning or within an expression
2866 return the inner-most containing expression, otherwise, return the
2867 preceding expression.
2868
2869 With prefix arg, or if transient mode set and the region is defined,
2870 use the current region as the expression.
2871
2872 With double prefix arg ARG prompt for an expression.
2873
2874 If EV is a valid event passed, pop-up a list from
2875 `idlwave-shell-examine-alist' from which to select the help
2876 command text. If instead COMPLETE-HELP-TYPE is non-nil, choose
2877 from `idlwave-shell-examine-alist' via mini-buffer shortcut key."
2878 (interactive "P")
2879
2880 ;; For speed: assume the helper routine hasn't been lost, e.g. with
2881 ;; .FULL_RESET_SESSION. We'll recover if necessary
2882 (unless idlwave-idlwave_routine_info-compiled
2883 (idlwave-shell-compile-helper-routines))
2884 (save-excursion
2885 (let* ((process (get-buffer-process (current-buffer)))
2886 (process-mark (if process (process-mark process)))
2887 (stack-label
2888 (if (and (integerp idlwave-shell-calling-stack-index)
2889 (> idlwave-shell-calling-stack-index 0))
2890 (format " [-%d:%s]"
2891 idlwave-shell-calling-stack-index
2892 idlwave-shell-calling-stack-routine)))
2893 expr beg end cmd)
2894 (cond
2895 ((equal arg '(16))
2896 (setq expr (read-string "Expression: ")))
2897 ((and (or arg (idlwave-region-active-p))
2898 (< (- (region-end) (region-beginning)) 2000))
2899 (setq beg (region-beginning)
2900 end (region-end)))
2901 (t
2902 (idlwave-with-special-syntax
2903 ;; Move to beginning of current or previous expression
2904 (if (looking-at "\\<\\|(")
2905 ;; At beginning of expression, don't move backwards unless
2906 ;; this is at the end of an identifier.
2907 (if (looking-at "\\>")
2908 (backward-sexp))
2909 (backward-sexp))
2910 (if (looking-at "\\>")
2911 ;; Move to beginning of identifier - must be an array or
2912 ;; function expression.
2913 (backward-sexp))
2914 ;; Move to end of expression
2915 (setq beg (point))
2916 (forward-sexp)
2917 (while (looking-at "\\>[[(]\\|\\.")
2918 ;; an array
2919 (forward-sexp))
2920 (setq end (point)))))
2921
2922 ;; Get expression, but first move the begin mark if a
2923 ;; process-mark is inside the region, to keep the overlay from
2924 ;; wandering in the Shell.
2925 (when (and beg end)
2926 (if (and process-mark (> process-mark beg) (< process-mark end))
2927 (setq beg (marker-position process-mark)))
2928 (setq expr (buffer-substring beg end)))
2929
2930 ;; Show the overlay(s) and attach any necessary hooks and filters
2931 (when (and beg end idlwave-shell-expression-overlay)
2932 (move-overlay idlwave-shell-expression-overlay beg end
2933 (current-buffer))
2934 (add-hook 'pre-command-hook
2935 'idlwave-shell-delete-expression-overlay))
2936 (add-hook 'pre-command-hook
2937 'idlwave-shell-delete-output-overlay)
2938
2939 ;; Remove empty or comment-only lines
2940 (while (string-match "\n[ \t]*\\(;.*\\)?\r*\n" expr)
2941 (setq expr (replace-match "\n" t t expr)))
2942 ;; Concatenate continuation lines
2943 (while (string-match "[ \t]*\\$[ \t]*\\(;.*\\)?\\(\n[ \t]*\\|$\\)" expr)
2944 (setq expr (replace-match "" t t expr)))
2945 ;; Remove final newline
2946 (if (string-match "\n[ \t\r]*\\'" expr)
2947 (setq expr (replace-match "" t t expr)))
2948
2949 (catch 'exit
2950 ;; Pop-up or complete on the examine selection list, if appropriate
2951 (if (or
2952 complete-help-type
2953 (and ev idlwave-shell-examine-alist)
2954 (consp help))
2955 (let ((help-cons
2956 (if (consp help) help
2957 (assoc
2958 ;; A cons from either a pop-up or mini-buffer completion
2959 (if complete-help-type
2960 (idlwave-one-key-select 'idlwave-shell-examine-alist
2961 "Examine with: " 1.5)
2962 ;; (idlwave-completing-read
2963 ;; "Examine with: "
2964 ;; idlwave-shell-examine-alist nil nil nil
2965 ;; 'idlwave-shell-examine-completion-list
2966 ;; "Print")
2967 (idlwave-popup-select
2968 ev
2969 (mapcar 'car idlwave-shell-examine-alist)
2970 "Examine with"))
2971 idlwave-shell-examine-alist))))
2972 (setq help (cdr help-cons))
2973 (if (null help) (throw 'exit nil))
2974 (if idlwave-shell-separate-examine-output
2975 (setq idlwave-shell-examine-label
2976 (concat
2977 (format "==>%s<==\n%s:" expr (car help-cons))
2978 stack-label "\n"))))
2979 ;; The regular help label (no popups, cons cells, etc.)
2980 (setq idlwave-shell-examine-label
2981 (concat
2982 (format "==>%s<==\n%s:" expr
2983 (cond ((null help) "print")
2984 ((stringp help) help)
2985 (t (symbol-name help))))
2986 stack-label "\n")))
2987
2988 ;; Send the command
2989 (if stack-label
2990 (setq expr (idlwave-retrieve-expression-from-level
2991 expr
2992 idlwave-shell-calling-stack-index)))
2993 (setq cmd (idlwave-shell-help-statement help expr))
2994 ;;(idlwave-shell-recenter-shell-window)
2995 (idlwave-shell-send-command
2996 cmd
2997 'idlwave-shell-check-compiled-and-display
2998 (if idlwave-shell-separate-examine-output 'hide))))))
2999
3000 (defvar idlwave-shell-examine-window-alist nil
3001 "Variable to hold the win/height pairs for all *Examine* windows.")
3002
3003 (defvar idlwave-shell-examine-map (make-sparse-keymap))
3004 (define-key idlwave-shell-examine-map "q" 'idlwave-shell-examine-display-quit)
3005 (define-key idlwave-shell-examine-map "c" 'idlwave-shell-examine-display-clear)
3006
3007
3008 (defun idlwave-shell-check-compiled-and-display ()
3009 "Check examine output for warning about undefined procedure/function."
3010 (if (string-match "% Attempt to call undefined" idlwave-shell-command-output)
3011 (idlwave-shell-compile-helper-routines))
3012 (if idlwave-shell-separate-examine-output
3013 (idlwave-shell-examine-display)
3014 (idlwave-shell-examine-highlight)))
3015
3016 (defun idlwave-shell-examine-display ()
3017 "View the examine command output in a separate buffer."
3018 (let (win cur-beg cur-end)
3019 (with-current-buffer (get-buffer-create "*Examine*")
3020 (use-local-map idlwave-shell-examine-map)
3021 (setq buffer-read-only nil)
3022 (goto-char (point-max))
3023 (save-restriction
3024 (narrow-to-region (point) (point))
3025 (if (string-match "^% Syntax error." idlwave-shell-command-output)
3026 (insert "% Syntax error.\n")
3027 (insert idlwave-shell-command-output)
3028 ;; Just take the last bit between the prompts (if more than one).
3029 (let* ((end (or
3030 (re-search-backward idlwave-shell-prompt-pattern nil t)
3031 (point-max)))
3032 (beg (progn
3033 (goto-char
3034 (or (progn (if (re-search-backward
3035 idlwave-shell-prompt-pattern nil t)
3036 (match-end 0)))
3037 (point-min)))
3038 (re-search-forward "\n")))
3039 (str (buffer-substring beg end)))
3040 (delete-region (point-min) (point-max))
3041 (insert str)
3042 (if idlwave-shell-examine-label
3043 (progn (goto-char (point-min))
3044 (insert idlwave-shell-examine-label)
3045 (setq idlwave-shell-examine-label nil)))))
3046 (setq cur-beg (point-min)
3047 cur-end (point-max))
3048 (setq buffer-read-only t)
3049 (move-overlay idlwave-shell-output-overlay cur-beg cur-end
3050 (current-buffer))
3051
3052 ;; Look for the examine buffer in all windows. If one is
3053 ;; found in a frame all by itself, use that, otherwise, switch
3054 ;; to or create an examine window in this frame, and resize if
3055 ;; it's a newly created window
3056 (let* ((winlist (get-buffer-window-list "*Examine*" nil 'visible)))
3057 (setq win (idlwave-display-buffer
3058 "*Examine*"
3059 nil
3060 (let ((list winlist) thiswin)
3061 (catch 'exit
3062 (save-selected-window
3063 (while (setq thiswin (pop list))
3064 (select-window thiswin)
3065 (if (one-window-p)
3066 (throw 'exit (window-frame thiswin)))))))))
3067 (set-window-start win (point-min)) ; Ensure the point is visible.
3068 (save-selected-window
3069 (select-window win)
3070 (let ((elt (assoc win idlwave-shell-examine-window-alist)))
3071 (when (and (not (one-window-p))
3072 (or (not (memq win winlist)) ;a newly created window
3073 (eq (window-height) (cdr elt))))
3074 ;; Autosize it.
3075 (enlarge-window (- (/ (frame-height) 2)
3076 (window-height)))
3077 (shrink-window-if-larger-than-buffer)
3078 ;; Clean the window list of dead windows
3079 (setq idlwave-shell-examine-window-alist
3080 (delq nil
3081 (mapcar (lambda (x) (if (window-live-p (car x)) x))
3082 idlwave-shell-examine-window-alist)))
3083 ;; And add the new value.
3084 (if (setq elt (assoc win idlwave-shell-examine-window-alist))
3085 (setcdr elt (window-height))
3086 (add-to-list 'idlwave-shell-examine-window-alist
3087 (cons win (window-height)))))))))
3088 ;; Recenter for maximum output, after widened
3089 (save-selected-window
3090 (select-window win)
3091 (goto-char (point-max))
3092 (skip-chars-backward "\n")
3093 (recenter -1)))))
3094
3095 (defun idlwave-shell-examine-display-quit ()
3096 (interactive)
3097 (let ((win (selected-window)))
3098 (if (one-window-p)
3099 (delete-frame (window-frame win))
3100 (delete-window win))))
3101
3102 (defun idlwave-shell-examine-display-clear ()
3103 (interactive)
3104 (let ((buf (get-buffer "*Examine*")))
3105 (when (bufferp buf)
3106 (with-current-buffer buf
3107 (let ((inhibit-read-only t))
3108 (erase-buffer))))))
3109
3110 (defun idlwave-retrieve-expression-from-level (expr level)
3111 "Return IDL command to print the expression EXPR from stack level LEVEL.
3112
3113 It does not seem possible to evaluate an expression on a different
3114 level than the current. Therefore, this function retrieves variables
3115 by reference from other levels, and then includes that variable in
3116 place of the chosen one.
3117
3118 Since this function depends upon the undocumented IDL routine
3119 routine_names, there is no guarantee that this will work with future
3120 versions of IDL."
3121 (let ((fetch (- 0 level))
3122 (start 0)
3123 var fetch-start fetch-end pre post)
3124
3125 ;; FIXME: In the following we try to find the variables in expression
3126 ;; This is quite empirical - I don't know in what situations this will
3127 ;; break. We will look for identifiers and exclude cases where we
3128 ;; know it is not a variable. To distinguish array references from
3129 ;; function calls, we require that arrays use [] instead of ()
3130
3131 (while (string-match
3132 "\\(\\`\\|[^a-zA-Z0-9$_][ \t]*\\)\\([a-zA-Z][a-zA-Z0-9$_]*\\)\\([ \t]*[^a-zA-Z0-9$_]\\|\\'\\)" expr start)
3133 (setq var (match-string 2 expr)
3134 start (match-end 2)
3135 pre (substring expr 0 (match-beginning 2))
3136 post (substring expr (match-end 2)))
3137 (cond
3138 ((or
3139 ;; Exclude identifiers which are not variables
3140 (string-match ",[ \t$\n]*/\\'" pre) ;; a `/' KEYWORD
3141 (and (string-match "[,(][ \t\n]*\\'" pre)
3142 (string-match "\\`[ \t]*=" post)) ;; a `=' KEYWORD
3143 (string-match "\\`(" post) ;; a function
3144 (string-match "->[ \t]*\\'" pre) ;; a method
3145 (string-match "\\.\\'" pre))) ;; structure member
3146
3147 ;; Skip over strings
3148 ((and (string-match "\\([\"\']\\)[^\1]*$" pre)
3149 (string-match (concat "^[^" (match-string 1 pre) "]*"
3150 (match-string 1 pre)) post))
3151 (setq start (+ start (match-end 0))))
3152
3153
3154 ;; seems to be a variable - delimit its name
3155 (t
3156 (put-text-property start (- start (length var)) 'fetch t expr))))
3157
3158 (setq start 0)
3159 (while (setq fetch-start
3160 (next-single-property-change start 'fetch expr))
3161 (if (get-text-property start 'fetch expr) ; it's on in range
3162 (setq fetch-end fetch-start ;it's off in range
3163 fetch-start start)
3164 (setq fetch-end (next-single-property-change fetch-start 'fetch expr)))
3165 (unless fetch-end (setq fetch-end (length expr)))
3166 (remove-text-properties fetch-start fetch-end '(fetch) expr)
3167 (setq expr (concat (substring expr 0 fetch-start)
3168 (format "(routine_names('%s',fetch=%d))"
3169 (substring expr fetch-start fetch-end)
3170 fetch)
3171 (substring expr fetch-end)))
3172 (setq start fetch-end))
3173 (if (get-text-property 0 'fetch expr) ; Full expression, left over
3174 (setq expr (format "(routine_names('%s',fetch=%d))" expr fetch)))
3175 expr))
3176
3177
3178 (defun idlwave-shell-help-statement (help expr)
3179 "Construct a help statement for printing expression EXPR.
3180
3181 HELP can be non-nil for `help,', nil for 'print,' or any string into which
3182 to insert expression in place of the marker ___, e.g.: print,
3183 size(___,/DIMENSIONS)"
3184 (cond
3185 ((null help)
3186 (concat "idlwave_print_safe, " expr ","
3187 (number-to-string idlwave-shell-max-print-length)))
3188 ((stringp help)
3189 (if (string-match "\\(^\\|[^_]\\)\\(___\\)\\([^_]\\|$\\)" help)
3190 (concat (substring help 0 (match-beginning 2))
3191 expr
3192 (substring help (match-end 2)))))
3193 (t
3194 (concat "help, " expr))))
3195
3196
3197 (defun idlwave-shell-examine-highlight ()
3198 "Highlight the most recent IDL output."
3199 (let* ((buffer (get-buffer (idlwave-shell-buffer)))
3200 (process (get-buffer-process buffer))
3201 (process-mark (if process (process-mark process)))
3202 output-begin output-end)
3203 (with-current-buffer buffer
3204 (goto-char process-mark)
3205 (beginning-of-line)
3206 (setq output-end (point))
3207 (re-search-backward idlwave-shell-prompt-pattern nil t)
3208 (beginning-of-line 2)
3209 (setq output-begin (point)))
3210
3211 ;; First make sure the shell window is visible
3212 (idlwave-display-buffer (idlwave-shell-buffer)
3213 nil (idlwave-shell-shell-frame))
3214 (if (and idlwave-shell-output-overlay process-mark)
3215 (move-overlay idlwave-shell-output-overlay
3216 output-begin output-end buffer))))
3217
3218 (defun idlwave-shell-delete-output-overlay ()
3219 (unless (or (eq this-command 'idlwave-shell-mouse-nop)
3220 (eq this-command 'handle-switch-frame))
3221 (condition-case nil
3222 (if idlwave-shell-output-overlay
3223 (delete-overlay idlwave-shell-output-overlay))
3224 (error nil))
3225 (remove-hook 'pre-command-hook 'idlwave-shell-delete-output-overlay)))
3226
3227 (defun idlwave-shell-delete-expression-overlay ()
3228 (unless (or (eq this-command 'idlwave-shell-mouse-nop)
3229 (eq this-command 'handle-switch-frame))
3230 (condition-case nil
3231 (if idlwave-shell-expression-overlay
3232 (delete-overlay idlwave-shell-expression-overlay))
3233 (error nil))
3234 (remove-hook 'pre-command-hook 'idlwave-shell-delete-expression-overlay)))
3235
3236 (defvar idlwave-shell-bp-alist nil
3237 "Alist of breakpoints.
3238 A breakpoint is a cons cell \(\(file line\) . \(\(index module\) data\)\)
3239
3240 The car is the `frame' for the breakpoint:
3241 file - full path file name.
3242 line - line number of breakpoint - integer.
3243
3244 The first element of the cdr is a list of internal IDL data:
3245 index - the index number of the breakpoint internal to IDL.
3246 module - the module for breakpoint internal to IDL.
3247
3248 Remaining elements of the cdr:
3249 data - Data associated with the breakpoint by idlwave-shell currently
3250 contains four items:
3251
3252 count - number of times to execute breakpoint. When count reaches 0
3253 the breakpoint is cleared and removed from the alist.
3254
3255 command - command to execute when breakpoint is reached, either a
3256 lisp function to be called with `funcall' with no arguments or a
3257 list to be evaluated with `eval'.
3258
3259 condition - any condition to apply to the breakpoint.
3260
3261 disabled - whether the bp is disabled.")
3262
3263 (defun idlwave-shell-run-region (beg end &optional n)
3264 "Compile and run the region using the IDL process.
3265 Copies the region to a temporary file `idlwave-shell-temp-pro-file'
3266 and issues the IDL .run command for the file. Because the region
3267 is compiled and run as a main program there is no problem with
3268 begin-end blocks extending over multiple lines - which would be
3269 a problem if `idlwave-shell-evaluate-region' was used.
3270 An END statement is appended to the region if necessary.
3271
3272 If there is a prefix argument, display IDL process."
3273 (interactive "r\nP")
3274 (let ((oldbuf (current-buffer)))
3275 (with-current-buffer (idlwave-find-file-noselect
3276 (idlwave-shell-temp-file 'pro) 'tmp)
3277 (set (make-local-variable 'comment-start-skip) ";+[ \t]*")
3278 (set (make-local-variable 'comment-start) ";")
3279 (erase-buffer)
3280 (insert-buffer-substring oldbuf beg end)
3281 (if (not (save-excursion
3282 (idlwave-previous-statement)
3283 (idlwave-look-at "\\<end\\>")))
3284 (insert "\nend\n"))
3285 (save-buffer 0)))
3286 (idlwave-shell-send-command (concat ".run \""
3287 idlwave-shell-temp-pro-file "\"")
3288 nil
3289 (if (idlwave-shell-hide-p 'run) 'mostly)
3290 nil t)
3291 (if n
3292 (idlwave-display-buffer (idlwave-shell-buffer)
3293 nil (idlwave-shell-shell-frame))))
3294
3295 (defun idlwave-shell-evaluate-region (beg end &optional n)
3296 "Send region to the IDL process.
3297 If there is a prefix argument, display IDL process.
3298 Does not work for a region with multiline blocks - use
3299 `idlwave-shell-run-region' for this."
3300 (interactive "r\nP")
3301 (idlwave-shell-send-command (buffer-substring beg end))
3302 (if n
3303 (idlwave-display-buffer (idlwave-shell-buffer)
3304 nil (idlwave-shell-shell-frame))))
3305
3306 (defun idlwave-shell-delete-temp-files ()
3307 "Delete the temporary files and kill associated buffers."
3308 (if (stringp idlwave-shell-temp-pro-file)
3309 (condition-case nil
3310 (let ((buf (idlwave-get-buffer-visiting
3311 idlwave-shell-temp-pro-file)))
3312 (if (buffer-live-p buf)
3313 (kill-buffer buf))
3314 (delete-file idlwave-shell-temp-pro-file))
3315 (error nil)))
3316 (if (stringp idlwave-shell-temp-rinfo-save-file)
3317 (condition-case nil
3318 (delete-file idlwave-shell-temp-rinfo-save-file)
3319 (error nil))))
3320
3321 (defun idlwave-display-buffer (buf not-this-window-p &optional frame)
3322 (if (featurep 'xemacs)
3323 ;; The XEmacs version enforces the frame
3324 (display-buffer buf not-this-window-p frame)
3325 ;; For Emacs, we need to force the frame ourselves.
3326 (let ((this-frame (selected-frame)))
3327 (save-excursion ;; make sure we end up in the same buffer
3328 (if (frame-live-p frame)
3329 (select-frame frame))
3330 (if (eq this-frame (selected-frame))
3331 ;; same frame: use display buffer, to make sure the current
3332 ;; window stays.
3333 (display-buffer buf)
3334 ;; different frame
3335 (if (one-window-p)
3336 ;; only window: switch
3337 (progn
3338 (switch-to-buffer buf)
3339 (selected-window)) ; must return the window.
3340 ;; several windows - use display-buffer
3341 (display-buffer buf not-this-window-p)))))))
3342 ; (if (not (frame-live-p frame)) (setq frame nil))
3343 ; (display-buffer buf not-this-window-p frame))
3344
3345 (defvar idlwave-shell-bp-buffer " *idlwave-shell-bp*"
3346 "Scratch buffer for parsing IDL breakpoint lists and other stuff.")
3347
3348 (defun idlwave-shell-bp-query (&optional no-show)
3349 "Reconcile idlwave-shell's breakpoint list with IDL's.
3350 Queries IDL using the string in `idlwave-shell-bp-query'."
3351 (interactive)
3352 (idlwave-shell-send-command idlwave-shell-bp-query
3353 `(progn
3354 (idlwave-shell-filter-bp (quote ,no-show)))
3355 'hide))
3356
3357 (defun idlwave-shell-bp-get (bp &optional item)
3358 "Get a value for a breakpoint.
3359 BP has the form of elements in `idlwave-shell-bp-alist'.
3360 Optional second arg ITEM is the particular value to retrieve.
3361 ITEM can be 'file, 'line, 'index, 'module, 'count, 'cmd,
3362 'condition, 'disabled, 'type, or 'data. 'data returns a list
3363 of 'count, 'cmd and 'condition. Defaults to 'index."
3364 (cond
3365 ;; Frame
3366 ((eq item 'line) (nth 1 (car bp)))
3367 ((eq item 'file) (nth 0 (car bp)))
3368 ;; idlwave-shell breakpoint data
3369 ((eq item 'data) (cdr (cdr bp)))
3370 ((eq item 'count) (nth 0 (cdr (cdr bp))))
3371 ((eq item 'cmd) (nth 1 (cdr (cdr bp))))
3372 ((eq item 'condition) (nth 2 (cdr (cdr bp))))
3373 ((eq item 'disabled) (nth 3 (cdr (cdr bp))))
3374 ;; IDL breakpoint info
3375 ((eq item 'module)
3376 (let ((module (nth 1 (car (cdr bp)))))
3377 (if (listp module) (car module) module)))
3378 ((eq item 'type)
3379 (let ((module (nth 1 (car (cdr bp)))))
3380 (if (listp module) (nth 1 module))))
3381 ;; index - default
3382 (t (nth 0 (car (cdr bp))))))
3383
3384 (defun idlwave-shell-filter-bp (&optional no-show)
3385 "Get the breakpoints from `idlwave-shell-command-output'.
3386 Create `idlwave-shell-bp-alist' updating breakpoint count and command
3387 data from previous breakpoint list. If NO-SHOW is set, don't update
3388 the breakpoint overlays."
3389 (with-current-buffer (get-buffer-create idlwave-shell-bp-buffer)
3390 (erase-buffer)
3391 (insert idlwave-shell-command-output)
3392 (goto-char (point-min))
3393 (let ((old-bp-alist idlwave-shell-bp-alist)
3394 ;; Searching the breakpoints
3395 ;; In IDL 5.5, the breakpoint reporting format changed.
3396 (bp-re54 "^[ \t]*\\([0-9]+\\)[ \t]+\\(\\S-+\\)?[ \t]+\\([0-9]+\\)[ \t]+\\(\\S-+\\)")
3397 (bp-re55
3398 (concat
3399 "^\\s-*\\([0-9]+\\)" ; 1 index
3400 "\\s-+\\([0-9]+\\)" ; 2 line number
3401 "\\s-+\\(Uncompiled\\|" ; 3-6 either uncompiled or routine name
3402 "\\(\\(Func=\\|Pro=\\)\\(\\$?[a-zA-Z][a-zA-Z0-9$_:]*\\$?\\)\\)\\)"
3403 "\\(\\s-*,\\s-*After=[0-9]+/\\([0-9]+\\)?\\)?" ; 7-8 After part
3404 "\\(\\s-*,\\s-*\\(BreakOnce\\)\\)?" ; 9-10 BreakOnce
3405 "\\(\\s-*,\\s-*\\(Condition='\\(.*\\)'\\)\n?\\)?" ; 11-13 Condition
3406 "\\(\\s-*,\\s-*\\(Disabled\\)\n?\\)?" ; 14-15 Disabled
3407 "\\s-+\\(\\S-+\\)")) ; 16 File name
3408 file line index module
3409 count condition disabled
3410 bp-re indmap)
3411 (setq idlwave-shell-bp-alist (list nil))
3412 ;; Search for either header type, and set the correct regexp
3413 (when (or
3414 (if (re-search-forward "^\\s-*Index.*\n\\s-*-" nil t)
3415 (setq bp-re bp-re54 ; versions <= 5.4
3416 indmap '(1 2 3 4))) ;index module line file
3417 (if (re-search-forward
3418 "^\\s-*Index\\s-*Line\\s-*Attributes\\s-*File" nil t)
3419 (setq bp-re bp-re55 ; versions >= 5.5
3420 indmap '(1 6 2 16)))) ; index module line file
3421 ;; There seems to be a breakpoint listing here, parse breakpoint lines.
3422 (while (re-search-forward bp-re nil t)
3423 (setq index (string-to-number (match-string (nth 0 indmap)))
3424 module (match-string (nth 1 indmap))
3425 line (string-to-number (match-string (nth 2 indmap)))
3426 file (idlwave-shell-file-name (match-string (nth 3 indmap))))
3427 (if (eq bp-re bp-re55)
3428 (setq count (if (match-string 10) 1
3429 (if (match-string 8)
3430 (string-to-number (match-string 8))))
3431 condition (match-string 13)
3432 disabled (not (null (match-string 15)))))
3433
3434 ;; Add the breakpoint info to the list
3435 (nconc idlwave-shell-bp-alist
3436 (list (cons (list file line)
3437 (list
3438 (list index module)
3439 ;; bp data: count, command, condition, disabled
3440 count nil condition disabled))))))
3441 (setq idlwave-shell-bp-alist (cdr idlwave-shell-bp-alist))
3442 ;; Update breakpoint data
3443 (if (eq bp-re bp-re54)
3444 (mapc 'idlwave-shell-update-bp old-bp-alist)
3445 (mapc 'idlwave-shell-update-bp-command-only old-bp-alist))))
3446 ;; Update the breakpoint overlays
3447 (unless no-show (idlwave-shell-update-bp-overlays))
3448 ;; Return the new list
3449 idlwave-shell-bp-alist)
3450
3451 (defun idlwave-shell-update-bp-command-only (bp)
3452 (idlwave-shell-update-bp bp t))
3453
3454 (defun idlwave-shell-update-bp (bp &optional command-only)
3455 "Update BP data in breakpoint list.
3456 If BP frame is in `idlwave-shell-bp-alist' updates the breakpoint data."
3457 (let ((match (assoc (car bp) idlwave-shell-bp-alist)))
3458 (if match
3459 (if command-only
3460 (setf (nth 1 (cdr (cdr match))) (nth 1 (cdr (cdr match))))
3461 (setcdr (cdr match) (cdr (cdr bp)))))))
3462
3463 (defun idlwave-shell-set-bp-data (bp data)
3464 "Set the data of BP to DATA."
3465 (setcdr (cdr bp) data))
3466
3467 (defun idlwave-shell-bp (frame &optional data module)
3468 "Create a breakpoint structure containing FRAME and DATA.
3469 Second and third args, DATA and MODULE, are optional. Returns
3470 a breakpoint of the format used in `idlwave-shell-bp-alist'.
3471 Can be used in commands attempting match a breakpoint in
3472 `idlwave-shell-bp-alist'."
3473 (cons frame ;; (file line)
3474 (cons (list nil module) ;; (index_id (module type) | module)
3475 data))) ;; (count command condition disabled)
3476
3477 (defvar idlwave-shell-old-bp nil
3478 "List of breakpoints previous to setting a new breakpoint.")
3479
3480 (defun idlwave-shell-sources-bp (bp)
3481 "Check `idlwave-shell-sources-alist' for source of breakpoint using BP.
3482 If an equivalency is found, return the IDL internal source name.
3483 Otherwise return the filename in BP."
3484 (let*
3485 ((bp-file (idlwave-shell-bp-get bp 'file))
3486 (bp-module (idlwave-shell-bp-get bp 'module))
3487 (internal-file-list
3488 (if bp-module
3489 (cdr (assoc bp-module idlwave-shell-sources-alist)))))
3490 (if (and internal-file-list
3491 (equal bp-file (nth 0 internal-file-list)))
3492 (nth 1 internal-file-list)
3493 bp-file)))
3494
3495 (defun idlwave-shell-set-bp (bp &optional no-show)
3496 "Try to set a breakpoint BP.
3497 The breakpoint will be placed at the beginning of the statement on the
3498 line specified by BP or at the next IDL statement if that line is not
3499 a statement. Determines IDL's internal representation for the
3500 breakpoint, which may have occurred at a different line than
3501 specified. If NO-SHOW is non-nil, don't do any updating."
3502 ;; Get and save the old breakpoints
3503 (idlwave-shell-send-command
3504 idlwave-shell-bp-query
3505 `(progn
3506 (idlwave-shell-filter-bp (quote ,no-show))
3507 (setq idlwave-shell-old-bp idlwave-shell-bp-alist))
3508 'hide)
3509
3510 ;; Get sources for this routine in the sources list
3511 (idlwave-shell-module-source-query (idlwave-shell-bp-get bp 'module)
3512 (idlwave-shell-bp-get bp 'type))
3513 (let*
3514 ((count (idlwave-shell-bp-get bp 'count))
3515 (condition (idlwave-shell-bp-get bp 'condition))
3516 (disabled (idlwave-shell-bp-get bp 'disabled))
3517 (key (concat (if (and count (numberp count))
3518 (cond
3519 ((= count 1) ",/once")
3520 ((> count 1) (format ",after=%d" count))))
3521 (if condition (concat ",CONDITION=\"" condition "\""))
3522 ;; IDL can't simultaneously set a condition/count
3523 ;; and disable a breakpoint, but it does keep both
3524 ;; of these when resetting the same BP. We assume
3525 ;; DISABLE and CONDITION/COUNT are not set
3526 ;; together for a newly created breakpoint.
3527 (if (and disabled (not condition) (not count))
3528 ",/DISABLE")))
3529 (line (idlwave-shell-bp-get bp 'line)))
3530 (idlwave-shell-send-command
3531 (concat "breakpoint,'"
3532 (idlwave-shell-sources-bp bp) "',"
3533 (if (integerp line) (setq line (int-to-string line)))
3534 key)
3535 ;; Check for failure and adjust breakpoint to match IDL's list
3536 `(progn
3537 (if (idlwave-shell-set-bp-check (quote ,bp))
3538 (idlwave-shell-set-bp-adjust (quote ,bp) (quote ,no-show))))
3539 ;; hide output?
3540 (idlwave-shell-hide-p 'breakpoint)
3541 'preempt t)))
3542
3543 (defun idlwave-shell-set-bp-adjust (bp &optional no-show)
3544 "Find the breakpoint in IDL's internal list of breakpoints."
3545 (idlwave-shell-send-command
3546 idlwave-shell-bp-query
3547 `(progn
3548 (idlwave-shell-filter-bp 'no-show)
3549 (idlwave-shell-new-bp (quote ,bp))
3550 (unless (quote ,no-show)
3551 (idlwave-shell-update-bp-overlays)))
3552 'hide
3553 'preempt))
3554
3555 (defun idlwave-shell-find-bp (frame)
3556 "Return breakpoint from `idlwave-shell-bp-alist' for frame.
3557 Returns nil if frame not found."
3558 (assoc frame idlwave-shell-bp-alist))
3559
3560 (defun idlwave-shell-find-current-bp ()
3561 "Find breakpoint here, or at halt location."
3562 (let ((bp (idlwave-shell-find-bp (idlwave-shell-current-frame))))
3563 (when (not bp)
3564 ;; Try moving to beginning of halted-at statement
3565 (save-excursion
3566 (idlwave-shell-goto-frame)
3567 (idlwave-beginning-of-statement)
3568 (setq bp (idlwave-shell-find-bp (idlwave-shell-current-frame))))
3569 (unless bp
3570 (beep)
3571 (message "Cannot identify breakpoint for this line")))
3572 bp))
3573
3574 (defun idlwave-shell-new-bp (bp)
3575 "Find the new breakpoint in IDL's list and update with DATA.
3576 The actual line number for a breakpoint in IDL may be different than
3577 the line number used with the IDL breakpoint command.
3578 Looks for a new breakpoint index number in the list. This is
3579 considered the new breakpoint if the file name of frame matches."
3580 (let ((obp-index (mapcar 'idlwave-shell-bp-get idlwave-shell-old-bp))
3581 (bpl idlwave-shell-bp-alist))
3582 (while (and (member (idlwave-shell-bp-get (car bpl)) obp-index)
3583 (setq bpl (cdr bpl))))
3584 (if (and
3585 (not bpl)
3586 ;; No additional breakpoint.
3587 ;; Need to check if we are just replacing a breakpoint.
3588 (setq bpl (assoc (car bp) idlwave-shell-bp-alist)))
3589 (setq bpl (list bpl)))
3590 (if (and bpl
3591 (equal (idlwave-shell-bp-get (setq bpl (car bpl)) 'file)
3592 (idlwave-shell-bp-get bp 'file)))
3593 ;; Got the breakpoint - add count, command to it.
3594 ;; This updates `idlwave-shell-bp-alist' because a deep copy was
3595 ;; not done for bpl.
3596 (idlwave-shell-set-bp-data bpl (idlwave-shell-bp-get bp 'data))
3597 (beep)
3598 (message "Failed to identify breakpoint in IDL"))))
3599
3600 (defvar idlwave-shell-bp-overlays nil
3601 "Alist of overlays marking breakpoints.")
3602 (defvar idlwave-shell-bp-glyph)
3603
3604 (defvar idlwave-shell-debug-line-map (make-sparse-keymap))
3605 (define-key idlwave-shell-debug-line-map
3606 (if (featurep 'xemacs) [button3] [mouse-3])
3607 'idlwave-shell-mouse-active-bp)
3608
3609 (defun idlwave-shell-update-bp-overlays ()
3610 "Update the overlays which mark breakpoints in the source code.
3611 Existing overlays are recycled, in order to minimize consumption."
3612 (when idlwave-shell-mark-breakpoints
3613 (let ((ov-alist (copy-alist idlwave-shell-bp-overlays))
3614 (bp-list idlwave-shell-bp-alist)
3615 (use-glyph (and (memq idlwave-shell-mark-breakpoints '(t glyph))
3616 idlwave-shell-bp-glyph))
3617 ov ov-list bp buf old-buffers win)
3618
3619 ;; Delete the old overlays from their buffers
3620 (if ov-alist
3621 (while (setq ov-list (pop ov-alist))
3622 (while (setq ov (pop (cdr ov-list)))
3623 (add-to-list 'old-buffers (overlay-buffer ov))
3624 (delete-overlay ov))))
3625
3626 (setq ov-alist idlwave-shell-bp-overlays
3627 idlwave-shell-bp-overlays
3628 (if idlwave-shell-bp-glyph
3629 (mapcar 'list (mapcar 'car idlwave-shell-bp-glyph))
3630 (list (list 'bp))))
3631 (while (setq bp (pop bp-list))
3632 (save-excursion
3633 (idlwave-shell-goto-frame (car bp))
3634 (let* ((end (point-at-eol))
3635 (beg (progn (beginning-of-line 1) (point)))
3636 (condition (idlwave-shell-bp-get bp 'condition))
3637 (count (idlwave-shell-bp-get bp 'count))
3638 (disabled (idlwave-shell-bp-get bp 'disabled))
3639 (type (if idlwave-shell-bp-glyph
3640 (cond
3641 (condition 'bp-cond )
3642 (count
3643 (cond
3644 ((<= count 0) 'bp)
3645 ((<= count 4)
3646 (intern
3647 (concat "bp-" (number-to-string count))))
3648 (t 'bp-n)))
3649 (t 'bp))
3650 'bp))
3651 (help-list
3652 (delq nil
3653 (list
3654 (if count
3655 (concat "after:" (int-to-string count)))
3656 (if condition
3657 (concat "condition:" condition))
3658 (if disabled "disabled"))))
3659 (help-text (concat
3660 "BP "
3661 (int-to-string (idlwave-shell-bp-get bp))
3662 (if help-list
3663 (concat
3664 " - "
3665 (mapconcat 'identity help-list ", ")))
3666 (if (and (not count) (not condition))
3667 " (use mouse-3 for breakpoint actions)")))
3668 (full-type (if disabled
3669 (intern (concat (symbol-name type)
3670 "-disabled"))
3671 type))
3672 (ov-existing (assq full-type ov-alist))
3673 (ov (or (and (cdr ov-existing)
3674 (pop (cdr ov-existing)))
3675 (idlwave-shell-make-new-bp-overlay type disabled)))
3676 match)
3677 (if idlwave-shell-breakpoint-popup-menu
3678 (overlay-put ov 'help-echo help-text))
3679 (move-overlay ov beg end)
3680 (if (setq match (assq full-type idlwave-shell-bp-overlays))
3681 (push ov (cdr match))
3682 (nconc idlwave-shell-bp-overlays
3683 (list (list full-type ov)))))
3684 ;; Take care of margins if using a glyph
3685 (when use-glyph
3686 (if old-buffers
3687 (setq old-buffers (delq (current-buffer) old-buffers)))
3688 (if (fboundp 'set-specifier) ;; XEmacs
3689 (set-specifier left-margin-width (cons (current-buffer) 2))
3690 (if (< left-margin-width 2)
3691 (setq left-margin-width 2)))
3692 (let ((window (get-buffer-window (current-buffer) 0)))
3693 (if window
3694 (set-window-margins
3695 window left-margin-width right-margin-width))))))
3696 (if use-glyph
3697 (while (setq buf (pop old-buffers))
3698 (with-current-buffer buf
3699 (if (fboundp 'set-specifier) ;; XEmacs
3700 (set-specifier left-margin-width (cons (current-buffer) 0))
3701 (setq left-margin-width 0))
3702 (let ((window (get-buffer-window buf 0)))
3703 (if window
3704 (set-window-margins
3705 window left-margin-width right-margin-width)))))))))
3706
3707 (defun idlwave-shell-make-new-bp-overlay (&optional type disabled)
3708 "Make a new overlay for highlighting breakpoints.
3709
3710 This stuff is strongly dependent upon the version of Emacs. If TYPE
3711 is passed, make an overlay of that type ('bp or 'bp-cond, currently
3712 only for glyphs)."
3713 (let ((ov (make-overlay 1 1))
3714 (use-glyph (and (memq idlwave-shell-mark-breakpoints '(t glyph))
3715 idlwave-shell-bp-glyph))
3716 (type (or type 'bp))
3717 (face (if disabled
3718 idlwave-shell-disabled-breakpoint-face
3719 idlwave-shell-breakpoint-face)))
3720 (if (featurep 'xemacs)
3721 ;; This is XEmacs
3722 (progn
3723 (when idlwave-shell-breakpoint-popup-menu
3724 (set-extent-property ov 'mouse-face 'highlight)
3725 (set-extent-property ov 'keymap idlwave-shell-debug-line-map))
3726
3727 (cond
3728 ;; tty's cannot display glyphs
3729 ((eq (console-type) 'tty)
3730 (set-extent-property ov 'face face))
3731
3732 ;; use the glyph
3733 (use-glyph
3734 (let ((glyph (cdr (assq type idlwave-shell-bp-glyph))))
3735 (if disabled (setq glyph (car glyph)) (setq glyph (nth 1 glyph)))
3736 (set-extent-property ov 'begin-glyph glyph)
3737 (set-extent-property ov 'begin-glyph-layout 'outside-margin)))
3738
3739 ;; use the face
3740 (idlwave-shell-mark-breakpoints
3741 (set-extent-property ov 'face face))
3742
3743 ;; no marking
3744 (t nil))
3745 (set-extent-priority ov -1)) ; make stop line face prevail
3746 ;; This is Emacs
3747 (when idlwave-shell-breakpoint-popup-menu
3748 (overlay-put ov 'mouse-face 'highlight)
3749 (overlay-put ov 'keymap idlwave-shell-debug-line-map))
3750 (cond
3751 (window-system
3752 (if use-glyph
3753 (let ((image-props (cdr (assq type idlwave-shell-bp-glyph)))
3754 string)
3755
3756 (if disabled (setq image-props
3757 (append image-props
3758 (list :conversion 'disabled))))
3759 (setq string
3760 (propertize "@"
3761 'display
3762 (list (list 'margin 'left-margin)
3763 image-props)))
3764 (overlay-put ov 'before-string string))
3765 ;; just the face
3766 (overlay-put ov 'face face)))
3767
3768 ;; use a face
3769 (idlwave-shell-mark-breakpoints
3770 (overlay-put ov 'face face))
3771
3772 ;; No marking
3773 (t nil)))
3774 ov))
3775
3776 (defun idlwave-shell-mouse-active-bp (ev)
3777 "Does right-click mouse action on breakpoint lines."
3778 (interactive "e")
3779 (if ev (mouse-set-point ev))
3780 (let ((bp (idlwave-shell-find-bp (idlwave-shell-current-frame)))
3781 index condition count select cmd disabled)
3782 (unless bp
3783 (error "Breakpoint not found"))
3784 (setq index (int-to-string (idlwave-shell-bp-get bp))
3785 condition (idlwave-shell-bp-get bp 'condition)
3786 cmd (idlwave-shell-bp-get bp 'cmd)
3787 count (idlwave-shell-bp-get bp 'count)
3788 disabled (idlwave-shell-bp-get bp 'disabled))
3789 (setq select (idlwave-popup-select
3790 ev
3791 (delq nil
3792 (list (if disabled "Enable" "Disable")
3793 "Clear"
3794 "Clear All"
3795 (if condition "Remove Condition" "Add Condition")
3796 (if condition "Change Condition")
3797 (if count "Remove Repeat Count"
3798 "Add Repeat Count")
3799 (if count "Change Repeat Count")))
3800 (concat "BreakPoint " index)))
3801 (if select
3802 (cond
3803 ((string-equal select "Clear All")
3804 (idlwave-shell-clear-all-bp))
3805 ((string-equal select "Clear")
3806 (idlwave-shell-clear-current-bp))
3807 ((string-match "Condition" select)
3808 (idlwave-shell-break-here count cmd
3809 (if (or (not condition)
3810 (string-match "Change" select))
3811 (read-string "Break Condition: "))
3812 disabled))
3813 ((string-match "Count" select)
3814 (idlwave-shell-break-here (if (or (not count)
3815 (string-match "Change" select))
3816 (string-to-number
3817 (read-string "Break After Count: ")))
3818 cmd condition disabled))
3819 ((string-match "able$" select)
3820 (idlwave-shell-toggle-enable-current-bp))
3821 (t
3822 (message "Unimplemented: %s" select))))))
3823
3824 (defun idlwave-shell-edit-default-command-line (arg)
3825 "Edit the current execute command."
3826 (interactive "P")
3827 (setq idlwave-shell-command-line-to-execute
3828 (read-string "IDL> " idlwave-shell-command-line-to-execute)))
3829
3830 (defun idlwave-shell-execute-default-command-line (arg)
3831 "Execute a command line. On first use, ask for the command.
3832 Also with prefix arg, ask for the command. You can also use the command
3833 `idlwave-shell-edit-default-command-line' to edit the line."
3834 (interactive "P")
3835 (cond
3836 ((equal arg '(16))
3837 (setq idlwave-shell-command-line-to-execute nil))
3838 ((equal arg '(4))
3839 (setq idlwave-shell-command-line-to-execute
3840 (read-string "IDL> " idlwave-shell-command-line-to-execute))))
3841 (idlwave-shell-reset 'hidden)
3842 (idlwave-shell-send-command
3843 (or idlwave-shell-command-line-to-execute
3844 (with-current-buffer (idlwave-shell-buffer)
3845 (ring-ref comint-input-ring 0)))
3846 '(idlwave-shell-redisplay 'hide)))
3847
3848 (defun idlwave-shell-save-and-run ()
3849 "Save file and run it in IDL.
3850 Runs `save-buffer' and sends a '.RUN' command for the associated file to IDL.
3851 When called from the shell buffer, re-run the file which was last handled by
3852 one of the save-and-.. commands."
3853 (interactive)
3854 (idlwave-shell-save-and-action 'run))
3855
3856 (defun idlwave-shell-save-and-compile ()
3857 "Save file and run it in IDL.
3858 Runs `save-buffer' and sends '.COMPILE' command for the associated file to IDL.
3859 When called from the shell buffer, re-compile the file which was last handled by
3860 one of the save-and-.. commands."
3861 (interactive)
3862 (idlwave-shell-save-and-action 'compile))
3863
3864 (defun idlwave-shell-save-and-batch ()
3865 "Save file and batch it in IDL.
3866 Runs `save-buffer' and sends a '@file' command for the associated file to IDL.
3867 When called from the shell buffer, re-batch the file which was last handled by
3868 one of the save-and-.. commands."
3869 (interactive)
3870 (idlwave-shell-save-and-action 'batch))
3871
3872 (defun idlwave-shell-save-and-action (action)
3873 "Save file and compile it in IDL.
3874 Runs `save-buffer' and sends a '.RUN' command for the associated file to IDL.
3875 When called from the shell buffer, re-compile the file which was last
3876 handled by this command."
3877 ;; Remove the stop overlay.
3878 (if idlwave-shell-stop-line-overlay
3879 (delete-overlay idlwave-shell-stop-line-overlay))
3880 (if idlwave-shell-is-stopped
3881 (idlwave-shell-electric-debug-all-off))
3882 (setq idlwave-shell-is-stopped nil)
3883 (setq overlay-arrow-string nil)
3884 (let (buf)
3885 (cond
3886 ((derived-mode-p 'idlwave-mode)
3887 (save-buffer)
3888 (setq idlwave-shell-last-save-and-action-file (buffer-file-name)))
3889 (idlwave-shell-last-save-and-action-file
3890 (if (setq buf (idlwave-get-buffer-visiting
3891 idlwave-shell-last-save-and-action-file))
3892 (with-current-buffer buf
3893 (save-buffer))))
3894 (t (setq idlwave-shell-last-save-and-action-file
3895 (read-file-name "File: ")))))
3896 (if (file-regular-p idlwave-shell-last-save-and-action-file)
3897 (progn
3898 (idlwave-shell-send-command
3899 (concat (cond ((eq action 'run) ".run ")
3900 ((eq action 'compile) ".compile ")
3901 ((eq action 'batch) "@")
3902 (t (error "Unknown action %s" action)))
3903 "\""
3904 idlwave-shell-last-save-and-action-file
3905 "\"")
3906 `(idlwave-shell-maybe-update-routine-info nil
3907 ,idlwave-shell-last-save-and-action-file)
3908 (if (idlwave-shell-hide-p 'run) 'mostly) nil t)
3909 (idlwave-shell-bp-query))
3910 (let ((msg (format "No such file %s"
3911 idlwave-shell-last-save-and-action-file)))
3912 (setq idlwave-shell-last-save-and-action-file nil)
3913 (error msg))))
3914
3915 (defun idlwave-shell-maybe-update-routine-info (&optional wait file)
3916 "Update the routine info if the shell is not stopped at an error."
3917 (if (and (not idlwave-shell-is-stopped)
3918 (or (eq t idlwave-auto-routine-info-updates)
3919 (memq 'compile-buffer idlwave-auto-routine-info-updates))
3920 idlwave-query-shell-for-routine-info
3921 idlwave-routines)
3922 (idlwave-shell-update-routine-info t nil wait file)))
3923
3924 (defvar idlwave-shell-sources-query "help,/source,/full"
3925 "IDL command to obtain source files for compiled procedures.")
3926
3927 (defvar idlwave-shell-sources-alist nil
3928 "Alist of IDL procedure names and compiled source files.
3929 Elements of the alist have the form:
3930
3931 (module name . (source-file-truename idlwave-internal-filename))")
3932
3933 (defun idlwave-shell-module-source-query (module &optional type)
3934 "Determine the source file for a given module.
3935 Query as a function if TYPE set to something beside 'pro."
3936 (if module
3937 (idlwave-shell-send-command
3938 (format "print,(routine_info('%s',/SOURCE%s)).PATH" module
3939 (if (eq type 'pro) "" ",/FUNCTIONS"))
3940 `(idlwave-shell-module-source-filter ,module)
3941 'hide 'wait)))
3942
3943 (defun idlwave-shell-module-source-filter (module)
3944 "Get module source, and update `idlwave-shell-sources-alist'."
3945 (let ((old (assoc (upcase module) idlwave-shell-sources-alist))
3946 filename)
3947 (when (string-match "\.PATH *[\n\r]\\([^%][^\r\n]+\\)[\n\r]"
3948 idlwave-shell-command-output)
3949 (setq filename (substring idlwave-shell-command-output
3950 (match-beginning 1) (match-end 1)))
3951 (if old
3952 (setcdr old (list (idlwave-shell-file-name filename) filename))
3953 (setq idlwave-shell-sources-alist
3954 (append idlwave-shell-sources-alist
3955 (list (cons (upcase module)
3956 (list (idlwave-shell-file-name filename)
3957 filename)))))))))
3958
3959 (defun idlwave-shell-sources-query ()
3960 "Determine source files for all IDL compiled procedures.
3961 Queries IDL using the string in `idlwave-shell-sources-query'."
3962 (interactive)
3963 (idlwave-shell-send-command idlwave-shell-sources-query
3964 'idlwave-shell-sources-filter
3965 'hide))
3966
3967 (defun idlwave-shell-sources-filter ()
3968 "Get source files from `idlwave-shell-sources-query' output.
3969 Create `idlwave-shell-sources-alist' consisting of list elements
3970 of the form:
3971 (module name . (source-file-truename idlwave-internal-filename))"
3972 (with-current-buffer (get-buffer-create idlwave-shell-bp-buffer)
3973 (erase-buffer)
3974 (insert idlwave-shell-command-output)
3975 (goto-char (point-min))
3976 (let (cpro cfun)
3977 (if (re-search-forward "Compiled Procedures:" nil t)
3978 (progn
3979 (forward-line) ; Skip $MAIN$
3980 (setq cpro (point))))
3981 (if (re-search-forward "Compiled Functions:" nil t)
3982 (progn
3983 (setq cfun (point))
3984 (setq idlwave-shell-sources-alist
3985 (append
3986 ;; compiled procedures
3987 (progn
3988 (narrow-to-region cpro (point-at-bol))
3989 (goto-char (point-min))
3990 (idlwave-shell-sources-grep))
3991 ;; compiled functions
3992 (progn
3993 (widen)
3994 (goto-char cfun)
3995 (idlwave-shell-sources-grep)))))))))
3996
3997 (defun idlwave-shell-sources-grep ()
3998 (save-excursion
3999 (let ((al (list nil)))
4000 (while (and
4001 (not (progn (forward-line) (eobp)))
4002 (re-search-forward
4003 "\\s-*\\(\\S-+\\)\\s-+\\(\\S-+\\)" nil t))
4004 (nconc al
4005 (list
4006 (cons
4007 (buffer-substring ; name
4008 (match-beginning 1) (match-end 1))
4009 (let ((internal-filename
4010 (buffer-substring ; source
4011 (match-beginning 2) (match-end 2))))
4012 (list
4013 (idlwave-shell-file-name internal-filename)
4014 internal-filename))
4015 ))))
4016 (cdr al))))
4017
4018 (defun idlwave-shell-clear-all-bp ()
4019 "Remove all breakpoints in IDL."
4020 (interactive)
4021 (idlwave-shell-send-command
4022 idlwave-shell-bp-query
4023 '(progn
4024 (idlwave-shell-filter-bp)
4025 (mapcar (lambda (x) (idlwave-shell-clear-bp x 'no-query))
4026 idlwave-shell-bp-alist)
4027 (idlwave-shell-bp-query))
4028 'hide))
4029
4030 (defun idlwave-shell-list-all-bp ()
4031 "List all breakpoints in IDL."
4032 (interactive)
4033 (idlwave-shell-send-command
4034 idlwave-shell-bp-query))
4035
4036 (defvar idlwave-shell-error-last 0
4037 "Position of last syntax error in `idlwave-shell-error-buffer'.")
4038
4039 (defun idlwave-shell-goto-next-error ()
4040 "Move point to next IDL syntax error."
4041 (interactive)
4042 (let (frame col)
4043 (with-current-buffer idlwave-shell-error-buffer
4044 (goto-char idlwave-shell-error-last)
4045 (if (or
4046 (re-search-forward idlwave-shell-syntax-error nil t)
4047 (re-search-forward idlwave-shell-other-error nil t))
4048 (progn
4049 (setq frame
4050 (list
4051 (save-match-data
4052 (idlwave-shell-file-name
4053 (buffer-substring (match-beginning 1 )
4054 (match-end 1))))
4055 (string-to-number
4056 (buffer-substring (match-beginning 2)
4057 (match-end 2)))))
4058 ;; Try to find the column of the error
4059 (save-excursion
4060 (setq col
4061 (if (re-search-backward "\\^" nil t)
4062 (current-column)
4063 0)))))
4064 (setq idlwave-shell-error-last (point)))
4065 (if frame
4066 (progn
4067 (idlwave-shell-display-line frame col 'disable))
4068 (beep)
4069 (message "No more errors."))))
4070
4071 (defun idlwave-shell-file-name (name)
4072 "If `idlwave-shell-use-truename' is non-nil, convert file name to true name.
4073 Otherwise, just expand the file name."
4074 (let ((def-dir (if (derived-mode-p 'idlwave-shell-mode)
4075 default-directory
4076 idlwave-shell-default-directory)))
4077 (if idlwave-shell-use-truename
4078 (file-truename name def-dir)
4079 (expand-file-name name def-dir))))
4080
4081 ;; Keybindings ------------------------------------------------------------
4082
4083 (defvar idlwave-shell-mode-map (copy-keymap comint-mode-map)
4084 "Keymap for `idlwave-mode'.")
4085 (defvar idlwave-shell-electric-debug-mode-map (make-sparse-keymap))
4086 (defvar idlwave-shell-mode-prefix-map (make-sparse-keymap))
4087 (fset 'idlwave-shell-mode-prefix-map idlwave-shell-mode-prefix-map)
4088 (defvar idlwave-mode-prefix-map (make-sparse-keymap))
4089 (fset 'idlwave-mode-prefix-map idlwave-mode-prefix-map)
4090
4091 (defun idlwave-shell-define-key-both (key hook)
4092 "Define a key in both the shell and buffer mode maps."
4093 (define-key idlwave-mode-map key hook)
4094 (define-key idlwave-shell-mode-map key hook))
4095
4096 ;(define-key idlwave-shell-mode-map "\M-?" 'comint-dynamic-list-completions)
4097 ;(define-key idlwave-shell-mode-map "\t" 'comint-dynamic-complete)
4098
4099 (define-key idlwave-shell-mode-map "\C-w" 'comint-kill-region)
4100 (define-key idlwave-shell-mode-map "\t" 'idlwave-shell-complete)
4101 (define-key idlwave-shell-mode-map "\M-\t" 'idlwave-shell-complete)
4102 (define-key idlwave-shell-mode-map "\C-c\C-s" 'idlwave-shell)
4103 (define-key idlwave-shell-mode-map "\C-c?" 'idlwave-routine-info)
4104 (define-key idlwave-shell-mode-map "\C-g" 'idlwave-keyboard-quit)
4105 (define-key idlwave-shell-mode-map "\M-?" 'idlwave-context-help)
4106 (define-key idlwave-shell-mode-map [(control meta ?\?)]
4107 'idlwave-help-assistant-help-with-topic)
4108 (define-key idlwave-shell-mode-map "\C-c\C-i" 'idlwave-update-routine-info)
4109 (define-key idlwave-shell-mode-map "\C-c\C-y" 'idlwave-shell-char-mode-loop)
4110 (define-key idlwave-shell-mode-map "\C-c\C-x" 'idlwave-shell-send-char)
4111 (define-key idlwave-shell-mode-map "\C-c=" 'idlwave-resolve)
4112 (define-key idlwave-shell-mode-map "\C-c\C-v" 'idlwave-find-module)
4113 (define-key idlwave-shell-mode-map "\C-c\C-k" 'idlwave-kill-autoloaded-buffers)
4114 (define-key idlwave-shell-mode-map idlwave-shell-prefix-key
4115 'idlwave-shell-debug-map)
4116 (define-key idlwave-shell-mode-map [(up)] 'idlwave-shell-up-or-history)
4117 (define-key idlwave-shell-mode-map [(down)] 'idlwave-shell-down-or-history)
4118 (define-key idlwave-mode-map "\C-c\C-y" 'idlwave-shell-char-mode-loop)
4119 (define-key idlwave-mode-map "\C-c\C-x" 'idlwave-shell-send-char)
4120
4121 ;; The mouse bindings for PRINT and HELP
4122 (idlwave-shell-define-key-both
4123 (if (featurep 'xemacs)
4124 [(shift button2)]
4125 [(shift down-mouse-2)])
4126 'idlwave-shell-mouse-print)
4127 (idlwave-shell-define-key-both
4128 (if (featurep 'xemacs)
4129 [(control meta button2)]
4130 [(control meta down-mouse-2)])
4131 'idlwave-shell-mouse-help)
4132 (idlwave-shell-define-key-both
4133 (if (featurep 'xemacs)
4134 [(control shift button2)]
4135 [(control shift down-mouse-2)])
4136 'idlwave-shell-examine-select)
4137 ;; Add this one from the idlwave-mode-map
4138 (define-key idlwave-shell-mode-map
4139 (if (featurep 'xemacs)
4140 [(shift button3)]
4141 [(shift mouse-3)])
4142 'idlwave-mouse-context-help)
4143
4144 ;; For Emacs, we need to turn off the button release events.
4145 (defun idlwave-shell-mouse-nop (event)
4146 (interactive "e"))
4147 (unless (featurep 'xemacs)
4148 (idlwave-shell-define-key-both
4149 [(shift mouse-2)] 'idlwave-shell-mouse-nop)
4150 (idlwave-shell-define-key-both
4151 [(shift control mouse-2)] 'idlwave-shell-mouse-nop)
4152 (idlwave-shell-define-key-both
4153 [(control meta mouse-2)] 'idlwave-shell-mouse-nop))
4154
4155
4156 ;; The following set of bindings is used to bind the debugging keys.
4157 ;; If `idlwave-shell-activate-prefix-keybindings' is non-nil, the
4158 ;; first key in the list gets bound the C-c C-d prefix map. If
4159 ;; `idlwave-shell-debug-modifiers' is non-nil, the second key in the
4160 ;; list gets bound with the specified modifiers in both
4161 ;; `idlwave-mode-map' and `idlwave-shell-mode-map'. The next list
4162 ;; item, if non-nil, means to bind this as a single key in the
4163 ;; electric-debug-mode-map.
4164 ;;
4165 ;; [C-c C-d]-binding debug-modifier-key command bind-electric-debug buf-only
4166 ;; Used keys: abcdef hijklmnopqrstuvwxyz
4167 ;; Unused keys: g
4168 (let* ((specs
4169 '(([(control ?b)] ?b idlwave-shell-break-here t t)
4170 ([(control ?i)] ?i idlwave-shell-break-in t t)
4171 ([(control ?j)] ?j idlwave-shell-break-this-module t t)
4172 ([(control ?d)] ?d idlwave-shell-clear-current-bp t)
4173 ([(control ?a)] ?a idlwave-shell-clear-all-bp t)
4174 ([(control ?\\)] ?\\ idlwave-shell-toggle-enable-current-bp t)
4175 ([(control ?s)] ?s idlwave-shell-step t)
4176 ([(control ?n)] ?n idlwave-shell-stepover t)
4177 ([(control ?k)] ?k idlwave-shell-skip t)
4178 ([(control ?u)] ?u idlwave-shell-up t)
4179 ([(control ?o)] ?o idlwave-shell-out t)
4180 ([(control ?m)] ?m idlwave-shell-return t)
4181 ([(control ?h)] ?h idlwave-shell-to-here t t)
4182 ([(control ?r)] ?r idlwave-shell-cont t)
4183 ([(control ?y)] ?y idlwave-shell-execute-default-command-line)
4184 ([(control ?z)] ?z idlwave-shell-reset t)
4185 ([(control ?q)] ?q idlwave-shell-quit)
4186 ([(control ?p)] ?p idlwave-shell-print t)
4187 ([( ??)] ?? idlwave-shell-help-expression t)
4188 ([(control ?v)] ?v idlwave-shell-toggle-electric-debug-mode t t)
4189 ([(control ?x)] ?x idlwave-shell-goto-next-error)
4190 ([(control ?c)] ?c idlwave-shell-save-and-run t)
4191 ([( ?@)] ?@ idlwave-shell-save-and-batch)
4192 ([(control ?e)] ?e idlwave-shell-run-region)
4193 ([(control ?w)] ?w idlwave-shell-resync-dirs)
4194 ([(control ?l)] ?l idlwave-shell-redisplay t)
4195 ([(control ?t)] ?t idlwave-shell-toggle-toolbar)
4196 ([(control up)] up idlwave-shell-stack-up)
4197 ([(control down)] down idlwave-shell-stack-down)
4198 ([( ?[)] ?[ idlwave-shell-goto-previous-bp t t)
4199 ([( ?])] ?] idlwave-shell-goto-next-bp t t)
4200 ([(control ?f)] ?f idlwave-shell-window)))
4201 (mod (cond ((and idlwave-shell-debug-modifiers
4202 (listp idlwave-shell-debug-modifiers)
4203 (not (equal '() idlwave-shell-debug-modifiers)))
4204 idlwave-shell-debug-modifiers)
4205 (idlwave-shell-activate-alt-keybindings
4206 '(alt))))
4207 (shift (memq 'shift mod))
4208 (mod-noshift (delete 'shift (copy-sequence mod)))
4209 s k1 c2 k2 cmd electric only-buffer cannotshift)
4210 (while (setq s (pop specs))
4211 (setq k1 (nth 0 s)
4212 c2 (nth 1 s)
4213 cmd (nth 2 s)
4214 electric (nth 3 s)
4215 only-buffer (nth 4 s)
4216 cannotshift (and shift (characterp c2) (eq c2 (upcase c2))))
4217
4218 ;; The regular prefix keymap.
4219 (when (and idlwave-shell-activate-prefix-keybindings k1)
4220 (unless only-buffer
4221 (define-key idlwave-shell-mode-prefix-map k1 cmd))
4222 (define-key idlwave-mode-prefix-map k1 cmd))
4223 ;; The debug modifier map
4224 (when (and mod window-system)
4225 (if (char-or-string-p c2)
4226 (setq k2 (vector (append mod-noshift
4227 (list (if shift (upcase c2) c2)))))
4228 (setq k2 (vector (append mod (list c2)))))
4229 (unless cannotshift
4230 (define-key idlwave-mode-map k2 cmd)
4231 (unless only-buffer (define-key idlwave-shell-mode-map k2 cmd))))
4232 ;; The electric debug single-keystroke map
4233 (if (and electric (char-or-string-p c2))
4234 (define-key idlwave-shell-electric-debug-mode-map (char-to-string c2)
4235 cmd))))
4236
4237 ;; A few extras in the electric debug map
4238 (define-key idlwave-shell-electric-debug-mode-map " " 'idlwave-shell-step)
4239 (define-key idlwave-shell-electric-debug-mode-map "+" 'idlwave-shell-stack-up)
4240 (define-key idlwave-shell-electric-debug-mode-map "=" 'idlwave-shell-stack-up)
4241 (define-key idlwave-shell-electric-debug-mode-map "-"
4242 'idlwave-shell-stack-down)
4243 (define-key idlwave-shell-electric-debug-mode-map "_"
4244 'idlwave-shell-stack-down)
4245 (define-key idlwave-shell-electric-debug-mode-map "e"
4246 (lambda () (interactive) (idlwave-shell-print '(16))))
4247 (define-key idlwave-shell-electric-debug-mode-map "q" 'idlwave-shell-retall)
4248 (define-key idlwave-shell-electric-debug-mode-map "t"
4249 (lambda () (interactive) (idlwave-shell-send-command "help,/TRACE")))
4250 (define-key idlwave-shell-electric-debug-mode-map [(control ??)]
4251 'idlwave-shell-electric-debug-help)
4252 (define-key idlwave-shell-electric-debug-mode-map "x"
4253 (lambda (arg) (interactive "P")
4254 (idlwave-shell-print arg nil nil t)))
4255
4256
4257 ; Enter the prefix map in two places.
4258 (fset 'idlwave-debug-map idlwave-mode-prefix-map)
4259 (fset 'idlwave-shell-debug-map idlwave-shell-mode-prefix-map)
4260
4261 ;; The Electric Debug Minor Mode --------------------------------------------
4262
4263 (defun idlwave-shell-toggle-electric-debug-mode ()
4264 "Toggle electric-debug-mode, suppressing re-entry into mode if turned off."
4265 (interactive)
4266 ;; If turning it off, make sure it stays off throughout the debug
4267 ;; session until we return or hit $MAIN$. Cancel this suppression
4268 ;; if it's explicitly turned on.
4269 (if idlwave-shell-electric-debug-mode
4270 (progn ;; Turn it off, and make sure it stays off.
4271 (setq idlwave-shell-suppress-electric-debug t)
4272 (idlwave-shell-electric-debug-mode 0))
4273 (setq idlwave-shell-suppress-electric-debug nil)
4274 (idlwave-shell-electric-debug-mode t)))
4275
4276 (defvar idlwave-shell-electric-debug-read-only)
4277 (defvar idlwave-shell-electric-debug-buffers nil)
4278
4279 (define-minor-mode idlwave-shell-electric-debug-mode
4280 "Toggle Idlwave Shell Electric Debug mode.
4281 With a prefix argument ARG, enable the mode if ARG is positive,
4282 and disable it otherwise. If called from Lisp, enable the mode
4283 if ARG is omitted or nil.
4284
4285 When Idlwave Shell Electric Debug mode is enabled, the Idlwave
4286 Shell debugging commands are available as single key sequences."
4287 nil " *Debugging*" idlwave-shell-electric-debug-mode-map)
4288
4289 (add-hook
4290 'idlwave-shell-electric-debug-mode-on-hook
4291 (lambda ()
4292 (set (make-local-variable 'idlwave-shell-electric-debug-read-only)
4293 buffer-read-only)
4294 (setq buffer-read-only t)
4295 (add-to-list 'idlwave-shell-electric-debug-buffers (current-buffer))
4296 (if idlwave-shell-stop-line-overlay
4297 (overlay-put idlwave-shell-stop-line-overlay 'face
4298 idlwave-shell-electric-stop-line-face))
4299 (if (facep 'fringe)
4300 (set-face-foreground 'fringe idlwave-shell-electric-stop-color
4301 (selected-frame)))))
4302
4303 (add-hook
4304 'idlwave-shell-electric-debug-mode-off-hook
4305 (lambda ()
4306 ;; Return to previous read-only state
4307 (setq buffer-read-only (if (boundp 'idlwave-shell-electric-debug-read-only)
4308 idlwave-shell-electric-debug-read-only))
4309 (setq idlwave-shell-electric-debug-buffers
4310 (delq (current-buffer) idlwave-shell-electric-debug-buffers))
4311 (if idlwave-shell-stop-line-overlay
4312 (overlay-put idlwave-shell-stop-line-overlay 'face
4313 idlwave-shell-stop-line-face)
4314 (if (facep 'fringe)
4315 (set-face-foreground 'fringe (face-foreground 'default))))))
4316
4317 ;; easy-mmode defines electric-debug-mode for us, so we need to advise it.
4318 (defadvice idlwave-shell-electric-debug-mode (after print-enter activate)
4319 "Print out an entrance message."
4320 (when idlwave-shell-electric-debug-mode
4321 (message
4322 "Electric Debugging mode entered. Press [C-?] for help, [q] to quit"))
4323 (force-mode-line-update))
4324
4325 ;; Turn it off in all relevant buffers
4326 (defvar idlwave-shell-electric-debug-buffers nil)
4327 (defun idlwave-shell-electric-debug-all-off ()
4328 (setq idlwave-shell-suppress-electric-debug nil)
4329 (let ((buffers idlwave-shell-electric-debug-buffers)
4330 buf)
4331 (save-excursion
4332 (while (setq buf (pop buffers))
4333 (when (buffer-live-p buf)
4334 (set-buffer buf)
4335 (when (and (derived-mode-p 'idlwave-mode)
4336 buffer-file-name
4337 idlwave-shell-electric-debug-mode)
4338 (idlwave-shell-electric-debug-mode 0))))))
4339 (setq idlwave-shell-electric-debug-buffers nil))
4340
4341 ;; Show the help text
4342 (defun idlwave-shell-electric-debug-help ()
4343 (interactive)
4344 (with-output-to-temp-buffer "*IDLWAVE Electric Debug Help*"
4345 (princ idlwave-shell-electric-debug-help))
4346 (let* ((current-window (selected-window))
4347 (window (get-buffer-window "*IDLWAVE Electric Debug Help*"))
4348 (window-lines (window-height window)))
4349 (select-window window)
4350 (enlarge-window (1+ (- (count-lines 1 (point-max)) window-lines)))
4351 (select-window current-window)))
4352
4353
4354 ;; The Menus --------------------------------------------------------------
4355 (defvar idlwave-shell-menu-def
4356 `("Debug"
4357 ["Electric Debug Mode"
4358 idlwave-shell-electric-debug-mode
4359 :style toggle :selected idlwave-shell-electric-debug-mode
4360 :included (derived-mode-p 'idlwave-mode) :keys "C-c C-d C-v"]
4361 "--"
4362 ("Compile & Run"
4363 ["Save and .RUN" idlwave-shell-save-and-run
4364 (or (derived-mode-p 'idlwave-mode)
4365 idlwave-shell-last-save-and-action-file)]
4366 ["Save and .COMPILE" idlwave-shell-save-and-compile
4367 (or (derived-mode-p 'idlwave-mode)
4368 idlwave-shell-last-save-and-action-file)]
4369 ["Save and @Batch" idlwave-shell-save-and-batch
4370 (or (derived-mode-p 'idlwave-mode)
4371 idlwave-shell-last-save-and-action-file)]
4372 "--"
4373 ["Goto Next Error" idlwave-shell-goto-next-error t]
4374 "--"
4375 ["Compile and Run Region" idlwave-shell-run-region
4376 (derived-mode-p 'idlwave-mode)]
4377 ["Evaluate Region" idlwave-shell-evaluate-region
4378 (derived-mode-p 'idlwave-mode)]
4379 "--"
4380 ["Execute Default Cmd" idlwave-shell-execute-default-command-line t]
4381 ["Edit Default Cmd" idlwave-shell-edit-default-command-line t])
4382 ("Breakpoints"
4383 ["Set Breakpoint" idlwave-shell-break-here
4384 :keys "C-c C-d C-b" :active (derived-mode-p 'idlwave-mode)]
4385 ("Set Special Breakpoint"
4386 ["Set After Count Breakpoint"
4387 (progn
4388 (let ((count (string-to-number (read-string "Break after count: "))))
4389 (if (integerp count) (idlwave-shell-break-here count))))
4390 :active (derived-mode-p 'idlwave-mode)]
4391 ["Set Condition Breakpoint"
4392 (idlwave-shell-break-here '(4))
4393 :active (derived-mode-p 'idlwave-mode)])
4394 ["Break in Module" idlwave-shell-break-in
4395 :keys "C-c C-d C-i" :active (derived-mode-p 'idlwave-mode)]
4396 ["Break in this Module" idlwave-shell-break-this-module
4397 :keys "C-c C-d C-j" :active (derived-mode-p 'idlwave-mode)]
4398 ["Clear Breakpoint" idlwave-shell-clear-current-bp t]
4399 ["Clear All Breakpoints" idlwave-shell-clear-all-bp t]
4400 ["Disable/Enable Breakpoint" idlwave-shell-toggle-enable-current-bp t]
4401 ["Goto Previous Breakpoint" idlwave-shell-goto-previous-bp
4402 :keys "C-c C-d [" :active (derived-mode-p 'idlwave-mode)]
4403 ["Goto Next Breakpoint" idlwave-shell-goto-next-bp
4404 :keys "C-c C-d ]" :active (derived-mode-p 'idlwave-mode)]
4405 ["List All Breakpoints" idlwave-shell-list-all-bp t]
4406 ["Resync Breakpoints" idlwave-shell-bp-query t])
4407 ("Continue/Step"
4408 ["Step (into)" idlwave-shell-step t]
4409 ["Step (over)" idlwave-shell-stepover t]
4410 ["Skip One Statement" idlwave-shell-skip t]
4411 ["Continue" idlwave-shell-cont t]
4412 ["... to End of Block" idlwave-shell-up t]
4413 ["... to End of Subprog" idlwave-shell-return t]
4414 ["... to End of Subprog+1" idlwave-shell-out t]
4415 ["... to Here (Cursor Line)" idlwave-shell-to-here
4416 :keys "C-c C-d C-h" :active (derived-mode-p 'idlwave-mode)])
4417 ("Examine Expressions"
4418 ["Print expression" idlwave-shell-print t]
4419 ["Help on expression" idlwave-shell-help-expression t]
4420 ("Examine nearby expression with"
4421 ,@(mapcar (lambda(x)
4422 `[ ,(car x) (idlwave-shell-print nil ',x) t ])
4423 idlwave-shell-examine-alist))
4424 ("Examine region with"
4425 ,@(mapcar (lambda(x)
4426 `[ ,(car x) (idlwave-shell-print '(4) ',x) t ])
4427 idlwave-shell-examine-alist)))
4428 ("Call Stack"
4429 ["Stack Up" idlwave-shell-stack-up t]
4430 ["Stack Down" idlwave-shell-stack-down t]
4431 "--"
4432 ["Redisplay and Sync" idlwave-shell-redisplay t])
4433 ("Show Commands"
4434 ["Everything" (if (eq idlwave-shell-show-commands 'everything)
4435 (progn
4436 (setq idlwave-shell-show-commands
4437 (get 'idlwave-shell-show-commands 'last-val))
4438 (put 'idlwave-shell-show-commands 'last-val nil))
4439 (put 'idlwave-shell-show-commands 'last-val
4440 idlwave-shell-show-commands)
4441 (setq idlwave-shell-show-commands 'everything))
4442 :style toggle :selected (and (not (listp idlwave-shell-show-commands))
4443 (eq idlwave-shell-show-commands
4444 'everything))]
4445 "--"
4446 ["Compiling Commands" (idlwave-shell-add-or-remove-show 'run)
4447 :style toggle
4448 :selected (not (idlwave-shell-hide-p
4449 'run
4450 (get 'idlwave-shell-show-commands 'last-val)))
4451 :active (not (eq idlwave-shell-show-commands 'everything))]
4452 ["Breakpoint Commands" (idlwave-shell-add-or-remove-show 'breakpoint)
4453 :style toggle
4454 :selected (not (idlwave-shell-hide-p
4455 'breakpoint
4456 (get 'idlwave-shell-show-commands 'last-val)))
4457 :active (not (eq idlwave-shell-show-commands 'everything))]
4458 ["Debug Commands" (idlwave-shell-add-or-remove-show 'debug)
4459 :style toggle
4460 :selected (not (idlwave-shell-hide-p
4461 'debug
4462 (get 'idlwave-shell-show-commands 'last-val)))
4463 :active (not (eq idlwave-shell-show-commands 'everything))]
4464 ["Miscellaneous Commands" (idlwave-shell-add-or-remove-show 'misc)
4465 :style toggle
4466 :selected (not (idlwave-shell-hide-p
4467 'misc
4468 (get 'idlwave-shell-show-commands 'last-val)))
4469 :active (not (eq idlwave-shell-show-commands 'everything))])
4470 ("Input Mode"
4471 ["Send one char" idlwave-shell-send-char t]
4472 ["Temporary Character Mode" idlwave-shell-char-mode-loop t]
4473 "--"
4474 ["Use Input Mode Magic"
4475 (setq idlwave-shell-use-input-mode-magic
4476 (not idlwave-shell-use-input-mode-magic))
4477 :style toggle :selected idlwave-shell-use-input-mode-magic])
4478 "--"
4479 ["Update Working Dir" idlwave-shell-resync-dirs t]
4480 ["Save Path Info"
4481 (idlwave-shell-send-command idlwave-shell-path-query
4482 'idlwave-shell-get-path-info
4483 'hide)
4484 t]
4485 ["Reset IDL" idlwave-shell-reset t]
4486 "--"
4487 ["Toggle Toolbar" idlwave-shell-toggle-toolbar t]
4488 ["Exit IDL" idlwave-shell-quit t]))
4489
4490 (if (or (featurep 'easymenu) (load "easymenu" t))
4491 (progn
4492 (easy-menu-define
4493 idlwave-mode-debug-menu idlwave-mode-map "IDL debugging menus"
4494 idlwave-shell-menu-def)
4495 (easy-menu-define
4496 idlwave-shell-mode-menu idlwave-shell-mode-map "IDL shell menus"
4497 idlwave-shell-menu-def)
4498 (save-current-buffer
4499 (dolist (buf (buffer-list))
4500 (set-buffer buf)
4501 (if (derived-mode-p 'idlwave-mode)
4502 (progn
4503 (easy-menu-remove idlwave-mode-debug-menu)
4504 (easy-menu-add idlwave-mode-debug-menu)))))))
4505
4506 ;; The Breakpoint Glyph -------------------------------------------------------
4507
4508 (defvar idlwave-shell-bp-glyph nil
4509 "The glyphs to mark breakpoint lines in the source code.")
4510
4511 (let ((image-alist
4512 '((bp . "/* XPM */
4513 static char * file[] = {
4514 \"14 12 3 1\",
4515 \" c None s backgroundColor\",
4516 \". c #4B4B4B4B4B4B\",
4517 \"R c #FFFF00000000\",
4518 \" \",
4519 \" .... \",
4520 \" .RRRR. \",
4521 \" .RRRRRR. \",
4522 \" .RRRRRRRR. \",
4523 \" .RRRRRRRR. \",
4524 \" .RRRRRRRR. \",
4525 \" .RRRRRRRR. \",
4526 \" .RRRRRR. \",
4527 \" .RRRR. \",
4528 \" .... \",
4529 \" \"};")
4530 (bp-cond . "/* XPM */
4531 static char * file[] = {
4532 \"14 12 4 1\",
4533 \" c None s backgroundColor\",
4534 \". c #4B4B4B4B4B4B\",
4535 \"R c #FFFF00000000\",
4536 \"B c #000000000000\",
4537 \" \",
4538 \" .... \",
4539 \" .RRRR. \",
4540 \" .RRRRRR. \",
4541 \" .RRRRRRRR. \",
4542 \" .RRBBBBRR. \",
4543 \" .RRRRRRRR. \",
4544 \" .RRBBBBRR. \",
4545 \" .RRRRRR. \",
4546 \" .RRRR. \",
4547 \" .... \",
4548 \" \"};")
4549 (bp-1 . "/* XPM */
4550 static char * file[] = {
4551 \"14 12 4 1\",
4552 \" c None s backgroundColor\",
4553 \". c #4B4B4B4B4B4B\",
4554 \"X c #FFFF00000000\",
4555 \"o c #000000000000\",
4556 \" \",
4557 \" .... \",
4558 \" .XXXX. \",
4559 \" .XXooXX. \",
4560 \" .XXoooXXX. \",
4561 \" .XXXooXXX. \",
4562 \" .XXXooXXX. \",
4563 \" .XXooooXX. \",
4564 \" .XooooX. \",
4565 \" .XXXX. \",
4566 \" .... \",
4567 \" \"};")
4568 (bp-2 . "/* XPM */
4569 static char * file[] = {
4570 \"14 12 4 1\",
4571 \" c None s backgroundColor\",
4572 \". c #4B4B4B4B4B4B\",
4573 \"X c #FFFF00000000\",
4574 \"o c #000000000000\",
4575 \" \",
4576 \" .... \",
4577 \" .XXXX. \",
4578 \" .XoooXX. \",
4579 \" .XXoXooXX. \",
4580 \" .XXXXooXX. \",
4581 \" .XXXooXXX. \",
4582 \" .XXooXXXX. \",
4583 \" .XooooX. \",
4584 \" .XXXX. \",
4585 \" .... \",
4586 \" \"};")
4587 (bp-3 . "/* XPM */
4588 static char * file[] = {
4589 \"14 12 4 1\",
4590 \" c None s backgroundColor\",
4591 \". c #4B4B4B4B4B4B\",
4592 \"X c #FFFF00000000\",
4593 \"o c #000000000000\",
4594 \" \",
4595 \" .... \",
4596 \" .XXXX. \",
4597 \" .XoooXX. \",
4598 \" .XXXXooXX. \",
4599 \" .XXXooXXX. \",
4600 \" .XXXXooXX. \",
4601 \" .XXoXooXX. \",
4602 \" .XoooXX. \",
4603 \" .XXXX. \",
4604 \" .... \",
4605 \" \"};")
4606 (bp-4 . "/* XPM */
4607 static char * file[] = {
4608 \"14 12 4 1\",
4609 \" c None s backgroundColor\",
4610 \". c #4B4B4B4B4B4B\",
4611 \"X c #FFFF00000000\",
4612 \"o c #000000000000\",
4613 \" \",
4614 \" .... \",
4615 \" .XXXX. \",
4616 \" .XoXXoX. \",
4617 \" .XXoXXoXX. \",
4618 \" .XXooooXX. \",
4619 \" .XXXXooXX. \",
4620 \" .XXXXooXX. \",
4621 \" .XXXooX. \",
4622 \" .XXXX. \",
4623 \" .... \",
4624 \" \"};")
4625 (bp-n . "/* XPM */
4626 static char * file[] = {
4627 \"14 12 4 1\",
4628 \" c None s backgroundColor\",
4629 \". c #4B4B4B4B4B4B\",
4630 \"X c #FFFF00000000\",
4631 \"o c #000000000000\",
4632 \" \",
4633 \" .... \",
4634 \" .XXXX. \",
4635 \" .XXXXXX. \",
4636 \" .XXoXoXXX. \",
4637 \" .XXooXoXX. \",
4638 \" .XXoXXoXX. \",
4639 \" .XXoXXoXX. \",
4640 \" .XoXXoX. \",
4641 \" .XXXX. \",
4642 \" .... \",
4643 \" \"};"))) im-cons im)
4644
4645 (while (setq im-cons (pop image-alist))
4646 (setq im (cond ((and (featurep 'xemacs)
4647 (featurep 'xpm))
4648 (list
4649 (let ((data (cdr im-cons)))
4650 (string-match "#FFFF00000000" data)
4651 (setq data (replace-match "#8F8F8F8F8F8F" t t data))
4652 (make-glyph data))
4653 (make-glyph (cdr im-cons))))
4654 ((and (not (featurep 'xemacs))
4655 (fboundp 'image-type-available-p)
4656 (image-type-available-p 'xpm))
4657 (list 'image :type 'xpm :data (cdr im-cons)
4658 :ascent 'center))
4659 (t nil)))
4660 (if im (push (cons (car im-cons) im) idlwave-shell-bp-glyph))))
4661
4662 (provide 'idlw-shell)
4663 (provide 'idlwave-shell)
4664
4665 ;; Load the toolbar when wanted by the user.
4666
4667 (autoload 'idlwave-toolbar-toggle "idlw-toolbar"
4668 "Toggle the IDLWAVE toolbar.")
4669 (autoload 'idlwave-toolbar-add-everywhere "idlw-toolbar"
4670 "Add IDLWAVE toolbar.")
4671 (defun idlwave-shell-toggle-toolbar ()
4672 "Toggle the display of the debugging toolbar."
4673 (interactive)
4674 (idlwave-toolbar-toggle))
4675
4676 (if idlwave-shell-use-toolbar
4677 (add-hook 'idlwave-shell-mode-hook 'idlwave-toolbar-add-everywhere))
4678
4679 ;;; idlw-shell.el ends here