]> code.delx.au - gnu-emacs/blob - lisp/progmodes/idlwave.el
Convert consecutive FSF copyright years to ranges.
[gnu-emacs] / lisp / progmodes / idlwave.el
1 ;; idlwave.el --- IDL editing mode for GNU Emacs
2
3 ;; Copyright (C) 1999-2011 Free Software Foundation, Inc.
4
5 ;; Authors: J.D. Smith <jdsmith@as.arizona.edu>
6 ;; Carsten Dominik <dominik@science.uva.nl>
7 ;; Chris Chase <chase@att.com>
8 ;; Maintainer: J.D. Smith <jdsmith@as.arizona.edu>
9 ;; Version: 6.1.22
10 ;; Keywords: languages
11
12 ;; This file is part of GNU Emacs.
13
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
18
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
23
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26
27 ;;; Commentary:
28
29 ;; IDLWAVE enables feature-rich development and interaction with IDL,
30 ;; the Interactive Data Language. It provides a compelling,
31 ;; full-featured alternative to the IDLDE development environment
32 ;; bundled with IDL.
33
34 ;; In the remotely distant past, based on pascal.el, though bears
35 ;; little resemblance to it now.
36 ;;
37 ;; Incorporates many ideas, such as abbrevs, action routines, and
38 ;; continuation line indenting, from wave.el.
39 ;; wave.el original written by Lubos Pochman, Precision Visuals, Boulder.
40 ;;
41 ;; See the mode description ("C-h m" in idlwave-mode or "C-h f idlwave-mode")
42 ;; for features, key bindings, and info.
43 ;; Also, Info format documentation is available with `M-x idlwave-info'
44 ;;
45 ;; New versions of IDLWAVE, documentation, and more information
46 ;; available from:
47 ;; http://idlwave.org
48 ;;
49 ;; INSTALLATION
50 ;; ============
51 ;;
52 ;; Follow the instructions in the INSTALL file of the distribution.
53 ;; In short, put this file on your load path and add the following
54 ;; lines to your .emacs file:
55 ;;
56 ;; (autoload 'idlwave-mode "idlwave" "IDLWAVE Mode" t)
57 ;; (autoload 'idlwave-shell "idlw-shell" "IDLWAVE Shell" t)
58 ;; (setq auto-mode-alist (cons '("\\.pro\\'" . idlwave-mode) auto-mode-alist))
59 ;;
60 ;;
61 ;; SOURCE
62 ;; ======
63 ;;
64 ;; The newest version of this file is available from the maintainer's
65 ;; Webpage:
66 ;;
67 ;; http://idlwave.org
68 ;;
69 ;; DOCUMENTATION
70 ;; =============
71 ;;
72 ;; IDLWAVE is documented online in info format. A printable version
73 ;; of the documentation is available from the maintainers webpage (see
74 ;; SOURCE).
75 ;;
76 ;;
77 ;; ACKNOWLEDGMENTS
78 ;; ===============
79 ;;
80 ;; Thanks to the following people for their contributions and comments:
81 ;;
82 ;; Ulrik Dickow <dickow_at_nbi.dk>
83 ;; Eric E. Dors <edors_at_lanl.gov>
84 ;; Stein Vidar H. Haugan <s.v.h.haugan_at_astro.uio.no>
85 ;; David Huenemoerder <dph_at_space.mit.edu>
86 ;; Kevin Ivory <Kevin.Ivory_at_linmpi.mpg.de>
87 ;; Dick Jackson <dick_at_d-jackson.com>
88 ;; Xuyong Liu <liu_at_stsci.edu>
89 ;; Simon Marshall <Simon.Marshall_at_esrin.esa.it>
90 ;; Laurent Mugnier <mugnier_at_onera.fr>
91 ;; Lubos Pochman <lubos_at_rsinc.com>
92 ;; Bob Portmann <portmann_at_al.noaa.gov>
93 ;; Patrick M. Ryan <pat_at_jaameri.gsfc.nasa.gov>
94 ;; Marty Ryba <ryba_at_ll.mit.edu>
95 ;; Paul Sorenson <aardvark62_at_msn.com>
96 ;; Phil Sterne <sterne_at_dublin.llnl.gov>
97 ;; Phil Williams <williams_at_irc.chmcc.org>
98 ;;
99 ;; CUSTOMIZATION:
100 ;; =============
101 ;;
102 ;; IDLWAVE has extensive customize support; to learn about the
103 ;; variables which control the mode's behavior, use `M-x
104 ;; idlwave-customize'.
105 ;;
106 ;; You can set your own preferred values with Customize, or with Lisp
107 ;; code in .emacs. For an example of what to put into .emacs, check
108 ;; the TexInfo documentation or see a complete .emacs available at the
109 ;; website.
110 ;;
111 ;; KNOWN PROBLEMS:
112 ;; ==============
113 ;;
114 ;; IDLWAVE support for the IDL-derived PV-WAVE CL language of Visual
115 ;; Numerics, Inc. is growing less and less complete as the two
116 ;; languages grow increasingly apart. The mode probably shouldn't
117 ;; even have "WAVE" in its title, but it's catchy, and was required
118 ;; to avoid conflict with the CORBA idl.el mode. Caveat WAVEor.
119 ;;
120 ;; Moving the point backwards in conjunction with abbrev expansion
121 ;; does not work as I would like it, but this is a problem with
122 ;; emacs abbrev expansion done by the self-insert-command. It ends
123 ;; up inserting the character that expanded the abbrev after moving
124 ;; point backward, e.g., "\cl" expanded with a space becomes
125 ;; "LONG( )" with point before the close paren. This is solved by
126 ;; using a temporary function in `post-command-hook' - not pretty,
127 ;; but it works.
128 ;;
129 ;; Tabs and spaces are treated equally as whitespace when filling a
130 ;; comment paragraph. To accomplish this, tabs are permanently
131 ;; replaced by spaces in the text surrounding the paragraph, which
132 ;; may be an undesirable side-effect. Replacing tabs with spaces is
133 ;; limited to comments only and occurs only when a comment
134 ;; paragraph is filled via `idlwave-fill-paragraph'.
135 ;;
136 ;; Muti-statement lines (using "&") on block begin and end lines can
137 ;; ruin the formatting. For example, multiple end statements on a
138 ;; line: endif & endif. Using "&" outside of block begin/end lines
139 ;; should be okay.
140 ;;
141 ;; Determining the expression at point for printing and other
142 ;; examination commands is somewhat rough: currently only fairly
143 ;; simple entities are found. You can always drag-select or examine
144 ;; a pre-selected region.
145 ;;
146 ;; When forcing completion of method keywords, the initial
147 ;; query for a method has multiple entries for some methods. Would
148 ;; be too difficult to fix this hardly used case.
149 ;;
150 \f
151 ;;; Code:
152
153
154 (eval-when-compile (require 'cl))
155 (require 'idlw-help)
156
157 ;; For XEmacs
158 (unless (fboundp 'line-beginning-position)
159 (defalias 'line-beginning-position 'point-at-bol))
160 (unless (fboundp 'line-end-position)
161 (defalias 'line-end-position 'point-at-eol))
162 (unless (fboundp 'char-valid-p)
163 (defalias 'char-valid-p 'characterp))
164 (unless (fboundp 'match-string-no-properties)
165 (defalias 'match-string-no-properties 'match-string))
166
167 (if (not (fboundp 'cancel-timer))
168 (condition-case nil
169 (require 'timer)
170 (error nil)))
171
172 (declare-function idlwave-shell-get-path-info "idlw-shell")
173 (declare-function idlwave-shell-temp-file "idlw-shell")
174 (declare-function idlwave-shell-is-running "idlw-shell")
175 (declare-function widget-value "wid-edit" (widget))
176 (declare-function comint-dynamic-complete-filename "comint" ())
177
178 (defgroup idlwave nil
179 "Major mode for editing IDL .pro files."
180 :tag "IDLWAVE"
181 :link '(url-link :tag "Home Page"
182 "http://idlwave.org")
183 :link '(emacs-commentary-link :tag "Commentary in idlw-shell.el"
184 "idlw-shell.el")
185 :link '(emacs-commentary-link :tag "Commentary in idlwave.el" "idlwave.el")
186 :link '(custom-manual "(idlwave)Top")
187 :prefix "idlwave"
188 :group 'languages)
189
190
191 ;;; Variables for indentation behavior ---------------------------------------
192
193 (defgroup idlwave-code-formatting nil
194 "Indentation and formatting options for IDLWAVE mode."
195 :group 'idlwave)
196
197 (defcustom idlwave-main-block-indent 2
198 "*Extra indentation for the main block of code.
199 That is the block between the FUNCTION/PRO statement and the END
200 statement for that program unit."
201 :group 'idlwave-code-formatting
202 :type 'integer)
203
204 (defcustom idlwave-block-indent 3
205 "*Extra indentation applied to block lines.
206 If you change this, you probably also want to change `idlwave-end-offset'."
207 :group 'idlwave-code-formatting
208 :type 'integer)
209
210 (defcustom idlwave-end-offset -3
211 "*Extra indentation applied to block END lines.
212 A value equal to negative `idlwave-block-indent' will make END lines
213 line up with the block BEGIN lines."
214 :group 'idlwave-code-formatting
215 :type 'integer)
216
217 (defcustom idlwave-continuation-indent 3
218 "*Extra indentation applied to continuation lines.
219 This extra offset applies to the first of a set of continuation lines.
220 The following lines receive the same indentation as the first."
221 :group 'idlwave-code-formatting
222 :type 'integer)
223
224 (defcustom idlwave-max-extra-continuation-indent 40
225 "*Maximum additional indentation for special continuation indent.
226 Several special indentations are tried to help line up continuation
227 lines in routine calls or definitions, other statements with
228 parentheses, or assignment statements. This variable specifies a
229 maximum amount by which this special indentation can exceed the
230 standard continuation indentation, otherwise defaulting to a fixed
231 offset. Set to 0 to effectively disable all special continuation
232 indentation, or to a large number (like 100) to enable it in all
233 cases. See also `idlwave-indent-to-open-paren', which can override
234 this variable."
235 :group 'idlwave-code-formatting
236 :type 'integer)
237
238 (defcustom idlwave-indent-to-open-paren t
239 "*Non-nil means, indent continuation lines to innermost open parenthesis.
240 This indentation occurs even if otherwise disallowed by
241 `idlwave-max-extra-continuation-indent'. Matching parens and the
242 interleaving args are lined up. Example:
243
244 x = function_a(function_b(function_c( a, b, [1,2,3, $
245 4,5,6 $
246 ], $
247 c, d $
248 )))
249
250 When this variable is nil, paren alignment may still occur, based on
251 the value of `idlwave-max-extra-continuation-indent', which, if zero,
252 would yield:
253
254 x = function_a(function_b(function_c( a, b, [1,2,3, $
255 4,5,6 $
256 ], $
257 c, d $
258 )))"
259 :group 'idlwave-code-formatting
260 :type 'boolean)
261
262 (defcustom idlwave-indent-parens-nested nil
263 "*Non-nil means, indent continuation lines with parens by nesting
264 lines at consecutively deeper levels."
265 :group 'idlwave-code-formatting
266 :type 'boolean)
267
268
269 (defcustom idlwave-hanging-indent t
270 "*If set non-nil then comment paragraphs are indented under the
271 hanging indent given by `idlwave-hang-indent-regexp' match in the first line
272 of the paragraph."
273 :group 'idlwave-code-formatting
274 :type 'boolean)
275
276 (defcustom idlwave-hang-indent-regexp "- "
277 "*Regular expression matching the position of the hanging indent
278 in the first line of a comment paragraph. The size of the indent
279 extends to the end of the match for the regular expression."
280 :group 'idlwave-code-formatting
281 :type 'regexp)
282
283 (defcustom idlwave-use-last-hang-indent nil
284 "*If non-nil then use last match on line for `idlwave-indent-regexp'."
285 :group 'idlwave-code-formatting
286 :type 'boolean)
287
288 (defcustom idlwave-fill-comment-line-only t
289 "*If non-nil then auto fill will only operate on comment lines."
290 :group 'idlwave-code-formatting
291 :type 'boolean)
292
293 (defcustom idlwave-auto-fill-split-string t
294 "*If non-nil then auto fill will split strings with the IDL `+' operator.
295 When the line end falls within a string, string concatenation with the
296 '+' operator will be used to distribute a long string over lines.
297 If nil and a string is split then a terminal beep and warning are issued.
298
299 This variable is ignored when `idlwave-fill-comment-line-only' is
300 non-nil, since in this case code is not auto-filled."
301 :group 'idlwave-code-formatting
302 :type 'boolean)
303
304 (defcustom idlwave-split-line-string t
305 "*If non-nil then `idlwave-split-line' will split strings with `+'.
306 When the splitting point of a line falls inside a string, split the string
307 using the `+' string concatenation operator. If nil and a string is
308 split then a terminal beep and warning are issued."
309 :group 'idlwave-code-formatting
310 :type 'boolean)
311
312 (defcustom idlwave-no-change-comment ";;;"
313 "*The indentation of a comment that starts with this regular
314 expression will not be changed. Note that the indentation of a comment
315 at the beginning of a line is never changed."
316 :group 'idlwave-code-formatting
317 :type 'string)
318
319 (defcustom idlwave-begin-line-comment nil
320 "*A comment anchored at the beginning of line.
321 A comment matching this regular expression will not have its
322 indentation changed. If nil the default is \"^;\", i.e., any line
323 beginning with a \";\". Expressions for comments at the beginning of
324 the line should begin with \"^\"."
325 :group 'idlwave-code-formatting
326 :type '(choice (const :tag "Any line beginning with `;'" nil)
327 'regexp))
328
329 (defcustom idlwave-code-comment ";;[^;]"
330 "*A comment that starts with this regular expression on a line by
331 itself is indented as if it is a part of IDL code. As a result if
332 the comment is not preceded by whitespace it is unchanged."
333 :group 'idlwave-code-formatting
334 :type 'regexp)
335
336 ;; Comments not matching any of the above will be indented as a
337 ;; right-margin comment, i.e., to a minimum of `comment-column'.
338
339 ;;; Routine Info and Completion ---------------------------------------
340
341 (defgroup idlwave-routine-info nil
342 "Routine Info options for IDLWAVE mode."
343 :group 'idlwave)
344
345 (defcustom idlwave-use-library-catalogs t
346 "*Non-nil means search the IDL path for library catalog files.
347
348 These files, named .idlwave_catalog, document routine information for
349 individual directories and libraries of IDL .pro files. Many popular
350 libraries come with catalog files by default, so leaving this on is
351 usually a good idea."
352 :group 'idlwave-routine-info
353 :type 'boolean)
354
355 (defcustom idlwave-init-rinfo-when-idle-after 10
356 "*Seconds of idle time before routine info is automatically initialized.
357 Initializing the routine info can take a long time, in particular if a
358 large number of library catalogs are involved. When Emacs is idle for
359 more than the number of seconds specified by this variable, it starts
360 the initialization. The process is split into five steps, in order to
361 keep work interruption as short as possible. If one of the steps
362 finishes, and no user input has arrived in the mean time, initialization
363 proceeds immediately to the next step. A good value for this variable
364 is about 1/3 of the time initialization take in your setup. So if you
365 have a fast machine and no problems with a slow network connection,
366 don't hesitate to set this to 2 seconds. A value of 0 means, don't
367 initialize automatically, but instead wait until routine information is
368 needed, and initialize then."
369 :group 'idlwave-routine-info
370 :type 'number)
371
372 (defcustom idlwave-scan-all-buffers-for-routine-info t
373 "*Non-nil means, scan buffers for IDL programs when updating info.
374 The scanning is done by the command `idlwave-update-routine-info'.
375 The following values are allowed:
376
377 nil Don't scan any buffers.
378 t Scan all `idlwave-mode' buffers in the current editing session.
379 current Scan only the current buffer, but no other buffers."
380 :group 'idlwave-routine-info
381 :type '(choice
382 (const :tag "No buffer" nil)
383 (const :tag "All buffers" t)
384 (const :tag "Current buffer only" 'current)))
385
386 (defcustom idlwave-query-shell-for-routine-info t
387 "*Non-nil means query the shell for info about compiled routines.
388 Querying the shell is useful to get information about compiled modules,
389 and it is turned on by default. However, when you have a complete library
390 scan, this is not necessary."
391 :group 'idlwave-routine-info
392 :type 'boolean)
393
394 (defcustom idlwave-auto-routine-info-updates
395 '(find-file save-buffer kill-buffer compile-buffer)
396 "*Controls under what circumstances routine info is updated automatically.
397 Possible values:
398 nil Never
399 t All available
400 \(...) A list of circumstances. Allowed members are:
401 find-file Add info for new IDLWAVE buffers.
402 save-buffer Update buffer info when buffer is saved
403 kill-buffer Remove buffer info when buffer gets killed
404 compile-buffer Update shell info after `idlwave-shell-save-and...'"
405 :group 'idlwave-routine-info
406 :type '(choice
407 (const :tag "Never" nil)
408 (const :tag "As often as possible" t)
409 (set :tag "Checklist" :greedy t
410 (const :tag "When visiting a file" find-file)
411 (const :tag "When saving a buffer" save-buffer)
412 (const :tag "After a buffer was killed" kill-buffer)
413 (const :tag "After a buffer was compiled successfully, update shell info" compile-buffer))))
414
415 (defcustom idlwave-rinfo-max-source-lines 5
416 "*Maximum number of source files displayed in the Routine Info window.
417 When an integer, it is the maximum number of source files displayed.
418 A value of t means to show all source files."
419 :group 'idlwave-routine-info
420 :type 'integer)
421
422 (defcustom idlwave-library-path nil
423 "Library path for Windows and MacOS (OS9). Not needed under UNIX.
424 When selecting the directories to scan for IDL user catalog routine
425 info, IDLWAVE can, under UNIX, query the shell for the exact search
426 path \(the value of !PATH). However, under Windows and MacOS
427 \(pre-OSX), the IDLWAVE shell does not work. In this case, this
428 variable can be set to specify the paths where IDLWAVE can find PRO
429 files. The shell will only be asked for a list of paths when this
430 variable is nil. The value is a list of directories. A directory
431 preceeded by a `+' will be searched recursively. If you set this
432 variable on a UNIX system, the shell will not be queried. See also
433 `idlwave-system-directory'."
434 :group 'idlwave-routine-info
435 :type '(repeat (directory)))
436
437 (defcustom idlwave-system-directory ""
438 "The IDL system directory for Windows and MacOS. Not needed under
439 UNIX. Set this to the value of the `!DIR' system variable in IDL.
440 IDLWAVE uses this to find out which of the library routines belong to
441 the official system library. All files inside the `lib' subdirectory
442 are considered system library files - so don't install private stuff
443 in this directory. On UNIX systems, IDLWAVE queries the shell for the
444 value of `!DIR'. See also `idlwave-library-path'."
445 :group 'idlwave-routine-info
446 :type 'directory)
447
448 ;; Configuration files
449 (defcustom idlwave-config-directory
450 (convert-standard-filename "~/.idlwave")
451 "*Directory for configuration files and user-library catalog."
452 :group 'idlwave-routine-info
453 :type 'file)
454
455 (defvar idlwave-user-catalog-file "idlusercat.el")
456 (defvar idlwave-xml-system-rinfo-converted-file "idl_xml_rinfo.el")
457 (defvar idlwave-path-file "idlpath.el")
458
459 (defvar idlwave-libinfo-file nil
460 "*Obsolete variable, no longer used.")
461
462 (defcustom idlwave-special-lib-alist nil
463 "Alist of regular expressions matching special library directories.
464 When listing routine source locations, IDLWAVE gives a short hint where
465 the file defining the routine is located. By default it lists `SystemLib'
466 for routines in the system library `!DIR/lib' and `Library' for anything
467 else. This variable can define additional types. The car of each entry
468 is a regular expression matching the file name (they normally will match
469 on the path). The cdr is the string to be used as identifier. Max 10
470 chars are allowed."
471 :group 'idlwave-routine-info
472 :type '(repeat
473 (cons regexp string)))
474
475 (defcustom idlwave-auto-write-paths t
476 "Write out path (!PATH) and system directory (!DIR) info automatically.
477 Path info is needed to locate library catalog files. If non-nil,
478 whenever the path-list changes as a result of shell-query, etc., it is
479 written to file. Otherwise, the menu option \"Write Paths\" can be
480 used to force a write."
481 :group 'idlwave-routine-info
482 :type 'boolean)
483
484 (defgroup idlwave-completion nil
485 "Completion options for IDLWAVE mode."
486 :prefix "idlwave"
487 :group 'idlwave)
488
489 (eval-and-compile
490 (defconst idlwave-tmp
491 '(choice :tag "by applying the function"
492 (const upcase)
493 (const downcase)
494 (const capitalize)
495 (const preserve)
496 (symbol :tag "Other"))))
497
498 (defcustom idlwave-completion-case '((routine . upcase)
499 (keyword . upcase)
500 (class . preserve)
501 (method . preserve))
502 "Association list setting the case of completed words.
503
504 This variable determines the case (UPPER/lower/Capitalized...) of
505 words inserted into the buffer by completion. The preferred case can
506 be specified separately for routine names, keywords, classes and
507 methods.
508 This alist should therefore have entries for `routine' (normal
509 functions and procedures, i.e. non-methods), `keyword', `class', and
510 `method'. Plausible values are
511
512 upcase upcase whole word, like `BOX_CURSOR'
513 downcase downcase whole word, like `read_ppm'
514 capitalize capitalize each part, like `Widget_Control'
515 preserve preserve case as is, like `IDLgrView'
516
517 The value can also be any Emacs Lisp function which transforms the
518 case of characters in a string.
519
520 A value of `preserve' means that the case of the completed word is
521 identical to the way it was written in the definition statement of the
522 routine. This was implemented to allow for mixed-case completion, in
523 particular of object classes and methods.
524 If a completable word is defined in multiple locations, the meaning of
525 `preserve' is not unique since the different definitions might be
526 cased differently. Therefore IDLWAVE always takes the case of the
527 *first* definition it encounters during routine info collection and
528 uses the case derived from it consistently.
529
530 Note that a lowercase-only string in the buffer will always be completed in
531 lower case (but see the variable `idlwave-completion-force-default-case').
532
533 After changing this variable, you need to either restart Emacs or press
534 `C-u C-c C-i' to update the internal lists."
535 :group 'idlwave-completion
536 :type `(repeat
537 (cons (symbol :tag "Derive completion case for")
538 ,idlwave-tmp)))
539
540 (defcustom idlwave-completion-force-default-case nil
541 "*Non-nil means, completion will always honor `idlwave-completion-case'.
542 When nil, only the completion of a mixed case or upper case string
543 will honor the default settings in `idlwave-completion-case', while
544 the completion of lower case strings will be completed entirely in
545 lower case."
546 :group 'idlwave-completion
547 :type 'boolean)
548
549 (defcustom idlwave-complete-empty-string-as-lower-case nil
550 "*Non-nil means, the empty string is considered downcase for completion.
551 The case of what is already in the buffer determines the case of completions.
552 When this variable is non-nil, the empty string is considered to be downcase.
553 Completing on the empty string then offers downcase versions of the possible
554 completions."
555 :group 'idlwave-completion
556 :type 'boolean)
557
558 (defvar idlwave-default-completion-case-is-down nil
559 "Obsolete variable. See `idlwave-complete-empty-string-as-lower-case' and
560 `idlwave-completion-case'.")
561
562 (defcustom idlwave-buffer-case-takes-precedence nil
563 "*Non-nil means, the case of tokens in buffers dominates over system stuff.
564 To make this possible, we need to re-case everything each time we update
565 the routine info from the buffers. This is slow.
566 The default is to consider the case given in the system and library files
567 first which makes updating much faster."
568 :group 'idlwave-completion
569 :type 'boolean)
570
571 (defcustom idlwave-highlight-help-links-in-completion t
572 "*Non-nil means, highlight completions for which system help is available.
573 Help can then be accessed with mouse-3.
574 This option is only effective when the online help system is installed."
575 :group 'idlwave-completion
576 :type 'boolean)
577
578 (defcustom idlwave-support-inheritance t
579 "Non-nil means, treat inheritance with completion, online help etc.
580 When nil, IDLWAVE only knows about the native methods and tags of a class,
581 not about inherited ones."
582 :group 'idlwave-routine-info
583 :type 'boolean)
584
585 (defcustom idlwave-keyword-class-inheritance '("^[gs]etproperty$" "^init$")
586 "List of regular expressions for class-driven keyword inheritance.
587 Keyword inheritance is often tied to class inheritance by \"chaining\"
588 up the class tree. While it cannot be assumed that the presence of an
589 _EXTRA or _REF_EXTRA symbol guarantees such chaining will occur, for
590 certain methods this assumption is almost always true. The methods
591 for which to assume this can be set here."
592 :group 'idlwave-routine-info
593 :type '(repeat (regexp :tag "Match method:")))
594
595
596 (defcustom idlwave-completion-show-classes 1
597 "*Number of classes to show when completing object methods and keywords.
598 When completing methods or keywords for an object with unknown class,
599 the *Completions* buffer will show the valid classes for each completion
600 like this:
601
602 MyMethod <Class1,Class2,Class3>
603
604 The value of this variable may be nil to inhibit display, or an integer to
605 indicate the maximum number of classes to display.
606
607 On XEmacs, a full list of classes will also be placed into a `help-echo'
608 property on the competion items, so that the list of classes for the current
609 item is displayed in the echo area. If the value of this variable is a
610 negative integer, the `help-echo' property will be suppressed."
611 :group 'idlwave-completion
612 :type '(choice (const :tag "Don't show" nil)
613 (integer :tag "Number of classes shown" 1)))
614
615 (defcustom idlwave-completion-fontify-classes t
616 "*Non-nil means, fontify the classes in completions buffer.
617 This makes it easier to distinguish the completion items from the extra
618 class info listed. See `idlwave-completion-show-classes'."
619 :group 'idlwave-completion
620 :type 'boolean)
621
622 (defcustom idlwave-query-class '((method-default . nil)
623 (keyword-default . nil))
624 "Association list governing specification of object classes for completion.
625
626 When IDLWAVE tries to complete object-oriented methods, it usually
627 cannot determine the class of a given object from context. In order
628 to provide the user with a correct list of methods or keywords, it
629 needs to determine the appropriate class. IDLWAVE has two ways of
630 doing this (well, three ways if you count the shell... see
631 `idlwave-shell-query-for-class'):
632
633 1. Combine the items of all available classes which contain this
634 method for the purpose of completion. So when completing a method,
635 all methods of all known classes are available, and when completing
636 a keyword, all keywords allowed for this method in any class are
637 shown. This behavior is very much like normal completion and is
638 therefore the default. It works much better than one might think -
639 only for the INIT, GETPROPERTY and SETPROPERTY the keyword lists
640 become uncomfortably long. See also
641 `idlwave-completion-show-classes'.
642
643 2. The second possibility is to ask the user on each occasion. To
644 make this less interruptive, IDLWAVE can store the class as a text
645 property on the object operator `->'. For a given object in the
646 source code, class selection will then be needed only once
647 - for example to complete the method. Keywords to the method can
648 then be completed directly, because the class is already known.
649 You will have to turn on the storage of the selected class
650 explicitly with the variable `idlwave-store-inquired-class'.
651
652 This variable allows you to configure IDLWAVE's method and
653 method-keyword completion behavior. Its value is an alist, which
654 should contain at least two elements: (method-default . VALUE) and
655 \(keyword-default . VALUE), where VALUE is either t or nil. These
656 specify if the class should be found during method and keyword
657 completion, respectively.
658
659 The alist may have additional entries specifying exceptions from the
660 keyword completion rule for specific methods, like INIT or
661 GETPROPERTY. In order to turn on class specification for the INIT
662 method, add an entry (\"INIT\" . t). The method name must be ALL-CAPS."
663 :group 'idlwave-completion
664 :type '(list
665 (cons (const method-default)
666 (boolean :tag "Determine class when completing METHODS "))
667 (cons (const keyword-default)
668 (boolean :tag "Determine class when completing KEYWORDS "))
669 (repeat
670 :tag "Exceptions to defaults"
671 :inline t
672 (cons (string :tag "MODULE" :value "")
673 (boolean :tag "Determine class for this method")))))
674
675 (defcustom idlwave-store-inquired-class t
676 "*Non-nil means, store class of a method call as text property on `->'.
677 IDLWAVE sometimes has to ask the user for the class associated with a
678 particular object method call. This happens during the commands
679 `idlwave-routine-info' and `idlwave-complete', depending upon the
680 value of the variable `idlwave-query-class'.
681
682 When you specify a class, this information can be stored as a text
683 property on the `->' arrow in the source code, so that during the same
684 editing session, IDLWAVE will not have to ask again. When this
685 variable is non-nil, IDLWAVE will store and reuse the class information.
686 The class stored can be checked and removed with `\\[idlwave-routine-info]'
687 on the arrow.
688
689 The default of this variable is nil, since the result of commands then
690 is more predictable. However, if you know what you are doing, it can
691 be nice to turn this on.
692
693 An arrow which knows the class will be highlighted with
694 `idlwave-class-arrow-face'. The command \\[idlwave-routine-info]
695 displays (with prefix arg: deletes) the class stored on the arrow
696 at point."
697 :group 'idlwave-completion
698 :type 'boolean)
699
700 (defcustom idlwave-class-arrow-face 'bold
701 "*Face to highlight object operator arrows `->' which carry a class property.
702 When IDLWAVE stores a class name as text property on an object arrow
703 \(see variable `idlwave-store-inquired-class', it highlights the arrow
704 with this font in order to remind the user that this arrow is special."
705 :group 'idlwave-completion
706 :type 'symbol)
707
708 (defcustom idlwave-resize-routine-help-window t
709 "*Non-nil means, resize the Routine-info *Help* window to fit the content."
710 :group 'idlwave-completion
711 :type 'boolean)
712
713 (defcustom idlwave-keyword-completion-adds-equal t
714 "*Non-nil means, completion automatically adds `=' after completed keywords."
715 :group 'idlwave-completion
716 :type 'boolean)
717
718 (defcustom idlwave-function-completion-adds-paren t
719 "*Non-nil means, completion automatically adds `(' after completed function.
720 nil means, don't add anything.
721 A value of `2' means, also add the closing parenthesis and position cursor
722 between the two."
723 :group 'idlwave-completion
724 :type '(choice (const :tag "Nothing" nil)
725 (const :tag "(" t)
726 (const :tag "()" 2)))
727
728 (defcustom idlwave-completion-restore-window-configuration t
729 "*Non-nil means, try to restore the window configuration after completion.
730 When completion is not unique, Emacs displays a list of completions.
731 This messes up your window configuration. With this variable set, IDLWAVE
732 restores the old configuration after successful completion."
733 :group 'idlwave-completion
734 :type 'boolean)
735
736 ;;; Variables for abbrev and action behavior -----------------------------
737
738 (defgroup idlwave-abbrev-and-indent-action nil
739 "IDLWAVE performs actions when expanding abbreviations or indenting lines.
740 The variables in this group govern this."
741 :group 'idlwave)
742
743 (defcustom idlwave-do-actions nil
744 "*Non-nil means performs actions when indenting.
745 The actions that can be performed are listed in `idlwave-indent-action-table'."
746 :group 'idlwave-abbrev-and-indent-action
747 :type 'boolean)
748
749 (defcustom idlwave-abbrev-start-char "\\"
750 "*A single character string used to start abbreviations in abbrev mode.
751 Possible characters to chose from: ~`\%
752 or even '?'. '.' is not a good choice because it can make structure
753 field names act like abbrevs in certain circumstances.
754
755 Changes to this in `idlwave-mode-hook' will have no effect. Instead a user
756 must set it directly using `setq' in the .emacs file before idlwave.el
757 is loaded."
758 :group 'idlwave-abbrev-and-indent-action
759 :type 'string)
760
761 (defcustom idlwave-surround-by-blank nil
762 "*Non-nil means, enable `idlwave-surround'.
763 If non-nil, `=',`<',`>',`&',`,', `->' are surrounded with spaces by
764 `idlwave-surround'.
765 See help for `idlwave-indent-action-table' for symbols using `idlwave-surround'.
766
767 Also see the default key bindings for keys using `idlwave-surround'.
768 Keys are bound and made into actions calling `idlwave-surround' with
769 `idlwave-action-and-binding'.
770 See help for `idlwave-action-and-binding' for examples.
771
772 Also see help for `idlwave-surround'."
773 :group 'idlwave-abbrev-and-indent-action
774 :type 'boolean)
775
776 (defcustom idlwave-pad-keyword t
777 "*Non-nil means pad '=' in keywords (routine calls or defs) like assignment.
778 Whenever `idlwave-surround' is non-nil then this affects how '=' is
779 padded for keywords and for variables. If t, pad the same as for
780 assignments. If nil then spaces are removed. With any other value,
781 spaces are left unchanged."
782 :group 'idlwave-abbrev-and-indent-action
783 :type '(choice
784 (const :tag "Pad like assignments" t)
785 (const :tag "Remove space near `='" nil)
786 (const :tag "Keep space near `='" 'keep)))
787
788 (defcustom idlwave-show-block t
789 "*Non-nil means point blinks to block beginning for `idlwave-show-begin'."
790 :group 'idlwave-abbrev-and-indent-action
791 :type 'boolean)
792
793 (defcustom idlwave-expand-generic-end nil
794 "*Non-nil means expand generic END to ENDIF/ENDELSE/ENDWHILE etc."
795 :group 'idlwave-abbrev-and-indent-action
796 :type 'boolean)
797
798 (defcustom idlwave-reindent-end t
799 "*Non-nil means re-indent line after END was typed."
800 :group 'idlwave-abbrev-and-indent-action
801 :type 'boolean)
802
803 (defcustom idlwave-abbrev-move t
804 "*Non-nil means the abbrev hook can move point.
805 Set to nil by `idlwave-expand-region-abbrevs'. To see the abbrev
806 definitions, use the command `list-abbrevs', for abbrevs that move
807 point. Moving point is useful, for example, to place point between
808 parentheses of expanded functions.
809
810 See `idlwave-check-abbrev'."
811 :group 'idlwave-abbrev-and-indent-action
812 :type 'boolean)
813
814 (defcustom idlwave-abbrev-change-case nil
815 "*Non-nil means all abbrevs will be forced to either upper or lower case.
816 If the value t, all expanded abbrevs will be upper case.
817 If the value is 'down then abbrevs will be forced to lower case.
818 If nil, the case will not change.
819 If `idlwave-reserved-word-upcase' is non-nil, reserved words will always be
820 upper case, regardless of this variable."
821 :group 'idlwave-abbrev-and-indent-action
822 :type 'boolean)
823
824 (defcustom idlwave-reserved-word-upcase nil
825 "*Non-nil means, reserved words will be made upper case via abbrev expansion.
826 If nil case of reserved words is controlled by `idlwave-abbrev-change-case'.
827 Has effect only if in abbrev-mode."
828 :group 'idlwave-abbrev-and-indent-action
829 :type 'boolean)
830
831 ;;; Action/Expand Tables.
832 ;;
833 ;; The average user may have difficulty modifying this directly. It
834 ;; can be modified/set in idlwave-mode-hook, but it is easier to use
835 ;; idlwave-action-and-binding. See help for idlwave-action-and-binding for
836 ;; examples of how to add an action.
837 ;;
838 ;; The action table is used by `idlwave-indent-line' whereas both the
839 ;; action and expand tables are used by `idlwave-indent-and-action'. In
840 ;; general, the expand table is only used when a line is explicitly
841 ;; indented. Whereas, in addition to being used when the expand table
842 ;; is used, the action table is used when a line is indirectly
843 ;; indented via line splitting, auto-filling or a new line creation.
844 ;;
845 ;; Example actions:
846 ;;
847 ;; Capitalize system vars
848 ;; (idlwave-action-and-binding idlwave-sysvar '(capitalize-word 1) t)
849 ;;
850 ;; Capitalize procedure name
851 ;; (idlwave-action-and-binding "\\<\\(pro\\|function\\)\\>[ \t]*\\<"
852 ;; '(capitalize-word 1) t)
853 ;;
854 ;; Capitalize common block name
855 ;; (idlwave-action-and-binding "\\<common\\>[ \t]+\\<"
856 ;; '(capitalize-word 1) t)
857 ;; Capitalize label
858 ;; (idlwave-action-and-binding (concat "^[ \t]*" idlwave-label)
859 ;; '(capitalize-word -1) t)
860
861 (defvar idlwave-indent-action-table nil
862 "*Associated array containing action lists of search string (car),
863 and function as a cdr. This table is used by `idlwave-indent-line'.
864 See documentation for `idlwave-do-action' for a complete description of
865 the action lists.
866
867 Additions to the table are made with `idlwave-action-and-binding' when a
868 binding is not requested.
869 See help on `idlwave-action-and-binding' for examples.")
870
871 (defvar idlwave-indent-expand-table nil
872 "*Associated array containing action lists of search string (car),
873 and function as a cdr. The table is used by the
874 `idlwave-indent-and-action' function. See documentation for
875 `idlwave-do-action' for a complete description of the action lists.
876
877 Additions to the table are made with `idlwave-action-and-binding' when a
878 binding is requested.
879 See help on `idlwave-action-and-binding' for examples.")
880
881 ;;; Documentation header and history keyword ---------------------------------
882
883 (defgroup idlwave-documentation nil
884 "Options for documenting IDLWAVE files."
885 :group 'idlwave)
886
887 ;; FIXME: make defcustom?
888 (defvar idlwave-file-header
889 (list nil
890 ";+
891 ; NAME:
892 ;
893 ;
894 ;
895 ; PURPOSE:
896 ;
897 ;
898 ;
899 ; CATEGORY:
900 ;
901 ;
902 ;
903 ; CALLING SEQUENCE:
904 ;
905 ;
906 ;
907 ; INPUTS:
908 ;
909 ;
910 ;
911 ; OPTIONAL INPUTS:
912 ;
913 ;
914 ;
915 ; KEYWORD PARAMETERS:
916 ;
917 ;
918 ;
919 ; OUTPUTS:
920 ;
921 ;
922 ;
923 ; OPTIONAL OUTPUTS:
924 ;
925 ;
926 ;
927 ; COMMON BLOCKS:
928 ;
929 ;
930 ;
931 ; SIDE EFFECTS:
932 ;
933 ;
934 ;
935 ; RESTRICTIONS:
936 ;
937 ;
938 ;
939 ; PROCEDURE:
940 ;
941 ;
942 ;
943 ; EXAMPLE:
944 ;
945 ;
946 ;
947 ; MODIFICATION HISTORY:
948 ;
949 ;-
950 ")
951 "*A list (PATHNAME STRING) specifying the doc-header template to use for
952 summarizing a file. If PATHNAME is non-nil then this file will be included.
953 Otherwise STRING is used. If nil, the file summary will be omitted.
954 For example you might set PATHNAME to the path for the
955 lib_template.pro file included in the IDL distribution.")
956
957 (defcustom idlwave-header-to-beginning-of-file t
958 "*Non-nil means, the documentation header will always be at start of file.
959 When nil, the header is positioned between the PRO/FUNCTION line of
960 the current routine and the code, allowing several routine headers in
961 a file."
962 :group 'idlwave-documentation
963 :type 'boolean)
964
965 (defcustom idlwave-timestamp-hook 'idlwave-default-insert-timestamp
966 "*The hook function used to update the timestamp of a function."
967 :group 'idlwave-documentation
968 :type 'function)
969
970 (defcustom idlwave-doc-modifications-keyword "HISTORY"
971 "*The modifications keyword to use with the log documentation commands.
972 A ':' is added to the keyword end.
973 Inserted by doc-header and used to position logs by doc-modification.
974 If nil it will not be inserted."
975 :group 'idlwave-documentation
976 :type 'string)
977
978 (defcustom idlwave-doclib-start "^;+\\+"
979 "*Regexp matching the start of a document library header."
980 :group 'idlwave-documentation
981 :type 'regexp)
982
983 (defcustom idlwave-doclib-end "^;+-"
984 "*Regexp matching the end of a document library header."
985 :group 'idlwave-documentation
986 :type 'regexp)
987
988 ;;; External Programs -------------------------------------------------------
989
990 (defgroup idlwave-external-programs nil
991 "Path locations of external commands used by IDLWAVE."
992 :group 'idlwave)
993
994 (defcustom idlwave-shell-explicit-file-name "idl"
995 "*If non-nil, this is the command to run IDL.
996 Should be an absolute file path or path relative to the current environment
997 execution search path. If you want to specify command line switches
998 for the IDL program, use `idlwave-shell-command-line-options'.
999
1000 I know the name of this variable is badly chosen, but I cannot change
1001 it without compromising backwards-compatibility."
1002 :group 'idlwave-external-programs
1003 :type 'string)
1004
1005 (defcustom idlwave-shell-command-line-options nil
1006 "*A list of command line options for calling the IDL program.
1007 Since IDL is executed directly without going through a shell like /bin/sh,
1008 this should be a list of strings like '(\"-rt=file\" \"-nw\") with a separate
1009 string for each argument. But you may also give a single string which
1010 contains the options whitespace-separated. Emacs will be kind enough to
1011 split it for you."
1012 :type '(choice
1013 string
1014 (repeat (string :value "")))
1015 :group 'idlwave-external-programs)
1016
1017 (defcustom idlwave-help-application "idlhelp"
1018 "*The external application providing reference help for programming.
1019 Obsolete, if the IDL Assistant is being used for help."
1020 :group 'idlwave-external-programs
1021 :type 'string)
1022
1023 ;;; Some Shell variables which must be defined here.-----------------------
1024
1025 (defcustom idlwave-shell-debug-modifiers '()
1026 "List of modifiers to be used for the debugging commands.
1027 Will be used to bind debugging commands in the shell buffer and in all
1028 source buffers. These are additional convenience bindings, the debugging
1029 commands are always available with the `C-c C-d' prefix.
1030 If you set this to '(control shift), this means setting a breakpoint will
1031 be on `C-S-b', compiling a source file on `C-S-c' etc. Possible modifiers
1032 are `control', `meta', `super', `hyper', `alt', and `shift'."
1033 :group 'idlwave-shell-general-setup
1034 :type '(set :tag "Specify modifiers"
1035 (const control)
1036 (const meta)
1037 (const super)
1038 (const hyper)
1039 (const alt)
1040 (const shift)))
1041
1042 (defcustom idlwave-shell-automatic-start nil
1043 "*If non-nil attempt invoke `idlwave-shell' if not already running.
1044 This is checked when an attempt to send a command to an
1045 IDL process is made."
1046 :group 'idlwave-shell-general-setup
1047 :type 'boolean)
1048
1049 ;;; Miscellaneous variables -------------------------------------------------
1050
1051 (defgroup idlwave-misc nil
1052 "Miscellaneous options for IDLWAVE mode."
1053 :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
1054 :group 'idlwave)
1055
1056 (defcustom idlwave-startup-message t
1057 "*Non-nil displays a startup message when `idlwave-mode' is first called."
1058 :group 'idlwave-misc
1059 :type 'boolean)
1060
1061 (defcustom idlwave-default-font-lock-items
1062 '(pros-and-functions batch-files idlwave-idl-keywords label goto
1063 common-blocks class-arrows)
1064 "Items which should be fontified on the default fontification level 2.
1065 IDLWAVE defines 3 levels of fontification. Level 1 is very little, level 3
1066 is everything and level 2 is specified by this list.
1067 This variable must be set before IDLWAVE gets loaded.
1068 It is a list of symbols; the following symbols are allowed:
1069
1070 pros-and-functions Procedure and Function definitions
1071 batch-files Batch Files
1072 idlwave-idl-keywords IDL Keywords
1073 label Statement Labels
1074 goto Goto Statements
1075 common-blocks Common Blocks
1076 keyword-parameters Keyword Parameters in routine definitions and calls
1077 system-variables System Variables
1078 fixme FIXME: Warning in comments (on XEmacs only v. 21.0 and up)
1079 class-arrows Object Arrows with class property"
1080 :group 'idlwave-misc
1081 :type '(set
1082 :inline t :greedy t
1083 (const :tag "Procedure and Function definitions" pros-and-functions)
1084 (const :tag "Batch Files" batch-files)
1085 (const :tag "IDL Keywords (reserved words)" idlwave-idl-keywords)
1086 (const :tag "Statement Labels" label)
1087 (const :tag "Goto Statements" goto)
1088 (const :tag "Tags in Structure Definition" structtag)
1089 (const :tag "Structure Name" structname)
1090 (const :tag "Common Blocks" common-blocks)
1091 (const :tag "Keyword Parameters" keyword-parameters)
1092 (const :tag "System Variables" system-variables)
1093 (const :tag "FIXME: Warning" fixme)
1094 (const :tag "Object Arrows with class property " class-arrows)))
1095
1096 (defcustom idlwave-mode-hook nil
1097 "Normal hook. Executed when a buffer is put into `idlwave-mode'."
1098 :group 'idlwave-misc
1099 :type 'hook)
1100
1101 (defcustom idlwave-load-hook nil
1102 "Normal hook. Executed when idlwave.el is loaded."
1103 :group 'idlwave-misc
1104 :type 'hook)
1105
1106 (defvar idlwave-experimental nil
1107 "Non-nil means turn on a few experimental features.
1108 This variable is only for the maintainer, to test difficult stuff,
1109 while still distributing stable releases.
1110 As a user, you should not set this to t.")
1111
1112 ;;;
1113 ;;; End customization variables section
1114 ;;;
1115
1116 ;;; Non customization variables
1117
1118 ;;; font-lock mode - Additions by Phil Williams, Ulrik Dickow and
1119 ;;; Simon Marshall <simon_at_gnu.ai.mit.edu>
1120 ;;; and Carsten Dominik...
1121
1122 ;; The following are the reserved words in IDL. Maybe we should
1123 ;; highlight some more stuff as well?
1124 ;; Procedure declarations. Fontify keyword plus procedure name.
1125 (defvar idlwave-idl-keywords
1126 ;; To update this regexp, update the list of keywords and
1127 ;; evaluate the form.
1128 ;; (insert
1129 ;; (prin1-to-string
1130 ;; (concat
1131 ;; "\\<\\("
1132 ;; (regexp-opt
1133 ;; '("||" "&&" "and" "or" "xor" "not"
1134 ;; "eq" "ge" "gt" "le" "lt" "ne"
1135 ;; "for" "do" "endfor"
1136 ;; "if" "then" "endif" "else" "endelse"
1137 ;; "case" "of" "endcase"
1138 ;; "switch" "break" "continue" "endswitch"
1139 ;; "begin" "end"
1140 ;; "repeat" "until" "endrep"
1141 ;; "while" "endwhile"
1142 ;; "goto" "return"
1143 ;; "inherits" "mod"
1144 ;; "compile_opt" "forward_function"
1145 ;; "on_error" "on_ioerror")) ; on_error is not officially reserved
1146 ;; "\\)\\>")))
1147 "\\<\\(&&\\|and\\|b\\(egin\\|reak\\)\\|c\\(ase\\|o\\(mpile_opt\\|ntinue\\)\\)\\|do\\|e\\(lse\\|nd\\(case\\|else\\|for\\|if\\|rep\\|switch\\|while\\)?\\|q\\)\\|for\\(ward_function\\)?\\|g\\(oto\\|[et]\\)\\|i\\(f\\|nherits\\)\\|l[et]\\|mod\\|n\\(e\\|ot\\)\\|o\\(n_\\(error\\|ioerror\\)\\|[fr]\\)\\|re\\(peat\\|turn\\)\\|switch\\|then\\|until\\|while\\|xor\\|||\\)\\>")
1148
1149
1150 (let* (;; Procedure declarations. Fontify keyword plus procedure name.
1151 ;; Function declarations. Fontify keyword plus function name.
1152 (pros-and-functions
1153 '("\\<\\(function\\|pro\\)\\>[ \t]+\\(\\sw+\\(::\\sw+\\)?\\)"
1154 (1 font-lock-keyword-face)
1155 (2 font-lock-function-name-face nil t)))
1156
1157 ;; Common blocks
1158 (common-blocks
1159 '("\\<\\(common\\)\\>[ \t]*\\(\\sw+\\)?[ \t]*,?"
1160 (1 font-lock-keyword-face) ; "common"
1161 (2 font-lock-reference-face nil t) ; block name
1162 ("[ \t]*\\(\\sw+\\)[ ,]*"
1163 ;; Start with point after block name and comma
1164 (goto-char (match-end 0)) ; needed for XEmacs, could be nil
1165 nil
1166 (1 font-lock-variable-name-face) ; variable names
1167 )))
1168
1169 ;; Batch files
1170 (batch-files
1171 '("^[ \t]*\\(@[^ \t\n]+\\)" (1 font-lock-string-face)))
1172
1173 ;; FIXME warning.
1174 (fixme
1175 '("\\<FIXME:" (0 font-lock-warning-face t)))
1176
1177 ;; Labels
1178 (label
1179 '("^[ \t]*\\([a-zA-Z]\\sw*:\\)" (1 font-lock-reference-face)))
1180
1181 ;; The goto statement and its label
1182 (goto
1183 '("\\(goto\\)[ \t]*,[ \t]*\\([a-zA-Z]\\sw*\\)"
1184 (1 font-lock-keyword-face)
1185 (2 font-lock-reference-face)))
1186
1187 ;; Tags in structure definitions. Note that this definition
1188 ;; actually collides with labels, so we have to use the same
1189 ;; face. It also matches named subscript ranges,
1190 ;; e.g. vec{bottom:top]. No good way around this.
1191 (structtag
1192 '("\\<\\([a-zA-Z][a-zA-Z0-9_]*:\\)[^:]" (1 font-lock-reference-face)))
1193
1194 ;; Structure names
1195 (structname
1196 '("\\({\\|\\<inherits\\s-\\)\\s-*\\([a-zA-Z][a-zA-Z0-9_]*\\)[},\t \n]"
1197 (2 font-lock-function-name-face)))
1198
1199 ;; Keyword parameters, like /xlog or ,xrange=[]
1200 ;; This is anchored to the comma preceeding the keyword.
1201 ;; Treats continuation lines, works only during whole buffer
1202 ;; fontification. Slow, use it only in fancy fontification.
1203 (keyword-parameters
1204 '("\\(,\\|[a-zA-Z0-9_](\\)[ \t]*\\(\\$[ \t]*\\(;.*\\)?\n\\([ \t]*\\(;.*\\)?\n\\)*[ \t]*\\)?\\(/[a-zA-Z_]\\sw*\\|[a-zA-Z_]\\sw*[ \t]*=\\)"
1205 (6 font-lock-reference-face)))
1206
1207 ;; System variables start with a bang.
1208 (system-variables
1209 '("\\(![a-zA-Z_0-9]+\\(\\.\\sw+\\)?\\)"
1210 (1 font-lock-variable-name-face)))
1211
1212 ;; Special and unusual operators (not used because too noisy)
1213 ;; (special-operators
1214 ;; '("[<>#]" (0 font-lock-keyword-face)))
1215
1216 ;; All operators (not used because too noisy)
1217 ;; (all-operators
1218 ;; '("[-*^#+<>/]" (0 font-lock-keyword-face)))
1219
1220 ;; Arrows with text property `idlwave-class'
1221 (class-arrows
1222 '(idlwave-match-class-arrows (0 idlwave-class-arrow-face))))
1223
1224 (defconst idlwave-font-lock-keywords-1
1225 (list pros-and-functions batch-files)
1226 "Subdued level highlighting for IDLWAVE mode.")
1227
1228 (defconst idlwave-font-lock-keywords-2
1229 (mapcar 'symbol-value idlwave-default-font-lock-items)
1230 "Medium level highlighting for IDLWAVE mode.")
1231
1232 (defconst idlwave-font-lock-keywords-3
1233 (list pros-and-functions
1234 batch-files
1235 idlwave-idl-keywords
1236 label goto
1237 structtag
1238 structname
1239 common-blocks
1240 keyword-parameters
1241 system-variables
1242 class-arrows)
1243 "Gaudy level highlighting for IDLWAVE mode."))
1244
1245 (defun idlwave-match-class-arrows (limit)
1246 ;; Match an object arrow with class property
1247 (and idlwave-store-inquired-class
1248 (re-search-forward "->" limit 'limit)
1249 (get-text-property (match-beginning 0) 'idlwave-class)))
1250
1251 (defvar idlwave-font-lock-keywords idlwave-font-lock-keywords-2
1252 "Default expressions to highlight in IDLWAVE mode.")
1253
1254 (defvar idlwave-font-lock-defaults
1255 '((idlwave-font-lock-keywords
1256 idlwave-font-lock-keywords-1
1257 idlwave-font-lock-keywords-2
1258 idlwave-font-lock-keywords-3)
1259 nil t
1260 ((?$ . "w") (?_ . "w") (?. . "w") (?| . "w") (?& . "w"))
1261 beginning-of-line))
1262
1263 (put 'idlwave-mode 'font-lock-defaults
1264 idlwave-font-lock-defaults) ; XEmacs
1265
1266 (defconst idlwave-comment-line-start-skip "^[ \t]*;"
1267 "Regexp to match the start of a full-line comment.
1268 That is the _beginning_ of a line containing a comment delimiter `;' preceded
1269 only by whitespace.")
1270
1271 (defconst idlwave-begin-block-reg
1272 "\\<\\(pro\\|function\\|begin\\|case\\|switch\\)\\>"
1273 "Regular expression to find the beginning of a block.
1274 The case does not matter. The search skips matches in comments.")
1275
1276 (defconst idlwave-begin-unit-reg "^\\s-*\\(pro\\|function\\)\\>\\|\\`"
1277 "Regular expression to find the beginning of a unit.
1278 The case does not matter.")
1279
1280 (defconst idlwave-end-unit-reg "^\\s-*\\(pro\\|function\\)\\>\\|\\'"
1281 "Regular expression to find the line that indicates the end of unit.
1282 This line is the end of buffer or the start of another unit.
1283 The case does not matter. The search skips matches in comments.")
1284
1285 (defconst idlwave-continue-line-reg "\\<\\$"
1286 "Regular expression to match a continued line.")
1287
1288 (defconst idlwave-end-block-reg
1289 "\\<end\\(\\|case\\|switch\\|else\\|for\\|if\\|rep\\|while\\)\\>"
1290 "Regular expression to find the end of a block.
1291 The case does not matter. The search skips matches in comments.")
1292
1293 (defconst idlwave-block-matches
1294 '(("pro" . "end")
1295 ("function" . "end")
1296 ("case" . "endcase")
1297 ("else" . "endelse")
1298 ("for" . "endfor")
1299 ("then" . "endif")
1300 ("repeat" . "endrep")
1301 ("switch" . "endswitch")
1302 ("while" . "endwhile"))
1303 "Matches between statements and the corresponding END variant.
1304 The cars are the reserved words starting a block. If the block really
1305 begins with BEGIN, the cars are the reserved words before the begin
1306 which can be used to identify the block type.
1307 This is used to check for the correct END type, to close blocks and
1308 to expand generic end statements to their detailed form.")
1309
1310 (defconst idlwave-block-match-regexp
1311 "\\<\\(else\\|for\\|then\\|repeat\\|while\\)\\>"
1312 "Regular expression matching reserved words which can stand before
1313 blocks starting with a BEGIN statement. The matches must have associations
1314 `idlwave-block-matches'.")
1315
1316 (defconst idlwave-identifier "[a-zA-Z_][a-zA-Z0-9$_]*"
1317 "Regular expression matching an IDL identifier.")
1318
1319 (defconst idlwave-sysvar (concat "!" idlwave-identifier)
1320 "Regular expression matching IDL system variables.")
1321
1322 (defconst idlwave-variable (concat idlwave-identifier "\\|" idlwave-sysvar)
1323 "Regular expression matching IDL variable names.")
1324
1325 (defconst idlwave-label (concat idlwave-identifier ":")
1326 "Regular expression matching IDL labels.")
1327
1328 (defconst idlwave-method-call (concat idlwave-identifier "\\s *->"
1329 "\\(\\s *" idlwave-identifier "::\\)?"
1330 ))
1331
1332 (defconst idlwave-statement-match
1333 (list
1334 ;; "endif else" is the only possible "end" that can be
1335 ;; followed by a statement on the same line.
1336 '(endelse . ("end\\(\\|if\\)\\s +else" "end\\(\\|if\\)\\s +else"))
1337 ;; all other "end"s can not be followed by a statement.
1338 (cons 'end (list idlwave-end-block-reg nil))
1339 '(if . ("if\\>" "then"))
1340 '(for . ("for\\>" "do"))
1341 '(begin . ("begin\\>" nil))
1342 '(pdef . ("pro\\>\\|function\\>" nil))
1343 '(while . ("while\\>" "do"))
1344 '(repeat . ("repeat\\>" "repeat"))
1345 '(goto . ("goto\\>" nil))
1346 '(case . ("case\\>" nil))
1347 '(switch . ("switch\\>" nil))
1348 (cons 'call (list (concat "\\(" idlwave-variable "\\) *= *"
1349 "\\(" idlwave-method-call "\\s *\\)?"
1350 idlwave-identifier
1351 "\\s *(") nil))
1352 (cons 'call (list (concat
1353 "\\(" idlwave-method-call "\\s *\\)?"
1354 idlwave-identifier
1355 "\\( *\\($\\|\\$\\)\\|\\s *,\\)") nil))
1356 (cons 'assign (list (concat
1357 "\\(" idlwave-variable "\\) *=") nil)))
1358
1359 "Associated list of statement matching regular expressions.
1360 Each regular expression matches the start of an IDL statement.
1361 The first element of each association is a symbol giving the statement
1362 type. The associated value is a list. The first element of this list
1363 is a regular expression matching the start of an IDL statement for
1364 identifying the statement type. The second element of this list is a
1365 regular expression for finding a substatement for the type. The
1366 substatement starts after the end of the found match modulo
1367 whitespace. If it is nil then the statement has no substatement. The
1368 list order matters since matching an assignment statement exactly is
1369 not possible without parsing. Thus assignment statement become just
1370 the leftover unidentified statements containing an equal sign.")
1371
1372 ;; FIXME: This var seems to only ever be set, but never actually used!
1373 (defvar idlwave-fill-function 'auto-fill-function
1374 "IDL mode auto fill function.")
1375
1376 (defvar idlwave-comment-indent-function 'comment-indent-function
1377 "IDL mode comment indent function.")
1378
1379 ;; Note that this is documented in the v18 manuals as being a string
1380 ;; of length one rather than a single character.
1381 ;; The code in this file accepts either format for compatibility.
1382 (defvar idlwave-comment-indent-char ?\
1383 "Character to be inserted for IDL comment indentation.
1384 Normally a space.")
1385
1386 (defconst idlwave-continuation-char ?$
1387 "Character which is inserted as a last character on previous line by
1388 \\[idlwave-split-line] to begin a continuation line. Normally $.")
1389
1390 (defconst idlwave-mode-version "6.1_em22")
1391
1392 (defmacro idlwave-keyword-abbrev (&rest args)
1393 "Creates a function for abbrev hooks to call `idlwave-check-abbrev' with args."
1394 `(quote (lambda ()
1395 ,(append '(idlwave-check-abbrev) args))))
1396
1397 ;; If I take the time I can replace idlwave-keyword-abbrev with
1398 ;; idlwave-code-abbrev and remove the quoted abbrev check from
1399 ;; idlwave-check-abbrev. Then, e.g, (idlwave-keyword-abbrev 0 t) becomes
1400 ;; (idlwave-code-abbrev idlwave-check-abbrev 0 t). In fact I should change
1401 ;; the name of idlwave-check-abbrev to something like idlwave-modify-abbrev.
1402
1403 (defmacro idlwave-code-abbrev (&rest args)
1404 "Creates a function for abbrev hooks that ensures abbrevs are not quoted.
1405 Specifically, if the abbrev is in a comment or string it is unexpanded.
1406 Otherwise ARGS forms a list that is evaluated."
1407 ;; FIXME: it would probably be better to rely on the new :enable-function
1408 ;; to enforce the "don't expand in comments or strings".
1409 `(lambda ()
1410 ,(prin1-to-string args) ;; Puts the code in the doc string
1411 (if (idlwave-quoted)
1412 (progn (unexpand-abbrev) nil)
1413 ,(append args))))
1414
1415 (autoload 'idlwave-shell "idlw-shell"
1416 "Run an inferior IDL, with I/O through buffer `(idlwave-shell-buffer)'." t)
1417 (autoload 'idlwave-shell-send-command "idlw-shell")
1418 (autoload 'idlwave-shell-recenter-shell-window "idlw-shell"
1419 "Run `idlwave-shell' and switch back to current window" t)
1420 (autoload 'idlwave-shell-save-and-run "idlw-shell"
1421 "Save and run buffer under the shell." t)
1422 (autoload 'idlwave-shell-break-here "idlw-shell"
1423 "Set breakpoint in current line." t)
1424 (autoload 'idlwave-shell-run-region "idlw-shell"
1425 "Compile and run the region." t)
1426
1427 (fset 'idlwave-debug-map (make-sparse-keymap))
1428
1429 (defvar idlwave-mode-map
1430 (let ((map (make-sparse-keymap)))
1431 (define-key map "\C-c " 'idlwave-hard-tab)
1432 (define-key map [(control tab)] 'idlwave-hard-tab)
1433 ;;(define-key map "\C-c\C- " 'idlwave-hard-tab)
1434 (define-key map "'" 'idlwave-show-matching-quote)
1435 (define-key map "\"" 'idlwave-show-matching-quote)
1436 (define-key map "\C-g" 'idlwave-keyboard-quit)
1437 (define-key map "\C-c;" 'idlwave-toggle-comment-region)
1438 (define-key map "\C-\M-a" 'idlwave-beginning-of-subprogram)
1439 (define-key map "\C-\M-e" 'idlwave-end-of-subprogram)
1440 (define-key map "\C-c{" 'idlwave-beginning-of-block)
1441 (define-key map "\C-c}" 'idlwave-end-of-block)
1442 (define-key map "\C-c]" 'idlwave-close-block)
1443 (define-key map [(meta control h)] 'idlwave-mark-subprogram)
1444 (define-key map "\M-\C-n" 'idlwave-forward-block)
1445 (define-key map "\M-\C-p" 'idlwave-backward-block)
1446 (define-key map "\M-\C-d" 'idlwave-down-block)
1447 (define-key map "\M-\C-u" 'idlwave-backward-up-block)
1448 (define-key map "\M-\r" 'idlwave-split-line)
1449 (define-key map "\M-\C-q" 'idlwave-indent-subprogram)
1450 (define-key map "\C-c\C-p" 'idlwave-previous-statement)
1451 (define-key map "\C-c\C-n" 'idlwave-next-statement)
1452 ;; (define-key map "\r" 'idlwave-newline)
1453 ;; (define-key map "\t" 'idlwave-indent-line)
1454 (define-key map [(shift iso-lefttab)] 'idlwave-indent-statement)
1455 (define-key map "\C-c\C-a" 'idlwave-auto-fill-mode)
1456 (define-key map "\M-q" 'idlwave-fill-paragraph)
1457 (define-key map "\M-s" 'idlwave-edit-in-idlde)
1458 (define-key map "\C-c\C-h" 'idlwave-doc-header)
1459 (define-key map "\C-c\C-m" 'idlwave-doc-modification)
1460 (define-key map "\C-c\C-c" 'idlwave-case)
1461 (define-key map "\C-c\C-d" 'idlwave-debug-map)
1462 (when (and (listp idlwave-shell-debug-modifiers)
1463 (not (equal idlwave-shell-debug-modifiers '())))
1464 ;; Bind the debug commands also with the special modifiers.
1465 (let ((shift (memq 'shift idlwave-shell-debug-modifiers))
1466 (mods-noshift
1467 (delq 'shift (copy-sequence idlwave-shell-debug-modifiers))))
1468 (define-key map
1469 (vector (append mods-noshift (list (if shift ?C ?c))))
1470 'idlwave-shell-save-and-run)
1471 (define-key map
1472 (vector (append mods-noshift (list (if shift ?B ?b))))
1473 'idlwave-shell-break-here)
1474 (define-key map
1475 (vector (append mods-noshift (list (if shift ?E ?e))))
1476 'idlwave-shell-run-region)))
1477 (define-key map "\C-c\C-d\C-c" 'idlwave-shell-save-and-run)
1478 (define-key map "\C-c\C-d\C-b" 'idlwave-shell-break-here)
1479 (define-key map "\C-c\C-d\C-e" 'idlwave-shell-run-region)
1480 (define-key map "\C-c\C-f" 'idlwave-for)
1481 ;; (define-key map "\C-c\C-f" 'idlwave-function)
1482 ;; (define-key map "\C-c\C-p" 'idlwave-procedure)
1483 (define-key map "\C-c\C-r" 'idlwave-repeat)
1484 (define-key map "\C-c\C-w" 'idlwave-while)
1485 (define-key map "\C-c\C-k" 'idlwave-kill-autoloaded-buffers)
1486 (define-key map "\C-c\C-s" 'idlwave-shell)
1487 (define-key map "\C-c\C-l" 'idlwave-shell-recenter-shell-window)
1488 (define-key map "\C-c\C-b" 'idlwave-list-buffer-load-path-shadows)
1489 (define-key map "\C-c\C-v" 'idlwave-find-module)
1490 (define-key map "\C-c\C-t" 'idlwave-find-module-this-file)
1491 (define-key map "\C-c?" 'idlwave-routine-info)
1492 (define-key map "\M-?" 'idlwave-context-help)
1493 (define-key map [(control meta ?\?)]
1494 'idlwave-help-assistant-help-with-topic)
1495 ;; Pickup both forms of Esc/Meta binding
1496 (define-key map [(meta tab)] 'idlwave-complete)
1497 (define-key map [?\e?\t] 'idlwave-complete)
1498 (define-key map "\M-\C-i" 'idlwave-complete)
1499 (define-key map "\C-c\C-i" 'idlwave-update-routine-info)
1500 (define-key map "\C-c=" 'idlwave-resolve)
1501 (define-key map
1502 (if (featurep 'xemacs) [(shift button3)] [(shift mouse-3)])
1503 'idlwave-mouse-context-help)
1504 map)
1505 "Keymap used in IDL mode.")
1506
1507 (defvar idlwave-mode-syntax-table
1508 (let ((st (make-syntax-table)))
1509 (modify-syntax-entry ?+ "." st)
1510 (modify-syntax-entry ?- "." st)
1511 (modify-syntax-entry ?* "." st)
1512 (modify-syntax-entry ?/ "." st)
1513 (modify-syntax-entry ?^ "." st)
1514 (modify-syntax-entry ?# "." st)
1515 (modify-syntax-entry ?= "." st)
1516 (modify-syntax-entry ?% "." st)
1517 (modify-syntax-entry ?< "." st)
1518 (modify-syntax-entry ?> "." st)
1519 (modify-syntax-entry ?\' "\"" st)
1520 (modify-syntax-entry ?\" "\"" st)
1521 (modify-syntax-entry ?\\ "." st)
1522 (modify-syntax-entry ?_ "_" st)
1523 (modify-syntax-entry ?{ "(}" st)
1524 (modify-syntax-entry ?} "){" st)
1525 (modify-syntax-entry ?$ "_" st)
1526 (modify-syntax-entry ?. "." st)
1527 (modify-syntax-entry ?\; "<" st)
1528 (modify-syntax-entry ?\n ">" st)
1529 (modify-syntax-entry ?\f ">" st)
1530 st)
1531 "Syntax table in use in `idlwave-mode' buffers.")
1532
1533 (defvar idlwave-find-symbol-syntax-table
1534 (let ((st (copy-syntax-table idlwave-mode-syntax-table)))
1535 (modify-syntax-entry ?$ "w" st)
1536 (modify-syntax-entry ?_ "w" st)
1537 (modify-syntax-entry ?! "w" st)
1538 (modify-syntax-entry ?. "w" st)
1539 st)
1540 "Syntax table that treats symbol characters as word characters.")
1541
1542 (defmacro idlwave-with-special-syntax (&rest body)
1543 "Execute BODY with a different syntax table."
1544 `(let ((saved-syntax (syntax-table)))
1545 (unwind-protect
1546 (progn
1547 (set-syntax-table idlwave-find-symbol-syntax-table)
1548 ,@body)
1549 (set-syntax-table saved-syntax))))
1550
1551 ;(defmacro idlwave-with-special-syntax1 (&rest body)
1552 ; "Execute BODY with a different syntax table."
1553 ; `(let ((saved-syntax (syntax-table)))
1554 ; (unwind-protect
1555 ; (progn
1556 ; (set-syntax-table idlwave-find-symbol-syntax-table)
1557 ; ,@body)
1558 ; (set-syntax-table saved-syntax))))
1559
1560 (defun idlwave-action-and-binding (key cmd &optional select)
1561 "KEY and CMD are made into a key binding and an indent action.
1562 KEY is a string - same as for the `define-key' function. CMD is a
1563 function of no arguments or a list to be evaluated. CMD is bound to
1564 KEY in `idlwave-mode-map' by defining an anonymous function calling
1565 `self-insert-command' followed by CMD. If KEY contains more than one
1566 character a binding will only be set if SELECT is 'both.
1567
1568 \(KEY . CMD\) is also placed in the `idlwave-indent-expand-table',
1569 replacing any previous value for KEY. If a binding is not set then it
1570 will instead be placed in `idlwave-indent-action-table'.
1571
1572 If the optional argument SELECT is nil then an action and binding are
1573 created. If SELECT is 'noaction, then a binding is always set and no
1574 action is created. If SELECT is 'both then an action and binding
1575 will both be created even if KEY contains more than one character.
1576 Otherwise, if SELECT is non-nil then only an action is created.
1577
1578 Some examples:
1579 No spaces before and 1 after a comma
1580 (idlwave-action-and-binding \",\" '(idlwave-surround 0 1))
1581 A minimum of 1 space before and after `=' (see `idlwave-expand-equal').
1582 (idlwave-action-and-binding \"=\" '(idlwave-expand-equal -1 -1))
1583 Capitalize system variables - action only
1584 (idlwave-action-and-binding idlwave-sysvar '(capitalize-word 1) t)"
1585 (if (not (equal select 'noaction))
1586 ;; Add action
1587 (let* ((table (if select 'idlwave-indent-action-table
1588 'idlwave-indent-expand-table))
1589 (table-key (regexp-quote key))
1590 (cell (assoc table-key (eval table))))
1591 (if cell
1592 ;; Replace action command
1593 (setcdr cell cmd)
1594 ;; New action
1595 (set table (append (eval table) (list (cons table-key cmd)))))))
1596 ;; Make key binding for action
1597 (if (or (and (null select) (= (length key) 1))
1598 (equal select 'noaction)
1599 (equal select 'both))
1600 (define-key idlwave-mode-map key
1601 `(lambda ()
1602 (interactive)
1603 (self-insert-command 1)
1604 ,(if (listp cmd) cmd (list cmd))))))
1605
1606 ;; Set action and key bindings.
1607 ;; See description of the function `idlwave-action-and-binding'.
1608 ;; Automatically add spaces for the following characters
1609
1610 ;; Actions for & are complicated by &&
1611 (idlwave-action-and-binding "&" 'idlwave-custom-ampersand-surround)
1612
1613 ;; Automatically add spaces to equal sign if not keyword. This needs
1614 ;; to go ahead of > and <, so >= and <= will be treated correctly
1615 (idlwave-action-and-binding "=" '(idlwave-expand-equal -1 -1))
1616
1617 ;; Actions for > and < are complicated by >=, <=, and ->...
1618 (idlwave-action-and-binding "<" '(idlwave-custom-ltgtr-surround nil))
1619 (idlwave-action-and-binding ">" '(idlwave-custom-ltgtr-surround 'gtr))
1620
1621 (idlwave-action-and-binding "," '(idlwave-surround 0 -1 1))
1622
1623
1624 ;;;
1625 ;;; Abbrev Section
1626 ;;;
1627 ;;; When expanding abbrevs and the abbrev hook moves backward, an extra
1628 ;;; space is inserted (this is the space typed by the user to expanded
1629 ;;; the abbrev).
1630 ;;;
1631 (defvar idlwave-mode-abbrev-table nil
1632 "Abbreviation table used for IDLWAVE mode.")
1633 (define-abbrev-table 'idlwave-mode-abbrev-table ())
1634
1635 (defun idlwave-define-abbrev (name expansion hook &optional noprefix table)
1636 "Define-abbrev with backward compatibility.
1637
1638 If NOPREFIX is non-nil, don't prepend prefix character. Installs into
1639 `idlwave-mode-abbrev-table' unless TABLE is non-nil."
1640 (let ((abbrevs-changed nil) ;; mask the current value to avoid save
1641 (args (list (or table idlwave-mode-abbrev-table)
1642 (if noprefix name (concat idlwave-abbrev-start-char name))
1643 expansion
1644 hook)))
1645 (condition-case nil
1646 (apply 'define-abbrev (append args '(0 t)))
1647 (error (apply 'define-abbrev args)))))
1648
1649 (condition-case nil
1650 (modify-syntax-entry (string-to-char idlwave-abbrev-start-char)
1651 "w" idlwave-mode-syntax-table)
1652 (error nil))
1653
1654 ;;
1655 ;; Templates
1656 ;;
1657 (idlwave-define-abbrev "c" "" (idlwave-code-abbrev idlwave-case))
1658 (idlwave-define-abbrev "sw" "" (idlwave-code-abbrev idlwave-switch))
1659 (idlwave-define-abbrev "f" "" (idlwave-code-abbrev idlwave-for))
1660 (idlwave-define-abbrev "fu" "" (idlwave-code-abbrev idlwave-function))
1661 (idlwave-define-abbrev "pr" "" (idlwave-code-abbrev idlwave-procedure))
1662 (idlwave-define-abbrev "r" "" (idlwave-code-abbrev idlwave-repeat))
1663 (idlwave-define-abbrev "w" "" (idlwave-code-abbrev idlwave-while))
1664 (idlwave-define-abbrev "i" "" (idlwave-code-abbrev idlwave-if))
1665 (idlwave-define-abbrev "elif" "" (idlwave-code-abbrev idlwave-elif))
1666 ;;
1667 ;; Keywords, system functions, conversion routines
1668 ;;
1669 (idlwave-define-abbrev "ap" "arg_present()" (idlwave-keyword-abbrev 1))
1670 (idlwave-define-abbrev "b" "begin" (idlwave-keyword-abbrev 0 t))
1671 (idlwave-define-abbrev "co" "common" (idlwave-keyword-abbrev 0 t))
1672 (idlwave-define-abbrev "cb" "byte()" (idlwave-keyword-abbrev 1))
1673 (idlwave-define-abbrev "cx" "fix()" (idlwave-keyword-abbrev 1))
1674 (idlwave-define-abbrev "cl" "long()" (idlwave-keyword-abbrev 1))
1675 (idlwave-define-abbrev "cf" "float()" (idlwave-keyword-abbrev 1))
1676 (idlwave-define-abbrev "cs" "string()" (idlwave-keyword-abbrev 1))
1677 (idlwave-define-abbrev "cc" "complex()" (idlwave-keyword-abbrev 1))
1678 (idlwave-define-abbrev "cd" "double()" (idlwave-keyword-abbrev 1))
1679 (idlwave-define-abbrev "e" "else" (idlwave-keyword-abbrev 0 t))
1680 (idlwave-define-abbrev "ec" "endcase" 'idlwave-show-begin)
1681 (idlwave-define-abbrev "es" "endswitch" 'idlwave-show-begin)
1682 (idlwave-define-abbrev "ee" "endelse" 'idlwave-show-begin)
1683 (idlwave-define-abbrev "ef" "endfor" 'idlwave-show-begin)
1684 (idlwave-define-abbrev "ei" "endif else if" 'idlwave-show-begin)
1685 (idlwave-define-abbrev "el" "endif else" 'idlwave-show-begin)
1686 (idlwave-define-abbrev "en" "endif" 'idlwave-show-begin)
1687 (idlwave-define-abbrev "er" "endrep" 'idlwave-show-begin)
1688 (idlwave-define-abbrev "ew" "endwhile" 'idlwave-show-begin)
1689 (idlwave-define-abbrev "g" "goto," (idlwave-keyword-abbrev 0 t))
1690 (idlwave-define-abbrev "h" "help," (idlwave-keyword-abbrev 0))
1691 (idlwave-define-abbrev "k" "keyword_set()" (idlwave-keyword-abbrev 1))
1692 (idlwave-define-abbrev "n" "n_elements()" (idlwave-keyword-abbrev 1))
1693 (idlwave-define-abbrev "on" "on_error," (idlwave-keyword-abbrev 0))
1694 (idlwave-define-abbrev "oi" "on_ioerror," (idlwave-keyword-abbrev 0 1))
1695 (idlwave-define-abbrev "ow" "openw," (idlwave-keyword-abbrev 0))
1696 (idlwave-define-abbrev "or" "openr," (idlwave-keyword-abbrev 0))
1697 (idlwave-define-abbrev "ou" "openu," (idlwave-keyword-abbrev 0))
1698 (idlwave-define-abbrev "p" "print," (idlwave-keyword-abbrev 0))
1699 (idlwave-define-abbrev "pt" "plot," (idlwave-keyword-abbrev 0))
1700 (idlwave-define-abbrev "re" "read," (idlwave-keyword-abbrev 0))
1701 (idlwave-define-abbrev "rf" "readf," (idlwave-keyword-abbrev 0))
1702 (idlwave-define-abbrev "ru" "readu," (idlwave-keyword-abbrev 0))
1703 (idlwave-define-abbrev "rt" "return" (idlwave-keyword-abbrev 0))
1704 (idlwave-define-abbrev "sc" "strcompress()" (idlwave-keyword-abbrev 1))
1705 (idlwave-define-abbrev "sn" "strlen()" (idlwave-keyword-abbrev 1))
1706 (idlwave-define-abbrev "sl" "strlowcase()" (idlwave-keyword-abbrev 1))
1707 (idlwave-define-abbrev "su" "strupcase()" (idlwave-keyword-abbrev 1))
1708 (idlwave-define-abbrev "sm" "strmid()" (idlwave-keyword-abbrev 1))
1709 (idlwave-define-abbrev "sp" "strpos()" (idlwave-keyword-abbrev 1))
1710 (idlwave-define-abbrev "st" "strput()" (idlwave-keyword-abbrev 1))
1711 (idlwave-define-abbrev "sr" "strtrim()" (idlwave-keyword-abbrev 1))
1712 (idlwave-define-abbrev "t" "then" (idlwave-keyword-abbrev 0 t))
1713 (idlwave-define-abbrev "u" "until" (idlwave-keyword-abbrev 0 t))
1714 (idlwave-define-abbrev "wu" "writeu," (idlwave-keyword-abbrev 0))
1715 (idlwave-define-abbrev "iap" "if arg_present() then" (idlwave-keyword-abbrev 6))
1716 (idlwave-define-abbrev "ik" "if keyword_set() then" (idlwave-keyword-abbrev 6))
1717 (idlwave-define-abbrev "ine" "if n_elements() eq 0 then" (idlwave-keyword-abbrev 11))
1718 (idlwave-define-abbrev "inn" "if n_elements() ne 0 then" (idlwave-keyword-abbrev 11))
1719 (idlwave-define-abbrev "np" "n_params()" (idlwave-keyword-abbrev 0))
1720 (idlwave-define-abbrev "s" "size()" (idlwave-keyword-abbrev 1))
1721 (idlwave-define-abbrev "wi" "widget_info()" (idlwave-keyword-abbrev 1))
1722 (idlwave-define-abbrev "wc" "widget_control," (idlwave-keyword-abbrev 0))
1723 (idlwave-define-abbrev "pv" "ptr_valid()" (idlwave-keyword-abbrev 1))
1724 (idlwave-define-abbrev "ipv" "if ptr_valid() then" (idlwave-keyword-abbrev 6))
1725
1726 ;; This section is reserved words only. (From IDL user manual)
1727 ;;
1728 (idlwave-define-abbrev "and" "and" (idlwave-keyword-abbrev 0 t) t)
1729 (idlwave-define-abbrev "begin" "begin" (idlwave-keyword-abbrev 0 t) t)
1730 (idlwave-define-abbrev "break" "break" (idlwave-keyword-abbrev 0 t) t)
1731 (idlwave-define-abbrev "case" "case" (idlwave-keyword-abbrev 0 t) t)
1732 (idlwave-define-abbrev "common" "common" (idlwave-keyword-abbrev 0 t) t)
1733 (idlwave-define-abbrev "continue" "continue" (idlwave-keyword-abbrev 0 t) t)
1734 (idlwave-define-abbrev "do" "do" (idlwave-keyword-abbrev 0 t) t)
1735 (idlwave-define-abbrev "else" "else" (idlwave-keyword-abbrev 0 t) t)
1736 (idlwave-define-abbrev "end" "end" 'idlwave-show-begin-check t)
1737 (idlwave-define-abbrev "endcase" "endcase" 'idlwave-show-begin-check t)
1738 (idlwave-define-abbrev "endelse" "endelse" 'idlwave-show-begin-check t)
1739 (idlwave-define-abbrev "endfor" "endfor" 'idlwave-show-begin-check t)
1740 (idlwave-define-abbrev "endif" "endif" 'idlwave-show-begin-check t)
1741 (idlwave-define-abbrev "endrep" "endrep" 'idlwave-show-begin-check t)
1742 (idlwave-define-abbrev "endswitch" "endswitch" 'idlwave-show-begin-check t)
1743 (idlwave-define-abbrev "endwhi" "endwhi" 'idlwave-show-begin-check t)
1744 (idlwave-define-abbrev "endwhile" "endwhile" 'idlwave-show-begin-check t)
1745 (idlwave-define-abbrev "eq" "eq" (idlwave-keyword-abbrev 0 t) t)
1746 (idlwave-define-abbrev "for" "for" (idlwave-keyword-abbrev 0 t) t)
1747 (idlwave-define-abbrev "function" "function" (idlwave-keyword-abbrev 0 t) t)
1748 (idlwave-define-abbrev "ge" "ge" (idlwave-keyword-abbrev 0 t) t)
1749 (idlwave-define-abbrev "goto" "goto" (idlwave-keyword-abbrev 0 t) t)
1750 (idlwave-define-abbrev "gt" "gt" (idlwave-keyword-abbrev 0 t) t)
1751 (idlwave-define-abbrev "if" "if" (idlwave-keyword-abbrev 0 t) t)
1752 (idlwave-define-abbrev "le" "le" (idlwave-keyword-abbrev 0 t) t)
1753 (idlwave-define-abbrev "lt" "lt" (idlwave-keyword-abbrev 0 t) t)
1754 (idlwave-define-abbrev "mod" "mod" (idlwave-keyword-abbrev 0 t) t)
1755 (idlwave-define-abbrev "ne" "ne" (idlwave-keyword-abbrev 0 t) t)
1756 (idlwave-define-abbrev "not" "not" (idlwave-keyword-abbrev 0 t) t)
1757 (idlwave-define-abbrev "of" "of" (idlwave-keyword-abbrev 0 t) t)
1758 (idlwave-define-abbrev "on_ioerror" "on_ioerror" (idlwave-keyword-abbrev 0 t) t)
1759 (idlwave-define-abbrev "or" "or" (idlwave-keyword-abbrev 0 t) t)
1760 (idlwave-define-abbrev "pro" "pro" (idlwave-keyword-abbrev 0 t) t)
1761 (idlwave-define-abbrev "repeat" "repeat" (idlwave-keyword-abbrev 0 t) t)
1762 (idlwave-define-abbrev "switch" "switch" (idlwave-keyword-abbrev 0 t) t)
1763 (idlwave-define-abbrev "then" "then" (idlwave-keyword-abbrev 0 t) t)
1764 (idlwave-define-abbrev "until" "until" (idlwave-keyword-abbrev 0 t) t)
1765 (idlwave-define-abbrev "while" "while" (idlwave-keyword-abbrev 0 t) t)
1766 (idlwave-define-abbrev "xor" "xor" (idlwave-keyword-abbrev 0 t) t)
1767
1768 (defvar imenu-create-index-function)
1769 (defvar extract-index-name-function)
1770 (defvar prev-index-position-function)
1771 (defvar imenu-extract-index-name-function)
1772 (defvar imenu-prev-index-position-function)
1773 ;; defined later - so just make the compiler hush
1774 (defvar idlwave-mode-menu)
1775 (defvar idlwave-mode-debug-menu)
1776
1777 ;;;###autoload
1778 (define-derived-mode idlwave-mode prog-mode "IDLWAVE"
1779 "Major mode for editing IDL source files (version 6.1_em22).
1780
1781 The main features of this mode are
1782
1783 1. Indentation and Formatting
1784 --------------------------
1785 Like other Emacs programming modes, C-j inserts a newline and indents.
1786 TAB is used for explicit indentation of the current line.
1787
1788 To start a continuation line, use \\[idlwave-split-line]. This
1789 function can also be used in the middle of a line to split the line
1790 at that point. When used inside a long constant string, the string
1791 is split at that point with the `+' concatenation operator.
1792
1793 Comments are indented as follows:
1794
1795 `;;;' Indentation remains unchanged.
1796 `;;' Indent like the surrounding code
1797 `;' Indent to a minimum column.
1798
1799 The indentation of comments starting in column 0 is never changed.
1800
1801 Use \\[idlwave-fill-paragraph] to refill a paragraph inside a
1802 comment. The indentation of the second line of the paragraph
1803 relative to the first will be retained. Use
1804 \\[idlwave-auto-fill-mode] to toggle auto-fill mode for these
1805 comments. When the variable `idlwave-fill-comment-line-only' is
1806 nil, code can also be auto-filled and auto-indented.
1807
1808 To convert pre-existing IDL code to your formatting style, mark the
1809 entire buffer with \\[mark-whole-buffer] and execute
1810 \\[idlwave-expand-region-abbrevs]. Then mark the entire buffer
1811 again followed by \\[indent-region] (`indent-region').
1812
1813 2. Routine Info
1814 ------------
1815 IDLWAVE displays information about the calling sequence and the
1816 accepted keyword parameters of a procedure or function with
1817 \\[idlwave-routine-info]. \\[idlwave-find-module] jumps to the
1818 source file of a module. These commands know about system
1819 routines, all routines in idlwave-mode buffers and (when the
1820 idlwave-shell is active) about all modules currently compiled under
1821 this shell. It also makes use of pre-compiled or custom-scanned
1822 user and library catalogs many popular libraries ship with by
1823 default. Use \\[idlwave-update-routine-info] to update this
1824 information, which is also used for completion (see item 4).
1825
1826 3. Online IDL Help
1827 ---------------
1828
1829 \\[idlwave-context-help] displays the IDL documentation relevant
1830 for the system variable, keyword, or routines at point. A single
1831 key stroke gets you directly to the right place in the docs. See
1832 the manual to configure where and how the HTML help is displayed.
1833
1834 4. Completion
1835 ----------
1836 \\[idlwave-complete] completes the names of procedures, functions
1837 class names, keyword parameters, system variables and tags, class
1838 tags, structure tags, filenames and much more. It is context
1839 sensitive and figures out what is expected at point. Lower case
1840 strings are completed in lower case, other strings in mixed or
1841 upper case.
1842
1843 5. Code Templates and Abbreviations
1844 --------------------------------
1845 Many Abbreviations are predefined to expand to code fragments and templates.
1846 The abbreviations start generally with a `\\`. Some examples:
1847
1848 \\pr PROCEDURE template
1849 \\fu FUNCTION template
1850 \\c CASE statement template
1851 \\sw SWITCH statement template
1852 \\f FOR loop template
1853 \\r REPEAT Loop template
1854 \\w WHILE loop template
1855 \\i IF statement template
1856 \\elif IF-ELSE statement template
1857 \\b BEGIN
1858
1859 For a full list, use \\[idlwave-list-abbrevs]. Some templates also
1860 have direct keybindings - see the list of keybindings below.
1861
1862 \\[idlwave-doc-header] inserts a documentation header at the
1863 beginning of the current program unit (pro, function or main).
1864 Change log entries can be added to the current program unit with
1865 \\[idlwave-doc-modification].
1866
1867 6. Automatic Case Conversion
1868 -------------------------
1869 The case of reserved words and some abbrevs is controlled by
1870 `idlwave-reserved-word-upcase' and `idlwave-abbrev-change-case'.
1871
1872 7. Automatic END completion
1873 ------------------------
1874 If the variable `idlwave-expand-generic-end' is non-nil, each END typed
1875 will be converted to the specific version, like ENDIF, ENDFOR, etc.
1876
1877 8. Hooks
1878 -----
1879 Loading idlwave.el runs `idlwave-load-hook'.
1880 Turning on `idlwave-mode' runs `idlwave-mode-hook'.
1881
1882 9. Documentation and Customization
1883 -------------------------------
1884 Info documentation for this package is available. Use
1885 \\[idlwave-info] to display (complain to your sysadmin if that does
1886 not work). For Postscript, PDF, and HTML versions of the
1887 documentation, check IDLWAVE's homepage at URL `http://idlwave.org'.
1888 IDLWAVE has customize support - see the group `idlwave'.
1889
1890 10.Keybindings
1891 -----------
1892 Here is a list of all keybindings of this mode.
1893 If some of the key bindings below show with ??, use \\[describe-key]
1894 followed by the key sequence to see what the key sequence does.
1895
1896 \\{idlwave-mode-map}"
1897 :abbrev-table idlwave-mode-abbrev-table
1898 (if idlwave-startup-message
1899 (message "Emacs IDLWAVE mode version %s." idlwave-mode-version))
1900 (setq idlwave-startup-message nil)
1901
1902 (set (make-local-variable 'indent-line-function) 'idlwave-indent-and-action)
1903
1904 (set (make-local-variable idlwave-comment-indent-function)
1905 #'idlwave-comment-hook)
1906
1907 (set (make-local-variable 'comment-start-skip) ";+[ \t]*")
1908 (set (make-local-variable 'comment-start) ";")
1909 (set (make-local-variable 'comment-add) 1) ; ";;" for new and regions
1910 (set (make-local-variable 'require-final-newline) t)
1911 (set (make-local-variable 'abbrev-all-caps) t)
1912 (set (make-local-variable 'indent-tabs-mode) nil)
1913 (set (make-local-variable 'completion-ignore-case) t)
1914
1915 (when (featurep 'easymenu)
1916 (easy-menu-add idlwave-mode-menu idlwave-mode-map)
1917 (easy-menu-add idlwave-mode-debug-menu idlwave-mode-map))
1918
1919 (setq abbrev-mode t)
1920
1921 (set (make-local-variable idlwave-fill-function) 'idlwave-auto-fill)
1922 (setq comment-end "")
1923 (set (make-local-variable 'comment-multi-line) nil)
1924 (set (make-local-variable 'paragraph-separate)
1925 "[ \t\f]*$\\|[ \t]*;+[ \t]*$\\|;+[+=-_*]+$")
1926 (set (make-local-variable 'paragraph-start) "[ \t\f]\\|[ \t]*;+[ \t]")
1927 (set (make-local-variable 'paragraph-ignore-fill-prefix) nil)
1928 (set (make-local-variable 'parse-sexp-ignore-comments) t)
1929
1930 ;; ChangeLog
1931 (set (make-local-variable 'add-log-current-defun-function)
1932 'idlwave-current-routine-fullname)
1933
1934 ;; Set tag table list to use IDLTAGS as file name.
1935 (if (boundp 'tag-table-alist)
1936 (add-to-list 'tag-table-alist '("\\.pro$" . "IDLTAGS")))
1937
1938 ;; Font-lock additions
1939 ;; Following line is for Emacs - XEmacs uses the corresponding property
1940 ;; on the `idlwave-mode' symbol.
1941 (set (make-local-variable 'font-lock-defaults) idlwave-font-lock-defaults)
1942 (set (make-local-variable 'font-lock-mark-block-function)
1943 'idlwave-mark-subprogram)
1944 (set (make-local-variable 'font-lock-fontify-region-function)
1945 'idlwave-font-lock-fontify-region)
1946
1947 ;; Imenu setup
1948 (set (make-local-variable 'imenu-create-index-function)
1949 'imenu-default-create-index-function)
1950 (set (make-local-variable 'imenu-extract-index-name-function)
1951 'idlwave-unit-name)
1952 (set (make-local-variable 'imenu-prev-index-position-function)
1953 'idlwave-prev-index-position)
1954
1955 ;; HideShow setup
1956 (add-to-list 'hs-special-modes-alist
1957 (list 'idlwave-mode
1958 idlwave-begin-block-reg
1959 idlwave-end-block-reg
1960 ";"
1961 'idlwave-forward-block nil))
1962
1963 ;; Make a local post-command-hook and add our hook to it
1964 ;; NB: `make-local-hook' needed for older/alternative Emacs compatibility
1965 ;; (make-local-hook 'post-command-hook)
1966 (add-hook 'post-command-hook 'idlwave-command-hook nil 'local)
1967
1968 ;; Make local hooks for buffer updates
1969 ;; NB: `make-local-hook' needed for older/alternative Emacs compatibility
1970 ;; (make-local-hook 'kill-buffer-hook)
1971 (add-hook 'kill-buffer-hook 'idlwave-kill-buffer-update nil 'local)
1972 ;; (make-local-hook 'after-save-hook)
1973 (add-hook 'after-save-hook 'idlwave-save-buffer-update nil 'local)
1974 (add-hook 'after-save-hook 'idlwave-revoke-license-to-kill nil 'local)
1975
1976 ;; Setup directories and file, if necessary
1977 (idlwave-setup)
1978
1979 ;; Update the routine info with info about current buffer?
1980 (idlwave-new-buffer-update)
1981
1982 ;; Check help location
1983 (idlwave-help-check-locations))
1984
1985 (defvar idlwave-setup-done nil)
1986 (defun idlwave-setup ()
1987 (unless idlwave-setup-done
1988 (if (not (file-directory-p idlwave-config-directory))
1989 (make-directory idlwave-config-directory))
1990 (setq
1991 idlwave-user-catalog-file (expand-file-name
1992 idlwave-user-catalog-file
1993 idlwave-config-directory)
1994 idlwave-xml-system-rinfo-converted-file
1995 (expand-file-name
1996 idlwave-xml-system-rinfo-converted-file
1997 idlwave-config-directory)
1998 idlwave-path-file (expand-file-name
1999 idlwave-path-file
2000 idlwave-config-directory))
2001 (idlwave-read-paths) ; we may need these early
2002 (setq idlwave-setup-done t)))
2003
2004 (defun idlwave-font-lock-fontify-region (beg end &optional verbose)
2005 "Fontify continuation lines correctly."
2006 (let (pos)
2007 (save-excursion
2008 (goto-char beg)
2009 (forward-line -1)
2010 (when (setq pos (idlwave-is-continuation-line))
2011 (goto-char pos)
2012 (idlwave-beginning-of-statement)
2013 (setq beg (point)))))
2014 (font-lock-default-fontify-region beg end verbose))
2015
2016 ;;
2017 ;; Code Formatting ----------------------------------------------------
2018 ;;
2019
2020 (defun idlwave-hard-tab ()
2021 "Insert TAB in buffer in current position."
2022 (interactive)
2023 (insert "\t"))
2024
2025 ;;; This stuff is experimental
2026
2027 (defvar idlwave-command-hook nil
2028 "If non-nil, a list that can be evaluated using `eval'.
2029 It is evaluated in the lisp function `idlwave-command-hook' which is
2030 placed in `post-command-hook'.")
2031
2032 (defun idlwave-command-hook ()
2033 "Command run after every command.
2034 Evaluates a non-nil value of the *variable* `idlwave-command-hook' and
2035 sets the variable to zero afterwards."
2036 (and idlwave-command-hook
2037 (listp idlwave-command-hook)
2038 (condition-case nil
2039 (eval idlwave-command-hook)
2040 (error nil)))
2041 (setq idlwave-command-hook nil))
2042
2043 ;;; End experiment
2044
2045 ;; It would be better to use expand.el for better abbrev handling and
2046 ;; versatility.
2047
2048 (defun idlwave-check-abbrev (arg &optional reserved)
2049 "Reverse abbrev expansion if in comment or string.
2050 Argument ARG is the number of characters to move point
2051 backward if `idlwave-abbrev-move' is non-nil.
2052 If optional argument RESERVED is non-nil then the expansion
2053 consists of reserved words, which will be capitalized if
2054 `idlwave-reserved-word-upcase' is non-nil.
2055 Otherwise, the abbrev will be capitalized if `idlwave-abbrev-change-case'
2056 is non-nil, unless its value is \`down in which case the abbrev will be
2057 made into all lowercase.
2058 Returns non-nil if abbrev is left expanded."
2059 (if (idlwave-quoted)
2060 (progn (unexpand-abbrev)
2061 nil)
2062 (if (and reserved idlwave-reserved-word-upcase)
2063 (upcase-region last-abbrev-location (point))
2064 (cond
2065 ((equal idlwave-abbrev-change-case 'down)
2066 (downcase-region last-abbrev-location (point)))
2067 (idlwave-abbrev-change-case
2068 (upcase-region last-abbrev-location (point)))))
2069 (if (and idlwave-abbrev-move (> arg 0))
2070 (if (boundp 'post-command-hook)
2071 (setq idlwave-command-hook (list 'backward-char (1+ arg)))
2072 (backward-char arg)))
2073 t))
2074
2075 (defun idlwave-in-comment ()
2076 "Return t if point is inside a comment, nil otherwise."
2077 (save-excursion
2078 (let ((here (point)))
2079 (and (idlwave-goto-comment) (> here (point))))))
2080
2081 (defun idlwave-goto-comment ()
2082 "Move to start of comment delimiter on current line.
2083 Moves to end of line if there is no comment delimiter.
2084 Ignores comment delimiters in strings.
2085 Returns point if comment found and nil otherwise."
2086 (let ((eos (point-at-eol))
2087 (data (match-data))
2088 found)
2089 ;; Look for first comment delimiter not in a string
2090 (beginning-of-line)
2091 (setq found (search-forward comment-start eos 'lim))
2092 (while (and found (idlwave-in-quote))
2093 (setq found (search-forward comment-start eos 'lim)))
2094 (store-match-data data)
2095 (and found (not (idlwave-in-quote))
2096 (progn
2097 (backward-char 1)
2098 (point)))))
2099
2100 (defun idlwave-region-active-p ()
2101 "Should we operate on an active region?"
2102 (if (fboundp 'use-region-p)
2103 (use-region-p)
2104 (region-active-p)))
2105
2106 (defun idlwave-show-matching-quote ()
2107 "Insert quote and show matching quote if this is end of a string."
2108 (interactive)
2109 (let ((bq (idlwave-in-quote))
2110 (inq last-command-event))
2111 (if (and bq (not (idlwave-in-comment)))
2112 (let ((delim (char-after bq)))
2113 (insert inq)
2114 (if (eq inq delim)
2115 (save-excursion
2116 (goto-char bq)
2117 (sit-for 1))))
2118 ;; Not the end of a string
2119 (insert inq))))
2120
2121 (defun idlwave-show-begin-check ()
2122 "Ensure that the previous word was a token before `idlwave-show-begin'.
2123 An END token must be preceded by whitespace."
2124 (if (not (idlwave-quoted))
2125 (if
2126 (save-excursion
2127 (backward-word 1)
2128 (backward-char 1)
2129 (looking-at "[ \t\n\f]"))
2130 (idlwave-show-begin))))
2131
2132 (defun idlwave-show-begin ()
2133 "Find the start of current block and blinks to it for a second.
2134 Also checks if the correct END statement has been used."
2135 ;; All end statements are reserved words
2136 ;; Re-indent end line
2137 ;;(insert-char ?\ 1) ;; So indent, etc. work well
2138 ;;(backward-char 1)
2139 (let* ((pos (point-marker))
2140 (last-abbrev-marker (copy-marker last-abbrev-location))
2141 (eol-pos (point-at-eol))
2142 begin-pos end-pos end end1 )
2143 (if idlwave-reindent-end (idlwave-indent-line))
2144 (setq last-abbrev-location (marker-position last-abbrev-marker))
2145 (when (and (idlwave-check-abbrev 0 t)
2146 idlwave-show-block)
2147 (save-excursion
2148 ;; Move inside current block
2149 (goto-char last-abbrev-marker)
2150 (idlwave-block-jump-out -1 'nomark)
2151 (setq begin-pos (point))
2152 (idlwave-block-jump-out 1 'nomark)
2153 (setq end-pos (point))
2154 (if (> end-pos eol-pos)
2155 (setq end-pos pos))
2156 (goto-char end-pos)
2157 (setq end (buffer-substring
2158 (progn
2159 (skip-chars-backward "a-zA-Z")
2160 (point))
2161 end-pos))
2162 (goto-char begin-pos)
2163 (when (setq end1 (cdr (idlwave-block-master)))
2164 (cond
2165 ((null end1)) ; no-operation
2166 ((string= (downcase end) (downcase end1))
2167 (sit-for 1))
2168 ((string= (downcase end) "end")
2169 ;; A generic end
2170 (if idlwave-expand-generic-end
2171 (save-excursion
2172 (goto-char pos)
2173 (backward-char 3)
2174 (insert (if (string= end "END") (upcase end1) end1))
2175 (delete-char 3)))
2176 (sit-for 1))
2177 (t
2178 (beep)
2179 (message "Warning: Shouldn't this be \"%s\" instead of \"%s\"?"
2180 end1 end)
2181 (sit-for 1))))))))
2182 ;;(delete-char 1))
2183
2184 (defun idlwave-block-master ()
2185 (let ((case-fold-search t))
2186 (save-excursion
2187 (cond
2188 ((looking-at "pro\\|case\\|switch\\|function\\>")
2189 (assoc (downcase (match-string 0)) idlwave-block-matches))
2190 ((looking-at "begin\\>")
2191 (let ((limit (save-excursion
2192 (idlwave-beginning-of-statement)
2193 (point))))
2194 (cond
2195 ((re-search-backward ":[ \t]*\\=" limit t)
2196 ;; seems to be a case thing
2197 '("begin" . "end"))
2198 ((re-search-backward idlwave-block-match-regexp limit t)
2199 (assoc (downcase (match-string 1))
2200 idlwave-block-matches))
2201 (t
2202 ;; Just a normal block
2203 '("begin" . "end")))))
2204 (t nil)))))
2205
2206 (defun idlwave-close-block ()
2207 "Terminate the current block with the correct END statement."
2208 (interactive)
2209 ;; Start new line if we are not in a new line
2210 (unless (save-excursion
2211 (skip-chars-backward " \t")
2212 (bolp))
2213 (let ((idlwave-show-block nil))
2214 (newline-and-indent)))
2215 (let ((last-abbrev-location (point))) ; for upcasing
2216 (insert "end")
2217 (idlwave-show-begin)))
2218
2219 (defun idlwave-custom-ampersand-surround (&optional is-action)
2220 "Surround &, leaving room for && (which surround as well)."
2221 (let* ((prev-char (char-after (- (point) 2)))
2222 (next-char (char-after (point)))
2223 (amp-left (eq prev-char ?&))
2224 (amp-right (eq next-char ?&))
2225 (len (if amp-left 2 1)))
2226 (unless amp-right ;no need to do it twice, amp-left will catch it.
2227 (idlwave-surround -1 (if (or is-action amp-left) -1) len))))
2228
2229 (defun idlwave-custom-ltgtr-surround (gtr &optional is-action)
2230 "Surround > and < by blanks, leaving room for >= and <=, and considering ->."
2231 (let* ((prev-char (char-after (- (point) 2)))
2232 (next-char (char-after (point)))
2233 (method-invoke (and gtr (eq prev-char ?-)))
2234 (len (if method-invoke 2 1)))
2235 (unless (eq next-char ?=)
2236 ;; Key binding: pad only on left, to save for possible >=/<=
2237 (idlwave-surround -1 (if (or is-action method-invoke) -1) len))))
2238
2239 (defun idlwave-surround (&optional before after length is-action)
2240 "Surround the LENGTH characters before point with blanks.
2241 LENGTH defaults to 1.
2242 Optional arguments BEFORE and AFTER affect the behavior before and
2243 after the characters (see also description of `idlwave-make-space'):
2244
2245 nil do nothing
2246 0 force no spaces
2247 integer > 0 force exactly n spaces
2248 integer < 0 at least |n| spaces
2249
2250 The function does nothing if any of the following conditions is true:
2251 - `idlwave-surround-by-blank' is nil
2252 - the character before point is inside a string or comment"
2253 (when (and idlwave-surround-by-blank (not (idlwave-quoted)))
2254 (let ((length (or length 1))) ; establish a default for LENGTH
2255 (backward-char length)
2256 (save-restriction
2257 (let ((here (point)))
2258 (skip-chars-backward " \t")
2259 (if (bolp)
2260 ;; avoid clobbering indent
2261 (progn
2262 (move-to-column (idlwave-calculate-indent))
2263 (if (<= (point) here)
2264 (narrow-to-region (point) here))
2265 (goto-char here)))
2266 (idlwave-make-space before))
2267 (skip-chars-forward " \t"))
2268 (forward-char length)
2269 (idlwave-make-space after)
2270 ;; Check to see if the line should auto wrap
2271 (if (and (equal (char-after (1- (point))) ?\ )
2272 (> (current-column) fill-column))
2273 (funcall auto-fill-function)))))
2274
2275 (defun idlwave-make-space (n)
2276 "Make space at point.
2277 The space affected is all the spaces and tabs around point.
2278 If n is non-nil then point is left abs(n) spaces from the beginning of
2279 the contiguous space.
2280 The amount of space at point is determined by N.
2281 If the value of N is:
2282 nil - do nothing.
2283 > 0 - exactly N spaces.
2284 < 0 - a minimum of -N spaces, i.e., do not change if there are
2285 already -N spaces.
2286 0 - no spaces (i.e. remove any existing space)."
2287 (if (integerp n)
2288 (let
2289 ((start-col (progn (skip-chars-backward " \t") (current-column)))
2290 (left (point))
2291 (end-col (progn (skip-chars-forward " \t") (current-column))))
2292 (delete-horizontal-space)
2293 (cond
2294 ((> n 0)
2295 (idlwave-indent-to (+ start-col n))
2296 (goto-char (+ left n)))
2297 ((< n 0)
2298 (idlwave-indent-to end-col (- n))
2299 (goto-char (- left n)))
2300 ;; n = 0, done
2301 ))))
2302
2303 (defun idlwave-newline ()
2304 "Insert a newline and indent the current and previous line."
2305 (interactive)
2306 ;;
2307 ;; Handle unterminated single and double quotes
2308 ;; If not in a comment and in a string then insertion of a newline
2309 ;; will mean unbalanced quotes.
2310 ;;
2311 (if (and (not (idlwave-in-comment)) (idlwave-in-quote))
2312 (progn (beep)
2313 (message "Warning: unbalanced quotes?")))
2314 (newline)
2315 ;;
2316 ;; The current line is being split, the cursor should be at the
2317 ;; beginning of the new line skipping the leading indentation.
2318 ;;
2319 ;; The reason we insert the new line before indenting is that the
2320 ;; indenting could be confused by keywords (e.g. END) on the line
2321 ;; after the split point. This prevents us from just using
2322 ;; `indent-for-tab-command' followed by `newline-and-indent'.
2323 ;;
2324 (beginning-of-line 0)
2325 (idlwave-indent-line)
2326 (forward-line)
2327 (idlwave-indent-line))
2328
2329 ;;
2330 ;; Use global variable 'comment-column' to set parallel comment
2331 ;;
2332 ;; Modeled on lisp.el
2333 ;; Emacs Lisp and IDL (Wave CL) have identical comment syntax
2334 (defun idlwave-comment-hook ()
2335 "Compute indent for the beginning of the IDL comment delimiter."
2336 (if (or (looking-at idlwave-no-change-comment)
2337 (looking-at (or idlwave-begin-line-comment "^;")))
2338 (current-column)
2339 (if (looking-at idlwave-code-comment)
2340 (if (save-excursion (skip-chars-backward " \t") (bolp))
2341 ;; On line by itself, indent as code
2342 (let ((tem (idlwave-calculate-indent)))
2343 (if (listp tem) (car tem) tem))
2344 ;; after code - do not change
2345 (current-column))
2346 (skip-chars-backward " \t")
2347 (max (if (bolp) 0 (1+ (current-column)))
2348 comment-column))))
2349
2350 (defun idlwave-split-line ()
2351 "Continue line by breaking line at point and indent the lines.
2352 For a code line insert continuation marker. If the line is a line comment
2353 then the new line will contain a comment with the same indentation.
2354 Splits strings with the IDL operator `+' if `idlwave-split-line-string' is
2355 non-nil."
2356 (interactive)
2357 ;; Expand abbreviation, just like normal RET would.
2358 (and abbrev-mode (expand-abbrev))
2359 (let (beg)
2360 (if (not (idlwave-in-comment))
2361 ;; For code line add continuation.
2362 ;; Check if splitting a string.
2363 (progn
2364 (if (setq beg (idlwave-in-quote))
2365 (if idlwave-split-line-string
2366 ;; Split the string.
2367 (progn (insert (setq beg (char-after beg)) " + "
2368 idlwave-continuation-char beg)
2369 (backward-char 1)
2370 (newline-and-indent)
2371 (forward-char 1))
2372 ;; Do not split the string.
2373 (beep)
2374 (message "Warning: continuation inside string!!")
2375 (insert " " idlwave-continuation-char))
2376 ;; Not splitting a string.
2377 (if (not (member (char-before) '(?\ ?\t)))
2378 (insert " "))
2379 (insert idlwave-continuation-char)
2380 (newline-and-indent)))
2381 (indent-new-comment-line))
2382 ;; Indent previous line
2383 (setq beg (- (point-max) (point)))
2384 (forward-line -1)
2385 (idlwave-indent-line)
2386 (goto-char (- (point-max) beg))
2387 ;; Reindent new line
2388 (idlwave-indent-line)))
2389
2390 (defun idlwave-beginning-of-subprogram (&optional nomark)
2391 "Move point to the beginning of the current program unit.
2392 If NOMARK is non-nil, do not push mark."
2393 (interactive)
2394 (idlwave-find-key idlwave-begin-unit-reg -1 nomark))
2395
2396 (defun idlwave-end-of-subprogram (&optional nomark)
2397 "Move point to the start of the next program unit.
2398 If NOMARK is non-nil, do not push mark."
2399 (interactive)
2400 (idlwave-end-of-statement)
2401 (idlwave-find-key idlwave-end-unit-reg 1 nomark))
2402
2403 (defun idlwave-mark-statement ()
2404 "Mark current IDL statement."
2405 (interactive)
2406 (idlwave-end-of-statement)
2407 (let ((end (point)))
2408 (idlwave-beginning-of-statement)
2409 (push-mark end nil t)))
2410
2411 (defun idlwave-mark-block ()
2412 "Mark containing block."
2413 (interactive)
2414 (idlwave-end-of-statement)
2415 (idlwave-backward-up-block -1)
2416 (idlwave-end-of-statement)
2417 (let ((end (point)))
2418 (idlwave-backward-block)
2419 (idlwave-beginning-of-statement)
2420 (push-mark end nil t)))
2421
2422
2423 (defun idlwave-mark-subprogram ()
2424 "Put mark at beginning of program, point at end.
2425 The marks are pushed."
2426 (interactive)
2427 (idlwave-end-of-statement)
2428 (idlwave-beginning-of-subprogram)
2429 (let ((beg (point)))
2430 (idlwave-forward-block)
2431 (push-mark beg nil t))
2432 (exchange-point-and-mark))
2433
2434 (defun idlwave-backward-up-block (&optional arg)
2435 "Move to beginning of enclosing block if prefix ARG >= 0.
2436 If prefix ARG < 0 then move forward to enclosing block end."
2437 (interactive "p")
2438 (idlwave-block-jump-out (- arg) 'nomark))
2439
2440 (defun idlwave-beginning-of-block ()
2441 "Go to the beginning of the current block."
2442 (interactive)
2443 (idlwave-block-jump-out -1 'nomark)
2444 (forward-word 1))
2445
2446 (defun idlwave-end-of-block ()
2447 "Go to the beginning of the current block."
2448 (interactive)
2449 (idlwave-block-jump-out 1 'nomark)
2450 (backward-word 1))
2451
2452 (defun idlwave-forward-block (&optional arg)
2453 "Move across next nested block."
2454 (interactive)
2455 (let ((arg (or arg 1)))
2456 (if (idlwave-down-block arg)
2457 (idlwave-block-jump-out arg 'nomark))))
2458
2459 (defun idlwave-backward-block ()
2460 "Move backward across previous nested block."
2461 (interactive)
2462 (if (idlwave-down-block -1)
2463 (idlwave-block-jump-out -1 'nomark)))
2464
2465 (defun idlwave-down-block (&optional arg)
2466 "Go down a block.
2467 With ARG: ARG >= 0 go forwards, ARG < 0 go backwards.
2468 Returns non-nil if successfull."
2469 (interactive "p")
2470 (let (status)
2471 (if (< arg 0)
2472 ;; Backward
2473 (let ((eos (save-excursion
2474 (idlwave-block-jump-out -1 'nomark)
2475 (point))))
2476 (if (setq status (idlwave-find-key
2477 idlwave-end-block-reg -1 'nomark eos))
2478 (idlwave-beginning-of-statement)
2479 (message "No nested block before beginning of containing block.")))
2480 ;; Forward
2481 (let ((eos (save-excursion
2482 (idlwave-block-jump-out 1 'nomark)
2483 (point))))
2484 (if (setq status (idlwave-find-key
2485 idlwave-begin-block-reg 1 'nomark eos))
2486 (idlwave-end-of-statement)
2487 (message "No nested block before end of containing block."))))
2488 status))
2489
2490 (defun idlwave-mark-doclib ()
2491 "Put point at beginning of doc library header, mark at end.
2492 The marks are pushed."
2493 (interactive)
2494 (let (beg
2495 (here (point)))
2496 (goto-char (point-max))
2497 (if (re-search-backward idlwave-doclib-start nil t)
2498 (progn
2499 (setq beg (progn (beginning-of-line) (point)))
2500 (if (re-search-forward idlwave-doclib-end nil t)
2501 (progn
2502 (forward-line 1)
2503 (push-mark beg nil t)
2504 (message "Could not find end of doc library header.")))
2505 (message "Could not find doc library header start.")
2506 (goto-char here)))))
2507
2508 (defun idlwave-current-routine-fullname ()
2509 (let ((name (idlwave-current-routine)))
2510 (idlwave-make-full-name (nth 2 name) (car name))))
2511
2512 (defun idlwave-current-routine ()
2513 "Return (NAME TYPE CLASS) of current routine."
2514 (idlwave-routines)
2515 (save-excursion
2516 (idlwave-beginning-of-subprogram 'nomark)
2517 (if (looking-at "[ \t]*\\<\\(pro\\|function\\)\\>\\s-+\\(\\([a-zA-Z0-9$_]+\\)::\\)?\\([a-zA-Z0-9$_]+\\)")
2518 (let* ((type (if (string= (downcase (match-string 1)) "pro")
2519 'pro 'function))
2520 (class (idlwave-sintern-class (match-string 3)))
2521 (name (idlwave-sintern-routine-or-method (match-string 4) class)))
2522 (list name type class)))))
2523
2524 (defvar idlwave-shell-prompt-pattern)
2525 (defun idlwave-beginning-of-statement ()
2526 "Move to beginning of the current statement.
2527 Skips back past statement continuations.
2528 Point is placed at the beginning of the line whether or not this is an
2529 actual statement."
2530 (interactive)
2531 (cond
2532 ((derived-mode-p 'idlwave-shell-mode)
2533 (if (re-search-backward idlwave-shell-prompt-pattern nil t)
2534 (goto-char (match-end 0))))
2535 (t
2536 (if (save-excursion (forward-line -1) (idlwave-is-continuation-line))
2537 (idlwave-previous-statement)
2538 (beginning-of-line)))))
2539
2540 (defun idlwave-previous-statement ()
2541 "Move point to beginning of the previous statement.
2542 Returns t if the current line before moving is the beginning of
2543 the first non-comment statement in the file, and nil otherwise."
2544 (interactive)
2545 (let (first-statement)
2546 (if (not (= (forward-line -1) 0))
2547 ;; first line in file
2548 t
2549 ;; skip blank lines, label lines, include lines and line comments
2550 (while (and
2551 ;; The current statement is the first statement until we
2552 ;; reach another statement.
2553 (setq first-statement
2554 (or
2555 (looking-at idlwave-comment-line-start-skip)
2556 (looking-at "[ \t]*$")
2557 (looking-at (concat "[ \t]*" idlwave-label "[ \t]*$"))
2558 (looking-at "^@")))
2559 (= (forward-line -1) 0)))
2560 ;; skip continuation lines
2561 (while (and
2562 (save-excursion
2563 (forward-line -1)
2564 (idlwave-is-continuation-line))
2565 (= (forward-line -1) 0)))
2566 first-statement)))
2567
2568 (defun idlwave-end-of-statement ()
2569 "Move point to the end of the current IDL statement.
2570 If not in a statement just moves to end of line. Returns position."
2571 (interactive)
2572 (while (and (idlwave-is-continuation-line)
2573 (= (forward-line 1) 0))
2574 (while (and (idlwave-is-comment-or-empty-line)
2575 (= (forward-line 1) 0))))
2576 (end-of-line)
2577 (point))
2578
2579 (defun idlwave-end-of-statement0 ()
2580 "Move point to the end of the current IDL statement.
2581 If not in a statement just moves to end of line. Returns position."
2582 (interactive)
2583 (while (and (idlwave-is-continuation-line)
2584 (= (forward-line 1) 0)))
2585 (end-of-line)
2586 (point))
2587
2588 (defun idlwave-next-statement ()
2589 "Move point to beginning of the next IDL statement.
2590 Returns t if that statement is the last non-comment IDL statement
2591 in the file, and nil otherwise."
2592 (interactive)
2593 (let (last-statement)
2594 (idlwave-end-of-statement)
2595 ;; skip blank lines, label lines, include lines and line comments
2596 (while (and (= (forward-line 1) 0)
2597 ;; The current statement is the last statement until
2598 ;; we reach a new statement.
2599 (setq last-statement
2600 (or
2601 (looking-at idlwave-comment-line-start-skip)
2602 (looking-at "[ \t]*$")
2603 (looking-at (concat "[ \t]*" idlwave-label "[ \t]*$"))
2604 (looking-at "^@")))))
2605 last-statement))
2606
2607 (defun idlwave-skip-multi-commands (&optional lim)
2608 "Skip past multiple commands on a line (with `&')."
2609 (let ((save-point (point)))
2610 (when (re-search-forward ".*&" lim t)
2611 (goto-char (match-end 0))
2612 (if (idlwave-quoted)
2613 (goto-char save-point)
2614 (if (eq (char-after (- (point) 2)) ?&) (goto-char save-point))))
2615 (point)))
2616
2617 (defun idlwave-skip-label-or-case ()
2618 "Skip label or case statement element.
2619 Returns position after label.
2620 If there is no label point is not moved and nil is returned."
2621 ;; Case expressions and labels are terminated by a colon.
2622 ;; So we find the first colon in the line and make sure
2623 ;; - no `?' is before it (might be a ? b : c)
2624 ;; - it is not in a comment
2625 ;; - not in a string constant
2626 ;; - not in parenthesis (like a[0:3])
2627 ;; - not followed by another ":" in explicit class, ala a->b::c
2628 ;; As many in this mode, this function is heuristic and not an exact
2629 ;; parser.
2630 (let* ((start (point))
2631 (eos (save-excursion (idlwave-end-of-statement) (point)))
2632 (end (idlwave-find-key ":" 1 'nomark eos)))
2633 (if (and end
2634 (= (nth 0 (parse-partial-sexp start end)) 0)
2635 (not (string-match "\\?" (buffer-substring start end)))
2636 (not (string-match "^::" (buffer-substring end eos))))
2637 (progn
2638 (forward-char)
2639 (point))
2640 (goto-char start)
2641 nil)))
2642
2643 (defun idlwave-start-of-substatement (&optional pre)
2644 "Move to start of next IDL substatement after point.
2645 Uses the type of the current IDL statement to determine if the next
2646 statement is on a new line or is a subpart of the current statement.
2647 Returns point at start of substatement modulo whitespace.
2648 If optional argument is non-nil move to beginning of current
2649 substatement."
2650 (let ((orig (point))
2651 (eos (idlwave-end-of-statement))
2652 (ifnest 0)
2653 st nst last)
2654 (idlwave-beginning-of-statement)
2655 (idlwave-skip-label-or-case)
2656 (if (< (point) orig)
2657 (idlwave-skip-multi-commands orig))
2658 (setq last (point))
2659 ;; Continue looking for substatements until we are past orig
2660 (while (and (<= (point) orig) (not (eobp)))
2661 (setq last (point))
2662 (setq nst (nth 1 (cdr (setq st (car (idlwave-statement-type))))))
2663 (if (equal (car st) 'if) (setq ifnest (1+ ifnest)))
2664 (cond ((and nst
2665 (idlwave-find-key nst 1 'nomark eos))
2666 (goto-char (match-end 0)))
2667 ((and (> ifnest 0) (idlwave-find-key "\\<else\\>" 1 'nomark eos))
2668 (setq ifnest (1- ifnest))
2669 (goto-char (match-end 0)))
2670 (t (setq ifnest 0)
2671 (idlwave-next-statement))))
2672 (if pre (goto-char last))
2673 ;; If a continuation line starts here, move to next line
2674 (if (looking-at "[ \t]*\\$\\([ \t]*\\(;\\|$\\)\\)")
2675 (beginning-of-line 2))
2676 (point)))
2677
2678 (defun idlwave-statement-type ()
2679 "Return the type of the current IDL statement.
2680 Uses `idlwave-statement-match' to return a cons of (type . point) with
2681 point the ending position where the type was determined. Type is the
2682 association from `idlwave-statement-match', i.e. the cons cell from the
2683 list not just the type symbol. Returns nil if not an identifiable
2684 statement."
2685 (save-excursion
2686 ;; Skip whitespace within a statement which is spaces, tabs, continuations
2687 ;; and possibly comments
2688 (while (looking-at "[ \t]*\\$")
2689 (forward-line 1))
2690 (skip-chars-forward " \t")
2691 (let ((st idlwave-statement-match)
2692 (case-fold-search t))
2693 (while (and (not (looking-at (nth 0 (cdr (car st)))))
2694 (setq st (cdr st))))
2695 (if st
2696 (append st (match-end 0))))))
2697
2698 (defun idlwave-expand-equal (&optional before after is-action)
2699 "Pad '=' with spaces.
2700 Two cases: Assignment statement, and keyword assignment.
2701 Which case is determined using `idlwave-start-of-substatement' and
2702 `idlwave-statement-type'. The equal sign will be surrounded by BEFORE
2703 and AFTER blanks. If `idlwave-pad-keyword' is t then keyword assignment
2704 is treated just like assignment statements. When nil, spaces are
2705 removed for keyword assignment. Any other value keeps the current space
2706 around the `='. Limits in for loops are treated as keyword assignment.
2707
2708 Starting with IDL 6.0, a number of op= assignments are available.
2709 Since ambiguities of the form:
2710
2711 r and= b
2712 rand= b
2713
2714 can occur, alphanumeric operator assignment will never be pre-padded,
2715 only post-padded. You must use a space before these to disambiguate
2716 \(not just for padding, but for proper parsing by IDL too!). Other
2717 operators, such as ##=, ^=, etc., will be pre-padded.
2718
2719 IS-ACTION is ignored.
2720
2721 See `idlwave-surround'."
2722 (if idlwave-surround-by-blank
2723 (let
2724 ((non-an-ops "\\(##\\|\\*\\|\\+\\|-\\|/\\|<\\|>\\|\\^\\)\\=")
2725 (an-ops
2726 "\\s-\\(AND\\|EQ\\|GE\\|GT\\|LE\\|LT\\|MOD\\|NE\\|OR\\|XOR\\)\\=")
2727 (len 1))
2728
2729 (save-excursion
2730 (let ((case-fold-search t))
2731 (backward-char)
2732 (if (or
2733 (re-search-backward non-an-ops nil t)
2734 ;; Why doesn't ##? work for both?
2735 (re-search-backward "\\(#\\)\\=" nil t))
2736 (setq len (1+ (length (match-string 1))))
2737 (when (re-search-backward an-ops nil t)
2738 ;(setq begin nil) ; won't modify begin
2739 (setq len (1+ (length (match-string 1))))))))
2740
2741 (if (eq t idlwave-pad-keyword)
2742 ;; Everything gets padded equally
2743 (idlwave-surround before after len)
2744 ;; Treating keywords/for variables specially...
2745 (let ((st (save-excursion ; To catch "for" variables
2746 (idlwave-start-of-substatement t)
2747 (idlwave-statement-type)))
2748 (what (save-excursion ; To catch keywords
2749 (skip-chars-backward "= \t")
2750 (nth 2 (idlwave-where)))))
2751 (cond ((or (memq what '(function-keyword procedure-keyword))
2752 (memq (caar st) '(for pdef)))
2753 (cond
2754 ((null idlwave-pad-keyword)
2755 (idlwave-surround 0 0)
2756 ) ; remove space
2757 (t))) ; leave any spaces alone
2758 (t (idlwave-surround before after len))))))))
2759
2760
2761 (defun idlwave-indent-and-action (&optional arg)
2762 "Call `idlwave-indent-line' and do expand actions.
2763 With prefix ARG non-nil, indent the entire sub-statement."
2764 (interactive "p")
2765 (save-excursion
2766 (if (and idlwave-expand-generic-end
2767 (re-search-backward "\\<\\(end\\)\\s-*\\="
2768 (max 0 (- (point) 10)) t)
2769 (looking-at "\\(end\\)\\([ \n\t]\\|\\'\\)"))
2770 (progn (goto-char (match-end 1))
2771 ;;Expand the END abbreviation, just as RET or Space would have.
2772 (if abbrev-mode (expand-abbrev)
2773 (idlwave-show-begin)))))
2774 (when (and (not arg) current-prefix-arg)
2775 (setq arg current-prefix-arg)
2776 (setq current-prefix-arg nil))
2777 (if arg
2778 (idlwave-indent-statement)
2779 (idlwave-indent-line t)))
2780
2781 (defun idlwave-indent-line (&optional expand)
2782 "Indent current IDL line as code or as a comment.
2783 The actions in `idlwave-indent-action-table' are performed.
2784 If the optional argument EXPAND is non-nil then the actions in
2785 `idlwave-indent-expand-table' are performed."
2786 (interactive)
2787 ;; Move point out of left margin.
2788 (if (save-excursion
2789 (skip-chars-backward " \t")
2790 (bolp))
2791 (skip-chars-forward " \t"))
2792 (let ((mloc (point-marker)))
2793 (save-excursion
2794 (beginning-of-line)
2795 (if (looking-at idlwave-comment-line-start-skip)
2796 ;; Indentation for a line comment
2797 (progn
2798 (skip-chars-forward " \t")
2799 (idlwave-indent-left-margin (idlwave-comment-hook)))
2800 ;;
2801 ;; Code Line
2802 ;;
2803 ;; Before indenting, run action routines.
2804 ;;
2805 (if (and expand idlwave-do-actions)
2806 (mapc 'idlwave-do-action idlwave-indent-expand-table))
2807 ;;
2808 (if idlwave-do-actions
2809 (mapc 'idlwave-do-action idlwave-indent-action-table))
2810 ;;
2811 ;; No longer expand abbrevs on the line. The user can do this
2812 ;; manually using expand-region-abbrevs.
2813 ;;
2814 ;; Indent for code line
2815 ;;
2816 (beginning-of-line)
2817 (if (or
2818 ;; a label line
2819 (looking-at (concat "^" idlwave-label "[ \t]*$"))
2820 ;; a batch command
2821 (looking-at "^[ \t]*@"))
2822 ;; leave flush left
2823 nil
2824 ;; indent the line
2825 (idlwave-indent-left-margin (idlwave-calculate-indent)))
2826 ;; Adjust parallel comment
2827 (end-of-line)
2828 (if (idlwave-in-comment)
2829 ;; Emacs 21 is too smart with fill-column on comment indent
2830 (let ((fill-column (if (fboundp 'comment-indent-new-line)
2831 (1- (frame-width))
2832 fill-column)))
2833 (indent-for-comment)))))
2834 (goto-char mloc)
2835 ;; Get rid of marker
2836 (set-marker mloc nil)))
2837
2838 (defun idlwave-do-action (action)
2839 "Perform an action repeatedly on a line.
2840 ACTION is a list (REG . FUNC). REG is a regular expression. FUNC is
2841 either a function name to be called with `funcall' or a list to be
2842 evaluated with `eval'. The action performed by FUNC should leave
2843 point after the match for REG - otherwise an infinite loop may be
2844 entered. FUNC is always passed a final argument of 'is-action, so it
2845 can discriminate between being run as an action, or a key binding."
2846 (let ((action-key (car action))
2847 (action-routine (cdr action)))
2848 (beginning-of-line)
2849 (while (idlwave-look-at action-key)
2850 (if (listp action-routine)
2851 (eval (append action-routine '('is-action)))
2852 (funcall action-routine 'is-action)))))
2853
2854 (defun idlwave-indent-to (col &optional min)
2855 "Indent from point with spaces until column COL.
2856 Inserts space before markers at point."
2857 (if (not min) (setq min 0))
2858 (insert-before-markers
2859 (make-string (max min (- col (current-column))) ?\ )))
2860
2861 (defun idlwave-indent-left-margin (col)
2862 "Indent the current line to column COL.
2863 Indents such that first non-whitespace character is at column COL
2864 Inserts spaces before markers at point."
2865 (save-excursion
2866 (beginning-of-line)
2867 (delete-horizontal-space)
2868 (idlwave-indent-to col)))
2869
2870 (defun idlwave-indent-subprogram ()
2871 "Indent program unit which contains point."
2872 (interactive)
2873 (save-excursion
2874 (idlwave-end-of-statement)
2875 (idlwave-beginning-of-subprogram)
2876 (let ((beg (point)))
2877 (idlwave-forward-block)
2878 (message "Indenting subprogram...")
2879 (indent-region beg (point) nil))
2880 (message "Indenting subprogram...done.")))
2881
2882 (defun idlwave-indent-statement ()
2883 "Indent current statement, including all continuation lines."
2884 (interactive)
2885 (save-excursion
2886 (idlwave-beginning-of-statement)
2887 (let ((beg (point)))
2888 (idlwave-end-of-statement)
2889 (indent-region beg (point) nil))))
2890
2891 (defun idlwave-calculate-indent ()
2892 "Return appropriate indentation for current line as IDL code."
2893 (save-excursion
2894 (beginning-of-line)
2895 (cond
2896 ;; Check for beginning of unit - main (beginning of buffer), pro, or
2897 ;; function
2898 ((idlwave-look-at idlwave-begin-unit-reg)
2899 0)
2900 ;; Check for continuation line
2901 ((save-excursion
2902 (and (= (forward-line -1) 0)
2903 (idlwave-is-continuation-line)))
2904 (idlwave-calculate-cont-indent))
2905 ;; calculate indent based on previous and current statements
2906 (t (let* (beg-prev-pos
2907 (the-indent
2908 ;; calculate indent based on previous statement
2909 (save-excursion
2910 (cond
2911 ;; Beginning of file
2912 ((prog1
2913 (idlwave-previous-statement)
2914 (setq beg-prev-pos (point)))
2915 0)
2916 ;; Main block
2917 ((idlwave-look-at idlwave-begin-unit-reg t)
2918 (+ (idlwave-current-statement-indent)
2919 idlwave-main-block-indent))
2920 ;; Begin block
2921 ((idlwave-look-at idlwave-begin-block-reg t)
2922 (+ (idlwave-min-current-statement-indent)
2923 idlwave-block-indent))
2924 ;; End Block
2925 ((idlwave-look-at idlwave-end-block-reg t)
2926 (progn
2927 ;; Match to the *beginning* of the block opener
2928 (goto-char beg-prev-pos)
2929 (idlwave-block-jump-out -1 'nomark) ; go to begin block
2930 (idlwave-min-current-statement-indent)))
2931 ;; idlwave-end-offset
2932 ;; idlwave-block-indent))
2933
2934 ;; Default to current indent
2935 ((idlwave-current-statement-indent))))))
2936 ;; adjust the indentation based on the current statement
2937 (cond
2938 ;; End block
2939 ((idlwave-look-at idlwave-end-block-reg)
2940 (+ the-indent idlwave-end-offset))
2941 (the-indent)))))))
2942
2943 ;;
2944 ;; Parentheses indent
2945 ;;
2946
2947 (defun idlwave-calculate-paren-indent (beg-reg end-reg close-exp)
2948 "Calculate the continuation indent inside a paren group.
2949 Returns a cons-cell with (open . indent), where open is the
2950 location of the open paren."
2951 (let ((open (nth 1 (parse-partial-sexp beg-reg end-reg))))
2952 ;; Found an innermost open paren.
2953 (when open
2954 (goto-char open)
2955 ;; Line up with next word unless this is a closing paren.
2956 (cons open
2957 (cond
2958 ;; Plain Kernighan-style nested indent
2959 (idlwave-indent-parens-nested
2960 (+ idlwave-continuation-indent (idlwave-current-indent)))
2961
2962 ;; This is a closed paren - line up under open paren.
2963 (close-exp
2964 (current-column))
2965
2966 ;; Empty (or just comment) follows -- revert to basic indent
2967 ((progn
2968 ;; Skip paren
2969 (forward-char 1)
2970 (looking-at "[ \t$]*\\(;.*\\)?$"))
2971 nil)
2972
2973 ;; Line up with first word after any blank space
2974 ((progn
2975 (skip-chars-forward " \t")
2976 (current-column))))))))
2977
2978 (defun idlwave-calculate-cont-indent ()
2979 "Calculates the IDL continuation indent column from the previous statement.
2980 Note that here previous statement usually means the beginning of the
2981 current statement if this statement is a continuation of the previous
2982 line. Various special types of continuations, including assignments,
2983 routine definitions, and parenthetical groupings, are treated separately."
2984 (save-excursion
2985 (let* ((case-fold-search t)
2986 (end-reg (progn (beginning-of-line) (point)))
2987 (beg-last-statement (save-excursion (idlwave-previous-statement)
2988 (point)))
2989 (beg-reg (progn (idlwave-start-of-substatement 'pre)
2990 (if (eq (line-beginning-position) end-reg)
2991 (goto-char beg-last-statement)
2992 (point))))
2993 (basic-indent (+ (idlwave-min-current-statement-indent end-reg)
2994 idlwave-continuation-indent))
2995 fancy-nonparen-indent fancy-paren-indent)
2996 (cond
2997 ;; Align then with its matching if, etc.
2998 ((let ((matchers '(("\\<if\\>" . "[ \t]*then")
2999 ("\\<\\(if\\|end\\(if\\)?\\)\\>" . "[ \t]*else")
3000 ("\\<\\(for\\|while\\)\\>" . "[ \t]*do")
3001 ("\\<\\(repeat\\|end\\(rep\\)?\\)\\>" .
3002 "[ \t]*until")
3003 ("\\<case\\>" . "[ \t]*of")))
3004 match cont-re)
3005 (goto-char end-reg)
3006 (and
3007 (setq cont-re
3008 (catch 'exit
3009 (while (setq match (car matchers))
3010 (if (looking-at (cdr match))
3011 (throw 'exit (car match)))
3012 (setq matchers (cdr matchers)))))
3013 (idlwave-find-key cont-re -1 'nomark beg-last-statement)))
3014 (if (looking-at "end") ;; that one's special
3015 (- (idlwave-current-indent)
3016 (+ idlwave-block-indent idlwave-end-offset))
3017 (idlwave-current-indent)))
3018
3019 ;; Indent in from the previous line for continuing statements
3020 ((let ((matchers '("\\<then\\>"
3021 "\\<do\\>"
3022 "\\<repeat\\>"
3023 "\\<else\\>"))
3024 match)
3025 (catch 'exit
3026 (goto-char end-reg)
3027 (if (/= (forward-line -1) 0)
3028 (throw 'exit nil))
3029 (while (setq match (car matchers))
3030 (if (looking-at (concat ".*" match "[ \t]*\\$[ \t]*"
3031 "\\(;.*\\)?$"))
3032 (throw 'exit t))
3033 (setq matchers (cdr matchers)))))
3034 (+ idlwave-continuation-indent (idlwave-current-indent)))
3035
3036 ;; Parenthetical indent, either traditional or Kernighan style
3037 ((setq fancy-paren-indent
3038 (let* ((end-reg end-reg)
3039 (close-exp (progn
3040 (goto-char end-reg)
3041 (skip-chars-forward " \t")
3042 (looking-at "\\s)")))
3043 indent-cons)
3044 (catch 'loop
3045 (while (setq indent-cons (idlwave-calculate-paren-indent
3046 beg-reg end-reg close-exp))
3047 ;; First permitted containing paren
3048 (if (or
3049 idlwave-indent-to-open-paren
3050 idlwave-indent-parens-nested
3051 (null (cdr indent-cons))
3052 (< (- (cdr indent-cons) basic-indent)
3053 idlwave-max-extra-continuation-indent))
3054 (throw 'loop (cdr indent-cons)))
3055 (setq end-reg (car indent-cons))))))
3056 fancy-paren-indent)
3057
3058 ;; A continued assignment, or procedure call/definition
3059 ((and
3060 (> idlwave-max-extra-continuation-indent 0)
3061 (setq fancy-nonparen-indent
3062 (progn
3063 (goto-char beg-reg)
3064 (while (idlwave-look-at "&")) ; skip continued statements
3065 (cond
3066 ;; A continued Procedure call or definition
3067 ((progn
3068 (idlwave-look-at "^[ \t]*\\(pro\\|function\\)") ;skip over
3069 (looking-at "[ \t]*\\([a-zA-Z0-9.$_]+[ \t]*->[ \t]*\\)?[a-zA-Z][:a-zA-Z0-9$_]*[ \t]*\\(,\\)[ \t]*"))
3070 (goto-char (match-end 0))
3071 ;; Comment only, or blank line with "$"? Basic indent.
3072 (if (save-match-data (looking-at "[ \t$]*\\(;.*\\)?$"))
3073 nil
3074 (current-column)))
3075
3076 ;; Continued assignment (with =):
3077 ((catch 'assign ;
3078 (while (looking-at "[^=\n\r]*\\(=\\)[ \t]*")
3079 (goto-char (match-end 0))
3080 (if (null (idlwave-what-function beg-reg))
3081 (throw 'assign t))))
3082 (unless (or
3083 (idlwave-in-quote)
3084 (looking-at "[ \t$]*\\(;.*\\)?$") ; use basic
3085 (save-excursion
3086 (goto-char beg-last-statement)
3087 (eq (caar (idlwave-statement-type)) 'for)))
3088 (current-column))))))
3089 (< (- fancy-nonparen-indent basic-indent)
3090 idlwave-max-extra-continuation-indent))
3091 (if fancy-paren-indent ;calculated but disallowed paren indent
3092 (+ fancy-nonparen-indent idlwave-continuation-indent)
3093 fancy-nonparen-indent))
3094
3095 ;; Basic indent, by default
3096 (t basic-indent)))))
3097
3098
3099
3100 (defun idlwave-find-key (key-re &optional dir nomark limit)
3101 "Move to next match of the regular expression KEY-RE.
3102 Matches inside comments or string constants will be ignored.
3103 If DIR is negative, the search will be backwards.
3104 At a successful match, the mark is pushed unless NOMARK is non-nil.
3105 Searches are limited to LIMIT.
3106 Searches are case-insensitive and use a special syntax table which
3107 treats `$' and `_' as word characters.
3108 Return value is the beginning of the match or (in case of failure) nil."
3109 (setq dir (or dir 0))
3110 (let ((case-fold-search t)
3111 (search-func (if (> dir 0) 're-search-forward 're-search-backward))
3112 found)
3113 (idlwave-with-special-syntax
3114 (save-excursion
3115 (catch 'exit
3116 (while (funcall search-func key-re limit t)
3117 (if (not (idlwave-quoted))
3118 (throw 'exit (setq found (match-beginning 0)))
3119 (if (or (and (> dir 0) (eobp))
3120 (and (< dir 0) (bobp)))
3121 (throw 'exit nil)))))))
3122 (if found
3123 (progn
3124 (if (not nomark) (push-mark))
3125 (goto-char found)
3126 found)
3127 nil)))
3128
3129 (defun idlwave-block-jump-out (&optional dir nomark)
3130 "When optional argument DIR is non-negative, move forward to end of
3131 current block using the `idlwave-begin-block-reg' and `idlwave-end-block-reg'
3132 regular expressions. When DIR is negative, move backwards to block beginning.
3133 Recursively calls itself to skip over nested blocks. DIR defaults to
3134 forward. Calls `push-mark' unless the optional argument NOMARK is
3135 non-nil. Movement is limited by the start of program units because of
3136 possibility of unbalanced blocks."
3137 (interactive "P")
3138 (or dir (setq dir 0))
3139 (let* ((here (point))
3140 (case-fold-search t)
3141 (limit (if (>= dir 0) (point-max) (point-min)))
3142 (block-limit (if (>= dir 0)
3143 idlwave-begin-block-reg
3144 idlwave-end-block-reg))
3145 found
3146 (block-reg (concat idlwave-begin-block-reg "\\|"
3147 idlwave-end-block-reg))
3148 (unit-limit (or (save-excursion
3149 (if (< dir 0)
3150 (idlwave-find-key
3151 idlwave-begin-unit-reg dir t limit)
3152 (end-of-line)
3153 (idlwave-find-key
3154 idlwave-end-unit-reg dir t limit)))
3155 limit)))
3156 (if (>= dir 0) (end-of-line)) ;Make sure we are in current block
3157 (if (setq found (idlwave-find-key block-reg dir t unit-limit))
3158 (while (and found (looking-at block-limit))
3159 (if (>= dir 0) (forward-word 1))
3160 (idlwave-block-jump-out dir t)
3161 (setq found (idlwave-find-key block-reg dir t unit-limit))))
3162 (if (not nomark) (push-mark here))
3163 (if (not found) (goto-char unit-limit)
3164 (if (>= dir 0) (forward-word 1)))))
3165
3166 (defun idlwave-min-current-statement-indent (&optional end-reg)
3167 "The minimum indent in the current statement."
3168 (idlwave-beginning-of-statement)
3169 (if (not (idlwave-is-continuation-line))
3170 (idlwave-current-indent)
3171 (let ((min (idlwave-current-indent)) comm-or-empty)
3172 (while (and (= (forward-line 1) 0)
3173 (or (setq comm-or-empty (idlwave-is-comment-or-empty-line))
3174 (idlwave-is-continuation-line))
3175 (or (null end-reg) (< (point) end-reg)))
3176 (unless comm-or-empty (setq min (min min (idlwave-current-indent)))))
3177 (if (or comm-or-empty (and end-reg (>= (point) end-reg)))
3178 min
3179 (min min (idlwave-current-indent))))))
3180
3181 (defun idlwave-current-statement-indent (&optional last-line)
3182 "Return indentation of the current statement.
3183 If in a statement, moves to beginning of statement before finding indent."
3184 (if last-line
3185 (idlwave-end-of-statement)
3186 (idlwave-beginning-of-statement))
3187 (idlwave-current-indent))
3188
3189 (defun idlwave-current-indent ()
3190 "Return the column of the indentation of the current line.
3191 Skips any whitespace. Returns 0 if the end-of-line follows the whitespace."
3192 (save-excursion
3193 (beginning-of-line)
3194 (skip-chars-forward " \t")
3195 ;; if we are at the end of blank line return 0
3196 (cond ((eolp) 0)
3197 ((current-column)))))
3198
3199 (defun idlwave-is-continuation-line ()
3200 "Test if current line is continuation line.
3201 Blank or comment-only lines following regular continuation lines (with
3202 `$') count as continuations too."
3203 (let (p)
3204 (save-excursion
3205 (or
3206 (idlwave-look-at "\\<\\$")
3207 (catch 'loop
3208 (while (and (looking-at "^[ \t]*\\(;.*\\)?$")
3209 (eq (forward-line -1) 0))
3210 (if (setq p (idlwave-look-at "\\<\\$")) (throw 'loop p))))))))
3211
3212 (defun idlwave-is-comment-line ()
3213 "Test if the current line is a comment line."
3214 (save-excursion
3215 (beginning-of-line 1)
3216 (looking-at "[ \t]*;")))
3217
3218 (defun idlwave-is-comment-or-empty-line ()
3219 "Test if the current line is a comment line."
3220 (save-excursion
3221 (beginning-of-line 1)
3222 (looking-at "[ \t]*[;\n]")))
3223
3224 (defun idlwave-look-at (regexp &optional cont beg)
3225 "Search current line from current point for REGEXP.
3226 If optional argument CONT is non-nil, searches to the end of
3227 the current statement.
3228 If optional arg BEG is non-nil, search starts from the beginning of the
3229 current statement.
3230 Ignores matches that end in a comment or inside a string expression.
3231 Returns point if successful, nil otherwise.
3232 This function produces unexpected results if REGEXP contains quotes or
3233 a comment delimiter. The search is case insensitive.
3234 If successful leaves point after the match, otherwise, does not move point."
3235 (let ((here (point))
3236 (case-fold-search t)
3237 (eos (save-excursion
3238 (if cont (idlwave-end-of-statement) (end-of-line))
3239 (point)))
3240 found)
3241 (idlwave-with-special-syntax
3242 (if beg (idlwave-beginning-of-statement))
3243 (while (and (setq found (re-search-forward regexp eos t))
3244 (idlwave-quoted))))
3245 (if (not found) (goto-char here))
3246 found))
3247
3248 (defun idlwave-fill-paragraph (&optional nohang)
3249 "Fill paragraphs in comments.
3250 A paragraph is made up of all contiguous lines having the same comment
3251 leader (the leading whitespace before the comment delimiter and the
3252 comment delimiter). In addition, paragraphs are separated by blank
3253 line comments. The indentation is given by the hanging indent of the
3254 first line, otherwise by the minimum indentation of the lines after
3255 the first line. The indentation of the first line does not change.
3256 Does not effect code lines. Does not fill comments on the same line
3257 with code. The hanging indent is given by the end of the first match
3258 matching `idlwave-hang-indent-regexp' on the paragraph's first line.
3259 If the optional argument NOHANG is non-nil then the hanging indent is
3260 ignored."
3261 (interactive "P")
3262 ;; check if this is a line comment
3263 (if (save-excursion
3264 (beginning-of-line)
3265 (skip-chars-forward " \t")
3266 (looking-at comment-start))
3267 (let
3268 ((indent 999)
3269 pre here diff fill-prefix-reg bcl first-indent
3270 hang start end)
3271 ;; Change tabs to spaces in the surrounding paragraph.
3272 ;; The surrounding paragraph will be the largest containing block of
3273 ;; contiguous line comments. Thus, we may be changing tabs in
3274 ;; a much larger area than is needed, but this is the easiest
3275 ;; brute force way to do it.
3276 ;;
3277 ;; This has the undesirable side effect of replacing the tabs
3278 ;; permanently without the user's request or knowledge.
3279 (save-excursion
3280 (backward-paragraph)
3281 (setq start (point)))
3282 (save-excursion
3283 (forward-paragraph)
3284 (setq end (point)))
3285 (untabify start end)
3286 ;;
3287 (setq here (point))
3288 (beginning-of-line)
3289 (setq bcl (point))
3290 (re-search-forward (concat "^[ \t]*" comment-start "+")
3291 (point-at-eol) t)
3292 ;; Get the comment leader on the line and its length
3293 (setq pre (current-column))
3294 ;; the comment leader is the indentation plus exactly the
3295 ;; number of consecutive ";".
3296 (setq fill-prefix-reg
3297 (concat
3298 (setq fill-prefix
3299 (regexp-quote (buffer-substring (point-at-bol) (point))))
3300 "[^;]"))
3301
3302 ;; Mark the beginning and end of the paragraph
3303 (goto-char bcl)
3304 (while (and (looking-at fill-prefix-reg)
3305 (not (looking-at paragraph-separate))
3306 (not (bobp)))
3307 (forward-line -1))
3308 ;; Move to first line of paragraph
3309 (if (/= (point) bcl)
3310 (forward-line 1))
3311 (setq start (point))
3312 (goto-char bcl)
3313 (while (and (looking-at fill-prefix-reg)
3314 (not (looking-at paragraph-separate))
3315 (not (eobp)))
3316 (forward-line 1))
3317 (beginning-of-line)
3318 (if (or (not (looking-at fill-prefix-reg))
3319 (looking-at paragraph-separate))
3320 (forward-line -1))
3321 (end-of-line)
3322 ;; if at end of buffer add a newline (need this because
3323 ;; fill-region needs END to be at the beginning of line after
3324 ;; the paragraph or it will add a line).
3325 (if (eobp)
3326 (progn (insert ?\n) (backward-char 1)))
3327 ;; Set END to the beginning of line after the paragraph
3328 ;; END is calculated as distance from end of buffer
3329 (setq end (- (point-max) (point) 1))
3330 ;;
3331 ;; Calculate the indentation for the paragraph.
3332 ;;
3333 ;; In the following while statements, after one iteration
3334 ;; point will be at the beginning of a line in which case
3335 ;; the while will not be executed for the
3336 ;; the first paragraph line and thus will not affect the
3337 ;; indentation.
3338 ;;
3339 ;; First check to see if indentation is based on hanging indent.
3340 (if (and (not nohang) idlwave-hanging-indent
3341 (setq hang
3342 (save-excursion
3343 (goto-char start)
3344 (idlwave-calc-hanging-indent))))
3345 ;; Adjust lines of paragraph by inserting spaces so that
3346 ;; each line's indent is at least as great as the hanging
3347 ;; indent. This is needed for fill-paragraph to work with
3348 ;; a fill-prefix.
3349 (progn
3350 (setq indent hang)
3351 (beginning-of-line)
3352 (while (> (point) start)
3353 (re-search-forward comment-start-skip (point-at-eol) t)
3354 (if (> (setq diff (- indent (current-column))) 0)
3355 (progn
3356 (if (>= here (point))
3357 ;; adjust the original location for the
3358 ;; inserted text.
3359 (setq here (+ here diff)))
3360 (insert (make-string diff ?\ ))))
3361 (forward-line -1))
3362 )
3363
3364 ;; No hang. Instead find minimum indentation of paragraph
3365 ;; after first line.
3366 ;; For the following while statement, since START is at the
3367 ;; beginning of line and END is at the end of line
3368 ;; point is greater than START at least once (which would
3369 ;; be the case for a single line paragraph).
3370 (while (> (point) start)
3371 (beginning-of-line)
3372 (setq indent
3373 (min indent
3374 (progn
3375 (re-search-forward comment-start-skip (point-at-eol) t)
3376 (current-column))))
3377 (forward-line -1)))
3378 (setq fill-prefix (concat fill-prefix
3379 (make-string (- indent pre)
3380 ?\ )))
3381 ;; first-line indent
3382 (setq first-indent
3383 (max
3384 (progn
3385 (re-search-forward comment-start-skip (point-at-eol) t)
3386 (current-column))
3387 indent))
3388
3389 ;; try to keep point at its original place
3390 (goto-char here)
3391
3392 ;; In place of the more modern fill-region-as-paragraph, a hack
3393 ;; to keep whitespace untouched on the first line within the
3394 ;; indent length and to preserve any indent on the first line
3395 ;; (first indent).
3396 (save-excursion
3397 (setq diff
3398 (buffer-substring start (+ start first-indent -1)))
3399 (subst-char-in-region start (+ start first-indent -1) ?\ ?~ nil)
3400 (fill-region-as-paragraph
3401 start
3402 (- (point-max) end)
3403 (current-justification)
3404 nil)
3405 (delete-region start (+ start first-indent -1))
3406 (goto-char start)
3407 (insert diff))
3408 ;; When we want the point at the beginning of the comment
3409 ;; body fill-region will put it at the beginning of the line.
3410 (if (bolp) (skip-chars-forward (concat " \t" comment-start)))
3411 (setq fill-prefix nil))))
3412
3413 (defun idlwave-calc-hanging-indent ()
3414 "Calculate the position of the hanging indent for the comment paragraph.
3415 The hanging indent position is given by the first match with the
3416 `idlwave-hang-indent-regexp'. If `idlwave-use-last-hang-indent' is
3417 non-nil then use last occurrence matching `idlwave-hang-indent-regexp'
3418 on the line.
3419 If not found returns nil."
3420 (if idlwave-use-last-hang-indent
3421 (save-excursion
3422 (end-of-line)
3423 (if (re-search-backward idlwave-hang-indent-regexp (point-at-bol) t)
3424 (+ (current-column) (length idlwave-hang-indent-regexp))))
3425 (save-excursion
3426 (beginning-of-line)
3427 (if (re-search-forward idlwave-hang-indent-regexp (point-at-eol) t)
3428 (current-column)))))
3429
3430 (defun idlwave-auto-fill ()
3431 "Called to break lines in auto fill mode.
3432 Only fills non-comment lines if `idlwave-fill-comment-line-only' is
3433 non-nil. Places a continuation character at the end of the line if
3434 not in a comment. Splits strings with IDL concatenation operator `+'
3435 if `idlwave-auto-fill-split-string' is non-nil."
3436 (if (<= (current-column) fill-column)
3437 nil ; do not to fill
3438 (if (or (not idlwave-fill-comment-line-only)
3439 (save-excursion
3440 ;; Check for comment line
3441 (beginning-of-line)
3442 (looking-at idlwave-comment-line-start-skip)))
3443 (let (beg)
3444 (idlwave-indent-line)
3445 ;; Prevent actions do-auto-fill which calls indent-line-function.
3446 (let (idlwave-do-actions
3447 (paragraph-separate ".")
3448 (fill-nobreak-predicate
3449 (if (and (idlwave-in-quote)
3450 idlwave-auto-fill-split-string)
3451 (lambda () ;; We'll need 5 spaces for " ' + $"
3452 (<= (- fill-column (current-column)) 5)
3453 ))))
3454 (do-auto-fill))
3455 (save-excursion
3456 (end-of-line 0)
3457 ;; Indent the split line
3458 (idlwave-indent-line))
3459 (if (save-excursion
3460 (beginning-of-line)
3461 (looking-at idlwave-comment-line-start-skip))
3462 ;; A continued line comment
3463 ;; We treat continued line comments as part of a comment
3464 ;; paragraph. So we check for a hanging indent.
3465 (if idlwave-hanging-indent
3466 (let ((here (- (point-max) (point)))
3467 (indent
3468 (save-excursion
3469 (forward-line -1)
3470 (idlwave-calc-hanging-indent))))
3471 (when indent
3472 ;; Remove whitespace between comment delimiter and
3473 ;; text, insert spaces for appropriate indentation.
3474 (beginning-of-line)
3475 (re-search-forward comment-start-skip (point-at-eol) t)
3476 (delete-horizontal-space)
3477 (idlwave-indent-to indent)
3478 (goto-char (- (point-max) here)))))
3479 ;; Split code or comment?
3480 (if (save-excursion
3481 (end-of-line 0)
3482 (idlwave-in-comment))
3483 ;; Splitting a non-full-line comment.
3484 ;; Insert the comment delimiter from split line
3485 (progn
3486 (save-excursion
3487 (beginning-of-line)
3488 (skip-chars-forward " \t")
3489 ;; Insert blank to keep off beginning of line
3490 (insert " "
3491 (save-excursion
3492 (forward-line -1)
3493 (buffer-substring (idlwave-goto-comment)
3494 (progn
3495 (skip-chars-forward "; ")
3496 (point))))))
3497 (idlwave-indent-line))
3498 ;; Split code line - add continuation character
3499 (save-excursion
3500 (end-of-line 0)
3501 ;; Check to see if we split a string
3502 (if (and (setq beg (idlwave-in-quote))
3503 idlwave-auto-fill-split-string)
3504 ;; Split the string and concatenate.
3505 ;; The first extra space is for the space
3506 ;; the line was split. That space was removed.
3507 (insert " " (char-after beg) " +"))
3508 (insert " $"))
3509 (if beg
3510 (if idlwave-auto-fill-split-string
3511 ;; Make the second part of continued string
3512 (save-excursion
3513 (beginning-of-line)
3514 (skip-chars-forward " \t")
3515 (insert (char-after beg)))
3516 ;; Warning
3517 (beep)
3518 (message "Warning: continuation inside a string.")))
3519 ;; Although do-auto-fill (via indent-new-comment-line) calls
3520 ;; idlwave-indent-line for the new line, re-indent again
3521 ;; because of the addition of the continuation character.
3522 (idlwave-indent-line))
3523 )))))
3524
3525 (defun idlwave-auto-fill-mode (arg)
3526 "Toggle auto-fill mode for IDL mode.
3527 With arg, turn auto-fill mode on if arg is positive.
3528 In auto-fill mode, inserting a space at a column beyond `fill-column'
3529 automatically breaks the line at a previous space."
3530 (interactive "P")
3531 (prog1 (set idlwave-fill-function
3532 (if (if (null arg)
3533 (not (symbol-value idlwave-fill-function))
3534 (> (prefix-numeric-value arg) 0))
3535 'idlwave-auto-fill
3536 nil))
3537 ;; update mode-line
3538 (set-buffer-modified-p (buffer-modified-p))))
3539
3540 ;(defun idlwave-fill-routine-call ()
3541 ; "Fill a routine definition or statement, indenting appropriately."
3542 ; (let ((where (idlwave-where)))))
3543
3544
3545 (defun idlwave-doc-header (&optional nomark)
3546 "Insert a documentation header at the beginning of the unit.
3547 Inserts the value of the variable `idlwave-file-header'. Sets mark
3548 before moving to do insertion unless the optional prefix argument
3549 NOMARK is non-nil."
3550 (interactive "P")
3551 (or nomark (push-mark))
3552 ;; make sure we catch the current line if it begins the unit
3553 (if idlwave-header-to-beginning-of-file
3554 (goto-char (point-min))
3555 (end-of-line)
3556 (idlwave-beginning-of-subprogram)
3557 (beginning-of-line)
3558 ;; skip function or procedure line
3559 (if (idlwave-look-at "\\<\\(pro\\|function\\)\\>")
3560 (progn
3561 (idlwave-end-of-statement)
3562 (if (> (forward-line 1) 0) (insert "\n")))))
3563 (let ((pos (point)))
3564 (if idlwave-file-header
3565 (cond ((car idlwave-file-header)
3566 (insert-file-contents (car idlwave-file-header)))
3567 ((stringp (car (cdr idlwave-file-header)))
3568 (insert (car (cdr idlwave-file-header))))))
3569 (goto-char pos)))
3570
3571 (defun idlwave-default-insert-timestamp ()
3572 "Default timestamp insertion function."
3573 (insert (current-time-string))
3574 (insert ", " (user-full-name))
3575 (if (boundp 'user-mail-address)
3576 (insert " <" user-mail-address ">")
3577 (insert " <" (user-login-name) "@" (system-name) ">"))
3578 ;; Remove extra spaces from line
3579 (idlwave-fill-paragraph)
3580 ;; Insert a blank line comment to separate from the date entry -
3581 ;; will keep the entry from flowing onto date line if re-filled.
3582 (insert "\n;\n;\t\t"))
3583
3584 (defun idlwave-doc-modification ()
3585 "Insert a brief modification log at the beginning of the current program.
3586 Looks for an occurrence of the value of user variable
3587 `idlwave-doc-modifications-keyword' if non-nil. Inserts time and user
3588 name and places the point for the user to add a log. Before moving, saves
3589 location on mark ring so that the user can return to previous point."
3590 (interactive)
3591 (push-mark)
3592 (let* (beg end)
3593 (if (and (or (re-search-backward idlwave-doclib-start nil t)
3594 (progn
3595 (goto-char (point-min))
3596 (re-search-forward idlwave-doclib-start nil t)))
3597 (setq beg (match-beginning 0))
3598 (re-search-forward idlwave-doclib-end nil t)
3599 (setq end (match-end 0)))
3600 (progn
3601 (goto-char beg)
3602 (if (re-search-forward
3603 (concat idlwave-doc-modifications-keyword ":")
3604 end t)
3605 (end-of-line)
3606 (goto-char end)
3607 (end-of-line -1)
3608 (insert "\n" comment-start "\n")
3609 (insert comment-start " " idlwave-doc-modifications-keyword ":"))
3610 (insert "\n;\n;\t")
3611 (run-hooks 'idlwave-timestamp-hook))
3612 (error "No valid DOCLIB header"))))
3613
3614
3615 ;; CJC 3/16/93
3616 ;; Interface to expand-region-abbrevs which did not work when the
3617 ;; abbrev hook associated with an abbrev moves point backwards
3618 ;; after abbrev expansion, e.g., as with the abbrev '.n'.
3619 ;; The original would enter an infinite loop in attempting to expand
3620 ;; .n (it would continually expand and unexpand the abbrev without expanding
3621 ;; because the point would keep going back to the beginning of the
3622 ;; abbrev instead of to the end of the abbrev). We now keep the
3623 ;; abbrev hook from moving backwards.
3624 ;;;
3625 (defun idlwave-expand-region-abbrevs (start end)
3626 "Expand each abbrev occurrence in the region.
3627 Calling from a program, arguments are START END."
3628 (interactive "r")
3629 (save-excursion
3630 (goto-char (min start end))
3631 (let ((idlwave-show-block nil) ;Do not blink
3632 (idlwave-abbrev-move nil)) ;Do not move
3633 (expand-region-abbrevs start end 'noquery))))
3634
3635 (defun idlwave-quoted ()
3636 "Return t if point is in a comment or quoted string.
3637 Returns nil otherwise."
3638 (or (idlwave-in-comment) (idlwave-in-quote)))
3639
3640 (defun idlwave-in-quote ()
3641 "Return location of the opening quote
3642 if point is in a IDL string constant, nil otherwise.
3643 Ignores comment delimiters on the current line.
3644 Properly handles nested quotation marks and octal
3645 constants - a double quote followed by an octal digit."
3646 ;; Treat an octal inside an apostrophe to be a normal string. Treat a
3647 ;; double quote followed by an octal digit to be an octal constant
3648 ;; rather than a string. Therefore, there is no terminating double
3649 ;; quote.
3650 (save-excursion
3651 ;; Because single and double quotes can quote each other we must
3652 ;; search for the string start from the beginning of line.
3653 (let* ((start (point))
3654 (eol (point-at-eol))
3655 (bq (progn (beginning-of-line) (point)))
3656 (endq (point))
3657 (data (match-data))
3658 delim
3659 found)
3660 (while (< endq start)
3661 ;; Find string start
3662 ;; Don't find an octal constant beginning with a double quote
3663 (if (re-search-forward "[\"']" eol 'lim)
3664 ;; Find the string end.
3665 ;; In IDL, two consecutive delimiters after the start of a
3666 ;; string act as an
3667 ;; escape for the delimiter in the string.
3668 ;; Two consecutive delimiters alone (i.e., not after the
3669 ;; start of a string) is the null string.
3670 (progn
3671 ;; Move to position after quote
3672 (goto-char (1+ (match-beginning 0)))
3673 (setq bq (1- (point)))
3674 ;; Get the string delimiter
3675 (setq delim (char-to-string (preceding-char)))
3676 ;; Check for null string
3677 (if (looking-at delim)
3678 (progn (setq endq (point)) (forward-char 1))
3679 ;; Look for next unpaired delimiter
3680 (setq found (search-forward delim eol 'lim))
3681 (while (looking-at delim)
3682 (forward-char 1)
3683 (setq found (search-forward delim eol 'lim)))
3684 (setq endq (if found (1- (point)) (point)))
3685 ))
3686 (progn (setq bq (point)) (setq endq (point)))))
3687 (store-match-data data)
3688 ;; return string beginning position or nil
3689 (if (> start bq) bq))))
3690
3691 (defun idlwave-is-pointer-dereference (&optional limit)
3692 "Determine if the character after point is a pointer dereference *."
3693 (and
3694 (eq (char-after) ?\*)
3695 (not (idlwave-in-quote))
3696 (save-excursion
3697 (forward-char)
3698 (re-search-backward (concat "\\(" idlwave-idl-keywords
3699 "\\|[[(*+-/=,^><]\\)\\s-*\\*") limit t))))
3700
3701
3702 ;; Statement templates
3703
3704 ;; Replace these with a general template function, something like
3705 ;; expand.el (I think there was also something with a name similar to
3706 ;; dmacro.el)
3707
3708 (defun idlwave-template (s1 s2 &optional prompt noindent)
3709 "Build a template with optional prompt expression.
3710
3711 Opens a line if point is not followed by a newline modulo intervening
3712 whitespace. S1 and S2 are strings. S1 is inserted at point followed
3713 by S2. Point is inserted between S1 and S2. The case of S1 and S2 is
3714 adjusted according to `idlwave-abbrev-change-case'. If optional
3715 argument PROMPT is a string then it is displayed as a message in the
3716 minibuffer. The PROMPT serves as a reminder to the user of an
3717 expression to enter.
3718
3719 The lines containing S1 and S2 are reindented using `indent-region'
3720 unless the optional second argument NOINDENT is non-nil."
3721 (if (derived-mode-p 'idlwave-shell-mode)
3722 ;; This is a gross hack to avoit template abbrev expansion
3723 ;; in the shell. FIXME: This is a dirty hack.
3724 (if (and (eq this-command 'self-insert-command)
3725 (equal last-abbrev-location (point)))
3726 (insert last-abbrev-text)
3727 (error "No templates in idlwave-shell"))
3728 (cond ((eq idlwave-abbrev-change-case 'down)
3729 (setq s1 (downcase s1) s2 (downcase s2)))
3730 (idlwave-abbrev-change-case
3731 (setq s1 (upcase s1) s2 (upcase s2))))
3732 (let ((beg (point-at-bol))
3733 end)
3734 (if (not (looking-at "\\s-*\n"))
3735 (open-line 1))
3736 (insert s1)
3737 (save-excursion
3738 (insert s2)
3739 (setq end (point)))
3740 (if (not noindent)
3741 (indent-region beg end nil))
3742 (if (stringp prompt)
3743 (message "%s" prompt)))))
3744
3745 (defun idlwave-rw-case (string)
3746 "Make STRING have the case required by `idlwave-reserved-word-upcase'."
3747 (if idlwave-reserved-word-upcase
3748 (upcase string)
3749 string))
3750
3751 (defun idlwave-elif ()
3752 "Build skeleton IDL if-else block."
3753 (interactive)
3754 (idlwave-template
3755 (idlwave-rw-case "if")
3756 (idlwave-rw-case " then begin\n\nendif else begin\n\nendelse")
3757 "Condition expression"))
3758
3759 (defun idlwave-case ()
3760 "Build skeleton IDL case statement."
3761 (interactive)
3762 (idlwave-template
3763 (idlwave-rw-case "case")
3764 (idlwave-rw-case " of\n\nendcase")
3765 "Selector expression"))
3766
3767 (defun idlwave-switch ()
3768 "Build skeleton IDL switch statement."
3769 (interactive)
3770 (idlwave-template
3771 (idlwave-rw-case "switch")
3772 (idlwave-rw-case " of\n\nendswitch")
3773 "Selector expression"))
3774
3775 (defun idlwave-for ()
3776 "Build skeleton IDL loop statement."
3777 (interactive)
3778 (idlwave-template
3779 (idlwave-rw-case "for")
3780 (idlwave-rw-case " do begin\n\nendfor")
3781 "Loop expression"))
3782
3783 (defun idlwave-if ()
3784 "Build skeleton IDL if statement."
3785 (interactive)
3786 (idlwave-template
3787 (idlwave-rw-case "if")
3788 (idlwave-rw-case " then begin\n\nendif")
3789 "Scalar logical expression"))
3790
3791 (defun idlwave-procedure ()
3792 (interactive)
3793 (idlwave-template
3794 (idlwave-rw-case "pro")
3795 (idlwave-rw-case "\n\nreturn\nend")
3796 "Procedure name"))
3797
3798 (defun idlwave-function ()
3799 (interactive)
3800 (idlwave-template
3801 (idlwave-rw-case "function")
3802 (idlwave-rw-case "\n\nreturn\nend")
3803 "Function name"))
3804
3805 (defun idlwave-repeat ()
3806 (interactive)
3807 (idlwave-template
3808 (idlwave-rw-case "repeat begin\n\nendrep until")
3809 (idlwave-rw-case "")
3810 "Exit condition"))
3811
3812 (defun idlwave-while ()
3813 (interactive)
3814 (idlwave-template
3815 (idlwave-rw-case "while")
3816 (idlwave-rw-case " do begin\n\nendwhile")
3817 "Entry condition"))
3818
3819 (defun idlwave-split-string (string &optional pattern)
3820 "Return a list of substrings of STRING which are separated by PATTERN.
3821 If PATTERN is omitted, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
3822 (or pattern
3823 (setq pattern "[ \f\t\n\r\v]+"))
3824 (let (parts (start 0))
3825 (while (string-match pattern string start)
3826 (setq parts (cons (substring string start (match-beginning 0)) parts)
3827 start (match-end 0)))
3828 (nreverse (cons (substring string start) parts))))
3829
3830 (defun idlwave-replace-string (string replace_string replace_with)
3831 (let* ((start 0)
3832 (last (length string))
3833 (ret_string "")
3834 end)
3835 (while (setq end (string-match replace_string string start))
3836 (setq ret_string
3837 (concat ret_string (substring string start end) replace_with))
3838 (setq start (match-end 0)))
3839 (setq ret_string (concat ret_string (substring string start last)))))
3840
3841 (defun idlwave-get-buffer-visiting (file)
3842 ;; Return the buffer currently visiting FILE
3843 (cond
3844 ((boundp 'find-file-compare-truenames) ; XEmacs
3845 (let ((find-file-compare-truenames t))
3846 (get-file-buffer file)))
3847 ((fboundp 'find-buffer-visiting) ; Emacs
3848 (find-buffer-visiting file))
3849 (t (error "This should not happen (idlwave-get-buffer-visiting)"))))
3850
3851 (defvar idlwave-outlawed-buffers nil
3852 "List of buffers pulled up by IDLWAVE for special reasons.
3853 Buffers in this list may be killed by `idlwave-kill-autoloaded-buffers'.")
3854
3855 (defun idlwave-find-file-noselect (file &optional why)
3856 ;; Return a buffer visiting file.
3857 (or (idlwave-get-buffer-visiting file)
3858 (let ((buf (find-file-noselect file)))
3859 (if why (add-to-list 'idlwave-outlawed-buffers (cons buf why)))
3860 buf)))
3861
3862 (defun idlwave-kill-autoloaded-buffers ()
3863 "Kill buffers created automatically by IDLWAVE.
3864 Function prompts for a letter to identify the buffers to kill.
3865 Possible letters are:
3866
3867 f Buffers created by the command \\[idlwave-find-module] or mouse
3868 clicks in the routine info window.
3869 s Buffers created by the IDLWAVE Shell to display where execution
3870 stopped or an error was found.
3871 a Both of the above.
3872
3873 Buffers containing unsaved changes require confirmation before they are killed."
3874 (interactive)
3875 (if (null idlwave-outlawed-buffers)
3876 (error "No IDLWAVE-created buffers available")
3877 (princ (format "Kill IDLWAVE-created buffers: [f]ind source(%d), [s]hell display(%d), [a]ll ? "
3878 (idlwave-count-outlawed-buffers 'find)
3879 (idlwave-count-outlawed-buffers 'shell)))
3880 (let ((c (read-char)))
3881 (cond
3882 ((member c '(?f ?\C-f))
3883 (idlwave-do-kill-autoloaded-buffers 'find))
3884 ((member c '(?s ?\C-s))
3885 (idlwave-do-kill-autoloaded-buffers 'shell))
3886 ((member c '(?a ?\C-a))
3887 (idlwave-do-kill-autoloaded-buffers t))
3888 (t (error "Abort"))))))
3889
3890 (defun idlwave-count-outlawed-buffers (tag)
3891 "How many outlawed buffers have tag TAG?"
3892 (length (delq nil
3893 (mapcar
3894 (lambda (x) (eq (cdr x) tag))
3895 idlwave-outlawed-buffers))))
3896
3897 (defun idlwave-do-kill-autoloaded-buffers (&rest reasons)
3898 "Kill all buffers pulled up by IDLWAVE matching REASONS."
3899 (let* ((list (copy-sequence idlwave-outlawed-buffers))
3900 (cnt 0)
3901 entry)
3902 (while (setq entry (pop list))
3903 (if (buffer-live-p (car entry))
3904 (and (or (memq t reasons)
3905 (memq (cdr entry) reasons))
3906 (kill-buffer (car entry))
3907 (incf cnt)
3908 (setq idlwave-outlawed-buffers
3909 (delq entry idlwave-outlawed-buffers)))
3910 (setq idlwave-outlawed-buffers
3911 (delq entry idlwave-outlawed-buffers))))
3912 (message "%d buffer%s killed" cnt (if (= cnt 1) "" "s"))))
3913
3914 (defun idlwave-revoke-license-to-kill ()
3915 "Remove BUFFER from the buffers which may be killed.
3916 Killing would be done by `idlwave-do-kill-autoloaded-buffers'.
3917 Intended for `after-save-hook'."
3918 (let* ((buf (current-buffer))
3919 (entry (assq buf idlwave-outlawed-buffers)))
3920 ;; Revoke license
3921 (if entry
3922 (setq idlwave-outlawed-buffers
3923 (delq entry idlwave-outlawed-buffers)))
3924 ;; Remove this function from the hook.
3925 (remove-hook 'after-save-hook 'idlwave-revoke-license-to-kill 'local)))
3926
3927 (defvar idlwave-path-alist)
3928 (defun idlwave-locate-lib-file (file)
3929 ;; Find FILE on the scanned lib path and return a buffer visiting it
3930 (let* ((dirs idlwave-path-alist)
3931 dir efile)
3932 (catch 'exit
3933 (while (setq dir (car (pop dirs)))
3934 (if (file-regular-p
3935 (setq efile (expand-file-name file dir)))
3936 (throw 'exit efile))))))
3937
3938 (defun idlwave-expand-lib-file-name (file)
3939 ;; Find FILE on the scanned lib path and return a buffer visiting it
3940 ;; This is for, e.g., finding source with no user catalog
3941 (cond
3942 ((null file) nil)
3943 ((file-name-absolute-p file) file)
3944 (t (idlwave-locate-lib-file file))))
3945
3946 (defun idlwave-make-tags ()
3947 "Create the IDL tags file IDLTAGS in the current directory from
3948 the list of directories specified in the minibuffer. Directories may be
3949 for example: . /usr/local/rsi/idl/lib. All the subdirectories of the
3950 specified top directories are searched if the directory name is prefixed
3951 by @. Specify @ directories with care, it may take a long, long time if
3952 you specify /."
3953 (interactive)
3954 (let (directory directories cmd append status numdirs dir getsubdirs
3955 buffer save_buffer files numfiles item errbuf)
3956
3957 ;;
3958 ;; Read list of directories
3959 (setq directory (read-string "Tag Directories: " "."))
3960 (setq directories (idlwave-split-string directory "[ \t]+"))
3961 ;;
3962 ;; Set etags command, vars
3963 (setq cmd "etags --output=IDLTAGS --language=none --regex='/[
3964 \\t]*[pP][Rr][Oo][ \\t]+\\([^ \\t,]+\\)/' --regex='/[
3965 \\t]*[Ff][Uu][Nn][Cc][Tt][Ii][Oo][Nn][ \\t]+\\([^ \\t,]+\\)/' ")
3966 (setq append " ")
3967 (setq status 0)
3968 ;;
3969 ;; For each directory
3970 (setq numdirs 0)
3971 (setq dir (nth numdirs directories))
3972 (while (and dir)
3973 ;;
3974 ;; Find the subdirectories
3975 (if (string-match "^[@]\\(.+\\)$" dir)
3976 (setq getsubdirs t) (setq getsubdirs nil))
3977 (if (and getsubdirs) (setq dir (substring dir 1 (length dir))))
3978 (setq dir (expand-file-name dir))
3979 (if (file-directory-p dir)
3980 (progn
3981 (if (and getsubdirs)
3982 (progn
3983 (setq buffer (get-buffer-create "*idltags*"))
3984 (call-process "sh" nil buffer nil "-c"
3985 (concat "find " dir " -type d -print"))
3986 (setq save_buffer (current-buffer))
3987 (set-buffer buffer)
3988 (setq files (idlwave-split-string
3989 (idlwave-replace-string
3990 (buffer-substring 1 (point-max))
3991 "\n" "/*.pro ")
3992 "[ \t]+"))
3993 (set-buffer save_buffer)
3994 (kill-buffer buffer))
3995 (setq files (list (concat dir "/*.pro"))))
3996 ;;
3997 ;; For each subdirectory
3998 (setq numfiles 0)
3999 (setq item (nth numfiles files))
4000 (while (and item)
4001 ;;
4002 ;; Call etags
4003 (if (not (string-match "^[ \\t]*$" item))
4004 (progn
4005 (message "%s" (concat "Tagging " item "..."))
4006 (setq errbuf (get-buffer-create "*idltags-error*"))
4007 (setq status (+ status
4008 (if (eq 0 (call-process
4009 "sh" nil errbuf nil "-c"
4010 (concat cmd append item)))
4011 0
4012 1)))
4013 ;;
4014 ;; Append additional tags
4015 (setq append " --append ")
4016 (setq numfiles (1+ numfiles))
4017 (setq item (nth numfiles files)))
4018 (progn
4019 (setq numfiles (1+ numfiles))
4020 (setq item (nth numfiles files))
4021 )))
4022
4023 (setq numdirs (1+ numdirs))
4024 (setq dir (nth numdirs directories)))
4025 (progn
4026 (setq numdirs (1+ numdirs))
4027 (setq dir (nth numdirs directories)))))
4028
4029 (setq errbuf (get-buffer-create "*idltags-error*"))
4030 (if (= status 0)
4031 (kill-buffer errbuf))
4032 (message "")
4033 ))
4034
4035 (defun idlwave-toggle-comment-region (beg end &optional n)
4036 "Comment the lines in the region if the first non-blank line is
4037 commented, and conversely, uncomment region. If optional prefix arg
4038 N is non-nil, then for N positive, add N comment delimiters or for N
4039 negative, remove N comment delimiters.
4040 Uses `comment-region' which does not place comment delimiters on
4041 blank lines."
4042 (interactive "r\nP")
4043 (if n
4044 (comment-region beg end (prefix-numeric-value n))
4045 (save-excursion
4046 (goto-char beg)
4047 (beginning-of-line)
4048 ;; skip blank lines
4049 (skip-chars-forward " \t\n")
4050 (if (looking-at (concat "[ \t]*\\(" comment-start "+\\)"))
4051 (if (fboundp 'uncomment-region)
4052 (uncomment-region beg end)
4053 (comment-region beg end
4054 (- (length (buffer-substring
4055 (match-beginning 1)
4056 (match-end 1))))))
4057 (comment-region beg end)))))
4058
4059
4060 ;; ----------------------------------------------------------------------------
4061 ;; ----------------------------------------------------------------------------
4062 ;; ----------------------------------------------------------------------------
4063 ;; ----------------------------------------------------------------------------
4064 ;;
4065 ;; Completion and Routine Info
4066 ;;
4067
4068 ;; String "intern" functions
4069
4070 ;; For the completion and routine info function, we want to normalize
4071 ;; the case of procedure names etc. We do this by "interning" these
4072 ;; string is a hand-crafted way. Hashes are used to map the downcase
4073 ;; version of the strings to the cased versions. Most *-sint-*
4074 ;; variables consist of *two* hashes, a buffer+shell, followed by a
4075 ;; system hash. The former is re-scanned, and the latter takes case
4076 ;; precedence.
4077 ;;
4078 ;; Since these cased versions are really lisp objects, we can use `eq'
4079 ;; to search, which is a large performance boost. All new strings
4080 ;; need to be "sinterned". We do this as early as possible after
4081 ;; getting these strings from completion or buffer substrings. So
4082 ;; most of the code can simply assume to deal with "sinterned"
4083 ;; strings. The only exception is that the functions which scan whole
4084 ;; buffers for routine information do not intern the grabbed strings.
4085 ;; This is only done afterwards. Therefore in these functions it is
4086 ;; *not* safe to assume the strings can be compared with `eq' and be
4087 ;; fed into the routine assq functions.
4088
4089 ;; Here we define the hashing functions.
4090
4091 ;; The variables which hold the hashes.
4092 (defvar idlwave-sint-routines '(nil))
4093 (defvar idlwave-sint-keywords '(nil))
4094 (defvar idlwave-sint-methods '(nil))
4095 (defvar idlwave-sint-classes '(nil))
4096 (defvar idlwave-sint-dirs '(nil))
4097 (defvar idlwave-sint-libnames '(nil))
4098
4099 (defun idlwave-reset-sintern (&optional what)
4100 "Reset all sintern hashes."
4101 ;; Make sure the hash functions are accessible.
4102 (unless (and (fboundp 'gethash)
4103 (fboundp 'puthash))
4104 (require 'cl)
4105 (or (fboundp 'puthash)
4106 (defalias 'puthash 'cl-puthash)))
4107 (let ((entries '((idlwave-sint-routines 1000 10)
4108 (idlwave-sint-keywords 1000 10)
4109 (idlwave-sint-methods 100 10)
4110 (idlwave-sint-classes 10 10))))
4111
4112 ;; Make sure these are lists
4113 (loop for entry in entries
4114 for var = (car entry)
4115 do (if (not (consp (symbol-value var))) (set var (list nil))))
4116
4117 ;; Reset the system & library hash
4118 (when (or (eq what t) (eq what 'syslib)
4119 (null (cdr idlwave-sint-routines)))
4120 (loop for entry in entries
4121 for var = (car entry) for size = (nth 1 entry)
4122 do (setcdr (symbol-value var)
4123 (make-hash-table ':size size ':test 'equal)))
4124 (setq idlwave-sint-dirs nil
4125 idlwave-sint-libnames nil))
4126
4127 ;; Reset the buffer & shell hash
4128 (when (or (eq what t) (eq what 'bufsh)
4129 (null (car idlwave-sint-routines)))
4130 (loop for entry in entries
4131 for var = (car entry) for size = (nth 1 entry)
4132 do (setcar (symbol-value var)
4133 (make-hash-table ':size size ':test 'equal))))))
4134
4135 (defun idlwave-sintern-routine-or-method (name &optional class set)
4136 (if class
4137 (idlwave-sintern-method name set)
4138 (idlwave-sintern-routine name set)))
4139
4140 (defun idlwave-sintern (stype &rest args)
4141 (apply (intern (concat "idlwave-sintern-" (symbol-name stype))) args))
4142
4143 ;;(defmacro idlwave-sintern (type var)
4144 ;; `(cond ((not (stringp name)) name)
4145 ;; ((gethash (downcase name) (cdr ,var)))
4146 ;; ((gethash (downcase name) (car ,var)))
4147 ;; (set (idlwave-sintern-set name ,type ,var set))
4148 ;; (name)))
4149
4150 (defun idlwave-sintern-routine (name &optional set)
4151 (cond ((not (stringp name)) name)
4152 ((gethash (downcase name) (cdr idlwave-sint-routines)))
4153 ((gethash (downcase name) (car idlwave-sint-routines)))
4154 (set (idlwave-sintern-set name 'routine idlwave-sint-routines set))
4155 (name)))
4156 (defun idlwave-sintern-keyword (name &optional set)
4157 (cond ((not (stringp name)) name)
4158 ((gethash (downcase name) (cdr idlwave-sint-keywords)))
4159 ((gethash (downcase name) (car idlwave-sint-keywords)))
4160 (set (idlwave-sintern-set name 'keyword idlwave-sint-keywords set))
4161 (name)))
4162 (defun idlwave-sintern-method (name &optional set)
4163 (cond ((not (stringp name)) name)
4164 ((gethash (downcase name) (cdr idlwave-sint-methods)))
4165 ((gethash (downcase name) (car idlwave-sint-methods)))
4166 (set (idlwave-sintern-set name 'method idlwave-sint-methods set))
4167 (name)))
4168 (defun idlwave-sintern-class (name &optional set)
4169 (cond ((not (stringp name)) name)
4170 ((gethash (downcase name) (cdr idlwave-sint-classes)))
4171 ((gethash (downcase name) (car idlwave-sint-classes)))
4172 (set (idlwave-sintern-set name 'class idlwave-sint-classes set))
4173 (name)))
4174
4175 (defun idlwave-sintern-dir (dir &optional set)
4176 (car (or (member dir idlwave-sint-dirs)
4177 (setq idlwave-sint-dirs (cons dir idlwave-sint-dirs)))))
4178 (defun idlwave-sintern-libname (name &optional set)
4179 (car (or (member name idlwave-sint-libnames)
4180 (setq idlwave-sint-libnames (cons name idlwave-sint-libnames)))))
4181
4182 (defun idlwave-sintern-set (name type tables set)
4183 (let* ((func (or (cdr (assq type idlwave-completion-case))
4184 'identity))
4185 (iname (funcall (if (eq func 'preserve) 'identity func) name))
4186 (table (if (eq set 'sys) (cdr tables) (car tables))))
4187 (puthash (downcase name) iname table)
4188 iname))
4189
4190 (defun idlwave-sintern-keyword-list (kwd-list &optional set)
4191 "Sintern a set of keywords (file (key . link) (key2 . link2) ...)"
4192 (mapc (lambda(x)
4193 (setcar x (idlwave-sintern-keyword (car x) set)))
4194 (cdr kwd-list))
4195 kwd-list)
4196
4197 (defun idlwave-sintern-rinfo-list (list &optional set default-dir)
4198 "Sintern all strings in the rinfo LIST.
4199 With optional parameter SET: also set new patterns. Probably this
4200 will always have to be t. If DEFAULT-DIR is passed, it is used as
4201 the base of the directory."
4202 (let (entry name type class kwds res source call new)
4203 (while list
4204 (setq entry (car list)
4205 list (cdr list)
4206 name (car entry)
4207 type (nth 1 entry)
4208 class (nth 2 entry)
4209 source (nth 3 entry)
4210 call (nth 4 entry)
4211 kwds (nthcdr 5 entry))
4212
4213 ;; The class and name
4214 (if class
4215 (progn
4216 (if (symbolp class) (setq class (symbol-name class)))
4217 (setq class (idlwave-sintern-class class set))
4218 (setq name (idlwave-sintern-method name set)))
4219 (setq name (idlwave-sintern-routine name set)))
4220
4221 ;; The source
4222 (let ((source-type (car source))
4223 (source-file (nth 1 source))
4224 (source-dir (if default-dir
4225 (file-name-as-directory default-dir)
4226 (nth 2 source)))
4227 (source-lib (nth 3 source)))
4228 (if (stringp source-dir)
4229 (setq source-dir (idlwave-sintern-dir source-dir set)))
4230 (if (stringp source-lib)
4231 (setq source-lib (idlwave-sintern-libname source-lib set)))
4232 (setq source (list source-type source-file source-dir source-lib)))
4233
4234 ;; The keywords
4235 (setq kwds (mapcar (lambda (x)
4236 (idlwave-sintern-keyword-list x set))
4237 kwds))
4238
4239 ;; Build a canonicalized list
4240 (setq new (nconc (list name type class source call) kwds)
4241 res (cons new res)))
4242 (nreverse res)))
4243
4244 ;; Creating new sintern tables
4245
4246 (defun idlwave-new-sintern-type (tag)
4247 "Define a variable and a function to sintern the new type TAG.
4248 This defines the function `idlwave-sintern-TAG' and the variable
4249 `idlwave-sint-TAGs'."
4250 (let* ((name (symbol-name tag))
4251 (names (concat name "s"))
4252 (var (intern (concat "idlwave-sint-" names)))
4253 (func (intern (concat "idlwave-sintern-" name))))
4254 (set var nil) ; initial value of the association list
4255 (fset func ; set the function
4256 `(lambda (name &optional set)
4257 (cond ((not (stringp name)) name)
4258 ((cdr (assoc (downcase name) ,var)))
4259 (set
4260 (setq ,var (cons (cons (downcase name) name) ,var))
4261 name)
4262 (name))))))
4263
4264 (defun idlwave-reset-sintern-type (tag)
4265 "Reset the sintern variable associated with TAG."
4266 (set (intern (concat "idlwave-sint-" (symbol-name tag) "s")) nil))
4267
4268 ;;---------------------------------------------------------------------------
4269
4270
4271 ;; The variables which hold the information
4272 (defvar idlwave-system-routines nil
4273 "Holds the routine-info obtained by scanning buffers.")
4274 (defvar idlwave-buffer-routines nil
4275 "Holds the routine-info obtained by scanning buffers.")
4276 (defvar idlwave-compiled-routines nil
4277 "Holds the routine-info obtained by asking the shell.")
4278 (defvar idlwave-unresolved-routines nil
4279 "Holds the unresolved routine-info obtained by asking the shell.")
4280 (defvar idlwave-user-catalog-routines nil
4281 "Holds the procedure routine-info from the user scan.")
4282 (defvar idlwave-library-catalog-routines nil
4283 "Holds the procedure routine-info from the .idlwave_catalog library files.")
4284 (defvar idlwave-library-catalog-libname nil
4285 "Name of library catalog loaded from .idlwave_catalog files.")
4286 (defvar idlwave-path-alist nil
4287 "Alist with !PATH directories and zero or more flags if the dir has
4288 been scanned in a user catalog ('user) or discovered in a library
4289 catalog \('lib).")
4290 (defvar idlwave-true-path-alist nil
4291 "Like `idlwave-path-alist', but with true filenames.")
4292 (defvar idlwave-routines nil
4293 "Holds the combined procedure/function/method routine-info.")
4294 (defvar idlwave-class-alist nil
4295 "Holds the class names known to IDLWAVE.")
4296 (defvar idlwave-class-history nil
4297 "The history of classes selected with the minibuffer.")
4298 (defvar idlwave-force-class-query nil)
4299 (defvar idlwave-before-completion-wconf nil
4300 "The window configuration just before the completion buffer was displayed.")
4301 (defvar idlwave-last-system-routine-info-cons-cell nil
4302 "The last cons cell in the system routine info.")
4303
4304 ;;
4305 ;; The code to get routine info from different sources.
4306
4307 (defvar idlwave-system-routines)
4308 (defvar idlwave-catalog-process nil
4309 "The background process currently updating the catalog.")
4310
4311 (defun idlwave-routines ()
4312 "Provide a list of IDL routines.
4313 This routine loads the builtin routines on the first call.
4314 Later it only returns the value of the variable."
4315 (if (and idlwave-catalog-process
4316 (processp idlwave-catalog-process))
4317 (progn
4318 (cond
4319 ((equal (process-status idlwave-catalog-process) 'exit)
4320 (message "updating........")
4321 (setq idlwave-catalog-process nil)
4322 (idlwave-update-routine-info '(4)))
4323 ((equal (process-status idlwave-catalog-process) 'run)
4324 ;; Keep it running...
4325 )
4326 (t
4327 ;; Something is wrong, get rid of the process
4328 (message "Problem with catalog process") (beep)
4329 (condition-case nil
4330 (kill-process idlwave-catalog-process)
4331 (error nil))
4332 (setq idlwave-catalog-process nil)))))
4333 (or idlwave-routines
4334 (progn
4335 (idlwave-update-routine-info)
4336 ;; return the current value
4337 idlwave-routines)))
4338
4339 (defvar idlwave-update-rinfo-hook nil
4340 "List of functions which should run after a global rinfo update.
4341 Does not run after automatic updates of buffer or the shell.")
4342
4343 (defun idlwave-rescan-catalog-directories ()
4344 "Rescan the previously selected directories. For batch processing."
4345 (idlwave-update-routine-info '(16)))
4346
4347 (defun idlwave-rescan-asynchronously ()
4348 "Dispatch another Emacs instance to update the idlwave catalog.
4349 After the process finishes normally, the first access to routine info
4350 will re-read the catalog."
4351 (interactive)
4352 (if (processp idlwave-catalog-process)
4353 (if (eq (process-status idlwave-catalog-process) 'run)
4354 (if (yes-or-no-p "A catalog-updating process is running. Kill it? ")
4355 (progn
4356 (condition-case nil
4357 (kill-process idlwave-catalog-process)
4358 (error nil))
4359 (error "Process killed, no new process started"))
4360 (error "Quit"))
4361 (condition-case nil
4362 (kill-process idlwave-catalog-process)
4363 (error nil))))
4364 (if (or (not idlwave-user-catalog-file)
4365 (not (stringp idlwave-user-catalog-file))
4366 (not (file-regular-p idlwave-user-catalog-file)))
4367 (error "No catalog has been produced yet"))
4368 (let* ((emacs (concat invocation-directory invocation-name))
4369 (args (list "-batch"
4370 "-l" (expand-file-name "~/.emacs")
4371 "-l" "idlwave"
4372 "-f" "idlwave-rescan-catalog-directories"))
4373 (process (apply 'start-process "idlcat"
4374 nil emacs args)))
4375 (setq idlwave-catalog-process process)
4376 (set-process-sentinel
4377 process
4378 (lambda (pro why)
4379 (when (string-match "finished" why)
4380 (setq idlwave-routines nil
4381 idlwave-system-routines nil
4382 idlwave-catalog-process nil)
4383 (or (idlwave-start-load-rinfo-timer)
4384 (idlwave-update-routine-info '(4))))))
4385 (message "Background job started to update catalog file")))
4386
4387
4388 ;; Format for all routine info user catalog, library catalogs, etc.:
4389 ;;
4390 ;; ("ROUTINE" type class
4391 ;; (system) | (lib pro_file dir "LIBNAME") | (user pro_file dir "USERLIB") |
4392 ;; (buffer pro_file dir) | (compiled pro_file dir)
4393 ;; "calling_string" ("HELPFILE" (("KWD1" . link1) ...))
4394 ;; ("HELPFILE2" (("KWD2" . link) ...)) ...)
4395 ;;
4396 ;; DIR will be supplied dynamically while loading library catalogs,
4397 ;; and is sinterned to save space, as is LIBNAME. PRO_FILE can be a
4398 ;; complete filepath, in which case DIR is unnecessary. HELPFILE can
4399 ;; be nil, as can LINK1, etc., if no HTML help is available.
4400
4401
4402 (defvar idlwave-load-rinfo-idle-timer)
4403 (defvar idlwave-shell-path-query)
4404
4405 (defun idlwave-update-routine-info (&optional arg no-concatenate)
4406 "Update the internal routine-info lists.
4407 These lists are used by `idlwave-routine-info' (\\[idlwave-routine-info])
4408 and by `idlwave-complete' (\\[idlwave-complete]) to provide information
4409 about individual routines.
4410
4411 The information can come from 4 sources:
4412 1. IDL programs in the current editing session
4413 2. Compiled modules in an IDL shell running as Emacs subprocess
4414 3. A list which covers the IDL system routines.
4415 4. A list which covers the prescanned library files.
4416
4417 Scans all IDLWAVE-mode buffers of the current editing session (see
4418 `idlwave-scan-all-buffers-for-routine-info').
4419 When an IDL shell is running, this command also queries the IDL program
4420 for currently compiled routines.
4421
4422 With prefix ARG, also reload the system and library lists.
4423 With two prefix ARG's, also rescans the chosen user catalog tree.
4424 With three prefix args, dispatch asynchronous process to do the update.
4425
4426 If NO-CONCATENATE is non-nil, don't pre-concatenate the routine info
4427 lists, but instead wait for the shell query to complete and
4428 asynchronously finish updating routine info. This is set
4429 automatically when called interactively. When you need routine
4430 information updated immediately, leave NO-CONCATENATE nil."
4431 (interactive "P\np")
4432 ;; Stop any idle processing
4433 (if (or (and (fboundp 'itimerp)
4434 (itimerp idlwave-load-rinfo-idle-timer))
4435 (and (fboundp 'timerp)
4436 (timerp idlwave-load-rinfo-idle-timer)))
4437 (cancel-timer idlwave-load-rinfo-idle-timer))
4438 (cond
4439 ((equal arg '(64))
4440 ;; Start a background process which updates the catalog.
4441 (idlwave-rescan-asynchronously))
4442 ((equal arg '(16))
4443 ;; Update the user catalog now, and wait for them.
4444 (idlwave-create-user-catalog-file t))
4445 (t
4446 (let* ((load (or arg
4447 idlwave-buffer-case-takes-precedence
4448 (null idlwave-routines)))
4449 ;; The override-idle means, even if the idle timer has done some
4450 ;; preparing work, load and renormalize everything anyway.
4451 (override-idle (or arg idlwave-buffer-case-takes-precedence)))
4452
4453 (setq idlwave-buffer-routines nil
4454 idlwave-compiled-routines nil
4455 idlwave-unresolved-routines nil)
4456 ;; Reset the appropriate hashes
4457 (if (get 'idlwave-reset-sintern 'done-by-idle)
4458 ;; reset was already done in idle time, so skip this step now once
4459 (put 'idlwave-reset-sintern 'done-by-idle nil)
4460 (idlwave-reset-sintern (cond (load t)
4461 ((null idlwave-system-routines) t)
4462 (t 'bufsh))))
4463
4464 (if idlwave-buffer-case-takes-precedence
4465 ;; We can safely scan the buffer stuff first
4466 (progn
4467 (idlwave-update-buffer-routine-info)
4468 (and load (idlwave-load-all-rinfo override-idle)))
4469 ;; We first do the system info, and then the buffers
4470 (and load (idlwave-load-all-rinfo override-idle))
4471 (idlwave-update-buffer-routine-info))
4472
4473 ;; Let's see if there is a shell
4474 (let* ((shell-is-running (and (fboundp 'idlwave-shell-is-running)
4475 (idlwave-shell-is-running)))
4476 (ask-shell (and shell-is-running
4477 idlwave-query-shell-for-routine-info)))
4478
4479 ;; Load the library catalogs again, first re-scanning the path
4480 (when arg
4481 (if shell-is-running
4482 (idlwave-shell-send-command idlwave-shell-path-query
4483 '(progn
4484 (idlwave-shell-get-path-info)
4485 (idlwave-scan-library-catalogs))
4486 'hide)
4487 (idlwave-scan-library-catalogs)))
4488
4489 (if (or (not ask-shell)
4490 (not no-concatenate))
4491 ;; 1. If we are not going to ask the shell, we need to do the
4492 ;; concatenation now.
4493 ;; 2. When this function is called non-interactively, it
4494 ;; means that someone needs routine info *now*. The
4495 ;; shell update causes the concatenation to be
4496 ;; *delayed*, so not in time for the current command.
4497 ;; Therefore, we do a concatenation now, even though
4498 ;; the shell might do it again.
4499 (idlwave-concatenate-rinfo-lists nil 'run-hooks))
4500
4501 (when ask-shell
4502 ;; Ask the shell about the routines it knows of.
4503 (message "Querying the shell")
4504 (idlwave-shell-update-routine-info nil t)))))))
4505
4506
4507 (defvar idlwave-load-rinfo-steps-done (make-vector 6 nil))
4508 (defvar idlwave-load-rinfo-idle-timer nil)
4509 (defun idlwave-start-load-rinfo-timer ()
4510 (if (or (and (fboundp 'itimerp)
4511 (itimerp idlwave-load-rinfo-idle-timer))
4512 (and (fboundp 'timerp)
4513 (timerp idlwave-load-rinfo-idle-timer)))
4514 (cancel-timer idlwave-load-rinfo-idle-timer))
4515 (setq idlwave-load-rinfo-steps-done (make-vector 6 nil))
4516 (setq idlwave-load-rinfo-idle-timer nil)
4517 (if (and idlwave-init-rinfo-when-idle-after
4518 (numberp idlwave-init-rinfo-when-idle-after)
4519 (not (equal 0 idlwave-init-rinfo-when-idle-after))
4520 (not idlwave-routines))
4521 (condition-case nil
4522 (progn
4523 (setq idlwave-load-rinfo-idle-timer
4524 (run-with-idle-timer
4525 idlwave-init-rinfo-when-idle-after
4526 nil 'idlwave-load-rinfo-next-step)))
4527 (error nil))))
4528
4529 (defvar idlwave-library-routines nil "Obsolete variable.")
4530
4531 ;;------ XML Help routine info system
4532 (defun idlwave-load-system-routine-info ()
4533 ;; Load the system routine info from the cached routine info file,
4534 ;; which, if necessary, will be re-created from the XML file on
4535 ;; disk. As a last fallback, load the (likely outdated) idlw-rinfo
4536 ;; file distributed with older IDLWAVE versions (<6.0)
4537 (unless (and (load idlwave-xml-system-rinfo-converted-file
4538 'noerror 'nomessage)
4539 (idlwave-xml-system-routine-info-up-to-date))
4540 ;; See if we can create it from XML source
4541 (condition-case nil
4542 (idlwave-convert-xml-system-routine-info)
4543 (error
4544 (unless (load idlwave-xml-system-rinfo-converted-file
4545 'noerror 'nomessage)
4546 (if idlwave-system-routines
4547 (message
4548 "Failed to load converted routine info, using old conversion.")
4549 (message
4550 "Failed to convert XML routine info, falling back on idlw-rinfo.")
4551 (if (not (load "idlw-rinfo" 'noerror 'nomessage))
4552 (message
4553 "Could not locate any system routine information."))))))))
4554
4555 (defun idlwave-xml-system-routine-info-up-to-date()
4556 (let* ((dir (file-name-as-directory
4557 (expand-file-name "help/online_help" (idlwave-sys-dir))))
4558 (catalog-file (expand-file-name "idl_catalog.xml" dir)))
4559 (file-newer-than-file-p ;converted file is newer than catalog
4560 idlwave-xml-system-rinfo-converted-file
4561 catalog-file)))
4562
4563 (defvar idlwave-system-class-info nil) ; Gathered from idlw-rinfo
4564 (defvar idlwave-system-variables-alist nil
4565 "Alist of system variables and the associated structure tags.
4566 Gets set in cached XML rinfo, or `idlw-rinfo.el'.")
4567 (defvar idlwave-executive-commands-alist nil
4568 "Alist of system variables and their help files.")
4569 (defvar idlwave-help-special-topic-words nil)
4570
4571
4572 (defun idlwave-shorten-syntax (syntax name &optional class)
4573 ;; From a list of syntax statements, shorten with %s and group with "or"
4574 (let ((case-fold-search t))
4575 (mapconcat
4576 (lambda (x)
4577 (while (string-match name x)
4578 (setq x (replace-match "%s" t t x)))
4579 (if class
4580 (while (string-match class x)
4581 (setq x (replace-match "%s" t t x))))
4582 x)
4583 (nreverse syntax)
4584 " or ")))
4585
4586 (defun idlwave-xml-create-class-method-lists (xml-entry)
4587 ;; Create a class list entry from the xml parsed list., returning a
4588 ;; cons of form (class-entry method-entries).
4589 (let* ((nameblock (nth 1 xml-entry))
4590 (class (cdr (assq 'name nameblock)))
4591 (link (cdr (assq 'link nameblock)))
4592 (params (cddr xml-entry))
4593 (case-fold-search t)
4594 class-entry
4595 method methods-entry extra-kwds
4596 props get-props set-props init-props inherits
4597 pelem ptype)
4598 (while params
4599 (setq pelem (car params))
4600 (when (listp pelem)
4601 (setq ptype (car pelem)
4602 props (car (cdr pelem)))
4603 (cond
4604 ((eq ptype 'SUPERCLASS)
4605 (let ((pname (cdr (assq 'name props)))
4606 (plink (cdr (assq 'link props))))
4607 (unless (and (string= pname "None")
4608 (string= plink "None"))
4609 (push pname inherits))))
4610
4611 ((eq ptype 'PROPERTY)
4612 (let ((pname (cdr (assq 'name props)))
4613 (plink (cdr (assq 'link props)))
4614 (get (string= (cdr (assq 'get props)) "Yes"))
4615 (set (string= (cdr (assq 'set props)) "Yes"))
4616 (init (string= (cdr (assq 'init props)) "Yes")))
4617 (if get (push (list pname plink) get-props))
4618 (if set (push (list pname plink) set-props))
4619 (if init (push (list pname plink) init-props))))
4620
4621 ((eq ptype 'METHOD)
4622 (setq method (cdr (assq 'name props)))
4623 (setq extra-kwds ;;Assume all property keywords are gathered already
4624 (cond
4625 ((string-match (concat class "::Init") method)
4626 (put 'init-props 'matched t)
4627 init-props)
4628 ((string-match (concat class "::GetProperty") method)
4629 (put 'get-props 'matched t)
4630 get-props)
4631 ((string-match (concat class "::SetProperty") method)
4632 (put 'set-props 'matched t)
4633 set-props)
4634 (t nil)))
4635 (setq methods-entry
4636 (nconc (idlwave-xml-create-rinfo-list pelem class extra-kwds)
4637 methods-entry)))
4638 (t)))
4639 (setq params (cdr params)))
4640 ;;(unless (get 'init-props 'matched)
4641 ;; (message "Failed to match Init in class %s" class))
4642 ;;(unless (get 'get-props 'matched)
4643 ;; (message "Failed to match GetProperty in class %s" class))
4644 ;;(unless (get 'set-props 'matched)
4645 ;; (message "Failed to match SetProperty in class %s" class))
4646 (setq class-entry
4647 (if inherits
4648 (list class (append '(inherits) inherits) (list 'link link))
4649 (list class (list 'link link))))
4650 (cons class-entry methods-entry)))
4651
4652 (defun idlwave-xml-create-rinfo-list (xml-entry &optional class extra-kws)
4653 ;; Create correctly structured list elements from ROUTINE or METHOD
4654 ;; XML list structures. Return a list of list elements, with more
4655 ;; than one sub-list possible if a routine can serve as both
4656 ;; procedure and function (e.g. call_method).
4657 (let* ((nameblock (nth 1 xml-entry))
4658 (name (cdr (assq 'name nameblock)))
4659 (link (cdr (assq 'link nameblock)))
4660 (params (cddr xml-entry))
4661 (syntax-vec (make-vector 3 nil)) ; procedure, function, exec command
4662 (case-fold-search t)
4663 syntax kwd klink pref-list kwds pelem ptype props result type)
4664 (if class ;; strip out class name from class method name string
4665 (if (string-match (concat class "::") name)
4666 (setq name (substring name (match-end 0)))))
4667 (while params
4668 (setq pelem (car params))
4669 (when (listp pelem)
4670 (setq ptype (car pelem)
4671 props (car (cdr pelem)))
4672 (cond
4673 ((eq ptype 'SYNTAX)
4674 (setq syntax (cdr (assq 'name props)))
4675 (if (string-match "-&gt;" syntax)
4676 (setq syntax (replace-match "->" t nil syntax)))
4677 (setq type (cdr (assq 'type props)))
4678 (push syntax
4679 (aref syntax-vec (cond
4680 ((string-match "^pro" type) 0)
4681 ((string-match "^fun" type) 1)
4682 ((string-match "^exec" type) 2)))))
4683 ((eq ptype 'KEYWORD)
4684 (setq kwd (cdr (assq 'name props))
4685 klink (cdr (assq 'link props)))
4686 (if (string-match "^\\[XY\\(Z?\\)\\]" kwd)
4687 (progn
4688 (setq pref-list
4689 (if (match-string 1 kwd) '("X" "Y" "Z") '("X" "Y"))
4690 kwd (substring kwd (match-end 0)))
4691 (loop for x in pref-list do
4692 (push (list (concat x kwd) klink) kwds)))
4693 (push (list kwd klink) kwds)))
4694
4695 (t))); Do nothing for the others
4696 (setq params (cdr params)))
4697
4698 ;; Debug
4699 ;; (if (and (null (aref syntax-vec 0))
4700 ;; (null (aref syntax-vec 1))
4701 ;; (null (aref syntax-vec 2)))
4702 ;; (with-current-buffer (get-buffer-create "IDL_XML_catalog_complaints")
4703 ;; (if class
4704 ;; (insert (format "Missing SYNTAX entry for %s::%s\n" class name))
4705 ;; (insert (message "Missing SYNTAX entry for %s\n" name)))))
4706
4707 ;; Executive commands are treated specially
4708 (if (aref syntax-vec 2)
4709 (cons (substring name 1) link)
4710 (if extra-kws (setq kwds (nconc kwds extra-kws)))
4711 (setq kwds (idlwave-rinfo-group-keywords kwds link))
4712 (loop for idx from 0 to 1 do
4713 (if (aref syntax-vec idx)
4714 (push (append (list name (if (eq idx 0) 'pro 'fun)
4715 class '(system)
4716 (idlwave-shorten-syntax
4717 (aref syntax-vec idx) name class))
4718 kwds) result)))
4719 result)))
4720
4721
4722 (defun idlwave-rinfo-group-keywords (kwds master-link)
4723 ;; Group keywords by link file, as a list with elements
4724 ;; (linkfile ( ("KWD1" . link1) ("KWD2" . link2))
4725 (let (kwd link anchor linkfiles block master-elt)
4726 (while kwds
4727 (setq kwd (car kwds)
4728 link (idlwave-split-link-target (nth 1 kwd))
4729 anchor (cdr link)
4730 link (car link)
4731 kwd (car kwd))
4732 (if (setq block (assoc link linkfiles))
4733 (push (cons kwd anchor) (cdr block))
4734 (push (list link (cons kwd anchor)) linkfiles))
4735 (setq kwds (cdr kwds)))
4736 ;; Ensure the master link is there
4737 (if (setq master-elt (assoc master-link linkfiles))
4738 (if (eq (car linkfiles) master-elt)
4739 linkfiles
4740 (cons master-elt (delq master-elt linkfiles)))
4741 (push (list master-link) linkfiles))))
4742
4743 (defun idlwave-convert-xml-clean-statement-aliases (aliases)
4744 ;; Clean up the syntax of routines which are actually aliases by
4745 ;; removing the "OR" from the statements
4746 (let (syntax entry)
4747 (loop for x in aliases do
4748 (setq entry (assoc x idlwave-system-routines))
4749 (when entry
4750 (while (string-match " +or +" (setq syntax (nth 4 entry)))
4751 (setf (nth 4 entry) (replace-match ", " t t syntax)))))))
4752
4753 (defun idlwave-convert-xml-clean-routine-aliases (aliases)
4754 ;; Duplicate and trim original routine aliases from rinfo list
4755 ;; This if for, e.g. OPENR/OPENW/OPENU
4756 (let (alias remove-list new parts all-parts)
4757 (loop for x in aliases do
4758 (when (setq parts (split-string (cdr x) "/"))
4759 (setq new (assoc (cdr x) all-parts))
4760 (unless new
4761 (setq new (cons (cdr x) parts))
4762 (push new all-parts))
4763 (setcdr new (delete (car x) (cdr new)))))
4764
4765 ;; Add any missing aliases (separate by slashes)
4766 (loop for x in all-parts do
4767 (if (cdr x)
4768 (push (cons (nth 1 x) (car x)) aliases)))
4769
4770 (loop for x in aliases do
4771 (when (setq alias (assoc (cdr x) idlwave-system-routines))
4772 (unless (memq alias remove-list) (push alias remove-list))
4773 (setq alias (copy-sequence alias))
4774 (setcar alias (car x))
4775 (push alias idlwave-system-routines)))
4776 (loop for x in remove-list do
4777 (delq x idlwave-system-routines))))
4778
4779 (defun idlwave-convert-xml-clean-sysvar-aliases (aliases)
4780 ;; Duplicate and trim original routine aliases from rinfo list
4781 ;; This if for, e.g. !X, !Y, !Z.
4782 (let (alias remove-list)
4783 (loop for x in aliases do
4784 (when (setq alias (assoc (cdr x) idlwave-system-variables-alist))
4785 (unless (memq alias remove-list) (push alias remove-list))
4786 (setq alias (copy-sequence alias))
4787 (setcar alias (car x))
4788 (push alias idlwave-system-variables-alist)))
4789 (loop for x in remove-list do
4790 (delq x idlwave-system-variables-alist))))
4791
4792
4793 (defun idlwave-xml-create-sysvar-alist (xml-entry)
4794 ;; Create a sysvar list entry from the xml parsed list.
4795 (let* ((nameblock (nth 1 xml-entry))
4796 (name (cdr (assq 'name nameblock)))
4797 (sysvar (substring name (progn (string-match "^ *!" name)
4798 (match-end 0))))
4799 (link (cdr (assq 'link nameblock)))
4800 (params (cddr xml-entry))
4801 (case-fold-search t)
4802 pelem ptype props tags)
4803 (while params
4804 (setq pelem (car params))
4805 (when (listp pelem)
4806 (setq ptype (car pelem)
4807 props (car (cdr pelem)))
4808 (cond
4809 ((eq ptype 'FIELD)
4810 (push (cons (cdr (assq 'name props))
4811 (cdr
4812 (idlwave-split-link-target (cdr (assq 'link props)))))
4813 tags))))
4814 (setq params (cdr params)))
4815 (delq nil
4816 (list sysvar (if tags (cons 'tags tags)) (list 'link link)))))
4817
4818
4819 (defvar idlwave-xml-routine-info-file nil)
4820
4821 (defun idlwave-save-routine-info ()
4822 (if idlwave-xml-routine-info-file
4823 (with-temp-file idlwave-xml-system-rinfo-converted-file
4824 (insert
4825 (concat ";; *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
4826 ;; IDLWAVE Routine Information File (IDLWAVE version " idlwave-mode-version ")
4827 ;; Automatically generated from source file:
4828 ;; " idlwave-xml-routine-info-file "
4829 ;; on " (current-time-string) "
4830 ;; Do not edit."))
4831 (insert (format "\n(setq idlwave-xml-routine-info-file \n \"%s\")"
4832 idlwave-xml-routine-info-file))
4833 (insert "\n(setq idlwave-system-routines\n '")
4834 (prin1 idlwave-system-routines (current-buffer))
4835 (insert ")")
4836 (insert "\n(setq idlwave-system-variables-alist\n '")
4837 (prin1 idlwave-system-variables-alist (current-buffer))
4838 (insert ")")
4839 (insert "\n(setq idlwave-system-class-info\n '")
4840 (prin1 idlwave-system-class-info (current-buffer))
4841 (insert ")")
4842 (insert "\n(setq idlwave-executive-commands-alist\n '")
4843 (prin1 idlwave-executive-commands-alist (current-buffer))
4844 (insert ")")
4845 (insert "\n(setq idlwave-help-special-topic-words\n '")
4846 (prin1 idlwave-help-special-topic-words (current-buffer))
4847 (insert ")"))))
4848
4849 (defun idlwave-convert-xml-system-routine-info ()
4850 "Convert XML supplied IDL routine info into internal form.
4851 Cache to disk for quick recovery."
4852 (interactive)
4853 (let* ((dir (file-name-as-directory
4854 (expand-file-name "help/online_help" (idlwave-sys-dir))))
4855 (catalog-file (expand-file-name "idl_catalog.xml" dir))
4856 (elem-cnt 0)
4857 props rinfo msg-cnt elem type nelem class-result alias
4858 routines routine-aliases statement-aliases sysvar-aliases)
4859 (if (not (file-exists-p catalog-file))
4860 (error "No such XML routine info file: %s" catalog-file)
4861 (if (not (file-readable-p catalog-file))
4862 (error "Cannot read XML routine info file: %s" catalog-file)))
4863 (message "Reading XML routine info...")
4864 (setq rinfo (xml-parse-file catalog-file))
4865 (message "Reading XML routine info...done")
4866 (setq rinfo (assq 'CATALOG rinfo))
4867 (unless rinfo (error "Failed to parse XML routine info"))
4868 ;;(setq rinfo (car rinfo)) ; Skip the catalog stuff.
4869
4870 (setq rinfo (cddr rinfo))
4871
4872 (setq nelem (length rinfo)
4873 msg-cnt (/ nelem 20))
4874
4875 (setq idlwave-xml-routine-info-file nil)
4876 (message "Converting XML routine info...")
4877 (setq idlwave-system-routines nil
4878 idlwave-system-variables-alist nil
4879 idlwave-system-class-info nil
4880 idlwave-executive-commands-alist nil
4881 idlwave-help-special-topic-words nil)
4882
4883 (while rinfo
4884 (setq elem (car rinfo)
4885 rinfo (cdr rinfo))
4886 (incf elem-cnt)
4887 (when (listp elem)
4888 (setq type (car elem)
4889 props (car (cdr elem)))
4890 (if (= (mod elem-cnt msg-cnt) 0)
4891 (message "Converting XML routine info...%2d%%"
4892 (/ (* elem-cnt 100) nelem)))
4893 (cond
4894 ((eq type 'ROUTINE)
4895 (if (setq alias (assq 'alias_to props))
4896 (push (cons (cdr (assq 'name props)) (cdr alias))
4897 routine-aliases)
4898 (setq routines (idlwave-xml-create-rinfo-list elem))
4899 (if (listp (cdr routines))
4900 (setq idlwave-system-routines
4901 (nconc idlwave-system-routines routines))
4902 ;; a cons cell is an executive commands
4903 (push routines idlwave-executive-commands-alist))))
4904
4905 ((eq type 'CLASS)
4906 (setq class-result (idlwave-xml-create-class-method-lists elem))
4907 (push (car class-result) idlwave-system-class-info)
4908 (setq idlwave-system-routines
4909 (nconc idlwave-system-routines (cdr class-result))))
4910
4911 ((eq type 'STATEMENT)
4912 (push (cons (cdr (assq 'name props))
4913 (cdr (assq 'link props)))
4914 idlwave-help-special-topic-words)
4915 ;; Save the links to those which are statement aliases (not routines)
4916 (if (setq alias (assq 'alias_to props))
4917 (unless (member (cdr alias) statement-aliases)
4918 (push (cdr alias) statement-aliases))))
4919
4920 ((eq type 'SYSVAR)
4921 (if (setq alias (cdr (assq 'alias_to props)))
4922 (push (cons (substring (cdr (assq 'name props)) 1)
4923 (substring alias 1))
4924 sysvar-aliases)
4925 (push (idlwave-xml-create-sysvar-alist elem)
4926 idlwave-system-variables-alist)))
4927 (t))))
4928 (idlwave-convert-xml-clean-routine-aliases routine-aliases)
4929 (idlwave-convert-xml-clean-statement-aliases statement-aliases)
4930 (idlwave-convert-xml-clean-sysvar-aliases sysvar-aliases)
4931
4932 (setq idlwave-xml-routine-info-file catalog-file)
4933 (idlwave-save-routine-info)
4934 (message "Converting XML routine info...done")))
4935
4936
4937 ;; ("ROUTINE" type class
4938 ;; (system) | (lib pro_file dir "LIBNAME") | (user pro_file dir "USERLIB") |
4939 ;; (buffer pro_file dir) | (compiled pro_file dir)
4940 ;; "calling_string" ("HELPFILE" (("KWD1" . link1) ...))
4941 ;; ("HELPFILE2" (("KWD2" . link) ...)) ...)
4942
4943
4944 (defun idlwave-load-rinfo-next-step ()
4945 (let ((inhibit-quit t)
4946 (arr idlwave-load-rinfo-steps-done))
4947 (if (catch 'exit
4948 (when (not (aref arr 0))
4949 (message "Loading system routine info in idle time...")
4950 (idlwave-load-system-routine-info)
4951 ;;(load "idlw-rinfo" 'noerror 'nomessage)
4952 (message "Loading system routine info in idle time...done")
4953 (aset arr 0 t)
4954 (throw 'exit t))
4955
4956 (when (not (aref arr 1))
4957 (message "Normalizing idlwave-system-routines in idle time...")
4958 (idlwave-reset-sintern t)
4959 (put 'idlwave-reset-sintern 'done-by-idle t)
4960 (setq idlwave-system-routines
4961 (idlwave-sintern-rinfo-list idlwave-system-routines 'sys))
4962 (message "Normalizing idlwave-system-routines in idle time...done")
4963 (aset arr 1 t)
4964 (throw 'exit t))
4965
4966 (when (not (aref arr 2))
4967 (when (and (stringp idlwave-user-catalog-file)
4968 (file-regular-p idlwave-user-catalog-file))
4969 (message "Loading user catalog in idle time...")
4970 (condition-case nil
4971 (load-file idlwave-user-catalog-file)
4972 (error (throw 'exit nil)))
4973 ;; Check for the old style catalog and warn
4974 (if (and
4975 (boundp 'idlwave-library-routines)
4976 idlwave-library-routines)
4977 (progn
4978 (setq idlwave-library-routines nil)
4979 (ding)
4980 (message "Outdated user catalog: %s... recreate"
4981 idlwave-user-catalog-file))
4982 (message "Loading user catalog in idle time...done")))
4983 (aset arr 2 t)
4984 (throw 'exit t))
4985
4986 (when (not (aref arr 3))
4987 (when idlwave-user-catalog-routines
4988 (message "Normalizing user catalog routines in idle time...")
4989 (setq idlwave-user-catalog-routines
4990 (idlwave-sintern-rinfo-list
4991 idlwave-user-catalog-routines 'sys))
4992 (message
4993 "Normalizing user catalog routines in idle time...done"))
4994 (aset arr 3 t)
4995 (throw 'exit t))
4996
4997 (when (not (aref arr 4))
4998 (idlwave-scan-library-catalogs
4999 "Loading and normalizing library catalogs in idle time...")
5000 (aset arr 4 t)
5001 (throw 'exit t))
5002 (when (not (aref arr 5))
5003 (message "Finishing initialization in idle time...")
5004 (idlwave-routines)
5005 (message "Finishing initialization in idle time...done")
5006 (aset arr 5 t)
5007 (throw 'exit nil)))
5008 ;; restart the timer
5009 (if (sit-for 1)
5010 (idlwave-load-rinfo-next-step)
5011 (setq idlwave-load-rinfo-idle-timer
5012 (run-with-idle-timer
5013 idlwave-init-rinfo-when-idle-after
5014 nil 'idlwave-load-rinfo-next-step))))))
5015
5016 (defvar idlwave-after-load-rinfo-hook nil)
5017
5018 (defun idlwave-load-all-rinfo (&optional force)
5019 ;; Load and case-treat the system, user catalog, and library routine
5020 ;; info files.
5021
5022 ;; System
5023 (when (or force (not (aref idlwave-load-rinfo-steps-done 0)))
5024 ;;(load "idlw-rinfo" 'noerror 'nomessage))
5025 (idlwave-load-system-routine-info))
5026 (when (or force (not (aref idlwave-load-rinfo-steps-done 1)))
5027 (message "Normalizing idlwave-system-routines...")
5028 (setq idlwave-system-routines
5029 (idlwave-sintern-rinfo-list idlwave-system-routines 'sys))
5030 (message "Normalizing idlwave-system-routines...done"))
5031 (when idlwave-system-routines
5032 (setq idlwave-routines (copy-sequence idlwave-system-routines))
5033 (setq idlwave-last-system-routine-info-cons-cell
5034 (nthcdr (1- (length idlwave-routines)) idlwave-routines)))
5035
5036 ;; User catalog
5037 (when (and (stringp idlwave-user-catalog-file)
5038 (file-regular-p idlwave-user-catalog-file))
5039 (condition-case nil
5040 (when (or force (not (aref idlwave-load-rinfo-steps-done 2)))
5041 (load-file idlwave-user-catalog-file))
5042 (error nil))
5043 (when (and
5044 (boundp 'idlwave-library-routines)
5045 idlwave-library-routines)
5046 (setq idlwave-library-routines nil)
5047 (error "Outdated user catalog: %s... recreate"
5048 idlwave-user-catalog-file))
5049 (setq idlwave-true-path-alist nil)
5050 (when (or force (not (aref idlwave-load-rinfo-steps-done 3)))
5051 (message "Normalizing user catalog routines...")
5052 (setq idlwave-user-catalog-routines
5053 (idlwave-sintern-rinfo-list
5054 idlwave-user-catalog-routines 'sys))
5055 (message "Normalizing user catalog routines...done")))
5056
5057 ;; Library catalog
5058 (when (or force (not (aref idlwave-load-rinfo-steps-done 4)))
5059 (idlwave-scan-library-catalogs
5060 "Loading and normalizing library catalogs..."))
5061 (run-hooks 'idlwave-after-load-rinfo-hook))
5062
5063
5064 (defun idlwave-update-buffer-routine-info ()
5065 (let (res)
5066 (cond
5067 ((eq idlwave-scan-all-buffers-for-routine-info t)
5068 ;; Scan all buffers, current buffer last
5069 (message "Scanning all buffers...")
5070 (setq res (idlwave-get-routine-info-from-buffers
5071 (reverse (buffer-list)))))
5072 ((null idlwave-scan-all-buffers-for-routine-info)
5073 ;; Don't scan any buffers
5074 (setq res nil))
5075 (t
5076 ;; Just scan this buffer
5077 (if (derived-mode-p 'idlwave-mode)
5078 (progn
5079 (message "Scanning current buffer...")
5080 (setq res (idlwave-get-routine-info-from-buffers
5081 (list (current-buffer))))))))
5082 ;; Put the result into the correct variable
5083 (setq idlwave-buffer-routines
5084 (idlwave-sintern-rinfo-list res 'set))))
5085
5086 (defun idlwave-concatenate-rinfo-lists (&optional quiet run-hook)
5087 "Put the different sources for routine information together."
5088 ;; The sequence here is important because earlier definitions shadow
5089 ;; later ones. We assume that if things in the buffers are newer
5090 ;; then in the shell of the system, they are meant to be different.
5091 (setcdr idlwave-last-system-routine-info-cons-cell
5092 (append idlwave-buffer-routines
5093 idlwave-compiled-routines
5094 idlwave-library-catalog-routines
5095 idlwave-user-catalog-routines))
5096 (setq idlwave-class-alist nil)
5097
5098 ;; Give a message with information about the number of routines we have.
5099 (unless quiet
5100 (message
5101 "Routines Found: buffer(%d) compiled(%d) library(%d) user(%d) system(%d)"
5102 (length idlwave-buffer-routines)
5103 (length idlwave-compiled-routines)
5104 (length idlwave-library-catalog-routines)
5105 (length idlwave-user-catalog-routines)
5106 (length idlwave-system-routines)))
5107 (if run-hook
5108 (run-hooks 'idlwave-update-rinfo-hook)))
5109
5110 (defun idlwave-class-alist ()
5111 "Return the class alist - make it if necessary."
5112 (or idlwave-class-alist
5113 (let (class)
5114 (loop for x in idlwave-routines do
5115 (when (and (setq class (nth 2 x))
5116 (not (assq class idlwave-class-alist)))
5117 (push (list class) idlwave-class-alist)))
5118 idlwave-class-alist)))
5119
5120 ;; Three functions for the hooks
5121 (defun idlwave-save-buffer-update ()
5122 (idlwave-update-current-buffer-info 'save-buffer))
5123 (defun idlwave-kill-buffer-update ()
5124 (idlwave-update-current-buffer-info 'kill-buffer))
5125 (defun idlwave-new-buffer-update ()
5126 (idlwave-update-current-buffer-info 'find-file))
5127
5128 (defun idlwave-update-current-buffer-info (why)
5129 "Update `idlwave-routines' for current buffer.
5130 Can run from `after-save-hook'."
5131 (when (and (derived-mode-p 'idlwave-mode)
5132 (or (eq t idlwave-auto-routine-info-updates)
5133 (memq why idlwave-auto-routine-info-updates))
5134 idlwave-scan-all-buffers-for-routine-info
5135 idlwave-routines)
5136 (condition-case nil
5137 (let (routines)
5138 (idlwave-replace-buffer-routine-info
5139 (buffer-file-name)
5140 (if (eq why 'kill-buffer)
5141 nil
5142 (setq routines
5143 (idlwave-sintern-rinfo-list
5144 (idlwave-get-routine-info-from-buffers
5145 (list (current-buffer))) 'set))))
5146 (idlwave-concatenate-rinfo-lists 'quiet)
5147 routines)
5148 (error nil))))
5149
5150 (defun idlwave-replace-buffer-routine-info (file new)
5151 "Cut the part from FILE out of `idlwave-buffer-routines' and add NEW."
5152 (let ((list idlwave-buffer-routines)
5153 found)
5154 (while list
5155 ;; The following test uses eq to make sure it works correctly
5156 ;; when two buffers visit the same file. Then the file names
5157 ;; will be equal, but not eq.
5158 (if (eq (idlwave-routine-source-file (nth 3 (car list))) file)
5159 (progn
5160 (setcar list nil)
5161 (setq found t))
5162 (if found
5163 ;; End of that section reached. Jump.
5164 (setq list nil)))
5165 (setq list (cdr list)))
5166 (setq idlwave-buffer-routines
5167 (append new (delq nil idlwave-buffer-routines)))))
5168
5169 ;;----- Scanning buffers -------------------
5170
5171 (defun idlwave-get-routine-info-from-buffers (buffers)
5172 "Call `idlwave-get-buffer-routine-info' on idlwave-mode buffers in BUFFERS."
5173 (let (buf routine-lists res)
5174 (save-excursion
5175 (while (setq buf (pop buffers))
5176 (set-buffer buf)
5177 (if (and (derived-mode-p 'idlwave-mode)
5178 buffer-file-name)
5179 ;; yes, this buffer has the right mode.
5180 (progn (setq res (condition-case nil
5181 (idlwave-get-buffer-routine-info)
5182 (error nil)))
5183 (push res routine-lists)))))
5184 ;; Concatenate the individual lists and return the result
5185 (apply 'nconc routine-lists)))
5186
5187 (defun idlwave-get-buffer-routine-info ()
5188 "Scan the current buffer for routine info. Return (PRO-LIST FUNC-LIST)."
5189 (let* ((case-fold-search t)
5190 routine-list string entry)
5191 (save-excursion
5192 (save-restriction
5193 (widen)
5194 (goto-char (point-min))
5195 (while (re-search-forward
5196 "^[ \t]*\\(pro\\|function\\)[ \t]" nil t)
5197 (setq string (buffer-substring-no-properties
5198 (match-beginning 0)
5199 (progn
5200 (idlwave-end-of-statement)
5201 (point))))
5202 (setq entry (idlwave-parse-definition string))
5203 (push entry routine-list))))
5204 routine-list))
5205
5206 (defvar idlwave-scanning-lib-dir)
5207 (defvar idlwave-scanning-lib)
5208 (defun idlwave-parse-definition (string)
5209 "Parse a module definition."
5210 (let ((case-fold-search t)
5211 start name args type keywords class)
5212 ;; Remove comments
5213 (while (string-match ";.*" string)
5214 (setq string (replace-match "" t t string)))
5215 ;; Remove the continuation line stuff
5216 (while (string-match "\\([^a-zA-Z0-9$_]\\)\\$[ \t]*\n" string)
5217 (setq string (replace-match "\\1 " t nil string)))
5218 (while (string-match "\n" string)
5219 (setq string (replace-match " " t nil string)))
5220 ;; Match the name and type.
5221 (when (string-match
5222 "\\<\\(pro\\|function\\)\\>\\s-+\\(\\([a-zA-Z0-9$_]+\\)::\\)?\\([a-zA-Z0-9$_]+\\)" string)
5223 (setq start (match-end 0))
5224 (setq type (downcase (match-string 1 string)))
5225 (if (match-beginning 3)
5226 (setq class (match-string 3 string)))
5227 (setq name (match-string 4 string)))
5228 ;; Match normal args and keyword args
5229 (while (string-match
5230 ",\\s-*\\([a-zA-Z][a-zA-Z0-9$_]*\\|\\(_ref\\)?_extra\\)\\s-*\\(=\\)?"
5231 string start)
5232 (setq start (match-end 0))
5233 (if (match-beginning 3)
5234 (push (match-string 1 string) keywords)
5235 (push (match-string 1 string) args)))
5236 ;; Normalize and sort.
5237 (setq args (nreverse args))
5238 (setq keywords (sort keywords (lambda (a b)
5239 (string< (downcase a) (downcase b)))))
5240 ;; Make and return the entry
5241 ;; We don't know which argument are optional, so this information
5242 ;; will not be contained in the calling sequence.
5243 (list name
5244 (if (equal type "pro") 'pro 'fun)
5245 class
5246 (cond ((not (boundp 'idlwave-scanning-lib))
5247 (list 'buffer (buffer-file-name)))
5248 ; ((string= (downcase
5249 ; (file-name-sans-extension
5250 ; (file-name-nondirectory (buffer-file-name))))
5251 ; (downcase name))
5252 ; (list 'lib))
5253 ; (t (cons 'lib (file-name-nondirectory (buffer-file-name))))
5254 (t (list 'user (file-name-nondirectory (buffer-file-name))
5255 idlwave-scanning-lib-dir "UserLib")))
5256 (concat
5257 (if (string= type "function") "Result = " "")
5258 (if class "Obj ->[%s::]" "")
5259 "%s"
5260 (if args
5261 (concat
5262 (if (string= type "function") "(" ", ")
5263 (mapconcat 'identity args ", ")
5264 (if (string= type "function") ")" ""))))
5265 (if keywords
5266 (cons nil (mapcar 'list keywords)) ;No help file
5267 nil))))
5268
5269
5270 ;;----- Scanning the user catalog -------------------
5271
5272 (defun idlwave-sys-dir ()
5273 "Return the syslib directory, or a dummy that never matches."
5274 (cond
5275 ((and idlwave-system-directory
5276 (not (string= idlwave-system-directory "")))
5277 idlwave-system-directory)
5278 ((getenv "IDL_DIR"))
5279 (t "@@@@@@@@")))
5280
5281
5282 (defun idlwave-create-user-catalog-file (&optional arg)
5283 "Scan all files on selected dirs of IDL search path for routine information.
5284
5285 A widget checklist will allow you to choose the directories. Write
5286 the result as a file `idlwave-user-catalog-file'. When this file
5287 exists, it will be automatically loaded to give routine information
5288 about library routines. With ARG, just rescan the same directories
5289 as last time - so no widget will pop up."
5290 (interactive "P")
5291 ;; Make sure the file is loaded if it exists.
5292 (if (and (stringp idlwave-user-catalog-file)
5293 (file-regular-p idlwave-user-catalog-file))
5294 (condition-case nil
5295 (load-file idlwave-user-catalog-file)
5296 (error nil)))
5297 ;; Make sure the file name makes sense
5298 (unless (and (stringp idlwave-user-catalog-file)
5299 (> (length idlwave-user-catalog-file) 0)
5300 (file-accessible-directory-p
5301 (file-name-directory idlwave-user-catalog-file))
5302 (not (string= "" (file-name-nondirectory
5303 idlwave-user-catalog-file))))
5304 (error "`idlwave-user-catalog-file' does not point to a file in an accessible directory"))
5305
5306 (cond
5307 ;; Rescan the known directories
5308 ((and arg idlwave-path-alist
5309 (consp (car idlwave-path-alist)))
5310 (idlwave-scan-user-lib-files idlwave-path-alist))
5311
5312 ;; Expand the directories from library-path and run the widget
5313 (idlwave-library-path
5314 (idlwave-display-user-catalog-widget
5315 (if idlwave-true-path-alist
5316 ;; Propagate any flags on the existing path-alist
5317 (mapcar (lambda (x)
5318 (let ((path-entry (assoc (file-truename x)
5319 idlwave-true-path-alist)))
5320 (if path-entry
5321 (cons x (cdr path-entry))
5322 (list x))))
5323 (idlwave-expand-path idlwave-library-path))
5324 (mapcar 'list (idlwave-expand-path idlwave-library-path)))))
5325
5326 ;; Ask the shell for the path and then run the widget
5327 (t
5328 (message "Asking the shell for IDL path...")
5329 (require 'idlw-shell)
5330 (idlwave-shell-send-command idlwave-shell-path-query
5331 '(idlwave-user-catalog-command-hook nil)
5332 'hide))))
5333
5334
5335 ;; Parse shell path information and select among it.
5336 (defun idlwave-user-catalog-command-hook (&optional arg)
5337 ;; Command hook used by `idlwave-create-user-catalog-file'.
5338 (if arg
5339 ;; Scan immediately
5340 (idlwave-scan-user-lib-files idlwave-path-alist)
5341 ;; Set the path and display the widget
5342 (idlwave-shell-get-path-info 'no-write) ; set to something path-alist
5343 (idlwave-scan-library-catalogs "Locating library catalogs..." 'no-load)
5344 (idlwave-display-user-catalog-widget idlwave-path-alist)))
5345
5346 (defconst idlwave-user-catalog-widget-help-string
5347 "This is the front-end to the creation of the IDLWAVE user catalog.
5348 Please select the directories on IDL's search path from which you
5349 would like to extract routine information, to be stored in the file:
5350
5351 %s
5352
5353 If this is not the correct file, first set variable
5354 `idlwave-user-catalog-file', and call this command again.
5355
5356 N.B. Many libraries include pre-scanned catalog files
5357 \(\".idlwave_catalog\"). These are marked with \"[LIB]\", and need
5358 not be scanned. You can scan your own libraries off-line using the
5359 perl script `idlwave_catalog'.
5360
5361 After selecting the directories, choose [Scan & Save] to scan the library
5362 directories and save the routine info.
5363 \n")
5364
5365 (defvar idlwave-widget)
5366 (defvar widget-keymap)
5367 (defun idlwave-display-user-catalog-widget (dirs-list)
5368 "Create the widget to select IDL search path directories for scanning."
5369 (interactive)
5370 (require 'widget)
5371 (require 'wid-edit)
5372 (unless dirs-list
5373 (error "Don't know IDL's search path"))
5374
5375 (kill-buffer (get-buffer-create "*IDLWAVE Widget*"))
5376 (switch-to-buffer (get-buffer-create "*IDLWAVE Widget*"))
5377 (kill-all-local-variables)
5378 (make-local-variable 'idlwave-widget)
5379 (widget-insert (format idlwave-user-catalog-widget-help-string
5380 idlwave-user-catalog-file))
5381
5382 (widget-create 'push-button
5383 :notify 'idlwave-widget-scan-user-lib-files
5384 "Scan & Save")
5385 (widget-insert " ")
5386 (widget-create 'push-button
5387 :notify 'idlwave-delete-user-catalog-file
5388 "Delete File")
5389 (widget-insert " ")
5390 (widget-create 'push-button
5391 :notify
5392 (lambda (&rest ignore)
5393 (let ((path-list (widget-get idlwave-widget :path-dirs)))
5394 (dolist (x path-list)
5395 (unless (memq 'lib (cdr x))
5396 (idlwave-path-alist-add-flag x 'user)))
5397 (idlwave-display-user-catalog-widget path-list)))
5398 "Select All Non-Lib")
5399 (widget-insert " ")
5400 (widget-create 'push-button
5401 :notify
5402 (lambda (&rest ignore)
5403 (let ((path-list (widget-get idlwave-widget :path-dirs)))
5404 (dolist (x path-list)
5405 (idlwave-path-alist-remove-flag x 'user))
5406 (idlwave-display-user-catalog-widget path-list)))
5407 "Deselect All")
5408 (widget-insert " ")
5409 (widget-create 'push-button
5410 :notify (lambda (&rest ignore)
5411 (kill-buffer (current-buffer)))
5412 "Quit")
5413 (widget-insert "\n\n")
5414
5415 (widget-insert "Select Directories: \n")
5416
5417 (setq idlwave-widget
5418 (apply 'widget-create
5419 'checklist
5420 :value (delq nil (mapcar (lambda (x)
5421 (if (memq 'user (cdr x))
5422 (car x)))
5423 dirs-list))
5424 :greedy t
5425 :tag "List of directories"
5426 (mapcar (lambda (x)
5427 (list 'item
5428 (if (memq 'lib (cdr x))
5429 (concat "[LIB] " (car x) )
5430 (car x)))) dirs-list)))
5431 (widget-put idlwave-widget :path-dirs dirs-list)
5432 (widget-insert "\n")
5433 (use-local-map widget-keymap)
5434 (widget-setup)
5435 (goto-char (point-min))
5436 (delete-other-windows))
5437
5438 (defun idlwave-delete-user-catalog-file (&rest ignore)
5439 (if (yes-or-no-p
5440 (format "Delete file %s " idlwave-user-catalog-file))
5441 (progn
5442 (delete-file idlwave-user-catalog-file)
5443 (message "%s has been deleted" idlwave-user-catalog-file))))
5444
5445 (defun idlwave-widget-scan-user-lib-files (&rest ignore)
5446 ;; Call `idlwave-scan-user-lib-files' with data taken from the widget.
5447 (let* ((widget idlwave-widget)
5448 (selected-dirs (widget-value widget))
5449 (path-alist (widget-get widget :path-dirs))
5450 (this-path-alist path-alist)
5451 dir-entry)
5452 (while (setq dir-entry (pop this-path-alist))
5453 (if (member
5454 (if (memq 'lib (cdr dir-entry))
5455 (concat "[LIB] " (car dir-entry))
5456 (car dir-entry))
5457 selected-dirs)
5458 (idlwave-path-alist-add-flag dir-entry 'user)
5459 (idlwave-path-alist-remove-flag dir-entry 'user)))
5460 (idlwave-scan-user-lib-files path-alist)))
5461
5462 (defvar font-lock-mode)
5463 (defun idlwave-scan-user-lib-files (path-alist)
5464 ;; Scan the PRO files in PATH-ALIST and store the info in the user catalog
5465 (let* ((idlwave-scanning-lib t)
5466 (idlwave-scanning-lib-dir "")
5467 (idlwave-completion-case nil)
5468 dirs-alist dir files file)
5469 (setq idlwave-user-catalog-routines nil
5470 idlwave-path-alist path-alist ; for library-path instead
5471 idlwave-true-path-alist nil)
5472 (if idlwave-auto-write-paths (idlwave-write-paths))
5473 (with-current-buffer (get-buffer-create "*idlwave-scan.pro*")
5474 (idlwave-mode)
5475 (setq dirs-alist (reverse path-alist))
5476 (while (setq dir (pop dirs-alist))
5477 (when (memq 'user (cdr dir)) ; Has it marked for scan?
5478 (setq dir (car dir))
5479 (setq idlwave-scanning-lib-dir dir)
5480 (when (file-directory-p dir)
5481 (setq files (directory-files dir 'full "\\.[pP][rR][oO]\\'"))
5482 (while (setq file (pop files))
5483 (when (file-regular-p file)
5484 (if (not (file-readable-p file))
5485 (message "Skipping %s (no read permission)" file)
5486 (message "Scanning %s..." file)
5487 (erase-buffer)
5488 (insert-file-contents file 'visit)
5489 (setq idlwave-user-catalog-routines
5490 (append (idlwave-get-routine-info-from-buffers
5491 (list (current-buffer)))
5492 idlwave-user-catalog-routines)))))))))
5493 (message "Creating user catalog file...")
5494 (kill-buffer "*idlwave-scan.pro*")
5495 (kill-buffer (get-buffer-create "*IDLWAVE Widget*"))
5496 (let ((font-lock-maximum-size 0)
5497 (auto-mode-alist nil))
5498 (find-file idlwave-user-catalog-file))
5499 (if (and (boundp 'font-lock-mode)
5500 font-lock-mode)
5501 (font-lock-mode 0))
5502 (erase-buffer)
5503 (insert ";; IDLWAVE user catalog file\n")
5504 (insert (format ";; Created %s\n\n" (current-time-string)))
5505
5506 ;; Define the routine info list
5507 (insert "\n(setq idlwave-user-catalog-routines\n '(")
5508 (let ((standard-output (current-buffer)))
5509 (mapc (lambda (x)
5510 (insert "\n ")
5511 (prin1 x)
5512 (goto-char (point-max)))
5513 idlwave-user-catalog-routines))
5514 (insert (format "))\n\n;;; %s ends here\n"
5515 (file-name-nondirectory idlwave-user-catalog-file)))
5516 (goto-char (point-min))
5517 ;; Save the buffer
5518 (save-buffer 0)
5519 (kill-buffer (current-buffer)))
5520 (message "Creating user catalog file...done")
5521 (message "Info for %d routines saved in %s"
5522 (length idlwave-user-catalog-routines)
5523 idlwave-user-catalog-file)
5524 (sit-for 2)
5525 (idlwave-update-routine-info t))
5526
5527 (defun idlwave-read-paths ()
5528 (if (and (stringp idlwave-path-file)
5529 (file-regular-p idlwave-path-file))
5530 (condition-case nil
5531 (load idlwave-path-file t t t)
5532 (error nil))))
5533
5534 (defun idlwave-write-paths ()
5535 (interactive)
5536 (when (and idlwave-path-alist idlwave-system-directory)
5537 (let ((font-lock-maximum-size 0)
5538 (auto-mode-alist nil))
5539 (find-file idlwave-path-file))
5540 (if (and (boundp 'font-lock-mode)
5541 font-lock-mode)
5542 (font-lock-mode 0))
5543 (erase-buffer)
5544 (insert ";; IDLWAVE paths\n")
5545 (insert (format ";; Created %s\n\n" (current-time-string)))
5546 ;; Define the variable which knows the value of "!DIR"
5547 (insert (format "\n(setq idlwave-system-directory \"%s\")\n"
5548 idlwave-system-directory))
5549
5550 ;; Define the variable which contains a list of all scanned directories
5551 (insert "\n(setq idlwave-path-alist\n '(")
5552 (let ((standard-output (current-buffer)))
5553 (mapc (lambda (x)
5554 (insert "\n ")
5555 (prin1 x)
5556 (goto-char (point-max)))
5557 idlwave-path-alist))
5558 (insert "))\n")
5559 (save-buffer 0)
5560 (kill-buffer (current-buffer))))
5561
5562
5563 (defun idlwave-expand-path (path &optional default-dir)
5564 ;; Expand parts of path starting with '+' recursively into directory list.
5565 ;; Relative recursive path elements are expanded relative to DEFAULT-DIR.
5566 (message "Expanding path...")
5567 (let (path1 dir recursive)
5568 (while (setq dir (pop path))
5569 (if (setq recursive (string= (substring dir 0 1) "+"))
5570 (setq dir (substring dir 1)))
5571 (if (and recursive
5572 (not (file-name-absolute-p dir)))
5573 (setq dir (expand-file-name dir default-dir)))
5574 (if recursive
5575 ;; Expand recursively
5576 (setq path1 (append (idlwave-recursive-directory-list dir) path1))
5577 ;; Keep unchanged
5578 (push dir path1)))
5579 (message "Expanding path...done")
5580 (nreverse path1)))
5581
5582 (defun idlwave-recursive-directory-list (dir)
5583 ;; Return a list of all directories below DIR, including DIR itself
5584 (let ((path (list dir)) path1 file files)
5585 (while (setq dir (pop path))
5586 (when (file-directory-p dir)
5587 (setq files (nreverse (directory-files dir t "[^.]")))
5588 (while (setq file (pop files))
5589 (if (file-directory-p file)
5590 (push (file-name-as-directory file) path)))
5591 (push dir path1)))
5592 path1))
5593
5594
5595 ;;----- Scanning the library catalogs ------------------
5596
5597
5598
5599
5600 (defun idlwave-scan-library-catalogs (&optional message-base no-load)
5601 "Scan for library catalog files (.idlwave_catalog) and ingest.
5602
5603 All directories on `idlwave-path-alist' (or `idlwave-library-path'
5604 instead, if present) are searched. Print MESSAGE-BASE along with the
5605 libraries being loaded, if passed, and skip loading/normalizing if
5606 NO-LOAD is non-nil. The variable `idlwave-use-library-catalogs' can
5607 be set to nil to disable library catalog scanning."
5608 (when idlwave-use-library-catalogs
5609 (let ((dirs
5610 (if idlwave-library-path
5611 (idlwave-expand-path idlwave-library-path)
5612 (mapcar 'car idlwave-path-alist)))
5613 (old-libname "")
5614 dir-entry dir catalog all-routines)
5615 (if message-base (message message-base))
5616 (while (setq dir (pop dirs))
5617 (catch 'continue
5618 (when (file-readable-p
5619 (setq catalog (expand-file-name ".idlwave_catalog" dir)))
5620 (unless no-load
5621 (setq idlwave-library-catalog-routines nil)
5622 ;; Load the catalog file
5623 (condition-case nil
5624 (load catalog t t t)
5625 (error (throw 'continue t)))
5626 (when (and
5627 message-base
5628 (not (string= idlwave-library-catalog-libname
5629 old-libname)))
5630 (message "%s" (concat message-base
5631 idlwave-library-catalog-libname))
5632 (setq old-libname idlwave-library-catalog-libname))
5633 (when idlwave-library-catalog-routines
5634 (setq all-routines
5635 (append
5636 (idlwave-sintern-rinfo-list
5637 idlwave-library-catalog-routines 'sys dir)
5638 all-routines))))
5639
5640 ;; Add a 'lib flag if on path-alist
5641 (when (and idlwave-path-alist
5642 (setq dir-entry (assoc dir idlwave-path-alist)))
5643 (idlwave-path-alist-add-flag dir-entry 'lib)))))
5644 (unless no-load (setq idlwave-library-catalog-routines all-routines))
5645 (if message-base (message (concat message-base "done"))))))
5646
5647 ;;----- Communicating with the Shell -------------------
5648
5649 ;; First, here is the idl program which can be used to query IDL for
5650 ;; defined routines.
5651 (defconst idlwave-routine-info.pro
5652 "
5653 ;; START OF IDLWAVE SUPPORT ROUTINES
5654 pro idlwave_print_safe,item,limit
5655 catch,err
5656 if err ne 0 then begin
5657 print,'Could not print item.'
5658 return
5659 endif
5660 if n_elements(item) gt limit then $
5661 print,item[0:limit-1],'<... truncated at ',strtrim(limit,2),' elements>' $
5662 else print,item
5663 end
5664
5665 pro idlwave_print_info_entry,name,func=func,separator=sep
5666 ;; See if it's an object method
5667 if name eq '' then return
5668 func = keyword_set(func)
5669 methsep = strpos(name,'::')
5670 meth = methsep ne -1
5671
5672 ;; Get routine info
5673 pars = routine_info(name,/parameters,functions=func)
5674 source = routine_info(name,/source,functions=func)
5675 nargs = pars.num_args
5676 nkw = pars.num_kw_args
5677 if nargs gt 0 then args = pars.args
5678 if nkw gt 0 then kwargs = pars.kw_args
5679
5680 ;; Trim the class, and make the name
5681 if meth then begin
5682 class = strmid(name,0,methsep)
5683 name = strmid(name,methsep+2,strlen(name)-1)
5684 if nargs gt 0 then begin
5685 ;; remove the self argument
5686 wh = where(args ne 'SELF',nargs)
5687 if nargs gt 0 then args = args[wh]
5688 endif
5689 endif else begin
5690 ;; No class, just a normal routine.
5691 class = \"\"
5692 endelse
5693
5694 ;; Calling sequence
5695 cs = \"\"
5696 if func then cs = 'Result = '
5697 if meth then cs = cs + 'Obj -> [' + '%s' + '::]'
5698 cs = cs + '%s'
5699 if func then cs = cs + '(' else if nargs gt 0 then cs = cs + ', '
5700 if nargs gt 0 then begin
5701 for j=0,nargs-1 do begin
5702 cs = cs + args[j]
5703 if j lt nargs-1 then cs = cs + ', '
5704 endfor
5705 end
5706 if func then cs = cs + ')'
5707 ;; Keyword arguments
5708 kwstring = ''
5709 if nkw gt 0 then begin
5710 for j=0,nkw-1 do begin
5711 kwstring = kwstring + ' ' + kwargs[j]
5712 endfor
5713 endif
5714
5715 ret=(['IDLWAVE-PRO','IDLWAVE-FUN'])[func]
5716
5717 print,ret + ': ' + name + sep + class + sep + source[0].path $
5718 + sep + cs + sep + kwstring
5719 end
5720
5721 pro idlwave_routine_info,file
5722 on_error,1
5723 sep = '<@>'
5724 print,'>>>BEGIN OF IDLWAVE ROUTINE INFO (\"' + sep + '\" IS THE SEPARATOR)'
5725 all = routine_info()
5726 fileQ=n_elements(file) ne 0
5727 if fileQ then file=strtrim(file,2)
5728 for i=0L,n_elements(all)-1L do begin
5729 if fileQ then begin
5730 if (routine_info(all[i],/SOURCE)).path eq file then $
5731 idlwave_print_info_entry,all[i],separator=sep
5732 endif else idlwave_print_info_entry,all[i],separator=sep
5733 endfor
5734 all = routine_info(/functions)
5735 for i=0L,n_elements(all)-1L do begin
5736 if fileQ then begin
5737 if (routine_info(all[i],/FUNCTIONS,/SOURCE)).path eq file then $
5738 idlwave_print_info_entry,all[i],separator=sep,/FUNC
5739 endif else idlwave_print_info_entry,all[i],separator=sep,/FUNC
5740 endfor
5741 print,'>>>END OF IDLWAVE ROUTINE INFO'
5742 end
5743
5744 pro idlwave_get_sysvars
5745 on_error,1
5746 catch,error_status
5747 if error_status ne 0 then begin
5748 print, 'Cannot get info about system variables'
5749 endif else begin
5750 help,/brief,output=s,/system_variables ; ? unsafe use of OUTPUT=
5751 s = strtrim(strjoin(s,' ',/single),2) ; make one line
5752 v = strsplit(s,' +',/regex,/extract) ; get variables
5753 for i=0L,n_elements(v)-1 do begin
5754 t = [''] ; get tag list
5755 a=execute('if n_tags('+v[i]+') gt 0 then t=tag_names('+v[i]+')')
5756 print, 'IDLWAVE-SYSVAR: '+v[i]+' '+strjoin(t,' ',/single)
5757 endfor
5758 endelse
5759 end
5760
5761 pro idlwave_get_class_tags, class
5762 res = execute('tags=tag_names({'+class+'})')
5763 if res then print,'IDLWAVE-CLASS-TAGS: '+class+' '+strjoin(tags,' ',/single)
5764 end
5765 ;; END OF IDLWAVE SUPPORT ROUTINES
5766 "
5767 "The IDL programs to get info from the shell.")
5768
5769 (defvar idlwave-idlwave_routine_info-compiled nil
5770 "Remember if the routine info procedure is already compiled.")
5771
5772 (defvar idlwave-shell-temp-pro-file)
5773 (defvar idlwave-shell-temp-rinfo-save-file)
5774
5775 (defun idlwave-shell-compile-helper-routines (&optional wait)
5776 (unless (and idlwave-idlwave_routine_info-compiled
5777 (file-readable-p (idlwave-shell-temp-file 'rinfo)))
5778 (with-current-buffer (idlwave-find-file-noselect
5779 (idlwave-shell-temp-file 'pro))
5780 (erase-buffer)
5781 (insert idlwave-routine-info.pro)
5782 (save-buffer 0))
5783 (idlwave-shell-send-command
5784 (concat ".run \"" idlwave-shell-temp-pro-file "\"")
5785 nil 'hide wait)
5786 (idlwave-shell-send-command
5787 (format "save,'idlwave_print_safe','idlwave_routine_info','idlwave_print_info_entry','idlwave_get_class_tags','idlwave_get_sysvars',FILE='%s',/ROUTINES"
5788 (idlwave-shell-temp-file 'rinfo))
5789 nil 'hide)
5790 (setq idlwave-idlwave_routine_info-compiled t))
5791
5792 ;; Restore if necessary. Must use execute to hide lame routine_info
5793 ;; errors on undefinded routine
5794 (idlwave-shell-send-command
5795 (format "if execute(\"_v=routine_info('idlwave_routine_info',/SOURCE)\") eq 0 then restore,'%s' else if _v.path eq '' then restore,'%s'"
5796 idlwave-shell-temp-rinfo-save-file
5797 idlwave-shell-temp-rinfo-save-file)
5798 nil 'hide))
5799
5800
5801 (defun idlwave-shell-update-routine-info (&optional quiet run-hooks wait file)
5802 "Query the shell for routine_info of compiled modules and update the lists."
5803 ;; Save and compile the procedure. The compiled procedure is then
5804 ;; saved into an IDL SAVE file, to allow for fast RESTORE. We may
5805 ;; need to test for and possibly RESTORE the procedure each time we
5806 ;; use it, since the user may have killed or redefined it. In
5807 ;; particular, .RESET_SESSION will kill all user procedures. If
5808 ;; FILE is set, only update routine info for routines in that file.
5809
5810 (idlwave-shell-compile-helper-routines wait)
5811 ; execute the routine_info procedure, and analyze the output
5812 (idlwave-shell-send-command
5813 (format "idlwave_routine_info%s" (if file (concat ",'" file "'") ""))
5814 `(progn
5815 (idlwave-shell-routine-info-filter)
5816 (idlwave-concatenate-rinfo-lists ,quiet ,run-hooks))
5817 'hide wait))
5818
5819 ;; ---------------------------------------------------------------------------
5820 ;;
5821 ;; Completion and displaying routine calling sequences
5822
5823 (defvar idlwave-completion-help-info nil)
5824 (defvar idlwave-completion-help-links nil)
5825 (defvar idlwave-current-obj_new-class nil)
5826 (defvar idlwave-complete-special nil)
5827 (defvar method-selector)
5828 (defvar class-selector)
5829 (defvar type-selector)
5830 (defvar super-classes)
5831
5832 (defun idlwave-complete (&optional arg module class)
5833 "Complete a function, procedure or keyword name at point.
5834 This function is smart and figures out what can be completed
5835 at this point.
5836 - At the beginning of a statement it completes procedure names.
5837 - In the middle of a statement it completes function names.
5838 - After a `(' or `,' in the argument list of a function or procedure,
5839 it completes a keyword of the relevant function or procedure.
5840 - In the first arg of `OBJ_NEW', it completes a class name.
5841
5842 When several completions are possible, a list will be displayed in
5843 the *Completions* buffer. If this list is too long to fit into the
5844 window, scrolling can be achieved by repeatedly pressing
5845 \\[idlwave-complete].
5846
5847 The function also knows about object methods. When it needs a class
5848 name, the action depends upon `idlwave-query-class', which see. You
5849 can force IDLWAVE to ask you for a class name with a
5850 \\[universal-argument] prefix argument to this command.
5851
5852 See also the variables `idlwave-keyword-completion-adds-equal' and
5853 `idlwave-function-completion-adds-paren'.
5854
5855 The optional ARG can be used to specify the completion type in order
5856 to override IDLWAVE's idea of what should be completed at point.
5857 Possible values are:
5858
5859 0 <=> query for the completion type
5860 1 <=> 'procedure
5861 2 <=> 'procedure-keyword
5862 3 <=> 'function
5863 4 <=> 'function-keyword
5864 5 <=> 'procedure-method
5865 6 <=> 'procedure-method-keyword
5866 7 <=> 'function-method
5867 8 <=> 'function-method-keyword
5868 9 <=> 'class
5869
5870 As a special case, the universal argument C-u forces completion of
5871 function names in places where the default would be a keyword.
5872
5873 Two prefix argument, C-u C-u, prompts for a regexp by which to limit
5874 completion.
5875
5876 For Lisp programmers only:
5877 When we force a keyword, optional argument MODULE can contain the module name.
5878 When we force a method or a method keyword, CLASS can specify the class."
5879 (interactive "P")
5880 (idlwave-routines)
5881 (let* ((where-list
5882 (if (and arg
5883 (or (and (integerp arg) (not (equal arg '(16))))
5884 (symbolp arg)))
5885 (idlwave-make-force-complete-where-list arg module class)
5886 (idlwave-where)))
5887 (what (nth 2 where-list))
5888 (idlwave-force-class-query (equal arg '(4)))
5889 (completion-regexp-list
5890 (if (equal arg '(16))
5891 (list (read-string (concat "Completion Regexp: "))))))
5892
5893 (if (and module (string-match "::" module))
5894 (setq class (substring module 0 (match-beginning 0))
5895 module (substring module (match-end 0))))
5896
5897 (cond
5898
5899 ((and (null arg)
5900 (eq (car-safe last-command) 'idlwave-display-completion-list)
5901 (get-buffer-window "*Completions*"))
5902 (setq this-command last-command)
5903 (idlwave-scroll-completions))
5904
5905 ;; Complete a filename in quotes
5906 ((and (idlwave-in-quote)
5907 (not (eq what 'class)))
5908 (idlwave-complete-filename))
5909
5910 ;; Check for any special completion functions
5911 ((and idlwave-complete-special
5912 (idlwave-call-special idlwave-complete-special)))
5913
5914 ((null what)
5915 (error "Nothing to complete here"))
5916
5917 ;; Complete a class
5918 ((eq what 'class)
5919 (setq idlwave-completion-help-info '(class))
5920 (idlwave-complete-class))
5921
5922 ((eq what 'procedure)
5923 ;; Complete a procedure name
5924 (let* ((cw-list (nth 3 where-list))
5925 (class-selector (idlwave-determine-class cw-list 'pro))
5926 (super-classes (unless (idlwave-explicit-class-listed cw-list)
5927 (idlwave-all-class-inherits class-selector)))
5928 (isa (concat "procedure" (if class-selector "-method" "")))
5929 (type-selector 'pro))
5930 (setq idlwave-completion-help-info
5931 (list 'routine nil type-selector class-selector nil super-classes))
5932 (idlwave-complete-in-buffer
5933 'procedure (if class-selector 'method 'routine)
5934 (idlwave-routines) 'idlwave-selector
5935 (format "Select a %s name%s"
5936 isa
5937 (if class-selector
5938 (format " (class is %s)"
5939 (if (eq class-selector t)
5940 "unknown" class-selector))
5941 ""))
5942 isa
5943 'idlwave-attach-method-classes 'idlwave-add-file-link-selector)))
5944
5945 ((eq what 'function)
5946 ;; Complete a function name
5947 (let* ((cw-list (nth 3 where-list))
5948 (class-selector (idlwave-determine-class cw-list 'fun))
5949 (super-classes (unless (idlwave-explicit-class-listed cw-list)
5950 (idlwave-all-class-inherits class-selector)))
5951 (isa (concat "function" (if class-selector "-method" "")))
5952 (type-selector 'fun))
5953 (setq idlwave-completion-help-info
5954 (list 'routine nil type-selector class-selector nil super-classes))
5955 (idlwave-complete-in-buffer
5956 'function (if class-selector 'method 'routine)
5957 (idlwave-routines) 'idlwave-selector
5958 (format "Select a %s name%s"
5959 isa
5960 (if class-selector
5961 (format " (class is %s)"
5962 (if (eq class-selector t)
5963 "unknown" class-selector))
5964 ""))
5965 isa
5966 'idlwave-attach-method-classes 'idlwave-add-file-link-selector)))
5967
5968 ((and (memq what '(procedure-keyword function-keyword)) ; Special Case
5969 (equal arg '(4)))
5970 (idlwave-complete 3))
5971
5972 ((eq what 'procedure-keyword)
5973 ;; Complete a procedure keyword
5974 (let* ((where (nth 3 where-list))
5975 (name (car where))
5976 (method-selector name)
5977 (type-selector 'pro)
5978 (class (idlwave-determine-class where 'pro))
5979 (class-selector class)
5980 (super-classes (idlwave-all-class-inherits class-selector))
5981 (isa (format "procedure%s-keyword" (if class "-method" "")))
5982 (entry (idlwave-best-rinfo-assq
5983 name 'pro class (idlwave-routines)))
5984 (system (if entry (eq (car (nth 3 entry)) 'system)))
5985 (list (idlwave-entry-keywords entry 'do-link)))
5986 (unless (or entry (eq class t))
5987 (error "Nothing known about procedure %s"
5988 (idlwave-make-full-name class name)))
5989 (setq list (idlwave-fix-keywords name 'pro class list
5990 super-classes system))
5991 (unless list (error "No keywords available for procedure %s"
5992 (idlwave-make-full-name class name)))
5993 (setq idlwave-completion-help-info
5994 (list 'keyword name type-selector class-selector entry super-classes))
5995 (idlwave-complete-in-buffer
5996 'keyword 'keyword list nil
5997 (format "Select keyword for procedure %s%s"
5998 (idlwave-make-full-name class name)
5999 (if (or (member '("_EXTRA") list)
6000 (member '("_REF_EXTRA") list))
6001 " (note _EXTRA)" ""))
6002 isa
6003 'idlwave-attach-keyword-classes)))
6004
6005 ((eq what 'function-keyword)
6006 ;; Complete a function keyword
6007 (let* ((where (nth 3 where-list))
6008 (name (car where))
6009 (method-selector name)
6010 (type-selector 'fun)
6011 (class (idlwave-determine-class where 'fun))
6012 (class-selector class)
6013 (super-classes (idlwave-all-class-inherits class-selector))
6014 (isa (format "function%s-keyword" (if class "-method" "")))
6015 (entry (idlwave-best-rinfo-assq
6016 name 'fun class (idlwave-routines)))
6017 (system (if entry (eq (car (nth 3 entry)) 'system)))
6018 (list (idlwave-entry-keywords entry 'do-link))
6019 msg-name)
6020 (unless (or entry (eq class t))
6021 (error "Nothing known about function %s"
6022 (idlwave-make-full-name class name)))
6023 (setq list (idlwave-fix-keywords name 'fun class list
6024 super-classes system))
6025 ;; OBJ_NEW: Messages mention the proper Init method
6026 (setq msg-name (if (and (null class)
6027 (string= (upcase name) "OBJ_NEW"))
6028 (concat idlwave-current-obj_new-class
6029 "::Init (via OBJ_NEW)")
6030 (idlwave-make-full-name class name)))
6031 (unless list (error "No keywords available for function %s"
6032 msg-name))
6033 (setq idlwave-completion-help-info
6034 (list 'keyword name type-selector class-selector nil super-classes))
6035 (idlwave-complete-in-buffer
6036 'keyword 'keyword list nil
6037 (format "Select keyword for function %s%s" msg-name
6038 (if (or (member '("_EXTRA") list)
6039 (member '("_REF_EXTRA") list))
6040 " (note _EXTRA)" ""))
6041 isa
6042 'idlwave-attach-keyword-classes)))
6043
6044 (t (error "This should not happen (idlwave-complete)")))))
6045
6046 (defvar idlwave-complete-special nil
6047 "List of special completion functions.
6048 These functions are called for each completion. Each function must
6049 check if its own special completion context is present. If yes, it
6050 should use `idlwave-complete-in-buffer' to do some completion and
6051 return t. If such a function returns t, *no further* attempts to
6052 complete other contexts will be done. If the function returns nil,
6053 other completions will be tried.")
6054
6055 (defun idlwave-call-special (functions &rest args)
6056 (let ((funcs functions)
6057 fun ret)
6058 (catch 'exit
6059 (while (setq fun (pop funcs))
6060 (if (setq ret (apply fun args))
6061 (throw 'exit ret)))
6062 nil)))
6063
6064 (defun idlwave-make-force-complete-where-list (what &optional module class)
6065 ;; Return an artificial WHERE specification to force the completion
6066 ;; routine to complete a specific item independent of context.
6067 ;; WHAT is the prefix arg of `idlwave-complete', see there for details.
6068 ;; MODULE and CLASS can be used to specify the routine name and class.
6069 ;; The class name will also be found in MODULE if that is like "class::mod".
6070 (let* ((what-list '(("procedure") ("procedure-keyword")
6071 ("function") ("function-keyword")
6072 ("procedure-method") ("procedure-method-keyword")
6073 ("function-method") ("function-method-keyword")
6074 ("class")))
6075 (module (idlwave-sintern-routine-or-method module class))
6076 (class (idlwave-sintern-class class))
6077 (what (cond
6078 ((equal what 0)
6079 (setq what
6080 (intern (completing-read
6081 "Complete what? " what-list nil t))))
6082 ((integerp what)
6083 (setq what (intern (car (nth (1- what) what-list)))))
6084 ((and what
6085 (symbolp what)
6086 (assoc (symbol-name what) what-list))
6087 what)
6088 (t (error "Invalid WHAT"))))
6089 (nil-list '(nil nil nil nil))
6090 (class-list (list nil nil (or class t) nil)))
6091
6092 (cond
6093
6094 ((eq what 'procedure)
6095 (list nil-list nil-list 'procedure nil-list nil))
6096
6097 ((eq what 'procedure-keyword)
6098 (let* ((class-selector nil)
6099 (super-classes nil)
6100 (type-selector 'pro)
6101 (pro (or module
6102 (idlwave-completing-read
6103 "Procedure: " (idlwave-routines) 'idlwave-selector))))
6104 (setq pro (idlwave-sintern-routine pro))
6105 (list nil-list nil-list 'procedure-keyword
6106 (list pro nil nil nil) nil)))
6107
6108 ((eq what 'function)
6109 (list nil-list nil-list 'function nil-list nil))
6110
6111 ((eq what 'function-keyword)
6112 (let* ((class-selector nil)
6113 (super-classes nil)
6114 (type-selector 'fun)
6115 (func (or module
6116 (idlwave-completing-read
6117 "Function: " (idlwave-routines) 'idlwave-selector))))
6118 (setq func (idlwave-sintern-routine func))
6119 (list nil-list nil-list 'function-keyword
6120 (list func nil nil nil) nil)))
6121
6122 ((eq what 'procedure-method)
6123 (list nil-list nil-list 'procedure class-list nil))
6124
6125 ((eq what 'procedure-method-keyword)
6126 (let* ((class (idlwave-determine-class class-list 'pro))
6127 (class-selector class)
6128 (super-classes (idlwave-all-class-inherits class-selector))
6129 (type-selector 'pro)
6130 (pro (or module
6131 (idlwave-completing-read
6132 (format "Procedure in %s class: " class-selector)
6133 (idlwave-routines) 'idlwave-selector))))
6134 (setq pro (idlwave-sintern-method pro))
6135 (list nil-list nil-list 'procedure-keyword
6136 (list pro nil class nil) nil)))
6137
6138 ((eq what 'function-method)
6139 (list nil-list nil-list 'function class-list nil))
6140
6141 ((eq what 'function-method-keyword)
6142 (let* ((class (idlwave-determine-class class-list 'fun))
6143 (class-selector class)
6144 (super-classes (idlwave-all-class-inherits class-selector))
6145 (type-selector 'fun)
6146 (func (or module
6147 (idlwave-completing-read
6148 (format "Function in %s class: " class-selector)
6149 (idlwave-routines) 'idlwave-selector))))
6150 (setq func (idlwave-sintern-method func))
6151 (list nil-list nil-list 'function-keyword
6152 (list func nil class nil) nil)))
6153
6154 ((eq what 'class)
6155 (list nil-list nil-list 'class nil-list nil))
6156
6157 (t (error "Invalid value for WHAT")))))
6158
6159 (defun idlwave-completing-read (&rest args)
6160 ;; Completing read, case insensitive
6161 (let ((old-value (default-value 'completion-ignore-case)))
6162 (unwind-protect
6163 (progn
6164 (setq-default completion-ignore-case t)
6165 (apply 'completing-read args))
6166 (setq-default completion-ignore-case old-value))))
6167
6168 (defvar idlwave-shell-default-directory)
6169 (defun idlwave-complete-filename ()
6170 "Use the comint stuff to complete a file name."
6171 (require 'comint)
6172 (let* ((comint-file-name-chars "~/A-Za-z0-9+@:_.$#%={}\\-")
6173 (comint-completion-addsuffix nil)
6174 (default-directory
6175 (if (and (boundp 'idlwave-shell-default-directory)
6176 (stringp idlwave-shell-default-directory)
6177 (file-directory-p idlwave-shell-default-directory))
6178 idlwave-shell-default-directory
6179 default-directory)))
6180 (comint-dynamic-complete-filename)))
6181
6182 (defun idlwave-make-full-name (class name)
6183 ;; Make a fully qualified module name including the class name
6184 (concat (if class (format "%s::" class) "") name))
6185
6186 (defun idlwave-rinfo-assoc (name type class list)
6187 "Like `idlwave-rinfo-assq', but sintern strings first."
6188 (idlwave-rinfo-assq
6189 (idlwave-sintern-routine-or-method name class)
6190 type (idlwave-sintern-class class) list))
6191
6192 (defun idlwave-rinfo-assq (name type class list)
6193 ;; Works like assq, but also checks type and class
6194 (catch 'exit
6195 (let (match)
6196 (while (setq match (assq name list))
6197 (and (or (eq type t)
6198 (eq (nth 1 match) type))
6199 (eq (nth 2 match) class)
6200 (throw 'exit match))
6201 (setq list (cdr (memq match list)))))))
6202
6203 (defun idlwave-rinfo-assq-any-class (name type class list)
6204 ;; Return the first matching method on the inheritance list
6205 (let* ((classes (cons class (idlwave-all-class-inherits class)))
6206 class rtn)
6207 (while classes
6208 (if (setq rtn (idlwave-rinfo-assq name type (pop classes) list))
6209 (setq classes nil)))
6210 rtn))
6211
6212 (defun idlwave-best-rinfo-assq (name type class list &optional with-file
6213 keep-system)
6214 "Like `idlwave-rinfo-assq', but get all twins and sort, then return first.
6215 If WITH-FILE is passed, find the best rinfo entry with a file
6216 included. If KEEP-SYSTEM is set, don't prune system for compiled
6217 syslib files."
6218 (let ((twins (idlwave-routine-twins
6219 (idlwave-rinfo-assq-any-class name type class list)
6220 list))
6221 syslibp)
6222 (when (> (length twins) 1)
6223 (setq twins (sort twins 'idlwave-routine-entry-compare-twins))
6224 (if (and (null keep-system)
6225 (eq 'system (car (nth 3 (car twins))))
6226 (setq syslibp (idlwave-any-syslib (cdr twins)))
6227 (not (equal 1 syslibp)))
6228 ;; Its a compiled syslib, so we need to remove the system entry
6229 (setq twins (cdr twins)))
6230 (if with-file
6231 (setq twins (delq nil
6232 (mapcar (lambda (x)
6233 (if (nth 1 (nth 3 x)) x))
6234 twins)))))
6235 (car twins)))
6236
6237 (defun idlwave-best-rinfo-assoc (name type class list &optional with-file
6238 keep-system)
6239 "Like `idlwave-best-rinfo-assq', but sintern strings first."
6240 (idlwave-best-rinfo-assq
6241 (idlwave-sintern-routine-or-method name class)
6242 type (idlwave-sintern-class class) list with-file keep-system))
6243
6244 (defun idlwave-any-syslib (entries)
6245 "Does the entry list ENTRIES contain a syslib entry?
6246 If yes, return the index (>=1)."
6247 (let (file (cnt 0))
6248 (catch 'exit
6249 (while entries
6250 (incf cnt)
6251 (setq file (idlwave-routine-source-file (nth 3 (car entries))))
6252 (if (and file (idlwave-syslib-p file))
6253 (throw 'exit cnt)
6254 (setq entries (cdr entries))))
6255 nil)))
6256
6257 (defun idlwave-all-assq (key list)
6258 "Return a list of all associations of Key in LIST."
6259 (let (rtn elt)
6260 (while (setq elt (assq key list))
6261 (push elt rtn)
6262 (setq list (cdr (memq elt list))))
6263 (nreverse rtn)))
6264
6265 (defun idlwave-all-method-classes (method &optional type)
6266 "Return all classes which have a method METHOD.
6267 TYPE is 'fun or 'pro.
6268 When TYPE is not specified, both procedures and functions will be considered."
6269 (if (null method)
6270 (mapcar 'car (idlwave-class-alist))
6271 (let (rtn)
6272 (mapc (lambda (x)
6273 (and (nth 2 x)
6274 (or (not type)
6275 (eq type (nth 1 x)))
6276 (push (nth 2 x) rtn)))
6277 (idlwave-all-assq method (idlwave-routines)))
6278 (idlwave-uniquify rtn))))
6279
6280 (defun idlwave-all-method-keyword-classes (method keyword &optional type)
6281 "Return all classes which have a method METHOD with keyword KEYWORD.
6282 TYPE is 'fun or 'pro.
6283 When TYPE is not specified, both procedures and functions will be considered."
6284 (if (or (null method)
6285 (null keyword))
6286 nil
6287 (let (rtn)
6288 (mapc (lambda (x)
6289 (and (nth 2 x) ; non-nil class
6290 (or (not type) ; correct or unspecified type
6291 (eq type (nth 1 x)))
6292 (assoc keyword (idlwave-entry-keywords x))
6293 (push (nth 2 x) rtn)))
6294 (idlwave-all-assq method (idlwave-routines)))
6295 (idlwave-uniquify rtn))))
6296
6297 (defun idlwave-members-only (list club)
6298 "Return list of all elements in LIST which are also in CLUB."
6299 (let (rtn)
6300 (while list
6301 (if (member (car list) club)
6302 (setq rtn (cons (car list) rtn)))
6303 (setq list (cdr list)))
6304 (nreverse rtn)))
6305
6306 (defun idlwave-nonmembers-only (list club)
6307 "Return list of all elements in LIST which are not in CLUB."
6308 (let (rtn)
6309 (while list
6310 (if (member (car list) club)
6311 nil
6312 (setq rtn (cons (car list) rtn)))
6313 (setq list (cdr list)))
6314 (nreverse rtn)))
6315
6316 (defun idlwave-explicit-class-listed (info)
6317 "Return whether or not the class is listed explicitly, ala a->b::c.
6318 INFO is as returned by `idlwave-what-function' or `-procedure'."
6319 (let ((apos (nth 3 info)))
6320 (if apos
6321 (save-excursion (goto-char apos)
6322 (looking-at "->[a-zA-Z][a-zA-Z0-9$_]*::")))))
6323
6324 (defvar idlwave-determine-class-special nil
6325 "List of special functions for determining class.
6326 Must accept two arguments: `apos' and `info'.")
6327
6328 (defun idlwave-determine-class (info type)
6329 ;; Determine the class of a routine call.
6330 ;; INFO is the `cw-list' structure as returned by idlwave-where.
6331 ;; The second element in this structure is the class. When nil, we
6332 ;; return nil. When t, try to get the class from text properties at
6333 ;; the arrow. When the object is "self", we use the class of the
6334 ;; current routine. otherwise prompt the user for a class name.
6335 ;; Also stores the selected class as a text property at the arrow.
6336 ;; TYPE is 'fun or 'pro.
6337 (let* ((class (nth 2 info))
6338 (apos (nth 3 info))
6339 (nassoc (assoc (if (stringp (car info))
6340 (upcase (car info))
6341 (car info))
6342 idlwave-query-class))
6343 (dassoc (assq (if (car info) 'keyword-default 'method-default)
6344 idlwave-query-class))
6345 (query (cond (nassoc (cdr nassoc))
6346 (dassoc (cdr dassoc))
6347 (t t)))
6348 (arrow (and apos (string= (buffer-substring apos (+ 2 apos)) "->")))
6349 (is-self
6350 (and arrow
6351 (save-excursion (goto-char apos)
6352 (forward-word -1)
6353 (let ((case-fold-search t))
6354 (looking-at "self\\>")))))
6355 (force-query idlwave-force-class-query)
6356 store special-class class-alist)
6357 (cond
6358 ((null class) nil)
6359 ((eq t class)
6360 ;; There is an object which would like to know its class
6361 (if (and arrow (get-text-property apos 'idlwave-class)
6362 idlwave-store-inquired-class
6363 (not force-query))
6364 (setq class (get-text-property apos 'idlwave-class)
6365 class (idlwave-sintern-class class)))
6366 (if (and (eq t class) is-self)
6367 (setq class (or (nth 2 (idlwave-current-routine)) class)))
6368
6369 ;; Before prompting, try any special class determination routines
6370 (when (and (eq t class)
6371 idlwave-determine-class-special
6372 (not force-query))
6373 (setq special-class
6374 (idlwave-call-special idlwave-determine-class-special apos))
6375 (if special-class
6376 (setq class (idlwave-sintern-class special-class)
6377 store idlwave-store-inquired-class)))
6378
6379 ;; Prompt for a class, if we need to
6380 (when (and (eq class t)
6381 (or force-query query))
6382 (setq class-alist
6383 (mapcar 'list (idlwave-all-method-classes (car info) type)))
6384 (setq class
6385 (idlwave-sintern-class
6386 (cond
6387 ((and (= (length class-alist) 0) (not force-query))
6388 (error "No classes available with method %s" (car info)))
6389 ((and (= (length class-alist) 1) (not force-query))
6390 (car (car class-alist)))
6391 (t
6392 (setq store idlwave-store-inquired-class)
6393 (idlwave-completing-read
6394 (format "Class%s: " (if (stringp (car info))
6395 (format " for %s method %s"
6396 type (car info))
6397 ""))
6398 class-alist nil nil nil 'idlwave-class-history))))))
6399
6400 ;; Store it, if requested
6401 (when (and class (not (eq t class)))
6402 ;; We have a real class here
6403 (when (and store arrow)
6404 (condition-case ()
6405 (add-text-properties
6406 apos (+ apos 2)
6407 `(idlwave-class ,class face ,idlwave-class-arrow-face
6408 rear-nonsticky t))
6409 (error nil)))
6410 (setf (nth 2 info) class))
6411 ;; Return the class
6412 class)
6413 ;; Default as fallback
6414 (t class))))
6415
6416 (defun idlwave-selector (a)
6417 (and (eq (nth 1 a) type-selector)
6418 (or (and (nth 2 a) (eq class-selector t))
6419 (eq (nth 2 a) class-selector)
6420 (memq (nth 2 a) super-classes))))
6421
6422 (defun idlwave-add-file-link-selector (a)
6423 ;; Record a file link, if any, for the tested names during selection.
6424 (let ((sel (idlwave-selector a)) file)
6425 (if (and sel (setq file (idlwave-entry-has-help a)))
6426 (push (cons (car a) file) idlwave-completion-help-links))
6427 sel))
6428
6429
6430 (defun idlwave-where ()
6431 "Find out where we are.
6432 The return value is a list with the following stuff:
6433 \(PRO-LIST FUNC-LIST COMPLETE-WHAT CW-LIST LAST-CHAR)
6434
6435 PRO-LIST (PRO POINT CLASS ARROW)
6436 FUNC-LIST (FUNC POINT CLASS ARROW)
6437 COMPLETE-WHAT a symbol indicating what kind of completion makes sense here
6438 CW-LIST (PRO-OR-FUNC POINT CLASS ARROW) Like PRO-LIST, for what can
6439 be completed here.
6440 LAST-CHAR last relevant character before point (non-white non-comment,
6441 not part of current identifier or leading slash).
6442
6443 In the lists, we have these meanings:
6444 PRO: Procedure name
6445 FUNC: Function name
6446 POINT: Where is this
6447 CLASS: What class has the routine (nil=no, t=is method, but class unknown)
6448 ARROW: Location of the arrow"
6449 (idlwave-routines)
6450 (let* (;(bos (save-excursion (idlwave-beginning-of-statement) (point)))
6451 (bos (save-excursion (idlwave-start-of-substatement 'pre) (point)))
6452 (func-entry (idlwave-what-function bos))
6453 (func (car func-entry))
6454 (func-class (nth 1 func-entry))
6455 (func-arrow (nth 2 func-entry))
6456 (func-point (or (nth 3 func-entry) 0))
6457 (func-level (or (nth 4 func-entry) 0))
6458 (pro-entry (idlwave-what-procedure bos))
6459 (pro (car pro-entry))
6460 (pro-class (nth 1 pro-entry))
6461 (pro-arrow (nth 2 pro-entry))
6462 (pro-point (or (nth 3 pro-entry) 0))
6463 (last-char (idlwave-last-valid-char))
6464 (case-fold-search t)
6465 (match-string (buffer-substring bos (point)))
6466 cw cw-mod cw-arrow cw-class cw-point)
6467 (if (< func-point pro-point) (setq func nil))
6468 (cond
6469 ((string-match "\\`[ \t]*\\(pro\\|function\\)[ \t]+[a-zA-Z0-9_]*\\'"
6470 match-string)
6471 (setq cw 'class))
6472 ((string-match
6473 "\\`[ \t]*\\([a-zA-Z][a-zA-Z0-9$_]*\\)?\\'"
6474 (if (> pro-point 0)
6475 (buffer-substring pro-point (point))
6476 match-string))
6477 (setq cw 'procedure cw-class pro-class cw-point pro-point
6478 cw-arrow pro-arrow))
6479 ((string-match "\\`[ \t]*\\(pro\\|function\\)\\>"
6480 match-string)
6481 nil)
6482 ((string-match "OBJ_NEW([ \t]*['\"]\\([a-zA-Z0-9$_]*\\)?\\'"
6483 match-string)
6484 (setq cw 'class))
6485 ((string-match "\\<inherits\\s-+\\([a-zA-Z0-9$_]*\\)?\\'"
6486 match-string)
6487 (setq cw 'class))
6488 ((and func
6489 (> func-point pro-point)
6490 (= func-level 1)
6491 (memq last-char '(?\( ?,)))
6492 (setq cw 'function-keyword cw-mod func cw-point func-point
6493 cw-class func-class cw-arrow func-arrow))
6494 ((and pro (eq last-char ?,))
6495 (setq cw 'procedure-keyword cw-mod pro cw-point pro-point
6496 cw-class pro-class cw-arrow pro-arrow))
6497 ; ((member last-char '(?\' ?\) ?\] ?!))
6498 ; ;; after these chars, a function makes no sense
6499 ; ;; FIXME: I am sure there can be more in this list
6500 ; ;; FIXME: Do we want to do this at all?
6501 ; nil)
6502 ;; Everywhere else we try a function.
6503 (t
6504 (setq cw 'function)
6505 (save-excursion
6506 (if (re-search-backward "->[ \t]*\\(\\$[ \t]*\\(;.*\\)?\n\\s-*\\)?\\(\\([$a-zA-Z0-9_]+\\)::\\)?[$a-zA-Z0-9_]*\\=" bos t)
6507 (setq cw-arrow (copy-marker (match-beginning 0))
6508 cw-class (if (match-end 4)
6509 (idlwave-sintern-class (match-string 4))
6510 t))))))
6511 (list (list pro pro-point pro-class pro-arrow)
6512 (list func func-point func-class func-arrow)
6513 cw
6514 (list cw-mod cw-point cw-class cw-arrow)
6515 last-char)))
6516
6517 (defun idlwave-this-word (&optional class)
6518 ;; Grab the word around point. CLASS is for the `skip-chars=...' functions
6519 (setq class (or class "a-zA-Z0-9$_."))
6520 (save-excursion
6521 (buffer-substring
6522 (progn (skip-chars-backward class) (point))
6523 (progn (skip-chars-forward class) (point)))))
6524
6525 (defun idlwave-what-function (&optional bound)
6526 ;; Find out if point is within the argument list of a function.
6527 ;; The return value is ("function-name" class arrow-start (point) level).
6528 ;; Level is 1 on the top level parentheses, higher further down.
6529
6530 ;; If the optional BOUND is an integer, bound backwards directed
6531 ;; searches to this point.
6532
6533 (catch 'exit
6534 (let (pos
6535 func-point
6536 (cnt 0)
6537 func arrow-start class)
6538 (idlwave-with-special-syntax
6539 (save-restriction
6540 (save-excursion
6541 (narrow-to-region (max 1 (or bound 0)) (point-max))
6542 ;; move back out of the current parenthesis
6543 (while (condition-case nil
6544 (progn (up-list -1) t)
6545 (error nil))
6546 (setq pos (point))
6547 (incf cnt)
6548 (when (and (= (following-char) ?\()
6549 (re-search-backward
6550 "\\(::\\|\\<\\)\\([a-zA-Z][a-zA-Z0-9$_]*\\)[ \t]*\\="
6551 bound t))
6552 (setq func (match-string 2)
6553 func-point (goto-char (match-beginning 2))
6554 pos func-point)
6555 (if (re-search-backward
6556 "->[ \t]*\\(\\([a-zA-Z][a-zA-Z0-9$_]*\\)::\\)?\\=" bound t)
6557 (setq arrow-start (copy-marker (match-beginning 0))
6558 class (or (match-string 2) t)))
6559 (throw
6560 'exit
6561 (list
6562 (idlwave-sintern-routine-or-method func class)
6563 (idlwave-sintern-class class)
6564 arrow-start func-point cnt)))
6565 (goto-char pos))
6566 (throw 'exit nil)))))))
6567
6568 (defun idlwave-what-procedure (&optional bound)
6569 ;; Find out if point is within the argument list of a procedure.
6570 ;; The return value is ("procedure-name" class arrow-pos (point)).
6571
6572 ;; If the optional BOUND is an integer, bound backwards directed
6573 ;; searches to this point.
6574 (let ((pos (point)) pro-point
6575 pro class arrow-start string)
6576 (save-excursion
6577 ;;(idlwave-beginning-of-statement)
6578 (idlwave-start-of-substatement 'pre)
6579 (setq string (buffer-substring (point) pos))
6580 (if (string-match
6581 "\\`[ \t]*\\([a-zA-Z][a-zA-Z0-9$_]*\\)[ \t]*\\(,\\|\\'\\)" string)
6582 (setq pro (match-string 1 string)
6583 pro-point (+ (point) (match-beginning 1)))
6584 (if (and (idlwave-skip-object)
6585 (setq string (buffer-substring (point) pos))
6586 (string-match
6587 "\\`[ \t]*\\(->\\)[ \t]*\\(\\([a-zA-Z][a-zA-Z0-9$_]*\\)::\\)?\\([a-zA-Z][a-zA-Z0-9$_]*\\)?[ \t]*\\(,\\|\\(\\$\\s *\\(;.*\\)?\\)?$\\)"
6588 string))
6589 (setq pro (if (match-beginning 4)
6590 (match-string 4 string))
6591 pro-point (if (match-beginning 4)
6592 (+ (point) (match-beginning 4))
6593 pos)
6594 arrow-start (copy-marker (+ (point) (match-beginning 1)))
6595 class (or (match-string 3 string) t)))))
6596 (list (idlwave-sintern-routine-or-method pro class)
6597 (idlwave-sintern-class class)
6598 arrow-start
6599 pro-point)))
6600
6601 (defun idlwave-skip-object ()
6602 ;; If there is an object at point, move over it and return t.
6603 (let ((pos (point)))
6604 (if (catch 'exit
6605 (save-excursion
6606 (skip-chars-forward " ") ; white space
6607 (skip-chars-forward "*") ; de-reference
6608 (cond
6609 ((looking-at idlwave-identifier)
6610 (goto-char (match-end 0)))
6611 ((eq (following-char) ?\()
6612 nil)
6613 (t (throw 'exit nil)))
6614 (catch 'endwhile
6615 (while t
6616 (cond ((eq (following-char) ?.)
6617 (forward-char 1)
6618 (if (not (looking-at idlwave-identifier))
6619 (throw 'exit nil))
6620 (goto-char (match-end 0)))
6621 ((memq (following-char) '(?\( ?\[))
6622 (condition-case nil
6623 (forward-list 1)
6624 (error (throw 'exit nil))))
6625 (t (throw 'endwhile t)))))
6626 (if (looking-at "[ \t]*->")
6627 (throw 'exit (setq pos (match-beginning 0)))
6628 (throw 'exit nil))))
6629 (goto-char pos)
6630 nil)))
6631
6632 (defun idlwave-last-valid-char ()
6633 "Return the last character before point which is not white or a comment
6634 and also not part of the current identifier. Since we do this in
6635 order to identify places where keywords are, we consider the initial
6636 `/' of a keyword as part of the identifier.
6637 This function is not general, can only be used for completion stuff."
6638 (catch 'exit
6639 (save-excursion
6640 ;; skip the current identifier
6641 (skip-chars-backward "a-zA-Z0-9_$")
6642 ;; also skip a leading slash which might be belong to the keyword
6643 (if (eq (preceding-char) ?/)
6644 (backward-char 1))
6645 ;; FIXME: does not check if this is a valid identifier
6646 (while t
6647 (skip-chars-backward " \t")
6648 (cond
6649 ((memq (preceding-char) '(?\; ?\$)) (throw 'exit nil))
6650 ((eq (preceding-char) ?\n)
6651 (beginning-of-line 0)
6652 (if (looking-at "\\([^\n]*\\)\\$[ \t]*\\(;[^\n]*\\)?\n")
6653 ;; continuation line
6654 (goto-char (match-end 1))
6655 (throw 'exit nil)))
6656 (t (throw 'exit (preceding-char))))))))
6657
6658 (defvar idlwave-complete-after-success-form nil
6659 "A form to evaluate after successful completion.")
6660 (defvar idlwave-complete-after-success-form-force nil
6661 "A form to evaluate after completion selection in *Completions* buffer.")
6662 (defconst idlwave-completion-mark (make-marker)
6663 "A mark pointing to the beginning of the completion string.")
6664 (defvar completion-highlight-first-word-only) ;XEmacs.
6665
6666 (defun idlwave-complete-in-buffer (type stype list selector prompt isa
6667 &optional prepare-display-function
6668 special-selector)
6669 "Perform TYPE completion of word before point against LIST.
6670 SELECTOR is the PREDICATE argument for the completion function. Show
6671 PROMPT in echo area. TYPE is one of the intern types, e.g. 'function,
6672 'procedure, 'class-tag, 'keyword, 'sysvar, etc. SPECIAL-SELECTOR is
6673 used only once, for `all-completions', and can be used to, e.g.,
6674 accumulate information on matching completions."
6675 (let* ((completion-ignore-case t)
6676 beg (end (point)) slash part spart completion all-completions
6677 dpart dcompletion)
6678
6679 (unless list
6680 (error (concat prompt ": No completions available")))
6681
6682 ;; What is already in the buffer?
6683 (save-excursion
6684 (skip-chars-backward "a-zA-Z0-9_$")
6685 (setq slash (eq (preceding-char) ?/)
6686 beg (point)
6687 idlwave-complete-after-success-form
6688 (list 'idlwave-after-successful-completion
6689 (list 'quote type) slash beg)
6690 idlwave-complete-after-success-form-force
6691 (list 'idlwave-after-successful-completion
6692 (list 'quote type) slash (list 'quote 'force))))
6693
6694 ;; Try a completion
6695 (setq part (buffer-substring beg end)
6696 dpart (downcase part)
6697 spart (idlwave-sintern stype part)
6698 completion (try-completion part list selector)
6699 dcompletion (if (stringp completion) (downcase completion))
6700 idlwave-completion-help-links nil)
6701 (cond
6702 ((null completion)
6703 ;; nothing available.
6704 (error (concat prompt ": no completion for \"%s\"") part))
6705 ((and (not (equal dpart dcompletion))
6706 (not (eq t completion)))
6707 ;; We can add something
6708 (delete-region beg end)
6709 (insert (if (and (string= part dpart)
6710 (or (not (string= part ""))
6711 idlwave-complete-empty-string-as-lower-case)
6712 (not idlwave-completion-force-default-case))
6713 dcompletion
6714 completion))
6715 (if (eq t (try-completion completion list selector))
6716 ;; Now this is a unique match
6717 (idlwave-after-successful-completion type slash beg))
6718 t)
6719 ((or (eq completion t)
6720 (and (= 1 (length (setq all-completions
6721 (idlwave-uniquify
6722 (all-completions part list
6723 (or special-selector
6724 selector))))))
6725 (equal dpart dcompletion)))
6726 ;; This is already complete
6727 (idlwave-after-successful-completion type slash beg)
6728 (message "%s is already the complete %s" part isa)
6729 nil)
6730 (t
6731 ;; We cannot add something - offer a list.
6732 (message "Making completion list...")
6733
6734 (unless idlwave-completion-help-links ; already set somewhere?
6735 (mapc (lambda (x) ; Pass link prop through to highlight-linked
6736 (let ((link (get-text-property 0 'link (car x))))
6737 (if link
6738 (push (cons (car x) link)
6739 idlwave-completion-help-links))))
6740 list))
6741 (let* ((list all-completions)
6742 ;; "complete" means, this is already a valid completion
6743 (complete (memq spart all-completions))
6744 (completion-highlight-first-word-only t)) ; XEmacs
6745 ;; (completion-fixup-function ; Emacs
6746 ;; (lambda () (and (eq (preceding-char) ?>)
6747 ;; (re-search-backward " <" beg t)))))
6748
6749 (setq list (sort list (lambda (a b)
6750 (string< (downcase a) (downcase b)))))
6751 (if prepare-display-function
6752 (setq list (funcall prepare-display-function list)))
6753 (if (and (string= part dpart)
6754 (or (not (string= part ""))
6755 idlwave-complete-empty-string-as-lower-case)
6756 (not idlwave-completion-force-default-case))
6757 (setq list (mapcar (lambda (x)
6758 (if (listp x)
6759 (setcar x (downcase (car x)))
6760 (setq x (downcase x)))
6761 x)
6762 list)))
6763 (idlwave-display-completion-list list prompt beg complete))
6764 t))))
6765
6766 (defun idlwave-complete-class ()
6767 "Complete a class at point."
6768 (interactive)
6769 ;; Call `idlwave-routines' to make sure the class list will be available
6770 (idlwave-routines)
6771 ;; Check for the special case of completing empty string after pro/function
6772 (if (let ((case-fold-search t))
6773 (save-excursion
6774 (and
6775 (re-search-backward "\\<\\(pro\\|function\\)[ \t]+\\="
6776 (- (point) 15) t)
6777 (goto-char (point-min))
6778 (re-search-forward
6779 "^[ \t]*\\(pro\\|function\\)[ \t]+\\([a-zA-Z0-9_]+::\\)" nil t))))
6780 ;; Yank the full class specification
6781 (insert (match-string 2))
6782 ;; Do the completion, using list gathered from `idlwave-routines'
6783 (idlwave-complete-in-buffer
6784 'class 'class (idlwave-class-alist) nil
6785 "Select a class" "class"
6786 (lambda (list) ;; Push it to help-links if system help available
6787 (mapcar (lambda (x)
6788 (let* ((entry (idlwave-class-info x))
6789 (link (nth 1 (assq 'link entry))))
6790 (if link (push (cons x link)
6791 idlwave-completion-help-links))
6792 x))
6793 list)))))
6794
6795 (defun idlwave-attach-classes (list type show-classes)
6796 ;; Attach the proper class list to a LIST of completion items.
6797 ;; TYPE, when 'kwd, shows classes for method keywords, when
6798 ;; 'class-tag, for class tags, and otherwise for methods.
6799 ;; SHOW-CLASSES is the value of `idlwave-completion-show-classes'.
6800 (if (or (null show-classes) ; don't want to see classes
6801 (null class-selector) ; not a method call
6802 (and
6803 (stringp class-selector) ; the class is already known
6804 (not super-classes))) ; no possibilities for inheritance
6805 ;; In these cases, we do not have to do anything
6806 list
6807 (let* ((do-prop (and (>= show-classes 0)
6808 (>= emacs-major-version 21)))
6809 (do-buf (not (= show-classes 0)))
6810 ;; (do-dots (featurep 'xemacs))
6811 (do-dots t)
6812 (inherit (if (and (not (eq type 'class-tag)) super-classes)
6813 (cons class-selector super-classes)))
6814 (max (abs show-classes))
6815 (lmax (if do-dots (apply 'max (mapcar 'length list))))
6816 classes nclasses class-info space)
6817 (mapcar
6818 (lambda (x)
6819 ;; get the classes
6820 (if (eq type 'class-tag)
6821 ;; Just one class for tags
6822 (setq classes
6823 (list
6824 (idlwave-class-or-superclass-with-tag class-selector x)))
6825 ;; Multiple classes for method or method-keyword
6826 (setq classes
6827 (if (eq type 'kwd)
6828 (idlwave-all-method-keyword-classes
6829 method-selector x type-selector)
6830 (idlwave-all-method-classes x type-selector)))
6831 (if inherit
6832 (setq classes
6833 (delq nil
6834 (mapcar (lambda (x) (if (memq x inherit) x nil))
6835 classes)))))
6836 (setq nclasses (length classes))
6837 ;; Make the separator between item and class-info
6838 (if do-dots
6839 (setq space (concat " " (make-string (- lmax (length x)) ?.)))
6840 (setq space " "))
6841 (if do-buf
6842 ;; We do want info in the buffer
6843 (if (<= nclasses max)
6844 (setq class-info (concat
6845 space
6846 "<" (mapconcat 'identity classes ",") ">"))
6847 (setq class-info (format "%s<%d classes>" space nclasses)))
6848 (setq class-info nil))
6849 (when do-prop
6850 ;; We do want properties
6851 (setq x (copy-sequence x))
6852 (put-text-property 0 (length x)
6853 'help-echo (mapconcat 'identity classes " ")
6854 x))
6855 (if class-info
6856 (list x class-info)
6857 x))
6858 list))))
6859
6860 (defun idlwave-attach-method-classes (list)
6861 ;; Call idlwave-attach-classes with method parameters
6862 (idlwave-attach-classes list 'method idlwave-completion-show-classes))
6863 (defun idlwave-attach-keyword-classes (list)
6864 ;; Call idlwave-attach-classes with keyword parameters
6865 (idlwave-attach-classes list 'kwd idlwave-completion-show-classes))
6866 (defun idlwave-attach-class-tag-classes (list)
6867 ;; Call idlwave-attach-classes with class structure tags
6868 (idlwave-attach-classes list 'class-tag idlwave-completion-show-classes))
6869
6870
6871 ;;----------------------------------------------------------------------
6872 ;;----------------------------------------------------------------------
6873 ;;----------------------------------------------------------------------
6874 ;;----------------------------------------------------------------------
6875 ;;----------------------------------------------------------------------
6876 (when (featurep 'xemacs)
6877 (defvar rtn)
6878 (defun idlwave-pset (item)
6879 (set 'rtn item)))
6880
6881 (defun idlwave-popup-select (ev list title &optional sort)
6882 "Select an item in LIST with a popup menu.
6883 TITLE is the title to put atop the popup. If SORT is non-nil,
6884 sort the list before displaying."
6885 (let ((maxpopup idlwave-max-popup-menu-items)
6886 rtn menu)
6887 (cond ((null list))
6888 ((= 1 (length list))
6889 (setq rtn (car list)))
6890 ((featurep 'xemacs)
6891 (if sort (setq list (sort list (lambda (a b)
6892 (string< (upcase a) (upcase b))))))
6893 (setq menu
6894 (append (list title)
6895 (mapcar (lambda (x) (vector x (list 'idlwave-pset
6896 x)))
6897 list)))
6898 (setq menu (idlwave-split-menu-xemacs menu maxpopup))
6899 (let ((resp (get-popup-menu-response menu)))
6900 (funcall (event-function resp) (event-object resp))))
6901 (t
6902 (if sort (setq list (sort list (lambda (a b)
6903 (string< (upcase a) (upcase b))))))
6904 (setq menu (cons title
6905 (list
6906 (append (list "")
6907 (mapcar (lambda(x) (cons x x)) list)))))
6908 (setq menu (idlwave-split-menu-emacs menu maxpopup))
6909 (setq rtn (x-popup-menu ev menu))))
6910 rtn))
6911
6912 (defun idlwave-split-menu-xemacs (menu N)
6913 "Split the MENU into submenus of maximum length N."
6914 (if (<= (length menu) (1+ N))
6915 ;; No splitting needed
6916 menu
6917 (let* ((title (car menu))
6918 (entries (cdr menu))
6919 (menu (list title))
6920 (cnt 0)
6921 (nextmenu nil))
6922 (while entries
6923 (while (and entries (< cnt N))
6924 (setq cnt (1+ cnt)
6925 nextmenu (cons (car entries) nextmenu)
6926 entries (cdr entries)))
6927 (setq nextmenu (nreverse nextmenu))
6928 (setq nextmenu (cons (format "%s...%s"
6929 (aref (car nextmenu) 0)
6930 (aref (nth (1- cnt) nextmenu) 0))
6931 nextmenu))
6932 (setq menu (cons nextmenu menu)
6933 nextmenu nil
6934 cnt 0))
6935 (nreverse menu))))
6936
6937 (defun idlwave-split-menu-emacs (menu N)
6938 "Split the MENU into submenus of maximum length N."
6939 (if (<= (length (nth 1 menu)) (1+ N))
6940 ;; No splitting needed
6941 menu
6942 (let* ((title (car menu))
6943 (entries (cdr (nth 1 menu)))
6944 (menu nil)
6945 (cnt 0)
6946 (nextmenu nil))
6947 (while entries
6948 (while (and entries (< cnt N))
6949 (setq cnt (1+ cnt)
6950 nextmenu (cons (car entries) nextmenu)
6951 entries (cdr entries)))
6952 (setq nextmenu (nreverse nextmenu))
6953 (prin1 nextmenu)
6954 (setq nextmenu (cons (format "%s...%s"
6955 (car (car nextmenu))
6956 (car (nth (1- cnt) nextmenu)))
6957 nextmenu))
6958 (setq menu (cons nextmenu menu)
6959 nextmenu nil
6960 cnt 0))
6961 (setq menu (nreverse menu))
6962 (setq menu (cons title menu))
6963 menu)))
6964
6965 (defvar idlwave-completion-setup-hook nil)
6966
6967 (defun idlwave-scroll-completions (&optional message)
6968 "Scroll the completion window on this frame."
6969 (let ((cwin (get-buffer-window "*Completions*" 'visible))
6970 (win (selected-window)))
6971 (unwind-protect
6972 (progn
6973 (select-window cwin)
6974 (condition-case nil
6975 (scroll-up)
6976 (error (if (and (listp last-command)
6977 (nth 2 last-command))
6978 (progn
6979 (select-window win)
6980 (eval idlwave-complete-after-success-form))
6981 (set-window-start cwin (point-min)))))
6982 (and message (message "%s" message)))
6983 (select-window win))))
6984
6985 (defun idlwave-display-completion-list (list &optional message beg complete)
6986 "Display the completions in LIST in the completions buffer and echo MESSAGE."
6987 (unless (and (get-buffer-window "*Completions*")
6988 (idlwave-local-value 'idlwave-completion-p "*Completions*"))
6989 (move-marker idlwave-completion-mark beg)
6990 (setq idlwave-before-completion-wconf (current-window-configuration)))
6991
6992 (if (featurep 'xemacs)
6993 (idlwave-display-completion-list-xemacs
6994 list)
6995 (idlwave-display-completion-list-emacs list))
6996
6997 ;; Store a special value in `this-command'. When `idlwave-complete'
6998 ;; finds this in `last-command', it will scroll the *Completions* buffer.
6999 (setq this-command (list 'idlwave-display-completion-list message complete))
7000
7001 ;; Mark the completions buffer as created by cib
7002 (idlwave-set-local 'idlwave-completion-p t "*Completions*")
7003
7004 ;; Fontify the classes
7005 (if (and idlwave-completion-fontify-classes
7006 (consp (car list)))
7007 (idlwave-completion-fontify-classes))
7008
7009 ;; Run the hook
7010 (run-hooks 'idlwave-completion-setup-hook)
7011
7012 ;; Display the message
7013 (message "%s" (or message "Making completion list...done")))
7014
7015 (defun idlwave-choose (function &rest args)
7016 "Call FUNCTION as a completion chooser and pass ARGS to it."
7017 (let ((completion-ignore-case t)) ; install correct value
7018 (apply function args))
7019 (if (and (derived-mode-p 'idlwave-shell-mode)
7020 (boundp 'font-lock-mode)
7021 (not font-lock-mode))
7022 ;; For the shell, remove the fontification of the word before point
7023 (let ((beg (save-excursion
7024 (skip-chars-backward "a-zA-Z0-9_")
7025 (point))))
7026 (remove-text-properties beg (point) '(face nil))))
7027 (eval idlwave-complete-after-success-form-force))
7028
7029 (defun idlwave-keyboard-quit ()
7030 (interactive)
7031 (unwind-protect
7032 (if (eq (car-safe last-command) 'idlwave-display-completion-list)
7033 (idlwave-restore-wconf-after-completion))
7034 (keyboard-quit)))
7035
7036 (defun idlwave-restore-wconf-after-completion ()
7037 "Restore the old (before completion) window configuration."
7038 (and idlwave-completion-restore-window-configuration
7039 idlwave-before-completion-wconf
7040 (set-window-configuration idlwave-before-completion-wconf)))
7041
7042 (defun idlwave-one-key-select (sym prompt delay)
7043 "Make the user select an element from the alist in the variable SYM.
7044 The keys of the alist are expected to be strings. The function returns the
7045 car of the selected association.
7046 To do this, PROMPT is displayed and the user must hit a letter key to
7047 select an entry. If the user does not reply within DELAY seconds, a help
7048 window with the options is displayed automatically.
7049 The key which is associated with each option is generated automatically.
7050 First, the strings are checked for preselected keys, like in \"[P]rint\".
7051 If these don't exist, a letter in the string is automatically selected."
7052 (let* ((alist (symbol-value sym))
7053 (temp-buffer-show-hook (if (fboundp 'fit-window-to-buffer)
7054 '(fit-window-to-buffer)))
7055 keys-alist char)
7056 ;; First check the cache
7057 (if (and (eq (symbol-value sym) (get sym :one-key-alist-last)))
7058 (setq keys-alist (get sym :one-key-alist-cache))
7059 ;; Need to make new list
7060 (setq keys-alist (idlwave-make-one-key-alist alist))
7061 (put sym :one-key-alist-cache keys-alist)
7062 (put sym :one-key-alist-last alist))
7063 ;; Display prompt and wait for quick reply
7064 (message "%s[%s]" prompt
7065 (mapconcat (lambda(x) (char-to-string (car x)))
7066 keys-alist ""))
7067 (if (sit-for delay)
7068 ;; No quick reply: Show help
7069 (save-window-excursion
7070 (with-output-to-temp-buffer "*Completions*"
7071 (mapcar (lambda(x)
7072 (princ (nth 1 x))
7073 (princ "\n"))
7074 keys-alist))
7075 (setq char (read-char)))
7076 (setq char (read-char)))
7077 (message nil)
7078 ;; Return the selected result
7079 (nth 2 (assoc char keys-alist))))
7080
7081 ;; Used for, e.g., electric debug super-examine.
7082 (defun idlwave-make-one-key-alist (alist)
7083 "Make an alist for single key selection."
7084 (let ((l alist) keys-alist name start char help
7085 (cnt 0)
7086 (case-fold-search nil))
7087 (while l
7088 (setq name (car (car l))
7089 l (cdr l))
7090 (catch 'exit
7091 ;; First check if the configuration predetermined a key
7092 (if (string-match "\\[\\(.\\)\\]" name)
7093 (progn
7094 (setq char (string-to-char (downcase (match-string 1 name)))
7095 help (format "%c: %s" char name)
7096 keys-alist (cons (list char help name) keys-alist))
7097 (throw 'exit t)))
7098 ;; Then check for capital letters
7099 (setq start 0)
7100 (while (string-match "[A-Z]" name start)
7101 (setq start (match-end 0)
7102 char (string-to-char (downcase (match-string 0 name))))
7103 (if (not (assoc char keys-alist))
7104 (progn
7105 (setq help (format "%c: %s" char
7106 (replace-match
7107 (concat "[" (match-string 0 name) "]")
7108 t t name))
7109 keys-alist (cons (list char help name) keys-alist))
7110 (throw 'exit t))))
7111 ;; Now check for lowercase letters
7112 (setq start 0)
7113 (while (string-match "[a-z]" name start)
7114 (setq start (match-end 0)
7115 char (string-to-char (match-string 0 name)))
7116 (if (not (assoc char keys-alist))
7117 (progn
7118 (setq help (format "%c: %s" char
7119 (replace-match
7120 (concat "[" (match-string 0 name) "]")
7121 t t name))
7122 keys-alist (cons (list char help name) keys-alist))
7123 (throw 'exit t))))
7124 ;; Bummer, nothing found! Use a stupid number
7125 (setq char (string-to-char (int-to-string (setq cnt (1+ cnt))))
7126 help (format "%c: %s" char name)
7127 keys-alist (cons (list char help name) keys-alist))))
7128 (nreverse keys-alist)))
7129
7130 (defun idlwave-set-local (var value &optional buffer)
7131 "Set the buffer-local value of VAR in BUFFER to VALUE."
7132 (with-current-buffer (or buffer (current-buffer))
7133 (set (make-local-variable var) value)))
7134
7135 (defun idlwave-local-value (var &optional buffer)
7136 "Return the value of VAR in BUFFER, but only if VAR is local to BUFFER."
7137 (with-current-buffer (or buffer (current-buffer))
7138 (and (local-variable-p var (current-buffer))
7139 (symbol-value var))))
7140
7141 ;; In XEmacs, we can use :activate-callback directly to advice the
7142 ;; choose functions. We use the private keymap only for the online
7143 ;; help feature.
7144
7145 (defvar idlwave-completion-map nil
7146 "Keymap for `completion-list-mode' with `idlwave-complete'.")
7147
7148 (defun idlwave-display-completion-list-xemacs (list &rest cl-args)
7149 (with-output-to-temp-buffer "*Completions*"
7150 (apply 'display-completion-list list
7151 ':activate-callback 'idlwave-default-choose-completion
7152 cl-args))
7153 (with-current-buffer "*Completions*"
7154 (use-local-map
7155 (or idlwave-completion-map
7156 (setq idlwave-completion-map
7157 (idlwave-make-modified-completion-map-xemacs
7158 (current-local-map)))))))
7159
7160 (defun idlwave-default-choose-completion (&rest args)
7161 "Execute `default-choose-completion' and then restore the win-conf."
7162 (apply 'idlwave-choose 'default-choose-completion args))
7163
7164 (defun idlwave-make-modified-completion-map-xemacs (old-map)
7165 "Replace `choose-completion' and `mouse-choose-completion' in OLD-MAP."
7166 (let ((new-map (copy-keymap old-map)))
7167 (define-key new-map [button3up] 'idlwave-mouse-completion-help)
7168 (define-key new-map [button3] (lambda ()
7169 (interactive)
7170 (setq this-command last-command)))
7171 new-map))
7172
7173 ;; In Emacs we also replace keybindings in the completion
7174 ;; map in order to install our wrappers.
7175
7176 (defun idlwave-display-completion-list-emacs (list)
7177 "Display completion list and install the choose wrappers."
7178 (with-output-to-temp-buffer "*Completions*"
7179 (display-completion-list list))
7180 (with-current-buffer "*Completions*"
7181 (use-local-map
7182 (or idlwave-completion-map
7183 (setq idlwave-completion-map
7184 (idlwave-make-modified-completion-map-emacs
7185 (current-local-map)))))))
7186
7187 (defun idlwave-make-modified-completion-map-emacs (old-map)
7188 "Replace `choose-completion' and `mouse-choose-completion' in OLD-MAP."
7189 (let ((new-map (copy-keymap old-map)))
7190 (substitute-key-definition
7191 'choose-completion 'idlwave-choose-completion new-map)
7192 (substitute-key-definition
7193 'mouse-choose-completion 'idlwave-mouse-choose-completion new-map)
7194 (define-key new-map [mouse-3] 'idlwave-mouse-completion-help)
7195 new-map))
7196
7197 (defun idlwave-choose-completion (&rest args)
7198 "Choose the completion that point is in or next to."
7199 (interactive)
7200 (apply 'idlwave-choose 'choose-completion args))
7201
7202 (defun idlwave-mouse-choose-completion (&rest args)
7203 "Click on an alternative in the `*Completions*' buffer to choose it."
7204 (interactive "e")
7205 (apply 'idlwave-choose 'mouse-choose-completion args))
7206
7207 ;;----------------------------------------------------------------------
7208 ;;----------------------------------------------------------------------
7209
7210 ;;; ------------------------------------------------------------------------
7211 ;;; Stucture parsing code, and code to manage class info
7212
7213 ;;
7214 ;; - Go again over the documentation how to write a completion
7215 ;; plugin. It is in self.el, but currently still very bad.
7216 ;; This could be in a separate file in the distribution, or
7217 ;; in an appendix for the manual.
7218
7219 (defvar idlwave-struct-skip
7220 "[ \t]*\\(\\$.*\n\\(^[ \t]*\\(\\$[ \t]*\\)?\\(;.*\\)?\n\\)*\\)?[ \t]*"
7221 "Regexp for skipping continued blank or comment-only lines in structures.")
7222
7223 (defvar idlwave-struct-tag-regexp
7224 (concat "[{,]" ;leading comma/brace
7225 idlwave-struct-skip ; 4 groups
7226 "\\([a-zA-Z][a-zA-Z0-9_]*\\)" ;the tag itself, group 5
7227 "[ \t]*:") ; the final colon
7228 "Regexp for structure tags.")
7229
7230 (defun idlwave-struct-tags ()
7231 "Return a list of all tags in the structure defined at point.
7232 Point is expected just before the opening `{' of the struct definition."
7233 (save-excursion
7234 (let* ((borders (idlwave-struct-borders))
7235 (beg (car borders))
7236 (end (cdr borders))
7237 tags)
7238 (goto-char beg)
7239 (save-restriction
7240 (narrow-to-region beg end)
7241 (while (re-search-forward idlwave-struct-tag-regexp end t)
7242 ;; Check if we are still on the top level of the structure.
7243 (if (and (condition-case nil (progn (up-list -1) t) (error nil))
7244 (= (point) beg))
7245 (push (match-string-no-properties 5) tags))
7246 (goto-char (match-end 0))))
7247 (nreverse tags))))
7248
7249 (defun idlwave-find-struct-tag (tag)
7250 "Find a given TAG in the structure defined at point."
7251 (let* ((borders (idlwave-struct-borders))
7252 (end (cdr borders))
7253 (case-fold-search t))
7254 (re-search-forward (concat "\\(^[ \t]*\\|[,{][ \t]*\\)" tag "[ \t]*:")
7255 end t)))
7256
7257 (defun idlwave-struct-inherits ()
7258 "Return a list of all `inherits' names in the struct at point.
7259 Point is expected just before the opening `{' of the struct definition."
7260 (save-excursion
7261 (let* ((borders (idlwave-struct-borders))
7262 (beg (car borders))
7263 (end (cdr borders))
7264 (case-fold-search t)
7265 names)
7266 (goto-char beg)
7267 (save-restriction
7268 (narrow-to-region beg end)
7269 (while (re-search-forward
7270 (concat "[{,]" ;leading comma/brace
7271 idlwave-struct-skip ; 4 groups
7272 "inherits" ; The INHERITS tag
7273 idlwave-struct-skip ; 4 more
7274 "\\([a-zA-Z][a-zA-Z0-9_]*\\)") ; The super-group, #9
7275 end t)
7276 ;; Check if we are still on the top level of the structure.
7277 (if (and (condition-case nil (progn (up-list -1) t) (error nil))
7278 (= (point) beg))
7279 (push (match-string-no-properties 9) names))
7280 (goto-char (match-end 0))))
7281 (nreverse names))))
7282
7283 (defun idlwave-in-structure ()
7284 "Return t if point is inside an IDL structure definition."
7285 (let ((beg (point)))
7286 (save-excursion
7287 (if (not (or (idlwave-in-comment) (idlwave-in-quote)))
7288 (if (idlwave-find-structure-definition nil nil 'back)
7289 (let ((borders (idlwave-struct-borders)))
7290 (or (= (car borders) (cdr borders)) ;; struct not yet closed...
7291 (and (> beg (car borders)) (< beg (cdr borders))))))))))
7292
7293 (defun idlwave-struct-borders ()
7294 "Return the borders of the {...} after point as a cons cell."
7295 (let (beg)
7296 (save-excursion
7297 (skip-chars-forward "^{")
7298 (setq beg (point))
7299 (condition-case nil (forward-list 1)
7300 (error (goto-char beg)))
7301 (cons beg (point)))))
7302
7303 (defun idlwave-find-structure-definition (&optional var name bound)
7304 "Search forward for a structure definition.
7305 If VAR is non-nil, search for a structure assigned to variable VAR.
7306 If NAME is non-nil, search for a named structure NAME, if a string,
7307 or a generic named structure otherwise. If BOUND is an integer, limit
7308 the search. If BOUND is the symbol `all', we search first back and
7309 then forward through the entire file. If BOUND is the symbol `back'
7310 we search only backward."
7311 (let* ((ws "[ \t]*\\(\\$.*\n[ \t]*\\)*")
7312 (case-fold-search t)
7313 (lim (if (integerp bound) bound nil))
7314 (re (concat
7315 (if var
7316 (concat "\\<" (regexp-quote (downcase var)) "\\>" ws)
7317 "\\(\\)")
7318 "=" ws "\\({\\)"
7319 (if name
7320 (if (stringp name)
7321 (concat ws "\\(\\<" (downcase name) "\\)[^a-zA-Z0-9_$]")
7322 ;; Just a generic name
7323 (concat ws "\\<\\([a-zA-Z_0-9$]+\\)" ws ","))
7324 ""))))
7325 (if (or (and (or (eq bound 'all) (eq bound 'back))
7326 (re-search-backward re nil t))
7327 (and (not (eq bound 'back)) (re-search-forward re lim t)))
7328 (progn
7329 (goto-char (match-beginning 3))
7330 (match-string-no-properties 5)))))
7331
7332 (defvar idlwave-class-info nil)
7333 (defvar idlwave-class-reset nil) ; to reset buffer-local classes
7334
7335 (add-hook 'idlwave-update-rinfo-hook
7336 (lambda () (setq idlwave-class-reset t)))
7337 (add-hook 'idlwave-after-load-rinfo-hook
7338 (lambda () (setq idlwave-class-info nil)))
7339
7340 (defun idlwave-class-info (class)
7341 (let (list entry)
7342 (if idlwave-class-info
7343 (if idlwave-class-reset
7344 (setq
7345 idlwave-class-reset nil
7346 idlwave-class-info ; Remove any visited in a buffer
7347 (delq nil (mapcar
7348 (lambda (x)
7349 (let ((filebuf
7350 (idlwave-class-file-or-buffer
7351 (or (cdr (assq 'found-in x)) (car x)))))
7352 (if (cdr filebuf)
7353 nil
7354 x)))
7355 idlwave-class-info))))
7356 ;; Info is nil, put in the system stuff to start.
7357 (setq idlwave-class-info idlwave-system-class-info)
7358 (setq list idlwave-class-info)
7359 (while (setq entry (pop list))
7360 (idlwave-sintern-class-info entry)))
7361 (setq class (idlwave-sintern-class class))
7362 (or (assq class idlwave-class-info)
7363 (progn (idlwave-scan-class-info class)
7364 (assq class idlwave-class-info)))))
7365
7366 (defun idlwave-sintern-class-info (entry)
7367 "Sintern the class names in a class-info entry."
7368 (let ((inherits (assq 'inherits entry)))
7369 (setcar entry (idlwave-sintern-class (car entry) 'set))
7370 (if inherits
7371 (setcdr inherits (mapcar (lambda (x) (idlwave-sintern-class x 'set))
7372 (cdr inherits))))))
7373
7374 (defun idlwave-find-class-definition (class &optional all-hook alt-class)
7375 "Find class structure definition(s).
7376 If ALL-HOOK is set, find all named structure definitions in a given
7377 class__define routine, on which ALL-HOOK will be run. If ALT-CLASS is
7378 set, look for the name__define pro, and inside of it, for the ALT-CLASS
7379 class/struct definition."
7380 (let ((case-fold-search t) end-lim name)
7381 (when (re-search-forward
7382 (concat "^[ \t]*pro[ \t]+" (downcase class) "__define" "\\>") nil t)
7383 (if all-hook
7384 (progn
7385 ;; For everything there
7386 (setq end-lim (save-excursion (idlwave-end-of-subprogram) (point)))
7387 (while (setq name
7388 (idlwave-find-structure-definition nil t end-lim))
7389 (funcall all-hook name)))
7390 (idlwave-find-structure-definition nil (or alt-class class))))))
7391
7392
7393 (defun idlwave-class-file-or-buffer (class)
7394 "Find buffer visiting CLASS definition."
7395 (let* ((pro (concat (downcase class) "__define"))
7396 (file (idlwave-routine-source-file
7397 (nth 3 (idlwave-rinfo-assoc pro 'pro nil
7398 (idlwave-routines))))))
7399 (cons file (if file (idlwave-get-buffer-visiting file)))))
7400
7401
7402 (defun idlwave-scan-class-info (class)
7403 "Scan all class and named structure info in the class__define pro."
7404 (let* ((idlwave-auto-routine-info-updates nil)
7405 (filebuf (idlwave-class-file-or-buffer class))
7406 (file (car filebuf))
7407 (buf (cdr filebuf))
7408 (class (idlwave-sintern-class class)))
7409 (if (or
7410 (not file)
7411 (and ;; neither a regular file nor a visited buffer
7412 (not buf)
7413 (not (file-regular-p file))))
7414 nil ; Cannot find the file/buffer to get any info
7415 (save-excursion
7416 (if buf (set-buffer buf)
7417 ;; Read the file in temporarily
7418 (set-buffer (get-buffer-create " *IDLWAVE-tmp*"))
7419 (erase-buffer)
7420 (unless (derived-mode-p 'idlwave-mode)
7421 (idlwave-mode))
7422 (insert-file-contents file))
7423 (save-excursion
7424 (goto-char 1)
7425 (idlwave-find-class-definition class
7426 ;; Scan all of the structures found there
7427 (lambda (name)
7428 (let* ((this-class (idlwave-sintern-class name))
7429 (entry
7430 (list this-class
7431 (cons 'tags (idlwave-struct-tags))
7432 (cons 'inherits (idlwave-struct-inherits)))))
7433 (if (not (eq this-class class))
7434 (setq entry (nconc entry (list (cons 'found-in class)))))
7435 (idlwave-sintern-class-info entry)
7436 (push entry idlwave-class-info)))))))))
7437
7438 (defun idlwave-class-found-in (class)
7439 "Return the FOUND-IN property of the CLASS."
7440 (cdr (assq 'found-in (idlwave-class-info class))))
7441 (defun idlwave-class-tags (class)
7442 "Return the native tags in CLASS."
7443 (cdr (assq 'tags (idlwave-class-info class))))
7444 (defun idlwave-class-inherits (class)
7445 "Return the direct superclasses of CLASS."
7446 (cdr (assq 'inherits (idlwave-class-info class))))
7447
7448
7449 (defun idlwave-all-class-tags (class)
7450 "Return a list of native and inherited tags in CLASS."
7451 (condition-case err
7452 (apply 'append (mapcar 'idlwave-class-tags
7453 (cons class (idlwave-all-class-inherits class))))
7454 (error
7455 (idlwave-class-tag-reset)
7456 (error "%s" (error-message-string err)))))
7457
7458
7459 (defun idlwave-all-class-inherits (class)
7460 "Return a list of all superclasses of CLASS (recursively expanded).
7461 The list is cached in `idlwave-class-info' for faster access."
7462 (cond
7463 ((not idlwave-support-inheritance) nil)
7464 ((eq class nil) nil)
7465 ((eq class t) nil)
7466 (t
7467 (let ((info (idlwave-class-info class))
7468 entry)
7469 (if (setq entry (assq 'all-inherits info))
7470 (cdr entry)
7471 ;; Save the depth of inheritance scan to check for circular references
7472 (let ((inherits (mapcar (lambda (x) (cons x 0))
7473 (idlwave-class-inherits class)))
7474 rtn all-inherits cl)
7475 (while inherits
7476 (setq cl (pop inherits)
7477 rtn (cons (car cl) rtn)
7478 inherits (append (mapcar (lambda (x)
7479 (cons x (1+ (cdr cl))))
7480 (idlwave-class-inherits (car cl)))
7481 inherits))
7482 (if (> (cdr cl) 999)
7483 (error
7484 "Class scan: inheritance depth exceeded. Circular inheritance?")
7485 ))
7486 (setq all-inherits (nreverse rtn))
7487 (nconc info (list (cons 'all-inherits all-inherits)))
7488 all-inherits))))))
7489
7490 (defun idlwave-entry-keywords (entry &optional record-link)
7491 "Return the flat entry keywords alist from routine-info entry.
7492 If RECORD-LINK is non-nil, the keyword text is copied and a text
7493 property indicating the link is added."
7494 (let (kwds)
7495 (mapc
7496 (lambda (key-list)
7497 (let ((file (car key-list)))
7498 (mapcar (lambda (key-cons)
7499 (let ((key (car key-cons))
7500 (link (cdr key-cons)))
7501 (when (and record-link file)
7502 (setq key (copy-sequence key))
7503 (put-text-property
7504 0 (length key)
7505 'link
7506 (concat
7507 file
7508 (if link
7509 (concat idlwave-html-link-sep
7510 (number-to-string link))))
7511 key))
7512 (push (list key) kwds)))
7513 (cdr key-list))))
7514 (nthcdr 5 entry))
7515 (nreverse kwds)))
7516
7517 (defun idlwave-entry-find-keyword (entry keyword)
7518 "Find keyword KEYWORD in entry ENTRY, and return (with link) if set."
7519 (catch 'exit
7520 (mapc
7521 (lambda (key-list)
7522 (let ((file (car key-list))
7523 (kwd (assoc keyword (cdr key-list))))
7524 (when kwd
7525 (setq kwd (cons (car kwd)
7526 (if (and file (cdr kwd))
7527 (concat file
7528 idlwave-html-link-sep
7529 (number-to-string (cdr kwd)))
7530 (cdr kwd))))
7531 (throw 'exit kwd))))
7532 (nthcdr 5 entry))))
7533
7534 ;;==========================================================================
7535 ;;
7536 ;; Completing class structure tags. This is a completion plugin.
7537 ;; The necessary taglist is constructed dynamically
7538
7539 (defvar idlwave-current-tags-class nil)
7540 (defvar idlwave-current-class-tags nil)
7541 (defvar idlwave-current-native-class-tags nil)
7542 (defvar idlwave-sint-class-tags nil)
7543 (declare-function idlwave-sintern-class-tag "idlwave" t t)
7544 (idlwave-new-sintern-type 'class-tag)
7545 (add-to-list 'idlwave-complete-special 'idlwave-complete-class-structure-tag)
7546 (add-hook 'idlwave-update-rinfo-hook 'idlwave-class-tag-reset)
7547
7548 (defun idlwave-complete-class-structure-tag ()
7549 "Complete a structure tag on a `self' argument in an object method."
7550 (interactive)
7551 (let ((pos (point))
7552 (case-fold-search t))
7553 (if (save-excursion
7554 ;; Check if the context is right
7555 (skip-chars-backward "a-zA-Z0-9._$")
7556 (and (< (point) (- pos 4))
7557 (looking-at "self\\.")))
7558 (let* ((class-selector (nth 2 (idlwave-current-routine)))
7559 (super-classes (idlwave-all-class-inherits class-selector)))
7560 ;; Check if we are in a class routine
7561 (unless class-selector
7562 (error "Not in a method procedure or function"))
7563 ;; Check if we need to update the "current" class
7564 (if (not (equal class-selector idlwave-current-tags-class))
7565 (idlwave-prepare-class-tag-completion class-selector))
7566 (setq idlwave-completion-help-info
7567 (list 'idlwave-complete-class-structure-tag-help
7568 (idlwave-sintern-routine
7569 (concat class-selector "__define"))
7570 nil))
7571 ;; FIXME: idlwave-cpl-bold doesn't seem used anywhere.
7572 (let ((idlwave-cpl-bold idlwave-current-native-class-tags))
7573 (idlwave-complete-in-buffer
7574 'class-tag 'class-tag
7575 idlwave-current-class-tags nil
7576 (format "Select a tag of class %s" class-selector)
7577 "class tag"
7578 'idlwave-attach-class-tag-classes))
7579 t) ; return t to skip other completions
7580 nil)))
7581
7582 (defun idlwave-class-tag-reset ()
7583 (setq idlwave-current-tags-class nil))
7584
7585 (defun idlwave-prepare-class-tag-completion (class)
7586 "Find and parse the necessary class definitions for class structure tags."
7587 (setq idlwave-sint-class-tags nil)
7588 (setq idlwave-current-tags-class class)
7589 (setq idlwave-current-class-tags
7590 (mapcar (lambda (x)
7591 (list (idlwave-sintern-class-tag x 'set)))
7592 (idlwave-all-class-tags class)))
7593 (setq idlwave-current-native-class-tags
7594 (mapcar 'downcase (idlwave-class-tags class))))
7595
7596 ;===========================================================================
7597 ;;
7598 ;; Completing system variables and their structure fields
7599 ;; This is also a plugin.
7600
7601 (defvar idlwave-sint-sysvars nil)
7602 (defvar idlwave-sint-sysvartags nil)
7603 (declare-function idlwave-sintern-sysvar "idlwave" t t)
7604 (declare-function idlwave-sintern-sysvartag "idlwave" t t)
7605 (idlwave-new-sintern-type 'sysvar)
7606 (idlwave-new-sintern-type 'sysvartag)
7607 (add-to-list 'idlwave-complete-special 'idlwave-complete-sysvar-or-tag)
7608 (add-hook 'idlwave-update-rinfo-hook 'idlwave-sysvars-reset)
7609 (add-hook 'idlwave-after-load-rinfo-hook 'idlwave-sintern-sysvar-alist)
7610
7611
7612 (defun idlwave-complete-sysvar-or-tag ()
7613 "Complete a system variable."
7614 (interactive)
7615 (let ((pos (point))
7616 (case-fold-search t))
7617 (cond ((save-excursion
7618 ;; Check if the context is right for system variable
7619 (skip-chars-backward "[a-zA-Z0-9_$]")
7620 (equal (char-before) ?!))
7621 (setq idlwave-completion-help-info '(idlwave-complete-sysvar-help))
7622 (idlwave-complete-in-buffer 'sysvar 'sysvar
7623 idlwave-system-variables-alist nil
7624 "Select a system variable"
7625 "system variable")
7626 t) ; return t to skip other completions
7627 ((save-excursion
7628 ;; Check if the context is right for sysvar tag
7629 (skip-chars-backward "a-zA-Z0-9_$.")
7630 (and (equal (char-before) ?!)
7631 (looking-at "\\([a-zA-Z][a-zA-Z0-9_$]*\\)\\.")
7632 (<= (match-end 0) pos)))
7633 ;; Complete a system variable tag
7634 (let* ((var (idlwave-sintern-sysvar (match-string 1)))
7635 (entry (assq var idlwave-system-variables-alist))
7636 (tags (cdr (assq 'tags entry))))
7637 (or entry (error "!%s is not a known system variable" var))
7638 (or tags (error "System variable !%s is not a structure" var))
7639 (setq idlwave-completion-help-info
7640 (list 'idlwave-complete-sysvar-tag-help var))
7641 (idlwave-complete-in-buffer 'sysvartag 'sysvartag
7642 tags nil
7643 "Select a system variable tag"
7644 "system variable tag")
7645 t)) ; return t to skip other completions
7646 (t nil))))
7647
7648 (defvar idlw-help-link) ;dynamic variables set by help callback
7649 (defun idlwave-complete-sysvar-help (mode word)
7650 (let ((word (or (nth 1 idlwave-completion-help-info) word))
7651 (entry (assoc word idlwave-system-variables-alist)))
7652 (cond
7653 ((eq mode 'test)
7654 (and (stringp word) entry (nth 1 (assq 'link entry))))
7655 ((eq mode 'set)
7656 ;; Setting dynamic!!!
7657 (if entry (setq idlw-help-link (nth 1 (assq 'link entry)))))
7658 (t (error "This should not happen")))))
7659
7660 (defun idlwave-complete-sysvar-tag-help (mode word)
7661 (let* ((var (nth 1 idlwave-completion-help-info))
7662 (entry (assoc var idlwave-system-variables-alist))
7663 (tags (cdr (assq 'tags entry)))
7664 (main (nth 1 (assq 'link entry)))
7665 target)
7666 (cond
7667 ((eq mode 'test) ; we can at least link the main
7668 (and (stringp word) entry main))
7669 ((eq mode 'set)
7670 (if entry
7671 (setq idlw-help-link
7672 (if (setq target (cdr (assoc-string word tags t)))
7673 (idlwave-substitute-link-target main target)
7674 main)))) ;; setting dynamic!!!
7675 (t (error "This should not happen")))))
7676
7677 (defun idlwave-split-link-target (link)
7678 "Split a given LINK into link file and anchor."
7679 (if (string-match idlwave-html-link-sep link)
7680 (cons (substring link 0 (match-beginning 0))
7681 (string-to-number (substring link (match-end 0))))))
7682
7683 (defun idlwave-substitute-link-target (link target)
7684 "Substitute the TARGET anchor for the given LINK."
7685 (let (main-base)
7686 (setq main-base (if (string-match "#" link)
7687 (substring link 0 (match-beginning 0))
7688 link))
7689 (if target
7690 (concat main-base idlwave-html-link-sep (number-to-string target))
7691 link)))
7692
7693 ;; Fake help in the source buffer for class structure tags.
7694 ;; IDLW-HELP-LINK AND IDLW-HELP-NAME ARE GLOBAL-VARIABLES HERE.
7695 ;; (from idlwave-do-mouse-completion-help)
7696 (defvar idlw-help-name)
7697 (defvar idlw-help-link)
7698 (defvar idlwave-help-do-class-struct-tag nil)
7699 (defun idlwave-complete-class-structure-tag-help (mode word)
7700 (cond
7701 ((eq mode 'test) ; nothing gets fontified for class tags
7702 nil)
7703 ((eq mode 'set)
7704 (let (class-with found-in)
7705 (when (setq class-with
7706 (idlwave-class-or-superclass-with-tag
7707 idlwave-current-tags-class
7708 word))
7709 (if (assq (idlwave-sintern-class class-with)
7710 idlwave-system-class-info)
7711 (error "No help available for system class tags"))
7712 (if (setq found-in (idlwave-class-found-in class-with))
7713 (setq idlw-help-name (cons (concat found-in "__define") class-with))
7714 (setq idlw-help-name (concat class-with "__define")))))
7715 (setq idlw-help-link word
7716 idlwave-help-do-class-struct-tag t))
7717 (t (error "This should not happen"))))
7718
7719 (defun idlwave-class-or-superclass-with-tag (class tag)
7720 "Find and return the CLASS or one of its superclass with the
7721 associated TAG, if any."
7722 (let ((sclasses (cons class (idlwave-all-class-inherits class)))
7723 cl)
7724 (catch 'exit
7725 (while sclasses
7726 (setq cl (pop sclasses))
7727 (let ((tags (idlwave-class-tags cl)))
7728 (while tags
7729 (if (eq t (compare-strings tag 0 nil (car tags) 0 nil t))
7730 (throw 'exit cl))
7731 (setq tags (cdr tags))))))))
7732
7733
7734 (defun idlwave-sysvars-reset ()
7735 (if (and (fboundp 'idlwave-shell-is-running)
7736 (idlwave-shell-is-running)
7737 idlwave-idlwave_routine_info-compiled)
7738 (idlwave-shell-send-command "idlwave_get_sysvars"
7739 'idlwave-process-sysvars 'hide)))
7740
7741 (defun idlwave-process-sysvars ()
7742 (idlwave-shell-filter-sysvars)
7743 (setq idlwave-sint-sysvars nil
7744 idlwave-sint-sysvartags nil)
7745 (idlwave-sintern-sysvar-alist))
7746
7747 (defun idlwave-sintern-sysvar-alist ()
7748 (let ((list idlwave-system-variables-alist) entry tags)
7749 (while (setq entry (pop list))
7750 (setcar entry (idlwave-sintern-sysvar (car entry) 'set))
7751 (setq tags (assq 'tags entry))
7752 (if tags
7753 (setcdr tags
7754 (mapcar (lambda (x)
7755 (cons (idlwave-sintern-sysvartag (car x) 'set)
7756 (cdr x)))
7757 (cdr tags)))))))
7758
7759 (defvar idlwave-shell-command-output)
7760 (defun idlwave-shell-filter-sysvars ()
7761 "Get any new system variables and tags."
7762 (let ((text idlwave-shell-command-output)
7763 (start 0)
7764 (old idlwave-system-variables-alist)
7765 var tags type name class link old-entry)
7766 (setq idlwave-system-variables-alist nil)
7767 (while (string-match "^IDLWAVE-SYSVAR: !\\([a-zA-Z0-9_$]+\\)\\( \\(.*\\)\\)?"
7768 text start)
7769 (setq start (match-end 0)
7770 var (match-string 1 text)
7771 tags (if (match-end 3)
7772 (idlwave-split-string (match-string 3 text))))
7773 ;; Maintain old links, if present
7774 (setq old-entry (assq (idlwave-sintern-sysvar var) old))
7775 (setq link (assq 'link old-entry))
7776 (setq idlwave-system-variables-alist
7777 (cons (list var
7778 (cons
7779 'tags
7780 (mapcar (lambda (x)
7781 (cons x
7782 (cdr (assq
7783 (idlwave-sintern-sysvartag x)
7784 (cdr (assq 'tags old-entry))))))
7785 tags)) link)
7786 idlwave-system-variables-alist)))
7787 ;; Keep the old value if query was not successful
7788 (setq idlwave-system-variables-alist
7789 (or idlwave-system-variables-alist old))))
7790
7791 (defun idlwave-completion-fontify-classes ()
7792 "Goto the *Completions* buffer and fontify the class info."
7793 (when (featurep 'font-lock)
7794 (with-current-buffer "*Completions*"
7795 (save-excursion
7796 (goto-char (point-min))
7797 (let ((buffer-read-only nil))
7798 (while (re-search-forward "\\.*<[^>]+>" nil t)
7799 (put-text-property (match-beginning 0) (match-end 0)
7800 'face 'font-lock-string-face)))))))
7801
7802 (defun idlwave-uniquify (list)
7803 (let ((ht (make-hash-table :size (length list) :test 'equal)))
7804 (delq nil
7805 (mapcar (lambda (x)
7806 (unless (gethash x ht)
7807 (puthash x t ht)
7808 x))
7809 list))))
7810
7811 (defun idlwave-after-successful-completion (type slash &optional verify)
7812 "Add `=' or `(' after successful completion of keyword and function.
7813 Restore the pre-completion window configuration if possible."
7814 (cond
7815 ((eq type 'procedure)
7816 nil)
7817 ((eq type 'function)
7818 (cond
7819 ((equal idlwave-function-completion-adds-paren nil) nil)
7820 ((or (equal idlwave-function-completion-adds-paren t)
7821 (equal idlwave-function-completion-adds-paren 1))
7822 (insert "("))
7823 ((equal idlwave-function-completion-adds-paren 2)
7824 (insert "()")
7825 (backward-char 1))
7826 (t nil)))
7827 ((eq type 'keyword)
7828 (if (and idlwave-keyword-completion-adds-equal
7829 (not slash))
7830 (progn (insert "=") t)
7831 nil)))
7832
7833 ;; Restore the pre-completion window configuration if this is safe.
7834
7835 (if (or (eq verify 'force) ; force
7836 (and
7837 (get-buffer-window "*Completions*") ; visible
7838 (idlwave-local-value 'idlwave-completion-p
7839 "*Completions*") ; cib-buffer
7840 (eq (marker-buffer idlwave-completion-mark)
7841 (current-buffer)) ; buffer OK
7842 (equal (marker-position idlwave-completion-mark)
7843 verify))) ; pos OK
7844 (idlwave-restore-wconf-after-completion))
7845 (move-marker idlwave-completion-mark nil)
7846 (setq idlwave-before-completion-wconf nil))
7847
7848 (defun idlwave-mouse-context-help (ev &optional arg)
7849 "Call `idlwave-context-help' on the clicked location."
7850 (interactive "eP")
7851 (mouse-set-point ev)
7852 (idlwave-context-help arg))
7853
7854 (defvar idlwave-last-context-help-pos nil)
7855 (defun idlwave-context-help (&optional arg)
7856 "Display IDL Online Help on context.
7857 If point is on a keyword, help for that keyword will be shown. If
7858 point is on a routine name or in the argument list of a routine, help
7859 for that routine will be displayed. Works for system routines and
7860 keywords, it pulls up text help. For other routies and keywords,
7861 visits the source file, finding help in the header (if
7862 `idlwave-help-source-try-header' is non-nil) or the routine definition
7863 itself."
7864 (interactive "P")
7865 (idlwave-do-context-help arg))
7866
7867 (defun idlwave-mouse-completion-help (ev)
7868 "Display online help about the completion at point."
7869 (interactive "eP")
7870 ;; Restore last-command for next command, to make
7871 ;; scrolling/cancelling of completions work.
7872 (setq this-command last-command)
7873 (idlwave-do-mouse-completion-help ev))
7874
7875 (defun idlwave-routine-info (&optional arg external)
7876 "Display a routines calling sequence and list of keywords.
7877 When point is on the name a function or procedure, or in the argument
7878 list of a function or procedure, this command displays a help buffer with
7879 the information. When called with prefix arg, enforce class query.
7880
7881 When point is on an object operator `->', display the class stored in
7882 this arrow, if any (see `idlwave-store-inquired-class'). With a prefix
7883 arg, the class property is cleared out."
7884
7885 (interactive "P")
7886 (idlwave-routines)
7887 (if (string-match "->" (buffer-substring
7888 (max (point-min) (1- (point)))
7889 (min (+ 2 (point)) (point-max))))
7890 ;; Cursor is on an arrow
7891 (if (get-text-property (point) 'idlwave-class)
7892 ;; arrow has class property
7893 (if arg
7894 ;; Remove property
7895 (save-excursion
7896 (backward-char 1)
7897 (when (looking-at ".?\\(->\\)")
7898 (remove-text-properties (match-beginning 1) (match-end 1)
7899 '(idlwave-class nil face nil))
7900 (message "Class property removed from arrow")))
7901 ;; Echo class property
7902 (message "Arrow has text property identifying object to be class %s"
7903 (get-text-property (point) 'idlwave-class)))
7904 ;; No property found
7905 (message "Arrow has no class text property"))
7906
7907 ;; Not on an arrow...
7908 (let* ((idlwave-query-class nil)
7909 (idlwave-force-class-query (equal arg '(4)))
7910 (module (idlwave-what-module)))
7911 (if (car module)
7912 (apply 'idlwave-display-calling-sequence
7913 (idlwave-fix-module-if-obj_new module))
7914 (error "Don't know which calling sequence to show")))))
7915
7916 (defun idlwave-resolve (&optional arg)
7917 "Call RESOLVE_ROUTINE on the module name at point.
7918 Like `idlwave-routine-info', this looks for a routine call at point.
7919 After confirmation in the minibuffer, it will use the shell to issue
7920 a RESOLVE call for this routine, to attempt to make it defined and its
7921 routine info available for IDLWAVE. If the routine is a method call,
7922 both `class__method' and `class__define' will be tried.
7923 With ARG, enforce query for the class of object methods."
7924 (interactive "P")
7925 (let* ((idlwave-query-class nil)
7926 (idlwave-force-class-query (equal arg '(4)))
7927 (module (idlwave-what-module))
7928 (name (idlwave-make-full-name (nth 2 module) (car module)))
7929 (type (if (eq (nth 1 module) 'pro) "pro" "function"))
7930 (resolve (read-string "Resolve: " (format "%s %s" type name)))
7931 (kwd "")
7932 class)
7933 (if (string-match "\\(pro\\|function\\)[ \t]+\\(\\(.*\\)::\\)?\\(.*\\)"
7934 resolve)
7935 (setq type (match-string 1 resolve)
7936 class (if (match-beginning 2)
7937 (match-string 3 resolve)
7938 nil)
7939 name (match-string 4 resolve)))
7940 (if (string= (downcase type) "function")
7941 (setq kwd ",/is_function"))
7942
7943 (cond
7944 ((null class)
7945 (idlwave-shell-send-command
7946 (format "resolve_routine,'%s'%s" (downcase name) kwd)
7947 'idlwave-update-routine-info
7948 nil t))
7949 (t
7950 (idlwave-shell-send-command
7951 (format "resolve_routine,'%s__define'%s" (downcase class) kwd)
7952 (list 'idlwave-shell-send-command
7953 (format "resolve_routine,'%s__%s'%s"
7954 (downcase class) (downcase name) kwd)
7955 '(idlwave-update-routine-info)
7956 nil t))))))
7957
7958 (defun idlwave-find-module-this-file ()
7959 (interactive)
7960 (idlwave-find-module '(4)))
7961
7962 (defun idlwave-find-module (&optional arg)
7963 "Find the source code of an IDL module.
7964 Works for modules for which IDLWAVE has routine info available.
7965 The function offers as default the module name `idlwave-routine-info'
7966 would use. With ARG limit to this buffer. With two prefix ARG's
7967 force class query for object methods."
7968 (interactive "P")
7969 (let* ((idlwave-query-class nil)
7970 (idlwave-force-class-query (equal arg '(16)))
7971 (this-buffer (equal arg '(4)))
7972 (module (idlwave-fix-module-if-obj_new (idlwave-what-module)))
7973 (default (if module
7974 (concat (idlwave-make-full-name
7975 (nth 2 module) (car module))
7976 (if (eq (nth 1 module) 'pro) "<p>" "<f>"))
7977 "none"))
7978 (list
7979 (idlwave-uniquify
7980 (delq nil
7981 (mapcar (lambda (x)
7982 (if (eq 'system (car-safe (nth 3 x)))
7983 ;; Take out system routines with no source.
7984 nil
7985 (list
7986 (concat (idlwave-make-full-name
7987 (nth 2 x) (car x))
7988 (if (eq (nth 1 x) 'pro) "<p>" "<f>")))))
7989 (if this-buffer
7990 (idlwave-save-buffer-update)
7991 (idlwave-routines))))))
7992 (name (idlwave-completing-read
7993 (if (or (not this-buffer)
7994 (assoc default list))
7995 (format "Module (Default %s): " default)
7996 (format "Module in this file: "))
7997 list))
7998 type class)
7999 (if (string-match "\\`\\s-*\\'" name)
8000 ;; Nothing, use the default.
8001 (setq name default))
8002 (if (string-match "<[fp]>" name)
8003 (setq type (substring name -2 -1)
8004 name (substring name 0 -3)))
8005 (if (string-match "\\(.*\\)::\\(.*\\)" name)
8006 (setq class (match-string 1 name)
8007 name (match-string 2 name)))
8008 (setq name (idlwave-sintern-routine-or-method name class)
8009 class (idlwave-sintern-class class)
8010 type (cond ((equal type "f") 'fun)
8011 ((equal type "p") 'pro)
8012 (t t)))
8013 (idlwave-do-find-module name type class nil this-buffer)))
8014
8015 (defun idlwave-do-find-module (name type class
8016 &optional force-source this-buffer)
8017 (let ((name1 (idlwave-make-full-name class name))
8018 source buf1 entry
8019 (buf (current-buffer))
8020 (pos (point))
8021 file name2)
8022 (setq entry (idlwave-best-rinfo-assq name type class (idlwave-routines)
8023 'WITH-FILE)
8024 source (or force-source (nth 3 entry))
8025 name2 (if (nth 2 entry)
8026 (idlwave-make-full-name (nth 2 entry) name)
8027 name1))
8028 (if source
8029 (setq file (idlwave-routine-source-file source)))
8030 (unless file ; Try to find it on the path.
8031 (setq file
8032 (idlwave-expand-lib-file-name
8033 (if class
8034 (format "%s__define.pro" (downcase class))
8035 (format "%s.pro" (downcase name))))))
8036 (cond
8037 ((or (null name) (equal name ""))
8038 (error "Abort"))
8039 ((eq (car source) 'system)
8040 (error "Source code for system routine %s is not available"
8041 name2))
8042 ((or (not file) (not (file-regular-p file)))
8043 (error "Source code for routine %s is not available"
8044 name2))
8045 (t
8046 (when (not this-buffer)
8047 (setq buf1
8048 (idlwave-find-file-noselect file 'find))
8049 (pop-to-buffer buf1 t))
8050 (goto-char (point-max))
8051 (let ((case-fold-search t))
8052 (if (re-search-backward
8053 (concat "^[ \t]*\\<"
8054 (cond ((eq type 'fun) "function")
8055 ((eq type 'pro) "pro")
8056 (t "\\(pro\\|function\\)"))
8057 "\\>[ \t]+"
8058 (regexp-quote (downcase name2))
8059 "[^a-zA-Z0-9_$]")
8060 nil t)
8061 (goto-char (match-beginning 0))
8062 (pop-to-buffer buf)
8063 (goto-char pos)
8064 (error "Could not find routine %s" name2)))))))
8065
8066 (defun idlwave-what-module ()
8067 "Return a default module for stuff near point.
8068 Used by `idlwave-routine-info' and `idlwave-find-module'."
8069 (idlwave-routines)
8070 (if (let ((case-fold-search t))
8071 (save-excursion
8072 (idlwave-beginning-of-statement)
8073 (looking-at "[ \t]*\\(pro\\|function\\)[ \t]+\\(\\([a-zA-Z0-9_$]+\\)::\\)?\\([a-zA-Z0-9$_]+\\)\\([, \t\n]\\|$\\)")))
8074 ;; This is a function or procedure definition statement
8075 ;; We return the defined routine as module.
8076 (list
8077 (idlwave-sintern-routine-or-method (match-string-no-properties 4)
8078 (match-string-no-properties 2))
8079 (if (equal (downcase (match-string 1)) "pro") 'pro 'fun)
8080 (idlwave-sintern-class (match-string 3)))
8081
8082 ;; Not a definition statement - analyze precise position.
8083 (let* ((where (idlwave-where))
8084 (cw (nth 2 where))
8085 (pro (car (nth 0 where)))
8086 (func (car (nth 1 where)))
8087 (this-word (idlwave-this-word "a-zA-Z0-9$_"))
8088 (next-char (save-excursion (skip-chars-forward "a-zA-Z0-9$_")
8089 (following-char)))
8090 )
8091 (cond
8092 ((and (eq cw 'procedure)
8093 (not (equal this-word "")))
8094 (setq this-word (idlwave-sintern-routine-or-method
8095 this-word (nth 2 (nth 3 where))))
8096 (list this-word 'pro
8097 (idlwave-determine-class
8098 (cons this-word (cdr (nth 3 where)))
8099 'pro)))
8100 ((and (eq cw 'function)
8101 (not (equal this-word ""))
8102 (or (eq next-char ?\() ; exclude arrays, vars.
8103 (looking-at "[a-zA-Z0-9_]*[ \t]*(")))
8104 (setq this-word (idlwave-sintern-routine-or-method
8105 this-word (nth 2 (nth 3 where))))
8106 (list this-word 'fun
8107 (idlwave-determine-class
8108 (cons this-word (cdr (nth 3 where)))
8109 'fun)))
8110 ((and (memq cw '(function-keyword procedure-keyword))
8111 (not (equal this-word ""))
8112 (eq next-char ?\()) ; A function!
8113 (setq this-word (idlwave-sintern-routine this-word))
8114 (list this-word 'fun nil))
8115 (func
8116 (list func 'fun (idlwave-determine-class (nth 1 where) 'fun)))
8117 (pro
8118 (list pro 'pro (idlwave-determine-class (nth 0 where) 'pro)))
8119 (t nil)))))
8120
8121 (defun idlwave-what-module-find-class ()
8122 "Call `idlwave-what-module' and find the inherited class if necessary."
8123 (let* ((module (idlwave-what-module))
8124 (class (nth 2 module)))
8125 (if (and (= (length module) 3)
8126 (stringp class))
8127 (list (car module)
8128 (nth 1 module)
8129 (apply 'idlwave-find-inherited-class module))
8130 module)))
8131
8132 (defun idlwave-find-inherited-class (name type class)
8133 "Find the class which defines TYPE NAME and is CLASS or inherited by CLASS."
8134 (let ((entry (idlwave-best-rinfo-assoc name type class (idlwave-routines))))
8135 (if entry
8136 (nth 2 entry)
8137 class)))
8138
8139 (defun idlwave-fix-module-if-obj_new (module)
8140 "Check if MODULE points to obj_new.
8141 If yes, and if the cursor is in the keyword region, change to the
8142 appropriate Init method."
8143 (let* ((name (car module))
8144 (pos (point))
8145 (case-fold-search t)
8146 string)
8147 (if (and (stringp name)
8148 (equal (downcase name) "obj_new")
8149 (save-excursion
8150 (idlwave-beginning-of-statement)
8151 (setq string (buffer-substring (point) pos))
8152 (string-match "obj_new([^'\"]*['\"]\\([a-zA-Z0-9_]+\\)"
8153 string)))
8154 (let ((name "Init")
8155 (class (match-string 1 string)))
8156 (setq module (list (idlwave-sintern-method "Init")
8157 'fun
8158 (idlwave-sintern-class class)))))
8159 module))
8160
8161 (defun idlwave-fix-keywords (name type class keywords
8162 &optional super-classes system)
8163 "Update a list of keywords.
8164 Translate OBJ_NEW, adding all super-class keywords, or all keywords
8165 from all classes if CLASS equals t. If SYSTEM is non-nil, don't
8166 demand _EXTRA in the keyword list."
8167 (let ((case-fold-search t))
8168
8169 ;; If this is the OBJ_NEW function, try to figure out the class and use
8170 ;; the keywords from the corresponding INIT method.
8171 (if (and (equal (upcase name) "OBJ_NEW")
8172 (derived-mode-p 'idlwave-mode 'idlwave-shell-mode))
8173 (let* ((bos (save-excursion (idlwave-beginning-of-statement) (point)))
8174 (string (buffer-substring bos (point)))
8175 (case-fold-search t)
8176 class)
8177 (and (string-match "obj_new([^'\"]*['\"]\\([a-zA-Z0-9_]+\\)"
8178 string)
8179 (setq class (idlwave-sintern-class (match-string 1 string)))
8180 (setq idlwave-current-obj_new-class class)
8181 (setq keywords
8182 (append keywords
8183 (idlwave-entry-keywords
8184 (idlwave-rinfo-assq
8185 (idlwave-sintern-method "INIT")
8186 'fun
8187 class
8188 (idlwave-routines)) 'do-link))))))
8189
8190 ;; If the class is `t', combine all keywords of all methods NAME
8191 (when (eq class t)
8192 (mapc (lambda (entry)
8193 (and
8194 (nth 2 entry) ; non-nil class
8195 (eq (nth 1 entry) type) ; correct type
8196 (setq keywords
8197 (append keywords
8198 (idlwave-entry-keywords entry 'do-link)))))
8199 (idlwave-all-assq name (idlwave-routines)))
8200 (setq keywords (idlwave-uniquify keywords)))
8201
8202 ;; If we have inheritance, add all keywords from superclasses, if
8203 ;; the user indicated that method in `idlwave-keyword-class-inheritance'
8204 (when (and
8205 super-classes
8206 idlwave-keyword-class-inheritance
8207 (stringp class)
8208 (or
8209 system
8210 (assq (idlwave-sintern-keyword "_extra") keywords)
8211 (assq (idlwave-sintern-keyword "_ref_extra") keywords))
8212 ;; Check if one of the keyword-class regexps matches the name
8213 (let ((regexps idlwave-keyword-class-inheritance) re)
8214 (catch 'exit
8215 (while (setq re (pop regexps))
8216 (if (string-match re name) (throw 'exit t))))))
8217
8218 (loop for entry in (idlwave-routines) do
8219 (and (nth 2 entry) ; non-nil class
8220 (memq (nth 2 entry) super-classes) ; an inherited class
8221 (eq (nth 1 entry) type) ; correct type
8222 (eq (car entry) name) ; correct name
8223 (mapc (lambda (k) (add-to-list 'keywords k))
8224 (idlwave-entry-keywords entry 'do-link))))
8225 (setq keywords (idlwave-uniquify keywords)))
8226
8227 ;; Return the final list
8228 keywords))
8229
8230 (defun idlwave-expand-keyword (keyword module)
8231 "Expand KEYWORD to one of the valid keyword parameters of MODULE.
8232 KEYWORD may be an exact match or an abbreviation of a keyword.
8233 If the match is exact, KEYWORD itself is returned, even if there may be other
8234 keywords of which KEYWORD is an abbreviation. This is necessary because some
8235 system routines have keywords which are prefixes of other keywords.
8236 If KEYWORD is an abbreviation of several keywords, a list of all possible
8237 completions is returned.
8238 If the abbreviation was unique, the correct keyword is returned.
8239 If it cannot be a keyword, the function return nil.
8240 If we do not know about MODULE, just return KEYWORD literally."
8241 (let* ((name (car module))
8242 (type (nth 1 module))
8243 (class (nth 2 module))
8244 (kwd (idlwave-sintern-keyword keyword))
8245 (entry (idlwave-best-rinfo-assoc name type class (idlwave-routines)))
8246 (kwd-alist (idlwave-entry-keywords entry))
8247 (extra (or (assq (idlwave-sintern-keyword "_EXTRA") kwd-alist)
8248 (assq (idlwave-sintern-keyword "_REF_EXTRA") kwd-alist)))
8249 (completion-ignore-case t)
8250 candidates)
8251 (cond ((assq kwd kwd-alist)
8252 kwd)
8253 ((setq candidates (all-completions kwd kwd-alist))
8254 (if (= (length candidates) 1)
8255 (car candidates)
8256 candidates))
8257 ((and entry extra)
8258 ;; Inheritance may cause this keyword to be correct
8259 keyword)
8260 (entry
8261 ;; We do know the function, which does not have the keyword.
8262 nil)
8263 (t
8264 ;; We do not know the function, so this just might be a correct
8265 ;; keyword - return it as it is.
8266 keyword))))
8267
8268 (defvar idlwave-rinfo-mouse-map (make-sparse-keymap))
8269 (defvar idlwave-rinfo-map (make-sparse-keymap))
8270 (define-key idlwave-rinfo-mouse-map
8271 (if (featurep 'xemacs) [button2] [mouse-2])
8272 'idlwave-mouse-active-rinfo)
8273 (define-key idlwave-rinfo-mouse-map
8274 (if (featurep 'xemacs) [(shift button2)] [(shift mouse-2)])
8275 'idlwave-mouse-active-rinfo-shift)
8276 (define-key idlwave-rinfo-mouse-map
8277 (if (featurep 'xemacs) [button3] [mouse-3])
8278 'idlwave-mouse-active-rinfo-right)
8279 (define-key idlwave-rinfo-mouse-map " " 'idlwave-active-rinfo-space)
8280 (define-key idlwave-rinfo-map "q" 'idlwave-quit-help)
8281 (define-key idlwave-rinfo-mouse-map "q" 'idlwave-quit-help)
8282 (defvar idlwave-popup-source nil)
8283 (defvar idlwave-rinfo-marker (make-marker))
8284
8285 (defun idlwave-quit-help ()
8286 (interactive)
8287 (let ((ri-window (get-buffer-window "*Help*"))
8288 (olh-window (get-buffer-window "*IDLWAVE Help*")))
8289 (when (and olh-window
8290 (fboundp 'idlwave-help-quit))
8291 (select-window olh-window)
8292 (idlwave-help-quit))
8293 (when (window-live-p ri-window)
8294 (delete-window ri-window))))
8295
8296 (defun idlwave-display-calling-sequence (name type class
8297 &optional initial-class)
8298 ;; Display the calling sequence of module NAME, type TYPE in class CLASS.
8299 (let* ((initial-class (or initial-class class))
8300 (entry (or (idlwave-best-rinfo-assq name type class
8301 (idlwave-routines))
8302 (idlwave-rinfo-assq name type class
8303 idlwave-unresolved-routines)))
8304 (name (or (car entry) name))
8305 (class (or (nth 2 entry) class))
8306 (superclasses (idlwave-all-class-inherits initial-class))
8307 (twins (idlwave-routine-twins entry))
8308 (dtwins (idlwave-study-twins twins))
8309 (all dtwins)
8310 (system (eq (car (nth 3 entry)) 'system))
8311 (calling-seq (nth 4 entry))
8312 (keywords (idlwave-entry-keywords entry 'do-link))
8313 (html-file (car (nth 5 entry)))
8314 (help-echo-kwd
8315 "Button2: Insert KEYWORD (SHIFT=`/KEYWORD') | Button3: Online Help ")
8316 (help-echo-use
8317 "Button2/3: Online Help")
8318 (help-echo-src
8319 "Button2: Jump to source and back | Button3: Source in Help window.")
8320 (help-echo-class
8321 "Button2: Display info about same method in superclass")
8322 (col 0)
8323 (data (list name type class (current-buffer) nil initial-class))
8324 (km-prop (if (featurep 'xemacs) 'keymap 'local-map))
8325 (face 'idlwave-help-link)
8326 beg props win cnt total)
8327 ;; Fix keywords, but don't add chained super-classes, since these
8328 ;; are shown separately for that super-class
8329 (setq keywords (idlwave-fix-keywords name type class keywords))
8330 (cond
8331 ((null entry)
8332 (error "No %s %s known %s" type name
8333 (if initial-class (concat "in class " initial-class) "")))
8334 ((or (null name) (equal name ""))
8335 (error "No function or procedure call at point"))
8336 ((null calling-seq)
8337 (error "Calling sequence of %s %s not available" type name))
8338 (t
8339 (move-marker idlwave-rinfo-marker (point))
8340 (with-current-buffer (get-buffer-create "*Help*")
8341 (use-local-map idlwave-rinfo-map)
8342 (setq buffer-read-only nil)
8343 (erase-buffer)
8344 (set (make-local-variable 'idlwave-popup-source) nil)
8345 (set (make-local-variable 'idlwave-current-obj_new-class)
8346 idlwave-current-obj_new-class)
8347 (when superclasses
8348 (setq props (list 'mouse-face 'highlight
8349 km-prop idlwave-rinfo-mouse-map
8350 'help-echo help-echo-class
8351 'data (cons 'class data)))
8352 (let ((classes (cons initial-class superclasses)) c)
8353 (insert "Classes: ")
8354 (while (setq c (pop classes))
8355 (insert " ")
8356 (setq beg (point))
8357 (insert c)
8358 (if (equal (downcase c) (downcase class))
8359 (add-text-properties beg (point) (list 'face 'bold))
8360 ;; If Method exists in a different class link it
8361 (if (idlwave-rinfo-assq name type c (idlwave-routines))
8362 (add-text-properties beg (point) props))))
8363 (insert "\n")))
8364 (setq props (list 'mouse-face 'highlight
8365 km-prop idlwave-rinfo-mouse-map
8366 'help-echo help-echo-use
8367 'data (cons 'usage data)))
8368 (if html-file (setq props (append (list 'face face 'link html-file)
8369 props)))
8370 (insert "Usage: ")
8371 (setq beg (point))
8372 (insert (if class
8373 (format calling-seq class name class name class name)
8374 (format calling-seq name name name name))
8375 "\n")
8376 (add-text-properties beg (point) props)
8377
8378 (insert "Keywords:")
8379 (if (null keywords)
8380 (insert " No keywords accepted.")
8381 (setq col 9)
8382 (mapc
8383 (lambda (x)
8384 (if (>= (+ col 1 (length (car x)))
8385 (window-width))
8386 (progn
8387 (insert "\n ")
8388 (setq col 9)))
8389 (insert " ")
8390 (setq beg (point)
8391 ;; Relevant keywords already have link property attached
8392 props (list 'mouse-face 'highlight
8393 km-prop idlwave-rinfo-mouse-map
8394 'data (cons 'keyword data)
8395 'help-echo help-echo-kwd
8396 'keyword (car x)))
8397 (if system (setq props (append (list 'face face) props)))
8398 (insert (car x))
8399 (add-text-properties beg (point) props)
8400 (setq col (+ col 1 (length (car x)))))
8401 keywords))
8402
8403 (setq cnt 1 total (length all))
8404 ;; Here entry is (key file (list of type-conses))
8405 (while (setq entry (pop all))
8406 (setq props (list 'mouse-face 'highlight
8407 km-prop idlwave-rinfo-mouse-map
8408 'help-echo help-echo-src
8409 'source (list (car (car (nth 2 entry))) ;type
8410 (nth 1 entry)
8411 nil
8412 (cdr (car (nth 2 entry))))
8413 'data (cons 'source data)))
8414 (idlwave-insert-source-location
8415 (format "\n%-8s %s"
8416 (if (equal cnt 1)
8417 (if (> total 1) "Sources:" "Source:")
8418 "")
8419 (if (> total 1) "- " ""))
8420 entry props)
8421 (incf cnt)
8422 (when (and all (> cnt idlwave-rinfo-max-source-lines))
8423 ;; No more source lines, please
8424 (insert (format
8425 "\n Source information truncated to %d entries."
8426 idlwave-rinfo-max-source-lines))
8427 (setq all nil)))
8428 (goto-char (point-min))
8429 (setq buffer-read-only t))
8430 (display-buffer "*Help*")
8431 (if (and (setq win (get-buffer-window "*Help*"))
8432 idlwave-resize-routine-help-window)
8433 (progn
8434 (let ((ww (selected-window)))
8435 (unwind-protect
8436 (progn
8437 (select-window win)
8438 (enlarge-window (- (/ (frame-height) 2)
8439 (window-height)))
8440 (shrink-window-if-larger-than-buffer))
8441 (select-window ww)))))))))
8442
8443 (defun idlwave-insert-source-location (prefix entry &optional file-props)
8444 "Insert a source location into the routine info buffer.
8445 Start line with PREFIX. If a file name is inserted, add FILE-PROPS
8446 to it."
8447 (let* ((key (car entry))
8448 (file (nth 1 entry))
8449 (types (nth 2 entry))
8450 (shell-flag (assq 'compiled types))
8451 (buffer-flag (assq 'buffer types))
8452 (user-flag (assq 'user types))
8453 (lib-flag (assq 'lib types))
8454 (ndupl (or (and buffer-flag (idlwave-count-memq 'buffer types))
8455 (and user-flag (idlwave-count-memq 'user types))
8456 (and lib-flag (idlwave-count-memq 'lib types))
8457 1))
8458 (doflags t)
8459 beg special)
8460
8461 (insert prefix)
8462
8463 (cond
8464 ((eq key 'system)
8465 (setq doflags nil)
8466 (insert "System "))
8467
8468 ((eq key 'builtin)
8469 (setq doflags nil)
8470 (insert "Builtin "))
8471
8472 ((and (not file) shell-flag)
8473 (insert "Unresolved"))
8474
8475 ((null file)
8476 (insert "ERROR"))
8477
8478 ((idlwave-syslib-p file)
8479 (if (string-match "obsolete" (file-name-directory file))
8480 (insert "Obsolete ")
8481 (insert "SystemLib ")))
8482
8483 ;; New special syntax: taken directly from routine-info for
8484 ;; library catalog routines
8485 ((setq special (or (cdr lib-flag) (cdr user-flag)))
8486 (insert (format "%-10s" special)))
8487
8488 ;; Old special syntax: a matching regexp
8489 ((setq special (idlwave-special-lib-test file))
8490 (insert (format "%-10s" special)))
8491
8492 ;; Catch-all with file
8493 ((idlwave-lib-p file) (insert "Library "))
8494
8495 ;; Sanity catch all
8496 (t (insert "Other ")))
8497
8498 (when doflags
8499 (insert (concat
8500 " ["
8501 (if lib-flag "L" "-")
8502 (if user-flag "C" "-")
8503 (if shell-flag "S" "-")
8504 (if buffer-flag "B" "-")
8505 "] ")))
8506 (when (> ndupl 1)
8507 (setq beg (point))
8508 (insert (format "(%dx) " ndupl))
8509 (add-text-properties beg (point) (list 'face 'bold)))
8510 (when (and file (not (equal file "")))
8511 (setq beg (point))
8512 (insert (apply 'abbreviate-file-name
8513 (if (featurep 'xemacs) (list file t) (list file))))
8514 (if file-props
8515 (add-text-properties beg (point) file-props)))))
8516
8517 (defun idlwave-special-lib-test (file)
8518 "Check the path of FILE against the regexps which define special libs.
8519 Return the name of the special lib if there is a match."
8520 (let ((alist idlwave-special-lib-alist)
8521 entry rtn)
8522 (cond
8523 ((stringp file)
8524 (while (setq entry (pop alist))
8525 (if (string-match (car entry) file)
8526 (setq rtn (cdr entry)
8527 alist nil)))
8528 rtn)
8529 (t nil))))
8530
8531 (defun idlwave-mouse-active-rinfo-right (ev)
8532 (interactive "e")
8533 (idlwave-mouse-active-rinfo ev 'right))
8534
8535 (defun idlwave-mouse-active-rinfo-shift (ev)
8536 (interactive "e")
8537 (idlwave-mouse-active-rinfo ev nil 'shift))
8538
8539 (defun idlwave-active-rinfo-space ()
8540 (interactive)
8541 (idlwave-mouse-active-rinfo nil 'right))
8542
8543 (defun idlwave-mouse-active-rinfo (ev &optional right shift)
8544 "Do the mouse actions in the routine info buffer.
8545 Optional args RIGHT and SHIFT indicate, if mouse-3 was used, and if SHIFT
8546 was pressed."
8547 (interactive "e")
8548 (if ev (mouse-set-point ev))
8549 (let (data id name type class buf bufwin source link keyword
8550 word initial-class)
8551 (setq data (get-text-property (point) 'data)
8552 source (get-text-property (point) 'source)
8553 keyword (get-text-property (point) 'keyword)
8554 link (get-text-property (point) 'link)
8555 id (car data)
8556 name (nth 1 data) type (nth 2 data) class (nth 3 data)
8557 buf (nth 4 data)
8558 initial-class (nth 6 data)
8559 word (idlwave-this-word)
8560 bufwin (get-buffer-window buf t))
8561
8562 (cond ((eq id 'class) ; Switch class being displayed
8563 (if (window-live-p bufwin) (select-window bufwin))
8564 (idlwave-display-calling-sequence
8565 (idlwave-sintern-method name)
8566 type (idlwave-sintern-class word)
8567 initial-class))
8568 ((eq id 'usage) ; Online help on this routine
8569 (idlwave-online-help link name type class))
8570 ((eq id 'source) ; Source in help or buffer
8571 (if right ; In help
8572 (let ((idlwave-extra-help-function 'idlwave-help-with-source)
8573 (idlwave-help-source-try-header nil)
8574 ;; Fake idlwave-routines so help will find the right entry
8575 (idlwave-routines
8576 (list (list name type class source ""))))
8577 (idlwave-help-get-special-help name type class nil))
8578 ;; Otherwise just pop to the source
8579 (setq idlwave-popup-source (not idlwave-popup-source))
8580 (if idlwave-popup-source
8581 (condition-case err
8582 (idlwave-do-find-module name type class source)
8583 (error
8584 (setq idlwave-popup-source nil)
8585 (if (window-live-p bufwin) (select-window bufwin))
8586 (error (nth 1 err))))
8587 (if bufwin
8588 (select-window bufwin)
8589 (pop-to-buffer buf))
8590 (goto-char (marker-position idlwave-rinfo-marker)))))
8591 ((eq id 'keyword)
8592 (if right
8593 (idlwave-online-help link name type class keyword)
8594 (idlwave-rinfo-insert-keyword keyword buf shift))))))
8595
8596 (defun idlwave-rinfo-insert-keyword (keyword buffer &optional shift)
8597 "Insert KEYWORD in BUFFER. Make sure buffer is displayed in a window."
8598 (let ((bwin (get-buffer-window buffer)))
8599 (if idlwave-complete-empty-string-as-lower-case
8600 (setq keyword (downcase keyword)))
8601 (if bwin
8602 (select-window bwin)
8603 (pop-to-buffer buffer)
8604 (setq bwin (get-buffer-window buffer)))
8605 (if (eq (preceding-char) ?/)
8606 (insert keyword)
8607 (unless (save-excursion
8608 (re-search-backward
8609 "[(,][ \t]*\\(\\$[ \t]*\\(;.*\\)?\n\\)?[ \t]*\\="
8610 (min (- (point) 100) (point-min)) t))
8611 (insert ", "))
8612 (if shift (insert "/"))
8613 (insert keyword)
8614 (if (and (not shift)
8615 idlwave-keyword-completion-adds-equal)
8616 (insert "=")))))
8617
8618 (defun idlwave-list-buffer-load-path-shadows (&optional arg)
8619 "List the load path shadows of all routines defined in current buffer."
8620 (interactive "P")
8621 (idlwave-routines)
8622 (if (derived-mode-p 'idlwave-mode)
8623 (idlwave-list-load-path-shadows
8624 nil (idlwave-update-current-buffer-info 'save-buffer)
8625 "in current buffer")
8626 (error "Current buffer is not in idlwave-mode")))
8627
8628 (defun idlwave-list-shell-load-path-shadows (&optional arg)
8629 "List the load path shadows of all routines compiled under the shell.
8630 This is very useful for checking an IDL application. Just compile the
8631 application, do RESOLVE_ALL, and `C-c C-i' to compile all referenced
8632 routines and update IDLWAVE internal info. Then check for shadowing
8633 with this command."
8634 (interactive "P")
8635 (cond
8636 ((or (not (fboundp 'idlwave-shell-is-running))
8637 (not (idlwave-shell-is-running)))
8638 (error "Shell is not running"))
8639 ((null idlwave-compiled-routines)
8640 (error "No compiled routines. Maybe you need to update with `C-c C-i'"))
8641 (t
8642 (idlwave-list-load-path-shadows nil idlwave-compiled-routines
8643 "in the shell"))))
8644
8645 (defun idlwave-list-all-load-path-shadows (&optional arg)
8646 "List the load path shadows of all routines known to IDLWAVE."
8647 (interactive "P")
8648 (idlwave-list-load-path-shadows nil nil "globally"))
8649
8650 (defvar idlwave-sort-prefer-buffer-info t
8651 "Internal variable used to influence `idlwave-routine-twin-compare'.")
8652
8653 (defun idlwave-list-load-path-shadows (arg &optional special-routines loc)
8654 "List the routines which are defined multiple times.
8655 Search the information IDLWAVE has about IDL routines for multiple
8656 definitions.
8657 When SPECIAL-ROUTINES in non-nil, only look for shadows of these routines.
8658
8659 When IDL hits a routine call which is not defined, it will search on
8660 the load path in order to find a definition. The output of this command
8661 can be used to detect possible name clashes during this process."
8662 (idlwave-routines) ; Make sure everything is loaded.
8663 (unless (or idlwave-user-catalog-routines idlwave-library-catalog-routines)
8664 (or (y-or-n-p
8665 "You don't have any user or library catalogs. Continue anyway? ")
8666 (error "Abort")))
8667 (let* ((routines (append idlwave-system-routines
8668 idlwave-compiled-routines
8669 idlwave-library-catalog-routines
8670 idlwave-user-catalog-routines
8671 idlwave-buffer-routines
8672 nil))
8673 (km-prop (if (featurep 'xemacs) 'keymap 'local-map))
8674 (keymap (make-sparse-keymap))
8675 (props (list 'mouse-face 'highlight
8676 km-prop keymap
8677 'help-echo "Mouse2: Find source"))
8678 (nroutines (length (or special-routines routines)))
8679 (step (/ nroutines 100))
8680 (n 0)
8681 (cnt 0)
8682 (idlwave-sort-prefer-buffer-info nil)
8683 routine twins dtwins twin done props1 lroutines)
8684
8685 (if special-routines
8686 ;; Just looking for shadows of a few special routines
8687 (setq lroutines routines
8688 routines special-routines))
8689
8690 (message "Sorting routines...")
8691 (setq routines (sort routines
8692 (lambda (a b)
8693 (string< (downcase (idlwave-make-full-name
8694 (nth 2 a) (car a)))
8695 (downcase (idlwave-make-full-name
8696 (nth 2 b) (car b)))))))
8697 (message "Sorting routines...done")
8698
8699 (define-key keymap (if (featurep 'xemacs) [(button2)] [(mouse-2)])
8700 (lambda (ev)
8701 (interactive "e")
8702 (mouse-set-point ev)
8703 (apply 'idlwave-do-find-module
8704 (get-text-property (point) 'find-args))))
8705 (define-key keymap [(return)]
8706 (lambda ()
8707 (interactive)
8708 (apply 'idlwave-do-find-module
8709 (get-text-property (point) 'find-args))))
8710 (message "Compiling list...( 0%%)")
8711 (with-current-buffer (get-buffer-create "*Shadows*")
8712 (setq buffer-read-only nil)
8713 (erase-buffer)
8714 (while (setq routine (pop routines))
8715 (if (= (mod (setq n (1+ n)) step) 0)
8716 (message "Compiling list...(%2d%%)" (/ (* n 100) nroutines)))
8717
8718 ;; Get a list of all twins
8719 (setq twins (idlwave-routine-twins routine (or lroutines routines)))
8720 (if (memq routine done)
8721 (setq dtwins nil)
8722 (setq dtwins (idlwave-study-twins twins)))
8723 ;; Mark all twins as dealt with
8724 (setq done (append twins done))
8725 (when (or (> (length dtwins) 1)
8726 (> (idlwave-count-memq 'lib (nth 2 (car dtwins))) 1)
8727 (> (idlwave-count-memq 'user (nth 2 (car dtwins))) 1)
8728 (> (idlwave-count-memq 'buffer (nth 2 (car dtwins))) 1))
8729 (incf cnt)
8730 (insert (format "\n%s%s"
8731 (idlwave-make-full-name (nth 2 routine)
8732 (car routine))
8733 (if (eq (nth 1 routine) 'fun) "()" "")))
8734 (while (setq twin (pop dtwins))
8735 (setq props1 (append (list 'find-args
8736 (list (nth 0 routine)
8737 (nth 1 routine)
8738 (nth 2 routine)))
8739 props))
8740 (idlwave-insert-source-location "\n - " twin props1))))
8741 (goto-char (point-min))
8742 (setq buffer-read-only t))
8743 (setq loc (or loc ""))
8744 (if (> cnt 0)
8745 (progn
8746 (display-buffer (get-buffer "*Shadows*"))
8747 (message "%d case%s of shadowing found %s"
8748 cnt (if (= cnt 1) "" "s") loc))
8749 (message "No shadowing conflicts found %s" loc))))
8750
8751 (defun idlwave-print-source (routine)
8752 (let* ((source (nth 3 routine))
8753 (stype (car source))
8754 (sfile (idlwave-routine-source-file source)))
8755 (if (idlwave-syslib-p sfile) (setq stype 'syslib))
8756 (if (and (eq stype 'compiled)
8757 (or (not (stringp sfile))
8758 (not (string-match "\\S-" sfile))))
8759 (setq stype 'unresolved))
8760 (princ (format " %-10s %s\n"
8761 stype
8762 (if sfile sfile "No source code available")))))
8763
8764 (defun idlwave-routine-twins (entry &optional list)
8765 "Return all twin entries of ENTRY in LIST.
8766 LIST defaults to `idlwave-routines'.
8767 Twin entries are those which have the same name, type, and class.
8768 ENTRY will also be returned, as the first item of this list."
8769 (let* ((name (car entry))
8770 (type (nth 1 entry))
8771 (class (nth 2 entry))
8772 (candidates (idlwave-all-assq name (or list (idlwave-routines))))
8773 twins candidate)
8774 (while (setq candidate (pop candidates))
8775 (if (and (not (eq candidate entry))
8776 (eq type (nth 1 candidate))
8777 (eq class (nth 2 candidate)))
8778 (push candidate twins)))
8779 (if (setq candidate (idlwave-rinfo-assq name type class
8780 idlwave-unresolved-routines))
8781 (push candidate twins))
8782 (cons entry (nreverse twins))))
8783
8784 (defun idlwave-study-twins (entries)
8785 "Return dangerous twins of first entry in ENTRIES.
8786 Dangerous twins are routines with same name, but in different files on
8787 the load path. If a file is in the system library and has an entry in
8788 the `idlwave-system-routines' list, we omit the latter as
8789 non-dangerous because many IDL routines are implemented as library
8790 routines, and may have been scanned."
8791 (let* ((entry (car entries))
8792 (idlwave-twin-name (car entry)) ;
8793 (type (nth 1 entry)) ; Must be bound for
8794 (idlwave-twin-class (nth 2 entry)) ; idlwave-routine-twin-compare
8795 (cnt 0)
8796 source type type-cons file alist syslibp key)
8797 (while (setq entry (pop entries))
8798 (incf cnt)
8799 (setq source (nth 3 entry)
8800 type (car source)
8801 type-cons (cons type (nth 3 source))
8802 file (idlwave-routine-source-file source))
8803
8804 ;; Make KEY to index entry properly
8805 (setq key (cond ((eq type 'system) type)
8806 (file (file-truename file))
8807 (t 'unresolved)))
8808
8809 ;; Check for an entry in the system library
8810 (if (and file
8811 (not syslibp)
8812 (idlwave-syslib-p file))
8813 (setq syslibp t))
8814
8815 ;; If there's more than one matching entry for the same file, just
8816 ;; append the type-cons to the type list.
8817 (if (setq entry (assoc key alist))
8818 (push type-cons (nth 2 entry))
8819 (push (list key file (list type-cons)) alist)))
8820
8821 (setq alist (nreverse alist))
8822
8823 (when syslibp
8824 ;; File is in system *library* - remove any 'system entry
8825 (setq alist (delq (assq 'system alist) alist)))
8826
8827 ;; If 'system remains and we've scanned the syslib, it's a builtin
8828 ;; (rather than a !DIR/lib/.pro file bundled as source).
8829 (when (and (idlwave-syslib-scanned-p)
8830 (setq entry (assoc 'system alist)))
8831 (setcar entry 'builtin))
8832 (sort alist 'idlwave-routine-twin-compare)))
8833
8834 ;; FIXME: Dynamically scoped vars need to use the `idlwave-' prefix.
8835 ;; (defvar type)
8836 (defmacro idlwave-xor (a b)
8837 `(and (or ,a ,b)
8838 (not (and ,a ,b))))
8839
8840 (defun idlwave-routine-entry-compare (a b)
8841 "Compare two routine info entries for sorting.
8842 This is the general case. It first compares class, names, and type.
8843 If it turns out that A and B are twins (same name, class, and type),
8844 calls another routine which compares twins on the basis of their file
8845 names and path locations."
8846 (let ((name (car a)) (type (nth 1 a)) (class (nth 2 a)))
8847 (cond
8848 ((not (equal (idlwave-downcase-safe class)
8849 (idlwave-downcase-safe (nth 2 b))))
8850 ;; Class decides
8851 (cond ((null (nth 2 b)) nil)
8852 ((null class) t)
8853 (t (string< (downcase class) (downcase (nth 2 b))))))
8854 ((not (equal (downcase name) (downcase (car b))))
8855 ;; Name decides
8856 (string< (downcase name) (downcase (car b))))
8857 ((not (eq type (nth 1 b)))
8858 ;; Type decides
8859 (< (if (eq type 'fun) 1 0) (if (eq (nth 1 b) 'fun) 1 0)))
8860 (t
8861 ;; A and B are twins - so the decision is more complicated.
8862 ;; Call twin-compare with the proper arguments.
8863 (idlwave-routine-entry-compare-twins a b)))))
8864
8865 (defun idlwave-routine-entry-compare-twins (a b)
8866 "Compare two routine entries, under the assumption that they are twins.
8867 This basically calls `idlwave-routine-twin-compare' with the correct args."
8868 (let* ((idlwave-twin-name (car a))
8869 (type (nth 1 a))
8870 (idlwave-twin-class (nth 2 a)) ; used in idlwave-routine-twin-compare
8871 (asrc (nth 3 a))
8872 (atype (car asrc))
8873 (bsrc (nth 3 b))
8874 (btype (car bsrc))
8875 (afile (idlwave-routine-source-file asrc))
8876 (bfile (idlwave-routine-source-file bsrc)))
8877 (idlwave-routine-twin-compare
8878 (if (stringp afile)
8879 (list (file-truename afile) afile (list atype))
8880 (list atype afile (list atype)))
8881 (if (stringp bfile)
8882 (list (file-truename bfile) bfile (list btype))
8883 (list btype bfile (list btype))))))
8884
8885 ;; Bound in idlwave-study-twins,idlwave-routine-entry-compare-twins.
8886 (defvar idlwave-twin-class)
8887 (defvar idlwave-twin-name)
8888
8889 (defun idlwave-routine-twin-compare (a b)
8890 "Compare two routine twin entries for sorting.
8891 In here, A and B are not normal routine info entries, but special
8892 lists (KEY FILENAME (TYPES...)).
8893 This expects NAME TYPE IDLWAVE-TWIN-CLASS to be bound to the right values."
8894 (let* (;; Dis-assemble entries
8895 (akey (car a)) (bkey (car b))
8896 (afile (nth 1 a)) (bfile (nth 1 b))
8897 (atypes (nth 2 a)) (btypes (nth 2 b))
8898 ;; System routines?
8899 (asysp (memq akey '(builtin system)))
8900 (bsysp (memq bkey '(builtin system)))
8901 ;; Compiled routines?
8902 (acompp (memq 'compiled atypes))
8903 (bcompp (memq 'compiled btypes))
8904 ;; Unresolved?
8905 (aunresp (or (eq akey 'unresolved)
8906 (and acompp (not afile))))
8907 (bunresp (or (eq bkey 'unresolved)
8908 (and bcompp (not bfile))))
8909 ;; Buffer info available?
8910 (abufp (memq 'buffer atypes))
8911 (bbufp (memq 'buffer btypes))
8912 ;; On search path?
8913 (tpath-alist (idlwave-true-path-alist))
8914 (apathp (and (stringp akey)
8915 (assoc (file-name-directory akey) tpath-alist)))
8916 (bpathp (and (stringp bkey)
8917 (assoc (file-name-directory bkey) tpath-alist)))
8918 ;; How early on search path? High number means early since we
8919 ;; measure the tail of the path list
8920 (anpath (length (memq apathp tpath-alist)))
8921 (bnpath (length (memq bpathp tpath-alist)))
8922 ;; Look at file names
8923 (aname (if (stringp afile) (downcase (file-name-nondirectory afile)) ""))
8924 (bname (if (stringp bfile) (downcase (file-name-nondirectory bfile)) ""))
8925 (fname-re (if idlwave-twin-class
8926 (format "\\`%s__\\(%s\\|define\\)\\.pro\\'"
8927 (regexp-quote (downcase idlwave-twin-class))
8928 (regexp-quote (downcase idlwave-twin-name)))
8929 (format "\\`%s\\.pro" (regexp-quote (downcase idlwave-twin-name)))))
8930 ;; Is file name derived from the routine name?
8931 ;; Method file or class definition file?
8932 (anamep (string-match fname-re aname))
8933 (adefp (and idlwave-twin-class anamep
8934 (string= "define" (match-string 1 aname))))
8935 (bnamep (string-match fname-re bname))
8936 (bdefp (and idlwave-twin-class bnamep
8937 (string= "define" (match-string 1 bname)))))
8938
8939 ;; Now: follow JD's ideas about sorting. Looks really simple now,
8940 ;; doesn't it? The difficult stuff is hidden above...
8941 (cond
8942 ((idlwave-xor asysp bsysp) asysp) ; System entries first
8943 ((idlwave-xor aunresp bunresp) bunresp) ; Unresolved last
8944 ((and idlwave-sort-prefer-buffer-info
8945 (idlwave-xor abufp bbufp)) abufp) ; Buffers before non-buffers
8946 ((idlwave-xor acompp bcompp) acompp) ; Compiled entries
8947 ((idlwave-xor apathp bpathp) apathp) ; Library before non-library
8948 ((idlwave-xor anamep bnamep) anamep) ; Correct file names first
8949 ((and idlwave-twin-class anamep bnamep ; both file names match ->
8950 (idlwave-xor adefp bdefp)) bdefp) ; __define after __method
8951 ((> anpath bnpath) t) ; Who is first on path?
8952 (t nil)))) ; Default
8953
8954 (defun idlwave-routine-source-file (source)
8955 (if (nth 2 source)
8956 (expand-file-name (nth 1 source) (nth 2 source))
8957 (nth 1 source)))
8958
8959 (defun idlwave-downcase-safe (string)
8960 "Donwcase if string, else return unchanged."
8961 (if (stringp string)
8962 (downcase string)
8963 string))
8964
8965 (defun idlwave-count-eq (elt list)
8966 "How often is ELT in LIST?"
8967 (length (delq nil (mapcar (lambda (x) (eq x elt)) list))))
8968
8969 (defun idlwave-count-memq (elt alist)
8970 "How often is ELT a key in ALIST?"
8971 (length (delq nil (mapcar (lambda (x) (eq (car x) elt)) alist))))
8972
8973 (defun idlwave-syslib-p (file)
8974 "Non-nil if FILE is in the system library."
8975 (let* ((true-syslib (file-name-as-directory
8976 (file-truename
8977 (expand-file-name "lib" (idlwave-sys-dir)))))
8978 (true-file (file-truename file)))
8979 (string-match (concat "^" (regexp-quote true-syslib)) true-file)))
8980
8981 (defun idlwave-lib-p (file)
8982 "Non-nil if FILE is in the library."
8983 (let ((true-dir (file-name-directory (file-truename file))))
8984 (assoc true-dir (idlwave-true-path-alist))))
8985
8986 (defun idlwave-path-alist-add-flag (list-entry flag)
8987 "Add a flag to the path list entry, if not set."
8988 (let ((flags (cdr list-entry)))
8989 (add-to-list 'flags flag)
8990 (setcdr list-entry flags)))
8991
8992 (defun idlwave-path-alist-remove-flag (list-entry flag)
8993 "Remove a flag to the path list entry, if set."
8994 (let ((flags (delq flag (cdr list-entry))))
8995 (setcdr list-entry flags)))
8996
8997 (defun idlwave-true-path-alist ()
8998 "Return `idlwave-path-alist' alist with true-names.
8999 Info is cached, but relies on the functions setting `idlwave-path-alist'
9000 to reset the variable `idlwave-true-path-alist' to nil."
9001 (or idlwave-true-path-alist
9002 (setq idlwave-true-path-alist
9003 (mapcar (lambda(x) (cons
9004 (file-name-as-directory
9005 (file-truename
9006 (directory-file-name
9007 (car x))))
9008 (cdr x)))
9009 idlwave-path-alist))))
9010
9011 (defun idlwave-syslib-scanned-p ()
9012 "Non-nil if the system lib file !DIR/lib has been scanned."
9013 (let* ((true-syslib (file-name-as-directory
9014 (file-truename
9015 (expand-file-name "lib" (idlwave-sys-dir))))))
9016 (cdr (assoc true-syslib (idlwave-true-path-alist)))))
9017
9018 ;; ----------------------------------------------------------------------------
9019 ;;
9020 ;; Online Help display
9021
9022
9023 ;; ----------------------------------------------------------------------------
9024 ;;
9025 ;; Additions for use with imenu.el and func-menu.el
9026 ;; (pop-up a list of IDL units in the current file).
9027 ;;
9028
9029 (defun idlwave-prev-index-position ()
9030 "Search for the previous procedure or function.
9031 Return nil if not found. For use with imenu.el."
9032 (save-match-data
9033 (cond
9034 ((idlwave-find-key "\\<\\(pro\\|function\\)\\>" -1 'nomark))
9035 ;; ((idlwave-find-key idlwave-begin-unit-reg 1 'nomark)
9036 (t nil))))
9037
9038 (defun idlwave-unit-name ()
9039 "Return the unit name.
9040 Assumes that point is at the beginning of the unit as found by
9041 `idlwave-prev-index-position'."
9042 (forward-sexp 2)
9043 (forward-sexp -1)
9044 (let ((begin (point)))
9045 (re-search-forward
9046 "[a-zA-Z_][a-zA-Z0-9$_]+\\(::[a-zA-Z_][a-zA-Z0-9$_]+\\)?")
9047 (if (fboundp 'buffer-substring-no-properties)
9048 (buffer-substring-no-properties begin (point))
9049 (buffer-substring begin (point)))))
9050
9051 (defalias 'idlwave-function-menu
9052 (condition-case nil
9053 (progn
9054 (require 'func-menu)
9055 'function-menu)
9056 (error (condition-case nil
9057 (progn
9058 (require 'imenu)
9059 'imenu)
9060 (error nil)))))
9061
9062 ;; Here we hack func-menu.el in order to support this new mode.
9063 ;; The latest versions of func-menu.el already have this stuff in, so
9064 ;; we hack only if it is not already there.
9065 (when (fboundp 'eval-after-load)
9066 (eval-after-load "func-menu"
9067 '(progn
9068 (or (assq 'idlwave-mode fume-function-name-regexp-alist)
9069 (not (boundp 'fume-function-name-regexp-idl)) ; avoid problems
9070 (setq fume-function-name-regexp-alist
9071 (cons '(idlwave-mode . fume-function-name-regexp-idl)
9072 fume-function-name-regexp-alist)))
9073 (or (assq 'idlwave-mode fume-find-function-name-method-alist)
9074 (not (fboundp 'fume-find-next-idl-function-name)) ; avoid problems
9075 (setq fume-find-function-name-method-alist
9076 (cons '(idlwave-mode . fume-find-next-idl-function-name)
9077 fume-find-function-name-method-alist))))))
9078
9079 (defun idlwave-edit-in-idlde ()
9080 "Edit the current file in IDL Development environment."
9081 (interactive)
9082 (start-process "idldeclient" nil
9083 idlwave-shell-explicit-file-name "-c" "-e"
9084 (buffer-file-name)))
9085
9086 (defvar idlwave-help-use-assistant)
9087 (defun idlwave-launch-idlhelp ()
9088 "Start the IDLhelp application."
9089 (interactive)
9090 (if idlwave-help-use-assistant
9091 (idlwave-help-assistant-raise)
9092 (start-process "idlhelp" nil idlwave-help-application)))
9093
9094 ;; Menus - using easymenu.el
9095 (defvar idlwave-mode-menu-def
9096 `("IDLWAVE"
9097 ["PRO/FUNC menu" idlwave-function-menu t]
9098 ("Motion"
9099 ["Subprogram Start" idlwave-beginning-of-subprogram t]
9100 ["Subprogram End" idlwave-end-of-subprogram t]
9101 ["Block Start" idlwave-beginning-of-block t]
9102 ["Block End" idlwave-end-of-block t]
9103 ["Up Block" idlwave-backward-up-block t]
9104 ["Down Block" idlwave-down-block t]
9105 ["Skip Block Backward" idlwave-backward-block t]
9106 ["Skip Block Forward" idlwave-forward-block t])
9107 ("Mark"
9108 ["Subprogram" idlwave-mark-subprogram t]
9109 ["Block" idlwave-mark-block t]
9110 ["Header" idlwave-mark-doclib t])
9111 ("Format"
9112 ["Indent Entire Statement" idlwave-indent-statement
9113 :active t :keys "C-u \\[indent-for-tab-command]" ]
9114 ["Indent Subprogram" idlwave-indent-subprogram t]
9115 ["(Un)Comment Region" idlwave-toggle-comment-region t]
9116 ["Continue/Split line" idlwave-split-line t]
9117 "--"
9118 ["Toggle Auto Fill" idlwave-auto-fill-mode :style toggle
9119 :selected (symbol-value idlwave-fill-function)])
9120 ("Templates"
9121 ["Procedure" idlwave-procedure t]
9122 ["Function" idlwave-function t]
9123 ["Doc Header" idlwave-doc-header t]
9124 ["Log" idlwave-doc-modification t]
9125 "--"
9126 ["Case" idlwave-case t]
9127 ["For" idlwave-for t]
9128 ["Repeat" idlwave-repeat t]
9129 ["While" idlwave-while t]
9130 "--"
9131 ["Close Block" idlwave-close-block t])
9132 ("Completion"
9133 ["Complete" idlwave-complete t]
9134 ("Complete Specific"
9135 ["1 Procedure Name" (idlwave-complete 'procedure) t]
9136 ["2 Procedure Keyword" (idlwave-complete 'procedure-keyword) t]
9137 "--"
9138 ["3 Function Name" (idlwave-complete 'function) t]
9139 ["4 Function Keyword" (idlwave-complete 'function-keyword) t]
9140 "--"
9141 ["5 Procedure Method Name" (idlwave-complete 'procedure-method) t]
9142 ["6 Procedure Method Keyword" (idlwave-complete 'procedure-method-keyword) t]
9143 "--"
9144 ["7 Function Method Name" (idlwave-complete 'function-method) t]
9145 ["8 Function Method Keyword" (idlwave-complete 'function-method-keyword) t]
9146 "--"
9147 ["9 Class Name" idlwave-complete-class t]))
9148 ("Routine Info"
9149 ["Show Routine Info" idlwave-routine-info t]
9150 ["Online Context Help" idlwave-context-help t]
9151 "--"
9152 ["Find Routine Source" idlwave-find-module t]
9153 ["Resolve Routine" idlwave-resolve (featurep 'idlw-shell)]
9154 "--"
9155 ["Update Routine Info" idlwave-update-routine-info t]
9156 ["Rescan XML Help Catalog" idlwave-convert-xml-system-routine-info t]
9157 "--"
9158 "IDL User Catalog"
9159 ["Select Catalog Directories" (idlwave-create-user-catalog-file nil) t]
9160 ["Scan Directories" (idlwave-update-routine-info '(16))
9161 (and idlwave-path-alist (not idlwave-catalog-process))]
9162 ["Scan Directories &" (idlwave-update-routine-info '(64))
9163 (and idlwave-path-alist (not idlwave-catalog-process))]
9164 "--"
9165 "Routine Shadows"
9166 ["Check Current Buffer" idlwave-list-buffer-load-path-shadows t]
9167 ["Check Compiled Routines" idlwave-list-shell-load-path-shadows t]
9168 ["Check Everything" idlwave-list-all-load-path-shadows t])
9169 ("Misc"
9170 ["Kill auto-created buffers" idlwave-kill-autoloaded-buffers t]
9171 "--"
9172 ["Insert TAB character" idlwave-hard-tab t])
9173 "--"
9174 ("External"
9175 ["Start IDL shell" idlwave-shell t]
9176 ["Edit file in IDLDE" idlwave-edit-in-idlde t]
9177 ["Launch IDL Help" idlwave-launch-idlhelp t])
9178 "--"
9179 ("Customize"
9180 ["Browse IDLWAVE Group" idlwave-customize t]
9181 "--"
9182 ["Build Full Customize Menu" idlwave-create-customize-menu
9183 (fboundp 'customize-menu-create)])
9184 ("Documentation"
9185 ["Describe Mode" describe-mode t]
9186 ["Abbreviation List" idlwave-list-abbrevs t]
9187 "--"
9188 ["Commentary in idlwave.el" idlwave-show-commentary t]
9189 ["Commentary in idlw-shell.el" idlwave-shell-show-commentary t]
9190 "--"
9191 ["Info" idlwave-info t]
9192 "--"
9193 ["Help with Topic" idlwave-help-assistant-help-with-topic
9194 idlwave-help-use-assistant]
9195 ["Launch IDL Help" idlwave-launch-idlhelp t])))
9196
9197 (defvar idlwave-mode-debug-menu-def
9198 '("Debug"
9199 ["Start IDL shell" idlwave-shell t]
9200 ["Save and .RUN buffer" idlwave-shell-save-and-run
9201 (and (boundp 'idlwave-shell-automatic-start)
9202 idlwave-shell-automatic-start)]))
9203
9204 (if (or (featurep 'easymenu) (load "easymenu" t))
9205 (progn
9206 (easy-menu-define idlwave-mode-menu idlwave-mode-map
9207 "IDL and WAVE CL editing menu"
9208 idlwave-mode-menu-def)
9209 (easy-menu-define idlwave-mode-debug-menu idlwave-mode-map
9210 "IDL and WAVE CL editing menu"
9211 idlwave-mode-debug-menu-def)))
9212
9213 (defun idlwave-customize ()
9214 "Call the customize function with `idlwave' as argument."
9215 (interactive)
9216 ;; Try to load the code for the shell, so that we can customize it
9217 ;; as well.
9218 (or (featurep 'idlw-shell)
9219 (load "idlw-shell" t))
9220 (customize-browse 'idlwave))
9221
9222 (defun idlwave-create-customize-menu ()
9223 "Create a full customization menu for IDLWAVE, insert it into the menu."
9224 (interactive)
9225 (if (fboundp 'customize-menu-create)
9226 (progn
9227 ;; Try to load the code for the shell, so that we can customize it
9228 ;; as well.
9229 (or (featurep 'idlw-shell)
9230 (load "idlw-shell" t))
9231 (easy-menu-change
9232 '("IDLWAVE") "Customize"
9233 `(["Browse IDLWAVE group" idlwave-customize t]
9234 "--"
9235 ,(customize-menu-create 'idlwave)
9236 ["Set" Custom-set t]
9237 ["Save" Custom-save t]
9238 ["Reset to Current" Custom-reset-current t]
9239 ["Reset to Saved" Custom-reset-saved t]
9240 ["Reset to Standard Settings" Custom-reset-standard t]))
9241 (message "\"IDLWAVE\"-menu now contains full customization menu"))
9242 (error "Cannot expand menu (outdated version of cus-edit.el)")))
9243
9244 (defun idlwave-show-commentary ()
9245 "Use the finder to view the file documentation from `idlwave.el'."
9246 (interactive)
9247 (finder-commentary "idlwave.el"))
9248
9249 (defun idlwave-shell-show-commentary ()
9250 "Use the finder to view the file documentation from `idlw-shell.el'."
9251 (interactive)
9252 (finder-commentary "idlw-shell.el"))
9253
9254 (defun idlwave-info ()
9255 "Read documentation for IDLWAVE in the info system."
9256 (interactive)
9257 (info "idlwave"))
9258
9259 (defun idlwave-list-abbrevs (arg)
9260 "Show the code abbreviations define in IDLWAVE mode.
9261 This lists all abbrevs where the replacement text differs from the input text.
9262 These are the ones the users want to learn to speed up their writing.
9263
9264 The function does *not* list abbrevs which replace a word with itself
9265 to call a hook. These hooks are used to change the case of words or
9266 to blink the matching `begin', and the user does not need to know them.
9267
9268 With arg, list all abbrevs with the corresponding hook.
9269
9270 This function was written since `list-abbrevs' looks terrible for IDLWAVE mode."
9271
9272 (interactive "P")
9273 (let ((table (symbol-value 'idlwave-mode-abbrev-table))
9274 abbrevs
9275 str rpl func fmt (len-str 0) (len-rpl 0))
9276 (mapatoms
9277 (lambda (sym)
9278 (if (symbol-value sym)
9279 (progn
9280 (setq str (symbol-name sym)
9281 rpl (symbol-value sym)
9282 func (symbol-function sym))
9283 (if arg
9284 (setq func (prin1-to-string func))
9285 (if (and (listp func) (stringp (nth 2 func)))
9286 (setq rpl (concat "EVAL: " (nth 2 func))
9287 func "")
9288 (setq func "")))
9289 (if (or arg (not (string= rpl str)))
9290 (progn
9291 (setq len-str (max len-str (length str)))
9292 (setq len-rpl (max len-rpl (length rpl)))
9293 (setq abbrevs (cons (list str rpl func) abbrevs)))))))
9294 table)
9295 ;; sort the list
9296 (setq abbrevs (sort abbrevs (lambda (a b) (string< (car a) (car b)))))
9297 ;; Make the format
9298 (setq fmt (format "%%-%ds %%-%ds %%s\n" len-str len-rpl))
9299 (with-output-to-temp-buffer "*Help*"
9300 (if arg
9301 (progn
9302 (princ "Abbreviations and Actions in IDLWAVE-Mode\n")
9303 (princ "=========================================\n\n")
9304 (princ (format fmt "KEY" "REPLACE" "HOOK"))
9305 (princ (format fmt "---" "-------" "----")))
9306 (princ "Code Abbreviations and Templates in IDLWAVE-Mode\n")
9307 (princ "================================================\n\n")
9308 (princ (format fmt "KEY" "ACTION" ""))
9309 (princ (format fmt "---" "------" "")))
9310 (mapcar
9311 (lambda (list)
9312 (setq str (car list)
9313 rpl (nth 1 list)
9314 func (nth 2 list))
9315 (princ (format fmt str rpl func)))
9316 abbrevs)))
9317 ;; Make sure each abbreviation uses only one display line
9318 (with-current-buffer "*Help*"
9319 (setq truncate-lines t)))
9320
9321 ;; Add .pro files to speedbar for support, if it's loaded
9322 (eval-after-load "speedbar" '(speedbar-add-supported-extension ".pro"))
9323
9324 ;; Set an idle timer to load the routine info.
9325 ;; Will only work on systems which support this.
9326 (or idlwave-routines (idlwave-start-load-rinfo-timer))
9327
9328 ;; Run the hook
9329 (run-hooks 'idlwave-load-hook)
9330
9331 (provide 'idlwave)
9332
9333 ;;; idlwave.el ends here