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