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