Merge from trunk
[bpt/emacs.git] / lisp / progmodes / idlw-shell.el
1 ;; idlw-shell.el --- run IDL as an inferior process of Emacs.
2
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4 ;; 2009, 2010 Free Software Foundation, Inc.
5
6 ;; Authors: J.D. Smith <jdsmith@as.arizona.edu>
7 ;; Carsten Dominik <dominik@astro.uva.nl>
8 ;; Chris Chase <chase@att.com>
9 ;; Maintainer: J.D. Smith <jdsmith@as.arizona.edu>
10 ;; Version: 6.1.22
11 ;; Keywords: processes
12 ;; Package: idlwave
13
14 ;; This file is part of GNU Emacs.
15
16 ;; GNU Emacs is free software: you can redistribute it and/or modify
17 ;; it under the terms of the GNU General Public License as published by
18 ;; the Free Software Foundation, either version 3 of the License, or
19 ;; (at your option) any later version.
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
27 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28
29 ;;; Commentary:
30 ;;
31 ;; This mode is for IDL version 5 or later. It should work on
32 ;; Emacs>20.3 or XEmacs>20.4.
33 ;;
34 ;; Runs IDL as an inferior process of Emacs, much like the Emacs
35 ;; `shell' or `telnet' commands. Provides command history and
36 ;; searching. Provides debugging commands available in buffers
37 ;; visiting IDL procedure files, e.g., breakpoint setting, stepping,
38 ;; execution until a certain line, printing expressions under point,
39 ;; visual line pointer for current execution line, etc.
40 ;;
41 ;; Documentation should be available online with `M-x idlwave-info'.
42 ;;
43 ;; New versions of IDLWAVE, documentation, and more information
44 ;; available from:
45 ;; http://idlwave.org
46 ;;
47 ;; INSTALLATION:
48 ;; =============
49 ;;
50 ;; Follow the instructions in the INSTALL file of the distribution.
51 ;; In short, put this file on your load path and add the following
52 ;; lines to your .emacs file:
53 ;;
54 ;; (autoload 'idlwave-shell "idlw-shell" "IDLWAVE Shell" t)
55 ;;
56 ;;
57 ;; SOURCE
58 ;; ======
59 ;;
60 ;; The newest version of this file can be found on the maintainers
61 ;; web site.
62 ;;
63 ;; http://idlwave.org
64 ;;
65 ;; DOCUMENTATION
66 ;; =============
67 ;;
68 ;; IDLWAVE is documented online in info format.
69 ;; A printable version of the documentation is available from the
70 ;; maintainers webpage (see under SOURCE)
71 ;;
72 ;;
73 ;; KNOWN PROBLEMS
74 ;; ==============
75 ;;
76 ;; Under XEmacs the Debug menu in the shell does not display the
77 ;; keybindings in the prefix map. There bindings are available anyway - so
78 ;; it is a bug in XEmacs.
79 ;; The Debug menu in source buffers *does* display the bindings correctly.
80 ;;
81 ;;
82 ;; CUSTOMIZATION VARIABLES
83 ;; =======================
84 ;;
85 ;; IDLWAVE has customize support - so if you want to learn about
86 ;; the variables which control the behavior of the mode, use
87 ;; `M-x idlwave-customize'.
88 ;;
89 ;;--------------------------------------------------------------------------
90 ;;
91 \f
92 ;;; Code:
93
94 (require 'comint)
95 (require 'idlwave)
96
97 (eval-when-compile (require 'cl))
98
99 (defvar idlwave-shell-have-new-custom nil)
100
101 ;;; Customizations: idlwave-shell group
102
103 ;; General/Misc. customizations
104 (defgroup idlwave-shell-general-setup nil
105 "General setup of the Shell interaction for IDLWAVE/Shell."
106 :prefix "idlwave-shell"
107 :group 'idlwave)
108
109 (defcustom idlwave-shell-prompt-pattern "^\r? ?IDL> "
110 "*Regexp to match IDL prompt at beginning of a line.
111 For example, \"^\r?IDL> \" or \"^\r?WAVE> \".
112 The \"^\r?\" is needed, to indicate the beginning of the line, with
113 optional return character (which IDL seems to output randomly).
114 This variable is used to initialize `comint-prompt-regexp' in the
115 process buffer.
116
117 This 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
123 process output is made by surrounding this name with `*'s."
124 :group 'idlwave-shell-general-setup
125 :type 'string)
126
127 ;; (defcustom idlwave-shell-automatic-start...) See idlwave.el
128
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
132 :type 'boolean)
133
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.
142 See also `idlwave-shell-use-dedicated-frame'.
143 The default makes the frame splittable, so that completion works correctly."
144 :group 'idlwave-shell-general-setup
145 :type '(repeat
146 (cons symbol sexp)))
147
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
153 (defcustom idlwave-shell-arrows-do-history t
154 "*Non-nil means UP and DOWN arrows move through command history.
155 This variable can have 3 values:
156 nil Arrows just move the cursor
157 t 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
167 ;; FIXME: add comint-input-ring-size?
168
169 (defcustom idlwave-shell-use-toolbar t
170 "*Non-nil means, use the debugging toolbar in all IDL related buffers.
171 Starting the shell will then add the toolbar to all idlwave-mode buffers.
172 Exiting the shell will removed everywhere.
173 Available on XEmacs and on Emacs 21.x or later.
174 At any time you can toggle the display of the toolbar with
175 `C-c C-d C-t' (`idlwave-shell-toggle-toolbar')."
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.
181 It should be an absolute pathname.
182 The full temporary file name is obtained by using `make-temp-file'
183 so 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
190 The documentation of this variable used to be:
191 If 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'.
195 This variable must already be set when idlwave-shell.el is loaded.
196 Setting it in the mode-hook is too late."
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.
202 The prefix key itself is given in the option `idlwave-shell-prefix-key'.
203 So by default setting a breakpoint will be on C-c C-d C-b."
204 :group 'idlwave-shell-general-setup
205 :type 'boolean)
206
207 (defcustom idlwave-shell-automatic-electric-debug 'breakpoint
208 "Enter the electric-debug minor mode automatically.
209 This occurs at a breakpoint or any other halt. The mode is exited
210 upon return to the main level. Can be set to 'breakpoint to enter
211 electric 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
220 file at which point is stopped. This takes point away from the shell
221 window, but is useful for stepping, etc."
222 :group 'idlwave-shell-general-setup
223 :type 'boolean)
224
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'.")
229
230 (defcustom idlwave-shell-use-truename nil
231 "*Non-nil means, use use `file-truename' when looking for buffers.
232 If this variable is non-nil, Emacs will use the function `file-truename' to
233 resolve symbolic links in the file paths printed by e.g., STOP commands.
234 This means, unvisited files will be loaded under their truename.
235 However, when a file is already visited under a different name, IDLWAVE will
236 reuse that buffer.
237 This option was once introduced in order to avoid multiple buffers visiting
238 the same file. However, IDLWAVE no longer makes this mistake, so it is safe
239 to set this option to nil."
240 :group 'idlwave-shell-general-setup
241 :type 'boolean)
242
243 (defcustom idlwave-shell-file-name-chars "~/A-Za-z0-9+:_.$#%={}\\- "
244 "The characters allowed in file names, as a string.
245 Used for file name completion. Must not contain `'', `,' and `\"'
246 because these are used as separators by IDL."
247 :group 'idlwave-shell-general-setup
248 :type 'string)
249
250 (defcustom idlwave-shell-mode-hook '()
251 "*Hook for customizing `idlwave-shell-mode'."
252 :group 'idlwave-shell-general-setup
253 :type 'hook)
254
255 (defcustom idlwave-shell-graphics-window-size '(500 400)
256 "Size of IDL graphics windows popped up by special IDLWAVE command.
257 The command is `C-c C-d C-f' and accepts as a prefix the window nr.
258 A 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
264
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
271 (defcustom idlwave-shell-initial-commands "!more=0 & defsysv,'!ERROR_STATE',EXISTS=__e & if __e then begin & !ERROR_STATE.MSG_PREFIX=\"% \" & delvar,__e & endif"
272 "Initial commands, separated by newlines, to send to IDL.
273 This string is sent to the IDL process by `idlwave-shell-mode' which is
274 invoked 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.
280 The file `idlwave-shell-command-history-file' is used to save and restore
281 the history."
282 :group 'idlwave-shell-command-setup
283 :type 'boolean)
284
285 (defcustom idlwave-shell-command-history-file "idlwhist"
286 "The file in which the command history of the idlwave shell is saved.
287 In order to change the size of the history, see the variable
288 `comint-input-ring-size'.
289 The history is only saved if the variable `idlwave-shell-save-command-history'
290 is non-nil."
291 :group 'idlwave-shell-command-setup
292 :type 'file)
293
294 (defcustom idlwave-shell-show-commands
295 '(run misc breakpoint)
296 "*A list of command types to show output from in the shell.
297 Possibilities are 'run, 'debug, 'breakpoint, and 'misc. Unselected
298 types are not displayed in the shell. The type 'everything causes all
299 the copious shell traffic to be displayed."
300 :group 'idlwave-shell-command-setup
301 :type '(choice
302 (const everything)
303 (set :tag "Checklist" :greedy t
304 (const :tag "All .run and .compile commands" run)
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))))
308
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
314 (defcustom idlwave-shell-examine-alist
315 `(("Print" . ,(concat "idlwave_print_safe,___,"
316 (number-to-string
317 idlwave-shell-max-print-length)))
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(___)")
325 ("Arg Present" . "print,arg_present(___)")
326 ("Widget Valid" . "print,widget_info(___,/VALID)")
327 ("Widget Geometry" . "help,widget_info(___,/GEOMETRY)"))
328 "Alist of special examine commands for popup selection.
329 The keys are used in the selection popup created by
330 `idlwave-shell-examine-select', and the corresponding value is sent as
331 a command to the shell, with special sequence `___' replaced by the
332 expression being examined."
333 :group 'idlwave-shell-command-setup
334 :type '(repeat
335 (cons
336 (string :tag "Label ")
337 (string :tag "Command"))))
338
339 (defvar idlwave-shell-print-expression-function nil
340 "*OBSOLETE VARIABLE, is no longer used.")
341
342 (defcustom idlwave-shell-separate-examine-output t
343 "*Non-nil means, put output of examine commands in their own buffer."
344 :group 'idlwave-shell-command-setup
345 :type 'boolean)
346
347 (defcustom idlwave-shell-comint-settings
348 '((comint-scroll-to-bottom-on-input . t)
349 (comint-scroll-to-bottom-on-output . t)
350 (comint-scroll-show-maximum-output . nil)
351 (comint-prompt-read-only . t))
352
353 "Alist of special settings for the comint variables in the IDLWAVE Shell.
354 Each entry is a cons cell with the name of a variable and a value.
355 The function `idlwave-shell-mode' will make local variables out of each entry.
356 Changes to this variable will only be active when the shell buffer is
357 newly 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
367 (defcustom idlwave-shell-use-input-mode-magic nil
368 "*Non-nil means, IDLWAVE should check for input mode spells in output.
369 The spells are strings printed by your IDL program and matched
370 by the regular expressions in `idlwave-shell-input-mode-spells'.
371 When these expressions match, IDLWAVE switches to character input mode and
372 back, respectively. See `idlwave-shell-input-mode-spells' for details."
373 :group 'idlwave-shell-command-setup
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
380 When the first regexp matches in the output stream of IDL, IDLWAVE
381 prompts for a single character and sends it immediately to IDL, similar
382 to the command \\[idlwave-shell-send-char].
383
384 When the second regexp matches, IDLWAVE switches to a blocking
385 single-character input mode. This is the same mode which can be entered
386 manually with \\[idlwave-shell-char-mode-loop].
387 This input mode exits when the third regexp matches in the output,
388 or when the IDL prompt is encountered.
389
390 The variable `idlwave-shell-use-input-mode-magic' must be non-nil to enable
391 scanning for these expressions. If the IDL program produces lots of
392 output, shell operation may be slowed down.
393
394 This mechanism is useful for correct interaction with the IDL function
395 GET_KBRD, because in normal operation IDLWAVE only sends \\n terminated
396 strings. Here is some example code which makes use of the default spells.
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
409 Since the IDLWAVE shell defines the system variable `!IDLWAVE_VERSION',
410 you could actually check if you are running under Emacs before printing
411 the magic strings. Here is a procedure which uses this.
412
413 Usage:
414 ======
415 idlwave_char_input ; Make IDLWAVE send one character
416 idlwave_char_input,/on ; Start the loop to send characters
417 idlwave_char_input,/off ; End the loop to send characters
418
419
420 pro 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>'
427 endif
428 end"
429 :group 'idlwave-shell-command-setup
430 :type '(list
431 (regexp :tag "One-char regexp")
432 (regexp :tag "Char-mode regexp")
433 (regexp :tag "Line-mode regexp")))
434
435 (defcustom idlwave-shell-breakpoint-popup-menu t
436 "*If non-nil, provide a menu on mouse-3 on breakpoint lines, and
437 popup help text on the line."
438 :group 'idlwave-shell-command-setup
439 :type 'boolean)
440
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
446 ;; Breakpoint Overlays etc
447 (defgroup idlwave-shell-highlighting-and-faces nil
448 "Highlighting and faces used by the IDLWAVE Shell mode."
449 :prefix "idlwave-shell"
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.
454 Value decides about the method which is used to mark the line. Valid values
455 are:
456
457 nil Do not mark the line
458 'arrow Use the overlay arrow
459 'face Use `idlwave-shell-stop-line-face' to highlight the line.
460 t Use what IDLWAVE thinks is best. Will be a face where possible,
461 otherwise the overlay arrow.
462 The overlay-arrow has the disadvantage to hide the first chars of a line.
463 Since many people do not have the main block of IDL programs indented,
464 a face highlighting may be better.
465 In Emacs 21, the overlay arrow is displayed in a special area and never
466 hides 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.
476 We use a single character by default, since the main block of IDL procedures
477 often has no indentation. Where possible, IDLWAVE will use overlays to
478 display the stop-lines. The arrow is only used on character-based terminals.
479 See 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'.
485 Allows you to choose the font, color and other properties for
486 line where IDL is stopped. See also `idlwave-shell-mark-stop-line'."
487 :group 'idlwave-shell-highlighting-and-faces
488 :type 'symbol)
489
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
495 (defcustom idlwave-shell-electric-stop-line-face
496 (prog1
497 (copy-face 'modeline 'idlwave-shell-electric-stop-line)
498 (set-face-background 'idlwave-shell-electric-stop-line
499 idlwave-shell-electric-stop-color)
500 (condition-case nil
501 (set-face-foreground 'idlwave-shell-electric-stop-line nil)
502 (error nil)))
503 "*The face for `idlwave-shell-stop-line-overlay' when in electric debug mode.
504 Allows you to choose the font, color and other properties for the line
505 where IDL is stopped, when in Electric Debug Mode."
506 :group 'idlwave-shell-highlighting-and-faces
507 :type 'symbol)
508
509 (defcustom idlwave-shell-mark-breakpoints t
510 "*Non-nil means, mark breakpoints in the source files.
511 Valid values are:
512 nil 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.
516 t 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
525 "Obsolete variable. See `idlwave-shell-mark-breakpoints'.")
526
527 (defcustom idlwave-shell-breakpoint-face 'idlwave-shell-bp
528 "*The face for breakpoint lines in the source code.
529 Allows you to choose the font, color and other properties for
530 lines which have a breakpoint. See also `idlwave-shell-mark-breakpoints'."
531 :group 'idlwave-shell-highlighting-and-faces
532 :type 'symbol)
533
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."
542 :group 'idlwave-shell-highlighting-and-faces))
543
544 (defcustom idlwave-shell-disabled-breakpoint-face
545 'idlwave-shell-disabled-bp
546 "*The face for disabled breakpoint lines in the source code.
547 Allows you to choose the font, color and other properties for
548 lines which have a breakpoint. See also `idlwave-shell-mark-breakpoints'."
549 :group 'idlwave-shell-highlighting-and-faces
550 :type 'symbol)
551
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."
560 :group 'idlwave-shell-highlighting-and-faces))
561
562
563 (defcustom idlwave-shell-expression-face 'secondary-selection
564 "*The face for `idlwave-shell-expression-overlay'.
565 Allows you to choose the font, color and other properties for
566 the expression printed by IDL."
567 :group 'idlwave-shell-highlighting-and-faces
568 :type 'symbol)
569
570 (defcustom idlwave-shell-output-face 'secondary-selection
571 "*The face for `idlwave-shell-output-overlay'.
572 Allows you to choose the font, color and other properties for
573 the expression output by IDL."
574 :group 'idlwave-shell-highlighting-and-faces
575 :type 'symbol)
576
577 ;;; End user customization variables
578
579 ;;; External variables
580 (defvar comint-last-input-start)
581 (defvar comint-last-input-end)
582
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.
589 This is used to speed up the reloading of the routine info procedure
590 before use by the shell.")
591
592 (defun idlwave-shell-temp-file (type)
593 "Return a temp file, creating it if necessary.
594
595 TYPE is either 'pro' or 'rinfo', and `idlwave-shell-temp-pro-file' or
596 `idlwave-shell-temp-rinfo-save-file' is set (respectively)."
597 (cond
598 ((eq type 'rinfo)
599 (or idlwave-shell-temp-rinfo-save-file
600 (setq idlwave-shell-temp-rinfo-save-file
601 (idlwave-shell-make-temp-file idlwave-shell-temp-pro-prefix))))
602 ((eq type 'pro)
603 (or idlwave-shell-temp-pro-file
604 (setq idlwave-shell-temp-pro-file
605 (idlwave-shell-make-temp-file idlwave-shell-temp-pro-prefix))))
606 (t (error "Wrong argument (idlwave-shell-temp-file): %s"
607 (symbol-name type)))))
608
609
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)))
633
634
635 (defvar idlwave-shell-dirstack-query "cd,current=___cur & print,___cur"
636 "Command used by `idlwave-shell-resync-dirs' to query IDL for
637 the directory stack.")
638
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.")
642
643 (defvar idlwave-shell-mode-line-info nil
644 "Additional info displayed in the mode line.")
645
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.")
660 (defvar idlwave-shell-is-stopped nil)
661 (defvar idlwave-shell-expression-overlay nil
662 "The overlay for the examined expression.")
663 (defvar idlwave-shell-output-overlay nil
664 "The overlay for the last IDL output.")
665
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))
672 (if (overlayp idlwave-shell-output-overlay)
673 (delete-overlay idlwave-shell-output-overlay))
674
675 ;; Set to nil initially
676 (setq idlwave-shell-stop-line-overlay nil
677 idlwave-shell-expression-overlay nil
678 idlwave-shell-output-overlay nil)
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))
695 (overlay-put idlwave-shell-stop-line-overlay
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))
703 (overlay-put idlwave-shell-stop-line-overlay
704 'face idlwave-shell-stop-line-face)))))
705
706 ;; Now the expression and output overlays
707 (setq idlwave-shell-expression-overlay (make-overlay 1 1))
708 (overlay-put idlwave-shell-expression-overlay
709 'face idlwave-shell-expression-face)
710 (overlay-put idlwave-shell-expression-overlay
711 'priority 1)
712 (setq idlwave-shell-output-overlay (make-overlay 1 1))
713 (overlay-put idlwave-shell-output-overlay
714 'face idlwave-shell-output-face)
715
716 (copy-face idlwave-shell-stop-line-face
717 'idlwave-shell-pending-stop)
718 (copy-face idlwave-shell-electric-stop-line-face
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
725 (defvar idlwave-shell-bp-query "help,/breakpoints"
726 "Command to obtain list of breakpoints.")
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.
733 The current command is finished when the IDL prompt is displayed.
734 This is evaluated if it is a list or called with funcall.")
735
736 (defvar idlwave-shell-sentinel-hook nil
737 "Hook run when the IDL process exits.")
738
739 (defvar idlwave-shell-hide-output nil
740 "If non-nil the process output is not inserted into the output buffer.")
741
742 (defvar idlwave-shell-show-if-error nil
743 "If non-nil the process output is inserted into the output buffer if
744 it contains an error message, even if hide-output is non-nil.")
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.
755 Each element of the list is list of \(CMD PCMD HIDE\), where CMD is a
756 string to be sent to IDL and PCMD is a post-command to be placed on
757 `idlwave-shell-post-command-hook'. If HIDE is non-nil, hide the output
758 from command CMD. PCMD and HIDE are optional.")
759
760 (defun idlwave-shell-buffer ()
761 "Name of buffer associated with IDL process.
762 The name of the buffer is made by surrounding `idlwave-shell-process-name'
763 with `*'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.
771 ;;;
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
783 '("^% Interrupted at:"
784 "^% Stepped to:"
785 "^% Skipped to:"
786 "^% Stop encountered:"
787 )
788 "*A list of regular expressions matching IDL messages.
789 These are the messages containing file and line information where
790 IDL is currently stopped.")
791
792
793 (defconst idlwave-shell-halt-messages-re
794 (mapconcat 'identity idlwave-shell-halt-messages "\\|")
795 "The regular expression computed from `idlwave-shell-halt-messages'.")
796
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
800 messages containing file and line information of a current
801 traceback.")
802
803 (defconst idlwave-shell-step-messages
804 '("^% Stepped to:"
805 )
806 "*A list of regular expressions matching stepped execution messages.
807 These are IDL messages containing file and line information where
808 IDL has currently stepped.")
809
810 (defvar idlwave-shell-break-message "^% Breakpoint at:"
811 "*Regular expression matching an IDL breakpoint message line.")
812
813 (defconst idlwave-shell-electric-debug-help
814 " ==> IDLWAVE Electric Debug Mode Help <==
815
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 ?]).
841 x Examine expression near point or in region ([C-u x]) with
842 letter completion of the examine type.
843 e Prompt for an expression to print.
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.")
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)
865 (defvar idlwave-shell-calling-stack-index)
866 (defvar idlwave-shell-only-prompt-pattern nil)
867 (defvar tool-bar-map)
868
869 (define-derived-mode idlwave-shell-mode comint-mode "IDL-Shell"
870 "Major mode for interacting with an inferior IDL process.
871
872 1. 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
876 process output copies the current line (except for the prompt) to
877 the end of the buffer.
878
879 Command history, searching of previous commands, command line
880 editing are available via the comint-mode key bindings, by default
881 mostly on the key `C-c'. Command history is also available with
882 the arrow keys UP and DOWN.
883
884 2. Completion
885 ----------
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'.
890
891 3. 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.
896 To update IDLWAVE's knowledge about compiled or edited modules, use
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.
900 \\[idlwave-context-help] shows the online help on the item at
901 point, if online help has been installed.
902
903
904 4. Debugging
905 ---------
906 A complete set of commands for compiling and debugging IDL programs
907 is available from the menu. Also keybindings starting with a
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
922 5. 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
934 6. Hooks
935 -----
936 Turning on `idlwave-shell-mode' runs `comint-mode-hook' and
937 `idlwave-shell-mode-hook' (in that order).
938
939 7. Documentation and Customization
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
944 homepage at URL `http://idlwave.org'.
945 IDLWAVE has customize support - see the group `idlwave'.
946
947 8. Keybindings
948 -----------
949 \\{idlwave-shell-mode-map}"
950 :abbrev-table idlwave-mode-abbrev-table
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
957 (setq comint-prompt-regexp idlwave-shell-prompt-pattern)
958 (setq comint-process-echoes t)
959
960 ;; Can not use history expansion because "!" is used for system variables.
961 (setq comint-input-autoexpand nil)
962 ;; (setq comint-input-ring-size 64)
963
964 (set (make-local-variable 'completion-ignore-case) t)
965 (set (make-local-variable 'comint-completion-addsuffix) '("/" . ""))
966 (setq comint-input-ignoredups t)
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 "-%-"))
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)
992 (setq idlwave-shell-calling-stack-index 0)
993 (setq idlwave-shell-only-prompt-pattern
994 (concat "\\`[ \t\n]*"
995 (substring idlwave-shell-prompt-pattern 1)
996 "[ \t\n]*\\'"))
997
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
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))
1006 (setq idlwave-idlwave_routine_info-compiled nil)
1007 (setq idlwave-shell-ready nil)
1008 (setq idlwave-shell-bp-alist nil)
1009 (idlwave-shell-update-bp-overlays) ; Throw away old overlays
1010 (setq idlwave-shell-post-command-hook nil ;clean up any old stuff
1011 idlwave-shell-sources-alist nil)
1012 (setq idlwave-shell-default-directory default-directory)
1013 (setq idlwave-shell-hide-output nil)
1014
1015 ;; NB: `make-local-hook' needed for older/alternative Emacs compatibility
1016 ;; (make-local-hook 'kill-buffer-hook)
1017 (add-hook 'kill-buffer-hook 'idlwave-shell-kill-shell-buffer-confirm
1018 nil 'local)
1019 (add-hook 'kill-buffer-hook 'idlwave-shell-delete-temp-files nil 'local)
1020 (add-hook 'kill-emacs-hook 'idlwave-shell-delete-temp-files)
1021 (easy-menu-add idlwave-shell-mode-menu idlwave-shell-mode-map)
1022
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)))))
1028
1029
1030 (unless (memq 'comint-carriage-motion
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)
1036 (let ((pmark (process-mark (get-buffer-process
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
1051 ;; IDLWAVE syntax, and turn on abbreviations
1052 (set (make-local-variable 'comment-start) ";")
1053 (setq abbrev-mode t)
1054
1055 ;; NB: `make-local-hook' needed for older/alternative Emacs compatibility
1056 ;; make-local-hook 'post-command-hook)
1057 (add-hook 'post-command-hook 'idlwave-command-hook nil t)
1058
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
1067 ;; Turn off the non-debug toolbar buttons (open,save,etc.)
1068 (set (make-local-variable 'tool-bar-map) nil)
1069
1070 (idlwave-shell-send-command idlwave-shell-initial-commands nil 'hide)
1071 ;; Turn off IDL's ^d interpreting, and define a system
1072 ;; variable which knows the version of IDLWAVE
1073 (idlwave-shell-send-command
1074 (format "defsysv,'!idlwave_version','%s',1" idlwave-mode-version)
1075 nil 'hide)
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))
1080
1081 (defvar idlwave-system-directory)
1082 (defun idlwave-shell-get-path-info (&optional no-write)
1083 "Get the path lists, writing to file unless NO-WRITE is set."
1084 (let* ((rpl (idlwave-shell-path-filter))
1085 (sysdir (car rpl))
1086 (dirs (cdr rpl))
1087 (old-path-alist idlwave-path-alist)
1088 (old-sys-dir idlwave-system-directory)
1089 path-changed sysdir-changed)
1090 (when sysdir
1091 (setq idlwave-system-directory sysdir)
1092 (if (setq sysdir-changed
1093 (not (string= idlwave-system-directory old-sys-dir)))
1094 (put 'idlwave-system-directory 'from-shell t)))
1095 ;; Preserve any existing flags
1096 (setq idlwave-path-alist
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))
1103 (if (setq path-changed (not (equal idlwave-path-alist old-path-alist)))
1104 (put 'idlwave-path-alist 'from-shell t))
1105 (if idlwave-path-alist
1106 (if (and (not no-write)
1107 idlwave-auto-write-paths
1108 (or sysdir-changed path-changed)
1109 (not idlwave-library-path))
1110 (idlwave-write-paths))
1111 ;; Fall back
1112 (setq idlwave-path-alist old-path-alist))))
1113
1114 (if (not (fboundp 'idl-shell))
1115 (fset 'idl-shell 'idlwave-shell))
1116
1117 (defvar idlwave-shell-idl-wframe nil
1118 "Frame for displaying the IDL shell window.")
1119 (defvar idlwave-shell-display-wframe nil
1120 "Frame for displaying the IDL source files.")
1121
1122 (defvar idlwave-shell-calling-stack-index 0)
1123 (defvar idlwave-shell-calling-stack-routine nil)
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.
1133 ;; However, if the current is the shell frame, we make a new frame,
1134 ;; or recycle the first existing visible frame
1135 (setq idlwave-shell-display-wframe
1136 (if (eq (selected-frame) idlwave-shell-idl-wframe)
1137 (or
1138 (let ((flist (visible-frame-list))
1139 (frame (selected-frame)))
1140 (catch 'exit
1141 (while flist
1142 (if (not (eq (car flist)
1143 idlwave-shell-idl-wframe))
1144 (throw 'exit (car flist))
1145 (setq flist (cdr flist))))))
1146 (make-frame))
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
1161 (setq idlwave-shell-idl-wframe
1162 (make-frame idlwave-shell-frame-parameters)))))
1163
1164 ;;;###autoload
1165 (defun idlwave-shell (&optional arg quick)
1166 "Run an inferior IDL, with I/O through buffer `(idlwave-shell-buffer)'.
1167 If buffer exists but shell process is not running, start new IDL.
1168 If buffer exists and shell process is running, just switch to the buffer.
1169
1170 When called with a prefix ARG, or when `idlwave-shell-use-dedicated-frame'
1171 is non-nil, the shell buffer and the source buffers will be in
1172 separate frames.
1173
1174 The command to run comes from variable `idlwave-shell-explicit-file-name',
1175 with options taken from `idlwave-shell-command-line-options'.
1176
1177 The buffer is put in `idlwave-shell-mode', providing commands for sending
1178 input and controlling the IDL job. See help on `idlwave-shell-mode'.
1179 See also the variable `idlwave-shell-prompt-pattern'.
1180
1181 \(Type \\[describe-mode] in the shell buffer for a list of commands.)"
1182 (interactive "P")
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)))
1190 (add-hook 'idlwave-shell-sentinel-hook
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))
1197
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))
1218 (if idlwave-shell-use-dedicated-window
1219 (set-window-dedicated-p window t))
1220 (select-window current-window)
1221 (if idlwave-shell-ready
1222 (raise-frame (window-frame window)))
1223 (if (eq (selected-frame) (window-frame window))
1224 (select-window window))))
1225 ;; Save the paths at the end, if they are from the Shell and new.
1226 (add-hook 'idlwave-shell-sentinel-hook
1227 (lambda ()
1228 (if (and
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)))))
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
1242 (defun idlwave-shell-hide-p (type &optional list)
1243 "Whether to hide this type of command.
1244 Return either nil or 'hide."
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."
1251 (if (listp idlwave-shell-show-commands)
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
1258
1259 (defun idlwave-shell-send-command (&optional cmd pcmd hide preempt
1260 show-if-error)
1261 "Send a command to IDL process.
1262
1263 \(CMD PCMD HIDE\) are placed at the end of `idlwave-shell-pending-commands'.
1264 If IDL is ready the first command in `idlwave-shell-pending-commands',
1265 CMD, is sent to the IDL process.
1266
1267 If optional second argument PCMD is non-nil it will be placed on
1268 `idlwave-shell-post-command-hook' when CMD is executed.
1269
1270 If the optional third argument HIDE is non-nil, then hide output from
1271 CMD, unless it is the symbol 'mostly, in which case only output
1272 beginning with \"%\" is hidden, and all other output (i.e., the
1273 results of a PRINT command), is shown. This helps with, e.g.,
1274 stepping through code with output.
1275
1276 If optional fourth argument PREEMPT is non-nil CMD is put at front of
1277 `idlwave-shell-pending-commands'. If PREEMPT is 'wait, wait for all
1278 output to complete and the next prompt to arrive before returning
1279 \(useful if you need an answer now\). IDL is considered ready if the
1280 prompt is present and if `idlwave-shell-ready' is non-nil.
1281
1282 If SHOW-IF-ERROR is non-nil, show the output if it contains an error
1283 message, independent of what HIDE is set to."
1284
1285 ; (setq hide nil) ; FIXME: turn this on for debugging only
1286 ; (if (null cmd)
1287 ; (progn
1288 ; (message "SENDING Pending commands: %s"
1289 ; (prin1-to-string idlwave-shell-pending-commands)))
1290 ; (message "SENDING %s|||%s" cmd pcmd))
1291 (if (and (symbolp idlwave-shell-show-commands)
1292 (eq idlwave-shell-show-commands 'everything))
1293 (setq hide nil))
1294 (let ((save-buffer (current-buffer))
1295 buf proc)
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)
1300 (error "%s"
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"))))
1309 (when (or cmd idlwave-shell-pending-commands)
1310 (set-buffer buf)
1311 ;; To make this easy, always push CMD onto pending commands
1312 (if cmd
1313 (setq idlwave-shell-pending-commands
1314 (if preempt
1315 ;; Put at front.
1316 (append (list (list cmd pcmd hide show-if-error))
1317 idlwave-shell-pending-commands)
1318 ;; Put at end.
1319 (append idlwave-shell-pending-commands
1320 (list (list cmd pcmd hide show-if-error))))))
1321 ;; Check if IDL ready
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))
1335 (hide (nth 2 lcmd))
1336 (show-if-error (nth 3 lcmd)))
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)
1344 ;;Showing errors
1345 (setq idlwave-shell-show-if-error show-if-error)
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)
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"))))))
1359 (goto-char save-point))
1360 (set-buffer save-buffer))))
1361
1362 (defun idlwave-shell-send-char (c &optional error)
1363 "Send one character to the shell, without a newline."
1364 (interactive "cChar to send to IDL: \np")
1365 (let ((errf (if error 'error 'message))
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"))
1370 (if (equal c ?\C-g)
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.
1377 See 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.
1394 Characters are sent one by one, without newlines. The loop is blocking
1395 and intercepts all input events to Emacs. You can use this command
1396 to interact with the IDL command GET_KBRD.
1397 The loop can be aborted by typing `C-g'. The loop also exits automatically
1398 when 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
1411 ;; OK, start the loop
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))
1428 (error (ding) (throw 'exit "Character mode off")))
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
1441 (defun idlwave-shell-move-or-history (up &optional arg)
1442 "When in last line of process buffer, do `comint-previous-input'.
1443 Otherwise 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
1450 (>= (1+ (point-at-eol)) proc-pos))
1451 (comint-previous-input arg)
1452 (forward-line (- arg)))))
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."
1457 (interactive "p")
1458 (idlwave-shell-move-or-history t arg))
1459
1460 (defun idlwave-shell-down-or-history (&optional arg)
1461 "When in last line of process buffer, move to next input.
1462 Otherwise just go down one line."
1463 (interactive "p")
1464 (idlwave-shell-move-or-history nil arg))
1465
1466 ;; Newer versions of comint.el changed the name of comint-filter to
1467 ;; comint-output-filter.
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
1477 (defun idlwave-shell-filter-hidden-output (output)
1478 "Filter hidden output, leaving the good stuff.
1479
1480 Remove everything to the first newline, and all lines with % in front
1481 of them, with optional follow-on lines starting with two spaces. This
1482 works well enough, since any print output typically arrives before
1483 error 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)))
1487 (unless
1488 (string-match idlwave-shell-only-prompt-pattern output)
1489 output))
1490
1491 (defvar idlwave-shell-hidden-output-buffer " *idlwave-shell-hidden-output*"
1492 "Buffer containing hidden output from IDL commands.")
1493 (defvar idlwave-shell-current-state nil)
1494
1495 (defun idlwave-shell-filter (proc string)
1496 "Watch for IDL prompt and filter incoming text.
1497 When the IDL prompt is received executes `idlwave-shell-post-command-hook'
1498 and 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
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
1526 (substring string
1527 (progn (string-match "\\(.*[\n\r]+\\)*"
1528 string)
1529 (match-end 0)))))
1530 (setq idlwave-shell-accumulation
1531 (concat idlwave-shell-accumulation string)))
1532
1533
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"))
1539
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
1546 (with-current-buffer idlwave-shell-hidden-output-buffer
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
1552 (buffer-substring-no-properties
1553 (point-min) (point)))
1554 (delete-region (point-min) (point)))
1555 (setq idlwave-shell-command-output
1556 (with-current-buffer (process-buffer proc)
1557 (buffer-substring-no-properties
1558 (save-excursion
1559 (goto-char (process-mark proc))
1560 (forward-line 0) ; Emacs 21 (beginning-of-line nil)
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
1570 (if idlwave-shell-hide-output
1571 (if (and idlwave-shell-show-if-error
1572 (eq idlwave-shell-current-state 'error))
1573 (idlwave-shell-comint-filter proc full-output)
1574 ;; If it's only *mostly* hidden, filter % lines,
1575 ;; and show anything that remains
1576 (if (eq idlwave-shell-hide-output 'mostly)
1577 (let ((filtered
1578 (idlwave-shell-filter-hidden-output
1579 full-output)))
1580 (if filtered
1581 (idlwave-shell-comint-filter
1582 proc filtered))))))
1583
1584 ;; Call the post-command hook
1585 (if (listp idlwave-shell-post-command-hook)
1586 (progn
1587 ;;(message "Calling list")
1588 ;;(prin1 idlwave-shell-post-command-hook)
1589 (eval idlwave-shell-post-command-hook))
1590 ;;(message "Calling command function")
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))
1600 ;; Done with post command. Do pending command if
1601 ;; any.
1602 (idlwave-shell-send-command)))
1603 (store-match-data data)))))
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)
1610 (with-current-buffer (idlwave-shell-buffer)
1611 (goto-char (point-max))
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)))))
1618
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))
1624 (when (and (window-live-p win)
1625 (not (one-window-p 'nomini)))
1626 (delete-window win))
1627 (idlwave-shell-cleanup)
1628 ;; Run the hook, if possible in the shell buffer.
1629 (if (get-buffer buf)
1630 (with-current-buffer buf
1631 (run-hooks 'idlwave-shell-sentinel-hook))
1632 (run-hooks 'idlwave-shell-sentinel-hook))))
1633
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
1641 "^% Syntax error.\\s-*\n\\s-*At:\\s-*\\(.*\\),\\s-*Line\\s-*\\(.*\\)"
1642 "A regular expression to match an IDL syntax error.
1643 The first pair matches the file name, the second pair matches the line
1644 number.")
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
1650 (defvar idlwave-shell-halting-error
1651 "^% .*\n\\([^%].*\n\\)*% Execution halted at:\\(\\s-*\\S-+\\s-*[0-9]+\\s-*.*\\)\n"
1652 "A regular expression to match errors which halt execution.")
1653
1654 (defvar idlwave-shell-cant-continue-error
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
1659 (concat
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.
1677 The 1st group should match the subroutine name.
1678 The 3rd group is the line number.
1679 The 5th group is the file name.
1680 All parts may contain linebreaks surrounded by spaces. This is important
1681 in IDL5 which inserts random linebreaks in long module and file names.")
1682
1683 (defvar idlwave-shell-electric-debug-mode) ; defined by easy-mmode
1684
1685 (defun idlwave-shell-scan-for-state ()
1686 "Scan for state info.
1687 Looks for messages in output from last IDL command indicating where
1688 IDL has stopped. The types of messages we are interested in are
1689 execution halted, stepped, breakpoint, interrupted at and trace
1690 messages. For breakpoint messages process any attached count or
1691 command parameters. Update the stop line if a message is found.
1692 The variable `idlwave-shell-current-state' is set to 'error, 'halt,
1693 or 'breakpoint, which describes the status, or nil for none of
1694 the above."
1695 (let (trace)
1696 (cond
1697 ;; Make sure we have output
1698 ((not idlwave-shell-command-output))
1699
1700 ;; First Priority: Syntax and other errors
1701 ((or
1702 (string-match idlwave-shell-syntax-error
1703 idlwave-shell-command-output)
1704 (string-match idlwave-shell-other-error
1705 idlwave-shell-command-output))
1706 (with-current-buffer
1707 (get-buffer-create idlwave-shell-error-buffer)
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))
1714
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
1721 (idlwave-shell-parse-line
1722 (substring idlwave-shell-command-output
1723 (match-beginning 2)))
1724 idlwave-shell-current-state 'error)
1725 (idlwave-shell-display-line (idlwave-shell-pc-frame)))
1726
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
1736 (idlwave-shell-parse-line
1737 (substring idlwave-shell-command-output (match-end 0))))
1738 (setq idlwave-shell-current-state 'halt)
1739 ;; Don't debug trace messages
1740 (idlwave-shell-display-line
1741 (idlwave-shell-pc-frame) nil
1742 (if trace 'disable
1743 (if idlwave-shell-electric-debug-mode 'force))))
1744
1745 ;; Fourth Priority: Breakpoints
1746 ((string-match idlwave-shell-break-message
1747 idlwave-shell-command-output)
1748 (setq idlwave-shell-calling-stack-index 0)
1749 (setq idlwave-shell-halt-frame
1750 (idlwave-shell-parse-line
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
1756 (let ((bp (assoc
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)))
1769 (setq idlwave-shell-current-state 'breakpoint)
1770 (idlwave-shell-display-line (idlwave-shell-pc-frame)))
1771
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)))))
1779
1780
1781 (defun idlwave-shell-parse-line (string &optional skip-main)
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
1787 ;to be pretty good though.
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.
1805
1806 (let (number procedure file)
1807 (when (and (not (if skip-main (string-match ":\\s-*\\$MAIN" string)))
1808 (string-match idlwave-shell-file-line-message string))
1809 (setq procedure (match-string 1 string)
1810 number (match-string 3 string)
1811 file (match-string 5 string))
1812
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)
1821 (string-to-number number)
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.
1834 The last line of STRING may be garbage - we check which one makes a valid
1835 file name."
1836 (let ((file1 "") (file2 "") (start 0))
1837 ;; We scan no further than to the next "^%" line
1838 (if (string-match "^%" file)
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
1863 idlwave-shell-calling-stack-index 0
1864 idlwave-idlwave_routine_info-compiled nil)
1865 (idlwave-shell-delete-temp-files)
1866 (idlwave-shell-display-line nil)
1867 (idlwave-shell-update-bp-overlays) ; kill old overlays
1868 (idlwave-shell-kill-buffer idlwave-shell-hidden-output-buffer)
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
1888 (defun idlwave-shell-window (n)
1889 "Issue a `window,N' command to IDL, with special window size.
1890 The size is given by `idlwave-shell-graphics-window-size'."
1891 (interactive "P")
1892 (let ((n (if n (prefix-numeric-value n) 0)))
1893 (idlwave-shell-send-command
1894 (apply 'format "window,%d,xs=%d,ys=%d"
1895 n idlwave-shell-graphics-window-size)
1896 nil (idlwave-shell-hide-p 'misc) nil t)))
1897
1898 (defun idlwave-shell-resync-dirs ()
1899 "Resync the buffer's idea of the current directory.
1900 This command queries IDL with the command bound to
1901 `idlwave-shell-dirstack-query', reads the output for the new
1902 directory."
1903 (interactive)
1904 (idlwave-shell-send-command idlwave-shell-dirstack-query
1905 'idlwave-shell-filter-directory
1906 'hide 'wait))
1907
1908 (defun idlwave-shell-retall (&optional arg)
1909 "Return from the entire calling stack.
1910 Also get rid of widget events in the queue."
1911 (interactive "P")
1912 (save-selected-window
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)
1916 nil t)
1917 (idlwave-shell-display-line nil)))
1918
1919 (defun idlwave-shell-closeall (&optional arg)
1920 "Close all open files."
1921 (interactive "P")
1922 (idlwave-shell-send-command "close,/all" nil
1923 (idlwave-shell-hide-p 'misc) nil t))
1924
1925 (defun idlwave-shell-quit (&optional arg)
1926 "Exit the IDL process after confirmation.
1927 With 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
1936 (defun idlwave-shell-reset (&optional hidden)
1937 "Reset IDL. Return to main level and destroy the leftover variables.
1938 This issues the following commands:
1939 RETALL
1940 WIDGET_CONTROL,/RESET
1941 CLOSE, /ALL
1942 HEAP_GC, /VERBOSE"
1943 ;; OBJ_DESTROY, OBJ_VALID() FIXME: should this be added?
1944 (interactive "P")
1945 (when (or idlwave-shell-reset-no-prompt
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)))
1956
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)
1963 (while (string-match "^PATH:[ \t]*<\\(.*\\)>[ \t]*\n" path-string start)
1964 (push (match-string 1 path-string) dirs)
1965 (setq start (match-end 0)))
1966 (setq dirs (mapcar 'file-name-as-directory dirs))
1967 (if (string-match "^SYSDIR:[ \t]*<\\(.*\\)>[ \t]*\n" path-string)
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)
1977 ;; (message "GOT: %s" text) ;??????????????????????
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 "")
1990 (if idlwave-idlwave_routine_info-compiled
1991 (message
1992 "Routine Info warning: No match for BEGIN line in \n>>>\n%s\n<<<\n"
1993 idlwave-shell-command-output)))
1994 (if (string-match "^>>>END OF IDLWAVE ROUTINE INFO.*" text)
1995 (setq text (substring text 0 (match-beginning 0)))
1996 (if idlwave-idlwave_routine_info-compiled
1997 (message
1998 "Routine Info warning: No match for END line in \n>>>\n%s\n<<<\n"
1999 idlwave-shell-command-output)))
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)))
2013 (mapcar 'list
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)
2018 keys (mapcar (lambda (x)
2019 (list (idlwave-sintern-keyword (car x) t))) keys))
2020
2021 ;; In the following ignore routines already defined in buffers,
2022 ;; assuming that if the buffer stuff differs, it is a "new"
2023 ;; version, not yet compiled, and should take precedence.
2024 ;; We could do the same for the library to avoid duplicates -
2025 ;; but I think frequently a user might have several versions of
2026 ;; the same function in different programs, and in this case the
2027 ;; compiled one will be the best guess of all versions.
2028 ;; Therefore, we leave duplicates of library routines in.
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)
2033 (not (idlwave-rinfo-assq name 'pro class
2034 idlwave-buffer-routines)))
2035 ;; FIXME: is it OK to make the library routines dominate?
2036 ;;(not (idlwave-rinfo-assq name 'pro class
2037 ;; idlwave-library-routines))
2038 )
2039 (setq entry (list name 'pro class
2040 (cons 'compiled
2041 (if file
2042 (list
2043 (file-name-nondirectory file)
2044 (idlwave-sintern-dir
2045 (file-name-directory file)))))
2046 cs (cons nil keys)))
2047 (if file
2048 (push entry idlwave-compiled-routines)
2049 (push entry idlwave-unresolved-routines)))
2050
2051 ((and (string= type "FUN")
2052 ;; FIXME: is it OK to make the buffer routines dominate?
2053 (or t (not file)
2054 (not (idlwave-rinfo-assq name 'fun class
2055 idlwave-buffer-routines)))
2056 ;; FIXME: is it OK to make the library routines dominate?
2057 ;; (not (idlwave-rinfo-assq name 'fun class
2058 ;; idlwave-library-routines))
2059 )
2060 (setq entry (list name 'fun class
2061 (cons 'compiled
2062 (if file
2063 (list
2064 (file-name-nondirectory file)
2065 (idlwave-sintern-dir
2066 (file-name-directory file)))))
2067 cs (cons nil keys)))
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
2075 (defun idlwave-shell-filter-directory ()
2076 "Get the current directory from `idlwave-shell-command-output'.
2077 Change the default directory for the process buffer to concur."
2078 (with-current-buffer (idlwave-shell-buffer)
2079 (if (string-match ",___cur[\n\r ]+\\([^\n\r]+\\)[\n\r]"
2080 idlwave-shell-command-output)
2081 (let ((dir (substring idlwave-shell-command-output
2082 (match-beginning 1) (match-end 1))))
2083 ;; (message "Setting Emacs working dir to %s" dir)
2084 (setq idlwave-shell-default-directory dir)
2085 (setq default-directory (file-name-as-directory dir))))))
2086
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)
2095 (setq expression (buffer-substring
2096 (catch 'exit
2097 (while t
2098 (if (not (re-search-backward
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
2107 (concat "if obj_valid(" expression ") then print,obj_class("
2108 expression ")")
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
2113 (not (assoc-string idlwave-shell-get-object-class
2114 (idlwave-class-alist) t)))
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."
2120 (let ((match "obj_class([^\n\r]+[\n\r ]"))
2121 (if (string-match (concat match "\\([A-Za-z_0-9]+\\) *[\n\r]\\("
2122 idlwave-shell-prompt-pattern "\\)")
2123 idlwave-shell-command-output)
2124 (setq idlwave-shell-get-object-class
2125 (match-string 1 idlwave-shell-command-output)))))
2126
2127 (defvar idlwave-sint-sysvars nil)
2128 (idlwave-new-sintern-type 'execcomm)
2129
2130 (defun idlwave-shell-complete (&optional arg)
2131 "Do completion in the idlwave-shell buffer.
2132 Calls `idlwave-shell-complete-filename' after some executive commands or
2133 in strings. Otherwise, calls `idlwave-complete' to complete modules and
2134 keywords."
2135 (interactive "P")
2136 (let (exec-cmd)
2137 (cond
2138 ((and
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")))
2144 ;; We are in a command line with an executive command
2145 (idlwave-shell-complete-filename))
2146
2147 ((car-safe exec-cmd)
2148 (setq idlwave-completion-help-info
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"))
2154
2155 ((idlwave-shell-batch-command)
2156 (idlwave-shell-complete-filename))
2157
2158 ((idlwave-shell-shell-command)
2159 (idlwave-shell-complete-filename))
2160
2161 ((and (idlwave-shell-filename-string)
2162 (save-excursion
2163 (beginning-of-line)
2164 (let ((case-fold-search t))
2165 (not (looking-at ".*obj_new")))))
2166 (idlwave-shell-complete-filename))
2167
2168 (t
2169 ;; Default completion of modules and keywords
2170 (idlwave-complete arg)))))
2171
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
2174 (defun idlwave-shell-complete-execcomm-help (mode word)
2175 (let ((word (or (nth 1 idlwave-completion-help-info) word))
2176 (entry (assoc-string word idlwave-executive-commands-alist t)))
2177 (cond
2178 ((eq mode 'test)
2179 (and (stringp word) entry (cdr entry)))
2180 ((eq mode 'set)
2181 (if entry (setq idlw-help-link (cdr entry)))) ; setting dynamic variable!
2182 (t (error "This should not happen")))))
2183
2184 (defun idlwave-shell-complete-filename (&optional arg)
2185 "Complete a file name at point if after a file name.
2186 We assume that we are after a file name when completing one of the
2187 args of an executive .run, .rnew or .compile."
2188 ;; CWD might have changed, resync, to set default directory
2189 (idlwave-shell-resync-dirs)
2190 (let ((comint-file-name-chars idlwave-shell-file-name-chars))
2191 (comint-dynamic-complete-as-filename)))
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)
2197 (cons (looking-at "[ \t]*\\.")
2198 (if (looking-at "[ \t]*[.]\\([^ \t\n\r]+\\)[ \t]")
2199 (match-string 1)))))
2200
2201 (defun idlwave-shell-filename-string ()
2202 "Return t if in a string and after what could be a file name."
2203 (let ((limit (point-at-bol)))
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
2210 (defun idlwave-shell-batch-command ()
2211 "Return t if we're in a batch command statement like @foo"
2212 (let ((limit (point-at-bol)))
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
2219 (defun idlwave-shell-shell-command ()
2220 "Return t if we're in a shell command statement like $ls"
2221 (save-excursion
2222 (idlwave-beginning-of-statement)
2223 (looking-at "\\$")))
2224
2225 ;; Debugging Commands ------------------------------------------------------
2226
2227 (defun idlwave-shell-redisplay (&optional hide)
2228 "Try to resync the display with where execution has stopped.
2229 Issues a \"help,/trace\" command followed by a call to
2230 `idlwave-shell-display-line'. Also updates the breakpoint
2231 overlays."
2232 (interactive)
2233 (setq idlwave-shell-calling-stack-index 0)
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)
2242 (idlwave-shell-send-command
2243 "help,/trace"
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))
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)
2258 (cond
2259 ((< nmax nmin)
2260 (setq idlwave-shell-calling-stack-index 0)
2261 (ding)
2262 (message "Problem with calling stack"))
2263 ((> idlwave-shell-calling-stack-index nmax)
2264 (ding)
2265 (setq idlwave-shell-calling-stack-index nmax
2266 message (format "%d is the highest calling stack level - can't go further up"
2267 (- nmax))))
2268 ((< idlwave-shell-calling-stack-index nmin)
2269 (ding)
2270 (setq idlwave-shell-calling-stack-index nmin
2271 message (format "%d is the current calling stack level - can't go further down"
2272 (- nmin)))))
2273 (setq idlwave-shell-calling-stack-routine
2274 (nth 2 (nth idlwave-shell-calling-stack-index stack)))
2275
2276 ;; force edebug for this frame if we're in that mode already
2277 (idlwave-shell-display-line
2278 (nth idlwave-shell-calling-stack-index stack) nil
2279 (if idlwave-shell-electric-debug-mode 'force))
2280 (message "%s" (or message
2281 (format "In routine %s (stack level %d)"
2282 idlwave-shell-calling-stack-routine
2283 (- idlwave-shell-calling-stack-index))))))
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.
2298 If the optional argument FRAME is nil then `idlwave-shell-pc-frame'
2299 is used. Does nothing if the resulting frame is nil."
2300 (if frame ()
2301 (setq frame (idlwave-shell-pc-frame)))
2302 (cond
2303 (frame
2304 (set-buffer (idlwave-find-file-noselect (car frame) 'shell))
2305 (widen)
2306 (goto-char (point-min))
2307 (forward-line (1- (nth 1 frame))))))
2308
2309 (defun idlwave-shell-pc-frame ()
2310 "Return the frame for IDL execution."
2311 (and idlwave-shell-halt-frame
2312 (list (nth 0 idlwave-shell-halt-frame)
2313 (nth 1 idlwave-shell-halt-frame)
2314 (nth 2 idlwave-shell-halt-frame))))
2315
2316 (defun idlwave-shell-valid-frame (frame)
2317 "Check that frame is for an existing file."
2318 (file-readable-p (car frame)))
2319
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))))
2327
2328 (defvar idlwave-shell-suppress-electric-debug nil)
2329 (defun idlwave-shell-display-line (frame &optional col debug)
2330 "Display frame file in other window with overlay arrow.
2331
2332 FRAME is a list of file name, line number, and subroutine name. If
2333 FRAME is nil then remove overlay. If COL is set, move point to that
2334 column in the line. If DEBUG is non-nil, enable the electric debug
2335 mode. 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
2337 matter what the settings of that variable."
2338 (if (not frame)
2339 ;; remove stop-line overlay from old position
2340 (progn
2341 (setq overlay-arrow-string nil)
2342 (setq idlwave-shell-mode-line-info nil)
2343 (setq idlwave-shell-is-stopped nil)
2344 (if idlwave-shell-stop-line-overlay
2345 (delete-overlay idlwave-shell-stop-line-overlay))
2346 ;; turn off electric debug everywhere, if it's on
2347 (idlwave-shell-electric-debug-all-off))
2348 (if (not (idlwave-shell-valid-frame frame))
2349 ;; fixme: errors are dangerous in shell filters. but i think i
2350 ;; have never encountered this one.
2351 (error "invalid frame - unable to access file: %s" (car frame))
2352 ;;
2353 ;; buffer : the buffer to display a line in.
2354 ;; select-shell: current buffer is the shell.
2355 ;;
2356 (setq idlwave-shell-mode-line-info
2357 (if (nth 2 frame)
2358 (format "[%d:%s]"
2359 (- idlwave-shell-calling-stack-index)
2360 (nth 2 frame))))
2361 (let* ((buffer (idlwave-find-file-noselect (car frame) 'shell))
2362 (select-shell (equal (buffer-name) (idlwave-shell-buffer)))
2363 window pos electric)
2364
2365 ;; first make sure the shell window is visible
2366 (idlwave-display-buffer (idlwave-shell-buffer)
2367 nil (idlwave-shell-shell-frame))
2368
2369 ;; now display the buffer and remember which window it is.
2370 (setq window (idlwave-display-buffer buffer
2371 nil (idlwave-shell-source-frame)))
2372
2373 ;; enter the buffer and mark the line
2374 (with-current-buffer buffer
2375 (save-restriction
2376 (widen)
2377 (goto-char (point-min))
2378 (forward-line (1- (nth 1 frame)))
2379 (setq pos (point))
2380 (setq idlwave-shell-is-stopped t)
2381
2382 (if idlwave-shell-stop-line-overlay
2383 (progn
2384 ;; restore face and move overlay
2385 (overlay-put idlwave-shell-stop-line-overlay 'face
2386 (if idlwave-shell-electric-debug-mode
2387 idlwave-shell-electric-stop-line-face
2388 idlwave-shell-stop-line-face))
2389 (move-overlay idlwave-shell-stop-line-overlay
2390 (point) (point-at-eol)
2391 (current-buffer)))
2392 ;; use the arrow instead, but only if marking is wanted.
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)))
2397 (set-marker overlay-arrow-position (point) buffer)))
2398
2399 ;; if the point is outside the restriction, widen the buffer.
2400 (if (or (< pos (point-min)) (> pos (point-max)))
2401 (progn
2402 (widen)
2403 (goto-char pos)))
2404
2405 ;; if we have the column of the error, move the cursor there.
2406 (if col (move-to-column col))
2407 (setq pos (point))
2408
2409 ;; enter electric debug mode, if not prohibited and not in
2410 ;; it already
2411 (when (and (not idlwave-shell-electric-debug-mode)
2412 (or (eq debug 'force)
2413 (and
2414 (not (eq debug 'disable)) ;; explicitly disabled
2415 (or
2416 (eq idlwave-shell-automatic-electric-debug t)
2417 (and
2418 (eq idlwave-shell-automatic-electric-debug
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))
2424
2425 ;; Make sure pos is really displayed in the window.
2426 (set-window-point window pos)
2427
2428 ;; If we came from the shell, go back there. Otherwise select
2429 ;; the window where the error/halt is displayed.
2430 (if (or (and idlwave-shell-electric-zap-to-file electric)
2431 (and (equal (buffer-name) (idlwave-shell-buffer))
2432 (not select-shell)))
2433 (select-window window))))))
2434
2435
2436 (defun idlwave-shell-step (arg)
2437 "Step one source line.
2438 If given prefix argument ARG, step ARG source lines."
2439 (interactive "p")
2440 (or (not arg) (< arg 1)
2441 (setq arg 1))
2442 (idlwave-shell-stop-line-pending)
2443 (idlwave-shell-send-command
2444 (concat ".s " (if (integerp arg) (int-to-string arg) arg))
2445 nil (if (idlwave-shell-hide-p 'debug) 'mostly) nil t))
2446
2447 (defun idlwave-shell-stepover (arg)
2448 "Stepover one source line.
2449 If given prefix argument ARG, step ARG source lines.
2450 Uses IDL's stepover executive command which does not enter called functions."
2451 (interactive "p")
2452 (or (not arg) (< arg 1)
2453 (setq arg 1))
2454 (idlwave-shell-stop-line-pending)
2455 (idlwave-shell-send-command
2456 (concat ".so " (if (integerp arg) (int-to-string arg) arg))
2457 nil (if (idlwave-shell-hide-p 'debug) 'mostly) nil t))
2458
2459 (defun idlwave-shell-break-here (&optional count cmd condition disabled
2460 no-show)
2461 "Set breakpoint at current line.
2462
2463 If COUNT is nil then an ordinary breakpoint is set. We treat a COUNT
2464 of 1 as a temporary breakpoint using the ONCE keyword. Counts greater
2465 than 1 use the IDL AFTER=count keyword to break only after reaching
2466 the statement COUNT times.
2467
2468 Optional argument CMD is a list or function to evaluate upon reaching
2469 the breakpoint. CONDITION is a break condition, and DISABLED, if
2470 non-nil disables the breakpoint."
2471 (interactive "P")
2472 (when (listp count)
2473 (if (equal (car count) 4)
2474 (setq condition (read-string "Break Condition: ")))
2475 (setq count nil))
2476 (idlwave-shell-set-bp
2477 ;; Create breakpoint
2478 (idlwave-shell-bp (idlwave-shell-current-frame)
2479 (list count cmd condition disabled)
2480 (idlwave-shell-current-module))
2481 no-show))
2482
2483 (defun idlwave-shell-set-bp-check (bp)
2484 "Check for failure to set breakpoint.
2485 This is run on `idlwave-shell-post-command-hook'.
2486 Offers to recompile the procedure if we failed. This usually fixes
2487 the problem with not being able to set the breakpoint."
2488 ;; Scan for message
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
2494 (if (progn
2495 (beep)
2496 (y-or-n-p
2497 (concat "Okay to recompile file "
2498 (idlwave-shell-bp-get bp 'file) "?")))
2499 ;; Recompile
2500 (progn
2501 ;; Clean up before retrying
2502 (idlwave-shell-command-failure)
2503 (idlwave-shell-send-command
2504 (concat ".run \"" (idlwave-shell-bp-get bp 'file) "\"") nil
2505 (if (idlwave-shell-hide-p 'run) 'mostly) nil t)
2506 ;; Try setting breakpoint again
2507 (idlwave-shell-set-bp bp))
2508 (beep)
2509 (message "Unable to set breakpoint.")
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)
2517
2518 (t 'okay))))
2519
2520 (defun idlwave-shell-command-failure ()
2521 "Do any necessary clean up when an IDL command fails.
2522 Call this from a function attached to `idlwave-shell-post-command-hook'
2523 that detects the failure of a command.
2524 For example, this is called from `idlwave-shell-set-bp-check' when a
2525 breakpoint can not be set."
2526 ;; Clear pending commands
2527 (setq idlwave-shell-pending-commands nil))
2528
2529 (defun idlwave-shell-cont (&optional no-show)
2530 "Continue executing."
2531 (interactive)
2532 (idlwave-shell-stop-line-pending)
2533 (idlwave-shell-send-command ".c" (unless no-show
2534 '(idlwave-shell-redisplay 'hide))
2535 (if (idlwave-shell-hide-p 'debug) 'mostly)
2536 nil t))
2537
2538 (defun idlwave-shell-go ()
2539 "Run .GO. This starts the main program of the last compiled file."
2540 (interactive)
2541 (idlwave-shell-stop-line-pending)
2542 (idlwave-shell-send-command ".go" '(idlwave-shell-redisplay 'hide)
2543 (if (idlwave-shell-hide-p 'debug) 'mostly)
2544 nil t))
2545
2546 (defun idlwave-shell-return ()
2547 "Run .RETURN (continue to next return, but stay in subprogram)."
2548 (interactive)
2549 (idlwave-shell-stop-line-pending)
2550 (idlwave-shell-send-command ".return" '(idlwave-shell-redisplay 'hide)
2551 (if (idlwave-shell-hide-p 'debug) 'mostly)
2552 nil t))
2553
2554 (defun idlwave-shell-skip ()
2555 "Run .SKIP (skip one line, then step)."
2556 (interactive)
2557 (idlwave-shell-stop-line-pending)
2558 (idlwave-shell-send-command ".skip" '(idlwave-shell-redisplay 'hide)
2559 (if (idlwave-shell-hide-p 'debug) 'mostly)
2560 nil t))
2561
2562 (defun idlwave-shell-clear-bp (bp &optional no-query)
2563 "Clear breakpoint BP.
2564 Clears 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
2569 (concat "breakpoint,/clear," (int-to-string index))
2570 nil (idlwave-shell-hide-p 'breakpoint) nil t)
2571 (unless no-query (idlwave-shell-bp-query))))))
2572
2573 (defun idlwave-shell-current-frame ()
2574 "Return a list containing the current file name and line point is in.
2575 If 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)
2583 (1+ (count-lines 1 (point-at-bol)))))))
2584
2585 (defun idlwave-shell-current-module ()
2586 "Return the name of the module for the current file.
2587 Returns 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)
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)))))))
2600
2601 (defun idlwave-shell-clear-current-bp ()
2602 "Remove breakpoint at current line.
2603 This command can be called from the shell buffer if IDL is currently
2604 stopped at a breakpoint."
2605 (interactive)
2606 (let ((bp (idlwave-shell-find-current-bp)))
2607 (if bp (idlwave-shell-clear-bp bp))))
2608
2609
2610 (defun idlwave-shell-toggle-enable-current-bp (&optional bp force
2611 no-update)
2612 "Disable or enable current breakpoint or a breakpoint passed in BP.
2613 If FORCE is 'disable or 'enable, for that condition instead of
2614 toggling. If NO-UPDATE is non-nil, don't update the breakpoint
2615 list after toggling."
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))
2623 (idlwave-shell-send-command
2624 (concat "breakpoint,"
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)))))
2629
2630 (defun idlwave-shell-enable-all-bp (&optional enable no-update bpl)
2631 "Disable all breakpoints we know about which need disabling.
2632 If 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))
2637 (idlwave-shell-toggle-enable-current-bp
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))
2643
2644 (defun idlwave-shell-to-here ()
2645 "Set a breakpoint with count 1 then continue."
2646 (interactive)
2647 ;; temporarily disable all other breakpoints
2648 (let ((disabled (idlwave-shell-enable-all-bp 'disable 'no-update)))
2649 (idlwave-shell-break-here 1 nil nil nil 'no-show)
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)))
2659
2660 (defun idlwave-shell-break-in ()
2661 "Look for a module name near point and set a break point for it.
2662 The command looks for an identifier near point and sets a breakpoint
2663 for the first line of the corresponding module. If MODULE is `t', set
2664 in the current routine."
2665 (interactive)
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)))
2670 (if module
2671 (progn
2672 (setq module (idlwave-make-full-name class name))
2673 (idlwave-shell-module-source-query module type)
2674 (idlwave-shell-set-bp-in-module name type class))
2675 (error "No identifier at point"))))
2676
2677
2678 (defun idlwave-shell-set-bp-in-module (name type class)
2679 "Set breakpoint in module.
2680 Assumes that `idlwave-shell-sources-alist' contains an entry for that module."
2681 (let* ((module (idlwave-make-full-name class name))
2682 (source-file
2683 (car-safe (cdr-safe
2684 (or
2685 (assoc (upcase module)
2686 idlwave-shell-sources-alist)
2687 (nth 3 (idlwave-best-rinfo-assoc name type class
2688 (idlwave-routines)))))))
2689 buf)
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))
2699 (with-current-buffer buf
2700 (save-excursion
2701 (goto-char (point-min))
2702 (let ((case-fold-search t))
2703 (if (re-search-forward
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.
2716 Sets 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.
2743 Runs to the last statement and then steps 1 statement. Use the .out command."
2744 (interactive)
2745 (idlwave-shell-send-command ".o" nil
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))))
2775 (unless bp-line (error "No further breakpoints"))
2776 (goto-char (point-min))
2777 (forward-line (1- bp-line))))
2778
2779 ;; Examine Commands ------------------------------------------------------
2780
2781 (defun idlwave-shell-help-expression (arg)
2782 "Print help on current expression. See `idlwave-shell-print'."
2783 (interactive "P")
2784 (idlwave-shell-print arg 'help))
2785
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")
2791 (let* ((drag-track (fboundp 'mouse-drag-track))
2792 (transient-mark-mode t)
2793 (zmacs-regions t)
2794 (tracker (if (featurep 'xemacs)
2795 (if (fboundp
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))))
2803 (funcall tracker event)
2804 (idlwave-shell-print (if (idlwave-region-active-p) '(4) nil)
2805 ,help ,ev))))
2806
2807 ;; Begin terrible hack section -- XEmacs tests for button2 explicitly
2808 ;; on drag events, calling drag-n-drop code if detected. Ughhh...
2809 (defun idlwave-default-mouse-track-event-is-with-button (event n)
2810 t)
2811
2812 (defun idlwave-xemacs-hack-mouse-track (event)
2813 (if (featurep 'xemacs)
2814 (let ((oldfunc (symbol-function
2815 'default-mouse-track-event-is-with-button)))
2816 (unwind-protect
2817 (progn
2818 (fset 'default-mouse-track-event-is-with-button
2819 'idlwave-default-mouse-track-event-is-with-button)
2820 (mouse-track event))
2821 (fset 'default-mouse-track-event-is-with-button oldfunc)))))
2822 ;;; End terrible hack section
2823
2824 (defun idlwave-shell-mouse-print (event)
2825 "Print value of variable at the mouse position, with `print'."
2826 (interactive "e")
2827 (funcall (idlwave-shell-mouse-examine nil) event))
2828
2829 (defun idlwave-shell-mouse-help (event)
2830 "Print value of variable at the mouse position, with `help'."
2831 (interactive "e")
2832 (funcall (idlwave-shell-mouse-examine 'help) event))
2833
2834 (defun idlwave-shell-examine-select (event)
2835 "Pop-up a list to select from for examining the expression."
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
2845 (defvar idlwave-shell-examine-label nil
2846 "Label to include with examine text if in a separate buffer.")
2847 (defvar idlwave-shell-examine-completion-list nil)
2848
2849 (defun idlwave-shell-print (arg &optional help ev complete-help-type)
2850 "Print current expression.
2851
2852 With HELP non-nil, show help on expression. If HELP is a string,
2853 the expression will be put in place of ___, e.g.:
2854
2855 print,size(___,/DIMENSIONS)
2856
2857 HELP can also be a cons cell ( NAME . STRING ) in which case NAME will
2858 be used to label the help print-out.
2859
2860 Otherwise, print is called on the expression.
2861
2862 An expression is an identifier plus 1 pair of matched parentheses
2863 directly following the identifier - an array or function call.
2864 Alternatively, an expression is the contents of any matched
2865 parentheses when the open parenthesis is not directly preceded by an
2866 identifier. If point is at the beginning or within an expression
2867 return the inner-most containing expression, otherwise, return the
2868 preceding expression.
2869
2870 With prefix arg, or if transient mode set and the region is defined,
2871 use the current region as the expression.
2872
2873 With double prefix arg ARG prompt for an expression.
2874
2875 If EV is a valid event passed, pop-up a list from
2876 `idlwave-shell-examine-alist' from which to select the help
2877 command text. If instead COMPLETE-HELP-TYPE is non-nil, choose
2878 from `idlwave-shell-examine-alist' via mini-buffer shortcut key."
2879 (interactive "P")
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))
2885 (save-excursion
2886 (let* ((process (get-buffer-process (current-buffer)))
2887 (process-mark (if process (process-mark process)))
2888 (stack-label
2889 (if (and (integerp idlwave-shell-calling-stack-index)
2890 (> idlwave-shell-calling-stack-index 0))
2891 (format " [-%d:%s]"
2892 idlwave-shell-calling-stack-index
2893 idlwave-shell-calling-stack-routine)))
2894 expr beg end cmd)
2895 (cond
2896 ((equal arg '(16))
2897 (setq expr (read-string "Expression: ")))
2898 ((and (or arg (idlwave-region-active-p))
2899 (< (- (region-end) (region-beginning)) 2000))
2900 (setq beg (region-beginning)
2901 end (region-end)))
2902 (t
2903 (idlwave-with-special-syntax
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))
2921 (setq end (point)))))
2922
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
2932 (when (and beg end idlwave-shell-expression-overlay)
2933 (move-overlay idlwave-shell-expression-overlay beg end
2934 (current-buffer))
2935 (add-hook 'pre-command-hook
2936 'idlwave-shell-delete-expression-overlay))
2937 (add-hook 'pre-command-hook
2938 'idlwave-shell-delete-output-overlay)
2939
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
2944 (while (string-match "[ \t]*\\$[ \t]*\\(;.*\\)?\\(\n[ \t]*\\|$\\)" expr)
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)))
2949
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))
2956 (let ((help-cons
2957 (if (consp help) help
2958 (assoc
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
2964 ;; "Examine with: "
2965 ;; idlwave-shell-examine-alist nil nil nil
2966 ;; 'idlwave-shell-examine-completion-list
2967 ;; "Print")
2968 (idlwave-popup-select
2969 ev
2970 (mapcar 'car idlwave-shell-examine-alist)
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
2976 (setq idlwave-shell-examine-label
2977 (concat
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
2983 (format "==>%s<==\n%s:" expr
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)
2996 (idlwave-shell-send-command
2997 cmd
2998 'idlwave-shell-check-compiled-and-display
2999 (if idlwave-shell-separate-examine-output 'hide))))))
3000
3001 (defvar idlwave-shell-examine-window-alist nil
3002 "Variable to hold the win/height pairs for all *Examine* windows.")
3003
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
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
3017 (defun idlwave-shell-examine-display ()
3018 "View the examine command output in a separate buffer."
3019 (let (win cur-beg cur-end)
3020 (with-current-buffer (get-buffer-create "*Examine*")
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)))
3033 (beg (progn
3034 (goto-char
3035 (or (progn (if (re-search-backward
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))
3052
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)))
3058 (setq win (idlwave-display-buffer
3059 "*Examine*"
3060 nil
3061 (let ((list winlist) thiswin)
3062 (catch 'exit
3063 (save-selected-window
3064 (while (setq thiswin (pop list))
3065 (select-window thiswin)
3066 (if (one-window-p)
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))
3087 (add-to-list 'idlwave-shell-examine-window-alist
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
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)
3105 (let ((buf (get-buffer "*Examine*")))
3106 (when (bufferp buf)
3107 (with-current-buffer buf
3108 (let ((inhibit-read-only t))
3109 (erase-buffer))))))
3110
3111 (defun idlwave-retrieve-expression-from-level (expr level)
3112 "Return IDL command to print the expression EXPR from stack level LEVEL.
3113
3114 It does not seem possible to evaluate an expression on a different
3115 level than the current. Therefore, this function retrieves variables
3116 by reference from other levels, and then includes that variable in
3117 place of the chosen one.
3118
3119 Since this function depends upon the undocumented IDL routine
3120 routine_names, there is no guarantee that this will work with future
3121 versions of IDL."
3122 (let ((fetch (- 0 level))
3123 (start 0)
3124 var fetch-start fetch-end pre post)
3125
3126 ;; FIXME: In the following we try to find the variables in expression
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 ()
3131
3132 (while (string-match
3133 "\\(\\`\\|[^a-zA-Z0-9$_][ \t]*\\)\\([a-zA-Z][a-zA-Z0-9$_]*\\)\\([ \t]*[^a-zA-Z0-9$_]\\|\\'\\)" expr start)
3134 (setq var (match-string 2 expr)
3135 start (match-end 2)
3136 pre (substring expr 0 (match-beginning 2))
3137 post (substring expr (match-end 2)))
3138 (cond
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
3149 ((and (string-match "\\([\"\']\\)[^\1]*$" pre)
3150 (string-match (concat "^[^" (match-string 1 pre) "]*"
3151 (match-string 1 pre)) post))
3152 (setq start (+ start (match-end 0))))
3153
3154
3155 ;; seems to be a variable - delimit its name
3156 (t
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)
3169 (format "(routine_names('%s',fetch=%d))"
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)))
3176 expr))
3177
3178
3179 (defun idlwave-shell-help-statement (help expr)
3180 "Construct a help statement for printing expression EXPR.
3181
3182 HELP can be non-nil for `help,', nil for 'print,' or any string into which
3183 to insert expression in place of the marker ___, e.g.: print,
3184 size(___,/DIMENSIONS)"
3185 (cond
3186 ((null help)
3187 (concat "idlwave_print_safe, " expr ","
3188 (number-to-string idlwave-shell-max-print-length)))
3189 ((stringp help)
3190 (if (string-match "\\(^\\|[^_]\\)\\(___\\)\\([^_]\\|$\\)" help)
3191 (concat (substring help 0 (match-beginning 2))
3192 expr
3193 (substring help (match-end 2)))))
3194 (t
3195 (concat "help, " expr))))
3196
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)
3204 (with-current-buffer buffer
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)))
3211
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)
3216 (move-overlay idlwave-shell-output-overlay
3217 output-begin output-end buffer))))
3218
3219 (defun idlwave-shell-delete-output-overlay ()
3220 (unless (or (eq this-command 'idlwave-shell-mouse-nop)
3221 (eq this-command 'handle-switch-frame))
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)))
3227
3228 (defun idlwave-shell-delete-expression-overlay ()
3229 (unless (or (eq this-command 'idlwave-shell-mouse-nop)
3230 (eq this-command 'handle-switch-frame))
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)))
3236
3237 (defvar idlwave-shell-bp-alist nil
3238 "Alist of breakpoints.
3239 A breakpoint is a cons cell \(\(file line\) . \(\(index module\) data\)\)
3240
3241 The car is the `frame' for the breakpoint:
3242 file - full path file name.
3243 line - line number of breakpoint - integer.
3244
3245 The first element of the cdr is a list of internal IDL data:
3246 index - the index number of the breakpoint internal to IDL.
3247 module - the module for breakpoint internal to IDL.
3248
3249 Remaining elements of the cdr:
3250 data - Data associated with the breakpoint by idlwave-shell currently
3251 contains four items:
3252
3253 count - number of times to execute breakpoint. When count reaches 0
3254 the breakpoint is cleared and removed from the alist.
3255
3256 command - command to execute when breakpoint is reached, either a
3257 lisp function to be called with `funcall' with no arguments or a
3258 list to be evaluated with `eval'.
3259
3260 condition - any condition to apply to the breakpoint.
3261
3262 disabled - whether the bp is disabled.")
3263
3264 (defun idlwave-shell-run-region (beg end &optional n)
3265 "Compile and run the region using the IDL process.
3266 Copies the region to a temporary file `idlwave-shell-temp-pro-file'
3267 and issues the IDL .run command for the file. Because the region
3268 is compiled and run as a main program there is no problem with
3269 begin-end blocks extending over multiple lines - which would be
3270 a problem if `idlwave-shell-evaluate-region' was used.
3271 An END statement is appended to the region if necessary.
3272
3273 If there is a prefix argument, display IDL process."
3274 (interactive "r\nP")
3275 (let ((oldbuf (current-buffer)))
3276 (with-current-buffer (idlwave-find-file-noselect
3277 (idlwave-shell-temp-file 'pro) 'tmp)
3278 (set (make-local-variable 'comment-start-skip) ";+[ \t]*")
3279 (set (make-local-variable 'comment-start) ";")
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)))
3287 (idlwave-shell-send-command (concat ".run \""
3288 idlwave-shell-temp-pro-file "\"")
3289 nil
3290 (if (idlwave-shell-hide-p 'run) 'mostly)
3291 nil t)
3292 (if n
3293 (idlwave-display-buffer (idlwave-shell-buffer)
3294 nil (idlwave-shell-shell-frame))))
3295
3296 (defun idlwave-shell-evaluate-region (beg end &optional n)
3297 "Send region to the IDL process.
3298 If there is a prefix argument, display IDL process.
3299 Does 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
3304 (idlwave-display-buffer (idlwave-shell-buffer)
3305 nil (idlwave-shell-shell-frame))))
3306
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
3322 (defun idlwave-display-buffer (buf not-this-window-p &optional frame)
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)))
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)))))))
3343 ; (if (not (frame-live-p frame)) (setq frame nil))
3344 ; (display-buffer buf not-this-window-p frame))
3345
3346 (defvar idlwave-shell-bp-buffer " *idlwave-shell-bp*"
3347 "Scratch buffer for parsing IDL breakpoint lists and other stuff.")
3348
3349 (defun idlwave-shell-bp-query (&optional no-show)
3350 "Reconcile idlwave-shell's breakpoint list with IDL's.
3351 Queries IDL using the string in `idlwave-shell-bp-query'."
3352 (interactive)
3353 (idlwave-shell-send-command idlwave-shell-bp-query
3354 `(progn
3355 (idlwave-shell-filter-bp (quote ,no-show)))
3356 'hide))
3357
3358 (defun idlwave-shell-bp-get (bp &optional item)
3359 "Get a value for a breakpoint.
3360 BP has the form of elements in `idlwave-shell-bp-alist'.
3361 Optional second arg ITEM is the particular value to retrieve.
3362 ITEM can be 'file, 'line, 'index, 'module, 'count, 'cmd,
3363 'condition, 'disabled, 'type, or 'data. 'data returns a list
3364 of 'count, 'cmd and 'condition. Defaults to 'index."
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))))
3373 ((eq item 'condition) (nth 2 (cdr (cdr bp))))
3374 ((eq item 'disabled) (nth 3 (cdr (cdr bp))))
3375 ;; IDL breakpoint info
3376 ((eq item 'module)
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))))
3382 ;; index - default
3383 (t (nth 0 (car (cdr bp))))))
3384
3385 (defun idlwave-shell-filter-bp (&optional no-show)
3386 "Get the breakpoints from `idlwave-shell-command-output'.
3387 Create `idlwave-shell-bp-alist' updating breakpoint count and command
3388 data from previous breakpoint list. If NO-SHOW is set, don't update
3389 the breakpoint overlays."
3390 (with-current-buffer (get-buffer-create idlwave-shell-bp-buffer)
3391 (erase-buffer)
3392 (insert idlwave-shell-command-output)
3393 (goto-char (point-min))
3394 (let ((old-bp-alist idlwave-shell-bp-alist)
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-+\\)")
3398 (bp-re55
3399 (concat
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
3409 file line index module
3410 count condition disabled
3411 bp-re indmap)
3412 (setq idlwave-shell-bp-alist (list nil))
3413 ;; Search for either header type, and set the correct regexp
3414 (when (or
3415 (if (re-search-forward "^\\s-*Index.*\n\\s-*-" nil t)
3416 (setq bp-re bp-re54 ; versions <= 5.4
3417 indmap '(1 2 3 4))) ;index module line file
3418 (if (re-search-forward
3419 "^\\s-*Index\\s-*Line\\s-*Attributes\\s-*File" nil t)
3420 (setq bp-re bp-re55 ; versions >= 5.5
3421 indmap '(1 6 2 16)))) ; index module line file
3422 ;; There seems to be a breakpoint listing here, parse breakpoint lines.
3423 (while (re-search-forward bp-re nil t)
3424 (setq index (string-to-number (match-string (nth 0 indmap)))
3425 module (match-string (nth 1 indmap))
3426 line (string-to-number (match-string (nth 2 indmap)))
3427 file (idlwave-shell-file-name (match-string (nth 3 indmap))))
3428 (if (eq bp-re bp-re55)
3429 (setq count (if (match-string 10) 1
3430 (if (match-string 8)
3431 (string-to-number (match-string 8))))
3432 condition (match-string 13)
3433 disabled (not (null (match-string 15)))))
3434
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)
3440 ;; bp data: count, command, condition, disabled
3441 count nil condition disabled))))))
3442 (setq idlwave-shell-bp-alist (cdr idlwave-shell-bp-alist))
3443 ;; Update breakpoint data
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))))
3447 ;; Update the breakpoint overlays
3448 (unless no-show (idlwave-shell-update-bp-overlays))
3449 ;; Return the new list
3450 idlwave-shell-bp-alist)
3451
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)
3456 "Update BP data in breakpoint list.
3457 If BP frame is in `idlwave-shell-bp-alist' updates the breakpoint data."
3458 (let ((match (assoc (car bp) idlwave-shell-bp-alist)))
3459 (if match
3460 (if command-only
3461 (setf (nth 1 (cdr (cdr match))) (nth 1 (cdr (cdr match))))
3462 (setcdr (cdr match) (cdr (cdr bp)))))))
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)
3469 "Create a breakpoint structure containing FRAME and DATA.
3470 Second and third args, DATA and MODULE, are optional. Returns
3471 a breakpoint of the format used in `idlwave-shell-bp-alist'.
3472 Can be used in commands attempting match a breakpoint in
3473 `idlwave-shell-bp-alist'."
3474 (cons frame ;; (file line)
3475 (cons (list nil module) ;; (index_id (module type) | module)
3476 data))) ;; (count command condition disabled)
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.
3483 If an equivalency is found, return the IDL internal source name.
3484 Otherwise return the filename in BP."
3485 (let*
3486 ((bp-file (idlwave-shell-bp-get bp 'file))
3487 (bp-module (idlwave-shell-bp-get bp 'module))
3488 (internal-file-list
3489 (if bp-module
3490 (cdr (assoc bp-module idlwave-shell-sources-alist)))))
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
3496 (defun idlwave-shell-set-bp (bp &optional no-show)
3497 "Try to set a breakpoint BP.
3498 The breakpoint will be placed at the beginning of the statement on the
3499 line specified by BP or at the next IDL statement if that line is not
3500 a statement. Determines IDL's internal representation for the
3501 breakpoint, which may have occurred at a different line than
3502 specified. If NO-SHOW is non-nil, don't do any updating."
3503 ;; Get and save the old breakpoints
3504 (idlwave-shell-send-command
3505 idlwave-shell-bp-query
3506 `(progn
3507 (idlwave-shell-filter-bp (quote ,no-show))
3508 (setq idlwave-shell-old-bp idlwave-shell-bp-alist))
3509 'hide)
3510
3511 ;; Get sources for this routine in the sources list
3512 (idlwave-shell-module-source-query (idlwave-shell-bp-get bp 'module)
3513 (idlwave-shell-bp-get bp 'type))
3514 (let*
3515 ((count (idlwave-shell-bp-get bp 'count))
3516 (condition (idlwave-shell-bp-get bp 'condition))
3517 (disabled (idlwave-shell-bp-get bp 'disabled))
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")))
3530 (line (idlwave-shell-bp-get bp 'line)))
3531 (idlwave-shell-send-command
3532 (concat "breakpoint,'"
3533 (idlwave-shell-sources-bp bp) "',"
3534 (if (integerp line) (setq line (int-to-string line)))
3535 key)
3536 ;; Check for failure and adjust breakpoint to match IDL's list
3537 `(progn
3538 (if (idlwave-shell-set-bp-check (quote ,bp))
3539 (idlwave-shell-set-bp-adjust (quote ,bp) (quote ,no-show))))
3540 ;; hide output?
3541 (idlwave-shell-hide-p 'breakpoint)
3542 'preempt t)))
3543
3544 (defun idlwave-shell-set-bp-adjust (bp &optional no-show)
3545 "Find the breakpoint in IDL's internal list of breakpoints."
3546 (idlwave-shell-send-command
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))
3555
3556 (defun idlwave-shell-find-bp (frame)
3557 "Return breakpoint from `idlwave-shell-bp-alist' for frame.
3558 Returns nil if frame not found."
3559 (assoc frame idlwave-shell-bp-alist))
3560
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
3575 (defun idlwave-shell-new-bp (bp)
3576 "Find the new breakpoint in IDL's list and update with DATA.
3577 The actual line number for a breakpoint in IDL may be different than
3578 the line number used with the IDL breakpoint command.
3579 Looks for a new breakpoint index number in the list. This is
3580 considered 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
3602 "Alist of overlays marking breakpoints.")
3603 (defvar idlwave-shell-bp-glyph)
3604
3605 (defvar idlwave-shell-debug-line-map (make-sparse-keymap))
3606 (define-key idlwave-shell-debug-line-map
3607 (if (featurep 'xemacs) [button3] [mouse-3])
3608 'idlwave-shell-mouse-active-bp)
3609
3610 (defun idlwave-shell-update-bp-overlays ()
3611 "Update the overlays which mark breakpoints in the source code.
3612 Existing overlays are recycled, in order to minimize consumption."
3613 (when idlwave-shell-mark-breakpoints
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
3620 ;; Delete the old overlays from their buffers
3621 (if ov-alist
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))))
3626
3627 (setq ov-alist idlwave-shell-bp-overlays
3628 idlwave-shell-bp-overlays
3629 (if idlwave-shell-bp-glyph
3630 (mapcar 'list (mapcar 'car idlwave-shell-bp-glyph))
3631 (list (list 'bp))))
3632 (while (setq bp (pop bp-list))
3633 (save-excursion
3634 (idlwave-shell-goto-frame (car bp))
3635 (let* ((end (point-at-eol))
3636 (beg (progn (beginning-of-line 1) (point)))
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))
3652 (help-list
3653 (delq nil
3654 (list
3655 (if count
3656 (concat "after:" (int-to-string count)))
3657 (if condition
3658 (concat "condition:" condition))
3659 (if disabled "disabled"))))
3660 (help-text (concat
3661 "BP "
3662 (int-to-string (idlwave-shell-bp-get bp))
3663 (if help-list
3664 (concat
3665 " - "
3666 (mapconcat 'identity help-list ", ")))
3667 (if (and (not count) (not condition))
3668 " (use mouse-3 for breakpoint actions)")))
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)))
3676 (idlwave-shell-make-new-bp-overlay type disabled)))
3677 match)
3678 (if idlwave-shell-breakpoint-popup-menu
3679 (overlay-put ov 'help-echo help-text))
3680 (move-overlay ov beg end)
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
3687 (if old-buffers
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))
3691 (if (< left-margin-width 2)
3692 (setq left-margin-width 2)))
3693 (let ((window (get-buffer-window (current-buffer) 0)))
3694 (if window
3695 (set-window-margins
3696 window left-margin-width right-margin-width))))))
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))
3703 (let ((window (get-buffer-window buf 0)))
3704 (if window
3705 (set-window-margins
3706 window left-margin-width right-margin-width)))))))))
3707
3708 (defun idlwave-shell-make-new-bp-overlay (&optional type disabled)
3709 "Make a new overlay for highlighting breakpoints.
3710
3711 This stuff is strongly dependant upon the version of Emacs. If TYPE
3712 is passed, make an overlay of that type ('bp or 'bp-cond, currently
3713 only for glyphs)."
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))
3718 (face (if disabled
3719 idlwave-shell-disabled-breakpoint-face
3720 idlwave-shell-breakpoint-face)))
3721 (if (featurep 'xemacs)
3722 ;; This is XEmacs
3723 (progn
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
3728 (cond
3729 ;; tty's cannot display glyphs
3730 ((eq (console-type) 'tty)
3731 (set-extent-property ov 'face face))
3732
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
3741 (idlwave-shell-mark-breakpoints
3742 (set-extent-property ov 'face face))
3743
3744 ;; no marking
3745 (t nil))
3746 (set-extent-priority ov -1)) ; make stop line face prevail
3747 ;; This is Emacs
3748 (when idlwave-shell-breakpoint-popup-menu
3749 (overlay-put ov 'mouse-face 'highlight)
3750 (overlay-put ov 'keymap idlwave-shell-debug-line-map))
3751 (cond
3752 (window-system
3753 (if use-glyph
3754 (let ((image-props (cdr (assq type idlwave-shell-bp-glyph)))
3755 string)
3756
3757 (if disabled (setq image-props
3758 (append image-props
3759 (list :conversion 'disabled))))
3760 (setq string
3761 (propertize "@"
3762 'display
3763 (list (list 'margin 'left-margin)
3764 image-props)))
3765 (overlay-put ov 'before-string string))
3766 ;; just the face
3767 (overlay-put ov 'face face)))
3768
3769 ;; use a face
3770 (idlwave-shell-mark-breakpoints
3771 (overlay-put ov 'face face))
3772
3773 ;; No marking
3774 (t nil)))
3775 ov))
3776
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))
3790 (setq select (idlwave-popup-select
3791 ev
3792 (delq nil
3793 (list (if disabled "Enable" "Disable")
3794 "Clear"
3795 "Clear All"
3796 (if condition "Remove Condition" "Add Condition")
3797 (if condition "Change Condition")
3798 (if count "Remove Repeat Count"
3799 "Add Repeat Count")
3800 (if count "Change Repeat Count")))
3801 (concat "BreakPoint " index)))
3802 (if select
3803 (cond
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)
3809 (idlwave-shell-break-here count cmd
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))
3822 (t
3823 (message "Unimplemented: %s" select))))))
3824
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.
3833 Also with prefix arg, ask for the command. You can also use the command
3834 `idlwave-shell-edit-default-command-line' to edit the line."
3835 (interactive "P")
3836 (cond
3837 ((equal arg '(16))
3838 (setq idlwave-shell-command-line-to-execute nil))
3839 ((equal arg '(4))
3840 (setq idlwave-shell-command-line-to-execute
3841 (read-string "IDL> " idlwave-shell-command-line-to-execute))))
3842 (idlwave-shell-reset 'hidden)
3843 (idlwave-shell-send-command
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)))
3848
3849 (defun idlwave-shell-save-and-run ()
3850 "Save file and run it in IDL.
3851 Runs `save-buffer' and sends a '.RUN' command for the associated file to IDL.
3852 When called from the shell buffer, re-run the file which was last handled by
3853 one of the save-and-.. commands."
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.
3859 Runs `save-buffer' and sends '.COMPILE' command for the associated file to IDL.
3860 When called from the shell buffer, re-compile the file which was last handled by
3861 one 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.
3867 Runs `save-buffer' and sends a '@file' command for the associated file to IDL.
3868 When called from the shell buffer, re-batch the file which was last handled by
3869 one of the save-and-.. commands."
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.
3875 Runs `save-buffer' and sends a '.RUN' command for the associated file to IDL.
3876 When called from the shell buffer, re-compile the file which was last
3877 handled by this command."
3878 ;; Remove the stop overlay.
3879 (if idlwave-shell-stop-line-overlay
3880 (delete-overlay idlwave-shell-stop-line-overlay))
3881 (if idlwave-shell-is-stopped
3882 (idlwave-shell-electric-debug-all-off))
3883 (setq idlwave-shell-is-stopped nil)
3884 (setq overlay-arrow-string nil)
3885 (let (buf)
3886 (cond
3887 ((derived-mode-p 'idlwave-mode)
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))
3893 (with-current-buffer buf
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)))
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)
3909 (if (idlwave-shell-hide-p 'run) 'mostly) nil t)
3910 (idlwave-shell-bp-query))
3911 (let ((msg (format "No such file %s"
3912 idlwave-shell-last-save-and-action-file)))
3913 (setq idlwave-shell-last-save-and-action-file nil)
3914 (error msg))))
3915
3916 (defun idlwave-shell-maybe-update-routine-info (&optional wait file)
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)
3923 (idlwave-shell-update-routine-info t nil wait file)))
3924
3925 (defvar idlwave-shell-sources-query "help,/source,/full"
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.
3930 Elements of the alist have the form:
3931
3932 (module name . (source-file-truename idlwave-internal-filename))")
3933
3934 (defun idlwave-shell-module-source-query (module &optional type)
3935 "Determine the source file for a given module.
3936 Query as a function if TYPE set to something beside 'pro."
3937 (if module
3938 (idlwave-shell-send-command
3939 (format "print,(routine_info('%s',/SOURCE%s)).PATH" module
3940 (if (eq type 'pro) "" ",/FUNCTIONS"))
3941 `(idlwave-shell-module-source-filter ,module)
3942 'hide 'wait)))
3943
3944 (defun idlwave-shell-module-source-filter (module)
3945 "Get module source, and update `idlwave-shell-sources-alist'."
3946 (let ((old (assoc (upcase module) idlwave-shell-sources-alist))
3947 filename)
3948 (when (string-match "\.PATH *[\n\r]\\([^%][^\r\n]+\\)[\n\r]"
3949 idlwave-shell-command-output)
3950 (setq filename (substring idlwave-shell-command-output
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
3955 (append idlwave-shell-sources-alist
3956 (list (cons (upcase module)
3957 (list (idlwave-shell-file-name filename)
3958 filename)))))))))
3959
3960 (defun idlwave-shell-sources-query ()
3961 "Determine source files for all IDL compiled procedures.
3962 Queries IDL using the string in `idlwave-shell-sources-query'."
3963 (interactive)
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.
3970 Create `idlwave-shell-sources-alist' consisting of list elements
3971 of the form:
3972 (module name . (source-file-truename idlwave-internal-filename))"
3973 (with-current-buffer (get-buffer-create idlwave-shell-bp-buffer)
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
3989 (narrow-to-region cpro (point-at-bol))
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
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)
4026 (mapcar (lambda (x) (idlwave-shell-clear-bp x 'no-query))
4027 idlwave-shell-bp-alist)
4028 (idlwave-shell-bp-query))
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)
4044 (with-current-buffer idlwave-shell-error-buffer
4045 (goto-char idlwave-shell-error-last)
4046 (if (or
4047 (re-search-forward idlwave-shell-syntax-error nil t)
4048 (re-search-forward idlwave-shell-other-error nil t))
4049 (progn
4050 (setq frame
4051 (list
4052 (save-match-data
4053 (idlwave-shell-file-name
4054 (buffer-substring (match-beginning 1 )
4055 (match-end 1))))
4056 (string-to-number
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
4068 (idlwave-shell-display-line frame col 'disable))
4069 (beep)
4070 (message "No more errors."))))
4071
4072 (defun idlwave-shell-file-name (name)
4073 "If `idlwave-shell-use-truename' is non-nil, convert file name to true name.
4074 Otherwise, just expand the file name."
4075 (let ((def-dir (if (derived-mode-p 'idlwave-shell-mode)
4076 default-directory
4077 idlwave-shell-default-directory)))
4078 (if idlwave-shell-use-truename
4079 (file-truename name def-dir)
4080 (expand-file-name name def-dir))))
4081
4082 ;; Keybindings ------------------------------------------------------------
4083
4084 (defvar idlwave-shell-mode-map (copy-keymap comint-mode-map)
4085 "Keymap for `idlwave-mode'.")
4086 (defvar idlwave-shell-electric-debug-mode-map (make-sparse-keymap))
4087 (defvar idlwave-shell-mode-prefix-map (make-sparse-keymap))
4088 (fset 'idlwave-shell-mode-prefix-map idlwave-shell-mode-prefix-map)
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))
4096
4097 ;(define-key idlwave-shell-mode-map "\M-?" 'comint-dynamic-list-completions)
4098 ;(define-key idlwave-shell-mode-map "\t" 'comint-dynamic-complete)
4099
4100 (define-key idlwave-shell-mode-map "\C-w" 'comint-kill-region)
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)
4105 (define-key idlwave-shell-mode-map "\C-g" 'idlwave-keyboard-quit)
4106 (define-key idlwave-shell-mode-map "\M-?" 'idlwave-context-help)
4107 (define-key idlwave-shell-mode-map [(control meta ?\?)]
4108 'idlwave-help-assistant-help-with-topic)
4109 (define-key idlwave-shell-mode-map "\C-c\C-i" 'idlwave-update-routine-info)
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)
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)
4114 (define-key idlwave-shell-mode-map "\C-c\C-k" 'idlwave-kill-autoloaded-buffers)
4115 (define-key idlwave-shell-mode-map idlwave-shell-prefix-key
4116 'idlwave-shell-debug-map)
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)
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)
4121
4122 ;; The mouse bindings for PRINT and HELP
4123 (idlwave-shell-define-key-both
4124 (if (featurep 'xemacs)
4125 [(shift button2)]
4126 [(shift down-mouse-2)])
4127 'idlwave-shell-mouse-print)
4128 (idlwave-shell-define-key-both
4129 (if (featurep 'xemacs)
4130 [(control meta button2)]
4131 [(control meta down-mouse-2)])
4132 'idlwave-shell-mouse-help)
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
4139 (define-key idlwave-shell-mode-map
4140 (if (featurep 'xemacs)
4141 [(shift button3)]
4142 [(shift mouse-3)])
4143 'idlwave-mouse-context-help)
4144
4145 ;; For Emacs, we need to turn off the button release events.
4146 (defun idlwave-shell-mouse-nop (event)
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))
4155
4156
4157 ;; The following set of bindings is used to bind the debugging keys.
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.
4165 ;;
4166 ;; [C-c C-d]-binding debug-modifier-key command bind-electric-debug buf-only
4167 ;; Used keys: abcdef hijklmnopqrstuvwxyz
4168 ;; Unused keys: g
4169 (let* ((specs
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)
4184 ([(control ?y)] ?y idlwave-shell-execute-default-command-line)
4185 ([(control ?z)] ?z idlwave-shell-reset t)
4186 ([(control ?q)] ?q idlwave-shell-quit)
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)
4190 ([(control ?x)] ?x idlwave-shell-goto-next-error)
4191 ([(control ?c)] ?c idlwave-shell-save-and-run t)
4192 ([( ?@)] ?@ idlwave-shell-save-and-batch)
4193 ([(control ?e)] ?e idlwave-shell-run-region)
4194 ([(control ?w)] ?w idlwave-shell-resync-dirs)
4195 ([(control ?l)] ?l idlwave-shell-redisplay t)
4196 ([(control ?t)] ?t idlwave-shell-toggle-toolbar)
4197 ([(control up)] up idlwave-shell-stack-up)
4198 ([(control down)] down idlwave-shell-stack-down)
4199 ([( ?[)] ?[ idlwave-shell-goto-previous-bp t t)
4200 ([( ?])] ?] idlwave-shell-goto-next-bp t t)
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)))
4210 s k1 c2 k2 cmd electric only-buffer cannotshift)
4211 (while (setq s (pop specs))
4212 (setq k1 (nth 0 s)
4213 c2 (nth 1 s)
4214 cmd (nth 2 s)
4215 electric (nth 3 s)
4216 only-buffer (nth 4 s)
4217 cannotshift (and shift (characterp c2) (eq c2 (upcase c2))))
4218
4219 ;; The regular prefix keymap.
4220 (when (and idlwave-shell-activate-prefix-keybindings k1)
4221 (unless only-buffer
4222 (define-key idlwave-shell-mode-prefix-map k1 cmd))
4223 (define-key idlwave-mode-prefix-map k1 cmd))
4224 ;; The debug modifier map
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)))))
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))
4235 (define-key idlwave-shell-electric-debug-mode-map (char-to-string c2)
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)
4242 (define-key idlwave-shell-electric-debug-mode-map "-"
4243 'idlwave-shell-stack-down)
4244 (define-key idlwave-shell-electric-debug-mode-map "_"
4245 'idlwave-shell-stack-down)
4246 (define-key idlwave-shell-electric-debug-mode-map "e"
4247 '(lambda () (interactive) (idlwave-shell-print '(16))))
4248 (define-key idlwave-shell-electric-debug-mode-map "q" 'idlwave-shell-retall)
4249 (define-key idlwave-shell-electric-debug-mode-map "t"
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)
4253 (define-key idlwave-shell-electric-debug-mode-map "x"
4254 '(lambda (arg) (interactive "P")
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)
4260 (fset 'idlwave-shell-debug-map idlwave-shell-mode-prefix-map)
4261
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
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)))
4276
4277 (defvar idlwave-shell-electric-debug-read-only)
4278 (defvar idlwave-shell-electric-debug-buffers nil)
4279
4280 (define-minor-mode idlwave-shell-electric-debug-mode
4281 "Toggle Electric Debug mode.
4282 With no argument, this command toggles the mode.
4283 Non-null prefix argument turns on the mode.
4284 Null prefix argument turns off the mode.
4285
4286 When Electric Debug mode is enabled, the many debugging commands are
4287 available as single key sequences."
4288 nil
4289 " *Debugging*"
4290 idlwave-shell-electric-debug-mode-map)
4291
4292 (add-hook
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
4300 (overlay-put idlwave-shell-stop-line-overlay 'face
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
4306 (add-hook
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
4315 (overlay-put idlwave-shell-stop-line-overlay 'face
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)
4322 "Print out an entrance message."
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
4329 (defvar idlwave-shell-electric-debug-buffers nil)
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)
4338 (when (and (derived-mode-p 'idlwave-mode)
4339 buffer-file-name
4340 idlwave-shell-electric-debug-mode)
4341 (idlwave-shell-electric-debug-mode 0))))))
4342 (setq idlwave-shell-electric-debug-buffers nil))
4343
4344 ;; Show the help text
4345 (defun idlwave-shell-electric-debug-help ()
4346 (interactive)
4347 (with-output-to-temp-buffer "*IDLWAVE Electric Debug Help*"
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
4356
4357 ;; The Menus --------------------------------------------------------------
4358 (defvar idlwave-shell-menu-def
4359 `("Debug"
4360 ["Electric Debug Mode"
4361 idlwave-shell-electric-debug-mode
4362 :style toggle :selected idlwave-shell-electric-debug-mode
4363 :included (derived-mode-p 'idlwave-mode) :keys "C-c C-d C-v"]
4364 "--"
4365 ("Compile & Run"
4366 ["Save and .RUN" idlwave-shell-save-and-run
4367 (or (derived-mode-p 'idlwave-mode)
4368 idlwave-shell-last-save-and-action-file)]
4369 ["Save and .COMPILE" idlwave-shell-save-and-compile
4370 (or (derived-mode-p 'idlwave-mode)
4371 idlwave-shell-last-save-and-action-file)]
4372 ["Save and @Batch" idlwave-shell-save-and-batch
4373 (or (derived-mode-p 'idlwave-mode)
4374 idlwave-shell-last-save-and-action-file)]
4375 "--"
4376 ["Goto Next Error" idlwave-shell-goto-next-error t]
4377 "--"
4378 ["Compile and Run Region" idlwave-shell-run-region
4379 (derived-mode-p 'idlwave-mode)]
4380 ["Evaluate Region" idlwave-shell-evaluate-region
4381 (derived-mode-p 'idlwave-mode)]
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"
4386 ["Set Breakpoint" idlwave-shell-break-here
4387 :keys "C-c C-d C-b" :active (derived-mode-p 'idlwave-mode)]
4388 ("Set Special Breakpoint"
4389 ["Set After Count Breakpoint"
4390 (progn
4391 (let ((count (string-to-number (read-string "Break after count: "))))
4392 (if (integerp count) (idlwave-shell-break-here count))))
4393 :active (derived-mode-p 'idlwave-mode)]
4394 ["Set Condition Breakpoint"
4395 (idlwave-shell-break-here '(4))
4396 :active (derived-mode-p 'idlwave-mode)])
4397 ["Break in Module" idlwave-shell-break-in
4398 :keys "C-c C-d C-i" :active (derived-mode-p 'idlwave-mode)]
4399 ["Break in this Module" idlwave-shell-break-this-module
4400 :keys "C-c C-d C-j" :active (derived-mode-p 'idlwave-mode)]
4401 ["Clear Breakpoint" idlwave-shell-clear-current-bp t]
4402 ["Clear All Breakpoints" idlwave-shell-clear-all-bp t]
4403 ["Disable/Enable Breakpoint" idlwave-shell-toggle-enable-current-bp t]
4404 ["Goto Previous Breakpoint" idlwave-shell-goto-previous-bp
4405 :keys "C-c C-d [" :active (derived-mode-p 'idlwave-mode)]
4406 ["Goto Next Breakpoint" idlwave-shell-goto-next-bp
4407 :keys "C-c C-d ]" :active (derived-mode-p 'idlwave-mode)]
4408 ["List All Breakpoints" idlwave-shell-list-all-bp t]
4409 ["Resync Breakpoints" idlwave-shell-bp-query t])
4410 ("Continue/Step"
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
4419 :keys "C-c C-d C-h" :active (derived-mode-p 'idlwave-mode)])
4420 ("Examine Expressions"
4421 ["Print expression" idlwave-shell-print t]
4422 ["Help on expression" idlwave-shell-help-expression t]
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"
4432 ["Stack Up" idlwave-shell-stack-up t]
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)
4438 (progn
4439 (setq idlwave-shell-show-commands
4440 (get 'idlwave-shell-show-commands 'last-val))
4441 (put 'idlwave-shell-show-commands 'last-val nil))
4442 (put 'idlwave-shell-show-commands 'last-val
4443 idlwave-shell-show-commands)
4444 (setq idlwave-shell-show-commands 'everything))
4445 :style toggle :selected (and (not (listp idlwave-shell-show-commands))
4446 (eq idlwave-shell-show-commands
4447 'everything))]
4448 "--"
4449 ["Compiling Commands" (idlwave-shell-add-or-remove-show 'run)
4450 :style toggle
4451 :selected (not (idlwave-shell-hide-p
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)
4456 :style toggle
4457 :selected (not (idlwave-shell-hide-p
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)
4462 :style toggle
4463 :selected (not (idlwave-shell-hide-p
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)
4468 :style toggle
4469 :selected (not (idlwave-shell-hide-p
4470 'misc
4471 (get 'idlwave-shell-show-commands 'last-val)))
4472 :active (not (eq idlwave-shell-show-commands 'everything))])
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]
4483 ["Save Path Info"
4484 (idlwave-shell-send-command idlwave-shell-path-query
4485 'idlwave-shell-get-path-info
4486 'hide)
4487 t]
4488 ["Reset IDL" idlwave-shell-reset t]
4489 "--"
4490 ["Toggle Toolbar" idlwave-shell-toggle-toolbar t]
4491 ["Exit IDL" idlwave-shell-quit t]))
4492
4493 (if (or (featurep 'easymenu) (load "easymenu" t))
4494 (progn
4495 (easy-menu-define
4496 idlwave-mode-debug-menu idlwave-mode-map "IDL debugging menus"
4497 idlwave-shell-menu-def)
4498 (easy-menu-define
4499 idlwave-shell-mode-menu idlwave-shell-mode-map "IDL shell menus"
4500 idlwave-shell-menu-def)
4501 (save-current-buffer
4502 (dolist (buf (buffer-list))
4503 (set-buffer buf)
4504 (if (derived-mode-p 'idlwave-mode)
4505 (progn
4506 (easy-menu-remove idlwave-mode-debug-menu)
4507 (easy-menu-add idlwave-mode-debug-menu)))))))
4508
4509 ;; The Breakpoint Glyph -------------------------------------------------------
4510
4511 (defvar idlwave-shell-bp-glyph nil
4512 "The glyphs to mark breakpoint lines in the source code.")
4513
4514 (let ((image-alist
4515 '((bp . "/* XPM */
4516 static char * file[] = {
4517 \"14 12 3 1\",
4518 \" c None s backgroundColor\",
4519 \". c #4B4B4B4B4B4B\",
4520 \"R c #FFFF00000000\",
4521 \" \",
4522 \" .... \",
4523 \" .RRRR. \",
4524 \" .RRRRRR. \",
4525 \" .RRRRRRRR. \",
4526 \" .RRRRRRRR. \",
4527 \" .RRRRRRRR. \",
4528 \" .RRRRRRRR. \",
4529 \" .RRRRRR. \",
4530 \" .RRRR. \",
4531 \" .... \",
4532 \" \"};")
4533 (bp-cond . "/* XPM */
4534 static 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 */
4553 static 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 */
4572 static 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 */
4591 static 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 */
4610 static char * file[] = {
4611 \"14 12 4 1\",
4612 \" c None s backgroundColor\",
4613 \". c #4B4B4B4B4B4B\",
4614 \"X c #FFFF00000000\",
4615 \"o c #000000000000\",
4616 \" \",
4617 \" .... \",
4618 \" .XXXX. \",
4619 \" .XoXXoX. \",
4620 \" .XXoXXoXX. \",
4621 \" .XXooooXX. \",
4622 \" .XXXXooXX. \",
4623 \" .XXXXooXX. \",
4624 \" .XXXooX. \",
4625 \" .XXXX. \",
4626 \" .... \",
4627 \" \"};")
4628 (bp-n . "/* XPM */
4629 static char * file[] = {
4630 \"14 12 4 1\",
4631 \" c None s backgroundColor\",
4632 \". c #4B4B4B4B4B4B\",
4633 \"X c #FFFF00000000\",
4634 \"o c #000000000000\",
4635 \" \",
4636 \" .... \",
4637 \" .XXXX. \",
4638 \" .XXXXXX. \",
4639 \" .XXoXoXXX. \",
4640 \" .XXooXoXX. \",
4641 \" .XXoXXoXX. \",
4642 \" .XXoXXoXX. \",
4643 \" .XoXXoX. \",
4644 \" .XXXX. \",
4645 \" .... \",
4646 \" \"};"))) im-cons im)
4647
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))
4660 (list 'image :type 'xpm :data (cdr im-cons)
4661 :ascent 'center))
4662 (t nil)))
4663 (if im (push (cons (car im-cons) im) idlwave-shell-bp-glyph))))
4664
4665 (provide 'idlw-shell)
4666 (provide 'idlwave-shell)
4667
4668 ;; Load the toolbar when wanted by the user.
4669
4670 (autoload 'idlwave-toolbar-toggle "idlw-toolbar"
4671 "Toggle the IDLWAVE toolbar.")
4672 (autoload 'idlwave-toolbar-add-everywhere "idlw-toolbar"
4673 "Add IDLWAVE toolbar.")
4674 (defun idlwave-shell-toggle-toolbar ()
4675 "Toggle the display of the debugging toolbar."
4676 (interactive)
4677 (idlwave-toolbar-toggle))
4678
4679 (if idlwave-shell-use-toolbar
4680 (add-hook 'idlwave-shell-mode-hook 'idlwave-toolbar-add-everywhere))
4681
4682 ;;; idlw-shell.el ends here