Add 2012 to FSF copyright years for Emacs files
[bpt/emacs.git] / lisp / progmodes / idlwave.el
1 ;; idlwave.el --- IDL editing mode for GNU Emacs
2
3 ;; Copyright (C) 1999-2012 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 preceded 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 completion 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 preceding 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 'abbrev-all-caps) t)
1911 (set (make-local-variable 'indent-tabs-mode) nil)
1912 (set (make-local-variable 'completion-ignore-case) t)
1913
1914 (when (featurep 'easymenu)
1915 (easy-menu-add idlwave-mode-menu idlwave-mode-map)
1916 (easy-menu-add idlwave-mode-debug-menu idlwave-mode-map))
1917
1918 (setq abbrev-mode t)
1919
1920 (set (make-local-variable idlwave-fill-function) 'idlwave-auto-fill)
1921 (setq comment-end "")
1922 (set (make-local-variable 'comment-multi-line) nil)
1923 (set (make-local-variable 'paragraph-separate)
1924 "[ \t\f]*$\\|[ \t]*;+[ \t]*$\\|;+[+=-_*]+$")
1925 (set (make-local-variable 'paragraph-start) "[ \t\f]\\|[ \t]*;+[ \t]")
1926 (set (make-local-variable 'paragraph-ignore-fill-prefix) nil)
1927 (set (make-local-variable 'parse-sexp-ignore-comments) t)
1928
1929 ;; ChangeLog
1930 (set (make-local-variable 'add-log-current-defun-function)
1931 'idlwave-current-routine-fullname)
1932
1933 ;; Set tag table list to use IDLTAGS as file name.
1934 (if (boundp 'tag-table-alist)
1935 (add-to-list 'tag-table-alist '("\\.pro$" . "IDLTAGS")))
1936
1937 ;; Font-lock additions
1938 ;; Following line is for Emacs - XEmacs uses the corresponding property
1939 ;; on the `idlwave-mode' symbol.
1940 (set (make-local-variable 'font-lock-defaults) idlwave-font-lock-defaults)
1941 (set (make-local-variable 'font-lock-mark-block-function)
1942 'idlwave-mark-subprogram)
1943 (set (make-local-variable 'font-lock-fontify-region-function)
1944 'idlwave-font-lock-fontify-region)
1945
1946 ;; Imenu setup
1947 (set (make-local-variable 'imenu-create-index-function)
1948 'imenu-default-create-index-function)
1949 (set (make-local-variable 'imenu-extract-index-name-function)
1950 'idlwave-unit-name)
1951 (set (make-local-variable 'imenu-prev-index-position-function)
1952 'idlwave-prev-index-position)
1953
1954 ;; HideShow setup
1955 (add-to-list 'hs-special-modes-alist
1956 (list 'idlwave-mode
1957 idlwave-begin-block-reg
1958 idlwave-end-block-reg
1959 ";"
1960 'idlwave-forward-block nil))
1961
1962 ;; Make a local post-command-hook and add our hook to it
1963 ;; NB: `make-local-hook' needed for older/alternative Emacs compatibility
1964 ;; (make-local-hook 'post-command-hook)
1965 (add-hook 'post-command-hook 'idlwave-command-hook nil 'local)
1966
1967 ;; Make local hooks for buffer updates
1968 ;; NB: `make-local-hook' needed for older/alternative Emacs compatibility
1969 ;; (make-local-hook 'kill-buffer-hook)
1970 (add-hook 'kill-buffer-hook 'idlwave-kill-buffer-update nil 'local)
1971 ;; (make-local-hook 'after-save-hook)
1972 (add-hook 'after-save-hook 'idlwave-save-buffer-update nil 'local)
1973 (add-hook 'after-save-hook 'idlwave-revoke-license-to-kill nil 'local)
1974
1975 ;; Setup directories and file, if necessary
1976 (idlwave-setup)
1977
1978 ;; Update the routine info with info about current buffer?
1979 (idlwave-new-buffer-update)
1980
1981 ;; Check help location
1982 (idlwave-help-check-locations))
1983
1984 (defvar idlwave-setup-done nil)
1985 (defun idlwave-setup ()
1986 (unless idlwave-setup-done
1987 (if (not (file-directory-p idlwave-config-directory))
1988 (make-directory idlwave-config-directory))
1989 (setq
1990 idlwave-user-catalog-file (expand-file-name
1991 idlwave-user-catalog-file
1992 idlwave-config-directory)
1993 idlwave-xml-system-rinfo-converted-file
1994 (expand-file-name
1995 idlwave-xml-system-rinfo-converted-file
1996 idlwave-config-directory)
1997 idlwave-path-file (expand-file-name
1998 idlwave-path-file
1999 idlwave-config-directory))
2000 (idlwave-read-paths) ; we may need these early
2001 (setq idlwave-setup-done t)))
2002
2003 (defun idlwave-font-lock-fontify-region (beg end &optional verbose)
2004 "Fontify continuation lines correctly."
2005 (let (pos)
2006 (save-excursion
2007 (goto-char beg)
2008 (forward-line -1)
2009 (when (setq pos (idlwave-is-continuation-line))
2010 (goto-char pos)
2011 (idlwave-beginning-of-statement)
2012 (setq beg (point)))))
2013 (font-lock-default-fontify-region beg end verbose))
2014
2015 ;;
2016 ;; Code Formatting ----------------------------------------------------
2017 ;;
2018
2019 (defun idlwave-hard-tab ()
2020 "Insert TAB in buffer in current position."
2021 (interactive)
2022 (insert "\t"))
2023
2024 ;;; This stuff is experimental
2025
2026 (defvar idlwave-command-hook nil
2027 "If non-nil, a list that can be evaluated using `eval'.
2028 It is evaluated in the lisp function `idlwave-command-hook' which is
2029 placed in `post-command-hook'.")
2030
2031 (defun idlwave-command-hook ()
2032 "Command run after every command.
2033 Evaluates a non-nil value of the *variable* `idlwave-command-hook' and
2034 sets the variable to zero afterwards."
2035 (and idlwave-command-hook
2036 (listp idlwave-command-hook)
2037 (condition-case nil
2038 (eval idlwave-command-hook)
2039 (error nil)))
2040 (setq idlwave-command-hook nil))
2041
2042 ;;; End experiment
2043
2044 ;; It would be better to use expand.el for better abbrev handling and
2045 ;; versatility.
2046
2047 (defun idlwave-check-abbrev (arg &optional reserved)
2048 "Reverse abbrev expansion if in comment or string.
2049 Argument ARG is the number of characters to move point
2050 backward if `idlwave-abbrev-move' is non-nil.
2051 If optional argument RESERVED is non-nil then the expansion
2052 consists of reserved words, which will be capitalized if
2053 `idlwave-reserved-word-upcase' is non-nil.
2054 Otherwise, the abbrev will be capitalized if `idlwave-abbrev-change-case'
2055 is non-nil, unless its value is \`down in which case the abbrev will be
2056 made into all lowercase.
2057 Returns non-nil if abbrev is left expanded."
2058 (if (idlwave-quoted)
2059 (progn (unexpand-abbrev)
2060 nil)
2061 (if (and reserved idlwave-reserved-word-upcase)
2062 (upcase-region last-abbrev-location (point))
2063 (cond
2064 ((equal idlwave-abbrev-change-case 'down)
2065 (downcase-region last-abbrev-location (point)))
2066 (idlwave-abbrev-change-case
2067 (upcase-region last-abbrev-location (point)))))
2068 (if (and idlwave-abbrev-move (> arg 0))
2069 (if (boundp 'post-command-hook)
2070 (setq idlwave-command-hook (list 'backward-char (1+ arg)))
2071 (backward-char arg)))
2072 t))
2073
2074 (defun idlwave-in-comment ()
2075 "Return t if point is inside a comment, nil otherwise."
2076 (save-excursion
2077 (let ((here (point)))
2078 (and (idlwave-goto-comment) (> here (point))))))
2079
2080 (defun idlwave-goto-comment ()
2081 "Move to start of comment delimiter on current line.
2082 Moves to end of line if there is no comment delimiter.
2083 Ignores comment delimiters in strings.
2084 Returns point if comment found and nil otherwise."
2085 (let ((eos (point-at-eol))
2086 (data (match-data))
2087 found)
2088 ;; Look for first comment delimiter not in a string
2089 (beginning-of-line)
2090 (setq found (search-forward comment-start eos 'lim))
2091 (while (and found (idlwave-in-quote))
2092 (setq found (search-forward comment-start eos 'lim)))
2093 (store-match-data data)
2094 (and found (not (idlwave-in-quote))
2095 (progn
2096 (backward-char 1)
2097 (point)))))
2098
2099 (defun idlwave-region-active-p ()
2100 "Should we operate on an active region?"
2101 (if (fboundp 'use-region-p)
2102 (use-region-p)
2103 (region-active-p)))
2104
2105 (defun idlwave-show-matching-quote ()
2106 "Insert quote and show matching quote if this is end of a string."
2107 (interactive)
2108 (let ((bq (idlwave-in-quote))
2109 (inq last-command-event))
2110 (if (and bq (not (idlwave-in-comment)))
2111 (let ((delim (char-after bq)))
2112 (insert inq)
2113 (if (eq inq delim)
2114 (save-excursion
2115 (goto-char bq)
2116 (sit-for 1))))
2117 ;; Not the end of a string
2118 (insert inq))))
2119
2120 (defun idlwave-show-begin-check ()
2121 "Ensure that the previous word was a token before `idlwave-show-begin'.
2122 An END token must be preceded by whitespace."
2123 (if (not (idlwave-quoted))
2124 (if
2125 (save-excursion
2126 (backward-word 1)
2127 (backward-char 1)
2128 (looking-at "[ \t\n\f]"))
2129 (idlwave-show-begin))))
2130
2131 (defun idlwave-show-begin ()
2132 "Find the start of current block and blinks to it for a second.
2133 Also checks if the correct END statement has been used."
2134 ;; All end statements are reserved words
2135 ;; Re-indent end line
2136 ;;(insert-char ?\ 1) ;; So indent, etc. work well
2137 ;;(backward-char 1)
2138 (let* ((pos (point-marker))
2139 (last-abbrev-marker (copy-marker last-abbrev-location))
2140 (eol-pos (point-at-eol))
2141 begin-pos end-pos end end1 )
2142 (if idlwave-reindent-end (idlwave-indent-line))
2143 (setq last-abbrev-location (marker-position last-abbrev-marker))
2144 (when (and (idlwave-check-abbrev 0 t)
2145 idlwave-show-block)
2146 (save-excursion
2147 ;; Move inside current block
2148 (goto-char last-abbrev-marker)
2149 (idlwave-block-jump-out -1 'nomark)
2150 (setq begin-pos (point))
2151 (idlwave-block-jump-out 1 'nomark)
2152 (setq end-pos (point))
2153 (if (> end-pos eol-pos)
2154 (setq end-pos pos))
2155 (goto-char end-pos)
2156 (setq end (buffer-substring
2157 (progn
2158 (skip-chars-backward "a-zA-Z")
2159 (point))
2160 end-pos))
2161 (goto-char begin-pos)
2162 (when (setq end1 (cdr (idlwave-block-master)))
2163 (cond
2164 ((null end1)) ; no-operation
2165 ((string= (downcase end) (downcase end1))
2166 (sit-for 1))
2167 ((string= (downcase end) "end")
2168 ;; A generic end
2169 (if idlwave-expand-generic-end
2170 (save-excursion
2171 (goto-char pos)
2172 (backward-char 3)
2173 (insert (if (string= end "END") (upcase end1) end1))
2174 (delete-char 3)))
2175 (sit-for 1))
2176 (t
2177 (beep)
2178 (message "Warning: Shouldn't this be \"%s\" instead of \"%s\"?"
2179 end1 end)
2180 (sit-for 1))))))))
2181 ;;(delete-char 1))
2182
2183 (defun idlwave-block-master ()
2184 (let ((case-fold-search t))
2185 (save-excursion
2186 (cond
2187 ((looking-at "pro\\|case\\|switch\\|function\\>")
2188 (assoc (downcase (match-string 0)) idlwave-block-matches))
2189 ((looking-at "begin\\>")
2190 (let ((limit (save-excursion
2191 (idlwave-beginning-of-statement)
2192 (point))))
2193 (cond
2194 ((re-search-backward ":[ \t]*\\=" limit t)
2195 ;; seems to be a case thing
2196 '("begin" . "end"))
2197 ((re-search-backward idlwave-block-match-regexp limit t)
2198 (assoc (downcase (match-string 1))
2199 idlwave-block-matches))
2200 (t
2201 ;; Just a normal block
2202 '("begin" . "end")))))
2203 (t nil)))))
2204
2205 (defun idlwave-close-block ()
2206 "Terminate the current block with the correct END statement."
2207 (interactive)
2208 ;; Start new line if we are not in a new line
2209 (unless (save-excursion
2210 (skip-chars-backward " \t")
2211 (bolp))
2212 (let ((idlwave-show-block nil))
2213 (newline-and-indent)))
2214 (let ((last-abbrev-location (point))) ; for upcasing
2215 (insert "end")
2216 (idlwave-show-begin)))
2217
2218 (defun idlwave-custom-ampersand-surround (&optional is-action)
2219 "Surround &, leaving room for && (which surround as well)."
2220 (let* ((prev-char (char-after (- (point) 2)))
2221 (next-char (char-after (point)))
2222 (amp-left (eq prev-char ?&))
2223 (amp-right (eq next-char ?&))
2224 (len (if amp-left 2 1)))
2225 (unless amp-right ;no need to do it twice, amp-left will catch it.
2226 (idlwave-surround -1 (if (or is-action amp-left) -1) len))))
2227
2228 (defun idlwave-custom-ltgtr-surround (gtr &optional is-action)
2229 "Surround > and < by blanks, leaving room for >= and <=, and considering ->."
2230 (let* ((prev-char (char-after (- (point) 2)))
2231 (next-char (char-after (point)))
2232 (method-invoke (and gtr (eq prev-char ?-)))
2233 (len (if method-invoke 2 1)))
2234 (unless (eq next-char ?=)
2235 ;; Key binding: pad only on left, to save for possible >=/<=
2236 (idlwave-surround -1 (if (or is-action method-invoke) -1) len))))
2237
2238 (defun idlwave-surround (&optional before after length is-action)
2239 "Surround the LENGTH characters before point with blanks.
2240 LENGTH defaults to 1.
2241 Optional arguments BEFORE and AFTER affect the behavior before and
2242 after the characters (see also description of `idlwave-make-space'):
2243
2244 nil do nothing
2245 0 force no spaces
2246 integer > 0 force exactly n spaces
2247 integer < 0 at least |n| spaces
2248
2249 The function does nothing if any of the following conditions is true:
2250 - `idlwave-surround-by-blank' is nil
2251 - the character before point is inside a string or comment"
2252 (when (and idlwave-surround-by-blank (not (idlwave-quoted)))
2253 (let ((length (or length 1))) ; establish a default for LENGTH
2254 (backward-char length)
2255 (save-restriction
2256 (let ((here (point)))
2257 (skip-chars-backward " \t")
2258 (if (bolp)
2259 ;; avoid clobbering indent
2260 (progn
2261 (move-to-column (idlwave-calculate-indent))
2262 (if (<= (point) here)
2263 (narrow-to-region (point) here))
2264 (goto-char here)))
2265 (idlwave-make-space before))
2266 (skip-chars-forward " \t"))
2267 (forward-char length)
2268 (idlwave-make-space after)
2269 ;; Check to see if the line should auto wrap
2270 (if (and (equal (char-after (1- (point))) ?\ )
2271 (> (current-column) fill-column))
2272 (funcall auto-fill-function)))))
2273
2274 (defun idlwave-make-space (n)
2275 "Make space at point.
2276 The space affected is all the spaces and tabs around point.
2277 If n is non-nil then point is left abs(n) spaces from the beginning of
2278 the contiguous space.
2279 The amount of space at point is determined by N.
2280 If the value of N is:
2281 nil - do nothing.
2282 > 0 - exactly N spaces.
2283 < 0 - a minimum of -N spaces, i.e., do not change if there are
2284 already -N spaces.
2285 0 - no spaces (i.e. remove any existing space)."
2286 (if (integerp n)
2287 (let
2288 ((start-col (progn (skip-chars-backward " \t") (current-column)))
2289 (left (point))
2290 (end-col (progn (skip-chars-forward " \t") (current-column))))
2291 (delete-horizontal-space)
2292 (cond
2293 ((> n 0)
2294 (idlwave-indent-to (+ start-col n))
2295 (goto-char (+ left n)))
2296 ((< n 0)
2297 (idlwave-indent-to end-col (- n))
2298 (goto-char (- left n)))
2299 ;; n = 0, done
2300 ))))
2301
2302 (defun idlwave-newline ()
2303 "Insert a newline and indent the current and previous line."
2304 (interactive)
2305 ;;
2306 ;; Handle unterminated single and double quotes
2307 ;; If not in a comment and in a string then insertion of a newline
2308 ;; will mean unbalanced quotes.
2309 ;;
2310 (if (and (not (idlwave-in-comment)) (idlwave-in-quote))
2311 (progn (beep)
2312 (message "Warning: unbalanced quotes?")))
2313 (newline)
2314 ;;
2315 ;; The current line is being split, the cursor should be at the
2316 ;; beginning of the new line skipping the leading indentation.
2317 ;;
2318 ;; The reason we insert the new line before indenting is that the
2319 ;; indenting could be confused by keywords (e.g. END) on the line
2320 ;; after the split point. This prevents us from just using
2321 ;; `indent-for-tab-command' followed by `newline-and-indent'.
2322 ;;
2323 (beginning-of-line 0)
2324 (idlwave-indent-line)
2325 (forward-line)
2326 (idlwave-indent-line))
2327
2328 ;;
2329 ;; Use global variable 'comment-column' to set parallel comment
2330 ;;
2331 ;; Modeled on lisp.el
2332 ;; Emacs Lisp and IDL (Wave CL) have identical comment syntax
2333 (defun idlwave-comment-hook ()
2334 "Compute indent for the beginning of the IDL comment delimiter."
2335 (if (or (looking-at idlwave-no-change-comment)
2336 (looking-at (or idlwave-begin-line-comment "^;")))
2337 (current-column)
2338 (if (looking-at idlwave-code-comment)
2339 (if (save-excursion (skip-chars-backward " \t") (bolp))
2340 ;; On line by itself, indent as code
2341 (let ((tem (idlwave-calculate-indent)))
2342 (if (listp tem) (car tem) tem))
2343 ;; after code - do not change
2344 (current-column))
2345 (skip-chars-backward " \t")
2346 (max (if (bolp) 0 (1+ (current-column)))
2347 comment-column))))
2348
2349 (defun idlwave-split-line ()
2350 "Continue line by breaking line at point and indent the lines.
2351 For a code line insert continuation marker. If the line is a line comment
2352 then the new line will contain a comment with the same indentation.
2353 Splits strings with the IDL operator `+' if `idlwave-split-line-string' is
2354 non-nil."
2355 (interactive)
2356 ;; Expand abbreviation, just like normal RET would.
2357 (and abbrev-mode (expand-abbrev))
2358 (let (beg)
2359 (if (not (idlwave-in-comment))
2360 ;; For code line add continuation.
2361 ;; Check if splitting a string.
2362 (progn
2363 (if (setq beg (idlwave-in-quote))
2364 (if idlwave-split-line-string
2365 ;; Split the string.
2366 (progn (insert (setq beg (char-after beg)) " + "
2367 idlwave-continuation-char beg)
2368 (backward-char 1)
2369 (newline-and-indent)
2370 (forward-char 1))
2371 ;; Do not split the string.
2372 (beep)
2373 (message "Warning: continuation inside string!!")
2374 (insert " " idlwave-continuation-char))
2375 ;; Not splitting a string.
2376 (if (not (member (char-before) '(?\ ?\t)))
2377 (insert " "))
2378 (insert idlwave-continuation-char)
2379 (newline-and-indent)))
2380 (indent-new-comment-line))
2381 ;; Indent previous line
2382 (setq beg (- (point-max) (point)))
2383 (forward-line -1)
2384 (idlwave-indent-line)
2385 (goto-char (- (point-max) beg))
2386 ;; Reindent new line
2387 (idlwave-indent-line)))
2388
2389 (defun idlwave-beginning-of-subprogram (&optional nomark)
2390 "Move point to the beginning of the current program unit.
2391 If NOMARK is non-nil, do not push mark."
2392 (interactive)
2393 (idlwave-find-key idlwave-begin-unit-reg -1 nomark))
2394
2395 (defun idlwave-end-of-subprogram (&optional nomark)
2396 "Move point to the start of the next program unit.
2397 If NOMARK is non-nil, do not push mark."
2398 (interactive)
2399 (idlwave-end-of-statement)
2400 (idlwave-find-key idlwave-end-unit-reg 1 nomark))
2401
2402 (defun idlwave-mark-statement ()
2403 "Mark current IDL statement."
2404 (interactive)
2405 (idlwave-end-of-statement)
2406 (let ((end (point)))
2407 (idlwave-beginning-of-statement)
2408 (push-mark end nil t)))
2409
2410 (defun idlwave-mark-block ()
2411 "Mark containing block."
2412 (interactive)
2413 (idlwave-end-of-statement)
2414 (idlwave-backward-up-block -1)
2415 (idlwave-end-of-statement)
2416 (let ((end (point)))
2417 (idlwave-backward-block)
2418 (idlwave-beginning-of-statement)
2419 (push-mark end nil t)))
2420
2421
2422 (defun idlwave-mark-subprogram ()
2423 "Put mark at beginning of program, point at end.
2424 The marks are pushed."
2425 (interactive)
2426 (idlwave-end-of-statement)
2427 (idlwave-beginning-of-subprogram)
2428 (let ((beg (point)))
2429 (idlwave-forward-block)
2430 (push-mark beg nil t))
2431 (exchange-point-and-mark))
2432
2433 (defun idlwave-backward-up-block (&optional arg)
2434 "Move to beginning of enclosing block if prefix ARG >= 0.
2435 If prefix ARG < 0 then move forward to enclosing block end."
2436 (interactive "p")
2437 (idlwave-block-jump-out (- arg) 'nomark))
2438
2439 (defun idlwave-beginning-of-block ()
2440 "Go to the beginning of the current block."
2441 (interactive)
2442 (idlwave-block-jump-out -1 'nomark)
2443 (forward-word 1))
2444
2445 (defun idlwave-end-of-block ()
2446 "Go to the beginning of the current block."
2447 (interactive)
2448 (idlwave-block-jump-out 1 'nomark)
2449 (backward-word 1))
2450
2451 (defun idlwave-forward-block (&optional arg)
2452 "Move across next nested block."
2453 (interactive)
2454 (let ((arg (or arg 1)))
2455 (if (idlwave-down-block arg)
2456 (idlwave-block-jump-out arg 'nomark))))
2457
2458 (defun idlwave-backward-block ()
2459 "Move backward across previous nested block."
2460 (interactive)
2461 (if (idlwave-down-block -1)
2462 (idlwave-block-jump-out -1 'nomark)))
2463
2464 (defun idlwave-down-block (&optional arg)
2465 "Go down a block.
2466 With ARG: ARG >= 0 go forwards, ARG < 0 go backwards.
2467 Returns non-nil if successful."
2468 (interactive "p")
2469 (let (status)
2470 (if (< arg 0)
2471 ;; Backward
2472 (let ((eos (save-excursion
2473 (idlwave-block-jump-out -1 'nomark)
2474 (point))))
2475 (if (setq status (idlwave-find-key
2476 idlwave-end-block-reg -1 'nomark eos))
2477 (idlwave-beginning-of-statement)
2478 (message "No nested block before beginning of containing block.")))
2479 ;; Forward
2480 (let ((eos (save-excursion
2481 (idlwave-block-jump-out 1 'nomark)
2482 (point))))
2483 (if (setq status (idlwave-find-key
2484 idlwave-begin-block-reg 1 'nomark eos))
2485 (idlwave-end-of-statement)
2486 (message "No nested block before end of containing block."))))
2487 status))
2488
2489 (defun idlwave-mark-doclib ()
2490 "Put point at beginning of doc library header, mark at end.
2491 The marks are pushed."
2492 (interactive)
2493 (let (beg
2494 (here (point)))
2495 (goto-char (point-max))
2496 (if (re-search-backward idlwave-doclib-start nil t)
2497 (progn
2498 (setq beg (progn (beginning-of-line) (point)))
2499 (if (re-search-forward idlwave-doclib-end nil t)
2500 (progn
2501 (forward-line 1)
2502 (push-mark beg nil t)
2503 (message "Could not find end of doc library header.")))
2504 (message "Could not find doc library header start.")
2505 (goto-char here)))))
2506
2507 (defun idlwave-current-routine-fullname ()
2508 (let ((name (idlwave-current-routine)))
2509 (idlwave-make-full-name (nth 2 name) (car name))))
2510
2511 (defun idlwave-current-routine ()
2512 "Return (NAME TYPE CLASS) of current routine."
2513 (idlwave-routines)
2514 (save-excursion
2515 (idlwave-beginning-of-subprogram 'nomark)
2516 (if (looking-at "[ \t]*\\<\\(pro\\|function\\)\\>\\s-+\\(\\([a-zA-Z0-9$_]+\\)::\\)?\\([a-zA-Z0-9$_]+\\)")
2517 (let* ((type (if (string= (downcase (match-string 1)) "pro")
2518 'pro 'function))
2519 (class (idlwave-sintern-class (match-string 3)))
2520 (name (idlwave-sintern-routine-or-method (match-string 4) class)))
2521 (list name type class)))))
2522
2523 (defvar idlwave-shell-prompt-pattern)
2524 (defun idlwave-beginning-of-statement ()
2525 "Move to beginning of the current statement.
2526 Skips back past statement continuations.
2527 Point is placed at the beginning of the line whether or not this is an
2528 actual statement."
2529 (interactive)
2530 (cond
2531 ((derived-mode-p 'idlwave-shell-mode)
2532 (if (re-search-backward idlwave-shell-prompt-pattern nil t)
2533 (goto-char (match-end 0))))
2534 (t
2535 (if (save-excursion (forward-line -1) (idlwave-is-continuation-line))
2536 (idlwave-previous-statement)
2537 (beginning-of-line)))))
2538
2539 (defun idlwave-previous-statement ()
2540 "Move point to beginning of the previous statement.
2541 Returns t if the current line before moving is the beginning of
2542 the first non-comment statement in the file, and nil otherwise."
2543 (interactive)
2544 (let (first-statement)
2545 (if (not (= (forward-line -1) 0))
2546 ;; first line in file
2547 t
2548 ;; skip blank lines, label lines, include lines and line comments
2549 (while (and
2550 ;; The current statement is the first statement until we
2551 ;; reach another statement.
2552 (setq first-statement
2553 (or
2554 (looking-at idlwave-comment-line-start-skip)
2555 (looking-at "[ \t]*$")
2556 (looking-at (concat "[ \t]*" idlwave-label "[ \t]*$"))
2557 (looking-at "^@")))
2558 (= (forward-line -1) 0)))
2559 ;; skip continuation lines
2560 (while (and
2561 (save-excursion
2562 (forward-line -1)
2563 (idlwave-is-continuation-line))
2564 (= (forward-line -1) 0)))
2565 first-statement)))
2566
2567 (defun idlwave-end-of-statement ()
2568 "Move point to the end of the current IDL statement.
2569 If not in a statement just moves to end of line. Returns position."
2570 (interactive)
2571 (while (and (idlwave-is-continuation-line)
2572 (= (forward-line 1) 0))
2573 (while (and (idlwave-is-comment-or-empty-line)
2574 (= (forward-line 1) 0))))
2575 (end-of-line)
2576 (point))
2577
2578 (defun idlwave-end-of-statement0 ()
2579 "Move point to the end of the current IDL statement.
2580 If not in a statement just moves to end of line. Returns position."
2581 (interactive)
2582 (while (and (idlwave-is-continuation-line)
2583 (= (forward-line 1) 0)))
2584 (end-of-line)
2585 (point))
2586
2587 (defun idlwave-next-statement ()
2588 "Move point to beginning of the next IDL statement.
2589 Returns t if that statement is the last non-comment IDL statement
2590 in the file, and nil otherwise."
2591 (interactive)
2592 (let (last-statement)
2593 (idlwave-end-of-statement)
2594 ;; skip blank lines, label lines, include lines and line comments
2595 (while (and (= (forward-line 1) 0)
2596 ;; The current statement is the last statement until
2597 ;; we reach a new statement.
2598 (setq last-statement
2599 (or
2600 (looking-at idlwave-comment-line-start-skip)
2601 (looking-at "[ \t]*$")
2602 (looking-at (concat "[ \t]*" idlwave-label "[ \t]*$"))
2603 (looking-at "^@")))))
2604 last-statement))
2605
2606 (defun idlwave-skip-multi-commands (&optional lim)
2607 "Skip past multiple commands on a line (with `&')."
2608 (let ((save-point (point)))
2609 (when (re-search-forward ".*&" lim t)
2610 (goto-char (match-end 0))
2611 (if (idlwave-quoted)
2612 (goto-char save-point)
2613 (if (eq (char-after (- (point) 2)) ?&) (goto-char save-point))))
2614 (point)))
2615
2616 (defun idlwave-skip-label-or-case ()
2617 "Skip label or case statement element.
2618 Returns position after label.
2619 If there is no label point is not moved and nil is returned."
2620 ;; Case expressions and labels are terminated by a colon.
2621 ;; So we find the first colon in the line and make sure
2622 ;; - no `?' is before it (might be a ? b : c)
2623 ;; - it is not in a comment
2624 ;; - not in a string constant
2625 ;; - not in parenthesis (like a[0:3])
2626 ;; - not followed by another ":" in explicit class, ala a->b::c
2627 ;; As many in this mode, this function is heuristic and not an exact
2628 ;; parser.
2629 (let* ((start (point))
2630 (eos (save-excursion (idlwave-end-of-statement) (point)))
2631 (end (idlwave-find-key ":" 1 'nomark eos)))
2632 (if (and end
2633 (= (nth 0 (parse-partial-sexp start end)) 0)
2634 (not (string-match "\\?" (buffer-substring start end)))
2635 (not (string-match "^::" (buffer-substring end eos))))
2636 (progn
2637 (forward-char)
2638 (point))
2639 (goto-char start)
2640 nil)))
2641
2642 (defun idlwave-start-of-substatement (&optional pre)
2643 "Move to start of next IDL substatement after point.
2644 Uses the type of the current IDL statement to determine if the next
2645 statement is on a new line or is a subpart of the current statement.
2646 Returns point at start of substatement modulo whitespace.
2647 If optional argument is non-nil move to beginning of current
2648 substatement."
2649 (let ((orig (point))
2650 (eos (idlwave-end-of-statement))
2651 (ifnest 0)
2652 st nst last)
2653 (idlwave-beginning-of-statement)
2654 (idlwave-skip-label-or-case)
2655 (if (< (point) orig)
2656 (idlwave-skip-multi-commands orig))
2657 (setq last (point))
2658 ;; Continue looking for substatements until we are past orig
2659 (while (and (<= (point) orig) (not (eobp)))
2660 (setq last (point))
2661 (setq nst (nth 1 (cdr (setq st (car (idlwave-statement-type))))))
2662 (if (equal (car st) 'if) (setq ifnest (1+ ifnest)))
2663 (cond ((and nst
2664 (idlwave-find-key nst 1 'nomark eos))
2665 (goto-char (match-end 0)))
2666 ((and (> ifnest 0) (idlwave-find-key "\\<else\\>" 1 'nomark eos))
2667 (setq ifnest (1- ifnest))
2668 (goto-char (match-end 0)))
2669 (t (setq ifnest 0)
2670 (idlwave-next-statement))))
2671 (if pre (goto-char last))
2672 ;; If a continuation line starts here, move to next line
2673 (if (looking-at "[ \t]*\\$\\([ \t]*\\(;\\|$\\)\\)")
2674 (beginning-of-line 2))
2675 (point)))
2676
2677 (defun idlwave-statement-type ()
2678 "Return the type of the current IDL statement.
2679 Uses `idlwave-statement-match' to return a cons of (type . point) with
2680 point the ending position where the type was determined. Type is the
2681 association from `idlwave-statement-match', i.e. the cons cell from the
2682 list not just the type symbol. Returns nil if not an identifiable
2683 statement."
2684 (save-excursion
2685 ;; Skip whitespace within a statement which is spaces, tabs, continuations
2686 ;; and possibly comments
2687 (while (looking-at "[ \t]*\\$")
2688 (forward-line 1))
2689 (skip-chars-forward " \t")
2690 (let ((st idlwave-statement-match)
2691 (case-fold-search t))
2692 (while (and (not (looking-at (nth 0 (cdr (car st)))))
2693 (setq st (cdr st))))
2694 (if st
2695 (append st (match-end 0))))))
2696
2697 (defun idlwave-expand-equal (&optional before after is-action)
2698 "Pad '=' with spaces.
2699 Two cases: Assignment statement, and keyword assignment.
2700 Which case is determined using `idlwave-start-of-substatement' and
2701 `idlwave-statement-type'. The equal sign will be surrounded by BEFORE
2702 and AFTER blanks. If `idlwave-pad-keyword' is t then keyword assignment
2703 is treated just like assignment statements. When nil, spaces are
2704 removed for keyword assignment. Any other value keeps the current space
2705 around the `='. Limits in for loops are treated as keyword assignment.
2706
2707 Starting with IDL 6.0, a number of op= assignments are available.
2708 Since ambiguities of the form:
2709
2710 r and= b
2711 rand= b
2712
2713 can occur, alphanumeric operator assignment will never be pre-padded,
2714 only post-padded. You must use a space before these to disambiguate
2715 \(not just for padding, but for proper parsing by IDL too!). Other
2716 operators, such as ##=, ^=, etc., will be pre-padded.
2717
2718 IS-ACTION is ignored.
2719
2720 See `idlwave-surround'."
2721 (if idlwave-surround-by-blank
2722 (let
2723 ((non-an-ops "\\(##\\|\\*\\|\\+\\|-\\|/\\|<\\|>\\|\\^\\)\\=")
2724 (an-ops
2725 "\\s-\\(AND\\|EQ\\|GE\\|GT\\|LE\\|LT\\|MOD\\|NE\\|OR\\|XOR\\)\\=")
2726 (len 1))
2727
2728 (save-excursion
2729 (let ((case-fold-search t))
2730 (backward-char)
2731 (if (or
2732 (re-search-backward non-an-ops nil t)
2733 ;; Why doesn't ##? work for both?
2734 (re-search-backward "\\(#\\)\\=" nil t))
2735 (setq len (1+ (length (match-string 1))))
2736 (when (re-search-backward an-ops nil t)
2737 ;(setq begin nil) ; won't modify begin
2738 (setq len (1+ (length (match-string 1))))))))
2739
2740 (if (eq t idlwave-pad-keyword)
2741 ;; Everything gets padded equally
2742 (idlwave-surround before after len)
2743 ;; Treating keywords/for variables specially...
2744 (let ((st (save-excursion ; To catch "for" variables
2745 (idlwave-start-of-substatement t)
2746 (idlwave-statement-type)))
2747 (what (save-excursion ; To catch keywords
2748 (skip-chars-backward "= \t")
2749 (nth 2 (idlwave-where)))))
2750 (cond ((or (memq what '(function-keyword procedure-keyword))
2751 (memq (caar st) '(for pdef)))
2752 (cond
2753 ((null idlwave-pad-keyword)
2754 (idlwave-surround 0 0)
2755 ) ; remove space
2756 (t))) ; leave any spaces alone
2757 (t (idlwave-surround before after len))))))))
2758
2759
2760 (defun idlwave-indent-and-action (&optional arg)
2761 "Call `idlwave-indent-line' and do expand actions.
2762 With prefix ARG non-nil, indent the entire sub-statement."
2763 (interactive "p")
2764 (save-excursion
2765 (if (and idlwave-expand-generic-end
2766 (re-search-backward "\\<\\(end\\)\\s-*\\="
2767 (max 0 (- (point) 10)) t)
2768 (looking-at "\\(end\\)\\([ \n\t]\\|\\'\\)"))
2769 (progn (goto-char (match-end 1))
2770 ;;Expand the END abbreviation, just as RET or Space would have.
2771 (if abbrev-mode (expand-abbrev)
2772 (idlwave-show-begin)))))
2773 (when (and (not arg) current-prefix-arg)
2774 (setq arg current-prefix-arg)
2775 (setq current-prefix-arg nil))
2776 (if arg
2777 (idlwave-indent-statement)
2778 (idlwave-indent-line t)))
2779
2780 (defun idlwave-indent-line (&optional expand)
2781 "Indent current IDL line as code or as a comment.
2782 The actions in `idlwave-indent-action-table' are performed.
2783 If the optional argument EXPAND is non-nil then the actions in
2784 `idlwave-indent-expand-table' are performed."
2785 (interactive)
2786 ;; Move point out of left margin.
2787 (if (save-excursion
2788 (skip-chars-backward " \t")
2789 (bolp))
2790 (skip-chars-forward " \t"))
2791 (let ((mloc (point-marker)))
2792 (save-excursion
2793 (beginning-of-line)
2794 (if (looking-at idlwave-comment-line-start-skip)
2795 ;; Indentation for a line comment
2796 (progn
2797 (skip-chars-forward " \t")
2798 (idlwave-indent-left-margin (idlwave-comment-hook)))
2799 ;;
2800 ;; Code Line
2801 ;;
2802 ;; Before indenting, run action routines.
2803 ;;
2804 (if (and expand idlwave-do-actions)
2805 (mapc 'idlwave-do-action idlwave-indent-expand-table))
2806 ;;
2807 (if idlwave-do-actions
2808 (mapc 'idlwave-do-action idlwave-indent-action-table))
2809 ;;
2810 ;; No longer expand abbrevs on the line. The user can do this
2811 ;; manually using expand-region-abbrevs.
2812 ;;
2813 ;; Indent for code line
2814 ;;
2815 (beginning-of-line)
2816 (if (or
2817 ;; a label line
2818 (looking-at (concat "^" idlwave-label "[ \t]*$"))
2819 ;; a batch command
2820 (looking-at "^[ \t]*@"))
2821 ;; leave flush left
2822 nil
2823 ;; indent the line
2824 (idlwave-indent-left-margin (idlwave-calculate-indent)))
2825 ;; Adjust parallel comment
2826 (end-of-line)
2827 (if (idlwave-in-comment)
2828 ;; Emacs 21 is too smart with fill-column on comment indent
2829 (let ((fill-column (if (fboundp 'comment-indent-new-line)
2830 (1- (frame-width))
2831 fill-column)))
2832 (indent-for-comment)))))
2833 (goto-char mloc)
2834 ;; Get rid of marker
2835 (set-marker mloc nil)))
2836
2837 (defun idlwave-do-action (action)
2838 "Perform an action repeatedly on a line.
2839 ACTION is a list (REG . FUNC). REG is a regular expression. FUNC is
2840 either a function name to be called with `funcall' or a list to be
2841 evaluated with `eval'. The action performed by FUNC should leave
2842 point after the match for REG - otherwise an infinite loop may be
2843 entered. FUNC is always passed a final argument of 'is-action, so it
2844 can discriminate between being run as an action, or a key binding."
2845 (let ((action-key (car action))
2846 (action-routine (cdr action)))
2847 (beginning-of-line)
2848 (while (idlwave-look-at action-key)
2849 (if (listp action-routine)
2850 (eval (append action-routine '('is-action)))
2851 (funcall action-routine 'is-action)))))
2852
2853 (defun idlwave-indent-to (col &optional min)
2854 "Indent from point with spaces until column COL.
2855 Inserts space before markers at point."
2856 (if (not min) (setq min 0))
2857 (insert-before-markers
2858 (make-string (max min (- col (current-column))) ?\ )))
2859
2860 (defun idlwave-indent-left-margin (col)
2861 "Indent the current line to column COL.
2862 Indents such that first non-whitespace character is at column COL
2863 Inserts spaces before markers at point."
2864 (save-excursion
2865 (beginning-of-line)
2866 (delete-horizontal-space)
2867 (idlwave-indent-to col)))
2868
2869 (defun idlwave-indent-subprogram ()
2870 "Indent program unit which contains point."
2871 (interactive)
2872 (save-excursion
2873 (idlwave-end-of-statement)
2874 (idlwave-beginning-of-subprogram)
2875 (let ((beg (point)))
2876 (idlwave-forward-block)
2877 (message "Indenting subprogram...")
2878 (indent-region beg (point) nil))
2879 (message "Indenting subprogram...done.")))
2880
2881 (defun idlwave-indent-statement ()
2882 "Indent current statement, including all continuation lines."
2883 (interactive)
2884 (save-excursion
2885 (idlwave-beginning-of-statement)
2886 (let ((beg (point)))
2887 (idlwave-end-of-statement)
2888 (indent-region beg (point) nil))))
2889
2890 (defun idlwave-calculate-indent ()
2891 "Return appropriate indentation for current line as IDL code."
2892 (save-excursion
2893 (beginning-of-line)
2894 (cond
2895 ;; Check for beginning of unit - main (beginning of buffer), pro, or
2896 ;; function
2897 ((idlwave-look-at idlwave-begin-unit-reg)
2898 0)
2899 ;; Check for continuation line
2900 ((save-excursion
2901 (and (= (forward-line -1) 0)
2902 (idlwave-is-continuation-line)))
2903 (idlwave-calculate-cont-indent))
2904 ;; calculate indent based on previous and current statements
2905 (t (let* (beg-prev-pos
2906 (the-indent
2907 ;; calculate indent based on previous statement
2908 (save-excursion
2909 (cond
2910 ;; Beginning of file
2911 ((prog1
2912 (idlwave-previous-statement)
2913 (setq beg-prev-pos (point)))
2914 0)
2915 ;; Main block
2916 ((idlwave-look-at idlwave-begin-unit-reg t)
2917 (+ (idlwave-current-statement-indent)
2918 idlwave-main-block-indent))
2919 ;; Begin block
2920 ((idlwave-look-at idlwave-begin-block-reg t)
2921 (+ (idlwave-min-current-statement-indent)
2922 idlwave-block-indent))
2923 ;; End Block
2924 ((idlwave-look-at idlwave-end-block-reg t)
2925 (progn
2926 ;; Match to the *beginning* of the block opener
2927 (goto-char beg-prev-pos)
2928 (idlwave-block-jump-out -1 'nomark) ; go to begin block
2929 (idlwave-min-current-statement-indent)))
2930 ;; idlwave-end-offset
2931 ;; idlwave-block-indent))
2932
2933 ;; Default to current indent
2934 ((idlwave-current-statement-indent))))))
2935 ;; adjust the indentation based on the current statement
2936 (cond
2937 ;; End block
2938 ((idlwave-look-at idlwave-end-block-reg)
2939 (+ the-indent idlwave-end-offset))
2940 (the-indent)))))))
2941
2942 ;;
2943 ;; Parentheses indent
2944 ;;
2945
2946 (defun idlwave-calculate-paren-indent (beg-reg end-reg close-exp)
2947 "Calculate the continuation indent inside a paren group.
2948 Returns a cons-cell with (open . indent), where open is the
2949 location of the open paren."
2950 (let ((open (nth 1 (parse-partial-sexp beg-reg end-reg))))
2951 ;; Found an innermost open paren.
2952 (when open
2953 (goto-char open)
2954 ;; Line up with next word unless this is a closing paren.
2955 (cons open
2956 (cond
2957 ;; Plain Kernighan-style nested indent
2958 (idlwave-indent-parens-nested
2959 (+ idlwave-continuation-indent (idlwave-current-indent)))
2960
2961 ;; This is a closed paren - line up under open paren.
2962 (close-exp
2963 (current-column))
2964
2965 ;; Empty (or just comment) follows -- revert to basic indent
2966 ((progn
2967 ;; Skip paren
2968 (forward-char 1)
2969 (looking-at "[ \t$]*\\(;.*\\)?$"))
2970 nil)
2971
2972 ;; Line up with first word after any blank space
2973 ((progn
2974 (skip-chars-forward " \t")
2975 (current-column))))))))
2976
2977 (defun idlwave-calculate-cont-indent ()
2978 "Calculates the IDL continuation indent column from the previous statement.
2979 Note that here previous statement usually means the beginning of the
2980 current statement if this statement is a continuation of the previous
2981 line. Various special types of continuations, including assignments,
2982 routine definitions, and parenthetical groupings, are treated separately."
2983 (save-excursion
2984 (let* ((case-fold-search t)
2985 (end-reg (progn (beginning-of-line) (point)))
2986 (beg-last-statement (save-excursion (idlwave-previous-statement)
2987 (point)))
2988 (beg-reg (progn (idlwave-start-of-substatement 'pre)
2989 (if (eq (line-beginning-position) end-reg)
2990 (goto-char beg-last-statement)
2991 (point))))
2992 (basic-indent (+ (idlwave-min-current-statement-indent end-reg)
2993 idlwave-continuation-indent))
2994 fancy-nonparen-indent fancy-paren-indent)
2995 (cond
2996 ;; Align then with its matching if, etc.
2997 ((let ((matchers '(("\\<if\\>" . "[ \t]*then")
2998 ("\\<\\(if\\|end\\(if\\)?\\)\\>" . "[ \t]*else")
2999 ("\\<\\(for\\|while\\)\\>" . "[ \t]*do")
3000 ("\\<\\(repeat\\|end\\(rep\\)?\\)\\>" .
3001 "[ \t]*until")
3002 ("\\<case\\>" . "[ \t]*of")))
3003 match cont-re)
3004 (goto-char end-reg)
3005 (and
3006 (setq cont-re
3007 (catch 'exit
3008 (while (setq match (car matchers))
3009 (if (looking-at (cdr match))
3010 (throw 'exit (car match)))
3011 (setq matchers (cdr matchers)))))
3012 (idlwave-find-key cont-re -1 'nomark beg-last-statement)))
3013 (if (looking-at "end") ;; that one's special
3014 (- (idlwave-current-indent)
3015 (+ idlwave-block-indent idlwave-end-offset))
3016 (idlwave-current-indent)))
3017
3018 ;; Indent in from the previous line for continuing statements
3019 ((let ((matchers '("\\<then\\>"
3020 "\\<do\\>"
3021 "\\<repeat\\>"
3022 "\\<else\\>"))
3023 match)
3024 (catch 'exit
3025 (goto-char end-reg)
3026 (if (/= (forward-line -1) 0)
3027 (throw 'exit nil))
3028 (while (setq match (car matchers))
3029 (if (looking-at (concat ".*" match "[ \t]*\\$[ \t]*"
3030 "\\(;.*\\)?$"))
3031 (throw 'exit t))
3032 (setq matchers (cdr matchers)))))
3033 (+ idlwave-continuation-indent (idlwave-current-indent)))
3034
3035 ;; Parenthetical indent, either traditional or Kernighan style
3036 ((setq fancy-paren-indent
3037 (let* ((end-reg end-reg)
3038 (close-exp (progn
3039 (goto-char end-reg)
3040 (skip-chars-forward " \t")
3041 (looking-at "\\s)")))
3042 indent-cons)
3043 (catch 'loop
3044 (while (setq indent-cons (idlwave-calculate-paren-indent
3045 beg-reg end-reg close-exp))
3046 ;; First permitted containing paren
3047 (if (or
3048 idlwave-indent-to-open-paren
3049 idlwave-indent-parens-nested
3050 (null (cdr indent-cons))
3051 (< (- (cdr indent-cons) basic-indent)
3052 idlwave-max-extra-continuation-indent))
3053 (throw 'loop (cdr indent-cons)))
3054 (setq end-reg (car indent-cons))))))
3055 fancy-paren-indent)
3056
3057 ;; A continued assignment, or procedure call/definition
3058 ((and
3059 (> idlwave-max-extra-continuation-indent 0)
3060 (setq fancy-nonparen-indent
3061 (progn
3062 (goto-char beg-reg)
3063 (while (idlwave-look-at "&")) ; skip continued statements
3064 (cond
3065 ;; A continued Procedure call or definition
3066 ((progn
3067 (idlwave-look-at "^[ \t]*\\(pro\\|function\\)") ;skip over
3068 (looking-at "[ \t]*\\([a-zA-Z0-9.$_]+[ \t]*->[ \t]*\\)?[a-zA-Z][:a-zA-Z0-9$_]*[ \t]*\\(,\\)[ \t]*"))
3069 (goto-char (match-end 0))
3070 ;; Comment only, or blank line with "$"? Basic indent.
3071 (if (save-match-data (looking-at "[ \t$]*\\(;.*\\)?$"))
3072 nil
3073 (current-column)))
3074
3075 ;; Continued assignment (with =):
3076 ((catch 'assign ;
3077 (while (looking-at "[^=\n\r]*\\(=\\)[ \t]*")
3078 (goto-char (match-end 0))
3079 (if (null (idlwave-what-function beg-reg))
3080 (throw 'assign t))))
3081 (unless (or
3082 (idlwave-in-quote)
3083 (looking-at "[ \t$]*\\(;.*\\)?$") ; use basic
3084 (save-excursion
3085 (goto-char beg-last-statement)
3086 (eq (caar (idlwave-statement-type)) 'for)))
3087 (current-column))))))
3088 (< (- fancy-nonparen-indent basic-indent)
3089 idlwave-max-extra-continuation-indent))
3090 (if fancy-paren-indent ;calculated but disallowed paren indent
3091 (+ fancy-nonparen-indent idlwave-continuation-indent)
3092 fancy-nonparen-indent))
3093
3094 ;; Basic indent, by default
3095 (t basic-indent)))))
3096
3097
3098
3099 (defun idlwave-find-key (key-re &optional dir nomark limit)
3100 "Move to next match of the regular expression KEY-RE.
3101 Matches inside comments or string constants will be ignored.
3102 If DIR is negative, the search will be backwards.
3103 At a successful match, the mark is pushed unless NOMARK is non-nil.
3104 Searches are limited to LIMIT.
3105 Searches are case-insensitive and use a special syntax table which
3106 treats `$' and `_' as word characters.
3107 Return value is the beginning of the match or (in case of failure) nil."
3108 (setq dir (or dir 0))
3109 (let ((case-fold-search t)
3110 (search-func (if (> dir 0) 're-search-forward 're-search-backward))
3111 found)
3112 (idlwave-with-special-syntax
3113 (save-excursion
3114 (catch 'exit
3115 (while (funcall search-func key-re limit t)
3116 (if (not (idlwave-quoted))
3117 (throw 'exit (setq found (match-beginning 0)))
3118 (if (or (and (> dir 0) (eobp))
3119 (and (< dir 0) (bobp)))
3120 (throw 'exit nil)))))))
3121 (if found
3122 (progn
3123 (if (not nomark) (push-mark))
3124 (goto-char found)
3125 found)
3126 nil)))
3127
3128 (defun idlwave-block-jump-out (&optional dir nomark)
3129 "When optional argument DIR is non-negative, move forward to end of
3130 current block using the `idlwave-begin-block-reg' and `idlwave-end-block-reg'
3131 regular expressions. When DIR is negative, move backwards to block beginning.
3132 Recursively calls itself to skip over nested blocks. DIR defaults to
3133 forward. Calls `push-mark' unless the optional argument NOMARK is
3134 non-nil. Movement is limited by the start of program units because of
3135 possibility of unbalanced blocks."
3136 (interactive "P")
3137 (or dir (setq dir 0))
3138 (let* ((here (point))
3139 (case-fold-search t)
3140 (limit (if (>= dir 0) (point-max) (point-min)))
3141 (block-limit (if (>= dir 0)
3142 idlwave-begin-block-reg
3143 idlwave-end-block-reg))
3144 found
3145 (block-reg (concat idlwave-begin-block-reg "\\|"
3146 idlwave-end-block-reg))
3147 (unit-limit (or (save-excursion
3148 (if (< dir 0)
3149 (idlwave-find-key
3150 idlwave-begin-unit-reg dir t limit)
3151 (end-of-line)
3152 (idlwave-find-key
3153 idlwave-end-unit-reg dir t limit)))
3154 limit)))
3155 (if (>= dir 0) (end-of-line)) ;Make sure we are in current block
3156 (if (setq found (idlwave-find-key block-reg dir t unit-limit))
3157 (while (and found (looking-at block-limit))
3158 (if (>= dir 0) (forward-word 1))
3159 (idlwave-block-jump-out dir t)
3160 (setq found (idlwave-find-key block-reg dir t unit-limit))))
3161 (if (not nomark) (push-mark here))
3162 (if (not found) (goto-char unit-limit)
3163 (if (>= dir 0) (forward-word 1)))))
3164
3165 (defun idlwave-min-current-statement-indent (&optional end-reg)
3166 "The minimum indent in the current statement."
3167 (idlwave-beginning-of-statement)
3168 (if (not (idlwave-is-continuation-line))
3169 (idlwave-current-indent)
3170 (let ((min (idlwave-current-indent)) comm-or-empty)
3171 (while (and (= (forward-line 1) 0)
3172 (or (setq comm-or-empty (idlwave-is-comment-or-empty-line))
3173 (idlwave-is-continuation-line))
3174 (or (null end-reg) (< (point) end-reg)))
3175 (unless comm-or-empty (setq min (min min (idlwave-current-indent)))))
3176 (if (or comm-or-empty (and end-reg (>= (point) end-reg)))
3177 min
3178 (min min (idlwave-current-indent))))))
3179
3180 (defun idlwave-current-statement-indent (&optional last-line)
3181 "Return indentation of the current statement.
3182 If in a statement, moves to beginning of statement before finding indent."
3183 (if last-line
3184 (idlwave-end-of-statement)
3185 (idlwave-beginning-of-statement))
3186 (idlwave-current-indent))
3187
3188 (defun idlwave-current-indent ()
3189 "Return the column of the indentation of the current line.
3190 Skips any whitespace. Returns 0 if the end-of-line follows the whitespace."
3191 (save-excursion
3192 (beginning-of-line)
3193 (skip-chars-forward " \t")
3194 ;; if we are at the end of blank line return 0
3195 (cond ((eolp) 0)
3196 ((current-column)))))
3197
3198 (defun idlwave-is-continuation-line ()
3199 "Test if current line is continuation line.
3200 Blank or comment-only lines following regular continuation lines (with
3201 `$') count as continuations too."
3202 (let (p)
3203 (save-excursion
3204 (or
3205 (idlwave-look-at "\\<\\$")
3206 (catch 'loop
3207 (while (and (looking-at "^[ \t]*\\(;.*\\)?$")
3208 (eq (forward-line -1) 0))
3209 (if (setq p (idlwave-look-at "\\<\\$")) (throw 'loop p))))))))
3210
3211 (defun idlwave-is-comment-line ()
3212 "Test if the current line is a comment line."
3213 (save-excursion
3214 (beginning-of-line 1)
3215 (looking-at "[ \t]*;")))
3216
3217 (defun idlwave-is-comment-or-empty-line ()
3218 "Test if the current line is a comment line."
3219 (save-excursion
3220 (beginning-of-line 1)
3221 (looking-at "[ \t]*[;\n]")))
3222
3223 (defun idlwave-look-at (regexp &optional cont beg)
3224 "Search current line from current point for REGEXP.
3225 If optional argument CONT is non-nil, searches to the end of
3226 the current statement.
3227 If optional arg BEG is non-nil, search starts from the beginning of the
3228 current statement.
3229 Ignores matches that end in a comment or inside a string expression.
3230 Returns point if successful, nil otherwise.
3231 This function produces unexpected results if REGEXP contains quotes or
3232 a comment delimiter. The search is case insensitive.
3233 If successful leaves point after the match, otherwise, does not move point."
3234 (let ((here (point))
3235 (case-fold-search t)
3236 (eos (save-excursion
3237 (if cont (idlwave-end-of-statement) (end-of-line))
3238 (point)))
3239 found)
3240 (idlwave-with-special-syntax
3241 (if beg (idlwave-beginning-of-statement))
3242 (while (and (setq found (re-search-forward regexp eos t))
3243 (idlwave-quoted))))
3244 (if (not found) (goto-char here))
3245 found))
3246
3247 (defun idlwave-fill-paragraph (&optional nohang)
3248 "Fill paragraphs in comments.
3249 A paragraph is made up of all contiguous lines having the same comment
3250 leader (the leading whitespace before the comment delimiter and the
3251 comment delimiter). In addition, paragraphs are separated by blank
3252 line comments. The indentation is given by the hanging indent of the
3253 first line, otherwise by the minimum indentation of the lines after
3254 the first line. The indentation of the first line does not change.
3255 Does not effect code lines. Does not fill comments on the same line
3256 with code. The hanging indent is given by the end of the first match
3257 matching `idlwave-hang-indent-regexp' on the paragraph's first line.
3258 If the optional argument NOHANG is non-nil then the hanging indent is
3259 ignored."
3260 (interactive "P")
3261 ;; check if this is a line comment
3262 (if (save-excursion
3263 (beginning-of-line)
3264 (skip-chars-forward " \t")
3265 (looking-at comment-start))
3266 (let
3267 ((indent 999)
3268 pre here diff fill-prefix-reg bcl first-indent
3269 hang start end)
3270 ;; Change tabs to spaces in the surrounding paragraph.
3271 ;; The surrounding paragraph will be the largest containing block of
3272 ;; contiguous line comments. Thus, we may be changing tabs in
3273 ;; a much larger area than is needed, but this is the easiest
3274 ;; brute force way to do it.
3275 ;;
3276 ;; This has the undesirable side effect of replacing the tabs
3277 ;; permanently without the user's request or knowledge.
3278 (save-excursion
3279 (backward-paragraph)
3280 (setq start (point)))
3281 (save-excursion
3282 (forward-paragraph)
3283 (setq end (point)))
3284 (untabify start end)
3285 ;;
3286 (setq here (point))
3287 (beginning-of-line)
3288 (setq bcl (point))
3289 (re-search-forward (concat "^[ \t]*" comment-start "+")
3290 (point-at-eol) t)
3291 ;; Get the comment leader on the line and its length
3292 (setq pre (current-column))
3293 ;; the comment leader is the indentation plus exactly the
3294 ;; number of consecutive ";".
3295 (setq fill-prefix-reg
3296 (concat
3297 (setq fill-prefix
3298 (regexp-quote (buffer-substring (point-at-bol) (point))))
3299 "[^;]"))
3300
3301 ;; Mark the beginning and end of the paragraph
3302 (goto-char bcl)
3303 (while (and (looking-at fill-prefix-reg)
3304 (not (looking-at paragraph-separate))
3305 (not (bobp)))
3306 (forward-line -1))
3307 ;; Move to first line of paragraph
3308 (if (/= (point) bcl)
3309 (forward-line 1))
3310 (setq start (point))
3311 (goto-char bcl)
3312 (while (and (looking-at fill-prefix-reg)
3313 (not (looking-at paragraph-separate))
3314 (not (eobp)))
3315 (forward-line 1))
3316 (beginning-of-line)
3317 (if (or (not (looking-at fill-prefix-reg))
3318 (looking-at paragraph-separate))
3319 (forward-line -1))
3320 (end-of-line)
3321 ;; if at end of buffer add a newline (need this because
3322 ;; fill-region needs END to be at the beginning of line after
3323 ;; the paragraph or it will add a line).
3324 (if (eobp)
3325 (progn (insert ?\n) (backward-char 1)))
3326 ;; Set END to the beginning of line after the paragraph
3327 ;; END is calculated as distance from end of buffer
3328 (setq end (- (point-max) (point) 1))
3329 ;;
3330 ;; Calculate the indentation for the paragraph.
3331 ;;
3332 ;; In the following while statements, after one iteration
3333 ;; point will be at the beginning of a line in which case
3334 ;; the while will not be executed for the
3335 ;; the first paragraph line and thus will not affect the
3336 ;; indentation.
3337 ;;
3338 ;; First check to see if indentation is based on hanging indent.
3339 (if (and (not nohang) idlwave-hanging-indent
3340 (setq hang
3341 (save-excursion
3342 (goto-char start)
3343 (idlwave-calc-hanging-indent))))
3344 ;; Adjust lines of paragraph by inserting spaces so that
3345 ;; each line's indent is at least as great as the hanging
3346 ;; indent. This is needed for fill-paragraph to work with
3347 ;; a fill-prefix.
3348 (progn
3349 (setq indent hang)
3350 (beginning-of-line)
3351 (while (> (point) start)
3352 (re-search-forward comment-start-skip (point-at-eol) t)
3353 (if (> (setq diff (- indent (current-column))) 0)
3354 (progn
3355 (if (>= here (point))
3356 ;; adjust the original location for the
3357 ;; inserted text.
3358 (setq here (+ here diff)))
3359 (insert (make-string diff ?\ ))))
3360 (forward-line -1))
3361 )
3362
3363 ;; No hang. Instead find minimum indentation of paragraph
3364 ;; after first line.
3365 ;; For the following while statement, since START is at the
3366 ;; beginning of line and END is at the end of line
3367 ;; point is greater than START at least once (which would
3368 ;; be the case for a single line paragraph).
3369 (while (> (point) start)
3370 (beginning-of-line)
3371 (setq indent
3372 (min indent
3373 (progn
3374 (re-search-forward comment-start-skip (point-at-eol) t)
3375 (current-column))))
3376 (forward-line -1)))
3377 (setq fill-prefix (concat fill-prefix
3378 (make-string (- indent pre)
3379 ?\ )))
3380 ;; first-line indent
3381 (setq first-indent
3382 (max
3383 (progn
3384 (re-search-forward comment-start-skip (point-at-eol) t)
3385 (current-column))
3386 indent))
3387
3388 ;; try to keep point at its original place
3389 (goto-char here)
3390
3391 ;; In place of the more modern fill-region-as-paragraph, a hack
3392 ;; to keep whitespace untouched on the first line within the
3393 ;; indent length and to preserve any indent on the first line
3394 ;; (first indent).
3395 (save-excursion
3396 (setq diff
3397 (buffer-substring start (+ start first-indent -1)))
3398 (subst-char-in-region start (+ start first-indent -1) ?\ ?~ nil)
3399 (fill-region-as-paragraph
3400 start
3401 (- (point-max) end)
3402 (current-justification)
3403 nil)
3404 (delete-region start (+ start first-indent -1))
3405 (goto-char start)
3406 (insert diff))
3407 ;; When we want the point at the beginning of the comment
3408 ;; body fill-region will put it at the beginning of the line.
3409 (if (bolp) (skip-chars-forward (concat " \t" comment-start)))
3410 (setq fill-prefix nil))))
3411
3412 (defun idlwave-calc-hanging-indent ()
3413 "Calculate the position of the hanging indent for the comment paragraph.
3414 The hanging indent position is given by the first match with the
3415 `idlwave-hang-indent-regexp'. If `idlwave-use-last-hang-indent' is
3416 non-nil then use last occurrence matching `idlwave-hang-indent-regexp'
3417 on the line.
3418 If not found returns nil."
3419 (if idlwave-use-last-hang-indent
3420 (save-excursion
3421 (end-of-line)
3422 (if (re-search-backward idlwave-hang-indent-regexp (point-at-bol) t)
3423 (+ (current-column) (length idlwave-hang-indent-regexp))))
3424 (save-excursion
3425 (beginning-of-line)
3426 (if (re-search-forward idlwave-hang-indent-regexp (point-at-eol) t)
3427 (current-column)))))
3428
3429 (defun idlwave-auto-fill ()
3430 "Called to break lines in auto fill mode.
3431 Only fills non-comment lines if `idlwave-fill-comment-line-only' is
3432 non-nil. Places a continuation character at the end of the line if
3433 not in a comment. Splits strings with IDL concatenation operator `+'
3434 if `idlwave-auto-fill-split-string' is non-nil."
3435 (if (<= (current-column) fill-column)
3436 nil ; do not to fill
3437 (if (or (not idlwave-fill-comment-line-only)
3438 (save-excursion
3439 ;; Check for comment line
3440 (beginning-of-line)
3441 (looking-at idlwave-comment-line-start-skip)))
3442 (let (beg)
3443 (idlwave-indent-line)
3444 ;; Prevent actions do-auto-fill which calls indent-line-function.
3445 (let (idlwave-do-actions
3446 (paragraph-separate ".")
3447 (fill-nobreak-predicate
3448 (if (and (idlwave-in-quote)
3449 idlwave-auto-fill-split-string)
3450 (lambda () ;; We'll need 5 spaces for " ' + $"
3451 (<= (- fill-column (current-column)) 5)
3452 ))))
3453 (do-auto-fill))
3454 (save-excursion
3455 (end-of-line 0)
3456 ;; Indent the split line
3457 (idlwave-indent-line))
3458 (if (save-excursion
3459 (beginning-of-line)
3460 (looking-at idlwave-comment-line-start-skip))
3461 ;; A continued line comment
3462 ;; We treat continued line comments as part of a comment
3463 ;; paragraph. So we check for a hanging indent.
3464 (if idlwave-hanging-indent
3465 (let ((here (- (point-max) (point)))
3466 (indent
3467 (save-excursion
3468 (forward-line -1)
3469 (idlwave-calc-hanging-indent))))
3470 (when indent
3471 ;; Remove whitespace between comment delimiter and
3472 ;; text, insert spaces for appropriate indentation.
3473 (beginning-of-line)
3474 (re-search-forward comment-start-skip (point-at-eol) t)
3475 (delete-horizontal-space)
3476 (idlwave-indent-to indent)
3477 (goto-char (- (point-max) here)))))
3478 ;; Split code or comment?
3479 (if (save-excursion
3480 (end-of-line 0)
3481 (idlwave-in-comment))
3482 ;; Splitting a non-full-line comment.
3483 ;; Insert the comment delimiter from split line
3484 (progn
3485 (save-excursion
3486 (beginning-of-line)
3487 (skip-chars-forward " \t")
3488 ;; Insert blank to keep off beginning of line
3489 (insert " "
3490 (save-excursion
3491 (forward-line -1)
3492 (buffer-substring (idlwave-goto-comment)
3493 (progn
3494 (skip-chars-forward "; ")
3495 (point))))))
3496 (idlwave-indent-line))
3497 ;; Split code line - add continuation character
3498 (save-excursion
3499 (end-of-line 0)
3500 ;; Check to see if we split a string
3501 (if (and (setq beg (idlwave-in-quote))
3502 idlwave-auto-fill-split-string)
3503 ;; Split the string and concatenate.
3504 ;; The first extra space is for the space
3505 ;; the line was split. That space was removed.
3506 (insert " " (char-after beg) " +"))
3507 (insert " $"))
3508 (if beg
3509 (if idlwave-auto-fill-split-string
3510 ;; Make the second part of continued string
3511 (save-excursion
3512 (beginning-of-line)
3513 (skip-chars-forward " \t")
3514 (insert (char-after beg)))
3515 ;; Warning
3516 (beep)
3517 (message "Warning: continuation inside a string.")))
3518 ;; Although do-auto-fill (via indent-new-comment-line) calls
3519 ;; idlwave-indent-line for the new line, re-indent again
3520 ;; because of the addition of the continuation character.
3521 (idlwave-indent-line))
3522 )))))
3523
3524 (defun idlwave-auto-fill-mode (arg)
3525 "Toggle auto-fill mode for IDL mode.
3526 With arg, turn auto-fill mode on if arg is positive.
3527 In auto-fill mode, inserting a space at a column beyond `fill-column'
3528 automatically breaks the line at a previous space."
3529 (interactive "P")
3530 (prog1 (set idlwave-fill-function
3531 (if (if (null arg)
3532 (not (symbol-value idlwave-fill-function))
3533 (> (prefix-numeric-value arg) 0))
3534 'idlwave-auto-fill
3535 nil))
3536 ;; update mode-line
3537 (set-buffer-modified-p (buffer-modified-p))))
3538
3539 ;(defun idlwave-fill-routine-call ()
3540 ; "Fill a routine definition or statement, indenting appropriately."
3541 ; (let ((where (idlwave-where)))))
3542
3543
3544 (defun idlwave-doc-header (&optional nomark)
3545 "Insert a documentation header at the beginning of the unit.
3546 Inserts the value of the variable `idlwave-file-header'. Sets mark
3547 before moving to do insertion unless the optional prefix argument
3548 NOMARK is non-nil."
3549 (interactive "P")
3550 (or nomark (push-mark))
3551 ;; make sure we catch the current line if it begins the unit
3552 (if idlwave-header-to-beginning-of-file
3553 (goto-char (point-min))
3554 (end-of-line)
3555 (idlwave-beginning-of-subprogram)
3556 (beginning-of-line)
3557 ;; skip function or procedure line
3558 (if (idlwave-look-at "\\<\\(pro\\|function\\)\\>")
3559 (progn
3560 (idlwave-end-of-statement)
3561 (if (> (forward-line 1) 0) (insert "\n")))))
3562 (let ((pos (point)))
3563 (if idlwave-file-header
3564 (cond ((car idlwave-file-header)
3565 (insert-file-contents (car idlwave-file-header)))
3566 ((stringp (car (cdr idlwave-file-header)))
3567 (insert (car (cdr idlwave-file-header))))))
3568 (goto-char pos)))
3569
3570 (defun idlwave-default-insert-timestamp ()
3571 "Default timestamp insertion function."
3572 (insert (current-time-string))
3573 (insert ", " (user-full-name))
3574 (if (boundp 'user-mail-address)
3575 (insert " <" user-mail-address ">")
3576 (insert " <" (user-login-name) "@" (system-name) ">"))
3577 ;; Remove extra spaces from line
3578 (idlwave-fill-paragraph)
3579 ;; Insert a blank line comment to separate from the date entry -
3580 ;; will keep the entry from flowing onto date line if re-filled.
3581 (insert "\n;\n;\t\t"))
3582
3583 (defun idlwave-doc-modification ()
3584 "Insert a brief modification log at the beginning of the current program.
3585 Looks for an occurrence of the value of user variable
3586 `idlwave-doc-modifications-keyword' if non-nil. Inserts time and user
3587 name and places the point for the user to add a log. Before moving, saves
3588 location on mark ring so that the user can return to previous point."
3589 (interactive)
3590 (push-mark)
3591 (let* (beg end)
3592 (if (and (or (re-search-backward idlwave-doclib-start nil t)
3593 (progn
3594 (goto-char (point-min))
3595 (re-search-forward idlwave-doclib-start nil t)))
3596 (setq beg (match-beginning 0))
3597 (re-search-forward idlwave-doclib-end nil t)
3598 (setq end (match-end 0)))
3599 (progn
3600 (goto-char beg)
3601 (if (re-search-forward
3602 (concat idlwave-doc-modifications-keyword ":")
3603 end t)
3604 (end-of-line)
3605 (goto-char end)
3606 (end-of-line -1)
3607 (insert "\n" comment-start "\n")
3608 (insert comment-start " " idlwave-doc-modifications-keyword ":"))
3609 (insert "\n;\n;\t")
3610 (run-hooks 'idlwave-timestamp-hook))
3611 (error "No valid DOCLIB header"))))
3612
3613
3614 ;; CJC 3/16/93
3615 ;; Interface to expand-region-abbrevs which did not work when the
3616 ;; abbrev hook associated with an abbrev moves point backwards
3617 ;; after abbrev expansion, e.g., as with the abbrev '.n'.
3618 ;; The original would enter an infinite loop in attempting to expand
3619 ;; .n (it would continually expand and unexpand the abbrev without expanding
3620 ;; because the point would keep going back to the beginning of the
3621 ;; abbrev instead of to the end of the abbrev). We now keep the
3622 ;; abbrev hook from moving backwards.
3623 ;;;
3624 (defun idlwave-expand-region-abbrevs (start end)
3625 "Expand each abbrev occurrence in the region.
3626 Calling from a program, arguments are START END."
3627 (interactive "r")
3628 (save-excursion
3629 (goto-char (min start end))
3630 (let ((idlwave-show-block nil) ;Do not blink
3631 (idlwave-abbrev-move nil)) ;Do not move
3632 (expand-region-abbrevs start end 'noquery))))
3633
3634 (defun idlwave-quoted ()
3635 "Return t if point is in a comment or quoted string.
3636 Returns nil otherwise."
3637 (or (idlwave-in-comment) (idlwave-in-quote)))
3638
3639 (defun idlwave-in-quote ()
3640 "Return location of the opening quote
3641 if point is in a IDL string constant, nil otherwise.
3642 Ignores comment delimiters on the current line.
3643 Properly handles nested quotation marks and octal
3644 constants - a double quote followed by an octal digit."
3645 ;; Treat an octal inside an apostrophe to be a normal string. Treat a
3646 ;; double quote followed by an octal digit to be an octal constant
3647 ;; rather than a string. Therefore, there is no terminating double
3648 ;; quote.
3649 (save-excursion
3650 ;; Because single and double quotes can quote each other we must
3651 ;; search for the string start from the beginning of line.
3652 (let* ((start (point))
3653 (eol (point-at-eol))
3654 (bq (progn (beginning-of-line) (point)))
3655 (endq (point))
3656 (data (match-data))
3657 delim
3658 found)
3659 (while (< endq start)
3660 ;; Find string start
3661 ;; Don't find an octal constant beginning with a double quote
3662 (if (re-search-forward "[\"']" eol 'lim)
3663 ;; Find the string end.
3664 ;; In IDL, two consecutive delimiters after the start of a
3665 ;; string act as an
3666 ;; escape for the delimiter in the string.
3667 ;; Two consecutive delimiters alone (i.e., not after the
3668 ;; start of a string) is the null string.
3669 (progn
3670 ;; Move to position after quote
3671 (goto-char (1+ (match-beginning 0)))
3672 (setq bq (1- (point)))
3673 ;; Get the string delimiter
3674 (setq delim (char-to-string (preceding-char)))
3675 ;; Check for null string
3676 (if (looking-at delim)
3677 (progn (setq endq (point)) (forward-char 1))
3678 ;; Look for next unpaired delimiter
3679 (setq found (search-forward delim eol 'lim))
3680 (while (looking-at delim)
3681 (forward-char 1)
3682 (setq found (search-forward delim eol 'lim)))
3683 (setq endq (if found (1- (point)) (point)))
3684 ))
3685 (progn (setq bq (point)) (setq endq (point)))))
3686 (store-match-data data)
3687 ;; return string beginning position or nil
3688 (if (> start bq) bq))))
3689
3690 (defun idlwave-is-pointer-dereference (&optional limit)
3691 "Determine if the character after point is a pointer dereference *."
3692 (and
3693 (eq (char-after) ?\*)
3694 (not (idlwave-in-quote))
3695 (save-excursion
3696 (forward-char)
3697 (re-search-backward (concat "\\(" idlwave-idl-keywords
3698 "\\|[[(*+-/=,^><]\\)\\s-*\\*") limit t))))
3699
3700
3701 ;; Statement templates
3702
3703 ;; Replace these with a general template function, something like
3704 ;; expand.el (I think there was also something with a name similar to
3705 ;; dmacro.el)
3706
3707 (defun idlwave-template (s1 s2 &optional prompt noindent)
3708 "Build a template with optional prompt expression.
3709
3710 Opens a line if point is not followed by a newline modulo intervening
3711 whitespace. S1 and S2 are strings. S1 is inserted at point followed
3712 by S2. Point is inserted between S1 and S2. The case of S1 and S2 is
3713 adjusted according to `idlwave-abbrev-change-case'. If optional
3714 argument PROMPT is a string then it is displayed as a message in the
3715 minibuffer. The PROMPT serves as a reminder to the user of an
3716 expression to enter.
3717
3718 The lines containing S1 and S2 are reindented using `indent-region'
3719 unless the optional second argument NOINDENT is non-nil."
3720 (if (derived-mode-p 'idlwave-shell-mode)
3721 ;; This is a gross hack to avoit template abbrev expansion
3722 ;; in the shell. FIXME: This is a dirty hack.
3723 (if (and (eq this-command 'self-insert-command)
3724 (equal last-abbrev-location (point)))
3725 (insert last-abbrev-text)
3726 (error "No templates in idlwave-shell"))
3727 (cond ((eq idlwave-abbrev-change-case 'down)
3728 (setq s1 (downcase s1) s2 (downcase s2)))
3729 (idlwave-abbrev-change-case
3730 (setq s1 (upcase s1) s2 (upcase s2))))
3731 (let ((beg (point-at-bol))
3732 end)
3733 (if (not (looking-at "\\s-*\n"))
3734 (open-line 1))
3735 (insert s1)
3736 (save-excursion
3737 (insert s2)
3738 (setq end (point)))
3739 (if (not noindent)
3740 (indent-region beg end nil))
3741 (if (stringp prompt)
3742 (message "%s" prompt)))))
3743
3744 (defun idlwave-rw-case (string)
3745 "Make STRING have the case required by `idlwave-reserved-word-upcase'."
3746 (if idlwave-reserved-word-upcase
3747 (upcase string)
3748 string))
3749
3750 (defun idlwave-elif ()
3751 "Build skeleton IDL if-else block."
3752 (interactive)
3753 (idlwave-template
3754 (idlwave-rw-case "if")
3755 (idlwave-rw-case " then begin\n\nendif else begin\n\nendelse")
3756 "Condition expression"))
3757
3758 (defun idlwave-case ()
3759 "Build skeleton IDL case statement."
3760 (interactive)
3761 (idlwave-template
3762 (idlwave-rw-case "case")
3763 (idlwave-rw-case " of\n\nendcase")
3764 "Selector expression"))
3765
3766 (defun idlwave-switch ()
3767 "Build skeleton IDL switch statement."
3768 (interactive)
3769 (idlwave-template
3770 (idlwave-rw-case "switch")
3771 (idlwave-rw-case " of\n\nendswitch")
3772 "Selector expression"))
3773
3774 (defun idlwave-for ()
3775 "Build skeleton IDL loop statement."
3776 (interactive)
3777 (idlwave-template
3778 (idlwave-rw-case "for")
3779 (idlwave-rw-case " do begin\n\nendfor")
3780 "Loop expression"))
3781
3782 (defun idlwave-if ()
3783 "Build skeleton IDL if statement."
3784 (interactive)
3785 (idlwave-template
3786 (idlwave-rw-case "if")
3787 (idlwave-rw-case " then begin\n\nendif")
3788 "Scalar logical expression"))
3789
3790 (defun idlwave-procedure ()
3791 (interactive)
3792 (idlwave-template
3793 (idlwave-rw-case "pro")
3794 (idlwave-rw-case "\n\nreturn\nend")
3795 "Procedure name"))
3796
3797 (defun idlwave-function ()
3798 (interactive)
3799 (idlwave-template
3800 (idlwave-rw-case "function")
3801 (idlwave-rw-case "\n\nreturn\nend")
3802 "Function name"))
3803
3804 (defun idlwave-repeat ()
3805 (interactive)
3806 (idlwave-template
3807 (idlwave-rw-case "repeat begin\n\nendrep until")
3808 (idlwave-rw-case "")
3809 "Exit condition"))
3810
3811 (defun idlwave-while ()
3812 (interactive)
3813 (idlwave-template
3814 (idlwave-rw-case "while")
3815 (idlwave-rw-case " do begin\n\nendwhile")
3816 "Entry condition"))
3817
3818 (defun idlwave-split-string (string &optional pattern)
3819 "Return a list of substrings of STRING which are separated by PATTERN.
3820 If PATTERN is omitted, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
3821 (or pattern
3822 (setq pattern "[ \f\t\n\r\v]+"))
3823 (let (parts (start 0))
3824 (while (string-match pattern string start)
3825 (setq parts (cons (substring string start (match-beginning 0)) parts)
3826 start (match-end 0)))
3827 (nreverse (cons (substring string start) parts))))
3828
3829 (defun idlwave-replace-string (string replace_string replace_with)
3830 (let* ((start 0)
3831 (last (length string))
3832 (ret_string "")
3833 end)
3834 (while (setq end (string-match replace_string string start))
3835 (setq ret_string
3836 (concat ret_string (substring string start end) replace_with))
3837 (setq start (match-end 0)))
3838 (setq ret_string (concat ret_string (substring string start last)))))
3839
3840 (defun idlwave-get-buffer-visiting (file)
3841 ;; Return the buffer currently visiting FILE
3842 (cond
3843 ((boundp 'find-file-compare-truenames) ; XEmacs
3844 (let ((find-file-compare-truenames t))
3845 (get-file-buffer file)))
3846 ((fboundp 'find-buffer-visiting) ; Emacs
3847 (find-buffer-visiting file))
3848 (t (error "This should not happen (idlwave-get-buffer-visiting)"))))
3849
3850 (defvar idlwave-outlawed-buffers nil
3851 "List of buffers pulled up by IDLWAVE for special reasons.
3852 Buffers in this list may be killed by `idlwave-kill-autoloaded-buffers'.")
3853
3854 (defun idlwave-find-file-noselect (file &optional why)
3855 ;; Return a buffer visiting file.
3856 (or (idlwave-get-buffer-visiting file)
3857 (let ((buf (find-file-noselect file)))
3858 (if why (add-to-list 'idlwave-outlawed-buffers (cons buf why)))
3859 buf)))
3860
3861 (defun idlwave-kill-autoloaded-buffers ()
3862 "Kill buffers created automatically by IDLWAVE.
3863 Function prompts for a letter to identify the buffers to kill.
3864 Possible letters are:
3865
3866 f Buffers created by the command \\[idlwave-find-module] or mouse
3867 clicks in the routine info window.
3868 s Buffers created by the IDLWAVE Shell to display where execution
3869 stopped or an error was found.
3870 a Both of the above.
3871
3872 Buffers containing unsaved changes require confirmation before they are killed."
3873 (interactive)
3874 (if (null idlwave-outlawed-buffers)
3875 (error "No IDLWAVE-created buffers available")
3876 (princ (format "Kill IDLWAVE-created buffers: [f]ind source(%d), [s]hell display(%d), [a]ll ? "
3877 (idlwave-count-outlawed-buffers 'find)
3878 (idlwave-count-outlawed-buffers 'shell)))
3879 (let ((c (read-char)))
3880 (cond
3881 ((member c '(?f ?\C-f))
3882 (idlwave-do-kill-autoloaded-buffers 'find))
3883 ((member c '(?s ?\C-s))
3884 (idlwave-do-kill-autoloaded-buffers 'shell))
3885 ((member c '(?a ?\C-a))
3886 (idlwave-do-kill-autoloaded-buffers t))
3887 (t (error "Abort"))))))
3888
3889 (defun idlwave-count-outlawed-buffers (tag)
3890 "How many outlawed buffers have tag TAG?"
3891 (length (delq nil
3892 (mapcar
3893 (lambda (x) (eq (cdr x) tag))
3894 idlwave-outlawed-buffers))))
3895
3896 (defun idlwave-do-kill-autoloaded-buffers (&rest reasons)
3897 "Kill all buffers pulled up by IDLWAVE matching REASONS."
3898 (let* ((list (copy-sequence idlwave-outlawed-buffers))
3899 (cnt 0)
3900 entry)
3901 (while (setq entry (pop list))
3902 (if (buffer-live-p (car entry))
3903 (and (or (memq t reasons)
3904 (memq (cdr entry) reasons))
3905 (kill-buffer (car entry))
3906 (incf cnt)
3907 (setq idlwave-outlawed-buffers
3908 (delq entry idlwave-outlawed-buffers)))
3909 (setq idlwave-outlawed-buffers
3910 (delq entry idlwave-outlawed-buffers))))
3911 (message "%d buffer%s killed" cnt (if (= cnt 1) "" "s"))))
3912
3913 (defun idlwave-revoke-license-to-kill ()
3914 "Remove BUFFER from the buffers which may be killed.
3915 Killing would be done by `idlwave-do-kill-autoloaded-buffers'.
3916 Intended for `after-save-hook'."
3917 (let* ((buf (current-buffer))
3918 (entry (assq buf idlwave-outlawed-buffers)))
3919 ;; Revoke license
3920 (if entry
3921 (setq idlwave-outlawed-buffers
3922 (delq entry idlwave-outlawed-buffers)))
3923 ;; Remove this function from the hook.
3924 (remove-hook 'after-save-hook 'idlwave-revoke-license-to-kill 'local)))
3925
3926 (defvar idlwave-path-alist)
3927 (defun idlwave-locate-lib-file (file)
3928 ;; Find FILE on the scanned lib path and return a buffer visiting it
3929 (let* ((dirs idlwave-path-alist)
3930 dir efile)
3931 (catch 'exit
3932 (while (setq dir (car (pop dirs)))
3933 (if (file-regular-p
3934 (setq efile (expand-file-name file dir)))
3935 (throw 'exit efile))))))
3936
3937 (defun idlwave-expand-lib-file-name (file)
3938 ;; Find FILE on the scanned lib path and return a buffer visiting it
3939 ;; This is for, e.g., finding source with no user catalog
3940 (cond
3941 ((null file) nil)
3942 ((file-name-absolute-p file) file)
3943 (t (idlwave-locate-lib-file file))))
3944
3945 (defun idlwave-make-tags ()
3946 "Create the IDL tags file IDLTAGS in the current directory from
3947 the list of directories specified in the minibuffer. Directories may be
3948 for example: . /usr/local/rsi/idl/lib. All the subdirectories of the
3949 specified top directories are searched if the directory name is prefixed
3950 by @. Specify @ directories with care, it may take a long, long time if
3951 you specify /."
3952 (interactive)
3953 (let (directory directories cmd append status numdirs dir getsubdirs
3954 buffer save_buffer files numfiles item errbuf)
3955
3956 ;;
3957 ;; Read list of directories
3958 (setq directory (read-string "Tag Directories: " "."))
3959 (setq directories (idlwave-split-string directory "[ \t]+"))
3960 ;;
3961 ;; Set etags command, vars
3962 (setq cmd "etags --output=IDLTAGS --language=none --regex='/[
3963 \\t]*[pP][Rr][Oo][ \\t]+\\([^ \\t,]+\\)/' --regex='/[
3964 \\t]*[Ff][Uu][Nn][Cc][Tt][Ii][Oo][Nn][ \\t]+\\([^ \\t,]+\\)/' ")
3965 (setq append " ")
3966 (setq status 0)
3967 ;;
3968 ;; For each directory
3969 (setq numdirs 0)
3970 (setq dir (nth numdirs directories))
3971 (while (and dir)
3972 ;;
3973 ;; Find the subdirectories
3974 (if (string-match "^[@]\\(.+\\)$" dir)
3975 (setq getsubdirs t) (setq getsubdirs nil))
3976 (if (and getsubdirs) (setq dir (substring dir 1 (length dir))))
3977 (setq dir (expand-file-name dir))
3978 (if (file-directory-p dir)
3979 (progn
3980 (if (and getsubdirs)
3981 (progn
3982 (setq buffer (get-buffer-create "*idltags*"))
3983 (call-process "sh" nil buffer nil "-c"
3984 (concat "find " dir " -type d -print"))
3985 (setq save_buffer (current-buffer))
3986 (set-buffer buffer)
3987 (setq files (idlwave-split-string
3988 (idlwave-replace-string
3989 (buffer-substring 1 (point-max))
3990 "\n" "/*.pro ")
3991 "[ \t]+"))
3992 (set-buffer save_buffer)
3993 (kill-buffer buffer))
3994 (setq files (list (concat dir "/*.pro"))))
3995 ;;
3996 ;; For each subdirectory
3997 (setq numfiles 0)
3998 (setq item (nth numfiles files))
3999 (while (and item)
4000 ;;
4001 ;; Call etags
4002 (if (not (string-match "^[ \\t]*$" item))
4003 (progn
4004 (message "%s" (concat "Tagging " item "..."))
4005 (setq errbuf (get-buffer-create "*idltags-error*"))
4006 (setq status (+ status
4007 (if (eq 0 (call-process
4008 "sh" nil errbuf nil "-c"
4009 (concat cmd append item)))
4010 0
4011 1)))
4012 ;;
4013 ;; Append additional tags
4014 (setq append " --append ")
4015 (setq numfiles (1+ numfiles))
4016 (setq item (nth numfiles files)))
4017 (progn
4018 (setq numfiles (1+ numfiles))
4019 (setq item (nth numfiles files))
4020 )))
4021
4022 (setq numdirs (1+ numdirs))
4023 (setq dir (nth numdirs directories)))
4024 (progn
4025 (setq numdirs (1+ numdirs))
4026 (setq dir (nth numdirs directories)))))
4027
4028 (setq errbuf (get-buffer-create "*idltags-error*"))
4029 (if (= status 0)
4030 (kill-buffer errbuf))
4031 (message "")
4032 ))
4033
4034 (defun idlwave-toggle-comment-region (beg end &optional n)
4035 "Comment the lines in the region if the first non-blank line is
4036 commented, and conversely, uncomment region. If optional prefix arg
4037 N is non-nil, then for N positive, add N comment delimiters or for N
4038 negative, remove N comment delimiters.
4039 Uses `comment-region' which does not place comment delimiters on
4040 blank lines."
4041 (interactive "r\nP")
4042 (if n
4043 (comment-region beg end (prefix-numeric-value n))
4044 (save-excursion
4045 (goto-char beg)
4046 (beginning-of-line)
4047 ;; skip blank lines
4048 (skip-chars-forward " \t\n")
4049 (if (looking-at (concat "[ \t]*\\(" comment-start "+\\)"))
4050 (if (fboundp 'uncomment-region)
4051 (uncomment-region beg end)
4052 (comment-region beg end
4053 (- (length (buffer-substring
4054 (match-beginning 1)
4055 (match-end 1))))))
4056 (comment-region beg end)))))
4057
4058
4059 ;; ----------------------------------------------------------------------------
4060 ;; ----------------------------------------------------------------------------
4061 ;; ----------------------------------------------------------------------------
4062 ;; ----------------------------------------------------------------------------
4063 ;;
4064 ;; Completion and Routine Info
4065 ;;
4066
4067 ;; String "intern" functions
4068
4069 ;; For the completion and routine info function, we want to normalize
4070 ;; the case of procedure names etc. We do this by "interning" these
4071 ;; string is a hand-crafted way. Hashes are used to map the downcase
4072 ;; version of the strings to the cased versions. Most *-sint-*
4073 ;; variables consist of *two* hashes, a buffer+shell, followed by a
4074 ;; system hash. The former is re-scanned, and the latter takes case
4075 ;; precedence.
4076 ;;
4077 ;; Since these cased versions are really lisp objects, we can use `eq'
4078 ;; to search, which is a large performance boost. All new strings
4079 ;; need to be "sinterned". We do this as early as possible after
4080 ;; getting these strings from completion or buffer substrings. So
4081 ;; most of the code can simply assume to deal with "sinterned"
4082 ;; strings. The only exception is that the functions which scan whole
4083 ;; buffers for routine information do not intern the grabbed strings.
4084 ;; This is only done afterwards. Therefore in these functions it is
4085 ;; *not* safe to assume the strings can be compared with `eq' and be
4086 ;; fed into the routine assq functions.
4087
4088 ;; Here we define the hashing functions.
4089
4090 ;; The variables which hold the hashes.
4091 (defvar idlwave-sint-routines '(nil))
4092 (defvar idlwave-sint-keywords '(nil))
4093 (defvar idlwave-sint-methods '(nil))
4094 (defvar idlwave-sint-classes '(nil))
4095 (defvar idlwave-sint-dirs '(nil))
4096 (defvar idlwave-sint-libnames '(nil))
4097
4098 (defun idlwave-reset-sintern (&optional what)
4099 "Reset all sintern hashes."
4100 ;; Make sure the hash functions are accessible.
4101 (unless (and (fboundp 'gethash)
4102 (fboundp 'puthash))
4103 (require 'cl)
4104 (or (fboundp 'puthash)
4105 (defalias 'puthash 'cl-puthash)))
4106 (let ((entries '((idlwave-sint-routines 1000 10)
4107 (idlwave-sint-keywords 1000 10)
4108 (idlwave-sint-methods 100 10)
4109 (idlwave-sint-classes 10 10))))
4110
4111 ;; Make sure these are lists
4112 (loop for entry in entries
4113 for var = (car entry)
4114 do (if (not (consp (symbol-value var))) (set var (list nil))))
4115
4116 ;; Reset the system & library hash
4117 (when (or (eq what t) (eq what 'syslib)
4118 (null (cdr idlwave-sint-routines)))
4119 (loop for entry in entries
4120 for var = (car entry) for size = (nth 1 entry)
4121 do (setcdr (symbol-value var)
4122 (make-hash-table ':size size ':test 'equal)))
4123 (setq idlwave-sint-dirs nil
4124 idlwave-sint-libnames nil))
4125
4126 ;; Reset the buffer & shell hash
4127 (when (or (eq what t) (eq what 'bufsh)
4128 (null (car idlwave-sint-routines)))
4129 (loop for entry in entries
4130 for var = (car entry) for size = (nth 1 entry)
4131 do (setcar (symbol-value var)
4132 (make-hash-table ':size size ':test 'equal))))))
4133
4134 (defun idlwave-sintern-routine-or-method (name &optional class set)
4135 (if class
4136 (idlwave-sintern-method name set)
4137 (idlwave-sintern-routine name set)))
4138
4139 (defun idlwave-sintern (stype &rest args)
4140 (apply (intern (concat "idlwave-sintern-" (symbol-name stype))) args))
4141
4142 ;;(defmacro idlwave-sintern (type var)
4143 ;; `(cond ((not (stringp name)) name)
4144 ;; ((gethash (downcase name) (cdr ,var)))
4145 ;; ((gethash (downcase name) (car ,var)))
4146 ;; (set (idlwave-sintern-set name ,type ,var set))
4147 ;; (name)))
4148
4149 (defun idlwave-sintern-routine (name &optional set)
4150 (cond ((not (stringp name)) name)
4151 ((gethash (downcase name) (cdr idlwave-sint-routines)))
4152 ((gethash (downcase name) (car idlwave-sint-routines)))
4153 (set (idlwave-sintern-set name 'routine idlwave-sint-routines set))
4154 (name)))
4155 (defun idlwave-sintern-keyword (name &optional set)
4156 (cond ((not (stringp name)) name)
4157 ((gethash (downcase name) (cdr idlwave-sint-keywords)))
4158 ((gethash (downcase name) (car idlwave-sint-keywords)))
4159 (set (idlwave-sintern-set name 'keyword idlwave-sint-keywords set))
4160 (name)))
4161 (defun idlwave-sintern-method (name &optional set)
4162 (cond ((not (stringp name)) name)
4163 ((gethash (downcase name) (cdr idlwave-sint-methods)))
4164 ((gethash (downcase name) (car idlwave-sint-methods)))
4165 (set (idlwave-sintern-set name 'method idlwave-sint-methods set))
4166 (name)))
4167 (defun idlwave-sintern-class (name &optional set)
4168 (cond ((not (stringp name)) name)
4169 ((gethash (downcase name) (cdr idlwave-sint-classes)))
4170 ((gethash (downcase name) (car idlwave-sint-classes)))
4171 (set (idlwave-sintern-set name 'class idlwave-sint-classes set))
4172 (name)))
4173
4174 (defun idlwave-sintern-dir (dir &optional set)
4175 (car (or (member dir idlwave-sint-dirs)
4176 (setq idlwave-sint-dirs (cons dir idlwave-sint-dirs)))))
4177 (defun idlwave-sintern-libname (name &optional set)
4178 (car (or (member name idlwave-sint-libnames)
4179 (setq idlwave-sint-libnames (cons name idlwave-sint-libnames)))))
4180
4181 (defun idlwave-sintern-set (name type tables set)
4182 (let* ((func (or (cdr (assq type idlwave-completion-case))
4183 'identity))
4184 (iname (funcall (if (eq func 'preserve) 'identity func) name))
4185 (table (if (eq set 'sys) (cdr tables) (car tables))))
4186 (puthash (downcase name) iname table)
4187 iname))
4188
4189 (defun idlwave-sintern-keyword-list (kwd-list &optional set)
4190 "Sintern a set of keywords (file (key . link) (key2 . link2) ...)"
4191 (mapc (lambda(x)
4192 (setcar x (idlwave-sintern-keyword (car x) set)))
4193 (cdr kwd-list))
4194 kwd-list)
4195
4196 (defun idlwave-sintern-rinfo-list (list &optional set default-dir)
4197 "Sintern all strings in the rinfo LIST.
4198 With optional parameter SET: also set new patterns. Probably this
4199 will always have to be t. If DEFAULT-DIR is passed, it is used as
4200 the base of the directory."
4201 (let (entry name type class kwds res source call new)
4202 (while list
4203 (setq entry (car list)
4204 list (cdr list)
4205 name (car entry)
4206 type (nth 1 entry)
4207 class (nth 2 entry)
4208 source (nth 3 entry)
4209 call (nth 4 entry)
4210 kwds (nthcdr 5 entry))
4211
4212 ;; The class and name
4213 (if class
4214 (progn
4215 (if (symbolp class) (setq class (symbol-name class)))
4216 (setq class (idlwave-sintern-class class set))
4217 (setq name (idlwave-sintern-method name set)))
4218 (setq name (idlwave-sintern-routine name set)))
4219
4220 ;; The source
4221 (let ((source-type (car source))
4222 (source-file (nth 1 source))
4223 (source-dir (if default-dir
4224 (file-name-as-directory default-dir)
4225 (nth 2 source)))
4226 (source-lib (nth 3 source)))
4227 (if (stringp source-dir)
4228 (setq source-dir (idlwave-sintern-dir source-dir set)))
4229 (if (stringp source-lib)
4230 (setq source-lib (idlwave-sintern-libname source-lib set)))
4231 (setq source (list source-type source-file source-dir source-lib)))
4232
4233 ;; The keywords
4234 (setq kwds (mapcar (lambda (x)
4235 (idlwave-sintern-keyword-list x set))
4236 kwds))
4237
4238 ;; Build a canonicalized list
4239 (setq new (nconc (list name type class source call) kwds)
4240 res (cons new res)))
4241 (nreverse res)))
4242
4243 ;; Creating new sintern tables
4244
4245 (defun idlwave-new-sintern-type (tag)
4246 "Define a variable and a function to sintern the new type TAG.
4247 This defines the function `idlwave-sintern-TAG' and the variable
4248 `idlwave-sint-TAGs'."
4249 (let* ((name (symbol-name tag))
4250 (names (concat name "s"))
4251 (var (intern (concat "idlwave-sint-" names)))
4252 (func (intern (concat "idlwave-sintern-" name))))
4253 (set var nil) ; initial value of the association list
4254 (fset func ; set the function
4255 `(lambda (name &optional set)
4256 (cond ((not (stringp name)) name)
4257 ((cdr (assoc (downcase name) ,var)))
4258 (set
4259 (setq ,var (cons (cons (downcase name) name) ,var))
4260 name)
4261 (name))))))
4262
4263 (defun idlwave-reset-sintern-type (tag)
4264 "Reset the sintern variable associated with TAG."
4265 (set (intern (concat "idlwave-sint-" (symbol-name tag) "s")) nil))
4266
4267 ;;---------------------------------------------------------------------------
4268
4269
4270 ;; The variables which hold the information
4271 (defvar idlwave-system-routines nil
4272 "Holds the routine-info obtained by scanning buffers.")
4273 (defvar idlwave-buffer-routines nil
4274 "Holds the routine-info obtained by scanning buffers.")
4275 (defvar idlwave-compiled-routines nil
4276 "Holds the routine-info obtained by asking the shell.")
4277 (defvar idlwave-unresolved-routines nil
4278 "Holds the unresolved routine-info obtained by asking the shell.")
4279 (defvar idlwave-user-catalog-routines nil
4280 "Holds the procedure routine-info from the user scan.")
4281 (defvar idlwave-library-catalog-routines nil
4282 "Holds the procedure routine-info from the .idlwave_catalog library files.")
4283 (defvar idlwave-library-catalog-libname nil
4284 "Name of library catalog loaded from .idlwave_catalog files.")
4285 (defvar idlwave-path-alist nil
4286 "Alist with !PATH directories and zero or more flags if the dir has
4287 been scanned in a user catalog ('user) or discovered in a library
4288 catalog \('lib).")
4289 (defvar idlwave-true-path-alist nil
4290 "Like `idlwave-path-alist', but with true filenames.")
4291 (defvar idlwave-routines nil
4292 "Holds the combined procedure/function/method routine-info.")
4293 (defvar idlwave-class-alist nil
4294 "Holds the class names known to IDLWAVE.")
4295 (defvar idlwave-class-history nil
4296 "The history of classes selected with the minibuffer.")
4297 (defvar idlwave-force-class-query nil)
4298 (defvar idlwave-before-completion-wconf nil
4299 "The window configuration just before the completion buffer was displayed.")
4300 (defvar idlwave-last-system-routine-info-cons-cell nil
4301 "The last cons cell in the system routine info.")
4302
4303 ;;
4304 ;; The code to get routine info from different sources.
4305
4306 (defvar idlwave-system-routines)
4307 (defvar idlwave-catalog-process nil
4308 "The background process currently updating the catalog.")
4309
4310 (defun idlwave-routines ()
4311 "Provide a list of IDL routines.
4312 This routine loads the builtin routines on the first call.
4313 Later it only returns the value of the variable."
4314 (if (and idlwave-catalog-process
4315 (processp idlwave-catalog-process))
4316 (progn
4317 (cond
4318 ((equal (process-status idlwave-catalog-process) 'exit)
4319 (message "updating........")
4320 (setq idlwave-catalog-process nil)
4321 (idlwave-update-routine-info '(4)))
4322 ((equal (process-status idlwave-catalog-process) 'run)
4323 ;; Keep it running...
4324 )
4325 (t
4326 ;; Something is wrong, get rid of the process
4327 (message "Problem with catalog process") (beep)
4328 (condition-case nil
4329 (kill-process idlwave-catalog-process)
4330 (error nil))
4331 (setq idlwave-catalog-process nil)))))
4332 (or idlwave-routines
4333 (progn
4334 (idlwave-update-routine-info)
4335 ;; return the current value
4336 idlwave-routines)))
4337
4338 (defvar idlwave-update-rinfo-hook nil
4339 "List of functions which should run after a global rinfo update.
4340 Does not run after automatic updates of buffer or the shell.")
4341
4342 (defun idlwave-rescan-catalog-directories ()
4343 "Rescan the previously selected directories. For batch processing."
4344 (idlwave-update-routine-info '(16)))
4345
4346 (defun idlwave-rescan-asynchronously ()
4347 "Dispatch another Emacs instance to update the idlwave catalog.
4348 After the process finishes normally, the first access to routine info
4349 will re-read the catalog."
4350 (interactive)
4351 (if (processp idlwave-catalog-process)
4352 (if (eq (process-status idlwave-catalog-process) 'run)
4353 (if (yes-or-no-p "A catalog-updating process is running. Kill it? ")
4354 (progn
4355 (condition-case nil
4356 (kill-process idlwave-catalog-process)
4357 (error nil))
4358 (error "Process killed, no new process started"))
4359 (error "Quit"))
4360 (condition-case nil
4361 (kill-process idlwave-catalog-process)
4362 (error nil))))
4363 (if (or (not idlwave-user-catalog-file)
4364 (not (stringp idlwave-user-catalog-file))
4365 (not (file-regular-p idlwave-user-catalog-file)))
4366 (error "No catalog has been produced yet"))
4367 (let* ((emacs (concat invocation-directory invocation-name))
4368 (args (list "-batch"
4369 "-l" (expand-file-name "~/.emacs")
4370 "-l" "idlwave"
4371 "-f" "idlwave-rescan-catalog-directories"))
4372 (process (apply 'start-process "idlcat"
4373 nil emacs args)))
4374 (setq idlwave-catalog-process process)
4375 (set-process-sentinel
4376 process
4377 (lambda (pro why)
4378 (when (string-match "finished" why)
4379 (setq idlwave-routines nil
4380 idlwave-system-routines nil
4381 idlwave-catalog-process nil)
4382 (or (idlwave-start-load-rinfo-timer)
4383 (idlwave-update-routine-info '(4))))))
4384 (message "Background job started to update catalog file")))
4385
4386
4387 ;; Format for all routine info user catalog, library catalogs, etc.:
4388 ;;
4389 ;; ("ROUTINE" type class
4390 ;; (system) | (lib pro_file dir "LIBNAME") | (user pro_file dir "USERLIB") |
4391 ;; (buffer pro_file dir) | (compiled pro_file dir)
4392 ;; "calling_string" ("HELPFILE" (("KWD1" . link1) ...))
4393 ;; ("HELPFILE2" (("KWD2" . link) ...)) ...)
4394 ;;
4395 ;; DIR will be supplied dynamically while loading library catalogs,
4396 ;; and is sinterned to save space, as is LIBNAME. PRO_FILE can be a
4397 ;; complete filepath, in which case DIR is unnecessary. HELPFILE can
4398 ;; be nil, as can LINK1, etc., if no HTML help is available.
4399
4400
4401 (defvar idlwave-load-rinfo-idle-timer)
4402 (defvar idlwave-shell-path-query)
4403
4404 (defun idlwave-update-routine-info (&optional arg no-concatenate)
4405 "Update the internal routine-info lists.
4406 These lists are used by `idlwave-routine-info' (\\[idlwave-routine-info])
4407 and by `idlwave-complete' (\\[idlwave-complete]) to provide information
4408 about individual routines.
4409
4410 The information can come from 4 sources:
4411 1. IDL programs in the current editing session
4412 2. Compiled modules in an IDL shell running as Emacs subprocess
4413 3. A list which covers the IDL system routines.
4414 4. A list which covers the prescanned library files.
4415
4416 Scans all IDLWAVE-mode buffers of the current editing session (see
4417 `idlwave-scan-all-buffers-for-routine-info').
4418 When an IDL shell is running, this command also queries the IDL program
4419 for currently compiled routines.
4420
4421 With prefix ARG, also reload the system and library lists.
4422 With two prefix ARG's, also rescans the chosen user catalog tree.
4423 With three prefix args, dispatch asynchronous process to do the update.
4424
4425 If NO-CONCATENATE is non-nil, don't pre-concatenate the routine info
4426 lists, but instead wait for the shell query to complete and
4427 asynchronously finish updating routine info. This is set
4428 automatically when called interactively. When you need routine
4429 information updated immediately, leave NO-CONCATENATE nil."
4430 (interactive "P\np")
4431 ;; Stop any idle processing
4432 (if (or (and (fboundp 'itimerp)
4433 (itimerp idlwave-load-rinfo-idle-timer))
4434 (and (fboundp 'timerp)
4435 (timerp idlwave-load-rinfo-idle-timer)))
4436 (cancel-timer idlwave-load-rinfo-idle-timer))
4437 (cond
4438 ((equal arg '(64))
4439 ;; Start a background process which updates the catalog.
4440 (idlwave-rescan-asynchronously))
4441 ((equal arg '(16))
4442 ;; Update the user catalog now, and wait for them.
4443 (idlwave-create-user-catalog-file t))
4444 (t
4445 (let* ((load (or arg
4446 idlwave-buffer-case-takes-precedence
4447 (null idlwave-routines)))
4448 ;; The override-idle means, even if the idle timer has done some
4449 ;; preparing work, load and renormalize everything anyway.
4450 (override-idle (or arg idlwave-buffer-case-takes-precedence)))
4451
4452 (setq idlwave-buffer-routines nil
4453 idlwave-compiled-routines nil
4454 idlwave-unresolved-routines nil)
4455 ;; Reset the appropriate hashes
4456 (if (get 'idlwave-reset-sintern 'done-by-idle)
4457 ;; reset was already done in idle time, so skip this step now once
4458 (put 'idlwave-reset-sintern 'done-by-idle nil)
4459 (idlwave-reset-sintern (cond (load t)
4460 ((null idlwave-system-routines) t)
4461 (t 'bufsh))))
4462
4463 (if idlwave-buffer-case-takes-precedence
4464 ;; We can safely scan the buffer stuff first
4465 (progn
4466 (idlwave-update-buffer-routine-info)
4467 (and load (idlwave-load-all-rinfo override-idle)))
4468 ;; We first do the system info, and then the buffers
4469 (and load (idlwave-load-all-rinfo override-idle))
4470 (idlwave-update-buffer-routine-info))
4471
4472 ;; Let's see if there is a shell
4473 (let* ((shell-is-running (and (fboundp 'idlwave-shell-is-running)
4474 (idlwave-shell-is-running)))
4475 (ask-shell (and shell-is-running
4476 idlwave-query-shell-for-routine-info)))
4477
4478 ;; Load the library catalogs again, first re-scanning the path
4479 (when arg
4480 (if shell-is-running
4481 (idlwave-shell-send-command idlwave-shell-path-query
4482 '(progn
4483 (idlwave-shell-get-path-info)
4484 (idlwave-scan-library-catalogs))
4485 'hide)
4486 (idlwave-scan-library-catalogs)))
4487
4488 (if (or (not ask-shell)
4489 (not no-concatenate))
4490 ;; 1. If we are not going to ask the shell, we need to do the
4491 ;; concatenation now.
4492 ;; 2. When this function is called non-interactively, it
4493 ;; means that someone needs routine info *now*. The
4494 ;; shell update causes the concatenation to be
4495 ;; *delayed*, so not in time for the current command.
4496 ;; Therefore, we do a concatenation now, even though
4497 ;; the shell might do it again.
4498 (idlwave-concatenate-rinfo-lists nil 'run-hooks))
4499
4500 (when ask-shell
4501 ;; Ask the shell about the routines it knows of.
4502 (message "Querying the shell")
4503 (idlwave-shell-update-routine-info nil t)))))))
4504
4505
4506 (defvar idlwave-load-rinfo-steps-done (make-vector 6 nil))
4507 (defvar idlwave-load-rinfo-idle-timer nil)
4508 (defun idlwave-start-load-rinfo-timer ()
4509 (if (or (and (fboundp 'itimerp)
4510 (itimerp idlwave-load-rinfo-idle-timer))
4511 (and (fboundp 'timerp)
4512 (timerp idlwave-load-rinfo-idle-timer)))
4513 (cancel-timer idlwave-load-rinfo-idle-timer))
4514 (setq idlwave-load-rinfo-steps-done (make-vector 6 nil))
4515 (setq idlwave-load-rinfo-idle-timer nil)
4516 (if (and idlwave-init-rinfo-when-idle-after
4517 (numberp idlwave-init-rinfo-when-idle-after)
4518 (not (equal 0 idlwave-init-rinfo-when-idle-after))
4519 (not idlwave-routines))
4520 (condition-case nil
4521 (progn
4522 (setq idlwave-load-rinfo-idle-timer
4523 (run-with-idle-timer
4524 idlwave-init-rinfo-when-idle-after
4525 nil 'idlwave-load-rinfo-next-step)))
4526 (error nil))))
4527
4528 (defvar idlwave-library-routines nil "Obsolete variable.")
4529
4530 ;;------ XML Help routine info system
4531 (defun idlwave-load-system-routine-info ()
4532 ;; Load the system routine info from the cached routine info file,
4533 ;; which, if necessary, will be re-created from the XML file on
4534 ;; disk. As a last fallback, load the (likely outdated) idlw-rinfo
4535 ;; file distributed with older IDLWAVE versions (<6.0)
4536 (unless (and (load idlwave-xml-system-rinfo-converted-file
4537 'noerror 'nomessage)
4538 (idlwave-xml-system-routine-info-up-to-date))
4539 ;; See if we can create it from XML source
4540 (condition-case nil
4541 (idlwave-convert-xml-system-routine-info)
4542 (error
4543 (unless (load idlwave-xml-system-rinfo-converted-file
4544 'noerror 'nomessage)
4545 (if idlwave-system-routines
4546 (message
4547 "Failed to load converted routine info, using old conversion.")
4548 (message
4549 "Failed to convert XML routine info, falling back on idlw-rinfo.")
4550 (if (not (load "idlw-rinfo" 'noerror 'nomessage))
4551 (message
4552 "Could not locate any system routine information."))))))))
4553
4554 (defun idlwave-xml-system-routine-info-up-to-date()
4555 (let* ((dir (file-name-as-directory
4556 (expand-file-name "help/online_help" (idlwave-sys-dir))))
4557 (catalog-file (expand-file-name "idl_catalog.xml" dir)))
4558 (file-newer-than-file-p ;converted file is newer than catalog
4559 idlwave-xml-system-rinfo-converted-file
4560 catalog-file)))
4561
4562 (defvar idlwave-system-class-info nil) ; Gathered from idlw-rinfo
4563 (defvar idlwave-system-variables-alist nil
4564 "Alist of system variables and the associated structure tags.
4565 Gets set in cached XML rinfo, or `idlw-rinfo.el'.")
4566 (defvar idlwave-executive-commands-alist nil
4567 "Alist of system variables and their help files.")
4568 (defvar idlwave-help-special-topic-words nil)
4569
4570
4571 (defun idlwave-shorten-syntax (syntax name &optional class)
4572 ;; From a list of syntax statements, shorten with %s and group with "or"
4573 (let ((case-fold-search t))
4574 (mapconcat
4575 (lambda (x)
4576 (while (string-match name x)
4577 (setq x (replace-match "%s" t t x)))
4578 (if class
4579 (while (string-match class x)
4580 (setq x (replace-match "%s" t t x))))
4581 x)
4582 (nreverse syntax)
4583 " or ")))
4584
4585 (defun idlwave-xml-create-class-method-lists (xml-entry)
4586 ;; Create a class list entry from the xml parsed list., returning a
4587 ;; cons of form (class-entry method-entries).
4588 (let* ((nameblock (nth 1 xml-entry))
4589 (class (cdr (assq 'name nameblock)))
4590 (link (cdr (assq 'link nameblock)))
4591 (params (cddr xml-entry))
4592 (case-fold-search t)
4593 class-entry
4594 method methods-entry extra-kwds
4595 props get-props set-props init-props inherits
4596 pelem ptype)
4597 (while params
4598 (setq pelem (car params))
4599 (when (listp pelem)
4600 (setq ptype (car pelem)
4601 props (car (cdr pelem)))
4602 (cond
4603 ((eq ptype 'SUPERCLASS)
4604 (let ((pname (cdr (assq 'name props)))
4605 (plink (cdr (assq 'link props))))
4606 (unless (and (string= pname "None")
4607 (string= plink "None"))
4608 (push pname inherits))))
4609
4610 ((eq ptype 'PROPERTY)
4611 (let ((pname (cdr (assq 'name props)))
4612 (plink (cdr (assq 'link props)))
4613 (get (string= (cdr (assq 'get props)) "Yes"))
4614 (set (string= (cdr (assq 'set props)) "Yes"))
4615 (init (string= (cdr (assq 'init props)) "Yes")))
4616 (if get (push (list pname plink) get-props))
4617 (if set (push (list pname plink) set-props))
4618 (if init (push (list pname plink) init-props))))
4619
4620 ((eq ptype 'METHOD)
4621 (setq method (cdr (assq 'name props)))
4622 (setq extra-kwds ;;Assume all property keywords are gathered already
4623 (cond
4624 ((string-match (concat class "::Init") method)
4625 (put 'init-props 'matched t)
4626 init-props)
4627 ((string-match (concat class "::GetProperty") method)
4628 (put 'get-props 'matched t)
4629 get-props)
4630 ((string-match (concat class "::SetProperty") method)
4631 (put 'set-props 'matched t)
4632 set-props)
4633 (t nil)))
4634 (setq methods-entry
4635 (nconc (idlwave-xml-create-rinfo-list pelem class extra-kwds)
4636 methods-entry)))
4637 (t)))
4638 (setq params (cdr params)))
4639 ;;(unless (get 'init-props 'matched)
4640 ;; (message "Failed to match Init in class %s" class))
4641 ;;(unless (get 'get-props 'matched)
4642 ;; (message "Failed to match GetProperty in class %s" class))
4643 ;;(unless (get 'set-props 'matched)
4644 ;; (message "Failed to match SetProperty in class %s" class))
4645 (setq class-entry
4646 (if inherits
4647 (list class (append '(inherits) inherits) (list 'link link))
4648 (list class (list 'link link))))
4649 (cons class-entry methods-entry)))
4650
4651 (defun idlwave-xml-create-rinfo-list (xml-entry &optional class extra-kws)
4652 ;; Create correctly structured list elements from ROUTINE or METHOD
4653 ;; XML list structures. Return a list of list elements, with more
4654 ;; than one sub-list possible if a routine can serve as both
4655 ;; procedure and function (e.g. call_method).
4656 (let* ((nameblock (nth 1 xml-entry))
4657 (name (cdr (assq 'name nameblock)))
4658 (link (cdr (assq 'link nameblock)))
4659 (params (cddr xml-entry))
4660 (syntax-vec (make-vector 3 nil)) ; procedure, function, exec command
4661 (case-fold-search t)
4662 syntax kwd klink pref-list kwds pelem ptype props result type)
4663 (if class ;; strip out class name from class method name string
4664 (if (string-match (concat class "::") name)
4665 (setq name (substring name (match-end 0)))))
4666 (while params
4667 (setq pelem (car params))
4668 (when (listp pelem)
4669 (setq ptype (car pelem)
4670 props (car (cdr pelem)))
4671 (cond
4672 ((eq ptype 'SYNTAX)
4673 (setq syntax (cdr (assq 'name props)))
4674 (if (string-match "-&gt;" syntax)
4675 (setq syntax (replace-match "->" t nil syntax)))
4676 (setq type (cdr (assq 'type props)))
4677 (push syntax
4678 (aref syntax-vec (cond
4679 ((string-match "^pro" type) 0)
4680 ((string-match "^fun" type) 1)
4681 ((string-match "^exec" type) 2)))))
4682 ((eq ptype 'KEYWORD)
4683 (setq kwd (cdr (assq 'name props))
4684 klink (cdr (assq 'link props)))
4685 (if (string-match "^\\[XY\\(Z?\\)\\]" kwd)
4686 (progn
4687 (setq pref-list
4688 (if (match-string 1 kwd) '("X" "Y" "Z") '("X" "Y"))
4689 kwd (substring kwd (match-end 0)))
4690 (loop for x in pref-list do
4691 (push (list (concat x kwd) klink) kwds)))
4692 (push (list kwd klink) kwds)))
4693
4694 (t))); Do nothing for the others
4695 (setq params (cdr params)))
4696
4697 ;; Debug
4698 ;; (if (and (null (aref syntax-vec 0))
4699 ;; (null (aref syntax-vec 1))
4700 ;; (null (aref syntax-vec 2)))
4701 ;; (with-current-buffer (get-buffer-create "IDL_XML_catalog_complaints")
4702 ;; (if class
4703 ;; (insert (format "Missing SYNTAX entry for %s::%s\n" class name))
4704 ;; (insert (message "Missing SYNTAX entry for %s\n" name)))))
4705
4706 ;; Executive commands are treated specially
4707 (if (aref syntax-vec 2)
4708 (cons (substring name 1) link)
4709 (if extra-kws (setq kwds (nconc kwds extra-kws)))
4710 (setq kwds (idlwave-rinfo-group-keywords kwds link))
4711 (loop for idx from 0 to 1 do
4712 (if (aref syntax-vec idx)
4713 (push (append (list name (if (eq idx 0) 'pro 'fun)
4714 class '(system)
4715 (idlwave-shorten-syntax
4716 (aref syntax-vec idx) name class))
4717 kwds) result)))
4718 result)))
4719
4720
4721 (defun idlwave-rinfo-group-keywords (kwds master-link)
4722 ;; Group keywords by link file, as a list with elements
4723 ;; (linkfile ( ("KWD1" . link1) ("KWD2" . link2))
4724 (let (kwd link anchor linkfiles block master-elt)
4725 (while kwds
4726 (setq kwd (car kwds)
4727 link (idlwave-split-link-target (nth 1 kwd))
4728 anchor (cdr link)
4729 link (car link)
4730 kwd (car kwd))
4731 (if (setq block (assoc link linkfiles))
4732 (push (cons kwd anchor) (cdr block))
4733 (push (list link (cons kwd anchor)) linkfiles))
4734 (setq kwds (cdr kwds)))
4735 ;; Ensure the master link is there
4736 (if (setq master-elt (assoc master-link linkfiles))
4737 (if (eq (car linkfiles) master-elt)
4738 linkfiles
4739 (cons master-elt (delq master-elt linkfiles)))
4740 (push (list master-link) linkfiles))))
4741
4742 (defun idlwave-convert-xml-clean-statement-aliases (aliases)
4743 ;; Clean up the syntax of routines which are actually aliases by
4744 ;; removing the "OR" from the statements
4745 (let (syntax entry)
4746 (loop for x in aliases do
4747 (setq entry (assoc x idlwave-system-routines))
4748 (when entry
4749 (while (string-match " +or +" (setq syntax (nth 4 entry)))
4750 (setf (nth 4 entry) (replace-match ", " t t syntax)))))))
4751
4752 (defun idlwave-convert-xml-clean-routine-aliases (aliases)
4753 ;; Duplicate and trim original routine aliases from rinfo list
4754 ;; This if for, e.g. OPENR/OPENW/OPENU
4755 (let (alias remove-list new parts all-parts)
4756 (loop for x in aliases do
4757 (when (setq parts (split-string (cdr x) "/"))
4758 (setq new (assoc (cdr x) all-parts))
4759 (unless new
4760 (setq new (cons (cdr x) parts))
4761 (push new all-parts))
4762 (setcdr new (delete (car x) (cdr new)))))
4763
4764 ;; Add any missing aliases (separate by slashes)
4765 (loop for x in all-parts do
4766 (if (cdr x)
4767 (push (cons (nth 1 x) (car x)) aliases)))
4768
4769 (loop for x in aliases do
4770 (when (setq alias (assoc (cdr x) idlwave-system-routines))
4771 (unless (memq alias remove-list) (push alias remove-list))
4772 (setq alias (copy-sequence alias))
4773 (setcar alias (car x))
4774 (push alias idlwave-system-routines)))
4775 (loop for x in remove-list do
4776 (delq x idlwave-system-routines))))
4777
4778 (defun idlwave-convert-xml-clean-sysvar-aliases (aliases)
4779 ;; Duplicate and trim original routine aliases from rinfo list
4780 ;; This if for, e.g. !X, !Y, !Z.
4781 (let (alias remove-list)
4782 (loop for x in aliases do
4783 (when (setq alias (assoc (cdr x) idlwave-system-variables-alist))
4784 (unless (memq alias remove-list) (push alias remove-list))
4785 (setq alias (copy-sequence alias))
4786 (setcar alias (car x))
4787 (push alias idlwave-system-variables-alist)))
4788 (loop for x in remove-list do
4789 (delq x idlwave-system-variables-alist))))
4790
4791
4792 (defun idlwave-xml-create-sysvar-alist (xml-entry)
4793 ;; Create a sysvar list entry from the xml parsed list.
4794 (let* ((nameblock (nth 1 xml-entry))
4795 (name (cdr (assq 'name nameblock)))
4796 (sysvar (substring name (progn (string-match "^ *!" name)
4797 (match-end 0))))
4798 (link (cdr (assq 'link nameblock)))
4799 (params (cddr xml-entry))
4800 (case-fold-search t)
4801 pelem ptype props tags)
4802 (while params
4803 (setq pelem (car params))
4804 (when (listp pelem)
4805 (setq ptype (car pelem)
4806 props (car (cdr pelem)))
4807 (cond
4808 ((eq ptype 'FIELD)
4809 (push (cons (cdr (assq 'name props))
4810 (cdr
4811 (idlwave-split-link-target (cdr (assq 'link props)))))
4812 tags))))
4813 (setq params (cdr params)))
4814 (delq nil
4815 (list sysvar (if tags (cons 'tags tags)) (list 'link link)))))
4816
4817
4818 (defvar idlwave-xml-routine-info-file nil)
4819
4820 (defun idlwave-save-routine-info ()
4821 (if idlwave-xml-routine-info-file
4822 (with-temp-file idlwave-xml-system-rinfo-converted-file
4823 (insert
4824 (concat ";; *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
4825 ;; IDLWAVE Routine Information File (IDLWAVE version " idlwave-mode-version ")
4826 ;; Automatically generated from source file:
4827 ;; " idlwave-xml-routine-info-file "
4828 ;; on " (current-time-string) "
4829 ;; Do not edit."))
4830 (insert (format "\n(setq idlwave-xml-routine-info-file \n \"%s\")"
4831 idlwave-xml-routine-info-file))
4832 (insert "\n(setq idlwave-system-routines\n '")
4833 (prin1 idlwave-system-routines (current-buffer))
4834 (insert ")")
4835 (insert "\n(setq idlwave-system-variables-alist\n '")
4836 (prin1 idlwave-system-variables-alist (current-buffer))
4837 (insert ")")
4838 (insert "\n(setq idlwave-system-class-info\n '")
4839 (prin1 idlwave-system-class-info (current-buffer))
4840 (insert ")")
4841 (insert "\n(setq idlwave-executive-commands-alist\n '")
4842 (prin1 idlwave-executive-commands-alist (current-buffer))
4843 (insert ")")
4844 (insert "\n(setq idlwave-help-special-topic-words\n '")
4845 (prin1 idlwave-help-special-topic-words (current-buffer))
4846 (insert ")"))))
4847
4848 (defun idlwave-convert-xml-system-routine-info ()
4849 "Convert XML supplied IDL routine info into internal form.
4850 Cache to disk for quick recovery."
4851 (interactive)
4852 (let* ((dir (file-name-as-directory
4853 (expand-file-name "help/online_help" (idlwave-sys-dir))))
4854 (catalog-file (expand-file-name "idl_catalog.xml" dir))
4855 (elem-cnt 0)
4856 props rinfo msg-cnt elem type nelem class-result alias
4857 routines routine-aliases statement-aliases sysvar-aliases)
4858 (if (not (file-exists-p catalog-file))
4859 (error "No such XML routine info file: %s" catalog-file)
4860 (if (not (file-readable-p catalog-file))
4861 (error "Cannot read XML routine info file: %s" catalog-file)))
4862 (message "Reading XML routine info...")
4863 (setq rinfo (xml-parse-file catalog-file))
4864 (message "Reading XML routine info...done")
4865 (setq rinfo (assq 'CATALOG rinfo))
4866 (unless rinfo (error "Failed to parse XML routine info"))
4867 ;;(setq rinfo (car rinfo)) ; Skip the catalog stuff.
4868
4869 (setq rinfo (cddr rinfo))
4870
4871 (setq nelem (length rinfo)
4872 msg-cnt (/ nelem 20))
4873
4874 (setq idlwave-xml-routine-info-file nil)
4875 (message "Converting XML routine info...")
4876 (setq idlwave-system-routines nil
4877 idlwave-system-variables-alist nil
4878 idlwave-system-class-info nil
4879 idlwave-executive-commands-alist nil
4880 idlwave-help-special-topic-words nil)
4881
4882 (while rinfo
4883 (setq elem (car rinfo)
4884 rinfo (cdr rinfo))
4885 (incf elem-cnt)
4886 (when (listp elem)
4887 (setq type (car elem)
4888 props (car (cdr elem)))
4889 (if (= (mod elem-cnt msg-cnt) 0)
4890 (message "Converting XML routine info...%2d%%"
4891 (/ (* elem-cnt 100) nelem)))
4892 (cond
4893 ((eq type 'ROUTINE)
4894 (if (setq alias (assq 'alias_to props))
4895 (push (cons (cdr (assq 'name props)) (cdr alias))
4896 routine-aliases)
4897 (setq routines (idlwave-xml-create-rinfo-list elem))
4898 (if (listp (cdr routines))
4899 (setq idlwave-system-routines
4900 (nconc idlwave-system-routines routines))
4901 ;; a cons cell is an executive commands
4902 (push routines idlwave-executive-commands-alist))))
4903
4904 ((eq type 'CLASS)
4905 (setq class-result (idlwave-xml-create-class-method-lists elem))
4906 (push (car class-result) idlwave-system-class-info)
4907 (setq idlwave-system-routines
4908 (nconc idlwave-system-routines (cdr class-result))))
4909
4910 ((eq type 'STATEMENT)
4911 (push (cons (cdr (assq 'name props))
4912 (cdr (assq 'link props)))
4913 idlwave-help-special-topic-words)
4914 ;; Save the links to those which are statement aliases (not routines)
4915 (if (setq alias (assq 'alias_to props))
4916 (unless (member (cdr alias) statement-aliases)
4917 (push (cdr alias) statement-aliases))))
4918
4919 ((eq type 'SYSVAR)
4920 (if (setq alias (cdr (assq 'alias_to props)))
4921 (push (cons (substring (cdr (assq 'name props)) 1)
4922 (substring alias 1))
4923 sysvar-aliases)
4924 (push (idlwave-xml-create-sysvar-alist elem)
4925 idlwave-system-variables-alist)))
4926 (t))))
4927 (idlwave-convert-xml-clean-routine-aliases routine-aliases)
4928 (idlwave-convert-xml-clean-statement-aliases statement-aliases)
4929 (idlwave-convert-xml-clean-sysvar-aliases sysvar-aliases)
4930
4931 (setq idlwave-xml-routine-info-file catalog-file)
4932 (idlwave-save-routine-info)
4933 (message "Converting XML routine info...done")))
4934
4935
4936 ;; ("ROUTINE" type class
4937 ;; (system) | (lib pro_file dir "LIBNAME") | (user pro_file dir "USERLIB") |
4938 ;; (buffer pro_file dir) | (compiled pro_file dir)
4939 ;; "calling_string" ("HELPFILE" (("KWD1" . link1) ...))
4940 ;; ("HELPFILE2" (("KWD2" . link) ...)) ...)
4941
4942
4943 (defun idlwave-load-rinfo-next-step ()
4944 (let ((inhibit-quit t)
4945 (arr idlwave-load-rinfo-steps-done))
4946 (if (catch 'exit
4947 (when (not (aref arr 0))
4948 (message "Loading system routine info in idle time...")
4949 (idlwave-load-system-routine-info)
4950 ;;(load "idlw-rinfo" 'noerror 'nomessage)
4951 (message "Loading system routine info in idle time...done")
4952 (aset arr 0 t)
4953 (throw 'exit t))
4954
4955 (when (not (aref arr 1))
4956 (message "Normalizing idlwave-system-routines in idle time...")
4957 (idlwave-reset-sintern t)
4958 (put 'idlwave-reset-sintern 'done-by-idle t)
4959 (setq idlwave-system-routines
4960 (idlwave-sintern-rinfo-list idlwave-system-routines 'sys))
4961 (message "Normalizing idlwave-system-routines in idle time...done")
4962 (aset arr 1 t)
4963 (throw 'exit t))
4964
4965 (when (not (aref arr 2))
4966 (when (and (stringp idlwave-user-catalog-file)
4967 (file-regular-p idlwave-user-catalog-file))
4968 (message "Loading user catalog in idle time...")
4969 (condition-case nil
4970 (load-file idlwave-user-catalog-file)
4971 (error (throw 'exit nil)))
4972 ;; Check for the old style catalog and warn
4973 (if (and
4974 (boundp 'idlwave-library-routines)
4975 idlwave-library-routines)
4976 (progn
4977 (setq idlwave-library-routines nil)
4978 (ding)
4979 (message "Outdated user catalog: %s... recreate"
4980 idlwave-user-catalog-file))
4981 (message "Loading user catalog in idle time...done")))
4982 (aset arr 2 t)
4983 (throw 'exit t))
4984
4985 (when (not (aref arr 3))
4986 (when idlwave-user-catalog-routines
4987 (message "Normalizing user catalog routines in idle time...")
4988 (setq idlwave-user-catalog-routines
4989 (idlwave-sintern-rinfo-list
4990 idlwave-user-catalog-routines 'sys))
4991 (message
4992 "Normalizing user catalog routines in idle time...done"))
4993 (aset arr 3 t)
4994 (throw 'exit t))
4995
4996 (when (not (aref arr 4))
4997 (idlwave-scan-library-catalogs
4998 "Loading and normalizing library catalogs in idle time...")
4999 (aset arr 4 t)
5000 (throw 'exit t))
5001 (when (not (aref arr 5))
5002 (message "Finishing initialization in idle time...")
5003 (idlwave-routines)
5004 (message "Finishing initialization in idle time...done")
5005 (aset arr 5 t)
5006 (throw 'exit nil)))
5007 ;; restart the timer
5008 (if (sit-for 1)
5009 (idlwave-load-rinfo-next-step)
5010 (setq idlwave-load-rinfo-idle-timer
5011 (run-with-idle-timer
5012 idlwave-init-rinfo-when-idle-after
5013 nil 'idlwave-load-rinfo-next-step))))))
5014
5015 (defvar idlwave-after-load-rinfo-hook nil)
5016
5017 (defun idlwave-load-all-rinfo (&optional force)
5018 ;; Load and case-treat the system, user catalog, and library routine
5019 ;; info files.
5020
5021 ;; System
5022 (when (or force (not (aref idlwave-load-rinfo-steps-done 0)))
5023 ;;(load "idlw-rinfo" 'noerror 'nomessage))
5024 (idlwave-load-system-routine-info))
5025 (when (or force (not (aref idlwave-load-rinfo-steps-done 1)))
5026 (message "Normalizing idlwave-system-routines...")
5027 (setq idlwave-system-routines
5028 (idlwave-sintern-rinfo-list idlwave-system-routines 'sys))
5029 (message "Normalizing idlwave-system-routines...done"))
5030 (when idlwave-system-routines
5031 (setq idlwave-routines (copy-sequence idlwave-system-routines))
5032 (setq idlwave-last-system-routine-info-cons-cell
5033 (nthcdr (1- (length idlwave-routines)) idlwave-routines)))
5034
5035 ;; User catalog
5036 (when (and (stringp idlwave-user-catalog-file)
5037 (file-regular-p idlwave-user-catalog-file))
5038 (condition-case nil
5039 (when (or force (not (aref idlwave-load-rinfo-steps-done 2)))
5040 (load-file idlwave-user-catalog-file))
5041 (error nil))
5042 (when (and
5043 (boundp 'idlwave-library-routines)
5044 idlwave-library-routines)
5045 (setq idlwave-library-routines nil)
5046 (error "Outdated user catalog: %s... recreate"
5047 idlwave-user-catalog-file))
5048 (setq idlwave-true-path-alist nil)
5049 (when (or force (not (aref idlwave-load-rinfo-steps-done 3)))
5050 (message "Normalizing user catalog routines...")
5051 (setq idlwave-user-catalog-routines
5052 (idlwave-sintern-rinfo-list
5053 idlwave-user-catalog-routines 'sys))
5054 (message "Normalizing user catalog routines...done")))
5055
5056 ;; Library catalog
5057 (when (or force (not (aref idlwave-load-rinfo-steps-done 4)))
5058 (idlwave-scan-library-catalogs
5059 "Loading and normalizing library catalogs..."))
5060 (run-hooks 'idlwave-after-load-rinfo-hook))
5061
5062
5063 (defun idlwave-update-buffer-routine-info ()
5064 (let (res)
5065 (cond
5066 ((eq idlwave-scan-all-buffers-for-routine-info t)
5067 ;; Scan all buffers, current buffer last
5068 (message "Scanning all buffers...")
5069 (setq res (idlwave-get-routine-info-from-buffers
5070 (reverse (buffer-list)))))
5071 ((null idlwave-scan-all-buffers-for-routine-info)
5072 ;; Don't scan any buffers
5073 (setq res nil))
5074 (t
5075 ;; Just scan this buffer
5076 (if (derived-mode-p 'idlwave-mode)
5077 (progn
5078 (message "Scanning current buffer...")
5079 (setq res (idlwave-get-routine-info-from-buffers
5080 (list (current-buffer))))))))
5081 ;; Put the result into the correct variable
5082 (setq idlwave-buffer-routines
5083 (idlwave-sintern-rinfo-list res 'set))))
5084
5085 (defun idlwave-concatenate-rinfo-lists (&optional quiet run-hook)
5086 "Put the different sources for routine information together."
5087 ;; The sequence here is important because earlier definitions shadow
5088 ;; later ones. We assume that if things in the buffers are newer
5089 ;; then in the shell of the system, they are meant to be different.
5090 (setcdr idlwave-last-system-routine-info-cons-cell
5091 (append idlwave-buffer-routines
5092 idlwave-compiled-routines
5093 idlwave-library-catalog-routines
5094 idlwave-user-catalog-routines))
5095 (setq idlwave-class-alist nil)
5096
5097 ;; Give a message with information about the number of routines we have.
5098 (unless quiet
5099 (message
5100 "Routines Found: buffer(%d) compiled(%d) library(%d) user(%d) system(%d)"
5101 (length idlwave-buffer-routines)
5102 (length idlwave-compiled-routines)
5103 (length idlwave-library-catalog-routines)
5104 (length idlwave-user-catalog-routines)
5105 (length idlwave-system-routines)))
5106 (if run-hook
5107 (run-hooks 'idlwave-update-rinfo-hook)))
5108
5109 (defun idlwave-class-alist ()
5110 "Return the class alist - make it if necessary."
5111 (or idlwave-class-alist
5112 (let (class)
5113 (loop for x in idlwave-routines do
5114 (when (and (setq class (nth 2 x))
5115 (not (assq class idlwave-class-alist)))
5116 (push (list class) idlwave-class-alist)))
5117 idlwave-class-alist)))
5118
5119 ;; Three functions for the hooks
5120 (defun idlwave-save-buffer-update ()
5121 (idlwave-update-current-buffer-info 'save-buffer))
5122 (defun idlwave-kill-buffer-update ()
5123 (idlwave-update-current-buffer-info 'kill-buffer))
5124 (defun idlwave-new-buffer-update ()
5125 (idlwave-update-current-buffer-info 'find-file))
5126
5127 (defun idlwave-update-current-buffer-info (why)
5128 "Update `idlwave-routines' for current buffer.
5129 Can run from `after-save-hook'."
5130 (when (and (derived-mode-p 'idlwave-mode)
5131 (or (eq t idlwave-auto-routine-info-updates)
5132 (memq why idlwave-auto-routine-info-updates))
5133 idlwave-scan-all-buffers-for-routine-info
5134 idlwave-routines)
5135 (condition-case nil
5136 (let (routines)
5137 (idlwave-replace-buffer-routine-info
5138 (buffer-file-name)
5139 (if (eq why 'kill-buffer)
5140 nil
5141 (setq routines
5142 (idlwave-sintern-rinfo-list
5143 (idlwave-get-routine-info-from-buffers
5144 (list (current-buffer))) 'set))))
5145 (idlwave-concatenate-rinfo-lists 'quiet)
5146 routines)
5147 (error nil))))
5148
5149 (defun idlwave-replace-buffer-routine-info (file new)
5150 "Cut the part from FILE out of `idlwave-buffer-routines' and add NEW."
5151 (let ((list idlwave-buffer-routines)
5152 found)
5153 (while list
5154 ;; The following test uses eq to make sure it works correctly
5155 ;; when two buffers visit the same file. Then the file names
5156 ;; will be equal, but not eq.
5157 (if (eq (idlwave-routine-source-file (nth 3 (car list))) file)
5158 (progn
5159 (setcar list nil)
5160 (setq found t))
5161 (if found
5162 ;; End of that section reached. Jump.
5163 (setq list nil)))
5164 (setq list (cdr list)))
5165 (setq idlwave-buffer-routines
5166 (append new (delq nil idlwave-buffer-routines)))))
5167
5168 ;;----- Scanning buffers -------------------
5169
5170 (defun idlwave-get-routine-info-from-buffers (buffers)
5171 "Call `idlwave-get-buffer-routine-info' on idlwave-mode buffers in BUFFERS."
5172 (let (buf routine-lists res)
5173 (save-excursion
5174 (while (setq buf (pop buffers))
5175 (set-buffer buf)
5176 (if (and (derived-mode-p 'idlwave-mode)
5177 buffer-file-name)
5178 ;; yes, this buffer has the right mode.
5179 (progn (setq res (condition-case nil
5180 (idlwave-get-buffer-routine-info)
5181 (error nil)))
5182 (push res routine-lists)))))
5183 ;; Concatenate the individual lists and return the result
5184 (apply 'nconc routine-lists)))
5185
5186 (defun idlwave-get-buffer-routine-info ()
5187 "Scan the current buffer for routine info. Return (PRO-LIST FUNC-LIST)."
5188 (let* ((case-fold-search t)
5189 routine-list string entry)
5190 (save-excursion
5191 (save-restriction
5192 (widen)
5193 (goto-char (point-min))
5194 (while (re-search-forward
5195 "^[ \t]*\\(pro\\|function\\)[ \t]" nil t)
5196 (setq string (buffer-substring-no-properties
5197 (match-beginning 0)
5198 (progn
5199 (idlwave-end-of-statement)
5200 (point))))
5201 (setq entry (idlwave-parse-definition string))
5202 (push entry routine-list))))
5203 routine-list))
5204
5205 (defvar idlwave-scanning-lib-dir)
5206 (defvar idlwave-scanning-lib)
5207 (defun idlwave-parse-definition (string)
5208 "Parse a module definition."
5209 (let ((case-fold-search t)
5210 start name args type keywords class)
5211 ;; Remove comments
5212 (while (string-match ";.*" string)
5213 (setq string (replace-match "" t t string)))
5214 ;; Remove the continuation line stuff
5215 (while (string-match "\\([^a-zA-Z0-9$_]\\)\\$[ \t]*\n" string)
5216 (setq string (replace-match "\\1 " t nil string)))
5217 (while (string-match "\n" string)
5218 (setq string (replace-match " " t nil string)))
5219 ;; Match the name and type.
5220 (when (string-match
5221 "\\<\\(pro\\|function\\)\\>\\s-+\\(\\([a-zA-Z0-9$_]+\\)::\\)?\\([a-zA-Z0-9$_]+\\)" string)
5222 (setq start (match-end 0))
5223 (setq type (downcase (match-string 1 string)))
5224 (if (match-beginning 3)
5225 (setq class (match-string 3 string)))
5226 (setq name (match-string 4 string)))
5227 ;; Match normal args and keyword args
5228 (while (string-match
5229 ",\\s-*\\([a-zA-Z][a-zA-Z0-9$_]*\\|\\(_ref\\)?_extra\\)\\s-*\\(=\\)?"
5230 string start)
5231 (setq start (match-end 0))
5232 (if (match-beginning 3)
5233 (push (match-string 1 string) keywords)
5234 (push (match-string 1 string) args)))
5235 ;; Normalize and sort.
5236 (setq args (nreverse args))
5237 (setq keywords (sort keywords (lambda (a b)
5238 (string< (downcase a) (downcase b)))))
5239 ;; Make and return the entry
5240 ;; We don't know which argument are optional, so this information
5241 ;; will not be contained in the calling sequence.
5242 (list name
5243 (if (equal type "pro") 'pro 'fun)
5244 class
5245 (cond ((not (boundp 'idlwave-scanning-lib))
5246 (list 'buffer (buffer-file-name)))
5247 ; ((string= (downcase
5248 ; (file-name-sans-extension
5249 ; (file-name-nondirectory (buffer-file-name))))
5250 ; (downcase name))
5251 ; (list 'lib))
5252 ; (t (cons 'lib (file-name-nondirectory (buffer-file-name))))
5253 (t (list 'user (file-name-nondirectory (buffer-file-name))
5254 idlwave-scanning-lib-dir "UserLib")))
5255 (concat
5256 (if (string= type "function") "Result = " "")
5257 (if class "Obj ->[%s::]" "")
5258 "%s"
5259 (if args
5260 (concat
5261 (if (string= type "function") "(" ", ")
5262 (mapconcat 'identity args ", ")
5263 (if (string= type "function") ")" ""))))
5264 (if keywords
5265 (cons nil (mapcar 'list keywords)) ;No help file
5266 nil))))
5267
5268
5269 ;;----- Scanning the user catalog -------------------
5270
5271 (defun idlwave-sys-dir ()
5272 "Return the syslib directory, or a dummy that never matches."
5273 (cond
5274 ((and idlwave-system-directory
5275 (not (string= idlwave-system-directory "")))
5276 idlwave-system-directory)
5277 ((getenv "IDL_DIR"))
5278 (t "@@@@@@@@")))
5279
5280
5281 (defun idlwave-create-user-catalog-file (&optional arg)
5282 "Scan all files on selected dirs of IDL search path for routine information.
5283
5284 A widget checklist will allow you to choose the directories. Write
5285 the result as a file `idlwave-user-catalog-file'. When this file
5286 exists, it will be automatically loaded to give routine information
5287 about library routines. With ARG, just rescan the same directories
5288 as last time - so no widget will pop up."
5289 (interactive "P")
5290 ;; Make sure the file is loaded if it exists.
5291 (if (and (stringp idlwave-user-catalog-file)
5292 (file-regular-p idlwave-user-catalog-file))
5293 (condition-case nil
5294 (load-file idlwave-user-catalog-file)
5295 (error nil)))
5296 ;; Make sure the file name makes sense
5297 (unless (and (stringp idlwave-user-catalog-file)
5298 (> (length idlwave-user-catalog-file) 0)
5299 (file-accessible-directory-p
5300 (file-name-directory idlwave-user-catalog-file))
5301 (not (string= "" (file-name-nondirectory
5302 idlwave-user-catalog-file))))
5303 (error "`idlwave-user-catalog-file' does not point to a file in an accessible directory"))
5304
5305 (cond
5306 ;; Rescan the known directories
5307 ((and arg idlwave-path-alist
5308 (consp (car idlwave-path-alist)))
5309 (idlwave-scan-user-lib-files idlwave-path-alist))
5310
5311 ;; Expand the directories from library-path and run the widget
5312 (idlwave-library-path
5313 (idlwave-display-user-catalog-widget
5314 (if idlwave-true-path-alist
5315 ;; Propagate any flags on the existing path-alist
5316 (mapcar (lambda (x)
5317 (let ((path-entry (assoc (file-truename x)
5318 idlwave-true-path-alist)))
5319 (if path-entry
5320 (cons x (cdr path-entry))
5321 (list x))))
5322 (idlwave-expand-path idlwave-library-path))
5323 (mapcar 'list (idlwave-expand-path idlwave-library-path)))))
5324
5325 ;; Ask the shell for the path and then run the widget
5326 (t
5327 (message "Asking the shell for IDL path...")
5328 (require 'idlw-shell)
5329 (idlwave-shell-send-command idlwave-shell-path-query
5330 '(idlwave-user-catalog-command-hook nil)
5331 'hide))))
5332
5333
5334 ;; Parse shell path information and select among it.
5335 (defun idlwave-user-catalog-command-hook (&optional arg)
5336 ;; Command hook used by `idlwave-create-user-catalog-file'.
5337 (if arg
5338 ;; Scan immediately
5339 (idlwave-scan-user-lib-files idlwave-path-alist)
5340 ;; Set the path and display the widget
5341 (idlwave-shell-get-path-info 'no-write) ; set to something path-alist
5342 (idlwave-scan-library-catalogs "Locating library catalogs..." 'no-load)
5343 (idlwave-display-user-catalog-widget idlwave-path-alist)))
5344
5345 (defconst idlwave-user-catalog-widget-help-string
5346 "This is the front-end to the creation of the IDLWAVE user catalog.
5347 Please select the directories on IDL's search path from which you
5348 would like to extract routine information, to be stored in the file:
5349
5350 %s
5351
5352 If this is not the correct file, first set variable
5353 `idlwave-user-catalog-file', and call this command again.
5354
5355 N.B. Many libraries include pre-scanned catalog files
5356 \(\".idlwave_catalog\"). These are marked with \"[LIB]\", and need
5357 not be scanned. You can scan your own libraries off-line using the
5358 perl script `idlwave_catalog'.
5359
5360 After selecting the directories, choose [Scan & Save] to scan the library
5361 directories and save the routine info.
5362 \n")
5363
5364 (defvar idlwave-widget)
5365 (defvar widget-keymap)
5366 (defun idlwave-display-user-catalog-widget (dirs-list)
5367 "Create the widget to select IDL search path directories for scanning."
5368 (interactive)
5369 (require 'widget)
5370 (require 'wid-edit)
5371 (unless dirs-list
5372 (error "Don't know IDL's search path"))
5373
5374 (kill-buffer (get-buffer-create "*IDLWAVE Widget*"))
5375 (switch-to-buffer (get-buffer-create "*IDLWAVE Widget*"))
5376 (kill-all-local-variables)
5377 (make-local-variable 'idlwave-widget)
5378 (widget-insert (format idlwave-user-catalog-widget-help-string
5379 idlwave-user-catalog-file))
5380
5381 (widget-create 'push-button
5382 :notify 'idlwave-widget-scan-user-lib-files
5383 "Scan & Save")
5384 (widget-insert " ")
5385 (widget-create 'push-button
5386 :notify 'idlwave-delete-user-catalog-file
5387 "Delete File")
5388 (widget-insert " ")
5389 (widget-create 'push-button
5390 :notify
5391 (lambda (&rest ignore)
5392 (let ((path-list (widget-get idlwave-widget :path-dirs)))
5393 (dolist (x path-list)
5394 (unless (memq 'lib (cdr x))
5395 (idlwave-path-alist-add-flag x 'user)))
5396 (idlwave-display-user-catalog-widget path-list)))
5397 "Select All Non-Lib")
5398 (widget-insert " ")
5399 (widget-create 'push-button
5400 :notify
5401 (lambda (&rest ignore)
5402 (let ((path-list (widget-get idlwave-widget :path-dirs)))
5403 (dolist (x path-list)
5404 (idlwave-path-alist-remove-flag x 'user))
5405 (idlwave-display-user-catalog-widget path-list)))
5406 "Deselect All")
5407 (widget-insert " ")
5408 (widget-create 'push-button
5409 :notify (lambda (&rest ignore)
5410 (kill-buffer (current-buffer)))
5411 "Quit")
5412 (widget-insert "\n\n")
5413
5414 (widget-insert "Select Directories: \n")
5415
5416 (setq idlwave-widget
5417 (apply 'widget-create
5418 'checklist
5419 :value (delq nil (mapcar (lambda (x)
5420 (if (memq 'user (cdr x))
5421 (car x)))
5422 dirs-list))
5423 :greedy t
5424 :tag "List of directories"
5425 (mapcar (lambda (x)
5426 (list 'item
5427 (if (memq 'lib (cdr x))
5428 (concat "[LIB] " (car x) )
5429 (car x)))) dirs-list)))
5430 (widget-put idlwave-widget :path-dirs dirs-list)
5431 (widget-insert "\n")
5432 (use-local-map widget-keymap)
5433 (widget-setup)
5434 (goto-char (point-min))
5435 (delete-other-windows))
5436
5437 (defun idlwave-delete-user-catalog-file (&rest ignore)
5438 (if (yes-or-no-p
5439 (format "Delete file %s " idlwave-user-catalog-file))
5440 (progn
5441 (delete-file idlwave-user-catalog-file)
5442 (message "%s has been deleted" idlwave-user-catalog-file))))
5443
5444 (defun idlwave-widget-scan-user-lib-files (&rest ignore)
5445 ;; Call `idlwave-scan-user-lib-files' with data taken from the widget.
5446 (let* ((widget idlwave-widget)
5447 (selected-dirs (widget-value widget))
5448 (path-alist (widget-get widget :path-dirs))
5449 (this-path-alist path-alist)
5450 dir-entry)
5451 (while (setq dir-entry (pop this-path-alist))
5452 (if (member
5453 (if (memq 'lib (cdr dir-entry))
5454 (concat "[LIB] " (car dir-entry))
5455 (car dir-entry))
5456 selected-dirs)
5457 (idlwave-path-alist-add-flag dir-entry 'user)
5458 (idlwave-path-alist-remove-flag dir-entry 'user)))
5459 (idlwave-scan-user-lib-files path-alist)))
5460
5461 (defvar font-lock-mode)
5462 (defun idlwave-scan-user-lib-files (path-alist)
5463 ;; Scan the PRO files in PATH-ALIST and store the info in the user catalog
5464 (let* ((idlwave-scanning-lib t)
5465 (idlwave-scanning-lib-dir "")
5466 (idlwave-completion-case nil)
5467 dirs-alist dir files file)
5468 (setq idlwave-user-catalog-routines nil
5469 idlwave-path-alist path-alist ; for library-path instead
5470 idlwave-true-path-alist nil)
5471 (if idlwave-auto-write-paths (idlwave-write-paths))
5472 (with-current-buffer (get-buffer-create "*idlwave-scan.pro*")
5473 (idlwave-mode)
5474 (setq dirs-alist (reverse path-alist))
5475 (while (setq dir (pop dirs-alist))
5476 (when (memq 'user (cdr dir)) ; Has it marked for scan?
5477 (setq dir (car dir))
5478 (setq idlwave-scanning-lib-dir dir)
5479 (when (file-directory-p dir)
5480 (setq files (directory-files dir 'full "\\.[pP][rR][oO]\\'"))
5481 (while (setq file (pop files))
5482 (when (file-regular-p file)
5483 (if (not (file-readable-p file))
5484 (message "Skipping %s (no read permission)" file)
5485 (message "Scanning %s..." file)
5486 (erase-buffer)
5487 (insert-file-contents file 'visit)
5488 (setq idlwave-user-catalog-routines
5489 (append (idlwave-get-routine-info-from-buffers
5490 (list (current-buffer)))
5491 idlwave-user-catalog-routines)))))))))
5492 (message "Creating user catalog file...")
5493 (kill-buffer "*idlwave-scan.pro*")
5494 (kill-buffer (get-buffer-create "*IDLWAVE Widget*"))
5495 (let ((font-lock-maximum-size 0)
5496 (auto-mode-alist nil))
5497 (find-file idlwave-user-catalog-file))
5498 (if (and (boundp 'font-lock-mode)
5499 font-lock-mode)
5500 (font-lock-mode 0))
5501 (erase-buffer)
5502 (insert ";; IDLWAVE user catalog file\n")
5503 (insert (format ";; Created %s\n\n" (current-time-string)))
5504
5505 ;; Define the routine info list
5506 (insert "\n(setq idlwave-user-catalog-routines\n '(")
5507 (let ((standard-output (current-buffer)))
5508 (mapc (lambda (x)
5509 (insert "\n ")
5510 (prin1 x)
5511 (goto-char (point-max)))
5512 idlwave-user-catalog-routines))
5513 (insert (format "))\n\n;;; %s ends here\n"
5514 (file-name-nondirectory idlwave-user-catalog-file)))
5515 (goto-char (point-min))
5516 ;; Save the buffer
5517 (save-buffer 0)
5518 (kill-buffer (current-buffer)))
5519 (message "Creating user catalog file...done")
5520 (message "Info for %d routines saved in %s"
5521 (length idlwave-user-catalog-routines)
5522 idlwave-user-catalog-file)
5523 (sit-for 2)
5524 (idlwave-update-routine-info t))
5525
5526 (defun idlwave-read-paths ()
5527 (if (and (stringp idlwave-path-file)
5528 (file-regular-p idlwave-path-file))
5529 (condition-case nil
5530 (load idlwave-path-file t t t)
5531 (error nil))))
5532
5533 (defun idlwave-write-paths ()
5534 (interactive)
5535 (when (and idlwave-path-alist idlwave-system-directory)
5536 (let ((font-lock-maximum-size 0)
5537 (auto-mode-alist nil))
5538 (find-file idlwave-path-file))
5539 (if (and (boundp 'font-lock-mode)
5540 font-lock-mode)
5541 (font-lock-mode 0))
5542 (erase-buffer)
5543 (insert ";; IDLWAVE paths\n")
5544 (insert (format ";; Created %s\n\n" (current-time-string)))
5545 ;; Define the variable which knows the value of "!DIR"
5546 (insert (format "\n(setq idlwave-system-directory \"%s\")\n"
5547 idlwave-system-directory))
5548
5549 ;; Define the variable which contains a list of all scanned directories
5550 (insert "\n(setq idlwave-path-alist\n '(")
5551 (let ((standard-output (current-buffer)))
5552 (mapc (lambda (x)
5553 (insert "\n ")
5554 (prin1 x)
5555 (goto-char (point-max)))
5556 idlwave-path-alist))
5557 (insert "))\n")
5558 (save-buffer 0)
5559 (kill-buffer (current-buffer))))
5560
5561
5562 (defun idlwave-expand-path (path &optional default-dir)
5563 ;; Expand parts of path starting with '+' recursively into directory list.
5564 ;; Relative recursive path elements are expanded relative to DEFAULT-DIR.
5565 (message "Expanding path...")
5566 (let (path1 dir recursive)
5567 (while (setq dir (pop path))
5568 (if (setq recursive (string= (substring dir 0 1) "+"))
5569 (setq dir (substring dir 1)))
5570 (if (and recursive
5571 (not (file-name-absolute-p dir)))
5572 (setq dir (expand-file-name dir default-dir)))
5573 (if recursive
5574 ;; Expand recursively
5575 (setq path1 (append (idlwave-recursive-directory-list dir) path1))
5576 ;; Keep unchanged
5577 (push dir path1)))
5578 (message "Expanding path...done")
5579 (nreverse path1)))
5580
5581 (defun idlwave-recursive-directory-list (dir)
5582 ;; Return a list of all directories below DIR, including DIR itself
5583 (let ((path (list dir)) path1 file files)
5584 (while (setq dir (pop path))
5585 (when (file-directory-p dir)
5586 (setq files (nreverse (directory-files dir t "[^.]")))
5587 (while (setq file (pop files))
5588 (if (file-directory-p file)
5589 (push (file-name-as-directory file) path)))
5590 (push dir path1)))
5591 path1))
5592
5593
5594 ;;----- Scanning the library catalogs ------------------
5595
5596
5597
5598
5599 (defun idlwave-scan-library-catalogs (&optional message-base no-load)
5600 "Scan for library catalog files (.idlwave_catalog) and ingest.
5601
5602 All directories on `idlwave-path-alist' (or `idlwave-library-path'
5603 instead, if present) are searched. Print MESSAGE-BASE along with the
5604 libraries being loaded, if passed, and skip loading/normalizing if
5605 NO-LOAD is non-nil. The variable `idlwave-use-library-catalogs' can
5606 be set to nil to disable library catalog scanning."
5607 (when idlwave-use-library-catalogs
5608 (let ((dirs
5609 (if idlwave-library-path
5610 (idlwave-expand-path idlwave-library-path)
5611 (mapcar 'car idlwave-path-alist)))
5612 (old-libname "")
5613 dir-entry dir catalog all-routines)
5614 (if message-base (message message-base))
5615 (while (setq dir (pop dirs))
5616 (catch 'continue
5617 (when (file-readable-p
5618 (setq catalog (expand-file-name ".idlwave_catalog" dir)))
5619 (unless no-load
5620 (setq idlwave-library-catalog-routines nil)
5621 ;; Load the catalog file
5622 (condition-case nil
5623 (load catalog t t t)
5624 (error (throw 'continue t)))
5625 (when (and
5626 message-base
5627 (not (string= idlwave-library-catalog-libname
5628 old-libname)))
5629 (message "%s" (concat message-base
5630 idlwave-library-catalog-libname))
5631 (setq old-libname idlwave-library-catalog-libname))
5632 (when idlwave-library-catalog-routines
5633 (setq all-routines
5634 (append
5635 (idlwave-sintern-rinfo-list
5636 idlwave-library-catalog-routines 'sys dir)
5637 all-routines))))
5638
5639 ;; Add a 'lib flag if on path-alist
5640 (when (and idlwave-path-alist
5641 (setq dir-entry (assoc dir idlwave-path-alist)))
5642 (idlwave-path-alist-add-flag dir-entry 'lib)))))
5643 (unless no-load (setq idlwave-library-catalog-routines all-routines))
5644 (if message-base (message (concat message-base "done"))))))
5645
5646 ;;----- Communicating with the Shell -------------------
5647
5648 ;; First, here is the idl program which can be used to query IDL for
5649 ;; defined routines.
5650 (defconst idlwave-routine-info.pro
5651 "
5652 ;; START OF IDLWAVE SUPPORT ROUTINES
5653 pro idlwave_print_safe,item,limit
5654 catch,err
5655 if err ne 0 then begin
5656 print,'Could not print item.'
5657 return
5658 endif
5659 if n_elements(item) gt limit then $
5660 print,item[0:limit-1],'<... truncated at ',strtrim(limit,2),' elements>' $
5661 else print,item
5662 end
5663
5664 pro idlwave_print_info_entry,name,func=func,separator=sep
5665 ;; See if it's an object method
5666 if name eq '' then return
5667 func = keyword_set(func)
5668 methsep = strpos(name,'::')
5669 meth = methsep ne -1
5670
5671 ;; Get routine info
5672 pars = routine_info(name,/parameters,functions=func)
5673 source = routine_info(name,/source,functions=func)
5674 nargs = pars.num_args
5675 nkw = pars.num_kw_args
5676 if nargs gt 0 then args = pars.args
5677 if nkw gt 0 then kwargs = pars.kw_args
5678
5679 ;; Trim the class, and make the name
5680 if meth then begin
5681 class = strmid(name,0,methsep)
5682 name = strmid(name,methsep+2,strlen(name)-1)
5683 if nargs gt 0 then begin
5684 ;; remove the self argument
5685 wh = where(args ne 'SELF',nargs)
5686 if nargs gt 0 then args = args[wh]
5687 endif
5688 endif else begin
5689 ;; No class, just a normal routine.
5690 class = \"\"
5691 endelse
5692
5693 ;; Calling sequence
5694 cs = \"\"
5695 if func then cs = 'Result = '
5696 if meth then cs = cs + 'Obj -> [' + '%s' + '::]'
5697 cs = cs + '%s'
5698 if func then cs = cs + '(' else if nargs gt 0 then cs = cs + ', '
5699 if nargs gt 0 then begin
5700 for j=0,nargs-1 do begin
5701 cs = cs + args[j]
5702 if j lt nargs-1 then cs = cs + ', '
5703 endfor
5704 end
5705 if func then cs = cs + ')'
5706 ;; Keyword arguments
5707 kwstring = ''
5708 if nkw gt 0 then begin
5709 for j=0,nkw-1 do begin
5710 kwstring = kwstring + ' ' + kwargs[j]
5711 endfor
5712 endif
5713
5714 ret=(['IDLWAVE-PRO','IDLWAVE-FUN'])[func]
5715
5716 print,ret + ': ' + name + sep + class + sep + source[0].path $
5717 + sep + cs + sep + kwstring
5718 end
5719
5720 pro idlwave_routine_info,file
5721 on_error,1
5722 sep = '<@>'
5723 print,'>>>BEGIN OF IDLWAVE ROUTINE INFO (\"' + sep + '\" IS THE SEPARATOR)'
5724 all = routine_info()
5725 fileQ=n_elements(file) ne 0
5726 if fileQ then file=strtrim(file,2)
5727 for i=0L,n_elements(all)-1L do begin
5728 if fileQ then begin
5729 if (routine_info(all[i],/SOURCE)).path eq file then $
5730 idlwave_print_info_entry,all[i],separator=sep
5731 endif else idlwave_print_info_entry,all[i],separator=sep
5732 endfor
5733 all = routine_info(/functions)
5734 for i=0L,n_elements(all)-1L do begin
5735 if fileQ then begin
5736 if (routine_info(all[i],/FUNCTIONS,/SOURCE)).path eq file then $
5737 idlwave_print_info_entry,all[i],separator=sep,/FUNC
5738 endif else idlwave_print_info_entry,all[i],separator=sep,/FUNC
5739 endfor
5740 print,'>>>END OF IDLWAVE ROUTINE INFO'
5741 end
5742
5743 pro idlwave_get_sysvars
5744 on_error,1
5745 catch,error_status
5746 if error_status ne 0 then begin
5747 print, 'Cannot get info about system variables'
5748 endif else begin
5749 help,/brief,output=s,/system_variables ; ? unsafe use of OUTPUT=
5750 s = strtrim(strjoin(s,' ',/single),2) ; make one line
5751 v = strsplit(s,' +',/regex,/extract) ; get variables
5752 for i=0L,n_elements(v)-1 do begin
5753 t = [''] ; get tag list
5754 a=execute('if n_tags('+v[i]+') gt 0 then t=tag_names('+v[i]+')')
5755 print, 'IDLWAVE-SYSVAR: '+v[i]+' '+strjoin(t,' ',/single)
5756 endfor
5757 endelse
5758 end
5759
5760 pro idlwave_get_class_tags, class
5761 res = execute('tags=tag_names({'+class+'})')
5762 if res then print,'IDLWAVE-CLASS-TAGS: '+class+' '+strjoin(tags,' ',/single)
5763 end
5764 ;; END OF IDLWAVE SUPPORT ROUTINES
5765 "
5766 "The IDL programs to get info from the shell.")
5767
5768 (defvar idlwave-idlwave_routine_info-compiled nil
5769 "Remember if the routine info procedure is already compiled.")
5770
5771 (defvar idlwave-shell-temp-pro-file)
5772 (defvar idlwave-shell-temp-rinfo-save-file)
5773
5774 (defun idlwave-shell-compile-helper-routines (&optional wait)
5775 (unless (and idlwave-idlwave_routine_info-compiled
5776 (file-readable-p (idlwave-shell-temp-file 'rinfo)))
5777 (with-current-buffer (idlwave-find-file-noselect
5778 (idlwave-shell-temp-file 'pro))
5779 (erase-buffer)
5780 (insert idlwave-routine-info.pro)
5781 (save-buffer 0))
5782 (idlwave-shell-send-command
5783 (concat ".run \"" idlwave-shell-temp-pro-file "\"")
5784 nil 'hide wait)
5785 (idlwave-shell-send-command
5786 (format "save,'idlwave_print_safe','idlwave_routine_info','idlwave_print_info_entry','idlwave_get_class_tags','idlwave_get_sysvars',FILE='%s',/ROUTINES"
5787 (idlwave-shell-temp-file 'rinfo))
5788 nil 'hide)
5789 (setq idlwave-idlwave_routine_info-compiled t))
5790
5791 ;; Restore if necessary. Must use execute to hide lame routine_info
5792 ;; errors on undefined routine
5793 (idlwave-shell-send-command
5794 (format "if execute(\"_v=routine_info('idlwave_routine_info',/SOURCE)\") eq 0 then restore,'%s' else if _v.path eq '' then restore,'%s'"
5795 idlwave-shell-temp-rinfo-save-file
5796 idlwave-shell-temp-rinfo-save-file)
5797 nil 'hide))
5798
5799
5800 (defun idlwave-shell-update-routine-info (&optional quiet run-hooks wait file)
5801 "Query the shell for routine_info of compiled modules and update the lists."
5802 ;; Save and compile the procedure. The compiled procedure is then
5803 ;; saved into an IDL SAVE file, to allow for fast RESTORE. We may
5804 ;; need to test for and possibly RESTORE the procedure each time we
5805 ;; use it, since the user may have killed or redefined it. In
5806 ;; particular, .RESET_SESSION will kill all user procedures. If
5807 ;; FILE is set, only update routine info for routines in that file.
5808
5809 (idlwave-shell-compile-helper-routines wait)
5810 ; execute the routine_info procedure, and analyze the output
5811 (idlwave-shell-send-command
5812 (format "idlwave_routine_info%s" (if file (concat ",'" file "'") ""))
5813 `(progn
5814 (idlwave-shell-routine-info-filter)
5815 (idlwave-concatenate-rinfo-lists ,quiet ,run-hooks))
5816 'hide wait))
5817
5818 ;; ---------------------------------------------------------------------------
5819 ;;
5820 ;; Completion and displaying routine calling sequences
5821
5822 (defvar idlwave-completion-help-info nil)
5823 (defvar idlwave-completion-help-links nil)
5824 (defvar idlwave-current-obj_new-class nil)
5825 (defvar idlwave-complete-special nil)
5826 (defvar method-selector)
5827 (defvar class-selector)
5828 (defvar type-selector)
5829 (defvar super-classes)
5830
5831 (defun idlwave-complete (&optional arg module class)
5832 "Complete a function, procedure or keyword name at point.
5833 This function is smart and figures out what can be completed
5834 at this point.
5835 - At the beginning of a statement it completes procedure names.
5836 - In the middle of a statement it completes function names.
5837 - After a `(' or `,' in the argument list of a function or procedure,
5838 it completes a keyword of the relevant function or procedure.
5839 - In the first arg of `OBJ_NEW', it completes a class name.
5840
5841 When several completions are possible, a list will be displayed in
5842 the *Completions* buffer. If this list is too long to fit into the
5843 window, scrolling can be achieved by repeatedly pressing
5844 \\[idlwave-complete].
5845
5846 The function also knows about object methods. When it needs a class
5847 name, the action depends upon `idlwave-query-class', which see. You
5848 can force IDLWAVE to ask you for a class name with a
5849 \\[universal-argument] prefix argument to this command.
5850
5851 See also the variables `idlwave-keyword-completion-adds-equal' and
5852 `idlwave-function-completion-adds-paren'.
5853
5854 The optional ARG can be used to specify the completion type in order
5855 to override IDLWAVE's idea of what should be completed at point.
5856 Possible values are:
5857
5858 0 <=> query for the completion type
5859 1 <=> 'procedure
5860 2 <=> 'procedure-keyword
5861 3 <=> 'function
5862 4 <=> 'function-keyword
5863 5 <=> 'procedure-method
5864 6 <=> 'procedure-method-keyword
5865 7 <=> 'function-method
5866 8 <=> 'function-method-keyword
5867 9 <=> 'class
5868
5869 As a special case, the universal argument C-u forces completion of
5870 function names in places where the default would be a keyword.
5871
5872 Two prefix argument, C-u C-u, prompts for a regexp by which to limit
5873 completion.
5874
5875 For Lisp programmers only:
5876 When we force a keyword, optional argument MODULE can contain the module name.
5877 When we force a method or a method keyword, CLASS can specify the class."
5878 (interactive "P")
5879 (idlwave-routines)
5880 (let* ((where-list
5881 (if (and arg
5882 (or (and (integerp arg) (not (equal arg '(16))))
5883 (symbolp arg)))
5884 (idlwave-make-force-complete-where-list arg module class)
5885 (idlwave-where)))
5886 (what (nth 2 where-list))
5887 (idlwave-force-class-query (equal arg '(4)))
5888 (completion-regexp-list
5889 (if (equal arg '(16))
5890 (list (read-string (concat "Completion Regexp: "))))))
5891
5892 (if (and module (string-match "::" module))
5893 (setq class (substring module 0 (match-beginning 0))
5894 module (substring module (match-end 0))))
5895
5896 (cond
5897
5898 ((and (null arg)
5899 (eq (car-safe last-command) 'idlwave-display-completion-list)
5900 (get-buffer-window "*Completions*"))
5901 (setq this-command last-command)
5902 (idlwave-scroll-completions))
5903
5904 ;; Complete a filename in quotes
5905 ((and (idlwave-in-quote)
5906 (not (eq what 'class)))
5907 (idlwave-complete-filename))
5908
5909 ;; Check for any special completion functions
5910 ((and idlwave-complete-special
5911 (idlwave-call-special idlwave-complete-special)))
5912
5913 ((null what)
5914 (error "Nothing to complete here"))
5915
5916 ;; Complete a class
5917 ((eq what 'class)
5918 (setq idlwave-completion-help-info '(class))
5919 (idlwave-complete-class))
5920
5921 ((eq what 'procedure)
5922 ;; Complete a procedure name
5923 (let* ((cw-list (nth 3 where-list))
5924 (class-selector (idlwave-determine-class cw-list 'pro))
5925 (super-classes (unless (idlwave-explicit-class-listed cw-list)
5926 (idlwave-all-class-inherits class-selector)))
5927 (isa (concat "procedure" (if class-selector "-method" "")))
5928 (type-selector 'pro))
5929 (setq idlwave-completion-help-info
5930 (list 'routine nil type-selector class-selector nil super-classes))
5931 (idlwave-complete-in-buffer
5932 'procedure (if class-selector 'method 'routine)
5933 (idlwave-routines) 'idlwave-selector
5934 (format "Select a %s name%s"
5935 isa
5936 (if class-selector
5937 (format " (class is %s)"
5938 (if (eq class-selector t)
5939 "unknown" class-selector))
5940 ""))
5941 isa
5942 'idlwave-attach-method-classes 'idlwave-add-file-link-selector)))
5943
5944 ((eq what 'function)
5945 ;; Complete a function name
5946 (let* ((cw-list (nth 3 where-list))
5947 (class-selector (idlwave-determine-class cw-list 'fun))
5948 (super-classes (unless (idlwave-explicit-class-listed cw-list)
5949 (idlwave-all-class-inherits class-selector)))
5950 (isa (concat "function" (if class-selector "-method" "")))
5951 (type-selector 'fun))
5952 (setq idlwave-completion-help-info
5953 (list 'routine nil type-selector class-selector nil super-classes))
5954 (idlwave-complete-in-buffer
5955 'function (if class-selector 'method 'routine)
5956 (idlwave-routines) 'idlwave-selector
5957 (format "Select a %s name%s"
5958 isa
5959 (if class-selector
5960 (format " (class is %s)"
5961 (if (eq class-selector t)
5962 "unknown" class-selector))
5963 ""))
5964 isa
5965 'idlwave-attach-method-classes 'idlwave-add-file-link-selector)))
5966
5967 ((and (memq what '(procedure-keyword function-keyword)) ; Special Case
5968 (equal arg '(4)))
5969 (idlwave-complete 3))
5970
5971 ((eq what 'procedure-keyword)
5972 ;; Complete a procedure keyword
5973 (let* ((where (nth 3 where-list))
5974 (name (car where))
5975 (method-selector name)
5976 (type-selector 'pro)
5977 (class (idlwave-determine-class where 'pro))
5978 (class-selector class)
5979 (super-classes (idlwave-all-class-inherits class-selector))
5980 (isa (format "procedure%s-keyword" (if class "-method" "")))
5981 (entry (idlwave-best-rinfo-assq
5982 name 'pro class (idlwave-routines)))
5983 (system (if entry (eq (car (nth 3 entry)) 'system)))
5984 (list (idlwave-entry-keywords entry 'do-link)))
5985 (unless (or entry (eq class t))
5986 (error "Nothing known about procedure %s"
5987 (idlwave-make-full-name class name)))
5988 (setq list (idlwave-fix-keywords name 'pro class list
5989 super-classes system))
5990 (unless list (error "No keywords available for procedure %s"
5991 (idlwave-make-full-name class name)))
5992 (setq idlwave-completion-help-info
5993 (list 'keyword name type-selector class-selector entry super-classes))
5994 (idlwave-complete-in-buffer
5995 'keyword 'keyword list nil
5996 (format "Select keyword for procedure %s%s"
5997 (idlwave-make-full-name class name)
5998 (if (or (member '("_EXTRA") list)
5999 (member '("_REF_EXTRA") list))
6000 " (note _EXTRA)" ""))
6001 isa
6002 'idlwave-attach-keyword-classes)))
6003
6004 ((eq what 'function-keyword)
6005 ;; Complete a function keyword
6006 (let* ((where (nth 3 where-list))
6007 (name (car where))
6008 (method-selector name)
6009 (type-selector 'fun)
6010 (class (idlwave-determine-class where 'fun))
6011 (class-selector class)
6012 (super-classes (idlwave-all-class-inherits class-selector))
6013 (isa (format "function%s-keyword" (if class "-method" "")))
6014 (entry (idlwave-best-rinfo-assq
6015 name 'fun class (idlwave-routines)))
6016 (system (if entry (eq (car (nth 3 entry)) 'system)))
6017 (list (idlwave-entry-keywords entry 'do-link))
6018 msg-name)
6019 (unless (or entry (eq class t))
6020 (error "Nothing known about function %s"
6021 (idlwave-make-full-name class name)))
6022 (setq list (idlwave-fix-keywords name 'fun class list
6023 super-classes system))
6024 ;; OBJ_NEW: Messages mention the proper Init method
6025 (setq msg-name (if (and (null class)
6026 (string= (upcase name) "OBJ_NEW"))
6027 (concat idlwave-current-obj_new-class
6028 "::Init (via OBJ_NEW)")
6029 (idlwave-make-full-name class name)))
6030 (unless list (error "No keywords available for function %s"
6031 msg-name))
6032 (setq idlwave-completion-help-info
6033 (list 'keyword name type-selector class-selector nil super-classes))
6034 (idlwave-complete-in-buffer
6035 'keyword 'keyword list nil
6036 (format "Select keyword for function %s%s" msg-name
6037 (if (or (member '("_EXTRA") list)
6038 (member '("_REF_EXTRA") list))
6039 " (note _EXTRA)" ""))
6040 isa
6041 'idlwave-attach-keyword-classes)))
6042
6043 (t (error "This should not happen (idlwave-complete)")))))
6044
6045 (defvar idlwave-complete-special nil
6046 "List of special completion functions.
6047 These functions are called for each completion. Each function must
6048 check if its own special completion context is present. If yes, it
6049 should use `idlwave-complete-in-buffer' to do some completion and
6050 return t. If such a function returns t, *no further* attempts to
6051 complete other contexts will be done. If the function returns nil,
6052 other completions will be tried.")
6053
6054 (defun idlwave-call-special (functions &rest args)
6055 (let ((funcs functions)
6056 fun ret)
6057 (catch 'exit
6058 (while (setq fun (pop funcs))
6059 (if (setq ret (apply fun args))
6060 (throw 'exit ret)))
6061 nil)))
6062
6063 (defun idlwave-make-force-complete-where-list (what &optional module class)
6064 ;; Return an artificial WHERE specification to force the completion
6065 ;; routine to complete a specific item independent of context.
6066 ;; WHAT is the prefix arg of `idlwave-complete', see there for details.
6067 ;; MODULE and CLASS can be used to specify the routine name and class.
6068 ;; The class name will also be found in MODULE if that is like "class::mod".
6069 (let* ((what-list '(("procedure") ("procedure-keyword")
6070 ("function") ("function-keyword")
6071 ("procedure-method") ("procedure-method-keyword")
6072 ("function-method") ("function-method-keyword")
6073 ("class")))
6074 (module (idlwave-sintern-routine-or-method module class))
6075 (class (idlwave-sintern-class class))
6076 (what (cond
6077 ((equal what 0)
6078 (setq what
6079 (intern (completing-read
6080 "Complete what? " what-list nil t))))
6081 ((integerp what)
6082 (setq what (intern (car (nth (1- what) what-list)))))
6083 ((and what
6084 (symbolp what)
6085 (assoc (symbol-name what) what-list))
6086 what)
6087 (t (error "Invalid WHAT"))))
6088 (nil-list '(nil nil nil nil))
6089 (class-list (list nil nil (or class t) nil)))
6090
6091 (cond
6092
6093 ((eq what 'procedure)
6094 (list nil-list nil-list 'procedure nil-list nil))
6095
6096 ((eq what 'procedure-keyword)
6097 (let* ((class-selector nil)
6098 (super-classes nil)
6099 (type-selector 'pro)
6100 (pro (or module
6101 (idlwave-completing-read
6102 "Procedure: " (idlwave-routines) 'idlwave-selector))))
6103 (setq pro (idlwave-sintern-routine pro))
6104 (list nil-list nil-list 'procedure-keyword
6105 (list pro nil nil nil) nil)))
6106
6107 ((eq what 'function)
6108 (list nil-list nil-list 'function nil-list nil))
6109
6110 ((eq what 'function-keyword)
6111 (let* ((class-selector nil)
6112 (super-classes nil)
6113 (type-selector 'fun)
6114 (func (or module
6115 (idlwave-completing-read
6116 "Function: " (idlwave-routines) 'idlwave-selector))))
6117 (setq func (idlwave-sintern-routine func))
6118 (list nil-list nil-list 'function-keyword
6119 (list func nil nil nil) nil)))
6120
6121 ((eq what 'procedure-method)
6122 (list nil-list nil-list 'procedure class-list nil))
6123
6124 ((eq what 'procedure-method-keyword)
6125 (let* ((class (idlwave-determine-class class-list 'pro))
6126 (class-selector class)
6127 (super-classes (idlwave-all-class-inherits class-selector))
6128 (type-selector 'pro)
6129 (pro (or module
6130 (idlwave-completing-read
6131 (format "Procedure in %s class: " class-selector)
6132 (idlwave-routines) 'idlwave-selector))))
6133 (setq pro (idlwave-sintern-method pro))
6134 (list nil-list nil-list 'procedure-keyword
6135 (list pro nil class nil) nil)))
6136
6137 ((eq what 'function-method)
6138 (list nil-list nil-list 'function class-list nil))
6139
6140 ((eq what 'function-method-keyword)
6141 (let* ((class (idlwave-determine-class class-list 'fun))
6142 (class-selector class)
6143 (super-classes (idlwave-all-class-inherits class-selector))
6144 (type-selector 'fun)
6145 (func (or module
6146 (idlwave-completing-read
6147 (format "Function in %s class: " class-selector)
6148 (idlwave-routines) 'idlwave-selector))))
6149 (setq func (idlwave-sintern-method func))
6150 (list nil-list nil-list 'function-keyword
6151 (list func nil class nil) nil)))
6152
6153 ((eq what 'class)
6154 (list nil-list nil-list 'class nil-list nil))
6155
6156 (t (error "Invalid value for WHAT")))))
6157
6158 (defun idlwave-completing-read (&rest args)
6159 ;; Completing read, case insensitive
6160 (let ((old-value (default-value 'completion-ignore-case)))
6161 (unwind-protect
6162 (progn
6163 (setq-default completion-ignore-case t)
6164 (apply 'completing-read args))
6165 (setq-default completion-ignore-case old-value))))
6166
6167 (defvar idlwave-shell-default-directory)
6168 (defun idlwave-complete-filename ()
6169 "Use the comint stuff to complete a file name."
6170 (require 'comint)
6171 (let* ((comint-file-name-chars "~/A-Za-z0-9+@:_.$#%={}\\-")
6172 (comint-completion-addsuffix nil)
6173 (default-directory
6174 (if (and (boundp 'idlwave-shell-default-directory)
6175 (stringp idlwave-shell-default-directory)
6176 (file-directory-p idlwave-shell-default-directory))
6177 idlwave-shell-default-directory
6178 default-directory)))
6179 (comint-dynamic-complete-filename)))
6180
6181 (defun idlwave-make-full-name (class name)
6182 ;; Make a fully qualified module name including the class name
6183 (concat (if class (format "%s::" class) "") name))
6184
6185 (defun idlwave-rinfo-assoc (name type class list)
6186 "Like `idlwave-rinfo-assq', but sintern strings first."
6187 (idlwave-rinfo-assq
6188 (idlwave-sintern-routine-or-method name class)
6189 type (idlwave-sintern-class class) list))
6190
6191 (defun idlwave-rinfo-assq (name type class list)
6192 ;; Works like assq, but also checks type and class
6193 (catch 'exit
6194 (let (match)
6195 (while (setq match (assq name list))
6196 (and (or (eq type t)
6197 (eq (nth 1 match) type))
6198 (eq (nth 2 match) class)
6199 (throw 'exit match))
6200 (setq list (cdr (memq match list)))))))
6201
6202 (defun idlwave-rinfo-assq-any-class (name type class list)
6203 ;; Return the first matching method on the inheritance list
6204 (let* ((classes (cons class (idlwave-all-class-inherits class)))
6205 class rtn)
6206 (while classes
6207 (if (setq rtn (idlwave-rinfo-assq name type (pop classes) list))
6208 (setq classes nil)))
6209 rtn))
6210
6211 (defun idlwave-best-rinfo-assq (name type class list &optional with-file
6212 keep-system)
6213 "Like `idlwave-rinfo-assq', but get all twins and sort, then return first.
6214 If WITH-FILE is passed, find the best rinfo entry with a file
6215 included. If KEEP-SYSTEM is set, don't prune system for compiled
6216 syslib files."
6217 (let ((twins (idlwave-routine-twins
6218 (idlwave-rinfo-assq-any-class name type class list)
6219 list))
6220 syslibp)
6221 (when (> (length twins) 1)
6222 (setq twins (sort twins 'idlwave-routine-entry-compare-twins))
6223 (if (and (null keep-system)
6224 (eq 'system (car (nth 3 (car twins))))
6225 (setq syslibp (idlwave-any-syslib (cdr twins)))
6226 (not (equal 1 syslibp)))
6227 ;; Its a compiled syslib, so we need to remove the system entry
6228 (setq twins (cdr twins)))
6229 (if with-file
6230 (setq twins (delq nil
6231 (mapcar (lambda (x)
6232 (if (nth 1 (nth 3 x)) x))
6233 twins)))))
6234 (car twins)))
6235
6236 (defun idlwave-best-rinfo-assoc (name type class list &optional with-file
6237 keep-system)
6238 "Like `idlwave-best-rinfo-assq', but sintern strings first."
6239 (idlwave-best-rinfo-assq
6240 (idlwave-sintern-routine-or-method name class)
6241 type (idlwave-sintern-class class) list with-file keep-system))
6242
6243 (defun idlwave-any-syslib (entries)
6244 "Does the entry list ENTRIES contain a syslib entry?
6245 If yes, return the index (>=1)."
6246 (let (file (cnt 0))
6247 (catch 'exit
6248 (while entries
6249 (incf cnt)
6250 (setq file (idlwave-routine-source-file (nth 3 (car entries))))
6251 (if (and file (idlwave-syslib-p file))
6252 (throw 'exit cnt)
6253 (setq entries (cdr entries))))
6254 nil)))
6255
6256 (defun idlwave-all-assq (key list)
6257 "Return a list of all associations of Key in LIST."
6258 (let (rtn elt)
6259 (while (setq elt (assq key list))
6260 (push elt rtn)
6261 (setq list (cdr (memq elt list))))
6262 (nreverse rtn)))
6263
6264 (defun idlwave-all-method-classes (method &optional type)
6265 "Return all classes which have a method METHOD.
6266 TYPE is 'fun or 'pro.
6267 When TYPE is not specified, both procedures and functions will be considered."
6268 (if (null method)
6269 (mapcar 'car (idlwave-class-alist))
6270 (let (rtn)
6271 (mapc (lambda (x)
6272 (and (nth 2 x)
6273 (or (not type)
6274 (eq type (nth 1 x)))
6275 (push (nth 2 x) rtn)))
6276 (idlwave-all-assq method (idlwave-routines)))
6277 (idlwave-uniquify rtn))))
6278
6279 (defun idlwave-all-method-keyword-classes (method keyword &optional type)
6280 "Return all classes which have a method METHOD with keyword KEYWORD.
6281 TYPE is 'fun or 'pro.
6282 When TYPE is not specified, both procedures and functions will be considered."
6283 (if (or (null method)
6284 (null keyword))
6285 nil
6286 (let (rtn)
6287 (mapc (lambda (x)
6288 (and (nth 2 x) ; non-nil class
6289 (or (not type) ; correct or unspecified type
6290 (eq type (nth 1 x)))
6291 (assoc keyword (idlwave-entry-keywords x))
6292 (push (nth 2 x) rtn)))
6293 (idlwave-all-assq method (idlwave-routines)))
6294 (idlwave-uniquify rtn))))
6295
6296 (defun idlwave-members-only (list club)
6297 "Return list of all elements in LIST which are also in CLUB."
6298 (let (rtn)
6299 (while list
6300 (if (member (car list) club)
6301 (setq rtn (cons (car list) rtn)))
6302 (setq list (cdr list)))
6303 (nreverse rtn)))
6304
6305 (defun idlwave-nonmembers-only (list club)
6306 "Return list of all elements in LIST which are not in CLUB."
6307 (let (rtn)
6308 (while list
6309 (if (member (car list) club)
6310 nil
6311 (setq rtn (cons (car list) rtn)))
6312 (setq list (cdr list)))
6313 (nreverse rtn)))
6314
6315 (defun idlwave-explicit-class-listed (info)
6316 "Return whether or not the class is listed explicitly, ala a->b::c.
6317 INFO is as returned by `idlwave-what-function' or `-procedure'."
6318 (let ((apos (nth 3 info)))
6319 (if apos
6320 (save-excursion (goto-char apos)
6321 (looking-at "->[a-zA-Z][a-zA-Z0-9$_]*::")))))
6322
6323 (defvar idlwave-determine-class-special nil
6324 "List of special functions for determining class.
6325 Must accept two arguments: `apos' and `info'.")
6326
6327 (defun idlwave-determine-class (info type)
6328 ;; Determine the class of a routine call.
6329 ;; INFO is the `cw-list' structure as returned by idlwave-where.
6330 ;; The second element in this structure is the class. When nil, we
6331 ;; return nil. When t, try to get the class from text properties at
6332 ;; the arrow. When the object is "self", we use the class of the
6333 ;; current routine. otherwise prompt the user for a class name.
6334 ;; Also stores the selected class as a text property at the arrow.
6335 ;; TYPE is 'fun or 'pro.
6336 (let* ((class (nth 2 info))
6337 (apos (nth 3 info))
6338 (nassoc (assoc (if (stringp (car info))
6339 (upcase (car info))
6340 (car info))
6341 idlwave-query-class))
6342 (dassoc (assq (if (car info) 'keyword-default 'method-default)
6343 idlwave-query-class))
6344 (query (cond (nassoc (cdr nassoc))
6345 (dassoc (cdr dassoc))
6346 (t t)))
6347 (arrow (and apos (string= (buffer-substring apos (+ 2 apos)) "->")))
6348 (is-self
6349 (and arrow
6350 (save-excursion (goto-char apos)
6351 (forward-word -1)
6352 (let ((case-fold-search t))
6353 (looking-at "self\\>")))))
6354 (force-query idlwave-force-class-query)
6355 store special-class class-alist)
6356 (cond
6357 ((null class) nil)
6358 ((eq t class)
6359 ;; There is an object which would like to know its class
6360 (if (and arrow (get-text-property apos 'idlwave-class)
6361 idlwave-store-inquired-class
6362 (not force-query))
6363 (setq class (get-text-property apos 'idlwave-class)
6364 class (idlwave-sintern-class class)))
6365 (if (and (eq t class) is-self)
6366 (setq class (or (nth 2 (idlwave-current-routine)) class)))
6367
6368 ;; Before prompting, try any special class determination routines
6369 (when (and (eq t class)
6370 idlwave-determine-class-special
6371 (not force-query))
6372 (setq special-class
6373 (idlwave-call-special idlwave-determine-class-special apos))
6374 (if special-class
6375 (setq class (idlwave-sintern-class special-class)
6376 store idlwave-store-inquired-class)))
6377
6378 ;; Prompt for a class, if we need to
6379 (when (and (eq class t)
6380 (or force-query query))
6381 (setq class-alist
6382 (mapcar 'list (idlwave-all-method-classes (car info) type)))
6383 (setq class
6384 (idlwave-sintern-class
6385 (cond
6386 ((and (= (length class-alist) 0) (not force-query))
6387 (error "No classes available with method %s" (car info)))
6388 ((and (= (length class-alist) 1) (not force-query))
6389 (car (car class-alist)))
6390 (t
6391 (setq store idlwave-store-inquired-class)
6392 (idlwave-completing-read
6393 (format "Class%s: " (if (stringp (car info))
6394 (format " for %s method %s"
6395 type (car info))
6396 ""))
6397 class-alist nil nil nil 'idlwave-class-history))))))
6398
6399 ;; Store it, if requested
6400 (when (and class (not (eq t class)))
6401 ;; We have a real class here
6402 (when (and store arrow)
6403 (condition-case ()
6404 (add-text-properties
6405 apos (+ apos 2)
6406 `(idlwave-class ,class face ,idlwave-class-arrow-face
6407 rear-nonsticky t))
6408 (error nil)))
6409 (setf (nth 2 info) class))
6410 ;; Return the class
6411 class)
6412 ;; Default as fallback
6413 (t class))))
6414
6415 (defun idlwave-selector (a)
6416 (and (eq (nth 1 a) type-selector)
6417 (or (and (nth 2 a) (eq class-selector t))
6418 (eq (nth 2 a) class-selector)
6419 (memq (nth 2 a) super-classes))))
6420
6421 (defun idlwave-add-file-link-selector (a)
6422 ;; Record a file link, if any, for the tested names during selection.
6423 (let ((sel (idlwave-selector a)) file)
6424 (if (and sel (setq file (idlwave-entry-has-help a)))
6425 (push (cons (car a) file) idlwave-completion-help-links))
6426 sel))
6427
6428
6429 (defun idlwave-where ()
6430 "Find out where we are.
6431 The return value is a list with the following stuff:
6432 \(PRO-LIST FUNC-LIST COMPLETE-WHAT CW-LIST LAST-CHAR)
6433
6434 PRO-LIST (PRO POINT CLASS ARROW)
6435 FUNC-LIST (FUNC POINT CLASS ARROW)
6436 COMPLETE-WHAT a symbol indicating what kind of completion makes sense here
6437 CW-LIST (PRO-OR-FUNC POINT CLASS ARROW) Like PRO-LIST, for what can
6438 be completed here.
6439 LAST-CHAR last relevant character before point (non-white non-comment,
6440 not part of current identifier or leading slash).
6441
6442 In the lists, we have these meanings:
6443 PRO: Procedure name
6444 FUNC: Function name
6445 POINT: Where is this
6446 CLASS: What class has the routine (nil=no, t=is method, but class unknown)
6447 ARROW: Location of the arrow"
6448 (idlwave-routines)
6449 (let* (;(bos (save-excursion (idlwave-beginning-of-statement) (point)))
6450 (bos (save-excursion (idlwave-start-of-substatement 'pre) (point)))
6451 (func-entry (idlwave-what-function bos))
6452 (func (car func-entry))
6453 (func-class (nth 1 func-entry))
6454 (func-arrow (nth 2 func-entry))
6455 (func-point (or (nth 3 func-entry) 0))
6456 (func-level (or (nth 4 func-entry) 0))
6457 (pro-entry (idlwave-what-procedure bos))
6458 (pro (car pro-entry))
6459 (pro-class (nth 1 pro-entry))
6460 (pro-arrow (nth 2 pro-entry))
6461 (pro-point (or (nth 3 pro-entry) 0))
6462 (last-char (idlwave-last-valid-char))
6463 (case-fold-search t)
6464 (match-string (buffer-substring bos (point)))
6465 cw cw-mod cw-arrow cw-class cw-point)
6466 (if (< func-point pro-point) (setq func nil))
6467 (cond
6468 ((string-match "\\`[ \t]*\\(pro\\|function\\)[ \t]+[a-zA-Z0-9_]*\\'"
6469 match-string)
6470 (setq cw 'class))
6471 ((string-match
6472 "\\`[ \t]*\\([a-zA-Z][a-zA-Z0-9$_]*\\)?\\'"
6473 (if (> pro-point 0)
6474 (buffer-substring pro-point (point))
6475 match-string))
6476 (setq cw 'procedure cw-class pro-class cw-point pro-point
6477 cw-arrow pro-arrow))
6478 ((string-match "\\`[ \t]*\\(pro\\|function\\)\\>"
6479 match-string)
6480 nil)
6481 ((string-match "OBJ_NEW([ \t]*['\"]\\([a-zA-Z0-9$_]*\\)?\\'"
6482 match-string)
6483 (setq cw 'class))
6484 ((string-match "\\<inherits\\s-+\\([a-zA-Z0-9$_]*\\)?\\'"
6485 match-string)
6486 (setq cw 'class))
6487 ((and func
6488 (> func-point pro-point)
6489 (= func-level 1)
6490 (memq last-char '(?\( ?,)))
6491 (setq cw 'function-keyword cw-mod func cw-point func-point
6492 cw-class func-class cw-arrow func-arrow))
6493 ((and pro (eq last-char ?,))
6494 (setq cw 'procedure-keyword cw-mod pro cw-point pro-point
6495 cw-class pro-class cw-arrow pro-arrow))
6496 ; ((member last-char '(?\' ?\) ?\] ?!))
6497 ; ;; after these chars, a function makes no sense
6498 ; ;; FIXME: I am sure there can be more in this list
6499 ; ;; FIXME: Do we want to do this at all?
6500 ; nil)
6501 ;; Everywhere else we try a function.
6502 (t
6503 (setq cw 'function)
6504 (save-excursion
6505 (if (re-search-backward "->[ \t]*\\(\\$[ \t]*\\(;.*\\)?\n\\s-*\\)?\\(\\([$a-zA-Z0-9_]+\\)::\\)?[$a-zA-Z0-9_]*\\=" bos t)
6506 (setq cw-arrow (copy-marker (match-beginning 0))
6507 cw-class (if (match-end 4)
6508 (idlwave-sintern-class (match-string 4))
6509 t))))))
6510 (list (list pro pro-point pro-class pro-arrow)
6511 (list func func-point func-class func-arrow)
6512 cw
6513 (list cw-mod cw-point cw-class cw-arrow)
6514 last-char)))
6515
6516 (defun idlwave-this-word (&optional class)
6517 ;; Grab the word around point. CLASS is for the `skip-chars=...' functions
6518 (setq class (or class "a-zA-Z0-9$_."))
6519 (save-excursion
6520 (buffer-substring
6521 (progn (skip-chars-backward class) (point))
6522 (progn (skip-chars-forward class) (point)))))
6523
6524 (defun idlwave-what-function (&optional bound)
6525 ;; Find out if point is within the argument list of a function.
6526 ;; The return value is ("function-name" class arrow-start (point) level).
6527 ;; Level is 1 on the top level parentheses, higher further down.
6528
6529 ;; If the optional BOUND is an integer, bound backwards directed
6530 ;; searches to this point.
6531
6532 (catch 'exit
6533 (let (pos
6534 func-point
6535 (cnt 0)
6536 func arrow-start class)
6537 (idlwave-with-special-syntax
6538 (save-restriction
6539 (save-excursion
6540 (narrow-to-region (max 1 (or bound 0)) (point-max))
6541 ;; move back out of the current parenthesis
6542 (while (condition-case nil
6543 (progn (up-list -1) t)
6544 (error nil))
6545 (setq pos (point))
6546 (incf cnt)
6547 (when (and (= (following-char) ?\()
6548 (re-search-backward
6549 "\\(::\\|\\<\\)\\([a-zA-Z][a-zA-Z0-9$_]*\\)[ \t]*\\="
6550 bound t))
6551 (setq func (match-string 2)
6552 func-point (goto-char (match-beginning 2))
6553 pos func-point)
6554 (if (re-search-backward
6555 "->[ \t]*\\(\\([a-zA-Z][a-zA-Z0-9$_]*\\)::\\)?\\=" bound t)
6556 (setq arrow-start (copy-marker (match-beginning 0))
6557 class (or (match-string 2) t)))
6558 (throw
6559 'exit
6560 (list
6561 (idlwave-sintern-routine-or-method func class)
6562 (idlwave-sintern-class class)
6563 arrow-start func-point cnt)))
6564 (goto-char pos))
6565 (throw 'exit nil)))))))
6566
6567 (defun idlwave-what-procedure (&optional bound)
6568 ;; Find out if point is within the argument list of a procedure.
6569 ;; The return value is ("procedure-name" class arrow-pos (point)).
6570
6571 ;; If the optional BOUND is an integer, bound backwards directed
6572 ;; searches to this point.
6573 (let ((pos (point)) pro-point
6574 pro class arrow-start string)
6575 (save-excursion
6576 ;;(idlwave-beginning-of-statement)
6577 (idlwave-start-of-substatement 'pre)
6578 (setq string (buffer-substring (point) pos))
6579 (if (string-match
6580 "\\`[ \t]*\\([a-zA-Z][a-zA-Z0-9$_]*\\)[ \t]*\\(,\\|\\'\\)" string)
6581 (setq pro (match-string 1 string)
6582 pro-point (+ (point) (match-beginning 1)))
6583 (if (and (idlwave-skip-object)
6584 (setq string (buffer-substring (point) pos))
6585 (string-match
6586 "\\`[ \t]*\\(->\\)[ \t]*\\(\\([a-zA-Z][a-zA-Z0-9$_]*\\)::\\)?\\([a-zA-Z][a-zA-Z0-9$_]*\\)?[ \t]*\\(,\\|\\(\\$\\s *\\(;.*\\)?\\)?$\\)"
6587 string))
6588 (setq pro (if (match-beginning 4)
6589 (match-string 4 string))
6590 pro-point (if (match-beginning 4)
6591 (+ (point) (match-beginning 4))
6592 pos)
6593 arrow-start (copy-marker (+ (point) (match-beginning 1)))
6594 class (or (match-string 3 string) t)))))
6595 (list (idlwave-sintern-routine-or-method pro class)
6596 (idlwave-sintern-class class)
6597 arrow-start
6598 pro-point)))
6599
6600 (defun idlwave-skip-object ()
6601 ;; If there is an object at point, move over it and return t.
6602 (let ((pos (point)))
6603 (if (catch 'exit
6604 (save-excursion
6605 (skip-chars-forward " ") ; white space
6606 (skip-chars-forward "*") ; de-reference
6607 (cond
6608 ((looking-at idlwave-identifier)
6609 (goto-char (match-end 0)))
6610 ((eq (following-char) ?\()
6611 nil)
6612 (t (throw 'exit nil)))
6613 (catch 'endwhile
6614 (while t
6615 (cond ((eq (following-char) ?.)
6616 (forward-char 1)
6617 (if (not (looking-at idlwave-identifier))
6618 (throw 'exit nil))
6619 (goto-char (match-end 0)))
6620 ((memq (following-char) '(?\( ?\[))
6621 (condition-case nil
6622 (forward-list 1)
6623 (error (throw 'exit nil))))
6624 (t (throw 'endwhile t)))))
6625 (if (looking-at "[ \t]*->")
6626 (throw 'exit (setq pos (match-beginning 0)))
6627 (throw 'exit nil))))
6628 (goto-char pos)
6629 nil)))
6630
6631 (defun idlwave-last-valid-char ()
6632 "Return the last character before point which is not white or a comment
6633 and also not part of the current identifier. Since we do this in
6634 order to identify places where keywords are, we consider the initial
6635 `/' of a keyword as part of the identifier.
6636 This function is not general, can only be used for completion stuff."
6637 (catch 'exit
6638 (save-excursion
6639 ;; skip the current identifier
6640 (skip-chars-backward "a-zA-Z0-9_$")
6641 ;; also skip a leading slash which might be belong to the keyword
6642 (if (eq (preceding-char) ?/)
6643 (backward-char 1))
6644 ;; FIXME: does not check if this is a valid identifier
6645 (while t
6646 (skip-chars-backward " \t")
6647 (cond
6648 ((memq (preceding-char) '(?\; ?\$)) (throw 'exit nil))
6649 ((eq (preceding-char) ?\n)
6650 (beginning-of-line 0)
6651 (if (looking-at "\\([^\n]*\\)\\$[ \t]*\\(;[^\n]*\\)?\n")
6652 ;; continuation line
6653 (goto-char (match-end 1))
6654 (throw 'exit nil)))
6655 (t (throw 'exit (preceding-char))))))))
6656
6657 (defvar idlwave-complete-after-success-form nil
6658 "A form to evaluate after successful completion.")
6659 (defvar idlwave-complete-after-success-form-force nil
6660 "A form to evaluate after completion selection in *Completions* buffer.")
6661 (defconst idlwave-completion-mark (make-marker)
6662 "A mark pointing to the beginning of the completion string.")
6663 (defvar completion-highlight-first-word-only) ;XEmacs.
6664
6665 (defun idlwave-complete-in-buffer (type stype list selector prompt isa
6666 &optional prepare-display-function
6667 special-selector)
6668 "Perform TYPE completion of word before point against LIST.
6669 SELECTOR is the PREDICATE argument for the completion function. Show
6670 PROMPT in echo area. TYPE is one of the intern types, e.g. 'function,
6671 'procedure, 'class-tag, 'keyword, 'sysvar, etc. SPECIAL-SELECTOR is
6672 used only once, for `all-completions', and can be used to, e.g.,
6673 accumulate information on matching completions."
6674 (let* ((completion-ignore-case t)
6675 beg (end (point)) slash part spart completion all-completions
6676 dpart dcompletion)
6677
6678 (unless list
6679 (error (concat prompt ": No completions available")))
6680
6681 ;; What is already in the buffer?
6682 (save-excursion
6683 (skip-chars-backward "a-zA-Z0-9_$")
6684 (setq slash (eq (preceding-char) ?/)
6685 beg (point)
6686 idlwave-complete-after-success-form
6687 (list 'idlwave-after-successful-completion
6688 (list 'quote type) slash beg)
6689 idlwave-complete-after-success-form-force
6690 (list 'idlwave-after-successful-completion
6691 (list 'quote type) slash (list 'quote 'force))))
6692
6693 ;; Try a completion
6694 (setq part (buffer-substring beg end)
6695 dpart (downcase part)
6696 spart (idlwave-sintern stype part)
6697 completion (try-completion part list selector)
6698 dcompletion (if (stringp completion) (downcase completion))
6699 idlwave-completion-help-links nil)
6700 (cond
6701 ((null completion)
6702 ;; nothing available.
6703 (error (concat prompt ": no completion for \"%s\"") part))
6704 ((and (not (equal dpart dcompletion))
6705 (not (eq t completion)))
6706 ;; We can add something
6707 (delete-region beg end)
6708 (insert (if (and (string= part dpart)
6709 (or (not (string= part ""))
6710 idlwave-complete-empty-string-as-lower-case)
6711 (not idlwave-completion-force-default-case))
6712 dcompletion
6713 completion))
6714 (if (eq t (try-completion completion list selector))
6715 ;; Now this is a unique match
6716 (idlwave-after-successful-completion type slash beg))
6717 t)
6718 ((or (eq completion t)
6719 (and (= 1 (length (setq all-completions
6720 (idlwave-uniquify
6721 (all-completions part list
6722 (or special-selector
6723 selector))))))
6724 (equal dpart dcompletion)))
6725 ;; This is already complete
6726 (idlwave-after-successful-completion type slash beg)
6727 (message "%s is already the complete %s" part isa)
6728 nil)
6729 (t
6730 ;; We cannot add something - offer a list.
6731 (message "Making completion list...")
6732
6733 (unless idlwave-completion-help-links ; already set somewhere?
6734 (mapc (lambda (x) ; Pass link prop through to highlight-linked
6735 (let ((link (get-text-property 0 'link (car x))))
6736 (if link
6737 (push (cons (car x) link)
6738 idlwave-completion-help-links))))
6739 list))
6740 (let* ((list all-completions)
6741 ;; "complete" means, this is already a valid completion
6742 (complete (memq spart all-completions))
6743 (completion-highlight-first-word-only t)) ; XEmacs
6744 ;; (completion-fixup-function ; Emacs
6745 ;; (lambda () (and (eq (preceding-char) ?>)
6746 ;; (re-search-backward " <" beg t)))))
6747
6748 (setq list (sort list (lambda (a b)
6749 (string< (downcase a) (downcase b)))))
6750 (if prepare-display-function
6751 (setq list (funcall prepare-display-function list)))
6752 (if (and (string= part dpart)
6753 (or (not (string= part ""))
6754 idlwave-complete-empty-string-as-lower-case)
6755 (not idlwave-completion-force-default-case))
6756 (setq list (mapcar (lambda (x)
6757 (if (listp x)
6758 (setcar x (downcase (car x)))
6759 (setq x (downcase x)))
6760 x)
6761 list)))
6762 (idlwave-display-completion-list list prompt beg complete))
6763 t))))
6764
6765 (defun idlwave-complete-class ()
6766 "Complete a class at point."
6767 (interactive)
6768 ;; Call `idlwave-routines' to make sure the class list will be available
6769 (idlwave-routines)
6770 ;; Check for the special case of completing empty string after pro/function
6771 (if (let ((case-fold-search t))
6772 (save-excursion
6773 (and
6774 (re-search-backward "\\<\\(pro\\|function\\)[ \t]+\\="
6775 (- (point) 15) t)
6776 (goto-char (point-min))
6777 (re-search-forward
6778 "^[ \t]*\\(pro\\|function\\)[ \t]+\\([a-zA-Z0-9_]+::\\)" nil t))))
6779 ;; Yank the full class specification
6780 (insert (match-string 2))
6781 ;; Do the completion, using list gathered from `idlwave-routines'
6782 (idlwave-complete-in-buffer
6783 'class 'class (idlwave-class-alist) nil
6784 "Select a class" "class"
6785 (lambda (list) ;; Push it to help-links if system help available
6786 (mapcar (lambda (x)
6787 (let* ((entry (idlwave-class-info x))
6788 (link (nth 1 (assq 'link entry))))
6789 (if link (push (cons x link)
6790 idlwave-completion-help-links))
6791 x))
6792 list)))))
6793
6794 (defun idlwave-attach-classes (list type show-classes)
6795 ;; Attach the proper class list to a LIST of completion items.
6796 ;; TYPE, when 'kwd, shows classes for method keywords, when
6797 ;; 'class-tag, for class tags, and otherwise for methods.
6798 ;; SHOW-CLASSES is the value of `idlwave-completion-show-classes'.
6799 (if (or (null show-classes) ; don't want to see classes
6800 (null class-selector) ; not a method call
6801 (and
6802 (stringp class-selector) ; the class is already known
6803 (not super-classes))) ; no possibilities for inheritance
6804 ;; In these cases, we do not have to do anything
6805 list
6806 (let* ((do-prop (and (>= show-classes 0)
6807 (>= emacs-major-version 21)))
6808 (do-buf (not (= show-classes 0)))
6809 ;; (do-dots (featurep 'xemacs))
6810 (do-dots t)
6811 (inherit (if (and (not (eq type 'class-tag)) super-classes)
6812 (cons class-selector super-classes)))
6813 (max (abs show-classes))
6814 (lmax (if do-dots (apply 'max (mapcar 'length list))))
6815 classes nclasses class-info space)
6816 (mapcar
6817 (lambda (x)
6818 ;; get the classes
6819 (if (eq type 'class-tag)
6820 ;; Just one class for tags
6821 (setq classes
6822 (list
6823 (idlwave-class-or-superclass-with-tag class-selector x)))
6824 ;; Multiple classes for method or method-keyword
6825 (setq classes
6826 (if (eq type 'kwd)
6827 (idlwave-all-method-keyword-classes
6828 method-selector x type-selector)
6829 (idlwave-all-method-classes x type-selector)))
6830 (if inherit
6831 (setq classes
6832 (delq nil
6833 (mapcar (lambda (x) (if (memq x inherit) x nil))
6834 classes)))))
6835 (setq nclasses (length classes))
6836 ;; Make the separator between item and class-info
6837 (if do-dots
6838 (setq space (concat " " (make-string (- lmax (length x)) ?.)))
6839 (setq space " "))
6840 (if do-buf
6841 ;; We do want info in the buffer
6842 (if (<= nclasses max)
6843 (setq class-info (concat
6844 space
6845 "<" (mapconcat 'identity classes ",") ">"))
6846 (setq class-info (format "%s<%d classes>" space nclasses)))
6847 (setq class-info nil))
6848 (when do-prop
6849 ;; We do want properties
6850 (setq x (copy-sequence x))
6851 (put-text-property 0 (length x)
6852 'help-echo (mapconcat 'identity classes " ")
6853 x))
6854 (if class-info
6855 (list x class-info)
6856 x))
6857 list))))
6858
6859 (defun idlwave-attach-method-classes (list)
6860 ;; Call idlwave-attach-classes with method parameters
6861 (idlwave-attach-classes list 'method idlwave-completion-show-classes))
6862 (defun idlwave-attach-keyword-classes (list)
6863 ;; Call idlwave-attach-classes with keyword parameters
6864 (idlwave-attach-classes list 'kwd idlwave-completion-show-classes))
6865 (defun idlwave-attach-class-tag-classes (list)
6866 ;; Call idlwave-attach-classes with class structure tags
6867 (idlwave-attach-classes list 'class-tag idlwave-completion-show-classes))
6868
6869
6870 ;;----------------------------------------------------------------------
6871 ;;----------------------------------------------------------------------
6872 ;;----------------------------------------------------------------------
6873 ;;----------------------------------------------------------------------
6874 ;;----------------------------------------------------------------------
6875 (when (featurep 'xemacs)
6876 (defvar rtn)
6877 (defun idlwave-pset (item)
6878 (set 'rtn item)))
6879
6880 (defun idlwave-popup-select (ev list title &optional sort)
6881 "Select an item in LIST with a popup menu.
6882 TITLE is the title to put atop the popup. If SORT is non-nil,
6883 sort the list before displaying."
6884 (let ((maxpopup idlwave-max-popup-menu-items)
6885 rtn menu)
6886 (cond ((null list))
6887 ((= 1 (length list))
6888 (setq rtn (car list)))
6889 ((featurep 'xemacs)
6890 (if sort (setq list (sort list (lambda (a b)
6891 (string< (upcase a) (upcase b))))))
6892 (setq menu
6893 (append (list title)
6894 (mapcar (lambda (x) (vector x (list 'idlwave-pset
6895 x)))
6896 list)))
6897 (setq menu (idlwave-split-menu-xemacs menu maxpopup))
6898 (let ((resp (get-popup-menu-response menu)))
6899 (funcall (event-function resp) (event-object resp))))
6900 (t
6901 (if sort (setq list (sort list (lambda (a b)
6902 (string< (upcase a) (upcase b))))))
6903 (setq menu (cons title
6904 (list
6905 (append (list "")
6906 (mapcar (lambda(x) (cons x x)) list)))))
6907 (setq menu (idlwave-split-menu-emacs menu maxpopup))
6908 (setq rtn (x-popup-menu ev menu))))
6909 rtn))
6910
6911 (defun idlwave-split-menu-xemacs (menu N)
6912 "Split the MENU into submenus of maximum length N."
6913 (if (<= (length menu) (1+ N))
6914 ;; No splitting needed
6915 menu
6916 (let* ((title (car menu))
6917 (entries (cdr menu))
6918 (menu (list title))
6919 (cnt 0)
6920 (nextmenu nil))
6921 (while entries
6922 (while (and entries (< cnt N))
6923 (setq cnt (1+ cnt)
6924 nextmenu (cons (car entries) nextmenu)
6925 entries (cdr entries)))
6926 (setq nextmenu (nreverse nextmenu))
6927 (setq nextmenu (cons (format "%s...%s"
6928 (aref (car nextmenu) 0)
6929 (aref (nth (1- cnt) nextmenu) 0))
6930 nextmenu))
6931 (setq menu (cons nextmenu menu)
6932 nextmenu nil
6933 cnt 0))
6934 (nreverse menu))))
6935
6936 (defun idlwave-split-menu-emacs (menu N)
6937 "Split the MENU into submenus of maximum length N."
6938 (if (<= (length (nth 1 menu)) (1+ N))
6939 ;; No splitting needed
6940 menu
6941 (let* ((title (car menu))
6942 (entries (cdr (nth 1 menu)))
6943 (menu nil)
6944 (cnt 0)
6945 (nextmenu nil))
6946 (while entries
6947 (while (and entries (< cnt N))
6948 (setq cnt (1+ cnt)
6949 nextmenu (cons (car entries) nextmenu)
6950 entries (cdr entries)))
6951 (setq nextmenu (nreverse nextmenu))
6952 (prin1 nextmenu)
6953 (setq nextmenu (cons (format "%s...%s"
6954 (car (car nextmenu))
6955 (car (nth (1- cnt) nextmenu)))
6956 nextmenu))
6957 (setq menu (cons nextmenu menu)
6958 nextmenu nil
6959 cnt 0))
6960 (setq menu (nreverse menu))
6961 (setq menu (cons title menu))
6962 menu)))
6963
6964 (defvar idlwave-completion-setup-hook nil)
6965
6966 (defun idlwave-scroll-completions (&optional message)
6967 "Scroll the completion window on this frame."
6968 (let ((cwin (get-buffer-window "*Completions*" 'visible))
6969 (win (selected-window)))
6970 (unwind-protect
6971 (progn
6972 (select-window cwin)
6973 (condition-case nil
6974 (scroll-up)
6975 (error (if (and (listp last-command)
6976 (nth 2 last-command))
6977 (progn
6978 (select-window win)
6979 (eval idlwave-complete-after-success-form))
6980 (set-window-start cwin (point-min)))))
6981 (and message (message "%s" message)))
6982 (select-window win))))
6983
6984 (defun idlwave-display-completion-list (list &optional message beg complete)
6985 "Display the completions in LIST in the completions buffer and echo MESSAGE."
6986 (unless (and (get-buffer-window "*Completions*")
6987 (idlwave-local-value 'idlwave-completion-p "*Completions*"))
6988 (move-marker idlwave-completion-mark beg)
6989 (setq idlwave-before-completion-wconf (current-window-configuration)))
6990
6991 (if (featurep 'xemacs)
6992 (idlwave-display-completion-list-xemacs
6993 list)
6994 (idlwave-display-completion-list-emacs list))
6995
6996 ;; Store a special value in `this-command'. When `idlwave-complete'
6997 ;; finds this in `last-command', it will scroll the *Completions* buffer.
6998 (setq this-command (list 'idlwave-display-completion-list message complete))
6999
7000 ;; Mark the completions buffer as created by cib
7001 (idlwave-set-local 'idlwave-completion-p t "*Completions*")
7002
7003 ;; Fontify the classes
7004 (if (and idlwave-completion-fontify-classes
7005 (consp (car list)))
7006 (idlwave-completion-fontify-classes))
7007
7008 ;; Run the hook
7009 (run-hooks 'idlwave-completion-setup-hook)
7010
7011 ;; Display the message
7012 (message "%s" (or message "Making completion list...done")))
7013
7014 (defun idlwave-choose (function &rest args)
7015 "Call FUNCTION as a completion chooser and pass ARGS to it."
7016 (let ((completion-ignore-case t)) ; install correct value
7017 (apply function args))
7018 (if (and (derived-mode-p 'idlwave-shell-mode)
7019 (boundp 'font-lock-mode)
7020 (not font-lock-mode))
7021 ;; For the shell, remove the fontification of the word before point
7022 (let ((beg (save-excursion
7023 (skip-chars-backward "a-zA-Z0-9_")
7024 (point))))
7025 (remove-text-properties beg (point) '(face nil))))
7026 (eval idlwave-complete-after-success-form-force))
7027
7028 (defun idlwave-keyboard-quit ()
7029 (interactive)
7030 (unwind-protect
7031 (if (eq (car-safe last-command) 'idlwave-display-completion-list)
7032 (idlwave-restore-wconf-after-completion))
7033 (keyboard-quit)))
7034
7035 (defun idlwave-restore-wconf-after-completion ()
7036 "Restore the old (before completion) window configuration."
7037 (and idlwave-completion-restore-window-configuration
7038 idlwave-before-completion-wconf
7039 (set-window-configuration idlwave-before-completion-wconf)))
7040
7041 (defun idlwave-one-key-select (sym prompt delay)
7042 "Make the user select an element from the alist in the variable SYM.
7043 The keys of the alist are expected to be strings. The function returns the
7044 car of the selected association.
7045 To do this, PROMPT is displayed and the user must hit a letter key to
7046 select an entry. If the user does not reply within DELAY seconds, a help
7047 window with the options is displayed automatically.
7048 The key which is associated with each option is generated automatically.
7049 First, the strings are checked for preselected keys, like in \"[P]rint\".
7050 If these don't exist, a letter in the string is automatically selected."
7051 (let* ((alist (symbol-value sym))
7052 (temp-buffer-show-hook (if (fboundp 'fit-window-to-buffer)
7053 '(fit-window-to-buffer)))
7054 keys-alist char)
7055 ;; First check the cache
7056 (if (and (eq (symbol-value sym) (get sym :one-key-alist-last)))
7057 (setq keys-alist (get sym :one-key-alist-cache))
7058 ;; Need to make new list
7059 (setq keys-alist (idlwave-make-one-key-alist alist))
7060 (put sym :one-key-alist-cache keys-alist)
7061 (put sym :one-key-alist-last alist))
7062 ;; Display prompt and wait for quick reply
7063 (message "%s[%s]" prompt
7064 (mapconcat (lambda(x) (char-to-string (car x)))
7065 keys-alist ""))
7066 (if (sit-for delay)
7067 ;; No quick reply: Show help
7068 (save-window-excursion
7069 (with-output-to-temp-buffer "*Completions*"
7070 (dolist (x keys-alist)
7071 (princ (nth 1 x))
7072 (princ "\n")))
7073 (setq char (read-char)))
7074 (setq char (read-char)))
7075 (message nil)
7076 ;; Return the selected result
7077 (nth 2 (assoc char keys-alist))))
7078
7079 ;; Used for, e.g., electric debug super-examine.
7080 (defun idlwave-make-one-key-alist (alist)
7081 "Make an alist for single key selection."
7082 (let ((l alist) keys-alist name start char help
7083 (cnt 0)
7084 (case-fold-search nil))
7085 (while l
7086 (setq name (car (car l))
7087 l (cdr l))
7088 (catch 'exit
7089 ;; First check if the configuration predetermined a key
7090 (if (string-match "\\[\\(.\\)\\]" name)
7091 (progn
7092 (setq char (string-to-char (downcase (match-string 1 name)))
7093 help (format "%c: %s" char name)
7094 keys-alist (cons (list char help name) keys-alist))
7095 (throw 'exit t)))
7096 ;; Then check for capital letters
7097 (setq start 0)
7098 (while (string-match "[A-Z]" name start)
7099 (setq start (match-end 0)
7100 char (string-to-char (downcase (match-string 0 name))))
7101 (if (not (assoc char keys-alist))
7102 (progn
7103 (setq help (format "%c: %s" char
7104 (replace-match
7105 (concat "[" (match-string 0 name) "]")
7106 t t name))
7107 keys-alist (cons (list char help name) keys-alist))
7108 (throw 'exit t))))
7109 ;; Now check for lowercase letters
7110 (setq start 0)
7111 (while (string-match "[a-z]" name start)
7112 (setq start (match-end 0)
7113 char (string-to-char (match-string 0 name)))
7114 (if (not (assoc char keys-alist))
7115 (progn
7116 (setq help (format "%c: %s" char
7117 (replace-match
7118 (concat "[" (match-string 0 name) "]")
7119 t t name))
7120 keys-alist (cons (list char help name) keys-alist))
7121 (throw 'exit t))))
7122 ;; Bummer, nothing found! Use a stupid number
7123 (setq char (string-to-char (int-to-string (setq cnt (1+ cnt))))
7124 help (format "%c: %s" char name)
7125 keys-alist (cons (list char help name) keys-alist))))
7126 (nreverse keys-alist)))
7127
7128 (defun idlwave-set-local (var value &optional buffer)
7129 "Set the buffer-local value of VAR in BUFFER to VALUE."
7130 (with-current-buffer (or buffer (current-buffer))
7131 (set (make-local-variable var) value)))
7132
7133 (defun idlwave-local-value (var &optional buffer)
7134 "Return the value of VAR in BUFFER, but only if VAR is local to BUFFER."
7135 (with-current-buffer (or buffer (current-buffer))
7136 (and (local-variable-p var (current-buffer))
7137 (symbol-value var))))
7138
7139 ;; In XEmacs, we can use :activate-callback directly to advice the
7140 ;; choose functions. We use the private keymap only for the online
7141 ;; help feature.
7142
7143 (defvar idlwave-completion-map nil
7144 "Keymap for `completion-list-mode' with `idlwave-complete'.")
7145
7146 (defun idlwave-display-completion-list-xemacs (list &rest cl-args)
7147 (with-output-to-temp-buffer "*Completions*"
7148 (apply 'display-completion-list list
7149 ':activate-callback 'idlwave-default-choose-completion
7150 cl-args))
7151 (with-current-buffer "*Completions*"
7152 (use-local-map
7153 (or idlwave-completion-map
7154 (setq idlwave-completion-map
7155 (idlwave-make-modified-completion-map-xemacs
7156 (current-local-map)))))))
7157
7158 (defun idlwave-default-choose-completion (&rest args)
7159 "Execute `default-choose-completion' and then restore the win-conf."
7160 (apply 'idlwave-choose 'default-choose-completion args))
7161
7162 (defun idlwave-make-modified-completion-map-xemacs (old-map)
7163 "Replace `choose-completion' and `mouse-choose-completion' in OLD-MAP."
7164 (let ((new-map (copy-keymap old-map)))
7165 (define-key new-map [button3up] 'idlwave-mouse-completion-help)
7166 (define-key new-map [button3] (lambda ()
7167 (interactive)
7168 (setq this-command last-command)))
7169 new-map))
7170
7171 ;; In Emacs we also replace keybindings in the completion
7172 ;; map in order to install our wrappers.
7173
7174 (defun idlwave-display-completion-list-emacs (list)
7175 "Display completion list and install the choose wrappers."
7176 (with-output-to-temp-buffer "*Completions*"
7177 (display-completion-list list))
7178 (with-current-buffer "*Completions*"
7179 (use-local-map
7180 (or idlwave-completion-map
7181 (setq idlwave-completion-map
7182 (idlwave-make-modified-completion-map-emacs
7183 (current-local-map)))))))
7184
7185 (defun idlwave-make-modified-completion-map-emacs (old-map)
7186 "Replace `choose-completion' and `mouse-choose-completion' in OLD-MAP."
7187 (let ((new-map (copy-keymap old-map)))
7188 (substitute-key-definition
7189 'choose-completion 'idlwave-choose-completion new-map)
7190 (substitute-key-definition
7191 'mouse-choose-completion 'idlwave-mouse-choose-completion new-map)
7192 (define-key new-map [mouse-3] 'idlwave-mouse-completion-help)
7193 new-map))
7194
7195 (defun idlwave-choose-completion (&rest args)
7196 "Choose the completion that point is in or next to."
7197 (interactive)
7198 (apply 'idlwave-choose 'choose-completion args))
7199
7200 (defun idlwave-mouse-choose-completion (&rest args)
7201 "Click on an alternative in the `*Completions*' buffer to choose it."
7202 (interactive "e")
7203 (apply 'idlwave-choose 'mouse-choose-completion args))
7204
7205 ;;----------------------------------------------------------------------
7206 ;;----------------------------------------------------------------------
7207
7208 ;;; ------------------------------------------------------------------------
7209 ;;; Structure parsing code, and code to manage class info
7210
7211 ;;
7212 ;; - Go again over the documentation how to write a completion
7213 ;; plugin. It is in self.el, but currently still very bad.
7214 ;; This could be in a separate file in the distribution, or
7215 ;; in an appendix for the manual.
7216
7217 (defvar idlwave-struct-skip
7218 "[ \t]*\\(\\$.*\n\\(^[ \t]*\\(\\$[ \t]*\\)?\\(;.*\\)?\n\\)*\\)?[ \t]*"
7219 "Regexp for skipping continued blank or comment-only lines in structures.")
7220
7221 (defvar idlwave-struct-tag-regexp
7222 (concat "[{,]" ;leading comma/brace
7223 idlwave-struct-skip ; 4 groups
7224 "\\([a-zA-Z][a-zA-Z0-9_]*\\)" ;the tag itself, group 5
7225 "[ \t]*:") ; the final colon
7226 "Regexp for structure tags.")
7227
7228 (defun idlwave-struct-tags ()
7229 "Return a list of all tags in the structure defined at point.
7230 Point is expected just before the opening `{' of the struct definition."
7231 (save-excursion
7232 (let* ((borders (idlwave-struct-borders))
7233 (beg (car borders))
7234 (end (cdr borders))
7235 tags)
7236 (goto-char beg)
7237 (save-restriction
7238 (narrow-to-region beg end)
7239 (while (re-search-forward idlwave-struct-tag-regexp end t)
7240 ;; Check if we are still on the top level of the structure.
7241 (if (and (condition-case nil (progn (up-list -1) t) (error nil))
7242 (= (point) beg))
7243 (push (match-string-no-properties 5) tags))
7244 (goto-char (match-end 0))))
7245 (nreverse tags))))
7246
7247 (defun idlwave-find-struct-tag (tag)
7248 "Find a given TAG in the structure defined at point."
7249 (let* ((borders (idlwave-struct-borders))
7250 (end (cdr borders))
7251 (case-fold-search t))
7252 (re-search-forward (concat "\\(^[ \t]*\\|[,{][ \t]*\\)" tag "[ \t]*:")
7253 end t)))
7254
7255 (defun idlwave-struct-inherits ()
7256 "Return a list of all `inherits' names in the struct at point.
7257 Point is expected just before the opening `{' of the struct definition."
7258 (save-excursion
7259 (let* ((borders (idlwave-struct-borders))
7260 (beg (car borders))
7261 (end (cdr borders))
7262 (case-fold-search t)
7263 names)
7264 (goto-char beg)
7265 (save-restriction
7266 (narrow-to-region beg end)
7267 (while (re-search-forward
7268 (concat "[{,]" ;leading comma/brace
7269 idlwave-struct-skip ; 4 groups
7270 "inherits" ; The INHERITS tag
7271 idlwave-struct-skip ; 4 more
7272 "\\([a-zA-Z][a-zA-Z0-9_]*\\)") ; The super-group, #9
7273 end t)
7274 ;; Check if we are still on the top level of the structure.
7275 (if (and (condition-case nil (progn (up-list -1) t) (error nil))
7276 (= (point) beg))
7277 (push (match-string-no-properties 9) names))
7278 (goto-char (match-end 0))))
7279 (nreverse names))))
7280
7281 (defun idlwave-in-structure ()
7282 "Return t if point is inside an IDL structure definition."
7283 (let ((beg (point)))
7284 (save-excursion
7285 (if (not (or (idlwave-in-comment) (idlwave-in-quote)))
7286 (if (idlwave-find-structure-definition nil nil 'back)
7287 (let ((borders (idlwave-struct-borders)))
7288 (or (= (car borders) (cdr borders)) ;; struct not yet closed...
7289 (and (> beg (car borders)) (< beg (cdr borders))))))))))
7290
7291 (defun idlwave-struct-borders ()
7292 "Return the borders of the {...} after point as a cons cell."
7293 (let (beg)
7294 (save-excursion
7295 (skip-chars-forward "^{")
7296 (setq beg (point))
7297 (condition-case nil (forward-list 1)
7298 (error (goto-char beg)))
7299 (cons beg (point)))))
7300
7301 (defun idlwave-find-structure-definition (&optional var name bound)
7302 "Search forward for a structure definition.
7303 If VAR is non-nil, search for a structure assigned to variable VAR.
7304 If NAME is non-nil, search for a named structure NAME, if a string,
7305 or a generic named structure otherwise. If BOUND is an integer, limit
7306 the search. If BOUND is the symbol `all', we search first back and
7307 then forward through the entire file. If BOUND is the symbol `back'
7308 we search only backward."
7309 (let* ((ws "[ \t]*\\(\\$.*\n[ \t]*\\)*")
7310 (case-fold-search t)
7311 (lim (if (integerp bound) bound nil))
7312 (re (concat
7313 (if var
7314 (concat "\\<" (regexp-quote (downcase var)) "\\>" ws)
7315 "\\(\\)")
7316 "=" ws "\\({\\)"
7317 (if name
7318 (if (stringp name)
7319 (concat ws "\\(\\<" (downcase name) "\\)[^a-zA-Z0-9_$]")
7320 ;; Just a generic name
7321 (concat ws "\\<\\([a-zA-Z_0-9$]+\\)" ws ","))
7322 ""))))
7323 (if (or (and (or (eq bound 'all) (eq bound 'back))
7324 (re-search-backward re nil t))
7325 (and (not (eq bound 'back)) (re-search-forward re lim t)))
7326 (progn
7327 (goto-char (match-beginning 3))
7328 (match-string-no-properties 5)))))
7329
7330 (defvar idlwave-class-info nil)
7331 (defvar idlwave-class-reset nil) ; to reset buffer-local classes
7332
7333 (add-hook 'idlwave-update-rinfo-hook
7334 (lambda () (setq idlwave-class-reset t)))
7335 (add-hook 'idlwave-after-load-rinfo-hook
7336 (lambda () (setq idlwave-class-info nil)))
7337
7338 (defun idlwave-class-info (class)
7339 (let (list entry)
7340 (if idlwave-class-info
7341 (if idlwave-class-reset
7342 (setq
7343 idlwave-class-reset nil
7344 idlwave-class-info ; Remove any visited in a buffer
7345 (delq nil (mapcar
7346 (lambda (x)
7347 (let ((filebuf
7348 (idlwave-class-file-or-buffer
7349 (or (cdr (assq 'found-in x)) (car x)))))
7350 (if (cdr filebuf)
7351 nil
7352 x)))
7353 idlwave-class-info))))
7354 ;; Info is nil, put in the system stuff to start.
7355 (setq idlwave-class-info idlwave-system-class-info)
7356 (setq list idlwave-class-info)
7357 (while (setq entry (pop list))
7358 (idlwave-sintern-class-info entry)))
7359 (setq class (idlwave-sintern-class class))
7360 (or (assq class idlwave-class-info)
7361 (progn (idlwave-scan-class-info class)
7362 (assq class idlwave-class-info)))))
7363
7364 (defun idlwave-sintern-class-info (entry)
7365 "Sintern the class names in a class-info entry."
7366 (let ((inherits (assq 'inherits entry)))
7367 (setcar entry (idlwave-sintern-class (car entry) 'set))
7368 (if inherits
7369 (setcdr inherits (mapcar (lambda (x) (idlwave-sintern-class x 'set))
7370 (cdr inherits))))))
7371
7372 (defun idlwave-find-class-definition (class &optional all-hook alt-class)
7373 "Find class structure definition(s).
7374 If ALL-HOOK is set, find all named structure definitions in a given
7375 class__define routine, on which ALL-HOOK will be run. If ALT-CLASS is
7376 set, look for the name__define pro, and inside of it, for the ALT-CLASS
7377 class/struct definition."
7378 (let ((case-fold-search t) end-lim name)
7379 (when (re-search-forward
7380 (concat "^[ \t]*pro[ \t]+" (downcase class) "__define" "\\>") nil t)
7381 (if all-hook
7382 (progn
7383 ;; For everything there
7384 (setq end-lim (save-excursion (idlwave-end-of-subprogram) (point)))
7385 (while (setq name
7386 (idlwave-find-structure-definition nil t end-lim))
7387 (funcall all-hook name)))
7388 (idlwave-find-structure-definition nil (or alt-class class))))))
7389
7390
7391 (defun idlwave-class-file-or-buffer (class)
7392 "Find buffer visiting CLASS definition."
7393 (let* ((pro (concat (downcase class) "__define"))
7394 (file (idlwave-routine-source-file
7395 (nth 3 (idlwave-rinfo-assoc pro 'pro nil
7396 (idlwave-routines))))))
7397 (cons file (if file (idlwave-get-buffer-visiting file)))))
7398
7399
7400 (defun idlwave-scan-class-info (class)
7401 "Scan all class and named structure info in the class__define pro."
7402 (let* ((idlwave-auto-routine-info-updates nil)
7403 (filebuf (idlwave-class-file-or-buffer class))
7404 (file (car filebuf))
7405 (buf (cdr filebuf))
7406 (class (idlwave-sintern-class class)))
7407 (if (or
7408 (not file)
7409 (and ;; neither a regular file nor a visited buffer
7410 (not buf)
7411 (not (file-regular-p file))))
7412 nil ; Cannot find the file/buffer to get any info
7413 (save-excursion
7414 (if buf (set-buffer buf)
7415 ;; Read the file in temporarily
7416 (set-buffer (get-buffer-create " *IDLWAVE-tmp*"))
7417 (erase-buffer)
7418 (unless (derived-mode-p 'idlwave-mode)
7419 (idlwave-mode))
7420 (insert-file-contents file))
7421 (save-excursion
7422 (goto-char 1)
7423 (idlwave-find-class-definition class
7424 ;; Scan all of the structures found there
7425 (lambda (name)
7426 (let* ((this-class (idlwave-sintern-class name))
7427 (entry
7428 (list this-class
7429 (cons 'tags (idlwave-struct-tags))
7430 (cons 'inherits (idlwave-struct-inherits)))))
7431 (if (not (eq this-class class))
7432 (setq entry (nconc entry (list (cons 'found-in class)))))
7433 (idlwave-sintern-class-info entry)
7434 (push entry idlwave-class-info)))))))))
7435
7436 (defun idlwave-class-found-in (class)
7437 "Return the FOUND-IN property of the CLASS."
7438 (cdr (assq 'found-in (idlwave-class-info class))))
7439 (defun idlwave-class-tags (class)
7440 "Return the native tags in CLASS."
7441 (cdr (assq 'tags (idlwave-class-info class))))
7442 (defun idlwave-class-inherits (class)
7443 "Return the direct superclasses of CLASS."
7444 (cdr (assq 'inherits (idlwave-class-info class))))
7445
7446
7447 (defun idlwave-all-class-tags (class)
7448 "Return a list of native and inherited tags in CLASS."
7449 (condition-case err
7450 (apply 'append (mapcar 'idlwave-class-tags
7451 (cons class (idlwave-all-class-inherits class))))
7452 (error
7453 (idlwave-class-tag-reset)
7454 (error "%s" (error-message-string err)))))
7455
7456
7457 (defun idlwave-all-class-inherits (class)
7458 "Return a list of all superclasses of CLASS (recursively expanded).
7459 The list is cached in `idlwave-class-info' for faster access."
7460 (cond
7461 ((not idlwave-support-inheritance) nil)
7462 ((eq class nil) nil)
7463 ((eq class t) nil)
7464 (t
7465 (let ((info (idlwave-class-info class))
7466 entry)
7467 (if (setq entry (assq 'all-inherits info))
7468 (cdr entry)
7469 ;; Save the depth of inheritance scan to check for circular references
7470 (let ((inherits (mapcar (lambda (x) (cons x 0))
7471 (idlwave-class-inherits class)))
7472 rtn all-inherits cl)
7473 (while inherits
7474 (setq cl (pop inherits)
7475 rtn (cons (car cl) rtn)
7476 inherits (append (mapcar (lambda (x)
7477 (cons x (1+ (cdr cl))))
7478 (idlwave-class-inherits (car cl)))
7479 inherits))
7480 (if (> (cdr cl) 999)
7481 (error
7482 "Class scan: inheritance depth exceeded. Circular inheritance?")
7483 ))
7484 (setq all-inherits (nreverse rtn))
7485 (nconc info (list (cons 'all-inherits all-inherits)))
7486 all-inherits))))))
7487
7488 (defun idlwave-entry-keywords (entry &optional record-link)
7489 "Return the flat entry keywords alist from routine-info entry.
7490 If RECORD-LINK is non-nil, the keyword text is copied and a text
7491 property indicating the link is added."
7492 (let (kwds)
7493 (mapc
7494 (lambda (key-list)
7495 (let ((file (car key-list)))
7496 (mapcar (lambda (key-cons)
7497 (let ((key (car key-cons))
7498 (link (cdr key-cons)))
7499 (when (and record-link file)
7500 (setq key (copy-sequence key))
7501 (put-text-property
7502 0 (length key)
7503 'link
7504 (concat
7505 file
7506 (if link
7507 (concat idlwave-html-link-sep
7508 (number-to-string link))))
7509 key))
7510 (push (list key) kwds)))
7511 (cdr key-list))))
7512 (nthcdr 5 entry))
7513 (nreverse kwds)))
7514
7515 (defun idlwave-entry-find-keyword (entry keyword)
7516 "Find keyword KEYWORD in entry ENTRY, and return (with link) if set."
7517 (catch 'exit
7518 (mapc
7519 (lambda (key-list)
7520 (let ((file (car key-list))
7521 (kwd (assoc keyword (cdr key-list))))
7522 (when kwd
7523 (setq kwd (cons (car kwd)
7524 (if (and file (cdr kwd))
7525 (concat file
7526 idlwave-html-link-sep
7527 (number-to-string (cdr kwd)))
7528 (cdr kwd))))
7529 (throw 'exit kwd))))
7530 (nthcdr 5 entry))))
7531
7532 ;;==========================================================================
7533 ;;
7534 ;; Completing class structure tags. This is a completion plugin.
7535 ;; The necessary taglist is constructed dynamically
7536
7537 (defvar idlwave-current-tags-class nil)
7538 (defvar idlwave-current-class-tags nil)
7539 (defvar idlwave-current-native-class-tags nil)
7540 (defvar idlwave-sint-class-tags nil)
7541 (declare-function idlwave-sintern-class-tag "idlwave" t t)
7542 (idlwave-new-sintern-type 'class-tag)
7543 (add-to-list 'idlwave-complete-special 'idlwave-complete-class-structure-tag)
7544 (add-hook 'idlwave-update-rinfo-hook 'idlwave-class-tag-reset)
7545
7546 (defun idlwave-complete-class-structure-tag ()
7547 "Complete a structure tag on a `self' argument in an object method."
7548 (interactive)
7549 (let ((pos (point))
7550 (case-fold-search t))
7551 (if (save-excursion
7552 ;; Check if the context is right
7553 (skip-chars-backward "a-zA-Z0-9._$")
7554 (and (< (point) (- pos 4))
7555 (looking-at "self\\.")))
7556 (let* ((class-selector (nth 2 (idlwave-current-routine)))
7557 (super-classes (idlwave-all-class-inherits class-selector)))
7558 ;; Check if we are in a class routine
7559 (unless class-selector
7560 (error "Not in a method procedure or function"))
7561 ;; Check if we need to update the "current" class
7562 (if (not (equal class-selector idlwave-current-tags-class))
7563 (idlwave-prepare-class-tag-completion class-selector))
7564 (setq idlwave-completion-help-info
7565 (list 'idlwave-complete-class-structure-tag-help
7566 (idlwave-sintern-routine
7567 (concat class-selector "__define"))
7568 nil))
7569 ;; FIXME: idlwave-cpl-bold doesn't seem used anywhere.
7570 (let ((idlwave-cpl-bold idlwave-current-native-class-tags))
7571 (idlwave-complete-in-buffer
7572 'class-tag 'class-tag
7573 idlwave-current-class-tags nil
7574 (format "Select a tag of class %s" class-selector)
7575 "class tag"
7576 'idlwave-attach-class-tag-classes))
7577 t) ; return t to skip other completions
7578 nil)))
7579
7580 (defun idlwave-class-tag-reset ()
7581 (setq idlwave-current-tags-class nil))
7582
7583 (defun idlwave-prepare-class-tag-completion (class)
7584 "Find and parse the necessary class definitions for class structure tags."
7585 (setq idlwave-sint-class-tags nil)
7586 (setq idlwave-current-tags-class class)
7587 (setq idlwave-current-class-tags
7588 (mapcar (lambda (x)
7589 (list (idlwave-sintern-class-tag x 'set)))
7590 (idlwave-all-class-tags class)))
7591 (setq idlwave-current-native-class-tags
7592 (mapcar 'downcase (idlwave-class-tags class))))
7593
7594 ;===========================================================================
7595 ;;
7596 ;; Completing system variables and their structure fields
7597 ;; This is also a plugin.
7598
7599 (defvar idlwave-sint-sysvars nil)
7600 (defvar idlwave-sint-sysvartags nil)
7601 (declare-function idlwave-sintern-sysvar "idlwave" t t)
7602 (declare-function idlwave-sintern-sysvartag "idlwave" t t)
7603 (idlwave-new-sintern-type 'sysvar)
7604 (idlwave-new-sintern-type 'sysvartag)
7605 (add-to-list 'idlwave-complete-special 'idlwave-complete-sysvar-or-tag)
7606 (add-hook 'idlwave-update-rinfo-hook 'idlwave-sysvars-reset)
7607 (add-hook 'idlwave-after-load-rinfo-hook 'idlwave-sintern-sysvar-alist)
7608
7609
7610 (defun idlwave-complete-sysvar-or-tag ()
7611 "Complete a system variable."
7612 (interactive)
7613 (let ((pos (point))
7614 (case-fold-search t))
7615 (cond ((save-excursion
7616 ;; Check if the context is right for system variable
7617 (skip-chars-backward "[a-zA-Z0-9_$]")
7618 (equal (char-before) ?!))
7619 (setq idlwave-completion-help-info '(idlwave-complete-sysvar-help))
7620 (idlwave-complete-in-buffer 'sysvar 'sysvar
7621 idlwave-system-variables-alist nil
7622 "Select a system variable"
7623 "system variable")
7624 t) ; return t to skip other completions
7625 ((save-excursion
7626 ;; Check if the context is right for sysvar tag
7627 (skip-chars-backward "a-zA-Z0-9_$.")
7628 (and (equal (char-before) ?!)
7629 (looking-at "\\([a-zA-Z][a-zA-Z0-9_$]*\\)\\.")
7630 (<= (match-end 0) pos)))
7631 ;; Complete a system variable tag
7632 (let* ((var (idlwave-sintern-sysvar (match-string 1)))
7633 (entry (assq var idlwave-system-variables-alist))
7634 (tags (cdr (assq 'tags entry))))
7635 (or entry (error "!%s is not a known system variable" var))
7636 (or tags (error "System variable !%s is not a structure" var))
7637 (setq idlwave-completion-help-info
7638 (list 'idlwave-complete-sysvar-tag-help var))
7639 (idlwave-complete-in-buffer 'sysvartag 'sysvartag
7640 tags nil
7641 "Select a system variable tag"
7642 "system variable tag")
7643 t)) ; return t to skip other completions
7644 (t nil))))
7645
7646 (defvar idlw-help-link) ;dynamic variables set by help callback
7647 (defun idlwave-complete-sysvar-help (mode word)
7648 (let ((word (or (nth 1 idlwave-completion-help-info) word))
7649 (entry (assoc word idlwave-system-variables-alist)))
7650 (cond
7651 ((eq mode 'test)
7652 (and (stringp word) entry (nth 1 (assq 'link entry))))
7653 ((eq mode 'set)
7654 ;; Setting dynamic!!!
7655 (if entry (setq idlw-help-link (nth 1 (assq 'link entry)))))
7656 (t (error "This should not happen")))))
7657
7658 (defun idlwave-complete-sysvar-tag-help (mode word)
7659 (let* ((var (nth 1 idlwave-completion-help-info))
7660 (entry (assoc var idlwave-system-variables-alist))
7661 (tags (cdr (assq 'tags entry)))
7662 (main (nth 1 (assq 'link entry)))
7663 target)
7664 (cond
7665 ((eq mode 'test) ; we can at least link the main
7666 (and (stringp word) entry main))
7667 ((eq mode 'set)
7668 (if entry
7669 (setq idlw-help-link
7670 (if (setq target (cdr (assoc-string word tags t)))
7671 (idlwave-substitute-link-target main target)
7672 main)))) ;; setting dynamic!!!
7673 (t (error "This should not happen")))))
7674
7675 (defun idlwave-split-link-target (link)
7676 "Split a given LINK into link file and anchor."
7677 (if (string-match idlwave-html-link-sep link)
7678 (cons (substring link 0 (match-beginning 0))
7679 (string-to-number (substring link (match-end 0))))))
7680
7681 (defun idlwave-substitute-link-target (link target)
7682 "Substitute the TARGET anchor for the given LINK."
7683 (let (main-base)
7684 (setq main-base (if (string-match "#" link)
7685 (substring link 0 (match-beginning 0))
7686 link))
7687 (if target
7688 (concat main-base idlwave-html-link-sep (number-to-string target))
7689 link)))
7690
7691 ;; Fake help in the source buffer for class structure tags.
7692 ;; IDLW-HELP-LINK AND IDLW-HELP-NAME ARE GLOBAL-VARIABLES HERE.
7693 ;; (from idlwave-do-mouse-completion-help)
7694 (defvar idlw-help-name)
7695 (defvar idlw-help-link)
7696 (defvar idlwave-help-do-class-struct-tag nil)
7697 (defun idlwave-complete-class-structure-tag-help (mode word)
7698 (cond
7699 ((eq mode 'test) ; nothing gets fontified for class tags
7700 nil)
7701 ((eq mode 'set)
7702 (let (class-with found-in)
7703 (when (setq class-with
7704 (idlwave-class-or-superclass-with-tag
7705 idlwave-current-tags-class
7706 word))
7707 (if (assq (idlwave-sintern-class class-with)
7708 idlwave-system-class-info)
7709 (error "No help available for system class tags"))
7710 (if (setq found-in (idlwave-class-found-in class-with))
7711 (setq idlw-help-name (cons (concat found-in "__define") class-with))
7712 (setq idlw-help-name (concat class-with "__define")))))
7713 (setq idlw-help-link word
7714 idlwave-help-do-class-struct-tag t))
7715 (t (error "This should not happen"))))
7716
7717 (defun idlwave-class-or-superclass-with-tag (class tag)
7718 "Find and return the CLASS or one of its superclass with the
7719 associated TAG, if any."
7720 (let ((sclasses (cons class (idlwave-all-class-inherits class)))
7721 cl)
7722 (catch 'exit
7723 (while sclasses
7724 (setq cl (pop sclasses))
7725 (let ((tags (idlwave-class-tags cl)))
7726 (while tags
7727 (if (eq t (compare-strings tag 0 nil (car tags) 0 nil t))
7728 (throw 'exit cl))
7729 (setq tags (cdr tags))))))))
7730
7731
7732 (defun idlwave-sysvars-reset ()
7733 (if (and (fboundp 'idlwave-shell-is-running)
7734 (idlwave-shell-is-running)
7735 idlwave-idlwave_routine_info-compiled)
7736 (idlwave-shell-send-command "idlwave_get_sysvars"
7737 'idlwave-process-sysvars 'hide)))
7738
7739 (defun idlwave-process-sysvars ()
7740 (idlwave-shell-filter-sysvars)
7741 (setq idlwave-sint-sysvars nil
7742 idlwave-sint-sysvartags nil)
7743 (idlwave-sintern-sysvar-alist))
7744
7745 (defun idlwave-sintern-sysvar-alist ()
7746 (let ((list idlwave-system-variables-alist) entry tags)
7747 (while (setq entry (pop list))
7748 (setcar entry (idlwave-sintern-sysvar (car entry) 'set))
7749 (setq tags (assq 'tags entry))
7750 (if tags
7751 (setcdr tags
7752 (mapcar (lambda (x)
7753 (cons (idlwave-sintern-sysvartag (car x) 'set)
7754 (cdr x)))
7755 (cdr tags)))))))
7756
7757 (defvar idlwave-shell-command-output)
7758 (defun idlwave-shell-filter-sysvars ()
7759 "Get any new system variables and tags."
7760 (let ((text idlwave-shell-command-output)
7761 (start 0)
7762 (old idlwave-system-variables-alist)
7763 var tags type name class link old-entry)
7764 (setq idlwave-system-variables-alist nil)
7765 (while (string-match "^IDLWAVE-SYSVAR: !\\([a-zA-Z0-9_$]+\\)\\( \\(.*\\)\\)?"
7766 text start)
7767 (setq start (match-end 0)
7768 var (match-string 1 text)
7769 tags (if (match-end 3)
7770 (idlwave-split-string (match-string 3 text))))
7771 ;; Maintain old links, if present
7772 (setq old-entry (assq (idlwave-sintern-sysvar var) old))
7773 (setq link (assq 'link old-entry))
7774 (setq idlwave-system-variables-alist
7775 (cons (list var
7776 (cons
7777 'tags
7778 (mapcar (lambda (x)
7779 (cons x
7780 (cdr (assq
7781 (idlwave-sintern-sysvartag x)
7782 (cdr (assq 'tags old-entry))))))
7783 tags)) link)
7784 idlwave-system-variables-alist)))
7785 ;; Keep the old value if query was not successful
7786 (setq idlwave-system-variables-alist
7787 (or idlwave-system-variables-alist old))))
7788
7789 (defun idlwave-completion-fontify-classes ()
7790 "Goto the *Completions* buffer and fontify the class info."
7791 (when (featurep 'font-lock)
7792 (with-current-buffer "*Completions*"
7793 (save-excursion
7794 (goto-char (point-min))
7795 (let ((buffer-read-only nil))
7796 (while (re-search-forward "\\.*<[^>]+>" nil t)
7797 (put-text-property (match-beginning 0) (match-end 0)
7798 'face 'font-lock-string-face)))))))
7799
7800 (defun idlwave-uniquify (list)
7801 (let ((ht (make-hash-table :size (length list) :test 'equal)))
7802 (delq nil
7803 (mapcar (lambda (x)
7804 (unless (gethash x ht)
7805 (puthash x t ht)
7806 x))
7807 list))))
7808
7809 (defun idlwave-after-successful-completion (type slash &optional verify)
7810 "Add `=' or `(' after successful completion of keyword and function.
7811 Restore the pre-completion window configuration if possible."
7812 (cond
7813 ((eq type 'procedure)
7814 nil)
7815 ((eq type 'function)
7816 (cond
7817 ((equal idlwave-function-completion-adds-paren nil) nil)
7818 ((or (equal idlwave-function-completion-adds-paren t)
7819 (equal idlwave-function-completion-adds-paren 1))
7820 (insert "("))
7821 ((equal idlwave-function-completion-adds-paren 2)
7822 (insert "()")
7823 (backward-char 1))
7824 (t nil)))
7825 ((eq type 'keyword)
7826 (if (and idlwave-keyword-completion-adds-equal
7827 (not slash))
7828 (progn (insert "=") t)
7829 nil)))
7830
7831 ;; Restore the pre-completion window configuration if this is safe.
7832
7833 (if (or (eq verify 'force) ; force
7834 (and
7835 (get-buffer-window "*Completions*") ; visible
7836 (idlwave-local-value 'idlwave-completion-p
7837 "*Completions*") ; cib-buffer
7838 (eq (marker-buffer idlwave-completion-mark)
7839 (current-buffer)) ; buffer OK
7840 (equal (marker-position idlwave-completion-mark)
7841 verify))) ; pos OK
7842 (idlwave-restore-wconf-after-completion))
7843 (move-marker idlwave-completion-mark nil)
7844 (setq idlwave-before-completion-wconf nil))
7845
7846 (defun idlwave-mouse-context-help (ev &optional arg)
7847 "Call `idlwave-context-help' on the clicked location."
7848 (interactive "eP")
7849 (mouse-set-point ev)
7850 (idlwave-context-help arg))
7851
7852 (defvar idlwave-last-context-help-pos nil)
7853 (defun idlwave-context-help (&optional arg)
7854 "Display IDL Online Help on context.
7855 If point is on a keyword, help for that keyword will be shown. If
7856 point is on a routine name or in the argument list of a routine, help
7857 for that routine will be displayed. Works for system routines and
7858 keywords, it pulls up text help. For other routines and keywords,
7859 visits the source file, finding help in the header (if
7860 `idlwave-help-source-try-header' is non-nil) or the routine definition
7861 itself."
7862 (interactive "P")
7863 (idlwave-do-context-help arg))
7864
7865 (defun idlwave-mouse-completion-help (ev)
7866 "Display online help about the completion at point."
7867 (interactive "eP")
7868 ;; Restore last-command for next command, to make
7869 ;; scrolling/canceling of completions work.
7870 (setq this-command last-command)
7871 (idlwave-do-mouse-completion-help ev))
7872
7873 (defun idlwave-routine-info (&optional arg external)
7874 "Display a routines calling sequence and list of keywords.
7875 When point is on the name a function or procedure, or in the argument
7876 list of a function or procedure, this command displays a help buffer with
7877 the information. When called with prefix arg, enforce class query.
7878
7879 When point is on an object operator `->', display the class stored in
7880 this arrow, if any (see `idlwave-store-inquired-class'). With a prefix
7881 arg, the class property is cleared out."
7882
7883 (interactive "P")
7884 (idlwave-routines)
7885 (if (string-match "->" (buffer-substring
7886 (max (point-min) (1- (point)))
7887 (min (+ 2 (point)) (point-max))))
7888 ;; Cursor is on an arrow
7889 (if (get-text-property (point) 'idlwave-class)
7890 ;; arrow has class property
7891 (if arg
7892 ;; Remove property
7893 (save-excursion
7894 (backward-char 1)
7895 (when (looking-at ".?\\(->\\)")
7896 (remove-text-properties (match-beginning 1) (match-end 1)
7897 '(idlwave-class nil face nil))
7898 (message "Class property removed from arrow")))
7899 ;; Echo class property
7900 (message "Arrow has text property identifying object to be class %s"
7901 (get-text-property (point) 'idlwave-class)))
7902 ;; No property found
7903 (message "Arrow has no class text property"))
7904
7905 ;; Not on an arrow...
7906 (let* ((idlwave-query-class nil)
7907 (idlwave-force-class-query (equal arg '(4)))
7908 (module (idlwave-what-module)))
7909 (if (car module)
7910 (apply 'idlwave-display-calling-sequence
7911 (idlwave-fix-module-if-obj_new module))
7912 (error "Don't know which calling sequence to show")))))
7913
7914 (defun idlwave-resolve (&optional arg)
7915 "Call RESOLVE_ROUTINE on the module name at point.
7916 Like `idlwave-routine-info', this looks for a routine call at point.
7917 After confirmation in the minibuffer, it will use the shell to issue
7918 a RESOLVE call for this routine, to attempt to make it defined and its
7919 routine info available for IDLWAVE. If the routine is a method call,
7920 both `class__method' and `class__define' will be tried.
7921 With ARG, enforce query for the class of object methods."
7922 (interactive "P")
7923 (let* ((idlwave-query-class nil)
7924 (idlwave-force-class-query (equal arg '(4)))
7925 (module (idlwave-what-module))
7926 (name (idlwave-make-full-name (nth 2 module) (car module)))
7927 (type (if (eq (nth 1 module) 'pro) "pro" "function"))
7928 (resolve (read-string "Resolve: " (format "%s %s" type name)))
7929 (kwd "")
7930 class)
7931 (if (string-match "\\(pro\\|function\\)[ \t]+\\(\\(.*\\)::\\)?\\(.*\\)"
7932 resolve)
7933 (setq type (match-string 1 resolve)
7934 class (if (match-beginning 2)
7935 (match-string 3 resolve)
7936 nil)
7937 name (match-string 4 resolve)))
7938 (if (string= (downcase type) "function")
7939 (setq kwd ",/is_function"))
7940
7941 (cond
7942 ((null class)
7943 (idlwave-shell-send-command
7944 (format "resolve_routine,'%s'%s" (downcase name) kwd)
7945 'idlwave-update-routine-info
7946 nil t))
7947 (t
7948 (idlwave-shell-send-command
7949 (format "resolve_routine,'%s__define'%s" (downcase class) kwd)
7950 (list 'idlwave-shell-send-command
7951 (format "resolve_routine,'%s__%s'%s"
7952 (downcase class) (downcase name) kwd)
7953 '(idlwave-update-routine-info)
7954 nil t))))))
7955
7956 (defun idlwave-find-module-this-file ()
7957 (interactive)
7958 (idlwave-find-module '(4)))
7959
7960 (defun idlwave-find-module (&optional arg)
7961 "Find the source code of an IDL module.
7962 Works for modules for which IDLWAVE has routine info available.
7963 The function offers as default the module name `idlwave-routine-info'
7964 would use. With ARG limit to this buffer. With two prefix ARG's
7965 force class query for object methods."
7966 (interactive "P")
7967 (let* ((idlwave-query-class nil)
7968 (idlwave-force-class-query (equal arg '(16)))
7969 (this-buffer (equal arg '(4)))
7970 (module (idlwave-fix-module-if-obj_new (idlwave-what-module)))
7971 (default (if module
7972 (concat (idlwave-make-full-name
7973 (nth 2 module) (car module))
7974 (if (eq (nth 1 module) 'pro) "<p>" "<f>"))
7975 "none"))
7976 (list
7977 (idlwave-uniquify
7978 (delq nil
7979 (mapcar (lambda (x)
7980 (if (eq 'system (car-safe (nth 3 x)))
7981 ;; Take out system routines with no source.
7982 nil
7983 (list
7984 (concat (idlwave-make-full-name
7985 (nth 2 x) (car x))
7986 (if (eq (nth 1 x) 'pro) "<p>" "<f>")))))
7987 (if this-buffer
7988 (idlwave-save-buffer-update)
7989 (idlwave-routines))))))
7990 (name (idlwave-completing-read
7991 (if (or (not this-buffer)
7992 (assoc default list))
7993 (format "Module (Default %s): " default)
7994 (format "Module in this file: "))
7995 list))
7996 type class)
7997 (if (string-match "\\`\\s-*\\'" name)
7998 ;; Nothing, use the default.
7999 (setq name default))
8000 (if (string-match "<[fp]>" name)
8001 (setq type (substring name -2 -1)
8002 name (substring name 0 -3)))
8003 (if (string-match "\\(.*\\)::\\(.*\\)" name)
8004 (setq class (match-string 1 name)
8005 name (match-string 2 name)))
8006 (setq name (idlwave-sintern-routine-or-method name class)
8007 class (idlwave-sintern-class class)
8008 type (cond ((equal type "f") 'fun)
8009 ((equal type "p") 'pro)
8010 (t t)))
8011 (idlwave-do-find-module name type class nil this-buffer)))
8012
8013 (defun idlwave-do-find-module (name type class
8014 &optional force-source this-buffer)
8015 (let ((name1 (idlwave-make-full-name class name))
8016 source buf1 entry
8017 (buf (current-buffer))
8018 (pos (point))
8019 file name2)
8020 (setq entry (idlwave-best-rinfo-assq name type class (idlwave-routines)
8021 'WITH-FILE)
8022 source (or force-source (nth 3 entry))
8023 name2 (if (nth 2 entry)
8024 (idlwave-make-full-name (nth 2 entry) name)
8025 name1))
8026 (if source
8027 (setq file (idlwave-routine-source-file source)))
8028 (unless file ; Try to find it on the path.
8029 (setq file
8030 (idlwave-expand-lib-file-name
8031 (if class
8032 (format "%s__define.pro" (downcase class))
8033 (format "%s.pro" (downcase name))))))
8034 (cond
8035 ((or (null name) (equal name ""))
8036 (error "Abort"))
8037 ((eq (car source) 'system)
8038 (error "Source code for system routine %s is not available"
8039 name2))
8040 ((or (not file) (not (file-regular-p file)))
8041 (error "Source code for routine %s is not available"
8042 name2))
8043 (t
8044 (when (not this-buffer)
8045 (setq buf1
8046 (idlwave-find-file-noselect file 'find))
8047 (pop-to-buffer buf1 t))
8048 (goto-char (point-max))
8049 (let ((case-fold-search t))
8050 (if (re-search-backward
8051 (concat "^[ \t]*\\<"
8052 (cond ((eq type 'fun) "function")
8053 ((eq type 'pro) "pro")
8054 (t "\\(pro\\|function\\)"))
8055 "\\>[ \t]+"
8056 (regexp-quote (downcase name2))
8057 "[^a-zA-Z0-9_$]")
8058 nil t)
8059 (goto-char (match-beginning 0))
8060 (pop-to-buffer buf)
8061 (goto-char pos)
8062 (error "Could not find routine %s" name2)))))))
8063
8064 (defun idlwave-what-module ()
8065 "Return a default module for stuff near point.
8066 Used by `idlwave-routine-info' and `idlwave-find-module'."
8067 (idlwave-routines)
8068 (if (let ((case-fold-search t))
8069 (save-excursion
8070 (idlwave-beginning-of-statement)
8071 (looking-at "[ \t]*\\(pro\\|function\\)[ \t]+\\(\\([a-zA-Z0-9_$]+\\)::\\)?\\([a-zA-Z0-9$_]+\\)\\([, \t\n]\\|$\\)")))
8072 ;; This is a function or procedure definition statement
8073 ;; We return the defined routine as module.
8074 (list
8075 (idlwave-sintern-routine-or-method (match-string-no-properties 4)
8076 (match-string-no-properties 2))
8077 (if (equal (downcase (match-string 1)) "pro") 'pro 'fun)
8078 (idlwave-sintern-class (match-string 3)))
8079
8080 ;; Not a definition statement - analyze precise position.
8081 (let* ((where (idlwave-where))
8082 (cw (nth 2 where))
8083 (pro (car (nth 0 where)))
8084 (func (car (nth 1 where)))
8085 (this-word (idlwave-this-word "a-zA-Z0-9$_"))
8086 (next-char (save-excursion (skip-chars-forward "a-zA-Z0-9$_")
8087 (following-char)))
8088 )
8089 (cond
8090 ((and (eq cw 'procedure)
8091 (not (equal this-word "")))
8092 (setq this-word (idlwave-sintern-routine-or-method
8093 this-word (nth 2 (nth 3 where))))
8094 (list this-word 'pro
8095 (idlwave-determine-class
8096 (cons this-word (cdr (nth 3 where)))
8097 'pro)))
8098 ((and (eq cw 'function)
8099 (not (equal this-word ""))
8100 (or (eq next-char ?\() ; exclude arrays, vars.
8101 (looking-at "[a-zA-Z0-9_]*[ \t]*(")))
8102 (setq this-word (idlwave-sintern-routine-or-method
8103 this-word (nth 2 (nth 3 where))))
8104 (list this-word 'fun
8105 (idlwave-determine-class
8106 (cons this-word (cdr (nth 3 where)))
8107 'fun)))
8108 ((and (memq cw '(function-keyword procedure-keyword))
8109 (not (equal this-word ""))
8110 (eq next-char ?\()) ; A function!
8111 (setq this-word (idlwave-sintern-routine this-word))
8112 (list this-word 'fun nil))
8113 (func
8114 (list func 'fun (idlwave-determine-class (nth 1 where) 'fun)))
8115 (pro
8116 (list pro 'pro (idlwave-determine-class (nth 0 where) 'pro)))
8117 (t nil)))))
8118
8119 (defun idlwave-what-module-find-class ()
8120 "Call `idlwave-what-module' and find the inherited class if necessary."
8121 (let* ((module (idlwave-what-module))
8122 (class (nth 2 module)))
8123 (if (and (= (length module) 3)
8124 (stringp class))
8125 (list (car module)
8126 (nth 1 module)
8127 (apply 'idlwave-find-inherited-class module))
8128 module)))
8129
8130 (defun idlwave-find-inherited-class (name type class)
8131 "Find the class which defines TYPE NAME and is CLASS or inherited by CLASS."
8132 (let ((entry (idlwave-best-rinfo-assoc name type class (idlwave-routines))))
8133 (if entry
8134 (nth 2 entry)
8135 class)))
8136
8137 (defun idlwave-fix-module-if-obj_new (module)
8138 "Check if MODULE points to obj_new.
8139 If yes, and if the cursor is in the keyword region, change to the
8140 appropriate Init method."
8141 (let* ((name (car module))
8142 (pos (point))
8143 (case-fold-search t)
8144 string)
8145 (if (and (stringp name)
8146 (equal (downcase name) "obj_new")
8147 (save-excursion
8148 (idlwave-beginning-of-statement)
8149 (setq string (buffer-substring (point) pos))
8150 (string-match "obj_new([^'\"]*['\"]\\([a-zA-Z0-9_]+\\)"
8151 string)))
8152 (let ((name "Init")
8153 (class (match-string 1 string)))
8154 (setq module (list (idlwave-sintern-method "Init")
8155 'fun
8156 (idlwave-sintern-class class)))))
8157 module))
8158
8159 (defun idlwave-fix-keywords (name type class keywords
8160 &optional super-classes system)
8161 "Update a list of keywords.
8162 Translate OBJ_NEW, adding all super-class keywords, or all keywords
8163 from all classes if CLASS equals t. If SYSTEM is non-nil, don't
8164 demand _EXTRA in the keyword list."
8165 (let ((case-fold-search t))
8166
8167 ;; If this is the OBJ_NEW function, try to figure out the class and use
8168 ;; the keywords from the corresponding INIT method.
8169 (if (and (equal (upcase name) "OBJ_NEW")
8170 (derived-mode-p 'idlwave-mode 'idlwave-shell-mode))
8171 (let* ((bos (save-excursion (idlwave-beginning-of-statement) (point)))
8172 (string (buffer-substring bos (point)))
8173 (case-fold-search t)
8174 class)
8175 (and (string-match "obj_new([^'\"]*['\"]\\([a-zA-Z0-9_]+\\)"
8176 string)
8177 (setq class (idlwave-sintern-class (match-string 1 string)))
8178 (setq idlwave-current-obj_new-class class)
8179 (setq keywords
8180 (append keywords
8181 (idlwave-entry-keywords
8182 (idlwave-rinfo-assq
8183 (idlwave-sintern-method "INIT")
8184 'fun
8185 class
8186 (idlwave-routines)) 'do-link))))))
8187
8188 ;; If the class is `t', combine all keywords of all methods NAME
8189 (when (eq class t)
8190 (mapc (lambda (entry)
8191 (and
8192 (nth 2 entry) ; non-nil class
8193 (eq (nth 1 entry) type) ; correct type
8194 (setq keywords
8195 (append keywords
8196 (idlwave-entry-keywords entry 'do-link)))))
8197 (idlwave-all-assq name (idlwave-routines)))
8198 (setq keywords (idlwave-uniquify keywords)))
8199
8200 ;; If we have inheritance, add all keywords from superclasses, if
8201 ;; the user indicated that method in `idlwave-keyword-class-inheritance'
8202 (when (and
8203 super-classes
8204 idlwave-keyword-class-inheritance
8205 (stringp class)
8206 (or
8207 system
8208 (assq (idlwave-sintern-keyword "_extra") keywords)
8209 (assq (idlwave-sintern-keyword "_ref_extra") keywords))
8210 ;; Check if one of the keyword-class regexps matches the name
8211 (let ((regexps idlwave-keyword-class-inheritance) re)
8212 (catch 'exit
8213 (while (setq re (pop regexps))
8214 (if (string-match re name) (throw 'exit t))))))
8215
8216 (loop for entry in (idlwave-routines) do
8217 (and (nth 2 entry) ; non-nil class
8218 (memq (nth 2 entry) super-classes) ; an inherited class
8219 (eq (nth 1 entry) type) ; correct type
8220 (eq (car entry) name) ; correct name
8221 (mapc (lambda (k) (add-to-list 'keywords k))
8222 (idlwave-entry-keywords entry 'do-link))))
8223 (setq keywords (idlwave-uniquify keywords)))
8224
8225 ;; Return the final list
8226 keywords))
8227
8228 (defun idlwave-expand-keyword (keyword module)
8229 "Expand KEYWORD to one of the valid keyword parameters of MODULE.
8230 KEYWORD may be an exact match or an abbreviation of a keyword.
8231 If the match is exact, KEYWORD itself is returned, even if there may be other
8232 keywords of which KEYWORD is an abbreviation. This is necessary because some
8233 system routines have keywords which are prefixes of other keywords.
8234 If KEYWORD is an abbreviation of several keywords, a list of all possible
8235 completions is returned.
8236 If the abbreviation was unique, the correct keyword is returned.
8237 If it cannot be a keyword, the function return nil.
8238 If we do not know about MODULE, just return KEYWORD literally."
8239 (let* ((name (car module))
8240 (type (nth 1 module))
8241 (class (nth 2 module))
8242 (kwd (idlwave-sintern-keyword keyword))
8243 (entry (idlwave-best-rinfo-assoc name type class (idlwave-routines)))
8244 (kwd-alist (idlwave-entry-keywords entry))
8245 (extra (or (assq (idlwave-sintern-keyword "_EXTRA") kwd-alist)
8246 (assq (idlwave-sintern-keyword "_REF_EXTRA") kwd-alist)))
8247 (completion-ignore-case t)
8248 candidates)
8249 (cond ((assq kwd kwd-alist)
8250 kwd)
8251 ((setq candidates (all-completions kwd kwd-alist))
8252 (if (= (length candidates) 1)
8253 (car candidates)
8254 candidates))
8255 ((and entry extra)
8256 ;; Inheritance may cause this keyword to be correct
8257 keyword)
8258 (entry
8259 ;; We do know the function, which does not have the keyword.
8260 nil)
8261 (t
8262 ;; We do not know the function, so this just might be a correct
8263 ;; keyword - return it as it is.
8264 keyword))))
8265
8266 (defvar idlwave-rinfo-mouse-map
8267 (let ((map (make-sparse-keymap)))
8268 (define-key map
8269 (if (featurep 'xemacs) [button2] [mouse-2])
8270 'idlwave-mouse-active-rinfo)
8271 (define-key map
8272 (if (featurep 'xemacs) [(shift button2)] [(shift mouse-2)])
8273 'idlwave-mouse-active-rinfo-shift)
8274 (define-key map
8275 (if (featurep 'xemacs) [button3] [mouse-3])
8276 'idlwave-mouse-active-rinfo-right)
8277 (define-key map " " 'idlwave-active-rinfo-space)
8278 (define-key map "q" 'idlwave-quit-help)
8279 map))
8280
8281 (defvar idlwave-rinfo-map
8282 (let ((map (make-sparse-keymap)))
8283 (define-key map "q" 'idlwave-quit-help)
8284 map))
8285
8286 (defvar idlwave-popup-source nil)
8287 (defvar idlwave-rinfo-marker (make-marker))
8288
8289 (defun idlwave-quit-help ()
8290 (interactive)
8291 (let ((ri-window (get-buffer-window "*Help*"))
8292 (olh-window (get-buffer-window "*IDLWAVE Help*")))
8293 (when (and olh-window
8294 (fboundp 'idlwave-help-quit))
8295 (select-window olh-window)
8296 (idlwave-help-quit))
8297 (when (window-live-p ri-window)
8298 (delete-window ri-window))))
8299
8300 (defun idlwave-display-calling-sequence (name type class
8301 &optional initial-class)
8302 ;; Display the calling sequence of module NAME, type TYPE in class CLASS.
8303 (let* ((initial-class (or initial-class class))
8304 (entry (or (idlwave-best-rinfo-assq name type class
8305 (idlwave-routines))
8306 (idlwave-rinfo-assq name type class
8307 idlwave-unresolved-routines)))
8308 (name (or (car entry) name))
8309 (class (or (nth 2 entry) class))
8310 (superclasses (idlwave-all-class-inherits initial-class))
8311 (twins (idlwave-routine-twins entry))
8312 (dtwins (idlwave-study-twins twins))
8313 (all dtwins)
8314 (system (eq (car (nth 3 entry)) 'system))
8315 (calling-seq (nth 4 entry))
8316 (keywords (idlwave-entry-keywords entry 'do-link))
8317 (html-file (car (nth 5 entry)))
8318 (help-echo-kwd
8319 "Button2: Insert KEYWORD (SHIFT=`/KEYWORD') | Button3: Online Help ")
8320 (help-echo-use
8321 "Button2/3: Online Help")
8322 (help-echo-src
8323 "Button2: Jump to source and back | Button3: Source in Help window.")
8324 (help-echo-class
8325 "Button2: Display info about same method in superclass")
8326 (col 0)
8327 (data (list name type class (current-buffer) nil initial-class))
8328 (km-prop (if (featurep 'xemacs) 'keymap 'local-map))
8329 (face 'idlwave-help-link)
8330 beg props win cnt total)
8331 ;; Fix keywords, but don't add chained super-classes, since these
8332 ;; are shown separately for that super-class
8333 (setq keywords (idlwave-fix-keywords name type class keywords))
8334 (cond
8335 ((null entry)
8336 (error "No %s %s known %s" type name
8337 (if initial-class (concat "in class " initial-class) "")))
8338 ((or (null name) (equal name ""))
8339 (error "No function or procedure call at point"))
8340 ((null calling-seq)
8341 (error "Calling sequence of %s %s not available" type name))
8342 (t
8343 (move-marker idlwave-rinfo-marker (point))
8344 (with-current-buffer (get-buffer-create "*Help*")
8345 (use-local-map idlwave-rinfo-map)
8346 (setq buffer-read-only nil)
8347 (erase-buffer)
8348 (set (make-local-variable 'idlwave-popup-source) nil)
8349 (set (make-local-variable 'idlwave-current-obj_new-class)
8350 idlwave-current-obj_new-class)
8351 (when superclasses
8352 (setq props (list 'mouse-face 'highlight
8353 km-prop idlwave-rinfo-mouse-map
8354 'help-echo help-echo-class
8355 'data (cons 'class data)))
8356 (let ((classes (cons initial-class superclasses)) c)
8357 (insert "Classes: ")
8358 (while (setq c (pop classes))
8359 (insert " ")
8360 (setq beg (point))
8361 (insert c)
8362 (if (equal (downcase c) (downcase class))
8363 (add-text-properties beg (point) (list 'face 'bold))
8364 ;; If Method exists in a different class link it
8365 (if (idlwave-rinfo-assq name type c (idlwave-routines))
8366 (add-text-properties beg (point) props))))
8367 (insert "\n")))
8368 (setq props (list 'mouse-face 'highlight
8369 km-prop idlwave-rinfo-mouse-map
8370 'help-echo help-echo-use
8371 'data (cons 'usage data)))
8372 (if html-file (setq props (append (list 'face face 'link html-file)
8373 props)))
8374 (insert "Usage: ")
8375 (setq beg (point))
8376 (insert (if class
8377 (format calling-seq class name class name class name)
8378 (format calling-seq name name name name))
8379 "\n")
8380 (add-text-properties beg (point) props)
8381
8382 (insert "Keywords:")
8383 (if (null keywords)
8384 (insert " No keywords accepted.")
8385 (setq col 9)
8386 (mapc
8387 (lambda (x)
8388 (if (>= (+ col 1 (length (car x)))
8389 (window-width))
8390 (progn
8391 (insert "\n ")
8392 (setq col 9)))
8393 (insert " ")
8394 (setq beg (point)
8395 ;; Relevant keywords already have link property attached
8396 props (list 'mouse-face 'highlight
8397 km-prop idlwave-rinfo-mouse-map
8398 'data (cons 'keyword data)
8399 'help-echo help-echo-kwd
8400 'keyword (car x)))
8401 (if system (setq props (append (list 'face face) props)))
8402 (insert (car x))
8403 (add-text-properties beg (point) props)
8404 (setq col (+ col 1 (length (car x)))))
8405 keywords))
8406
8407 (setq cnt 1 total (length all))
8408 ;; Here entry is (key file (list of type-conses))
8409 (while (setq entry (pop all))
8410 (setq props (list 'mouse-face 'highlight
8411 km-prop idlwave-rinfo-mouse-map
8412 'help-echo help-echo-src
8413 'source (list (car (car (nth 2 entry))) ;type
8414 (nth 1 entry)
8415 nil
8416 (cdr (car (nth 2 entry))))
8417 'data (cons 'source data)))
8418 (idlwave-insert-source-location
8419 (format "\n%-8s %s"
8420 (if (equal cnt 1)
8421 (if (> total 1) "Sources:" "Source:")
8422 "")
8423 (if (> total 1) "- " ""))
8424 entry props)
8425 (incf cnt)
8426 (when (and all (> cnt idlwave-rinfo-max-source-lines))
8427 ;; No more source lines, please
8428 (insert (format
8429 "\n Source information truncated to %d entries."
8430 idlwave-rinfo-max-source-lines))
8431 (setq all nil)))
8432 (goto-char (point-min))
8433 (setq buffer-read-only t))
8434 (display-buffer "*Help*")
8435 (if (and (setq win (get-buffer-window "*Help*"))
8436 idlwave-resize-routine-help-window)
8437 (progn
8438 (let ((ww (selected-window)))
8439 (unwind-protect
8440 (progn
8441 (select-window win)
8442 (enlarge-window (- (/ (frame-height) 2)
8443 (window-height)))
8444 (shrink-window-if-larger-than-buffer))
8445 (select-window ww)))))))))
8446
8447 (defun idlwave-insert-source-location (prefix entry &optional file-props)
8448 "Insert a source location into the routine info buffer.
8449 Start line with PREFIX. If a file name is inserted, add FILE-PROPS
8450 to it."
8451 (let* ((key (car entry))
8452 (file (nth 1 entry))
8453 (types (nth 2 entry))
8454 (shell-flag (assq 'compiled types))
8455 (buffer-flag (assq 'buffer types))
8456 (user-flag (assq 'user types))
8457 (lib-flag (assq 'lib types))
8458 (ndupl (or (and buffer-flag (idlwave-count-memq 'buffer types))
8459 (and user-flag (idlwave-count-memq 'user types))
8460 (and lib-flag (idlwave-count-memq 'lib types))
8461 1))
8462 (doflags t)
8463 beg special)
8464
8465 (insert prefix)
8466
8467 (cond
8468 ((eq key 'system)
8469 (setq doflags nil)
8470 (insert "System "))
8471
8472 ((eq key 'builtin)
8473 (setq doflags nil)
8474 (insert "Builtin "))
8475
8476 ((and (not file) shell-flag)
8477 (insert "Unresolved"))
8478
8479 ((null file)
8480 (insert "ERROR"))
8481
8482 ((idlwave-syslib-p file)
8483 (if (string-match "obsolete" (file-name-directory file))
8484 (insert "Obsolete ")
8485 (insert "SystemLib ")))
8486
8487 ;; New special syntax: taken directly from routine-info for
8488 ;; library catalog routines
8489 ((setq special (or (cdr lib-flag) (cdr user-flag)))
8490 (insert (format "%-10s" special)))
8491
8492 ;; Old special syntax: a matching regexp
8493 ((setq special (idlwave-special-lib-test file))
8494 (insert (format "%-10s" special)))
8495
8496 ;; Catch-all with file
8497 ((idlwave-lib-p file) (insert "Library "))
8498
8499 ;; Sanity catch all
8500 (t (insert "Other ")))
8501
8502 (when doflags
8503 (insert (concat
8504 " ["
8505 (if lib-flag "L" "-")
8506 (if user-flag "C" "-")
8507 (if shell-flag "S" "-")
8508 (if buffer-flag "B" "-")
8509 "] ")))
8510 (when (> ndupl 1)
8511 (setq beg (point))
8512 (insert (format "(%dx) " ndupl))
8513 (add-text-properties beg (point) (list 'face 'bold)))
8514 (when (and file (not (equal file "")))
8515 (setq beg (point))
8516 (insert (apply 'abbreviate-file-name
8517 (if (featurep 'xemacs) (list file t) (list file))))
8518 (if file-props
8519 (add-text-properties beg (point) file-props)))))
8520
8521 (defun idlwave-special-lib-test (file)
8522 "Check the path of FILE against the regexps which define special libs.
8523 Return the name of the special lib if there is a match."
8524 (let ((alist idlwave-special-lib-alist)
8525 entry rtn)
8526 (cond
8527 ((stringp file)
8528 (while (setq entry (pop alist))
8529 (if (string-match (car entry) file)
8530 (setq rtn (cdr entry)
8531 alist nil)))
8532 rtn)
8533 (t nil))))
8534
8535 (defun idlwave-mouse-active-rinfo-right (ev)
8536 (interactive "e")
8537 (idlwave-mouse-active-rinfo ev 'right))
8538
8539 (defun idlwave-mouse-active-rinfo-shift (ev)
8540 (interactive "e")
8541 (idlwave-mouse-active-rinfo ev nil 'shift))
8542
8543 (defun idlwave-active-rinfo-space ()
8544 (interactive)
8545 (idlwave-mouse-active-rinfo nil 'right))
8546
8547 (defun idlwave-mouse-active-rinfo (ev &optional right shift)
8548 "Do the mouse actions in the routine info buffer.
8549 Optional args RIGHT and SHIFT indicate, if mouse-3 was used, and if SHIFT
8550 was pressed."
8551 (interactive "e")
8552 (if ev (mouse-set-point ev))
8553 (let (data id name type class buf bufwin source link keyword
8554 word initial-class)
8555 (setq data (get-text-property (point) 'data)
8556 source (get-text-property (point) 'source)
8557 keyword (get-text-property (point) 'keyword)
8558 link (get-text-property (point) 'link)
8559 id (car data)
8560 name (nth 1 data) type (nth 2 data) class (nth 3 data)
8561 buf (nth 4 data)
8562 initial-class (nth 6 data)
8563 word (idlwave-this-word)
8564 bufwin (get-buffer-window buf t))
8565
8566 (cond ((eq id 'class) ; Switch class being displayed
8567 (if (window-live-p bufwin) (select-window bufwin))
8568 (idlwave-display-calling-sequence
8569 (idlwave-sintern-method name)
8570 type (idlwave-sintern-class word)
8571 initial-class))
8572 ((eq id 'usage) ; Online help on this routine
8573 (idlwave-online-help link name type class))
8574 ((eq id 'source) ; Source in help or buffer
8575 (if right ; In help
8576 (let ((idlwave-extra-help-function 'idlwave-help-with-source)
8577 (idlwave-help-source-try-header nil)
8578 ;; Fake idlwave-routines so help will find the right entry
8579 (idlwave-routines
8580 (list (list name type class source ""))))
8581 (idlwave-help-get-special-help name type class nil))
8582 ;; Otherwise just pop to the source
8583 (setq idlwave-popup-source (not idlwave-popup-source))
8584 (if idlwave-popup-source
8585 (condition-case err
8586 (idlwave-do-find-module name type class source)
8587 (error
8588 (setq idlwave-popup-source nil)
8589 (if (window-live-p bufwin) (select-window bufwin))
8590 (error (nth 1 err))))
8591 (if bufwin
8592 (select-window bufwin)
8593 (pop-to-buffer buf))
8594 (goto-char (marker-position idlwave-rinfo-marker)))))
8595 ((eq id 'keyword)
8596 (if right
8597 (idlwave-online-help link name type class keyword)
8598 (idlwave-rinfo-insert-keyword keyword buf shift))))))
8599
8600 (defun idlwave-rinfo-insert-keyword (keyword buffer &optional shift)
8601 "Insert KEYWORD in BUFFER. Make sure buffer is displayed in a window."
8602 (let ((bwin (get-buffer-window buffer)))
8603 (if idlwave-complete-empty-string-as-lower-case
8604 (setq keyword (downcase keyword)))
8605 (if bwin
8606 (select-window bwin)
8607 (pop-to-buffer buffer)
8608 (setq bwin (get-buffer-window buffer)))
8609 (if (eq (preceding-char) ?/)
8610 (insert keyword)
8611 (unless (save-excursion
8612 (re-search-backward
8613 "[(,][ \t]*\\(\\$[ \t]*\\(;.*\\)?\n\\)?[ \t]*\\="
8614 (min (- (point) 100) (point-min)) t))
8615 (insert ", "))
8616 (if shift (insert "/"))
8617 (insert keyword)
8618 (if (and (not shift)
8619 idlwave-keyword-completion-adds-equal)
8620 (insert "=")))))
8621
8622 (defun idlwave-list-buffer-load-path-shadows (&optional arg)
8623 "List the load path shadows of all routines defined in current buffer."
8624 (interactive "P")
8625 (idlwave-routines)
8626 (if (derived-mode-p 'idlwave-mode)
8627 (idlwave-list-load-path-shadows
8628 nil (idlwave-update-current-buffer-info 'save-buffer)
8629 "in current buffer")
8630 (error "Current buffer is not in idlwave-mode")))
8631
8632 (defun idlwave-list-shell-load-path-shadows (&optional arg)
8633 "List the load path shadows of all routines compiled under the shell.
8634 This is very useful for checking an IDL application. Just compile the
8635 application, do RESOLVE_ALL, and `C-c C-i' to compile all referenced
8636 routines and update IDLWAVE internal info. Then check for shadowing
8637 with this command."
8638 (interactive "P")
8639 (cond
8640 ((or (not (fboundp 'idlwave-shell-is-running))
8641 (not (idlwave-shell-is-running)))
8642 (error "Shell is not running"))
8643 ((null idlwave-compiled-routines)
8644 (error "No compiled routines. Maybe you need to update with `C-c C-i'"))
8645 (t
8646 (idlwave-list-load-path-shadows nil idlwave-compiled-routines
8647 "in the shell"))))
8648
8649 (defun idlwave-list-all-load-path-shadows (&optional arg)
8650 "List the load path shadows of all routines known to IDLWAVE."
8651 (interactive "P")
8652 (idlwave-list-load-path-shadows nil nil "globally"))
8653
8654 (defvar idlwave-sort-prefer-buffer-info t
8655 "Internal variable used to influence `idlwave-routine-twin-compare'.")
8656
8657 (defun idlwave-list-load-path-shadows (arg &optional special-routines loc)
8658 "List the routines which are defined multiple times.
8659 Search the information IDLWAVE has about IDL routines for multiple
8660 definitions.
8661 When SPECIAL-ROUTINES in non-nil, only look for shadows of these routines.
8662
8663 When IDL hits a routine call which is not defined, it will search on
8664 the load path in order to find a definition. The output of this command
8665 can be used to detect possible name clashes during this process."
8666 (idlwave-routines) ; Make sure everything is loaded.
8667 (unless (or idlwave-user-catalog-routines idlwave-library-catalog-routines)
8668 (or (y-or-n-p
8669 "You don't have any user or library catalogs. Continue anyway? ")
8670 (error "Abort")))
8671 (let* ((routines (append idlwave-system-routines
8672 idlwave-compiled-routines
8673 idlwave-library-catalog-routines
8674 idlwave-user-catalog-routines
8675 idlwave-buffer-routines
8676 nil))
8677 (km-prop (if (featurep 'xemacs) 'keymap 'local-map))
8678 (keymap (make-sparse-keymap))
8679 (props (list 'mouse-face 'highlight
8680 km-prop keymap
8681 'help-echo "Mouse2: Find source"))
8682 (nroutines (length (or special-routines routines)))
8683 (step (/ nroutines 100))
8684 (n 0)
8685 (cnt 0)
8686 (idlwave-sort-prefer-buffer-info nil)
8687 routine twins dtwins twin done props1 lroutines)
8688
8689 (if special-routines
8690 ;; Just looking for shadows of a few special routines
8691 (setq lroutines routines
8692 routines special-routines))
8693
8694 (message "Sorting routines...")
8695 (setq routines (sort routines
8696 (lambda (a b)
8697 (string< (downcase (idlwave-make-full-name
8698 (nth 2 a) (car a)))
8699 (downcase (idlwave-make-full-name
8700 (nth 2 b) (car b)))))))
8701 (message "Sorting routines...done")
8702
8703 (define-key keymap (if (featurep 'xemacs) [(button2)] [(mouse-2)])
8704 (lambda (ev)
8705 (interactive "e")
8706 (mouse-set-point ev)
8707 (apply 'idlwave-do-find-module
8708 (get-text-property (point) 'find-args))))
8709 (define-key keymap [(return)]
8710 (lambda ()
8711 (interactive)
8712 (apply 'idlwave-do-find-module
8713 (get-text-property (point) 'find-args))))
8714 (message "Compiling list...( 0%%)")
8715 (with-current-buffer (get-buffer-create "*Shadows*")
8716 (setq buffer-read-only nil)
8717 (erase-buffer)
8718 (while (setq routine (pop routines))
8719 (if (= (mod (setq n (1+ n)) step) 0)
8720 (message "Compiling list...(%2d%%)" (/ (* n 100) nroutines)))
8721
8722 ;; Get a list of all twins
8723 (setq twins (idlwave-routine-twins routine (or lroutines routines)))
8724 (if (memq routine done)
8725 (setq dtwins nil)
8726 (setq dtwins (idlwave-study-twins twins)))
8727 ;; Mark all twins as dealt with
8728 (setq done (append twins done))
8729 (when (or (> (length dtwins) 1)
8730 (> (idlwave-count-memq 'lib (nth 2 (car dtwins))) 1)
8731 (> (idlwave-count-memq 'user (nth 2 (car dtwins))) 1)
8732 (> (idlwave-count-memq 'buffer (nth 2 (car dtwins))) 1))
8733 (incf cnt)
8734 (insert (format "\n%s%s"
8735 (idlwave-make-full-name (nth 2 routine)
8736 (car routine))
8737 (if (eq (nth 1 routine) 'fun) "()" "")))
8738 (while (setq twin (pop dtwins))
8739 (setq props1 (append (list 'find-args
8740 (list (nth 0 routine)
8741 (nth 1 routine)
8742 (nth 2 routine)))
8743 props))
8744 (idlwave-insert-source-location "\n - " twin props1))))
8745 (goto-char (point-min))
8746 (setq buffer-read-only t))
8747 (setq loc (or loc ""))
8748 (if (> cnt 0)
8749 (progn
8750 (display-buffer (get-buffer "*Shadows*"))
8751 (message "%d case%s of shadowing found %s"
8752 cnt (if (= cnt 1) "" "s") loc))
8753 (message "No shadowing conflicts found %s" loc))))
8754
8755 (defun idlwave-print-source (routine)
8756 (let* ((source (nth 3 routine))
8757 (stype (car source))
8758 (sfile (idlwave-routine-source-file source)))
8759 (if (idlwave-syslib-p sfile) (setq stype 'syslib))
8760 (if (and (eq stype 'compiled)
8761 (or (not (stringp sfile))
8762 (not (string-match "\\S-" sfile))))
8763 (setq stype 'unresolved))
8764 (princ (format " %-10s %s\n"
8765 stype
8766 (if sfile sfile "No source code available")))))
8767
8768 (defun idlwave-routine-twins (entry &optional list)
8769 "Return all twin entries of ENTRY in LIST.
8770 LIST defaults to `idlwave-routines'.
8771 Twin entries are those which have the same name, type, and class.
8772 ENTRY will also be returned, as the first item of this list."
8773 (let* ((name (car entry))
8774 (type (nth 1 entry))
8775 (class (nth 2 entry))
8776 (candidates (idlwave-all-assq name (or list (idlwave-routines))))
8777 twins candidate)
8778 (while (setq candidate (pop candidates))
8779 (if (and (not (eq candidate entry))
8780 (eq type (nth 1 candidate))
8781 (eq class (nth 2 candidate)))
8782 (push candidate twins)))
8783 (if (setq candidate (idlwave-rinfo-assq name type class
8784 idlwave-unresolved-routines))
8785 (push candidate twins))
8786 (cons entry (nreverse twins))))
8787
8788 (defun idlwave-study-twins (entries)
8789 "Return dangerous twins of first entry in ENTRIES.
8790 Dangerous twins are routines with same name, but in different files on
8791 the load path. If a file is in the system library and has an entry in
8792 the `idlwave-system-routines' list, we omit the latter as
8793 non-dangerous because many IDL routines are implemented as library
8794 routines, and may have been scanned."
8795 (let* ((entry (car entries))
8796 (idlwave-twin-name (car entry)) ;
8797 (type (nth 1 entry)) ; Must be bound for
8798 (idlwave-twin-class (nth 2 entry)) ; idlwave-routine-twin-compare
8799 (cnt 0)
8800 source type type-cons file alist syslibp key)
8801 (while (setq entry (pop entries))
8802 (incf cnt)
8803 (setq source (nth 3 entry)
8804 type (car source)
8805 type-cons (cons type (nth 3 source))
8806 file (idlwave-routine-source-file source))
8807
8808 ;; Make KEY to index entry properly
8809 (setq key (cond ((eq type 'system) type)
8810 (file (file-truename file))
8811 (t 'unresolved)))
8812
8813 ;; Check for an entry in the system library
8814 (if (and file
8815 (not syslibp)
8816 (idlwave-syslib-p file))
8817 (setq syslibp t))
8818
8819 ;; If there's more than one matching entry for the same file, just
8820 ;; append the type-cons to the type list.
8821 (if (setq entry (assoc key alist))
8822 (push type-cons (nth 2 entry))
8823 (push (list key file (list type-cons)) alist)))
8824
8825 (setq alist (nreverse alist))
8826
8827 (when syslibp
8828 ;; File is in system *library* - remove any 'system entry
8829 (setq alist (delq (assq 'system alist) alist)))
8830
8831 ;; If 'system remains and we've scanned the syslib, it's a builtin
8832 ;; (rather than a !DIR/lib/.pro file bundled as source).
8833 (when (and (idlwave-syslib-scanned-p)
8834 (setq entry (assoc 'system alist)))
8835 (setcar entry 'builtin))
8836 (sort alist 'idlwave-routine-twin-compare)))
8837
8838 ;; FIXME: Dynamically scoped vars need to use the `idlwave-' prefix.
8839 ;; (defvar type)
8840 (defmacro idlwave-xor (a b)
8841 `(and (or ,a ,b)
8842 (not (and ,a ,b))))
8843
8844 (defun idlwave-routine-entry-compare (a b)
8845 "Compare two routine info entries for sorting.
8846 This is the general case. It first compares class, names, and type.
8847 If it turns out that A and B are twins (same name, class, and type),
8848 calls another routine which compares twins on the basis of their file
8849 names and path locations."
8850 (let ((name (car a)) (type (nth 1 a)) (class (nth 2 a)))
8851 (cond
8852 ((not (equal (idlwave-downcase-safe class)
8853 (idlwave-downcase-safe (nth 2 b))))
8854 ;; Class decides
8855 (cond ((null (nth 2 b)) nil)
8856 ((null class) t)
8857 (t (string< (downcase class) (downcase (nth 2 b))))))
8858 ((not (equal (downcase name) (downcase (car b))))
8859 ;; Name decides
8860 (string< (downcase name) (downcase (car b))))
8861 ((not (eq type (nth 1 b)))
8862 ;; Type decides
8863 (< (if (eq type 'fun) 1 0) (if (eq (nth 1 b) 'fun) 1 0)))
8864 (t
8865 ;; A and B are twins - so the decision is more complicated.
8866 ;; Call twin-compare with the proper arguments.
8867 (idlwave-routine-entry-compare-twins a b)))))
8868
8869 (defun idlwave-routine-entry-compare-twins (a b)
8870 "Compare two routine entries, under the assumption that they are twins.
8871 This basically calls `idlwave-routine-twin-compare' with the correct args."
8872 (let* ((idlwave-twin-name (car a))
8873 (type (nth 1 a))
8874 (idlwave-twin-class (nth 2 a)) ; used in idlwave-routine-twin-compare
8875 (asrc (nth 3 a))
8876 (atype (car asrc))
8877 (bsrc (nth 3 b))
8878 (btype (car bsrc))
8879 (afile (idlwave-routine-source-file asrc))
8880 (bfile (idlwave-routine-source-file bsrc)))
8881 (idlwave-routine-twin-compare
8882 (if (stringp afile)
8883 (list (file-truename afile) afile (list atype))
8884 (list atype afile (list atype)))
8885 (if (stringp bfile)
8886 (list (file-truename bfile) bfile (list btype))
8887 (list btype bfile (list btype))))))
8888
8889 ;; Bound in idlwave-study-twins,idlwave-routine-entry-compare-twins.
8890 (defvar idlwave-twin-class)
8891 (defvar idlwave-twin-name)
8892
8893 (defun idlwave-routine-twin-compare (a b)
8894 "Compare two routine twin entries for sorting.
8895 In here, A and B are not normal routine info entries, but special
8896 lists (KEY FILENAME (TYPES...)).
8897 This expects NAME TYPE IDLWAVE-TWIN-CLASS to be bound to the right values."
8898 (let* (;; Dis-assemble entries
8899 (akey (car a)) (bkey (car b))
8900 (afile (nth 1 a)) (bfile (nth 1 b))
8901 (atypes (nth 2 a)) (btypes (nth 2 b))
8902 ;; System routines?
8903 (asysp (memq akey '(builtin system)))
8904 (bsysp (memq bkey '(builtin system)))
8905 ;; Compiled routines?
8906 (acompp (memq 'compiled atypes))
8907 (bcompp (memq 'compiled btypes))
8908 ;; Unresolved?
8909 (aunresp (or (eq akey 'unresolved)
8910 (and acompp (not afile))))
8911 (bunresp (or (eq bkey 'unresolved)
8912 (and bcompp (not bfile))))
8913 ;; Buffer info available?
8914 (abufp (memq 'buffer atypes))
8915 (bbufp (memq 'buffer btypes))
8916 ;; On search path?
8917 (tpath-alist (idlwave-true-path-alist))
8918 (apathp (and (stringp akey)
8919 (assoc (file-name-directory akey) tpath-alist)))
8920 (bpathp (and (stringp bkey)
8921 (assoc (file-name-directory bkey) tpath-alist)))
8922 ;; How early on search path? High number means early since we
8923 ;; measure the tail of the path list
8924 (anpath (length (memq apathp tpath-alist)))
8925 (bnpath (length (memq bpathp tpath-alist)))
8926 ;; Look at file names
8927 (aname (if (stringp afile) (downcase (file-name-nondirectory afile)) ""))
8928 (bname (if (stringp bfile) (downcase (file-name-nondirectory bfile)) ""))
8929 (fname-re (if idlwave-twin-class
8930 (format "\\`%s__\\(%s\\|define\\)\\.pro\\'"
8931 (regexp-quote (downcase idlwave-twin-class))
8932 (regexp-quote (downcase idlwave-twin-name)))
8933 (format "\\`%s\\.pro" (regexp-quote (downcase idlwave-twin-name)))))
8934 ;; Is file name derived from the routine name?
8935 ;; Method file or class definition file?
8936 (anamep (string-match fname-re aname))
8937 (adefp (and idlwave-twin-class anamep
8938 (string= "define" (match-string 1 aname))))
8939 (bnamep (string-match fname-re bname))
8940 (bdefp (and idlwave-twin-class bnamep
8941 (string= "define" (match-string 1 bname)))))
8942
8943 ;; Now: follow JD's ideas about sorting. Looks really simple now,
8944 ;; doesn't it? The difficult stuff is hidden above...
8945 (cond
8946 ((idlwave-xor asysp bsysp) asysp) ; System entries first
8947 ((idlwave-xor aunresp bunresp) bunresp) ; Unresolved last
8948 ((and idlwave-sort-prefer-buffer-info
8949 (idlwave-xor abufp bbufp)) abufp) ; Buffers before non-buffers
8950 ((idlwave-xor acompp bcompp) acompp) ; Compiled entries
8951 ((idlwave-xor apathp bpathp) apathp) ; Library before non-library
8952 ((idlwave-xor anamep bnamep) anamep) ; Correct file names first
8953 ((and idlwave-twin-class anamep bnamep ; both file names match ->
8954 (idlwave-xor adefp bdefp)) bdefp) ; __define after __method
8955 ((> anpath bnpath) t) ; Who is first on path?
8956 (t nil)))) ; Default
8957
8958 (defun idlwave-routine-source-file (source)
8959 (if (nth 2 source)
8960 (expand-file-name (nth 1 source) (nth 2 source))
8961 (nth 1 source)))
8962
8963 (defun idlwave-downcase-safe (string)
8964 "Downcase if string, else return unchanged."
8965 (if (stringp string)
8966 (downcase string)
8967 string))
8968
8969 (defun idlwave-count-eq (elt list)
8970 "How often is ELT in LIST?"
8971 (length (delq nil (mapcar (lambda (x) (eq x elt)) list))))
8972
8973 (defun idlwave-count-memq (elt alist)
8974 "How often is ELT a key in ALIST?"
8975 (length (delq nil (mapcar (lambda (x) (eq (car x) elt)) alist))))
8976
8977 (defun idlwave-syslib-p (file)
8978 "Non-nil if FILE is in the system library."
8979 (let* ((true-syslib (file-name-as-directory
8980 (file-truename
8981 (expand-file-name "lib" (idlwave-sys-dir)))))
8982 (true-file (file-truename file)))
8983 (string-match (concat "^" (regexp-quote true-syslib)) true-file)))
8984
8985 (defun idlwave-lib-p (file)
8986 "Non-nil if FILE is in the library."
8987 (let ((true-dir (file-name-directory (file-truename file))))
8988 (assoc true-dir (idlwave-true-path-alist))))
8989
8990 (defun idlwave-path-alist-add-flag (list-entry flag)
8991 "Add a flag to the path list entry, if not set."
8992 (let ((flags (cdr list-entry)))
8993 (add-to-list 'flags flag)
8994 (setcdr list-entry flags)))
8995
8996 (defun idlwave-path-alist-remove-flag (list-entry flag)
8997 "Remove a flag to the path list entry, if set."
8998 (let ((flags (delq flag (cdr list-entry))))
8999 (setcdr list-entry flags)))
9000
9001 (defun idlwave-true-path-alist ()
9002 "Return `idlwave-path-alist' alist with true-names.
9003 Info is cached, but relies on the functions setting `idlwave-path-alist'
9004 to reset the variable `idlwave-true-path-alist' to nil."
9005 (or idlwave-true-path-alist
9006 (setq idlwave-true-path-alist
9007 (mapcar (lambda(x) (cons
9008 (file-name-as-directory
9009 (file-truename
9010 (directory-file-name
9011 (car x))))
9012 (cdr x)))
9013 idlwave-path-alist))))
9014
9015 (defun idlwave-syslib-scanned-p ()
9016 "Non-nil if the system lib file !DIR/lib has been scanned."
9017 (let* ((true-syslib (file-name-as-directory
9018 (file-truename
9019 (expand-file-name "lib" (idlwave-sys-dir))))))
9020 (cdr (assoc true-syslib (idlwave-true-path-alist)))))
9021
9022 ;; ----------------------------------------------------------------------------
9023 ;;
9024 ;; Online Help display
9025
9026
9027 ;; ----------------------------------------------------------------------------
9028 ;;
9029 ;; Additions for use with imenu.el and func-menu.el
9030 ;; (pop-up a list of IDL units in the current file).
9031 ;;
9032
9033 (defun idlwave-prev-index-position ()
9034 "Search for the previous procedure or function.
9035 Return nil if not found. For use with imenu.el."
9036 (save-match-data
9037 (cond
9038 ((idlwave-find-key "\\<\\(pro\\|function\\)\\>" -1 'nomark))
9039 ;; ((idlwave-find-key idlwave-begin-unit-reg 1 'nomark)
9040 (t nil))))
9041
9042 (defun idlwave-unit-name ()
9043 "Return the unit name.
9044 Assumes that point is at the beginning of the unit as found by
9045 `idlwave-prev-index-position'."
9046 (forward-sexp 2)
9047 (forward-sexp -1)
9048 (let ((begin (point)))
9049 (re-search-forward
9050 "[a-zA-Z_][a-zA-Z0-9$_]+\\(::[a-zA-Z_][a-zA-Z0-9$_]+\\)?")
9051 (if (fboundp 'buffer-substring-no-properties)
9052 (buffer-substring-no-properties begin (point))
9053 (buffer-substring begin (point)))))
9054
9055 (defalias 'idlwave-function-menu
9056 (condition-case nil
9057 (progn
9058 (require 'func-menu)
9059 'function-menu)
9060 (error (condition-case nil
9061 (progn
9062 (require 'imenu)
9063 'imenu)
9064 (error nil)))))
9065
9066 ;; Here we hack func-menu.el in order to support this new mode.
9067 ;; The latest versions of func-menu.el already have this stuff in, so
9068 ;; we hack only if it is not already there.
9069 (when (fboundp 'eval-after-load)
9070 (eval-after-load "func-menu"
9071 '(progn
9072 (or (assq 'idlwave-mode fume-function-name-regexp-alist)
9073 (not (boundp 'fume-function-name-regexp-idl)) ; avoid problems
9074 (setq fume-function-name-regexp-alist
9075 (cons '(idlwave-mode . fume-function-name-regexp-idl)
9076 fume-function-name-regexp-alist)))
9077 (or (assq 'idlwave-mode fume-find-function-name-method-alist)
9078 (not (fboundp 'fume-find-next-idl-function-name)) ; avoid problems
9079 (setq fume-find-function-name-method-alist
9080 (cons '(idlwave-mode . fume-find-next-idl-function-name)
9081 fume-find-function-name-method-alist))))))
9082
9083 (defun idlwave-edit-in-idlde ()
9084 "Edit the current file in IDL Development environment."
9085 (interactive)
9086 (start-process "idldeclient" nil
9087 idlwave-shell-explicit-file-name "-c" "-e"
9088 (buffer-file-name)))
9089
9090 (defvar idlwave-help-use-assistant)
9091 (defun idlwave-launch-idlhelp ()
9092 "Start the IDLhelp application."
9093 (interactive)
9094 (if idlwave-help-use-assistant
9095 (idlwave-help-assistant-raise)
9096 (start-process "idlhelp" nil idlwave-help-application)))
9097
9098 ;; Menus - using easymenu.el
9099 (defvar idlwave-mode-menu-def
9100 `("IDLWAVE"
9101 ["PRO/FUNC menu" idlwave-function-menu t]
9102 ("Motion"
9103 ["Subprogram Start" idlwave-beginning-of-subprogram t]
9104 ["Subprogram End" idlwave-end-of-subprogram t]
9105 ["Block Start" idlwave-beginning-of-block t]
9106 ["Block End" idlwave-end-of-block t]
9107 ["Up Block" idlwave-backward-up-block t]
9108 ["Down Block" idlwave-down-block t]
9109 ["Skip Block Backward" idlwave-backward-block t]
9110 ["Skip Block Forward" idlwave-forward-block t])
9111 ("Mark"
9112 ["Subprogram" idlwave-mark-subprogram t]
9113 ["Block" idlwave-mark-block t]
9114 ["Header" idlwave-mark-doclib t])
9115 ("Format"
9116 ["Indent Entire Statement" idlwave-indent-statement
9117 :active t :keys "C-u \\[indent-for-tab-command]" ]
9118 ["Indent Subprogram" idlwave-indent-subprogram t]
9119 ["(Un)Comment Region" idlwave-toggle-comment-region t]
9120 ["Continue/Split line" idlwave-split-line t]
9121 "--"
9122 ["Toggle Auto Fill" idlwave-auto-fill-mode :style toggle
9123 :selected (symbol-value idlwave-fill-function)])
9124 ("Templates"
9125 ["Procedure" idlwave-procedure t]
9126 ["Function" idlwave-function t]
9127 ["Doc Header" idlwave-doc-header t]
9128 ["Log" idlwave-doc-modification t]
9129 "--"
9130 ["Case" idlwave-case t]
9131 ["For" idlwave-for t]
9132 ["Repeat" idlwave-repeat t]
9133 ["While" idlwave-while t]
9134 "--"
9135 ["Close Block" idlwave-close-block t])
9136 ("Completion"
9137 ["Complete" idlwave-complete t]
9138 ("Complete Specific"
9139 ["1 Procedure Name" (idlwave-complete 'procedure) t]
9140 ["2 Procedure Keyword" (idlwave-complete 'procedure-keyword) t]
9141 "--"
9142 ["3 Function Name" (idlwave-complete 'function) t]
9143 ["4 Function Keyword" (idlwave-complete 'function-keyword) t]
9144 "--"
9145 ["5 Procedure Method Name" (idlwave-complete 'procedure-method) t]
9146 ["6 Procedure Method Keyword" (idlwave-complete 'procedure-method-keyword) t]
9147 "--"
9148 ["7 Function Method Name" (idlwave-complete 'function-method) t]
9149 ["8 Function Method Keyword" (idlwave-complete 'function-method-keyword) t]
9150 "--"
9151 ["9 Class Name" idlwave-complete-class t]))
9152 ("Routine Info"
9153 ["Show Routine Info" idlwave-routine-info t]
9154 ["Online Context Help" idlwave-context-help t]
9155 "--"
9156 ["Find Routine Source" idlwave-find-module t]
9157 ["Resolve Routine" idlwave-resolve (featurep 'idlw-shell)]
9158 "--"
9159 ["Update Routine Info" idlwave-update-routine-info t]
9160 ["Rescan XML Help Catalog" idlwave-convert-xml-system-routine-info t]
9161 "--"
9162 "IDL User Catalog"
9163 ["Select Catalog Directories" (idlwave-create-user-catalog-file nil) t]
9164 ["Scan Directories" (idlwave-update-routine-info '(16))
9165 (and idlwave-path-alist (not idlwave-catalog-process))]
9166 ["Scan Directories &" (idlwave-update-routine-info '(64))
9167 (and idlwave-path-alist (not idlwave-catalog-process))]
9168 "--"
9169 "Routine Shadows"
9170 ["Check Current Buffer" idlwave-list-buffer-load-path-shadows t]
9171 ["Check Compiled Routines" idlwave-list-shell-load-path-shadows t]
9172 ["Check Everything" idlwave-list-all-load-path-shadows t])
9173 ("Misc"
9174 ["Kill auto-created buffers" idlwave-kill-autoloaded-buffers t]
9175 "--"
9176 ["Insert TAB character" idlwave-hard-tab t])
9177 "--"
9178 ("External"
9179 ["Start IDL shell" idlwave-shell t]
9180 ["Edit file in IDLDE" idlwave-edit-in-idlde t]
9181 ["Launch IDL Help" idlwave-launch-idlhelp t])
9182 "--"
9183 ("Customize"
9184 ["Browse IDLWAVE Group" idlwave-customize t]
9185 "--"
9186 ["Build Full Customize Menu" idlwave-create-customize-menu
9187 (fboundp 'customize-menu-create)])
9188 ("Documentation"
9189 ["Describe Mode" describe-mode t]
9190 ["Abbreviation List" idlwave-list-abbrevs t]
9191 "--"
9192 ["Commentary in idlwave.el" idlwave-show-commentary t]
9193 ["Commentary in idlw-shell.el" idlwave-shell-show-commentary t]
9194 "--"
9195 ["Info" idlwave-info t]
9196 "--"
9197 ["Help with Topic" idlwave-help-assistant-help-with-topic
9198 idlwave-help-use-assistant]
9199 ["Launch IDL Help" idlwave-launch-idlhelp t])))
9200
9201 (defvar idlwave-mode-debug-menu-def
9202 '("Debug"
9203 ["Start IDL shell" idlwave-shell t]
9204 ["Save and .RUN buffer" idlwave-shell-save-and-run
9205 (and (boundp 'idlwave-shell-automatic-start)
9206 idlwave-shell-automatic-start)]))
9207
9208 (if (or (featurep 'easymenu) (load "easymenu" t))
9209 (progn
9210 (easy-menu-define idlwave-mode-menu idlwave-mode-map
9211 "IDL and WAVE CL editing menu"
9212 idlwave-mode-menu-def)
9213 (easy-menu-define idlwave-mode-debug-menu idlwave-mode-map
9214 "IDL and WAVE CL editing menu"
9215 idlwave-mode-debug-menu-def)))
9216
9217 (defun idlwave-customize ()
9218 "Call the customize function with `idlwave' as argument."
9219 (interactive)
9220 ;; Try to load the code for the shell, so that we can customize it
9221 ;; as well.
9222 (or (featurep 'idlw-shell)
9223 (load "idlw-shell" t))
9224 (customize-browse 'idlwave))
9225
9226 (defun idlwave-create-customize-menu ()
9227 "Create a full customization menu for IDLWAVE, insert it into the menu."
9228 (interactive)
9229 (if (fboundp 'customize-menu-create)
9230 (progn
9231 ;; Try to load the code for the shell, so that we can customize it
9232 ;; as well.
9233 (or (featurep 'idlw-shell)
9234 (load "idlw-shell" t))
9235 (easy-menu-change
9236 '("IDLWAVE") "Customize"
9237 `(["Browse IDLWAVE group" idlwave-customize t]
9238 "--"
9239 ,(customize-menu-create 'idlwave)
9240 ["Set" Custom-set t]
9241 ["Save" Custom-save t]
9242 ["Reset to Current" Custom-reset-current t]
9243 ["Reset to Saved" Custom-reset-saved t]
9244 ["Reset to Standard Settings" Custom-reset-standard t]))
9245 (message "\"IDLWAVE\"-menu now contains full customization menu"))
9246 (error "Cannot expand menu (outdated version of cus-edit.el)")))
9247
9248 (defun idlwave-show-commentary ()
9249 "Use the finder to view the file documentation from `idlwave.el'."
9250 (interactive)
9251 (finder-commentary "idlwave.el"))
9252
9253 (defun idlwave-shell-show-commentary ()
9254 "Use the finder to view the file documentation from `idlw-shell.el'."
9255 (interactive)
9256 (finder-commentary "idlw-shell.el"))
9257
9258 (defun idlwave-info ()
9259 "Read documentation for IDLWAVE in the info system."
9260 (interactive)
9261 (info "idlwave"))
9262
9263 (defun idlwave-list-abbrevs (arg)
9264 "Show the code abbreviations define in IDLWAVE mode.
9265 This lists all abbrevs where the replacement text differs from the input text.
9266 These are the ones the users want to learn to speed up their writing.
9267
9268 The function does *not* list abbrevs which replace a word with itself
9269 to call a hook. These hooks are used to change the case of words or
9270 to blink the matching `begin', and the user does not need to know them.
9271
9272 With arg, list all abbrevs with the corresponding hook.
9273
9274 This function was written since `list-abbrevs' looks terrible for IDLWAVE mode."
9275
9276 (interactive "P")
9277 (let ((table (symbol-value 'idlwave-mode-abbrev-table))
9278 abbrevs
9279 str rpl func fmt (len-str 0) (len-rpl 0))
9280 (mapatoms
9281 (lambda (sym)
9282 (if (symbol-value sym)
9283 (progn
9284 (setq str (symbol-name sym)
9285 rpl (symbol-value sym)
9286 func (symbol-function sym))
9287 (if arg
9288 (setq func (prin1-to-string func))
9289 (if (and (listp func) (stringp (nth 2 func)))
9290 (setq rpl (concat "EVAL: " (nth 2 func))
9291 func "")
9292 (setq func "")))
9293 (if (or arg (not (string= rpl str)))
9294 (progn
9295 (setq len-str (max len-str (length str)))
9296 (setq len-rpl (max len-rpl (length rpl)))
9297 (setq abbrevs (cons (list str rpl func) abbrevs)))))))
9298 table)
9299 ;; sort the list
9300 (setq abbrevs (sort abbrevs (lambda (a b) (string< (car a) (car b)))))
9301 ;; Make the format
9302 (setq fmt (format "%%-%ds %%-%ds %%s\n" len-str len-rpl))
9303 (with-output-to-temp-buffer "*Help*"
9304 (if arg
9305 (progn
9306 (princ "Abbreviations and Actions in IDLWAVE-Mode\n")
9307 (princ "=========================================\n\n")
9308 (princ (format fmt "KEY" "REPLACE" "HOOK"))
9309 (princ (format fmt "---" "-------" "----")))
9310 (princ "Code Abbreviations and Templates in IDLWAVE-Mode\n")
9311 (princ "================================================\n\n")
9312 (princ (format fmt "KEY" "ACTION" ""))
9313 (princ (format fmt "---" "------" "")))
9314 (dolist (list abbrevs)
9315 (setq str (car list)
9316 rpl (nth 1 list)
9317 func (nth 2 list))
9318 (princ (format fmt str rpl func)))))
9319 ;; Make sure each abbreviation uses only one display line
9320 (with-current-buffer "*Help*"
9321 (setq truncate-lines t)))
9322
9323 ;; Add .pro files to speedbar for support, if it's loaded
9324 (eval-after-load "speedbar" '(speedbar-add-supported-extension ".pro"))
9325
9326 ;; Set an idle timer to load the routine info.
9327 ;; Will only work on systems which support this.
9328 (or idlwave-routines (idlwave-start-load-rinfo-timer))
9329
9330 ;; Run the hook
9331 (run-hooks 'idlwave-load-hook)
9332
9333 (provide 'idlwave)
9334
9335 ;;; idlwave.el ends here