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