Merged in changes from CVS trunk.
[bpt/emacs.git] / lisp / progmodes / idlwave.el
1 ;; idlwave.el --- IDL editing mode for GNU Emacs
2 ;; Copyright (c) 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation
3
4 ;; Authors: J.D. Smith <jdsmith@as.arizona.edu>
5 ;; Carsten Dominik <dominik@astro.uva.nl>
6 ;; Chris Chase <chase@att.com>
7 ;; Maintainer: J.D. Smith <jdsmith@as.arizona.edu>
8 ;; Version: 5.5
9 ;; Keywords: languages
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
27
28 ;;; Commentary:
29
30 ;; In the remotely distant past, based on pascal.el, though bears
31 ;; little resemblance to it now.
32 ;;
33 ;; Incorporates many ideas, such as abbrevs, action routines, and
34 ;; continuation line indenting, from wave.el.
35 ;; wave.el original written by Lubos Pochman, Precision Visuals, Boulder.
36 ;;
37 ;; See the mode description ("C-h m" in idlwave-mode or "C-h f idlwave-mode")
38 ;; for features, key bindings, and info.
39 ;; Also, Info format documentation is available with `M-x idlwave-info'
40 ;;
41 ;; New versions of IDLWAVE, documentation, and more information
42 ;; available from:
43 ;; http://idlwave.org
44 ;;
45 ;; INSTALLATION
46 ;; ============
47 ;;
48 ;; Follow the instructions in the INSTALL file of the distribution.
49 ;; In short, put this file on your load path and add the following
50 ;; lines to your .emacs file:
51 ;;
52 ;; (autoload 'idlwave-mode "idlwave" "IDLWAVE Mode" t)
53 ;; (autoload 'idlwave-shell "idlw-shell" "IDLWAVE Shell" t)
54 ;; (setq auto-mode-alist (cons '("\\.pro\\'" . idlwave-mode) auto-mode-alist))
55 ;;
56 ;;
57 ;; SOURCE
58 ;; ======
59 ;;
60 ;; The newest version of this file is available from the maintainer's
61 ;; Webpage:
62 ;;
63 ;; http://idlwave.org
64 ;;
65 ;; DOCUMENTATION
66 ;; =============
67 ;;
68 ;; IDLWAVE is documented online in info format. A printable version
69 ;; of the documentation is available from the maintainers webpage (see
70 ;; SOURCE).
71 ;;
72 ;;
73 ;; ACKNOWLEDGMENTS
74 ;; ===============
75 ;;
76 ;; Thanks to the following people for their contributions and comments:
77 ;;
78 ;; Ulrik Dickow <dickow_at_nbi.dk>
79 ;; Eric E. Dors <edors_at_lanl.gov>
80 ;; Stein Vidar H. Haugan <s.v.h.haugan_at_astro.uio.no>
81 ;; David Huenemoerder <dph_at_space.mit.edu>
82 ;; Kevin Ivory <Kevin.Ivory_at_linmpi.mpg.de>
83 ;; Dick Jackson <dick_at_d-jackson.com>
84 ;; Xuyong Liu <liu_at_stsci.edu>
85 ;; Simon Marshall <Simon.Marshall_at_esrin.esa.it>
86 ;; Laurent Mugnier <mugnier_at_onera.fr>
87 ;; Lubos Pochman <lubos_at_rsinc.com>
88 ;; Bob Portmann <portmann_at_al.noaa.gov>
89 ;; Patrick M. Ryan <pat_at_jaameri.gsfc.nasa.gov>
90 ;; Marty Ryba <ryba_at_ll.mit.edu>
91 ;; Paul Sorenson <aardvark62_at_msn.com>
92 ;; Phil Sterne <sterne_at_dublin.llnl.gov>
93 ;; Phil Williams <williams_at_irc.chmcc.org>
94 ;;
95 ;; CUSTOMIZATION:
96 ;; =============
97 ;;
98 ;; IDLWAVE has extensive customize support; to learn about the
99 ;; variables which control the mode's behavior, use `M-x
100 ;; idlwave-customize'.
101 ;;
102 ;; You can set your own preferred values with Customize, or with Lisp
103 ;; code in .emacs. For an example of what to put into .emacs, check
104 ;; the TexInfo documentation or see a complete .emacs available at the
105 ;; website.
106 ;;
107 ;; KNOWN PROBLEMS:
108 ;; ==============
109 ;;
110 ;; IDLWAVE support for the IDL-derived PV-WAVE CL language of Visual
111 ;; Numerics, Inc. is growing less and less complete as the two
112 ;; languages grow increasingly apart. The mode probably shouldn't
113 ;; even have "WAVE" in it's title, but it's catchy, and was required
114 ;; to avoid conflict with the CORBA idl.el mode. Caveat WAVEor.
115 ;;
116 ;; Moving the point backwards in conjunction with abbrev expansion
117 ;; does not work as I would like it, but this is a problem with
118 ;; emacs abbrev expansion done by the self-insert-command. It ends
119 ;; up inserting the character that expanded the abbrev after moving
120 ;; point backward, e.g., "\cl" expanded with a space becomes
121 ;; "LONG( )" with point before the close paren. This is solved by
122 ;; using a temporary function in `post-command-hook' - not pretty,
123 ;; but it works.
124 ;;
125 ;; Tabs and spaces are treated equally as whitespace when filling a
126 ;; comment paragraph. To accomplish this, tabs are permanently
127 ;; replaced by spaces in the text surrounding the paragraph, which
128 ;; may be an undesirable side-effect. Replacing tabs with spaces is
129 ;; limited to comments only and occurs only when a comment
130 ;; paragraph is filled via `idlwave-fill-paragraph'.
131 ;;
132 ;; Muti-statement lines (using "&") on block begin and end lines can
133 ;; ruin the formatting. For example, multiple end statements on a
134 ;; line: endif & endif. Using "&" outside of block begin/end lines
135 ;; should be okay.
136 ;;
137 ;; Determining the expression at point for printing and other
138 ;; examination commands is somewhat rough: currently only fairly
139 ;; simple entities are found. You can always drag-select or examine
140 ;; a pre-selected region.
141 ;;
142 ;; When forcing completion of method keywords, the initial
143 ;; query for a method has multiple entries for some methods. Would
144 ;; be too difficult to fix this hardly used case.
145 ;;
146 \f
147 ;;; Code:
148
149
150 (eval-when-compile (require 'cl))
151 (require 'idlw-help)
152
153 ;; For XEmacs
154 (unless (fboundp 'line-beginning-position)
155 (defalias 'line-beginning-position 'point-at-bol))
156 (unless (fboundp 'line-end-position)
157 (defalias 'line-end-position 'point-at-eol))
158 (unless (fboundp 'char-valid-p)
159 (defalias 'char-valid-p 'characterp))
160
161 (eval-and-compile
162 ;; Kludge to allow `defcustom' for Emacs 19.
163 (condition-case () (require 'custom) (error nil))
164 (if (and (featurep 'custom) (fboundp 'custom-declare-variable))
165 nil ;; We've got what we needed
166 ;; We have the old or no custom-library, hack around it!
167 (defmacro defgroup (&rest args) nil)
168 (defmacro defcustom (var value doc &rest args)
169 `(defvar ,var ,value ,doc))))
170
171 (defgroup idlwave nil
172 "Major mode for editing IDL .pro files"
173 :tag "IDLWAVE"
174 :link '(url-link :tag "Home Page"
175 "http://idlwave.org")
176 :link '(emacs-commentary-link :tag "Commentary in idlw-shell.el"
177 "idlw-shell.el")
178 :link '(emacs-commentary-link :tag "Commentary in idlwave.el" "idlwave.el")
179 :link '(custom-manual "(idlwave)Top")
180 :prefix "idlwave"
181 :group 'languages)
182
183
184 ;;; Variables for indentation behavior ---------------------------------------
185
186 (defgroup idlwave-code-formatting nil
187 "Indentation and formatting options for IDLWAVE mode."
188 :group 'idlwave)
189
190 (defcustom idlwave-main-block-indent 0
191 "*Extra indentation for the main block of code.
192 That is the block between the FUNCTION/PRO statement and the END
193 statement for that program unit."
194 :group 'idlwave-code-formatting
195 :type 'integer)
196
197 (defcustom idlwave-block-indent 4
198 "*Extra indentation applied to block lines.
199 If you change this, you probably also want to change `idlwave-end-offset'."
200 :group 'idlwave-code-formatting
201 :type 'integer)
202
203 (defcustom idlwave-end-offset -4
204 "*Extra indentation applied to block END lines.
205 A value equal to negative `idlwave-block-indent' will make END lines
206 line up with the block BEGIN lines."
207 :group 'idlwave-code-formatting
208 :type 'integer)
209
210 (defcustom idlwave-continuation-indent 2
211 "*Extra indentation applied to continuation lines.
212 This extra offset applies to the first of a set of continuation lines.
213 The following lines receive the same indentation as the first."
214 :group 'idlwave-code-formatting
215 :type 'integer)
216
217 (defcustom idlwave-max-extra-continuation-indent 20
218 "*Maximum additional indentation for special continuation indent.
219 Several special indentations are tried to help line up continuation
220 lines in routine calls or definitions, other statements with
221 parentheses, or assignment statements. This variable specifies a
222 maximum amount by which this special indentation can exceed the
223 standard continuation indentation, otherwise defaulting to a fixed
224 offset. Set to 0 to effectively disable all special continuation
225 indentation, or to a large number (like 100) to enable it in all
226 cases. See also `idlwave-indent-to-open-paren', which can override
227 this variable."
228 :group 'idlwave-code-formatting
229 :type 'integer)
230
231 (defcustom idlwave-indent-to-open-paren t
232 "*Non-nil means, indent continuation lines to innermost open
233 parenthesis. This indentation occurs even if otherwise disallowed by
234 `idlwave-max-extra-continuation-indent'. Matching parens and the
235 interleaving args are lined up. Example:
236
237 x = function_a(function_b(function_c( a, b, [1,2,3, $
238 4,5,6 $
239 ], $
240 c, d $
241 )))
242
243 When this variable is nil, paren alignment may still occur, based on
244 the value of `max-extra-continuation-indent', which, if zero, would
245 yield:
246
247 x = function_a(function_b(function_c( a, b, [1,2,3, $
248 4,5,6 $
249 ], $
250 c, d $
251 )))"
252 :group 'idlwave-code-formatting
253 :type 'boolean)
254
255 (defcustom idlwave-indent-parens-nested nil
256 "*Non-nil means, indent continuation lines with parens by nesting
257 lines at consecutively deeper levels."
258 :group 'idlwave-code-formatting
259 :type 'boolean)
260
261
262 (defcustom idlwave-hanging-indent t
263 "*If set non-nil then comment paragraphs are indented under the
264 hanging indent given by `idlwave-hang-indent-regexp' match in the first line
265 of the paragraph."
266 :group 'idlwave-code-formatting
267 :type 'boolean)
268
269 (defcustom idlwave-hang-indent-regexp "- "
270 "*Regular expression matching the position of the hanging indent
271 in the first line of a comment paragraph. The size of the indent
272 extends to the end of the match for the regular expression."
273 :group 'idlwave-code-formatting
274 :type 'regexp)
275
276 (defcustom idlwave-use-last-hang-indent nil
277 "*If non-nil then use last match on line for `idlwave-indent-regexp'."
278 :group 'idlwave-code-formatting
279 :type 'boolean)
280
281 (defcustom idlwave-fill-comment-line-only t
282 "*If non-nil then auto fill will only operate on comment lines."
283 :group 'idlwave-code-formatting
284 :type 'boolean)
285
286 (defcustom idlwave-auto-fill-split-string t
287 "*If non-nil then auto fill will split strings with the IDL `+' operator.
288 When the line end falls within a string, string concatenation with the
289 '+' operator will be used to distribute a long string over lines.
290 If nil and a string is split then a terminal beep and warning are issued.
291
292 This variable is ignored when `idlwave-fill-comment-line-only' is
293 non-nil, since in this case code is not auto-filled."
294 :group 'idlwave-code-formatting
295 :type 'boolean)
296
297 (defcustom idlwave-split-line-string t
298 "*If non-nil then `idlwave-split-line' will split strings with `+'.
299 When the splitting point of a line falls inside a string, split the string
300 using the `+' string concatenation operator. If nil and a string is
301 split then a terminal beep and warning are issued."
302 :group 'idlwave-code-formatting
303 :type 'boolean)
304
305 (defcustom idlwave-no-change-comment ";;;"
306 "*The indentation of a comment that starts with this regular
307 expression will not be changed. Note that the indentation of a comment
308 at the beginning of a line is never changed."
309 :group 'idlwave-code-formatting
310 :type 'string)
311
312 (defcustom idlwave-begin-line-comment nil
313 "*A comment anchored at the beginning of line.
314 A comment matching this regular expression will not have its
315 indentation changed. If nil the default is \"^;\", i.e., any line
316 beginning with a \";\". Expressions for comments at the beginning of
317 the line should begin with \"^\"."
318 :group 'idlwave-code-formatting
319 :type '(choice (const :tag "Any line beginning with `;'" nil)
320 'regexp))
321
322 (defcustom idlwave-code-comment ";;[^;]"
323 "*A comment that starts with this regular expression on a line by
324 itself is indented as if it is a part of IDL code. As a result if
325 the comment is not preceded by whitespace it is unchanged."
326 :group 'idlwave-code-formatting
327 :type 'regexp)
328
329 ;; Comments not matching any of the above will be indented as a
330 ;; right-margin comment, i.e., to a minimum of `comment-column'.
331
332 ;;; Routine Info and Completion ---------------------------------------
333
334 (defgroup idlwave-routine-info nil
335 "Routine Info options for IDLWAVE mode."
336 :group 'idlwave)
337
338 (defcustom idlwave-use-library-catalogs t
339 "*Non-nil means search the IDL path for library catalog files.
340
341 These files, named .idlwave_catalog, document routine information for
342 individual directories and libraries of IDL .pro files. Many popular
343 libraries come with catalog files by default, so leaving this on is a
344 usually a good idea.."
345 :group 'idlwave-routine-info
346 :type 'boolean)
347
348 (defcustom idlwave-init-rinfo-when-idle-after 10
349 "*Seconds of idle time before routine info is automatically initialized.
350 Initializing the routine info can take long, in particular if a large
351 library catalog is involved. When Emacs is idle for more than the number
352 of seconds specified by this variable, it starts the initialization.
353 The process is split into five steps, in order to keep possible work
354 interruption as short as possible. If one of the steps finishes, and no
355 user input has arrived in the mean time, initialization proceeds immediately
356 to the next step.
357 A good value for this variable is about 1/3 of the time initialization
358 take in you setup. So if you have a fast machine and no problems with a slow network connection, don't hesitate to set this to 2 seconds.
359 A Value of 0 means, don't initialize automatically."
360 :group 'idlwave-routine-info
361 :type 'number)
362
363 (defcustom idlwave-scan-all-buffers-for-routine-info t
364 "*Non-nil means, scan buffers for IDL programs when updating info.
365 The scanning is done by the command `idlwave-update-routine-info'.
366 The following values are allowed:
367
368 nil Don't scan any buffers.
369 t Scan all idlwave-mode buffers in the current editing session.
370 current Scan only the current buffer, but no other buffers."
371 :group 'idlwave-routine-info
372 :type '(choice
373 (const :tag "No buffer" nil)
374 (const :tag "All buffers" t)
375 (const :tag "Current buffer only" 'current)))
376
377 (defcustom idlwave-query-shell-for-routine-info t
378 "*Non-nil means query the shell for info about compiled routines.
379 Querying the shell is useful to get information about compiled modules,
380 and it is turned on by default. However, when you have a complete library
381 scan, this is not necessary."
382 :group 'idlwave-routine-info
383 :type 'boolean)
384
385 (defcustom idlwave-auto-routine-info-updates
386 '(find-file save-buffer kill-buffer compile-buffer)
387 "*Controls under what circumstances routine info is updated automatically.
388 Possible values:
389 nil Never
390 t All available
391 \(...) A list of circumstances. Allowed members are:
392 find-file Add info for new IDLWAVE buffers.
393 save-buffer Update buffer info when buffer is saved
394 kill-buffer Remove buffer info when buffer gets killed
395 compile-buffer Update shell info after `idlwave-shell-save-and...'"
396 :group 'idlwave-routine-info
397 :type '(choice
398 (const :tag "Never" nil)
399 (const :tag "As often as possible" t)
400 (set :tag "Checklist" :greedy t
401 (const :tag "When visiting a file" find-file)
402 (const :tag "When saving a buffer" save-buffer)
403 (const :tag "After a buffer was killed" kill-buffer)
404 (const :tag "After a buffer was compiled successfully, update shell info" compile-buffer))))
405
406 (defcustom idlwave-rinfo-max-source-lines 5
407 "*Maximum number of source files displayed in the Routine Info window.
408 When an integer, it is the maximum number of source files displayed.
409 t means to show all source files."
410 :group 'idlwave-routine-info
411 :type 'integer)
412
413 (defcustom idlwave-library-path nil
414 "Library path for Windows and MacOS. Not needed under Unix. When
415 selecting the directories to scan for IDL user catalog routine info,
416 IDLWAVE can, under UNIX, query the shell for the exact search path
417 \(the value of !PATH). However, under Windows and MacOS (pre-OSX),
418 the IDLWAVE shell does not work. In this case, this variable can be
419 set to specify the paths where IDLWAVE can find PRO files. The shell
420 will only be asked for a list of paths when this variable is nil. The
421 value is a list of directories. A directory preceeded by a `+' will
422 be searched recursively. If you set this variable on a UNIX system,
423 the shell will not be queried. See also `idlwave-system-directory'."
424 :group 'idlwave-routine-info
425 :type '(repeat (directory)))
426
427 (defcustom idlwave-system-directory ""
428 "The IDL system directory for Windows and MacOS. Not needed under
429 UNIX. Set this to the value of the `!DIR' system variable in IDL.
430 IDLWAVE uses this to find out which of the library routines belong to
431 the official system library. All files inside the `lib' subdirectory
432 are considered system library files - so don't install private stuff
433 in this directory. On UNIX systems, IDLWAVE queries the shell for the
434 value of `!DIR'. See also `idlwave-library-path'."
435 :group 'idlwave-routine-info
436 :type 'directory)
437
438 (defcustom idlwave-config-directory
439 (convert-standard-filename "~/.idlwave")
440 "*Directory for configuration files and user-library catalog."
441 :group 'idlwave-routine-info
442 :type 'file)
443
444 (defvar idlwave-user-catalog-file "idlusercat.el")
445 (defvar idlwave-path-file "idlpath.el")
446
447 (defvar idlwave-libinfo-file nil
448 "*Obsolete variable, no longer used.")
449
450 (defcustom idlwave-special-lib-alist nil
451 "Alist of regular expressions matching special library directories.
452 When listing routine source locations, IDLWAVE gives a short hint where
453 the file defining the routine is located. By default it lists `SystemLib'
454 for routines in the system library `!DIR/lib' and `Library' for anything
455 else. This variable can define additional types. The car of each entry
456 is a regular expression matching the file name (they normally will match
457 on the path). The cdr is the string to be used as identifier. Max 10
458 chars are allowed."
459 :group 'idlwave-routine-info
460 :type '(repeat
461 (cons regexp string)))
462
463 (defcustom idlwave-auto-write-paths t
464 "Write out path (!PATH) and system directory (!DIR) info automatically.
465 Path info is needed to locate library catalog files. If non-nil,
466 whenever the path-list changes as a result of shell-query, etc., it is
467 written to file. Otherwise, the menu option \"Write Paths\" can be
468 used to force a write."
469 :group 'idlwave-routine-info
470 :type 'boolean)
471
472 (defgroup idlwave-completion nil
473 "Completion options for IDLWAVE mode."
474 :prefix "idlwave"
475 :group 'idlwave)
476
477 (eval-and-compile
478 (defconst idlwave-tmp
479 '(choice :tag "by applying the function"
480 (const upcase)
481 (const downcase)
482 (const capitalize)
483 (const preserve)
484 (symbol :tag "Other"))))
485
486 (defcustom idlwave-completion-case '((routine . upcase)
487 (keyword . upcase)
488 (class . preserve)
489 (method . preserve))
490 "Association list setting the case of completed words.
491
492 This variable determines the case (UPPER/lower/Capitalized...) of
493 words inserted into the buffer by completion. The preferred case can
494 be specified separately for routine names, keywords, classes and
495 methods.
496 This alist should therefore have entries for `routine' (normal
497 functions and procedures, i.e. non-methods), `keyword', `class', and
498 `method'. Plausible values are
499
500 upcase upcase whole word, like `BOX_CURSOR'
501 downcase downcase whole word, like `read_ppm'
502 capitalize capitalize each part, like `Widget_Control'
503 preserve preserve case as is, like `IDLgrView'
504
505 The value can also be any Emacs Lisp function which transforms the
506 case of characters in a string.
507
508 A value of `preserve' means that the case of the completed word is
509 identical to the way it was written in the definition statement of the
510 routine. This was implemented to allow for mixed-case completion, in
511 particular of object classes and methods.
512 If a completable word is defined in multiple locations, the meaning of
513 `preserve' is not unique since the different definitions might be
514 cased differently. Therefore IDLWAVE always takes the case of the
515 *first* definition it encounters during routine info collection and
516 uses the case derived from it consistently.
517
518 Note that a lowercase-only string in the buffer will always be completed in
519 lower case (but see the variable `idlwave-completion-force-default-case').
520
521 After changing this variable, you need to either restart Emacs or press
522 `C-u C-c C-i' to update the internal lists."
523 :group 'idlwave-completion
524 :type `(repeat
525 (cons (symbol :tag "Derive completion case for")
526 ,idlwave-tmp)))
527
528 (defcustom idlwave-completion-force-default-case nil
529 "*Non-nil means, completion will always honor `idlwave-completion-case'.
530 When nil, only the completion of a mixed case or upper case string
531 will honor the default settings in `idlwave-completion-case', while
532 the completion of lower case strings will be completed entirely in
533 lower case."
534 :group 'idlwave-completion
535 :type 'boolean)
536
537 (defcustom idlwave-complete-empty-string-as-lower-case nil
538 "*Non-nil means, the empty string is considered downcase for completion.
539 The case of what is already in the buffer determines the case of completions.
540 When this variable is non-nil, the empty string is considered to be downcase.
541 Completing on the empty string then offers downcase versions of the possible
542 completions."
543 :group 'idlwave-completion
544 :type 'boolean)
545
546 (defvar idlwave-default-completion-case-is-down nil
547 "Obsolete variable. See `idlwave-complete-empty-string-as-lower-case' and
548 `idlwave-completion-case'.")
549
550 (defcustom idlwave-buffer-case-takes-precedence nil
551 "*Non-nil means, the case of tokens in buffers dominates over system stuff.
552 To make this possible, we need to re-case everything each time we update
553 the routine info from the buffers. This is slow.
554 The default is to consider the case given in the system and library files
555 first which makes updating much faster."
556 :group 'idlwave-completion
557 :type 'boolean)
558
559 (defcustom idlwave-highlight-help-links-in-completion t
560 "*Non-nil means, highlight completions for which system help is available.
561 Help can then be accessed with mouse-3.
562 This option is only effective when the online help system is installed."
563 :group 'idlwave-completion
564 :type 'boolean)
565
566 (defcustom idlwave-support-inheritance t
567 "Non-nil means, treat inheritance with completion, online help etc.
568 When nil, IDLWAVE only knows about the native methods and tags of a class,
569 not about inherited ones."
570 :group 'idlwave-routine-info
571 :type 'boolean)
572
573 (defcustom idlwave-keyword-class-inheritance '("^[gs]etproperty$" "^init$")
574 "List of regular expressions for class-driven keyword inheritance.
575 Keyword inheritance is often tied to class inheritance by \"chaining\"
576 up the class tree. While it cannot be assumed that the presence of an
577 _EXTRA or _REF_EXTRA symbol guarantees such chaining will occur, for
578 certain methods this assumption is almost always true. The methods
579 for which to assume this can be set here."
580 :group 'idlwave-routine-info
581 :type '(repeat (regexp :tag "Match method:")))
582
583
584 (defcustom idlwave-completion-show-classes 1
585 "*Number of classes to show when completing object methods and keywords.
586 When completing methods or keywords for an object with unknown class,
587 the *Completions* buffer will show the legal classes for each completion
588 like this:
589
590 MyMethod <Class1,Class2,Class3>
591
592 The value of this variable may be nil to inhibit display, or an integer to
593 indicate the maximum number of classes to display.
594
595 On XEmacs, a full list of classes will also be placed into a `help-echo'
596 property on the competion items, so that the list of classes for the current
597 item is displayed in the echo area. If the value of this variable is a
598 negative integer, the `help-echo' property will be suppressed."
599 :group 'idlwave-completion
600 :type '(choice (const :tag "Don't show" nil)
601 (integer :tag "Number of classes shown" 1)))
602
603 (defcustom idlwave-completion-fontify-classes t
604 "*Non-nil means, fontify the classes in completions buffer.
605 This makes it easier to distinguish the completion items from the extra
606 class info listed. See `idlwave-completion-show-classes'."
607 :group 'idlwave-completion
608 :type 'boolean)
609
610 (defcustom idlwave-query-class '((method-default . nil)
611 (keyword-default . nil))
612 "Association list governing specification of object classes for completion.
613
614 When IDLWAVE tries to complete object-oriented methods, it usually
615 cannot determine the class of a given object from context. In order
616 to provide the user with a correct list of methods or keywords, it
617 needs to determine the appropriate class. IDLWAVE has two ways of
618 doing this (well, three ways if you count the shell... see
619 `idlwave-shell-query-for-class'):
620
621 1. Combine the items of all available classes which contain this
622 method for the purpose of completion. So when completing a method,
623 all methods of all known classes are available, and when completing
624 a keyword, all keywords allowed for this method in any class are
625 shown. This behavior is very much like normal completion and is
626 therefore the default. It works much better than one might think -
627 only for the INIT, GETPROPERTY and SETPROPERTY the keyword lists
628 become uncomfortably long. See also
629 `idlwave-completion-show-classes'.
630
631 2. The second possibility is to ask the user on each occasion. To
632 make this less interruptive, IDLWAVE can store the class as a text
633 property on the object operator `->'. For a given object in the
634 source code, class selection will then be needed only once
635 - for example to complete the method. Keywords to the method can
636 then be completed directly, because the class is already known.
637 You will have to turn on the storage of the selected class
638 explicitly with the variable `idlwave-store-inquired-class'.
639
640 This variable allows you to configure IDLWAVE's method and
641 method-keyword completion behavior. Its value is an alist, which
642 should contain at least two elements: (method-default . VALUE) and
643 \(keyword-default . VALUE), where VALUE is either t or nil. These
644 specify if the class should be found during method and keyword
645 completion, respectively.
646
647 The alist may have additional entries specifying exceptions from the
648 keyword completion rule for specific methods, like INIT or
649 GETPROPERTY. In order to turn on class specification for the INIT
650 method, add an entry (\"INIT\" . t). The method name must be ALL-CAPS."
651 :group 'idlwave-completion
652 :type '(list
653 (cons (const method-default)
654 (boolean :tag "Determine class when completing METHODS "))
655 (cons (const keyword-default)
656 (boolean :tag "Determine class when completing KEYWORDS "))
657 (repeat
658 :tag "Exceptions to defaults"
659 :inline t
660 (cons (string :tag "MODULE" :value "")
661 (boolean :tag "Determine class for this method")))))
662
663 (defcustom idlwave-store-inquired-class nil
664 "*Non-nil means, store class of a method call as text property on `->'.
665 IDLWAVE sometimes has to ask the user for the class associated with a
666 particular object method call. This happens during the commands
667 `idlwave-routine-info' and `idlwave-complete', depending upon the
668 value of the variable `idlwave-query-class'.
669
670 When you specify a class, this information can be stored as a text
671 property on the `->' arrow in the source code, so that during the same
672 editing session, IDLWAVE will not have to ask again. When this
673 variable is non-nil, IDLWAVE will store and reuse the class information.
674 The class stored can be checked and removed with `\\[idlwave-routine-info]'
675 on the arrow.
676
677 The default of this variable is nil, since the result of commands then
678 is more predictable. However, if you know what you are doing, it can
679 be nice to turn this on.
680
681 An arrow which knows the class will be highlighted with
682 `idlwave-class-arrow-face'. The command \\[idlwave-routine-info]
683 displays (with prefix arg: deletes) the class stored on the arrow
684 at point."
685 :group 'idlwave-completion
686 :type 'boolean)
687
688 (defcustom idlwave-class-arrow-face 'bold
689 "*Face to highlight object operator arrows `->' which carry a class property.
690 When IDLWAVE stores a class name as text property on an object arrow
691 \(see variable `idlwave-store-inquired-class', it highlights the arrow
692 with this font in order to remind the user that this arrow is special."
693 :group 'idlwave-completion
694 :type 'symbol)
695
696 (defcustom idlwave-resize-routine-help-window t
697 "*Non-nil means, resize the Routine-info *Help* window to fit the content."
698 :group 'idlwave-completion
699 :type 'boolean)
700
701 (defcustom idlwave-keyword-completion-adds-equal t
702 "*Non-nil means, completion automatically adds `=' after completed keywords."
703 :group 'idlwave-completion
704 :type 'boolean)
705
706 (defcustom idlwave-function-completion-adds-paren t
707 "*Non-nil means, completion automatically adds `(' after completed function.
708 nil means, don't add anything.
709 A value of `2' means, also add the closing parenthesis and position cursor
710 between the two."
711 :group 'idlwave-completion
712 :type '(choice (const :tag "Nothing" nil)
713 (const :tag "(" t)
714 (const :tag "()" 2)))
715
716 (defcustom idlwave-completion-restore-window-configuration t
717 "*Non-nil means, try to restore the window configuration after completion.
718 When completion is not unique, Emacs displays a list of completions.
719 This messes up your window configuration. With this variable set, IDLWAVE
720 restores the old configuration after successful completion."
721 :group 'idlwave-completion
722 :type 'boolean)
723
724 ;;; Variables for abbrev and action behavior -----------------------------
725
726 (defgroup idlwave-abbrev-and-indent-action nil
727 "IDLWAVE performs actions when expanding abbreviations or indenting lines.
728 The variables in this group govern this."
729 :group 'idlwave)
730
731 (defcustom idlwave-do-actions nil
732 "*Non-nil means performs actions when indenting.
733 The actions that can be performed are listed in `idlwave-indent-action-table'."
734 :group 'idlwave-abbrev-and-indent-action
735 :type 'boolean)
736
737 (defcustom idlwave-abbrev-start-char "\\"
738 "*A single character string used to start abbreviations in abbrev mode.
739 Possible characters to chose from: ~`\%
740 or even '?'. '.' is not a good choice because it can make structure
741 field names act like abbrevs in certain circumstances.
742
743 Changes to this in `idlwave-mode-hook' will have no effect. Instead a user
744 must set it directly using `setq' in the .emacs file before idlwave.el
745 is loaded."
746 :group 'idlwave-abbrev-and-indent-action
747 :type 'string)
748
749 (defcustom idlwave-surround-by-blank nil
750 "*Non-nil means, enable `idlwave-surround'.
751 If non-nil, `=',`<',`>',`&',`,', `->' are surrounded with spaces by
752 `idlwave-surround'.
753 See help for `idlwave-indent-action-table' for symbols using `idlwave-surround'.
754
755 Also see the default key bindings for keys using `idlwave-surround'.
756 Keys are bound and made into actions calling `idlwave-surround' with
757 `idlwave-action-and-binding'.
758 See help for `idlwave-action-and-binding' for examples.
759
760 Also see help for `idlwave-surround'."
761 :group 'idlwave-abbrev-and-indent-action
762 :type 'boolean)
763
764 (defcustom idlwave-pad-keyword t
765 "*Non-nil means pad '=' in keywords (routine calls or defs) like assignment.
766 Whenever `idlwave-surround' is non-nil then this affects how '=' is
767 padded for keywords and for variables. If t, pad the same as for
768 assignments. If nil then spaces are removed. With any other value,
769 spaces are left unchanged."
770 :group 'idlwave-abbrev-and-indent-action
771 :type '(choice
772 (const :tag "Pad like assignments" t)
773 (const :tag "Remove space near `='" nil)
774 (const :tag "Keep space near `='" 'keep)))
775
776 (defcustom idlwave-show-block t
777 "*Non-nil means point blinks to block beginning for `idlwave-show-begin'."
778 :group 'idlwave-abbrev-and-indent-action
779 :type 'boolean)
780
781 (defcustom idlwave-expand-generic-end nil
782 "*Non-nil means expand generic END to ENDIF/ENDELSE/ENDWHILE etc."
783 :group 'idlwave-abbrev-and-indent-action
784 :type 'boolean)
785
786 (defcustom idlwave-reindent-end t
787 "*Non-nil means re-indent line after END was typed."
788 :group 'idlwave-abbrev-and-indent-action
789 :type 'boolean)
790
791 (defcustom idlwave-abbrev-move t
792 "*Non-nil means the abbrev hook can move point.
793 Set to nil by `idlwave-expand-region-abbrevs'. To see the abbrev
794 definitions, use the command `list-abbrevs', for abbrevs that move
795 point. Moving point is useful, for example, to place point between
796 parentheses of expanded functions.
797
798 See `idlwave-check-abbrev'."
799 :group 'idlwave-abbrev-and-indent-action
800 :type 'boolean)
801
802 (defcustom idlwave-abbrev-change-case nil
803 "*Non-nil means all abbrevs will be forced to either upper or lower case.
804 If the value t, all expanded abbrevs will be upper case.
805 If the value is 'down then abbrevs will be forced to lower case.
806 If nil, the case will not change.
807 If `idlwave-reserved-word-upcase' is non-nil, reserved words will always be
808 upper case, regardless of this variable."
809 :group 'idlwave-abbrev-and-indent-action
810 :type 'boolean)
811
812 (defcustom idlwave-reserved-word-upcase nil
813 "*Non-nil means, reserved words will be made upper case via abbrev expansion.
814 If nil case of reserved words is controlled by `idlwave-abbrev-change-case'.
815 Has effect only if in abbrev-mode."
816 :group 'idlwave-abbrev-and-indent-action
817 :type 'boolean)
818
819 ;;; Action/Expand Tables.
820 ;;
821 ;; The average user may have difficulty modifying this directly. It
822 ;; can be modified/set in idlwave-mode-hook, but it is easier to use
823 ;; idlwave-action-and-binding. See help for idlwave-action-and-binding for
824 ;; examples of how to add an action.
825 ;;
826 ;; The action table is used by `idlwave-indent-line' whereas both the
827 ;; action and expand tables are used by `idlwave-indent-and-action'. In
828 ;; general, the expand table is only used when a line is explicitly
829 ;; indented. Whereas, in addition to being used when the expand table
830 ;; is used, the action table is used when a line is indirectly
831 ;; indented via line splitting, auto-filling or a new line creation.
832 ;;
833 ;; Example actions:
834 ;;
835 ;; Capitalize system vars
836 ;; (idlwave-action-and-binding idlwave-sysvar '(capitalize-word 1) t)
837 ;;
838 ;; Capitalize procedure name
839 ;; (idlwave-action-and-binding "\\<\\(pro\\|function\\)\\>[ \t]*\\<"
840 ;; '(capitalize-word 1) t)
841 ;;
842 ;; Capitalize common block name
843 ;; (idlwave-action-and-binding "\\<common\\>[ \t]+\\<"
844 ;; '(capitalize-word 1) t)
845 ;; Capitalize label
846 ;; (idlwave-action-and-binding (concat "^[ \t]*" idlwave-label)
847 ;; '(capitalize-word -1) t)
848
849 (defvar idlwave-indent-action-table nil
850 "*Associated array containing action lists of search string (car),
851 and function as a cdr. This table is used by `idlwave-indent-line'.
852 See documentation for `idlwave-do-action' for a complete description of
853 the action lists.
854
855 Additions to the table are made with `idlwave-action-and-binding' when a
856 binding is not requested.
857 See help on `idlwave-action-and-binding' for examples.")
858
859 (defvar idlwave-indent-expand-table nil
860 "*Associated array containing action lists of search string (car),
861 and function as a cdr. The table is used by the
862 `idlwave-indent-and-action' function. See documentation for
863 `idlwave-do-action' for a complete description of the action lists.
864
865 Additions to the table are made with `idlwave-action-and-binding' when a
866 binding is requested.
867 See help on `idlwave-action-and-binding' for examples.")
868
869 ;;; Documentation header and history keyword ---------------------------------
870
871 (defgroup idlwave-documentation nil
872 "Options for documenting IDLWAVE files."
873 :group 'idlwave)
874
875 ;; FIXME: make defcustom?
876 (defvar idlwave-file-header
877 (list nil
878 ";+
879 ; NAME:
880 ;
881 ;
882 ;
883 ; PURPOSE:
884 ;
885 ;
886 ;
887 ; CATEGORY:
888 ;
889 ;
890 ;
891 ; CALLING SEQUENCE:
892 ;
893 ;
894 ;
895 ; INPUTS:
896 ;
897 ;
898 ;
899 ; OPTIONAL INPUTS:
900 ;
901 ;
902 ;
903 ; KEYWORD PARAMETERS:
904 ;
905 ;
906 ;
907 ; OUTPUTS:
908 ;
909 ;
910 ;
911 ; OPTIONAL OUTPUTS:
912 ;
913 ;
914 ;
915 ; COMMON BLOCKS:
916 ;
917 ;
918 ;
919 ; SIDE EFFECTS:
920 ;
921 ;
922 ;
923 ; RESTRICTIONS:
924 ;
925 ;
926 ;
927 ; PROCEDURE:
928 ;
929 ;
930 ;
931 ; EXAMPLE:
932 ;
933 ;
934 ;
935 ; MODIFICATION HISTORY:
936 ;
937 ;-
938 ")
939 "*A list (PATHNAME STRING) specifying the doc-header template to use for
940 summarizing a file. If PATHNAME is non-nil then this file will be included.
941 Otherwise STRING is used. If nil, the file summary will be omitted.
942 For example you might set PATHNAME to the path for the
943 lib_template.pro file included in the IDL distribution.")
944
945 (defcustom idlwave-header-to-beginning-of-file nil
946 "*Non-nil means, the documentation header will always be at start of file.
947 When nil, the header is positioned between the PRO/FUNCTION line of
948 the current routine and the code, allowing several routine headers in
949 a file."
950 :group 'idlwave-documentation
951 :type 'boolean)
952
953 (defcustom idlwave-timestamp-hook 'idlwave-default-insert-timestamp
954 "*The hook function used to update the timestamp of a function."
955 :group 'idlwave-documentation
956 :type 'function)
957
958 (defcustom idlwave-doc-modifications-keyword "HISTORY"
959 "*The modifications keyword to use with the log documentation commands.
960 A ':' is added to the keyword end.
961 Inserted by doc-header and used to position logs by doc-modification.
962 If nil it will not be inserted."
963 :group 'idlwave-documentation
964 :type 'string)
965
966 (defcustom idlwave-doclib-start "^;+\\+"
967 "*Regexp matching the start of a document library header."
968 :group 'idlwave-documentation
969 :type 'regexp)
970
971 (defcustom idlwave-doclib-end "^;+-"
972 "*Regexp matching the end of a document library header."
973 :group 'idlwave-documentation
974 :type 'regexp)
975
976 ;;; External Programs -------------------------------------------------------
977
978 (defgroup idlwave-external-programs nil
979 "Path locations of external commands used by IDLWAVE."
980 :group 'idlwave)
981
982 ;; WARNING: The following variable has recently been moved from
983 ;; idlw-shell.el to this file. I hope this does not break
984 ;; anything.
985
986 (defcustom idlwave-shell-explicit-file-name "idl"
987 "*If non-nil, this is the command to run IDL.
988 Should be an absolute file path or path relative to the current environment
989 execution search path. If you want to specify command line switches
990 for the idl program, use `idlwave-shell-command-line-options'.
991
992 I know the name of this variable is badly chosen, but I cannot change
993 it without compromizing backwards-compatibility."
994 :group 'idlwave-external-programs
995 :type 'string)
996
997 (defcustom idlwave-shell-command-line-options nil
998 "*A list of command line options for calling the IDL program.
999 Since IDL is executed directly without going through a shell like /bin/sh,
1000 this should be a list of strings like '(\"-rt=file\" \"-nw\") with a separate
1001 string for each argument. But you may also give a single string which
1002 contains the options whitespace-separated. Emacs will be kind enough to
1003 split it for you."
1004 :type '(choice
1005 string
1006 (repeat (string :value "")))
1007 :group 'idlwave-external-programs)
1008
1009 (defcustom idlwave-help-application "idlhelp"
1010 "*The external application providing reference help for programming."
1011 :group 'idlwave-external-programs
1012 :type 'string)
1013
1014 ;;; Some Shell variables which must be defined here.-----------------------
1015
1016 (defcustom idlwave-shell-debug-modifiers '()
1017 "List of modifiers to be used for the debugging commands.
1018 Will be used to bind debugging commands in the shell buffer and in all
1019 source buffers. These are additional convenience bindings, the debugging
1020 commands are always available with the `C-c C-d' prefix.
1021 If you set this to '(control shift), this means setting a breakpoint will
1022 be on `C-S-b', compiling a source file on `C-S-c' etc. Possible modifiers
1023 are `control', `meta', `super', `hyper', `alt', and `shift'."
1024 :group 'idlwave-shell-general-setup
1025 :type '(set :tag "Specify modifiers"
1026 (const control)
1027 (const meta)
1028 (const super)
1029 (const hyper)
1030 (const alt)
1031 (const shift)))
1032
1033 (defcustom idlwave-shell-automatic-start nil
1034 "*If non-nil attempt invoke idlwave-shell if not already running.
1035 This is checked when an attempt to send a command to an
1036 IDL process is made."
1037 :group 'idlwave-shell-general-setup
1038 :type 'boolean)
1039
1040 ;;; Miscellaneous variables -------------------------------------------------
1041
1042 (defgroup idlwave-misc nil
1043 "Miscellaneous options for IDLWAVE mode."
1044 :group 'idlwave)
1045
1046 (defcustom idlwave-startup-message t
1047 "*Non-nil displays a startup message when `idlwave-mode' is first called."
1048 :group 'idlwave-misc
1049 :type 'boolean)
1050
1051 (defcustom idlwave-default-font-lock-items
1052 '(pros-and-functions batch-files idlwave-idl-keywords label goto
1053 common-blocks class-arrows)
1054 "Items which should be fontified on the default fontification level 2.
1055 IDLWAVE defines 3 levels of fontification. Level 1 is very little, level 3
1056 is everything and level 2 is specified by this list.
1057 This variable must be set before IDLWAVE gets loaded. It is
1058 a list of symbols, the following symbols are allowed.
1059
1060 pros-and-functions Procedure and Function definitions
1061 batch-files Batch Files
1062 idlwave-idl-keywords IDL Keywords
1063 label Statement Labels
1064 goto Goto Statements
1065 common-blocks Common Blocks
1066 keyword-parameters Keyword Parameters in routine definitions and calls
1067 system-variables System Variables
1068 fixme FIXME: Warning in comments (on XEmacs only v. 21.0 and up)
1069 class-arrows Object Arrows with class property"
1070 :group 'idlwave-misc
1071 :type '(set
1072 :inline t :greedy t
1073 (const :tag "Procedure and Function definitions" pros-and-functions)
1074 (const :tag "Batch Files" batch-files)
1075 (const :tag "IDL Keywords (reserved words)" idlwave-idl-keywords)
1076 (const :tag "Statement Labels" label)
1077 (const :tag "Goto Statements" goto)
1078 (const :tag "Tags in Structure Definition" structtag)
1079 (const :tag "Structure Name" structname)
1080 (const :tag "Common Blocks" common-blocks)
1081 (const :tag "Keyword Parameters" keyword-parameters)
1082 (const :tag "System Variables" system-variables)
1083 (const :tag "FIXME: Warning" fixme)
1084 (const :tag "Object Arrows with class property " class-arrows)))
1085
1086 (defcustom idlwave-mode-hook nil
1087 "Normal hook. Executed when a buffer is put into `idlwave-mode'."
1088 :group 'idlwave-misc
1089 :type 'hook)
1090
1091 (defcustom idlwave-load-hook nil
1092 "Normal hook. Executed when idlwave.el is loaded."
1093 :group 'idlwave-misc
1094 :type 'hook)
1095
1096 (defvar idlwave-experimental nil
1097 "Non-nil means turn on a few experimental features.
1098 This variable is only for the maintainer, to test difficult stuff,
1099 while still distributing stable releases.
1100 As a user, you should not set this to t.")
1101
1102 ;;;
1103 ;;; End customization variables section
1104 ;;;
1105
1106 ;;; Non customization variables
1107
1108 ;;; font-lock mode - Additions by Phil Williams, Ulrik Dickow and
1109 ;;; Simon Marshall <simon_at_gnu.ai.mit.edu>
1110 ;;; and Carsten Dominik...
1111
1112 ;; The following are the reserved words in IDL. Maybe we should
1113 ;; highlight some more stuff as well?
1114 ;; Procedure declarations. Fontify keyword plus procedure name.
1115 (defvar idlwave-idl-keywords
1116 ;; To update this regexp, update the list of keywords and
1117 ;; evaluate the form.
1118 ;; (insert
1119 ;; (prin1-to-string
1120 ;; (concat
1121 ;; "\\<\\("
1122 ;; (regexp-opt
1123 ;; '("||" "&&" "and" "or" "xor" "not"
1124 ;; "eq" "ge" "gt" "le" "lt" "ne"
1125 ;; "for" "do" "endfor"
1126 ;; "if" "then" "endif" "else" "endelse"
1127 ;; "case" "of" "endcase"
1128 ;; "switch" "break" "continue" "endswitch"
1129 ;; "begin" "end"
1130 ;; "repeat" "until" "endrep"
1131 ;; "while" "endwhile"
1132 ;; "goto" "return"
1133 ;; "inherits" "mod"
1134 ;; "compile_opt" "forward_function"
1135 ;; "on_error" "on_ioerror")) ; on_error is not officially reserved
1136 ;; "\\)\\>")))
1137 "\\<\\(&&\\|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\\|||\\)\\>")
1138
1139
1140 (let* (;; Procedure declarations. Fontify keyword plus procedure name.
1141 ;; Function declarations. Fontify keyword plus function name.
1142 (pros-and-functions
1143 '("\\<\\(function\\|pro\\)\\>[ \t]+\\(\\sw+\\(::\\sw+\\)?\\)"
1144 (1 font-lock-keyword-face)
1145 (2 font-lock-function-name-face nil t)))
1146
1147 ;; Common blocks
1148 (common-blocks
1149 '("\\<\\(common\\)\\>[ \t]*\\(\\sw+\\)?[ \t]*,?"
1150 (1 font-lock-keyword-face) ; "common"
1151 (2 font-lock-reference-face nil t) ; block name
1152 (font-lock-match-c++-style-declaration-item-and-skip-to-next
1153 ;; Start with point after block name and comma
1154 (goto-char (match-end 0)) ; needed for XEmacs, could be nil
1155 nil
1156 (1 font-lock-variable-name-face) ; variable names
1157 )))
1158
1159 ;; Batch files
1160 (batch-files
1161 '("^[ \t]*\\(@[^ \t\n]+\\)" (1 font-lock-string-face)))
1162
1163 ;; FIXME warning.
1164 (fixme
1165 '("\\<FIXME:" (0 font-lock-warning-face t)))
1166
1167 ;; Labels
1168 (label
1169 '("^[ \t]*\\([a-zA-Z]\\sw*:\\)" (1 font-lock-reference-face)))
1170
1171 ;; The goto statement and its label
1172 (goto
1173 '("\\(goto\\)[ \t]*,[ \t]*\\([a-zA-Z]\\sw*\\)"
1174 (1 font-lock-keyword-face)
1175 (2 font-lock-reference-face)))
1176
1177 ;; Tags in structure definitions. Note that this definition
1178 ;; actually collides with labels, so we have to use the same
1179 ;; face. It also matches named subscript ranges,
1180 ;; e.g. vec{bottom:top]. No good way around this.
1181 (structtag
1182 '("\\<\\([a-zA-Z][a-zA-Z0-9_]*:\\)[^:]" (1 font-lock-reference-face)))
1183
1184 ;; Structure names
1185 (structname
1186 '("\\({\\|\\<inherits\\s-\\)\\s-*\\([a-zA-Z][a-zA-Z0-9_]*\\)[},\t \n]"
1187 (2 font-lock-function-name-face)))
1188
1189 ;; Keyword parameters, like /xlog or ,xrange=[]
1190 ;; This is anchored to the comma preceeding the keyword.
1191 ;; Treats continuation lines, works only during whole buffer
1192 ;; fontification. Slow, use it only in fancy fontification.
1193 (keyword-parameters
1194 '("\\(,\\|[a-zA-Z0-9_](\\)[ \t]*\\(\\$[ \t]*\\(;.*\\)?\\(\n[ \t]*;.*\\)*\n[ \t]*\\)?\\(/[a-zA-Z_]\\sw*\\|[a-zA-Z_]\\sw*[ \t]*=\\)"
1195 (5 font-lock-reference-face)))
1196
1197 ;; System variables start with a bang.
1198 (system-variables
1199 '("\\(![a-zA-Z_0-9]+\\(\\.\\sw+\\)?\\)"
1200 (1 font-lock-variable-name-face)))
1201
1202 ;; Special and unusual operators (not used because too noisy)
1203 (special-operators
1204 '("[<>#]" (0 font-lock-keyword-face)))
1205
1206 ;; All operators (not used because too noisy)
1207 (all-operators
1208 '("[-*^#+<>/]" (0 font-lock-keyword-face)))
1209
1210 ;; Arrows with text property `idlwave-class'
1211 (class-arrows
1212 '(idlwave-match-class-arrows (0 idlwave-class-arrow-face))))
1213
1214 (defconst idlwave-font-lock-keywords-1
1215 (list pros-and-functions batch-files)
1216 "Subdued level highlighting for IDLWAVE mode.")
1217
1218 (defconst idlwave-font-lock-keywords-2
1219 (mapcar 'symbol-value idlwave-default-font-lock-items)
1220 "Medium level highlighting for IDLWAVE mode.")
1221
1222 (defconst idlwave-font-lock-keywords-3
1223 (list pros-and-functions
1224 batch-files
1225 idlwave-idl-keywords
1226 label goto
1227 structtag
1228 structname
1229 common-blocks
1230 keyword-parameters
1231 system-variables
1232 class-arrows)
1233 "Gaudy level highlighting for IDLWAVE mode."))
1234
1235 (defun idlwave-match-class-arrows (limit)
1236 ;; Match an object arrow with class property
1237 (and idlwave-store-inquired-class
1238 (re-search-forward "->" limit 'limit)
1239 (get-text-property (match-beginning 0) 'idlwave-class)))
1240
1241 (defvar idlwave-font-lock-keywords idlwave-font-lock-keywords-2
1242 "Default expressions to highlight in IDLWAVE mode.")
1243
1244 (defvar idlwave-font-lock-defaults
1245 '((idlwave-font-lock-keywords
1246 idlwave-font-lock-keywords-1
1247 idlwave-font-lock-keywords-2
1248 idlwave-font-lock-keywords-3)
1249 nil t
1250 ((?$ . "w") (?_ . "w") (?. . "w") (?| . "w") (?& . "w"))
1251 beginning-of-line))
1252
1253 (put 'idlwave-mode 'font-lock-defaults
1254 idlwave-font-lock-defaults) ; XEmacs
1255
1256 (defconst idlwave-comment-line-start-skip "^[ \t]*;"
1257 "Regexp to match the start of a full-line comment.
1258 That is the _beginning_ of a line containing a comment delimiter `;' preceded
1259 only by whitespace.")
1260
1261 (defconst idlwave-begin-block-reg
1262 "\\<\\(pro\\|function\\|begin\\|case\\|switch\\)\\>"
1263 "Regular expression to find the beginning of a block. The case does
1264 not matter. The search skips matches in comments.")
1265
1266 (defconst idlwave-begin-unit-reg "^\\s-*\\(pro\\|function\\)\\>\\|\\`"
1267 "Regular expression to find the beginning of a unit. The case does
1268 not matter.")
1269
1270 (defconst idlwave-end-unit-reg "^\\s-*\\(pro\\|function\\)\\>\\|\\'"
1271 "Regular expression to find the line that indicates the end of unit.
1272 This line is the end of buffer or the start of another unit. The case does
1273 not matter. The search skips matches in comments.")
1274
1275 (defconst idlwave-continue-line-reg "\\<\\$"
1276 "Regular expression to match a continued line.")
1277
1278 (defconst idlwave-end-block-reg
1279 "\\<end\\(\\|case\\|switch\\|else\\|for\\|if\\|rep\\|while\\)\\>"
1280 "Regular expression to find the end of a block. The case does
1281 not matter. The search skips matches found in comments.")
1282
1283 (defconst idlwave-block-matches
1284 '(("pro" . "end")
1285 ("function" . "end")
1286 ("case" . "endcase")
1287 ("else" . "endelse")
1288 ("for" . "endfor")
1289 ("then" . "endif")
1290 ("repeat" . "endrep")
1291 ("switch" . "endswitch")
1292 ("while" . "endwhile"))
1293 "Matches between statements and the corresponding END variant.
1294 The cars are the reserved words starting a block. If the block really
1295 begins with BEGIN, the cars are the reserved words before the begin
1296 which can be used to identify the block type.
1297 This is used to check for the correct END type, to close blocks and
1298 to expand generic end statements to their detailed form.")
1299
1300 (defconst idlwave-block-match-regexp
1301 "\\<\\(else\\|for\\|then\\|repeat\\|while\\)\\>"
1302 "Regular expression matching reserved words which can stand before
1303 blocks starting with a BEGIN statement. The matches must have associations
1304 `idlwave-block-matches'")
1305
1306 (defconst idlwave-identifier "[a-zA-Z_][a-zA-Z0-9$_]*"
1307 "Regular expression matching an IDL identifier.")
1308
1309 (defconst idlwave-sysvar (concat "!" idlwave-identifier)
1310 "Regular expression matching IDL system variables.")
1311
1312 (defconst idlwave-variable (concat idlwave-identifier "\\|" idlwave-sysvar)
1313 "Regular expression matching IDL variable names.")
1314
1315 (defconst idlwave-label (concat idlwave-identifier ":")
1316 "Regular expression matching IDL labels.")
1317
1318 (defconst idlwave-method-call (concat idlwave-identifier "\\s *->"
1319 "\\(\\s *" idlwave-identifier "::\\)?"
1320 ))
1321
1322 (defconst idlwave-statement-match
1323 (list
1324 ;; "endif else" is the only possible "end" that can be
1325 ;; followed by a statement on the same line.
1326 '(endelse . ("end\\(\\|if\\)\\s +else" "end\\(\\|if\\)\\s +else"))
1327 ;; all other "end"s can not be followed by a statement.
1328 (cons 'end (list idlwave-end-block-reg nil))
1329 '(if . ("if\\>" "then"))
1330 '(for . ("for\\>" "do"))
1331 '(begin . ("begin\\>" nil))
1332 '(pdef . ("pro\\>\\|function\\>" nil))
1333 '(while . ("while\\>" "do"))
1334 '(repeat . ("repeat\\>" "repeat"))
1335 '(goto . ("goto\\>" nil))
1336 '(case . ("case\\>" nil))
1337 '(switch . ("switch\\>" nil))
1338 (cons 'call (list (concat "\\(" idlwave-variable "\\) *= *"
1339 "\\(" idlwave-method-call "\\s *\\)?"
1340 idlwave-identifier
1341 "\\s *(") nil))
1342 (cons 'call (list (concat
1343 "\\(" idlwave-method-call "\\s *\\)?"
1344 idlwave-identifier
1345 "\\( *\\($\\|\\$\\)\\|\\s *,\\)") nil))
1346 (cons 'assign (list (concat
1347 "\\(" idlwave-variable "\\) *=") nil)))
1348
1349 "Associated list of statement matching regular expressions.
1350 Each regular expression matches the start of an IDL statement. The
1351 first element of each association is a symbol giving the statement
1352 type. The associated value is a list. The first element of this list
1353 is a regular expression matching the start of an IDL statement for
1354 identifying the statement type. The second element of this list is a
1355 regular expression for finding a substatement for the type. The
1356 substatement starts after the end of the found match modulo
1357 whitespace. If it is nil then the statement has no substatement. The
1358 list order matters since matching an assignment statement exactly is
1359 not possible without parsing. Thus assignment statement become just
1360 the leftover unidentified statements containing an equal sign." )
1361
1362 (defvar idlwave-fill-function 'auto-fill-function
1363 "IDL mode auto fill function.")
1364
1365 (defvar idlwave-comment-indent-function 'comment-indent-function
1366 "IDL mode comment indent function.")
1367
1368 ;; Note that this is documented in the v18 manuals as being a string
1369 ;; of length one rather than a single character.
1370 ;; The code in this file accepts either format for compatibility.
1371 (defvar idlwave-comment-indent-char ?\
1372 "Character to be inserted for IDL comment indentation.
1373 Normally a space.")
1374
1375 (defconst idlwave-continuation-char ?$
1376 "Character which is inserted as a last character on previous line by
1377 \\[idlwave-split-line] to begin a continuation line. Normally $.")
1378
1379 (defconst idlwave-mode-version "5.5")
1380
1381 (defmacro idlwave-keyword-abbrev (&rest args)
1382 "Creates a function for abbrev hooks to call `idlwave-check-abbrev' with args."
1383 `(quote (lambda ()
1384 ,(append '(idlwave-check-abbrev) args))))
1385
1386 ;; If I take the time I can replace idlwave-keyword-abbrev with
1387 ;; idlwave-code-abbrev and remove the quoted abbrev check from
1388 ;; idlwave-check-abbrev. Then, e.g, (idlwave-keyword-abbrev 0 t) becomes
1389 ;; (idlwave-code-abbrev idlwave-check-abbrev 0 t). In fact I should change
1390 ;; the name of idlwave-check-abbrev to something like idlwave-modify-abbrev.
1391
1392 (defmacro idlwave-code-abbrev (&rest args)
1393 "Creates a function for abbrev hooks that ensures abbrevs are not quoted.
1394 Specifically, if the abbrev is in a comment or string it is unexpanded.
1395 Otherwise ARGS forms a list that is evaluated."
1396 `(quote (lambda ()
1397 ,(prin1-to-string args) ;; Puts the code in the doc string
1398 (if (idlwave-quoted)
1399 (progn (unexpand-abbrev) nil)
1400 ,(append args)))))
1401
1402 (defvar idlwave-mode-map (make-sparse-keymap)
1403 "Keymap used in IDL mode.")
1404
1405 (defvar idlwave-mode-syntax-table (make-syntax-table)
1406 "Syntax table in use in `idlwave-mode' buffers.")
1407
1408 (modify-syntax-entry ?+ "." idlwave-mode-syntax-table)
1409 (modify-syntax-entry ?- "." idlwave-mode-syntax-table)
1410 (modify-syntax-entry ?* "." idlwave-mode-syntax-table)
1411 (modify-syntax-entry ?/ "." idlwave-mode-syntax-table)
1412 (modify-syntax-entry ?^ "." idlwave-mode-syntax-table)
1413 (modify-syntax-entry ?# "." idlwave-mode-syntax-table)
1414 (modify-syntax-entry ?= "." idlwave-mode-syntax-table)
1415 (modify-syntax-entry ?% "." idlwave-mode-syntax-table)
1416 (modify-syntax-entry ?< "." idlwave-mode-syntax-table)
1417 (modify-syntax-entry ?> "." idlwave-mode-syntax-table)
1418 (modify-syntax-entry ?\' "\"" idlwave-mode-syntax-table)
1419 (modify-syntax-entry ?\" "\"" idlwave-mode-syntax-table)
1420 (modify-syntax-entry ?\\ "." idlwave-mode-syntax-table)
1421 (modify-syntax-entry ?_ "_" idlwave-mode-syntax-table)
1422 (modify-syntax-entry ?{ "(}" idlwave-mode-syntax-table)
1423 (modify-syntax-entry ?} "){" idlwave-mode-syntax-table)
1424 (modify-syntax-entry ?$ "_" idlwave-mode-syntax-table)
1425 (modify-syntax-entry ?. "." idlwave-mode-syntax-table)
1426 (modify-syntax-entry ?\; "<" idlwave-mode-syntax-table)
1427 (modify-syntax-entry ?\n ">" idlwave-mode-syntax-table)
1428 (modify-syntax-entry ?\f ">" idlwave-mode-syntax-table)
1429
1430 (defvar idlwave-find-symbol-syntax-table
1431 (copy-syntax-table idlwave-mode-syntax-table)
1432 "Syntax table that treats symbol characters as word characters.")
1433
1434 (modify-syntax-entry ?$ "w" idlwave-find-symbol-syntax-table)
1435 (modify-syntax-entry ?_ "w" idlwave-find-symbol-syntax-table)
1436 (modify-syntax-entry ?! "w" idlwave-find-symbol-syntax-table)
1437 (modify-syntax-entry ?. "w" idlwave-find-symbol-syntax-table)
1438
1439 (defmacro idlwave-with-special-syntax (&rest body)
1440 "Execute BODY with a different syntax table."
1441 `(let ((saved-syntax (syntax-table)))
1442 (unwind-protect
1443 (progn
1444 (set-syntax-table idlwave-find-symbol-syntax-table)
1445 ,@body)
1446 (set-syntax-table saved-syntax))))
1447
1448 ;(defmacro idlwave-with-special-syntax1 (&rest body)
1449 ; "Execute BODY with a different syntax table."
1450 ; `(let ((saved-syntax (syntax-table)))
1451 ; (unwind-protect
1452 ; (progn
1453 ; (set-syntax-table idlwave-find-symbol-syntax-table)
1454 ; ,@body)
1455 ; (set-syntax-table saved-syntax))))
1456
1457 (defun idlwave-action-and-binding (key cmd &optional select)
1458 "KEY and CMD are made into a key binding and an indent action.
1459 KEY is a string - same as for the `define-key' function. CMD is a
1460 function of no arguments or a list to be evaluated. CMD is bound to
1461 KEY in `idlwave-mode-map' by defining an anonymous function calling
1462 `self-insert-command' followed by CMD. If KEY contains more than one
1463 character a binding will only be set if SELECT is 'both.
1464
1465 \(KEY . CMD\) is also placed in the `idlwave-indent-expand-table',
1466 replacing any previous value for KEY. If a binding is not set then it
1467 will instead be placed in `idlwave-indent-action-table'.
1468
1469 If the optional argument SELECT is nil then an action and binding are
1470 created. If SELECT is 'noaction, then a binding is always set and no
1471 action is created. If SELECT is 'both then an action and binding
1472 will both be created even if KEY contains more than one character.
1473 Otherwise, if SELECT is non-nil then only an action is created.
1474
1475 Some examples:
1476 No spaces before and 1 after a comma
1477 (idlwave-action-and-binding \",\" '(idlwave-surround 0 1))
1478 A minimum of 1 space before and after `=' (see `idlwave-expand-equal').
1479 (idlwave-action-and-binding \"=\" '(idlwave-expand-equal -1 -1))
1480 Capitalize system variables - action only
1481 (idlwave-action-and-binding idlwave-sysvar '(capitalize-word 1) t)"
1482 (if (not (equal select 'noaction))
1483 ;; Add action
1484 (let* ((table (if select 'idlwave-indent-action-table
1485 'idlwave-indent-expand-table))
1486 (cell (assoc key (eval table))))
1487 (if cell
1488 ;; Replace action command
1489 (setcdr cell cmd)
1490 ;; New action
1491 (set table (append (eval table) (list (cons key cmd)))))))
1492 ;; Make key binding for action
1493 (if (or (and (null select) (= (length key) 1))
1494 (equal select 'noaction)
1495 (equal select 'both))
1496 (define-key idlwave-mode-map key
1497 (append '(lambda ()
1498 (interactive)
1499 (self-insert-command 1))
1500 (list (if (listp cmd)
1501 cmd
1502 (list cmd)))))))
1503
1504 (fset 'idlwave-debug-map (make-sparse-keymap))
1505
1506 (define-key idlwave-mode-map "\C-c " 'idlwave-hard-tab)
1507 (define-key idlwave-mode-map [(control tab)] 'idlwave-hard-tab)
1508 ;(define-key idlwave-mode-map "\C-c\C- " 'idlwave-hard-tab)
1509 (define-key idlwave-mode-map "'" 'idlwave-show-matching-quote)
1510 (define-key idlwave-mode-map "\"" 'idlwave-show-matching-quote)
1511 (define-key idlwave-mode-map "\C-g" 'idlwave-keyboard-quit)
1512 (define-key idlwave-mode-map "\C-c;" 'idlwave-toggle-comment-region)
1513 (define-key idlwave-mode-map "\C-\M-a" 'idlwave-beginning-of-subprogram)
1514 (define-key idlwave-mode-map "\C-\M-e" 'idlwave-end-of-subprogram)
1515 (define-key idlwave-mode-map "\C-c{" 'idlwave-beginning-of-block)
1516 (define-key idlwave-mode-map "\C-c}" 'idlwave-end-of-block)
1517 (define-key idlwave-mode-map "\C-c]" 'idlwave-close-block)
1518 (define-key idlwave-mode-map "\M-\C-h" 'idlwave-mark-subprogram)
1519 (define-key idlwave-mode-map "\M-\C-n" 'idlwave-forward-block)
1520 (define-key idlwave-mode-map "\M-\C-p" 'idlwave-backward-block)
1521 (define-key idlwave-mode-map "\M-\C-d" 'idlwave-down-block)
1522 (define-key idlwave-mode-map "\M-\C-u" 'idlwave-backward-up-block)
1523 (define-key idlwave-mode-map "\M-\r" 'idlwave-split-line)
1524 (define-key idlwave-mode-map "\M-\C-q" 'idlwave-indent-subprogram)
1525 (define-key idlwave-mode-map "\C-c\C-p" 'idlwave-previous-statement)
1526 (define-key idlwave-mode-map "\C-c\C-n" 'idlwave-next-statement)
1527 ;; (define-key idlwave-mode-map "\r" 'idlwave-newline)
1528 ;; (define-key idlwave-mode-map "\t" 'idlwave-indent-line)
1529 (define-key idlwave-mode-map [(shift tab)] 'idlwave-indent-statement)
1530 (define-key idlwave-mode-map "\C-c\C-a" 'idlwave-auto-fill-mode)
1531 (define-key idlwave-mode-map "\M-q" 'idlwave-fill-paragraph)
1532 (define-key idlwave-mode-map "\M-s" 'idlwave-edit-in-idlde)
1533 (define-key idlwave-mode-map "\C-c\C-h" 'idlwave-doc-header)
1534 (define-key idlwave-mode-map "\C-c\C-m" 'idlwave-doc-modification)
1535 (define-key idlwave-mode-map "\C-c\C-c" 'idlwave-case)
1536 (define-key idlwave-mode-map "\C-c\C-d" 'idlwave-debug-map)
1537 (when (and (boundp 'idlwave-shell-debug-modifiers)
1538 (listp idlwave-shell-debug-modifiers)
1539 (not (equal idlwave-shell-debug-modifiers '())))
1540 ;; Bind the debug commands also with the special modifiers.
1541 (let ((shift (memq 'shift idlwave-shell-debug-modifiers))
1542 (mods-noshift (delq 'shift
1543 (copy-sequence idlwave-shell-debug-modifiers))))
1544 (define-key idlwave-mode-map
1545 (vector (append mods-noshift (list (if shift ?C ?c))))
1546 'idlwave-shell-save-and-run)
1547 (define-key idlwave-mode-map
1548 (vector (append mods-noshift (list (if shift ?B ?b))))
1549 'idlwave-shell-break-here)
1550 (define-key idlwave-mode-map
1551 (vector (append mods-noshift (list (if shift ?E ?e))))
1552 'idlwave-shell-run-region)))
1553 (define-key idlwave-mode-map "\C-c\C-d\C-c" 'idlwave-shell-save-and-run)
1554 (define-key idlwave-mode-map "\C-c\C-d\C-b" 'idlwave-shell-break-here)
1555 (define-key idlwave-mode-map "\C-c\C-d\C-e" 'idlwave-shell-run-region)
1556 (define-key idlwave-mode-map "\C-c\C-f" 'idlwave-for)
1557 ;; (define-key idlwave-mode-map "\C-c\C-f" 'idlwave-function)
1558 ;; (define-key idlwave-mode-map "\C-c\C-p" 'idlwave-procedure)
1559 (define-key idlwave-mode-map "\C-c\C-r" 'idlwave-repeat)
1560 (define-key idlwave-mode-map "\C-c\C-w" 'idlwave-while)
1561 (define-key idlwave-mode-map "\C-c\C-k" 'idlwave-kill-autoloaded-buffers)
1562 (define-key idlwave-mode-map "\C-c\C-s" 'idlwave-shell)
1563 (define-key idlwave-mode-map "\C-c\C-l" 'idlwave-shell-recenter-shell-window)
1564 (define-key idlwave-mode-map "\C-c\C-b" 'idlwave-list-buffer-load-path-shadows)
1565 (autoload 'idlwave-shell "idlw-shell"
1566 "Run an inferior IDL, with I/O through buffer `(idlwave-shell-buffer)'." t)
1567 (autoload 'idlwave-shell-send-command "idlw-shell")
1568 (autoload 'idlwave-shell-recenter-shell-window "idlw-shell"
1569 "Run `idlwave-shell' and switch back to current window" t)
1570 (autoload 'idlwave-shell-save-and-run "idlw-shell"
1571 "Save and run buffer under the shell." t)
1572 (autoload 'idlwave-shell-break-here "idlw-shell"
1573 "Set breakpoint in current line." t)
1574 (autoload 'idlwave-shell-run-region "idlw-shell"
1575 "Compile and run the region." t)
1576 (define-key idlwave-mode-map "\C-c\C-v" 'idlwave-find-module)
1577 (define-key idlwave-mode-map "\C-c?" 'idlwave-routine-info)
1578 (define-key idlwave-mode-map "\M-?" 'idlwave-context-help)
1579 (define-key idlwave-mode-map [(control meta ?\?)] 'idlwave-online-help)
1580 ;; Pickup both forms of Esc/Meta binding
1581 (define-key idlwave-mode-map [(meta tab)] 'idlwave-complete)
1582 (define-key idlwave-mode-map [?\e?\t] 'idlwave-complete)
1583 (define-key idlwave-mode-map "\M-\C-i" 'idlwave-complete)
1584 (define-key idlwave-mode-map "\C-c\C-i" 'idlwave-update-routine-info)
1585 (define-key idlwave-mode-map "\C-c=" 'idlwave-resolve)
1586 (define-key idlwave-mode-map
1587 (if (featurep 'xemacs) [(shift button3)] [(shift mouse-3)])
1588 'idlwave-mouse-context-help)
1589
1590 ;; Set action and key bindings.
1591 ;; See description of the function `idlwave-action-and-binding'.
1592 ;; Automatically add spaces for the following characters
1593 ;(idlwave-action-and-binding "&" '(idlwave-surround -1 -1 '(?&) 1
1594 ; (lambda (char) 0)))
1595 (idlwave-action-and-binding "<" '(idlwave-surround -1 -1))
1596 ;; Binding works for both > and ->, by changing the length of the token.
1597 (idlwave-action-and-binding ">" '(idlwave-surround -1 -1 '(?-) 1
1598 'idlwave-gtr-pad-hook))
1599 (idlwave-action-and-binding "->" '(idlwave-surround -1 -1 nil 2) t)
1600 (idlwave-action-and-binding "," '(idlwave-surround 0 -1))
1601
1602 ;; Automatically add spaces to equal sign if not keyword
1603 (idlwave-action-and-binding "=" '(idlwave-expand-equal -1 -1))
1604
1605 ;;;
1606 ;;; Abbrev Section
1607 ;;;
1608 ;;; When expanding abbrevs and the abbrev hook moves backward, an extra
1609 ;;; space is inserted (this is the space typed by the user to expanded
1610 ;;; the abbrev).
1611 ;;;
1612 (defvar idlwave-mode-abbrev-table nil
1613 "Abbreviation table used for IDLWAVE mode")
1614 (define-abbrev-table 'idlwave-mode-abbrev-table ())
1615
1616 (defun idlwave-define-abbrev (name expansion hook &optional noprefix table)
1617 "Define-abbrev with backward compatibility.
1618
1619 If NOPREFIX is non-nil, don't prepend prefix character. Installs into
1620 idlwave-mode-abbrev-table unless TABLE is non-nil."
1621 (let ((abbrevs-changed nil) ;; mask the current value to avoid save
1622 (args (list (or table idlwave-mode-abbrev-table)
1623 (if noprefix name (concat idlwave-abbrev-start-char name))
1624 expansion
1625 hook)))
1626 (condition-case nil
1627 (apply 'define-abbrev (append args '(0 t)))
1628 (error (apply 'define-abbrev args)))))
1629
1630 (condition-case nil
1631 (modify-syntax-entry (string-to-char idlwave-abbrev-start-char)
1632 "w" idlwave-mode-syntax-table)
1633 (error nil))
1634
1635 ;;
1636 ;; Templates
1637 ;;
1638 (idlwave-define-abbrev "c" "" (idlwave-code-abbrev idlwave-case))
1639 (idlwave-define-abbrev "sw" "" (idlwave-code-abbrev idlwave-switch))
1640 (idlwave-define-abbrev "f" "" (idlwave-code-abbrev idlwave-for))
1641 (idlwave-define-abbrev "fu" "" (idlwave-code-abbrev idlwave-function))
1642 (idlwave-define-abbrev "pr" "" (idlwave-code-abbrev idlwave-procedure))
1643 (idlwave-define-abbrev "r" "" (idlwave-code-abbrev idlwave-repeat))
1644 (idlwave-define-abbrev "w" "" (idlwave-code-abbrev idlwave-while))
1645 (idlwave-define-abbrev "i" "" (idlwave-code-abbrev idlwave-if))
1646 (idlwave-define-abbrev "elif" "" (idlwave-code-abbrev idlwave-elif))
1647 ;;
1648 ;; Keywords, system functions, conversion routines
1649 ;;
1650 (idlwave-define-abbrev "ap" "arg_present()" (idlwave-keyword-abbrev 1))
1651 (idlwave-define-abbrev "b" "begin" (idlwave-keyword-abbrev 0 t))
1652 (idlwave-define-abbrev "co" "common" (idlwave-keyword-abbrev 0 t))
1653 (idlwave-define-abbrev "cb" "byte()" (idlwave-keyword-abbrev 1))
1654 (idlwave-define-abbrev "cx" "fix()" (idlwave-keyword-abbrev 1))
1655 (idlwave-define-abbrev "cl" "long()" (idlwave-keyword-abbrev 1))
1656 (idlwave-define-abbrev "cf" "float()" (idlwave-keyword-abbrev 1))
1657 (idlwave-define-abbrev "cs" "string()" (idlwave-keyword-abbrev 1))
1658 (idlwave-define-abbrev "cc" "complex()" (idlwave-keyword-abbrev 1))
1659 (idlwave-define-abbrev "cd" "double()" (idlwave-keyword-abbrev 1))
1660 (idlwave-define-abbrev "e" "else" (idlwave-keyword-abbrev 0 t))
1661 (idlwave-define-abbrev "ec" "endcase" 'idlwave-show-begin)
1662 (idlwave-define-abbrev "es" "endswitch" 'idlwave-show-begin)
1663 (idlwave-define-abbrev "ee" "endelse" 'idlwave-show-begin)
1664 (idlwave-define-abbrev "ef" "endfor" 'idlwave-show-begin)
1665 (idlwave-define-abbrev "ei" "endif else if" 'idlwave-show-begin)
1666 (idlwave-define-abbrev "el" "endif else" 'idlwave-show-begin)
1667 (idlwave-define-abbrev "en" "endif" 'idlwave-show-begin)
1668 (idlwave-define-abbrev "er" "endrep" 'idlwave-show-begin)
1669 (idlwave-define-abbrev "ew" "endwhile" 'idlwave-show-begin)
1670 (idlwave-define-abbrev "g" "goto," (idlwave-keyword-abbrev 0 t))
1671 (idlwave-define-abbrev "h" "help," (idlwave-keyword-abbrev 0))
1672 (idlwave-define-abbrev "k" "keyword_set()" (idlwave-keyword-abbrev 1))
1673 (idlwave-define-abbrev "n" "n_elements()" (idlwave-keyword-abbrev 1))
1674 (idlwave-define-abbrev "on" "on_error," (idlwave-keyword-abbrev 0))
1675 (idlwave-define-abbrev "oi" "on_ioerror," (idlwave-keyword-abbrev 0 1))
1676 (idlwave-define-abbrev "ow" "openw," (idlwave-keyword-abbrev 0))
1677 (idlwave-define-abbrev "or" "openr," (idlwave-keyword-abbrev 0))
1678 (idlwave-define-abbrev "ou" "openu," (idlwave-keyword-abbrev 0))
1679 (idlwave-define-abbrev "p" "print," (idlwave-keyword-abbrev 0))
1680 (idlwave-define-abbrev "pt" "plot," (idlwave-keyword-abbrev 0))
1681 (idlwave-define-abbrev "re" "read," (idlwave-keyword-abbrev 0))
1682 (idlwave-define-abbrev "rf" "readf," (idlwave-keyword-abbrev 0))
1683 (idlwave-define-abbrev "ru" "readu," (idlwave-keyword-abbrev 0))
1684 (idlwave-define-abbrev "rt" "return" (idlwave-keyword-abbrev 0))
1685 (idlwave-define-abbrev "sc" "strcompress()" (idlwave-keyword-abbrev 1))
1686 (idlwave-define-abbrev "sn" "strlen()" (idlwave-keyword-abbrev 1))
1687 (idlwave-define-abbrev "sl" "strlowcase()" (idlwave-keyword-abbrev 1))
1688 (idlwave-define-abbrev "su" "strupcase()" (idlwave-keyword-abbrev 1))
1689 (idlwave-define-abbrev "sm" "strmid()" (idlwave-keyword-abbrev 1))
1690 (idlwave-define-abbrev "sp" "strpos()" (idlwave-keyword-abbrev 1))
1691 (idlwave-define-abbrev "st" "strput()" (idlwave-keyword-abbrev 1))
1692 (idlwave-define-abbrev "sr" "strtrim()" (idlwave-keyword-abbrev 1))
1693 (idlwave-define-abbrev "t" "then" (idlwave-keyword-abbrev 0 t))
1694 (idlwave-define-abbrev "u" "until" (idlwave-keyword-abbrev 0 t))
1695 (idlwave-define-abbrev "wu" "writeu," (idlwave-keyword-abbrev 0))
1696 (idlwave-define-abbrev "iap" "if arg_present() then" (idlwave-keyword-abbrev 6))
1697 (idlwave-define-abbrev "ik" "if keyword_set() then" (idlwave-keyword-abbrev 6))
1698 (idlwave-define-abbrev "ine" "if n_elements() eq 0 then" (idlwave-keyword-abbrev 11))
1699 (idlwave-define-abbrev "inn" "if n_elements() ne 0 then" (idlwave-keyword-abbrev 11))
1700 (idlwave-define-abbrev "np" "n_params()" (idlwave-keyword-abbrev 0))
1701 (idlwave-define-abbrev "s" "size()" (idlwave-keyword-abbrev 1))
1702 (idlwave-define-abbrev "wi" "widget_info()" (idlwave-keyword-abbrev 1))
1703 (idlwave-define-abbrev "wc" "widget_control," (idlwave-keyword-abbrev 0))
1704
1705 ;; This section is reserved words only. (From IDL user manual)
1706 ;;
1707 (idlwave-define-abbrev "and" "and" (idlwave-keyword-abbrev 0 t) t)
1708 (idlwave-define-abbrev "begin" "begin" (idlwave-keyword-abbrev 0 t) t)
1709 (idlwave-define-abbrev "break" "break" (idlwave-keyword-abbrev 0 t) t)
1710 (idlwave-define-abbrev "case" "case" (idlwave-keyword-abbrev 0 t) t)
1711 (idlwave-define-abbrev "common" "common" (idlwave-keyword-abbrev 0 t) t)
1712 (idlwave-define-abbrev "continue" "continue" (idlwave-keyword-abbrev 0 t) t)
1713 (idlwave-define-abbrev "do" "do" (idlwave-keyword-abbrev 0 t) t)
1714 (idlwave-define-abbrev "else" "else" (idlwave-keyword-abbrev 0 t) t)
1715 (idlwave-define-abbrev "end" "end" 'idlwave-show-begin-check t)
1716 (idlwave-define-abbrev "endcase" "endcase" 'idlwave-show-begin-check t)
1717 (idlwave-define-abbrev "endelse" "endelse" 'idlwave-show-begin-check t)
1718 (idlwave-define-abbrev "endfor" "endfor" 'idlwave-show-begin-check t)
1719 (idlwave-define-abbrev "endif" "endif" 'idlwave-show-begin-check t)
1720 (idlwave-define-abbrev "endrep" "endrep" 'idlwave-show-begin-check t)
1721 (idlwave-define-abbrev "endswitch" "endswitch" 'idlwave-show-begin-check t)
1722 (idlwave-define-abbrev "endwhi" "endwhi" 'idlwave-show-begin-check t)
1723 (idlwave-define-abbrev "endwhile" "endwhile" 'idlwave-show-begin-check t)
1724 (idlwave-define-abbrev "eq" "eq" (idlwave-keyword-abbrev 0 t) t)
1725 (idlwave-define-abbrev "for" "for" (idlwave-keyword-abbrev 0 t) t)
1726 (idlwave-define-abbrev "function" "function" (idlwave-keyword-abbrev 0 t) t)
1727 (idlwave-define-abbrev "ge" "ge" (idlwave-keyword-abbrev 0 t) t)
1728 (idlwave-define-abbrev "goto" "goto" (idlwave-keyword-abbrev 0 t) t)
1729 (idlwave-define-abbrev "gt" "gt" (idlwave-keyword-abbrev 0 t) t)
1730 (idlwave-define-abbrev "if" "if" (idlwave-keyword-abbrev 0 t) t)
1731 (idlwave-define-abbrev "le" "le" (idlwave-keyword-abbrev 0 t) t)
1732 (idlwave-define-abbrev "lt" "lt" (idlwave-keyword-abbrev 0 t) t)
1733 (idlwave-define-abbrev "mod" "mod" (idlwave-keyword-abbrev 0 t) t)
1734 (idlwave-define-abbrev "ne" "ne" (idlwave-keyword-abbrev 0 t) t)
1735 (idlwave-define-abbrev "not" "not" (idlwave-keyword-abbrev 0 t) t)
1736 (idlwave-define-abbrev "of" "of" (idlwave-keyword-abbrev 0 t) t)
1737 (idlwave-define-abbrev "on_ioerror" "on_ioerror" (idlwave-keyword-abbrev 0 t) t)
1738 (idlwave-define-abbrev "or" "or" (idlwave-keyword-abbrev 0 t) t)
1739 (idlwave-define-abbrev "pro" "pro" (idlwave-keyword-abbrev 0 t) t)
1740 (idlwave-define-abbrev "repeat" "repeat" (idlwave-keyword-abbrev 0 t) t)
1741 (idlwave-define-abbrev "switch" "switch" (idlwave-keyword-abbrev 0 t) t)
1742 (idlwave-define-abbrev "then" "then" (idlwave-keyword-abbrev 0 t) t)
1743 (idlwave-define-abbrev "until" "until" (idlwave-keyword-abbrev 0 t) t)
1744 (idlwave-define-abbrev "while" "while" (idlwave-keyword-abbrev 0 t) t)
1745 (idlwave-define-abbrev "xor" "xor" (idlwave-keyword-abbrev 0 t) t)
1746
1747 (defvar imenu-create-index-function)
1748 (defvar extract-index-name-function)
1749 (defvar prev-index-position-function)
1750 (defvar imenu-extract-index-name-function)
1751 (defvar imenu-prev-index-position-function)
1752 ;; defined later - so just make the compiler hush
1753 (defvar idlwave-mode-menu)
1754 (defvar idlwave-mode-debug-menu)
1755
1756 ;;;###autoload
1757 (defun idlwave-mode ()
1758 "Major mode for editing IDL source files (version 5.5).
1759
1760 The main features of this mode are
1761
1762 1. Indentation and Formatting
1763 --------------------------
1764 Like other Emacs programming modes, C-j inserts a newline and indents.
1765 TAB is used for explicit indentation of the current line.
1766
1767 To start a continuation line, use \\[idlwave-split-line]. This
1768 function can also be used in the middle of a line to split the line
1769 at that point. When used inside a long constant string, the string
1770 is split at that point with the `+' concatenation operator.
1771
1772 Comments are indented as follows:
1773
1774 `;;;' Indentation remains unchanged.
1775 `;;' Indent like the surrounding code
1776 `;' Indent to a minimum column.
1777
1778 The indentation of comments starting in column 0 is never changed.
1779
1780 Use \\[idlwave-fill-paragraph] to refill a paragraph inside a
1781 comment. The indentation of the second line of the paragraph
1782 relative to the first will be retained. Use
1783 \\[idlwave-auto-fill-mode] to toggle auto-fill mode for these
1784 comments. When the variable `idlwave-fill-comment-line-only' is
1785 nil, code can also be auto-filled and auto-indented.
1786
1787 To convert pre-existing IDL code to your formatting style, mark the
1788 entire buffer with \\[mark-whole-buffer] and execute
1789 \\[idlwave-expand-region-abbrevs]. Then mark the entire buffer
1790 again followed by \\[indent-region] (`indent-region').
1791
1792 2. Routine Info
1793 ------------
1794 IDLWAVE displays information about the calling sequence and the
1795 accepted keyword parameters of a procedure or function with
1796 \\[idlwave-routine-info]. \\[idlwave-find-module] jumps to the
1797 source file of a module. These commands know about system
1798 routines, all routines in idlwave-mode buffers and (when the
1799 idlwave-shell is active) about all modules currently compiled under
1800 this shell. It also makes use of pre-compiled or custom-scanned
1801 user and library catalogs many popular libraries ship with by
1802 default. Use \\[idlwave-update-routine-info] to update this
1803 information, which is also used for completion (see item 4).
1804
1805 3. Online IDL Help
1806 ---------------
1807 \\[idlwave-context-help] displays the IDL documentation relevant
1808 for the system variable, keyword, or routine at point. A single
1809 key stroke gets you directly to the right place in the docs. The
1810 HTML help files package must be installed for this to work -- check
1811 the IDLWAVE webpage for the correct package for your version. See
1812 the manual to configure where and how the HTML help is displayed.
1813
1814 4. Completion
1815 ----------
1816 \\[idlwave-complete] completes the names of procedures, functions
1817 class names, keyword parameters, system variables and tags, class
1818 tags, structure tags, filenames and much more. It is context
1819 sensitive and figures out what is expected at point. Lower case
1820 strings are completed in lower case, other strings in mixed or
1821 upper case.
1822
1823 5. Code Templates and Abbreviations
1824 --------------------------------
1825 Many Abbreviations are predefined to expand to code fragments and templates.
1826 The abbreviations start generally with a `\\`. Some examples
1827
1828 \\pr PROCEDURE template
1829 \\fu FUNCTION template
1830 \\c CASE statement template
1831 \\sw SWITCH statement template
1832 \\f FOR loop template
1833 \\r REPEAT Loop template
1834 \\w WHILE loop template
1835 \\i IF statement template
1836 \\elif IF-ELSE statement template
1837 \\b BEGIN
1838
1839 For a full list, use \\[idlwave-list-abbrevs]. Some templates also
1840 have direct keybindings - see the list of keybindings below.
1841
1842 \\[idlwave-doc-header] inserts a documentation header at the
1843 beginning of the current program unit (pro, function or main).
1844 Change log entries can be added to the current program unit with
1845 \\[idlwave-doc-modification].
1846
1847 6. Automatic Case Conversion
1848 -------------------------
1849 The case of reserved words and some abbrevs is controlled by
1850 `idlwave-reserved-word-upcase' and `idlwave-abbrev-change-case'.
1851
1852 7. Automatic END completion
1853 ------------------------
1854 If the variable `idlwave-expand-generic-end' is non-nil, each END typed
1855 will be converted to the specific version, like ENDIF, ENDFOR, etc.
1856
1857 8. Hooks
1858 -----
1859 Loading idlwave.el runs `idlwave-load-hook'.
1860 Turning on `idlwave-mode' runs `idlwave-mode-hook'.
1861
1862 9. Documentation and Customization
1863 -------------------------------
1864 Info documentation for this package is available. Use
1865 \\[idlwave-info] to display (complain to your sysadmin if that does
1866 not work). For Postscript, PDF, and HTML versions of the
1867 documentation, check IDLWAVE's homepage at `http://idlwave.org'.
1868 IDLWAVE has customize support - see the group `idlwave'.
1869
1870 10.Keybindings
1871 -----------
1872 Here is a list of all keybindings of this mode.
1873 If some of the key bindings below show with ??, use \\[describe-key]
1874 followed by the key sequence to see what the key sequence does.
1875
1876 \\{idlwave-mode-map}"
1877
1878 (interactive)
1879 (kill-all-local-variables)
1880
1881 (if idlwave-startup-message
1882 (message "Emacs IDLWAVE mode version %s." idlwave-mode-version))
1883 (setq idlwave-startup-message nil)
1884
1885 (setq local-abbrev-table idlwave-mode-abbrev-table)
1886 (set-syntax-table idlwave-mode-syntax-table)
1887
1888 (set (make-local-variable 'indent-line-function) 'idlwave-indent-and-action)
1889
1890 (make-local-variable idlwave-comment-indent-function)
1891 (set idlwave-comment-indent-function 'idlwave-comment-hook)
1892
1893 (set (make-local-variable 'comment-start-skip) ";+[ \t]*")
1894 (set (make-local-variable 'comment-start) ";")
1895 (set (make-local-variable 'require-final-newline) t)
1896 (set (make-local-variable 'abbrev-all-caps) t)
1897 (set (make-local-variable 'indent-tabs-mode) nil)
1898 (set (make-local-variable 'completion-ignore-case) t)
1899
1900 (use-local-map idlwave-mode-map)
1901
1902 (when (featurep 'easymenu)
1903 (easy-menu-add idlwave-mode-menu idlwave-mode-map)
1904 (easy-menu-add idlwave-mode-debug-menu idlwave-mode-map))
1905
1906 (setq mode-name "IDLWAVE")
1907 (setq major-mode 'idlwave-mode)
1908 (setq abbrev-mode t)
1909
1910 (set (make-local-variable idlwave-fill-function) 'idlwave-auto-fill)
1911 (setq comment-end "")
1912 (set (make-local-variable 'comment-multi-line) nil)
1913 (set (make-local-variable 'paragraph-separate)
1914 "[ \t\f]*$\\|[ \t]*;+[ \t]*$\\|;+[+=-_*]+$")
1915 (set (make-local-variable 'paragraph-start) "[ \t\f]\\|[ \t]*;+[ \t]")
1916 (set (make-local-variable 'paragraph-ignore-fill-prefix) nil)
1917 (set (make-local-variable 'parse-sexp-ignore-comments) t)
1918
1919 ;; Set tag table list to use IDLTAGS as file name.
1920 (if (boundp 'tag-table-alist)
1921 (add-to-list 'tag-table-alist '("\\.pro$" . "IDLTAGS")))
1922
1923 ;; Font-lock additions - originally Phil Williams, then Ulrik Dickow
1924 ;; Following line is for Emacs - XEmacs uses the corresponding property
1925 ;; on the `idlwave-mode' symbol.
1926 (set (make-local-variable 'font-lock-defaults) idlwave-font-lock-defaults)
1927
1928 ;; Imenu setup
1929 (set (make-local-variable 'imenu-create-index-function)
1930 'imenu-default-create-index-function)
1931 (set (make-local-variable 'imenu-extract-index-name-function)
1932 'idlwave-unit-name)
1933 (set (make-local-variable 'imenu-prev-index-position-function)
1934 'idlwave-prev-index-position)
1935
1936 ;; Make a local post-command-hook and add our hook to it
1937 ;; NB: `make-local-hook' needed for older/alternative Emacs compatibility
1938 (make-local-hook 'post-command-hook)
1939 (add-hook 'post-command-hook 'idlwave-command-hook nil 'local)
1940
1941 ;; Make local hooks for buffer updates
1942 ;; NB: `make-local-hook' needed for older/alternative Emacs compatibility
1943 (make-local-hook 'kill-buffer-hook)
1944 (add-hook 'kill-buffer-hook 'idlwave-kill-buffer-update nil 'local)
1945 (make-local-hook 'after-save-hook)
1946 (add-hook 'after-save-hook 'idlwave-save-buffer-update nil 'local)
1947 (add-hook 'after-save-hook 'idlwave-revoke-license-to-kill nil 'local)
1948
1949 ;; Setup directories and file, if necessary
1950 (idlwave-setup)
1951
1952 ;; Update the routine info with info about current buffer?
1953 (idlwave-new-buffer-update)
1954
1955 ;; Run the mode hook
1956 (run-hooks 'idlwave-mode-hook))
1957
1958 (defvar idlwave-setup-done nil)
1959 (defun idlwave-setup ()
1960 (unless idlwave-setup-done
1961 (if (not (file-directory-p idlwave-config-directory))
1962 (make-directory idlwave-config-directory))
1963 (setq idlwave-user-catalog-file (expand-file-name
1964 idlwave-user-catalog-file
1965 idlwave-config-directory)
1966 idlwave-path-file (expand-file-name
1967 idlwave-path-file
1968 idlwave-config-directory))
1969 (idlwave-read-paths) ; we may need these early
1970 (setq idlwave-setup-done t)))
1971
1972 ;;
1973 ;; Code Formatting ----------------------------------------------------
1974 ;;
1975
1976 (defun idlwave-push-mark (&rest rest)
1977 "Push mark for compatibility with Emacs 18/19."
1978 (if (fboundp 'iconify-frame)
1979 (apply 'push-mark rest)
1980 (push-mark)))
1981
1982 (defun idlwave-hard-tab ()
1983 "Inserts TAB in buffer in current position."
1984 (interactive)
1985 (insert "\t"))
1986
1987 ;;; This stuff is experimental
1988
1989 (defvar idlwave-command-hook nil
1990 "If non-nil, a list that can be evaluated using `eval'.
1991 It is evaluated in the lisp function `idlwave-command-hook' which is
1992 placed in `post-command-hook'.")
1993
1994 (defun idlwave-command-hook ()
1995 "Command run after every command.
1996 Evaluates a non-nil value of the *variable* `idlwave-command-hook' and
1997 sets the variable to zero afterwards."
1998 (and idlwave-command-hook
1999 (listp idlwave-command-hook)
2000 (condition-case nil
2001 (eval idlwave-command-hook)
2002 (error nil)))
2003 (setq idlwave-command-hook nil))
2004
2005 ;;; End experiment
2006
2007 ;; It would be better to use expand.el for better abbrev handling and
2008 ;; versatility.
2009
2010 (defun idlwave-check-abbrev (arg &optional reserved)
2011 "Reverses abbrev expansion if in comment or string.
2012 Argument ARG is the number of characters to move point
2013 backward if `idlwave-abbrev-move' is non-nil.
2014 If optional argument RESERVED is non-nil then the expansion
2015 consists of reserved words, which will be capitalized if
2016 `idlwave-reserved-word-upcase' is non-nil.
2017 Otherwise, the abbrev will be capitalized if `idlwave-abbrev-change-case'
2018 is non-nil, unless its value is \`down in which case the abbrev will be
2019 made into all lowercase.
2020 Returns non-nil if abbrev is left expanded."
2021 (if (idlwave-quoted)
2022 (progn (unexpand-abbrev)
2023 nil)
2024 (if (and reserved idlwave-reserved-word-upcase)
2025 (upcase-region last-abbrev-location (point))
2026 (cond
2027 ((equal idlwave-abbrev-change-case 'down)
2028 (downcase-region last-abbrev-location (point)))
2029 (idlwave-abbrev-change-case
2030 (upcase-region last-abbrev-location (point)))))
2031 (if (and idlwave-abbrev-move (> arg 0))
2032 (if (boundp 'post-command-hook)
2033 (setq idlwave-command-hook (list 'backward-char (1+ arg)))
2034 (backward-char arg)))
2035 t))
2036
2037 (defun idlwave-in-comment ()
2038 "Returns t if point is inside a comment, nil otherwise."
2039 (save-excursion
2040 (let ((here (point)))
2041 (and (idlwave-goto-comment) (> here (point))))))
2042
2043 (defun idlwave-goto-comment ()
2044 "Move to start of comment delimiter on current line.
2045 Moves to end of line if there is no comment delimiter.
2046 Ignores comment delimiters in strings.
2047 Returns point if comment found and nil otherwise."
2048 (let ((eos (progn (end-of-line) (point)))
2049 (data (match-data))
2050 found)
2051 ;; Look for first comment delimiter not in a string
2052 (beginning-of-line)
2053 (setq found (search-forward comment-start eos 'lim))
2054 (while (and found (idlwave-in-quote))
2055 (setq found (search-forward comment-start eos 'lim)))
2056 (store-match-data data)
2057 (and found (not (idlwave-in-quote))
2058 (progn
2059 (backward-char 1)
2060 (point)))))
2061
2062 (defvar transient-mark-mode)
2063 (defvar zmacs-regions)
2064 (defvar mark-active)
2065 (defun idlwave-region-active-p ()
2066 "Is transient-mark-mode on and the region active?
2067 Works on both Emacs and XEmacs."
2068 (if (featurep 'xemacs)
2069 (and zmacs-regions (region-active-p))
2070 (and transient-mark-mode mark-active)))
2071
2072 (defun idlwave-show-matching-quote ()
2073 "Insert quote and show matching quote if this is end of a string."
2074 (interactive)
2075 (let ((bq (idlwave-in-quote))
2076 (inq last-command-char))
2077 (if (and bq (not (idlwave-in-comment)))
2078 (let ((delim (char-after bq)))
2079 (insert inq)
2080 (if (eq inq delim)
2081 (save-excursion
2082 (goto-char bq)
2083 (sit-for 1))))
2084 ;; Not the end of a string
2085 (insert inq))))
2086
2087 (defun idlwave-show-begin-check ()
2088 "Ensure that the previous word was a token before `idlwave-show-begin'.
2089 An END token must be preceded by whitespace."
2090 (if (not (idlwave-quoted))
2091 (if
2092 (save-excursion
2093 (backward-word 1)
2094 (backward-char 1)
2095 (looking-at "[ \t\n\f]"))
2096 (idlwave-show-begin))))
2097
2098 (defun idlwave-show-begin ()
2099 "Finds the start of current block and blinks to it for a second.
2100 Also checks if the correct end statement has been used."
2101 ;; All end statements are reserved words
2102 ;; Re-indent end line
2103 ;;(insert-char ?\ 1) ;; So indent, etc. work well
2104 ;;(backward-char 1)
2105 (let* ((pos (point-marker))
2106 (last-abbrev-marker (copy-marker last-abbrev-location))
2107 (eol-pos (save-excursion (end-of-line) (point)))
2108 begin-pos end-pos end end1 )
2109 (if idlwave-reindent-end (idlwave-indent-line))
2110 (setq last-abbrev-location (marker-position last-abbrev-marker))
2111 (when (and (idlwave-check-abbrev 0 t)
2112 idlwave-show-block)
2113 (save-excursion
2114 ;; Move inside current block
2115 (goto-char last-abbrev-marker)
2116 (idlwave-block-jump-out -1 'nomark)
2117 (setq begin-pos (point))
2118 (idlwave-block-jump-out 1 'nomark)
2119 (setq end-pos (point))
2120 (if (> end-pos eol-pos)
2121 (setq end-pos pos))
2122 (goto-char end-pos)
2123 (setq end (buffer-substring
2124 (progn
2125 (skip-chars-backward "a-zA-Z")
2126 (point))
2127 end-pos))
2128 (goto-char begin-pos)
2129 (when (setq end1 (cdr (idlwave-block-master)))
2130 (cond
2131 ((null end1)) ; no-operation
2132 ((string= (downcase end) (downcase end1))
2133 (sit-for 1))
2134 ((string= (downcase end) "end")
2135 ;; A generic end
2136 (if idlwave-expand-generic-end
2137 (save-excursion
2138 (goto-char pos)
2139 (backward-char 3)
2140 (insert (if (string= end "END") (upcase end1) end1))
2141 (delete-char 3)))
2142 (sit-for 1))
2143 (t
2144 (beep)
2145 (message "Warning: Shouldn't this be \"%s\" instead of \"%s\"?"
2146 end1 end)
2147 (sit-for 1))))))))
2148 ;;(delete-char 1))
2149
2150 (defun idlwave-block-master ()
2151 (let ((case-fold-search t))
2152 (save-excursion
2153 (cond
2154 ((looking-at "pro\\|case\\|switch\\|function\\>")
2155 (assoc (downcase (match-string 0)) idlwave-block-matches))
2156 ((looking-at "begin\\>")
2157 (let ((limit (save-excursion
2158 (idlwave-beginning-of-statement)
2159 (point))))
2160 (cond
2161 ((re-search-backward ":[ \t]*\\=" limit t)
2162 ;; seems to be a case thing
2163 '("begin" . "end"))
2164 ((re-search-backward idlwave-block-match-regexp limit t)
2165 (assoc (downcase (match-string 1))
2166 idlwave-block-matches))
2167 (t
2168 ;; Just a normal block
2169 '("begin" . "end")))))
2170 (t nil)))))
2171
2172 (defun idlwave-close-block ()
2173 "Terminate the current block with the correct END statement."
2174 (interactive)
2175
2176 ;; Start new line if we are not in a new line
2177 (unless (save-excursion
2178 (skip-chars-backward " \t")
2179 (bolp))
2180 (let ((idlwave-show-block nil))
2181 (newline-and-indent)))
2182 (let ((last-abbrev-location (point))) ; for upcasing
2183 (insert "end")
2184 (idlwave-show-begin)))
2185
2186 (defun idlwave-gtr-pad-hook (char)
2187 "Let the > symbol expand around -> if present. The new token length
2188 is returned."
2189 2)
2190
2191 (defun idlwave-surround (&optional before after escape-chars length ec-hook)
2192 "Surround the LENGTH characters before point with blanks.
2193 LENGTH defaults to 1.
2194 Optional arguments BEFORE and AFTER affect the behavior before and
2195 after the characters (see also description of `idlwave-make-space'):
2196
2197 nil do nothing
2198 0 force no spaces
2199 integer > 0 force exactly n spaces
2200 integer < 0 at least |n| spaces
2201
2202 The function does nothing if any of the following conditions is true:
2203 - `idlwave-surround-by-blank' is nil
2204 - the character before point is inside a string or comment
2205 - the char preceeding the string to be surrounded is a member of ESCAPE-CHARS.
2206 This hack is used to avoid padding of `>' when it is part of
2207 the '->' operator. In this case, ESCAPE-CHARS would be '(?-).
2208
2209 If a function is passed in EC-HOOK, and an ESCAPE-CHARS match occurs,
2210 the named function will be called with a single argument of the
2211 preceeding character. Then idlwave-surround will run as usual if
2212 EC-HOOK returns non-nil, and a new length will be taken from the
2213 return value."
2214 (when (and idlwave-surround-by-blank (not (idlwave-quoted)))
2215 (let* ((length (or length 1)) ; establish a default for LENGTH
2216 (prev-char (char-after (- (point) (1+ length)))))
2217 (when (or (not (memq prev-char escape-chars))
2218 (and (fboundp ec-hook)
2219 (setq length
2220 (save-excursion (funcall ec-hook prev-char)))))
2221 (backward-char length)
2222 (save-restriction
2223 (let ((here (point)))
2224 (skip-chars-backward " \t")
2225 (if (bolp)
2226 ;; avoid clobbering indent
2227 (progn
2228 (move-to-column (idlwave-calculate-indent))
2229 (if (<= (point) here)
2230 (narrow-to-region (point) here))
2231 (goto-char here)))
2232 (idlwave-make-space before))
2233 (skip-chars-forward " \t"))
2234 (forward-char length)
2235 (idlwave-make-space after)
2236 ;; Check to see if the line should auto wrap
2237 (if (and (equal (char-after (1- (point))) ?\ )
2238 (> (current-column) fill-column))
2239 (funcall auto-fill-function))))))
2240
2241 (defun idlwave-make-space (n)
2242 "Make space at point.
2243 The space affected is all the spaces and tabs around point.
2244 If n is non-nil then point is left abs(n) spaces from the beginning of
2245 the contiguous space.
2246 The amount of space at point is determined by N.
2247 If the value of N is:
2248 nil - do nothing.
2249 > 0 - exactly N spaces.
2250 < 0 - a minimum of -N spaces, i.e., do not change if there are
2251 already -N spaces.
2252 0 - no spaces (i.e. remove any existing space)."
2253 (if (integerp n)
2254 (let
2255 ((start-col (progn (skip-chars-backward " \t") (current-column)))
2256 (left (point))
2257 (end-col (progn (skip-chars-forward " \t") (current-column))))
2258 (delete-horizontal-space)
2259 (cond
2260 ((> n 0)
2261 (idlwave-indent-to (+ start-col n))
2262 (goto-char (+ left n)))
2263 ((< n 0)
2264 (idlwave-indent-to end-col (- n))
2265 (goto-char (- left n)))
2266 ;; n = 0, done
2267 ))))
2268
2269 (defun idlwave-newline ()
2270 "Inserts a newline and indents the current and previous line."
2271 (interactive)
2272 ;;
2273 ;; Handle unterminated single and double quotes
2274 ;; If not in a comment and in a string then insertion of a newline
2275 ;; will mean unbalanced quotes.
2276 ;;
2277 (if (and (not (idlwave-in-comment)) (idlwave-in-quote))
2278 (progn (beep)
2279 (message "Warning: unbalanced quotes?")))
2280 (newline)
2281 ;;
2282 ;; The current line is being split, the cursor should be at the
2283 ;; beginning of the new line skipping the leading indentation.
2284 ;;
2285 ;; The reason we insert the new line before indenting is that the
2286 ;; indenting could be confused by keywords (e.g. END) on the line
2287 ;; after the split point. This prevents us from just using
2288 ;; `indent-for-tab-command' followed by `newline-and-indent'.
2289 ;;
2290 (beginning-of-line 0)
2291 (idlwave-indent-line)
2292 (forward-line)
2293 (idlwave-indent-line))
2294
2295 ;;
2296 ;; Use global variable 'comment-column' to set parallel comment
2297 ;;
2298 ;; Modeled on lisp.el
2299 ;; Emacs Lisp and IDL (Wave CL) have identical comment syntax
2300 (defun idlwave-comment-hook ()
2301 "Compute indent for the beginning of the IDL comment delimiter."
2302 (if (or (looking-at idlwave-no-change-comment)
2303 (if idlwave-begin-line-comment
2304 (looking-at idlwave-begin-line-comment)
2305 (looking-at "^;")))
2306 (current-column)
2307 (if (looking-at idlwave-code-comment)
2308 (if (save-excursion (skip-chars-backward " \t") (bolp))
2309 ;; On line by itself, indent as code
2310 (let ((tem (idlwave-calculate-indent)))
2311 (if (listp tem) (car tem) tem))
2312 ;; after code - do not change
2313 (current-column))
2314 (skip-chars-backward " \t")
2315 (max (if (bolp) 0 (1+ (current-column)))
2316 comment-column))))
2317
2318 (defun idlwave-split-line ()
2319 "Continue line by breaking line at point and indent the lines.
2320 For a code line insert continuation marker. If the line is a line comment
2321 then the new line will contain a comment with the same indentation.
2322 Splits strings with the IDL operator `+' if `idlwave-split-line-string' is
2323 non-nil."
2324 (interactive)
2325 ;; Expand abbreviation, just like normal RET would.
2326 (and abbrev-mode (expand-abbrev))
2327 (let (beg)
2328 (if (not (idlwave-in-comment))
2329 ;; For code line add continuation.
2330 ;; Check if splitting a string.
2331 (progn
2332 (if (setq beg (idlwave-in-quote))
2333 (if idlwave-split-line-string
2334 ;; Split the string.
2335 (progn (insert (setq beg (char-after beg)) " + "
2336 idlwave-continuation-char beg)
2337 (backward-char 1)
2338 (newline-and-indent)
2339 (forward-char 1))
2340 ;; Do not split the string.
2341 (beep)
2342 (message "Warning: continuation inside string!!")
2343 (insert " " idlwave-continuation-char))
2344 ;; Not splitting a string.
2345 (if (not (member (char-before) '(?\ ?\t)))
2346 (insert " "))
2347 (insert idlwave-continuation-char)
2348 (newline-and-indent)))
2349 (indent-new-comment-line))
2350 ;; Indent previous line
2351 (setq beg (- (point-max) (point)))
2352 (forward-line -1)
2353 (idlwave-indent-line)
2354 (goto-char (- (point-max) beg))
2355 ;; Reindent new line
2356 (idlwave-indent-line)))
2357
2358 (defun idlwave-beginning-of-subprogram ()
2359 "Moves point to the beginning of the current program unit."
2360 (interactive)
2361 (idlwave-find-key idlwave-begin-unit-reg -1))
2362
2363 (defun idlwave-end-of-subprogram ()
2364 "Moves point to the start of the next program unit."
2365 (interactive)
2366 (idlwave-end-of-statement)
2367 (idlwave-find-key idlwave-end-unit-reg 1))
2368
2369 (defun idlwave-mark-statement ()
2370 "Mark current IDL statement."
2371 (interactive)
2372 (idlwave-end-of-statement)
2373 (let ((end (point)))
2374 (idlwave-beginning-of-statement)
2375 (idlwave-push-mark end nil t)))
2376
2377 (defun idlwave-mark-block ()
2378 "Mark containing block."
2379 (interactive)
2380 (idlwave-end-of-statement)
2381 (idlwave-backward-up-block -1)
2382 (idlwave-end-of-statement)
2383 (let ((end (point)))
2384 (idlwave-backward-block)
2385 (idlwave-beginning-of-statement)
2386 (idlwave-push-mark end nil t)))
2387
2388
2389 (defun idlwave-mark-subprogram ()
2390 "Put mark at beginning of program, point at end.
2391 The marks are pushed."
2392 (interactive)
2393 (idlwave-end-of-statement)
2394 (idlwave-beginning-of-subprogram)
2395 (let ((beg (point)))
2396 (idlwave-forward-block)
2397 (idlwave-push-mark beg nil t))
2398 (exchange-point-and-mark))
2399
2400 (defun idlwave-backward-up-block (&optional arg)
2401 "Move to beginning of enclosing block if prefix ARG >= 0.
2402 If prefix ARG < 0 then move forward to enclosing block end."
2403 (interactive "p")
2404 (idlwave-block-jump-out (- arg) 'nomark))
2405
2406 (defun idlwave-beginning-of-block ()
2407 "Go to the beginning of the current block."
2408 (interactive)
2409 (idlwave-block-jump-out -1 'nomark)
2410 (forward-word 1))
2411
2412 (defun idlwave-end-of-block ()
2413 "Go to the beginning of the current block."
2414 (interactive)
2415 (idlwave-block-jump-out 1 'nomark)
2416 (backward-word 1))
2417
2418 (defun idlwave-forward-block ()
2419 "Move across next nested block."
2420 (interactive)
2421 (if (idlwave-down-block 1)
2422 (idlwave-block-jump-out 1 'nomark)))
2423
2424 (defun idlwave-backward-block ()
2425 "Move backward across previous nested block."
2426 (interactive)
2427 (if (idlwave-down-block -1)
2428 (idlwave-block-jump-out -1 'nomark)))
2429
2430 (defun idlwave-down-block (&optional arg)
2431 "Go down a block.
2432 With ARG: ARG >= 0 go forwards, ARG < 0 go backwards.
2433 Returns non-nil if successfull."
2434 (interactive "p")
2435 (let (status)
2436 (if (< arg 0)
2437 ;; Backward
2438 (let ((eos (save-excursion
2439 (idlwave-block-jump-out -1 'nomark)
2440 (point))))
2441 (if (setq status (idlwave-find-key
2442 idlwave-end-block-reg -1 'nomark eos))
2443 (idlwave-beginning-of-statement)
2444 (message "No nested block before beginning of containing block.")))
2445 ;; Forward
2446 (let ((eos (save-excursion
2447 (idlwave-block-jump-out 1 'nomark)
2448 (point))))
2449 (if (setq status (idlwave-find-key
2450 idlwave-begin-block-reg 1 'nomark eos))
2451 (idlwave-end-of-statement)
2452 (message "No nested block before end of containing block."))))
2453 status))
2454
2455 (defun idlwave-mark-doclib ()
2456 "Put point at beginning of doc library header, mark at end.
2457 The marks are pushed."
2458 (interactive)
2459 (let (beg
2460 (here (point)))
2461 (goto-char (point-max))
2462 (if (re-search-backward idlwave-doclib-start nil t)
2463 (progn
2464 (setq beg (progn (beginning-of-line) (point)))
2465 (if (re-search-forward idlwave-doclib-end nil t)
2466 (progn
2467 (forward-line 1)
2468 (idlwave-push-mark beg nil t)
2469 (message "Could not find end of doc library header.")))
2470 (message "Could not find doc library header start.")
2471 (goto-char here)))))
2472
2473
2474 (defun idlwave-current-routine ()
2475 "Return (NAME TYPE CLASS) of current routine."
2476 (idlwave-routines)
2477 (save-excursion
2478 (idlwave-beginning-of-subprogram)
2479 (if (looking-at "[ \t]*\\<\\(pro\\|function\\)\\>\\s-+\\(\\([a-zA-Z0-9$_]+\\)::\\)?\\([a-zA-Z0-9$_]+\\)")
2480 (let* ((type (if (string= (downcase (match-string 1)) "pro")
2481 'pro 'function))
2482 (class (idlwave-sintern-class (match-string 3)))
2483 (name (idlwave-sintern-routine-or-method (match-string 4) class)))
2484 (list name type class)))))
2485
2486 (defvar idlwave-shell-prompt-pattern)
2487 (defun idlwave-beginning-of-statement ()
2488 "Move to beginning of the current statement.
2489 Skips back past statement continuations.
2490 Point is placed at the beginning of the line whether or not this is an
2491 actual statement."
2492 (interactive)
2493 (cond
2494 ((eq major-mode 'idlwave-shell-mode)
2495 (if (re-search-backward idlwave-shell-prompt-pattern nil t)
2496 (goto-char (match-end 0))))
2497 (t
2498 (if (save-excursion (forward-line -1) (idlwave-is-continuation-line))
2499 (idlwave-previous-statement)
2500 (beginning-of-line)))))
2501
2502 (defun idlwave-previous-statement ()
2503 "Moves point to beginning of the previous statement.
2504 Returns t if the current line before moving is the beginning of
2505 the first non-comment statement in the file, and nil otherwise."
2506 (interactive)
2507 (let (first-statement)
2508 (if (not (= (forward-line -1) 0))
2509 ;; first line in file
2510 t
2511 ;; skip blank lines, label lines, include lines and line comments
2512 (while (and
2513 ;; The current statement is the first statement until we
2514 ;; reach another statement.
2515 (setq first-statement
2516 (or
2517 (looking-at idlwave-comment-line-start-skip)
2518 (looking-at "[ \t]*$")
2519 (looking-at (concat "[ \t]*" idlwave-label "[ \t]*$"))
2520 (looking-at "^@")))
2521 (= (forward-line -1) 0)))
2522 ;; skip continuation lines
2523 (while (and
2524 (save-excursion
2525 (forward-line -1)
2526 (idlwave-is-continuation-line))
2527 (= (forward-line -1) 0)))
2528 first-statement)))
2529
2530 (defun idlwave-end-of-statement ()
2531 "Moves point to the end of the current IDL statement.
2532 If not in a statement just moves to end of line. Returns position."
2533 (interactive)
2534 (while (and (idlwave-is-continuation-line)
2535 (= (forward-line 1) 0))
2536 (while (and (idlwave-is-comment-or-empty-line)
2537 (= (forward-line 1) 0))))
2538 (end-of-line)
2539 (point))
2540
2541 (defun idlwave-end-of-statement0 ()
2542 "Moves point to the end of the current IDL statement.
2543 If not in a statement just moves to end of line. Returns position."
2544 (interactive)
2545 (while (and (idlwave-is-continuation-line)
2546 (= (forward-line 1) 0)))
2547 (end-of-line)
2548 (point))
2549
2550 (defun idlwave-next-statement ()
2551 "Moves point to beginning of the next IDL statement.
2552 Returns t if that statement is the last
2553 non-comment IDL statement in the file, and nil otherwise."
2554 (interactive)
2555 (let (last-statement)
2556 (idlwave-end-of-statement)
2557 ;; skip blank lines, label lines, include lines and line comments
2558 (while (and (= (forward-line 1) 0)
2559 ;; The current statement is the last statement until
2560 ;; we reach a new statement.
2561 (setq last-statement
2562 (or
2563 (looking-at idlwave-comment-line-start-skip)
2564 (looking-at "[ \t]*$")
2565 (looking-at (concat "[ \t]*" idlwave-label "[ \t]*$"))
2566 (looking-at "^@")))))
2567 last-statement))
2568
2569 (defun idlwave-skip-multi-commands (&optional lim)
2570 "Skip past multiple commands on a line (with `&')."
2571 (let ((save-point (point)))
2572 (when (re-search-forward ".*&" lim t)
2573 (goto-char (match-end 0))
2574 (if (idlwave-quoted)
2575 (goto-char save-point)
2576 (if (eq (char-after (- (point) 2)) ?&) (goto-char save-point))))
2577 (point)))
2578
2579 (defun idlwave-skip-label-or-case ()
2580 "Skip label or case statement element.
2581 Returns position after label.
2582 If there is no label point is not moved and nil is returned."
2583 ;; Case expressions and labels are terminated by a colon.
2584 ;; So we find the first colon in the line and make sure
2585 ;; - no `?' is before it (might be a ? b : c)
2586 ;; - it is not in a comment
2587 ;; - not in a string constant
2588 ;; - not in parenthesis (like a[0:3])
2589 ;; - not followed by another ":" in explicit class, ala a->b::c
2590 ;; As many in this mode, this function is heuristic and not an exact
2591 ;; parser.
2592 (let* ((start (point))
2593 (eos (save-excursion (idlwave-end-of-statement) (point)))
2594 (end (idlwave-find-key ":" 1 'nomark eos)))
2595 (if (and end
2596 (= (nth 0 (parse-partial-sexp start end)) 0)
2597 (not (string-match "\\?" (buffer-substring start end)))
2598 (not (string-match "^::" (buffer-substring end eos))))
2599 (progn
2600 (forward-char)
2601 (point))
2602 (goto-char start)
2603 nil)))
2604
2605 (defun idlwave-start-of-substatement (&optional pre)
2606 "Move to start of next IDL substatement after point.
2607 Uses the type of the current IDL statement to determine if the next
2608 statement is on a new line or is a subpart of the current statement.
2609 Returns point at start of substatement modulo whitespace.
2610 If optional argument is non-nil move to beginning of current
2611 substatement."
2612 (let ((orig (point))
2613 (eos (idlwave-end-of-statement))
2614 (ifnest 0)
2615 st nst last)
2616 (idlwave-beginning-of-statement)
2617 (idlwave-skip-label-or-case)
2618 (if (< (point) orig)
2619 (idlwave-skip-multi-commands orig))
2620 (setq last (point))
2621 ;; Continue looking for substatements until we are past orig
2622 (while (and (<= (point) orig) (not (eobp)))
2623 (setq last (point))
2624 (setq nst (nth 1 (cdr (setq st (car (idlwave-statement-type))))))
2625 (if (equal (car st) 'if) (setq ifnest (1+ ifnest)))
2626 (cond ((and nst
2627 (idlwave-find-key nst 1 'nomark eos))
2628 (goto-char (match-end 0)))
2629 ((and (> ifnest 0) (idlwave-find-key "\\<else\\>" 1 'nomark eos))
2630 (setq ifnest (1- ifnest))
2631 (goto-char (match-end 0)))
2632 (t (setq ifnest 0)
2633 (idlwave-next-statement))))
2634 (if pre (goto-char last))
2635 ;; If a continuation line starts here, move to next line
2636 (if (looking-at "[ \t]*\\$\\([ \t]*\\(;\\|$\\)\\)")
2637 (beginning-of-line 2))
2638 (point)))
2639
2640 (defun idlwave-statement-type ()
2641 "Return the type of the current IDL statement.
2642 Uses `idlwave-statement-match' to return a cons of (type . point) with
2643 point the ending position where the type was determined. Type is the
2644 association from `idlwave-statement-match', i.e. the cons cell from the
2645 list not just the type symbol. Returns nil if not an identifiable
2646 statement."
2647 (save-excursion
2648 ;; Skip whitespace within a statement which is spaces, tabs, continuations
2649 ;; and possibly comments
2650 (while (looking-at "[ \t]*\\$")
2651 (forward-line 1))
2652 (skip-chars-forward " \t")
2653 (let ((st idlwave-statement-match)
2654 (case-fold-search t))
2655 (while (and (not (looking-at (nth 0 (cdr (car st)))))
2656 (setq st (cdr st))))
2657 (if st
2658 (append st (match-end 0))))))
2659
2660 (defun idlwave-expand-equal (&optional before after)
2661 "Pad '=' with spaces. Two cases: Assignment statement, and keyword
2662 assignment. Which case is determined using
2663 `idlwave-start-of-substatement' and `idlwave-statement-type'. The
2664 equal sign will be surrounded by BEFORE and AFTER blanks. If
2665 `idlwave-pad-keyword' is t then keyword assignment is treated just
2666 like assignment statements. When nil, spaces are removed for keyword
2667 assignment. Any other value keeps the current space around the `='.
2668 Limits in for loops are treated as keyword assignment.
2669
2670 Starting with IDL 6.0, a number of op= assignments are available.
2671 Since ambiguities of the form:
2672
2673 r and= b
2674 rand= b
2675
2676 can occur, alphanumeric operator assignment will never be pre-padded,
2677 only post-padded. You must use a space before these to disambiguate
2678 \(not just for padding, but for proper parsing by IDL too!). Other
2679 operators, such as ##=, ^=, etc., will be pre-padded.
2680
2681 See `idlwave-surround'."
2682 (if idlwave-surround-by-blank
2683 (let
2684 ((non-an-ops "\\(##\\|\\*\\|\\+\\|-\\|/\\|<\\|>\\|\\^\\)\\=")
2685 (an-ops
2686 "\\s-\\(AND\\|EQ\\|GE\\|GT\\|LE\\|LT\\|MOD\\|NE\\|OR\\|XOR\\)\\=")
2687 (len 1))
2688
2689 (save-excursion
2690 (let ((case-fold-search t))
2691 (backward-char)
2692 (if (or
2693 (re-search-backward non-an-ops nil t)
2694 ;; Why doesn't ##? work for both?
2695 (re-search-backward "\\(#\\)\\=" nil t))
2696 (setq len (1+ (length (match-string 1))))
2697 (when (re-search-backward an-ops nil t)
2698 (setq begin nil) ; won't modify begin
2699 (setq len (1+ (length (match-string 1))))))))
2700
2701 (if (eq t idlwave-pad-keyword)
2702 ;; Everything gets padded equally
2703 (idlwave-surround before after nil len)
2704 ;; Treating keywords/for variables specially...
2705 (let ((st (save-excursion ; To catch "for" variables
2706 (idlwave-start-of-substatement t)
2707 (idlwave-statement-type)))
2708 (what (save-excursion ; To catch keywords
2709 (skip-chars-backward "= \t")
2710 (nth 2 (idlwave-where)))))
2711 (cond ((or (memq what '(function-keyword procedure-keyword))
2712 (memq (caar st) '(for pdef)))
2713 (cond
2714 ((null idlwave-pad-keyword)
2715 (idlwave-surround 0 0)
2716 ) ; remove space
2717 (t))) ; leave any spaces alone
2718 (t (idlwave-surround before after nil len))))))))
2719
2720
2721 (defun idlwave-indent-and-action (&optional arg)
2722 "Call `idlwave-indent-line' and do expand actions.
2723 With prefix ARG non-nil, indent the entire sub-statement."
2724 (interactive "p")
2725 (save-excursion
2726 (if (and idlwave-expand-generic-end
2727 (re-search-backward "\\<\\(end\\)\\s-*\\="
2728 (max 0 (- (point) 10)) t)
2729 (looking-at "\\(end\\)\\([ \n\t]\\|\\'\\)"))
2730 (progn (goto-char (match-end 1))
2731 ;;Expand the END abbreviation, just as RET or Space would have.
2732 (if abbrev-mode (expand-abbrev)
2733 (idlwave-show-begin)))))
2734 (when (and (not arg) current-prefix-arg)
2735 (setq arg current-prefix-arg)
2736 (setq current-prefix-arg nil))
2737 (if arg
2738 (idlwave-indent-statement)
2739 (idlwave-indent-line t)))
2740
2741 (defun idlwave-indent-line (&optional expand)
2742 "Indents current IDL line as code or as a comment.
2743 The actions in `idlwave-indent-action-table' are performed.
2744 If the optional argument EXPAND is non-nil then the actions in
2745 `idlwave-indent-expand-table' are performed."
2746 (interactive)
2747 ;; Move point out of left margin.
2748 (if (save-excursion
2749 (skip-chars-backward " \t")
2750 (bolp))
2751 (skip-chars-forward " \t"))
2752 (let ((mloc (point-marker)))
2753 (save-excursion
2754 (beginning-of-line)
2755 (if (looking-at idlwave-comment-line-start-skip)
2756 ;; Indentation for a line comment
2757 (progn
2758 (skip-chars-forward " \t")
2759 (idlwave-indent-left-margin (idlwave-comment-hook)))
2760 ;;
2761 ;; Code Line
2762 ;;
2763 ;; Before indenting, run action routines.
2764 ;;
2765 (if (and expand idlwave-do-actions)
2766 (mapcar 'idlwave-do-action idlwave-indent-expand-table))
2767 ;;
2768 (if idlwave-do-actions
2769 (mapcar 'idlwave-do-action idlwave-indent-action-table))
2770 ;;
2771 ;; No longer expand abbrevs on the line. The user can do this
2772 ;; manually using expand-region-abbrevs.
2773 ;;
2774 ;; Indent for code line
2775 ;;
2776 (beginning-of-line)
2777 (if (or
2778 ;; a label line
2779 (looking-at (concat "^" idlwave-label "[ \t]*$"))
2780 ;; a batch command
2781 (looking-at "^[ \t]*@"))
2782 ;; leave flush left
2783 nil
2784 ;; indent the line
2785 (idlwave-indent-left-margin (idlwave-calculate-indent)))
2786 ;; Adjust parallel comment
2787 (end-of-line)
2788 (if (idlwave-in-comment)
2789 ;; Emacs 21 is too smart with fill-column on comment indent
2790 (let ((fill-column (if (fboundp 'comment-indent-new-line)
2791 (1- (frame-width))
2792 fill-column)))
2793 (indent-for-comment)))))
2794 (goto-char mloc)
2795 ;; Get rid of marker
2796 (set-marker mloc nil)))
2797
2798 (defun idlwave-do-action (action)
2799 "Perform an action repeatedly on a line.
2800 ACTION is a list (REG . FUNC). REG is a regular expression. FUNC is
2801 either a function name to be called with `funcall' or a list to be
2802 evaluated with `eval'. The action performed by FUNC should leave point
2803 after the match for REG - otherwise an infinite loop may be entered."
2804 (let ((action-key (car action))
2805 (action-routine (cdr action)))
2806 (beginning-of-line)
2807 (while (idlwave-look-at action-key)
2808 (if (listp action-routine)
2809 (eval action-routine)
2810 (funcall action-routine)))))
2811
2812 (defun idlwave-indent-to (col &optional min)
2813 "Indent from point with spaces until column COL.
2814 Inserts space before markers at point."
2815 (if (not min) (setq min 0))
2816 (insert-before-markers
2817 (make-string (max min (- col (current-column))) ?\ )))
2818
2819 (defun idlwave-indent-left-margin (col)
2820 "Indent the current line to column COL.
2821 Indents such that first non-whitespace character is at column COL
2822 Inserts spaces before markers at point."
2823 (save-excursion
2824 (beginning-of-line)
2825 (delete-horizontal-space)
2826 (idlwave-indent-to col)))
2827
2828 (defun idlwave-indent-subprogram ()
2829 "Indents program unit which contains point."
2830 (interactive)
2831 (save-excursion
2832 (idlwave-end-of-statement)
2833 (idlwave-beginning-of-subprogram)
2834 (let ((beg (point)))
2835 (idlwave-forward-block)
2836 (message "Indenting subprogram...")
2837 (indent-region beg (point) nil))
2838 (message "Indenting subprogram...done.")))
2839
2840 (defun idlwave-indent-statement ()
2841 "Indent current statement, including all continuation lines."
2842 (interactive)
2843 (save-excursion
2844 (idlwave-beginning-of-statement)
2845 (let ((beg (point)))
2846 (idlwave-end-of-statement)
2847 (indent-region beg (point) nil))))
2848
2849 (defun idlwave-calculate-indent ()
2850 "Return appropriate indentation for current line as IDL code."
2851 (save-excursion
2852 (beginning-of-line)
2853 (cond
2854 ;; Check for beginning of unit - main (beginning of buffer), pro, or
2855 ;; function
2856 ((idlwave-look-at idlwave-begin-unit-reg)
2857 0)
2858 ;; Check for continuation line
2859 ((save-excursion
2860 (and (= (forward-line -1) 0)
2861 (idlwave-is-continuation-line)))
2862 (idlwave-calculate-cont-indent))
2863 ;; calculate indent based on previous and current statements
2864 (t (let* (beg-prev-pos
2865 (the-indent
2866 ;; calculate indent based on previous statement
2867 (save-excursion
2868 (cond
2869 ;; Beginning of file
2870 ((prog1
2871 (idlwave-previous-statement)
2872 (setq beg-prev-pos (point)))
2873 0)
2874 ;; Main block
2875 ((idlwave-look-at idlwave-begin-unit-reg t)
2876 (+ (idlwave-current-statement-indent)
2877 idlwave-main-block-indent))
2878 ;; Begin block
2879 ((idlwave-look-at idlwave-begin-block-reg t)
2880 (+ (idlwave-min-current-statement-indent)
2881 idlwave-block-indent))
2882 ;; End Block
2883 ((idlwave-look-at idlwave-end-block-reg t)
2884 (progn
2885 ;; Match to the *beginning* of the block opener
2886 (goto-char beg-prev-pos)
2887 (idlwave-block-jump-out -1 'nomark) ; go to begin block
2888 (idlwave-min-current-statement-indent)))
2889 ;; idlwave-end-offset
2890 ;; idlwave-block-indent))
2891
2892 ;; Default to current indent
2893 ((idlwave-current-statement-indent))))))
2894 ;; adjust the indentation based on the current statement
2895 (cond
2896 ;; End block
2897 ((idlwave-look-at idlwave-end-block-reg)
2898 (+ the-indent idlwave-end-offset))
2899 (the-indent)))))))
2900
2901 ;;
2902 ;; Parentheses indent
2903 ;;
2904
2905 (defun idlwave-calculate-paren-indent (beg-reg end-reg close-exp)
2906 "Calculate the continuation indent inside a paren group.
2907 Returns a cons-cell with (open . indent), where open is the
2908 location of the open paren"
2909 (let ((open (nth 1 (parse-partial-sexp beg-reg end-reg))))
2910 ;; Found an innermost open paren.
2911 (when open
2912 (goto-char open)
2913 ;; Line up with next word unless this is a closing paren.
2914 (cons open
2915 (cond
2916 ;; Plain Kernighan-style nested indent
2917 (idlwave-indent-parens-nested
2918 (+ idlwave-continuation-indent (idlwave-current-indent)))
2919
2920 ;; This is a closed paren - line up under open paren.
2921 (close-exp
2922 (current-column))
2923
2924 ;; Empty (or just comment) follows -- revert to basic indent
2925 ((progn
2926 ;; Skip paren
2927 (forward-char 1)
2928 (looking-at "[ \t$]*\\(;.*\\)?$"))
2929 nil)
2930
2931 ;; Line up with first word after any blank space
2932 ((progn
2933 (skip-chars-forward " \t")
2934 (current-column))))))))
2935
2936 (defun idlwave-calculate-cont-indent ()
2937 "Calculates the IDL continuation indent column from the previous
2938 statement. Note that here previous statement usually means the
2939 beginning of the current statement if this statement is a continuation
2940 of the previous line. Various special types of continuations,
2941 including assignments, routine definitions, and parenthetical
2942 groupings, are treated separately."
2943 (save-excursion
2944 (let* ((case-fold-search t)
2945 (end-reg (progn (beginning-of-line) (point)))
2946 (beg-last-statement (save-excursion (idlwave-previous-statement)
2947 (point)))
2948 (beg-reg (progn (idlwave-start-of-substatement 'pre)
2949 (if (eq (line-beginning-position) end-reg)
2950 (goto-char beg-last-statement)
2951 (point))))
2952 (basic-indent (+ (idlwave-min-current-statement-indent end-reg)
2953 idlwave-continuation-indent))
2954 fancy-nonparen-indent fancy-paren-indent)
2955 (cond
2956 ;; Align then with its matching if, etc.
2957 ((let ((matchers '(("\\<if\\>" . "[ \t]*then")
2958 ("\\<\\(if\\|end\\(if\\)?\\)\\>" . "[ \t]*else")
2959 ("\\<\\(for\\|while\\)\\>" . "[ \t]*do")
2960 ("\\<\\(repeat\\|end\\(rep\\)?\\)\\>" .
2961 "[ \t]*until")
2962 ("\\<case\\>" . "[ \t]*of")))
2963 match cont-re)
2964 (goto-char end-reg)
2965 (and
2966 (setq cont-re
2967 (catch 'exit
2968 (while (setq match (car matchers))
2969 (if (looking-at (cdr match))
2970 (throw 'exit (car match)))
2971 (setq matchers (cdr matchers)))))
2972 (idlwave-find-key cont-re -1 'nomark beg-last-statement)))
2973 (if (looking-at "end") ;; that one's special
2974 (- (idlwave-current-indent)
2975 (+ idlwave-block-indent idlwave-end-offset))
2976 (idlwave-current-indent)))
2977
2978 ;; Indent in from the previous line for continuing statements
2979 ((let ((matchers '("\\<then\\>"
2980 "\\<do\\>"
2981 "\\<repeat\\>"
2982 "\\<else\\>"))
2983 match)
2984 (catch 'exit
2985 (goto-char end-reg)
2986 (if (/= (forward-line -1) 0)
2987 (throw 'exit nil))
2988 (while (setq match (car matchers))
2989 (if (looking-at (concat ".*" match "[ \t]*\\$[ \t]*"
2990 "\\(;.*\\)?$"))
2991 (throw 'exit t))
2992 (setq matchers (cdr matchers)))))
2993 (+ idlwave-continuation-indent (idlwave-current-indent)))
2994
2995 ;; Parenthetical indent, either traditional or Kernighan style
2996 ((setq fancy-paren-indent
2997 (let* ((end-reg end-reg)
2998 (close-exp (progn
2999 (goto-char end-reg)
3000 (skip-chars-forward " \t")
3001 (looking-at "\\s)")))
3002 indent-cons)
3003 (catch 'loop
3004 (while (setq indent-cons (idlwave-calculate-paren-indent
3005 beg-reg end-reg close-exp))
3006 ;; First permitted containing paren
3007 (if (or
3008 idlwave-indent-to-open-paren
3009 idlwave-indent-parens-nested
3010 (null (cdr indent-cons))
3011 (< (- (cdr indent-cons) basic-indent)
3012 idlwave-max-extra-continuation-indent))
3013 (throw 'loop (cdr indent-cons)))
3014 (setq end-reg (car indent-cons))))))
3015 fancy-paren-indent)
3016
3017 ;; A continued assignment, or procedure call/definition
3018 ((and
3019 (> idlwave-max-extra-continuation-indent 0)
3020 (setq fancy-nonparen-indent
3021 (progn
3022 (goto-char beg-reg)
3023 (while (idlwave-look-at "&")) ; skip continued statements
3024 (cond
3025 ;; A continued Procedure call or definition
3026 ((progn
3027 (idlwave-look-at "^[ \t]*\\(pro\\|function\\)") ;skip over
3028 (looking-at "[ \t]*\\([a-zA-Z0-9.$_]+[ \t]*->[ \t]*\\)?[a-zA-Z][:a-zA-Z0-9$_]*[ \t]*\\(,\\)[ \t]*"))
3029 (goto-char (match-end 0))
3030 ;; Comment only, or blank line with "$"? Basic indent.
3031 (if (save-match-data (looking-at "[ \t$]*\\(;.*\\)?$"))
3032 nil
3033 (current-column)))
3034
3035 ;; Continued assignment (with =):
3036 ((catch 'assign ;
3037 (while (looking-at "[^=\n\r]*\\(=\\)[ \t]*")
3038 (goto-char (match-end 0))
3039 (if (null (idlwave-what-function beg-reg))
3040 (throw 'assign t))))
3041 (unless (or
3042 (idlwave-in-quote)
3043 (looking-at "[ \t$]*\\(;.*\\)?$") ; use basic
3044 (save-excursion
3045 (goto-char beg-last-statement)
3046 (eq (caar (idlwave-statement-type)) 'for)))
3047 (current-column))))))
3048 (< (- fancy-nonparen-indent basic-indent)
3049 idlwave-max-extra-continuation-indent))
3050 (if fancy-paren-indent ;calculated but disallowed paren indent
3051 (+ fancy-nonparen-indent idlwave-continuation-indent)
3052 fancy-nonparen-indent))
3053
3054 ;; Basic indent, by default
3055 (t basic-indent)))))
3056
3057
3058
3059 (defun idlwave-find-key (key-re &optional dir nomark limit)
3060 "Move to next match of the regular expression KEY-RE.
3061 Matches inside comments or string constants will be ignored.
3062 If DIR is negative, the search will be backwards.
3063 At a successful match, the mark is pushed unless NOMARK is non-nil.
3064 Searches are limited to LIMIT.
3065 Searches are case-insensitive and use a special syntax table which
3066 treats `$' and `_' as word characters.
3067 Return value is the beginning of the match or (in case of failure) nil."
3068 (setq dir (or dir 0))
3069 (let ((case-fold-search t)
3070 (search-func (if (> dir 0) 're-search-forward 're-search-backward))
3071 found)
3072 (idlwave-with-special-syntax
3073 (save-excursion
3074 (catch 'exit
3075 (while (funcall search-func key-re limit t)
3076 (if (not (idlwave-quoted))
3077 (throw 'exit (setq found (match-beginning 0)))
3078 (if (or (and (> dir 0) (eobp))
3079 (and (< dir 0) (bobp)))
3080 (throw 'exit nil)))))))
3081 (if found
3082 (progn
3083 (if (not nomark) (push-mark))
3084 (goto-char found)
3085 found)
3086 nil)))
3087
3088 (defun idlwave-block-jump-out (&optional dir nomark)
3089 "When optional argument DIR is non-negative, move forward to end of
3090 current block using the `idlwave-begin-block-reg' and `idlwave-end-block-reg'
3091 regular expressions. When DIR is negative, move backwards to block beginning.
3092 Recursively calls itself to skip over nested blocks. DIR defaults to
3093 forward. Calls `push-mark' unless the optional argument NOMARK is
3094 non-nil. Movement is limited by the start of program units because of
3095 possibility of unbalanced blocks."
3096 (interactive "P")
3097 (or dir (setq dir 0))
3098 (let* ((here (point))
3099 (case-fold-search t)
3100 (limit (if (>= dir 0) (point-max) (point-min)))
3101 (block-limit (if (>= dir 0)
3102 idlwave-begin-block-reg
3103 idlwave-end-block-reg))
3104 found
3105 (block-reg (concat idlwave-begin-block-reg "\\|"
3106 idlwave-end-block-reg))
3107 (unit-limit (or (save-excursion
3108 (if (< dir 0)
3109 (idlwave-find-key
3110 idlwave-begin-unit-reg dir t limit)
3111 (end-of-line)
3112 (idlwave-find-key
3113 idlwave-end-unit-reg dir t limit)))
3114 limit)))
3115 (if (>= dir 0) (end-of-line)) ;Make sure we are in current block
3116 (if (setq found (idlwave-find-key block-reg dir t unit-limit))
3117 (while (and found (looking-at block-limit))
3118 (if (>= dir 0) (forward-word 1))
3119 (idlwave-block-jump-out dir t)
3120 (setq found (idlwave-find-key block-reg dir t unit-limit))))
3121 (if (not nomark) (push-mark here))
3122 (if (not found) (goto-char unit-limit)
3123 (if (>= dir 0) (forward-word 1)))))
3124
3125 (defun idlwave-min-current-statement-indent (&optional end-reg)
3126 "The minimum indent in the current statement."
3127 (idlwave-beginning-of-statement)
3128 (if (not (idlwave-is-continuation-line))
3129 (idlwave-current-indent)
3130 (let ((min (idlwave-current-indent)) comm-or-empty)
3131 (while (and (= (forward-line 1) 0)
3132 (or (setq comm-or-empty (idlwave-is-comment-or-empty-line))
3133 (idlwave-is-continuation-line))
3134 (or (null end-reg) (< (point) end-reg)))
3135 (unless comm-or-empty (setq min (min min (idlwave-current-indent)))))
3136 (if (or comm-or-empty (and end-reg (>= (point) end-reg)))
3137 min
3138 (min min (idlwave-current-indent))))))
3139
3140 (defun idlwave-current-statement-indent (&optional last-line)
3141 "Return indentation of the current statement.
3142 If in a statement, moves to beginning of statement before finding indent."
3143 (if last-line
3144 (idlwave-end-of-statement)
3145 (idlwave-beginning-of-statement))
3146 (idlwave-current-indent))
3147
3148 (defun idlwave-current-indent ()
3149 "Return the column of the indentation of the current line.
3150 Skips any whitespace. Returns 0 if the end-of-line follows the whitespace."
3151 (save-excursion
3152 (beginning-of-line)
3153 (skip-chars-forward " \t")
3154 ;; if we are at the end of blank line return 0
3155 (cond ((eolp) 0)
3156 ((current-column)))))
3157
3158 (defun idlwave-is-continuation-line ()
3159 "Tests if current line is continuation line.
3160 Blank or comment-only lines following regular continuation lines (with
3161 `$') count as continuations too."
3162 (save-excursion
3163 (or
3164 (idlwave-look-at "\\<\\$")
3165 (catch 'loop
3166 (while (and (looking-at "^[ \t]*\\(;.*\\)?$")
3167 (eq (forward-line -1) 0))
3168 (if (idlwave-look-at "\\<\\$") (throw 'loop t)))))))
3169
3170 (defun idlwave-is-comment-line ()
3171 "Tests if the current line is a comment line."
3172 (save-excursion
3173 (beginning-of-line 1)
3174 (looking-at "[ \t]*;")))
3175
3176 (defun idlwave-is-comment-or-empty-line ()
3177 "Tests if the current line is a comment line."
3178 (save-excursion
3179 (beginning-of-line 1)
3180 (looking-at "[ \t]*[;\n]")))
3181
3182 (defun idlwave-look-at (regexp &optional cont beg)
3183 "Searches current line from current point for REGEXP.
3184 If optional argument CONT is non-nil, searches to the end of
3185 the current statement.
3186 If optional arg BEG is non-nil, search starts from the beginning of the
3187 current statement.
3188 Ignores matches that end in a comment or inside a string expression.
3189 Returns point if successful, nil otherwise.
3190 This function produces unexpected results if REGEXP contains quotes or
3191 a comment delimiter. The search is case insensitive.
3192 If successful leaves point after the match, otherwise, does not move point."
3193 (let ((here (point))
3194 (case-fold-search t)
3195 (eos (save-excursion
3196 (if cont (idlwave-end-of-statement) (end-of-line))
3197 (point)))
3198 found)
3199 (idlwave-with-special-syntax
3200 (if beg (idlwave-beginning-of-statement))
3201 (while (and (setq found (re-search-forward regexp eos t))
3202 (idlwave-quoted))))
3203 (if (not found) (goto-char here))
3204 found))
3205
3206 (defun idlwave-fill-paragraph (&optional nohang)
3207 "Fills paragraphs in comments.
3208 A paragraph is made up of all contiguous lines having the same comment
3209 leader (the leading whitespace before the comment delimiter and the
3210 comment delimiter). In addition, paragraphs are separated by blank
3211 line comments. The indentation is given by the hanging indent of the
3212 first line, otherwise by the minimum indentation of the lines after
3213 the first line. The indentation of the first line does not change.
3214 Does not effect code lines. Does not fill comments on the same line
3215 with code. The hanging indent is given by the end of the first match
3216 matching `idlwave-hang-indent-regexp' on the paragraph's first line . If the
3217 optional argument NOHANG is non-nil then the hanging indent is
3218 ignored."
3219 (interactive "P")
3220 ;; check if this is a line comment
3221 (if (save-excursion
3222 (beginning-of-line)
3223 (skip-chars-forward " \t")
3224 (looking-at comment-start))
3225 (let
3226 ((indent 999)
3227 pre here diff fill-prefix-reg bcl first-indent
3228 hang start end)
3229 ;; Change tabs to spaces in the surrounding paragraph.
3230 ;; The surrounding paragraph will be the largest containing block of
3231 ;; contiguous line comments. Thus, we may be changing tabs in
3232 ;; a much larger area than is needed, but this is the easiest
3233 ;; brute force way to do it.
3234 ;;
3235 ;; This has the undesirable side effect of replacing the tabs
3236 ;; permanently without the user's request or knowledge.
3237 (save-excursion
3238 (backward-paragraph)
3239 (setq start (point)))
3240 (save-excursion
3241 (forward-paragraph)
3242 (setq end (point)))
3243 (untabify start end)
3244 ;;
3245 (setq here (point))
3246 (beginning-of-line)
3247 (setq bcl (point))
3248 (re-search-forward
3249 (concat "^[ \t]*" comment-start "+")
3250 (save-excursion (end-of-line) (point))
3251 t)
3252 ;; Get the comment leader on the line and its length
3253 (setq pre (current-column))
3254 ;; the comment leader is the indentation plus exactly the
3255 ;; number of consecutive ";".
3256 (setq fill-prefix-reg
3257 (concat
3258 (setq fill-prefix
3259 (regexp-quote
3260 (buffer-substring (save-excursion
3261 (beginning-of-line) (point))
3262 (point))))
3263 "[^;]"))
3264
3265 ;; Mark the beginning and end of the paragraph
3266 (goto-char bcl)
3267 (while (and (looking-at fill-prefix-reg)
3268 (not (looking-at paragraph-separate))
3269 (not (bobp)))
3270 (forward-line -1))
3271 ;; Move to first line of paragraph
3272 (if (/= (point) bcl)
3273 (forward-line 1))
3274 (setq start (point))
3275 (goto-char bcl)
3276 (while (and (looking-at fill-prefix-reg)
3277 (not (looking-at paragraph-separate))
3278 (not (eobp)))
3279 (forward-line 1))
3280 (beginning-of-line)
3281 (if (or (not (looking-at fill-prefix-reg))
3282 (looking-at paragraph-separate))
3283 (forward-line -1))
3284 (end-of-line)
3285 ;; if at end of buffer add a newline (need this because
3286 ;; fill-region needs END to be at the beginning of line after
3287 ;; the paragraph or it will add a line).
3288 (if (eobp)
3289 (progn (insert ?\n) (backward-char 1)))
3290 ;; Set END to the beginning of line after the paragraph
3291 ;; END is calculated as distance from end of buffer
3292 (setq end (- (point-max) (point) 1))
3293 ;;
3294 ;; Calculate the indentation for the paragraph.
3295 ;;
3296 ;; In the following while statements, after one iteration
3297 ;; point will be at the beginning of a line in which case
3298 ;; the while will not be executed for the
3299 ;; the first paragraph line and thus will not affect the
3300 ;; indentation.
3301 ;;
3302 ;; First check to see if indentation is based on hanging indent.
3303 (if (and (not nohang) idlwave-hanging-indent
3304 (setq hang
3305 (save-excursion
3306 (goto-char start)
3307 (idlwave-calc-hanging-indent))))
3308 ;; Adjust lines of paragraph by inserting spaces so that
3309 ;; each line's indent is at least as great as the hanging
3310 ;; indent. This is needed for fill-paragraph to work with
3311 ;; a fill-prefix.
3312 (progn
3313 (setq indent hang)
3314 (beginning-of-line)
3315 (while (> (point) start)
3316 (re-search-forward comment-start-skip
3317 (save-excursion (end-of-line) (point))
3318 t)
3319 (if (> (setq diff (- indent (current-column))) 0)
3320 (progn
3321 (if (>= here (point))
3322 ;; adjust the original location for the
3323 ;; inserted text.
3324 (setq here (+ here diff)))
3325 (insert (make-string diff ?\ ))))
3326 (forward-line -1))
3327 )
3328
3329 ;; No hang. Instead find minimum indentation of paragraph
3330 ;; after first line.
3331 ;; For the following while statement, since START is at the
3332 ;; beginning of line and END is at the end of line
3333 ;; point is greater than START at least once (which would
3334 ;; be the case for a single line paragraph).
3335 (while (> (point) start)
3336 (beginning-of-line)
3337 (setq indent
3338 (min indent
3339 (progn
3340 (re-search-forward
3341 comment-start-skip
3342 (save-excursion (end-of-line) (point))
3343 t)
3344 (current-column))))
3345 (forward-line -1))
3346 )
3347 (setq fill-prefix (concat fill-prefix
3348 (make-string (- indent pre)
3349 ?\ )))
3350 ;; first-line indent
3351 (setq first-indent
3352 (max
3353 (progn
3354 (re-search-forward
3355 comment-start-skip
3356 (save-excursion (end-of-line) (point))
3357 t)
3358 (current-column))
3359 indent))
3360
3361 ;; try to keep point at its original place
3362 (goto-char here)
3363
3364 ;; In place of the more modern fill-region-as-paragraph, a hack
3365 ;; to keep whitespace untouched on the first line within the
3366 ;; indent length and to preserve any indent on the first line
3367 ;; (first indent).
3368 (save-excursion
3369 (setq diff
3370 (buffer-substring start (+ start first-indent -1)))
3371 (subst-char-in-region start (+ start first-indent -1) ?\ ?~ nil)
3372 (fill-region-as-paragraph
3373 start
3374 (- (point-max) end)
3375 (current-justification)
3376 nil)
3377 (delete-region start (+ start first-indent -1))
3378 (goto-char start)
3379 (insert diff))
3380 ;; When we want the point at the beginning of the comment
3381 ;; body fill-region will put it at the beginning of the line.
3382 (if (bolp) (skip-chars-forward (concat " \t" comment-start)))
3383 (setq fill-prefix nil))))
3384
3385 (defun idlwave-calc-hanging-indent ()
3386 "Calculate the position of the hanging indent for the comment
3387 paragraph. The hanging indent position is given by the first match
3388 with the `idlwave-hang-indent-regexp'. If `idlwave-use-last-hang-indent' is
3389 non-nil then use last occurrence matching `idlwave-hang-indent-regexp' on
3390 the line.
3391 If not found returns nil."
3392 (if idlwave-use-last-hang-indent
3393 (save-excursion
3394 (end-of-line)
3395 (if (re-search-backward
3396 idlwave-hang-indent-regexp
3397 (save-excursion (beginning-of-line) (point))
3398 t)
3399 (+ (current-column) (length idlwave-hang-indent-regexp))))
3400 (save-excursion
3401 (beginning-of-line)
3402 (if (re-search-forward
3403 idlwave-hang-indent-regexp
3404 (save-excursion (end-of-line) (point))
3405 t)
3406 (current-column)))))
3407
3408 (defun idlwave-auto-fill ()
3409 "Called to break lines in auto fill mode.
3410 Only fills non-comment lines if `idlwave-fill-comment-line-only' is
3411 non-nil. Places a continuation character at the end of the line if
3412 not in a comment. Splits strings with IDL concatenation operator `+'
3413 if `idlwave-auto-fill-split-string' is non-nil."
3414 (if (<= (current-column) fill-column)
3415 nil ; do not to fill
3416 (if (or (not idlwave-fill-comment-line-only)
3417 (save-excursion
3418 ;; Check for comment line
3419 (beginning-of-line)
3420 (looking-at idlwave-comment-line-start-skip)))
3421 (let (beg)
3422 (idlwave-indent-line)
3423 ;; Prevent actions do-auto-fill which calls indent-line-function.
3424 (let (idlwave-do-actions
3425 (paragraph-start ".")
3426 (paragraph-separate ".")
3427 (fill-nobreak-predicate
3428 (if (and (idlwave-in-quote)
3429 idlwave-auto-fill-split-string)
3430 (lambda () ;; We'll need 5 spaces for " ' + $"
3431 (<= (- fill-column (current-column)) 5)
3432 ))))
3433 (do-auto-fill))
3434 (save-excursion
3435 (end-of-line 0)
3436 ;; Indent the split line
3437 (idlwave-indent-line)
3438 )
3439 (if (save-excursion
3440 (beginning-of-line)
3441 (looking-at idlwave-comment-line-start-skip))
3442 ;; A continued line comment
3443 ;; We treat continued line comments as part of a comment
3444 ;; paragraph. So we check for a hanging indent.
3445 (if idlwave-hanging-indent
3446 (let ((here (- (point-max) (point)))
3447 (indent
3448 (save-excursion
3449 (forward-line -1)
3450 (idlwave-calc-hanging-indent))))
3451 (if indent
3452 (progn
3453 ;; Remove whitespace between comment delimiter and
3454 ;; text, insert spaces for appropriate indentation.
3455 (beginning-of-line)
3456 (re-search-forward
3457 comment-start-skip
3458 (save-excursion (end-of-line) (point)) t)
3459 (delete-horizontal-space)
3460 (idlwave-indent-to indent)
3461 (goto-char (- (point-max) here)))
3462 )))
3463 ;; Split code or comment?
3464 (if (save-excursion
3465 (end-of-line 0)
3466 (idlwave-in-comment))
3467 ;; Splitting a non-full-line comment.
3468 ;; Insert the comment delimiter from split line
3469 (progn
3470 (save-excursion
3471 (beginning-of-line)
3472 (skip-chars-forward " \t")
3473 ;; Insert blank to keep off beginning of line
3474 (insert " "
3475 (save-excursion
3476 (forward-line -1)
3477 (buffer-substring (idlwave-goto-comment)
3478 (progn
3479 (skip-chars-forward "; ")
3480 (point))))))
3481 (idlwave-indent-line))
3482 ;; Split code line - add continuation character
3483 (save-excursion
3484 (end-of-line 0)
3485 ;; Check to see if we split a string
3486 (if (and (setq beg (idlwave-in-quote))
3487 idlwave-auto-fill-split-string)
3488 ;; Split the string and concatenate.
3489 ;; The first extra space is for the space
3490 ;; the line was split. That space was removed.
3491 (insert " " (char-after beg) " +"))
3492 (insert " $"))
3493 (if beg
3494 (if idlwave-auto-fill-split-string
3495 ;; Make the second part of continued string
3496 (save-excursion
3497 (beginning-of-line)
3498 (skip-chars-forward " \t")
3499 (insert (char-after beg)))
3500 ;; Warning
3501 (beep)
3502 (message "Warning: continuation inside a string.")))
3503 ;; Although do-auto-fill (via indent-new-comment-line) calls
3504 ;; idlwave-indent-line for the new line, re-indent again
3505 ;; because of the addition of the continuation character.
3506 (idlwave-indent-line))
3507 )))))
3508
3509 (defun idlwave-auto-fill-mode (arg)
3510 "Toggle auto-fill mode for IDL mode.
3511 With arg, turn auto-fill mode on if arg is positive.
3512 In auto-fill mode, inserting a space at a column beyond `fill-column'
3513 automatically breaks the line at a previous space."
3514 (interactive "P")
3515 (prog1 (set idlwave-fill-function
3516 (if (if (null arg)
3517 (not (symbol-value idlwave-fill-function))
3518 (> (prefix-numeric-value arg) 0))
3519 'idlwave-auto-fill
3520 nil))
3521 ;; update mode-line
3522 (set-buffer-modified-p (buffer-modified-p))))
3523
3524 ;(defun idlwave-fill-routine-call ()
3525 ; "Fill a routine definition or statement, indenting appropriately."
3526 ; (let ((where (idlwave-where)))))
3527
3528
3529 (defun idlwave-doc-header (&optional nomark )
3530 "Insert a documentation header at the beginning of the unit.
3531 Inserts the value of the variable idlwave-file-header. Sets mark before
3532 moving to do insertion unless the optional prefix argument NOMARK
3533 is non-nil."
3534 (interactive "P")
3535 (or nomark (push-mark))
3536 ;; make sure we catch the current line if it begins the unit
3537 (if idlwave-header-to-beginning-of-file
3538 (goto-char (point-min))
3539 (end-of-line)
3540 (idlwave-beginning-of-subprogram)
3541 (beginning-of-line)
3542 ;; skip function or procedure line
3543 (if (idlwave-look-at "\\<\\(pro\\|function\\)\\>")
3544 (progn
3545 (idlwave-end-of-statement)
3546 (if (> (forward-line 1) 0) (insert "\n")))))
3547 (let ((pos (point)))
3548 (if idlwave-file-header
3549 (cond ((car idlwave-file-header)
3550 (insert-file (car idlwave-file-header)))
3551 ((stringp (car (cdr idlwave-file-header)))
3552 (insert (car (cdr idlwave-file-header))))))
3553 (goto-char pos)))
3554
3555 (defun idlwave-default-insert-timestamp ()
3556 "Default timestamp insertion function"
3557 (insert (current-time-string))
3558 (insert ", " (user-full-name))
3559 (if (boundp 'user-mail-address)
3560 (insert " <" user-mail-address ">")
3561 (insert " <" (user-login-name) "@" (system-name) ">"))
3562 ;; Remove extra spaces from line
3563 (idlwave-fill-paragraph)
3564 ;; Insert a blank line comment to separate from the date entry -
3565 ;; will keep the entry from flowing onto date line if re-filled.
3566 (insert "\n;\n;\t\t"))
3567
3568 (defun idlwave-doc-modification ()
3569 "Insert a brief modification log at the beginning of the current program.
3570 Looks for an occurrence of the value of user variable
3571 `idlwave-doc-modifications-keyword' if non-nil. Inserts time and user name
3572 and places the point for the user to add a log. Before moving, saves
3573 location on mark ring so that the user can return to previous point."
3574 (interactive)
3575 (push-mark)
3576 (let* (beg end)
3577 (if (and (or (re-search-backward idlwave-doclib-start nil t)
3578 (progn
3579 (goto-char (point-min))
3580 (re-search-forward idlwave-doclib-start nil t)))
3581 (setq beg (match-beginning 0))
3582 (re-search-forward idlwave-doclib-end nil t)
3583 (setq end (match-end 0)))
3584 (progn
3585 (goto-char beg)
3586 (if (re-search-forward
3587 (concat idlwave-doc-modifications-keyword ":")
3588 end t)
3589 (end-of-line)
3590 (goto-char end)
3591 (end-of-line -1)
3592 (insert "\n" comment-start "\n")
3593 (insert comment-start " " idlwave-doc-modifications-keyword ":"))
3594 (insert "\n;\n;\t")
3595 (run-hooks 'idlwave-timestamp-hook))
3596 (error "No valid DOCLIB header"))))
3597
3598 ;;; CJC 3/16/93
3599 ;;; Interface to expand-region-abbrevs which did not work when the
3600 ;;; abbrev hook associated with an abbrev moves point backwards
3601 ;;; after abbrev expansion, e.g., as with the abbrev '.n'.
3602 ;;; The original would enter an infinite loop in attempting to expand
3603 ;;; .n (it would continually expand and unexpand the abbrev without expanding
3604 ;;; because the point would keep going back to the beginning of the
3605 ;;; abbrev instead of to the end of the abbrev). We now keep the
3606 ;;; abbrev hook from moving backwards.
3607 ;;;
3608 (defun idlwave-expand-region-abbrevs (start end)
3609 "Expand each abbrev occurrence in the region.
3610 Calling from a program, arguments are START END."
3611 (interactive "r")
3612 (save-excursion
3613 (goto-char (min start end))
3614 (let ((idlwave-show-block nil) ;Do not blink
3615 (idlwave-abbrev-move nil)) ;Do not move
3616 (expand-region-abbrevs start end 'noquery))))
3617
3618 (defun idlwave-quoted ()
3619 "Returns t if point is in a comment or quoted string.
3620 nil otherwise."
3621 (or (idlwave-in-comment) (idlwave-in-quote)))
3622
3623 (defun idlwave-in-quote ()
3624 "Returns location of the opening quote
3625 if point is in a IDL string constant, nil otherwise.
3626 Ignores comment delimiters on the current line.
3627 Properly handles nested quotation marks and octal
3628 constants - a double quote followed by an octal digit."
3629 ;;; Treat an octal inside an apostrophe to be a normal string. Treat a
3630 ;;; double quote followed by an octal digit to be an octal constant
3631 ;;; rather than a string. Therefore, there is no terminating double
3632 ;;; quote.
3633 (save-excursion
3634 ;; Because single and double quotes can quote each other we must
3635 ;; search for the string start from the beginning of line.
3636 (let* ((start (point))
3637 (eol (progn (end-of-line) (point)))
3638 (bq (progn (beginning-of-line) (point)))
3639 (endq (point))
3640 (data (match-data))
3641 delim
3642 found)
3643 (while (< endq start)
3644 ;; Find string start
3645 ;; Don't find an octal constant beginning with a double quote
3646 (if (re-search-forward "[\"']" eol 'lim)
3647 ;; Find the string end.
3648 ;; In IDL, two consecutive delimiters after the start of a
3649 ;; string act as an
3650 ;; escape for the delimiter in the string.
3651 ;; Two consecutive delimiters alone (i.e., not after the
3652 ;; start of a string) is the null string.
3653 (progn
3654 ;; Move to position after quote
3655 (goto-char (1+ (match-beginning 0)))
3656 (setq bq (1- (point)))
3657 ;; Get the string delimiter
3658 (setq delim (char-to-string (preceding-char)))
3659 ;; Check for null string
3660 (if (looking-at delim)
3661 (progn (setq endq (point)) (forward-char 1))
3662 ;; Look for next unpaired delimiter
3663 (setq found (search-forward delim eol 'lim))
3664 (while (looking-at delim)
3665 (forward-char 1)
3666 (setq found (search-forward delim eol 'lim)))
3667 (if found
3668 (setq endq (- (point) 1))
3669 (setq endq (point)))
3670 ))
3671 (progn (setq bq (point)) (setq endq (point)))))
3672 (store-match-data data)
3673 ;; return string beginning position or nil
3674 (if (> start bq) bq))))
3675
3676 (defun idlwave-is-pointer-dereference (&optional limit)
3677 "Determines if the character after point is a pointer dereference *."
3678 (let ((pos (point)))
3679 (and
3680 (eq (char-after) ?\*)
3681 (not (idlwave-in-quote))
3682 (save-excursion
3683 (forward-char)
3684 (re-search-backward (concat "\\(" idlwave-idl-keywords
3685 "\\|[[(*+-/=,^><]\\)\\s-*\\*") limit t)))))
3686
3687
3688 ;; Statement templates
3689
3690 ;; Replace these with a general template function, something like
3691 ;; expand.el (I think there was also something with a name similar to
3692 ;; dmacro.el)
3693
3694 (defun idlwave-template (s1 s2 &optional prompt noindent)
3695 "Build a template with optional prompt expression.
3696
3697 Opens a line if point is not followed by a newline modulo intervening
3698 whitespace. S1 and S2 are strings. S1 is inserted at point followed
3699 by S2. Point is inserted between S1 and S2. The case of S1 and S2 is
3700 adjusted according to `idlwave-abbrev-change-case'. If optional argument
3701 PROMPT is a string then it is displayed as a message in the
3702 minibuffer. The PROMPT serves as a reminder to the user of an
3703 expression to enter.
3704
3705 The lines containing S1 and S2 are reindented using `indent-region'
3706 unless the optional second argument NOINDENT is non-nil."
3707 (if (eq major-mode 'idlwave-shell-mode)
3708 ;; This is a gross hack to avoit template abbrev expansion
3709 ;; in the shell. FIXME: This is a dirty hack.
3710 (if (and (eq this-command 'self-insert-command)
3711 (equal last-abbrev-location (point)))
3712 (insert last-abbrev-text)
3713 (error "No templates in idlwave-shell"))
3714 (cond ((eq idlwave-abbrev-change-case 'down)
3715 (setq s1 (downcase s1) s2 (downcase s2)))
3716 (idlwave-abbrev-change-case
3717 (setq s1 (upcase s1) s2 (upcase s2))))
3718 (let ((beg (save-excursion (beginning-of-line) (point)))
3719 end)
3720 (if (not (looking-at "\\s-*\n"))
3721 (open-line 1))
3722 (insert s1)
3723 (save-excursion
3724 (insert s2)
3725 (setq end (point)))
3726 (if (not noindent)
3727 (indent-region beg end nil))
3728 (if (stringp prompt)
3729 (message prompt)))))
3730
3731 (defun idlwave-rw-case (string)
3732 "Make STRING have the case required by `idlwave-reserved-word-upcase'."
3733 (if idlwave-reserved-word-upcase
3734 (upcase string)
3735 string))
3736
3737 (defun idlwave-elif ()
3738 "Build skeleton IDL if-else block."
3739 (interactive)
3740 (idlwave-template
3741 (idlwave-rw-case "if")
3742 (idlwave-rw-case " then begin\n\nendif else begin\n\nendelse")
3743 "Condition expression"))
3744
3745 (defun idlwave-case ()
3746 "Build skeleton IDL case statement."
3747 (interactive)
3748 (idlwave-template
3749 (idlwave-rw-case "case")
3750 (idlwave-rw-case " of\n\nendcase")
3751 "Selector expression"))
3752
3753 (defun idlwave-switch ()
3754 "Build skeleton IDL switch statement."
3755 (interactive)
3756 (idlwave-template
3757 (idlwave-rw-case "switch")
3758 (idlwave-rw-case " of\n\nendswitch")
3759 "Selector expression"))
3760
3761 (defun idlwave-for ()
3762 "Build skeleton for loop statment."
3763 (interactive)
3764 (idlwave-template
3765 (idlwave-rw-case "for")
3766 (idlwave-rw-case " do begin\n\nendfor")
3767 "Loop expression"))
3768
3769 (defun idlwave-if ()
3770 "Build skeleton for loop statment."
3771 (interactive)
3772 (idlwave-template
3773 (idlwave-rw-case "if")
3774 (idlwave-rw-case " then begin\n\nendif")
3775 "Scalar logical expression"))
3776
3777 (defun idlwave-procedure ()
3778 (interactive)
3779 (idlwave-template
3780 (idlwave-rw-case "pro")
3781 (idlwave-rw-case "\n\nreturn\nend")
3782 "Procedure name"))
3783
3784 (defun idlwave-function ()
3785 (interactive)
3786 (idlwave-template
3787 (idlwave-rw-case "function")
3788 (idlwave-rw-case "\n\nreturn\nend")
3789 "Function name"))
3790
3791 (defun idlwave-repeat ()
3792 (interactive)
3793 (idlwave-template
3794 (idlwave-rw-case "repeat begin\n\nendrep until")
3795 (idlwave-rw-case "")
3796 "Exit condition"))
3797
3798 (defun idlwave-while ()
3799 (interactive)
3800 (idlwave-template
3801 (idlwave-rw-case "while")
3802 (idlwave-rw-case " do begin\n\nendwhile")
3803 "Entry condition"))
3804
3805 (defun idlwave-split-string (string &optional pattern)
3806 "Return a list of substrings of STRING which are separated by PATTERN.
3807 If PATTERN is omitted, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
3808 (or pattern
3809 (setq pattern "[ \f\t\n\r\v]+"))
3810 (let (parts (start 0))
3811 (while (string-match pattern string start)
3812 (setq parts (cons (substring string start (match-beginning 0)) parts)
3813 start (match-end 0)))
3814 (nreverse (cons (substring string start) parts))))
3815
3816 (defun idlwave-replace-string (string replace_string replace_with)
3817 (let* ((start 0)
3818 (last (length string))
3819 (ret_string "")
3820 end)
3821 (while (setq end (string-match replace_string string start))
3822 (setq ret_string
3823 (concat ret_string (substring string start end) replace_with))
3824 (setq start (match-end 0)))
3825 (setq ret_string (concat ret_string (substring string start last)))))
3826
3827 (defun idlwave-get-buffer-visiting (file)
3828 ;; Return the buffer currently visiting FILE
3829 (cond
3830 ((boundp 'find-file-compare-truenames) ; XEmacs
3831 (let ((find-file-compare-truenames t))
3832 (get-file-buffer file)))
3833 ((fboundp 'find-buffer-visiting) ; Emacs
3834 (find-buffer-visiting file))
3835 (t (error "This should not happen (idlwave-get-buffer-visiting)"))))
3836
3837 (defvar idlwave-outlawed-buffers nil
3838 "List of buffer pulled up by idlwave for special reasons.
3839 Buffers in this list may be killed by `idlwave-kill-autoloaded-buffers'.")
3840
3841 (defun idlwave-find-file-noselect (file &optional why)
3842 ;; Return a buffer visiting file.
3843 (or (idlwave-get-buffer-visiting file)
3844 (let ((buf (find-file-noselect file)))
3845 (if why (add-to-list 'idlwave-outlawed-buffers (cons buf why)))
3846 buf)))
3847
3848 (defun idlwave-kill-autoloaded-buffers ()
3849 "Kill buffers created automatically by IDLWAVE.
3850 Function prompts for a letter to identify the buffers to kill.
3851 Possible letters are:
3852
3853 f Buffers created by the command \\[idlwave-find-module] or mouse
3854 clicks in the routine info window.
3855 s Buffers created by the IDLWAVE Shell to display where execution
3856 stopped or an error was found.
3857 a Both of the above.
3858
3859 Buffer containing unsaved changes require confirmation before they are killed."
3860 (interactive)
3861 (if (null idlwave-outlawed-buffers)
3862 (error "No IDLWAVE-created buffers available")
3863 (princ (format "Kill IDLWAVE-created buffers: [f]ind source(%d), [s]hell display(%d), [a]ll ? "
3864 (idlwave-count-outlawed-buffers 'find)
3865 (idlwave-count-outlawed-buffers 'shell)))
3866 (let ((c (read-char)))
3867 (cond
3868 ((member c '(?f ?\C-f))
3869 (idlwave-do-kill-autoloaded-buffers 'find))
3870 ((member c '(?s ?\C-s))
3871 (idlwave-do-kill-autoloaded-buffers 'shell))
3872 ((member c '(?a ?\C-a))
3873 (idlwave-do-kill-autoloaded-buffers t))
3874 (t (error "Abort"))))))
3875
3876 (defun idlwave-count-outlawed-buffers (tag)
3877 "How many outlawed buffers have tag TAG?"
3878 (length (delq nil
3879 (mapcar
3880 (lambda (x) (eq (cdr x) tag))
3881 idlwave-outlawed-buffers))))
3882
3883 (defun idlwave-do-kill-autoloaded-buffers (&rest reasons)
3884 "Kill all buffers pulled up by IDLWAVE matching REASONS."
3885 (let* ((list (copy-sequence idlwave-outlawed-buffers))
3886 (cnt 0)
3887 entry)
3888 (while (setq entry (pop list))
3889 (if (buffer-live-p (car entry))
3890 (and (or (memq t reasons)
3891 (memq (cdr entry) reasons))
3892 (kill-buffer (car entry))
3893 (incf cnt)
3894 (setq idlwave-outlawed-buffers
3895 (delq entry idlwave-outlawed-buffers)))
3896 (setq idlwave-outlawed-buffers
3897 (delq entry idlwave-outlawed-buffers))))
3898 (message "%d buffer%s killed" cnt (if (= cnt 1) "" "s"))))
3899
3900 (defun idlwave-revoke-license-to-kill ()
3901 "Remove BUFFER from the buffers which may be killed.
3902 Killing would be done by `idlwave-do-kill-autoloaded-buffers'.
3903 Intended for `after-save-hook'."
3904 (let* ((buf (current-buffer))
3905 (entry (assq buf idlwave-outlawed-buffers)))
3906 ;; Revoke license
3907 (if entry
3908 (setq idlwave-outlawed-buffers
3909 (delq entry idlwave-outlawed-buffers)))
3910 ;; Remove this function from the hook.
3911 (remove-hook 'after-save-hook 'idlwave-revoke-license-to-kill 'local)))
3912
3913 (defvar idlwave-path-alist)
3914 (defun idlwave-locate-lib-file (file)
3915 ;; Find FILE on the scanned lib path and return a buffer visiting it
3916 (let* ((dirs idlwave-path-alist)
3917 dir efile)
3918 (catch 'exit
3919 (while (setq dir (car (pop dirs)))
3920 (if (file-regular-p
3921 (setq efile (expand-file-name file dir)))
3922 (throw 'exit efile))))))
3923
3924 (defun idlwave-expand-lib-file-name (file)
3925 ;; Find FILE on the scanned lib path and return a buffer visiting it
3926 ;; This is for, e.g., finding source with no user catalog
3927 (cond
3928 ((null file) nil)
3929 ((file-name-absolute-p file) file)
3930 (t (idlwave-locate-lib-file file))))
3931
3932 (defun idlwave-make-tags ()
3933 "Creates the IDL tags file IDLTAGS in the current directory from
3934 the list of directories specified in the minibuffer. Directories may be
3935 for example: . /usr/local/rsi/idl/lib. All the subdirectories of the
3936 specified top directories are searched if the directory name is prefixed
3937 by @. Specify @ directories with care, it may take a long, long time if
3938 you specify /."
3939 (interactive)
3940 (let (directory directories cmd append status numdirs dir getsubdirs
3941 buffer save_buffer files numfiles item errbuf)
3942
3943 ;;
3944 ;; Read list of directories
3945 (setq directory (read-string "Tag Directories: " "."))
3946 (setq directories (idlwave-split-string directory "[ \t]+"))
3947 ;;
3948 ;; Set etags command, vars
3949 (setq cmd "etags --output=IDLTAGS --language=none --regex='/[
3950 \\t]*[pP][Rr][Oo][ \\t]+\\([^ \\t,]+\\)/' --regex='/[
3951 \\t]*[Ff][Uu][Nn][Cc][Tt][Ii][Oo][Nn][ \\t]+\\([^ \\t,]+\\)/' ")
3952 (setq append " ")
3953 (setq status 0)
3954 ;;
3955 ;; For each directory
3956 (setq numdirs 0)
3957 (setq dir (nth numdirs directories))
3958 (while (and dir)
3959 ;;
3960 ;; Find the subdirectories
3961 (if (string-match "^[@]\\(.+\\)$" dir)
3962 (setq getsubdirs t) (setq getsubdirs nil))
3963 (if (and getsubdirs) (setq dir (substring dir 1 (length dir))))
3964 (setq dir (expand-file-name dir))
3965 (if (file-directory-p dir)
3966 (progn
3967 (if (and getsubdirs)
3968 (progn
3969 (setq buffer (get-buffer-create "*idltags*"))
3970 (call-process "sh" nil buffer nil "-c"
3971 (concat "find " dir " -type d -print"))
3972 (setq save_buffer (current-buffer))
3973 (set-buffer buffer)
3974 (setq files (idlwave-split-string
3975 (idlwave-replace-string
3976 (buffer-substring 1 (point-max))
3977 "\n" "/*.pro ")
3978 "[ \t]+"))
3979 (set-buffer save_buffer)
3980 (kill-buffer buffer))
3981 (setq files (list (concat dir "/*.pro"))))
3982 ;;
3983 ;; For each subdirectory
3984 (setq numfiles 0)
3985 (setq item (nth numfiles files))
3986 (while (and item)
3987 ;;
3988 ;; Call etags
3989 (if (not (string-match "^[ \\t]*$" item))
3990 (progn
3991 (message (concat "Tagging " item "..."))
3992 (setq errbuf (get-buffer-create "*idltags-error*"))
3993 (setq status (+ status
3994 (if (eq 0 (call-process
3995 "sh" nil errbuf nil "-c"
3996 (concat cmd append item)))
3997 0
3998 1)))
3999 ;;
4000 ;; Append additional tags
4001 (setq append " --append ")
4002 (setq numfiles (1+ numfiles))
4003 (setq item (nth numfiles files)))
4004 (progn
4005 (setq numfiles (1+ numfiles))
4006 (setq item (nth numfiles files))
4007 )))
4008
4009 (setq numdirs (1+ numdirs))
4010 (setq dir (nth numdirs directories)))
4011 (progn
4012 (setq numdirs (1+ numdirs))
4013 (setq dir (nth numdirs directories)))))
4014
4015 (setq errbuf (get-buffer-create "*idltags-error*"))
4016 (if (= status 0)
4017 (kill-buffer errbuf))
4018 (message "")
4019 ))
4020
4021 (defun idlwave-toggle-comment-region (beg end &optional n)
4022 "Comment the lines in the region if the first non-blank line is
4023 commented, and conversely, uncomment region. If optional prefix arg
4024 N is non-nil, then for N positive, add N comment delimiters or for N
4025 negative, remove N comment delimiters.
4026 Uses `comment-region' which does not place comment delimiters on
4027 blank lines."
4028 (interactive "r\nP")
4029 (if n
4030 (comment-region beg end (prefix-numeric-value n))
4031 (save-excursion
4032 (goto-char beg)
4033 (beginning-of-line)
4034 ;; skip blank lines
4035 (skip-chars-forward " \t\n")
4036 (if (looking-at (concat "[ \t]*\\(" comment-start "+\\)"))
4037 (if (fboundp 'uncomment-region)
4038 (uncomment-region beg end)
4039 (comment-region beg end
4040 (- (length (buffer-substring
4041 (match-beginning 1)
4042 (match-end 1))))))
4043 (comment-region beg end)))))
4044
4045
4046 ;; ----------------------------------------------------------------------------
4047 ;; ----------------------------------------------------------------------------
4048 ;; ----------------------------------------------------------------------------
4049 ;; ----------------------------------------------------------------------------
4050 ;;
4051 ;; Completion and Routine Info
4052 ;;
4053
4054 ;; String "intern" functions
4055
4056 ;; For the completion and routine info function, we want to normalize
4057 ;; the case of procedure names etc. We do this by "interning" these
4058 ;; string is a hand-crafted way. Hashes are used to map the downcase
4059 ;; version of the strings to the cased versions. Most *-sint-*
4060 ;; variables consist of *two* hashes, a buffer+shell, followed by a
4061 ;; system hash. The former is re-scanned, and the latter takes case
4062 ;; precedence.
4063 ;;
4064 ;; Since these cased versions are really lisp objects, we can use `eq'
4065 ;; to search, which is a large performance boost. All new strings
4066 ;; need to be "sinterned". We do this as early as possible after
4067 ;; getting these strings from completion or buffer substrings. So
4068 ;; most of the code can simply assume to deal with "sinterned"
4069 ;; strings. The only exception is that the functions which scan whole
4070 ;; buffers for routine information do not intern the grabbed strings.
4071 ;; This is only done afterwards. Therefore in these functions it is
4072 ;; *not* safe to assume the strings can be compared with `eq' and be
4073 ;; fed into the routine assq functions.
4074
4075 ;; Here we define the hashing functions.
4076
4077 ;; The variables which hold the hashes.
4078 (defvar idlwave-sint-routines '(nil))
4079 (defvar idlwave-sint-keywords '(nil))
4080 (defvar idlwave-sint-methods '(nil))
4081 (defvar idlwave-sint-classes '(nil))
4082 (defvar idlwave-sint-dirs '(nil))
4083 (defvar idlwave-sint-libnames '(nil))
4084
4085 (defun idlwave-reset-sintern (&optional what)
4086 "Reset all sintern hashes."
4087 ;; Make sure the hash functions are accessible.
4088 (if (or (not (fboundp 'gethash))
4089 (not (fboundp 'puthash)))
4090 (progn
4091 (require 'cl)
4092 (or (fboundp 'puthash)
4093 (defalias 'puthash 'cl-puthash))))
4094 (let ((entries '((idlwave-sint-routines 1000 10)
4095 (idlwave-sint-keywords 1000 10)
4096 (idlwave-sint-methods 100 10)
4097 (idlwave-sint-classes 10 10))))
4098
4099 ;; Make sure these are lists
4100 (loop for entry in entries
4101 for var = (car entry)
4102 do (if (not (consp (symbol-value var))) (set var (list nil))))
4103
4104 (when (or (eq what t) (eq what 'syslib)
4105 (null (cdr idlwave-sint-routines)))
4106 ;; Reset the system & library hash
4107 (loop for entry in entries
4108 for var = (car entry) for size = (nth 1 entry)
4109 do (setcdr (symbol-value var)
4110 (make-hash-table ':size size ':test 'equal)))
4111 (setq idlwave-sint-dirs nil
4112 idlwave-sint-libnames nil))
4113
4114 (when (or (eq what t) (eq what 'bufsh)
4115 (null (car idlwave-sint-routines)))
4116 ;; Reset the buffer & shell hash
4117 (loop for entry in entries
4118 for var = (car entry) for size = (nth 1 entry)
4119 do (setcar (symbol-value var)
4120 (make-hash-table ':size size ':test 'equal))))))
4121
4122 (defun idlwave-sintern-routine-or-method (name &optional class set)
4123 (if class
4124 (idlwave-sintern-method name set)
4125 (idlwave-sintern-routine name set)))
4126
4127 (defun idlwave-sintern (stype &rest args)
4128 (apply (intern (concat "idlwave-sintern-" (symbol-name stype))) args))
4129
4130 ;;(defmacro idlwave-sintern (type var)
4131 ;; `(cond ((not (stringp name)) name)
4132 ;; ((gethash (downcase name) (cdr ,var)))
4133 ;; ((gethash (downcase name) (car ,var)))
4134 ;; (set (idlwave-sintern-set name ,type ,var set))
4135 ;; (name)))
4136
4137 (defun idlwave-sintern-routine (name &optional set)
4138 (cond ((not (stringp name)) name)
4139 ((gethash (downcase name) (cdr idlwave-sint-routines)))
4140 ((gethash (downcase name) (car idlwave-sint-routines)))
4141 (set (idlwave-sintern-set name 'routine idlwave-sint-routines set))
4142 (name)))
4143 (defun idlwave-sintern-keyword (name &optional set)
4144 (cond ((not (stringp name)) name)
4145 ((gethash (downcase name) (cdr idlwave-sint-keywords)))
4146 ((gethash (downcase name) (car idlwave-sint-keywords)))
4147 (set (idlwave-sintern-set name 'keyword idlwave-sint-keywords set))
4148 (name)))
4149 (defun idlwave-sintern-method (name &optional set)
4150 (cond ((not (stringp name)) name)
4151 ((gethash (downcase name) (cdr idlwave-sint-methods)))
4152 ((gethash (downcase name) (car idlwave-sint-methods)))
4153 (set (idlwave-sintern-set name 'method idlwave-sint-methods set))
4154 (name)))
4155 (defun idlwave-sintern-class (name &optional set)
4156 (cond ((not (stringp name)) name)
4157 ((gethash (downcase name) (cdr idlwave-sint-classes)))
4158 ((gethash (downcase name) (car idlwave-sint-classes)))
4159 (set (idlwave-sintern-set name 'class idlwave-sint-classes set))
4160 (name)))
4161
4162 (defun idlwave-sintern-dir (dir &optional set)
4163 (car (or (member dir idlwave-sint-dirs)
4164 (setq idlwave-sint-dirs (cons dir idlwave-sint-dirs)))))
4165 (defun idlwave-sintern-libname (name &optional set)
4166 (car (or (member name idlwave-sint-libnames)
4167 (setq idlwave-sint-libnames (cons name idlwave-sint-libnames)))))
4168
4169 (defun idlwave-sintern-set (name type tables set)
4170 (let* ((func (or (cdr (assq type idlwave-completion-case))
4171 'identity))
4172 (iname (funcall (if (eq func 'preserve) 'identity func) name))
4173 (table (if (eq set 'sys) (cdr tables) (car tables))))
4174 (puthash (downcase name) iname table)
4175 iname))
4176
4177 (defun idlwave-sintern-keyword-list (kwd-list &optional set)
4178 "Sintern a set of keywords (file (key . link) (key2 . link2) ...)"
4179 (mapcar (lambda(x)
4180 (setcar x (idlwave-sintern-keyword (car x) set)))
4181 (cdr kwd-list))
4182 kwd-list)
4183
4184 (defun idlwave-sintern-rinfo-list (list &optional set default-dir)
4185 "Sintern all strings in the rinfo LIST. With optional parameter
4186 SET: also set new patterns. Probably this will always have to be t.
4187 If DEFAULT-DIR is passed, it is used as the base of the directory"
4188 (let (entry name type class kwds res source call new)
4189 (while list
4190 (setq entry (car list)
4191 list (cdr list)
4192 name (car entry)
4193 type (nth 1 entry)
4194 class (nth 2 entry)
4195 source (nth 3 entry)
4196 call (nth 4 entry)
4197 kwds (nthcdr 5 entry))
4198
4199 ;; The class and name
4200 (if class
4201 (progn
4202 (if (symbolp class) (setq class (symbol-name class)))
4203 (setq class (idlwave-sintern-class class set))
4204 (setq name (idlwave-sintern-method name set)))
4205 (setq name (idlwave-sintern-routine name set)))
4206
4207 ;; The source
4208 (let ((source-type (car source))
4209 (source-file (nth 1 source))
4210 (source-dir (if default-dir
4211 (file-name-as-directory default-dir)
4212 (nth 2 source)))
4213 (source-lib (nth 3 source)))
4214 (if (stringp source-dir)
4215 (setq source-dir (idlwave-sintern-dir source-dir set)))
4216 (if (stringp source-lib)
4217 (setq source-lib (idlwave-sintern-libname source-lib set)))
4218 (setq source (list source-type source-file source-dir source-lib)))
4219
4220 ;; The keywords
4221 (setq kwds (mapcar (lambda (x)
4222 (idlwave-sintern-keyword-list x set))
4223 kwds))
4224
4225 ;; Build a canonicalized list
4226 (setq new (nconc (list name type class source call) kwds)
4227 res (cons new res)))
4228 (nreverse res)))
4229
4230 ;; Creating new sintern tables
4231
4232 (defun idlwave-new-sintern-type (tag)
4233 "Define a variable and a function to sintern the new type TAG.
4234 This defines the function `idlwave-sintern-TAG' and the variable
4235 `idlwave-sint-TAGs'."
4236 (let* ((name (symbol-name tag))
4237 (names (concat name "s"))
4238 (var (intern (concat "idlwave-sint-" names)))
4239 (func (intern (concat "idlwave-sintern-" name))))
4240 (set var nil) ; initial value of the association list
4241 (fset func ; set the function
4242 `(lambda (name &optional set)
4243 (cond ((not (stringp name)) name)
4244 ((cdr (assoc (downcase name) ,var)))
4245 (set
4246 (setq ,var (cons (cons (downcase name) name) ,var))
4247 name)
4248 (name))))))
4249
4250 (defun idlwave-reset-sintern-type (tag)
4251 "Reset the sintern variable associated with TAG."
4252 (set (intern (concat "idlwave-sint-" (symbol-name tag) "s")) nil))
4253
4254 ;;---------------------------------------------------------------------------
4255
4256
4257 ;; The variables which hold the information
4258 (defvar idlwave-system-routines nil
4259 "Holds the routine-info obtained by scanning buffers.")
4260 (defvar idlwave-buffer-routines nil
4261 "Holds the routine-info obtained by scanning buffers.")
4262 (defvar idlwave-compiled-routines nil
4263 "Holds the routine-info obtained by asking the shell.")
4264 (defvar idlwave-unresolved-routines nil
4265 "Holds the unresolved routine-info obtained by asking the shell.")
4266 (defvar idlwave-user-catalog-routines nil
4267 "Holds the procedure routine-info from the user scan.")
4268 (defvar idlwave-library-catalog-routines nil
4269 "Holds the procedure routine-info from the library catalog files.")
4270 (defvar idlwave-path-alist nil
4271 "Alist with !PATH directories and zero or more flags if the dir has
4272 been scanned in a user catalog ('user) or discovered in a library
4273 catalog \('lib).")
4274 (defvar idlwave-true-path-alist nil
4275 "Like `idlwave-path-alist', but with true filenames.")
4276 (defvar idlwave-routines nil
4277 "Holds the combinded procedure/function/method routine-info.")
4278 (defvar idlwave-class-alist nil
4279 "Holds the class names known to IDLWAVE.")
4280 (defvar idlwave-class-history nil
4281 "The history of classes selected with the minibuffer.")
4282 (defvar idlwave-force-class-query nil)
4283 (defvar idlwave-before-completion-wconf nil
4284 "The window configuration just before the completion buffer was displayed.")
4285 (defvar idlwave-last-system-routine-info-cons-cell nil
4286 "The last cons cell in the system routine info.")
4287
4288 ;;
4289 ;; The code to get routine info from different sources.
4290
4291 (defvar idlwave-system-routines)
4292 (defvar idlwave-catalog-process nil
4293 "The background process currently updating the catalog.")
4294
4295 (defun idlwave-routines ()
4296 "Provide a list of IDL routines.
4297 This routine loads the builtin routines on the first call. Later it
4298 only returns the value of the variable."
4299 (if (and idlwave-catalog-process
4300 (processp idlwave-catalog-process))
4301 (progn
4302 (cond
4303 ((equal (process-status idlwave-catalog-process) 'exit)
4304 (message "updating........")
4305 (setq idlwave-catalog-process nil)
4306 (idlwave-update-routine-info '(4)))
4307 ((equal (process-status idlwave-catalog-process) 'run)
4308 ;; Keep it running...
4309 )
4310 (t
4311 ;; Something is wrong, get rid of the process
4312 (message "Problem with catalog process") (beep)
4313 (condition-case nil
4314 (kill-process idlwave-catalog-process)
4315 (error nil))
4316 (setq idlwave-catalog-process nil)))))
4317 (or idlwave-routines
4318 (progn
4319 (idlwave-update-routine-info)
4320 ;; return the current value
4321 idlwave-routines)))
4322
4323 (defvar idlwave-update-rinfo-hook nil
4324 "List of functions which should run after a global rinfo update.
4325 Does not run after automatic updates of buffer or the shell.")
4326
4327 (defun idlwave-rescan-catalog-directories ()
4328 "Rescan the previously selected directories. For batch processing."
4329 (idlwave-update-routine-info '(16)))
4330
4331 (defun idlwave-rescan-asynchronously ()
4332 "Dispatch another emacs instance to update the idlwave catalog.
4333 After the process finishes normally, the first access to routine info
4334 will re-read the catalog."
4335 (interactive)
4336 (if (processp idlwave-catalog-process)
4337 (if (eq (process-status idlwave-catalog-process) 'run)
4338 (if (yes-or-no-p "A catalog-updating process is running. Kill it? ")
4339 (progn
4340 (condition-case nil
4341 (kill-process idlwave-catalog-process)
4342 (error nil))
4343 (error "Process killed, no new process started"))
4344 (error "Quit"))
4345 (condition-case nil
4346 (kill-process idlwave-catalog-process)
4347 (error nil))))
4348 (if (or (not idlwave-user-catalog-file)
4349 (not (stringp idlwave-user-catalog-file))
4350 (not (file-regular-p idlwave-user-catalog-file)))
4351 (error "No catalog has been produced yet"))
4352 (let* ((emacs (expand-file-name (invocation-name) (invocation-directory)))
4353 (args (list "-batch"
4354 "-l" (expand-file-name "~/.emacs")
4355 "-l" "idlwave"
4356 "-f" "idlwave-rescan-catalog-directories"))
4357 (process (apply 'start-process "idlcat"
4358 nil emacs args)))
4359 (setq idlwave-catalog-process process)
4360 (set-process-sentinel
4361 process
4362 (lambda (pro why)
4363 (when (string-match "finished" why)
4364 (setq idlwave-routines nil
4365 idlwave-system-routines nil
4366 idlwave-catalog-process nil)
4367 (or (idlwave-start-load-rinfo-timer)
4368 (idlwave-update-routine-info '(4))))))
4369 (message "Background job started to update catalog file")))
4370
4371
4372 ;; Format for all routine info user catalog, library catalogs, etc.:
4373 ;;
4374 ;; ("ROUTINE" type class
4375 ;; (system) | (lib pro_file dir "LIBNAME") | (user pro_file dir "USERLIB") |
4376 ;; (buffer pro_file dir) | (compiled pro_file dir)
4377 ;; "calling_string" ("HELPFILE" (("KWD1" . link1) ...)))
4378 ;;
4379 ;; DIR will be supplied dynamically while loading library catalogs,
4380 ;; and is sinterned to save space, as is LIBNAME. PRO_FILE can be a
4381 ;; complete filepath, in which case DIR is unnecessary. HELPFILE can
4382 ;; be nil, as can LINK1, etc., if no HTML help is available.
4383
4384
4385 (defvar idlwave-load-rinfo-idle-timer)
4386 (defun idlwave-update-routine-info (&optional arg no-concatenate)
4387 "Update the internal routine-info lists.
4388 These lists are used by `idlwave-routine-info' (\\[idlwave-routine-info])
4389 and by `idlwave-complete' (\\[idlwave-complete]) to provide information
4390 about individual routines.
4391
4392 The information can come from 4 sources:
4393 1. IDL programs in the current editing session
4394 2. Compiled modules in an IDL shell running as Emacs subprocess
4395 3. A list which covers the IDL system routines.
4396 4. A list which covers the prescanned library files.
4397
4398 Scans all IDLWAVE-mode buffers of the current editing session (see
4399 `idlwave-scan-all-buffers-for-routine-info').
4400 When an IDL shell is running, this command also queries the IDL program
4401 for currently compiled routines.
4402
4403 With prefix ARG, also reload the system and library lists.
4404 With two prefix ARG's, also rescans the chosen user catalog tree.
4405 With three prefix args, dispatch asynchronous process to do the update.
4406
4407 If NO-CONCATENATE is non-nil, don't pre-concatenate the routine info
4408 lists, but instead wait for the shell query to complete and
4409 asynchronously finish updating routine info. This is set
4410 automatically when called interactively. When you need routine
4411 information updated immediately, leave NO-CONCATENATE nil."
4412 (interactive "P\np")
4413 ;; Stop any idle processing
4414 (if (or (and (fboundp 'itimerp)
4415 (itimerp idlwave-load-rinfo-idle-timer))
4416 (and (fboundp 'timerp)
4417 (timerp idlwave-load-rinfo-idle-timer)))
4418 (cancel-timer idlwave-load-rinfo-idle-timer))
4419 (cond
4420 ((equal arg '(64))
4421 ;; Start a background process which updates the catalog.
4422 (idlwave-rescan-asynchronously))
4423 ((equal arg '(16))
4424 ;; Update the user catalog now, and wait for them.
4425 (idlwave-create-user-catalog-file t))
4426 (t
4427 (let* ((load (or arg
4428 idlwave-buffer-case-takes-precedence
4429 (null idlwave-routines)))
4430 ;; The override-idle means, even if the idle timer has done some
4431 ;; preparing work, load and renormalize everything anyway.
4432 (override-idle (or arg idlwave-buffer-case-takes-precedence)))
4433
4434 (setq idlwave-buffer-routines nil
4435 idlwave-compiled-routines nil
4436 idlwave-unresolved-routines nil)
4437 ;; Reset the appropriate hashes
4438 (if (get 'idlwave-reset-sintern 'done-by-idle)
4439 ;; reset was already done in idle time, so skip this step now once
4440 (put 'idlwave-reset-sintern 'done-by-idle nil)
4441 (idlwave-reset-sintern (cond (load t)
4442 ((null idlwave-system-routines) t)
4443 (t 'bufsh))))
4444
4445 (if idlwave-buffer-case-takes-precedence
4446 ;; We can safely scan the buffer stuff first
4447 (progn
4448 (idlwave-update-buffer-routine-info)
4449 (and load (idlwave-load-system-rinfo override-idle)))
4450 ;; We first do the system info, and then the buffers
4451 (and load (idlwave-load-system-rinfo override-idle))
4452 (idlwave-update-buffer-routine-info))
4453
4454 ;; Let's see if there is a shell
4455 (let* ((shell-is-running (and (fboundp 'idlwave-shell-is-running)
4456 (idlwave-shell-is-running)))
4457 (ask-shell (and shell-is-running
4458 idlwave-query-shell-for-routine-info)))
4459
4460 ;; Load the library catalogs again, first re-scanning the path
4461 (when arg
4462 (if shell-is-running
4463 (idlwave-shell-send-command idlwave-shell-path-query
4464 '(progn
4465 (idlwave-shell-get-path-info)
4466 (idlwave-scan-library-catalogs))
4467 'hide)
4468 (idlwave-scan-library-catalogs)))
4469
4470 (if (or (not ask-shell)
4471 (not no-concatenate))
4472 ;; 1. If we are not going to ask the shell, we need to do the
4473 ;; concatenation now.
4474 ;; 2. When this function is called non-interactively, it
4475 ;; means that someone needs routine info *now*. The
4476 ;; shell update causes the concatenation to be
4477 ;; *delayed*, so not in time for the current command.
4478 ;; Therefore, we do a concatenation now, even though
4479 ;; the shell might do it again.
4480 (idlwave-concatenate-rinfo-lists nil 'run-hooks))
4481
4482 (when ask-shell
4483 ;; Ask the shell about the routines it knows of.
4484 (message "Querying the shell")
4485 (idlwave-shell-update-routine-info nil t)))))))
4486
4487
4488 (defvar idlwave-load-rinfo-steps-done (make-vector 6 nil))
4489 (defvar idlwave-load-rinfo-idle-timer nil)
4490 (defun idlwave-start-load-rinfo-timer ()
4491 (if (or (and (fboundp 'itimerp)
4492 (itimerp idlwave-load-rinfo-idle-timer))
4493 (and (fboundp 'timerp)
4494 (timerp idlwave-load-rinfo-idle-timer)))
4495 (cancel-timer idlwave-load-rinfo-idle-timer))
4496 (setq idlwave-load-rinfo-steps-done (make-vector 6 nil))
4497 (setq idlwave-load-rinfo-idle-timer nil)
4498 (if (and idlwave-init-rinfo-when-idle-after
4499 (numberp idlwave-init-rinfo-when-idle-after)
4500 (not (equal 0 idlwave-init-rinfo-when-idle-after))
4501 (not idlwave-routines))
4502 (condition-case nil
4503 (progn
4504 (setq idlwave-load-rinfo-idle-timer
4505 (run-with-idle-timer
4506 idlwave-init-rinfo-when-idle-after
4507 nil 'idlwave-load-rinfo-next-step)))
4508 (error nil))))
4509
4510 (defun idlwave-load-rinfo-next-step ()
4511 (let ((inhibit-quit t)
4512 (arr idlwave-load-rinfo-steps-done))
4513 (when (catch 'exit
4514 (when (not (aref arr 0))
4515 (message "Loading idlw-rinfo.el in idle time...")
4516 (load "idlw-rinfo" 'noerror 'nomessage)
4517 (message "Loading idlw-rinfo.el in idle time...done")
4518 (aset arr 0 t)
4519 (throw 'exit t))
4520 (when (not (aref arr 1))
4521 (message "Normalizing idlwave-system-routines in idle time...")
4522 (idlwave-reset-sintern t)
4523 (put 'idlwave-reset-sintern 'done-by-idle t)
4524 (setq idlwave-system-routines
4525 (idlwave-sintern-rinfo-list idlwave-system-routines 'sys))
4526 (message "Normalizing idlwave-system-routines in idle time...done")
4527 (aset arr 1 t)
4528 (throw 'exit t))
4529 (when (not (aref arr 2))
4530 (when (and (stringp idlwave-user-catalog-file)
4531 (file-regular-p idlwave-user-catalog-file))
4532 (message "Loading user catalog in idle time...")
4533 (condition-case nil
4534 (load-file idlwave-user-catalog-file)
4535 (error (throw 'exit nil)))
4536 ;; Check for the old style catalog and warn
4537 (if (and
4538 (boundp 'idlwave-library-routines)
4539 idlwave-library-routines)
4540 (progn
4541 (setq idlwave-library-routines nil)
4542 (ding)
4543 (message "Outdated user catalog: %s... recreate"
4544 idlwave-user-catalog-file))
4545 (message "Loading user catalog in idle time...done"))
4546 (aset arr 2 t)
4547 (throw 'exit t)))
4548 (when (not (aref arr 3))
4549 (when idlwave-user-catalog-routines
4550 (message "Normalizing user catalog routines in idle time...")
4551 (setq idlwave-user-catalog-routines
4552 (idlwave-sintern-rinfo-list
4553 idlwave-user-catalog-routines 'sys))
4554 (message
4555 "Normalizing user catalog routines in idle time...done"))
4556 (aset arr 3 t)
4557 (throw 'exit t))
4558 (when (not (aref arr 4))
4559 (idlwave-scan-library-catalogs
4560 "Loading and normalizing library catalogs in idle time...")
4561 (aset arr 4 t)
4562 (throw 'exit t))
4563 (when (not (aref arr 5))
4564 (message "Finishing initialization in idle time...")
4565 (idlwave-routines)
4566 (message "Finishing initialization in idle time...done")
4567 (throw 'exit nil)))
4568 ;; restart the timer
4569 (if (sit-for 1)
4570 (idlwave-load-rinfo-next-step)
4571 (setq idlwave-load-rinfo-idle-timer
4572 (run-with-idle-timer
4573 idlwave-init-rinfo-when-idle-after
4574 nil 'idlwave-load-rinfo-next-step))))))
4575
4576 (defun idlwave-load-system-rinfo (&optional force)
4577 ;; Load and case-treat the system and catalog files.
4578 (when (or force (not (aref idlwave-load-rinfo-steps-done 0)))
4579 (load "idlw-rinfo" 'noerror 'nomessage))
4580 (when (or force (not (aref idlwave-load-rinfo-steps-done 1)))
4581 (message "Normalizing idlwave-system-routines...")
4582 (setq idlwave-system-routines
4583 (idlwave-sintern-rinfo-list idlwave-system-routines 'sys))
4584 (message "Normalizing idlwave-system-routines...done"))
4585 (setq idlwave-routines (copy-sequence idlwave-system-routines))
4586 (setq idlwave-last-system-routine-info-cons-cell
4587 (nthcdr (1- (length idlwave-routines)) idlwave-routines))
4588 (when (and (stringp idlwave-user-catalog-file)
4589 (file-regular-p idlwave-user-catalog-file))
4590 (condition-case nil
4591 (when (or force (not (aref idlwave-load-rinfo-steps-done 2)))
4592 (load-file idlwave-user-catalog-file))
4593 (error nil))
4594 (when (boundp 'idlwave-library-routines)
4595 (setq idlwave-library-routines nil)
4596 (error "Outdated user catalog: %s... recreate" idlwave-user-catalog-file))
4597 (setq idlwave-true-path-alist nil)
4598 (when (or force (not (aref idlwave-load-rinfo-steps-done 3)))
4599 (message "Normalizing user catalog routines...")
4600 (setq idlwave-user-catalog-routines
4601 (idlwave-sintern-rinfo-list
4602 idlwave-user-catalog-routines 'sys))
4603 (message "Normalizing user catalog routines...done")))
4604 (when (or force (not (aref idlwave-load-rinfo-steps-done 4)))
4605 (idlwave-scan-library-catalogs
4606 "Loading and normalizing library catalogs..."))
4607 (run-hooks 'idlwave-after-load-rinfo-hook))
4608
4609
4610 (defun idlwave-update-buffer-routine-info ()
4611 (let (res)
4612 (cond
4613 ((eq idlwave-scan-all-buffers-for-routine-info t)
4614 ;; Scan all buffers, current buffer last
4615 (message "Scanning all buffers...")
4616 (setq res (idlwave-get-routine-info-from-buffers
4617 (reverse (buffer-list)))))
4618 ((null idlwave-scan-all-buffers-for-routine-info)
4619 ;; Don't scan any buffers
4620 (setq res nil))
4621 (t
4622 ;; Just scan this buffer
4623 (if (eq major-mode 'idlwave-mode)
4624 (progn
4625 (message "Scanning current buffer...")
4626 (setq res (idlwave-get-routine-info-from-buffers
4627 (list (current-buffer))))))))
4628 ;; Put the result into the correct variable
4629 (setq idlwave-buffer-routines
4630 (idlwave-sintern-rinfo-list res 'set))))
4631
4632 (defun idlwave-concatenate-rinfo-lists (&optional quiet run-hook)
4633 "Put the different sources for routine information together."
4634 ;; The sequence here is important because earlier definitions shadow
4635 ;; later ones. We assume that if things in the buffers are newer
4636 ;; then in the shell of the system, they are meant to be different.
4637 (setcdr idlwave-last-system-routine-info-cons-cell
4638 (append idlwave-buffer-routines
4639 idlwave-compiled-routines
4640 idlwave-library-catalog-routines
4641 idlwave-user-catalog-routines))
4642 (setq idlwave-class-alist nil)
4643
4644 ;; Give a message with information about the number of routines we have.
4645 (unless quiet
4646 (message
4647 "Routines Found: buffer(%d) compiled(%d) library(%d) user(%d) system(%d)"
4648 (length idlwave-buffer-routines)
4649 (length idlwave-compiled-routines)
4650 (length idlwave-library-catalog-routines)
4651 (length idlwave-user-catalog-routines)
4652 (length idlwave-system-routines)))
4653 (if run-hook
4654 (run-hooks 'idlwave-update-rinfo-hook)))
4655
4656 (defun idlwave-class-alist ()
4657 "Return the class alist - make it if necessary."
4658 (or idlwave-class-alist
4659 (let (class)
4660 (loop for x in idlwave-routines do
4661 (when (and (setq class (nth 2 x))
4662 (not (assq class idlwave-class-alist)))
4663 (push (list class) idlwave-class-alist)))
4664 idlwave-class-alist)))
4665
4666 ;; Three functions for the hooks
4667 (defun idlwave-save-buffer-update ()
4668 (idlwave-update-current-buffer-info 'save-buffer))
4669 (defun idlwave-kill-buffer-update ()
4670 (idlwave-update-current-buffer-info 'kill-buffer))
4671 (defun idlwave-new-buffer-update ()
4672 (idlwave-update-current-buffer-info 'find-file))
4673
4674 (defun idlwave-update-current-buffer-info (why)
4675 "Update idlwave-routines for current buffer. Can run from after-save-hook."
4676 (when (and (eq major-mode 'idlwave-mode)
4677 (or (eq t idlwave-auto-routine-info-updates)
4678 (memq why idlwave-auto-routine-info-updates))
4679 idlwave-scan-all-buffers-for-routine-info
4680 idlwave-routines)
4681 (condition-case nil
4682 (let (routines)
4683 (idlwave-replace-buffer-routine-info
4684 (buffer-file-name)
4685 (if (eq why 'kill-buffer)
4686 nil
4687 (setq routines
4688 (idlwave-sintern-rinfo-list
4689 (idlwave-get-routine-info-from-buffers
4690 (list (current-buffer))) 'set))))
4691 (idlwave-concatenate-rinfo-lists 'quiet)
4692 routines)
4693 (error nil))))
4694
4695 (defun idlwave-replace-buffer-routine-info (file new)
4696 "Cut the part from FILE out of `idlwave-buffer-routines' and add NEW."
4697 (let ((list idlwave-buffer-routines)
4698 found)
4699 (while list
4700 ;; The following test uses eq to make sure it works correctly
4701 ;; when two buffers visit the same file. Then the file names
4702 ;; will be equal, but not eq.
4703 (if (eq (idlwave-routine-source-file (nth 3 (car list))) file)
4704 (progn
4705 (setcar list nil)
4706 (setq found t))
4707 (if found
4708 ;; End of that section reached. Jump.
4709 (setq list nil)))
4710 (setq list (cdr list)))
4711 (setq idlwave-buffer-routines
4712 (append new (delq nil idlwave-buffer-routines)))))
4713
4714 ;;----- Scanning buffers -------------------
4715
4716 (defun idlwave-get-routine-info-from-buffers (buffers)
4717 "Call `idlwave-get-buffer-routine-info' on idlwave-mode buffers in BUFFERS."
4718 (let (buf routine-lists res)
4719 (save-excursion
4720 (while (setq buf (pop buffers))
4721 (set-buffer buf)
4722 (if (and (eq major-mode 'idlwave-mode)
4723 buffer-file-name)
4724 ;; yes, this buffer has the right mode.
4725 (progn (setq res (condition-case nil
4726 (idlwave-get-buffer-routine-info)
4727 (error nil)))
4728 (push res routine-lists)))))
4729 ;; Concatenate the individual lists and return the result
4730 (apply 'nconc routine-lists)))
4731
4732 (defun idlwave-get-buffer-routine-info ()
4733 "Scan the current buffer for routine info. Return (PRO-LIST FUNC-LIST)."
4734 (let* ((case-fold-search t)
4735 routine-list string entry)
4736 (save-excursion
4737 (save-restriction
4738 (widen)
4739 (goto-char (point-min))
4740 (while (re-search-forward
4741 "^[ \t]*\\(pro\\|function\\)[ \t]" nil t)
4742 (setq string (buffer-substring-no-properties
4743 (match-beginning 0)
4744 (progn
4745 (idlwave-end-of-statement)
4746 (point))))
4747 (setq entry (idlwave-parse-definition string))
4748 (push entry routine-list))))
4749 routine-list))
4750
4751 (defvar idlwave-scanning-lib-dir)
4752 (defun idlwave-parse-definition (string)
4753 "Parse a module definition."
4754 (let ((case-fold-search t)
4755 start name args type keywords class)
4756 ;; Remove comments
4757 (while (string-match ";.*" string)
4758 (setq string (replace-match "" t t string)))
4759 ;; Remove the continuation line stuff
4760 (while (string-match "\\([^a-zA-Z0-9$_]\\)\\$[ \t]*\n" string)
4761 (setq string (replace-match "\\1 " t nil string)))
4762 (while (string-match "\n" string)
4763 (setq string (replace-match " " t nil string)))
4764 ;; Match the name and type.
4765 (when (string-match
4766 "\\<\\(pro\\|function\\)\\>\\s-+\\(\\([a-zA-Z0-9$_]+\\)::\\)?\\([a-zA-Z0-9$_]+\\)" string)
4767 (setq start (match-end 0))
4768 (setq type (downcase (match-string 1 string)))
4769 (if (match-beginning 3)
4770 (setq class (match-string 3 string)))
4771 (setq name (match-string 4 string)))
4772 ;; Match normal args and keyword args
4773 (while (string-match
4774 ",\\s-*\\([a-zA-Z][a-zA-Z0-9$_]*\\|\\(_ref\\)?_extra\\)\\s-*\\(=\\)?"
4775 string start)
4776 (setq start (match-end 0))
4777 (if (match-beginning 3)
4778 (push (match-string 1 string) keywords)
4779 (push (match-string 1 string) args)))
4780 ;; Normalize and sort.
4781 (setq args (nreverse args))
4782 (setq keywords (sort keywords (lambda (a b)
4783 (string< (downcase a) (downcase b)))))
4784 ;; Make and return the entry
4785 ;; We don't know which argument are optional, so this information
4786 ;; will not be contained in the calling sequence.
4787 (list name
4788 (if (equal type "pro") 'pro 'fun)
4789 class
4790 (cond ((not (boundp 'idlwave-scanning-lib))
4791 (list 'buffer (buffer-file-name)))
4792 ; ((string= (downcase
4793 ; (file-name-sans-extension
4794 ; (file-name-nondirectory (buffer-file-name))))
4795 ; (downcase name))
4796 ; (list 'lib))
4797 ; (t (cons 'lib (file-name-nondirectory (buffer-file-name))))
4798 (t (list 'user (file-name-nondirectory (buffer-file-name))
4799 idlwave-scanning-lib-dir "UserLib")))
4800 (concat
4801 (if (string= type "function") "Result = " "")
4802 (if class "Obj ->[%s::]" "")
4803 "%s"
4804 (if args
4805 (concat
4806 (if (string= type "function") "(" ", ")
4807 (mapconcat 'identity args ", ")
4808 (if (string= type "function") ")" ""))))
4809 (if keywords
4810 (cons nil (mapcar 'list keywords)) ;No help file
4811 nil))))
4812
4813
4814 ;;----- Scanning the user catalog -------------------
4815
4816 (defun idlwave-sys-dir ()
4817 "Return the syslib directory, or a dummy that never matches."
4818 (if (string= idlwave-system-directory "")
4819 "@@@@@@@@"
4820 idlwave-system-directory))
4821
4822
4823 (defvar idlwave-shell-path-query)
4824 (defun idlwave-create-user-catalog-file (&optional arg)
4825 "Scan all files on selected dirs of IDL search path for routine information.
4826
4827 A widget checklist will allow you to choose the directories. Write
4828 the result as a file `idlwave-user-catalog-file'. When this file
4829 exists, will be automatically loaded to give routine information about
4830 library routines. With ARG, just rescan the same directories as last
4831 time - so no widget will pop up."
4832 (interactive "P")
4833 ;; Make sure the file is loaded if it exists.
4834 (if (and (stringp idlwave-user-catalog-file)
4835 (file-regular-p idlwave-user-catalog-file))
4836 (condition-case nil
4837 (load-file idlwave-user-catalog-file)
4838 (error nil)))
4839 ;; Make sure the file name makes sense
4840 (unless (and (stringp idlwave-user-catalog-file)
4841 (> (length idlwave-user-catalog-file) 0)
4842 (file-accessible-directory-p
4843 (file-name-directory idlwave-user-catalog-file))
4844 (not (string= "" (file-name-nondirectory
4845 idlwave-user-catalog-file))))
4846 (error "`idlwave-user-catalog-file' does not point to a file in an accessible directory"))
4847
4848 (cond
4849 ;; Rescan the known directories
4850 ((and arg idlwave-path-alist
4851 (consp (car idlwave-path-alist)))
4852 (idlwave-scan-user-lib-files idlwave-path-alist))
4853
4854 ;; Expand the directories from library-path and run the widget
4855 (idlwave-library-path
4856 (idlwave-display-user-catalog-widget
4857 (if idlwave-true-path-alist
4858 ;; Propagate any flags on the existing path-alist
4859 (mapcar (lambda (x)
4860 (let ((path-entry (assoc (file-truename x)
4861 idlwave-true-path-alist)))
4862 (if path-entry
4863 (cons x (cdr path-entry))
4864 (list x))))
4865 (idlwave-expand-path idlwave-library-path))
4866 (mapcar 'list (idlwave-expand-path idlwave-library-path)))))
4867
4868 ;; Ask the shell for the path and then run the widget
4869 (t
4870 (message "Asking the shell for IDL path...")
4871 (require 'idlw-shell)
4872 (idlwave-shell-send-command idlwave-shell-path-query
4873 '(idlwave-user-catalog-command-hook nil)
4874 'hide))))
4875
4876
4877 ;; Parse shell path information and select among it.
4878 (defun idlwave-user-catalog-command-hook (&optional arg)
4879 ;; Command hook used by `idlwave-create-user-catalog-file'.
4880 (if arg
4881 ;; Scan immediately
4882 (idlwave-scan-user-lib-files idlwave-path-alist)
4883 ;; Set the path and display the widget
4884 (idlwave-shell-get-path-info 'no-write) ; set to something path-alist
4885 (idlwave-scan-library-catalogs "Locating library catalogs..." 'no-load)
4886 (idlwave-display-user-catalog-widget idlwave-path-alist)))
4887
4888 (defconst idlwave-user-catalog-widget-help-string
4889 "This is the front-end to the creation of the IDLWAVE user catalog.
4890 Please select the directories on IDL's search path from which you
4891 would like to extract routine information, to be stored in the file:
4892
4893 %s
4894
4895 If this is not the correct file, first set variable
4896 `idlwave-user-catalog-file', and call this command again.
4897
4898 N.B. Many libraries include pre-scanned catalog files
4899 \(\".idlwave_catalog\"). These are marked with \"[LIB]\", and need
4900 not be scanned. You can scan your own libraries off-line using the
4901 perl script `idlwave_catalog'.
4902
4903 After selecting the directories, choose [Scan & Save] to scan the library
4904 directories and save the routine info.
4905 \n")
4906
4907 (defvar idlwave-widget)
4908 (defvar widget-keymap)
4909 (defun idlwave-display-user-catalog-widget (dirs-list)
4910 "Create the widget to select IDL search path directories for scanning."
4911 (interactive)
4912 (require 'widget)
4913 (require 'wid-edit)
4914 (unless dirs-list
4915 (error "Don't know IDL's search path"))
4916
4917 (kill-buffer (get-buffer-create "*IDLWAVE Widget*"))
4918 (switch-to-buffer (get-buffer-create "*IDLWAVE Widget*"))
4919 (kill-all-local-variables)
4920 (make-local-variable 'idlwave-widget)
4921 (widget-insert (format idlwave-user-catalog-widget-help-string
4922 idlwave-user-catalog-file))
4923
4924 (widget-create 'push-button
4925 :notify 'idlwave-widget-scan-user-lib-files
4926 "Scan & Save")
4927 (widget-insert " ")
4928 (widget-create 'push-button
4929 :notify 'idlwave-delete-user-catalog-file
4930 "Delete File")
4931 (widget-insert " ")
4932 (widget-create 'push-button
4933 :notify
4934 '(lambda (&rest ignore)
4935 (let ((path-list (widget-get idlwave-widget :path-dirs)))
4936 (mapcar (lambda (x)
4937 (unless (memq 'lib (cdr x))
4938 (idlwave-path-alist-add-flag x 'user)))
4939 path-list)
4940 (idlwave-display-user-catalog-widget path-list)))
4941 "Select All Non-Lib")
4942 (widget-insert " ")
4943 (widget-create 'push-button
4944 :notify
4945 '(lambda (&rest ignore)
4946 (let ((path-list (widget-get idlwave-widget :path-dirs)))
4947 (mapcar (lambda (x)
4948 (idlwave-path-alist-remove-flag x 'user))
4949 path-list)
4950 (idlwave-display-user-catalog-widget path-list)))
4951 "Deselect All")
4952 (widget-insert " ")
4953 (widget-create 'push-button
4954 :notify (lambda (&rest ignore)
4955 (kill-buffer (current-buffer)))
4956 "Quit")
4957 (widget-insert "\n\n")
4958
4959 (widget-insert "Select Directories: \n")
4960
4961 (setq idlwave-widget
4962 (apply 'widget-create
4963 'checklist
4964 :value (delq nil (mapcar (lambda (x)
4965 (if (memq 'user (cdr x))
4966 (car x)))
4967 dirs-list))
4968 :greedy t
4969 :tag "List of directories"
4970 (mapcar (lambda (x)
4971 (list 'item
4972 (if (memq 'lib (cdr x))
4973 (concat "[LIB] " (car x) )
4974 (car x)))) dirs-list)))
4975 (widget-put idlwave-widget :path-dirs dirs-list)
4976 (widget-insert "\n")
4977 (use-local-map widget-keymap)
4978 (widget-setup)
4979 (goto-char (point-min))
4980 (delete-other-windows))
4981
4982 (defun idlwave-delete-user-catalog-file (&rest ignore)
4983 (if (yes-or-no-p
4984 (format "Delete file %s " idlwave-user-catalog-file))
4985 (progn
4986 (delete-file idlwave-user-catalog-file)
4987 (message "%s has been deleted" idlwave-user-catalog-file))))
4988
4989 (defun idlwave-widget-scan-user-lib-files (&rest ignore)
4990 ;; Call `idlwave-scan-user-lib-files' with data taken from the widget.
4991 (let* ((widget idlwave-widget)
4992 (selected-dirs (widget-value widget))
4993 (path-alist (widget-get widget :path-dirs))
4994 (this-path-alist path-alist)
4995 dir-entry)
4996 (while (setq dir-entry (pop this-path-alist))
4997 (if (member
4998 (if (memq 'lib (cdr dir-entry))
4999 (concat "[LIB] " (car dir-entry))
5000 (car dir-entry))
5001 selected-dirs)
5002 (idlwave-path-alist-add-flag dir-entry 'user)
5003 (idlwave-path-alist-remove-flag dir-entry 'user)))
5004 (idlwave-scan-user-lib-files path-alist)))
5005
5006 (defvar font-lock-mode)
5007 (defun idlwave-scan-user-lib-files (path-alist)
5008 ;; Scan the PRO files in PATH-ALIST and store the info in the user catalog
5009 (let* ((idlwave-scanning-lib t)
5010 (idlwave-scanning-lib-dir "")
5011 (idlwave-completion-case nil)
5012 dirs-alist dir files file)
5013 (setq idlwave-user-catalog-routines nil
5014 idlwave-path-alist path-alist ; for library-path instead
5015 idlwave-true-path-alist nil)
5016 (if idlwave-auto-write-paths (idlwave-write-paths))
5017 (save-excursion
5018 (set-buffer (get-buffer-create "*idlwave-scan.pro*"))
5019 (idlwave-mode)
5020 (setq dirs-alist (reverse path-alist))
5021 (while (setq dir (pop dirs-alist))
5022 (when (memq 'user (cdr dir)) ; Has it marked for scan?
5023 (setq dir (car dir))
5024 (setq idlwave-scanning-lib-dir dir)
5025 (when (file-directory-p dir)
5026 (setq files (directory-files dir 'full "\\.[pP][rR][oO]\\'"))
5027 (while (setq file (pop files))
5028 (when (file-regular-p file)
5029 (if (not (file-readable-p file))
5030 (message "Skipping %s (no read permission)" file)
5031 (message "Scanning %s..." file)
5032 (erase-buffer)
5033 (insert-file-contents file 'visit)
5034 (setq idlwave-user-catalog-routines
5035 (append (idlwave-get-routine-info-from-buffers
5036 (list (current-buffer)))
5037 idlwave-user-catalog-routines)))))))))
5038 (message "Creating user catalog file...")
5039 (kill-buffer "*idlwave-scan.pro*")
5040 (kill-buffer (get-buffer-create "*IDLWAVE Widget*"))
5041 (let ((font-lock-maximum-size 0)
5042 (auto-mode-alist nil))
5043 (find-file idlwave-user-catalog-file))
5044 (if (and (boundp 'font-lock-mode)
5045 font-lock-mode)
5046 (font-lock-mode 0))
5047 (erase-buffer)
5048 (insert ";; IDLWAVE user catalog file\n")
5049 (insert (format ";; Created %s\n\n" (current-time-string)))
5050
5051 ;; Define the routine info list
5052 (insert "\n(setq idlwave-user-catalog-routines\n '(")
5053 (let ((standard-output (current-buffer)))
5054 (mapcar (lambda (x)
5055 (insert "\n ")
5056 (prin1 x)
5057 (goto-char (point-max)))
5058 idlwave-user-catalog-routines))
5059 (insert (format "))\n\n;;; %s ends here\n"
5060 (file-name-nondirectory idlwave-user-catalog-file)))
5061 (goto-char (point-min))
5062 ;; Save the buffer
5063 (save-buffer 0)
5064 (kill-buffer (current-buffer)))
5065 (message "Creating user catalog file...done")
5066 (message "Info for %d routines saved in %s"
5067 (length idlwave-user-catalog-routines)
5068 idlwave-user-catalog-file)
5069 (sit-for 2)
5070 (idlwave-update-routine-info t))
5071
5072 (defun idlwave-read-paths ()
5073 (if (and (stringp idlwave-path-file)
5074 (file-regular-p idlwave-path-file))
5075 (condition-case nil
5076 (load idlwave-path-file t t t)
5077 (error nil))))
5078
5079 (defun idlwave-write-paths ()
5080 (interactive)
5081 (when (and idlwave-path-alist idlwave-system-directory)
5082 (let ((font-lock-maximum-size 0)
5083 (auto-mode-alist nil))
5084 (find-file idlwave-path-file))
5085 (if (and (boundp 'font-lock-mode)
5086 font-lock-mode)
5087 (font-lock-mode 0))
5088 (erase-buffer)
5089 (insert ";; IDLWAVE paths\n")
5090 (insert (format ";; Created %s\n\n" (current-time-string)))
5091 ;; Define the variable which knows the value of "!DIR"
5092 (insert (format "\n(setq idlwave-system-directory \"%s\")\n"
5093 idlwave-system-directory))
5094
5095 ;; Define the variable which contains a list of all scanned directories
5096 (insert "\n(setq idlwave-path-alist\n '(")
5097 (let ((standard-output (current-buffer)))
5098 (mapcar (lambda (x)
5099 (insert "\n ")
5100 (prin1 x)
5101 (goto-char (point-max)))
5102 idlwave-path-alist))
5103 (insert "))\n")
5104 (save-buffer 0)
5105 (kill-buffer (current-buffer))))
5106
5107
5108 (defun idlwave-expand-path (path &optional default-dir)
5109 ;; Expand parts of path starting with '+' recursively into directory list.
5110 ;; Relative recursive path elements are expanded relative to DEFAULT-DIR.
5111 (message "Expanding path...")
5112 (let (path1 dir recursive)
5113 (while (setq dir (pop path))
5114 (if (setq recursive (string= (substring dir 0 1) "+"))
5115 (setq dir (substring dir 1)))
5116 (if (and recursive
5117 (not (file-name-absolute-p dir)))
5118 (setq dir (expand-file-name dir default-dir)))
5119 (if recursive
5120 ;; Expand recursively
5121 (setq path1 (append (idlwave-recursive-directory-list dir) path1))
5122 ;; Keep unchanged
5123 (push dir path1)))
5124 (message "Expanding path...done")
5125 (nreverse path1)))
5126
5127 (defun idlwave-recursive-directory-list (dir)
5128 ;; Return a list of all directories below DIR, including DIR itself
5129 (let ((path (list dir)) path1 file files)
5130 (while (setq dir (pop path))
5131 (when (file-directory-p dir)
5132 (setq files (nreverse (directory-files dir t "[^.]")))
5133 (while (setq file (pop files))
5134 (if (file-directory-p file)
5135 (push (file-name-as-directory file) path)))
5136 (push dir path1)))
5137 path1))
5138
5139
5140 ;;----- Scanning the library catalogs ------------------
5141
5142 (defun idlwave-scan-library-catalogs (&optional message-base no-load)
5143 "Scan for library catalog files (.idlwave_catalog) and ingest.
5144
5145 All directories on `idlwave-path-alist' (or `idlwave-library-path'
5146 instead, if present) are searched. Print MESSAGE-BASE along with the
5147 libraries being loaded, if passed, and skip loading/normalizing if
5148 NO-LOAD is non-nil. The variable `idlwave-use-library-catalogs' can
5149 be set to nil to disable library catalog scanning."
5150 (when idlwave-use-library-catalogs
5151 (let ((dirs
5152 (if idlwave-library-path
5153 (idlwave-expand-path idlwave-library-path)
5154 (mapcar 'car idlwave-path-alist)))
5155 (old-libname "")
5156 dir-entry dir flags catalog all-routines)
5157 (if message-base (message message-base))
5158 (while (setq dir (pop dirs))
5159 (catch 'continue
5160 (when (file-readable-p
5161 (setq catalog (expand-file-name ".idlwave_catalog" dir)))
5162 (unless no-load
5163 (setq idlwave-library-catalog-routines nil)
5164 ;; Load the catalog file
5165 (condition-case nil
5166 (load catalog t t t)
5167 (error (throw 'continue t)))
5168 (when (and
5169 message-base
5170 (not (string= idlwave-library-catalog-libname
5171 old-libname)))
5172 (message (concat message-base
5173 idlwave-library-catalog-libname))
5174 (setq old-libname idlwave-library-catalog-libname))
5175 (when idlwave-library-catalog-routines
5176 (setq all-routines
5177 (append
5178 (idlwave-sintern-rinfo-list
5179 idlwave-library-catalog-routines 'sys dir)
5180 all-routines))))
5181
5182 ;; Add a 'lib flag if on path-alist
5183 (when (and idlwave-path-alist
5184 (setq dir-entry (assoc dir idlwave-path-alist)))
5185 (idlwave-path-alist-add-flag dir-entry 'lib)))))
5186 (unless no-load (setq idlwave-library-catalog-routines all-routines))
5187 (if message-base (message (concat message-base "done"))))))
5188
5189 ;;----- Communicating with the Shell -------------------
5190
5191 ;; First, here is the idl program which can be used to query IDL for
5192 ;; defined routines.
5193 (defconst idlwave-routine-info.pro
5194 "
5195 ;; START OF IDLWAVE SUPPORT ROUTINES
5196 pro idlwave_print_info_entry,name,func=func,separator=sep
5197 ;; See if it's an object method
5198 if name eq '' then return
5199 func = keyword_set(func)
5200 methsep = strpos(name,'::')
5201 meth = methsep ne -1
5202
5203 ;; Get routine info
5204 pars = routine_info(name,/parameters,functions=func)
5205 source = routine_info(name,/source,functions=func)
5206 nargs = pars.num_args
5207 nkw = pars.num_kw_args
5208 if nargs gt 0 then args = pars.args
5209 if nkw gt 0 then kwargs = pars.kw_args
5210
5211 ;; Trim the class, and make the name
5212 if meth then begin
5213 class = strmid(name,0,methsep)
5214 name = strmid(name,methsep+2,strlen(name)-1)
5215 if nargs gt 0 then begin
5216 ;; remove the self argument
5217 wh = where(args ne 'SELF',nargs)
5218 if nargs gt 0 then args = args[wh]
5219 endif
5220 endif else begin
5221 ;; No class, just a normal routine.
5222 class = \"\"
5223 endelse
5224
5225 ;; Calling sequence
5226 cs = \"\"
5227 if func then cs = 'Result = '
5228 if meth then cs = cs + 'Obj -> [' + '%s' + '::]'
5229 cs = cs + '%s'
5230 if func then cs = cs + '(' else if nargs gt 0 then cs = cs + ', '
5231 if nargs gt 0 then begin
5232 for j=0,nargs-1 do begin
5233 cs = cs + args[j]
5234 if j lt nargs-1 then cs = cs + ', '
5235 endfor
5236 end
5237 if func then cs = cs + ')'
5238 ;; Keyword arguments
5239 kwstring = ''
5240 if nkw gt 0 then begin
5241 for j=0,nkw-1 do begin
5242 kwstring = kwstring + ' ' + kwargs[j]
5243 endfor
5244 endif
5245
5246 ret=(['IDLWAVE-PRO','IDLWAVE-FUN'])[func]
5247
5248 print,ret + ': ' + name + sep + class + sep + source[0].path $
5249 + sep + cs + sep + kwstring
5250 end
5251
5252 pro idlwave_routine_info
5253 on_error,1
5254 sep = '<@>'
5255 print,'>>>BEGIN OF IDLWAVE ROUTINE INFO (\"' + sep + '\" IS THE SEPARATOR)'
5256 all = routine_info()
5257 for i=0,n_elements(all)-1 do $
5258 idlwave_print_info_entry,all[i],separator=sep
5259 all = routine_info(/functions)
5260 for i=0,n_elements(all)-1 do $
5261 idlwave_print_info_entry,all[i],/func,separator=sep
5262 print,'>>>END OF IDLWAVE ROUTINE INFO'
5263 end
5264
5265 pro idlwave_get_sysvars
5266 on_error,1
5267 catch,error_status
5268 if error_status ne 0 then begin
5269 print, 'Cannot get info about system variables'
5270 endif else begin
5271 help,/brief,output=s,/system_variables ; ? unsafe use of OUTPUT=
5272 s = strtrim(strjoin(s,' ',/single),2) ; make one line
5273 v = strsplit(s,' +',/regex,/extract) ; get variables
5274 for i=0,n_elements(v)-1 do begin
5275 t = [''] ; get tag list
5276 a=execute('if n_tags('+v[i]+') gt 0 then t=tag_names('+v[i]+')')
5277 print, 'IDLWAVE-SYSVAR: '+v[i]+' '+strjoin(t,' ',/single)
5278 endfor
5279 endelse
5280 end
5281
5282 pro idlwave_get_class_tags, class
5283 res = execute('tags=tag_names({'+class+'})')
5284 if res then print,'IDLWAVE-CLASS-TAGS: '+class+' '+strjoin(tags,' ',/single)
5285 end
5286 ;; END OF IDLWAVE SUPPORT ROUTINES
5287 "
5288 "The idl programs to get info from the shell.")
5289
5290 (defvar idlwave-idlwave_routine_info-compiled nil
5291 "Remembers if the routine info procedure is already compiled.")
5292
5293 (defvar idlwave-shell-temp-pro-file)
5294 (defvar idlwave-shell-temp-rinfo-save-file)
5295 (defun idlwave-shell-update-routine-info (&optional quiet run-hooks wait)
5296 "Query the shell for routine_info of compiled modules and update the lists."
5297 ;; Save and compile the procedure. The compiled procedure is then
5298 ;; saved into an IDL SAVE file, to allow for fast RESTORE.
5299 ;; We need to RESTORE the procedure each time we use it, since
5300 ;; the user may have killed or redefined it. In particular,
5301 ;; .RESET_SESSION will kill all user procedures.
5302 (unless (and idlwave-idlwave_routine_info-compiled
5303 (file-readable-p (idlwave-shell-temp-file 'rinfo)))
5304 (save-excursion
5305 (set-buffer (idlwave-find-file-noselect
5306 (idlwave-shell-temp-file 'pro)))
5307 (erase-buffer)
5308 (insert idlwave-routine-info.pro)
5309 (save-buffer 0))
5310 (idlwave-shell-send-command
5311 (concat ".run " idlwave-shell-temp-pro-file)
5312 nil 'hide wait)
5313 ; (message "SENDING SAVE") ; ????????????????????????
5314 (idlwave-shell-send-command
5315 (format "save,'idlwave_routine_info','idlwave_print_info_entry','idlwave_get_class_tags','idlwave_get_sysvars',FILE='%s',/ROUTINES"
5316 (idlwave-shell-temp-file 'rinfo))
5317 nil 'hide wait))
5318
5319 ;; Restore and execute the procedure, analyze the output
5320 ; (message "SENDING RESTORE & EXECUTE") ; ????????????????????????
5321 (idlwave-shell-send-command
5322 (format "RESTORE, '%s' & idlwave_routine_info"
5323 idlwave-shell-temp-rinfo-save-file)
5324 `(progn
5325 (idlwave-shell-routine-info-filter)
5326 (idlwave-concatenate-rinfo-lists ,quiet ,run-hooks))
5327 'hide wait))
5328
5329 ;; ---------------------------------------------------------------------------
5330 ;;
5331 ;; Completion and displaying routine calling sequences
5332
5333 (defvar idlwave-completion-help-info nil)
5334 (defvar idlwave-completion-help-links nil)
5335 (defvar idlwave-current-obj_new-class nil)
5336 (defvar idlwave-complete-special nil)
5337
5338 (defun idlwave-complete (&optional arg module class)
5339 "Complete a function, procedure or keyword name at point.
5340 This function is smart and figures out what can be legally completed
5341 at this point.
5342 - At the beginning of a statement it completes procedure names.
5343 - In the middle of a statement it completes function names.
5344 - after a `(' or `,' in the argument list of a function or procedure,
5345 it completes a keyword of the relevant function or procedure.
5346 - In the first arg of `OBJ_NEW', it completes a class name.
5347
5348 When several completions are possible, a list will be displayed in the
5349 *Completions* buffer. If this list is too long to fit into the
5350 window, scrolling can be achieved by repeatedly pressing
5351 \\[idlwave-complete].
5352
5353 The function also knows about object methods. When it needs a class
5354 name, the action depends upon `idlwave-query-class', which see. You
5355 can force IDLWAVE to ask you for a class name with a
5356 \\[universal-argument] prefix argument to this command.
5357
5358 See also the variables `idlwave-keyword-completion-adds-equal' and
5359 `idlwave-function-completion-adds-paren'.
5360
5361 The optional ARG can be used to specify the completion type in order
5362 to override IDLWAVE's idea of what should be completed at point.
5363 Possible values are:
5364
5365 0 <=> query for the completion type
5366 1 <=> 'procedure
5367 2 <=> 'procedure-keyword
5368 3 <=> 'function
5369 4 <=> 'function-keyword
5370 5 <=> 'procedure-method
5371 6 <=> 'procedure-method-keyword
5372 7 <=> 'function-method
5373 8 <=> 'function-method-keyword
5374 9 <=> 'class
5375
5376 As a special case, the universal argument C-u forces completion of
5377 function names in places where the default would be a keyword.
5378
5379 Two prefix argument, C-u C-u, prompts for a regexp by which to limit
5380 completion.
5381
5382 For Lisp programmers only:
5383 When we force a keyword, optional argument MODULE can contain the module name.
5384 When we force a method or a method keyword, CLASS can specify the class."
5385 (interactive "P")
5386 (idlwave-routines)
5387 (let* ((where-list
5388 (if (and arg
5389 (or (and (integerp arg) (not (equal arg '(16))))
5390 (symbolp arg)))
5391 (idlwave-make-force-complete-where-list arg module class)
5392 (idlwave-where)))
5393 (what (nth 2 where-list))
5394 (idlwave-force-class-query (equal arg '(4)))
5395 (completion-regexp-list
5396 (if (equal arg '(16))
5397 (list (read-string (concat "Completion Regexp: "))))))
5398
5399 (if (and module (string-match "::" module))
5400 (setq class (substring module 0 (match-beginning 0))
5401 module (substring module (match-end 0))))
5402
5403 (cond
5404
5405 ((and (null arg)
5406 (eq (car-safe last-command) 'idlwave-display-completion-list)
5407 (get-buffer-window "*Completions*"))
5408 (setq this-command last-command)
5409 (idlwave-scroll-completions))
5410
5411 ;; Complete a filename in quotes
5412 ((and (idlwave-in-quote)
5413 (not (eq what 'class)))
5414 (idlwave-complete-filename))
5415
5416 ;; Check for any special completion functions
5417 ((and idlwave-complete-special
5418 (idlwave-call-special idlwave-complete-special)))
5419
5420 ((null what)
5421 (error "Nothing to complete here"))
5422
5423 ;; Complete a class
5424 ((eq what 'class)
5425 (setq idlwave-completion-help-info '(class))
5426 (idlwave-complete-class))
5427
5428 ((eq what 'procedure)
5429 ;; Complete a procedure name
5430 (let* ((cw-list (nth 3 where-list))
5431 (class-selector (idlwave-determine-class cw-list 'pro))
5432 (super-classes (unless (idlwave-explicit-class-listed cw-list)
5433 (idlwave-all-class-inherits class-selector)))
5434 (isa (concat "procedure" (if class-selector "-method" "")))
5435 (type-selector 'pro))
5436 (setq idlwave-completion-help-info
5437 (list 'routine nil type-selector class-selector nil super-classes))
5438 (idlwave-complete-in-buffer
5439 'procedure (if class-selector 'method 'routine)
5440 (idlwave-routines) 'idlwave-selector
5441 (format "Select a %s name%s"
5442 isa
5443 (if class-selector
5444 (format " (class is %s)"
5445 (if (eq class-selector t)
5446 "unknown" class-selector))
5447 ""))
5448 isa
5449 'idlwave-attach-method-classes 'idlwave-add-file-link-selector)))
5450
5451 ((eq what 'function)
5452 ;; Complete a function name
5453 (let* ((cw-list (nth 3 where-list))
5454 (class-selector (idlwave-determine-class cw-list 'fun))
5455 (super-classes (unless (idlwave-explicit-class-listed cw-list)
5456 (idlwave-all-class-inherits class-selector)))
5457 (isa (concat "function" (if class-selector "-method" "")))
5458 (type-selector 'fun))
5459 (setq idlwave-completion-help-info
5460 (list 'routine nil type-selector class-selector nil super-classes))
5461 (idlwave-complete-in-buffer
5462 'function (if class-selector 'method 'routine)
5463 (idlwave-routines) 'idlwave-selector
5464 (format "Select a %s name%s"
5465 isa
5466 (if class-selector
5467 (format " (class is %s)"
5468 (if (eq class-selector t)
5469 "unknown" class-selector))
5470 ""))
5471 isa
5472 'idlwave-attach-method-classes 'idlwave-add-file-link-selector)))
5473
5474 ((and (memq what '(procedure-keyword function-keyword)) ; Special Case
5475 (equal arg '(4)))
5476 (idlwave-complete 3))
5477
5478 ((eq what 'procedure-keyword)
5479 ;; Complete a procedure keyword
5480 (let* ((where (nth 3 where-list))
5481 (name (car where))
5482 (method-selector name)
5483 (type-selector 'pro)
5484 (class (idlwave-determine-class where 'pro))
5485 (class-selector class)
5486 (super-classes (idlwave-all-class-inherits class-selector))
5487 (isa (format "procedure%s-keyword" (if class "-method" "")))
5488 (entry (idlwave-best-rinfo-assq
5489 name 'pro class (idlwave-routines)))
5490 (list (idlwave-entry-keywords entry 'do-link)))
5491 (unless (or entry (eq class t))
5492 (error "Nothing known about procedure %s"
5493 (idlwave-make-full-name class name)))
5494 (setq list (idlwave-fix-keywords name 'pro class list super-classes))
5495 (unless list (error (format "No keywords available for procedure %s"
5496 (idlwave-make-full-name class name))))
5497 (setq idlwave-completion-help-info
5498 (list 'keyword name type-selector class-selector entry super-classes))
5499 (idlwave-complete-in-buffer
5500 'keyword 'keyword list nil
5501 (format "Select keyword for procedure %s%s"
5502 (idlwave-make-full-name class name)
5503 (if (or (member '("_EXTRA") list)
5504 (member '("_REF_EXTRA") list))
5505 " (note _EXTRA)" ""))
5506 isa
5507 'idlwave-attach-keyword-classes)))
5508
5509 ((eq what 'function-keyword)
5510 ;; Complete a function keyword
5511 (let* ((where (nth 3 where-list))
5512 (name (car where))
5513 (method-selector name)
5514 (type-selector 'fun)
5515 (class (idlwave-determine-class where 'fun))
5516 (class-selector class)
5517 (super-classes (idlwave-all-class-inherits class-selector))
5518 (isa (format "function%s-keyword" (if class "-method" "")))
5519 (entry (idlwave-best-rinfo-assq
5520 name 'fun class (idlwave-routines)))
5521 (list (idlwave-entry-keywords entry 'do-link))
5522 msg-name)
5523 (unless (or entry (eq class t))
5524 (error "Nothing known about function %s"
5525 (idlwave-make-full-name class name)))
5526 (setq list (idlwave-fix-keywords name 'fun class list super-classes))
5527 ;; OBJ_NEW: Messages mention the proper Init method
5528 (setq msg-name (if (and (null class)
5529 (string= (upcase name) "OBJ_NEW"))
5530 (concat idlwave-current-obj_new-class
5531 "::Init (via OBJ_NEW)")
5532 (idlwave-make-full-name class name)))
5533 (unless list (error (format "No keywords available for function %s"
5534 msg-name)))
5535 (setq idlwave-completion-help-info
5536 (list 'keyword name type-selector class-selector nil super-classes))
5537 (idlwave-complete-in-buffer
5538 'keyword 'keyword list nil
5539 (format "Select keyword for function %s%s" msg-name
5540 (if (or (member '("_EXTRA") list)
5541 (member '("_REF_EXTRA") list))
5542 " (note _EXTRA)" ""))
5543 isa
5544 'idlwave-attach-keyword-classes)))
5545
5546 (t (error "This should not happen (idlwave-complete)")))))
5547
5548 (defvar idlwave-complete-special nil
5549 "List of special completion functions.
5550 These functions are called for each completion. Each function must
5551 check if its own special completion context is present. If yes, it
5552 should use `idlwave-complete-in-buffer' to do some completion and
5553 return t. If such a function returns t, *no further* attempts to
5554 complete other contexts will be done. If the function returns nil,
5555 other completions will be tried.")
5556
5557 (defun idlwave-call-special (functions &rest args)
5558 (let ((funcs functions)
5559 fun ret)
5560 (catch 'exit
5561 (while (setq fun (pop funcs))
5562 (if (setq ret (apply fun args))
5563 (throw 'exit ret)))
5564 nil)))
5565
5566 (defun idlwave-make-force-complete-where-list (what &optional module class)
5567 ;; Return an artificial WHERE specification to force the completion
5568 ;; routine to complete a specific item independent of context.
5569 ;; WHAT is the prefix arg of `idlwave-complete', see there for details.
5570 ;; MODULE and CLASS can be used to specify the routine name and class.
5571 ;; The class name will also be found in MODULE if that is like "class::mod".
5572 (let* ((what-list '(("procedure") ("procedure-keyword")
5573 ("function") ("function-keyword")
5574 ("procedure-method") ("procedure-method-keyword")
5575 ("function-method") ("function-method-keyword")
5576 ("class")))
5577 (module (idlwave-sintern-routine-or-method module class))
5578 (class (idlwave-sintern-class class))
5579 (what (cond
5580 ((equal what 0)
5581 (setq what
5582 (intern (completing-read
5583 "Complete what? " what-list nil t))))
5584 ((integerp what)
5585 (setq what (intern (car (nth (1- what) what-list)))))
5586 ((and what
5587 (symbolp what)
5588 (assoc (symbol-name what) what-list))
5589 what)
5590 (t (error "Illegal WHAT"))))
5591 (nil-list '(nil nil nil nil))
5592 (class-list (list nil nil (or class t) nil)))
5593
5594 (cond
5595
5596 ((eq what 'procedure)
5597 (list nil-list nil-list 'procedure nil-list nil))
5598
5599 ((eq what 'procedure-keyword)
5600 (let* ((class-selector nil)
5601 (super-classes nil)
5602 (type-selector 'pro)
5603 (pro (or module
5604 (idlwave-completing-read
5605 "Procedure: " (idlwave-routines) 'idlwave-selector))))
5606 (setq pro (idlwave-sintern-routine pro))
5607 (list nil-list nil-list 'procedure-keyword
5608 (list pro nil nil nil) nil)))
5609
5610 ((eq what 'function)
5611 (list nil-list nil-list 'function nil-list nil))
5612
5613 ((eq what 'function-keyword)
5614 (let* ((class-selector nil)
5615 (super-classes nil)
5616 (type-selector 'fun)
5617 (func (or module
5618 (idlwave-completing-read
5619 "Function: " (idlwave-routines) 'idlwave-selector))))
5620 (setq func (idlwave-sintern-routine func))
5621 (list nil-list nil-list 'function-keyword
5622 (list func nil nil nil) nil)))
5623
5624 ((eq what 'procedure-method)
5625 (list nil-list nil-list 'procedure class-list nil))
5626
5627 ((eq what 'procedure-method-keyword)
5628 (let* ((class (idlwave-determine-class class-list 'pro))
5629 (class-selector class)
5630 (super-classes (idlwave-all-class-inherits class-selector))
5631 (type-selector 'pro)
5632 (pro (or module
5633 (idlwave-completing-read
5634 (format "Procedure in %s class: " class-selector)
5635 (idlwave-routines) 'idlwave-selector))))
5636 (setq pro (idlwave-sintern-method pro))
5637 (list nil-list nil-list 'procedure-keyword
5638 (list pro nil class nil) nil)))
5639
5640 ((eq what 'function-method)
5641 (list nil-list nil-list 'function class-list nil))
5642
5643 ((eq what 'function-method-keyword)
5644 (let* ((class (idlwave-determine-class class-list 'fun))
5645 (class-selector class)
5646 (super-classes (idlwave-all-class-inherits class-selector))
5647 (type-selector 'fun)
5648 (func (or module
5649 (idlwave-completing-read
5650 (format "Function in %s class: " class-selector)
5651 (idlwave-routines) 'idlwave-selector))))
5652 (setq func (idlwave-sintern-method func))
5653 (list nil-list nil-list 'function-keyword
5654 (list func nil class nil) nil)))
5655
5656 ((eq what 'class)
5657 (list nil-list nil-list 'class nil-list nil))
5658
5659 (t (error "Illegal value for WHAT")))))
5660
5661 (defun idlwave-completing-read (&rest args)
5662 ;; Completing read, case insensitive
5663 (let ((old-value (default-value 'completion-ignore-case)))
5664 (unwind-protect
5665 (progn
5666 (setq-default completion-ignore-case t)
5667 (apply 'completing-read args))
5668 (setq-default completion-ignore-case old-value))))
5669
5670 (defvar idlwave-shell-default-directory)
5671 (defun idlwave-complete-filename ()
5672 "Use the comint stuff to complete a file name."
5673 (require 'comint)
5674 (let* ((comint-file-name-chars "~/A-Za-z0-9+@:_.$#%={}\\-")
5675 (comint-completion-addsuffix nil)
5676 (default-directory
5677 (if (and (boundp 'idlwave-shell-default-directory)
5678 (stringp idlwave-shell-default-directory)
5679 (file-directory-p idlwave-shell-default-directory))
5680 idlwave-shell-default-directory
5681 default-directory)))
5682 (comint-dynamic-complete-filename)))
5683
5684 (defun idlwave-make-full-name (class name)
5685 ;; Make a fully qualified module name including the class name
5686 (concat (if class (format "%s::" class) "") name))
5687
5688 (defun idlwave-rinfo-assoc (name type class list)
5689 "Like `idlwave-rinfo-assq', but sintern strings first."
5690 (idlwave-rinfo-assq
5691 (idlwave-sintern-routine-or-method name class)
5692 type (idlwave-sintern-class class) list))
5693
5694 (defun idlwave-rinfo-assq (name type class list)
5695 ;; Works like assq, but also checks type and class
5696 (catch 'exit
5697 (let (match)
5698 (while (setq match (assq name list))
5699 (and (or (eq type t)
5700 (eq (nth 1 match) type))
5701 (eq (nth 2 match) class)
5702 (throw 'exit match))
5703 (setq list (cdr (memq match list)))))))
5704
5705 (defun idlwave-rinfo-assq-any-class (name type class list)
5706 ;; Return the first matching method on the inheritance list
5707 (let* ((classes (cons class (idlwave-all-class-inherits class)))
5708 class rtn)
5709 (while classes
5710 (if (setq rtn (idlwave-rinfo-assq name type (pop classes) list))
5711 (setq classes nil)))
5712 rtn))
5713
5714 (defun idlwave-best-rinfo-assq (name type class list &optional with-file
5715 keep-system)
5716 "Like `idlwave-rinfo-assq', but get all twins and sort, then return first.
5717 If WITH-FILE is passed, find the best rinfo entry with a file
5718 included. If KEEP-SYSTEM is set, don't prune system for compiled
5719 syslib files."
5720 (let ((twins (idlwave-routine-twins
5721 (idlwave-rinfo-assq-any-class name type class list)
5722 list))
5723 syslibp)
5724 (when (> (length twins) 1)
5725 (setq twins (sort twins 'idlwave-routine-entry-compare-twins))
5726 (if (and (null keep-system)
5727 (eq 'system (car (nth 3 (car twins))))
5728 (setq syslibp (idlwave-any-syslib (cdr twins)))
5729 (not (equal 1 syslibp)))
5730 ;; Its a compiled syslib, so we need to remove the system entry
5731 (setq twins (cdr twins)))
5732 (if with-file
5733 (setq twins (delq nil
5734 (mapcar (lambda (x)
5735 (if (nth 1 (nth 3 x)) x))
5736 twins)))))
5737 (car twins)))
5738
5739 (defun idlwave-best-rinfo-assoc (name type class list &optional with-file
5740 keep-system)
5741 "Like `idlwave-best-rinfo-assq', but sintern strings first."
5742 (idlwave-best-rinfo-assq
5743 (idlwave-sintern-routine-or-method name class)
5744 type (idlwave-sintern-class class) list with-file keep-system))
5745
5746 (defun idlwave-any-syslib (entries)
5747 "Does the entry list ENTRIES contain a syslib entry?
5748 If yes, return the index (>=1)."
5749 (let (file (cnt 0))
5750 (catch 'exit
5751 (while entries
5752 (incf cnt)
5753 (setq file (idlwave-routine-source-file (nth 3 (car entries))))
5754 (if (and file (idlwave-syslib-p file))
5755 (throw 'exit cnt)
5756 (setq entries (cdr entries))))
5757 nil)))
5758
5759 (defun idlwave-all-assq (key list)
5760 "Return a list of all associations of Key in LIST."
5761 (let (rtn elt)
5762 (while (setq elt (assq key list))
5763 (push elt rtn)
5764 (setq list (cdr (memq elt list))))
5765 (nreverse rtn)))
5766
5767 (defun idlwave-all-method-classes (method &optional type)
5768 "Return all classes which have a method METHOD. TYPE is 'fun or 'pro.
5769 When TYPE is not specified, both procedures and functions will be considered."
5770 (if (null method)
5771 (mapcar 'car (idlwave-class-alist))
5772 (let (rtn)
5773 (mapcar (lambda (x)
5774 (and (nth 2 x)
5775 (or (not type)
5776 (eq type (nth 1 x)))
5777 (push (nth 2 x) rtn)))
5778 (idlwave-all-assq method (idlwave-routines)))
5779 (idlwave-uniquify rtn))))
5780
5781 (defun idlwave-all-method-keyword-classes (method keyword &optional type)
5782 "Return all classes which have a method METHOD with keyword KEYWORD.
5783 TYPE is 'fun or 'pro.
5784 When TYPE is not specified, both procedures and functions will be considered."
5785 (if (or (null method)
5786 (null keyword))
5787 nil
5788 (let (rtn)
5789 (mapcar (lambda (x)
5790 (and (nth 2 x) ; non-nil class
5791 (or (not type) ; correct or unspecified type
5792 (eq type (nth 1 x)))
5793 (assoc keyword (idlwave-entry-keywords x))
5794 (push (nth 2 x) rtn)))
5795 (idlwave-all-assq method (idlwave-routines)))
5796 (idlwave-uniquify rtn))))
5797
5798 (defun idlwave-members-only (list club)
5799 "Return list of all elements in LIST which are also in CLUB."
5800 (let (rtn)
5801 (while list
5802 (if (member (car list) club)
5803 (setq rtn (cons (car list) rtn)))
5804 (setq list (cdr list)))
5805 (nreverse rtn)))
5806
5807 (defun idlwave-nonmembers-only (list club)
5808 "Return list of all elements in LIST which are not in CLUB."
5809 (let (rtn)
5810 (while list
5811 (if (member (car list) club)
5812 nil
5813 (setq rtn (cons (car list) rtn)))
5814 (setq list (cdr list)))
5815 (nreverse rtn)))
5816
5817 (defun idlwave-explicit-class-listed (info)
5818 "Return whether or not the class is listed explicitly, ala a->b::c.
5819 INFO is as returned by idlwave-what-function or -procedure."
5820 (let ((apos (nth 3 info)))
5821 (if apos
5822 (save-excursion (goto-char apos)
5823 (looking-at "->[a-zA-Z][a-zA-Z0-9$_]*::")))))
5824
5825 (defvar idlwave-determine-class-special nil
5826 "List of special functions for determining class.
5827 Must accept two arguments: `apos' and `info'")
5828
5829 (defun idlwave-determine-class (info type)
5830 ;; Determine the class of a routine call.
5831 ;; INFO is the `cw-list' structure as returned by idlwave-where.
5832 ;; The second element in this structure is the class. When nil, we
5833 ;; return nil. When t, try to get the class from text properties at
5834 ;; the arrow. When the object is "self", we use the class of the
5835 ;; current routine. otherwise prompt the user for a class name.
5836 ;; Also stores the selected class as a text property at the arrow.
5837 ;; TYPE is 'fun or 'pro.
5838 (let* ((class (nth 2 info))
5839 (apos (nth 3 info))
5840 (nassoc (assoc (if (stringp (car info))
5841 (upcase (car info))
5842 (car info))
5843 idlwave-query-class))
5844 (dassoc (assq (if (car info) 'keyword-default 'method-default)
5845 idlwave-query-class))
5846 (query (cond (nassoc (cdr nassoc))
5847 (dassoc (cdr dassoc))
5848 (t t)))
5849 (arrow (and apos (string= (buffer-substring apos (+ 2 apos)) "->")))
5850 (is-self
5851 (and arrow
5852 (save-excursion (goto-char apos)
5853 (forward-word -1)
5854 (let ((case-fold-search t))
5855 (looking-at "self\\>")))))
5856 (force-query idlwave-force-class-query)
5857 store special-class class-alist)
5858 (cond
5859 ((null class) nil)
5860 ((eq t class)
5861 ;; There is an object which would like to know its class
5862 (if (and arrow (get-text-property apos 'idlwave-class)
5863 idlwave-store-inquired-class
5864 (not force-query))
5865 (setq class (get-text-property apos 'idlwave-class)
5866 class (idlwave-sintern-class class)))
5867 (if (and (eq t class) is-self)
5868 (setq class (or (nth 2 (idlwave-current-routine)) class)))
5869
5870 ;; Before prompting, try any special class determination routines
5871 (when (and (eq t class)
5872 idlwave-determine-class-special
5873 (not force-query))
5874 (setq special-class
5875 (idlwave-call-special idlwave-determine-class-special apos))
5876 (if special-class
5877 (setq class (idlwave-sintern-class special-class)
5878 store idlwave-store-inquired-class)))
5879
5880 ;; Prompt for a class, if we need to
5881 (when (and (eq class t)
5882 (or force-query query))
5883 (setq class-alist
5884 (mapcar 'list (idlwave-all-method-classes (car info) type)))
5885 (setq class
5886 (idlwave-sintern-class
5887 (cond
5888 ((and (= (length class-alist) 0) (not force-query))
5889 (error "No classes available with method %s" (car info)))
5890 ((and (= (length class-alist) 1) (not force-query))
5891 (car (car class-alist)))
5892 (t
5893 (setq store idlwave-store-inquired-class)
5894 (idlwave-completing-read
5895 (format "Class%s: " (if (stringp (car info))
5896 (format " for %s method %s"
5897 type (car info))
5898 ""))
5899 class-alist nil nil nil 'idlwave-class-history))))))
5900
5901 ;; Store it, if requested
5902 (when (and class (not (eq t class)))
5903 ;; We have a real class here
5904 (when (and store arrow)
5905 (condition-case ()
5906 (add-text-properties
5907 apos (+ apos 2)
5908 `(idlwave-class ,class face ,idlwave-class-arrow-face
5909 rear-nonsticky t))
5910 (error nil)))
5911 (setf (nth 2 info) class))
5912 ;; Return the class
5913 class)
5914 ;; Default as fallback
5915 (t class))))
5916
5917 (defvar type-selector)
5918 (defvar class-selector)
5919 (defvar method-selector)
5920 (defvar super-classes)
5921 (defun idlwave-selector (a)
5922 (and (eq (nth 1 a) type-selector)
5923 (or (and (nth 2 a) (eq class-selector t))
5924 (eq (nth 2 a) class-selector)
5925 (memq (nth 2 a) super-classes))))
5926
5927 (defun idlwave-add-file-link-selector (a)
5928 ;; Record a file link, if any, for the tested names during selection.
5929 (let ((sel (idlwave-selector a)) file)
5930 (if (and sel (setq file (idlwave-entry-has-help a)))
5931 (push (cons (car a) file) idlwave-completion-help-links))
5932 sel))
5933
5934
5935 (defun idlwave-where ()
5936 "Find out where we are.
5937 The return value is a list with the following stuff:
5938 \(PRO-LIST FUNC-LIST COMPLETE-WHAT CW-LIST LAST-CHAR)
5939
5940 PRO-LIST (PRO POINT CLASS ARROW)
5941 FUNC-LIST (FUNC POINT CLASS ARROW)
5942 COMPLETE-WHAT a symbol indicating what kind of completion makes sense here
5943 CW-LIST (PRO-OR-FUNC POINT CLASS ARROW) Like PRO-LIST, for what can
5944 be completed here.
5945 LAST-CHAR last relevant character before point (non-white non-comment,
5946 not part of current identifier or leading slash).
5947
5948 In the lists, we have these meanings:
5949 PRO: Procedure name
5950 FUNC: Function name
5951 POINT: Where is this
5952 CLASS: What class has the routine (nil=no, t=is method, but class unknown)
5953 ARROW: Location of the arrow"
5954 (idlwave-routines)
5955 (let* (;(bos (save-excursion (idlwave-beginning-of-statement) (point)))
5956 (bos (save-excursion (idlwave-start-of-substatement 'pre) (point)))
5957 (func-entry (idlwave-what-function bos))
5958 (func (car func-entry))
5959 (func-class (nth 1 func-entry))
5960 (func-arrow (nth 2 func-entry))
5961 (func-point (or (nth 3 func-entry) 0))
5962 (func-level (or (nth 4 func-entry) 0))
5963 (pro-entry (idlwave-what-procedure bos))
5964 (pro (car pro-entry))
5965 (pro-class (nth 1 pro-entry))
5966 (pro-arrow (nth 2 pro-entry))
5967 (pro-point (or (nth 3 pro-entry) 0))
5968 (last-char (idlwave-last-valid-char))
5969 (case-fold-search t)
5970 (match-string (buffer-substring bos (point)))
5971 cw cw-mod cw-arrow cw-class cw-point)
5972 (if (< func-point pro-point) (setq func nil))
5973 (cond
5974 ((string-match "\\`[ \t]*\\(pro\\|function\\)[ \t]+[a-zA-Z0-9_]*\\'"
5975 match-string)
5976 (setq cw 'class))
5977 ((string-match
5978 "\\`[ \t]*\\([a-zA-Z][a-zA-Z0-9$_]*\\)?\\'"
5979 (if (> pro-point 0)
5980 (buffer-substring pro-point (point))
5981 match-string))
5982 (setq cw 'procedure cw-class pro-class cw-point pro-point
5983 cw-arrow pro-arrow))
5984 ((string-match "\\`[ \t]*\\(pro\\|function\\)\\>"
5985 match-string)
5986 nil)
5987 ((string-match "OBJ_NEW([ \t]*['\"]\\([a-zA-Z0-9$_]*\\)?\\'"
5988 match-string)
5989 (setq cw 'class))
5990 ((string-match "\\<inherits\\s-+\\([a-zA-Z0-9$_]*\\)?\\'"
5991 match-string)
5992 (setq cw 'class))
5993 ((and func
5994 (> func-point pro-point)
5995 (= func-level 1)
5996 (memq last-char '(?\( ?,)))
5997 (setq cw 'function-keyword cw-mod func cw-point func-point
5998 cw-class func-class cw-arrow func-arrow))
5999 ((and pro (eq last-char ?,))
6000 (setq cw 'procedure-keyword cw-mod pro cw-point pro-point
6001 cw-class pro-class cw-arrow pro-arrow))
6002 ; ((member last-char '(?\' ?\) ?\] ?!))
6003 ; ;; after these chars, a function makes no sense
6004 ; ;; FIXME: I am sure there can be more in this list
6005 ; ;; FIXME: Do we want to do this at all?
6006 ; nil)
6007 ;; Everywhere else we try a function.
6008 (t
6009 (setq cw 'function)
6010 (save-excursion
6011 (if (re-search-backward "->[ \t]*\\(\\$[ \t]*\\(;.*\\)?\n\\s-*\\)?\\(\\([$a-zA-Z0-9_]+\\)::\\)?[$a-zA-Z0-9_]*\\=" bos t)
6012 (setq cw-arrow (copy-marker (match-beginning 0))
6013 cw-class (if (match-end 4)
6014 (idlwave-sintern-class (match-string 4))
6015 t))))))
6016 (list (list pro pro-point pro-class pro-arrow)
6017 (list func func-point func-class func-arrow)
6018 cw
6019 (list cw-mod cw-point cw-class cw-arrow)
6020 last-char)))
6021
6022 (defun idlwave-this-word (&optional class)
6023 ;; Grab the word around point. CLASS is for the `skip-chars=...' functions
6024 (setq class (or class "a-zA-Z0-9$_."))
6025 (save-excursion
6026 (buffer-substring
6027 (progn (skip-chars-backward class) (point))
6028 (progn (skip-chars-forward class) (point)))))
6029
6030 (defun idlwave-what-function (&optional bound)
6031 ;; Find out if point is within the argument list of a function.
6032 ;; The return value is ("function-name" class arrow-start (point) level).
6033 ;; Level is 1 on the top level parentheses, higher further down.
6034
6035 ;; If the optional BOUND is an integer, bound backwards directed
6036 ;; searches to this point.
6037
6038 (catch 'exit
6039 (let (pos
6040 func-point
6041 (cnt 0)
6042 func arrow-start class)
6043 (idlwave-with-special-syntax
6044 (save-restriction
6045 (save-excursion
6046 (narrow-to-region (max 1 (or bound 0)) (point-max))
6047 ;; move back out of the current parenthesis
6048 (while (condition-case nil
6049 (progn (up-list -1) t)
6050 (error nil))
6051 (setq pos (point))
6052 (incf cnt)
6053 (when (and (= (following-char) ?\()
6054 (re-search-backward
6055 "\\(::\\|\\<\\)\\([a-zA-Z][a-zA-Z0-9$_]*\\)[ \t]*\\="
6056 bound t))
6057 (setq func (match-string 2)
6058 func-point (goto-char (match-beginning 2))
6059 pos func-point)
6060 (if (re-search-backward
6061 "->[ \t]*\\(\\([a-zA-Z][a-zA-Z0-9$_]*\\)::\\)?\\=" bound t)
6062 (setq arrow-start (copy-marker (match-beginning 0))
6063 class (or (match-string 2) t)))
6064 (throw
6065 'exit
6066 (list
6067 (idlwave-sintern-routine-or-method func class)
6068 (idlwave-sintern-class class)
6069 arrow-start func-point cnt)))
6070 (goto-char pos))
6071 (throw 'exit nil)))))))
6072
6073 (defun idlwave-what-procedure (&optional bound)
6074 ;; Find out if point is within the argument list of a procedure.
6075 ;; The return value is ("procedure-name" class arrow-pos (point)).
6076
6077 ;; If the optional BOUND is an integer, bound backwards directed
6078 ;; searches to this point.
6079 (let ((pos (point)) pro-point
6080 pro class arrow-start string)
6081 (save-excursion
6082 ;;(idlwave-beginning-of-statement)
6083 (idlwave-start-of-substatement 'pre)
6084 (setq string (buffer-substring (point) pos))
6085 (if (string-match
6086 "\\`[ \t]*\\([a-zA-Z][a-zA-Z0-9$_]*\\)[ \t]*\\(,\\|\\'\\)" string)
6087 (setq pro (match-string 1 string)
6088 pro-point (+ (point) (match-beginning 1)))
6089 (if (and (idlwave-skip-object)
6090 (setq string (buffer-substring (point) pos))
6091 (string-match
6092 "\\`[ \t]*\\(->\\)[ \t]*\\(\\([a-zA-Z][a-zA-Z0-9$_]*\\)::\\)?\\([a-zA-Z][a-zA-Z0-9$_]*\\)?[ \t]*\\(,\\|\\(\\$\\s *\\(;.*\\)?\\)?$\\)"
6093 string))
6094 (setq pro (if (match-beginning 4)
6095 (match-string 4 string))
6096 pro-point (if (match-beginning 4)
6097 (+ (point) (match-beginning 4))
6098 pos)
6099 arrow-start (copy-marker (+ (point) (match-beginning 1)))
6100 class (or (match-string 3 string) t)))))
6101 (list (idlwave-sintern-routine-or-method pro class)
6102 (idlwave-sintern-class class)
6103 arrow-start
6104 pro-point)))
6105
6106 (defun idlwave-skip-object ()
6107 ;; If there is an object at point, move over it and return t.
6108 (let ((pos (point)))
6109 (if (catch 'exit
6110 (save-excursion
6111 (skip-chars-forward " ") ; white space
6112 (skip-chars-forward "*") ; de-reference
6113 (cond
6114 ((looking-at idlwave-identifier)
6115 (goto-char (match-end 0)))
6116 ((eq (following-char) ?\()
6117 nil)
6118 (t (throw 'exit nil)))
6119 (catch 'endwhile
6120 (while t
6121 (cond ((eq (following-char) ?.)
6122 (forward-char 1)
6123 (if (not (looking-at idlwave-identifier))
6124 (throw 'exit nil))
6125 (goto-char (match-end 0)))
6126 ((memq (following-char) '(?\( ?\[))
6127 (condition-case nil
6128 (forward-list 1)
6129 (error (throw 'exit nil))))
6130 (t (throw 'endwhile t)))))
6131 (if (looking-at "[ \t]*->")
6132 (throw 'exit (setq pos (match-beginning 0)))
6133 (throw 'exit nil))))
6134 (goto-char pos)
6135 nil)))
6136
6137 (defun idlwave-last-valid-char ()
6138 "Return the last character before point which is not white or a comment
6139 and also not part of the current identifier. Since we do this in
6140 order to identify places where keywords are, we consider the initial
6141 `/' of a keyword as part of the identifier.
6142 This function is not general, can only be used for completion stuff."
6143 (catch 'exit
6144 (save-excursion
6145 ;; skip the current identifier
6146 (skip-chars-backward "a-zA-Z0-9_$")
6147 ;; also skip a leading slash which might be belong to the keyword
6148 (if (eq (preceding-char) ?/)
6149 (backward-char 1))
6150 ;; FIXME: does not check if this is a valid identifier
6151 (while t
6152 (skip-chars-backward " \t")
6153 (cond
6154 ((memq (preceding-char) '(?\; ?\$)) (throw 'exit nil))
6155 ((eq (preceding-char) ?\n)
6156 (beginning-of-line 0)
6157 (if (looking-at "\\([^;\n]*\\)\\$[ \t]*\\(;[^\n]*\\)?\n")
6158 ;; continuation line
6159 (goto-char (match-end 1))
6160 (throw 'exit nil)))
6161 (t (throw 'exit (preceding-char))))))))
6162
6163 (defvar idlwave-complete-after-success-form nil
6164 "A form to evaluate after successful completion.")
6165 (defvar idlwave-complete-after-success-form-force nil
6166 "A form to evaluate after completion selection in *Completions* buffer.")
6167 (defconst idlwave-completion-mark (make-marker)
6168 "A mark pointing to the beginning of the completion string.")
6169
6170 (defun idlwave-complete-in-buffer (type stype list selector prompt isa
6171 &optional prepare-display-function
6172 special-selector)
6173 "Perform TYPE completion of word before point against LIST.
6174 SELECTOR is the PREDICATE argument for the completion function. Show
6175 PROMPT in echo area. TYPE is one of the intern types, e.g. 'function,
6176 'procedure, 'class-tag, 'keyword, 'sysvar, etc.. SPECIAL-SELECTOR is
6177 used only once, for `all-completions', and can be used to, e.g.,
6178 accumulate information on matching completions."
6179 (let* ((completion-ignore-case t)
6180 beg (end (point)) slash part spart completion all-completions
6181 dpart dcompletion)
6182
6183 (unless list
6184 (error (concat prompt ": No completions available")))
6185
6186 ;; What is already in the buffer?
6187 (save-excursion
6188 (skip-chars-backward "a-zA-Z0-9_$")
6189 (setq slash (eq (preceding-char) ?/)
6190 beg (point)
6191 idlwave-complete-after-success-form
6192 (list 'idlwave-after-successful-completion
6193 (list 'quote type) slash beg)
6194 idlwave-complete-after-success-form-force
6195 (list 'idlwave-after-successful-completion
6196 (list 'quote type) slash (list 'quote 'force))))
6197
6198 ;; Try a completion
6199 (setq part (buffer-substring beg end)
6200 dpart (downcase part)
6201 spart (idlwave-sintern stype part)
6202 completion (try-completion part list selector)
6203 dcompletion (if (stringp completion) (downcase completion))
6204 idlwave-completion-help-links nil)
6205 (cond
6206 ((null completion)
6207 ;; nothing available.
6208 (error (concat prompt ": no completion for \"%s\"") part))
6209 ((and (not (equal dpart dcompletion))
6210 (not (eq t completion)))
6211 ;; We can add something
6212 (delete-region beg end)
6213 (if (and (string= part dpart)
6214 (or (not (string= part ""))
6215 idlwave-complete-empty-string-as-lower-case)
6216 (not idlwave-completion-force-default-case))
6217 (insert dcompletion)
6218 (insert completion))
6219 (if (eq t (try-completion completion list selector))
6220 ;; Now this is a unique match
6221 (idlwave-after-successful-completion type slash beg))
6222 t)
6223 ((or (eq completion t)
6224 (and (= 1 (length (setq all-completions
6225 (idlwave-uniquify
6226 (all-completions part list
6227 (or special-selector
6228 selector))))))
6229 (equal dpart dcompletion)))
6230 ;; This is already complete
6231 (idlwave-after-successful-completion type slash beg)
6232 (message "%s is already the complete %s" part isa)
6233 nil)
6234 (t
6235 ;; We cannot add something - offer a list.
6236 (message "Making completion list...")
6237
6238 (unless idlwave-completion-help-links ; already set somewhere?
6239 (mapcar (lambda (x) ; Pass link prop through to highlight-linked
6240 (let ((link (get-text-property 0 'link (car x))))
6241 (if link
6242 (push (cons (car x) link)
6243 idlwave-completion-help-links))))
6244 list))
6245 (let* ((list all-completions)
6246 ;; "complete" means, this is already a valid completion
6247 (complete (memq spart all-completions))
6248 (completion-highlight-first-word-only t)) ; XEmacs
6249 ; (completion-fixup-function ; Emacs
6250 ; (lambda () (and (eq (preceding-char) ?>)
6251 ; (re-search-backward " <" beg t)))))
6252
6253 (setq list (sort list (lambda (a b)
6254 (string< (downcase a) (downcase b)))))
6255 (if prepare-display-function
6256 (setq list (funcall prepare-display-function list)))
6257 (if (and (string= part dpart)
6258 (or (not (string= part ""))
6259 idlwave-complete-empty-string-as-lower-case)
6260 (not idlwave-completion-force-default-case))
6261 (setq list (mapcar (lambda (x)
6262 (if (listp x)
6263 (setcar x (downcase (car x)))
6264 (setq x (downcase x)))
6265 x)
6266 list)))
6267 (idlwave-display-completion-list list prompt beg complete))
6268 t))))
6269
6270 (defun idlwave-complete-class ()
6271 "Complete a class at point."
6272 (interactive)
6273 ;; Call `idlwave-routines' to make sure the class list will be available
6274 (idlwave-routines)
6275 ;; Check for the special case of completing empty string after pro/function
6276 (if (let ((case-fold-search t))
6277 (save-excursion
6278 (and
6279 (re-search-backward "\\<\\(pro\\|function\\)[ \t]+\\="
6280 (- (point) 15) t)
6281 (goto-char (point-min))
6282 (re-search-forward
6283 "^[ \t]*\\(pro\\|function\\)[ \t]+\\([a-zA-Z0-9_]+::\\)" nil t))))
6284 ;; Yank the full class specification
6285 (insert (match-string 2))
6286 ;; Do the completion, using list gathered from `idlwave-routines'
6287 (idlwave-complete-in-buffer
6288 'class 'class (idlwave-class-alist) nil
6289 "Select a class" "class"
6290 '(lambda (list) ;; Push it to help-links if system help available
6291 (mapcar (lambda (x)
6292 (let* ((entry (idlwave-class-info x))
6293 (link (nth 1 (assq 'link entry))))
6294 (if link (push (cons x link)
6295 idlwave-completion-help-links))
6296 x))
6297 list)))))
6298
6299 (defun idlwave-attach-classes (list type show-classes)
6300 ;; Attach the proper class list to a LIST of completion items.
6301 ;; TYPE, when 'kwd, shows classes for method keywords, when
6302 ;; 'class-tag, for class tags, and otherwise for methods.
6303 ;; SHOW-CLASSES is the value of `idlwave-completion-show-classes'.
6304 (if (or (null show-classes) ; don't want to see classes
6305 (null class-selector) ; not a method call
6306 (and
6307 (stringp class-selector) ; the class is already known
6308 (not super-classes))) ; no possibilities for inheritance
6309 ;; In these cases, we do not have to do anything
6310 list
6311 (let* ((do-prop (and (>= show-classes 0)
6312 (>= emacs-major-version 21)))
6313 (do-buf (not (= show-classes 0)))
6314 ;; (do-dots (featurep 'xemacs))
6315 (do-dots t)
6316 (inherit (if (and (not (eq type 'class-tag)) super-classes)
6317 (cons class-selector super-classes)))
6318 (max (abs show-classes))
6319 (lmax (if do-dots (apply 'max (mapcar 'length list))))
6320 classes nclasses class-info space)
6321 (mapcar
6322 (lambda (x)
6323 ;; get the classes
6324 (if (eq type 'class-tag)
6325 ;; Just one class for tags
6326 (setq classes
6327 (list
6328 (idlwave-class-or-superclass-with-tag class-selector x)))
6329 ;; Multiple classes for method or method-keyword
6330 (setq classes
6331 (if (eq type 'kwd)
6332 (idlwave-all-method-keyword-classes
6333 method-selector x type-selector)
6334 (idlwave-all-method-classes x type-selector)))
6335 (if inherit
6336 (setq classes
6337 (delq nil
6338 (mapcar (lambda (x) (if (memq x inherit) x nil))
6339 classes)))))
6340 (setq nclasses (length classes))
6341 ;; Make the separator between item and class-info
6342 (if do-dots
6343 (setq space (concat " " (make-string (- lmax (length x)) ?.)))
6344 (setq space " "))
6345 (if do-buf
6346 ;; We do want info in the buffer
6347 (if (<= nclasses max)
6348 (setq class-info (concat
6349 space
6350 "<" (mapconcat 'identity classes ",") ">"))
6351 (setq class-info (format "%s<%d classes>" space nclasses)))
6352 (setq class-info nil))
6353 (when do-prop
6354 ;; We do want properties
6355 (setq x (copy-sequence x))
6356 (put-text-property 0 (length x)
6357 'help-echo (mapconcat 'identity classes " ")
6358 x))
6359 (if class-info
6360 (list x class-info)
6361 x))
6362 list))))
6363
6364 (defun idlwave-attach-method-classes (list)
6365 ;; Call idlwave-attach-classes with method parameters
6366 (idlwave-attach-classes list 'method idlwave-completion-show-classes))
6367 (defun idlwave-attach-keyword-classes (list)
6368 ;; Call idlwave-attach-classes with keyword parameters
6369 (idlwave-attach-classes list 'kwd idlwave-completion-show-classes))
6370 (defun idlwave-attach-class-tag-classes (list)
6371 ;; Call idlwave-attach-classes with class structure tags
6372 (idlwave-attach-classes list 'class-tag idlwave-completion-show-classes))
6373
6374
6375 ;;----------------------------------------------------------------------
6376 ;;----------------------------------------------------------------------
6377 ;;----------------------------------------------------------------------
6378 ;;----------------------------------------------------------------------
6379 ;;----------------------------------------------------------------------
6380 (defvar rtn)
6381 (defun idlwave-pset (item)
6382 (set 'rtn item))
6383
6384 (defun idlwave-popup-select (ev list title &optional sort)
6385 "Select an item in LIST with a popup menu.
6386 TITLE is the title to put atop the popup. If SORT is non-nil,
6387 sort the list before displaying"
6388 (let ((maxpopup idlwave-max-popup-menu-items)
6389 rtn menu resp)
6390 (cond ((null list))
6391 ((= 1 (length list))
6392 (setq rtn (car list)))
6393 ((featurep 'xemacs)
6394 (if sort (setq list (sort list (lambda (a b)
6395 (string< (upcase a) (upcase b))))))
6396 (setq menu
6397 (append (list title)
6398 (mapcar (lambda (x) (vector x (list 'idlwave-pset
6399 x)))
6400 list)))
6401 (setq menu (idlwave-split-menu-xemacs menu maxpopup))
6402 (setq resp (get-popup-menu-response menu))
6403 (funcall (event-function resp) (event-object resp)))
6404 (t
6405 (if sort (setq list (sort list (lambda (a b)
6406 (string< (upcase a) (upcase b))))))
6407 (setq menu (cons title
6408 (list
6409 (append (list "")
6410 (mapcar (lambda(x) (cons x x)) list)))))
6411 (setq menu (idlwave-split-menu-emacs menu maxpopup))
6412 (setq rtn (x-popup-menu ev menu))))
6413 rtn))
6414
6415 (defun idlwave-split-menu-xemacs (menu N)
6416 "Split the MENU into submenus of maximum length N."
6417 (if (<= (length menu) (1+ N))
6418 ;; No splitting needed
6419 menu
6420 (let* ((title (car menu))
6421 (entries (cdr menu))
6422 (menu (list title))
6423 (cnt 0)
6424 (nextmenu nil))
6425 (while entries
6426 (while (and entries (< cnt N))
6427 (setq cnt (1+ cnt)
6428 nextmenu (cons (car entries) nextmenu)
6429 entries (cdr entries)))
6430 (setq nextmenu (nreverse nextmenu))
6431 (setq nextmenu (cons (format "%s...%s"
6432 (aref (car nextmenu) 0)
6433 (aref (nth (1- cnt) nextmenu) 0))
6434 nextmenu))
6435 (setq menu (cons nextmenu menu)
6436 nextmenu nil
6437 cnt 0))
6438 (nreverse menu))))
6439
6440 (defun idlwave-split-menu-emacs (menu N)
6441 "Split the MENU into submenus of maximum length N."
6442 (if (<= (length (nth 1 menu)) (1+ N))
6443 ;; No splitting needed
6444 menu
6445 (let* ((title (car menu))
6446 (entries (cdr (nth 1 menu)))
6447 (menu nil)
6448 (cnt 0)
6449 (nextmenu nil))
6450 (while entries
6451 (while (and entries (< cnt N))
6452 (setq cnt (1+ cnt)
6453 nextmenu (cons (car entries) nextmenu)
6454 entries (cdr entries)))
6455 (setq nextmenu (nreverse nextmenu))
6456 (prin1 nextmenu)
6457 (setq nextmenu (cons (format "%s...%s"
6458 (car (car nextmenu))
6459 (car (nth (1- cnt) nextmenu)))
6460 nextmenu))
6461 (setq menu (cons nextmenu menu)
6462 nextmenu nil
6463 cnt 0))
6464 (setq menu (nreverse menu))
6465 (setq menu (cons title menu))
6466 menu)))
6467
6468 (defvar idlwave-completion-setup-hook nil)
6469
6470 (defun idlwave-scroll-completions (&optional message)
6471 "Scroll the completion window on this frame."
6472 (let ((cwin (get-buffer-window "*Completions*" 'visible))
6473 (win (selected-window)))
6474 (unwind-protect
6475 (progn
6476 (select-window cwin)
6477 (condition-case nil
6478 (scroll-up)
6479 (error (if (and (listp last-command)
6480 (nth 2 last-command))
6481 (progn
6482 (select-window win)
6483 (eval idlwave-complete-after-success-form))
6484 (set-window-start cwin (point-min)))))
6485 (and message (message message)))
6486 (select-window win))))
6487
6488 (defun idlwave-display-completion-list (list &optional message beg complete)
6489 "Display the completions in LIST in the completions buffer and echo MESSAGE."
6490 (unless (and (get-buffer-window "*Completions*")
6491 (idlwave-local-value 'idlwave-completion-p "*Completions*"))
6492 (move-marker idlwave-completion-mark beg)
6493 (setq idlwave-before-completion-wconf (current-window-configuration)))
6494
6495 (if (featurep 'xemacs)
6496 (idlwave-display-completion-list-xemacs
6497 list)
6498 (idlwave-display-completion-list-emacs list))
6499
6500 ;; Store a special value in `this-command'. When `idlwave-complete'
6501 ;; finds this in `last-command', it will scroll the *Completions* buffer.
6502 (setq this-command (list 'idlwave-display-completion-list message complete))
6503
6504 ;; Mark the completions buffer as created by cib
6505 (idlwave-set-local 'idlwave-completion-p t "*Completions*")
6506
6507 ;; Fontify the classes
6508 (if (and idlwave-completion-fontify-classes
6509 (consp (car list)))
6510 (idlwave-completion-fontify-classes))
6511
6512 ;; Run the hook
6513 (run-hooks 'idlwave-completion-setup-hook)
6514
6515 ;; Display the message
6516 (message (or message "Making completion list...done")))
6517
6518 (defun idlwave-choose (function &rest args)
6519 "Call FUNCTION as a completion chooser and pass ARGS to it."
6520 (let ((completion-ignore-case t)) ; install correct value
6521 (apply function args))
6522 (if (and (eq major-mode 'idlwave-shell-mode)
6523 (boundp 'font-lock-mode)
6524 (not font-lock-mode))
6525 ;; For the shell, remove the fontification of the word before point
6526 (let ((beg (save-excursion
6527 (skip-chars-backward "a-zA-Z0-9_")
6528 (point))))
6529 (remove-text-properties beg (point) '(face nil))))
6530 (eval idlwave-complete-after-success-form-force))
6531
6532 (defun idlwave-keyboard-quit ()
6533 (interactive)
6534 (unwind-protect
6535 (if (eq (car-safe last-command) 'idlwave-display-completion-list)
6536 (idlwave-restore-wconf-after-completion))
6537 (keyboard-quit)))
6538
6539 (defun idlwave-restore-wconf-after-completion ()
6540 "Restore the old (before completion) window configuration."
6541 (and idlwave-completion-restore-window-configuration
6542 idlwave-before-completion-wconf
6543 (set-window-configuration idlwave-before-completion-wconf)))
6544
6545 (defun idlwave-one-key-select (sym prompt delay)
6546 "Make the user select an element from the alist in the variable SYM.
6547 The keys of the alist are expected to be strings. The function returns the
6548 car of the selected association.
6549 To do this, PROMPT is displayed and and the user must hit a letter key to
6550 select an entry. If the user does not reply within DELAY seconds, a help
6551 window with the options is displayed automatically.
6552 The key which is associated with each option is generated automatically.
6553 First, the strings are checked for preselected keys, like in \"[P]rint\".
6554 If these don't exist, a letter in the string is automatically selected."
6555 (let* ((alist (symbol-value sym))
6556 (temp-buffer-show-hook (if (fboundp 'fit-window-to-buffer)
6557 '(fit-window-to-buffer)))
6558 keys-alist char)
6559 ;; First check the cache
6560 (if (and (eq (symbol-value sym) (get sym :one-key-alist-last)))
6561 (setq keys-alist (get sym :one-key-alist-cache))
6562 ;; Need to make new list
6563 (setq keys-alist (idlwave-make-one-key-alist alist))
6564 (put sym :one-key-alist-cache keys-alist)
6565 (put sym :one-key-alist-last alist))
6566 ;; Display prompt and wait for quick reply
6567 (message "%s[%s]" prompt
6568 (mapconcat (lambda(x) (char-to-string (car x)))
6569 keys-alist ""))
6570 (if (sit-for delay)
6571 ;; No quick reply: Show help
6572 (save-window-excursion
6573 (with-output-to-temp-buffer "*Completions*"
6574 (mapcar (lambda(x)
6575 (princ (nth 1 x))
6576 (princ "\n"))
6577 keys-alist))
6578 (setq char (read-char)))
6579 (setq char (read-char)))
6580 (message nil)
6581 ;; Return the selected result
6582 (nth 2 (assoc char keys-alist))))
6583
6584 ;; Used for, e.g., electric debug super-examine.
6585 (defun idlwave-make-one-key-alist (alist)
6586 "Make an alist for single key selection."
6587 (let ((l alist) keys-alist name start char help
6588 (cnt 0)
6589 (case-fold-search nil))
6590 (while l
6591 (setq name (car (car l))
6592 l (cdr l))
6593 (catch 'exit
6594 ;; First check if the configuration predetermined a key
6595 (if (string-match "\\[\\(.\\)\\]" name)
6596 (progn
6597 (setq char (string-to-char (downcase (match-string 1 name)))
6598 help (format "%c: %s" char name)
6599 keys-alist (cons (list char help name) keys-alist))
6600 (throw 'exit t)))
6601 ;; Then check for capital letters
6602 (setq start 0)
6603 (while (string-match "[A-Z]" name start)
6604 (setq start (match-end 0)
6605 char (string-to-char (downcase (match-string 0 name))))
6606 (if (not (assoc char keys-alist))
6607 (progn
6608 (setq help (format "%c: %s" char
6609 (replace-match
6610 (concat "[" (match-string 0 name) "]")
6611 t t name))
6612 keys-alist (cons (list char help name) keys-alist))
6613 (throw 'exit t))))
6614 ;; Now check for lowercase letters
6615 (setq start 0)
6616 (while (string-match "[a-z]" name start)
6617 (setq start (match-end 0)
6618 char (string-to-char (match-string 0 name)))
6619 (if (not (assoc char keys-alist))
6620 (progn
6621 (setq help (format "%c: %s" char
6622 (replace-match
6623 (concat "[" (match-string 0 name) "]")
6624 t t name))
6625 keys-alist (cons (list char help name) keys-alist))
6626 (throw 'exit t))))
6627 ;; Bummer, nothing found! Use a stupid number
6628 (setq char (string-to-char (int-to-string (setq cnt (1+ cnt))))
6629 help (format "%c: %s" char name)
6630 keys-alist (cons (list char help name) keys-alist))))
6631 (nreverse keys-alist)))
6632
6633 (defun idlwave-set-local (var value &optional buffer)
6634 "Set the buffer-local value of VAR in BUFFER to VALUE."
6635 (save-excursion
6636 (set-buffer (or buffer (current-buffer)))
6637 (set (make-local-variable var) value)))
6638
6639 (defun idlwave-local-value (var &optional buffer)
6640 "Return the value of VAR in BUFFER, but only if VAR is local to BUFFER."
6641 (save-excursion
6642 (set-buffer (or buffer (current-buffer)))
6643 (and (local-variable-p var (current-buffer))
6644 (symbol-value var))))
6645
6646 ;; In XEmacs, we can use :activate-callback directly to advice the
6647 ;; choose functions. We use the private keymap only for the online
6648 ;; help feature.
6649
6650 (defvar idlwave-completion-map nil
6651 "Keymap for completion-list-mode with idlwave-complete.")
6652
6653 (defun idlwave-display-completion-list-xemacs (list &rest cl-args)
6654 (with-output-to-temp-buffer "*Completions*"
6655 (apply 'display-completion-list list
6656 ':activate-callback 'idlwave-default-choose-completion
6657 cl-args))
6658 (save-excursion
6659 (set-buffer "*Completions*")
6660 (use-local-map
6661 (or idlwave-completion-map
6662 (setq idlwave-completion-map
6663 (idlwave-make-modified-completion-map-xemacs
6664 (current-local-map)))))))
6665
6666 (defun idlwave-default-choose-completion (&rest args)
6667 "Execute `default-choose-completion' and then restore the win-conf."
6668 (apply 'idlwave-choose 'default-choose-completion args))
6669
6670 (defun idlwave-make-modified-completion-map-xemacs (old-map)
6671 "Replace `choose-completion' and `mouse-choose-completion' in OLD-MAP."
6672 (let ((new-map (copy-keymap old-map)))
6673 (define-key new-map [button3up] 'idlwave-mouse-completion-help)
6674 (define-key new-map [button3] (lambda ()
6675 (interactive)
6676 (setq this-command last-command)))
6677 new-map))
6678
6679 ;; In Emacs we also replace keybindings in the completion
6680 ;; map in order to install our wrappers.
6681
6682 (defun idlwave-display-completion-list-emacs (list)
6683 "Display completion list and install the choose wrappers."
6684 (with-output-to-temp-buffer "*Completions*"
6685 (display-completion-list list))
6686 (save-excursion
6687 (set-buffer "*Completions*")
6688 (use-local-map
6689 (or idlwave-completion-map
6690 (setq idlwave-completion-map
6691 (idlwave-make-modified-completion-map-emacs
6692 (current-local-map)))))))
6693
6694 (defun idlwave-make-modified-completion-map-emacs (old-map)
6695 "Replace `choose-completion' and `mouse-choose-completion' in OLD-MAP."
6696 (let ((new-map (copy-keymap old-map)))
6697 (substitute-key-definition
6698 'choose-completion 'idlwave-choose-completion new-map)
6699 (substitute-key-definition
6700 'mouse-choose-completion 'idlwave-mouse-choose-completion new-map)
6701 (define-key new-map [mouse-3] 'idlwave-mouse-completion-help)
6702 new-map))
6703
6704 (defun idlwave-choose-completion (&rest args)
6705 "Choose the completion that point is in or next to."
6706 (interactive)
6707 (apply 'idlwave-choose 'choose-completion args))
6708
6709 (defun idlwave-mouse-choose-completion (&rest args)
6710 "Click on an alternative in the `*Completions*' buffer to choose it."
6711 (interactive "e")
6712 (apply 'idlwave-choose 'mouse-choose-completion args))
6713
6714 ;;----------------------------------------------------------------------
6715 ;;----------------------------------------------------------------------
6716
6717 ;;; ------------------------------------------------------------------------
6718 ;;; Stucture parsing code, and code to manage class info
6719
6720 ;;
6721 ;; - Go again over the documentation how to write a completion
6722 ;; plugin. It is in self.el, but currently still very bad.
6723 ;; This could be in a separate file in the distribution, or
6724 ;; in an appendix for the manual.
6725
6726 (defvar idlwave-struct-skip
6727 "[ \t]*\\(\\$.*\n\\(^[ \t]*\\(\\$[ \t]*\\)?\\(;.*\\)?\n\\)*\\)?[ \t]*"
6728 "Regexp for skipping continued blank or comment-only lines in
6729 structures")
6730
6731 (defvar idlwave-struct-tag-regexp
6732 (concat "[{,]" ;leading comma/brace
6733 idlwave-struct-skip ; 4 groups
6734 "\\([a-zA-Z][a-zA-Z0-9_]*\\)" ;the tag itself, group 5
6735 "[ \t]*:") ; the final colon
6736 "Regexp for structure tags.")
6737
6738 (defun idlwave-struct-tags ()
6739 "Return a list of all tags in the structure defined at point.
6740 Point is expected just before the opening `{' of the struct definition."
6741 (save-excursion
6742 (let* ((borders (idlwave-struct-borders))
6743 (beg (car borders))
6744 (end (cdr borders))
6745 tags)
6746 (goto-char beg)
6747 (save-restriction
6748 (narrow-to-region beg end)
6749 (while (re-search-forward idlwave-struct-tag-regexp end t)
6750 ;; Check if we are still on the top level of the structure.
6751 (if (and (condition-case nil (progn (up-list -1) t) (error nil))
6752 (= (point) beg))
6753 (push (match-string-no-properties 5) tags))
6754 (goto-char (match-end 0))))
6755 (nreverse tags))))
6756
6757 (defun idlwave-find-struct-tag (tag)
6758 "Find a given TAG in the structure defined at point."
6759 (let* ((borders (idlwave-struct-borders))
6760 (beg (car borders))
6761 (end (cdr borders))
6762 (case-fold-search t))
6763 (re-search-forward (concat "\\(^[ \t]*\\|[,{][ \t]*\\)" tag "[ \t]*:")
6764 end t)))
6765
6766 (defun idlwave-struct-inherits ()
6767 "Return a list of all `inherits' names in the struct at point.
6768 Point is expected just before the opening `{' of the struct definition."
6769 (save-excursion
6770 (let* ((borders (idlwave-struct-borders))
6771 (beg (car borders))
6772 (end (cdr borders))
6773 (case-fold-search t)
6774 names)
6775 (goto-char beg)
6776 (save-restriction
6777 (narrow-to-region beg end)
6778 (while (re-search-forward
6779 (concat "[{,]" ;leading comma/brace
6780 idlwave-struct-skip ; 4 groups
6781 "inherits" ; The INHERITS tag
6782 idlwave-struct-skip ; 4 more
6783 "\\([a-zA-Z][a-zA-Z0-9_]*\\)") ; The super-group, #9
6784 end t)
6785 ;; Check if we are still on the top level of the structure.
6786 (if (and (condition-case nil (progn (up-list -1) t) (error nil))
6787 (= (point) beg))
6788 (push (match-string-no-properties 9) names))
6789 (goto-char (match-end 0))))
6790 (nreverse names))))
6791
6792 (defun idlwave-in-structure ()
6793 "Return t if point is inside an IDL structure definition."
6794 (let ((beg (point)))
6795 (save-excursion
6796 (if (not (or (idlwave-in-comment) (idlwave-in-quote)))
6797 (if (idlwave-find-structure-definition nil nil 'back)
6798 (let ((borders (idlwave-struct-borders)))
6799 (or (= (car borders) (cdr borders)) ;; struct not yet closed...
6800 (and (> beg (car borders)) (< beg (cdr borders))))))))))
6801
6802 (defun idlwave-struct-borders ()
6803 "Return the borders of the {...} after point as a cons cell."
6804 (let (beg)
6805 (save-excursion
6806 (skip-chars-forward "^{")
6807 (setq beg (point))
6808 (condition-case nil (forward-list 1)
6809 (error (goto-char beg)))
6810 (cons beg (point)))))
6811
6812 (defun idlwave-find-structure-definition (&optional var name bound)
6813 "Search forward for a structure definition. If VAR is non-nil,
6814 search for a structure assigned to variable VAR. If NAME is non-nil,
6815 search for a named structure NAME, if a string, or a generic named
6816 structure otherwise. If BOUND is an integer, limit the search. If
6817 BOUND is the symbol `all', we search first back and then forward
6818 through the entire file. If BOUND is the symbol `back' we search only
6819 backward."
6820 (let* ((ws "[ \t]*\\(\\$.*\n[ \t]*\\)*")
6821 (case-fold-search t)
6822 (lim (if (integerp bound) bound nil))
6823 (re (concat
6824 (if var
6825 (concat "\\<" (regexp-quote (downcase var)) "\\>" ws)
6826 "\\(\\)")
6827 "=" ws "\\({\\)"
6828 (if name
6829 (if (stringp name)
6830 (concat ws "\\(\\<" (downcase name) "\\)[^a-zA-Z0-9_$]")
6831 ;; Just a generic name
6832 (concat ws "\\<\\([a-zA-Z_0-9$]+\\)" ws ","))
6833 ""))))
6834 (if (or (and (or (eq bound 'all) (eq bound 'back))
6835 (re-search-backward re nil t))
6836 (and (not (eq bound 'back)) (re-search-forward re lim t)))
6837 (progn
6838 (goto-char (match-beginning 3))
6839 (match-string-no-properties 5)))))
6840
6841 (defvar idlwave-class-info nil)
6842 (defvar idlwave-system-class-info nil) ; Gathered from idlw-rinfo
6843 (defvar idlwave-class-reset nil) ; to reset buffer-local classes
6844
6845 (add-hook 'idlwave-update-rinfo-hook
6846 (lambda () (setq idlwave-class-reset t)))
6847 (add-hook 'idlwave-after-load-rinfo-hook
6848 (lambda () (setq idlwave-class-info nil)))
6849
6850 (defun idlwave-class-info (class)
6851 (let (list entry)
6852 (if idlwave-class-info
6853 (if idlwave-class-reset
6854 (setq
6855 idlwave-class-reset nil
6856 idlwave-class-info ; Remove any visited in a buffer
6857 (delq nil (mapcar
6858 (lambda (x)
6859 (let ((filebuf
6860 (idlwave-class-file-or-buffer
6861 (or (cdr (assq 'found-in x)) (car x)))))
6862 (if (cdr filebuf)
6863 nil
6864 x)))
6865 idlwave-class-info))))
6866 ;; Info is nil, put in the system stuff to start.
6867 (setq idlwave-class-info idlwave-system-class-info)
6868 (setq list idlwave-class-info)
6869 (while (setq entry (pop list))
6870 (idlwave-sintern-class-info entry)))
6871 (setq class (idlwave-sintern-class class))
6872 (or (assq class idlwave-class-info)
6873 (progn (idlwave-scan-class-info class)
6874 (assq class idlwave-class-info)))))
6875
6876 (defun idlwave-sintern-class-info (entry)
6877 "Sintern the class names in a class-info entry."
6878 (let ((taglist (assq 'tags entry))
6879 (inherits (assq 'inherits entry)))
6880 (setcar entry (idlwave-sintern-class (car entry) 'set))
6881 (if inherits
6882 (setcdr inherits (mapcar (lambda (x) (idlwave-sintern-class x 'set))
6883 (cdr inherits))))))
6884
6885 (defun idlwave-find-class-definition (class &optional all-hook alt-class)
6886 "Find class structure definition(s)
6887 If ALL-HOOK is set, find all named structure definitions in a given
6888 class__define routine, on which ALL-HOOK will be run. If ALT-CLASS is
6889 set, look for the name__define pro, and inside of it, for the ALT-CLASS
6890 class/struct definition"
6891 (let ((case-fold-search t) end-lim list name)
6892 (when (re-search-forward
6893 (concat "^[ \t]*pro[ \t]+" (downcase class) "__define" "\\>") nil t)
6894 (if all-hook
6895 (progn
6896 ;; For everything there
6897 (setq end-lim (save-excursion (idlwave-end-of-subprogram) (point)))
6898 (while (setq name
6899 (idlwave-find-structure-definition nil t end-lim))
6900 (funcall all-hook name)))
6901 (idlwave-find-structure-definition nil (or alt-class class))))))
6902
6903
6904 (defun idlwave-class-file-or-buffer (class)
6905 "Find buffer visiting CLASS definition"
6906 (let* ((pro (concat (downcase class) "__define"))
6907 (file (idlwave-routine-source-file
6908 (nth 3 (idlwave-rinfo-assoc pro 'pro nil
6909 (idlwave-routines))))))
6910 (cons file (if file (idlwave-get-buffer-visiting file)))))
6911
6912
6913 (defun idlwave-scan-class-info (class)
6914 "Scan all class and named structure info in the class__define pro"
6915 (let* ((idlwave-auto-routine-info-updates nil)
6916 (filebuf (idlwave-class-file-or-buffer class))
6917 (file (car filebuf))
6918 (buf (cdr filebuf))
6919 (class (idlwave-sintern-class class)))
6920 (if (or
6921 (not file)
6922 (and ;; neither a regular file nor a visited buffer
6923 (not buf)
6924 (not (file-regular-p file))))
6925 nil ; Cannot find the file/buffer to get any info
6926 (save-excursion
6927 (if buf (set-buffer buf)
6928 ;; Read the file in temporarily
6929 (set-buffer (get-buffer-create " *IDLWAVE-tmp*"))
6930 (erase-buffer)
6931 (unless (eq major-mode 'idlwave-mode)
6932 (idlwave-mode))
6933 (insert-file-contents file))
6934 (save-excursion
6935 (goto-char 1)
6936 (idlwave-find-class-definition class
6937 ;; Scan all of the structures found there
6938 (lambda (name)
6939 (let* ((this-class (idlwave-sintern-class name))
6940 (entry
6941 (list this-class
6942 (cons 'tags (idlwave-struct-tags))
6943 (cons 'inherits (idlwave-struct-inherits)))))
6944 (if (not (eq this-class class))
6945 (setq entry (nconc entry (list (cons 'found-in class)))))
6946 (idlwave-sintern-class-info entry)
6947 (push entry idlwave-class-info)))))))))
6948
6949 (defun idlwave-class-found-in (class)
6950 "Return the FOUND-IN property of the class."
6951 (cdr (assq 'found-in (idlwave-class-info class))))
6952 (defun idlwave-class-tags (class)
6953 "Return the native tags in CLASS."
6954 (cdr (assq 'tags (idlwave-class-info class))))
6955 (defun idlwave-class-inherits (class)
6956 "Return the direct superclasses of CLASS."
6957 (cdr (assq 'inherits (idlwave-class-info class))))
6958
6959
6960 (defun idlwave-all-class-tags (class)
6961 "Return a list of native and inherited tags in CLASS."
6962 (condition-case err
6963 (apply 'append (mapcar 'idlwave-class-tags
6964 (cons class (idlwave-all-class-inherits class))))
6965 (error
6966 (idlwave-class-tag-reset)
6967 (error "%s" (error-message-string err)))))
6968
6969
6970 (defun idlwave-all-class-inherits (class)
6971 "Return a list of all superclasses of CLASS (recursively expanded).
6972 The list is cached in `idlwave-class-info' for faster access."
6973 (cond
6974 ((not idlwave-support-inheritance) nil)
6975 ((eq class nil) nil)
6976 ((eq class t) nil)
6977 (t
6978 (let ((info (idlwave-class-info class))
6979 entry)
6980 (if (setq entry (assq 'all-inherits info))
6981 (cdr entry)
6982 ;; Save the depth of inheritance scan to check for circular references
6983 (let ((inherits (mapcar (lambda (x) (cons x 0))
6984 (idlwave-class-inherits class)))
6985 rtn all-inherits cl)
6986 (while inherits
6987 (setq cl (pop inherits)
6988 rtn (cons (car cl) rtn)
6989 inherits (append (mapcar (lambda (x)
6990 (cons x (1+ (cdr cl))))
6991 (idlwave-class-inherits (car cl)))
6992 inherits))
6993 (if (> (cdr cl) 999)
6994 (error
6995 "Class scan: inheritance depth exceeded. Circular inheritance?")
6996 ))
6997 (setq all-inherits (nreverse rtn))
6998 (nconc info (list (cons 'all-inherits all-inherits)))
6999 all-inherits))))))
7000
7001 (defun idlwave-entry-keywords (entry &optional record-link)
7002 "Return the flat entry keywords alist from routine-info entry.
7003 If RECORD-LINK is non-nil, the keyword text is copied and a text
7004 property indicating the link is added."
7005 (let (kwds)
7006 (mapcar
7007 (lambda (key-list)
7008 (let ((file (car key-list)))
7009 (mapcar (lambda (key-cons)
7010 (let ((key (car key-cons))
7011 (link (cdr key-cons)))
7012 (when (and record-link file)
7013 (setq key (copy-sequence key))
7014 (put-text-property
7015 0 (length key)
7016 'link
7017 (concat
7018 file
7019 (if link
7020 (concat idlwave-html-link-sep
7021 (number-to-string link))))
7022 key))
7023 (push (list key) kwds)))
7024 (cdr key-list))))
7025 (nthcdr 5 entry))
7026 (nreverse kwds)))
7027
7028 (defun idlwave-entry-find-keyword (entry keyword)
7029 "Find keyword KEYWORD in entry ENTRY, and return (with link) if set"
7030 (catch 'exit
7031 (mapc
7032 (lambda (key-list)
7033 (let ((file (car key-list))
7034 (kwd (assoc keyword (cdr key-list))))
7035 (when kwd
7036 (setq kwd (cons (car kwd)
7037 (if (and file (cdr kwd))
7038 (concat file
7039 idlwave-html-link-sep
7040 (number-to-string (cdr kwd)))
7041 (cdr kwd))))
7042 (throw 'exit kwd))))
7043 (nthcdr 5 entry))))
7044
7045 ;;==========================================================================
7046 ;;
7047 ;; Completing class structure tags. This is a completion plugin.
7048 ;; The necessary taglist is constructed dynamically
7049
7050 (defvar idlwave-current-tags-class nil)
7051 (defvar idlwave-current-class-tags nil)
7052 (defvar idlwave-current-native-class-tags nil)
7053 (defvar idlwave-sint-class-tags nil)
7054 (idlwave-new-sintern-type 'class-tag)
7055 (add-to-list 'idlwave-complete-special 'idlwave-complete-class-structure-tag)
7056 (add-hook 'idlwave-update-rinfo-hook 'idlwave-class-tag-reset)
7057
7058 (defun idlwave-complete-class-structure-tag ()
7059 "Complete a structure tag on a `self' argument in an object method."
7060 (interactive)
7061 (let ((pos (point))
7062 (case-fold-search t))
7063 (if (save-excursion
7064 ;; Check if the context is right
7065 (skip-chars-backward "a-zA-Z0-9._$")
7066 (and (< (point) (- pos 4))
7067 (looking-at "self\\.")))
7068 (let* ((class-selector (nth 2 (idlwave-current-routine)))
7069 (super-classes (idlwave-all-class-inherits class-selector)))
7070 ;; Check if we are in a class routine
7071 (unless class-selector
7072 (error "Not in a method procedure or function"))
7073 ;; Check if we need to update the "current" class
7074 (if (not (equal class-selector idlwave-current-tags-class))
7075 (idlwave-prepare-class-tag-completion class-selector))
7076 (setq idlwave-completion-help-info
7077 (list 'idlwave-complete-class-structure-tag-help
7078 (idlwave-sintern-routine
7079 (concat class-selector "__define"))
7080 nil))
7081 (let ((idlwave-cpl-bold idlwave-current-native-class-tags))
7082 (idlwave-complete-in-buffer
7083 'class-tag 'class-tag
7084 idlwave-current-class-tags nil
7085 (format "Select a tag of class %s" class-selector)
7086 "class tag"
7087 'idlwave-attach-class-tag-classes))
7088 t) ; return t to skip other completions
7089 nil)))
7090
7091 (defun idlwave-class-tag-reset ()
7092 (setq idlwave-current-tags-class nil))
7093
7094 (defun idlwave-prepare-class-tag-completion (class)
7095 "Find and parse the necessary class definitions for class structure tags."
7096 (setq idlwave-sint-class-tags nil)
7097 (setq idlwave-current-tags-class class)
7098 (setq idlwave-current-class-tags
7099 (mapcar (lambda (x)
7100 (list (idlwave-sintern-class-tag x 'set)))
7101 (idlwave-all-class-tags class)))
7102 (setq idlwave-current-native-class-tags
7103 (mapcar 'downcase (idlwave-class-tags class))))
7104
7105 ;===========================================================================
7106 ;;
7107 ;; Completing system variables and their structure fields
7108 ;; This is also a plugin.
7109
7110 (defvar idlwave-sint-sysvars nil)
7111 (defvar idlwave-sint-sysvartags nil)
7112 (idlwave-new-sintern-type 'sysvar)
7113 (idlwave-new-sintern-type 'sysvartag)
7114 (add-to-list 'idlwave-complete-special 'idlwave-complete-sysvar-or-tag)
7115 (add-hook 'idlwave-update-rinfo-hook 'idlwave-sysvars-reset)
7116 (add-hook 'idlwave-after-load-rinfo-hook 'idlwave-sintern-sysvar-alist)
7117
7118 (defvar idlwave-executive-commands-alist nil
7119 "Alist of system variables and their help files.")
7120
7121 (defvar idlwave-system-variables-alist nil
7122 "Alist of system variables and the associated structure tags.
7123 Gets set in `idlw-rinfo.el'.")
7124
7125 (defun idlwave-complete-sysvar-or-tag ()
7126 "Complete a system variable."
7127 (interactive)
7128 (let ((pos (point))
7129 (case-fold-search t))
7130 (cond ((save-excursion
7131 ;; Check if the context is right for system variable
7132 (skip-chars-backward "[a-zA-Z0-9_$]")
7133 (equal (char-before) ?!))
7134 (setq idlwave-completion-help-info '(idlwave-complete-sysvar-help))
7135 (idlwave-complete-in-buffer 'sysvar 'sysvar
7136 idlwave-system-variables-alist nil
7137 "Select a system variable"
7138 "system variable")
7139 t) ; return t to skip other completions
7140 ((save-excursion
7141 ;; Check if the context is right for sysvar tag
7142 (skip-chars-backward "a-zA-Z0-9_$.")
7143 (and (equal (char-before) ?!)
7144 (looking-at "\\([a-zA-Z][a-zA-Z0-9_$]*\\)\\.")
7145 (<= (match-end 0) pos)))
7146 ;; Complete a system variable tag
7147 (let* ((var (idlwave-sintern-sysvar (match-string 1)))
7148 (entry (assq var idlwave-system-variables-alist))
7149 (tags (cdr (assq 'tags entry))))
7150 (or entry (error "!%s is not a known system variable" var))
7151 (or tags (error "System variable !%s is not a structure" var))
7152 (setq idlwave-completion-help-info
7153 (list 'idlwave-complete-sysvar-tag-help var))
7154 (idlwave-complete-in-buffer 'sysvartag 'sysvartag
7155 tags nil
7156 "Select a system variable tag"
7157 "system variable tag")
7158 t)) ; return t to skip other completions
7159 (t nil))))
7160
7161 (defun idlwave-complete-sysvar-help (mode word)
7162 (let ((word (or (nth 1 idlwave-completion-help-info) word))
7163 (entry (assoc word idlwave-system-variables-alist)))
7164 (cond
7165 ((eq mode 'test)
7166 (and (stringp word) entry (nth 1 (assq 'link entry))))
7167 ((eq mode 'set)
7168 (if entry (setq link (nth 1 (assq 'link entry))))) ;; setting dynamic!!!
7169 (t (error "This should not happen")))))
7170
7171 (defun idlwave-complete-sysvar-tag-help (mode word)
7172 (let* ((var (nth 1 idlwave-completion-help-info))
7173 (entry (assoc var idlwave-system-variables-alist))
7174 (tags (cdr (assq 'tags entry)))
7175 (main (nth 1 (assq 'link entry)))
7176 target main-base)
7177 (cond
7178 ((eq mode 'test) ; we can at least link the main
7179 (and (stringp word) entry main))
7180 ((eq mode 'set)
7181 (if entry
7182 (setq link
7183 (if (setq target (cdr (assoc word tags)))
7184 (idlwave-substitute-link-target main target)
7185 main)))) ;; setting dynamic!!!
7186 (t (error "This should not happen")))))
7187
7188 (defun idlwave-substitute-link-target (link target)
7189 "Substitute the target anchor for the given link."
7190 (let (main-base)
7191 (setq main-base (if (string-match "#" link)
7192 (substring link 0 (match-beginning 0))
7193 link))
7194 (if target
7195 (concat main-base idlwave-html-link-sep (number-to-string target))
7196 link)))
7197
7198 ;; Fake help in the source buffer for class structure tags.
7199 ;; KWD AND NAME ARE GLOBAL-VARIABLES HERE.
7200 (defvar name)
7201 (defvar kwd)
7202 (defvar idlwave-help-do-class-struct-tag nil)
7203 (defun idlwave-complete-class-structure-tag-help (mode word)
7204 (cond
7205 ((eq mode 'test) ; nothing gets fontified for class tags
7206 nil)
7207 ((eq mode 'set)
7208 (let (class-with found-in)
7209 (when (setq class-with
7210 (idlwave-class-or-superclass-with-tag
7211 idlwave-current-tags-class
7212 word))
7213 (if (assq (idlwave-sintern-class class-with)
7214 idlwave-system-class-info)
7215 (error "No help available for system class tags."))
7216 (if (setq found-in (idlwave-class-found-in class-with))
7217 (setq name (cons (concat found-in "__define") class-with))
7218 (setq name (concat class-with "__define")))))
7219 (setq kwd word
7220 idlwave-help-do-class-struct-tag t))
7221 (t (error "This should not happen"))))
7222
7223 (defun idlwave-class-or-superclass-with-tag (class tag)
7224 "Find and return the CLASS or one of its superclass with the
7225 associated TAG, if any."
7226 (let ((sclasses (cons class (cdr (assq 'all-inherits
7227 (idlwave-class-info class)))))
7228 cl)
7229 (catch 'exit
7230 (while sclasses
7231 (setq cl (pop sclasses))
7232 (let ((tags (idlwave-class-tags cl)))
7233 (while tags
7234 (if (eq t (compare-strings tag 0 nil (car tags) 0 nil t))
7235 (throw 'exit cl))
7236 (setq tags (cdr tags))))))))
7237
7238
7239 (defun idlwave-sysvars-reset ()
7240 (if (and (fboundp 'idlwave-shell-is-running)
7241 (idlwave-shell-is-running)
7242 idlwave-idlwave_routine_info-compiled)
7243 (idlwave-shell-send-command "idlwave_get_sysvars"
7244 'idlwave-process-sysvars 'hide)))
7245
7246 (defun idlwave-process-sysvars ()
7247 (idlwave-shell-filter-sysvars)
7248 (setq idlwave-sint-sysvars nil
7249 idlwave-sint-sysvartags nil)
7250 (idlwave-sintern-sysvar-alist))
7251
7252 (defun idlwave-sintern-sysvar-alist ()
7253 (let ((list idlwave-system-variables-alist) entry tags)
7254 (while (setq entry (pop list))
7255 (setcar entry (idlwave-sintern-sysvar (car entry) 'set))
7256 (setq tags (assq 'tags entry))
7257 (if tags
7258 (setcdr tags
7259 (mapcar (lambda (x)
7260 (cons (idlwave-sintern-sysvartag (car x) 'set)
7261 (cdr x)))
7262 (cdr tags)))))))
7263
7264 (defvar idlwave-shell-command-output)
7265 (defun idlwave-shell-filter-sysvars ()
7266 "Get any new system variables and tags."
7267 (let ((text idlwave-shell-command-output)
7268 (start 0)
7269 (old idlwave-system-variables-alist)
7270 var tags type name class link old-entry)
7271 (setq idlwave-system-variables-alist nil)
7272 (while (string-match "^IDLWAVE-SYSVAR: !\\([a-zA-Z0-9_$]+\\)\\( \\(.*\\)\\)?"
7273 text start)
7274 (setq start (match-end 0)
7275 var (match-string 1 text)
7276 tags (if (match-end 3)
7277 (idlwave-split-string (match-string 3 text))))
7278 ;; Maintain old links, if present
7279 (setq old-entry (assq (idlwave-sintern-sysvar var) old))
7280 (setq link (assq 'link old-entry))
7281 (setq idlwave-system-variables-alist
7282 (cons (list var
7283 (cons
7284 'tags
7285 (mapcar (lambda (x)
7286 (cons x
7287 (cdr (assq
7288 (idlwave-sintern-sysvartag x)
7289 (cdr (assq 'tags old-entry))))))
7290 tags)) link)
7291 idlwave-system-variables-alist)))
7292 ;; Keep the old value if query was not successful
7293 (setq idlwave-system-variables-alist
7294 (or idlwave-system-variables-alist old))))
7295
7296 (defun idlwave-completion-fontify-classes ()
7297 "Goto the *Completions* buffer and fontify the class info."
7298 (when (featurep 'font-lock)
7299 (save-excursion
7300 (set-buffer "*Completions*")
7301 (save-excursion
7302 (goto-char (point-min))
7303 (let ((buffer-read-only nil))
7304 (while (re-search-forward "\\.*<[^>]+>" nil t)
7305 (put-text-property (match-beginning 0) (match-end 0)
7306 'face 'font-lock-string-face)))))))
7307
7308 (defun idlwave-uniquify (list)
7309 (let ((ht (make-hash-table :size (length list) :test 'equal)))
7310 (delq nil
7311 (mapcar (lambda (x)
7312 (unless (gethash x ht)
7313 (puthash x t ht)
7314 x))
7315 list))))
7316
7317 (defun idlwave-after-successful-completion (type slash &optional verify)
7318 "Add `=' or `(' after successful completion of keyword and function.
7319 Restore the pre-completion window configuration if possible."
7320 (cond
7321 ((eq type 'procedure)
7322 nil)
7323 ((eq type 'function)
7324 (cond
7325 ((equal idlwave-function-completion-adds-paren nil) nil)
7326 ((or (equal idlwave-function-completion-adds-paren t)
7327 (equal idlwave-function-completion-adds-paren 1))
7328 (insert "("))
7329 ((equal idlwave-function-completion-adds-paren 2)
7330 (insert "()")
7331 (backward-char 1))
7332 (t nil)))
7333 ((eq type 'keyword)
7334 (if (and idlwave-keyword-completion-adds-equal
7335 (not slash))
7336 (progn (insert "=") t)
7337 nil)))
7338
7339 ;; Restore the pre-completion window configuration if this is safe.
7340
7341 (if (or (eq verify 'force) ; force
7342 (and
7343 (get-buffer-window "*Completions*") ; visible
7344 (idlwave-local-value 'idlwave-completion-p
7345 "*Completions*") ; cib-buffer
7346 (eq (marker-buffer idlwave-completion-mark)
7347 (current-buffer)) ; buffer OK
7348 (equal (marker-position idlwave-completion-mark)
7349 verify))) ; pos OK
7350 (idlwave-restore-wconf-after-completion))
7351 (move-marker idlwave-completion-mark nil)
7352 (setq idlwave-before-completion-wconf nil))
7353
7354 (defun idlwave-mouse-context-help (ev &optional arg)
7355 "Call `idlwave-context-help' on the clicked location."
7356 (interactive "eP")
7357 (mouse-set-point ev)
7358 (idlwave-context-help arg))
7359
7360 (defvar idlwave-last-context-help-pos nil)
7361 (defun idlwave-context-help (&optional arg)
7362 "Display IDL Online Help on context.
7363 If point is on a keyword, help for that keyword will be shown. If
7364 point is on a routine name or in the argument list of a routine, help
7365 for that routine will be displayed. Works for system routines and
7366 keywords, it pulls up text help. For other routies and keywords,
7367 visits the source file, finding help in the header (if
7368 `idlwave-help-source-try-header' is non-nil) or the routine definition
7369 itself."
7370 (interactive "P")
7371 (idlwave-do-context-help arg))
7372
7373 (defun idlwave-mouse-completion-help (ev)
7374 "Display online help about the completion at point."
7375 (interactive "eP")
7376 ;; Restore last-command for next command, to make
7377 ;; scrolling/cancelling of completions work.
7378 (setq this-command last-command)
7379 (idlwave-do-mouse-completion-help ev))
7380
7381 (defun idlwave-routine-info (&optional arg external)
7382 "Display a routines calling sequence and list of keywords. When
7383 point is on the name a function or procedure, or in the argument list
7384 of a function or procedure, this command displays a help buffer with
7385 the information. When called with prefix arg, enforce class query.
7386
7387 When point is on an object operator `->', display the class stored in
7388 this arrow, if any (see `idlwave-store-inquired-class'). With a
7389 prefix arg, the class property is cleared out."
7390
7391 (interactive "P")
7392 (idlwave-routines)
7393 (if (string-match "->" (buffer-substring
7394 (max (point-min) (1- (point)))
7395 (min (+ 2 (point)) (point-max))))
7396 ;; Cursor is on an arrow
7397 (if (get-text-property (point) 'idlwave-class)
7398 ;; arrow has class property
7399 (if arg
7400 ;; Remove property
7401 (save-excursion
7402 (backward-char 1)
7403 (when (looking-at ".?\\(->\\)")
7404 (remove-text-properties (match-beginning 1) (match-end 1)
7405 '(idlwave-class nil face nil))
7406 (message "Class property removed from arrow")))
7407 ;; Echo class property
7408 (message "Arrow has text property identifying object to be class %s"
7409 (get-text-property (point) 'idlwave-class)))
7410 ;; No property found
7411 (message "Arrow has no class text property"))
7412
7413 ;; Not on an arrow...
7414 (let* ((idlwave-query-class nil)
7415 (idlwave-force-class-query (equal arg '(4)))
7416 (module (idlwave-what-module)))
7417 (if (car module)
7418 (apply 'idlwave-display-calling-sequence
7419 (idlwave-fix-module-if-obj_new module))
7420 (error "Don't know which calling sequence to show")))))
7421
7422 (defun idlwave-resolve (&optional arg)
7423 "Call RESOLVE_ROUTINE on the module name at point.
7424 Like `idlwave-routine-info', this looks for a routine call at point.
7425 After confirmation in the minibuffer, it will use the shell to issue
7426 a RESOLVE call for this routine, to attempt to make it defined and its
7427 routine info available for IDLWAVE. If the routine is a method call,
7428 both `class__method' and `class__define' will be tried.
7429 With ARG, enforce query for the class of object methods."
7430 (interactive "P")
7431 (let* ((idlwave-query-class nil)
7432 (idlwave-force-class-query (equal arg '(4)))
7433 (module (idlwave-what-module))
7434 (name (idlwave-make-full-name (nth 2 module) (car module)))
7435 (type (if (eq (nth 1 module) 'pro) "pro" "function"))
7436 (resolve (read-string "Resolve: " (format "%s %s" type name)))
7437 (kwd "")
7438 class)
7439 (if (string-match "\\(pro\\|function\\)[ \t]+\\(\\(.*\\)::\\)?\\(.*\\)"
7440 resolve)
7441 (setq type (match-string 1 resolve)
7442 class (if (match-beginning 2)
7443 (match-string 3 resolve)
7444 nil)
7445 name (match-string 4 resolve)))
7446 (if (string= (downcase type) "function")
7447 (setq kwd ",/is_function"))
7448
7449 (cond
7450 ((null class)
7451 (idlwave-shell-send-command
7452 (format "resolve_routine,'%s'%s" (downcase name) kwd)
7453 'idlwave-update-routine-info
7454 nil t))
7455 (t
7456 (idlwave-shell-send-command
7457 (format "resolve_routine,'%s__define'%s" (downcase class) kwd)
7458 (list 'idlwave-shell-send-command
7459 (format "resolve_routine,'%s__%s'%s"
7460 (downcase class) (downcase name) kwd)
7461 '(idlwave-update-routine-info)
7462 nil t))))))
7463
7464 (defun idlwave-find-module (&optional arg)
7465 "Find the source code of an IDL module.
7466 Works for modules for which IDLWAVE has routine info available. The
7467 function offers as default the module name `idlwave-routine-info'
7468 would use. With ARG limit to this buffer. With two prefix ARG's
7469 force class query for object methods."
7470 (interactive "P")
7471 (let* ((idlwave-query-class nil)
7472 (idlwave-force-class-query (equal arg '(16)))
7473 (this-buffer (equal arg '(4)))
7474 (module (idlwave-fix-module-if-obj_new (idlwave-what-module)))
7475 (default (if module
7476 (concat (idlwave-make-full-name
7477 (nth 2 module) (car module))
7478 (if (eq (nth 1 module) 'pro) "<p>" "<f>"))
7479 "none"))
7480 (list
7481 (idlwave-uniquify
7482 (delq nil
7483 (mapcar (lambda (x)
7484 (if (eq 'system (car-safe (nth 3 x)))
7485 ;; Take out system routines with no source.
7486 nil
7487 (list
7488 (concat (idlwave-make-full-name
7489 (nth 2 x) (car x))
7490 (if (eq (nth 1 x) 'pro) "<p>" "<f>")))))
7491 (if this-buffer
7492 (idlwave-save-buffer-update)
7493 (idlwave-routines))))))
7494 (name (idlwave-completing-read
7495 (if (or (not this-buffer)
7496 (assoc default list))
7497 (format "Module (Default %s): " default)
7498 (format "Module in this file: "))
7499 list))
7500 type class)
7501 (if (string-match "\\`\\s-*\\'" name)
7502 ;; Nothing, use the default.
7503 (setq name default))
7504 (if (string-match "<[fp]>" name)
7505 (setq type (substring name -2 -1)
7506 name (substring name 0 -3)))
7507 (if (string-match "\\(.*\\)::\\(.*\\)" name)
7508 (setq class (match-string 1 name)
7509 name (match-string 2 name)))
7510 (setq name (idlwave-sintern-routine-or-method name class)
7511 class (idlwave-sintern-class class)
7512 type (cond ((equal type "f") 'fun)
7513 ((equal type "p") 'pro)
7514 (t t)))
7515 (idlwave-do-find-module name type class nil this-buffer)))
7516
7517 (defun idlwave-do-find-module (name type class
7518 &optional force-source this-buffer)
7519 (let ((name1 (idlwave-make-full-name class name))
7520 source buf1 entry
7521 (buf (current-buffer))
7522 (pos (point))
7523 file name2)
7524 (setq entry (idlwave-best-rinfo-assq name type class (idlwave-routines)
7525 'WITH-FILE)
7526 source (or force-source (nth 3 entry))
7527 name2 (if (nth 2 entry)
7528 (idlwave-make-full-name (nth 2 entry) name)
7529 name1))
7530 (if source
7531 (setq file (idlwave-routine-source-file source)))
7532 (unless file ; Try to find it on the path.
7533 (setq file
7534 (idlwave-expand-lib-file-name
7535 (if class
7536 (format "%s__define.pro" (downcase class))
7537 (format "%s.pro" (downcase name))))))
7538 (cond
7539 ((or (null name) (equal name ""))
7540 (error "Abort"))
7541 ((eq (car source) 'system)
7542 (error "Source code for system routine %s is not available"
7543 name2))
7544 ((or (not file) (not (file-regular-p file)))
7545 (error "Source code for routine %s is not available"
7546 name2))
7547 (t
7548 (when (not this-buffer)
7549 (setq buf1
7550 (idlwave-find-file-noselect file 'find))
7551 (pop-to-buffer buf1 t))
7552 (goto-char (point-max))
7553 (let ((case-fold-search t))
7554 (if (re-search-backward
7555 (concat "^[ \t]*\\<"
7556 (cond ((eq type 'fun) "function")
7557 ((eq type 'pro) "pro")
7558 (t "\\(pro\\|function\\)"))
7559 "\\>[ \t]+"
7560 (regexp-quote (downcase name2))
7561 "[^a-zA-Z0-9_$]")
7562 nil t)
7563 (goto-char (match-beginning 0))
7564 (pop-to-buffer buf)
7565 (goto-char pos)
7566 (error "Could not find routine %s" name2)))))))
7567
7568 (defun idlwave-what-module ()
7569 "Return a default module for stuff near point.
7570 Used by `idlwave-routine-info' and `idlwave-find-module'."
7571 (idlwave-routines)
7572 (if (let ((case-fold-search t))
7573 (save-excursion
7574 (idlwave-beginning-of-statement)
7575 (looking-at "[ \t]*\\(pro\\|function\\)[ \t]+\\(\\([a-zA-Z0-9_$]+\\)::\\)?\\([a-zA-Z0-9$_]+\\)\\([, \t\n]\\|$\\)")))
7576 ;; This is a function or procedure definition statement
7577 ;; We return the defined routine as module.
7578 (list
7579 (idlwave-sintern-routine-or-method (match-string-no-properties 4)
7580 (match-string-no-properties 2))
7581 (if (equal (downcase (match-string 1)) "pro") 'pro 'fun)
7582 (idlwave-sintern-class (match-string 3)))
7583
7584 ;; Not a definition statement - analyze precise position.
7585 (let* ((where (idlwave-where))
7586 (cw (nth 2 where))
7587 (pro (car (nth 0 where)))
7588 (func (car (nth 1 where)))
7589 (this-word (idlwave-this-word "a-zA-Z0-9$_"))
7590 (next-char (save-excursion (skip-chars-forward "a-zA-Z0-9$_")
7591 (following-char)))
7592 )
7593 (cond
7594 ((and (eq cw 'procedure)
7595 (not (equal this-word "")))
7596 (setq this-word (idlwave-sintern-routine-or-method
7597 this-word (nth 2 (nth 3 where))))
7598 (list this-word 'pro
7599 (idlwave-determine-class
7600 (cons this-word (cdr (nth 3 where)))
7601 'pro)))
7602 ((and (eq cw 'function)
7603 (not (equal this-word ""))
7604 (or (eq next-char ?\() ; exclude arrays, vars.
7605 (looking-at "[a-zA-Z0-9_]*[ \t]*(")))
7606 (setq this-word (idlwave-sintern-routine-or-method
7607 this-word (nth 2 (nth 3 where))))
7608 (list this-word 'fun
7609 (idlwave-determine-class
7610 (cons this-word (cdr (nth 3 where)))
7611 'fun)))
7612 ((and (memq cw '(function-keyword procedure-keyword))
7613 (not (equal this-word ""))
7614 (eq next-char ?\()) ; A function!
7615 (setq this-word (idlwave-sintern-routine this-word))
7616 (list this-word 'fun nil))
7617 (func
7618 (list func 'fun (idlwave-determine-class (nth 1 where) 'fun)))
7619 (pro
7620 (list pro 'pro (idlwave-determine-class (nth 0 where) 'pro)))
7621 (t nil)))))
7622
7623 (defun idlwave-what-module-find-class ()
7624 "Call idlwave-what-module and find the inherited class if necessary."
7625 (let* ((module (idlwave-what-module))
7626 (class (nth 2 module))
7627 classes)
7628 (if (and (= (length module) 3)
7629 (stringp class))
7630 (list (car module)
7631 (nth 1 module)
7632 (apply 'idlwave-find-inherited-class module))
7633 module)))
7634
7635 (defun idlwave-find-inherited-class (name type class)
7636 "Find the class which defines TYPE NAME and is CLASS or inherited by CLASS."
7637 (let ((entry (idlwave-best-rinfo-assoc name type class (idlwave-routines))))
7638 (if entry
7639 (nth 2 entry)
7640 class)))
7641
7642 (defun idlwave-fix-module-if-obj_new (module)
7643 "Check if MODULE points to obj_new.
7644 If yes, and if the cursor is in the keyword region, change to the
7645 appropriate Init method."
7646 (let* ((name (car module))
7647 (pos (point))
7648 (case-fold-search t)
7649 string)
7650 (if (and (stringp name)
7651 (equal (downcase name) "obj_new")
7652 (save-excursion
7653 (idlwave-beginning-of-statement)
7654 (setq string (buffer-substring (point) pos))
7655 (string-match "obj_new([^'\"]*['\"]\\([a-zA-Z0-9_]+\\)"
7656 string)))
7657 (let ((name "Init")
7658 (class (match-string 1 string)))
7659 (setq module (list (idlwave-sintern-method "Init")
7660 'fun
7661 (idlwave-sintern-class class)))))
7662 module))
7663
7664 (defun idlwave-fix-keywords (name type class keywords &optional super-classes)
7665 "Update a list of keywords.
7666 Translate OBJ_NEW, adding all super-class keywords, or all keywords
7667 from all classes if class equals t."
7668 (let ((case-fold-search t))
7669
7670 ;; If this is the OBJ_NEW function, try to figure out the class and use
7671 ;; the keywords from the corresponding INIT method.
7672 (if (and (equal (upcase name) "OBJ_NEW")
7673 (or (eq major-mode 'idlwave-mode)
7674 (eq major-mode 'idlwave-shell-mode)))
7675 (let* ((bos (save-excursion (idlwave-beginning-of-statement) (point)))
7676 (string (buffer-substring bos (point)))
7677 (case-fold-search t)
7678 class)
7679 (and (string-match "obj_new([^'\"]*['\"]\\([a-zA-Z0-9_]+\\)"
7680 string)
7681 (setq class (idlwave-sintern-class (match-string 1 string)))
7682 (setq idlwave-current-obj_new-class class)
7683 (setq keywords
7684 (append keywords
7685 (idlwave-entry-keywords
7686 (idlwave-rinfo-assq
7687 (idlwave-sintern-method "INIT")
7688 'fun
7689 class
7690 (idlwave-routines)) 'do-link))))))
7691
7692 ;; If the class is `t', combine all keywords of all methods NAME
7693 (when (eq class t)
7694 (mapc (lambda (entry)
7695 (and
7696 (nth 2 entry) ; non-nil class
7697 (eq (nth 1 entry) type) ; correct type
7698 (setq keywords
7699 (append keywords
7700 (idlwave-entry-keywords entry 'do-link)))))
7701 (idlwave-all-assq name (idlwave-routines)))
7702 (setq keywords (idlwave-uniquify keywords)))
7703
7704 ;; If we have inheritance, add all keywords from superclasses, if
7705 ;; the user indicated that method in `idlwave-keyword-class-inheritance'
7706 (when (and
7707 super-classes
7708 idlwave-keyword-class-inheritance
7709 (stringp class)
7710 (or (assq (idlwave-sintern-keyword "_extra") keywords)
7711 (assq (idlwave-sintern-keyword "_ref_extra") keywords))
7712 ;; Check if one of the keyword-class regexps matches the name
7713 (let ((regexps idlwave-keyword-class-inheritance) re)
7714 (catch 'exit
7715 (while (setq re (pop regexps))
7716 (if (string-match re name) (throw 'exit t))))))
7717
7718 (loop for entry in (idlwave-routines) do
7719 (and (nth 2 entry) ; non-nil class
7720 (memq (nth 2 entry) super-classes) ; an inherited class
7721 (eq (nth 1 entry) type) ; correct type
7722 (eq (car entry) name) ; correct name
7723 (mapcar (lambda (k) (add-to-list 'keywords k))
7724 (idlwave-entry-keywords entry 'do-link))))
7725 (setq keywords (idlwave-uniquify keywords)))
7726
7727 ;; Return the final list
7728 keywords))
7729
7730 (defun idlwave-expand-keyword (keyword module)
7731 "Expand KEYWORD to one of the legal keyword parameters of MODULE.
7732 KEYWORD may be an exact match or an abbreviation of a keyword.
7733 If the match is exact, KEYWORD itself is returned, even if there may be other
7734 keywords of which KEYWORD is an abbreviation. This is necessary because some
7735 system routines have keywords which are prefixes of other keywords.
7736 If KEYWORD is an abbreviation of several keywords, a list of all possible
7737 completions is returned.
7738 If the abbreviation was unique, the correct keyword is returned.
7739 If it cannot be a keyword, the function return nil.
7740 If we do not know about MODULE, just return KEYWORD literally."
7741 (let* ((name (car module))
7742 (type (nth 1 module))
7743 (class (nth 2 module))
7744 (kwd (idlwave-sintern-keyword keyword))
7745 (entry (idlwave-best-rinfo-assoc name type class (idlwave-routines)))
7746 (kwd-alist (idlwave-entry-keywords entry))
7747 (extra (or (assq (idlwave-sintern-keyword "_EXTRA") kwd-alist)
7748 (assq (idlwave-sintern-keyword "_REF_EXTRA") kwd-alist)))
7749 (completion-ignore-case t)
7750 candidates)
7751 (cond ((assq kwd kwd-alist)
7752 kwd)
7753 ((setq candidates (all-completions kwd kwd-alist))
7754 (if (= (length candidates) 1)
7755 (car candidates)
7756 candidates))
7757 ((and entry extra)
7758 ;; Inheritance may cause this keyword to be correct
7759 keyword)
7760 (entry
7761 ;; We do know the function, which does not have the keyword.
7762 nil)
7763 (t
7764 ;; We do not know the function, so this just might be a correct
7765 ;; keyword - return it as it is.
7766 keyword))))
7767
7768 (defvar idlwave-rinfo-mouse-map (make-sparse-keymap))
7769 (defvar idlwave-rinfo-map (make-sparse-keymap))
7770 (define-key idlwave-rinfo-mouse-map
7771 (if (featurep 'xemacs) [button2] [mouse-2])
7772 'idlwave-mouse-active-rinfo)
7773 (define-key idlwave-rinfo-mouse-map
7774 (if (featurep 'xemacs) [(shift button2)] [(shift mouse-2)])
7775 'idlwave-mouse-active-rinfo-shift)
7776 (define-key idlwave-rinfo-mouse-map
7777 (if (featurep 'xemacs) [button3] [mouse-3])
7778 'idlwave-mouse-active-rinfo-right)
7779 (define-key idlwave-rinfo-mouse-map " " 'idlwave-active-rinfo-space)
7780 (define-key idlwave-rinfo-map "q" 'idlwave-quit-help)
7781 (define-key idlwave-rinfo-mouse-map "q" 'idlwave-quit-help)
7782 (defvar idlwave-popup-source nil)
7783 (defvar idlwave-rinfo-marker (make-marker))
7784
7785 (defun idlwave-quit-help ()
7786 (interactive)
7787 (let ((ri-window (get-buffer-window "*Help*"))
7788 (olh-window (get-buffer-window "*IDLWAVE Help*")))
7789 (when (and olh-window
7790 (fboundp 'idlwave-help-quit))
7791 (select-window olh-window)
7792 (idlwave-help-quit))
7793 (when (window-live-p ri-window)
7794 (delete-window ri-window))))
7795
7796 (defun idlwave-display-calling-sequence (name type class
7797 &optional initial-class)
7798 ;; Display the calling sequence of module NAME, type TYPE in class CLASS.
7799 (let* ((initial-class (or initial-class class))
7800 (entry (or (idlwave-best-rinfo-assq name type class
7801 (idlwave-routines))
7802 (idlwave-rinfo-assq name type class
7803 idlwave-unresolved-routines)))
7804 (name (or (car entry) name))
7805 (class (or (nth 2 entry) class))
7806 (superclasses (idlwave-all-class-inherits initial-class))
7807 (twins (idlwave-routine-twins entry))
7808 (dtwins (idlwave-study-twins twins))
7809 (all dtwins)
7810 (system (eq (car (nth 3 entry)) 'system))
7811 (calling-seq (nth 4 entry))
7812 (keywords (idlwave-entry-keywords entry 'do-link))
7813 (html-file (car (nth 5 entry)))
7814 (help-echo-kwd
7815 "Button2: Insert KEYWORD (SHIFT=`/KEYWORD') | Button3: Online Help ")
7816 (help-echo-use
7817 "Button2/3: Online Help")
7818 (help-echo-src
7819 "Button2: Jump to source and back | Button3: Source in Help window.")
7820 (help-echo-class
7821 "Button2: Display info about same method in superclass")
7822 (col 0)
7823 (data (list name type class (current-buffer) nil initial-class))
7824 (km-prop (if (featurep 'xemacs) 'keymap 'local-map))
7825 (face 'idlwave-help-link-face)
7826 beg props win cnt total)
7827 ;; Fix keywords, but don't add chained super-classes, since these
7828 ;; are shown separately for that super-class
7829 (setq keywords (idlwave-fix-keywords name type class keywords))
7830 (cond
7831 ((null entry)
7832 (error "No %s %s known %s" type name
7833 (if initial-class (concat "in class " initial-class) "")))
7834 ((or (null name) (equal name ""))
7835 (error "No function or procedure call at point"))
7836 ((null calling-seq)
7837 (error "Calling sequence of %s %s not available" type name))
7838 (t
7839 (save-excursion
7840 (move-marker idlwave-rinfo-marker (point))
7841 (set-buffer (get-buffer-create "*Help*"))
7842 (use-local-map idlwave-rinfo-map)
7843 (setq buffer-read-only nil)
7844 (erase-buffer)
7845 (set (make-local-variable 'idlwave-popup-source) nil)
7846 (set (make-local-variable 'idlwave-current-obj_new-class)
7847 idlwave-current-obj_new-class)
7848 (when superclasses
7849 (setq props (list 'mouse-face 'highlight
7850 km-prop idlwave-rinfo-mouse-map
7851 'help-echo help-echo-class
7852 'data (cons 'class data)))
7853 (let ((classes (cons initial-class superclasses)) c)
7854 (insert "Classes: ")
7855 (while (setq c (pop classes))
7856 (insert " ")
7857 (setq beg (point))
7858 (insert c)
7859 (if (equal (downcase c) (downcase class))
7860 (add-text-properties beg (point) (list 'face 'bold))
7861 ;; If Method exists in a different class link it
7862 (if (idlwave-rinfo-assq name type c (idlwave-routines))
7863 (add-text-properties beg (point) props))))
7864 (insert "\n")))
7865 (setq props (list 'mouse-face 'highlight
7866 km-prop idlwave-rinfo-mouse-map
7867 'help-echo help-echo-use
7868 'data (cons 'usage data)))
7869 (if html-file (setq props (append (list 'face face 'link html-file)
7870 props)))
7871 (insert "Usage: ")
7872 (setq beg (point))
7873 (insert (if class
7874 (format calling-seq class name class name class name)
7875 (format calling-seq name name name name))
7876 "\n")
7877 (add-text-properties beg (point) props)
7878
7879 (insert "Keywords:")
7880 (if (null keywords)
7881 (insert " No keywords accepted.")
7882 (setq col 9)
7883 (mapcar
7884 (lambda (x)
7885 (if (>= (+ col 1 (length (car x)))
7886 (window-width))
7887 (progn
7888 (insert "\n ")
7889 (setq col 9)))
7890 (insert " ")
7891 (setq beg (point)
7892 ;; Relevant keywords already have link property attached
7893 props (list 'mouse-face 'highlight
7894 km-prop idlwave-rinfo-mouse-map
7895 'data (cons 'keyword data)
7896 'help-echo help-echo-kwd
7897 'keyword (car x)))
7898 (if system (setq props (append (list 'face face) props)))
7899 (insert (car x))
7900 (add-text-properties beg (point) props)
7901 (setq col (+ col 1 (length (car x)))))
7902 keywords))
7903
7904 (setq cnt 1 total (length all))
7905 ;; Here entry is (key file (list of type-conses))
7906 (while (setq entry (pop all))
7907 (setq props (list 'mouse-face 'highlight
7908 km-prop idlwave-rinfo-mouse-map
7909 'help-echo help-echo-src
7910 'source (list (car (car (nth 2 entry))) ;type
7911 (nth 1 entry)
7912 nil
7913 (cdr (car (nth 2 entry))))
7914 'data (cons 'source data)))
7915 (idlwave-insert-source-location
7916 (format "\n%-8s %s"
7917 (if (equal cnt 1)
7918 (if (> total 1) "Sources:" "Source:")
7919 "")
7920 (if (> total 1) "- " ""))
7921 entry props)
7922 (incf cnt)
7923 (when (and all (> cnt idlwave-rinfo-max-source-lines))
7924 ;; No more source lines, please
7925 (insert (format
7926 "\n Source information truncated to %d entries."
7927 idlwave-rinfo-max-source-lines))
7928 (setq all nil)))
7929 (beginning-of-buffer)
7930 (setq buffer-read-only t))
7931 (display-buffer "*Help*")
7932 (if (and (setq win (get-buffer-window "*Help*"))
7933 idlwave-resize-routine-help-window)
7934 (progn
7935 (let ((ww (selected-window)))
7936 (unwind-protect
7937 (progn
7938 (select-window win)
7939 (enlarge-window (- (/ (frame-height) 2)
7940 (window-height)))
7941 (shrink-window-if-larger-than-buffer))
7942 (select-window ww)))))))))
7943
7944 (defun idlwave-insert-source-location (prefix entry &optional file-props)
7945 "Insert a source location into the routine info buffer.
7946 Start line with PREFIX. If a file name is inserted, add FILE-PROPS to
7947 it."
7948 (let* ((key (car entry))
7949 (file (nth 1 entry))
7950 (types (nth 2 entry))
7951 (shell-flag (assq 'compiled types))
7952 (buffer-flag (assq 'buffer types))
7953 (user-flag (assq 'user types))
7954 (lib-flag (assq 'lib types))
7955 (ndupl (or (and buffer-flag (idlwave-count-memq 'buffer types))
7956 (and user-flag (idlwave-count-memq 'user types))
7957 (and lib-flag (idlwave-count-memq 'lib types))
7958 1))
7959 (doflags t)
7960 beg special)
7961
7962 (insert prefix)
7963
7964 (cond
7965 ((eq key 'system)
7966 (setq doflags nil)
7967 (insert "System "))
7968
7969 ((eq key 'builtin)
7970 (setq doflags nil)
7971 (insert "Builtin "))
7972
7973 ((and (not file) shell-flag)
7974 (insert "Unresolved"))
7975
7976 ((null file)
7977 (insert "ERROR"))
7978
7979 ((idlwave-syslib-p file)
7980 (if (string-match "obsolete" (file-name-directory file))
7981 (insert "Obsolete ")
7982 (insert "SystemLib ")))
7983
7984 ;; New special syntax: taken directly from routine-info for
7985 ;; library catalog routines
7986 ((setq special (or (cdr lib-flag) (cdr user-flag)))
7987 (insert (format "%-10s" special)))
7988
7989 ;; Old special syntax: a matching regexp
7990 ((setq special (idlwave-special-lib-test file))
7991 (insert (format "%-10s" special)))
7992
7993 ;; Catch-all with file
7994 ((idlwave-lib-p file) (insert "Library "))
7995
7996 ;; Sanity catch all
7997 (t (insert "Other ")))
7998
7999 (when doflags
8000 (insert (concat
8001 " ["
8002 (if lib-flag "L" "-")
8003 (if user-flag "C" "-")
8004 (if shell-flag "S" "-")
8005 (if buffer-flag "B" "-")
8006 "] ")))
8007 (when (> ndupl 1)
8008 (setq beg (point))
8009 (insert (format "(%dx) " ndupl))
8010 (add-text-properties beg (point) (list 'face 'bold)))
8011 (when (and file (not (equal file "")))
8012 (setq beg (point))
8013 (insert (apply 'abbreviate-file-name
8014 (if (featurep 'xemacs) (list file t) (list file))))
8015 (if file-props
8016 (add-text-properties beg (point) file-props)))))
8017
8018 (defun idlwave-special-lib-test (file)
8019 "Check the path of FILE against the regexps which define special libs.
8020 Return the name of the special lib if there is a match."
8021 (let ((alist idlwave-special-lib-alist)
8022 entry rtn)
8023 (cond
8024 ((stringp file)
8025 (while (setq entry (pop alist))
8026 (if (string-match (car entry) file)
8027 (setq rtn (cdr entry)
8028 alist nil)))
8029 rtn)
8030 (t nil))))
8031
8032 (defun idlwave-mouse-active-rinfo-right (ev)
8033 (interactive "e")
8034 (idlwave-mouse-active-rinfo ev 'right))
8035
8036 (defun idlwave-mouse-active-rinfo-shift (ev)
8037 (interactive "e")
8038 (idlwave-mouse-active-rinfo ev nil 'shift))
8039
8040 (defun idlwave-active-rinfo-space ()
8041 (interactive)
8042 (idlwave-mouse-active-rinfo nil 'right))
8043
8044 (defun idlwave-mouse-active-rinfo (ev &optional right shift)
8045 "Does the mouse actions in the routine info buffer.
8046 Optional args RIGHT and SHIFT indicate, if mouse-3 was used, and if SHIFT
8047 was pressed."
8048 (interactive "e")
8049 (if ev (mouse-set-point ev))
8050 (let (data id name type class buf bufwin source word initial-class)
8051 (setq data (get-text-property (point) 'data)
8052 source (get-text-property (point) 'source)
8053 keyword (get-text-property (point) 'keyword)
8054 link (get-text-property (point) 'link)
8055 id (car data)
8056 name (nth 1 data) type (nth 2 data) class (nth 3 data)
8057 buf (nth 4 data)
8058 initial-class (nth 6 data)
8059 word (idlwave-this-word)
8060 bufwin (get-buffer-window buf t))
8061
8062 (cond ((eq id 'class) ; Switch class being displayed
8063 (if (window-live-p bufwin) (select-window bufwin))
8064 (idlwave-display-calling-sequence
8065 (idlwave-sintern-method name)
8066 type (idlwave-sintern-class word)
8067 initial-class))
8068 ((eq id 'usage) ; Online help on this routine
8069 (idlwave-online-help link name type class))
8070 ((eq id 'source) ; Source in help or buffer
8071 (if right ; In help
8072 (let ((idlwave-extra-help-function 'idlwave-help-with-source)
8073 (idlwave-help-source-try-header nil)
8074 ;; Fake idlwave-routines so help will find the right entry
8075 (idlwave-routines
8076 (list (list name type class source ""))))
8077 (idlwave-help-get-special-help name type class nil))
8078 ;; Otherwise just pop to the source
8079 (setq idlwave-popup-source (not idlwave-popup-source))
8080 (if idlwave-popup-source
8081 (condition-case err
8082 (idlwave-do-find-module name type class source)
8083 (error
8084 (setq idlwave-popup-source nil)
8085 (if (window-live-p bufwin) (select-window bufwin))
8086 (error (nth 1 err))))
8087 (if bufwin
8088 (select-window bufwin)
8089 (pop-to-buffer buf))
8090 (goto-char (marker-position idlwave-rinfo-marker)))))
8091 ((eq id 'keyword)
8092 (if right
8093 (idlwave-online-help link name type class keyword)
8094 (idlwave-rinfo-insert-keyword keyword buf shift))))))
8095
8096 (defun idlwave-rinfo-insert-keyword (keyword buffer &optional shift)
8097 "Insert KEYWORD in BUFFER. Make sure buffer is displayed in a window."
8098 (let ((bwin (get-buffer-window buffer)))
8099 (if idlwave-complete-empty-string-as-lower-case
8100 (setq keyword (downcase keyword)))
8101 (if bwin
8102 (select-window bwin)
8103 (pop-to-buffer buffer)
8104 (setq bwin (get-buffer-window buffer)))
8105 (if (eq (preceding-char) ?/)
8106 (insert keyword)
8107 (unless (save-excursion
8108 (re-search-backward
8109 "[(,][ \t]*\\(\\$[ \t]*\\(;.*\\)?\n\\)?[ \t]*\\="
8110 (min (- (point) 100) (point-min)) t))
8111 (insert ", "))
8112 (if shift (insert "/"))
8113 (insert keyword)
8114 (if (and (not shift)
8115 idlwave-keyword-completion-adds-equal)
8116 (insert "=")))))
8117
8118 (defun idlwave-list-buffer-load-path-shadows (&optional arg)
8119 "List the load path shadows of all routines defined in current buffer."
8120 (interactive "P")
8121 (idlwave-routines)
8122 (if (eq major-mode 'idlwave-mode)
8123 (idlwave-list-load-path-shadows
8124 nil (idlwave-update-current-buffer-info 'save-buffer)
8125 "in current buffer")
8126 (error "Current buffer is not in idlwave-mode")))
8127
8128 (defun idlwave-list-shell-load-path-shadows (&optional arg)
8129 "List the load path shadows of all routines compiled under the shell.
8130 This is very useful for checking an IDL application. Just compile the
8131 application, do RESOLVE_ALL, and `C-c C-i' to compile all referenced
8132 routines and update IDLWAVE internal info. Then check for shadowing
8133 with this command."
8134 (interactive "P")
8135 (cond
8136 ((or (not (fboundp 'idlwave-shell-is-running))
8137 (not (idlwave-shell-is-running)))
8138 (error "Shell is not running"))
8139 ((null idlwave-compiled-routines)
8140 (error "No compiled routines. Maybe you need to update with `C-c C-i'"))
8141 (t
8142 (idlwave-list-load-path-shadows nil idlwave-compiled-routines
8143 "in the shell"))))
8144
8145 (defun idlwave-list-all-load-path-shadows (&optional arg)
8146 "List the load path shadows of all routines known to IDLWAVE."
8147 (interactive "P")
8148 (idlwave-list-load-path-shadows nil nil "globally"))
8149
8150 (defun idlwave-list-load-path-shadows (arg &optional special-routines loc)
8151 "List the routines which are defined multiple times.
8152 Search the information IDLWAVE has about IDL routines for multiple
8153 definitions.
8154 When SPECIAL-ROUTINES in non-nil, only look for shadows of these routines.
8155
8156 When IDL hits a routine call which is not defined, it will search on
8157 the load path in order to find a definition. The output of this
8158 command can be used to detect possible name clashes during this process."
8159 (idlwave-routines) ; Make sure everything is loaded.
8160 (unless (or idlwave-user-catalog-routines idlwave-library-catalog-routines)
8161 (or (y-or-n-p
8162 "You don't have any user or library catalogs. Continue anyway? ")
8163 (error "Abort")))
8164 (let* ((routines (append idlwave-system-routines
8165 idlwave-compiled-routines
8166 idlwave-library-catalog-routines
8167 idlwave-user-catalog-routines
8168 idlwave-buffer-routines
8169 nil))
8170 (km-prop (if (featurep 'xemacs) 'keymap 'local-map))
8171 (keymap (make-sparse-keymap))
8172 (props (list 'mouse-face 'highlight
8173 km-prop keymap
8174 'help-echo "Mouse2: Find source"))
8175 (nroutines (length (or special-routines routines)))
8176 (step (/ nroutines 99))
8177 (n 0)
8178 (next-perc 1)
8179 (cnt 0)
8180 (idlwave-sort-prefer-buffer-info nil)
8181 routine twins dtwins twin done props1 lroutines)
8182
8183 (if special-routines
8184 ;; Just looking for shadows of a few special routines
8185 (setq lroutines routines
8186 routines special-routines))
8187
8188 (message "Sorting routines...")
8189 (setq routines (sort routines
8190 (lambda (a b)
8191 (string< (downcase (idlwave-make-full-name
8192 (nth 2 a) (car a)))
8193 (downcase (idlwave-make-full-name
8194 (nth 2 b) (car b)))))))
8195 (message "Sorting routines...done")
8196
8197 (define-key keymap (if (featurep 'xemacs) [(button2)] [(mouse-2)])
8198 (lambda (ev)
8199 (interactive "e")
8200 (mouse-set-point ev)
8201 (apply 'idlwave-do-find-module
8202 (get-text-property (point) 'find-args))))
8203 (define-key keymap [(return)]
8204 (lambda ()
8205 (interactive)
8206 (apply 'idlwave-do-find-module
8207 (get-text-property (point) 'find-args))))
8208 (message "Compiling list...( 0%%)")
8209 (save-excursion
8210 (set-buffer (get-buffer-create "*Shadows*"))
8211 (setq buffer-read-only nil)
8212 (erase-buffer)
8213 (while (setq routine (pop routines))
8214 (setq n (1+ n))
8215 (if (= (* next-perc step) n)
8216 (progn
8217 (message "Compiling list...(%2d%%)" next-perc)
8218 (setq next-perc (1+ next-perc))))
8219 ;; Get a list of all twins
8220 (setq twins (idlwave-routine-twins routine (or lroutines routines)))
8221 (if (memq routine done)
8222 (setq dtwins nil)
8223 (setq dtwins (idlwave-study-twins twins)))
8224 ;; Mark all twins as dealt with
8225 (setq done (append twins done))
8226 (when (or (> (length dtwins) 1)
8227 (> (idlwave-count-memq 'lib (nth 2 (car dtwins))) 1)
8228 (> (idlwave-count-memq 'user (nth 2 (car dtwins))) 1)
8229 (> (idlwave-count-memq 'buffer (nth 2 (car dtwins))) 1))
8230 (incf cnt)
8231 (insert (format "\n%s%s"
8232 (idlwave-make-full-name (nth 2 routine)
8233 (car routine))
8234 (if (eq (nth 1 routine) 'fun) "()" "")))
8235 (while (setq twin (pop dtwins))
8236 (setq props1 (append (list 'find-args
8237 (list (nth 0 routine)
8238 (nth 1 routine)
8239 (nth 2 routine)))
8240 props))
8241 (idlwave-insert-source-location "\n - " twin props1))))
8242 (goto-char (point-min))
8243 (setq buffer-read-only t))
8244 (setq loc (or loc ""))
8245 (if (> cnt 0)
8246 (progn
8247 (display-buffer (get-buffer "*Shadows*"))
8248 (message "%d case%s of shadowing found %s"
8249 cnt (if (= cnt 1) "" "s") loc))
8250 (message "No shadowing conflicts found %s" loc))))
8251
8252 (defun idlwave-print-source (routine)
8253 (let* ((source (nth 3 routine))
8254 (stype (car source))
8255 (sfile (idlwave-routine-source-file source)))
8256 (if (idlwave-syslib-p sfile) (setq stype 'syslib))
8257 (if (and (eq stype 'compiled)
8258 (or (not (stringp sfile))
8259 (not (string-match "\\S-" sfile))))
8260 (setq stype 'unresolved))
8261 (princ (format " %-10s %s\n"
8262 stype
8263 (if sfile sfile "No source code available")))))
8264
8265 (defun idlwave-routine-twins (entry &optional list)
8266 "Return all twin entries of ENTRY in LIST.
8267 LIST defaults to `idlwave-routines'.
8268 Twin entries are those which have the same name, type, and class.
8269 ENTRY will also be returned, as the first item of this list."
8270 (let* ((name (car entry))
8271 (type (nth 1 entry))
8272 (class (nth 2 entry))
8273 (candidates (idlwave-all-assq name (or list (idlwave-routines))))
8274 twins candidate)
8275 (while (setq candidate (pop candidates))
8276 (if (and (not (eq candidate entry))
8277 (eq type (nth 1 candidate))
8278 (eq class (nth 2 candidate)))
8279 (push candidate twins)))
8280 (if (setq candidate (idlwave-rinfo-assq name type class
8281 idlwave-unresolved-routines))
8282 (push candidate twins))
8283 (cons entry (nreverse twins))))
8284
8285 (defun idlwave-study-twins (entries)
8286 "Return dangerous twins of first entry in ENTRIES.
8287 Dangerous twins are routines with same name, but in different files on
8288 the load path. If a file is in the system library and has an entry in
8289 the `idlwave-system-routines' list, we omit the latter as
8290 non-dangerous because many IDL routines are implemented as library
8291 routines, and may have been scanned."
8292 (let* ((entry (car entries))
8293 (name (car entry)) ;
8294 (type (nth 1 entry)) ; Must be bound for
8295 (class (nth 2 entry)) ; idlwave-routine-twin-compare
8296 (cnt 0)
8297 source type type-cons file alist syslibp key)
8298 (while (setq entry (pop entries))
8299 (incf cnt)
8300 (setq source (nth 3 entry)
8301 type (car source)
8302 type-cons (cons type (nth 3 source))
8303 file (idlwave-routine-source-file source))
8304
8305 ;; Make KEY to index entry properly
8306 (setq key (cond ((eq type 'system) type)
8307 (file (file-truename file))
8308 (t 'unresolved)))
8309
8310 ;; Check for an entry in the system library
8311 (if (and file
8312 (not syslibp)
8313 (idlwave-syslib-p file))
8314 (setq syslibp t))
8315
8316 ;; If there's more than one matching entry for the same file, just
8317 ;; append the type-cons to the type list.
8318 (if (setq entry (assoc key alist))
8319 (push type-cons (nth 2 entry))
8320 (push (list key file (list type-cons)) alist)))
8321
8322 (setq alist (nreverse alist))
8323
8324 (when syslibp
8325 ;; File is in system *library* - remove any 'system entry
8326 (setq alist (delq (assq 'system alist) alist)))
8327
8328 ;; If 'system remains and we've scanned the syslib, it's a builtin
8329 ;; (rather than a !DIR/lib/.pro file bundled as source).
8330 (when (and (idlwave-syslib-scanned-p)
8331 (setq entry (assoc 'system alist)))
8332 (setcar entry 'builtin))
8333 (sort alist 'idlwave-routine-twin-compare)))
8334
8335 (defvar name)
8336 (defvar type)
8337 (defvar class)
8338 (defvar idlwave-sort-prefer-buffer-info t
8339 "Internal variable used to influence `idlwave-routine-twin-compare'.")
8340
8341 (defmacro idlwave-xor (a b)
8342 `(and (or ,a ,b)
8343 (not (and ,a ,b))))
8344
8345 (defun idlwave-routine-entry-compare (a b)
8346 "Compare two routine info entries for sortiung. This is the general case.
8347 It first compates class, names, and type. If it turns out that A and B
8348 are twins (same name, class, and type), calls another routine which
8349 compares twins on the basis of their file names and path locations."
8350 (let ((name (car a)) (type (nth 1 a)) (class (nth 2 a)))
8351 (cond
8352 ((not (equal (idlwave-downcase-safe class)
8353 (idlwave-downcase-safe (nth 2 b))))
8354 ;; Class decides
8355 (cond ((null (nth 2 b)) nil)
8356 ((null class) t)
8357 (t (string< (downcase class) (downcase (nth 2 b))))))
8358 ((not (equal (downcase name) (downcase (car b))))
8359 ;; Name decides
8360 (string< (downcase name) (downcase (car b))))
8361 ((not (eq type (nth 1 b)))
8362 ;; Type decides
8363 (< (if (eq type 'fun) 1 0) (if (eq (nth 1 b) 'fun) 1 0)))
8364 (t
8365 ;; A and B are twins - so the decision is more complicated.
8366 ;; Call twin-compare with the proper arguments.
8367 (idlwave-routine-entry-compare-twins a b)))))
8368
8369 (defun idlwave-routine-entry-compare-twins (a b)
8370 "Compare two routine entries, under the assumption that they are
8371 twins. This basically calls `idlwave-routine-twin-compare' with the
8372 correct args."
8373 (let* ((name (car a)) (type (nth 1 a)) (class (nth 2 a)) ; needed outside
8374 (asrc (nth 3 a))
8375 (atype (car asrc))
8376 (bsrc (nth 3 b))
8377 (btype (car bsrc))
8378 (afile (idlwave-routine-source-file asrc))
8379 (bfile (idlwave-routine-source-file bsrc)))
8380 (idlwave-routine-twin-compare
8381 (if (stringp afile)
8382 (list (file-truename afile) afile (list atype))
8383 (list atype afile (list atype)))
8384 (if (stringp bfile)
8385 (list (file-truename bfile) bfile (list btype))
8386 (list btype bfile (list btype))))
8387 ))
8388
8389 (defun idlwave-routine-twin-compare (a b)
8390 "Compare two routine twin entries for sorting.
8391 In here, A and B are not normal routine info entries, but special
8392 lists (KEY FILENAME (TYPES...)).
8393 This expects NAME TYPE CLASS to be bound to the right values."
8394 (let* (;; Dis-assemble entries
8395 (akey (car a)) (bkey (car b))
8396 (afile (nth 1 a)) (bfile (nth 1 b))
8397 (atypes (nth 2 a)) (btypes (nth 2 b))
8398 ;; System routines?
8399 (asysp (memq akey '(builtin system)))
8400 (bsysp (memq bkey '(builtin system)))
8401 ;; Compiled routines?
8402 (acompp (memq 'compiled atypes))
8403 (bcompp (memq 'compiled btypes))
8404 ;; Unresolved?
8405 (aunresp (or (eq akey 'unresolved)
8406 (and acompp (not afile))))
8407 (bunresp (or (eq bkey 'unresolved)
8408 (and bcompp (not bfile))))
8409 ;; Buffer info available?
8410 (abufp (memq 'buffer atypes))
8411 (bbufp (memq 'buffer btypes))
8412 ;; On search path?
8413 (tpath-alist (idlwave-true-path-alist))
8414 (apathp (and (stringp akey)
8415 (assoc (file-name-directory akey) tpath-alist)))
8416 (bpathp (and (stringp bkey)
8417 (assoc (file-name-directory bkey) tpath-alist)))
8418 ;; How early on search path? High number means early since we
8419 ;; measure the tail of the path list
8420 (anpath (length (memq apathp tpath-alist)))
8421 (bnpath (length (memq bpathp tpath-alist)))
8422 ;; Look at file names
8423 (aname (if (stringp afile) (downcase (file-name-nondirectory afile)) ""))
8424 (bname (if (stringp bfile) (downcase (file-name-nondirectory bfile)) ""))
8425 (fname-re (if class (format "\\`%s__\\(%s\\|define\\)\\.pro\\'"
8426 (regexp-quote (downcase class))
8427 (regexp-quote (downcase name)))
8428 (format "\\`%s\\.pro" (regexp-quote (downcase name)))))
8429 ;; Is file name derived from the routine name?
8430 ;; Method file or class definition file?
8431 (anamep (string-match fname-re aname))
8432 (adefp (and class anamep (string= "define" (match-string 1 aname))))
8433 (bnamep (string-match fname-re bname))
8434 (bdefp (and class bnamep (string= "define" (match-string 1 bname)))))
8435
8436 ;; Now: follow JD's ideas about sorting. Looks really simple now,
8437 ;; doesn't it? The difficult stuff is hidden above...
8438 (cond
8439 ((idlwave-xor asysp bsysp) asysp) ; System entries first
8440 ((idlwave-xor aunresp bunresp) bunresp) ; Unresolved last
8441 ((and idlwave-sort-prefer-buffer-info
8442 (idlwave-xor abufp bbufp)) abufp) ; Buffers before non-buffers
8443 ((idlwave-xor acompp bcompp) acompp) ; Compiled entries
8444 ((idlwave-xor apathp bpathp) apathp) ; Library before non-library
8445 ((idlwave-xor anamep bnamep) anamep) ; Correct file names first
8446 ((and class anamep bnamep ; both file names match ->
8447 (idlwave-xor adefp bdefp)) bdefp) ; __define after __method
8448 ((> anpath bnpath) t) ; Who is first on path?
8449 (t nil)))) ; Default
8450
8451 (defun idlwave-routine-source-file (source)
8452 (if (nth 2 source)
8453 (expand-file-name (nth 1 source) (nth 2 source))
8454 (nth 1 source)))
8455
8456 (defun idlwave-downcase-safe (string)
8457 "Donwcase if string, else return unchanged."
8458 (if (stringp string)
8459 (downcase string)
8460 string))
8461
8462 (defun idlwave-count-eq (elt list)
8463 "How often is ELT in LIST?"
8464 (length (delq nil (mapcar (lambda (x) (eq x elt)) list))))
8465
8466 (defun idlwave-count-memq (elt alist)
8467 "How often is ELT a key in ALIST?"
8468 (length (delq nil (mapcar (lambda (x) (eq (car x) elt)) alist))))
8469
8470 (defun idlwave-syslib-p (file)
8471 "Non-nil if FILE is in the system library."
8472 (let* ((true-syslib (file-name-as-directory
8473 (file-truename
8474 (expand-file-name "lib" (idlwave-sys-dir)))))
8475 (true-file (file-truename file)))
8476 (string-match (concat "^" (regexp-quote true-syslib)) true-file)))
8477
8478 (defun idlwave-lib-p (file)
8479 "Non-nil if file is in the library"
8480 (let ((true-dir (file-name-directory (file-truename file))))
8481 (assoc true-dir (idlwave-true-path-alist))))
8482
8483 (defun idlwave-path-alist-add-flag (list-entry flag)
8484 "Add a flag to the path list entry, if not set."
8485 (let ((flags (cdr list-entry)))
8486 (add-to-list 'flags flag)
8487 (setcdr list-entry flags)))
8488
8489 (defun idlwave-path-alist-remove-flag (list-entry flag)
8490 "Remove a flag to the path list entry, if set."
8491 (let ((flags (delq flag (cdr list-entry))))
8492 (setcdr list-entry flags)))
8493
8494 (defun idlwave-true-path-alist ()
8495 "Return `idlwave-path-alist' alist with true-names.
8496 Info is cached, but relies on the functions setting `idlwave-path-alist'
8497 to reset the variable `idlwave-true-path-alist' to nil."
8498 (or idlwave-true-path-alist
8499 (setq idlwave-true-path-alist
8500 (mapcar (lambda(x) (cons
8501 (file-name-as-directory
8502 (file-truename
8503 (directory-file-name
8504 (car x))))
8505 (cdr x)))
8506 idlwave-path-alist))))
8507
8508 (defun idlwave-syslib-scanned-p ()
8509 "Non-nil if the system lib file !DIR/lib has been scanned."
8510 (let* ((true-syslib (file-name-as-directory
8511 (file-truename
8512 (expand-file-name "lib" (idlwave-sys-dir))))))
8513 (cdr (assoc true-syslib (idlwave-true-path-alist)))))
8514
8515 ;; ----------------------------------------------------------------------------
8516 ;;
8517 ;; Online Help display
8518
8519
8520 ;; ----------------------------------------------------------------------------
8521 ;;
8522 ;; Additions for use with imenu.el and func-menu.el
8523 ;; (pop-up a list of IDL units in the current file).
8524 ;;
8525
8526 (defun idlwave-prev-index-position ()
8527 "Search for the previous procedure or function.
8528 Return nil if not found. For use with imenu.el."
8529 (save-match-data
8530 (cond
8531 ((idlwave-find-key "\\<\\(pro\\|function\\)\\>" -1 'nomark))
8532 ;; ((idlwave-find-key idlwave-begin-unit-reg 1 'nomark)
8533 (t nil))))
8534
8535 (defun idlwave-unit-name ()
8536 "Return the unit name.
8537 Assumes that point is at the beginning of the unit as found by
8538 `idlwave-prev-index-position'."
8539 (forward-sexp 2)
8540 (forward-sexp -1)
8541 (let ((begin (point)))
8542 (re-search-forward
8543 "[a-zA-Z_][a-zA-Z0-9$_]+\\(::[a-zA-Z_][a-zA-Z0-9$_]+\\)?")
8544 (if (fboundp 'buffer-substring-no-properties)
8545 (buffer-substring-no-properties begin (point))
8546 (buffer-substring begin (point)))))
8547
8548 (defalias 'idlwave-function-menu
8549 (condition-case nil
8550 (progn
8551 (require 'func-menu)
8552 'function-menu)
8553 (error (condition-case nil
8554 (progn
8555 (require 'imenu)
8556 'imenu)
8557 (error nil)))))
8558
8559 ;; Here we hack func-menu.el in order to support this new mode.
8560 ;; The latest versions of func-menu.el already have this stuff in, so
8561 ;; we hack only if it is not already there.
8562 (when (fboundp 'eval-after-load)
8563 (eval-after-load "func-menu"
8564 '(progn
8565 (or (assq 'idlwave-mode fume-function-name-regexp-alist)
8566 (not (boundp 'fume-function-name-regexp-idl)) ; avoid problems
8567 (setq fume-function-name-regexp-alist
8568 (cons '(idlwave-mode . fume-function-name-regexp-idl)
8569 fume-function-name-regexp-alist)))
8570 (or (assq 'idlwave-mode fume-find-function-name-method-alist)
8571 (not (fboundp 'fume-find-next-idl-function-name)) ; avoid problems
8572 (setq fume-find-function-name-method-alist
8573 (cons '(idlwave-mode . fume-find-next-idl-function-name)
8574 fume-find-function-name-method-alist))))))
8575
8576 (defun idlwave-edit-in-idlde ()
8577 "Edit the current file in IDL Development environment."
8578 (interactive)
8579 (start-process "idldeclient" nil
8580 idlwave-shell-explicit-file-name "-c" "-e"
8581 (buffer-file-name) "&"))
8582
8583 (defun idlwave-launch-idlhelp ()
8584 "Start the IDLhelp application."
8585 (interactive)
8586 (start-process "idlhelp" nil idlwave-help-application))
8587
8588 ;; Menus - using easymenu.el
8589 (defvar idlwave-mode-menu-def
8590 `("IDLWAVE"
8591 ["PRO/FUNC menu" idlwave-function-menu t]
8592 ("Motion"
8593 ["Subprogram Start" idlwave-beginning-of-subprogram t]
8594 ["Subprogram End" idlwave-end-of-subprogram t]
8595 ["Block Start" idlwave-beginning-of-block t]
8596 ["Block End" idlwave-end-of-block t]
8597 ["Up Block" idlwave-backward-up-block t]
8598 ["Down Block" idlwave-down-block t]
8599 ["Skip Block Backward" idlwave-backward-block t]
8600 ["Skip Block Forward" idlwave-forward-block t])
8601 ("Mark"
8602 ["Subprogram" idlwave-mark-subprogram t]
8603 ["Block" idlwave-mark-block t]
8604 ["Header" idlwave-mark-doclib t])
8605 ("Format"
8606 ["Indent Subprogram" idlwave-indent-subprogram t]
8607 ["(Un)Comment Region" idlwave-toggle-comment-region "C-c ;"]
8608 ["Continue/Split line" idlwave-split-line t]
8609 "--"
8610 ["Toggle Auto Fill" idlwave-auto-fill-mode :style toggle
8611 :selected (symbol-value idlwave-fill-function)])
8612 ("Templates"
8613 ["Procedure" idlwave-procedure t]
8614 ["Function" idlwave-function t]
8615 ["Doc Header" idlwave-doc-header t]
8616 ["Log" idlwave-doc-modification t]
8617 "--"
8618 ["Case" idlwave-case t]
8619 ["For" idlwave-for t]
8620 ["Repeat" idlwave-repeat t]
8621 ["While" idlwave-while t]
8622 "--"
8623 ["Close Block" idlwave-close-block t])
8624 ("Completion"
8625 ["Complete" idlwave-complete t]
8626 ("Complete Special"
8627 ["1 Procedure Name" (idlwave-complete 'procedure) t]
8628 ["2 Procedure Keyword" (idlwave-complete 'procedure-keyword) t]
8629 "--"
8630 ["3 Function Name" (idlwave-complete 'function) t]
8631 ["4 Function Keyword" (idlwave-complete 'function-keyword) t]
8632 "--"
8633 ["5 Procedure Method Name" (idlwave-complete 'procedure-method) t]
8634 ["6 Procedure Method Keyword" (idlwave-complete 'procedure-method-keyword) t]
8635 "--"
8636 ["7 Function Method Name" (idlwave-complete 'function-method) t]
8637 ["8 Function Method Keyword" (idlwave-complete 'function-method-keyword) t]
8638 "--"
8639 ["9 Class Name" idlwave-complete-class t]))
8640 ("Routine Info"
8641 ["Show Routine Info" idlwave-routine-info t]
8642 ["Online Context Help" idlwave-context-help t]
8643 "--"
8644 ["Find Routine Source" idlwave-find-module t]
8645 ["Resolve Routine" idlwave-resolve (featurep 'idlw-shell)]
8646 "--"
8647 ["Update Routine Info" idlwave-update-routine-info t]
8648 "--"
8649 "IDL User Catalog"
8650 ["Select Catalog Directories" (idlwave-create-user-catalog-file nil) t]
8651 ["Scan Directories" (idlwave-update-routine-info '(16))
8652 (and idlwave-path-alist (not idlwave-catalog-process))]
8653 ["Scan Directories &" (idlwave-update-routine-info '(64))
8654 (and idlwave-path-alist (not idlwave-catalog-process))]
8655 "--"
8656 "Routine Shadows"
8657 ["Check Current Buffer" idlwave-list-buffer-load-path-shadows t]
8658 ["Check Compiled Routines" idlwave-list-shell-load-path-shadows t]
8659 ["Check Everything" idlwave-list-all-load-path-shadows t])
8660 ("Misc"
8661 ["Kill auto-created buffers" idlwave-kill-autoloaded-buffers t]
8662 "--"
8663 ["Insert TAB character" idlwave-hard-tab t])
8664 "--"
8665 ("External"
8666 ["Generate IDL tags" idlwave-make-tags t]
8667 ["Start IDL shell" idlwave-shell t]
8668 ["Edit file in IDLDE" idlwave-edit-in-idlde t]
8669 ["Launch IDL Help" idlwave-launch-idlhelp t])
8670 "--"
8671 ("Customize"
8672 ["Browse IDLWAVE Group" idlwave-customize t]
8673 "--"
8674 ["Build Full Customize Menu" idlwave-create-customize-menu
8675 (fboundp 'customize-menu-create)])
8676 ("Documentation"
8677 ["Describe Mode" describe-mode t]
8678 ["Abbreviation List" idlwave-list-abbrevs t]
8679 "--"
8680 ["Commentary in idlwave.el" idlwave-show-commentary t]
8681 ["Commentary in idlw-shell.el" idlwave-shell-show-commentary t]
8682 "--"
8683 ["Info" idlwave-info t]
8684 "--"
8685 ["Launch IDL Help" idlwave-launch-idlhelp t])))
8686
8687 (defvar idlwave-mode-debug-menu-def
8688 '("Debug"
8689 ["Start IDL shell" idlwave-shell t]
8690 ["Save and .RUN buffer" idlwave-shell-save-and-run
8691 (and (boundp 'idlwave-shell-automatic-start)
8692 idlwave-shell-automatic-start)]))
8693
8694 (if (or (featurep 'easymenu) (load "easymenu" t))
8695 (progn
8696 (easy-menu-define idlwave-mode-menu idlwave-mode-map
8697 "IDL and WAVE CL editing menu"
8698 idlwave-mode-menu-def)
8699 (easy-menu-define idlwave-mode-debug-menu idlwave-mode-map
8700 "IDL and WAVE CL editing menu"
8701 idlwave-mode-debug-menu-def)))
8702
8703 (defun idlwave-customize ()
8704 "Call the customize function with idlwave as argument."
8705 (interactive)
8706 ;; Try to load the code for the shell, so that we can customize it
8707 ;; as well.
8708 (or (featurep 'idlw-shell)
8709 (load "idlw-shell" t))
8710 (customize-browse 'idlwave))
8711
8712 (defun idlwave-create-customize-menu ()
8713 "Create a full customization menu for IDLWAVE, insert it into the menu."
8714 (interactive)
8715 (if (fboundp 'customize-menu-create)
8716 (progn
8717 ;; Try to load the code for the shell, so that we can customize it
8718 ;; as well.
8719 (or (featurep 'idlw-shell)
8720 (load "idlw-shell" t))
8721 (easy-menu-change
8722 '("IDLWAVE") "Customize"
8723 `(["Browse IDLWAVE group" idlwave-customize t]
8724 "--"
8725 ,(customize-menu-create 'idlwave)
8726 ["Set" Custom-set t]
8727 ["Save" Custom-save t]
8728 ["Reset to Current" Custom-reset-current t]
8729 ["Reset to Saved" Custom-reset-saved t]
8730 ["Reset to Standard Settings" Custom-reset-standard t]))
8731 (message "\"IDLWAVE\"-menu now contains full customization menu"))
8732 (error "Cannot expand menu (outdated version of cus-edit.el)")))
8733
8734 (defun idlwave-show-commentary ()
8735 "Use the finder to view the file documentation from `idlwave.el'."
8736 (interactive)
8737 (require 'finder)
8738 (finder-commentary "idlwave.el"))
8739
8740 (defun idlwave-shell-show-commentary ()
8741 "Use the finder to view the file documentation from `idlw-shell.el'."
8742 (interactive)
8743 (require 'finder)
8744 (finder-commentary "idlw-shell.el"))
8745
8746 (defun idlwave-info ()
8747 "Read documentation for IDLWAVE in the info system."
8748 (interactive)
8749 (require 'info)
8750 (Info-goto-node "(idlwave)"))
8751
8752 (defun idlwave-list-abbrevs (arg)
8753 "Show the code abbreviations define in IDLWAVE mode.
8754 This lists all abbrevs where the replacement text differs from the input text.
8755 These are the ones the users want to learn to speed up their writing.
8756
8757 The function does *not* list abbrevs which replace a word with itself
8758 to call a hook. These hooks are used to change the case of words or
8759 to blink the matching `begin', and the user does not need to know them.
8760
8761 With arg, list all abbrevs with the corresponding hook.
8762
8763 This function was written since `list-abbrevs' looks terrible for IDLWAVE mode."
8764
8765 (interactive "P")
8766 (let ((table (symbol-value 'idlwave-mode-abbrev-table))
8767 abbrevs
8768 str rpl func fmt (len-str 0) (len-rpl 0))
8769 (mapatoms
8770 (lambda (sym)
8771 (if (symbol-value sym)
8772 (progn
8773 (setq str (symbol-name sym)
8774 rpl (symbol-value sym)
8775 func (symbol-function sym))
8776 (if arg
8777 (setq func (prin1-to-string func))
8778 (if (and (listp func) (stringp (nth 2 func)))
8779 (setq rpl (concat "EVAL: " (nth 2 func))
8780 func "")
8781 (setq func "")))
8782 (if (or arg (not (string= rpl str)))
8783 (progn
8784 (setq len-str (max len-str (length str)))
8785 (setq len-rpl (max len-rpl (length rpl)))
8786 (setq abbrevs (cons (list str rpl func) abbrevs)))))))
8787 table)
8788 ;; sort the list
8789 (setq abbrevs (sort abbrevs (lambda (a b) (string< (car a) (car b)))))
8790 ;; Make the format
8791 (setq fmt (format "%%-%ds %%-%ds %%s\n" len-str len-rpl))
8792 (with-output-to-temp-buffer "*Help*"
8793 (if arg
8794 (progn
8795 (princ "Abbreviations and Actions in IDLWAVE-Mode\n")
8796 (princ "=========================================\n\n")
8797 (princ (format fmt "KEY" "REPLACE" "HOOK"))
8798 (princ (format fmt "---" "-------" "----")))
8799 (princ "Code Abbreviations and Templates in IDLWAVE-Mode\n")
8800 (princ "================================================\n\n")
8801 (princ (format fmt "KEY" "ACTION" ""))
8802 (princ (format fmt "---" "------" "")))
8803 (mapcar
8804 (lambda (list)
8805 (setq str (car list)
8806 rpl (nth 1 list)
8807 func (nth 2 list))
8808 (princ (format fmt str rpl func)))
8809 abbrevs)))
8810 ;; Make sure each abbreviation uses only one display line
8811 (save-excursion
8812 (set-buffer "*Help*")
8813 (setq truncate-lines t)))
8814
8815 ;; Add .pro files to speedbar for support, if it's loaded
8816 (eval-after-load "speedbar" '(speedbar-add-supported-extension ".pro"))
8817
8818 ;; Set an idle timer to load the routine info.
8819 ;; Will only work on systems which support this.
8820 (or idlwave-routines (idlwave-start-load-rinfo-timer))
8821
8822 ;;;###autoload(add-to-list 'auto-mode-alist '("\\.[Pp][Rr][Oo]\\'" . idlwave-mode))
8823
8824 ;; Run the hook
8825 (run-hooks 'idlwave-load-hook)
8826
8827 (provide 'idlwave)
8828
8829 ;; arch-tag: f77f3b0c-c37c-424f-a328-0886fd42b6fb
8830 ;;; idlwave.el ends here