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