]> code.delx.au - gnu-emacs/blob - lisp/progmodes/idlwave.el
(idlwave-library-path): Fix typo in docstring.
[gnu-emacs] / lisp / progmodes / idlwave.el
1 ;; idlwave.el --- IDL editing mode for GNU Emacs
2 ;; Copyright (c) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
3 ;; Free Software Foundation
4
5 ;; Authors: J.D. Smith <jdsmith@as.arizona.edu>
6 ;; Carsten Dominik <dominik@science.uva.nl>
7 ;; Chris Chase <chase@att.com>
8 ;; Maintainer: J.D. Smith <jdsmith@as.arizona.edu>
9 ;; Version: 6.1_em22
10 ;; Keywords: languages
11
12 ;; This file is part of GNU Emacs.
13
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; any later version.
18
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
23
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
27 ;; Boston, MA 02110-1301, USA.
28
29 ;;; Commentary:
30
31 ;; IDLWAVE enables feature-rich development and interaction with IDL,
32 ;; the Interactive Data Language. It provides a compelling,
33 ;; full-featured alternative to the IDLDE development environment
34 ;; bundled with IDL.
35
36 ;; In the remotely distant past, based on pascal.el, though bears
37 ;; little resemblance to it now.
38 ;;
39 ;; Incorporates many ideas, such as abbrevs, action routines, and
40 ;; continuation line indenting, from wave.el.
41 ;; wave.el original written by Lubos Pochman, Precision Visuals, Boulder.
42 ;;
43 ;; See the mode description ("C-h m" in idlwave-mode or "C-h f idlwave-mode")
44 ;; for features, key bindings, and info.
45 ;; Also, Info format documentation is available with `M-x idlwave-info'
46 ;;
47 ;; New versions of IDLWAVE, documentation, and more information
48 ;; available from:
49 ;; http://idlwave.org
50 ;;
51 ;; INSTALLATION
52 ;; ============
53 ;;
54 ;; Follow the instructions in the INSTALL file of the distribution.
55 ;; In short, put this file on your load path and add the following
56 ;; lines to your .emacs file:
57 ;;
58 ;; (autoload 'idlwave-mode "idlwave" "IDLWAVE Mode" t)
59 ;; (autoload 'idlwave-shell "idlw-shell" "IDLWAVE Shell" t)
60 ;; (setq auto-mode-alist (cons '("\\.pro\\'" . idlwave-mode) auto-mode-alist))
61 ;;
62 ;;
63 ;; SOURCE
64 ;; ======
65 ;;
66 ;; The newest version of this file is available from the maintainer's
67 ;; Webpage:
68 ;;
69 ;; http://idlwave.org
70 ;;
71 ;; DOCUMENTATION
72 ;; =============
73 ;;
74 ;; IDLWAVE is documented online in info format. A printable version
75 ;; of the documentation is available from the maintainers webpage (see
76 ;; SOURCE).
77 ;;
78 ;;
79 ;; ACKNOWLEDGMENTS
80 ;; ===============
81 ;;
82 ;; Thanks to the following people for their contributions and comments:
83 ;;
84 ;; Ulrik Dickow <dickow_at_nbi.dk>
85 ;; Eric E. Dors <edors_at_lanl.gov>
86 ;; Stein Vidar H. Haugan <s.v.h.haugan_at_astro.uio.no>
87 ;; David Huenemoerder <dph_at_space.mit.edu>
88 ;; Kevin Ivory <Kevin.Ivory_at_linmpi.mpg.de>
89 ;; Dick Jackson <dick_at_d-jackson.com>
90 ;; Xuyong Liu <liu_at_stsci.edu>
91 ;; Simon Marshall <Simon.Marshall_at_esrin.esa.it>
92 ;; Laurent Mugnier <mugnier_at_onera.fr>
93 ;; Lubos Pochman <lubos_at_rsinc.com>
94 ;; Bob Portmann <portmann_at_al.noaa.gov>
95 ;; Patrick M. Ryan <pat_at_jaameri.gsfc.nasa.gov>
96 ;; Marty Ryba <ryba_at_ll.mit.edu>
97 ;; Paul Sorenson <aardvark62_at_msn.com>
98 ;; Phil Sterne <sterne_at_dublin.llnl.gov>
99 ;; Phil Williams <williams_at_irc.chmcc.org>
100 ;;
101 ;; CUSTOMIZATION:
102 ;; =============
103 ;;
104 ;; IDLWAVE has extensive customize support; to learn about the
105 ;; variables which control the mode's behavior, use `M-x
106 ;; idlwave-customize'.
107 ;;
108 ;; You can set your own preferred values with Customize, or with Lisp
109 ;; code in .emacs. For an example of what to put into .emacs, check
110 ;; the TexInfo documentation or see a complete .emacs available at the
111 ;; website.
112 ;;
113 ;; KNOWN PROBLEMS:
114 ;; ==============
115 ;;
116 ;; IDLWAVE support for the IDL-derived PV-WAVE CL language of Visual
117 ;; Numerics, Inc. is growing less and less complete as the two
118 ;; languages grow increasingly apart. The mode probably shouldn't
119 ;; even have "WAVE" in its title, but it's catchy, and was required
120 ;; to avoid conflict with the CORBA idl.el mode. Caveat WAVEor.
121 ;;
122 ;; Moving the point backwards in conjunction with abbrev expansion
123 ;; does not work as I would like it, but this is a problem with
124 ;; emacs abbrev expansion done by the self-insert-command. It ends
125 ;; up inserting the character that expanded the abbrev after moving
126 ;; point backward, e.g., "\cl" expanded with a space becomes
127 ;; "LONG( )" with point before the close paren. This is solved by
128 ;; using a temporary function in `post-command-hook' - not pretty,
129 ;; but it works.
130 ;;
131 ;; Tabs and spaces are treated equally as whitespace when filling a
132 ;; comment paragraph. To accomplish this, tabs are permanently
133 ;; replaced by spaces in the text surrounding the paragraph, which
134 ;; may be an undesirable side-effect. Replacing tabs with spaces is
135 ;; limited to comments only and occurs only when a comment
136 ;; paragraph is filled via `idlwave-fill-paragraph'.
137 ;;
138 ;; Muti-statement lines (using "&") on block begin and end lines can
139 ;; ruin the formatting. For example, multiple end statements on a
140 ;; line: endif & endif. Using "&" outside of block begin/end lines
141 ;; should be okay.
142 ;;
143 ;; Determining the expression at point for printing and other
144 ;; examination commands is somewhat rough: currently only fairly
145 ;; simple entities are found. You can always drag-select or examine
146 ;; a pre-selected region.
147 ;;
148 ;; When forcing completion of method keywords, the initial
149 ;; query for a method has multiple entries for some methods. Would
150 ;; be too difficult to fix this hardly used case.
151 ;;
152 \f
153 ;;; Code:
154
155
156 (eval-when-compile (require 'cl))
157 (require 'idlw-help)
158
159 ;; For XEmacs
160 (unless (fboundp 'line-beginning-position)
161 (defalias 'line-beginning-position 'point-at-bol))
162 (unless (fboundp 'line-end-position)
163 (defalias 'line-end-position 'point-at-eol))
164 (unless (fboundp 'char-valid-p)
165 (defalias 'char-valid-p 'characterp))
166 (unless (fboundp 'match-string-no-properties)
167 (defalias 'match-string-no-properties 'match-string))
168
169 (if (not (fboundp 'cancel-timer))
170 (condition-case nil
171 (require 'timer)
172 (error nil)))
173
174 (eval-and-compile
175 ;; Kludge to allow `defcustom' for Emacs 19.
176 (condition-case () (require 'custom) (error nil))
177 (if (and (featurep 'custom) (fboundp 'custom-declare-variable))
178 nil ;; We've got what we needed
179 ;; We have the old or no custom-library, hack around it!
180 (defmacro defgroup (&rest args) nil)
181 (defmacro defcustom (var value doc &rest args)
182 `(defvar ,var ,value ,doc))))
183
184 (defgroup idlwave nil
185 "Major mode for editing IDL .pro files."
186 :tag "IDLWAVE"
187 :link '(url-link :tag "Home Page"
188 "http://idlwave.org")
189 :link '(emacs-commentary-link :tag "Commentary in idlw-shell.el"
190 "idlw-shell.el")
191 :link '(emacs-commentary-link :tag "Commentary in idlwave.el" "idlwave.el")
192 :link '(custom-manual "(idlwave)Top")
193 :prefix "idlwave"
194 :group 'languages)
195
196
197 ;;; Variables for indentation behavior ---------------------------------------
198
199 (defgroup idlwave-code-formatting nil
200 "Indentation and formatting options for IDLWAVE mode."
201 :group 'idlwave)
202
203 (defcustom idlwave-main-block-indent 2
204 "*Extra indentation for the main block of code.
205 That is the block between the FUNCTION/PRO statement and the END
206 statement for that program unit."
207 :group 'idlwave-code-formatting
208 :type 'integer)
209
210 (defcustom idlwave-block-indent 3
211 "*Extra indentation applied to block lines.
212 If you change this, you probably also want to change `idlwave-end-offset'."
213 :group 'idlwave-code-formatting
214 :type 'integer)
215
216 (defcustom idlwave-end-offset -3
217 "*Extra indentation applied to block END lines.
218 A value equal to negative `idlwave-block-indent' will make END lines
219 line up with the block BEGIN lines."
220 :group 'idlwave-code-formatting
221 :type 'integer)
222
223 (defcustom idlwave-continuation-indent 3
224 "*Extra indentation applied to continuation lines.
225 This extra offset applies to the first of a set of continuation lines.
226 The following lines receive the same indentation as the first."
227 :group 'idlwave-code-formatting
228 :type 'integer)
229
230 (defcustom idlwave-max-extra-continuation-indent 40
231 "*Maximum additional indentation for special continuation indent.
232 Several special indentations are tried to help line up continuation
233 lines in routine calls or definitions, other statements with
234 parentheses, or assignment statements. This variable specifies a
235 maximum amount by which this special indentation can exceed the
236 standard continuation indentation, otherwise defaulting to a fixed
237 offset. Set to 0 to effectively disable all special continuation
238 indentation, or to a large number (like 100) to enable it in all
239 cases. See also `idlwave-indent-to-open-paren', which can override
240 this variable."
241 :group 'idlwave-code-formatting
242 :type 'integer)
243
244 (defcustom idlwave-indent-to-open-paren t
245 "*Non-nil means, indent continuation lines to innermost open
246 parenthesis. This indentation occurs even if otherwise disallowed by
247 `idlwave-max-extra-continuation-indent'. Matching parens and the
248 interleaving args are lined up. Example:
249
250 x = function_a(function_b(function_c( a, b, [1,2,3, $
251 4,5,6 $
252 ], $
253 c, d $
254 )))
255
256 When this variable is nil, paren alignment may still occur, based on
257 the value of `max-extra-continuation-indent', which, if zero, would
258 yield:
259
260 x = function_a(function_b(function_c( a, b, [1,2,3, $
261 4,5,6 $
262 ], $
263 c, d $
264 )))"
265 :group 'idlwave-code-formatting
266 :type 'boolean)
267
268 (defcustom idlwave-indent-parens-nested nil
269 "*Non-nil means, indent continuation lines with parens by nesting
270 lines at consecutively deeper levels."
271 :group 'idlwave-code-formatting
272 :type 'boolean)
273
274
275 (defcustom idlwave-hanging-indent t
276 "*If set non-nil then comment paragraphs are indented under the
277 hanging indent given by `idlwave-hang-indent-regexp' match in the first line
278 of the paragraph."
279 :group 'idlwave-code-formatting
280 :type 'boolean)
281
282 (defcustom idlwave-hang-indent-regexp "- "
283 "*Regular expression matching the position of the hanging indent
284 in the first line of a comment paragraph. The size of the indent
285 extends to the end of the match for the regular expression."
286 :group 'idlwave-code-formatting
287 :type 'regexp)
288
289 (defcustom idlwave-use-last-hang-indent nil
290 "*If non-nil then use last match on line for `idlwave-indent-regexp'."
291 :group 'idlwave-code-formatting
292 :type 'boolean)
293
294 (defcustom idlwave-fill-comment-line-only t
295 "*If non-nil then auto fill will only operate on comment lines."
296 :group 'idlwave-code-formatting
297 :type 'boolean)
298
299 (defcustom idlwave-auto-fill-split-string t
300 "*If non-nil then auto fill will split strings with the IDL `+' operator.
301 When the line end falls within a string, string concatenation with the
302 '+' operator will be used to distribute a long string over lines.
303 If nil and a string is split then a terminal beep and warning are issued.
304
305 This variable is ignored when `idlwave-fill-comment-line-only' is
306 non-nil, since in this case code is not auto-filled."
307 :group 'idlwave-code-formatting
308 :type 'boolean)
309
310 (defcustom idlwave-split-line-string t
311 "*If non-nil then `idlwave-split-line' will split strings with `+'.
312 When the splitting point of a line falls inside a string, split the string
313 using the `+' string concatenation operator. If nil and a string is
314 split then a terminal beep and warning are issued."
315 :group 'idlwave-code-formatting
316 :type 'boolean)
317
318 (defcustom idlwave-no-change-comment ";;;"
319 "*The indentation of a comment that starts with this regular
320 expression will not be changed. Note that the indentation of a comment
321 at the beginning of a line is never changed."
322 :group 'idlwave-code-formatting
323 :type 'string)
324
325 (defcustom idlwave-begin-line-comment nil
326 "*A comment anchored at the beginning of line.
327 A comment matching this regular expression will not have its
328 indentation changed. If nil the default is \"^;\", i.e., any line
329 beginning with a \";\". Expressions for comments at the beginning of
330 the line should begin with \"^\"."
331 :group 'idlwave-code-formatting
332 :type '(choice (const :tag "Any line beginning with `;'" nil)
333 'regexp))
334
335 (defcustom idlwave-code-comment ";;[^;]"
336 "*A comment that starts with this regular expression on a line by
337 itself is indented as if it is a part of IDL code. As a result if
338 the comment is not preceded by whitespace it is unchanged."
339 :group 'idlwave-code-formatting
340 :type 'regexp)
341
342 ;; Comments not matching any of the above will be indented as a
343 ;; right-margin comment, i.e., to a minimum of `comment-column'.
344
345 ;;; Routine Info and Completion ---------------------------------------
346
347 (defgroup idlwave-routine-info nil
348 "Routine Info options for IDLWAVE mode."
349 :group 'idlwave)
350
351 (defcustom idlwave-use-library-catalogs t
352 "*Non-nil means search the IDL path for library catalog files.
353
354 These files, named .idlwave_catalog, document routine information for
355 individual directories and libraries of IDL .pro files. Many popular
356 libraries come with catalog files by default, so leaving this on is a
357 usually a good idea.."
358 :group 'idlwave-routine-info
359 :type 'boolean)
360
361 (defcustom idlwave-init-rinfo-when-idle-after 10
362 "*Seconds of idle time before routine info is automatically
363 initialized. Initializing the routine info can take a long time, in
364 particular if a large number of library catalogs are involved. When
365 Emacs is idle for more than the number of seconds specified by this
366 variable, it starts the initialization. The process is split into
367 five steps, in order to keep work interruption as short as possible.
368 If one of the steps finishes, and no user input has arrived in the
369 mean time, initialization proceeds immediately to the next step. A
370 good value for this variable is about 1/3 of the time initialization
371 take in your setup. So if you have a fast machine and no problems
372 with a slow network connection, don't hesitate to set this to 2
373 seconds. A Value of 0 means, don't initialize automatically, but
374 instead wait until routine information is needed, and initialize
375 then."
376 :group 'idlwave-routine-info
377 :type 'number)
378
379 (defcustom idlwave-scan-all-buffers-for-routine-info t
380 "*Non-nil means, scan buffers for IDL programs when updating info.
381 The scanning is done by the command `idlwave-update-routine-info'.
382 The following values are allowed:
383
384 nil Don't scan any buffers.
385 t Scan all idlwave-mode buffers in the current editing session.
386 current Scan only the current buffer, but no other buffers."
387 :group 'idlwave-routine-info
388 :type '(choice
389 (const :tag "No buffer" nil)
390 (const :tag "All buffers" t)
391 (const :tag "Current buffer only" 'current)))
392
393 (defcustom idlwave-query-shell-for-routine-info t
394 "*Non-nil means query the shell for info about compiled routines.
395 Querying the shell is useful to get information about compiled modules,
396 and it is turned on by default. However, when you have a complete library
397 scan, this is not necessary."
398 :group 'idlwave-routine-info
399 :type 'boolean)
400
401 (defcustom idlwave-auto-routine-info-updates
402 '(find-file save-buffer kill-buffer compile-buffer)
403 "*Controls under what circumstances routine info is updated automatically.
404 Possible values:
405 nil Never
406 t All available
407 \(...) A list of circumstances. Allowed members are:
408 find-file Add info for new IDLWAVE buffers.
409 save-buffer Update buffer info when buffer is saved
410 kill-buffer Remove buffer info when buffer gets killed
411 compile-buffer Update shell info after `idlwave-shell-save-and...'"
412 :group 'idlwave-routine-info
413 :type '(choice
414 (const :tag "Never" nil)
415 (const :tag "As often as possible" t)
416 (set :tag "Checklist" :greedy t
417 (const :tag "When visiting a file" find-file)
418 (const :tag "When saving a buffer" save-buffer)
419 (const :tag "After a buffer was killed" kill-buffer)
420 (const :tag "After a buffer was compiled successfully, update shell info" compile-buffer))))
421
422 (defcustom idlwave-rinfo-max-source-lines 5
423 "*Maximum number of source files displayed in the Routine Info window.
424 When an integer, it is the maximum number of source files displayed.
425 t means to show all source files."
426 :group 'idlwave-routine-info
427 :type 'integer)
428
429 (defcustom idlwave-library-path nil
430 "Library path for Windows and MacOS (OS9). Not needed under UNIX.
431 When selecting the directories to scan for IDL user catalog routine
432 info, IDLWAVE can, under UNIX, query the shell for the exact search
433 path \(the value of !PATH). However, under Windows and MacOS
434 \(pre-OSX), the IDLWAVE shell does not work. In this case, this
435 variable can be set to specify the paths where IDLWAVE can find PRO
436 files. The shell will only be asked for a list of paths when this
437 variable is nil. The value is a list of directories. A directory
438 preceeded by a `+' will be searched recursively. If you set this
439 variable on a UNIX system, the shell will not be queried. See also
440 `idlwave-system-directory'."
441 :group 'idlwave-routine-info
442 :type '(repeat (directory)))
443
444 (defcustom idlwave-system-directory ""
445 "The IDL system directory for Windows and MacOS. Not needed under
446 UNIX. Set this to the value of the `!DIR' system variable in IDL.
447 IDLWAVE uses this to find out which of the library routines belong to
448 the official system library. All files inside the `lib' subdirectory
449 are considered system library files - so don't install private stuff
450 in this directory. On UNIX systems, IDLWAVE queries the shell for the
451 value of `!DIR'. See also `idlwave-library-path'."
452 :group 'idlwave-routine-info
453 :type 'directory)
454
455 ;; Configuration files
456 (defcustom idlwave-config-directory
457 (convert-standard-filename "~/.idlwave")
458 "*Directory for configuration files and user-library catalog."
459 :group 'idlwave-routine-info
460 :type 'file)
461
462 (defvar idlwave-user-catalog-file "idlusercat.el")
463 (defvar idlwave-xml-system-rinfo-converted-file "idl_xml_rinfo.el")
464 (defvar idlwave-path-file "idlpath.el")
465
466 (defvar idlwave-libinfo-file nil
467 "*Obsolete variable, no longer used.")
468
469 (defcustom idlwave-special-lib-alist nil
470 "Alist of regular expressions matching special library directories.
471 When listing routine source locations, IDLWAVE gives a short hint where
472 the file defining the routine is located. By default it lists `SystemLib'
473 for routines in the system library `!DIR/lib' and `Library' for anything
474 else. This variable can define additional types. The car of each entry
475 is a regular expression matching the file name (they normally will match
476 on the path). The cdr is the string to be used as identifier. Max 10
477 chars are allowed."
478 :group 'idlwave-routine-info
479 :type '(repeat
480 (cons regexp string)))
481
482 (defcustom idlwave-auto-write-paths t
483 "Write out path (!PATH) and system directory (!DIR) info automatically.
484 Path info is needed to locate library catalog files. If non-nil,
485 whenever the path-list changes as a result of shell-query, etc., it is
486 written to file. Otherwise, the menu option \"Write Paths\" can be
487 used to force a write."
488 :group 'idlwave-routine-info
489 :type 'boolean)
490
491 (defgroup idlwave-completion nil
492 "Completion options for IDLWAVE mode."
493 :prefix "idlwave"
494 :group 'idlwave)
495
496 (eval-and-compile
497 (defconst idlwave-tmp
498 '(choice :tag "by applying the function"
499 (const upcase)
500 (const downcase)
501 (const capitalize)
502 (const preserve)
503 (symbol :tag "Other"))))
504
505 (defcustom idlwave-completion-case '((routine . upcase)
506 (keyword . upcase)
507 (class . preserve)
508 (method . preserve))
509 "Association list setting the case of completed words.
510
511 This variable determines the case (UPPER/lower/Capitalized...) of
512 words inserted into the buffer by completion. The preferred case can
513 be specified separately for routine names, keywords, classes and
514 methods.
515 This alist should therefore have entries for `routine' (normal
516 functions and procedures, i.e. non-methods), `keyword', `class', and
517 `method'. Plausible values are
518
519 upcase upcase whole word, like `BOX_CURSOR'
520 downcase downcase whole word, like `read_ppm'
521 capitalize capitalize each part, like `Widget_Control'
522 preserve preserve case as is, like `IDLgrView'
523
524 The value can also be any Emacs Lisp function which transforms the
525 case of characters in a string.
526
527 A value of `preserve' means that the case of the completed word is
528 identical to the way it was written in the definition statement of the
529 routine. This was implemented to allow for mixed-case completion, in
530 particular of object classes and methods.
531 If a completable word is defined in multiple locations, the meaning of
532 `preserve' is not unique since the different definitions might be
533 cased differently. Therefore IDLWAVE always takes the case of the
534 *first* definition it encounters during routine info collection and
535 uses the case derived from it consistently.
536
537 Note that a lowercase-only string in the buffer will always be completed in
538 lower case (but see the variable `idlwave-completion-force-default-case').
539
540 After changing this variable, you need to either restart Emacs or press
541 `C-u C-c C-i' to update the internal lists."
542 :group 'idlwave-completion
543 :type `(repeat
544 (cons (symbol :tag "Derive completion case for")
545 ,idlwave-tmp)))
546
547 (defcustom idlwave-completion-force-default-case nil
548 "*Non-nil means, completion will always honor `idlwave-completion-case'.
549 When nil, only the completion of a mixed case or upper case string
550 will honor the default settings in `idlwave-completion-case', while
551 the completion of lower case strings will be completed entirely in
552 lower case."
553 :group 'idlwave-completion
554 :type 'boolean)
555
556 (defcustom idlwave-complete-empty-string-as-lower-case nil
557 "*Non-nil means, the empty string is considered downcase for completion.
558 The case of what is already in the buffer determines the case of completions.
559 When this variable is non-nil, the empty string is considered to be downcase.
560 Completing on the empty string then offers downcase versions of the possible
561 completions."
562 :group 'idlwave-completion
563 :type 'boolean)
564
565 (defvar idlwave-default-completion-case-is-down nil
566 "Obsolete variable. See `idlwave-complete-empty-string-as-lower-case' and
567 `idlwave-completion-case'.")
568
569 (defcustom idlwave-buffer-case-takes-precedence nil
570 "*Non-nil means, the case of tokens in buffers dominates over system stuff.
571 To make this possible, we need to re-case everything each time we update
572 the routine info from the buffers. This is slow.
573 The default is to consider the case given in the system and library files
574 first which makes updating much faster."
575 :group 'idlwave-completion
576 :type 'boolean)
577
578 (defcustom idlwave-highlight-help-links-in-completion t
579 "*Non-nil means, highlight completions for which system help is available.
580 Help can then be accessed with mouse-3.
581 This option is only effective when the online help system is installed."
582 :group 'idlwave-completion
583 :type 'boolean)
584
585 (defcustom idlwave-support-inheritance t
586 "Non-nil means, treat inheritance with completion, online help etc.
587 When nil, IDLWAVE only knows about the native methods and tags of a class,
588 not about inherited ones."
589 :group 'idlwave-routine-info
590 :type 'boolean)
591
592 (defcustom idlwave-keyword-class-inheritance '("^[gs]etproperty$" "^init$")
593 "List of regular expressions for class-driven keyword inheritance.
594 Keyword inheritance is often tied to class inheritance by \"chaining\"
595 up the class tree. While it cannot be assumed that the presence of an
596 _EXTRA or _REF_EXTRA symbol guarantees such chaining will occur, for
597 certain methods this assumption is almost always true. The methods
598 for which to assume this can be set here."
599 :group 'idlwave-routine-info
600 :type '(repeat (regexp :tag "Match method:")))
601
602
603 (defcustom idlwave-completion-show-classes 1
604 "*Number of classes to show when completing object methods and keywords.
605 When completing methods or keywords for an object with unknown class,
606 the *Completions* buffer will show the valid classes for each completion
607 like this:
608
609 MyMethod <Class1,Class2,Class3>
610
611 The value of this variable may be nil to inhibit display, or an integer to
612 indicate the maximum number of classes to display.
613
614 On XEmacs, a full list of classes will also be placed into a `help-echo'
615 property on the competion items, so that the list of classes for the current
616 item is displayed in the echo area. If the value of this variable is a
617 negative integer, the `help-echo' property will be suppressed."
618 :group 'idlwave-completion
619 :type '(choice (const :tag "Don't show" nil)
620 (integer :tag "Number of classes shown" 1)))
621
622 (defcustom idlwave-completion-fontify-classes t
623 "*Non-nil means, fontify the classes in completions buffer.
624 This makes it easier to distinguish the completion items from the extra
625 class info listed. See `idlwave-completion-show-classes'."
626 :group 'idlwave-completion
627 :type 'boolean)
628
629 (defcustom idlwave-query-class '((method-default . nil)
630 (keyword-default . nil))
631 "Association list governing specification of object classes for completion.
632
633 When IDLWAVE tries to complete object-oriented methods, it usually
634 cannot determine the class of a given object from context. In order
635 to provide the user with a correct list of methods or keywords, it
636 needs to determine the appropriate class. IDLWAVE has two ways of
637 doing this (well, three ways if you count the shell... see
638 `idlwave-shell-query-for-class'):
639
640 1. Combine the items of all available classes which contain this
641 method for the purpose of completion. So when completing a method,
642 all methods of all known classes are available, and when completing
643 a keyword, all keywords allowed for this method in any class are
644 shown. This behavior is very much like normal completion and is
645 therefore the default. It works much better than one might think -
646 only for the INIT, GETPROPERTY and SETPROPERTY the keyword lists
647 become uncomfortably long. See also
648 `idlwave-completion-show-classes'.
649
650 2. The second possibility is to ask the user on each occasion. To
651 make this less interruptive, IDLWAVE can store the class as a text
652 property on the object operator `->'. For a given object in the
653 source code, class selection will then be needed only once
654 - for example to complete the method. Keywords to the method can
655 then be completed directly, because the class is already known.
656 You will have to turn on the storage of the selected class
657 explicitly with the variable `idlwave-store-inquired-class'.
658
659 This variable allows you to configure IDLWAVE's method and
660 method-keyword completion behavior. Its value is an alist, which
661 should contain at least two elements: (method-default . VALUE) and
662 \(keyword-default . VALUE), where VALUE is either t or nil. These
663 specify if the class should be found during method and keyword
664 completion, respectively.
665
666 The alist may have additional entries specifying exceptions from the
667 keyword completion rule for specific methods, like INIT or
668 GETPROPERTY. In order to turn on class specification for the INIT
669 method, add an entry (\"INIT\" . t). The method name must be ALL-CAPS."
670 :group 'idlwave-completion
671 :type '(list
672 (cons (const method-default)
673 (boolean :tag "Determine class when completing METHODS "))
674 (cons (const keyword-default)
675 (boolean :tag "Determine class when completing KEYWORDS "))
676 (repeat
677 :tag "Exceptions to defaults"
678 :inline t
679 (cons (string :tag "MODULE" :value "")
680 (boolean :tag "Determine class for this method")))))
681
682 (defcustom idlwave-store-inquired-class t
683 "*Non-nil means, store class of a method call as text property on `->'.
684 IDLWAVE sometimes has to ask the user for the class associated with a
685 particular object method call. This happens during the commands
686 `idlwave-routine-info' and `idlwave-complete', depending upon the
687 value of the variable `idlwave-query-class'.
688
689 When you specify a class, this information can be stored as a text
690 property on the `->' arrow in the source code, so that during the same
691 editing session, IDLWAVE will not have to ask again. When this
692 variable is non-nil, IDLWAVE will store and reuse the class information.
693 The class stored can be checked and removed with `\\[idlwave-routine-info]'
694 on the arrow.
695
696 The default of this variable is nil, since the result of commands then
697 is more predictable. However, if you know what you are doing, it can
698 be nice to turn this on.
699
700 An arrow which knows the class will be highlighted with
701 `idlwave-class-arrow-face'. The command \\[idlwave-routine-info]
702 displays (with prefix arg: deletes) the class stored on the arrow
703 at point."
704 :group 'idlwave-completion
705 :type 'boolean)
706
707 (defcustom idlwave-class-arrow-face 'bold
708 "*Face to highlight object operator arrows `->' which carry a class property.
709 When IDLWAVE stores a class name as text property on an object arrow
710 \(see variable `idlwave-store-inquired-class', it highlights the arrow
711 with this font in order to remind the user that this arrow is special."
712 :group 'idlwave-completion
713 :type 'symbol)
714
715 (defcustom idlwave-resize-routine-help-window t
716 "*Non-nil means, resize the Routine-info *Help* window to fit the content."
717 :group 'idlwave-completion
718 :type 'boolean)
719
720 (defcustom idlwave-keyword-completion-adds-equal t
721 "*Non-nil means, completion automatically adds `=' after completed keywords."
722 :group 'idlwave-completion
723 :type 'boolean)
724
725 (defcustom idlwave-function-completion-adds-paren t
726 "*Non-nil means, completion automatically adds `(' after completed function.
727 nil means, don't add anything.
728 A value of `2' means, also add the closing parenthesis and position cursor
729 between the two."
730 :group 'idlwave-completion
731 :type '(choice (const :tag "Nothing" nil)
732 (const :tag "(" t)
733 (const :tag "()" 2)))
734
735 (defcustom idlwave-completion-restore-window-configuration t
736 "*Non-nil means, try to restore the window configuration after completion.
737 When completion is not unique, Emacs displays a list of completions.
738 This messes up your window configuration. With this variable set, IDLWAVE
739 restores the old configuration after successful completion."
740 :group 'idlwave-completion
741 :type 'boolean)
742
743 ;;; Variables for abbrev and action behavior -----------------------------
744
745 (defgroup idlwave-abbrev-and-indent-action nil
746 "IDLWAVE performs actions when expanding abbreviations or indenting lines.
747 The variables in this group govern this."
748 :group 'idlwave)
749
750 (defcustom idlwave-do-actions nil
751 "*Non-nil means performs actions when indenting.
752 The actions that can be performed are listed in `idlwave-indent-action-table'."
753 :group 'idlwave-abbrev-and-indent-action
754 :type 'boolean)
755
756 (defcustom idlwave-abbrev-start-char "\\"
757 "*A single character string used to start abbreviations in abbrev mode.
758 Possible characters to chose from: ~`\%
759 or even '?'. '.' is not a good choice because it can make structure
760 field names act like abbrevs in certain circumstances.
761
762 Changes to this in `idlwave-mode-hook' will have no effect. Instead a user
763 must set it directly using `setq' in the .emacs file before idlwave.el
764 is loaded."
765 :group 'idlwave-abbrev-and-indent-action
766 :type 'string)
767
768 (defcustom idlwave-surround-by-blank nil
769 "*Non-nil means, enable `idlwave-surround'.
770 If non-nil, `=',`<',`>',`&',`,', `->' are surrounded with spaces by
771 `idlwave-surround'.
772 See help for `idlwave-indent-action-table' for symbols using `idlwave-surround'.
773
774 Also see the default key bindings for keys using `idlwave-surround'.
775 Keys are bound and made into actions calling `idlwave-surround' with
776 `idlwave-action-and-binding'.
777 See help for `idlwave-action-and-binding' for examples.
778
779 Also see help for `idlwave-surround'."
780 :group 'idlwave-abbrev-and-indent-action
781 :type 'boolean)
782
783 (defcustom idlwave-pad-keyword t
784 "*Non-nil means pad '=' in keywords (routine calls or defs) like assignment.
785 Whenever `idlwave-surround' is non-nil then this affects how '=' is
786 padded for keywords and for variables. If t, pad the same as for
787 assignments. If nil then spaces are removed. With any other value,
788 spaces are left unchanged."
789 :group 'idlwave-abbrev-and-indent-action
790 :type '(choice
791 (const :tag "Pad like assignments" t)
792 (const :tag "Remove space near `='" nil)
793 (const :tag "Keep space near `='" 'keep)))
794
795 (defcustom idlwave-show-block t
796 "*Non-nil means point blinks to block beginning for `idlwave-show-begin'."
797 :group 'idlwave-abbrev-and-indent-action
798 :type 'boolean)
799
800 (defcustom idlwave-expand-generic-end nil
801 "*Non-nil means expand generic END to ENDIF/ENDELSE/ENDWHILE etc."
802 :group 'idlwave-abbrev-and-indent-action
803 :type 'boolean)
804
805 (defcustom idlwave-reindent-end t
806 "*Non-nil means re-indent line after END was typed."
807 :group 'idlwave-abbrev-and-indent-action
808 :type 'boolean)
809
810 (defcustom idlwave-abbrev-move t
811 "*Non-nil means the abbrev hook can move point.
812 Set to nil by `idlwave-expand-region-abbrevs'. To see the abbrev
813 definitions, use the command `list-abbrevs', for abbrevs that move
814 point. Moving point is useful, for example, to place point between
815 parentheses of expanded functions.
816
817 See `idlwave-check-abbrev'."
818 :group 'idlwave-abbrev-and-indent-action
819 :type 'boolean)
820
821 (defcustom idlwave-abbrev-change-case nil
822 "*Non-nil means all abbrevs will be forced to either upper or lower case.
823 If the value t, all expanded abbrevs will be upper case.
824 If the value is 'down then abbrevs will be forced to lower case.
825 If nil, the case will not change.
826 If `idlwave-reserved-word-upcase' is non-nil, reserved words will always be
827 upper case, regardless of this variable."
828 :group 'idlwave-abbrev-and-indent-action
829 :type 'boolean)
830
831 (defcustom idlwave-reserved-word-upcase nil
832 "*Non-nil means, reserved words will be made upper case via abbrev expansion.
833 If nil case of reserved words is controlled by `idlwave-abbrev-change-case'.
834 Has effect only if in abbrev-mode."
835 :group 'idlwave-abbrev-and-indent-action
836 :type 'boolean)
837
838 ;;; Action/Expand Tables.
839 ;;
840 ;; The average user may have difficulty modifying this directly. It
841 ;; can be modified/set in idlwave-mode-hook, but it is easier to use
842 ;; idlwave-action-and-binding. See help for idlwave-action-and-binding for
843 ;; examples of how to add an action.
844 ;;
845 ;; The action table is used by `idlwave-indent-line' whereas both the
846 ;; action and expand tables are used by `idlwave-indent-and-action'. In
847 ;; general, the expand table is only used when a line is explicitly
848 ;; indented. Whereas, in addition to being used when the expand table
849 ;; is used, the action table is used when a line is indirectly
850 ;; indented via line splitting, auto-filling or a new line creation.
851 ;;
852 ;; Example actions:
853 ;;
854 ;; Capitalize system vars
855 ;; (idlwave-action-and-binding idlwave-sysvar '(capitalize-word 1) t)
856 ;;
857 ;; Capitalize procedure name
858 ;; (idlwave-action-and-binding "\\<\\(pro\\|function\\)\\>[ \t]*\\<"
859 ;; '(capitalize-word 1) t)
860 ;;
861 ;; Capitalize common block name
862 ;; (idlwave-action-and-binding "\\<common\\>[ \t]+\\<"
863 ;; '(capitalize-word 1) t)
864 ;; Capitalize label
865 ;; (idlwave-action-and-binding (concat "^[ \t]*" idlwave-label)
866 ;; '(capitalize-word -1) t)
867
868 (defvar idlwave-indent-action-table nil
869 "*Associated array containing action lists of search string (car),
870 and function as a cdr. This table is used by `idlwave-indent-line'.
871 See documentation for `idlwave-do-action' for a complete description of
872 the action lists.
873
874 Additions to the table are made with `idlwave-action-and-binding' when a
875 binding is not requested.
876 See help on `idlwave-action-and-binding' for examples.")
877
878 (defvar idlwave-indent-expand-table nil
879 "*Associated array containing action lists of search string (car),
880 and function as a cdr. The table is used by the
881 `idlwave-indent-and-action' function. See documentation for
882 `idlwave-do-action' for a complete description of the action lists.
883
884 Additions to the table are made with `idlwave-action-and-binding' when a
885 binding is requested.
886 See help on `idlwave-action-and-binding' for examples.")
887
888 ;;; Documentation header and history keyword ---------------------------------
889
890 (defgroup idlwave-documentation nil
891 "Options for documenting IDLWAVE files."
892 :group 'idlwave)
893
894 ;; FIXME: make defcustom?
895 (defvar idlwave-file-header
896 (list nil
897 ";+
898 ; NAME:
899 ;
900 ;
901 ;
902 ; PURPOSE:
903 ;
904 ;
905 ;
906 ; CATEGORY:
907 ;
908 ;
909 ;
910 ; CALLING SEQUENCE:
911 ;
912 ;
913 ;
914 ; INPUTS:
915 ;
916 ;
917 ;
918 ; OPTIONAL INPUTS:
919 ;
920 ;
921 ;
922 ; KEYWORD PARAMETERS:
923 ;
924 ;
925 ;
926 ; OUTPUTS:
927 ;
928 ;
929 ;
930 ; OPTIONAL OUTPUTS:
931 ;
932 ;
933 ;
934 ; COMMON BLOCKS:
935 ;
936 ;
937 ;
938 ; SIDE EFFECTS:
939 ;
940 ;
941 ;
942 ; RESTRICTIONS:
943 ;
944 ;
945 ;
946 ; PROCEDURE:
947 ;
948 ;
949 ;
950 ; EXAMPLE:
951 ;
952 ;
953 ;
954 ; MODIFICATION HISTORY:
955 ;
956 ;-
957 ")
958 "*A list (PATHNAME STRING) specifying the doc-header template to use for
959 summarizing a file. If PATHNAME is non-nil then this file will be included.
960 Otherwise STRING is used. If nil, the file summary will be omitted.
961 For example you might set PATHNAME to the path for the
962 lib_template.pro file included in the IDL distribution.")
963
964 (defcustom idlwave-header-to-beginning-of-file t
965 "*Non-nil means, the documentation header will always be at start of file.
966 When nil, the header is positioned between the PRO/FUNCTION line of
967 the current routine and the code, allowing several routine headers in
968 a file."
969 :group 'idlwave-documentation
970 :type 'boolean)
971
972 (defcustom idlwave-timestamp-hook 'idlwave-default-insert-timestamp
973 "*The hook function used to update the timestamp of a function."
974 :group 'idlwave-documentation
975 :type 'function)
976
977 (defcustom idlwave-doc-modifications-keyword "HISTORY"
978 "*The modifications keyword to use with the log documentation commands.
979 A ':' is added to the keyword end.
980 Inserted by doc-header and used to position logs by doc-modification.
981 If nil it will not be inserted."
982 :group 'idlwave-documentation
983 :type 'string)
984
985 (defcustom idlwave-doclib-start "^;+\\+"
986 "*Regexp matching the start of a document library header."
987 :group 'idlwave-documentation
988 :type 'regexp)
989
990 (defcustom idlwave-doclib-end "^;+-"
991 "*Regexp matching the end of a document library header."
992 :group 'idlwave-documentation
993 :type 'regexp)
994
995 ;;; External Programs -------------------------------------------------------
996
997 (defgroup idlwave-external-programs nil
998 "Path locations of external commands used by IDLWAVE."
999 :group 'idlwave)
1000
1001 (defcustom idlwave-shell-explicit-file-name "idl"
1002 "*If non-nil, this is the command to run IDL.
1003 Should be an absolute file path or path relative to the current environment
1004 execution search path. If you want to specify command line switches
1005 for the idl program, use `idlwave-shell-command-line-options'.
1006
1007 I know the name of this variable is badly chosen, but I cannot change
1008 it without compromizing backwards-compatibility."
1009 :group 'idlwave-external-programs
1010 :type 'string)
1011
1012 (defcustom idlwave-shell-command-line-options nil
1013 "*A list of command line options for calling the IDL program.
1014 Since IDL is executed directly without going through a shell like /bin/sh,
1015 this should be a list of strings like '(\"-rt=file\" \"-nw\") with a separate
1016 string for each argument. But you may also give a single string which
1017 contains the options whitespace-separated. Emacs will be kind enough to
1018 split it for you."
1019 :type '(choice
1020 string
1021 (repeat (string :value "")))
1022 :group 'idlwave-external-programs)
1023
1024 (defcustom idlwave-help-application "idlhelp"
1025 "*The external application providing reference help for programming.
1026 Obsolete, if the IDL Assistant is being used for help."
1027 :group 'idlwave-external-programs
1028 :type 'string)
1029
1030 ;;; Some Shell variables which must be defined here.-----------------------
1031
1032 (defcustom idlwave-shell-debug-modifiers '()
1033 "List of modifiers to be used for the debugging commands.
1034 Will be used to bind debugging commands in the shell buffer and in all
1035 source buffers. These are additional convenience bindings, the debugging
1036 commands are always available with the `C-c C-d' prefix.
1037 If you set this to '(control shift), this means setting a breakpoint will
1038 be on `C-S-b', compiling a source file on `C-S-c' etc. Possible modifiers
1039 are `control', `meta', `super', `hyper', `alt', and `shift'."
1040 :group 'idlwave-shell-general-setup
1041 :type '(set :tag "Specify modifiers"
1042 (const control)
1043 (const meta)
1044 (const super)
1045 (const hyper)
1046 (const alt)
1047 (const shift)))
1048
1049 (defcustom idlwave-shell-automatic-start nil
1050 "*If non-nil attempt invoke idlwave-shell if not already running.
1051 This is checked when an attempt to send a command to an
1052 IDL process is made."
1053 :group 'idlwave-shell-general-setup
1054 :type 'boolean)
1055
1056 ;;; Miscellaneous variables -------------------------------------------------
1057
1058 (defgroup idlwave-misc nil
1059 "Miscellaneous options for IDLWAVE mode."
1060 :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
1061 :group 'idlwave)
1062
1063 (defcustom idlwave-startup-message t
1064 "*Non-nil displays a startup message when `idlwave-mode' is first called."
1065 :group 'idlwave-misc
1066 :type 'boolean)
1067
1068 (defcustom idlwave-default-font-lock-items
1069 '(pros-and-functions batch-files idlwave-idl-keywords label goto
1070 common-blocks class-arrows)
1071 "Items which should be fontified on the default fontification level 2.
1072 IDLWAVE defines 3 levels of fontification. Level 1 is very little, level 3
1073 is everything and level 2 is specified by this list.
1074 This variable must be set before IDLWAVE gets loaded. It is
1075 a list of symbols, the following symbols are allowed.
1076
1077 pros-and-functions Procedure and Function definitions
1078 batch-files Batch Files
1079 idlwave-idl-keywords IDL Keywords
1080 label Statement Labels
1081 goto Goto Statements
1082 common-blocks Common Blocks
1083 keyword-parameters Keyword Parameters in routine definitions and calls
1084 system-variables System Variables
1085 fixme FIXME: Warning in comments (on XEmacs only v. 21.0 and up)
1086 class-arrows Object Arrows with class property"
1087 :group 'idlwave-misc
1088 :type '(set
1089 :inline t :greedy t
1090 (const :tag "Procedure and Function definitions" pros-and-functions)
1091 (const :tag "Batch Files" batch-files)
1092 (const :tag "IDL Keywords (reserved words)" idlwave-idl-keywords)
1093 (const :tag "Statement Labels" label)
1094 (const :tag "Goto Statements" goto)
1095 (const :tag "Tags in Structure Definition" structtag)
1096 (const :tag "Structure Name" structname)
1097 (const :tag "Common Blocks" common-blocks)
1098 (const :tag "Keyword Parameters" keyword-parameters)
1099 (const :tag "System Variables" system-variables)
1100 (const :tag "FIXME: Warning" fixme)
1101 (const :tag "Object Arrows with class property " class-arrows)))
1102
1103 (defcustom idlwave-mode-hook nil
1104 "Normal hook. Executed when a buffer is put into `idlwave-mode'."
1105 :group 'idlwave-misc
1106 :type 'hook)
1107
1108 (defcustom idlwave-load-hook nil
1109 "Normal hook. Executed when idlwave.el is loaded."
1110 :group 'idlwave-misc
1111 :type 'hook)
1112
1113 (defvar idlwave-experimental nil
1114 "Non-nil means turn on a few experimental features.
1115 This variable is only for the maintainer, to test difficult stuff,
1116 while still distributing stable releases.
1117 As a user, you should not set this to t.")
1118
1119 ;;;
1120 ;;; End customization variables section
1121 ;;;
1122
1123 ;;; Non customization variables
1124
1125 ;;; font-lock mode - Additions by Phil Williams, Ulrik Dickow and
1126 ;;; Simon Marshall <simon_at_gnu.ai.mit.edu>
1127 ;;; and Carsten Dominik...
1128
1129 ;; The following are the reserved words in IDL. Maybe we should
1130 ;; highlight some more stuff as well?
1131 ;; Procedure declarations. Fontify keyword plus procedure name.
1132 (defvar idlwave-idl-keywords
1133 ;; To update this regexp, update the list of keywords and
1134 ;; evaluate the form.
1135 ;; (insert
1136 ;; (prin1-to-string
1137 ;; (concat
1138 ;; "\\<\\("
1139 ;; (regexp-opt
1140 ;; '("||" "&&" "and" "or" "xor" "not"
1141 ;; "eq" "ge" "gt" "le" "lt" "ne"
1142 ;; "for" "do" "endfor"
1143 ;; "if" "then" "endif" "else" "endelse"
1144 ;; "case" "of" "endcase"
1145 ;; "switch" "break" "continue" "endswitch"
1146 ;; "begin" "end"
1147 ;; "repeat" "until" "endrep"
1148 ;; "while" "endwhile"
1149 ;; "goto" "return"
1150 ;; "inherits" "mod"
1151 ;; "compile_opt" "forward_function"
1152 ;; "on_error" "on_ioerror")) ; on_error is not officially reserved
1153 ;; "\\)\\>")))
1154 "\\<\\(&&\\|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\\|||\\)\\>")
1155
1156
1157 (let* (;; Procedure declarations. Fontify keyword plus procedure name.
1158 ;; Function declarations. Fontify keyword plus function name.
1159 (pros-and-functions
1160 '("\\<\\(function\\|pro\\)\\>[ \t]+\\(\\sw+\\(::\\sw+\\)?\\)"
1161 (1 font-lock-keyword-face)
1162 (2 font-lock-function-name-face nil t)))
1163
1164 ;; Common blocks
1165 (common-blocks
1166 '("\\<\\(common\\)\\>[ \t]*\\(\\sw+\\)?[ \t]*,?"
1167 (1 font-lock-keyword-face) ; "common"
1168 (2 font-lock-reference-face nil t) ; block name
1169 ("[ \t]*\\(\\sw+\\)[ ,]*"
1170 ;; Start with point after block name and comma
1171 (goto-char (match-end 0)) ; needed for XEmacs, could be nil
1172 nil
1173 (1 font-lock-variable-name-face) ; variable names
1174 )))
1175
1176 ;; Batch files
1177 (batch-files
1178 '("^[ \t]*\\(@[^ \t\n]+\\)" (1 font-lock-string-face)))
1179
1180 ;; FIXME warning.
1181 (fixme
1182 '("\\<FIXME:" (0 font-lock-warning-face t)))
1183
1184 ;; Labels
1185 (label
1186 '("^[ \t]*\\([a-zA-Z]\\sw*:\\)" (1 font-lock-reference-face)))
1187
1188 ;; The goto statement and its label
1189 (goto
1190 '("\\(goto\\)[ \t]*,[ \t]*\\([a-zA-Z]\\sw*\\)"
1191 (1 font-lock-keyword-face)
1192 (2 font-lock-reference-face)))
1193
1194 ;; Tags in structure definitions. Note that this definition
1195 ;; actually collides with labels, so we have to use the same
1196 ;; face. It also matches named subscript ranges,
1197 ;; e.g. vec{bottom:top]. No good way around this.
1198 (structtag
1199 '("\\<\\([a-zA-Z][a-zA-Z0-9_]*:\\)[^:]" (1 font-lock-reference-face)))
1200
1201 ;; Structure names
1202 (structname
1203 '("\\({\\|\\<inherits\\s-\\)\\s-*\\([a-zA-Z][a-zA-Z0-9_]*\\)[},\t \n]"
1204 (2 font-lock-function-name-face)))
1205
1206 ;; Keyword parameters, like /xlog or ,xrange=[]
1207 ;; This is anchored to the comma preceeding the keyword.
1208 ;; Treats continuation lines, works only during whole buffer
1209 ;; fontification. Slow, use it only in fancy fontification.
1210 (keyword-parameters
1211 '("\\(,\\|[a-zA-Z0-9_](\\)[ \t]*\\(\\$[ \t]*\\(;.*\\)?\n\\([ \t]*\\(;.*\\)?\n\\)*[ \t]*\\)?\\(/[a-zA-Z_]\\sw*\\|[a-zA-Z_]\\sw*[ \t]*=\\)"
1212 (6 font-lock-reference-face)))
1213
1214 ;; System variables start with a bang.
1215 (system-variables
1216 '("\\(![a-zA-Z_0-9]+\\(\\.\\sw+\\)?\\)"
1217 (1 font-lock-variable-name-face)))
1218
1219 ;; Special and unusual operators (not used because too noisy)
1220 (special-operators
1221 '("[<>#]" (0 font-lock-keyword-face)))
1222
1223 ;; All operators (not used because too noisy)
1224 (all-operators
1225 '("[-*^#+<>/]" (0 font-lock-keyword-face)))
1226
1227 ;; Arrows with text property `idlwave-class'
1228 (class-arrows
1229 '(idlwave-match-class-arrows (0 idlwave-class-arrow-face))))
1230
1231 (defconst idlwave-font-lock-keywords-1
1232 (list pros-and-functions batch-files)
1233 "Subdued level highlighting for IDLWAVE mode.")
1234
1235 (defconst idlwave-font-lock-keywords-2
1236 (mapcar 'symbol-value idlwave-default-font-lock-items)
1237 "Medium level highlighting for IDLWAVE mode.")
1238
1239 (defconst idlwave-font-lock-keywords-3
1240 (list pros-and-functions
1241 batch-files
1242 idlwave-idl-keywords
1243 label goto
1244 structtag
1245 structname
1246 common-blocks
1247 keyword-parameters
1248 system-variables
1249 class-arrows)
1250 "Gaudy level highlighting for IDLWAVE mode."))
1251
1252 (defun idlwave-match-class-arrows (limit)
1253 ;; Match an object arrow with class property
1254 (and idlwave-store-inquired-class
1255 (re-search-forward "->" limit 'limit)
1256 (get-text-property (match-beginning 0) 'idlwave-class)))
1257
1258 (defvar idlwave-font-lock-keywords idlwave-font-lock-keywords-2
1259 "Default expressions to highlight in IDLWAVE mode.")
1260
1261 (defvar idlwave-font-lock-defaults
1262 '((idlwave-font-lock-keywords
1263 idlwave-font-lock-keywords-1
1264 idlwave-font-lock-keywords-2
1265 idlwave-font-lock-keywords-3)
1266 nil t
1267 ((?$ . "w") (?_ . "w") (?. . "w") (?| . "w") (?& . "w"))
1268 beginning-of-line))
1269
1270 (put 'idlwave-mode 'font-lock-defaults
1271 idlwave-font-lock-defaults) ; XEmacs
1272
1273 (defconst idlwave-comment-line-start-skip "^[ \t]*;"
1274 "Regexp to match the start of a full-line comment.
1275 That is the _beginning_ of a line containing a comment delimiter `;' preceded
1276 only by whitespace.")
1277
1278 (defconst idlwave-begin-block-reg
1279 "\\<\\(pro\\|function\\|begin\\|case\\|switch\\)\\>"
1280 "Regular expression to find the beginning of a block. The case does
1281 not matter. The search skips matches in comments.")
1282
1283 (defconst idlwave-begin-unit-reg "^\\s-*\\(pro\\|function\\)\\>\\|\\`"
1284 "Regular expression to find the beginning of a unit. The case does
1285 not matter.")
1286
1287 (defconst idlwave-end-unit-reg "^\\s-*\\(pro\\|function\\)\\>\\|\\'"
1288 "Regular expression to find the line that indicates the end of unit.
1289 This line is the end of buffer or the start of another unit. The case does
1290 not matter. The search skips matches in comments.")
1291
1292 (defconst idlwave-continue-line-reg "\\<\\$"
1293 "Regular expression to match a continued line.")
1294
1295 (defconst idlwave-end-block-reg
1296 "\\<end\\(\\|case\\|switch\\|else\\|for\\|if\\|rep\\|while\\)\\>"
1297 "Regular expression to find the end of a block. The case does
1298 not matter. The search skips matches found in comments.")
1299
1300 (defconst idlwave-block-matches
1301 '(("pro" . "end")
1302 ("function" . "end")
1303 ("case" . "endcase")
1304 ("else" . "endelse")
1305 ("for" . "endfor")
1306 ("then" . "endif")
1307 ("repeat" . "endrep")
1308 ("switch" . "endswitch")
1309 ("while" . "endwhile"))
1310 "Matches between statements and the corresponding END variant.
1311 The cars are the reserved words starting a block. If the block really
1312 begins with BEGIN, the cars are the reserved words before the begin
1313 which can be used to identify the block type.
1314 This is used to check for the correct END type, to close blocks and
1315 to expand generic end statements to their detailed form.")
1316
1317 (defconst idlwave-block-match-regexp
1318 "\\<\\(else\\|for\\|then\\|repeat\\|while\\)\\>"
1319 "Regular expression matching reserved words which can stand before
1320 blocks starting with a BEGIN statement. The matches must have associations
1321 `idlwave-block-matches'")
1322
1323 (defconst idlwave-identifier "[a-zA-Z_][a-zA-Z0-9$_]*"
1324 "Regular expression matching an IDL identifier.")
1325
1326 (defconst idlwave-sysvar (concat "!" idlwave-identifier)
1327 "Regular expression matching IDL system variables.")
1328
1329 (defconst idlwave-variable (concat idlwave-identifier "\\|" idlwave-sysvar)
1330 "Regular expression matching IDL variable names.")
1331
1332 (defconst idlwave-label (concat idlwave-identifier ":")
1333 "Regular expression matching IDL labels.")
1334
1335 (defconst idlwave-method-call (concat idlwave-identifier "\\s *->"
1336 "\\(\\s *" idlwave-identifier "::\\)?"
1337 ))
1338
1339 (defconst idlwave-statement-match
1340 (list
1341 ;; "endif else" is the only possible "end" that can be
1342 ;; followed by a statement on the same line.
1343 '(endelse . ("end\\(\\|if\\)\\s +else" "end\\(\\|if\\)\\s +else"))
1344 ;; all other "end"s can not be followed by a statement.
1345 (cons 'end (list idlwave-end-block-reg nil))
1346 '(if . ("if\\>" "then"))
1347 '(for . ("for\\>" "do"))
1348 '(begin . ("begin\\>" nil))
1349 '(pdef . ("pro\\>\\|function\\>" nil))
1350 '(while . ("while\\>" "do"))
1351 '(repeat . ("repeat\\>" "repeat"))
1352 '(goto . ("goto\\>" nil))
1353 '(case . ("case\\>" nil))
1354 '(switch . ("switch\\>" nil))
1355 (cons 'call (list (concat "\\(" idlwave-variable "\\) *= *"
1356 "\\(" idlwave-method-call "\\s *\\)?"
1357 idlwave-identifier
1358 "\\s *(") nil))
1359 (cons 'call (list (concat
1360 "\\(" idlwave-method-call "\\s *\\)?"
1361 idlwave-identifier
1362 "\\( *\\($\\|\\$\\)\\|\\s *,\\)") nil))
1363 (cons 'assign (list (concat
1364 "\\(" idlwave-variable "\\) *=") nil)))
1365
1366 "Associated list of statement matching regular expressions.
1367 Each regular expression matches the start of an IDL statement. The
1368 first element of each association is a symbol giving the statement
1369 type. The associated value is a list. The first element of this list
1370 is a regular expression matching the start of an IDL statement for
1371 identifying the statement type. The second element of this list is a
1372 regular expression for finding a substatement for the type. The
1373 substatement starts after the end of the found match modulo
1374 whitespace. If it is nil then the statement has no substatement. The
1375 list order matters since matching an assignment statement exactly is
1376 not possible without parsing. Thus assignment statement become just
1377 the leftover unidentified statements containing an equal sign." )
1378
1379 (defvar idlwave-fill-function 'auto-fill-function
1380 "IDL mode auto fill function.")
1381
1382 (defvar idlwave-comment-indent-function 'comment-indent-function
1383 "IDL mode comment indent function.")
1384
1385 ;; Note that this is documented in the v18 manuals as being a string
1386 ;; of length one rather than a single character.
1387 ;; The code in this file accepts either format for compatibility.
1388 (defvar idlwave-comment-indent-char ?\
1389 "Character to be inserted for IDL comment indentation.
1390 Normally a space.")
1391
1392 (defconst idlwave-continuation-char ?$
1393 "Character which is inserted as a last character on previous line by
1394 \\[idlwave-split-line] to begin a continuation line. Normally $.")
1395
1396 (defconst idlwave-mode-version "6.1_em22")
1397
1398 (defmacro idlwave-keyword-abbrev (&rest args)
1399 "Creates a function for abbrev hooks to call `idlwave-check-abbrev' with args."
1400 `(quote (lambda ()
1401 ,(append '(idlwave-check-abbrev) args))))
1402
1403 ;; If I take the time I can replace idlwave-keyword-abbrev with
1404 ;; idlwave-code-abbrev and remove the quoted abbrev check from
1405 ;; idlwave-check-abbrev. Then, e.g, (idlwave-keyword-abbrev 0 t) becomes
1406 ;; (idlwave-code-abbrev idlwave-check-abbrev 0 t). In fact I should change
1407 ;; the name of idlwave-check-abbrev to something like idlwave-modify-abbrev.
1408
1409 (defmacro idlwave-code-abbrev (&rest args)
1410 "Creates a function for abbrev hooks that ensures abbrevs are not quoted.
1411 Specifically, if the abbrev is in a comment or string it is unexpanded.
1412 Otherwise ARGS forms a list that is evaluated."
1413 `(quote (lambda ()
1414 ,(prin1-to-string args) ;; Puts the code in the doc string
1415 (if (idlwave-quoted)
1416 (progn (unexpand-abbrev) nil)
1417 ,(append args)))))
1418
1419 (defvar idlwave-mode-map (make-sparse-keymap)
1420 "Keymap used in IDL mode.")
1421
1422 (defvar idlwave-mode-syntax-table (make-syntax-table)
1423 "Syntax table in use in `idlwave-mode' buffers.")
1424
1425 (modify-syntax-entry ?+ "." idlwave-mode-syntax-table)
1426 (modify-syntax-entry ?- "." idlwave-mode-syntax-table)
1427 (modify-syntax-entry ?* "." idlwave-mode-syntax-table)
1428 (modify-syntax-entry ?/ "." idlwave-mode-syntax-table)
1429 (modify-syntax-entry ?^ "." idlwave-mode-syntax-table)
1430 (modify-syntax-entry ?# "." idlwave-mode-syntax-table)
1431 (modify-syntax-entry ?= "." idlwave-mode-syntax-table)
1432 (modify-syntax-entry ?% "." idlwave-mode-syntax-table)
1433 (modify-syntax-entry ?< "." idlwave-mode-syntax-table)
1434 (modify-syntax-entry ?> "." idlwave-mode-syntax-table)
1435 (modify-syntax-entry ?\' "\"" idlwave-mode-syntax-table)
1436 (modify-syntax-entry ?\" "\"" idlwave-mode-syntax-table)
1437 (modify-syntax-entry ?\\ "." idlwave-mode-syntax-table)
1438 (modify-syntax-entry ?_ "_" idlwave-mode-syntax-table)
1439 (modify-syntax-entry ?{ "(}" idlwave-mode-syntax-table)
1440 (modify-syntax-entry ?} "){" idlwave-mode-syntax-table)
1441 (modify-syntax-entry ?$ "_" idlwave-mode-syntax-table)
1442 (modify-syntax-entry ?. "." idlwave-mode-syntax-table)
1443 (modify-syntax-entry ?\; "<" idlwave-mode-syntax-table)
1444 (modify-syntax-entry ?\n ">" idlwave-mode-syntax-table)
1445 (modify-syntax-entry ?\f ">" idlwave-mode-syntax-table)
1446
1447 (defvar idlwave-find-symbol-syntax-table
1448 (copy-syntax-table idlwave-mode-syntax-table)
1449 "Syntax table that treats symbol characters as word characters.")
1450
1451 (modify-syntax-entry ?$ "w" idlwave-find-symbol-syntax-table)
1452 (modify-syntax-entry ?_ "w" idlwave-find-symbol-syntax-table)
1453 (modify-syntax-entry ?! "w" idlwave-find-symbol-syntax-table)
1454 (modify-syntax-entry ?. "w" idlwave-find-symbol-syntax-table)
1455
1456 (defmacro idlwave-with-special-syntax (&rest body)
1457 "Execute BODY with a different syntax table."
1458 `(let ((saved-syntax (syntax-table)))
1459 (unwind-protect
1460 (progn
1461 (set-syntax-table idlwave-find-symbol-syntax-table)
1462 ,@body)
1463 (set-syntax-table saved-syntax))))
1464
1465 ;(defmacro idlwave-with-special-syntax1 (&rest body)
1466 ; "Execute BODY with a different syntax table."
1467 ; `(let ((saved-syntax (syntax-table)))
1468 ; (unwind-protect
1469 ; (progn
1470 ; (set-syntax-table idlwave-find-symbol-syntax-table)
1471 ; ,@body)
1472 ; (set-syntax-table saved-syntax))))
1473
1474 (defun idlwave-action-and-binding (key cmd &optional select)
1475 "KEY and CMD are made into a key binding and an indent action.
1476 KEY is a string - same as for the `define-key' function. CMD is a
1477 function of no arguments or a list to be evaluated. CMD is bound to
1478 KEY in `idlwave-mode-map' by defining an anonymous function calling
1479 `self-insert-command' followed by CMD. If KEY contains more than one
1480 character a binding will only be set if SELECT is 'both.
1481
1482 \(KEY . CMD\) is also placed in the `idlwave-indent-expand-table',
1483 replacing any previous value for KEY. If a binding is not set then it
1484 will instead be placed in `idlwave-indent-action-table'.
1485
1486 If the optional argument SELECT is nil then an action and binding are
1487 created. If SELECT is 'noaction, then a binding is always set and no
1488 action is created. If SELECT is 'both then an action and binding
1489 will both be created even if KEY contains more than one character.
1490 Otherwise, if SELECT is non-nil then only an action is created.
1491
1492 Some examples:
1493 No spaces before and 1 after a comma
1494 (idlwave-action-and-binding \",\" '(idlwave-surround 0 1))
1495 A minimum of 1 space before and after `=' (see `idlwave-expand-equal').
1496 (idlwave-action-and-binding \"=\" '(idlwave-expand-equal -1 -1))
1497 Capitalize system variables - action only
1498 (idlwave-action-and-binding idlwave-sysvar '(capitalize-word 1) t)"
1499 (if (not (equal select 'noaction))
1500 ;; Add action
1501 (let* ((table (if select 'idlwave-indent-action-table
1502 'idlwave-indent-expand-table))
1503 (table-key (regexp-quote key))
1504 (cell (assoc table-key (eval table))))
1505 (if cell
1506 ;; Replace action command
1507 (setcdr cell cmd)
1508 ;; New action
1509 (set table (append (eval table) (list (cons table-key cmd)))))))
1510 ;; Make key binding for action
1511 (if (or (and (null select) (= (length key) 1))
1512 (equal select 'noaction)
1513 (equal select 'both))
1514 (define-key idlwave-mode-map key
1515 (append '(lambda ()
1516 (interactive)
1517 (self-insert-command 1))
1518 (list (if (listp cmd)
1519 cmd
1520 (list cmd)))))))
1521
1522 (fset 'idlwave-debug-map (make-sparse-keymap))
1523
1524 (define-key idlwave-mode-map "\C-c " 'idlwave-hard-tab)
1525 (define-key idlwave-mode-map [(control tab)] 'idlwave-hard-tab)
1526 ;(define-key idlwave-mode-map "\C-c\C- " 'idlwave-hard-tab)
1527 (define-key idlwave-mode-map "'" 'idlwave-show-matching-quote)
1528 (define-key idlwave-mode-map "\"" 'idlwave-show-matching-quote)
1529 (define-key idlwave-mode-map "\C-g" 'idlwave-keyboard-quit)
1530 (define-key idlwave-mode-map "\C-c;" 'idlwave-toggle-comment-region)
1531 (define-key idlwave-mode-map "\C-\M-a" 'idlwave-beginning-of-subprogram)
1532 (define-key idlwave-mode-map "\C-\M-e" 'idlwave-end-of-subprogram)
1533 (define-key idlwave-mode-map "\C-c{" 'idlwave-beginning-of-block)
1534 (define-key idlwave-mode-map "\C-c}" 'idlwave-end-of-block)
1535 (define-key idlwave-mode-map "\C-c]" 'idlwave-close-block)
1536 (define-key idlwave-mode-map [(meta control h)] 'idlwave-mark-subprogram)
1537 (define-key idlwave-mode-map "\M-\C-n" 'idlwave-forward-block)
1538 (define-key idlwave-mode-map "\M-\C-p" 'idlwave-backward-block)
1539 (define-key idlwave-mode-map "\M-\C-d" 'idlwave-down-block)
1540 (define-key idlwave-mode-map "\M-\C-u" 'idlwave-backward-up-block)
1541 (define-key idlwave-mode-map "\M-\r" 'idlwave-split-line)
1542 (define-key idlwave-mode-map "\M-\C-q" 'idlwave-indent-subprogram)
1543 (define-key idlwave-mode-map "\C-c\C-p" 'idlwave-previous-statement)
1544 (define-key idlwave-mode-map "\C-c\C-n" 'idlwave-next-statement)
1545 ;; (define-key idlwave-mode-map "\r" 'idlwave-newline)
1546 ;; (define-key idlwave-mode-map "\t" 'idlwave-indent-line)
1547 (define-key idlwave-mode-map [(shift iso-lefttab)] 'idlwave-indent-statement)
1548 (define-key idlwave-mode-map "\C-c\C-a" 'idlwave-auto-fill-mode)
1549 (define-key idlwave-mode-map "\M-q" 'idlwave-fill-paragraph)
1550 (define-key idlwave-mode-map "\M-s" 'idlwave-edit-in-idlde)
1551 (define-key idlwave-mode-map "\C-c\C-h" 'idlwave-doc-header)
1552 (define-key idlwave-mode-map "\C-c\C-m" 'idlwave-doc-modification)
1553 (define-key idlwave-mode-map "\C-c\C-c" 'idlwave-case)
1554 (define-key idlwave-mode-map "\C-c\C-d" 'idlwave-debug-map)
1555 (when (and (boundp 'idlwave-shell-debug-modifiers)
1556 (listp idlwave-shell-debug-modifiers)
1557 (not (equal idlwave-shell-debug-modifiers '())))
1558 ;; Bind the debug commands also with the special modifiers.
1559 (let ((shift (memq 'shift idlwave-shell-debug-modifiers))
1560 (mods-noshift (delq 'shift
1561 (copy-sequence idlwave-shell-debug-modifiers))))
1562 (define-key idlwave-mode-map
1563 (vector (append mods-noshift (list (if shift ?C ?c))))
1564 'idlwave-shell-save-and-run)
1565 (define-key idlwave-mode-map
1566 (vector (append mods-noshift (list (if shift ?B ?b))))
1567 'idlwave-shell-break-here)
1568 (define-key idlwave-mode-map
1569 (vector (append mods-noshift (list (if shift ?E ?e))))
1570 'idlwave-shell-run-region)))
1571 (define-key idlwave-mode-map "\C-c\C-d\C-c" 'idlwave-shell-save-and-run)
1572 (define-key idlwave-mode-map "\C-c\C-d\C-b" 'idlwave-shell-break-here)
1573 (define-key idlwave-mode-map "\C-c\C-d\C-e" 'idlwave-shell-run-region)
1574 (define-key idlwave-mode-map "\C-c\C-f" 'idlwave-for)
1575 ;; (define-key idlwave-mode-map "\C-c\C-f" 'idlwave-function)
1576 ;; (define-key idlwave-mode-map "\C-c\C-p" 'idlwave-procedure)
1577 (define-key idlwave-mode-map "\C-c\C-r" 'idlwave-repeat)
1578 (define-key idlwave-mode-map "\C-c\C-w" 'idlwave-while)
1579 (define-key idlwave-mode-map "\C-c\C-k" 'idlwave-kill-autoloaded-buffers)
1580 (define-key idlwave-mode-map "\C-c\C-s" 'idlwave-shell)
1581 (define-key idlwave-mode-map "\C-c\C-l" 'idlwave-shell-recenter-shell-window)
1582 (define-key idlwave-mode-map "\C-c\C-b" 'idlwave-list-buffer-load-path-shadows)
1583 (autoload 'idlwave-shell "idlw-shell"
1584 "Run an inferior IDL, with I/O through buffer `(idlwave-shell-buffer)'." t)
1585 (autoload 'idlwave-shell-send-command "idlw-shell")
1586 (autoload 'idlwave-shell-recenter-shell-window "idlw-shell"
1587 "Run `idlwave-shell' and switch back to current window" t)
1588 (autoload 'idlwave-shell-save-and-run "idlw-shell"
1589 "Save and run buffer under the shell." t)
1590 (autoload 'idlwave-shell-break-here "idlw-shell"
1591 "Set breakpoint in current line." t)
1592 (autoload 'idlwave-shell-run-region "idlw-shell"
1593 "Compile and run the region." t)
1594 (define-key idlwave-mode-map "\C-c\C-v" 'idlwave-find-module)
1595 (define-key idlwave-mode-map "\C-c\C-t" 'idlwave-find-module-this-file)
1596 (define-key idlwave-mode-map "\C-c?" 'idlwave-routine-info)
1597 (define-key idlwave-mode-map "\M-?" 'idlwave-context-help)
1598 (define-key idlwave-mode-map [(control meta ?\?)]
1599 'idlwave-help-assistant-help-with-topic)
1600 ;; Pickup both forms of Esc/Meta binding
1601 (define-key idlwave-mode-map [(meta tab)] 'idlwave-complete)
1602 (define-key idlwave-mode-map [?\e?\t] 'idlwave-complete)
1603 (define-key idlwave-mode-map "\M-\C-i" 'idlwave-complete)
1604 (define-key idlwave-mode-map "\C-c\C-i" 'idlwave-update-routine-info)
1605 (define-key idlwave-mode-map "\C-c=" 'idlwave-resolve)
1606 (define-key idlwave-mode-map
1607 (if (featurep 'xemacs) [(shift button3)] [(shift mouse-3)])
1608 'idlwave-mouse-context-help)
1609
1610 ;; Set action and key bindings.
1611 ;; See description of the function `idlwave-action-and-binding'.
1612 ;; Automatically add spaces for the following characters
1613
1614 ;; Actions for & are complicated by &&
1615 (idlwave-action-and-binding "&" 'idlwave-custom-ampersand-surround)
1616
1617 ;; Automatically add spaces to equal sign if not keyword. This needs
1618 ;; to go ahead of > and <, so >= and <= will be treated correctly
1619 (idlwave-action-and-binding "=" '(idlwave-expand-equal -1 -1))
1620
1621 ;; Actions for > and < are complicated by >=, <=, and ->...
1622 (idlwave-action-and-binding "<" '(idlwave-custom-ltgtr-surround nil))
1623 (idlwave-action-and-binding ">" '(idlwave-custom-ltgtr-surround 'gtr))
1624
1625 (idlwave-action-and-binding "," '(idlwave-surround 0 -1 1))
1626
1627
1628 ;;;
1629 ;;; Abbrev Section
1630 ;;;
1631 ;;; When expanding abbrevs and the abbrev hook moves backward, an extra
1632 ;;; space is inserted (this is the space typed by the user to expanded
1633 ;;; the abbrev).
1634 ;;;
1635 (defvar idlwave-mode-abbrev-table nil
1636 "Abbreviation table used for IDLWAVE mode")
1637 (define-abbrev-table 'idlwave-mode-abbrev-table ())
1638
1639 (defun idlwave-define-abbrev (name expansion hook &optional noprefix table)
1640 "Define-abbrev with backward compatibility.
1641
1642 If NOPREFIX is non-nil, don't prepend prefix character. Installs into
1643 idlwave-mode-abbrev-table unless TABLE is non-nil."
1644 (let ((abbrevs-changed nil) ;; mask the current value to avoid save
1645 (args (list (or table idlwave-mode-abbrev-table)
1646 (if noprefix name (concat idlwave-abbrev-start-char name))
1647 expansion
1648 hook)))
1649 (condition-case nil
1650 (apply 'define-abbrev (append args '(0 t)))
1651 (error (apply 'define-abbrev args)))))
1652
1653 (condition-case nil
1654 (modify-syntax-entry (string-to-char idlwave-abbrev-start-char)
1655 "w" idlwave-mode-syntax-table)
1656 (error nil))
1657
1658 ;;
1659 ;; Templates
1660 ;;
1661 (idlwave-define-abbrev "c" "" (idlwave-code-abbrev idlwave-case))
1662 (idlwave-define-abbrev "sw" "" (idlwave-code-abbrev idlwave-switch))
1663 (idlwave-define-abbrev "f" "" (idlwave-code-abbrev idlwave-for))
1664 (idlwave-define-abbrev "fu" "" (idlwave-code-abbrev idlwave-function))
1665 (idlwave-define-abbrev "pr" "" (idlwave-code-abbrev idlwave-procedure))
1666 (idlwave-define-abbrev "r" "" (idlwave-code-abbrev idlwave-repeat))
1667 (idlwave-define-abbrev "w" "" (idlwave-code-abbrev idlwave-while))
1668 (idlwave-define-abbrev "i" "" (idlwave-code-abbrev idlwave-if))
1669 (idlwave-define-abbrev "elif" "" (idlwave-code-abbrev idlwave-elif))
1670 ;;
1671 ;; Keywords, system functions, conversion routines
1672 ;;
1673 (idlwave-define-abbrev "ap" "arg_present()" (idlwave-keyword-abbrev 1))
1674 (idlwave-define-abbrev "b" "begin" (idlwave-keyword-abbrev 0 t))
1675 (idlwave-define-abbrev "co" "common" (idlwave-keyword-abbrev 0 t))
1676 (idlwave-define-abbrev "cb" "byte()" (idlwave-keyword-abbrev 1))
1677 (idlwave-define-abbrev "cx" "fix()" (idlwave-keyword-abbrev 1))
1678 (idlwave-define-abbrev "cl" "long()" (idlwave-keyword-abbrev 1))
1679 (idlwave-define-abbrev "cf" "float()" (idlwave-keyword-abbrev 1))
1680 (idlwave-define-abbrev "cs" "string()" (idlwave-keyword-abbrev 1))
1681 (idlwave-define-abbrev "cc" "complex()" (idlwave-keyword-abbrev 1))
1682 (idlwave-define-abbrev "cd" "double()" (idlwave-keyword-abbrev 1))
1683 (idlwave-define-abbrev "e" "else" (idlwave-keyword-abbrev 0 t))
1684 (idlwave-define-abbrev "ec" "endcase" 'idlwave-show-begin)
1685 (idlwave-define-abbrev "es" "endswitch" 'idlwave-show-begin)
1686 (idlwave-define-abbrev "ee" "endelse" 'idlwave-show-begin)
1687 (idlwave-define-abbrev "ef" "endfor" 'idlwave-show-begin)
1688 (idlwave-define-abbrev "ei" "endif else if" 'idlwave-show-begin)
1689 (idlwave-define-abbrev "el" "endif else" 'idlwave-show-begin)
1690 (idlwave-define-abbrev "en" "endif" 'idlwave-show-begin)
1691 (idlwave-define-abbrev "er" "endrep" 'idlwave-show-begin)
1692 (idlwave-define-abbrev "ew" "endwhile" 'idlwave-show-begin)
1693 (idlwave-define-abbrev "g" "goto," (idlwave-keyword-abbrev 0 t))
1694 (idlwave-define-abbrev "h" "help," (idlwave-keyword-abbrev 0))
1695 (idlwave-define-abbrev "k" "keyword_set()" (idlwave-keyword-abbrev 1))
1696 (idlwave-define-abbrev "n" "n_elements()" (idlwave-keyword-abbrev 1))
1697 (idlwave-define-abbrev "on" "on_error," (idlwave-keyword-abbrev 0))
1698 (idlwave-define-abbrev "oi" "on_ioerror," (idlwave-keyword-abbrev 0 1))
1699 (idlwave-define-abbrev "ow" "openw," (idlwave-keyword-abbrev 0))
1700 (idlwave-define-abbrev "or" "openr," (idlwave-keyword-abbrev 0))
1701 (idlwave-define-abbrev "ou" "openu," (idlwave-keyword-abbrev 0))
1702 (idlwave-define-abbrev "p" "print," (idlwave-keyword-abbrev 0))
1703 (idlwave-define-abbrev "pt" "plot," (idlwave-keyword-abbrev 0))
1704 (idlwave-define-abbrev "re" "read," (idlwave-keyword-abbrev 0))
1705 (idlwave-define-abbrev "rf" "readf," (idlwave-keyword-abbrev 0))
1706 (idlwave-define-abbrev "ru" "readu," (idlwave-keyword-abbrev 0))
1707 (idlwave-define-abbrev "rt" "return" (idlwave-keyword-abbrev 0))
1708 (idlwave-define-abbrev "sc" "strcompress()" (idlwave-keyword-abbrev 1))
1709 (idlwave-define-abbrev "sn" "strlen()" (idlwave-keyword-abbrev 1))
1710 (idlwave-define-abbrev "sl" "strlowcase()" (idlwave-keyword-abbrev 1))
1711 (idlwave-define-abbrev "su" "strupcase()" (idlwave-keyword-abbrev 1))
1712 (idlwave-define-abbrev "sm" "strmid()" (idlwave-keyword-abbrev 1))
1713 (idlwave-define-abbrev "sp" "strpos()" (idlwave-keyword-abbrev 1))
1714 (idlwave-define-abbrev "st" "strput()" (idlwave-keyword-abbrev 1))
1715 (idlwave-define-abbrev "sr" "strtrim()" (idlwave-keyword-abbrev 1))
1716 (idlwave-define-abbrev "t" "then" (idlwave-keyword-abbrev 0 t))
1717 (idlwave-define-abbrev "u" "until" (idlwave-keyword-abbrev 0 t))
1718 (idlwave-define-abbrev "wu" "writeu," (idlwave-keyword-abbrev 0))
1719 (idlwave-define-abbrev "iap" "if arg_present() then" (idlwave-keyword-abbrev 6))
1720 (idlwave-define-abbrev "ik" "if keyword_set() then" (idlwave-keyword-abbrev 6))
1721 (idlwave-define-abbrev "ine" "if n_elements() eq 0 then" (idlwave-keyword-abbrev 11))
1722 (idlwave-define-abbrev "inn" "if n_elements() ne 0 then" (idlwave-keyword-abbrev 11))
1723 (idlwave-define-abbrev "np" "n_params()" (idlwave-keyword-abbrev 0))
1724 (idlwave-define-abbrev "s" "size()" (idlwave-keyword-abbrev 1))
1725 (idlwave-define-abbrev "wi" "widget_info()" (idlwave-keyword-abbrev 1))
1726 (idlwave-define-abbrev "wc" "widget_control," (idlwave-keyword-abbrev 0))
1727 (idlwave-define-abbrev "pv" "ptr_valid()" (idlwave-keyword-abbrev 1))
1728 (idlwave-define-abbrev "ipv" "if ptr_valid() then" (idlwave-keyword-abbrev 6))
1729
1730 ;; This section is reserved words only. (From IDL user manual)
1731 ;;
1732 (idlwave-define-abbrev "and" "and" (idlwave-keyword-abbrev 0 t) t)
1733 (idlwave-define-abbrev "begin" "begin" (idlwave-keyword-abbrev 0 t) t)
1734 (idlwave-define-abbrev "break" "break" (idlwave-keyword-abbrev 0 t) t)
1735 (idlwave-define-abbrev "case" "case" (idlwave-keyword-abbrev 0 t) t)
1736 (idlwave-define-abbrev "common" "common" (idlwave-keyword-abbrev 0 t) t)
1737 (idlwave-define-abbrev "continue" "continue" (idlwave-keyword-abbrev 0 t) t)
1738 (idlwave-define-abbrev "do" "do" (idlwave-keyword-abbrev 0 t) t)
1739 (idlwave-define-abbrev "else" "else" (idlwave-keyword-abbrev 0 t) t)
1740 (idlwave-define-abbrev "end" "end" 'idlwave-show-begin-check t)
1741 (idlwave-define-abbrev "endcase" "endcase" 'idlwave-show-begin-check t)
1742 (idlwave-define-abbrev "endelse" "endelse" 'idlwave-show-begin-check t)
1743 (idlwave-define-abbrev "endfor" "endfor" 'idlwave-show-begin-check t)
1744 (idlwave-define-abbrev "endif" "endif" 'idlwave-show-begin-check t)
1745 (idlwave-define-abbrev "endrep" "endrep" 'idlwave-show-begin-check t)
1746 (idlwave-define-abbrev "endswitch" "endswitch" 'idlwave-show-begin-check t)
1747 (idlwave-define-abbrev "endwhi" "endwhi" 'idlwave-show-begin-check t)
1748 (idlwave-define-abbrev "endwhile" "endwhile" 'idlwave-show-begin-check t)
1749 (idlwave-define-abbrev "eq" "eq" (idlwave-keyword-abbrev 0 t) t)
1750 (idlwave-define-abbrev "for" "for" (idlwave-keyword-abbrev 0 t) t)
1751 (idlwave-define-abbrev "function" "function" (idlwave-keyword-abbrev 0 t) t)
1752 (idlwave-define-abbrev "ge" "ge" (idlwave-keyword-abbrev 0 t) t)
1753 (idlwave-define-abbrev "goto" "goto" (idlwave-keyword-abbrev 0 t) t)
1754 (idlwave-define-abbrev "gt" "gt" (idlwave-keyword-abbrev 0 t) t)
1755 (idlwave-define-abbrev "if" "if" (idlwave-keyword-abbrev 0 t) t)
1756 (idlwave-define-abbrev "le" "le" (idlwave-keyword-abbrev 0 t) t)
1757 (idlwave-define-abbrev "lt" "lt" (idlwave-keyword-abbrev 0 t) t)
1758 (idlwave-define-abbrev "mod" "mod" (idlwave-keyword-abbrev 0 t) t)
1759 (idlwave-define-abbrev "ne" "ne" (idlwave-keyword-abbrev 0 t) t)
1760 (idlwave-define-abbrev "not" "not" (idlwave-keyword-abbrev 0 t) t)
1761 (idlwave-define-abbrev "of" "of" (idlwave-keyword-abbrev 0 t) t)
1762 (idlwave-define-abbrev "on_ioerror" "on_ioerror" (idlwave-keyword-abbrev 0 t) t)
1763 (idlwave-define-abbrev "or" "or" (idlwave-keyword-abbrev 0 t) t)
1764 (idlwave-define-abbrev "pro" "pro" (idlwave-keyword-abbrev 0 t) t)
1765 (idlwave-define-abbrev "repeat" "repeat" (idlwave-keyword-abbrev 0 t) t)
1766 (idlwave-define-abbrev "switch" "switch" (idlwave-keyword-abbrev 0 t) t)
1767 (idlwave-define-abbrev "then" "then" (idlwave-keyword-abbrev 0 t) t)
1768 (idlwave-define-abbrev "until" "until" (idlwave-keyword-abbrev 0 t) t)
1769 (idlwave-define-abbrev "while" "while" (idlwave-keyword-abbrev 0 t) t)
1770 (idlwave-define-abbrev "xor" "xor" (idlwave-keyword-abbrev 0 t) t)
1771
1772 (defvar imenu-create-index-function)
1773 (defvar extract-index-name-function)
1774 (defvar prev-index-position-function)
1775 (defvar imenu-extract-index-name-function)
1776 (defvar imenu-prev-index-position-function)
1777 ;; defined later - so just make the compiler hush
1778 (defvar idlwave-mode-menu)
1779 (defvar idlwave-mode-debug-menu)
1780
1781 ;;;###autoload
1782 (defun idlwave-mode ()
1783 "Major mode for editing IDL source files (version 6.1_em22).
1784
1785 The main features of this mode are
1786
1787 1. Indentation and Formatting
1788 --------------------------
1789 Like other Emacs programming modes, C-j inserts a newline and indents.
1790 TAB is used for explicit indentation of the current line.
1791
1792 To start a continuation line, use \\[idlwave-split-line]. This
1793 function can also be used in the middle of a line to split the line
1794 at that point. When used inside a long constant string, the string
1795 is split at that point with the `+' concatenation operator.
1796
1797 Comments are indented as follows:
1798
1799 `;;;' Indentation remains unchanged.
1800 `;;' Indent like the surrounding code
1801 `;' Indent to a minimum column.
1802
1803 The indentation of comments starting in column 0 is never changed.
1804
1805 Use \\[idlwave-fill-paragraph] to refill a paragraph inside a
1806 comment. The indentation of the second line of the paragraph
1807 relative to the first will be retained. Use
1808 \\[idlwave-auto-fill-mode] to toggle auto-fill mode for these
1809 comments. When the variable `idlwave-fill-comment-line-only' is
1810 nil, code can also be auto-filled and auto-indented.
1811
1812 To convert pre-existing IDL code to your formatting style, mark the
1813 entire buffer with \\[mark-whole-buffer] and execute
1814 \\[idlwave-expand-region-abbrevs]. Then mark the entire buffer
1815 again followed by \\[indent-region] (`indent-region').
1816
1817 2. Routine Info
1818 ------------
1819 IDLWAVE displays information about the calling sequence and the
1820 accepted keyword parameters of a procedure or function with
1821 \\[idlwave-routine-info]. \\[idlwave-find-module] jumps to the
1822 source file of a module. These commands know about system
1823 routines, all routines in idlwave-mode buffers and (when the
1824 idlwave-shell is active) about all modules currently compiled under
1825 this shell. It also makes use of pre-compiled or custom-scanned
1826 user and library catalogs many popular libraries ship with by
1827 default. Use \\[idlwave-update-routine-info] to update this
1828 information, which is also used for completion (see item 4).
1829
1830 3. Online IDL Help
1831 ---------------
1832
1833 \\[idlwave-context-help] displays the IDL documentation relevant
1834 for the system variable, keyword, or routines at point. A single
1835 key stroke gets you directly to the right place in the docs. See
1836 the manual to configure where and how the HTML help is displayed.
1837
1838 4. Completion
1839 ----------
1840 \\[idlwave-complete] completes the names of procedures, functions
1841 class names, keyword parameters, system variables and tags, class
1842 tags, structure tags, filenames and much more. It is context
1843 sensitive and figures out what is expected at point. Lower case
1844 strings are completed in lower case, other strings in mixed or
1845 upper case.
1846
1847 5. Code Templates and Abbreviations
1848 --------------------------------
1849 Many Abbreviations are predefined to expand to code fragments and templates.
1850 The abbreviations start generally with a `\\`. Some examples
1851
1852 \\pr PROCEDURE template
1853 \\fu FUNCTION template
1854 \\c CASE statement template
1855 \\sw SWITCH statement template
1856 \\f FOR loop template
1857 \\r REPEAT Loop template
1858 \\w WHILE loop template
1859 \\i IF statement template
1860 \\elif IF-ELSE statement template
1861 \\b BEGIN
1862
1863 For a full list, use \\[idlwave-list-abbrevs]. Some templates also
1864 have direct keybindings - see the list of keybindings below.
1865
1866 \\[idlwave-doc-header] inserts a documentation header at the
1867 beginning of the current program unit (pro, function or main).
1868 Change log entries can be added to the current program unit with
1869 \\[idlwave-doc-modification].
1870
1871 6. Automatic Case Conversion
1872 -------------------------
1873 The case of reserved words and some abbrevs is controlled by
1874 `idlwave-reserved-word-upcase' and `idlwave-abbrev-change-case'.
1875
1876 7. Automatic END completion
1877 ------------------------
1878 If the variable `idlwave-expand-generic-end' is non-nil, each END typed
1879 will be converted to the specific version, like ENDIF, ENDFOR, etc.
1880
1881 8. Hooks
1882 -----
1883 Loading idlwave.el runs `idlwave-load-hook'.
1884 Turning on `idlwave-mode' runs `idlwave-mode-hook'.
1885
1886 9. Documentation and Customization
1887 -------------------------------
1888 Info documentation for this package is available. Use
1889 \\[idlwave-info] to display (complain to your sysadmin if that does
1890 not work). For Postscript, PDF, and HTML versions of the
1891 documentation, check IDLWAVE's homepage at `http://idlwave.org'.
1892 IDLWAVE has customize support - see the group `idlwave'.
1893
1894 10.Keybindings
1895 -----------
1896 Here is a list of all keybindings of this mode.
1897 If some of the key bindings below show with ??, use \\[describe-key]
1898 followed by the key sequence to see what the key sequence does.
1899
1900 \\{idlwave-mode-map}"
1901
1902 (interactive)
1903 (kill-all-local-variables)
1904
1905 (if idlwave-startup-message
1906 (message "Emacs IDLWAVE mode version %s." idlwave-mode-version))
1907 (setq idlwave-startup-message nil)
1908
1909 (setq local-abbrev-table idlwave-mode-abbrev-table)
1910 (set-syntax-table idlwave-mode-syntax-table)
1911
1912 (set (make-local-variable 'indent-line-function) 'idlwave-indent-and-action)
1913
1914 (make-local-variable idlwave-comment-indent-function)
1915 (set idlwave-comment-indent-function 'idlwave-comment-hook)
1916
1917 (set (make-local-variable 'comment-start-skip) ";+[ \t]*")
1918 (set (make-local-variable 'comment-start) ";")
1919 (set (make-local-variable 'comment-add) 1) ; ";;" for new and regions
1920 (set (make-local-variable 'require-final-newline) t)
1921 (set (make-local-variable 'abbrev-all-caps) t)
1922 (set (make-local-variable 'indent-tabs-mode) nil)
1923 (set (make-local-variable 'completion-ignore-case) t)
1924
1925 (use-local-map idlwave-mode-map)
1926
1927 (when (featurep 'easymenu)
1928 (easy-menu-add idlwave-mode-menu idlwave-mode-map)
1929 (easy-menu-add idlwave-mode-debug-menu idlwave-mode-map))
1930
1931 (setq mode-name "IDLWAVE")
1932 (setq major-mode 'idlwave-mode)
1933 (setq abbrev-mode t)
1934
1935 (set (make-local-variable idlwave-fill-function) 'idlwave-auto-fill)
1936 (setq comment-end "")
1937 (set (make-local-variable 'comment-multi-line) nil)
1938 (set (make-local-variable 'paragraph-separate)
1939 "[ \t\f]*$\\|[ \t]*;+[ \t]*$\\|;+[+=-_*]+$")
1940 (set (make-local-variable 'paragraph-start) "[ \t\f]\\|[ \t]*;+[ \t]")
1941 (set (make-local-variable 'paragraph-ignore-fill-prefix) nil)
1942 (set (make-local-variable 'parse-sexp-ignore-comments) t)
1943
1944 ;; ChangeLog
1945 (set (make-local-variable 'add-log-current-defun-function)
1946 'idlwave-current-routine-fullname)
1947
1948 ;; Set tag table list to use IDLTAGS as file name.
1949 (if (boundp 'tag-table-alist)
1950 (add-to-list 'tag-table-alist '("\\.pro$" . "IDLTAGS")))
1951
1952 ;; Font-lock additions
1953 ;; Following line is for Emacs - XEmacs uses the corresponding property
1954 ;; on the `idlwave-mode' symbol.
1955 (set (make-local-variable 'font-lock-defaults) idlwave-font-lock-defaults)
1956 (set (make-local-variable 'font-lock-mark-block-function)
1957 'idlwave-mark-subprogram)
1958 (set (make-local-variable 'font-lock-fontify-region-function)
1959 'idlwave-font-lock-fontify-region)
1960
1961 ;; Imenu setup
1962 (set (make-local-variable 'imenu-create-index-function)
1963 'imenu-default-create-index-function)
1964 (set (make-local-variable 'imenu-extract-index-name-function)
1965 'idlwave-unit-name)
1966 (set (make-local-variable 'imenu-prev-index-position-function)
1967 'idlwave-prev-index-position)
1968
1969 ;; HideShow setup
1970 (add-to-list 'hs-special-modes-alist
1971 (list 'idlwave-mode
1972 idlwave-begin-block-reg
1973 idlwave-end-block-reg
1974 ";"
1975 'idlwave-forward-block nil))
1976
1977 ;; Make a local post-command-hook and add our hook to it
1978 ;; NB: `make-local-hook' needed for older/alternative Emacs compatibility
1979 ;; (make-local-hook 'post-command-hook)
1980 (add-hook 'post-command-hook 'idlwave-command-hook nil 'local)
1981
1982 ;; Make local hooks for buffer updates
1983 ;; NB: `make-local-hook' needed for older/alternative Emacs compatibility
1984 ;; (make-local-hook 'kill-buffer-hook)
1985 (add-hook 'kill-buffer-hook 'idlwave-kill-buffer-update nil 'local)
1986 ;; (make-local-hook 'after-save-hook)
1987 (add-hook 'after-save-hook 'idlwave-save-buffer-update nil 'local)
1988 (add-hook 'after-save-hook 'idlwave-revoke-license-to-kill nil 'local)
1989
1990 ;; Setup directories and file, if necessary
1991 (idlwave-setup)
1992
1993 ;; Update the routine info with info about current buffer?
1994 (idlwave-new-buffer-update)
1995
1996 ;; Check help location
1997 (idlwave-help-check-locations)
1998
1999 ;; Run the mode hook
2000 (run-mode-hooks 'idlwave-mode-hook))
2001
2002 (defvar idlwave-setup-done nil)
2003 (defun idlwave-setup ()
2004 (unless idlwave-setup-done
2005 (if (not (file-directory-p idlwave-config-directory))
2006 (make-directory idlwave-config-directory))
2007 (setq
2008 idlwave-user-catalog-file (expand-file-name
2009 idlwave-user-catalog-file
2010 idlwave-config-directory)
2011 idlwave-xml-system-rinfo-converted-file
2012 (expand-file-name
2013 idlwave-xml-system-rinfo-converted-file
2014 idlwave-config-directory)
2015 idlwave-path-file (expand-file-name
2016 idlwave-path-file
2017 idlwave-config-directory))
2018 (idlwave-read-paths) ; we may need these early
2019 (setq idlwave-setup-done t)))
2020
2021 (defun idlwave-font-lock-fontify-region (beg end &optional verbose)
2022 "Fontify continuation lines correctly."
2023 (let (pos)
2024 (save-excursion
2025 (goto-char beg)
2026 (forward-line -1)
2027 (when (setq pos (idlwave-is-continuation-line))
2028 (goto-char pos)
2029 (idlwave-beginning-of-statement)
2030 (setq beg (point)))))
2031 (font-lock-default-fontify-region beg end verbose))
2032
2033 ;;
2034 ;; Code Formatting ----------------------------------------------------
2035 ;;
2036
2037 (defun idlwave-hard-tab ()
2038 "Inserts TAB in buffer in current position."
2039 (interactive)
2040 (insert "\t"))
2041
2042 ;;; This stuff is experimental
2043
2044 (defvar idlwave-command-hook nil
2045 "If non-nil, a list that can be evaluated using `eval'.
2046 It is evaluated in the lisp function `idlwave-command-hook' which is
2047 placed in `post-command-hook'.")
2048
2049 (defun idlwave-command-hook ()
2050 "Command run after every command.
2051 Evaluates a non-nil value of the *variable* `idlwave-command-hook' and
2052 sets the variable to zero afterwards."
2053 (and idlwave-command-hook
2054 (listp idlwave-command-hook)
2055 (condition-case nil
2056 (eval idlwave-command-hook)
2057 (error nil)))
2058 (setq idlwave-command-hook nil))
2059
2060 ;;; End experiment
2061
2062 ;; It would be better to use expand.el for better abbrev handling and
2063 ;; versatility.
2064
2065 (defun idlwave-check-abbrev (arg &optional reserved)
2066 "Reverses abbrev expansion if in comment or string.
2067 Argument ARG is the number of characters to move point
2068 backward if `idlwave-abbrev-move' is non-nil.
2069 If optional argument RESERVED is non-nil then the expansion
2070 consists of reserved words, which will be capitalized if
2071 `idlwave-reserved-word-upcase' is non-nil.
2072 Otherwise, the abbrev will be capitalized if `idlwave-abbrev-change-case'
2073 is non-nil, unless its value is \`down in which case the abbrev will be
2074 made into all lowercase.
2075 Returns non-nil if abbrev is left expanded."
2076 (if (idlwave-quoted)
2077 (progn (unexpand-abbrev)
2078 nil)
2079 (if (and reserved idlwave-reserved-word-upcase)
2080 (upcase-region last-abbrev-location (point))
2081 (cond
2082 ((equal idlwave-abbrev-change-case 'down)
2083 (downcase-region last-abbrev-location (point)))
2084 (idlwave-abbrev-change-case
2085 (upcase-region last-abbrev-location (point)))))
2086 (if (and idlwave-abbrev-move (> arg 0))
2087 (if (boundp 'post-command-hook)
2088 (setq idlwave-command-hook (list 'backward-char (1+ arg)))
2089 (backward-char arg)))
2090 t))
2091
2092 (defun idlwave-in-comment ()
2093 "Returns t if point is inside a comment, nil otherwise."
2094 (save-excursion
2095 (let ((here (point)))
2096 (and (idlwave-goto-comment) (> here (point))))))
2097
2098 (defun idlwave-goto-comment ()
2099 "Move to start of comment delimiter on current line.
2100 Moves to end of line if there is no comment delimiter.
2101 Ignores comment delimiters in strings.
2102 Returns point if comment found and nil otherwise."
2103 (let ((eos (progn (end-of-line) (point)))
2104 (data (match-data))
2105 found)
2106 ;; Look for first comment delimiter not in a string
2107 (beginning-of-line)
2108 (setq found (search-forward comment-start eos 'lim))
2109 (while (and found (idlwave-in-quote))
2110 (setq found (search-forward comment-start eos 'lim)))
2111 (store-match-data data)
2112 (and found (not (idlwave-in-quote))
2113 (progn
2114 (backward-char 1)
2115 (point)))))
2116
2117 (defvar transient-mark-mode)
2118 (defvar zmacs-regions)
2119 (defvar mark-active)
2120 (defun idlwave-region-active-p ()
2121 "Is transient-mark-mode on and the region active?
2122 Works on both Emacs and XEmacs."
2123 (if (featurep 'xemacs)
2124 (and zmacs-regions (region-active-p))
2125 (and transient-mark-mode mark-active)))
2126
2127 (defun idlwave-show-matching-quote ()
2128 "Insert quote and show matching quote if this is end of a string."
2129 (interactive)
2130 (let ((bq (idlwave-in-quote))
2131 (inq last-command-char))
2132 (if (and bq (not (idlwave-in-comment)))
2133 (let ((delim (char-after bq)))
2134 (insert inq)
2135 (if (eq inq delim)
2136 (save-excursion
2137 (goto-char bq)
2138 (sit-for 1))))
2139 ;; Not the end of a string
2140 (insert inq))))
2141
2142 (defun idlwave-show-begin-check ()
2143 "Ensure that the previous word was a token before `idlwave-show-begin'.
2144 An END token must be preceded by whitespace."
2145 (if (not (idlwave-quoted))
2146 (if
2147 (save-excursion
2148 (backward-word 1)
2149 (backward-char 1)
2150 (looking-at "[ \t\n\f]"))
2151 (idlwave-show-begin))))
2152
2153 (defun idlwave-show-begin ()
2154 "Finds the start of current block and blinks to it for a second.
2155 Also checks if the correct end statement has been used."
2156 ;; All end statements are reserved words
2157 ;; Re-indent end line
2158 ;;(insert-char ?\ 1) ;; So indent, etc. work well
2159 ;;(backward-char 1)
2160 (let* ((pos (point-marker))
2161 (last-abbrev-marker (copy-marker last-abbrev-location))
2162 (eol-pos (save-excursion (end-of-line) (point)))
2163 begin-pos end-pos end end1 )
2164 (if idlwave-reindent-end (idlwave-indent-line))
2165 (setq last-abbrev-location (marker-position last-abbrev-marker))
2166 (when (and (idlwave-check-abbrev 0 t)
2167 idlwave-show-block)
2168 (save-excursion
2169 ;; Move inside current block
2170 (goto-char last-abbrev-marker)
2171 (idlwave-block-jump-out -1 'nomark)
2172 (setq begin-pos (point))
2173 (idlwave-block-jump-out 1 'nomark)
2174 (setq end-pos (point))
2175 (if (> end-pos eol-pos)
2176 (setq end-pos pos))
2177 (goto-char end-pos)
2178 (setq end (buffer-substring
2179 (progn
2180 (skip-chars-backward "a-zA-Z")
2181 (point))
2182 end-pos))
2183 (goto-char begin-pos)
2184 (when (setq end1 (cdr (idlwave-block-master)))
2185 (cond
2186 ((null end1)) ; no-operation
2187 ((string= (downcase end) (downcase end1))
2188 (sit-for 1))
2189 ((string= (downcase end) "end")
2190 ;; A generic end
2191 (if idlwave-expand-generic-end
2192 (save-excursion
2193 (goto-char pos)
2194 (backward-char 3)
2195 (insert (if (string= end "END") (upcase end1) end1))
2196 (delete-char 3)))
2197 (sit-for 1))
2198 (t
2199 (beep)
2200 (message "Warning: Shouldn't this be \"%s\" instead of \"%s\"?"
2201 end1 end)
2202 (sit-for 1))))))))
2203 ;;(delete-char 1))
2204
2205 (defun idlwave-block-master ()
2206 (let ((case-fold-search t))
2207 (save-excursion
2208 (cond
2209 ((looking-at "pro\\|case\\|switch\\|function\\>")
2210 (assoc (downcase (match-string 0)) idlwave-block-matches))
2211 ((looking-at "begin\\>")
2212 (let ((limit (save-excursion
2213 (idlwave-beginning-of-statement)
2214 (point))))
2215 (cond
2216 ((re-search-backward ":[ \t]*\\=" limit t)
2217 ;; seems to be a case thing
2218 '("begin" . "end"))
2219 ((re-search-backward idlwave-block-match-regexp limit t)
2220 (assoc (downcase (match-string 1))
2221 idlwave-block-matches))
2222 (t
2223 ;; Just a normal block
2224 '("begin" . "end")))))
2225 (t nil)))))
2226
2227 (defun idlwave-close-block ()
2228 "Terminate the current block with the correct END statement."
2229 (interactive)
2230 ;; Start new line if we are not in a new line
2231 (unless (save-excursion
2232 (skip-chars-backward " \t")
2233 (bolp))
2234 (let ((idlwave-show-block nil))
2235 (newline-and-indent)))
2236 (let ((last-abbrev-location (point))) ; for upcasing
2237 (insert "end")
2238 (idlwave-show-begin)))
2239
2240 (defun idlwave-custom-ampersand-surround (&optional is-action)
2241 "Surround &, leaving room for && (which surrround as well)."
2242 (let* ((prev-char (char-after (- (point) 2)))
2243 (next-char (char-after (point)))
2244 (amp-left (eq prev-char ?&))
2245 (amp-right (eq next-char ?&))
2246 (len (if amp-left 2 1)))
2247 (unless amp-right ;no need to do it twice, amp-left will catch it.
2248 (idlwave-surround -1 (if (or is-action amp-left) -1) len))))
2249
2250 (defun idlwave-custom-ltgtr-surround (gtr &optional is-action)
2251 "Surround > and < by blanks, leaving room for >= and <=, and considering ->."
2252 (let* ((prev-char (char-after (- (point) 2)))
2253 (next-char (char-after (point)))
2254 (method-invoke (and gtr (eq prev-char ?-)))
2255 (len (if method-invoke 2 1)))
2256 (unless (eq next-char ?=)
2257 ;; Key binding: pad only on left, to save for possible >=/<=
2258 (idlwave-surround -1 (if (or is-action method-invoke) -1) len))))
2259
2260 (defun idlwave-surround (&optional before after length is-action)
2261 "Surround the LENGTH characters before point with blanks.
2262 LENGTH defaults to 1.
2263 Optional arguments BEFORE and AFTER affect the behavior before and
2264 after the characters (see also description of `idlwave-make-space'):
2265
2266 nil do nothing
2267 0 force no spaces
2268 integer > 0 force exactly n spaces
2269 integer < 0 at least |n| spaces
2270
2271 The function does nothing if any of the following conditions is true:
2272 - `idlwave-surround-by-blank' is nil
2273 - the character before point is inside a string or comment"
2274 (when (and idlwave-surround-by-blank (not (idlwave-quoted)))
2275 (let ((length (or length 1))) ; establish a default for LENGTH
2276 (backward-char length)
2277 (save-restriction
2278 (let ((here (point)))
2279 (skip-chars-backward " \t")
2280 (if (bolp)
2281 ;; avoid clobbering indent
2282 (progn
2283 (move-to-column (idlwave-calculate-indent))
2284 (if (<= (point) here)
2285 (narrow-to-region (point) here))
2286 (goto-char here)))
2287 (idlwave-make-space before))
2288 (skip-chars-forward " \t"))
2289 (forward-char length)
2290 (idlwave-make-space after)
2291 ;; Check to see if the line should auto wrap
2292 (if (and (equal (char-after (1- (point))) ?\ )
2293 (> (current-column) fill-column))
2294 (funcall auto-fill-function)))))
2295
2296 (defun idlwave-make-space (n)
2297 "Make space at point.
2298 The space affected is all the spaces and tabs around point.
2299 If n is non-nil then point is left abs(n) spaces from the beginning of
2300 the contiguous space.
2301 The amount of space at point is determined by N.
2302 If the value of N is:
2303 nil - do nothing.
2304 > 0 - exactly N spaces.
2305 < 0 - a minimum of -N spaces, i.e., do not change if there are
2306 already -N spaces.
2307 0 - no spaces (i.e. remove any existing space)."
2308 (if (integerp n)
2309 (let
2310 ((start-col (progn (skip-chars-backward " \t") (current-column)))
2311 (left (point))
2312 (end-col (progn (skip-chars-forward " \t") (current-column))))
2313 (delete-horizontal-space)
2314 (cond
2315 ((> n 0)
2316 (idlwave-indent-to (+ start-col n))
2317 (goto-char (+ left n)))
2318 ((< n 0)
2319 (idlwave-indent-to end-col (- n))
2320 (goto-char (- left n)))
2321 ;; n = 0, done
2322 ))))
2323
2324 (defun idlwave-newline ()
2325 "Inserts a newline and indents the current and previous line."
2326 (interactive)
2327 ;;
2328 ;; Handle unterminated single and double quotes
2329 ;; If not in a comment and in a string then insertion of a newline
2330 ;; will mean unbalanced quotes.
2331 ;;
2332 (if (and (not (idlwave-in-comment)) (idlwave-in-quote))
2333 (progn (beep)
2334 (message "Warning: unbalanced quotes?")))
2335 (newline)
2336 ;;
2337 ;; The current line is being split, the cursor should be at the
2338 ;; beginning of the new line skipping the leading indentation.
2339 ;;
2340 ;; The reason we insert the new line before indenting is that the
2341 ;; indenting could be confused by keywords (e.g. END) on the line
2342 ;; after the split point. This prevents us from just using
2343 ;; `indent-for-tab-command' followed by `newline-and-indent'.
2344 ;;
2345 (beginning-of-line 0)
2346 (idlwave-indent-line)
2347 (forward-line)
2348 (idlwave-indent-line))
2349
2350 ;;
2351 ;; Use global variable 'comment-column' to set parallel comment
2352 ;;
2353 ;; Modeled on lisp.el
2354 ;; Emacs Lisp and IDL (Wave CL) have identical comment syntax
2355 (defun idlwave-comment-hook ()
2356 "Compute indent for the beginning of the IDL comment delimiter."
2357 (if (or (looking-at idlwave-no-change-comment)
2358 (if idlwave-begin-line-comment
2359 (looking-at idlwave-begin-line-comment)
2360 (looking-at "^;")))
2361 (current-column)
2362 (if (looking-at idlwave-code-comment)
2363 (if (save-excursion (skip-chars-backward " \t") (bolp))
2364 ;; On line by itself, indent as code
2365 (let ((tem (idlwave-calculate-indent)))
2366 (if (listp tem) (car tem) tem))
2367 ;; after code - do not change
2368 (current-column))
2369 (skip-chars-backward " \t")
2370 (max (if (bolp) 0 (1+ (current-column)))
2371 comment-column))))
2372
2373 (defun idlwave-split-line ()
2374 "Continue line by breaking line at point and indent the lines.
2375 For a code line insert continuation marker. If the line is a line comment
2376 then the new line will contain a comment with the same indentation.
2377 Splits strings with the IDL operator `+' if `idlwave-split-line-string' is
2378 non-nil."
2379 (interactive)
2380 ;; Expand abbreviation, just like normal RET would.
2381 (and abbrev-mode (expand-abbrev))
2382 (let (beg)
2383 (if (not (idlwave-in-comment))
2384 ;; For code line add continuation.
2385 ;; Check if splitting a string.
2386 (progn
2387 (if (setq beg (idlwave-in-quote))
2388 (if idlwave-split-line-string
2389 ;; Split the string.
2390 (progn (insert (setq beg (char-after beg)) " + "
2391 idlwave-continuation-char beg)
2392 (backward-char 1)
2393 (newline-and-indent)
2394 (forward-char 1))
2395 ;; Do not split the string.
2396 (beep)
2397 (message "Warning: continuation inside string!!")
2398 (insert " " idlwave-continuation-char))
2399 ;; Not splitting a string.
2400 (if (not (member (char-before) '(?\ ?\t)))
2401 (insert " "))
2402 (insert idlwave-continuation-char)
2403 (newline-and-indent)))
2404 (indent-new-comment-line))
2405 ;; Indent previous line
2406 (setq beg (- (point-max) (point)))
2407 (forward-line -1)
2408 (idlwave-indent-line)
2409 (goto-char (- (point-max) beg))
2410 ;; Reindent new line
2411 (idlwave-indent-line)))
2412
2413 (defun idlwave-beginning-of-subprogram ()
2414 "Moves point to the beginning of the current program unit."
2415 (interactive)
2416 (idlwave-find-key idlwave-begin-unit-reg -1))
2417
2418 (defun idlwave-end-of-subprogram ()
2419 "Moves point to the start of the next program unit."
2420 (interactive)
2421 (idlwave-end-of-statement)
2422 (idlwave-find-key idlwave-end-unit-reg 1))
2423
2424 (defun idlwave-mark-statement ()
2425 "Mark current IDL statement."
2426 (interactive)
2427 (idlwave-end-of-statement)
2428 (let ((end (point)))
2429 (idlwave-beginning-of-statement)
2430 (push-mark end nil t)))
2431
2432 (defun idlwave-mark-block ()
2433 "Mark containing block."
2434 (interactive)
2435 (idlwave-end-of-statement)
2436 (idlwave-backward-up-block -1)
2437 (idlwave-end-of-statement)
2438 (let ((end (point)))
2439 (idlwave-backward-block)
2440 (idlwave-beginning-of-statement)
2441 (push-mark end nil t)))
2442
2443
2444 (defun idlwave-mark-subprogram ()
2445 "Put mark at beginning of program, point at end.
2446 The marks are pushed."
2447 (interactive)
2448 (idlwave-end-of-statement)
2449 (idlwave-beginning-of-subprogram)
2450 (let ((beg (point)))
2451 (idlwave-forward-block)
2452 (push-mark beg nil t))
2453 (exchange-point-and-mark))
2454
2455 (defun idlwave-backward-up-block (&optional arg)
2456 "Move to beginning of enclosing block if prefix ARG >= 0.
2457 If prefix ARG < 0 then move forward to enclosing block end."
2458 (interactive "p")
2459 (idlwave-block-jump-out (- arg) 'nomark))
2460
2461 (defun idlwave-beginning-of-block ()
2462 "Go to the beginning of the current block."
2463 (interactive)
2464 (idlwave-block-jump-out -1 'nomark)
2465 (forward-word 1))
2466
2467 (defun idlwave-end-of-block ()
2468 "Go to the beginning of the current block."
2469 (interactive)
2470 (idlwave-block-jump-out 1 'nomark)
2471 (backward-word 1))
2472
2473 (defun idlwave-forward-block (&optional arg)
2474 "Move across next nested block."
2475 (interactive)
2476 (let ((arg (or arg 1)))
2477 (if (idlwave-down-block arg)
2478 (idlwave-block-jump-out arg 'nomark))))
2479
2480 (defun idlwave-backward-block ()
2481 "Move backward across previous nested block."
2482 (interactive)
2483 (if (idlwave-down-block -1)
2484 (idlwave-block-jump-out -1 'nomark)))
2485
2486 (defun idlwave-down-block (&optional arg)
2487 "Go down a block.
2488 With ARG: ARG >= 0 go forwards, ARG < 0 go backwards.
2489 Returns non-nil if successfull."
2490 (interactive "p")
2491 (let (status)
2492 (if (< arg 0)
2493 ;; Backward
2494 (let ((eos (save-excursion
2495 (idlwave-block-jump-out -1 'nomark)
2496 (point))))
2497 (if (setq status (idlwave-find-key
2498 idlwave-end-block-reg -1 'nomark eos))
2499 (idlwave-beginning-of-statement)
2500 (message "No nested block before beginning of containing block.")))
2501 ;; Forward
2502 (let ((eos (save-excursion
2503 (idlwave-block-jump-out 1 'nomark)
2504 (point))))
2505 (if (setq status (idlwave-find-key
2506 idlwave-begin-block-reg 1 'nomark eos))
2507 (idlwave-end-of-statement)
2508 (message "No nested block before end of containing block."))))
2509 status))
2510
2511 (defun idlwave-mark-doclib ()
2512 "Put point at beginning of doc library header, mark at end.
2513 The marks are pushed."
2514 (interactive)
2515 (let (beg
2516 (here (point)))
2517 (goto-char (point-max))
2518 (if (re-search-backward idlwave-doclib-start nil t)
2519 (progn
2520 (setq beg (progn (beginning-of-line) (point)))
2521 (if (re-search-forward idlwave-doclib-end nil t)
2522 (progn
2523 (forward-line 1)
2524 (push-mark beg nil t)
2525 (message "Could not find end of doc library header.")))
2526 (message "Could not find doc library header start.")
2527 (goto-char here)))))
2528
2529 (defun idlwave-current-routine-fullname ()
2530 (let ((name (idlwave-current-routine)))
2531 (idlwave-make-full-name (nth 2 name) (car name))))
2532
2533 (defun idlwave-current-routine ()
2534 "Return (NAME TYPE CLASS) of current routine."
2535 (idlwave-routines)
2536 (save-excursion
2537 (idlwave-beginning-of-subprogram)
2538 (if (looking-at "[ \t]*\\<\\(pro\\|function\\)\\>\\s-+\\(\\([a-zA-Z0-9$_]+\\)::\\)?\\([a-zA-Z0-9$_]+\\)")
2539 (let* ((type (if (string= (downcase (match-string 1)) "pro")
2540 'pro 'function))
2541 (class (idlwave-sintern-class (match-string 3)))
2542 (name (idlwave-sintern-routine-or-method (match-string 4) class)))
2543 (list name type class)))))
2544
2545 (defvar idlwave-shell-prompt-pattern)
2546 (defun idlwave-beginning-of-statement ()
2547 "Move to beginning of the current statement.
2548 Skips back past statement continuations.
2549 Point is placed at the beginning of the line whether or not this is an
2550 actual statement."
2551 (interactive)
2552 (cond
2553 ((eq major-mode 'idlwave-shell-mode)
2554 (if (re-search-backward idlwave-shell-prompt-pattern nil t)
2555 (goto-char (match-end 0))))
2556 (t
2557 (if (save-excursion (forward-line -1) (idlwave-is-continuation-line))
2558 (idlwave-previous-statement)
2559 (beginning-of-line)))))
2560
2561 (defun idlwave-previous-statement ()
2562 "Moves point to beginning of the previous statement.
2563 Returns t if the current line before moving is the beginning of
2564 the first non-comment statement in the file, and nil otherwise."
2565 (interactive)
2566 (let (first-statement)
2567 (if (not (= (forward-line -1) 0))
2568 ;; first line in file
2569 t
2570 ;; skip blank lines, label lines, include lines and line comments
2571 (while (and
2572 ;; The current statement is the first statement until we
2573 ;; reach another statement.
2574 (setq first-statement
2575 (or
2576 (looking-at idlwave-comment-line-start-skip)
2577 (looking-at "[ \t]*$")
2578 (looking-at (concat "[ \t]*" idlwave-label "[ \t]*$"))
2579 (looking-at "^@")))
2580 (= (forward-line -1) 0)))
2581 ;; skip continuation lines
2582 (while (and
2583 (save-excursion
2584 (forward-line -1)
2585 (idlwave-is-continuation-line))
2586 (= (forward-line -1) 0)))
2587 first-statement)))
2588
2589 (defun idlwave-end-of-statement ()
2590 "Moves point to the end of the current IDL statement.
2591 If not in a statement just moves to end of line. Returns position."
2592 (interactive)
2593 (while (and (idlwave-is-continuation-line)
2594 (= (forward-line 1) 0))
2595 (while (and (idlwave-is-comment-or-empty-line)
2596 (= (forward-line 1) 0))))
2597 (end-of-line)
2598 (point))
2599
2600 (defun idlwave-end-of-statement0 ()
2601 "Moves point to the end of the current IDL statement.
2602 If not in a statement just moves to end of line. Returns position."
2603 (interactive)
2604 (while (and (idlwave-is-continuation-line)
2605 (= (forward-line 1) 0)))
2606 (end-of-line)
2607 (point))
2608
2609 (defun idlwave-next-statement ()
2610 "Moves point to beginning of the next IDL statement.
2611 Returns t if that statement is the last
2612 non-comment IDL statement in the file, and nil otherwise."
2613 (interactive)
2614 (let (last-statement)
2615 (idlwave-end-of-statement)
2616 ;; skip blank lines, label lines, include lines and line comments
2617 (while (and (= (forward-line 1) 0)
2618 ;; The current statement is the last statement until
2619 ;; we reach a new statement.
2620 (setq last-statement
2621 (or
2622 (looking-at idlwave-comment-line-start-skip)
2623 (looking-at "[ \t]*$")
2624 (looking-at (concat "[ \t]*" idlwave-label "[ \t]*$"))
2625 (looking-at "^@")))))
2626 last-statement))
2627
2628 (defun idlwave-skip-multi-commands (&optional lim)
2629 "Skip past multiple commands on a line (with `&')."
2630 (let ((save-point (point)))
2631 (when (re-search-forward ".*&" lim t)
2632 (goto-char (match-end 0))
2633 (if (idlwave-quoted)
2634 (goto-char save-point)
2635 (if (eq (char-after (- (point) 2)) ?&) (goto-char save-point))))
2636 (point)))
2637
2638 (defun idlwave-skip-label-or-case ()
2639 "Skip label or case statement element.
2640 Returns position after label.
2641 If there is no label point is not moved and nil is returned."
2642 ;; Case expressions and labels are terminated by a colon.
2643 ;; So we find the first colon in the line and make sure
2644 ;; - no `?' is before it (might be a ? b : c)
2645 ;; - it is not in a comment
2646 ;; - not in a string constant
2647 ;; - not in parenthesis (like a[0:3])
2648 ;; - not followed by another ":" in explicit class, ala a->b::c
2649 ;; As many in this mode, this function is heuristic and not an exact
2650 ;; parser.
2651 (let* ((start (point))
2652 (eos (save-excursion (idlwave-end-of-statement) (point)))
2653 (end (idlwave-find-key ":" 1 'nomark eos)))
2654 (if (and end
2655 (= (nth 0 (parse-partial-sexp start end)) 0)
2656 (not (string-match "\\?" (buffer-substring start end)))
2657 (not (string-match "^::" (buffer-substring end eos))))
2658 (progn
2659 (forward-char)
2660 (point))
2661 (goto-char start)
2662 nil)))
2663
2664 (defun idlwave-start-of-substatement (&optional pre)
2665 "Move to start of next IDL substatement after point.
2666 Uses the type of the current IDL statement to determine if the next
2667 statement is on a new line or is a subpart of the current statement.
2668 Returns point at start of substatement modulo whitespace.
2669 If optional argument is non-nil move to beginning of current
2670 substatement."
2671 (let ((orig (point))
2672 (eos (idlwave-end-of-statement))
2673 (ifnest 0)
2674 st nst last)
2675 (idlwave-beginning-of-statement)
2676 (idlwave-skip-label-or-case)
2677 (if (< (point) orig)
2678 (idlwave-skip-multi-commands orig))
2679 (setq last (point))
2680 ;; Continue looking for substatements until we are past orig
2681 (while (and (<= (point) orig) (not (eobp)))
2682 (setq last (point))
2683 (setq nst (nth 1 (cdr (setq st (car (idlwave-statement-type))))))
2684 (if (equal (car st) 'if) (setq ifnest (1+ ifnest)))
2685 (cond ((and nst
2686 (idlwave-find-key nst 1 'nomark eos))
2687 (goto-char (match-end 0)))
2688 ((and (> ifnest 0) (idlwave-find-key "\\<else\\>" 1 'nomark eos))
2689 (setq ifnest (1- ifnest))
2690 (goto-char (match-end 0)))
2691 (t (setq ifnest 0)
2692 (idlwave-next-statement))))
2693 (if pre (goto-char last))
2694 ;; If a continuation line starts here, move to next line
2695 (if (looking-at "[ \t]*\\$\\([ \t]*\\(;\\|$\\)\\)")
2696 (beginning-of-line 2))
2697 (point)))
2698
2699 (defun idlwave-statement-type ()
2700 "Return the type of the current IDL statement.
2701 Uses `idlwave-statement-match' to return a cons of (type . point) with
2702 point the ending position where the type was determined. Type is the
2703 association from `idlwave-statement-match', i.e. the cons cell from the
2704 list not just the type symbol. Returns nil if not an identifiable
2705 statement."
2706 (save-excursion
2707 ;; Skip whitespace within a statement which is spaces, tabs, continuations
2708 ;; and possibly comments
2709 (while (looking-at "[ \t]*\\$")
2710 (forward-line 1))
2711 (skip-chars-forward " \t")
2712 (let ((st idlwave-statement-match)
2713 (case-fold-search t))
2714 (while (and (not (looking-at (nth 0 (cdr (car st)))))
2715 (setq st (cdr st))))
2716 (if st
2717 (append st (match-end 0))))))
2718
2719 (defun idlwave-expand-equal (&optional before after is-action)
2720 "Pad '=' with spaces. Two cases: Assignment statement, and keyword
2721 assignment. Which case is determined using
2722 `idlwave-start-of-substatement' and `idlwave-statement-type'. The
2723 equal sign will be surrounded by BEFORE and AFTER blanks. If
2724 `idlwave-pad-keyword' is t then keyword assignment is treated just
2725 like assignment statements. When nil, spaces are removed for keyword
2726 assignment. Any other value keeps the current space around the `='.
2727 Limits in for loops are treated as keyword assignment.
2728
2729 Starting with IDL 6.0, a number of op= assignments are available.
2730 Since ambiguities of the form:
2731
2732 r and= b
2733 rand= b
2734
2735 can occur, alphanumeric operator assignment will never be pre-padded,
2736 only post-padded. You must use a space before these to disambiguate
2737 \(not just for padding, but for proper parsing by IDL too!). Other
2738 operators, such as ##=, ^=, etc., will be pre-padded.
2739
2740 IS-ACTION is ignored.
2741
2742 See `idlwave-surround'."
2743 (if idlwave-surround-by-blank
2744 (let
2745 ((non-an-ops "\\(##\\|\\*\\|\\+\\|-\\|/\\|<\\|>\\|\\^\\)\\=")
2746 (an-ops
2747 "\\s-\\(AND\\|EQ\\|GE\\|GT\\|LE\\|LT\\|MOD\\|NE\\|OR\\|XOR\\)\\=")
2748 (len 1))
2749
2750 (save-excursion
2751 (let ((case-fold-search t))
2752 (backward-char)
2753 (if (or
2754 (re-search-backward non-an-ops nil t)
2755 ;; Why doesn't ##? work for both?
2756 (re-search-backward "\\(#\\)\\=" nil t))
2757 (setq len (1+ (length (match-string 1))))
2758 (when (re-search-backward an-ops nil t)
2759 ;(setq begin nil) ; won't modify begin
2760 (setq len (1+ (length (match-string 1))))))))
2761
2762 (if (eq t idlwave-pad-keyword)
2763 ;; Everything gets padded equally
2764 (idlwave-surround before after len)
2765 ;; Treating keywords/for variables specially...
2766 (let ((st (save-excursion ; To catch "for" variables
2767 (idlwave-start-of-substatement t)
2768 (idlwave-statement-type)))
2769 (what (save-excursion ; To catch keywords
2770 (skip-chars-backward "= \t")
2771 (nth 2 (idlwave-where)))))
2772 (cond ((or (memq what '(function-keyword procedure-keyword))
2773 (memq (caar st) '(for pdef)))
2774 (cond
2775 ((null idlwave-pad-keyword)
2776 (idlwave-surround 0 0)
2777 ) ; remove space
2778 (t))) ; leave any spaces alone
2779 (t (idlwave-surround before after len))))))))
2780
2781
2782 (defun idlwave-indent-and-action (&optional arg)
2783 "Call `idlwave-indent-line' and do expand actions.
2784 With prefix ARG non-nil, indent the entire sub-statement."
2785 (interactive "p")
2786 (save-excursion
2787 (if (and idlwave-expand-generic-end
2788 (re-search-backward "\\<\\(end\\)\\s-*\\="
2789 (max 0 (- (point) 10)) t)
2790 (looking-at "\\(end\\)\\([ \n\t]\\|\\'\\)"))
2791 (progn (goto-char (match-end 1))
2792 ;;Expand the END abbreviation, just as RET or Space would have.
2793 (if abbrev-mode (expand-abbrev)
2794 (idlwave-show-begin)))))
2795 (when (and (not arg) current-prefix-arg)
2796 (setq arg current-prefix-arg)
2797 (setq current-prefix-arg nil))
2798 (if arg
2799 (idlwave-indent-statement)
2800 (idlwave-indent-line t)))
2801
2802 (defun idlwave-indent-line (&optional expand)
2803 "Indents current IDL line as code or as a comment.
2804 The actions in `idlwave-indent-action-table' are performed.
2805 If the optional argument EXPAND is non-nil then the actions in
2806 `idlwave-indent-expand-table' are performed."
2807 (interactive)
2808 ;; Move point out of left margin.
2809 (if (save-excursion
2810 (skip-chars-backward " \t")
2811 (bolp))
2812 (skip-chars-forward " \t"))
2813 (let ((mloc (point-marker)))
2814 (save-excursion
2815 (beginning-of-line)
2816 (if (looking-at idlwave-comment-line-start-skip)
2817 ;; Indentation for a line comment
2818 (progn
2819 (skip-chars-forward " \t")
2820 (idlwave-indent-left-margin (idlwave-comment-hook)))
2821 ;;
2822 ;; Code Line
2823 ;;
2824 ;; Before indenting, run action routines.
2825 ;;
2826 (if (and expand idlwave-do-actions)
2827 (mapcar 'idlwave-do-action idlwave-indent-expand-table))
2828 ;;
2829 (if idlwave-do-actions
2830 (mapcar 'idlwave-do-action idlwave-indent-action-table))
2831 ;;
2832 ;; No longer expand abbrevs on the line. The user can do this
2833 ;; manually using expand-region-abbrevs.
2834 ;;
2835 ;; Indent for code line
2836 ;;
2837 (beginning-of-line)
2838 (if (or
2839 ;; a label line
2840 (looking-at (concat "^" idlwave-label "[ \t]*$"))
2841 ;; a batch command
2842 (looking-at "^[ \t]*@"))
2843 ;; leave flush left
2844 nil
2845 ;; indent the line
2846 (idlwave-indent-left-margin (idlwave-calculate-indent)))
2847 ;; Adjust parallel comment
2848 (end-of-line)
2849 (if (idlwave-in-comment)
2850 ;; Emacs 21 is too smart with fill-column on comment indent
2851 (let ((fill-column (if (fboundp 'comment-indent-new-line)
2852 (1- (frame-width))
2853 fill-column)))
2854 (indent-for-comment)))))
2855 (goto-char mloc)
2856 ;; Get rid of marker
2857 (set-marker mloc nil)))
2858
2859 (defun idlwave-do-action (action)
2860 "Perform an action repeatedly on a line. ACTION is a list (REG
2861 . FUNC). REG is a regular expression. FUNC is either a function name
2862 to be called with `funcall' or a list to be evaluated with `eval'.
2863 The action performed by FUNC should leave point after the match for
2864 REG - otherwise an infinite loop may be entered. FUNC is always
2865 passed a final argument of 'is-action, so it can discriminate between
2866 being run as an action, or a key binding"
2867 (let ((action-key (car action))
2868 (action-routine (cdr action)))
2869 (beginning-of-line)
2870 (while (idlwave-look-at action-key)
2871 (if (listp action-routine)
2872 (eval (append action-routine '('is-action)))
2873 (funcall action-routine 'is-action)))))
2874
2875 (defun idlwave-indent-to (col &optional min)
2876 "Indent from point with spaces until column COL.
2877 Inserts space before markers at point."
2878 (if (not min) (setq min 0))
2879 (insert-before-markers
2880 (make-string (max min (- col (current-column))) ?\ )))
2881
2882 (defun idlwave-indent-left-margin (col)
2883 "Indent the current line to column COL.
2884 Indents such that first non-whitespace character is at column COL
2885 Inserts spaces before markers at point."
2886 (save-excursion
2887 (beginning-of-line)
2888 (delete-horizontal-space)
2889 (idlwave-indent-to col)))
2890
2891 (defun idlwave-indent-subprogram ()
2892 "Indents program unit which contains point."
2893 (interactive)
2894 (save-excursion
2895 (idlwave-end-of-statement)
2896 (idlwave-beginning-of-subprogram)
2897 (let ((beg (point)))
2898 (idlwave-forward-block)
2899 (message "Indenting subprogram...")
2900 (indent-region beg (point) nil))
2901 (message "Indenting subprogram...done.")))
2902
2903 (defun idlwave-indent-statement ()
2904 "Indent current statement, including all continuation lines."
2905 (interactive)
2906 (save-excursion
2907 (idlwave-beginning-of-statement)
2908 (let ((beg (point)))
2909 (idlwave-end-of-statement)
2910 (indent-region beg (point) nil))))
2911
2912 (defun idlwave-calculate-indent ()
2913 "Return appropriate indentation for current line as IDL code."
2914 (save-excursion
2915 (beginning-of-line)
2916 (cond
2917 ;; Check for beginning of unit - main (beginning of buffer), pro, or
2918 ;; function
2919 ((idlwave-look-at idlwave-begin-unit-reg)
2920 0)
2921 ;; Check for continuation line
2922 ((save-excursion
2923 (and (= (forward-line -1) 0)
2924 (idlwave-is-continuation-line)))
2925 (idlwave-calculate-cont-indent))
2926 ;; calculate indent based on previous and current statements
2927 (t (let* (beg-prev-pos
2928 (the-indent
2929 ;; calculate indent based on previous statement
2930 (save-excursion
2931 (cond
2932 ;; Beginning of file
2933 ((prog1
2934 (idlwave-previous-statement)
2935 (setq beg-prev-pos (point)))
2936 0)
2937 ;; Main block
2938 ((idlwave-look-at idlwave-begin-unit-reg t)
2939 (+ (idlwave-current-statement-indent)
2940 idlwave-main-block-indent))
2941 ;; Begin block
2942 ((idlwave-look-at idlwave-begin-block-reg t)
2943 (+ (idlwave-min-current-statement-indent)
2944 idlwave-block-indent))
2945 ;; End Block
2946 ((idlwave-look-at idlwave-end-block-reg t)
2947 (progn
2948 ;; Match to the *beginning* of the block opener
2949 (goto-char beg-prev-pos)
2950 (idlwave-block-jump-out -1 'nomark) ; go to begin block
2951 (idlwave-min-current-statement-indent)))
2952 ;; idlwave-end-offset
2953 ;; idlwave-block-indent))
2954
2955 ;; Default to current indent
2956 ((idlwave-current-statement-indent))))))
2957 ;; adjust the indentation based on the current statement
2958 (cond
2959 ;; End block
2960 ((idlwave-look-at idlwave-end-block-reg)
2961 (+ the-indent idlwave-end-offset))
2962 (the-indent)))))))
2963
2964 ;;
2965 ;; Parentheses indent
2966 ;;
2967
2968 (defun idlwave-calculate-paren-indent (beg-reg end-reg close-exp)
2969 "Calculate the continuation indent inside a paren group.
2970 Returns a cons-cell with (open . indent), where open is the
2971 location of the open paren"
2972 (let ((open (nth 1 (parse-partial-sexp beg-reg end-reg))))
2973 ;; Found an innermost open paren.
2974 (when open
2975 (goto-char open)
2976 ;; Line up with next word unless this is a closing paren.
2977 (cons open
2978 (cond
2979 ;; Plain Kernighan-style nested indent
2980 (idlwave-indent-parens-nested
2981 (+ idlwave-continuation-indent (idlwave-current-indent)))
2982
2983 ;; This is a closed paren - line up under open paren.
2984 (close-exp
2985 (current-column))
2986
2987 ;; Empty (or just comment) follows -- revert to basic indent
2988 ((progn
2989 ;; Skip paren
2990 (forward-char 1)
2991 (looking-at "[ \t$]*\\(;.*\\)?$"))
2992 nil)
2993
2994 ;; Line up with first word after any blank space
2995 ((progn
2996 (skip-chars-forward " \t")
2997 (current-column))))))))
2998
2999 (defun idlwave-calculate-cont-indent ()
3000 "Calculates the IDL continuation indent column from the previous
3001 statement. Note that here previous statement usually means the
3002 beginning of the current statement if this statement is a continuation
3003 of the previous line. Various special types of continuations,
3004 including assignments, routine definitions, and parenthetical
3005 groupings, are treated separately."
3006 (save-excursion
3007 (let* ((case-fold-search t)
3008 (end-reg (progn (beginning-of-line) (point)))
3009 (beg-last-statement (save-excursion (idlwave-previous-statement)
3010 (point)))
3011 (beg-reg (progn (idlwave-start-of-substatement 'pre)
3012 (if (eq (line-beginning-position) end-reg)
3013 (goto-char beg-last-statement)
3014 (point))))
3015 (basic-indent (+ (idlwave-min-current-statement-indent end-reg)
3016 idlwave-continuation-indent))
3017 fancy-nonparen-indent fancy-paren-indent)
3018 (cond
3019 ;; Align then with its matching if, etc.
3020 ((let ((matchers '(("\\<if\\>" . "[ \t]*then")
3021 ("\\<\\(if\\|end\\(if\\)?\\)\\>" . "[ \t]*else")
3022 ("\\<\\(for\\|while\\)\\>" . "[ \t]*do")
3023 ("\\<\\(repeat\\|end\\(rep\\)?\\)\\>" .
3024 "[ \t]*until")
3025 ("\\<case\\>" . "[ \t]*of")))
3026 match cont-re)
3027 (goto-char end-reg)
3028 (and
3029 (setq cont-re
3030 (catch 'exit
3031 (while (setq match (car matchers))
3032 (if (looking-at (cdr match))
3033 (throw 'exit (car match)))
3034 (setq matchers (cdr matchers)))))
3035 (idlwave-find-key cont-re -1 'nomark beg-last-statement)))
3036 (if (looking-at "end") ;; that one's special
3037 (- (idlwave-current-indent)
3038 (+ idlwave-block-indent idlwave-end-offset))
3039 (idlwave-current-indent)))
3040
3041 ;; Indent in from the previous line for continuing statements
3042 ((let ((matchers '("\\<then\\>"
3043 "\\<do\\>"
3044 "\\<repeat\\>"
3045 "\\<else\\>"))
3046 match)
3047 (catch 'exit
3048 (goto-char end-reg)
3049 (if (/= (forward-line -1) 0)
3050 (throw 'exit nil))
3051 (while (setq match (car matchers))
3052 (if (looking-at (concat ".*" match "[ \t]*\\$[ \t]*"
3053 "\\(;.*\\)?$"))
3054 (throw 'exit t))
3055 (setq matchers (cdr matchers)))))
3056 (+ idlwave-continuation-indent (idlwave-current-indent)))
3057
3058 ;; Parenthetical indent, either traditional or Kernighan style
3059 ((setq fancy-paren-indent
3060 (let* ((end-reg end-reg)
3061 (close-exp (progn
3062 (goto-char end-reg)
3063 (skip-chars-forward " \t")
3064 (looking-at "\\s)")))
3065 indent-cons)
3066 (catch 'loop
3067 (while (setq indent-cons (idlwave-calculate-paren-indent
3068 beg-reg end-reg close-exp))
3069 ;; First permitted containing paren
3070 (if (or
3071 idlwave-indent-to-open-paren
3072 idlwave-indent-parens-nested
3073 (null (cdr indent-cons))
3074 (< (- (cdr indent-cons) basic-indent)
3075 idlwave-max-extra-continuation-indent))
3076 (throw 'loop (cdr indent-cons)))
3077 (setq end-reg (car indent-cons))))))
3078 fancy-paren-indent)
3079
3080 ;; A continued assignment, or procedure call/definition
3081 ((and
3082 (> idlwave-max-extra-continuation-indent 0)
3083 (setq fancy-nonparen-indent
3084 (progn
3085 (goto-char beg-reg)
3086 (while (idlwave-look-at "&")) ; skip continued statements
3087 (cond
3088 ;; A continued Procedure call or definition
3089 ((progn
3090 (idlwave-look-at "^[ \t]*\\(pro\\|function\\)") ;skip over
3091 (looking-at "[ \t]*\\([a-zA-Z0-9.$_]+[ \t]*->[ \t]*\\)?[a-zA-Z][:a-zA-Z0-9$_]*[ \t]*\\(,\\)[ \t]*"))
3092 (goto-char (match-end 0))
3093 ;; Comment only, or blank line with "$"? Basic indent.
3094 (if (save-match-data (looking-at "[ \t$]*\\(;.*\\)?$"))
3095 nil
3096 (current-column)))
3097
3098 ;; Continued assignment (with =):
3099 ((catch 'assign ;
3100 (while (looking-at "[^=\n\r]*\\(=\\)[ \t]*")
3101 (goto-char (match-end 0))
3102 (if (null (idlwave-what-function beg-reg))
3103 (throw 'assign t))))
3104 (unless (or
3105 (idlwave-in-quote)
3106 (looking-at "[ \t$]*\\(;.*\\)?$") ; use basic
3107 (save-excursion
3108 (goto-char beg-last-statement)
3109 (eq (caar (idlwave-statement-type)) 'for)))
3110 (current-column))))))
3111 (< (- fancy-nonparen-indent basic-indent)
3112 idlwave-max-extra-continuation-indent))
3113 (if fancy-paren-indent ;calculated but disallowed paren indent
3114 (+ fancy-nonparen-indent idlwave-continuation-indent)
3115 fancy-nonparen-indent))
3116
3117 ;; Basic indent, by default
3118 (t basic-indent)))))
3119
3120
3121
3122 (defun idlwave-find-key (key-re &optional dir nomark limit)
3123 "Move to next match of the regular expression KEY-RE.
3124 Matches inside comments or string constants will be ignored.
3125 If DIR is negative, the search will be backwards.
3126 At a successful match, the mark is pushed unless NOMARK is non-nil.
3127 Searches are limited to LIMIT.
3128 Searches are case-insensitive and use a special syntax table which
3129 treats `$' and `_' as word characters.
3130 Return value is the beginning of the match or (in case of failure) nil."
3131 (setq dir (or dir 0))
3132 (let ((case-fold-search t)
3133 (search-func (if (> dir 0) 're-search-forward 're-search-backward))
3134 found)
3135 (idlwave-with-special-syntax
3136 (save-excursion
3137 (catch 'exit
3138 (while (funcall search-func key-re limit t)
3139 (if (not (idlwave-quoted))
3140 (throw 'exit (setq found (match-beginning 0)))
3141 (if (or (and (> dir 0) (eobp))
3142 (and (< dir 0) (bobp)))
3143 (throw 'exit nil)))))))
3144 (if found
3145 (progn
3146 (if (not nomark) (push-mark))
3147 (goto-char found)
3148 found)
3149 nil)))
3150
3151 (defun idlwave-block-jump-out (&optional dir nomark)
3152 "When optional argument DIR is non-negative, move forward to end of
3153 current block using the `idlwave-begin-block-reg' and `idlwave-end-block-reg'
3154 regular expressions. When DIR is negative, move backwards to block beginning.
3155 Recursively calls itself to skip over nested blocks. DIR defaults to
3156 forward. Calls `push-mark' unless the optional argument NOMARK is
3157 non-nil. Movement is limited by the start of program units because of
3158 possibility of unbalanced blocks."
3159 (interactive "P")
3160 (or dir (setq dir 0))
3161 (let* ((here (point))
3162 (case-fold-search t)
3163 (limit (if (>= dir 0) (point-max) (point-min)))
3164 (block-limit (if (>= dir 0)
3165 idlwave-begin-block-reg
3166 idlwave-end-block-reg))
3167 found
3168 (block-reg (concat idlwave-begin-block-reg "\\|"
3169 idlwave-end-block-reg))
3170 (unit-limit (or (save-excursion
3171 (if (< dir 0)
3172 (idlwave-find-key
3173 idlwave-begin-unit-reg dir t limit)
3174 (end-of-line)
3175 (idlwave-find-key
3176 idlwave-end-unit-reg dir t limit)))
3177 limit)))
3178 (if (>= dir 0) (end-of-line)) ;Make sure we are in current block
3179 (if (setq found (idlwave-find-key block-reg dir t unit-limit))
3180 (while (and found (looking-at block-limit))
3181 (if (>= dir 0) (forward-word 1))
3182 (idlwave-block-jump-out dir t)
3183 (setq found (idlwave-find-key block-reg dir t unit-limit))))
3184 (if (not nomark) (push-mark here))
3185 (if (not found) (goto-char unit-limit)
3186 (if (>= dir 0) (forward-word 1)))))
3187
3188 (defun idlwave-min-current-statement-indent (&optional end-reg)
3189 "The minimum indent in the current statement."
3190 (idlwave-beginning-of-statement)
3191 (if (not (idlwave-is-continuation-line))
3192 (idlwave-current-indent)
3193 (let ((min (idlwave-current-indent)) comm-or-empty)
3194 (while (and (= (forward-line 1) 0)
3195 (or (setq comm-or-empty (idlwave-is-comment-or-empty-line))
3196 (idlwave-is-continuation-line))
3197 (or (null end-reg) (< (point) end-reg)))
3198 (unless comm-or-empty (setq min (min min (idlwave-current-indent)))))
3199 (if (or comm-or-empty (and end-reg (>= (point) end-reg)))
3200 min
3201 (min min (idlwave-current-indent))))))
3202
3203 (defun idlwave-current-statement-indent (&optional last-line)
3204 "Return indentation of the current statement.
3205 If in a statement, moves to beginning of statement before finding indent."
3206 (if last-line
3207 (idlwave-end-of-statement)
3208 (idlwave-beginning-of-statement))
3209 (idlwave-current-indent))
3210
3211 (defun idlwave-current-indent ()
3212 "Return the column of the indentation of the current line.
3213 Skips any whitespace. Returns 0 if the end-of-line follows the whitespace."
3214 (save-excursion
3215 (beginning-of-line)
3216 (skip-chars-forward " \t")
3217 ;; if we are at the end of blank line return 0
3218 (cond ((eolp) 0)
3219 ((current-column)))))
3220
3221 (defun idlwave-is-continuation-line ()
3222 "Tests if current line is continuation line.
3223 Blank or comment-only lines following regular continuation lines (with
3224 `$') count as continuations too."
3225 (let (p)
3226 (save-excursion
3227 (or
3228 (idlwave-look-at "\\<\\$")
3229 (catch 'loop
3230 (while (and (looking-at "^[ \t]*\\(;.*\\)?$")
3231 (eq (forward-line -1) 0))
3232 (if (setq p (idlwave-look-at "\\<\\$")) (throw 'loop p))))))))
3233
3234 (defun idlwave-is-comment-line ()
3235 "Tests if the current line is a comment line."
3236 (save-excursion
3237 (beginning-of-line 1)
3238 (looking-at "[ \t]*;")))
3239
3240 (defun idlwave-is-comment-or-empty-line ()
3241 "Tests if the current line is a comment line."
3242 (save-excursion
3243 (beginning-of-line 1)
3244 (looking-at "[ \t]*[;\n]")))
3245
3246 (defun idlwave-look-at (regexp &optional cont beg)
3247 "Searches current line from current point for REGEXP.
3248 If optional argument CONT is non-nil, searches to the end of
3249 the current statement.
3250 If optional arg BEG is non-nil, search starts from the beginning of the
3251 current statement.
3252 Ignores matches that end in a comment or inside a string expression.
3253 Returns point if successful, nil otherwise.
3254 This function produces unexpected results if REGEXP contains quotes or
3255 a comment delimiter. The search is case insensitive.
3256 If successful leaves point after the match, otherwise, does not move point."
3257 (let ((here (point))
3258 (case-fold-search t)
3259 (eos (save-excursion
3260 (if cont (idlwave-end-of-statement) (end-of-line))
3261 (point)))
3262 found)
3263 (idlwave-with-special-syntax
3264 (if beg (idlwave-beginning-of-statement))
3265 (while (and (setq found (re-search-forward regexp eos t))
3266 (idlwave-quoted))))
3267 (if (not found) (goto-char here))
3268 found))
3269
3270 (defun idlwave-fill-paragraph (&optional nohang)
3271 "Fills paragraphs in comments.
3272 A paragraph is made up of all contiguous lines having the same comment
3273 leader (the leading whitespace before the comment delimiter and the
3274 comment delimiter). In addition, paragraphs are separated by blank
3275 line comments. The indentation is given by the hanging indent of the
3276 first line, otherwise by the minimum indentation of the lines after
3277 the first line. The indentation of the first line does not change.
3278 Does not effect code lines. Does not fill comments on the same line
3279 with code. The hanging indent is given by the end of the first match
3280 matching `idlwave-hang-indent-regexp' on the paragraph's first line . If the
3281 optional argument NOHANG is non-nil then the hanging indent is
3282 ignored."
3283 (interactive "P")
3284 ;; check if this is a line comment
3285 (if (save-excursion
3286 (beginning-of-line)
3287 (skip-chars-forward " \t")
3288 (looking-at comment-start))
3289 (let
3290 ((indent 999)
3291 pre here diff fill-prefix-reg bcl first-indent
3292 hang start end)
3293 ;; Change tabs to spaces in the surrounding paragraph.
3294 ;; The surrounding paragraph will be the largest containing block of
3295 ;; contiguous line comments. Thus, we may be changing tabs in
3296 ;; a much larger area than is needed, but this is the easiest
3297 ;; brute force way to do it.
3298 ;;
3299 ;; This has the undesirable side effect of replacing the tabs
3300 ;; permanently without the user's request or knowledge.
3301 (save-excursion
3302 (backward-paragraph)
3303 (setq start (point)))
3304 (save-excursion
3305 (forward-paragraph)
3306 (setq end (point)))
3307 (untabify start end)
3308 ;;
3309 (setq here (point))
3310 (beginning-of-line)
3311 (setq bcl (point))
3312 (re-search-forward
3313 (concat "^[ \t]*" comment-start "+")
3314 (save-excursion (end-of-line) (point))
3315 t)
3316 ;; Get the comment leader on the line and its length
3317 (setq pre (current-column))
3318 ;; the comment leader is the indentation plus exactly the
3319 ;; number of consecutive ";".
3320 (setq fill-prefix-reg
3321 (concat
3322 (setq fill-prefix
3323 (regexp-quote
3324 (buffer-substring (save-excursion
3325 (beginning-of-line) (point))
3326 (point))))
3327 "[^;]"))
3328
3329 ;; Mark the beginning and end of the paragraph
3330 (goto-char bcl)
3331 (while (and (looking-at fill-prefix-reg)
3332 (not (looking-at paragraph-separate))
3333 (not (bobp)))
3334 (forward-line -1))
3335 ;; Move to first line of paragraph
3336 (if (/= (point) bcl)
3337 (forward-line 1))
3338 (setq start (point))
3339 (goto-char bcl)
3340 (while (and (looking-at fill-prefix-reg)
3341 (not (looking-at paragraph-separate))
3342 (not (eobp)))
3343 (forward-line 1))
3344 (beginning-of-line)
3345 (if (or (not (looking-at fill-prefix-reg))
3346 (looking-at paragraph-separate))
3347 (forward-line -1))
3348 (end-of-line)
3349 ;; if at end of buffer add a newline (need this because
3350 ;; fill-region needs END to be at the beginning of line after
3351 ;; the paragraph or it will add a line).
3352 (if (eobp)
3353 (progn (insert ?\n) (backward-char 1)))
3354 ;; Set END to the beginning of line after the paragraph
3355 ;; END is calculated as distance from end of buffer
3356 (setq end (- (point-max) (point) 1))
3357 ;;
3358 ;; Calculate the indentation for the paragraph.
3359 ;;
3360 ;; In the following while statements, after one iteration
3361 ;; point will be at the beginning of a line in which case
3362 ;; the while will not be executed for the
3363 ;; the first paragraph line and thus will not affect the
3364 ;; indentation.
3365 ;;
3366 ;; First check to see if indentation is based on hanging indent.
3367 (if (and (not nohang) idlwave-hanging-indent
3368 (setq hang
3369 (save-excursion
3370 (goto-char start)
3371 (idlwave-calc-hanging-indent))))
3372 ;; Adjust lines of paragraph by inserting spaces so that
3373 ;; each line's indent is at least as great as the hanging
3374 ;; indent. This is needed for fill-paragraph to work with
3375 ;; a fill-prefix.
3376 (progn
3377 (setq indent hang)
3378 (beginning-of-line)
3379 (while (> (point) start)
3380 (re-search-forward comment-start-skip
3381 (save-excursion (end-of-line) (point))
3382 t)
3383 (if (> (setq diff (- indent (current-column))) 0)
3384 (progn
3385 (if (>= here (point))
3386 ;; adjust the original location for the
3387 ;; inserted text.
3388 (setq here (+ here diff)))
3389 (insert (make-string diff ?\ ))))
3390 (forward-line -1))
3391 )
3392
3393 ;; No hang. Instead find minimum indentation of paragraph
3394 ;; after first line.
3395 ;; For the following while statement, since START is at the
3396 ;; beginning of line and END is at the end of line
3397 ;; point is greater than START at least once (which would
3398 ;; be the case for a single line paragraph).
3399 (while (> (point) start)
3400 (beginning-of-line)
3401 (setq indent
3402 (min indent
3403 (progn
3404 (re-search-forward
3405 comment-start-skip
3406 (save-excursion (end-of-line) (point))
3407 t)
3408 (current-column))))
3409 (forward-line -1))
3410 )
3411 (setq fill-prefix (concat fill-prefix
3412 (make-string (- indent pre)
3413 ?\ )))
3414 ;; first-line indent
3415 (setq first-indent
3416 (max
3417 (progn
3418 (re-search-forward
3419 comment-start-skip
3420 (save-excursion (end-of-line) (point))
3421 t)
3422 (current-column))
3423 indent))
3424
3425 ;; try to keep point at its original place
3426 (goto-char here)
3427
3428 ;; In place of the more modern fill-region-as-paragraph, a hack
3429 ;; to keep whitespace untouched on the first line within the
3430 ;; indent length and to preserve any indent on the first line
3431 ;; (first indent).
3432 (save-excursion
3433 (setq diff
3434 (buffer-substring start (+ start first-indent -1)))
3435 (subst-char-in-region start (+ start first-indent -1) ?\ ?~ nil)
3436 (fill-region-as-paragraph
3437 start
3438 (- (point-max) end)
3439 (current-justification)
3440 nil)
3441 (delete-region start (+ start first-indent -1))
3442 (goto-char start)
3443 (insert diff))
3444 ;; When we want the point at the beginning of the comment
3445 ;; body fill-region will put it at the beginning of the line.
3446 (if (bolp) (skip-chars-forward (concat " \t" comment-start)))
3447 (setq fill-prefix nil))))
3448
3449 (defun idlwave-calc-hanging-indent ()
3450 "Calculate the position of the hanging indent for the comment
3451 paragraph. The hanging indent position is given by the first match
3452 with the `idlwave-hang-indent-regexp'. If `idlwave-use-last-hang-indent' is
3453 non-nil then use last occurrence matching `idlwave-hang-indent-regexp' on
3454 the line.
3455 If not found returns nil."
3456 (if idlwave-use-last-hang-indent
3457 (save-excursion
3458 (end-of-line)
3459 (if (re-search-backward
3460 idlwave-hang-indent-regexp
3461 (save-excursion (beginning-of-line) (point))
3462 t)
3463 (+ (current-column) (length idlwave-hang-indent-regexp))))
3464 (save-excursion
3465 (beginning-of-line)
3466 (if (re-search-forward
3467 idlwave-hang-indent-regexp
3468 (save-excursion (end-of-line) (point))
3469 t)
3470 (current-column)))))
3471
3472 (defun idlwave-auto-fill ()
3473 "Called to break lines in auto fill mode.
3474 Only fills non-comment lines if `idlwave-fill-comment-line-only' is
3475 non-nil. Places a continuation character at the end of the line if
3476 not in a comment. Splits strings with IDL concatenation operator `+'
3477 if `idlwave-auto-fill-split-string' is non-nil."
3478 (if (<= (current-column) fill-column)
3479 nil ; do not to fill
3480 (if (or (not idlwave-fill-comment-line-only)
3481 (save-excursion
3482 ;; Check for comment line
3483 (beginning-of-line)
3484 (looking-at idlwave-comment-line-start-skip)))
3485 (let (beg)
3486 (idlwave-indent-line)
3487 ;; Prevent actions do-auto-fill which calls indent-line-function.
3488 (let (idlwave-do-actions
3489 (fill-nobreak-predicate
3490 (if (and (idlwave-in-quote)
3491 idlwave-auto-fill-split-string)
3492 (lambda () ;; We'll need 5 spaces for " ' + $"
3493 (<= (- fill-column (current-column)) 5)
3494 ))))
3495 (do-auto-fill))
3496 (save-excursion
3497 (end-of-line 0)
3498 ;; Indent the split line
3499 (idlwave-indent-line)
3500 )
3501 (if (save-excursion
3502 (beginning-of-line)
3503 (looking-at idlwave-comment-line-start-skip))
3504 ;; A continued line comment
3505 ;; We treat continued line comments as part of a comment
3506 ;; paragraph. So we check for a hanging indent.
3507 (if idlwave-hanging-indent
3508 (let ((here (- (point-max) (point)))
3509 (indent
3510 (save-excursion
3511 (forward-line -1)
3512 (idlwave-calc-hanging-indent))))
3513 (if indent
3514 (progn
3515 ;; Remove whitespace between comment delimiter and
3516 ;; text, insert spaces for appropriate indentation.
3517 (beginning-of-line)
3518 (re-search-forward
3519 comment-start-skip
3520 (save-excursion (end-of-line) (point)) t)
3521 (delete-horizontal-space)
3522 (idlwave-indent-to indent)
3523 (goto-char (- (point-max) here)))
3524 )))
3525 ;; Split code or comment?
3526 (if (save-excursion
3527 (end-of-line 0)
3528 (idlwave-in-comment))
3529 ;; Splitting a non-full-line comment.
3530 ;; Insert the comment delimiter from split line
3531 (progn
3532 (save-excursion
3533 (beginning-of-line)
3534 (skip-chars-forward " \t")
3535 ;; Insert blank to keep off beginning of line
3536 (insert " "
3537 (save-excursion
3538 (forward-line -1)
3539 (buffer-substring (idlwave-goto-comment)
3540 (progn
3541 (skip-chars-forward "; ")
3542 (point))))))
3543 (idlwave-indent-line))
3544 ;; Split code line - add continuation character
3545 (save-excursion
3546 (end-of-line 0)
3547 ;; Check to see if we split a string
3548 (if (and (setq beg (idlwave-in-quote))
3549 idlwave-auto-fill-split-string)
3550 ;; Split the string and concatenate.
3551 ;; The first extra space is for the space
3552 ;; the line was split. That space was removed.
3553 (insert " " (char-after beg) " +"))
3554 (insert " $"))
3555 (if beg
3556 (if idlwave-auto-fill-split-string
3557 ;; Make the second part of continued string
3558 (save-excursion
3559 (beginning-of-line)
3560 (skip-chars-forward " \t")
3561 (insert (char-after beg)))
3562 ;; Warning
3563 (beep)
3564 (message "Warning: continuation inside a string.")))
3565 ;; Although do-auto-fill (via indent-new-comment-line) calls
3566 ;; idlwave-indent-line for the new line, re-indent again
3567 ;; because of the addition of the continuation character.
3568 (idlwave-indent-line))
3569 )))))
3570
3571 (defun idlwave-auto-fill-mode (arg)
3572 "Toggle auto-fill mode for IDL mode.
3573 With arg, turn auto-fill mode on if arg is positive.
3574 In auto-fill mode, inserting a space at a column beyond `fill-column'
3575 automatically breaks the line at a previous space."
3576 (interactive "P")
3577 (prog1 (set idlwave-fill-function
3578 (if (if (null arg)
3579 (not (symbol-value idlwave-fill-function))
3580 (> (prefix-numeric-value arg) 0))
3581 'idlwave-auto-fill
3582 nil))
3583 ;; update mode-line
3584 (set-buffer-modified-p (buffer-modified-p))))
3585
3586 ;(defun idlwave-fill-routine-call ()
3587 ; "Fill a routine definition or statement, indenting appropriately."
3588 ; (let ((where (idlwave-where)))))
3589
3590
3591 (defun idlwave-doc-header (&optional nomark )
3592 "Insert a documentation header at the beginning of the unit.
3593 Inserts the value of the variable idlwave-file-header. Sets mark before
3594 moving to do insertion unless the optional prefix argument NOMARK
3595 is non-nil."
3596 (interactive "P")
3597 (or nomark (push-mark))
3598 ;; make sure we catch the current line if it begins the unit
3599 (if idlwave-header-to-beginning-of-file
3600 (goto-char (point-min))
3601 (end-of-line)
3602 (idlwave-beginning-of-subprogram)
3603 (beginning-of-line)
3604 ;; skip function or procedure line
3605 (if (idlwave-look-at "\\<\\(pro\\|function\\)\\>")
3606 (progn
3607 (idlwave-end-of-statement)
3608 (if (> (forward-line 1) 0) (insert "\n")))))
3609 (let ((pos (point)))
3610 (if idlwave-file-header
3611 (cond ((car idlwave-file-header)
3612 (insert-file-contents (car idlwave-file-header)))
3613 ((stringp (car (cdr idlwave-file-header)))
3614 (insert (car (cdr idlwave-file-header))))))
3615 (goto-char pos)))
3616
3617 (defun idlwave-default-insert-timestamp ()
3618 "Default timestamp insertion function"
3619 (insert (current-time-string))
3620 (insert ", " (user-full-name))
3621 (if (boundp 'user-mail-address)
3622 (insert " <" user-mail-address ">")
3623 (insert " <" (user-login-name) "@" (system-name) ">"))
3624 ;; Remove extra spaces from line
3625 (idlwave-fill-paragraph)
3626 ;; Insert a blank line comment to separate from the date entry -
3627 ;; will keep the entry from flowing onto date line if re-filled.
3628 (insert "\n;\n;\t\t"))
3629
3630 (defun idlwave-doc-modification ()
3631 "Insert a brief modification log at the beginning of the current program.
3632 Looks for an occurrence of the value of user variable
3633 `idlwave-doc-modifications-keyword' if non-nil. Inserts time and user name
3634 and places the point for the user to add a log. Before moving, saves
3635 location on mark ring so that the user can return to previous point."
3636 (interactive)
3637 (push-mark)
3638 (let* (beg end)
3639 (if (and (or (re-search-backward idlwave-doclib-start nil t)
3640 (progn
3641 (goto-char (point-min))
3642 (re-search-forward idlwave-doclib-start nil t)))
3643 (setq beg (match-beginning 0))
3644 (re-search-forward idlwave-doclib-end nil t)
3645 (setq end (match-end 0)))
3646 (progn
3647 (goto-char beg)
3648 (if (re-search-forward
3649 (concat idlwave-doc-modifications-keyword ":")
3650 end t)
3651 (end-of-line)
3652 (goto-char end)
3653 (end-of-line -1)
3654 (insert "\n" comment-start "\n")
3655 (insert comment-start " " idlwave-doc-modifications-keyword ":"))
3656 (insert "\n;\n;\t")
3657 (run-hooks 'idlwave-timestamp-hook))
3658 (error "No valid DOCLIB header"))))
3659
3660
3661 ;;; CJC 3/16/93
3662 ;;; Interface to expand-region-abbrevs which did not work when the
3663 ;;; abbrev hook associated with an abbrev moves point backwards
3664 ;;; after abbrev expansion, e.g., as with the abbrev '.n'.
3665 ;;; The original would enter an infinite loop in attempting to expand
3666 ;;; .n (it would continually expand and unexpand the abbrev without expanding
3667 ;;; because the point would keep going back to the beginning of the
3668 ;;; abbrev instead of to the end of the abbrev). We now keep the
3669 ;;; abbrev hook from moving backwards.
3670 ;;;
3671 (defun idlwave-expand-region-abbrevs (start end)
3672 "Expand each abbrev occurrence in the region.
3673 Calling from a program, arguments are START END."
3674 (interactive "r")
3675 (save-excursion
3676 (goto-char (min start end))
3677 (let ((idlwave-show-block nil) ;Do not blink
3678 (idlwave-abbrev-move nil)) ;Do not move
3679 (expand-region-abbrevs start end 'noquery))))
3680
3681 (defun idlwave-quoted ()
3682 "Returns t if point is in a comment or quoted string.
3683 nil otherwise."
3684 (or (idlwave-in-comment) (idlwave-in-quote)))
3685
3686 (defun idlwave-in-quote ()
3687 "Returns location of the opening quote
3688 if point is in a IDL string constant, nil otherwise.
3689 Ignores comment delimiters on the current line.
3690 Properly handles nested quotation marks and octal
3691 constants - a double quote followed by an octal digit."
3692 ;;; Treat an octal inside an apostrophe to be a normal string. Treat a
3693 ;;; double quote followed by an octal digit to be an octal constant
3694 ;;; rather than a string. Therefore, there is no terminating double
3695 ;;; quote.
3696 (save-excursion
3697 ;; Because single and double quotes can quote each other we must
3698 ;; search for the string start from the beginning of line.
3699 (let* ((start (point))
3700 (eol (progn (end-of-line) (point)))
3701 (bq (progn (beginning-of-line) (point)))
3702 (endq (point))
3703 (data (match-data))
3704 delim
3705 found)
3706 (while (< endq start)
3707 ;; Find string start
3708 ;; Don't find an octal constant beginning with a double quote
3709 (if (re-search-forward "[\"']" eol 'lim)
3710 ;; Find the string end.
3711 ;; In IDL, two consecutive delimiters after the start of a
3712 ;; string act as an
3713 ;; escape for the delimiter in the string.
3714 ;; Two consecutive delimiters alone (i.e., not after the
3715 ;; start of a string) is the null string.
3716 (progn
3717 ;; Move to position after quote
3718 (goto-char (1+ (match-beginning 0)))
3719 (setq bq (1- (point)))
3720 ;; Get the string delimiter
3721 (setq delim (char-to-string (preceding-char)))
3722 ;; Check for null string
3723 (if (looking-at delim)
3724 (progn (setq endq (point)) (forward-char 1))
3725 ;; Look for next unpaired delimiter
3726 (setq found (search-forward delim eol 'lim))
3727 (while (looking-at delim)
3728 (forward-char 1)
3729 (setq found (search-forward delim eol 'lim)))
3730 (if found
3731 (setq endq (- (point) 1))
3732 (setq endq (point)))
3733 ))
3734 (progn (setq bq (point)) (setq endq (point)))))
3735 (store-match-data data)
3736 ;; return string beginning position or nil
3737 (if (> start bq) bq))))
3738
3739 (defun idlwave-is-pointer-dereference (&optional limit)
3740 "Determines if the character after point is a pointer dereference *."
3741 (let ((pos (point)))
3742 (and
3743 (eq (char-after) ?\*)
3744 (not (idlwave-in-quote))
3745 (save-excursion
3746 (forward-char)
3747 (re-search-backward (concat "\\(" idlwave-idl-keywords
3748 "\\|[[(*+-/=,^><]\\)\\s-*\\*") limit t)))))
3749
3750
3751 ;; Statement templates
3752
3753 ;; Replace these with a general template function, something like
3754 ;; expand.el (I think there was also something with a name similar to
3755 ;; dmacro.el)
3756
3757 (defun idlwave-template (s1 s2 &optional prompt noindent)
3758 "Build a template with optional prompt expression.
3759
3760 Opens a line if point is not followed by a newline modulo intervening
3761 whitespace. S1 and S2 are strings. S1 is inserted at point followed
3762 by S2. Point is inserted between S1 and S2. The case of S1 and S2 is
3763 adjusted according to `idlwave-abbrev-change-case'. If optional argument
3764 PROMPT is a string then it is displayed as a message in the
3765 minibuffer. The PROMPT serves as a reminder to the user of an
3766 expression to enter.
3767
3768 The lines containing S1 and S2 are reindented using `indent-region'
3769 unless the optional second argument NOINDENT is non-nil."
3770 (if (eq major-mode 'idlwave-shell-mode)
3771 ;; This is a gross hack to avoit template abbrev expansion
3772 ;; in the shell. FIXME: This is a dirty hack.
3773 (if (and (eq this-command 'self-insert-command)
3774 (equal last-abbrev-location (point)))
3775 (insert last-abbrev-text)
3776 (error "No templates in idlwave-shell"))
3777 (cond ((eq idlwave-abbrev-change-case 'down)
3778 (setq s1 (downcase s1) s2 (downcase s2)))
3779 (idlwave-abbrev-change-case
3780 (setq s1 (upcase s1) s2 (upcase s2))))
3781 (let ((beg (save-excursion (beginning-of-line) (point)))
3782 end)
3783 (if (not (looking-at "\\s-*\n"))
3784 (open-line 1))
3785 (insert s1)
3786 (save-excursion
3787 (insert s2)
3788 (setq end (point)))
3789 (if (not noindent)
3790 (indent-region beg end nil))
3791 (if (stringp prompt)
3792 (message prompt)))))
3793
3794 (defun idlwave-rw-case (string)
3795 "Make STRING have the case required by `idlwave-reserved-word-upcase'."
3796 (if idlwave-reserved-word-upcase
3797 (upcase string)
3798 string))
3799
3800 (defun idlwave-elif ()
3801 "Build skeleton IDL if-else block."
3802 (interactive)
3803 (idlwave-template
3804 (idlwave-rw-case "if")
3805 (idlwave-rw-case " then begin\n\nendif else begin\n\nendelse")
3806 "Condition expression"))
3807
3808 (defun idlwave-case ()
3809 "Build skeleton IDL case statement."
3810 (interactive)
3811 (idlwave-template
3812 (idlwave-rw-case "case")
3813 (idlwave-rw-case " of\n\nendcase")
3814 "Selector expression"))
3815
3816 (defun idlwave-switch ()
3817 "Build skeleton IDL switch statement."
3818 (interactive)
3819 (idlwave-template
3820 (idlwave-rw-case "switch")
3821 (idlwave-rw-case " of\n\nendswitch")
3822 "Selector expression"))
3823
3824 (defun idlwave-for ()
3825 "Build skeleton for loop statment."
3826 (interactive)
3827 (idlwave-template
3828 (idlwave-rw-case "for")
3829 (idlwave-rw-case " do begin\n\nendfor")
3830 "Loop expression"))
3831
3832 (defun idlwave-if ()
3833 "Build skeleton for loop statment."
3834 (interactive)
3835 (idlwave-template
3836 (idlwave-rw-case "if")
3837 (idlwave-rw-case " then begin\n\nendif")
3838 "Scalar logical expression"))
3839
3840 (defun idlwave-procedure ()
3841 (interactive)
3842 (idlwave-template
3843 (idlwave-rw-case "pro")
3844 (idlwave-rw-case "\n\nreturn\nend")
3845 "Procedure name"))
3846
3847 (defun idlwave-function ()
3848 (interactive)
3849 (idlwave-template
3850 (idlwave-rw-case "function")
3851 (idlwave-rw-case "\n\nreturn\nend")
3852 "Function name"))
3853
3854 (defun idlwave-repeat ()
3855 (interactive)
3856 (idlwave-template
3857 (idlwave-rw-case "repeat begin\n\nendrep until")
3858 (idlwave-rw-case "")
3859 "Exit condition"))
3860
3861 (defun idlwave-while ()
3862 (interactive)
3863 (idlwave-template
3864 (idlwave-rw-case "while")
3865 (idlwave-rw-case " do begin\n\nendwhile")
3866 "Entry condition"))
3867
3868 (defun idlwave-split-string (string &optional pattern)
3869 "Return a list of substrings of STRING which are separated by PATTERN.
3870 If PATTERN is omitted, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
3871 (or pattern
3872 (setq pattern "[ \f\t\n\r\v]+"))
3873 (let (parts (start 0))
3874 (while (string-match pattern string start)
3875 (setq parts (cons (substring string start (match-beginning 0)) parts)
3876 start (match-end 0)))
3877 (nreverse (cons (substring string start) parts))))
3878
3879 (defun idlwave-replace-string (string replace_string replace_with)
3880 (let* ((start 0)
3881 (last (length string))
3882 (ret_string "")
3883 end)
3884 (while (setq end (string-match replace_string string start))
3885 (setq ret_string
3886 (concat ret_string (substring string start end) replace_with))
3887 (setq start (match-end 0)))
3888 (setq ret_string (concat ret_string (substring string start last)))))
3889
3890 (defun idlwave-get-buffer-visiting (file)
3891 ;; Return the buffer currently visiting FILE
3892 (cond
3893 ((boundp 'find-file-compare-truenames) ; XEmacs
3894 (let ((find-file-compare-truenames t))
3895 (get-file-buffer file)))
3896 ((fboundp 'find-buffer-visiting) ; Emacs
3897 (find-buffer-visiting file))
3898 (t (error "This should not happen (idlwave-get-buffer-visiting)"))))
3899
3900 (defvar idlwave-outlawed-buffers nil
3901 "List of buffer pulled up by idlwave for special reasons.
3902 Buffers in this list may be killed by `idlwave-kill-autoloaded-buffers'.")
3903
3904 (defun idlwave-find-file-noselect (file &optional why)
3905 ;; Return a buffer visiting file.
3906 (or (idlwave-get-buffer-visiting file)
3907 (let ((buf (find-file-noselect file)))
3908 (if why (add-to-list 'idlwave-outlawed-buffers (cons buf why)))
3909 buf)))
3910
3911 (defun idlwave-kill-autoloaded-buffers ()
3912 "Kill buffers created automatically by IDLWAVE.
3913 Function prompts for a letter to identify the buffers to kill.
3914 Possible letters are:
3915
3916 f Buffers created by the command \\[idlwave-find-module] or mouse
3917 clicks in the routine info window.
3918 s Buffers created by the IDLWAVE Shell to display where execution
3919 stopped or an error was found.
3920 a Both of the above.
3921
3922 Buffer containing unsaved changes require confirmation before they are killed."
3923 (interactive)
3924 (if (null idlwave-outlawed-buffers)
3925 (error "No IDLWAVE-created buffers available")
3926 (princ (format "Kill IDLWAVE-created buffers: [f]ind source(%d), [s]hell display(%d), [a]ll ? "
3927 (idlwave-count-outlawed-buffers 'find)
3928 (idlwave-count-outlawed-buffers 'shell)))
3929 (let ((c (read-char)))
3930 (cond
3931 ((member c '(?f ?\C-f))
3932 (idlwave-do-kill-autoloaded-buffers 'find))
3933 ((member c '(?s ?\C-s))
3934 (idlwave-do-kill-autoloaded-buffers 'shell))
3935 ((member c '(?a ?\C-a))
3936 (idlwave-do-kill-autoloaded-buffers t))
3937 (t (error "Abort"))))))
3938
3939 (defun idlwave-count-outlawed-buffers (tag)
3940 "How many outlawed buffers have tag TAG?"
3941 (length (delq nil
3942 (mapcar
3943 (lambda (x) (eq (cdr x) tag))
3944 idlwave-outlawed-buffers))))
3945
3946 (defun idlwave-do-kill-autoloaded-buffers (&rest reasons)
3947 "Kill all buffers pulled up by IDLWAVE matching REASONS."
3948 (let* ((list (copy-sequence idlwave-outlawed-buffers))
3949 (cnt 0)
3950 entry)
3951 (while (setq entry (pop list))
3952 (if (buffer-live-p (car entry))
3953 (and (or (memq t reasons)
3954 (memq (cdr entry) reasons))
3955 (kill-buffer (car entry))
3956 (incf cnt)
3957 (setq idlwave-outlawed-buffers
3958 (delq entry idlwave-outlawed-buffers)))
3959 (setq idlwave-outlawed-buffers
3960 (delq entry idlwave-outlawed-buffers))))
3961 (message "%d buffer%s killed" cnt (if (= cnt 1) "" "s"))))
3962
3963 (defun idlwave-revoke-license-to-kill ()
3964 "Remove BUFFER from the buffers which may be killed.
3965 Killing would be done by `idlwave-do-kill-autoloaded-buffers'.
3966 Intended for `after-save-hook'."
3967 (let* ((buf (current-buffer))
3968 (entry (assq buf idlwave-outlawed-buffers)))
3969 ;; Revoke license
3970 (if entry
3971 (setq idlwave-outlawed-buffers
3972 (delq entry idlwave-outlawed-buffers)))
3973 ;; Remove this function from the hook.
3974 (remove-hook 'after-save-hook 'idlwave-revoke-license-to-kill 'local)))
3975
3976 (defvar idlwave-path-alist)
3977 (defun idlwave-locate-lib-file (file)
3978 ;; Find FILE on the scanned lib path and return a buffer visiting it
3979 (let* ((dirs idlwave-path-alist)
3980 dir efile)
3981 (catch 'exit
3982 (while (setq dir (car (pop dirs)))
3983 (if (file-regular-p
3984 (setq efile (expand-file-name file dir)))
3985 (throw 'exit efile))))))
3986
3987 (defun idlwave-expand-lib-file-name (file)
3988 ;; Find FILE on the scanned lib path and return a buffer visiting it
3989 ;; This is for, e.g., finding source with no user catalog
3990 (cond
3991 ((null file) nil)
3992 ((file-name-absolute-p file) file)
3993 (t (idlwave-locate-lib-file file))))
3994
3995 (defun idlwave-make-tags ()
3996 "Creates the IDL tags file IDLTAGS in the current directory from
3997 the list of directories specified in the minibuffer. Directories may be
3998 for example: . /usr/local/rsi/idl/lib. All the subdirectories of the
3999 specified top directories are searched if the directory name is prefixed
4000 by @. Specify @ directories with care, it may take a long, long time if
4001 you specify /."
4002 (interactive)
4003 (let (directory directories cmd append status numdirs dir getsubdirs
4004 buffer save_buffer files numfiles item errbuf)
4005
4006 ;;
4007 ;; Read list of directories
4008 (setq directory (read-string "Tag Directories: " "."))
4009 (setq directories (idlwave-split-string directory "[ \t]+"))
4010 ;;
4011 ;; Set etags command, vars
4012 (setq cmd "etags --output=IDLTAGS --language=none --regex='/[
4013 \\t]*[pP][Rr][Oo][ \\t]+\\([^ \\t,]+\\)/' --regex='/[
4014 \\t]*[Ff][Uu][Nn][Cc][Tt][Ii][Oo][Nn][ \\t]+\\([^ \\t,]+\\)/' ")
4015 (setq append " ")
4016 (setq status 0)
4017 ;;
4018 ;; For each directory
4019 (setq numdirs 0)
4020 (setq dir (nth numdirs directories))
4021 (while (and dir)
4022 ;;
4023 ;; Find the subdirectories
4024 (if (string-match "^[@]\\(.+\\)$" dir)
4025 (setq getsubdirs t) (setq getsubdirs nil))
4026 (if (and getsubdirs) (setq dir (substring dir 1 (length dir))))
4027 (setq dir (expand-file-name dir))
4028 (if (file-directory-p dir)
4029 (progn
4030 (if (and getsubdirs)
4031 (progn
4032 (setq buffer (get-buffer-create "*idltags*"))
4033 (call-process "sh" nil buffer nil "-c"
4034 (concat "find " dir " -type d -print"))
4035 (setq save_buffer (current-buffer))
4036 (set-buffer buffer)
4037 (setq files (idlwave-split-string
4038 (idlwave-replace-string
4039 (buffer-substring 1 (point-max))
4040 "\n" "/*.pro ")
4041 "[ \t]+"))
4042 (set-buffer save_buffer)
4043 (kill-buffer buffer))
4044 (setq files (list (concat dir "/*.pro"))))
4045 ;;
4046 ;; For each subdirectory
4047 (setq numfiles 0)
4048 (setq item (nth numfiles files))
4049 (while (and item)
4050 ;;
4051 ;; Call etags
4052 (if (not (string-match "^[ \\t]*$" item))
4053 (progn
4054 (message "%s" (concat "Tagging " item "..."))
4055 (setq errbuf (get-buffer-create "*idltags-error*"))
4056 (setq status (+ status
4057 (if (eq 0 (call-process
4058 "sh" nil errbuf nil "-c"
4059 (concat cmd append item)))
4060 0
4061 1)))
4062 ;;
4063 ;; Append additional tags
4064 (setq append " --append ")
4065 (setq numfiles (1+ numfiles))
4066 (setq item (nth numfiles files)))
4067 (progn
4068 (setq numfiles (1+ numfiles))
4069 (setq item (nth numfiles files))
4070 )))
4071
4072 (setq numdirs (1+ numdirs))
4073 (setq dir (nth numdirs directories)))
4074 (progn
4075 (setq numdirs (1+ numdirs))
4076 (setq dir (nth numdirs directories)))))
4077
4078 (setq errbuf (get-buffer-create "*idltags-error*"))
4079 (if (= status 0)
4080 (kill-buffer errbuf))
4081 (message "")
4082 ))
4083
4084 (defun idlwave-toggle-comment-region (beg end &optional n)
4085 "Comment the lines in the region if the first non-blank line is
4086 commented, and conversely, uncomment region. If optional prefix arg
4087 N is non-nil, then for N positive, add N comment delimiters or for N
4088 negative, remove N comment delimiters.
4089 Uses `comment-region' which does not place comment delimiters on
4090 blank lines."
4091 (interactive "r\nP")
4092 (if n
4093 (comment-region beg end (prefix-numeric-value n))
4094 (save-excursion
4095 (goto-char beg)
4096 (beginning-of-line)
4097 ;; skip blank lines
4098 (skip-chars-forward " \t\n")
4099 (if (looking-at (concat "[ \t]*\\(" comment-start "+\\)"))
4100 (if (fboundp 'uncomment-region)
4101 (uncomment-region beg end)
4102 (comment-region beg end
4103 (- (length (buffer-substring
4104 (match-beginning 1)
4105 (match-end 1))))))
4106 (comment-region beg end)))))
4107
4108
4109 ;; ----------------------------------------------------------------------------
4110 ;; ----------------------------------------------------------------------------
4111 ;; ----------------------------------------------------------------------------
4112 ;; ----------------------------------------------------------------------------
4113 ;;
4114 ;; Completion and Routine Info
4115 ;;
4116
4117 ;; String "intern" functions
4118
4119 ;; For the completion and routine info function, we want to normalize
4120 ;; the case of procedure names etc. We do this by "interning" these
4121 ;; string is a hand-crafted way. Hashes are used to map the downcase
4122 ;; version of the strings to the cased versions. Most *-sint-*
4123 ;; variables consist of *two* hashes, a buffer+shell, followed by a
4124 ;; system hash. The former is re-scanned, and the latter takes case
4125 ;; precedence.
4126 ;;
4127 ;; Since these cased versions are really lisp objects, we can use `eq'
4128 ;; to search, which is a large performance boost. All new strings
4129 ;; need to be "sinterned". We do this as early as possible after
4130 ;; getting these strings from completion or buffer substrings. So
4131 ;; most of the code can simply assume to deal with "sinterned"
4132 ;; strings. The only exception is that the functions which scan whole
4133 ;; buffers for routine information do not intern the grabbed strings.
4134 ;; This is only done afterwards. Therefore in these functions it is
4135 ;; *not* safe to assume the strings can be compared with `eq' and be
4136 ;; fed into the routine assq functions.
4137
4138 ;; Here we define the hashing functions.
4139
4140 ;; The variables which hold the hashes.
4141 (defvar idlwave-sint-routines '(nil))
4142 (defvar idlwave-sint-keywords '(nil))
4143 (defvar idlwave-sint-methods '(nil))
4144 (defvar idlwave-sint-classes '(nil))
4145 (defvar idlwave-sint-dirs '(nil))
4146 (defvar idlwave-sint-libnames '(nil))
4147
4148 (defun idlwave-reset-sintern (&optional what)
4149 "Reset all sintern hashes."
4150 ;; Make sure the hash functions are accessible.
4151 (if (or (not (fboundp 'gethash))
4152 (not (fboundp 'puthash)))
4153 (progn
4154 (require 'cl)
4155 (or (fboundp 'puthash)
4156 (defalias 'puthash 'cl-puthash))))
4157 (let ((entries '((idlwave-sint-routines 1000 10)
4158 (idlwave-sint-keywords 1000 10)
4159 (idlwave-sint-methods 100 10)
4160 (idlwave-sint-classes 10 10))))
4161
4162 ;; Make sure these are lists
4163 (loop for entry in entries
4164 for var = (car entry)
4165 do (if (not (consp (symbol-value var))) (set var (list nil))))
4166
4167 ;; Reset the system & library hash
4168 (when (or (eq what t) (eq what 'syslib)
4169 (null (cdr idlwave-sint-routines)))
4170 (loop for entry in entries
4171 for var = (car entry) for size = (nth 1 entry)
4172 do (setcdr (symbol-value var)
4173 (make-hash-table ':size size ':test 'equal)))
4174 (setq idlwave-sint-dirs nil
4175 idlwave-sint-libnames nil))
4176
4177 ;; Reset the buffer & shell hash
4178 (when (or (eq what t) (eq what 'bufsh)
4179 (null (car idlwave-sint-routines)))
4180 (loop for entry in entries
4181 for var = (car entry) for size = (nth 1 entry)
4182 do (setcar (symbol-value var)
4183 (make-hash-table ':size size ':test 'equal))))))
4184
4185 (defun idlwave-sintern-routine-or-method (name &optional class set)
4186 (if class
4187 (idlwave-sintern-method name set)
4188 (idlwave-sintern-routine name set)))
4189
4190 (defun idlwave-sintern (stype &rest args)
4191 (apply (intern (concat "idlwave-sintern-" (symbol-name stype))) args))
4192
4193 ;;(defmacro idlwave-sintern (type var)
4194 ;; `(cond ((not (stringp name)) name)
4195 ;; ((gethash (downcase name) (cdr ,var)))
4196 ;; ((gethash (downcase name) (car ,var)))
4197 ;; (set (idlwave-sintern-set name ,type ,var set))
4198 ;; (name)))
4199
4200 (defun idlwave-sintern-routine (name &optional set)
4201 (cond ((not (stringp name)) name)
4202 ((gethash (downcase name) (cdr idlwave-sint-routines)))
4203 ((gethash (downcase name) (car idlwave-sint-routines)))
4204 (set (idlwave-sintern-set name 'routine idlwave-sint-routines set))
4205 (name)))
4206 (defun idlwave-sintern-keyword (name &optional set)
4207 (cond ((not (stringp name)) name)
4208 ((gethash (downcase name) (cdr idlwave-sint-keywords)))
4209 ((gethash (downcase name) (car idlwave-sint-keywords)))
4210 (set (idlwave-sintern-set name 'keyword idlwave-sint-keywords set))
4211 (name)))
4212 (defun idlwave-sintern-method (name &optional set)
4213 (cond ((not (stringp name)) name)
4214 ((gethash (downcase name) (cdr idlwave-sint-methods)))
4215 ((gethash (downcase name) (car idlwave-sint-methods)))
4216 (set (idlwave-sintern-set name 'method idlwave-sint-methods set))
4217 (name)))
4218 (defun idlwave-sintern-class (name &optional set)
4219 (cond ((not (stringp name)) name)
4220 ((gethash (downcase name) (cdr idlwave-sint-classes)))
4221 ((gethash (downcase name) (car idlwave-sint-classes)))
4222 (set (idlwave-sintern-set name 'class idlwave-sint-classes set))
4223 (name)))
4224
4225 (defun idlwave-sintern-dir (dir &optional set)
4226 (car (or (member dir idlwave-sint-dirs)
4227 (setq idlwave-sint-dirs (cons dir idlwave-sint-dirs)))))
4228 (defun idlwave-sintern-libname (name &optional set)
4229 (car (or (member name idlwave-sint-libnames)
4230 (setq idlwave-sint-libnames (cons name idlwave-sint-libnames)))))
4231
4232 (defun idlwave-sintern-set (name type tables set)
4233 (let* ((func (or (cdr (assq type idlwave-completion-case))
4234 'identity))
4235 (iname (funcall (if (eq func 'preserve) 'identity func) name))
4236 (table (if (eq set 'sys) (cdr tables) (car tables))))
4237 (puthash (downcase name) iname table)
4238 iname))
4239
4240 (defun idlwave-sintern-keyword-list (kwd-list &optional set)
4241 "Sintern a set of keywords (file (key . link) (key2 . link2) ...)"
4242 (mapcar (lambda(x)
4243 (setcar x (idlwave-sintern-keyword (car x) set)))
4244 (cdr kwd-list))
4245 kwd-list)
4246
4247 (defun idlwave-sintern-rinfo-list (list &optional set default-dir)
4248 "Sintern all strings in the rinfo LIST. With optional parameter
4249 SET: also set new patterns. Probably this will always have to be t.
4250 If DEFAULT-DIR is passed, it is used as the base of the directory"
4251 (let (entry name type class kwds res source call new)
4252 (while list
4253 (setq entry (car list)
4254 list (cdr list)
4255 name (car entry)
4256 type (nth 1 entry)
4257 class (nth 2 entry)
4258 source (nth 3 entry)
4259 call (nth 4 entry)
4260 kwds (nthcdr 5 entry))
4261
4262 ;; The class and name
4263 (if class
4264 (progn
4265 (if (symbolp class) (setq class (symbol-name class)))
4266 (setq class (idlwave-sintern-class class set))
4267 (setq name (idlwave-sintern-method name set)))
4268 (setq name (idlwave-sintern-routine name set)))
4269
4270 ;; The source
4271 (let ((source-type (car source))
4272 (source-file (nth 1 source))
4273 (source-dir (if default-dir
4274 (file-name-as-directory default-dir)
4275 (nth 2 source)))
4276 (source-lib (nth 3 source)))
4277 (if (stringp source-dir)
4278 (setq source-dir (idlwave-sintern-dir source-dir set)))
4279 (if (stringp source-lib)
4280 (setq source-lib (idlwave-sintern-libname source-lib set)))
4281 (setq source (list source-type source-file source-dir source-lib)))
4282
4283 ;; The keywords
4284 (setq kwds (mapcar (lambda (x)
4285 (idlwave-sintern-keyword-list x set))
4286 kwds))
4287
4288 ;; Build a canonicalized list
4289 (setq new (nconc (list name type class source call) kwds)
4290 res (cons new res)))
4291 (nreverse res)))
4292
4293 ;; Creating new sintern tables
4294
4295 (defun idlwave-new-sintern-type (tag)
4296 "Define a variable and a function to sintern the new type TAG.
4297 This defines the function `idlwave-sintern-TAG' and the variable
4298 `idlwave-sint-TAGs'."
4299 (let* ((name (symbol-name tag))
4300 (names (concat name "s"))
4301 (var (intern (concat "idlwave-sint-" names)))
4302 (func (intern (concat "idlwave-sintern-" name))))
4303 (set var nil) ; initial value of the association list
4304 (fset func ; set the function
4305 `(lambda (name &optional set)
4306 (cond ((not (stringp name)) name)
4307 ((cdr (assoc (downcase name) ,var)))
4308 (set
4309 (setq ,var (cons (cons (downcase name) name) ,var))
4310 name)
4311 (name))))))
4312
4313 (defun idlwave-reset-sintern-type (tag)
4314 "Reset the sintern variable associated with TAG."
4315 (set (intern (concat "idlwave-sint-" (symbol-name tag) "s")) nil))
4316
4317 ;;---------------------------------------------------------------------------
4318
4319
4320 ;; The variables which hold the information
4321 (defvar idlwave-system-routines nil
4322 "Holds the routine-info obtained by scanning buffers.")
4323 (defvar idlwave-buffer-routines nil
4324 "Holds the routine-info obtained by scanning buffers.")
4325 (defvar idlwave-compiled-routines nil
4326 "Holds the routine-info obtained by asking the shell.")
4327 (defvar idlwave-unresolved-routines nil
4328 "Holds the unresolved routine-info obtained by asking the shell.")
4329 (defvar idlwave-user-catalog-routines nil
4330 "Holds the procedure routine-info from the user scan.")
4331 (defvar idlwave-library-catalog-routines nil
4332 "Holds the procedure routine-info from the .idlwave_catalog library files.")
4333 (defvar idlwave-library-catalog-libname nil
4334 "Name of library catalog loaded from .idlwave_catalog files.")
4335 (defvar idlwave-path-alist nil
4336 "Alist with !PATH directories and zero or more flags if the dir has
4337 been scanned in a user catalog ('user) or discovered in a library
4338 catalog \('lib).")
4339 (defvar idlwave-true-path-alist nil
4340 "Like `idlwave-path-alist', but with true filenames.")
4341 (defvar idlwave-routines nil
4342 "Holds the combinded procedure/function/method routine-info.")
4343 (defvar idlwave-class-alist nil
4344 "Holds the class names known to IDLWAVE.")
4345 (defvar idlwave-class-history nil
4346 "The history of classes selected with the minibuffer.")
4347 (defvar idlwave-force-class-query nil)
4348 (defvar idlwave-before-completion-wconf nil
4349 "The window configuration just before the completion buffer was displayed.")
4350 (defvar idlwave-last-system-routine-info-cons-cell nil
4351 "The last cons cell in the system routine info.")
4352
4353 ;;
4354 ;; The code to get routine info from different sources.
4355
4356 (defvar idlwave-system-routines)
4357 (defvar idlwave-catalog-process nil
4358 "The background process currently updating the catalog.")
4359
4360 (defun idlwave-routines ()
4361 "Provide a list of IDL routines.
4362 This routine loads the builtin routines on the first call. Later it
4363 only returns the value of the variable."
4364 (if (and idlwave-catalog-process
4365 (processp idlwave-catalog-process))
4366 (progn
4367 (cond
4368 ((equal (process-status idlwave-catalog-process) 'exit)
4369 (message "updating........")
4370 (setq idlwave-catalog-process nil)
4371 (idlwave-update-routine-info '(4)))
4372 ((equal (process-status idlwave-catalog-process) 'run)
4373 ;; Keep it running...
4374 )
4375 (t
4376 ;; Something is wrong, get rid of the process
4377 (message "Problem with catalog process") (beep)
4378 (condition-case nil
4379 (kill-process idlwave-catalog-process)
4380 (error nil))
4381 (setq idlwave-catalog-process nil)))))
4382 (or idlwave-routines
4383 (progn
4384 (idlwave-update-routine-info)
4385 ;; return the current value
4386 idlwave-routines)))
4387
4388 (defvar idlwave-update-rinfo-hook nil
4389 "List of functions which should run after a global rinfo update.
4390 Does not run after automatic updates of buffer or the shell.")
4391
4392 (defun idlwave-rescan-catalog-directories ()
4393 "Rescan the previously selected directories. For batch processing."
4394 (idlwave-update-routine-info '(16)))
4395
4396 (defun idlwave-rescan-asynchronously ()
4397 "Dispatch another Emacs instance to update the idlwave catalog.
4398 After the process finishes normally, the first access to routine info
4399 will re-read the catalog."
4400 (interactive)
4401 (if (processp idlwave-catalog-process)
4402 (if (eq (process-status idlwave-catalog-process) 'run)
4403 (if (yes-or-no-p "A catalog-updating process is running. Kill it? ")
4404 (progn
4405 (condition-case nil
4406 (kill-process idlwave-catalog-process)
4407 (error nil))
4408 (error "Process killed, no new process started"))
4409 (error "Quit"))
4410 (condition-case nil
4411 (kill-process idlwave-catalog-process)
4412 (error nil))))
4413 (if (or (not idlwave-user-catalog-file)
4414 (not (stringp idlwave-user-catalog-file))
4415 (not (file-regular-p idlwave-user-catalog-file)))
4416 (error "No catalog has been produced yet"))
4417 (let* ((emacs (concat invocation-directory invocation-name))
4418 (args (list "-batch"
4419 "-l" (expand-file-name "~/.emacs")
4420 "-l" "idlwave"
4421 "-f" "idlwave-rescan-catalog-directories"))
4422 (process (apply 'start-process "idlcat"
4423 nil emacs args)))
4424 (setq idlwave-catalog-process process)
4425 (set-process-sentinel
4426 process
4427 (lambda (pro why)
4428 (when (string-match "finished" why)
4429 (setq idlwave-routines nil
4430 idlwave-system-routines nil
4431 idlwave-catalog-process nil)
4432 (or (idlwave-start-load-rinfo-timer)
4433 (idlwave-update-routine-info '(4))))))
4434 (message "Background job started to update catalog file")))
4435
4436
4437 ;; Format for all routine info user catalog, library catalogs, etc.:
4438 ;;
4439 ;; ("ROUTINE" type class
4440 ;; (system) | (lib pro_file dir "LIBNAME") | (user pro_file dir "USERLIB") |
4441 ;; (buffer pro_file dir) | (compiled pro_file dir)
4442 ;; "calling_string" ("HELPFILE" (("KWD1" . link1) ...))
4443 ;; ("HELPFILE2" (("KWD2" . link) ...)) ...)
4444 ;;
4445 ;; DIR will be supplied dynamically while loading library catalogs,
4446 ;; and is sinterned to save space, as is LIBNAME. PRO_FILE can be a
4447 ;; complete filepath, in which case DIR is unnecessary. HELPFILE can
4448 ;; be nil, as can LINK1, etc., if no HTML help is available.
4449
4450
4451 (defvar idlwave-load-rinfo-idle-timer)
4452 (defvar idlwave-shell-path-query)
4453
4454 (defun idlwave-update-routine-info (&optional arg no-concatenate)
4455 "Update the internal routine-info lists.
4456 These lists are used by `idlwave-routine-info' (\\[idlwave-routine-info])
4457 and by `idlwave-complete' (\\[idlwave-complete]) to provide information
4458 about individual routines.
4459
4460 The information can come from 4 sources:
4461 1. IDL programs in the current editing session
4462 2. Compiled modules in an IDL shell running as Emacs subprocess
4463 3. A list which covers the IDL system routines.
4464 4. A list which covers the prescanned library files.
4465
4466 Scans all IDLWAVE-mode buffers of the current editing session (see
4467 `idlwave-scan-all-buffers-for-routine-info').
4468 When an IDL shell is running, this command also queries the IDL program
4469 for currently compiled routines.
4470
4471 With prefix ARG, also reload the system and library lists.
4472 With two prefix ARG's, also rescans the chosen user catalog tree.
4473 With three prefix args, dispatch asynchronous process to do the update.
4474
4475 If NO-CONCATENATE is non-nil, don't pre-concatenate the routine info
4476 lists, but instead wait for the shell query to complete and
4477 asynchronously finish updating routine info. This is set
4478 automatically when called interactively. When you need routine
4479 information updated immediately, leave NO-CONCATENATE nil."
4480 (interactive "P\np")
4481 ;; Stop any idle processing
4482 (if (or (and (fboundp 'itimerp)
4483 (itimerp idlwave-load-rinfo-idle-timer))
4484 (and (fboundp 'timerp)
4485 (timerp idlwave-load-rinfo-idle-timer)))
4486 (cancel-timer idlwave-load-rinfo-idle-timer))
4487 (cond
4488 ((equal arg '(64))
4489 ;; Start a background process which updates the catalog.
4490 (idlwave-rescan-asynchronously))
4491 ((equal arg '(16))
4492 ;; Update the user catalog now, and wait for them.
4493 (idlwave-create-user-catalog-file t))
4494 (t
4495 (let* ((load (or arg
4496 idlwave-buffer-case-takes-precedence
4497 (null idlwave-routines)))
4498 ;; The override-idle means, even if the idle timer has done some
4499 ;; preparing work, load and renormalize everything anyway.
4500 (override-idle (or arg idlwave-buffer-case-takes-precedence)))
4501
4502 (setq idlwave-buffer-routines nil
4503 idlwave-compiled-routines nil
4504 idlwave-unresolved-routines nil)
4505 ;; Reset the appropriate hashes
4506 (if (get 'idlwave-reset-sintern 'done-by-idle)
4507 ;; reset was already done in idle time, so skip this step now once
4508 (put 'idlwave-reset-sintern 'done-by-idle nil)
4509 (idlwave-reset-sintern (cond (load t)
4510 ((null idlwave-system-routines) t)
4511 (t 'bufsh))))
4512
4513 (if idlwave-buffer-case-takes-precedence
4514 ;; We can safely scan the buffer stuff first
4515 (progn
4516 (idlwave-update-buffer-routine-info)
4517 (and load (idlwave-load-all-rinfo override-idle)))
4518 ;; We first do the system info, and then the buffers
4519 (and load (idlwave-load-all-rinfo override-idle))
4520 (idlwave-update-buffer-routine-info))
4521
4522 ;; Let's see if there is a shell
4523 (let* ((shell-is-running (and (fboundp 'idlwave-shell-is-running)
4524 (idlwave-shell-is-running)))
4525 (ask-shell (and shell-is-running
4526 idlwave-query-shell-for-routine-info)))
4527
4528 ;; Load the library catalogs again, first re-scanning the path
4529 (when arg
4530 (if shell-is-running
4531 (idlwave-shell-send-command idlwave-shell-path-query
4532 '(progn
4533 (idlwave-shell-get-path-info)
4534 (idlwave-scan-library-catalogs))
4535 'hide)
4536 (idlwave-scan-library-catalogs)))
4537
4538 (if (or (not ask-shell)
4539 (not no-concatenate))
4540 ;; 1. If we are not going to ask the shell, we need to do the
4541 ;; concatenation now.
4542 ;; 2. When this function is called non-interactively, it
4543 ;; means that someone needs routine info *now*. The
4544 ;; shell update causes the concatenation to be
4545 ;; *delayed*, so not in time for the current command.
4546 ;; Therefore, we do a concatenation now, even though
4547 ;; the shell might do it again.
4548 (idlwave-concatenate-rinfo-lists nil 'run-hooks))
4549
4550 (when ask-shell
4551 ;; Ask the shell about the routines it knows of.
4552 (message "Querying the shell")
4553 (idlwave-shell-update-routine-info nil t)))))))
4554
4555
4556 (defvar idlwave-load-rinfo-steps-done (make-vector 6 nil))
4557 (defvar idlwave-load-rinfo-idle-timer nil)
4558 (defun idlwave-start-load-rinfo-timer ()
4559 (if (or (and (fboundp 'itimerp)
4560 (itimerp idlwave-load-rinfo-idle-timer))
4561 (and (fboundp 'timerp)
4562 (timerp idlwave-load-rinfo-idle-timer)))
4563 (cancel-timer idlwave-load-rinfo-idle-timer))
4564 (setq idlwave-load-rinfo-steps-done (make-vector 6 nil))
4565 (setq idlwave-load-rinfo-idle-timer nil)
4566 (if (and idlwave-init-rinfo-when-idle-after
4567 (numberp idlwave-init-rinfo-when-idle-after)
4568 (not (equal 0 idlwave-init-rinfo-when-idle-after))
4569 (not idlwave-routines))
4570 (condition-case nil
4571 (progn
4572 (setq idlwave-load-rinfo-idle-timer
4573 (run-with-idle-timer
4574 idlwave-init-rinfo-when-idle-after
4575 nil 'idlwave-load-rinfo-next-step)))
4576 (error nil))))
4577
4578 (defvar idlwave-library-routines nil "Obsolete variable.")
4579
4580 ;;------ XML Help routine info system
4581 (defun idlwave-load-system-routine-info ()
4582 ;; Load the system routine info from the cached routine info file,
4583 ;; which, if necessary, will be re-created from the XML file on
4584 ;; disk. As a last fallback, load the (likely outdated) idlw-rinfo
4585 ;; file distributed with older IDLWAVE versions (<6.0)
4586 (unless (and (load idlwave-xml-system-rinfo-converted-file
4587 'noerror 'nomessage)
4588 (idlwave-xml-system-routine-info-up-to-date))
4589 ;; See if we can create it from XML source
4590 (condition-case nil
4591 (idlwave-convert-xml-system-routine-info)
4592 (error
4593 (unless (load idlwave-xml-system-rinfo-converted-file
4594 'noerror 'nomessage)
4595 (if idlwave-system-routines
4596 (message
4597 "Failed to load converted routine info, using old conversion.")
4598 (message
4599 "Failed to convert XML routine info, falling back on idlw-rinfo.")
4600 (if (not (load "idlw-rinfo" 'noerror 'nomessage))
4601 (message
4602 "Could not locate any system routine information."))))))))
4603
4604 (defun idlwave-xml-system-routine-info-up-to-date()
4605 (let* ((dir (file-name-as-directory
4606 (expand-file-name "help/online_help" (idlwave-sys-dir))))
4607 (catalog-file (expand-file-name "idl_catalog.xml" dir)))
4608 (file-newer-than-file-p ;converted file is newer than catalog
4609 idlwave-xml-system-rinfo-converted-file
4610 catalog-file)))
4611
4612 (defvar idlwave-system-class-info nil) ; Gathered from idlw-rinfo
4613 (defvar idlwave-system-variables-alist nil
4614 "Alist of system variables and the associated structure tags.
4615 Gets set in cached XML rinfo, or `idlw-rinfo.el'.")
4616 (defvar idlwave-executive-commands-alist nil
4617 "Alist of system variables and their help files.")
4618 (defvar idlwave-help-special-topic-words nil)
4619
4620
4621 (defun idlwave-shorten-syntax (syntax name &optional class)
4622 ;; From a list of syntax statments, shorten with %s and group with "or"
4623 (let ((case-fold-search t))
4624 (mapconcat
4625 (lambda (x)
4626 (while (string-match name x)
4627 (setq x (replace-match "%s" t t x)))
4628 (if class
4629 (while (string-match class x)
4630 (setq x (replace-match "%s" t t x))))
4631 x)
4632 (nreverse syntax)
4633 " or ")))
4634
4635 (defun idlwave-xml-create-class-method-lists (xml-entry)
4636 ;; Create a class list entry from the xml parsed list., returning a
4637 ;; cons of form (class-entry method-entries).
4638 (let* ((nameblock (nth 1 xml-entry))
4639 (class (cdr (assq 'name nameblock)))
4640 (link (cdr (assq 'link nameblock)))
4641 (params (cddr xml-entry))
4642 (case-fold-search t)
4643 class-entry
4644 method methods-entry extra-kwds
4645 props get-props set-props init-props inherits
4646 pelem ptype)
4647 (while params
4648 (setq pelem (car params))
4649 (when (listp pelem)
4650 (setq ptype (car pelem)
4651 props (car (cdr pelem)))
4652 (cond
4653 ((eq ptype 'SUPERCLASS)
4654 (let ((pname (cdr (assq 'name props)))
4655 (plink (cdr (assq 'link props))))
4656 (unless (and (string= pname "None")
4657 (string= plink "None"))
4658 (push pname inherits))))
4659
4660 ((eq ptype 'PROPERTY)
4661 (let ((pname (cdr (assq 'name props)))
4662 (plink (cdr (assq 'link props)))
4663 (get (string= (cdr (assq 'get props)) "Yes"))
4664 (set (string= (cdr (assq 'set props)) "Yes"))
4665 (init (string= (cdr (assq 'init props)) "Yes")))
4666 (if get (push (list pname plink) get-props))
4667 (if set (push (list pname plink) set-props))
4668 (if init (push (list pname plink) init-props))))
4669
4670 ((eq ptype 'METHOD)
4671 (setq method (cdr (assq 'name props)))
4672 (setq extra-kwds ;;Assume all property keywords are gathered already
4673 (cond
4674 ((string-match (concat class "::Init") method)
4675 (put 'init-props 'matched t)
4676 init-props)
4677 ((string-match (concat class "::GetProperty") method)
4678 (put 'get-props 'matched t)
4679 get-props)
4680 ((string-match (concat class "::SetProperty") method)
4681 (put 'set-props 'matched t)
4682 set-props)
4683 (t nil)))
4684 (setq methods-entry
4685 (nconc (idlwave-xml-create-rinfo-list pelem class extra-kwds)
4686 methods-entry)))
4687 (t)))
4688 (setq params (cdr params)))
4689 ;(unless (get 'init-props 'matched)
4690 ; (message "Failed to match Init in class %s" class))
4691 ;(unless (get 'get-props 'matched)
4692 ; (message "Failed to match GetProperty in class %s" class))
4693 ;(unless (get 'set-props 'matched)
4694 ; (message "Failed to match SetProperty in class %s" class))
4695 (setq class-entry
4696 (if inherits
4697 (list class (append '(inherits) inherits) (list 'link link))
4698 (list class (list 'link link))))
4699 (cons class-entry methods-entry)))
4700
4701 (defun idlwave-xml-create-rinfo-list (xml-entry &optional class extra-kws)
4702 ;; Create correctly structured list elements from ROUTINE or METHOD
4703 ;; XML list structures. Return a list of list elements, with more
4704 ;; than one sub-list possible if a routine can serve as both
4705 ;; procedure and function (e.g. call_method).
4706 (let* ((nameblock (nth 1 xml-entry))
4707 (name (cdr (assq 'name nameblock)))
4708 (link (cdr (assq 'link nameblock)))
4709 (params (cddr xml-entry))
4710 (syntax-vec (make-vector 3 nil)) ; procedure, function, exec command
4711 (case-fold-search t)
4712 syntax kwd klink pref-list kwds pelem ptype entry props result type)
4713 (if class ;; strip out class name from class method name string
4714 (if (string-match (concat class "::") name)
4715 (setq name (substring name (match-end 0)))))
4716 (while params
4717 (setq pelem (car params))
4718 (when (listp pelem)
4719 (setq ptype (car pelem)
4720 props (car (cdr pelem)))
4721 (cond
4722 ((eq ptype 'SYNTAX)
4723 (setq syntax (cdr (assq 'name props)))
4724 (if (string-match "-&gt;" syntax)
4725 (setq syntax (replace-match "->" t nil syntax)))
4726 (setq type (cdr (assq 'type props)))
4727 (push syntax
4728 (aref syntax-vec (cond
4729 ((string-match "^pro" type) 0)
4730 ((string-match "^fun" type) 1)
4731 ((string-match "^exec" type) 2)))))
4732 ((eq ptype 'KEYWORD)
4733 (setq kwd (cdr (assq 'name props))
4734 klink (cdr (assq 'link props)))
4735 (if (string-match "^\\[XY\\(Z?\\)\\]" kwd)
4736 (progn
4737 (setq pref-list
4738 (if (match-string 1 kwd) '("X" "Y" "Z") '("X" "Y"))
4739 kwd (substring kwd (match-end 0)))
4740 (loop for x in pref-list do
4741 (push (list (concat x kwd) klink) kwds)))
4742 (push (list kwd klink) kwds)))
4743
4744 (t))); Do nothing for the others
4745 (setq params (cdr params)))
4746
4747 ;; Debug
4748 ; (if (and (null (aref syntax-vec 0))
4749 ; (null (aref syntax-vec 1))
4750 ; (null (aref syntax-vec 2)))
4751 ; (with-current-buffer (get-buffer-create "IDL_XML_catalog_complaints")
4752 ; (if class
4753 ; (insert (format "Missing SYNTAX entry for %s::%s\n" class name))
4754 ; (insert (message "Missing SYNTAX entry for %s\n" name)))))
4755
4756 ;; Executive commands are treated specially
4757 (if (aref syntax-vec 2)
4758 (cons (substring name 1) link)
4759 (if extra-kws (setq kwds (nconc kwds extra-kws)))
4760 (setq kwds (idlwave-rinfo-group-keywords kwds link))
4761 (loop for idx from 0 to 1 do
4762 (if (aref syntax-vec idx)
4763 (push (append (list name (if (eq idx 0) 'pro 'fun)
4764 class '(system)
4765 (idlwave-shorten-syntax
4766 (aref syntax-vec idx) name class))
4767 kwds) result)))
4768 result)))
4769
4770
4771 (defun idlwave-rinfo-group-keywords (kwds master-link)
4772 ;; Group keywords by link file, as a list with elements
4773 ;; (linkfile ( ("KWD1" . link1) ("KWD2" . link2))
4774 (let (kwd link anchor linkfiles block master-elt)
4775 (while kwds
4776 (setq kwd (car kwds)
4777 link (idlwave-split-link-target (nth 1 kwd))
4778 anchor (cdr link)
4779 link (car link)
4780 kwd (car kwd))
4781 (if (setq block (assoc link linkfiles))
4782 (push (cons kwd anchor) (cdr block))
4783 (push (list link (cons kwd anchor)) linkfiles))
4784 (setq kwds (cdr kwds)))
4785 ;; Ensure the master link is there
4786 (if (setq master-elt (assoc master-link linkfiles))
4787 (if (eq (car linkfiles) master-elt)
4788 linkfiles
4789 (cons master-elt (delq master-elt linkfiles)))
4790 (push (list master-link) linkfiles))))
4791
4792 (defun idlwave-convert-xml-clean-statement-aliases (aliases)
4793 ;; Clean up the syntax of routines which are actually aliases by
4794 ;; removing the "OR" from the statements
4795 (let (syntax entry)
4796 (loop for x in aliases do
4797 (setq entry (assoc x idlwave-system-routines))
4798 (when entry
4799 (while (string-match " +or +" (setq syntax (nth 4 entry)))
4800 (setf (nth 4 entry) (replace-match ", " t t syntax)))))))
4801
4802 (defun idlwave-convert-xml-clean-routine-aliases (aliases)
4803 ;; Duplicate and trim original routine aliases from rinfo list
4804 ;; This if for, e.g. OPENR/OPENW/OPENU
4805 (let (alias remove-list new parts all-parts)
4806 (loop for x in aliases do
4807 (when (setq parts (split-string (cdr x) "/"))
4808 (setq new (assoc (cdr x) all-parts))
4809 (unless new
4810 (setq new (cons (cdr x) parts))
4811 (push new all-parts))
4812 (setcdr new (delete (car x) (cdr new)))))
4813
4814 ;; Add any missing aliases (separate by slashes)
4815 (loop for x in all-parts do
4816 (if (cdr x)
4817 (push (cons (nth 1 x) (car x)) aliases)))
4818
4819 (loop for x in aliases do
4820 (when (setq alias (assoc (cdr x) idlwave-system-routines))
4821 (unless (memq alias remove-list) (push alias remove-list))
4822 (setq alias (copy-sequence alias))
4823 (setcar alias (car x))
4824 (push alias idlwave-system-routines)))
4825 (loop for x in remove-list do
4826 (delq x idlwave-system-routines))))
4827
4828 (defun idlwave-convert-xml-clean-sysvar-aliases (aliases)
4829 ;; Duplicate and trim original routine aliases from rinfo list
4830 ;; This if for, e.g. !X, !Y, !Z.
4831 (let (alias remove-list new parts all-parts)
4832 (loop for x in aliases do
4833 (when (setq alias (assoc (cdr x) idlwave-system-variables-alist))
4834 (unless (memq alias remove-list) (push alias remove-list))
4835 (setq alias (copy-sequence alias))
4836 (setcar alias (car x))
4837 (push alias idlwave-system-variables-alist)))
4838 (loop for x in remove-list do
4839 (delq x idlwave-system-variables-alist))))
4840
4841
4842 (defun idlwave-xml-create-sysvar-alist (xml-entry)
4843 ;; Create a sysvar list entry from the xml parsed list.
4844 (let* ((nameblock (nth 1 xml-entry))
4845 (sysvar (substring (cdr (assq 'name nameblock)) 1))
4846 (link (cdr (assq 'link nameblock)))
4847 (params (cddr xml-entry))
4848 (case-fold-search t)
4849 pelem ptype props fields tags)
4850 (while params
4851 (setq pelem (car params))
4852 (when (listp pelem)
4853 (setq ptype (car pelem)
4854 props (car (cdr pelem)))
4855 (cond
4856 ((eq ptype 'FIELD)
4857 (push (cons (cdr (assq 'name props))
4858 (cdr
4859 (idlwave-split-link-target (cdr (assq 'link props)))))
4860 tags))))
4861 (setq params (cdr params)))
4862 (delq nil
4863 (list sysvar (if tags (cons 'tags tags)) (list 'link link)))))
4864
4865
4866 (defvar idlwave-xml-routine-info-file nil)
4867
4868 (defun idlwave-save-routine-info ()
4869 (if idlwave-xml-routine-info-file
4870 (with-temp-file idlwave-xml-system-rinfo-converted-file
4871 (insert
4872 (concat ";; *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
4873 ;; IDLWAVE Routine Information File (IDLWAVE version " idlwave-mode-version ")
4874 ;; Automatically generated from source file:
4875 ;; " idlwave-xml-routine-info-file "
4876 ;; on " (current-time-string) "
4877 ;; Do not edit."))
4878 (insert (format "\n(setq idlwave-xml-routine-info-file \n \"%s\")"
4879 idlwave-xml-routine-info-file))
4880 (insert "\n(setq idlwave-system-routines\n '")
4881 (prin1 idlwave-system-routines (current-buffer))
4882 (insert ")")
4883 (insert "\n(setq idlwave-system-variables-alist\n '")
4884 (prin1 idlwave-system-variables-alist (current-buffer))
4885 (insert ")")
4886 (insert "\n(setq idlwave-system-class-info\n '")
4887 (prin1 idlwave-system-class-info (current-buffer))
4888 (insert ")")
4889 (insert "\n(setq idlwave-executive-commands-alist\n '")
4890 (prin1 idlwave-executive-commands-alist (current-buffer))
4891 (insert ")")
4892 (insert "\n(setq idlwave-help-special-topic-words\n '")
4893 (prin1 idlwave-help-special-topic-words (current-buffer))
4894 (insert ")"))))
4895
4896 (defun idlwave-convert-xml-system-routine-info ()
4897 "Convert XML supplied IDL routine info into internal form.
4898 Cache to disk for quick recovery."
4899 (interactive)
4900 (let* ((dir (file-name-as-directory
4901 (expand-file-name "help/online_help" (idlwave-sys-dir))))
4902 (catalog-file (expand-file-name "idl_catalog.xml" dir))
4903 (elem-cnt 0)
4904 props rinfo msg-cnt elem type nelem class-result alias
4905 routines routine-aliases statement-aliases sysvar-aliases
4906 version-string)
4907 (if (not (file-exists-p catalog-file))
4908 (error "No such XML routine info file: %s" catalog-file)
4909 (if (not (file-readable-p catalog-file))
4910 (error "Cannot read XML routine info file: %s" catalog-file)))
4911 (require 'xml)
4912 (message "Reading XML routine info...")
4913 (setq rinfo (xml-parse-file catalog-file))
4914 (message "Reading XML routine info...done")
4915 (setq rinfo (assq 'CATALOG rinfo))
4916 (unless rinfo (error "Failed to parse XML routine info"))
4917 ;;(setq rinfo (car rinfo)) ; Skip the catalog stuff.
4918
4919 (setq version-string (cdr (assq 'version (nth 1 rinfo)))
4920 rinfo (cddr rinfo))
4921
4922 (setq nelem (length rinfo)
4923 msg-cnt (/ nelem 20))
4924
4925 (setq idlwave-xml-routine-info-file nil)
4926 (message "Converting XML routine info...")
4927 (setq idlwave-system-routines nil
4928 idlwave-system-variables-alist nil
4929 idlwave-system-class-info nil
4930 idlwave-executive-commands-alist nil
4931 idlwave-help-special-topic-words nil)
4932
4933 (while rinfo
4934 (setq elem (car rinfo)
4935 rinfo (cdr rinfo))
4936 (incf elem-cnt)
4937 (when (listp elem)
4938 (setq type (car elem)
4939 props (car (cdr elem)))
4940 (if (= (mod elem-cnt msg-cnt) 0)
4941 (message "Converting XML routine info...%2d%%"
4942 (/ (* elem-cnt 100) nelem)))
4943 (cond
4944 ((eq type 'ROUTINE)
4945 (if (setq alias (assq 'alias_to props))
4946 (push (cons (cdr (assq 'name props)) (cdr alias))
4947 routine-aliases)
4948 (setq routines (idlwave-xml-create-rinfo-list elem))
4949 (if (listp (cdr routines))
4950 (setq idlwave-system-routines
4951 (nconc idlwave-system-routines routines))
4952 ;; a cons cell is an executive commands
4953 (push routines idlwave-executive-commands-alist))))
4954
4955 ((eq type 'CLASS)
4956 (setq class-result (idlwave-xml-create-class-method-lists elem))
4957 (push (car class-result) idlwave-system-class-info)
4958 (setq idlwave-system-routines
4959 (nconc idlwave-system-routines (cdr class-result))))
4960
4961 ((eq type 'STATEMENT)
4962 (push (cons (cdr (assq 'name props))
4963 (cdr (assq 'link props)))
4964 idlwave-help-special-topic-words)
4965 ;; Save the links to those which are statement aliases (not routines)
4966 (if (setq alias (assq 'alias_to props))
4967 (unless (member (cdr alias) statement-aliases)
4968 (push (cdr alias) statement-aliases))))
4969
4970 ((eq type 'SYSVAR)
4971 (if (setq alias (cdr (assq 'alias_to props)))
4972 (push (cons (substring (cdr (assq 'name props)) 1)
4973 (substring alias 1))
4974 sysvar-aliases)
4975 (push (idlwave-xml-create-sysvar-alist elem)
4976 idlwave-system-variables-alist)))
4977 (t))))
4978 (idlwave-convert-xml-clean-routine-aliases routine-aliases)
4979 (idlwave-convert-xml-clean-statement-aliases statement-aliases)
4980 (idlwave-convert-xml-clean-sysvar-aliases sysvar-aliases)
4981
4982 (setq idlwave-xml-routine-info-file catalog-file)
4983 (idlwave-save-routine-info)
4984 (message "Converting XML routine info...done")))
4985
4986
4987 ;; ("ROUTINE" type class
4988 ;; (system) | (lib pro_file dir "LIBNAME") | (user pro_file dir "USERLIB") |
4989 ;; (buffer pro_file dir) | (compiled pro_file dir)
4990 ;; "calling_string" ("HELPFILE" (("KWD1" . link1) ...))
4991 ;; ("HELPFILE2" (("KWD2" . link) ...)) ...)
4992
4993
4994 (defun idlwave-load-rinfo-next-step ()
4995 (let ((inhibit-quit t)
4996 (arr idlwave-load-rinfo-steps-done))
4997 (if (catch 'exit
4998 (when (not (aref arr 0))
4999 (message "Loading system routine info in idle time...")
5000 (idlwave-load-system-routine-info)
5001 ;;(load "idlw-rinfo" 'noerror 'nomessage)
5002 (message "Loading system routine info in idle time...done")
5003 (aset arr 0 t)
5004 (throw 'exit t))
5005
5006 (when (not (aref arr 1))
5007 (message "Normalizing idlwave-system-routines in idle time...")
5008 (idlwave-reset-sintern t)
5009 (put 'idlwave-reset-sintern 'done-by-idle t)
5010 (setq idlwave-system-routines
5011 (idlwave-sintern-rinfo-list idlwave-system-routines 'sys))
5012 (message "Normalizing idlwave-system-routines in idle time...done")
5013 (aset arr 1 t)
5014 (throw 'exit t))
5015
5016 (when (not (aref arr 2))
5017 (when (and (stringp idlwave-user-catalog-file)
5018 (file-regular-p idlwave-user-catalog-file))
5019 (message "Loading user catalog in idle time...")
5020 (condition-case nil
5021 (load-file idlwave-user-catalog-file)
5022 (error (throw 'exit nil)))
5023 ;; Check for the old style catalog and warn
5024 (if (and
5025 (boundp 'idlwave-library-routines)
5026 idlwave-library-routines)
5027 (progn
5028 (setq idlwave-library-routines nil)
5029 (ding)
5030 (message "Outdated user catalog: %s... recreate"
5031 idlwave-user-catalog-file))
5032 (message "Loading user catalog in idle time...done")))
5033 (aset arr 2 t)
5034 (throw 'exit t))
5035
5036 (when (not (aref arr 3))
5037 (when idlwave-user-catalog-routines
5038 (message "Normalizing user catalog routines in idle time...")
5039 (setq idlwave-user-catalog-routines
5040 (idlwave-sintern-rinfo-list
5041 idlwave-user-catalog-routines 'sys))
5042 (message
5043 "Normalizing user catalog routines in idle time...done"))
5044 (aset arr 3 t)
5045 (throw 'exit t))
5046
5047 (when (not (aref arr 4))
5048 (idlwave-scan-library-catalogs
5049 "Loading and normalizing library catalogs in idle time...")
5050 (aset arr 4 t)
5051 (throw 'exit t))
5052 (when (not (aref arr 5))
5053 (message "Finishing initialization in idle time...")
5054 (idlwave-routines)
5055 (message "Finishing initialization in idle time...done")
5056 (aset arr 5 t)
5057 (throw 'exit nil)))
5058 ;; restart the timer
5059 (if (sit-for 1)
5060 (idlwave-load-rinfo-next-step)
5061 (setq idlwave-load-rinfo-idle-timer
5062 (run-with-idle-timer
5063 idlwave-init-rinfo-when-idle-after
5064 nil 'idlwave-load-rinfo-next-step))))))
5065
5066 (defun idlwave-load-all-rinfo (&optional force)
5067 ;; Load and case-treat the system, user catalog, and library routine
5068 ;; info files.
5069
5070 ;; System
5071 (when (or force (not (aref idlwave-load-rinfo-steps-done 0)))
5072 ;;(load "idlw-rinfo" 'noerror 'nomessage))
5073 (idlwave-load-system-routine-info))
5074 (when (or force (not (aref idlwave-load-rinfo-steps-done 1)))
5075 (message "Normalizing idlwave-system-routines...")
5076 (setq idlwave-system-routines
5077 (idlwave-sintern-rinfo-list idlwave-system-routines 'sys))
5078 (message "Normalizing idlwave-system-routines...done"))
5079 (when idlwave-system-routines
5080 (setq idlwave-routines (copy-sequence idlwave-system-routines))
5081 (setq idlwave-last-system-routine-info-cons-cell
5082 (nthcdr (1- (length idlwave-routines)) idlwave-routines)))
5083
5084 ;; User catalog
5085 (when (and (stringp idlwave-user-catalog-file)
5086 (file-regular-p idlwave-user-catalog-file))
5087 (condition-case nil
5088 (when (or force (not (aref idlwave-load-rinfo-steps-done 2)))
5089 (load-file idlwave-user-catalog-file))
5090 (error nil))
5091 (when (and
5092 (boundp 'idlwave-library-routines)
5093 idlwave-library-routines)
5094 (setq idlwave-library-routines nil)
5095 (error "Outdated user catalog: %s... recreate"
5096 idlwave-user-catalog-file))
5097 (setq idlwave-true-path-alist nil)
5098 (when (or force (not (aref idlwave-load-rinfo-steps-done 3)))
5099 (message "Normalizing user catalog routines...")
5100 (setq idlwave-user-catalog-routines
5101 (idlwave-sintern-rinfo-list
5102 idlwave-user-catalog-routines 'sys))
5103 (message "Normalizing user catalog routines...done")))
5104
5105 ;; Library catalog
5106 (when (or force (not (aref idlwave-load-rinfo-steps-done 4)))
5107 (idlwave-scan-library-catalogs
5108 "Loading and normalizing library catalogs..."))
5109 (run-hooks 'idlwave-after-load-rinfo-hook))
5110
5111
5112 (defun idlwave-update-buffer-routine-info ()
5113 (let (res)
5114 (cond
5115 ((eq idlwave-scan-all-buffers-for-routine-info t)
5116 ;; Scan all buffers, current buffer last
5117 (message "Scanning all buffers...")
5118 (setq res (idlwave-get-routine-info-from-buffers
5119 (reverse (buffer-list)))))
5120 ((null idlwave-scan-all-buffers-for-routine-info)
5121 ;; Don't scan any buffers
5122 (setq res nil))
5123 (t
5124 ;; Just scan this buffer
5125 (if (eq major-mode 'idlwave-mode)
5126 (progn
5127 (message "Scanning current buffer...")
5128 (setq res (idlwave-get-routine-info-from-buffers
5129 (list (current-buffer))))))))
5130 ;; Put the result into the correct variable
5131 (setq idlwave-buffer-routines
5132 (idlwave-sintern-rinfo-list res 'set))))
5133
5134 (defun idlwave-concatenate-rinfo-lists (&optional quiet run-hook)
5135 "Put the different sources for routine information together."
5136 ;; The sequence here is important because earlier definitions shadow
5137 ;; later ones. We assume that if things in the buffers are newer
5138 ;; then in the shell of the system, they are meant to be different.
5139 (setcdr idlwave-last-system-routine-info-cons-cell
5140 (append idlwave-buffer-routines
5141 idlwave-compiled-routines
5142 idlwave-library-catalog-routines
5143 idlwave-user-catalog-routines))
5144 (setq idlwave-class-alist nil)
5145
5146 ;; Give a message with information about the number of routines we have.
5147 (unless quiet
5148 (message
5149 "Routines Found: buffer(%d) compiled(%d) library(%d) user(%d) system(%d)"
5150 (length idlwave-buffer-routines)
5151 (length idlwave-compiled-routines)
5152 (length idlwave-library-catalog-routines)
5153 (length idlwave-user-catalog-routines)
5154 (length idlwave-system-routines)))
5155 (if run-hook
5156 (run-hooks 'idlwave-update-rinfo-hook)))
5157
5158 (defun idlwave-class-alist ()
5159 "Return the class alist - make it if necessary."
5160 (or idlwave-class-alist
5161 (let (class)
5162 (loop for x in idlwave-routines do
5163 (when (and (setq class (nth 2 x))
5164 (not (assq class idlwave-class-alist)))
5165 (push (list class) idlwave-class-alist)))
5166 idlwave-class-alist)))
5167
5168 ;; Three functions for the hooks
5169 (defun idlwave-save-buffer-update ()
5170 (idlwave-update-current-buffer-info 'save-buffer))
5171 (defun idlwave-kill-buffer-update ()
5172 (idlwave-update-current-buffer-info 'kill-buffer))
5173 (defun idlwave-new-buffer-update ()
5174 (idlwave-update-current-buffer-info 'find-file))
5175
5176 (defun idlwave-update-current-buffer-info (why)
5177 "Update idlwave-routines for current buffer. Can run from after-save-hook."
5178 (when (and (eq major-mode 'idlwave-mode)
5179 (or (eq t idlwave-auto-routine-info-updates)
5180 (memq why idlwave-auto-routine-info-updates))
5181 idlwave-scan-all-buffers-for-routine-info
5182 idlwave-routines)
5183 (condition-case nil
5184 (let (routines)
5185 (idlwave-replace-buffer-routine-info
5186 (buffer-file-name)
5187 (if (eq why 'kill-buffer)
5188 nil
5189 (setq routines
5190 (idlwave-sintern-rinfo-list
5191 (idlwave-get-routine-info-from-buffers
5192 (list (current-buffer))) 'set))))
5193 (idlwave-concatenate-rinfo-lists 'quiet)
5194 routines)
5195 (error nil))))
5196
5197 (defun idlwave-replace-buffer-routine-info (file new)
5198 "Cut the part from FILE out of `idlwave-buffer-routines' and add NEW."
5199 (let ((list idlwave-buffer-routines)
5200 found)
5201 (while list
5202 ;; The following test uses eq to make sure it works correctly
5203 ;; when two buffers visit the same file. Then the file names
5204 ;; will be equal, but not eq.
5205 (if (eq (idlwave-routine-source-file (nth 3 (car list))) file)
5206 (progn
5207 (setcar list nil)
5208 (setq found t))
5209 (if found
5210 ;; End of that section reached. Jump.
5211 (setq list nil)))
5212 (setq list (cdr list)))
5213 (setq idlwave-buffer-routines
5214 (append new (delq nil idlwave-buffer-routines)))))
5215
5216 ;;----- Scanning buffers -------------------
5217
5218 (defun idlwave-get-routine-info-from-buffers (buffers)
5219 "Call `idlwave-get-buffer-routine-info' on idlwave-mode buffers in BUFFERS."
5220 (let (buf routine-lists res)
5221 (save-excursion
5222 (while (setq buf (pop buffers))
5223 (set-buffer buf)
5224 (if (and (eq major-mode 'idlwave-mode)
5225 buffer-file-name)
5226 ;; yes, this buffer has the right mode.
5227 (progn (setq res (condition-case nil
5228 (idlwave-get-buffer-routine-info)
5229 (error nil)))
5230 (push res routine-lists)))))
5231 ;; Concatenate the individual lists and return the result
5232 (apply 'nconc routine-lists)))
5233
5234 (defun idlwave-get-buffer-routine-info ()
5235 "Scan the current buffer for routine info. Return (PRO-LIST FUNC-LIST)."
5236 (let* ((case-fold-search t)
5237 routine-list string entry)
5238 (save-excursion
5239 (save-restriction
5240 (widen)
5241 (goto-char (point-min))
5242 (while (re-search-forward
5243 "^[ \t]*\\(pro\\|function\\)[ \t]" nil t)
5244 (setq string (buffer-substring-no-properties
5245 (match-beginning 0)
5246 (progn
5247 (idlwave-end-of-statement)
5248 (point))))
5249 (setq entry (idlwave-parse-definition string))
5250 (push entry routine-list))))
5251 routine-list))
5252
5253 (defvar idlwave-scanning-lib-dir)
5254 (defun idlwave-parse-definition (string)
5255 "Parse a module definition."
5256 (let ((case-fold-search t)
5257 start name args type keywords class)
5258 ;; Remove comments
5259 (while (string-match ";.*" string)
5260 (setq string (replace-match "" t t string)))
5261 ;; Remove the continuation line stuff
5262 (while (string-match "\\([^a-zA-Z0-9$_]\\)\\$[ \t]*\n" string)
5263 (setq string (replace-match "\\1 " t nil string)))
5264 (while (string-match "\n" string)
5265 (setq string (replace-match " " t nil string)))
5266 ;; Match the name and type.
5267 (when (string-match
5268 "\\<\\(pro\\|function\\)\\>\\s-+\\(\\([a-zA-Z0-9$_]+\\)::\\)?\\([a-zA-Z0-9$_]+\\)" string)
5269 (setq start (match-end 0))
5270 (setq type (downcase (match-string 1 string)))
5271 (if (match-beginning 3)
5272 (setq class (match-string 3 string)))
5273 (setq name (match-string 4 string)))
5274 ;; Match normal args and keyword args
5275 (while (string-match
5276 ",\\s-*\\([a-zA-Z][a-zA-Z0-9$_]*\\|\\(_ref\\)?_extra\\)\\s-*\\(=\\)?"
5277 string start)
5278 (setq start (match-end 0))
5279 (if (match-beginning 3)
5280 (push (match-string 1 string) keywords)
5281 (push (match-string 1 string) args)))
5282 ;; Normalize and sort.
5283 (setq args (nreverse args))
5284 (setq keywords (sort keywords (lambda (a b)
5285 (string< (downcase a) (downcase b)))))
5286 ;; Make and return the entry
5287 ;; We don't know which argument are optional, so this information
5288 ;; will not be contained in the calling sequence.
5289 (list name
5290 (if (equal type "pro") 'pro 'fun)
5291 class
5292 (cond ((not (boundp 'idlwave-scanning-lib))
5293 (list 'buffer (buffer-file-name)))
5294 ; ((string= (downcase
5295 ; (file-name-sans-extension
5296 ; (file-name-nondirectory (buffer-file-name))))
5297 ; (downcase name))
5298 ; (list 'lib))
5299 ; (t (cons 'lib (file-name-nondirectory (buffer-file-name))))
5300 (t (list 'user (file-name-nondirectory (buffer-file-name))
5301 idlwave-scanning-lib-dir "UserLib")))
5302 (concat
5303 (if (string= type "function") "Result = " "")
5304 (if class "Obj ->[%s::]" "")
5305 "%s"
5306 (if args
5307 (concat
5308 (if (string= type "function") "(" ", ")
5309 (mapconcat 'identity args ", ")
5310 (if (string= type "function") ")" ""))))
5311 (if keywords
5312 (cons nil (mapcar 'list keywords)) ;No help file
5313 nil))))
5314
5315
5316 ;;----- Scanning the user catalog -------------------
5317
5318 (defun idlwave-sys-dir ()
5319 "Return the syslib directory, or a dummy that never matches."
5320 (cond
5321 ((and idlwave-system-directory
5322 (not (string= idlwave-system-directory "")))
5323 idlwave-system-directory)
5324 ((getenv "IDL_DIR"))
5325 (t "@@@@@@@@")))
5326
5327
5328 (defun idlwave-create-user-catalog-file (&optional arg)
5329 "Scan all files on selected dirs of IDL search path for routine information.
5330
5331 A widget checklist will allow you to choose the directories. Write
5332 the result as a file `idlwave-user-catalog-file'. When this file
5333 exists, will be automatically loaded to give routine information about
5334 library routines. With ARG, just rescan the same directories as last
5335 time - so no widget will pop up."
5336 (interactive "P")
5337 ;; Make sure the file is loaded if it exists.
5338 (if (and (stringp idlwave-user-catalog-file)
5339 (file-regular-p idlwave-user-catalog-file))
5340 (condition-case nil
5341 (load-file idlwave-user-catalog-file)
5342 (error nil)))
5343 ;; Make sure the file name makes sense
5344 (unless (and (stringp idlwave-user-catalog-file)
5345 (> (length idlwave-user-catalog-file) 0)
5346 (file-accessible-directory-p
5347 (file-name-directory idlwave-user-catalog-file))
5348 (not (string= "" (file-name-nondirectory
5349 idlwave-user-catalog-file))))
5350 (error "`idlwave-user-catalog-file' does not point to a file in an accessible directory"))
5351
5352 (cond
5353 ;; Rescan the known directories
5354 ((and arg idlwave-path-alist
5355 (consp (car idlwave-path-alist)))
5356 (idlwave-scan-user-lib-files idlwave-path-alist))
5357
5358 ;; Expand the directories from library-path and run the widget
5359 (idlwave-library-path
5360 (idlwave-display-user-catalog-widget
5361 (if idlwave-true-path-alist
5362 ;; Propagate any flags on the existing path-alist
5363 (mapcar (lambda (x)
5364 (let ((path-entry (assoc (file-truename x)
5365 idlwave-true-path-alist)))
5366 (if path-entry
5367 (cons x (cdr path-entry))
5368 (list x))))
5369 (idlwave-expand-path idlwave-library-path))
5370 (mapcar 'list (idlwave-expand-path idlwave-library-path)))))
5371
5372 ;; Ask the shell for the path and then run the widget
5373 (t
5374 (message "Asking the shell for IDL path...")
5375 (require 'idlw-shell)
5376 (idlwave-shell-send-command idlwave-shell-path-query
5377 '(idlwave-user-catalog-command-hook nil)
5378 'hide))))
5379
5380
5381 ;; Parse shell path information and select among it.
5382 (defun idlwave-user-catalog-command-hook (&optional arg)
5383 ;; Command hook used by `idlwave-create-user-catalog-file'.
5384 (if arg
5385 ;; Scan immediately
5386 (idlwave-scan-user-lib-files idlwave-path-alist)
5387 ;; Set the path and display the widget
5388 (idlwave-shell-get-path-info 'no-write) ; set to something path-alist
5389 (idlwave-scan-library-catalogs "Locating library catalogs..." 'no-load)
5390 (idlwave-display-user-catalog-widget idlwave-path-alist)))
5391
5392 (defconst idlwave-user-catalog-widget-help-string
5393 "This is the front-end to the creation of the IDLWAVE user catalog.
5394 Please select the directories on IDL's search path from which you
5395 would like to extract routine information, to be stored in the file:
5396
5397 %s
5398
5399 If this is not the correct file, first set variable
5400 `idlwave-user-catalog-file', and call this command again.
5401
5402 N.B. Many libraries include pre-scanned catalog files
5403 \(\".idlwave_catalog\"). These are marked with \"[LIB]\", and need
5404 not be scanned. You can scan your own libraries off-line using the
5405 perl script `idlwave_catalog'.
5406
5407 After selecting the directories, choose [Scan & Save] to scan the library
5408 directories and save the routine info.
5409 \n")
5410
5411 (defvar idlwave-widget)
5412 (defvar widget-keymap)
5413 (defun idlwave-display-user-catalog-widget (dirs-list)
5414 "Create the widget to select IDL search path directories for scanning."
5415 (interactive)
5416 (require 'widget)
5417 (require 'wid-edit)
5418 (unless dirs-list
5419 (error "Don't know IDL's search path"))
5420
5421 (kill-buffer (get-buffer-create "*IDLWAVE Widget*"))
5422 (switch-to-buffer (get-buffer-create "*IDLWAVE Widget*"))
5423 (kill-all-local-variables)
5424 (make-local-variable 'idlwave-widget)
5425 (widget-insert (format idlwave-user-catalog-widget-help-string
5426 idlwave-user-catalog-file))
5427
5428 (widget-create 'push-button
5429 :notify 'idlwave-widget-scan-user-lib-files
5430 "Scan & Save")
5431 (widget-insert " ")
5432 (widget-create 'push-button
5433 :notify 'idlwave-delete-user-catalog-file
5434 "Delete File")
5435 (widget-insert " ")
5436 (widget-create 'push-button
5437 :notify
5438 '(lambda (&rest ignore)
5439 (let ((path-list (widget-get idlwave-widget :path-dirs)))
5440 (mapcar (lambda (x)
5441 (unless (memq 'lib (cdr x))
5442 (idlwave-path-alist-add-flag x 'user)))
5443 path-list)
5444 (idlwave-display-user-catalog-widget path-list)))
5445 "Select All Non-Lib")
5446 (widget-insert " ")
5447 (widget-create 'push-button
5448 :notify
5449 '(lambda (&rest ignore)
5450 (let ((path-list (widget-get idlwave-widget :path-dirs)))
5451 (mapcar (lambda (x)
5452 (idlwave-path-alist-remove-flag x 'user))
5453 path-list)
5454 (idlwave-display-user-catalog-widget path-list)))
5455 "Deselect All")
5456 (widget-insert " ")
5457 (widget-create 'push-button
5458 :notify (lambda (&rest ignore)
5459 (kill-buffer (current-buffer)))
5460 "Quit")
5461 (widget-insert "\n\n")
5462
5463 (widget-insert "Select Directories: \n")
5464
5465 (setq idlwave-widget
5466 (apply 'widget-create
5467 'checklist
5468 :value (delq nil (mapcar (lambda (x)
5469 (if (memq 'user (cdr x))
5470 (car x)))
5471 dirs-list))
5472 :greedy t
5473 :tag "List of directories"
5474 (mapcar (lambda (x)
5475 (list 'item
5476 (if (memq 'lib (cdr x))
5477 (concat "[LIB] " (car x) )
5478 (car x)))) dirs-list)))
5479 (widget-put idlwave-widget :path-dirs dirs-list)
5480 (widget-insert "\n")
5481 (use-local-map widget-keymap)
5482 (widget-setup)
5483 (goto-char (point-min))
5484 (delete-other-windows))
5485
5486 (defun idlwave-delete-user-catalog-file (&rest ignore)
5487 (if (yes-or-no-p
5488 (format "Delete file %s " idlwave-user-catalog-file))
5489 (progn
5490 (delete-file idlwave-user-catalog-file)
5491 (message "%s has been deleted" idlwave-user-catalog-file))))
5492
5493 (defun idlwave-widget-scan-user-lib-files (&rest ignore)
5494 ;; Call `idlwave-scan-user-lib-files' with data taken from the widget.
5495 (let* ((widget idlwave-widget)
5496 (selected-dirs (widget-value widget))
5497 (path-alist (widget-get widget :path-dirs))
5498 (this-path-alist path-alist)
5499 dir-entry)
5500 (while (setq dir-entry (pop this-path-alist))
5501 (if (member
5502 (if (memq 'lib (cdr dir-entry))
5503 (concat "[LIB] " (car dir-entry))
5504 (car dir-entry))
5505 selected-dirs)
5506 (idlwave-path-alist-add-flag dir-entry 'user)
5507 (idlwave-path-alist-remove-flag dir-entry 'user)))
5508 (idlwave-scan-user-lib-files path-alist)))
5509
5510 (defvar font-lock-mode)
5511 (defun idlwave-scan-user-lib-files (path-alist)
5512 ;; Scan the PRO files in PATH-ALIST and store the info in the user catalog
5513 (let* ((idlwave-scanning-lib t)
5514 (idlwave-scanning-lib-dir "")
5515 (idlwave-completion-case nil)
5516 dirs-alist dir files file)
5517 (setq idlwave-user-catalog-routines nil
5518 idlwave-path-alist path-alist ; for library-path instead
5519 idlwave-true-path-alist nil)
5520 (if idlwave-auto-write-paths (idlwave-write-paths))
5521 (save-excursion
5522 (set-buffer (get-buffer-create "*idlwave-scan.pro*"))
5523 (idlwave-mode)
5524 (setq dirs-alist (reverse path-alist))
5525 (while (setq dir (pop dirs-alist))
5526 (when (memq 'user (cdr dir)) ; Has it marked for scan?
5527 (setq dir (car dir))
5528 (setq idlwave-scanning-lib-dir dir)
5529 (when (file-directory-p dir)
5530 (setq files (directory-files dir 'full "\\.[pP][rR][oO]\\'"))
5531 (while (setq file (pop files))
5532 (when (file-regular-p file)
5533 (if (not (file-readable-p file))
5534 (message "Skipping %s (no read permission)" file)
5535 (message "Scanning %s..." file)
5536 (erase-buffer)
5537 (insert-file-contents file 'visit)
5538 (setq idlwave-user-catalog-routines
5539 (append (idlwave-get-routine-info-from-buffers
5540 (list (current-buffer)))
5541 idlwave-user-catalog-routines)))))))))
5542 (message "Creating user catalog file...")
5543 (kill-buffer "*idlwave-scan.pro*")
5544 (kill-buffer (get-buffer-create "*IDLWAVE Widget*"))
5545 (let ((font-lock-maximum-size 0)
5546 (auto-mode-alist nil))
5547 (find-file idlwave-user-catalog-file))
5548 (if (and (boundp 'font-lock-mode)
5549 font-lock-mode)
5550 (font-lock-mode 0))
5551 (erase-buffer)
5552 (insert ";; IDLWAVE user catalog file\n")
5553 (insert (format ";; Created %s\n\n" (current-time-string)))
5554
5555 ;; Define the routine info list
5556 (insert "\n(setq idlwave-user-catalog-routines\n '(")
5557 (let ((standard-output (current-buffer)))
5558 (mapcar (lambda (x)
5559 (insert "\n ")
5560 (prin1 x)
5561 (goto-char (point-max)))
5562 idlwave-user-catalog-routines))
5563 (insert (format "))\n\n;;; %s ends here\n"
5564 (file-name-nondirectory idlwave-user-catalog-file)))
5565 (goto-char (point-min))
5566 ;; Save the buffer
5567 (save-buffer 0)
5568 (kill-buffer (current-buffer)))
5569 (message "Creating user catalog file...done")
5570 (message "Info for %d routines saved in %s"
5571 (length idlwave-user-catalog-routines)
5572 idlwave-user-catalog-file)
5573 (sit-for 2)
5574 (idlwave-update-routine-info t))
5575
5576 (defun idlwave-read-paths ()
5577 (if (and (stringp idlwave-path-file)
5578 (file-regular-p idlwave-path-file))
5579 (condition-case nil
5580 (load idlwave-path-file t t t)
5581 (error nil))))
5582
5583 (defun idlwave-write-paths ()
5584 (interactive)
5585 (when (and idlwave-path-alist idlwave-system-directory)
5586 (let ((font-lock-maximum-size 0)
5587 (auto-mode-alist nil))
5588 (find-file idlwave-path-file))
5589 (if (and (boundp 'font-lock-mode)
5590 font-lock-mode)
5591 (font-lock-mode 0))
5592 (erase-buffer)
5593 (insert ";; IDLWAVE paths\n")
5594 (insert (format ";; Created %s\n\n" (current-time-string)))
5595 ;; Define the variable which knows the value of "!DIR"
5596 (insert (format "\n(setq idlwave-system-directory \"%s\")\n"
5597 idlwave-system-directory))
5598
5599 ;; Define the variable which contains a list of all scanned directories
5600 (insert "\n(setq idlwave-path-alist\n '(")
5601 (let ((standard-output (current-buffer)))
5602 (mapcar (lambda (x)
5603 (insert "\n ")
5604 (prin1 x)
5605 (goto-char (point-max)))
5606 idlwave-path-alist))
5607 (insert "))\n")
5608 (save-buffer 0)
5609 (kill-buffer (current-buffer))))
5610
5611
5612 (defun idlwave-expand-path (path &optional default-dir)
5613 ;; Expand parts of path starting with '+' recursively into directory list.
5614 ;; Relative recursive path elements are expanded relative to DEFAULT-DIR.
5615 (message "Expanding path...")
5616 (let (path1 dir recursive)
5617 (while (setq dir (pop path))
5618 (if (setq recursive (string= (substring dir 0 1) "+"))
5619 (setq dir (substring dir 1)))
5620 (if (and recursive
5621 (not (file-name-absolute-p dir)))
5622 (setq dir (expand-file-name dir default-dir)))
5623 (if recursive
5624 ;; Expand recursively
5625 (setq path1 (append (idlwave-recursive-directory-list dir) path1))
5626 ;; Keep unchanged
5627 (push dir path1)))
5628 (message "Expanding path...done")
5629 (nreverse path1)))
5630
5631 (defun idlwave-recursive-directory-list (dir)
5632 ;; Return a list of all directories below DIR, including DIR itself
5633 (let ((path (list dir)) path1 file files)
5634 (while (setq dir (pop path))
5635 (when (file-directory-p dir)
5636 (setq files (nreverse (directory-files dir t "[^.]")))
5637 (while (setq file (pop files))
5638 (if (file-directory-p file)
5639 (push (file-name-as-directory file) path)))
5640 (push dir path1)))
5641 path1))
5642
5643
5644 ;;----- Scanning the library catalogs ------------------
5645
5646
5647
5648
5649 (defun idlwave-scan-library-catalogs (&optional message-base no-load)
5650 "Scan for library catalog files (.idlwave_catalog) and ingest.
5651
5652 All directories on `idlwave-path-alist' (or `idlwave-library-path'
5653 instead, if present) are searched. Print MESSAGE-BASE along with the
5654 libraries being loaded, if passed, and skip loading/normalizing if
5655 NO-LOAD is non-nil. The variable `idlwave-use-library-catalogs' can
5656 be set to nil to disable library catalog scanning."
5657 (when idlwave-use-library-catalogs
5658 (let ((dirs
5659 (if idlwave-library-path
5660 (idlwave-expand-path idlwave-library-path)
5661 (mapcar 'car idlwave-path-alist)))
5662 (old-libname "")
5663 dir-entry dir flags catalog all-routines)
5664 (if message-base (message message-base))
5665 (while (setq dir (pop dirs))
5666 (catch 'continue
5667 (when (file-readable-p
5668 (setq catalog (expand-file-name ".idlwave_catalog" dir)))
5669 (unless no-load
5670 (setq idlwave-library-catalog-routines nil)
5671 ;; Load the catalog file
5672 (condition-case nil
5673 (load catalog t t t)
5674 (error (throw 'continue t)))
5675 (when (and
5676 message-base
5677 (not (string= idlwave-library-catalog-libname
5678 old-libname)))
5679 (message "%s" (concat message-base
5680 idlwave-library-catalog-libname))
5681 (setq old-libname idlwave-library-catalog-libname))
5682 (when idlwave-library-catalog-routines
5683 (setq all-routines
5684 (append
5685 (idlwave-sintern-rinfo-list
5686 idlwave-library-catalog-routines 'sys dir)
5687 all-routines))))
5688
5689 ;; Add a 'lib flag if on path-alist
5690 (when (and idlwave-path-alist
5691 (setq dir-entry (assoc dir idlwave-path-alist)))
5692 (idlwave-path-alist-add-flag dir-entry 'lib)))))
5693 (unless no-load (setq idlwave-library-catalog-routines all-routines))
5694 (if message-base (message (concat message-base "done"))))))
5695
5696 ;;----- Communicating with the Shell -------------------
5697
5698 ;; First, here is the idl program which can be used to query IDL for
5699 ;; defined routines.
5700 (defconst idlwave-routine-info.pro
5701 "
5702 ;; START OF IDLWAVE SUPPORT ROUTINES
5703 pro idlwave_print_safe,item,limit
5704 catch,err
5705 if err ne 0 then begin
5706 print,'Could not print item.'
5707 return
5708 endif
5709 if n_elements(item) gt limit then $
5710 print,item[0:limit-1],'<... truncated at ',strtrim(limit,2),' elements>' $
5711 else print,item
5712 end
5713
5714 pro idlwave_print_info_entry,name,func=func,separator=sep
5715 ;; See if it's an object method
5716 if name eq '' then return
5717 func = keyword_set(func)
5718 methsep = strpos(name,'::')
5719 meth = methsep ne -1
5720
5721 ;; Get routine info
5722 pars = routine_info(name,/parameters,functions=func)
5723 source = routine_info(name,/source,functions=func)
5724 nargs = pars.num_args
5725 nkw = pars.num_kw_args
5726 if nargs gt 0 then args = pars.args
5727 if nkw gt 0 then kwargs = pars.kw_args
5728
5729 ;; Trim the class, and make the name
5730 if meth then begin
5731 class = strmid(name,0,methsep)
5732 name = strmid(name,methsep+2,strlen(name)-1)
5733 if nargs gt 0 then begin
5734 ;; remove the self argument
5735 wh = where(args ne 'SELF',nargs)
5736 if nargs gt 0 then args = args[wh]
5737 endif
5738 endif else begin
5739 ;; No class, just a normal routine.
5740 class = \"\"
5741 endelse
5742
5743 ;; Calling sequence
5744 cs = \"\"
5745 if func then cs = 'Result = '
5746 if meth then cs = cs + 'Obj -> [' + '%s' + '::]'
5747 cs = cs + '%s'
5748 if func then cs = cs + '(' else if nargs gt 0 then cs = cs + ', '
5749 if nargs gt 0 then begin
5750 for j=0,nargs-1 do begin
5751 cs = cs + args[j]
5752 if j lt nargs-1 then cs = cs + ', '
5753 endfor
5754 end
5755 if func then cs = cs + ')'
5756 ;; Keyword arguments
5757 kwstring = ''
5758 if nkw gt 0 then begin
5759 for j=0,nkw-1 do begin
5760 kwstring = kwstring + ' ' + kwargs[j]
5761 endfor
5762 endif
5763
5764 ret=(['IDLWAVE-PRO','IDLWAVE-FUN'])[func]
5765
5766 print,ret + ': ' + name + sep + class + sep + source[0].path $
5767 + sep + cs + sep + kwstring
5768 end
5769
5770 pro idlwave_routine_info,file
5771 on_error,1
5772 sep = '<@>'
5773 print,'>>>BEGIN OF IDLWAVE ROUTINE INFO (\"' + sep + '\" IS THE SEPARATOR)'
5774 all = routine_info()
5775 fileQ=n_elements(file) ne 0
5776 if fileQ then file=strtrim(file,2)
5777 for i=0L,n_elements(all)-1L do begin
5778 if fileQ then begin
5779 if (routine_info(all[i],/SOURCE)).path eq file then $
5780 idlwave_print_info_entry,all[i],separator=sep
5781 endif else idlwave_print_info_entry,all[i],separator=sep
5782 endfor
5783 all = routine_info(/functions)
5784 for i=0L,n_elements(all)-1L do begin
5785 if fileQ then begin
5786 if (routine_info(all[i],/FUNCTIONS,/SOURCE)).path eq file then $
5787 idlwave_print_info_entry,all[i],separator=sep,/FUNC
5788 endif else idlwave_print_info_entry,all[i],separator=sep,/FUNC
5789 endfor
5790 print,'>>>END OF IDLWAVE ROUTINE INFO'
5791 end
5792
5793 pro idlwave_get_sysvars
5794 on_error,1
5795 catch,error_status
5796 if error_status ne 0 then begin
5797 print, 'Cannot get info about system variables'
5798 endif else begin
5799 help,/brief,output=s,/system_variables ; ? unsafe use of OUTPUT=
5800 s = strtrim(strjoin(s,' ',/single),2) ; make one line
5801 v = strsplit(s,' +',/regex,/extract) ; get variables
5802 for i=0L,n_elements(v)-1 do begin
5803 t = [''] ; get tag list
5804 a=execute('if n_tags('+v[i]+') gt 0 then t=tag_names('+v[i]+')')
5805 print, 'IDLWAVE-SYSVAR: '+v[i]+' '+strjoin(t,' ',/single)
5806 endfor
5807 endelse
5808 end
5809
5810 pro idlwave_get_class_tags, class
5811 res = execute('tags=tag_names({'+class+'})')
5812 if res then print,'IDLWAVE-CLASS-TAGS: '+class+' '+strjoin(tags,' ',/single)
5813 end
5814 ;; END OF IDLWAVE SUPPORT ROUTINES
5815 "
5816 "The idl programs to get info from the shell.")
5817
5818 (defvar idlwave-idlwave_routine_info-compiled nil
5819 "Remembers if the routine info procedure is already compiled.")
5820
5821 (defvar idlwave-shell-temp-pro-file)
5822 (defvar idlwave-shell-temp-rinfo-save-file)
5823
5824 (defun idlwave-shell-compile-helper-routines (&optional wait)
5825 (unless (and idlwave-idlwave_routine_info-compiled
5826 (file-readable-p (idlwave-shell-temp-file 'rinfo)))
5827 (save-excursion
5828 (set-buffer (idlwave-find-file-noselect
5829 (idlwave-shell-temp-file 'pro)))
5830 (erase-buffer)
5831 (insert idlwave-routine-info.pro)
5832 (save-buffer 0))
5833 (idlwave-shell-send-command
5834 (concat ".run \"" idlwave-shell-temp-pro-file "\"")
5835 nil 'hide wait)
5836 (idlwave-shell-send-command
5837 (format "save,'idlwave_print_safe','idlwave_routine_info','idlwave_print_info_entry','idlwave_get_class_tags','idlwave_get_sysvars',FILE='%s',/ROUTINES"
5838 (idlwave-shell-temp-file 'rinfo))
5839 nil 'hide)
5840 (setq idlwave-idlwave_routine_info-compiled t))
5841
5842 ;; Restore if necessary. Must use execute to hide lame routine_info
5843 ;; errors on undefinded routine
5844 (idlwave-shell-send-command
5845 (format "if execute(\"_v=routine_info('idlwave_routine_info',/SOURCE)\") eq 0 then restore,'%s' else if _v.path eq '' then restore,'%s'"
5846 idlwave-shell-temp-rinfo-save-file
5847 idlwave-shell-temp-rinfo-save-file)
5848 nil 'hide))
5849
5850
5851 (defun idlwave-shell-update-routine-info (&optional quiet run-hooks wait file)
5852 "Query the shell for routine_info of compiled modules and update the lists."
5853 ;; Save and compile the procedure. The compiled procedure is then
5854 ;; saved into an IDL SAVE file, to allow for fast RESTORE. We may
5855 ;; need to test for and possibly RESTORE the procedure each time we
5856 ;; use it, since the user may have killed or redefined it. In
5857 ;; particular, .RESET_SESSION will kill all user procedures. If
5858 ;; FILE is set, only update routine info for routines in that file.
5859
5860 (idlwave-shell-compile-helper-routines wait)
5861 ; execute the routine_info procedure, and analyze the output
5862 (idlwave-shell-send-command
5863 (format "idlwave_routine_info%s" (if file (concat ",'" file "'") ""))
5864 `(progn
5865 (idlwave-shell-routine-info-filter)
5866 (idlwave-concatenate-rinfo-lists ,quiet ,run-hooks))
5867 'hide wait))
5868
5869 ;; ---------------------------------------------------------------------------
5870 ;;
5871 ;; Completion and displaying routine calling sequences
5872
5873 (defvar idlwave-completion-help-info nil)
5874 (defvar idlwave-completion-help-links nil)
5875 (defvar idlwave-current-obj_new-class nil)
5876 (defvar idlwave-complete-special nil)
5877
5878 (defun idlwave-complete (&optional arg module class)
5879 "Complete a function, procedure or keyword name at point.
5880 This function is smart and figures out what can be completed
5881 at this point.
5882 - At the beginning of a statement it completes procedure names.
5883 - In the middle of a statement it completes function names.
5884 - after a `(' or `,' in the argument list of a function or procedure,
5885 it completes a keyword of the relevant function or procedure.
5886 - In the first arg of `OBJ_NEW', it completes a class name.
5887
5888 When several completions are possible, a list will be displayed in the
5889 *Completions* buffer. If this list is too long to fit into the
5890 window, scrolling can be achieved by repeatedly pressing
5891 \\[idlwave-complete].
5892
5893 The function also knows about object methods. When it needs a class
5894 name, the action depends upon `idlwave-query-class', which see. You
5895 can force IDLWAVE to ask you for a class name with a
5896 \\[universal-argument] prefix argument to this command.
5897
5898 See also the variables `idlwave-keyword-completion-adds-equal' and
5899 `idlwave-function-completion-adds-paren'.
5900
5901 The optional ARG can be used to specify the completion type in order
5902 to override IDLWAVE's idea of what should be completed at point.
5903 Possible values are:
5904
5905 0 <=> query for the completion type
5906 1 <=> 'procedure
5907 2 <=> 'procedure-keyword
5908 3 <=> 'function
5909 4 <=> 'function-keyword
5910 5 <=> 'procedure-method
5911 6 <=> 'procedure-method-keyword
5912 7 <=> 'function-method
5913 8 <=> 'function-method-keyword
5914 9 <=> 'class
5915
5916 As a special case, the universal argument C-u forces completion of
5917 function names in places where the default would be a keyword.
5918
5919 Two prefix argument, C-u C-u, prompts for a regexp by which to limit
5920 completion.
5921
5922 For Lisp programmers only:
5923 When we force a keyword, optional argument MODULE can contain the module name.
5924 When we force a method or a method keyword, CLASS can specify the class."
5925 (interactive "P")
5926 (idlwave-routines)
5927 (let* ((where-list
5928 (if (and arg
5929 (or (and (integerp arg) (not (equal arg '(16))))
5930 (symbolp arg)))
5931 (idlwave-make-force-complete-where-list arg module class)
5932 (idlwave-where)))
5933 (what (nth 2 where-list))
5934 (idlwave-force-class-query (equal arg '(4)))
5935 (completion-regexp-list
5936 (if (equal arg '(16))
5937 (list (read-string (concat "Completion Regexp: "))))))
5938
5939 (if (and module (string-match "::" module))
5940 (setq class (substring module 0 (match-beginning 0))
5941 module (substring module (match-end 0))))
5942
5943 (cond
5944
5945 ((and (null arg)
5946 (eq (car-safe last-command) 'idlwave-display-completion-list)
5947 (get-buffer-window "*Completions*"))
5948 (setq this-command last-command)
5949 (idlwave-scroll-completions))
5950
5951 ;; Complete a filename in quotes
5952 ((and (idlwave-in-quote)
5953 (not (eq what 'class)))
5954 (idlwave-complete-filename))
5955
5956 ;; Check for any special completion functions
5957 ((and idlwave-complete-special
5958 (idlwave-call-special idlwave-complete-special)))
5959
5960 ((null what)
5961 (error "Nothing to complete here"))
5962
5963 ;; Complete a class
5964 ((eq what 'class)
5965 (setq idlwave-completion-help-info '(class))
5966 (idlwave-complete-class))
5967
5968 ((eq what 'procedure)
5969 ;; Complete a procedure name
5970 (let* ((cw-list (nth 3 where-list))
5971 (class-selector (idlwave-determine-class cw-list 'pro))
5972 (super-classes (unless (idlwave-explicit-class-listed cw-list)
5973 (idlwave-all-class-inherits class-selector)))
5974 (isa (concat "procedure" (if class-selector "-method" "")))
5975 (type-selector 'pro))
5976 (setq idlwave-completion-help-info
5977 (list 'routine nil type-selector class-selector nil super-classes))
5978 (idlwave-complete-in-buffer
5979 'procedure (if class-selector 'method 'routine)
5980 (idlwave-routines) 'idlwave-selector
5981 (format "Select a %s name%s"
5982 isa
5983 (if class-selector
5984 (format " (class is %s)"
5985 (if (eq class-selector t)
5986 "unknown" class-selector))
5987 ""))
5988 isa
5989 'idlwave-attach-method-classes 'idlwave-add-file-link-selector)))
5990
5991 ((eq what 'function)
5992 ;; Complete a function name
5993 (let* ((cw-list (nth 3 where-list))
5994 (class-selector (idlwave-determine-class cw-list 'fun))
5995 (super-classes (unless (idlwave-explicit-class-listed cw-list)
5996 (idlwave-all-class-inherits class-selector)))
5997 (isa (concat "function" (if class-selector "-method" "")))
5998 (type-selector 'fun))
5999 (setq idlwave-completion-help-info
6000 (list 'routine nil type-selector class-selector nil super-classes))
6001 (idlwave-complete-in-buffer
6002 'function (if class-selector 'method 'routine)
6003 (idlwave-routines) 'idlwave-selector
6004 (format "Select a %s name%s"
6005 isa
6006 (if class-selector
6007 (format " (class is %s)"
6008 (if (eq class-selector t)
6009 "unknown" class-selector))
6010 ""))
6011 isa
6012 'idlwave-attach-method-classes 'idlwave-add-file-link-selector)))
6013
6014 ((and (memq what '(procedure-keyword function-keyword)) ; Special Case
6015 (equal arg '(4)))
6016 (idlwave-complete 3))
6017
6018 ((eq what 'procedure-keyword)
6019 ;; Complete a procedure keyword
6020 (let* ((where (nth 3 where-list))
6021 (name (car where))
6022 (method-selector name)
6023 (type-selector 'pro)
6024 (class (idlwave-determine-class where 'pro))
6025 (class-selector class)
6026 (super-classes (idlwave-all-class-inherits class-selector))
6027 (isa (format "procedure%s-keyword" (if class "-method" "")))
6028 (entry (idlwave-best-rinfo-assq
6029 name 'pro class (idlwave-routines)))
6030 (system (if entry (eq (car (nth 3 entry)) 'system)))
6031 (list (idlwave-entry-keywords entry 'do-link)))
6032 (unless (or entry (eq class t))
6033 (error "Nothing known about procedure %s"
6034 (idlwave-make-full-name class name)))
6035 (setq list (idlwave-fix-keywords name 'pro class list
6036 super-classes system))
6037 (unless list (error "No keywords available for procedure %s"
6038 (idlwave-make-full-name class name)))
6039 (setq idlwave-completion-help-info
6040 (list 'keyword name type-selector class-selector entry super-classes))
6041 (idlwave-complete-in-buffer
6042 'keyword 'keyword list nil
6043 (format "Select keyword for procedure %s%s"
6044 (idlwave-make-full-name class name)
6045 (if (or (member '("_EXTRA") list)
6046 (member '("_REF_EXTRA") list))
6047 " (note _EXTRA)" ""))
6048 isa
6049 'idlwave-attach-keyword-classes)))
6050
6051 ((eq what 'function-keyword)
6052 ;; Complete a function keyword
6053 (let* ((where (nth 3 where-list))
6054 (name (car where))
6055 (method-selector name)
6056 (type-selector 'fun)
6057 (class (idlwave-determine-class where 'fun))
6058 (class-selector class)
6059 (super-classes (idlwave-all-class-inherits class-selector))
6060 (isa (format "function%s-keyword" (if class "-method" "")))
6061 (entry (idlwave-best-rinfo-assq
6062 name 'fun class (idlwave-routines)))
6063 (system (if entry (eq (car (nth 3 entry)) 'system)))
6064 (list (idlwave-entry-keywords entry 'do-link))
6065 msg-name)
6066 (unless (or entry (eq class t))
6067 (error "Nothing known about function %s"
6068 (idlwave-make-full-name class name)))
6069 (setq list (idlwave-fix-keywords name 'fun class list
6070 super-classes system))
6071 ;; OBJ_NEW: Messages mention the proper Init method
6072 (setq msg-name (if (and (null class)
6073 (string= (upcase name) "OBJ_NEW"))
6074 (concat idlwave-current-obj_new-class
6075 "::Init (via OBJ_NEW)")
6076 (idlwave-make-full-name class name)))
6077 (unless list (error "No keywords available for function %s"
6078 msg-name))
6079 (setq idlwave-completion-help-info
6080 (list 'keyword name type-selector class-selector nil super-classes))
6081 (idlwave-complete-in-buffer
6082 'keyword 'keyword list nil
6083 (format "Select keyword for function %s%s" msg-name
6084 (if (or (member '("_EXTRA") list)
6085 (member '("_REF_EXTRA") list))
6086 " (note _EXTRA)" ""))
6087 isa
6088 'idlwave-attach-keyword-classes)))
6089
6090 (t (error "This should not happen (idlwave-complete)")))))
6091
6092 (defvar idlwave-complete-special nil
6093 "List of special completion functions.
6094 These functions are called for each completion. Each function must
6095 check if its own special completion context is present. If yes, it
6096 should use `idlwave-complete-in-buffer' to do some completion and
6097 return t. If such a function returns t, *no further* attempts to
6098 complete other contexts will be done. If the function returns nil,
6099 other completions will be tried.")
6100
6101 (defun idlwave-call-special (functions &rest args)
6102 (let ((funcs functions)
6103 fun ret)
6104 (catch 'exit
6105 (while (setq fun (pop funcs))
6106 (if (setq ret (apply fun args))
6107 (throw 'exit ret)))
6108 nil)))
6109
6110 (defun idlwave-make-force-complete-where-list (what &optional module class)
6111 ;; Return an artificial WHERE specification to force the completion
6112 ;; routine to complete a specific item independent of context.
6113 ;; WHAT is the prefix arg of `idlwave-complete', see there for details.
6114 ;; MODULE and CLASS can be used to specify the routine name and class.
6115 ;; The class name will also be found in MODULE if that is like "class::mod".
6116 (let* ((what-list '(("procedure") ("procedure-keyword")
6117 ("function") ("function-keyword")
6118 ("procedure-method") ("procedure-method-keyword")
6119 ("function-method") ("function-method-keyword")
6120 ("class")))
6121 (module (idlwave-sintern-routine-or-method module class))
6122 (class (idlwave-sintern-class class))
6123 (what (cond
6124 ((equal what 0)
6125 (setq what
6126 (intern (completing-read
6127 "Complete what? " what-list nil t))))
6128 ((integerp what)
6129 (setq what (intern (car (nth (1- what) what-list)))))
6130 ((and what
6131 (symbolp what)
6132 (assoc (symbol-name what) what-list))
6133 what)
6134 (t (error "Invalid WHAT"))))
6135 (nil-list '(nil nil nil nil))
6136 (class-list (list nil nil (or class t) nil)))
6137
6138 (cond
6139
6140 ((eq what 'procedure)
6141 (list nil-list nil-list 'procedure nil-list nil))
6142
6143 ((eq what 'procedure-keyword)
6144 (let* ((class-selector nil)
6145 (super-classes nil)
6146 (type-selector 'pro)
6147 (pro (or module
6148 (idlwave-completing-read
6149 "Procedure: " (idlwave-routines) 'idlwave-selector))))
6150 (setq pro (idlwave-sintern-routine pro))
6151 (list nil-list nil-list 'procedure-keyword
6152 (list pro nil nil nil) nil)))
6153
6154 ((eq what 'function)
6155 (list nil-list nil-list 'function nil-list nil))
6156
6157 ((eq what 'function-keyword)
6158 (let* ((class-selector nil)
6159 (super-classes nil)
6160 (type-selector 'fun)
6161 (func (or module
6162 (idlwave-completing-read
6163 "Function: " (idlwave-routines) 'idlwave-selector))))
6164 (setq func (idlwave-sintern-routine func))
6165 (list nil-list nil-list 'function-keyword
6166 (list func nil nil nil) nil)))
6167
6168 ((eq what 'procedure-method)
6169 (list nil-list nil-list 'procedure class-list nil))
6170
6171 ((eq what 'procedure-method-keyword)
6172 (let* ((class (idlwave-determine-class class-list 'pro))
6173 (class-selector class)
6174 (super-classes (idlwave-all-class-inherits class-selector))
6175 (type-selector 'pro)
6176 (pro (or module
6177 (idlwave-completing-read
6178 (format "Procedure in %s class: " class-selector)
6179 (idlwave-routines) 'idlwave-selector))))
6180 (setq pro (idlwave-sintern-method pro))
6181 (list nil-list nil-list 'procedure-keyword
6182 (list pro nil class nil) nil)))
6183
6184 ((eq what 'function-method)
6185 (list nil-list nil-list 'function class-list nil))
6186
6187 ((eq what 'function-method-keyword)
6188 (let* ((class (idlwave-determine-class class-list 'fun))
6189 (class-selector class)
6190 (super-classes (idlwave-all-class-inherits class-selector))
6191 (type-selector 'fun)
6192 (func (or module
6193 (idlwave-completing-read
6194 (format "Function in %s class: " class-selector)
6195 (idlwave-routines) 'idlwave-selector))))
6196 (setq func (idlwave-sintern-method func))
6197 (list nil-list nil-list 'function-keyword
6198 (list func nil class nil) nil)))
6199
6200 ((eq what 'class)
6201 (list nil-list nil-list 'class nil-list nil))
6202
6203 (t (error "Invalid value for WHAT")))))
6204
6205 (defun idlwave-completing-read (&rest args)
6206 ;; Completing read, case insensitive
6207 (let ((old-value (default-value 'completion-ignore-case)))
6208 (unwind-protect
6209 (progn
6210 (setq-default completion-ignore-case t)
6211 (apply 'completing-read args))
6212 (setq-default completion-ignore-case old-value))))
6213
6214 (defvar idlwave-shell-default-directory)
6215 (defun idlwave-complete-filename ()
6216 "Use the comint stuff to complete a file name."
6217 (require 'comint)
6218 (let* ((comint-file-name-chars "~/A-Za-z0-9+@:_.$#%={}\\-")
6219 (comint-completion-addsuffix nil)
6220 (default-directory
6221 (if (and (boundp 'idlwave-shell-default-directory)
6222 (stringp idlwave-shell-default-directory)
6223 (file-directory-p idlwave-shell-default-directory))
6224 idlwave-shell-default-directory
6225 default-directory)))
6226 (comint-dynamic-complete-filename)))
6227
6228 (defun idlwave-make-full-name (class name)
6229 ;; Make a fully qualified module name including the class name
6230 (concat (if class (format "%s::" class) "") name))
6231
6232 (defun idlwave-rinfo-assoc (name type class list)
6233 "Like `idlwave-rinfo-assq', but sintern strings first."
6234 (idlwave-rinfo-assq
6235 (idlwave-sintern-routine-or-method name class)
6236 type (idlwave-sintern-class class) list))
6237
6238 (defun idlwave-rinfo-assq (name type class list)
6239 ;; Works like assq, but also checks type and class
6240 (catch 'exit
6241 (let (match)
6242 (while (setq match (assq name list))
6243 (and (or (eq type t)
6244 (eq (nth 1 match) type))
6245 (eq (nth 2 match) class)
6246 (throw 'exit match))
6247 (setq list (cdr (memq match list)))))))
6248
6249 (defun idlwave-rinfo-assq-any-class (name type class list)
6250 ;; Return the first matching method on the inheritance list
6251 (let* ((classes (cons class (idlwave-all-class-inherits class)))
6252 class rtn)
6253 (while classes
6254 (if (setq rtn (idlwave-rinfo-assq name type (pop classes) list))
6255 (setq classes nil)))
6256 rtn))
6257
6258 (defun idlwave-best-rinfo-assq (name type class list &optional with-file
6259 keep-system)
6260 "Like `idlwave-rinfo-assq', but get all twins and sort, then return first.
6261 If WITH-FILE is passed, find the best rinfo entry with a file
6262 included. If KEEP-SYSTEM is set, don't prune system for compiled
6263 syslib files."
6264 (let ((twins (idlwave-routine-twins
6265 (idlwave-rinfo-assq-any-class name type class list)
6266 list))
6267 syslibp)
6268 (when (> (length twins) 1)
6269 (setq twins (sort twins 'idlwave-routine-entry-compare-twins))
6270 (if (and (null keep-system)
6271 (eq 'system (car (nth 3 (car twins))))
6272 (setq syslibp (idlwave-any-syslib (cdr twins)))
6273 (not (equal 1 syslibp)))
6274 ;; Its a compiled syslib, so we need to remove the system entry
6275 (setq twins (cdr twins)))
6276 (if with-file
6277 (setq twins (delq nil
6278 (mapcar (lambda (x)
6279 (if (nth 1 (nth 3 x)) x))
6280 twins)))))
6281 (car twins)))
6282
6283 (defun idlwave-best-rinfo-assoc (name type class list &optional with-file
6284 keep-system)
6285 "Like `idlwave-best-rinfo-assq', but sintern strings first."
6286 (idlwave-best-rinfo-assq
6287 (idlwave-sintern-routine-or-method name class)
6288 type (idlwave-sintern-class class) list with-file keep-system))
6289
6290 (defun idlwave-any-syslib (entries)
6291 "Does the entry list ENTRIES contain a syslib entry?
6292 If yes, return the index (>=1)."
6293 (let (file (cnt 0))
6294 (catch 'exit
6295 (while entries
6296 (incf cnt)
6297 (setq file (idlwave-routine-source-file (nth 3 (car entries))))
6298 (if (and file (idlwave-syslib-p file))
6299 (throw 'exit cnt)
6300 (setq entries (cdr entries))))
6301 nil)))
6302
6303 (defun idlwave-all-assq (key list)
6304 "Return a list of all associations of Key in LIST."
6305 (let (rtn elt)
6306 (while (setq elt (assq key list))
6307 (push elt rtn)
6308 (setq list (cdr (memq elt list))))
6309 (nreverse rtn)))
6310
6311 (defun idlwave-all-method-classes (method &optional type)
6312 "Return all classes which have a method METHOD. TYPE is 'fun or 'pro.
6313 When TYPE is not specified, both procedures and functions will be considered."
6314 (if (null method)
6315 (mapcar 'car (idlwave-class-alist))
6316 (let (rtn)
6317 (mapcar (lambda (x)
6318 (and (nth 2 x)
6319 (or (not type)
6320 (eq type (nth 1 x)))
6321 (push (nth 2 x) rtn)))
6322 (idlwave-all-assq method (idlwave-routines)))
6323 (idlwave-uniquify rtn))))
6324
6325 (defun idlwave-all-method-keyword-classes (method keyword &optional type)
6326 "Return all classes which have a method METHOD with keyword KEYWORD.
6327 TYPE is 'fun or 'pro.
6328 When TYPE is not specified, both procedures and functions will be considered."
6329 (if (or (null method)
6330 (null keyword))
6331 nil
6332 (let (rtn)
6333 (mapcar (lambda (x)
6334 (and (nth 2 x) ; non-nil class
6335 (or (not type) ; correct or unspecified type
6336 (eq type (nth 1 x)))
6337 (assoc keyword (idlwave-entry-keywords x))
6338 (push (nth 2 x) rtn)))
6339 (idlwave-all-assq method (idlwave-routines)))
6340 (idlwave-uniquify rtn))))
6341
6342 (defun idlwave-members-only (list club)
6343 "Return list of all elements in LIST which are also in CLUB."
6344 (let (rtn)
6345 (while list
6346 (if (member (car list) club)
6347 (setq rtn (cons (car list) rtn)))
6348 (setq list (cdr list)))
6349 (nreverse rtn)))
6350
6351 (defun idlwave-nonmembers-only (list club)
6352 "Return list of all elements in LIST which are not in CLUB."
6353 (let (rtn)
6354 (while list
6355 (if (member (car list) club)
6356 nil
6357 (setq rtn (cons (car list) rtn)))
6358 (setq list (cdr list)))
6359 (nreverse rtn)))
6360
6361 (defun idlwave-explicit-class-listed (info)
6362 "Return whether or not the class is listed explicitly, ala a->b::c.
6363 INFO is as returned by idlwave-what-function or -procedure."
6364 (let ((apos (nth 3 info)))
6365 (if apos
6366 (save-excursion (goto-char apos)
6367 (looking-at "->[a-zA-Z][a-zA-Z0-9$_]*::")))))
6368
6369 (defvar idlwave-determine-class-special nil
6370 "List of special functions for determining class.
6371 Must accept two arguments: `apos' and `info'")
6372
6373 (defun idlwave-determine-class (info type)
6374 ;; Determine the class of a routine call.
6375 ;; INFO is the `cw-list' structure as returned by idlwave-where.
6376 ;; The second element in this structure is the class. When nil, we
6377 ;; return nil. When t, try to get the class from text properties at
6378 ;; the arrow. When the object is "self", we use the class of the
6379 ;; current routine. otherwise prompt the user for a class name.
6380 ;; Also stores the selected class as a text property at the arrow.
6381 ;; TYPE is 'fun or 'pro.
6382 (let* ((class (nth 2 info))
6383 (apos (nth 3 info))
6384 (nassoc (assoc (if (stringp (car info))
6385 (upcase (car info))
6386 (car info))
6387 idlwave-query-class))
6388 (dassoc (assq (if (car info) 'keyword-default 'method-default)
6389 idlwave-query-class))
6390 (query (cond (nassoc (cdr nassoc))
6391 (dassoc (cdr dassoc))
6392 (t t)))
6393 (arrow (and apos (string= (buffer-substring apos (+ 2 apos)) "->")))
6394 (is-self
6395 (and arrow
6396 (save-excursion (goto-char apos)
6397 (forward-word -1)
6398 (let ((case-fold-search t))
6399 (looking-at "self\\>")))))
6400 (force-query idlwave-force-class-query)
6401 store special-class class-alist)
6402 (cond
6403 ((null class) nil)
6404 ((eq t class)
6405 ;; There is an object which would like to know its class
6406 (if (and arrow (get-text-property apos 'idlwave-class)
6407 idlwave-store-inquired-class
6408 (not force-query))
6409 (setq class (get-text-property apos 'idlwave-class)
6410 class (idlwave-sintern-class class)))
6411 (if (and (eq t class) is-self)
6412 (setq class (or (nth 2 (idlwave-current-routine)) class)))
6413
6414 ;; Before prompting, try any special class determination routines
6415 (when (and (eq t class)
6416 idlwave-determine-class-special
6417 (not force-query))
6418 (setq special-class
6419 (idlwave-call-special idlwave-determine-class-special apos))
6420 (if special-class
6421 (setq class (idlwave-sintern-class special-class)
6422 store idlwave-store-inquired-class)))
6423
6424 ;; Prompt for a class, if we need to
6425 (when (and (eq class t)
6426 (or force-query query))
6427 (setq class-alist
6428 (mapcar 'list (idlwave-all-method-classes (car info) type)))
6429 (setq class
6430 (idlwave-sintern-class
6431 (cond
6432 ((and (= (length class-alist) 0) (not force-query))
6433 (error "No classes available with method %s" (car info)))
6434 ((and (= (length class-alist) 1) (not force-query))
6435 (car (car class-alist)))
6436 (t
6437 (setq store idlwave-store-inquired-class)
6438 (idlwave-completing-read
6439 (format "Class%s: " (if (stringp (car info))
6440 (format " for %s method %s"
6441 type (car info))
6442 ""))
6443 class-alist nil nil nil 'idlwave-class-history))))))
6444
6445 ;; Store it, if requested
6446 (when (and class (not (eq t class)))
6447 ;; We have a real class here
6448 (when (and store arrow)
6449 (condition-case ()
6450 (add-text-properties
6451 apos (+ apos 2)
6452 `(idlwave-class ,class face ,idlwave-class-arrow-face
6453 rear-nonsticky t))
6454 (error nil)))
6455 (setf (nth 2 info) class))
6456 ;; Return the class
6457 class)
6458 ;; Default as fallback
6459 (t class))))
6460
6461 (defvar type-selector)
6462 (defvar class-selector)
6463 (defvar method-selector)
6464 (defvar super-classes)
6465 (defun idlwave-selector (a)
6466 (and (eq (nth 1 a) type-selector)
6467 (or (and (nth 2 a) (eq class-selector t))
6468 (eq (nth 2 a) class-selector)
6469 (memq (nth 2 a) super-classes))))
6470
6471 (defun idlwave-add-file-link-selector (a)
6472 ;; Record a file link, if any, for the tested names during selection.
6473 (let ((sel (idlwave-selector a)) file)
6474 (if (and sel (setq file (idlwave-entry-has-help a)))
6475 (push (cons (car a) file) idlwave-completion-help-links))
6476 sel))
6477
6478
6479 (defun idlwave-where ()
6480 "Find out where we are.
6481 The return value is a list with the following stuff:
6482 \(PRO-LIST FUNC-LIST COMPLETE-WHAT CW-LIST LAST-CHAR)
6483
6484 PRO-LIST (PRO POINT CLASS ARROW)
6485 FUNC-LIST (FUNC POINT CLASS ARROW)
6486 COMPLETE-WHAT a symbol indicating what kind of completion makes sense here
6487 CW-LIST (PRO-OR-FUNC POINT CLASS ARROW) Like PRO-LIST, for what can
6488 be completed here.
6489 LAST-CHAR last relevant character before point (non-white non-comment,
6490 not part of current identifier or leading slash).
6491
6492 In the lists, we have these meanings:
6493 PRO: Procedure name
6494 FUNC: Function name
6495 POINT: Where is this
6496 CLASS: What class has the routine (nil=no, t=is method, but class unknown)
6497 ARROW: Location of the arrow"
6498 (idlwave-routines)
6499 (let* (;(bos (save-excursion (idlwave-beginning-of-statement) (point)))
6500 (bos (save-excursion (idlwave-start-of-substatement 'pre) (point)))
6501 (func-entry (idlwave-what-function bos))
6502 (func (car func-entry))
6503 (func-class (nth 1 func-entry))
6504 (func-arrow (nth 2 func-entry))
6505 (func-point (or (nth 3 func-entry) 0))
6506 (func-level (or (nth 4 func-entry) 0))
6507 (pro-entry (idlwave-what-procedure bos))
6508 (pro (car pro-entry))
6509 (pro-class (nth 1 pro-entry))
6510 (pro-arrow (nth 2 pro-entry))
6511 (pro-point (or (nth 3 pro-entry) 0))
6512 (last-char (idlwave-last-valid-char))
6513 (case-fold-search t)
6514 (match-string (buffer-substring bos (point)))
6515 cw cw-mod cw-arrow cw-class cw-point)
6516 (if (< func-point pro-point) (setq func nil))
6517 (cond
6518 ((string-match "\\`[ \t]*\\(pro\\|function\\)[ \t]+[a-zA-Z0-9_]*\\'"
6519 match-string)
6520 (setq cw 'class))
6521 ((string-match
6522 "\\`[ \t]*\\([a-zA-Z][a-zA-Z0-9$_]*\\)?\\'"
6523 (if (> pro-point 0)
6524 (buffer-substring pro-point (point))
6525 match-string))
6526 (setq cw 'procedure cw-class pro-class cw-point pro-point
6527 cw-arrow pro-arrow))
6528 ((string-match "\\`[ \t]*\\(pro\\|function\\)\\>"
6529 match-string)
6530 nil)
6531 ((string-match "OBJ_NEW([ \t]*['\"]\\([a-zA-Z0-9$_]*\\)?\\'"
6532 match-string)
6533 (setq cw 'class))
6534 ((string-match "\\<inherits\\s-+\\([a-zA-Z0-9$_]*\\)?\\'"
6535 match-string)
6536 (setq cw 'class))
6537 ((and func
6538 (> func-point pro-point)
6539 (= func-level 1)
6540 (memq last-char '(?\( ?,)))
6541 (setq cw 'function-keyword cw-mod func cw-point func-point
6542 cw-class func-class cw-arrow func-arrow))
6543 ((and pro (eq last-char ?,))
6544 (setq cw 'procedure-keyword cw-mod pro cw-point pro-point
6545 cw-class pro-class cw-arrow pro-arrow))
6546 ; ((member last-char '(?\' ?\) ?\] ?!))
6547 ; ;; after these chars, a function makes no sense
6548 ; ;; FIXME: I am sure there can be more in this list
6549 ; ;; FIXME: Do we want to do this at all?
6550 ; nil)
6551 ;; Everywhere else we try a function.
6552 (t
6553 (setq cw 'function)
6554 (save-excursion
6555 (if (re-search-backward "->[ \t]*\\(\\$[ \t]*\\(;.*\\)?\n\\s-*\\)?\\(\\([$a-zA-Z0-9_]+\\)::\\)?[$a-zA-Z0-9_]*\\=" bos t)
6556 (setq cw-arrow (copy-marker (match-beginning 0))
6557 cw-class (if (match-end 4)
6558 (idlwave-sintern-class (match-string 4))
6559 t))))))
6560 (list (list pro pro-point pro-class pro-arrow)
6561 (list func func-point func-class func-arrow)
6562 cw
6563 (list cw-mod cw-point cw-class cw-arrow)
6564 last-char)))
6565
6566 (defun idlwave-this-word (&optional class)
6567 ;; Grab the word around point. CLASS is for the `skip-chars=...' functions
6568 (setq class (or class "a-zA-Z0-9$_."))
6569 (save-excursion
6570 (buffer-substring
6571 (progn (skip-chars-backward class) (point))
6572 (progn (skip-chars-forward class) (point)))))
6573
6574 (defun idlwave-what-function (&optional bound)
6575 ;; Find out if point is within the argument list of a function.
6576 ;; The return value is ("function-name" class arrow-start (point) level).
6577 ;; Level is 1 on the top level parentheses, higher further down.
6578
6579 ;; If the optional BOUND is an integer, bound backwards directed
6580 ;; searches to this point.
6581
6582 (catch 'exit
6583 (let (pos
6584 func-point
6585 (cnt 0)
6586 func arrow-start class)
6587 (idlwave-with-special-syntax
6588 (save-restriction
6589 (save-excursion
6590 (narrow-to-region (max 1 (or bound 0)) (point-max))
6591 ;; move back out of the current parenthesis
6592 (while (condition-case nil
6593 (progn (up-list -1) t)
6594 (error nil))
6595 (setq pos (point))
6596 (incf cnt)
6597 (when (and (= (following-char) ?\()
6598 (re-search-backward
6599 "\\(::\\|\\<\\)\\([a-zA-Z][a-zA-Z0-9$_]*\\)[ \t]*\\="
6600 bound t))
6601 (setq func (match-string 2)
6602 func-point (goto-char (match-beginning 2))
6603 pos func-point)
6604 (if (re-search-backward
6605 "->[ \t]*\\(\\([a-zA-Z][a-zA-Z0-9$_]*\\)::\\)?\\=" bound t)
6606 (setq arrow-start (copy-marker (match-beginning 0))
6607 class (or (match-string 2) t)))
6608 (throw
6609 'exit
6610 (list
6611 (idlwave-sintern-routine-or-method func class)
6612 (idlwave-sintern-class class)
6613 arrow-start func-point cnt)))
6614 (goto-char pos))
6615 (throw 'exit nil)))))))
6616
6617 (defun idlwave-what-procedure (&optional bound)
6618 ;; Find out if point is within the argument list of a procedure.
6619 ;; The return value is ("procedure-name" class arrow-pos (point)).
6620
6621 ;; If the optional BOUND is an integer, bound backwards directed
6622 ;; searches to this point.
6623 (let ((pos (point)) pro-point
6624 pro class arrow-start string)
6625 (save-excursion
6626 ;;(idlwave-beginning-of-statement)
6627 (idlwave-start-of-substatement 'pre)
6628 (setq string (buffer-substring (point) pos))
6629 (if (string-match
6630 "\\`[ \t]*\\([a-zA-Z][a-zA-Z0-9$_]*\\)[ \t]*\\(,\\|\\'\\)" string)
6631 (setq pro (match-string 1 string)
6632 pro-point (+ (point) (match-beginning 1)))
6633 (if (and (idlwave-skip-object)
6634 (setq string (buffer-substring (point) pos))
6635 (string-match
6636 "\\`[ \t]*\\(->\\)[ \t]*\\(\\([a-zA-Z][a-zA-Z0-9$_]*\\)::\\)?\\([a-zA-Z][a-zA-Z0-9$_]*\\)?[ \t]*\\(,\\|\\(\\$\\s *\\(;.*\\)?\\)?$\\)"
6637 string))
6638 (setq pro (if (match-beginning 4)
6639 (match-string 4 string))
6640 pro-point (if (match-beginning 4)
6641 (+ (point) (match-beginning 4))
6642 pos)
6643 arrow-start (copy-marker (+ (point) (match-beginning 1)))
6644 class (or (match-string 3 string) t)))))
6645 (list (idlwave-sintern-routine-or-method pro class)
6646 (idlwave-sintern-class class)
6647 arrow-start
6648 pro-point)))
6649
6650 (defun idlwave-skip-object ()
6651 ;; If there is an object at point, move over it and return t.
6652 (let ((pos (point)))
6653 (if (catch 'exit
6654 (save-excursion
6655 (skip-chars-forward " ") ; white space
6656 (skip-chars-forward "*") ; de-reference
6657 (cond
6658 ((looking-at idlwave-identifier)
6659 (goto-char (match-end 0)))
6660 ((eq (following-char) ?\()
6661 nil)
6662 (t (throw 'exit nil)))
6663 (catch 'endwhile
6664 (while t
6665 (cond ((eq (following-char) ?.)
6666 (forward-char 1)
6667 (if (not (looking-at idlwave-identifier))
6668 (throw 'exit nil))
6669 (goto-char (match-end 0)))
6670 ((memq (following-char) '(?\( ?\[))
6671 (condition-case nil
6672 (forward-list 1)
6673 (error (throw 'exit nil))))
6674 (t (throw 'endwhile t)))))
6675 (if (looking-at "[ \t]*->")
6676 (throw 'exit (setq pos (match-beginning 0)))
6677 (throw 'exit nil))))
6678 (goto-char pos)
6679 nil)))
6680
6681 (defun idlwave-last-valid-char ()
6682 "Return the last character before point which is not white or a comment
6683 and also not part of the current identifier. Since we do this in
6684 order to identify places where keywords are, we consider the initial
6685 `/' of a keyword as part of the identifier.
6686 This function is not general, can only be used for completion stuff."
6687 (catch 'exit
6688 (save-excursion
6689 ;; skip the current identifier
6690 (skip-chars-backward "a-zA-Z0-9_$")
6691 ;; also skip a leading slash which might be belong to the keyword
6692 (if (eq (preceding-char) ?/)
6693 (backward-char 1))
6694 ;; FIXME: does not check if this is a valid identifier
6695 (while t
6696 (skip-chars-backward " \t")
6697 (cond
6698 ((memq (preceding-char) '(?\; ?\$)) (throw 'exit nil))
6699 ((eq (preceding-char) ?\n)
6700 (beginning-of-line 0)
6701 (if (looking-at "\\([^\n]*\\)\\$[ \t]*\\(;[^\n]*\\)?\n")
6702 ;; continuation line
6703 (goto-char (match-end 1))
6704 (throw 'exit nil)))
6705 (t (throw 'exit (preceding-char))))))))
6706
6707 (defvar idlwave-complete-after-success-form nil
6708 "A form to evaluate after successful completion.")
6709 (defvar idlwave-complete-after-success-form-force nil
6710 "A form to evaluate after completion selection in *Completions* buffer.")
6711 (defconst idlwave-completion-mark (make-marker)
6712 "A mark pointing to the beginning of the completion string.")
6713
6714 (defun idlwave-complete-in-buffer (type stype list selector prompt isa
6715 &optional prepare-display-function
6716 special-selector)
6717 "Perform TYPE completion of word before point against LIST.
6718 SELECTOR is the PREDICATE argument for the completion function. Show
6719 PROMPT in echo area. TYPE is one of the intern types, e.g. 'function,
6720 'procedure, 'class-tag, 'keyword, 'sysvar, etc.. SPECIAL-SELECTOR is
6721 used only once, for `all-completions', and can be used to, e.g.,
6722 accumulate information on matching completions."
6723 (let* ((completion-ignore-case t)
6724 beg (end (point)) slash part spart completion all-completions
6725 dpart dcompletion)
6726
6727 (unless list
6728 (error (concat prompt ": No completions available")))
6729
6730 ;; What is already in the buffer?
6731 (save-excursion
6732 (skip-chars-backward "a-zA-Z0-9_$")
6733 (setq slash (eq (preceding-char) ?/)
6734 beg (point)
6735 idlwave-complete-after-success-form
6736 (list 'idlwave-after-successful-completion
6737 (list 'quote type) slash beg)
6738 idlwave-complete-after-success-form-force
6739 (list 'idlwave-after-successful-completion
6740 (list 'quote type) slash (list 'quote 'force))))
6741
6742 ;; Try a completion
6743 (setq part (buffer-substring beg end)
6744 dpart (downcase part)
6745 spart (idlwave-sintern stype part)
6746 completion (try-completion part list selector)
6747 dcompletion (if (stringp completion) (downcase completion))
6748 idlwave-completion-help-links nil)
6749 (cond
6750 ((null completion)
6751 ;; nothing available.
6752 (error (concat prompt ": no completion for \"%s\"") part))
6753 ((and (not (equal dpart dcompletion))
6754 (not (eq t completion)))
6755 ;; We can add something
6756 (delete-region beg end)
6757 (if (and (string= part dpart)
6758 (or (not (string= part ""))
6759 idlwave-complete-empty-string-as-lower-case)
6760 (not idlwave-completion-force-default-case))
6761 (insert dcompletion)
6762 (insert completion))
6763 (if (eq t (try-completion completion list selector))
6764 ;; Now this is a unique match
6765 (idlwave-after-successful-completion type slash beg))
6766 t)
6767 ((or (eq completion t)
6768 (and (= 1 (length (setq all-completions
6769 (idlwave-uniquify
6770 (all-completions part list
6771 (or special-selector
6772 selector))))))
6773 (equal dpart dcompletion)))
6774 ;; This is already complete
6775 (idlwave-after-successful-completion type slash beg)
6776 (message "%s is already the complete %s" part isa)
6777 nil)
6778 (t
6779 ;; We cannot add something - offer a list.
6780 (message "Making completion list...")
6781
6782 (unless idlwave-completion-help-links ; already set somewhere?
6783 (mapcar (lambda (x) ; Pass link prop through to highlight-linked
6784 (let ((link (get-text-property 0 'link (car x))))
6785 (if link
6786 (push (cons (car x) link)
6787 idlwave-completion-help-links))))
6788 list))
6789 (let* ((list all-completions)
6790 ;; "complete" means, this is already a valid completion
6791 (complete (memq spart all-completions))
6792 (completion-highlight-first-word-only t)) ; XEmacs
6793 ; (completion-fixup-function ; Emacs
6794 ; (lambda () (and (eq (preceding-char) ?>)
6795 ; (re-search-backward " <" beg t)))))
6796
6797 (setq list (sort list (lambda (a b)
6798 (string< (downcase a) (downcase b)))))
6799 (if prepare-display-function
6800 (setq list (funcall prepare-display-function list)))
6801 (if (and (string= part dpart)
6802 (or (not (string= part ""))
6803 idlwave-complete-empty-string-as-lower-case)
6804 (not idlwave-completion-force-default-case))
6805 (setq list (mapcar (lambda (x)
6806 (if (listp x)
6807 (setcar x (downcase (car x)))
6808 (setq x (downcase x)))
6809 x)
6810 list)))
6811 (idlwave-display-completion-list list prompt beg complete))
6812 t))))
6813
6814 (defun idlwave-complete-class ()
6815 "Complete a class at point."
6816 (interactive)
6817 ;; Call `idlwave-routines' to make sure the class list will be available
6818 (idlwave-routines)
6819 ;; Check for the special case of completing empty string after pro/function
6820 (if (let ((case-fold-search t))
6821 (save-excursion
6822 (and
6823 (re-search-backward "\\<\\(pro\\|function\\)[ \t]+\\="
6824 (- (point) 15) t)
6825 (goto-char (point-min))
6826 (re-search-forward
6827 "^[ \t]*\\(pro\\|function\\)[ \t]+\\([a-zA-Z0-9_]+::\\)" nil t))))
6828 ;; Yank the full class specification
6829 (insert (match-string 2))
6830 ;; Do the completion, using list gathered from `idlwave-routines'
6831 (idlwave-complete-in-buffer
6832 'class 'class (idlwave-class-alist) nil
6833 "Select a class" "class"
6834 '(lambda (list) ;; Push it to help-links if system help available
6835 (mapcar (lambda (x)
6836 (let* ((entry (idlwave-class-info x))
6837 (link (nth 1 (assq 'link entry))))
6838 (if link (push (cons x link)
6839 idlwave-completion-help-links))
6840 x))
6841 list)))))
6842
6843 (defun idlwave-attach-classes (list type show-classes)
6844 ;; Attach the proper class list to a LIST of completion items.
6845 ;; TYPE, when 'kwd, shows classes for method keywords, when
6846 ;; 'class-tag, for class tags, and otherwise for methods.
6847 ;; SHOW-CLASSES is the value of `idlwave-completion-show-classes'.
6848 (if (or (null show-classes) ; don't want to see classes
6849 (null class-selector) ; not a method call
6850 (and
6851 (stringp class-selector) ; the class is already known
6852 (not super-classes))) ; no possibilities for inheritance
6853 ;; In these cases, we do not have to do anything
6854 list
6855 (let* ((do-prop (and (>= show-classes 0)
6856 (>= emacs-major-version 21)))
6857 (do-buf (not (= show-classes 0)))
6858 ;; (do-dots (featurep 'xemacs))
6859 (do-dots t)
6860 (inherit (if (and (not (eq type 'class-tag)) super-classes)
6861 (cons class-selector super-classes)))
6862 (max (abs show-classes))
6863 (lmax (if do-dots (apply 'max (mapcar 'length list))))
6864 classes nclasses class-info space)
6865 (mapcar
6866 (lambda (x)
6867 ;; get the classes
6868 (if (eq type 'class-tag)
6869 ;; Just one class for tags
6870 (setq classes
6871 (list
6872 (idlwave-class-or-superclass-with-tag class-selector x)))
6873 ;; Multiple classes for method or method-keyword
6874 (setq classes
6875 (if (eq type 'kwd)
6876 (idlwave-all-method-keyword-classes
6877 method-selector x type-selector)
6878 (idlwave-all-method-classes x type-selector)))
6879 (if inherit
6880 (setq classes
6881 (delq nil
6882 (mapcar (lambda (x) (if (memq x inherit) x nil))
6883 classes)))))
6884 (setq nclasses (length classes))
6885 ;; Make the separator between item and class-info
6886 (if do-dots
6887 (setq space (concat " " (make-string (- lmax (length x)) ?.)))
6888 (setq space " "))
6889 (if do-buf
6890 ;; We do want info in the buffer
6891 (if (<= nclasses max)
6892 (setq class-info (concat
6893 space
6894 "<" (mapconcat 'identity classes ",") ">"))
6895 (setq class-info (format "%s<%d classes>" space nclasses)))
6896 (setq class-info nil))
6897 (when do-prop
6898 ;; We do want properties
6899 (setq x (copy-sequence x))
6900 (put-text-property 0 (length x)
6901 'help-echo (mapconcat 'identity classes " ")
6902 x))
6903 (if class-info
6904 (list x class-info)
6905 x))
6906 list))))
6907
6908 (defun idlwave-attach-method-classes (list)
6909 ;; Call idlwave-attach-classes with method parameters
6910 (idlwave-attach-classes list 'method idlwave-completion-show-classes))
6911 (defun idlwave-attach-keyword-classes (list)
6912 ;; Call idlwave-attach-classes with keyword parameters
6913 (idlwave-attach-classes list 'kwd idlwave-completion-show-classes))
6914 (defun idlwave-attach-class-tag-classes (list)
6915 ;; Call idlwave-attach-classes with class structure tags
6916 (idlwave-attach-classes list 'class-tag idlwave-completion-show-classes))
6917
6918
6919 ;;----------------------------------------------------------------------
6920 ;;----------------------------------------------------------------------
6921 ;;----------------------------------------------------------------------
6922 ;;----------------------------------------------------------------------
6923 ;;----------------------------------------------------------------------
6924 (defvar rtn)
6925 (defun idlwave-pset (item)
6926 (set 'rtn item))
6927
6928 (defun idlwave-popup-select (ev list title &optional sort)
6929 "Select an item in LIST with a popup menu.
6930 TITLE is the title to put atop the popup. If SORT is non-nil,
6931 sort the list before displaying"
6932 (let ((maxpopup idlwave-max-popup-menu-items)
6933 rtn menu resp)
6934 (cond ((null list))
6935 ((= 1 (length list))
6936 (setq rtn (car list)))
6937 ((featurep 'xemacs)
6938 (if sort (setq list (sort list (lambda (a b)
6939 (string< (upcase a) (upcase b))))))
6940 (setq menu
6941 (append (list title)
6942 (mapcar (lambda (x) (vector x (list 'idlwave-pset
6943 x)))
6944 list)))
6945 (setq menu (idlwave-split-menu-xemacs menu maxpopup))
6946 (setq resp (get-popup-menu-response menu))
6947 (funcall (event-function resp) (event-object resp)))
6948 (t
6949 (if sort (setq list (sort list (lambda (a b)
6950 (string< (upcase a) (upcase b))))))
6951 (setq menu (cons title
6952 (list
6953 (append (list "")
6954 (mapcar (lambda(x) (cons x x)) list)))))
6955 (setq menu (idlwave-split-menu-emacs menu maxpopup))
6956 (setq rtn (x-popup-menu ev menu))))
6957 rtn))
6958
6959 (defun idlwave-split-menu-xemacs (menu N)
6960 "Split the MENU into submenus of maximum length N."
6961 (if (<= (length menu) (1+ N))
6962 ;; No splitting needed
6963 menu
6964 (let* ((title (car menu))
6965 (entries (cdr menu))
6966 (menu (list title))
6967 (cnt 0)
6968 (nextmenu nil))
6969 (while entries
6970 (while (and entries (< cnt N))
6971 (setq cnt (1+ cnt)
6972 nextmenu (cons (car entries) nextmenu)
6973 entries (cdr entries)))
6974 (setq nextmenu (nreverse nextmenu))
6975 (setq nextmenu (cons (format "%s...%s"
6976 (aref (car nextmenu) 0)
6977 (aref (nth (1- cnt) nextmenu) 0))
6978 nextmenu))
6979 (setq menu (cons nextmenu menu)
6980 nextmenu nil
6981 cnt 0))
6982 (nreverse menu))))
6983
6984 (defun idlwave-split-menu-emacs (menu N)
6985 "Split the MENU into submenus of maximum length N."
6986 (if (<= (length (nth 1 menu)) (1+ N))
6987 ;; No splitting needed
6988 menu
6989 (let* ((title (car menu))
6990 (entries (cdr (nth 1 menu)))
6991 (menu nil)
6992 (cnt 0)
6993 (nextmenu nil))
6994 (while entries
6995 (while (and entries (< cnt N))
6996 (setq cnt (1+ cnt)
6997 nextmenu (cons (car entries) nextmenu)
6998 entries (cdr entries)))
6999 (setq nextmenu (nreverse nextmenu))
7000 (prin1 nextmenu)
7001 (setq nextmenu (cons (format "%s...%s"
7002 (car (car nextmenu))
7003 (car (nth (1- cnt) nextmenu)))
7004 nextmenu))
7005 (setq menu (cons nextmenu menu)
7006 nextmenu nil
7007 cnt 0))
7008 (setq menu (nreverse menu))
7009 (setq menu (cons title menu))
7010 menu)))
7011
7012 (defvar idlwave-completion-setup-hook nil)
7013
7014 (defun idlwave-scroll-completions (&optional message)
7015 "Scroll the completion window on this frame."
7016 (let ((cwin (get-buffer-window "*Completions*" 'visible))
7017 (win (selected-window)))
7018 (unwind-protect
7019 (progn
7020 (select-window cwin)
7021 (condition-case nil
7022 (scroll-up)
7023 (error (if (and (listp last-command)
7024 (nth 2 last-command))
7025 (progn
7026 (select-window win)
7027 (eval idlwave-complete-after-success-form))
7028 (set-window-start cwin (point-min)))))
7029 (and message (message message)))
7030 (select-window win))))
7031
7032 (defun idlwave-display-completion-list (list &optional message beg complete)
7033 "Display the completions in LIST in the completions buffer and echo MESSAGE."
7034 (unless (and (get-buffer-window "*Completions*")
7035 (idlwave-local-value 'idlwave-completion-p "*Completions*"))
7036 (move-marker idlwave-completion-mark beg)
7037 (setq idlwave-before-completion-wconf (current-window-configuration)))
7038
7039 (if (featurep 'xemacs)
7040 (idlwave-display-completion-list-xemacs
7041 list)
7042 (idlwave-display-completion-list-emacs list))
7043
7044 ;; Store a special value in `this-command'. When `idlwave-complete'
7045 ;; finds this in `last-command', it will scroll the *Completions* buffer.
7046 (setq this-command (list 'idlwave-display-completion-list message complete))
7047
7048 ;; Mark the completions buffer as created by cib
7049 (idlwave-set-local 'idlwave-completion-p t "*Completions*")
7050
7051 ;; Fontify the classes
7052 (if (and idlwave-completion-fontify-classes
7053 (consp (car list)))
7054 (idlwave-completion-fontify-classes))
7055
7056 ;; Run the hook
7057 (run-hooks 'idlwave-completion-setup-hook)
7058
7059 ;; Display the message
7060 (message (or message "Making completion list...done")))
7061
7062 (defun idlwave-choose (function &rest args)
7063 "Call FUNCTION as a completion chooser and pass ARGS to it."
7064 (let ((completion-ignore-case t)) ; install correct value
7065 (apply function args))
7066 (if (and (eq major-mode 'idlwave-shell-mode)
7067 (boundp 'font-lock-mode)
7068 (not font-lock-mode))
7069 ;; For the shell, remove the fontification of the word before point
7070 (let ((beg (save-excursion
7071 (skip-chars-backward "a-zA-Z0-9_")
7072 (point))))
7073 (remove-text-properties beg (point) '(face nil))))
7074 (eval idlwave-complete-after-success-form-force))
7075
7076 (defun idlwave-keyboard-quit ()
7077 (interactive)
7078 (unwind-protect
7079 (if (eq (car-safe last-command) 'idlwave-display-completion-list)
7080 (idlwave-restore-wconf-after-completion))
7081 (keyboard-quit)))
7082
7083 (defun idlwave-restore-wconf-after-completion ()
7084 "Restore the old (before completion) window configuration."
7085 (and idlwave-completion-restore-window-configuration
7086 idlwave-before-completion-wconf
7087 (set-window-configuration idlwave-before-completion-wconf)))
7088
7089 (defun idlwave-one-key-select (sym prompt delay)
7090 "Make the user select an element from the alist in the variable SYM.
7091 The keys of the alist are expected to be strings. The function returns the
7092 car of the selected association.
7093 To do this, PROMPT is displayed and and the user must hit a letter key to
7094 select an entry. If the user does not reply within DELAY seconds, a help
7095 window with the options is displayed automatically.
7096 The key which is associated with each option is generated automatically.
7097 First, the strings are checked for preselected keys, like in \"[P]rint\".
7098 If these don't exist, a letter in the string is automatically selected."
7099 (let* ((alist (symbol-value sym))
7100 (temp-buffer-show-hook (if (fboundp 'fit-window-to-buffer)
7101 '(fit-window-to-buffer)))
7102 keys-alist char)
7103 ;; First check the cache
7104 (if (and (eq (symbol-value sym) (get sym :one-key-alist-last)))
7105 (setq keys-alist (get sym :one-key-alist-cache))
7106 ;; Need to make new list
7107 (setq keys-alist (idlwave-make-one-key-alist alist))
7108 (put sym :one-key-alist-cache keys-alist)
7109 (put sym :one-key-alist-last alist))
7110 ;; Display prompt and wait for quick reply
7111 (message "%s[%s]" prompt
7112 (mapconcat (lambda(x) (char-to-string (car x)))
7113 keys-alist ""))
7114 (if (sit-for delay)
7115 ;; No quick reply: Show help
7116 (save-window-excursion
7117 (with-output-to-temp-buffer "*Completions*"
7118 (mapcar (lambda(x)
7119 (princ (nth 1 x))
7120 (princ "\n"))
7121 keys-alist))
7122 (setq char (read-char)))
7123 (setq char (read-char)))
7124 (message nil)
7125 ;; Return the selected result
7126 (nth 2 (assoc char keys-alist))))
7127
7128 ;; Used for, e.g., electric debug super-examine.
7129 (defun idlwave-make-one-key-alist (alist)
7130 "Make an alist for single key selection."
7131 (let ((l alist) keys-alist name start char help
7132 (cnt 0)
7133 (case-fold-search nil))
7134 (while l
7135 (setq name (car (car l))
7136 l (cdr l))
7137 (catch 'exit
7138 ;; First check if the configuration predetermined a key
7139 (if (string-match "\\[\\(.\\)\\]" name)
7140 (progn
7141 (setq char (string-to-char (downcase (match-string 1 name)))
7142 help (format "%c: %s" char name)
7143 keys-alist (cons (list char help name) keys-alist))
7144 (throw 'exit t)))
7145 ;; Then check for capital letters
7146 (setq start 0)
7147 (while (string-match "[A-Z]" name start)
7148 (setq start (match-end 0)
7149 char (string-to-char (downcase (match-string 0 name))))
7150 (if (not (assoc char keys-alist))
7151 (progn
7152 (setq help (format "%c: %s" char
7153 (replace-match
7154 (concat "[" (match-string 0 name) "]")
7155 t t name))
7156 keys-alist (cons (list char help name) keys-alist))
7157 (throw 'exit t))))
7158 ;; Now check for lowercase letters
7159 (setq start 0)
7160 (while (string-match "[a-z]" name start)
7161 (setq start (match-end 0)
7162 char (string-to-char (match-string 0 name)))
7163 (if (not (assoc char keys-alist))
7164 (progn
7165 (setq help (format "%c: %s" char
7166 (replace-match
7167 (concat "[" (match-string 0 name) "]")
7168 t t name))
7169 keys-alist (cons (list char help name) keys-alist))
7170 (throw 'exit t))))
7171 ;; Bummer, nothing found! Use a stupid number
7172 (setq char (string-to-char (int-to-string (setq cnt (1+ cnt))))
7173 help (format "%c: %s" char name)
7174 keys-alist (cons (list char help name) keys-alist))))
7175 (nreverse keys-alist)))
7176
7177 (defun idlwave-set-local (var value &optional buffer)
7178 "Set the buffer-local value of VAR in BUFFER to VALUE."
7179 (save-excursion
7180 (set-buffer (or buffer (current-buffer)))
7181 (set (make-local-variable var) value)))
7182
7183 (defun idlwave-local-value (var &optional buffer)
7184 "Return the value of VAR in BUFFER, but only if VAR is local to BUFFER."
7185 (save-excursion
7186 (set-buffer (or buffer (current-buffer)))
7187 (and (local-variable-p var (current-buffer))
7188 (symbol-value var))))
7189
7190 ;; In XEmacs, we can use :activate-callback directly to advice the
7191 ;; choose functions. We use the private keymap only for the online
7192 ;; help feature.
7193
7194 (defvar idlwave-completion-map nil
7195 "Keymap for completion-list-mode with idlwave-complete.")
7196
7197 (defun idlwave-display-completion-list-xemacs (list &rest cl-args)
7198 (with-output-to-temp-buffer "*Completions*"
7199 (apply 'display-completion-list list
7200 ':activate-callback 'idlwave-default-choose-completion
7201 cl-args))
7202 (save-excursion
7203 (set-buffer "*Completions*")
7204 (use-local-map
7205 (or idlwave-completion-map
7206 (setq idlwave-completion-map
7207 (idlwave-make-modified-completion-map-xemacs
7208 (current-local-map)))))))
7209
7210 (defun idlwave-default-choose-completion (&rest args)
7211 "Execute `default-choose-completion' and then restore the win-conf."
7212 (apply 'idlwave-choose 'default-choose-completion args))
7213
7214 (defun idlwave-make-modified-completion-map-xemacs (old-map)
7215 "Replace `choose-completion' and `mouse-choose-completion' in OLD-MAP."
7216 (let ((new-map (copy-keymap old-map)))
7217 (define-key new-map [button3up] 'idlwave-mouse-completion-help)
7218 (define-key new-map [button3] (lambda ()
7219 (interactive)
7220 (setq this-command last-command)))
7221 new-map))
7222
7223 ;; In Emacs we also replace keybindings in the completion
7224 ;; map in order to install our wrappers.
7225
7226 (defun idlwave-display-completion-list-emacs (list)
7227 "Display completion list and install the choose wrappers."
7228 (with-output-to-temp-buffer "*Completions*"
7229 (display-completion-list list))
7230 (save-excursion
7231 (set-buffer "*Completions*")
7232 (use-local-map
7233 (or idlwave-completion-map
7234 (setq idlwave-completion-map
7235 (idlwave-make-modified-completion-map-emacs
7236 (current-local-map)))))))
7237
7238 (defun idlwave-make-modified-completion-map-emacs (old-map)
7239 "Replace `choose-completion' and `mouse-choose-completion' in OLD-MAP."
7240 (let ((new-map (copy-keymap old-map)))
7241 (substitute-key-definition
7242 'choose-completion 'idlwave-choose-completion new-map)
7243 (substitute-key-definition
7244 'mouse-choose-completion 'idlwave-mouse-choose-completion new-map)
7245 (define-key new-map [mouse-3] 'idlwave-mouse-completion-help)
7246 new-map))
7247
7248 (defun idlwave-choose-completion (&rest args)
7249 "Choose the completion that point is in or next to."
7250 (interactive)
7251 (apply 'idlwave-choose 'choose-completion args))
7252
7253 (defun idlwave-mouse-choose-completion (&rest args)
7254 "Click on an alternative in the `*Completions*' buffer to choose it."
7255 (interactive "e")
7256 (apply 'idlwave-choose 'mouse-choose-completion args))
7257
7258 ;;----------------------------------------------------------------------
7259 ;;----------------------------------------------------------------------
7260
7261 ;;; ------------------------------------------------------------------------
7262 ;;; Stucture parsing code, and code to manage class info
7263
7264 ;;
7265 ;; - Go again over the documentation how to write a completion
7266 ;; plugin. It is in self.el, but currently still very bad.
7267 ;; This could be in a separate file in the distribution, or
7268 ;; in an appendix for the manual.
7269
7270 (defvar idlwave-struct-skip
7271 "[ \t]*\\(\\$.*\n\\(^[ \t]*\\(\\$[ \t]*\\)?\\(;.*\\)?\n\\)*\\)?[ \t]*"
7272 "Regexp for skipping continued blank or comment-only lines in
7273 structures")
7274
7275 (defvar idlwave-struct-tag-regexp
7276 (concat "[{,]" ;leading comma/brace
7277 idlwave-struct-skip ; 4 groups
7278 "\\([a-zA-Z][a-zA-Z0-9_]*\\)" ;the tag itself, group 5
7279 "[ \t]*:") ; the final colon
7280 "Regexp for structure tags.")
7281
7282 (defun idlwave-struct-tags ()
7283 "Return a list of all tags in the structure defined at point.
7284 Point is expected just before the opening `{' of the struct definition."
7285 (save-excursion
7286 (let* ((borders (idlwave-struct-borders))
7287 (beg (car borders))
7288 (end (cdr borders))
7289 tags)
7290 (goto-char beg)
7291 (save-restriction
7292 (narrow-to-region beg end)
7293 (while (re-search-forward idlwave-struct-tag-regexp end t)
7294 ;; Check if we are still on the top level of the structure.
7295 (if (and (condition-case nil (progn (up-list -1) t) (error nil))
7296 (= (point) beg))
7297 (push (match-string-no-properties 5) tags))
7298 (goto-char (match-end 0))))
7299 (nreverse tags))))
7300
7301 (defun idlwave-find-struct-tag (tag)
7302 "Find a given TAG in the structure defined at point."
7303 (let* ((borders (idlwave-struct-borders))
7304 (beg (car borders))
7305 (end (cdr borders))
7306 (case-fold-search t))
7307 (re-search-forward (concat "\\(^[ \t]*\\|[,{][ \t]*\\)" tag "[ \t]*:")
7308 end t)))
7309
7310 (defun idlwave-struct-inherits ()
7311 "Return a list of all `inherits' names in the struct at point.
7312 Point is expected just before the opening `{' of the struct definition."
7313 (save-excursion
7314 (let* ((borders (idlwave-struct-borders))
7315 (beg (car borders))
7316 (end (cdr borders))
7317 (case-fold-search t)
7318 names)
7319 (goto-char beg)
7320 (save-restriction
7321 (narrow-to-region beg end)
7322 (while (re-search-forward
7323 (concat "[{,]" ;leading comma/brace
7324 idlwave-struct-skip ; 4 groups
7325 "inherits" ; The INHERITS tag
7326 idlwave-struct-skip ; 4 more
7327 "\\([a-zA-Z][a-zA-Z0-9_]*\\)") ; The super-group, #9
7328 end t)
7329 ;; Check if we are still on the top level of the structure.
7330 (if (and (condition-case nil (progn (up-list -1) t) (error nil))
7331 (= (point) beg))
7332 (push (match-string-no-properties 9) names))
7333 (goto-char (match-end 0))))
7334 (nreverse names))))
7335
7336 (defun idlwave-in-structure ()
7337 "Return t if point is inside an IDL structure definition."
7338 (let ((beg (point)))
7339 (save-excursion
7340 (if (not (or (idlwave-in-comment) (idlwave-in-quote)))
7341 (if (idlwave-find-structure-definition nil nil 'back)
7342 (let ((borders (idlwave-struct-borders)))
7343 (or (= (car borders) (cdr borders)) ;; struct not yet closed...
7344 (and (> beg (car borders)) (< beg (cdr borders))))))))))
7345
7346 (defun idlwave-struct-borders ()
7347 "Return the borders of the {...} after point as a cons cell."
7348 (let (beg)
7349 (save-excursion
7350 (skip-chars-forward "^{")
7351 (setq beg (point))
7352 (condition-case nil (forward-list 1)
7353 (error (goto-char beg)))
7354 (cons beg (point)))))
7355
7356 (defun idlwave-find-structure-definition (&optional var name bound)
7357 "Search forward for a structure definition. If VAR is non-nil,
7358 search for a structure assigned to variable VAR. If NAME is non-nil,
7359 search for a named structure NAME, if a string, or a generic named
7360 structure otherwise. If BOUND is an integer, limit the search. If
7361 BOUND is the symbol `all', we search first back and then forward
7362 through the entire file. If BOUND is the symbol `back' we search only
7363 backward."
7364 (let* ((ws "[ \t]*\\(\\$.*\n[ \t]*\\)*")
7365 (case-fold-search t)
7366 (lim (if (integerp bound) bound nil))
7367 (re (concat
7368 (if var
7369 (concat "\\<" (regexp-quote (downcase var)) "\\>" ws)
7370 "\\(\\)")
7371 "=" ws "\\({\\)"
7372 (if name
7373 (if (stringp name)
7374 (concat ws "\\(\\<" (downcase name) "\\)[^a-zA-Z0-9_$]")
7375 ;; Just a generic name
7376 (concat ws "\\<\\([a-zA-Z_0-9$]+\\)" ws ","))
7377 ""))))
7378 (if (or (and (or (eq bound 'all) (eq bound 'back))
7379 (re-search-backward re nil t))
7380 (and (not (eq bound 'back)) (re-search-forward re lim t)))
7381 (progn
7382 (goto-char (match-beginning 3))
7383 (match-string-no-properties 5)))))
7384
7385 (defvar idlwave-class-info nil)
7386 (defvar idlwave-class-reset nil) ; to reset buffer-local classes
7387
7388 (add-hook 'idlwave-update-rinfo-hook
7389 (lambda () (setq idlwave-class-reset t)))
7390 (add-hook 'idlwave-after-load-rinfo-hook
7391 (lambda () (setq idlwave-class-info nil)))
7392
7393 (defun idlwave-class-info (class)
7394 (let (list entry)
7395 (if idlwave-class-info
7396 (if idlwave-class-reset
7397 (setq
7398 idlwave-class-reset nil
7399 idlwave-class-info ; Remove any visited in a buffer
7400 (delq nil (mapcar
7401 (lambda (x)
7402 (let ((filebuf
7403 (idlwave-class-file-or-buffer
7404 (or (cdr (assq 'found-in x)) (car x)))))
7405 (if (cdr filebuf)
7406 nil
7407 x)))
7408 idlwave-class-info))))
7409 ;; Info is nil, put in the system stuff to start.
7410 (setq idlwave-class-info idlwave-system-class-info)
7411 (setq list idlwave-class-info)
7412 (while (setq entry (pop list))
7413 (idlwave-sintern-class-info entry)))
7414 (setq class (idlwave-sintern-class class))
7415 (or (assq class idlwave-class-info)
7416 (progn (idlwave-scan-class-info class)
7417 (assq class idlwave-class-info)))))
7418
7419 (defun idlwave-sintern-class-info (entry)
7420 "Sintern the class names in a class-info entry."
7421 (let ((taglist (assq 'tags entry))
7422 (inherits (assq 'inherits entry)))
7423 (setcar entry (idlwave-sintern-class (car entry) 'set))
7424 (if inherits
7425 (setcdr inherits (mapcar (lambda (x) (idlwave-sintern-class x 'set))
7426 (cdr inherits))))))
7427
7428 (defun idlwave-find-class-definition (class &optional all-hook alt-class)
7429 "Find class structure definition(s)
7430 If ALL-HOOK is set, find all named structure definitions in a given
7431 class__define routine, on which ALL-HOOK will be run. If ALT-CLASS is
7432 set, look for the name__define pro, and inside of it, for the ALT-CLASS
7433 class/struct definition"
7434 (let ((case-fold-search t) end-lim list name)
7435 (when (re-search-forward
7436 (concat "^[ \t]*pro[ \t]+" (downcase class) "__define" "\\>") nil t)
7437 (if all-hook
7438 (progn
7439 ;; For everything there
7440 (setq end-lim (save-excursion (idlwave-end-of-subprogram) (point)))
7441 (while (setq name
7442 (idlwave-find-structure-definition nil t end-lim))
7443 (funcall all-hook name)))
7444 (idlwave-find-structure-definition nil (or alt-class class))))))
7445
7446
7447 (defun idlwave-class-file-or-buffer (class)
7448 "Find buffer visiting CLASS definition"
7449 (let* ((pro (concat (downcase class) "__define"))
7450 (file (idlwave-routine-source-file
7451 (nth 3 (idlwave-rinfo-assoc pro 'pro nil
7452 (idlwave-routines))))))
7453 (cons file (if file (idlwave-get-buffer-visiting file)))))
7454
7455
7456 (defun idlwave-scan-class-info (class)
7457 "Scan all class and named structure info in the class__define pro"
7458 (let* ((idlwave-auto-routine-info-updates nil)
7459 (filebuf (idlwave-class-file-or-buffer class))
7460 (file (car filebuf))
7461 (buf (cdr filebuf))
7462 (class (idlwave-sintern-class class)))
7463 (if (or
7464 (not file)
7465 (and ;; neither a regular file nor a visited buffer
7466 (not buf)
7467 (not (file-regular-p file))))
7468 nil ; Cannot find the file/buffer to get any info
7469 (save-excursion
7470 (if buf (set-buffer buf)
7471 ;; Read the file in temporarily
7472 (set-buffer (get-buffer-create " *IDLWAVE-tmp*"))
7473 (erase-buffer)
7474 (unless (eq major-mode 'idlwave-mode)
7475 (idlwave-mode))
7476 (insert-file-contents file))
7477 (save-excursion
7478 (goto-char 1)
7479 (idlwave-find-class-definition class
7480 ;; Scan all of the structures found there
7481 (lambda (name)
7482 (let* ((this-class (idlwave-sintern-class name))
7483 (entry
7484 (list this-class
7485 (cons 'tags (idlwave-struct-tags))
7486 (cons 'inherits (idlwave-struct-inherits)))))
7487 (if (not (eq this-class class))
7488 (setq entry (nconc entry (list (cons 'found-in class)))))
7489 (idlwave-sintern-class-info entry)
7490 (push entry idlwave-class-info)))))))))
7491
7492 (defun idlwave-class-found-in (class)
7493 "Return the FOUND-IN property of the class."
7494 (cdr (assq 'found-in (idlwave-class-info class))))
7495 (defun idlwave-class-tags (class)
7496 "Return the native tags in CLASS."
7497 (cdr (assq 'tags (idlwave-class-info class))))
7498 (defun idlwave-class-inherits (class)
7499 "Return the direct superclasses of CLASS."
7500 (cdr (assq 'inherits (idlwave-class-info class))))
7501
7502
7503 (defun idlwave-all-class-tags (class)
7504 "Return a list of native and inherited tags in CLASS."
7505 (condition-case err
7506 (apply 'append (mapcar 'idlwave-class-tags
7507 (cons class (idlwave-all-class-inherits class))))
7508 (error
7509 (idlwave-class-tag-reset)
7510 (error "%s" (error-message-string err)))))
7511
7512
7513 (defun idlwave-all-class-inherits (class)
7514 "Return a list of all superclasses of CLASS (recursively expanded).
7515 The list is cached in `idlwave-class-info' for faster access."
7516 (cond
7517 ((not idlwave-support-inheritance) nil)
7518 ((eq class nil) nil)
7519 ((eq class t) nil)
7520 (t
7521 (let ((info (idlwave-class-info class))
7522 entry)
7523 (if (setq entry (assq 'all-inherits info))
7524 (cdr entry)
7525 ;; Save the depth of inheritance scan to check for circular references
7526 (let ((inherits (mapcar (lambda (x) (cons x 0))
7527 (idlwave-class-inherits class)))
7528 rtn all-inherits cl)
7529 (while inherits
7530 (setq cl (pop inherits)
7531 rtn (cons (car cl) rtn)
7532 inherits (append (mapcar (lambda (x)
7533 (cons x (1+ (cdr cl))))
7534 (idlwave-class-inherits (car cl)))
7535 inherits))
7536 (if (> (cdr cl) 999)
7537 (error
7538 "Class scan: inheritance depth exceeded. Circular inheritance?")
7539 ))
7540 (setq all-inherits (nreverse rtn))
7541 (nconc info (list (cons 'all-inherits all-inherits)))
7542 all-inherits))))))
7543
7544 (defun idlwave-entry-keywords (entry &optional record-link)
7545 "Return the flat entry keywords alist from routine-info entry.
7546 If RECORD-LINK is non-nil, the keyword text is copied and a text
7547 property indicating the link is added."
7548 (let (kwds)
7549 (mapcar
7550 (lambda (key-list)
7551 (let ((file (car key-list)))
7552 (mapcar (lambda (key-cons)
7553 (let ((key (car key-cons))
7554 (link (cdr key-cons)))
7555 (when (and record-link file)
7556 (setq key (copy-sequence key))
7557 (put-text-property
7558 0 (length key)
7559 'link
7560 (concat
7561 file
7562 (if link
7563 (concat idlwave-html-link-sep
7564 (number-to-string link))))
7565 key))
7566 (push (list key) kwds)))
7567 (cdr key-list))))
7568 (nthcdr 5 entry))
7569 (nreverse kwds)))
7570
7571 (defun idlwave-entry-find-keyword (entry keyword)
7572 "Find keyword KEYWORD in entry ENTRY, and return (with link) if set"
7573 (catch 'exit
7574 (mapc
7575 (lambda (key-list)
7576 (let ((file (car key-list))
7577 (kwd (assoc keyword (cdr key-list))))
7578 (when kwd
7579 (setq kwd (cons (car kwd)
7580 (if (and file (cdr kwd))
7581 (concat file
7582 idlwave-html-link-sep
7583 (number-to-string (cdr kwd)))
7584 (cdr kwd))))
7585 (throw 'exit kwd))))
7586 (nthcdr 5 entry))))
7587
7588 ;;==========================================================================
7589 ;;
7590 ;; Completing class structure tags. This is a completion plugin.
7591 ;; The necessary taglist is constructed dynamically
7592
7593 (defvar idlwave-current-tags-class nil)
7594 (defvar idlwave-current-class-tags nil)
7595 (defvar idlwave-current-native-class-tags nil)
7596 (defvar idlwave-sint-class-tags nil)
7597 (idlwave-new-sintern-type 'class-tag)
7598 (add-to-list 'idlwave-complete-special 'idlwave-complete-class-structure-tag)
7599 (add-hook 'idlwave-update-rinfo-hook 'idlwave-class-tag-reset)
7600
7601 (defun idlwave-complete-class-structure-tag ()
7602 "Complete a structure tag on a `self' argument in an object method."
7603 (interactive)
7604 (let ((pos (point))
7605 (case-fold-search t))
7606 (if (save-excursion
7607 ;; Check if the context is right
7608 (skip-chars-backward "a-zA-Z0-9._$")
7609 (and (< (point) (- pos 4))
7610 (looking-at "self\\.")))
7611 (let* ((class-selector (nth 2 (idlwave-current-routine)))
7612 (super-classes (idlwave-all-class-inherits class-selector)))
7613 ;; Check if we are in a class routine
7614 (unless class-selector
7615 (error "Not in a method procedure or function"))
7616 ;; Check if we need to update the "current" class
7617 (if (not (equal class-selector idlwave-current-tags-class))
7618 (idlwave-prepare-class-tag-completion class-selector))
7619 (setq idlwave-completion-help-info
7620 (list 'idlwave-complete-class-structure-tag-help
7621 (idlwave-sintern-routine
7622 (concat class-selector "__define"))
7623 nil))
7624 (let ((idlwave-cpl-bold idlwave-current-native-class-tags))
7625 (idlwave-complete-in-buffer
7626 'class-tag 'class-tag
7627 idlwave-current-class-tags nil
7628 (format "Select a tag of class %s" class-selector)
7629 "class tag"
7630 'idlwave-attach-class-tag-classes))
7631 t) ; return t to skip other completions
7632 nil)))
7633
7634 (defun idlwave-class-tag-reset ()
7635 (setq idlwave-current-tags-class nil))
7636
7637 (defun idlwave-prepare-class-tag-completion (class)
7638 "Find and parse the necessary class definitions for class structure tags."
7639 (setq idlwave-sint-class-tags nil)
7640 (setq idlwave-current-tags-class class)
7641 (setq idlwave-current-class-tags
7642 (mapcar (lambda (x)
7643 (list (idlwave-sintern-class-tag x 'set)))
7644 (idlwave-all-class-tags class)))
7645 (setq idlwave-current-native-class-tags
7646 (mapcar 'downcase (idlwave-class-tags class))))
7647
7648 ;===========================================================================
7649 ;;
7650 ;; Completing system variables and their structure fields
7651 ;; This is also a plugin.
7652
7653 (defvar idlwave-sint-sysvars nil)
7654 (defvar idlwave-sint-sysvartags nil)
7655 (idlwave-new-sintern-type 'sysvar)
7656 (idlwave-new-sintern-type 'sysvartag)
7657 (add-to-list 'idlwave-complete-special 'idlwave-complete-sysvar-or-tag)
7658 (add-hook 'idlwave-update-rinfo-hook 'idlwave-sysvars-reset)
7659 (add-hook 'idlwave-after-load-rinfo-hook 'idlwave-sintern-sysvar-alist)
7660
7661
7662 (defun idlwave-complete-sysvar-or-tag ()
7663 "Complete a system variable."
7664 (interactive)
7665 (let ((pos (point))
7666 (case-fold-search t))
7667 (cond ((save-excursion
7668 ;; Check if the context is right for system variable
7669 (skip-chars-backward "[a-zA-Z0-9_$]")
7670 (equal (char-before) ?!))
7671 (setq idlwave-completion-help-info '(idlwave-complete-sysvar-help))
7672 (idlwave-complete-in-buffer 'sysvar 'sysvar
7673 idlwave-system-variables-alist nil
7674 "Select a system variable"
7675 "system variable")
7676 t) ; return t to skip other completions
7677 ((save-excursion
7678 ;; Check if the context is right for sysvar tag
7679 (skip-chars-backward "a-zA-Z0-9_$.")
7680 (and (equal (char-before) ?!)
7681 (looking-at "\\([a-zA-Z][a-zA-Z0-9_$]*\\)\\.")
7682 (<= (match-end 0) pos)))
7683 ;; Complete a system variable tag
7684 (let* ((var (idlwave-sintern-sysvar (match-string 1)))
7685 (entry (assq var idlwave-system-variables-alist))
7686 (tags (cdr (assq 'tags entry))))
7687 (or entry (error "!%s is not a known system variable" var))
7688 (or tags (error "System variable !%s is not a structure" var))
7689 (setq idlwave-completion-help-info
7690 (list 'idlwave-complete-sysvar-tag-help var))
7691 (idlwave-complete-in-buffer 'sysvartag 'sysvartag
7692 tags nil
7693 "Select a system variable tag"
7694 "system variable tag")
7695 t)) ; return t to skip other completions
7696 (t nil))))
7697
7698 (defvar link) ;dynamic variables set by help callback
7699 (defvar props)
7700 (defun idlwave-complete-sysvar-help (mode word)
7701 (let ((word (or (nth 1 idlwave-completion-help-info) word))
7702 (entry (assoc word idlwave-system-variables-alist)))
7703 (cond
7704 ((eq mode 'test)
7705 (and (stringp word) entry (nth 1 (assq 'link entry))))
7706 ((eq mode 'set)
7707 (if entry (setq link (nth 1 (assq 'link entry))))) ;; setting dynamic!!!
7708 (t (error "This should not happen")))))
7709
7710 (defun idlwave-complete-sysvar-tag-help (mode word)
7711 (let* ((var (nth 1 idlwave-completion-help-info))
7712 (entry (assoc var idlwave-system-variables-alist))
7713 (tags (cdr (assq 'tags entry)))
7714 (main (nth 1 (assq 'link entry)))
7715 target main-base)
7716 (cond
7717 ((eq mode 'test) ; we can at least link the main
7718 (and (stringp word) entry main))
7719 ((eq mode 'set)
7720 (if entry
7721 (setq link
7722 (if (setq target (cdr (assoc-string word tags t)))
7723 (idlwave-substitute-link-target main target)
7724 main)))) ;; setting dynamic!!!
7725 (t (error "This should not happen")))))
7726
7727 (defun idlwave-split-link-target (link)
7728 "Split a given link into link file and anchor."
7729 (if (string-match idlwave-html-link-sep link)
7730 (cons (substring link 0 (match-beginning 0))
7731 (string-to-number (substring link (match-end 0))))))
7732
7733 (defun idlwave-substitute-link-target (link target)
7734 "Substitute the target anchor for the given link."
7735 (let (main-base)
7736 (setq main-base (if (string-match "#" link)
7737 (substring link 0 (match-beginning 0))
7738 link))
7739 (if target
7740 (concat main-base idlwave-html-link-sep (number-to-string target))
7741 link)))
7742
7743 ;; Fake help in the source buffer for class structure tags.
7744 ;; KWD AND NAME ARE GLOBAL-VARIABLES HERE.
7745 (defvar name)
7746 (defvar kwd)
7747 (defvar idlwave-help-do-class-struct-tag nil)
7748 (defun idlwave-complete-class-structure-tag-help (mode word)
7749 (cond
7750 ((eq mode 'test) ; nothing gets fontified for class tags
7751 nil)
7752 ((eq mode 'set)
7753 (let (class-with found-in)
7754 (when (setq class-with
7755 (idlwave-class-or-superclass-with-tag
7756 idlwave-current-tags-class
7757 word))
7758 (if (assq (idlwave-sintern-class class-with)
7759 idlwave-system-class-info)
7760 (error "No help available for system class tags"))
7761 (if (setq found-in (idlwave-class-found-in class-with))
7762 (setq name (cons (concat found-in "__define") class-with))
7763 (setq name (concat class-with "__define")))))
7764 (setq kwd word
7765 idlwave-help-do-class-struct-tag t))
7766 (t (error "This should not happen"))))
7767
7768 (defun idlwave-class-or-superclass-with-tag (class tag)
7769 "Find and return the CLASS or one of its superclass with the
7770 associated TAG, if any."
7771 (let ((sclasses (cons class (idlwave-all-class-inherits class)))
7772 cl)
7773 (catch 'exit
7774 (while sclasses
7775 (setq cl (pop sclasses))
7776 (let ((tags (idlwave-class-tags cl)))
7777 (while tags
7778 (if (eq t (compare-strings tag 0 nil (car tags) 0 nil t))
7779 (throw 'exit cl))
7780 (setq tags (cdr tags))))))))
7781
7782
7783 (defun idlwave-sysvars-reset ()
7784 (if (and (fboundp 'idlwave-shell-is-running)
7785 (idlwave-shell-is-running)
7786 idlwave-idlwave_routine_info-compiled)
7787 (idlwave-shell-send-command "idlwave_get_sysvars"
7788 'idlwave-process-sysvars 'hide)))
7789
7790 (defun idlwave-process-sysvars ()
7791 (idlwave-shell-filter-sysvars)
7792 (setq idlwave-sint-sysvars nil
7793 idlwave-sint-sysvartags nil)
7794 (idlwave-sintern-sysvar-alist))
7795
7796 (defun idlwave-sintern-sysvar-alist ()
7797 (let ((list idlwave-system-variables-alist) entry tags)
7798 (while (setq entry (pop list))
7799 (setcar entry (idlwave-sintern-sysvar (car entry) 'set))
7800 (setq tags (assq 'tags entry))
7801 (if tags
7802 (setcdr tags
7803 (mapcar (lambda (x)
7804 (cons (idlwave-sintern-sysvartag (car x) 'set)
7805 (cdr x)))
7806 (cdr tags)))))))
7807
7808 (defvar idlwave-shell-command-output)
7809 (defun idlwave-shell-filter-sysvars ()
7810 "Get any new system variables and tags."
7811 (let ((text idlwave-shell-command-output)
7812 (start 0)
7813 (old idlwave-system-variables-alist)
7814 var tags type name class link old-entry)
7815 (setq idlwave-system-variables-alist nil)
7816 (while (string-match "^IDLWAVE-SYSVAR: !\\([a-zA-Z0-9_$]+\\)\\( \\(.*\\)\\)?"
7817 text start)
7818 (setq start (match-end 0)
7819 var (match-string 1 text)
7820 tags (if (match-end 3)
7821 (idlwave-split-string (match-string 3 text))))
7822 ;; Maintain old links, if present
7823 (setq old-entry (assq (idlwave-sintern-sysvar var) old))
7824 (setq link (assq 'link old-entry))
7825 (setq idlwave-system-variables-alist
7826 (cons (list var
7827 (cons
7828 'tags
7829 (mapcar (lambda (x)
7830 (cons x
7831 (cdr (assq
7832 (idlwave-sintern-sysvartag x)
7833 (cdr (assq 'tags old-entry))))))
7834 tags)) link)
7835 idlwave-system-variables-alist)))
7836 ;; Keep the old value if query was not successful
7837 (setq idlwave-system-variables-alist
7838 (or idlwave-system-variables-alist old))))
7839
7840 (defun idlwave-completion-fontify-classes ()
7841 "Goto the *Completions* buffer and fontify the class info."
7842 (when (featurep 'font-lock)
7843 (save-excursion
7844 (set-buffer "*Completions*")
7845 (save-excursion
7846 (goto-char (point-min))
7847 (let ((buffer-read-only nil))
7848 (while (re-search-forward "\\.*<[^>]+>" nil t)
7849 (put-text-property (match-beginning 0) (match-end 0)
7850 'face 'font-lock-string-face)))))))
7851
7852 (defun idlwave-uniquify (list)
7853 (let ((ht (make-hash-table :size (length list) :test 'equal)))
7854 (delq nil
7855 (mapcar (lambda (x)
7856 (unless (gethash x ht)
7857 (puthash x t ht)
7858 x))
7859 list))))
7860
7861 (defun idlwave-after-successful-completion (type slash &optional verify)
7862 "Add `=' or `(' after successful completion of keyword and function.
7863 Restore the pre-completion window configuration if possible."
7864 (cond
7865 ((eq type 'procedure)
7866 nil)
7867 ((eq type 'function)
7868 (cond
7869 ((equal idlwave-function-completion-adds-paren nil) nil)
7870 ((or (equal idlwave-function-completion-adds-paren t)
7871 (equal idlwave-function-completion-adds-paren 1))
7872 (insert "("))
7873 ((equal idlwave-function-completion-adds-paren 2)
7874 (insert "()")
7875 (backward-char 1))
7876 (t nil)))
7877 ((eq type 'keyword)
7878 (if (and idlwave-keyword-completion-adds-equal
7879 (not slash))
7880 (progn (insert "=") t)
7881 nil)))
7882
7883 ;; Restore the pre-completion window configuration if this is safe.
7884
7885 (if (or (eq verify 'force) ; force
7886 (and
7887 (get-buffer-window "*Completions*") ; visible
7888 (idlwave-local-value 'idlwave-completion-p
7889 "*Completions*") ; cib-buffer
7890 (eq (marker-buffer idlwave-completion-mark)
7891 (current-buffer)) ; buffer OK
7892 (equal (marker-position idlwave-completion-mark)
7893 verify))) ; pos OK
7894 (idlwave-restore-wconf-after-completion))
7895 (move-marker idlwave-completion-mark nil)
7896 (setq idlwave-before-completion-wconf nil))
7897
7898 (defun idlwave-mouse-context-help (ev &optional arg)
7899 "Call `idlwave-context-help' on the clicked location."
7900 (interactive "eP")
7901 (mouse-set-point ev)
7902 (idlwave-context-help arg))
7903
7904 (defvar idlwave-last-context-help-pos nil)
7905 (defun idlwave-context-help (&optional arg)
7906 "Display IDL Online Help on context.
7907 If point is on a keyword, help for that keyword will be shown. If
7908 point is on a routine name or in the argument list of a routine, help
7909 for that routine will be displayed. Works for system routines and
7910 keywords, it pulls up text help. For other routies and keywords,
7911 visits the source file, finding help in the header (if
7912 `idlwave-help-source-try-header' is non-nil) or the routine definition
7913 itself."
7914 (interactive "P")
7915 (idlwave-do-context-help arg))
7916
7917 (defun idlwave-mouse-completion-help (ev)
7918 "Display online help about the completion at point."
7919 (interactive "eP")
7920 ;; Restore last-command for next command, to make
7921 ;; scrolling/cancelling of completions work.
7922 (setq this-command last-command)
7923 (idlwave-do-mouse-completion-help ev))
7924
7925 (defun idlwave-routine-info (&optional arg external)
7926 "Display a routines calling sequence and list of keywords. When
7927 point is on the name a function or procedure, or in the argument list
7928 of a function or procedure, this command displays a help buffer with
7929 the information. When called with prefix arg, enforce class query.
7930
7931 When point is on an object operator `->', display the class stored in
7932 this arrow, if any (see `idlwave-store-inquired-class'). With a
7933 prefix arg, the class property is cleared out."
7934
7935 (interactive "P")
7936 (idlwave-routines)
7937 (if (string-match "->" (buffer-substring
7938 (max (point-min) (1- (point)))
7939 (min (+ 2 (point)) (point-max))))
7940 ;; Cursor is on an arrow
7941 (if (get-text-property (point) 'idlwave-class)
7942 ;; arrow has class property
7943 (if arg
7944 ;; Remove property
7945 (save-excursion
7946 (backward-char 1)
7947 (when (looking-at ".?\\(->\\)")
7948 (remove-text-properties (match-beginning 1) (match-end 1)
7949 '(idlwave-class nil face nil))
7950 (message "Class property removed from arrow")))
7951 ;; Echo class property
7952 (message "Arrow has text property identifying object to be class %s"
7953 (get-text-property (point) 'idlwave-class)))
7954 ;; No property found
7955 (message "Arrow has no class text property"))
7956
7957 ;; Not on an arrow...
7958 (let* ((idlwave-query-class nil)
7959 (idlwave-force-class-query (equal arg '(4)))
7960 (module (idlwave-what-module)))
7961 (if (car module)
7962 (apply 'idlwave-display-calling-sequence
7963 (idlwave-fix-module-if-obj_new module))
7964 (error "Don't know which calling sequence to show")))))
7965
7966 (defun idlwave-resolve (&optional arg)
7967 "Call RESOLVE_ROUTINE on the module name at point.
7968 Like `idlwave-routine-info', this looks for a routine call at point.
7969 After confirmation in the minibuffer, it will use the shell to issue
7970 a RESOLVE call for this routine, to attempt to make it defined and its
7971 routine info available for IDLWAVE. If the routine is a method call,
7972 both `class__method' and `class__define' will be tried.
7973 With ARG, enforce query for the class of object methods."
7974 (interactive "P")
7975 (let* ((idlwave-query-class nil)
7976 (idlwave-force-class-query (equal arg '(4)))
7977 (module (idlwave-what-module))
7978 (name (idlwave-make-full-name (nth 2 module) (car module)))
7979 (type (if (eq (nth 1 module) 'pro) "pro" "function"))
7980 (resolve (read-string "Resolve: " (format "%s %s" type name)))
7981 (kwd "")
7982 class)
7983 (if (string-match "\\(pro\\|function\\)[ \t]+\\(\\(.*\\)::\\)?\\(.*\\)"
7984 resolve)
7985 (setq type (match-string 1 resolve)
7986 class (if (match-beginning 2)
7987 (match-string 3 resolve)
7988 nil)
7989 name (match-string 4 resolve)))
7990 (if (string= (downcase type) "function")
7991 (setq kwd ",/is_function"))
7992
7993 (cond
7994 ((null class)
7995 (idlwave-shell-send-command
7996 (format "resolve_routine,'%s'%s" (downcase name) kwd)
7997 'idlwave-update-routine-info
7998 nil t))
7999 (t
8000 (idlwave-shell-send-command
8001 (format "resolve_routine,'%s__define'%s" (downcase class) kwd)
8002 (list 'idlwave-shell-send-command
8003 (format "resolve_routine,'%s__%s'%s"
8004 (downcase class) (downcase name) kwd)
8005 '(idlwave-update-routine-info)
8006 nil t))))))
8007
8008 (defun idlwave-find-module-this-file ()
8009 (interactive)
8010 (idlwave-find-module '(4)))
8011
8012 (defun idlwave-find-module (&optional arg)
8013 "Find the source code of an IDL module.
8014 Works for modules for which IDLWAVE has routine info available. The
8015 function offers as default the module name `idlwave-routine-info'
8016 would use. With ARG limit to this buffer. With two prefix ARG's
8017 force class query for object methods."
8018 (interactive "P")
8019 (let* ((idlwave-query-class nil)
8020 (idlwave-force-class-query (equal arg '(16)))
8021 (this-buffer (equal arg '(4)))
8022 (module (idlwave-fix-module-if-obj_new (idlwave-what-module)))
8023 (default (if module
8024 (concat (idlwave-make-full-name
8025 (nth 2 module) (car module))
8026 (if (eq (nth 1 module) 'pro) "<p>" "<f>"))
8027 "none"))
8028 (list
8029 (idlwave-uniquify
8030 (delq nil
8031 (mapcar (lambda (x)
8032 (if (eq 'system (car-safe (nth 3 x)))
8033 ;; Take out system routines with no source.
8034 nil
8035 (list
8036 (concat (idlwave-make-full-name
8037 (nth 2 x) (car x))
8038 (if (eq (nth 1 x) 'pro) "<p>" "<f>")))))
8039 (if this-buffer
8040 (idlwave-save-buffer-update)
8041 (idlwave-routines))))))
8042 (name (idlwave-completing-read
8043 (if (or (not this-buffer)
8044 (assoc default list))
8045 (format "Module (Default %s): " default)
8046 (format "Module in this file: "))
8047 list))
8048 type class)
8049 (if (string-match "\\`\\s-*\\'" name)
8050 ;; Nothing, use the default.
8051 (setq name default))
8052 (if (string-match "<[fp]>" name)
8053 (setq type (substring name -2 -1)
8054 name (substring name 0 -3)))
8055 (if (string-match "\\(.*\\)::\\(.*\\)" name)
8056 (setq class (match-string 1 name)
8057 name (match-string 2 name)))
8058 (setq name (idlwave-sintern-routine-or-method name class)
8059 class (idlwave-sintern-class class)
8060 type (cond ((equal type "f") 'fun)
8061 ((equal type "p") 'pro)
8062 (t t)))
8063 (idlwave-do-find-module name type class nil this-buffer)))
8064
8065 (defun idlwave-do-find-module (name type class
8066 &optional force-source this-buffer)
8067 (let ((name1 (idlwave-make-full-name class name))
8068 source buf1 entry
8069 (buf (current-buffer))
8070 (pos (point))
8071 file name2)
8072 (setq entry (idlwave-best-rinfo-assq name type class (idlwave-routines)
8073 'WITH-FILE)
8074 source (or force-source (nth 3 entry))
8075 name2 (if (nth 2 entry)
8076 (idlwave-make-full-name (nth 2 entry) name)
8077 name1))
8078 (if source
8079 (setq file (idlwave-routine-source-file source)))
8080 (unless file ; Try to find it on the path.
8081 (setq file
8082 (idlwave-expand-lib-file-name
8083 (if class
8084 (format "%s__define.pro" (downcase class))
8085 (format "%s.pro" (downcase name))))))
8086 (cond
8087 ((or (null name) (equal name ""))
8088 (error "Abort"))
8089 ((eq (car source) 'system)
8090 (error "Source code for system routine %s is not available"
8091 name2))
8092 ((or (not file) (not (file-regular-p file)))
8093 (error "Source code for routine %s is not available"
8094 name2))
8095 (t
8096 (when (not this-buffer)
8097 (setq buf1
8098 (idlwave-find-file-noselect file 'find))
8099 (pop-to-buffer buf1 t))
8100 (goto-char (point-max))
8101 (let ((case-fold-search t))
8102 (if (re-search-backward
8103 (concat "^[ \t]*\\<"
8104 (cond ((eq type 'fun) "function")
8105 ((eq type 'pro) "pro")
8106 (t "\\(pro\\|function\\)"))
8107 "\\>[ \t]+"
8108 (regexp-quote (downcase name2))
8109 "[^a-zA-Z0-9_$]")
8110 nil t)
8111 (goto-char (match-beginning 0))
8112 (pop-to-buffer buf)
8113 (goto-char pos)
8114 (error "Could not find routine %s" name2)))))))
8115
8116 (defun idlwave-what-module ()
8117 "Return a default module for stuff near point.
8118 Used by `idlwave-routine-info' and `idlwave-find-module'."
8119 (idlwave-routines)
8120 (if (let ((case-fold-search t))
8121 (save-excursion
8122 (idlwave-beginning-of-statement)
8123 (looking-at "[ \t]*\\(pro\\|function\\)[ \t]+\\(\\([a-zA-Z0-9_$]+\\)::\\)?\\([a-zA-Z0-9$_]+\\)\\([, \t\n]\\|$\\)")))
8124 ;; This is a function or procedure definition statement
8125 ;; We return the defined routine as module.
8126 (list
8127 (idlwave-sintern-routine-or-method (match-string-no-properties 4)
8128 (match-string-no-properties 2))
8129 (if (equal (downcase (match-string 1)) "pro") 'pro 'fun)
8130 (idlwave-sintern-class (match-string 3)))
8131
8132 ;; Not a definition statement - analyze precise position.
8133 (let* ((where (idlwave-where))
8134 (cw (nth 2 where))
8135 (pro (car (nth 0 where)))
8136 (func (car (nth 1 where)))
8137 (this-word (idlwave-this-word "a-zA-Z0-9$_"))
8138 (next-char (save-excursion (skip-chars-forward "a-zA-Z0-9$_")
8139 (following-char)))
8140 )
8141 (cond
8142 ((and (eq cw 'procedure)
8143 (not (equal this-word "")))
8144 (setq this-word (idlwave-sintern-routine-or-method
8145 this-word (nth 2 (nth 3 where))))
8146 (list this-word 'pro
8147 (idlwave-determine-class
8148 (cons this-word (cdr (nth 3 where)))
8149 'pro)))
8150 ((and (eq cw 'function)
8151 (not (equal this-word ""))
8152 (or (eq next-char ?\() ; exclude arrays, vars.
8153 (looking-at "[a-zA-Z0-9_]*[ \t]*(")))
8154 (setq this-word (idlwave-sintern-routine-or-method
8155 this-word (nth 2 (nth 3 where))))
8156 (list this-word 'fun
8157 (idlwave-determine-class
8158 (cons this-word (cdr (nth 3 where)))
8159 'fun)))
8160 ((and (memq cw '(function-keyword procedure-keyword))
8161 (not (equal this-word ""))
8162 (eq next-char ?\()) ; A function!
8163 (setq this-word (idlwave-sintern-routine this-word))
8164 (list this-word 'fun nil))
8165 (func
8166 (list func 'fun (idlwave-determine-class (nth 1 where) 'fun)))
8167 (pro
8168 (list pro 'pro (idlwave-determine-class (nth 0 where) 'pro)))
8169 (t nil)))))
8170
8171 (defun idlwave-what-module-find-class ()
8172 "Call idlwave-what-module and find the inherited class if necessary."
8173 (let* ((module (idlwave-what-module))
8174 (class (nth 2 module))
8175 classes)
8176 (if (and (= (length module) 3)
8177 (stringp class))
8178 (list (car module)
8179 (nth 1 module)
8180 (apply 'idlwave-find-inherited-class module))
8181 module)))
8182
8183 (defun idlwave-find-inherited-class (name type class)
8184 "Find the class which defines TYPE NAME and is CLASS or inherited by CLASS."
8185 (let ((entry (idlwave-best-rinfo-assoc name type class (idlwave-routines))))
8186 (if entry
8187 (nth 2 entry)
8188 class)))
8189
8190 (defun idlwave-fix-module-if-obj_new (module)
8191 "Check if MODULE points to obj_new.
8192 If yes, and if the cursor is in the keyword region, change to the
8193 appropriate Init method."
8194 (let* ((name (car module))
8195 (pos (point))
8196 (case-fold-search t)
8197 string)
8198 (if (and (stringp name)
8199 (equal (downcase name) "obj_new")
8200 (save-excursion
8201 (idlwave-beginning-of-statement)
8202 (setq string (buffer-substring (point) pos))
8203 (string-match "obj_new([^'\"]*['\"]\\([a-zA-Z0-9_]+\\)"
8204 string)))
8205 (let ((name "Init")
8206 (class (match-string 1 string)))
8207 (setq module (list (idlwave-sintern-method "Init")
8208 'fun
8209 (idlwave-sintern-class class)))))
8210 module))
8211
8212 (defun idlwave-fix-keywords (name type class keywords
8213 &optional super-classes system)
8214 "Update a list of keywords.
8215 Translate OBJ_NEW, adding all super-class keywords, or all keywords
8216 from all classes if class equals t. If SYSTEM is non-nil, don't
8217 demand _EXTRA in the keyword list."
8218 (let ((case-fold-search t))
8219
8220 ;; If this is the OBJ_NEW function, try to figure out the class and use
8221 ;; the keywords from the corresponding INIT method.
8222 (if (and (equal (upcase name) "OBJ_NEW")
8223 (or (eq major-mode 'idlwave-mode)
8224 (eq major-mode 'idlwave-shell-mode)))
8225 (let* ((bos (save-excursion (idlwave-beginning-of-statement) (point)))
8226 (string (buffer-substring bos (point)))
8227 (case-fold-search t)
8228 class)
8229 (and (string-match "obj_new([^'\"]*['\"]\\([a-zA-Z0-9_]+\\)"
8230 string)
8231 (setq class (idlwave-sintern-class (match-string 1 string)))
8232 (setq idlwave-current-obj_new-class class)
8233 (setq keywords
8234 (append keywords
8235 (idlwave-entry-keywords
8236 (idlwave-rinfo-assq
8237 (idlwave-sintern-method "INIT")
8238 'fun
8239 class
8240 (idlwave-routines)) 'do-link))))))
8241
8242 ;; If the class is `t', combine all keywords of all methods NAME
8243 (when (eq class t)
8244 (mapc (lambda (entry)
8245 (and
8246 (nth 2 entry) ; non-nil class
8247 (eq (nth 1 entry) type) ; correct type
8248 (setq keywords
8249 (append keywords
8250 (idlwave-entry-keywords entry 'do-link)))))
8251 (idlwave-all-assq name (idlwave-routines)))
8252 (setq keywords (idlwave-uniquify keywords)))
8253
8254 ;; If we have inheritance, add all keywords from superclasses, if
8255 ;; the user indicated that method in `idlwave-keyword-class-inheritance'
8256 (when (and
8257 super-classes
8258 idlwave-keyword-class-inheritance
8259 (stringp class)
8260 (or
8261 system
8262 (assq (idlwave-sintern-keyword "_extra") keywords)
8263 (assq (idlwave-sintern-keyword "_ref_extra") keywords))
8264 ;; Check if one of the keyword-class regexps matches the name
8265 (let ((regexps idlwave-keyword-class-inheritance) re)
8266 (catch 'exit
8267 (while (setq re (pop regexps))
8268 (if (string-match re name) (throw 'exit t))))))
8269
8270 (loop for entry in (idlwave-routines) do
8271 (and (nth 2 entry) ; non-nil class
8272 (memq (nth 2 entry) super-classes) ; an inherited class
8273 (eq (nth 1 entry) type) ; correct type
8274 (eq (car entry) name) ; correct name
8275 (mapcar (lambda (k) (add-to-list 'keywords k))
8276 (idlwave-entry-keywords entry 'do-link))))
8277 (setq keywords (idlwave-uniquify keywords)))
8278
8279 ;; Return the final list
8280 keywords))
8281
8282 (defun idlwave-expand-keyword (keyword module)
8283 "Expand KEYWORD to one of the valid keyword parameters of MODULE.
8284 KEYWORD may be an exact match or an abbreviation of a keyword.
8285 If the match is exact, KEYWORD itself is returned, even if there may be other
8286 keywords of which KEYWORD is an abbreviation. This is necessary because some
8287 system routines have keywords which are prefixes of other keywords.
8288 If KEYWORD is an abbreviation of several keywords, a list of all possible
8289 completions is returned.
8290 If the abbreviation was unique, the correct keyword is returned.
8291 If it cannot be a keyword, the function return nil.
8292 If we do not know about MODULE, just return KEYWORD literally."
8293 (let* ((name (car module))
8294 (type (nth 1 module))
8295 (class (nth 2 module))
8296 (kwd (idlwave-sintern-keyword keyword))
8297 (entry (idlwave-best-rinfo-assoc name type class (idlwave-routines)))
8298 (kwd-alist (idlwave-entry-keywords entry))
8299 (extra (or (assq (idlwave-sintern-keyword "_EXTRA") kwd-alist)
8300 (assq (idlwave-sintern-keyword "_REF_EXTRA") kwd-alist)))
8301 (completion-ignore-case t)
8302 candidates)
8303 (cond ((assq kwd kwd-alist)
8304 kwd)
8305 ((setq candidates (all-completions kwd kwd-alist))
8306 (if (= (length candidates) 1)
8307 (car candidates)
8308 candidates))
8309 ((and entry extra)
8310 ;; Inheritance may cause this keyword to be correct
8311 keyword)
8312 (entry
8313 ;; We do know the function, which does not have the keyword.
8314 nil)
8315 (t
8316 ;; We do not know the function, so this just might be a correct
8317 ;; keyword - return it as it is.
8318 keyword))))
8319
8320 (defvar idlwave-rinfo-mouse-map (make-sparse-keymap))
8321 (defvar idlwave-rinfo-map (make-sparse-keymap))
8322 (define-key idlwave-rinfo-mouse-map
8323 (if (featurep 'xemacs) [button2] [mouse-2])
8324 'idlwave-mouse-active-rinfo)
8325 (define-key idlwave-rinfo-mouse-map
8326 (if (featurep 'xemacs) [(shift button2)] [(shift mouse-2)])
8327 'idlwave-mouse-active-rinfo-shift)
8328 (define-key idlwave-rinfo-mouse-map
8329 (if (featurep 'xemacs) [button3] [mouse-3])
8330 'idlwave-mouse-active-rinfo-right)
8331 (define-key idlwave-rinfo-mouse-map " " 'idlwave-active-rinfo-space)
8332 (define-key idlwave-rinfo-map "q" 'idlwave-quit-help)
8333 (define-key idlwave-rinfo-mouse-map "q" 'idlwave-quit-help)
8334 (defvar idlwave-popup-source nil)
8335 (defvar idlwave-rinfo-marker (make-marker))
8336
8337 (defun idlwave-quit-help ()
8338 (interactive)
8339 (let ((ri-window (get-buffer-window "*Help*"))
8340 (olh-window (get-buffer-window "*IDLWAVE Help*")))
8341 (when (and olh-window
8342 (fboundp 'idlwave-help-quit))
8343 (select-window olh-window)
8344 (idlwave-help-quit))
8345 (when (window-live-p ri-window)
8346 (delete-window ri-window))))
8347
8348 (defun idlwave-display-calling-sequence (name type class
8349 &optional initial-class)
8350 ;; Display the calling sequence of module NAME, type TYPE in class CLASS.
8351 (let* ((initial-class (or initial-class class))
8352 (entry (or (idlwave-best-rinfo-assq name type class
8353 (idlwave-routines))
8354 (idlwave-rinfo-assq name type class
8355 idlwave-unresolved-routines)))
8356 (name (or (car entry) name))
8357 (class (or (nth 2 entry) class))
8358 (superclasses (idlwave-all-class-inherits initial-class))
8359 (twins (idlwave-routine-twins entry))
8360 (dtwins (idlwave-study-twins twins))
8361 (all dtwins)
8362 (system (eq (car (nth 3 entry)) 'system))
8363 (calling-seq (nth 4 entry))
8364 (keywords (idlwave-entry-keywords entry 'do-link))
8365 (html-file (car (nth 5 entry)))
8366 (help-echo-kwd
8367 "Button2: Insert KEYWORD (SHIFT=`/KEYWORD') | Button3: Online Help ")
8368 (help-echo-use
8369 "Button2/3: Online Help")
8370 (help-echo-src
8371 "Button2: Jump to source and back | Button3: Source in Help window.")
8372 (help-echo-class
8373 "Button2: Display info about same method in superclass")
8374 (col 0)
8375 (data (list name type class (current-buffer) nil initial-class))
8376 (km-prop (if (featurep 'xemacs) 'keymap 'local-map))
8377 (face 'idlwave-help-link)
8378 beg props win cnt total)
8379 ;; Fix keywords, but don't add chained super-classes, since these
8380 ;; are shown separately for that super-class
8381 (setq keywords (idlwave-fix-keywords name type class keywords))
8382 (cond
8383 ((null entry)
8384 (error "No %s %s known %s" type name
8385 (if initial-class (concat "in class " initial-class) "")))
8386 ((or (null name) (equal name ""))
8387 (error "No function or procedure call at point"))
8388 ((null calling-seq)
8389 (error "Calling sequence of %s %s not available" type name))
8390 (t
8391 (save-excursion
8392 (move-marker idlwave-rinfo-marker (point))
8393 (set-buffer (get-buffer-create "*Help*"))
8394 (use-local-map idlwave-rinfo-map)
8395 (setq buffer-read-only nil)
8396 (erase-buffer)
8397 (set (make-local-variable 'idlwave-popup-source) nil)
8398 (set (make-local-variable 'idlwave-current-obj_new-class)
8399 idlwave-current-obj_new-class)
8400 (when superclasses
8401 (setq props (list 'mouse-face 'highlight
8402 km-prop idlwave-rinfo-mouse-map
8403 'help-echo help-echo-class
8404 'data (cons 'class data)))
8405 (let ((classes (cons initial-class superclasses)) c)
8406 (insert "Classes: ")
8407 (while (setq c (pop classes))
8408 (insert " ")
8409 (setq beg (point))
8410 (insert c)
8411 (if (equal (downcase c) (downcase class))
8412 (add-text-properties beg (point) (list 'face 'bold))
8413 ;; If Method exists in a different class link it
8414 (if (idlwave-rinfo-assq name type c (idlwave-routines))
8415 (add-text-properties beg (point) props))))
8416 (insert "\n")))
8417 (setq props (list 'mouse-face 'highlight
8418 km-prop idlwave-rinfo-mouse-map
8419 'help-echo help-echo-use
8420 'data (cons 'usage data)))
8421 (if html-file (setq props (append (list 'face face 'link html-file)
8422 props)))
8423 (insert "Usage: ")
8424 (setq beg (point))
8425 (insert (if class
8426 (format calling-seq class name class name class name)
8427 (format calling-seq name name name name))
8428 "\n")
8429 (add-text-properties beg (point) props)
8430
8431 (insert "Keywords:")
8432 (if (null keywords)
8433 (insert " No keywords accepted.")
8434 (setq col 9)
8435 (mapcar
8436 (lambda (x)
8437 (if (>= (+ col 1 (length (car x)))
8438 (window-width))
8439 (progn
8440 (insert "\n ")
8441 (setq col 9)))
8442 (insert " ")
8443 (setq beg (point)
8444 ;; Relevant keywords already have link property attached
8445 props (list 'mouse-face 'highlight
8446 km-prop idlwave-rinfo-mouse-map
8447 'data (cons 'keyword data)
8448 'help-echo help-echo-kwd
8449 'keyword (car x)))
8450 (if system (setq props (append (list 'face face) props)))
8451 (insert (car x))
8452 (add-text-properties beg (point) props)
8453 (setq col (+ col 1 (length (car x)))))
8454 keywords))
8455
8456 (setq cnt 1 total (length all))
8457 ;; Here entry is (key file (list of type-conses))
8458 (while (setq entry (pop all))
8459 (setq props (list 'mouse-face 'highlight
8460 km-prop idlwave-rinfo-mouse-map
8461 'help-echo help-echo-src
8462 'source (list (car (car (nth 2 entry))) ;type
8463 (nth 1 entry)
8464 nil
8465 (cdr (car (nth 2 entry))))
8466 'data (cons 'source data)))
8467 (idlwave-insert-source-location
8468 (format "\n%-8s %s"
8469 (if (equal cnt 1)
8470 (if (> total 1) "Sources:" "Source:")
8471 "")
8472 (if (> total 1) "- " ""))
8473 entry props)
8474 (incf cnt)
8475 (when (and all (> cnt idlwave-rinfo-max-source-lines))
8476 ;; No more source lines, please
8477 (insert (format
8478 "\n Source information truncated to %d entries."
8479 idlwave-rinfo-max-source-lines))
8480 (setq all nil)))
8481 (goto-char (point-min))
8482 (setq buffer-read-only t))
8483 (display-buffer "*Help*")
8484 (if (and (setq win (get-buffer-window "*Help*"))
8485 idlwave-resize-routine-help-window)
8486 (progn
8487 (let ((ww (selected-window)))
8488 (unwind-protect
8489 (progn
8490 (select-window win)
8491 (enlarge-window (- (/ (frame-height) 2)
8492 (window-height)))
8493 (shrink-window-if-larger-than-buffer))
8494 (select-window ww)))))))))
8495
8496 (defun idlwave-insert-source-location (prefix entry &optional file-props)
8497 "Insert a source location into the routine info buffer.
8498 Start line with PREFIX. If a file name is inserted, add FILE-PROPS to
8499 it."
8500 (let* ((key (car entry))
8501 (file (nth 1 entry))
8502 (types (nth 2 entry))
8503 (shell-flag (assq 'compiled types))
8504 (buffer-flag (assq 'buffer types))
8505 (user-flag (assq 'user types))
8506 (lib-flag (assq 'lib types))
8507 (ndupl (or (and buffer-flag (idlwave-count-memq 'buffer types))
8508 (and user-flag (idlwave-count-memq 'user types))
8509 (and lib-flag (idlwave-count-memq 'lib types))
8510 1))
8511 (doflags t)
8512 beg special)
8513
8514 (insert prefix)
8515
8516 (cond
8517 ((eq key 'system)
8518 (setq doflags nil)
8519 (insert "System "))
8520
8521 ((eq key 'builtin)
8522 (setq doflags nil)
8523 (insert "Builtin "))
8524
8525 ((and (not file) shell-flag)
8526 (insert "Unresolved"))
8527
8528 ((null file)
8529 (insert "ERROR"))
8530
8531 ((idlwave-syslib-p file)
8532 (if (string-match "obsolete" (file-name-directory file))
8533 (insert "Obsolete ")
8534 (insert "SystemLib ")))
8535
8536 ;; New special syntax: taken directly from routine-info for
8537 ;; library catalog routines
8538 ((setq special (or (cdr lib-flag) (cdr user-flag)))
8539 (insert (format "%-10s" special)))
8540
8541 ;; Old special syntax: a matching regexp
8542 ((setq special (idlwave-special-lib-test file))
8543 (insert (format "%-10s" special)))
8544
8545 ;; Catch-all with file
8546 ((idlwave-lib-p file) (insert "Library "))
8547
8548 ;; Sanity catch all
8549 (t (insert "Other ")))
8550
8551 (when doflags
8552 (insert (concat
8553 " ["
8554 (if lib-flag "L" "-")
8555 (if user-flag "C" "-")
8556 (if shell-flag "S" "-")
8557 (if buffer-flag "B" "-")
8558 "] ")))
8559 (when (> ndupl 1)
8560 (setq beg (point))
8561 (insert (format "(%dx) " ndupl))
8562 (add-text-properties beg (point) (list 'face 'bold)))
8563 (when (and file (not (equal file "")))
8564 (setq beg (point))
8565 (insert (apply 'abbreviate-file-name
8566 (if (featurep 'xemacs) (list file t) (list file))))
8567 (if file-props
8568 (add-text-properties beg (point) file-props)))))
8569
8570 (defun idlwave-special-lib-test (file)
8571 "Check the path of FILE against the regexps which define special libs.
8572 Return the name of the special lib if there is a match."
8573 (let ((alist idlwave-special-lib-alist)
8574 entry rtn)
8575 (cond
8576 ((stringp file)
8577 (while (setq entry (pop alist))
8578 (if (string-match (car entry) file)
8579 (setq rtn (cdr entry)
8580 alist nil)))
8581 rtn)
8582 (t nil))))
8583
8584 (defun idlwave-mouse-active-rinfo-right (ev)
8585 (interactive "e")
8586 (idlwave-mouse-active-rinfo ev 'right))
8587
8588 (defun idlwave-mouse-active-rinfo-shift (ev)
8589 (interactive "e")
8590 (idlwave-mouse-active-rinfo ev nil 'shift))
8591
8592 (defun idlwave-active-rinfo-space ()
8593 (interactive)
8594 (idlwave-mouse-active-rinfo nil 'right))
8595
8596 (defun idlwave-mouse-active-rinfo (ev &optional right shift)
8597 "Does the mouse actions in the routine info buffer.
8598 Optional args RIGHT and SHIFT indicate, if mouse-3 was used, and if SHIFT
8599 was pressed."
8600 (interactive "e")
8601 (if ev (mouse-set-point ev))
8602 (let (data id name type class buf bufwin source link keyword
8603 word initial-class)
8604 (setq data (get-text-property (point) 'data)
8605 source (get-text-property (point) 'source)
8606 keyword (get-text-property (point) 'keyword)
8607 link (get-text-property (point) 'link)
8608 id (car data)
8609 name (nth 1 data) type (nth 2 data) class (nth 3 data)
8610 buf (nth 4 data)
8611 initial-class (nth 6 data)
8612 word (idlwave-this-word)
8613 bufwin (get-buffer-window buf t))
8614
8615 (cond ((eq id 'class) ; Switch class being displayed
8616 (if (window-live-p bufwin) (select-window bufwin))
8617 (idlwave-display-calling-sequence
8618 (idlwave-sintern-method name)
8619 type (idlwave-sintern-class word)
8620 initial-class))
8621 ((eq id 'usage) ; Online help on this routine
8622 (idlwave-online-help link name type class))
8623 ((eq id 'source) ; Source in help or buffer
8624 (if right ; In help
8625 (let ((idlwave-extra-help-function 'idlwave-help-with-source)
8626 (idlwave-help-source-try-header nil)
8627 ;; Fake idlwave-routines so help will find the right entry
8628 (idlwave-routines
8629 (list (list name type class source ""))))
8630 (idlwave-help-get-special-help name type class nil))
8631 ;; Otherwise just pop to the source
8632 (setq idlwave-popup-source (not idlwave-popup-source))
8633 (if idlwave-popup-source
8634 (condition-case err
8635 (idlwave-do-find-module name type class source)
8636 (error
8637 (setq idlwave-popup-source nil)
8638 (if (window-live-p bufwin) (select-window bufwin))
8639 (error (nth 1 err))))
8640 (if bufwin
8641 (select-window bufwin)
8642 (pop-to-buffer buf))
8643 (goto-char (marker-position idlwave-rinfo-marker)))))
8644 ((eq id 'keyword)
8645 (if right
8646 (idlwave-online-help link name type class keyword)
8647 (idlwave-rinfo-insert-keyword keyword buf shift))))))
8648
8649 (defun idlwave-rinfo-insert-keyword (keyword buffer &optional shift)
8650 "Insert KEYWORD in BUFFER. Make sure buffer is displayed in a window."
8651 (let ((bwin (get-buffer-window buffer)))
8652 (if idlwave-complete-empty-string-as-lower-case
8653 (setq keyword (downcase keyword)))
8654 (if bwin
8655 (select-window bwin)
8656 (pop-to-buffer buffer)
8657 (setq bwin (get-buffer-window buffer)))
8658 (if (eq (preceding-char) ?/)
8659 (insert keyword)
8660 (unless (save-excursion
8661 (re-search-backward
8662 "[(,][ \t]*\\(\\$[ \t]*\\(;.*\\)?\n\\)?[ \t]*\\="
8663 (min (- (point) 100) (point-min)) t))
8664 (insert ", "))
8665 (if shift (insert "/"))
8666 (insert keyword)
8667 (if (and (not shift)
8668 idlwave-keyword-completion-adds-equal)
8669 (insert "=")))))
8670
8671 (defun idlwave-list-buffer-load-path-shadows (&optional arg)
8672 "List the load path shadows of all routines defined in current buffer."
8673 (interactive "P")
8674 (idlwave-routines)
8675 (if (eq major-mode 'idlwave-mode)
8676 (idlwave-list-load-path-shadows
8677 nil (idlwave-update-current-buffer-info 'save-buffer)
8678 "in current buffer")
8679 (error "Current buffer is not in idlwave-mode")))
8680
8681 (defun idlwave-list-shell-load-path-shadows (&optional arg)
8682 "List the load path shadows of all routines compiled under the shell.
8683 This is very useful for checking an IDL application. Just compile the
8684 application, do RESOLVE_ALL, and `C-c C-i' to compile all referenced
8685 routines and update IDLWAVE internal info. Then check for shadowing
8686 with this command."
8687 (interactive "P")
8688 (cond
8689 ((or (not (fboundp 'idlwave-shell-is-running))
8690 (not (idlwave-shell-is-running)))
8691 (error "Shell is not running"))
8692 ((null idlwave-compiled-routines)
8693 (error "No compiled routines. Maybe you need to update with `C-c C-i'"))
8694 (t
8695 (idlwave-list-load-path-shadows nil idlwave-compiled-routines
8696 "in the shell"))))
8697
8698 (defun idlwave-list-all-load-path-shadows (&optional arg)
8699 "List the load path shadows of all routines known to IDLWAVE."
8700 (interactive "P")
8701 (idlwave-list-load-path-shadows nil nil "globally"))
8702
8703 (defun idlwave-list-load-path-shadows (arg &optional special-routines loc)
8704 "List the routines which are defined multiple times.
8705 Search the information IDLWAVE has about IDL routines for multiple
8706 definitions.
8707 When SPECIAL-ROUTINES in non-nil, only look for shadows of these routines.
8708
8709 When IDL hits a routine call which is not defined, it will search on
8710 the load path in order to find a definition. The output of this
8711 command can be used to detect possible name clashes during this process."
8712 (idlwave-routines) ; Make sure everything is loaded.
8713 (unless (or idlwave-user-catalog-routines idlwave-library-catalog-routines)
8714 (or (y-or-n-p
8715 "You don't have any user or library catalogs. Continue anyway? ")
8716 (error "Abort")))
8717 (let* ((routines (append idlwave-system-routines
8718 idlwave-compiled-routines
8719 idlwave-library-catalog-routines
8720 idlwave-user-catalog-routines
8721 idlwave-buffer-routines
8722 nil))
8723 (km-prop (if (featurep 'xemacs) 'keymap 'local-map))
8724 (keymap (make-sparse-keymap))
8725 (props (list 'mouse-face 'highlight
8726 km-prop keymap
8727 'help-echo "Mouse2: Find source"))
8728 (nroutines (length (or special-routines routines)))
8729 (step (/ nroutines 100))
8730 (n 0)
8731 (cnt 0)
8732 (idlwave-sort-prefer-buffer-info nil)
8733 routine twins dtwins twin done props1 lroutines)
8734
8735 (if special-routines
8736 ;; Just looking for shadows of a few special routines
8737 (setq lroutines routines
8738 routines special-routines))
8739
8740 (message "Sorting routines...")
8741 (setq routines (sort routines
8742 (lambda (a b)
8743 (string< (downcase (idlwave-make-full-name
8744 (nth 2 a) (car a)))
8745 (downcase (idlwave-make-full-name
8746 (nth 2 b) (car b)))))))
8747 (message "Sorting routines...done")
8748
8749 (define-key keymap (if (featurep 'xemacs) [(button2)] [(mouse-2)])
8750 (lambda (ev)
8751 (interactive "e")
8752 (mouse-set-point ev)
8753 (apply 'idlwave-do-find-module
8754 (get-text-property (point) 'find-args))))
8755 (define-key keymap [(return)]
8756 (lambda ()
8757 (interactive)
8758 (apply 'idlwave-do-find-module
8759 (get-text-property (point) 'find-args))))
8760 (message "Compiling list...( 0%%)")
8761 (save-excursion
8762 (set-buffer (get-buffer-create "*Shadows*"))
8763 (setq buffer-read-only nil)
8764 (erase-buffer)
8765 (while (setq routine (pop routines))
8766 (if (= (mod (setq n (1+ n)) step) 0)
8767 (message "Compiling list...(%2d%%)" (/ (* n 100) nroutines)))
8768
8769 ;; Get a list of all twins
8770 (setq twins (idlwave-routine-twins routine (or lroutines routines)))
8771 (if (memq routine done)
8772 (setq dtwins nil)
8773 (setq dtwins (idlwave-study-twins twins)))
8774 ;; Mark all twins as dealt with
8775 (setq done (append twins done))
8776 (when (or (> (length dtwins) 1)
8777 (> (idlwave-count-memq 'lib (nth 2 (car dtwins))) 1)
8778 (> (idlwave-count-memq 'user (nth 2 (car dtwins))) 1)
8779 (> (idlwave-count-memq 'buffer (nth 2 (car dtwins))) 1))
8780 (incf cnt)
8781 (insert (format "\n%s%s"
8782 (idlwave-make-full-name (nth 2 routine)
8783 (car routine))
8784 (if (eq (nth 1 routine) 'fun) "()" "")))
8785 (while (setq twin (pop dtwins))
8786 (setq props1 (append (list 'find-args
8787 (list (nth 0 routine)
8788 (nth 1 routine)
8789 (nth 2 routine)))
8790 props))
8791 (idlwave-insert-source-location "\n - " twin props1))))
8792 (goto-char (point-min))
8793 (setq buffer-read-only t))
8794 (setq loc (or loc ""))
8795 (if (> cnt 0)
8796 (progn
8797 (display-buffer (get-buffer "*Shadows*"))
8798 (message "%d case%s of shadowing found %s"
8799 cnt (if (= cnt 1) "" "s") loc))
8800 (message "No shadowing conflicts found %s" loc))))
8801
8802 (defun idlwave-print-source (routine)
8803 (let* ((source (nth 3 routine))
8804 (stype (car source))
8805 (sfile (idlwave-routine-source-file source)))
8806 (if (idlwave-syslib-p sfile) (setq stype 'syslib))
8807 (if (and (eq stype 'compiled)
8808 (or (not (stringp sfile))
8809 (not (string-match "\\S-" sfile))))
8810 (setq stype 'unresolved))
8811 (princ (format " %-10s %s\n"
8812 stype
8813 (if sfile sfile "No source code available")))))
8814
8815 (defun idlwave-routine-twins (entry &optional list)
8816 "Return all twin entries of ENTRY in LIST.
8817 LIST defaults to `idlwave-routines'.
8818 Twin entries are those which have the same name, type, and class.
8819 ENTRY will also be returned, as the first item of this list."
8820 (let* ((name (car entry))
8821 (type (nth 1 entry))
8822 (class (nth 2 entry))
8823 (candidates (idlwave-all-assq name (or list (idlwave-routines))))
8824 twins candidate)
8825 (while (setq candidate (pop candidates))
8826 (if (and (not (eq candidate entry))
8827 (eq type (nth 1 candidate))
8828 (eq class (nth 2 candidate)))
8829 (push candidate twins)))
8830 (if (setq candidate (idlwave-rinfo-assq name type class
8831 idlwave-unresolved-routines))
8832 (push candidate twins))
8833 (cons entry (nreverse twins))))
8834
8835 (defun idlwave-study-twins (entries)
8836 "Return dangerous twins of first entry in ENTRIES.
8837 Dangerous twins are routines with same name, but in different files on
8838 the load path. If a file is in the system library and has an entry in
8839 the `idlwave-system-routines' list, we omit the latter as
8840 non-dangerous because many IDL routines are implemented as library
8841 routines, and may have been scanned."
8842 (let* ((entry (car entries))
8843 (name (car entry)) ;
8844 (type (nth 1 entry)) ; Must be bound for
8845 (class (nth 2 entry)) ; idlwave-routine-twin-compare
8846 (cnt 0)
8847 source type type-cons file alist syslibp key)
8848 (while (setq entry (pop entries))
8849 (incf cnt)
8850 (setq source (nth 3 entry)
8851 type (car source)
8852 type-cons (cons type (nth 3 source))
8853 file (idlwave-routine-source-file source))
8854
8855 ;; Make KEY to index entry properly
8856 (setq key (cond ((eq type 'system) type)
8857 (file (file-truename file))
8858 (t 'unresolved)))
8859
8860 ;; Check for an entry in the system library
8861 (if (and file
8862 (not syslibp)
8863 (idlwave-syslib-p file))
8864 (setq syslibp t))
8865
8866 ;; If there's more than one matching entry for the same file, just
8867 ;; append the type-cons to the type list.
8868 (if (setq entry (assoc key alist))
8869 (push type-cons (nth 2 entry))
8870 (push (list key file (list type-cons)) alist)))
8871
8872 (setq alist (nreverse alist))
8873
8874 (when syslibp
8875 ;; File is in system *library* - remove any 'system entry
8876 (setq alist (delq (assq 'system alist) alist)))
8877
8878 ;; If 'system remains and we've scanned the syslib, it's a builtin
8879 ;; (rather than a !DIR/lib/.pro file bundled as source).
8880 (when (and (idlwave-syslib-scanned-p)
8881 (setq entry (assoc 'system alist)))
8882 (setcar entry 'builtin))
8883 (sort alist 'idlwave-routine-twin-compare)))
8884
8885 (defvar type)
8886 (defvar class)
8887 (defvar idlwave-sort-prefer-buffer-info t
8888 "Internal variable used to influence `idlwave-routine-twin-compare'.")
8889
8890 (defmacro idlwave-xor (a b)
8891 `(and (or ,a ,b)
8892 (not (and ,a ,b))))
8893
8894 (defun idlwave-routine-entry-compare (a b)
8895 "Compare two routine info entries for sortiung. This is the general case.
8896 It first compates class, names, and type. If it turns out that A and B
8897 are twins (same name, class, and type), calls another routine which
8898 compares twins on the basis of their file names and path locations."
8899 (let ((name (car a)) (type (nth 1 a)) (class (nth 2 a)))
8900 (cond
8901 ((not (equal (idlwave-downcase-safe class)
8902 (idlwave-downcase-safe (nth 2 b))))
8903 ;; Class decides
8904 (cond ((null (nth 2 b)) nil)
8905 ((null class) t)
8906 (t (string< (downcase class) (downcase (nth 2 b))))))
8907 ((not (equal (downcase name) (downcase (car b))))
8908 ;; Name decides
8909 (string< (downcase name) (downcase (car b))))
8910 ((not (eq type (nth 1 b)))
8911 ;; Type decides
8912 (< (if (eq type 'fun) 1 0) (if (eq (nth 1 b) 'fun) 1 0)))
8913 (t
8914 ;; A and B are twins - so the decision is more complicated.
8915 ;; Call twin-compare with the proper arguments.
8916 (idlwave-routine-entry-compare-twins a b)))))
8917
8918 (defun idlwave-routine-entry-compare-twins (a b)
8919 "Compare two routine entries, under the assumption that they are
8920 twins. This basically calls `idlwave-routine-twin-compare' with the
8921 correct args."
8922 (let* ((name (car a)) (type (nth 1 a)) (class (nth 2 a)) ; needed outside
8923 (asrc (nth 3 a))
8924 (atype (car asrc))
8925 (bsrc (nth 3 b))
8926 (btype (car bsrc))
8927 (afile (idlwave-routine-source-file asrc))
8928 (bfile (idlwave-routine-source-file bsrc)))
8929 (idlwave-routine-twin-compare
8930 (if (stringp afile)
8931 (list (file-truename afile) afile (list atype))
8932 (list atype afile (list atype)))
8933 (if (stringp bfile)
8934 (list (file-truename bfile) bfile (list btype))
8935 (list btype bfile (list btype))))
8936 ))
8937
8938 (defun idlwave-routine-twin-compare (a b)
8939 "Compare two routine twin entries for sorting.
8940 In here, A and B are not normal routine info entries, but special
8941 lists (KEY FILENAME (TYPES...)).
8942 This expects NAME TYPE CLASS to be bound to the right values."
8943 (let* (;; Dis-assemble entries
8944 (akey (car a)) (bkey (car b))
8945 (afile (nth 1 a)) (bfile (nth 1 b))
8946 (atypes (nth 2 a)) (btypes (nth 2 b))
8947 ;; System routines?
8948 (asysp (memq akey '(builtin system)))
8949 (bsysp (memq bkey '(builtin system)))
8950 ;; Compiled routines?
8951 (acompp (memq 'compiled atypes))
8952 (bcompp (memq 'compiled btypes))
8953 ;; Unresolved?
8954 (aunresp (or (eq akey 'unresolved)
8955 (and acompp (not afile))))
8956 (bunresp (or (eq bkey 'unresolved)
8957 (and bcompp (not bfile))))
8958 ;; Buffer info available?
8959 (abufp (memq 'buffer atypes))
8960 (bbufp (memq 'buffer btypes))
8961 ;; On search path?
8962 (tpath-alist (idlwave-true-path-alist))
8963 (apathp (and (stringp akey)
8964 (assoc (file-name-directory akey) tpath-alist)))
8965 (bpathp (and (stringp bkey)
8966 (assoc (file-name-directory bkey) tpath-alist)))
8967 ;; How early on search path? High number means early since we
8968 ;; measure the tail of the path list
8969 (anpath (length (memq apathp tpath-alist)))
8970 (bnpath (length (memq bpathp tpath-alist)))
8971 ;; Look at file names
8972 (aname (if (stringp afile) (downcase (file-name-nondirectory afile)) ""))
8973 (bname (if (stringp bfile) (downcase (file-name-nondirectory bfile)) ""))
8974 (fname-re (if class (format "\\`%s__\\(%s\\|define\\)\\.pro\\'"
8975 (regexp-quote (downcase class))
8976 (regexp-quote (downcase name)))
8977 (format "\\`%s\\.pro" (regexp-quote (downcase name)))))
8978 ;; Is file name derived from the routine name?
8979 ;; Method file or class definition file?
8980 (anamep (string-match fname-re aname))
8981 (adefp (and class anamep (string= "define" (match-string 1 aname))))
8982 (bnamep (string-match fname-re bname))
8983 (bdefp (and class bnamep (string= "define" (match-string 1 bname)))))
8984
8985 ;; Now: follow JD's ideas about sorting. Looks really simple now,
8986 ;; doesn't it? The difficult stuff is hidden above...
8987 (cond
8988 ((idlwave-xor asysp bsysp) asysp) ; System entries first
8989 ((idlwave-xor aunresp bunresp) bunresp) ; Unresolved last
8990 ((and idlwave-sort-prefer-buffer-info
8991 (idlwave-xor abufp bbufp)) abufp) ; Buffers before non-buffers
8992 ((idlwave-xor acompp bcompp) acompp) ; Compiled entries
8993 ((idlwave-xor apathp bpathp) apathp) ; Library before non-library
8994 ((idlwave-xor anamep bnamep) anamep) ; Correct file names first
8995 ((and class anamep bnamep ; both file names match ->
8996 (idlwave-xor adefp bdefp)) bdefp) ; __define after __method
8997 ((> anpath bnpath) t) ; Who is first on path?
8998 (t nil)))) ; Default
8999
9000 (defun idlwave-routine-source-file (source)
9001 (if (nth 2 source)
9002 (expand-file-name (nth 1 source) (nth 2 source))
9003 (nth 1 source)))
9004
9005 (defun idlwave-downcase-safe (string)
9006 "Donwcase if string, else return unchanged."
9007 (if (stringp string)
9008 (downcase string)
9009 string))
9010
9011 (defun idlwave-count-eq (elt list)
9012 "How often is ELT in LIST?"
9013 (length (delq nil (mapcar (lambda (x) (eq x elt)) list))))
9014
9015 (defun idlwave-count-memq (elt alist)
9016 "How often is ELT a key in ALIST?"
9017 (length (delq nil (mapcar (lambda (x) (eq (car x) elt)) alist))))
9018
9019 (defun idlwave-syslib-p (file)
9020 "Non-nil if FILE is in the system library."
9021 (let* ((true-syslib (file-name-as-directory
9022 (file-truename
9023 (expand-file-name "lib" (idlwave-sys-dir)))))
9024 (true-file (file-truename file)))
9025 (string-match (concat "^" (regexp-quote true-syslib)) true-file)))
9026
9027 (defun idlwave-lib-p (file)
9028 "Non-nil if file is in the library"
9029 (let ((true-dir (file-name-directory (file-truename file))))
9030 (assoc true-dir (idlwave-true-path-alist))))
9031
9032 (defun idlwave-path-alist-add-flag (list-entry flag)
9033 "Add a flag to the path list entry, if not set."
9034 (let ((flags (cdr list-entry)))
9035 (add-to-list 'flags flag)
9036 (setcdr list-entry flags)))
9037
9038 (defun idlwave-path-alist-remove-flag (list-entry flag)
9039 "Remove a flag to the path list entry, if set."
9040 (let ((flags (delq flag (cdr list-entry))))
9041 (setcdr list-entry flags)))
9042
9043 (defun idlwave-true-path-alist ()
9044 "Return `idlwave-path-alist' alist with true-names.
9045 Info is cached, but relies on the functions setting `idlwave-path-alist'
9046 to reset the variable `idlwave-true-path-alist' to nil."
9047 (or idlwave-true-path-alist
9048 (setq idlwave-true-path-alist
9049 (mapcar (lambda(x) (cons
9050 (file-name-as-directory
9051 (file-truename
9052 (directory-file-name
9053 (car x))))
9054 (cdr x)))
9055 idlwave-path-alist))))
9056
9057 (defun idlwave-syslib-scanned-p ()
9058 "Non-nil if the system lib file !DIR/lib has been scanned."
9059 (let* ((true-syslib (file-name-as-directory
9060 (file-truename
9061 (expand-file-name "lib" (idlwave-sys-dir))))))
9062 (cdr (assoc true-syslib (idlwave-true-path-alist)))))
9063
9064 ;; ----------------------------------------------------------------------------
9065 ;;
9066 ;; Online Help display
9067
9068
9069 ;; ----------------------------------------------------------------------------
9070 ;;
9071 ;; Additions for use with imenu.el and func-menu.el
9072 ;; (pop-up a list of IDL units in the current file).
9073 ;;
9074
9075 (defun idlwave-prev-index-position ()
9076 "Search for the previous procedure or function.
9077 Return nil if not found. For use with imenu.el."
9078 (save-match-data
9079 (cond
9080 ((idlwave-find-key "\\<\\(pro\\|function\\)\\>" -1 'nomark))
9081 ;; ((idlwave-find-key idlwave-begin-unit-reg 1 'nomark)
9082 (t nil))))
9083
9084 (defun idlwave-unit-name ()
9085 "Return the unit name.
9086 Assumes that point is at the beginning of the unit as found by
9087 `idlwave-prev-index-position'."
9088 (forward-sexp 2)
9089 (forward-sexp -1)
9090 (let ((begin (point)))
9091 (re-search-forward
9092 "[a-zA-Z_][a-zA-Z0-9$_]+\\(::[a-zA-Z_][a-zA-Z0-9$_]+\\)?")
9093 (if (fboundp 'buffer-substring-no-properties)
9094 (buffer-substring-no-properties begin (point))
9095 (buffer-substring begin (point)))))
9096
9097 (defalias 'idlwave-function-menu
9098 (condition-case nil
9099 (progn
9100 (require 'func-menu)
9101 'function-menu)
9102 (error (condition-case nil
9103 (progn
9104 (require 'imenu)
9105 'imenu)
9106 (error nil)))))
9107
9108 ;; Here we hack func-menu.el in order to support this new mode.
9109 ;; The latest versions of func-menu.el already have this stuff in, so
9110 ;; we hack only if it is not already there.
9111 (when (fboundp 'eval-after-load)
9112 (eval-after-load "func-menu"
9113 '(progn
9114 (or (assq 'idlwave-mode fume-function-name-regexp-alist)
9115 (not (boundp 'fume-function-name-regexp-idl)) ; avoid problems
9116 (setq fume-function-name-regexp-alist
9117 (cons '(idlwave-mode . fume-function-name-regexp-idl)
9118 fume-function-name-regexp-alist)))
9119 (or (assq 'idlwave-mode fume-find-function-name-method-alist)
9120 (not (fboundp 'fume-find-next-idl-function-name)) ; avoid problems
9121 (setq fume-find-function-name-method-alist
9122 (cons '(idlwave-mode . fume-find-next-idl-function-name)
9123 fume-find-function-name-method-alist))))))
9124
9125 (defun idlwave-edit-in-idlde ()
9126 "Edit the current file in IDL Development environment."
9127 (interactive)
9128 (start-process "idldeclient" nil
9129 idlwave-shell-explicit-file-name "-c" "-e"
9130 (buffer-file-name)))
9131
9132 (defvar idlwave-help-use-assistant)
9133 (defun idlwave-launch-idlhelp ()
9134 "Start the IDLhelp application."
9135 (interactive)
9136 (if idlwave-help-use-assistant
9137 (idlwave-help-assistant-raise)
9138 (start-process "idlhelp" nil idlwave-help-application)))
9139
9140 ;; Menus - using easymenu.el
9141 (defvar idlwave-mode-menu-def
9142 `("IDLWAVE"
9143 ["PRO/FUNC menu" idlwave-function-menu t]
9144 ("Motion"
9145 ["Subprogram Start" idlwave-beginning-of-subprogram t]
9146 ["Subprogram End" idlwave-end-of-subprogram t]
9147 ["Block Start" idlwave-beginning-of-block t]
9148 ["Block End" idlwave-end-of-block t]
9149 ["Up Block" idlwave-backward-up-block t]
9150 ["Down Block" idlwave-down-block t]
9151 ["Skip Block Backward" idlwave-backward-block t]
9152 ["Skip Block Forward" idlwave-forward-block t])
9153 ("Mark"
9154 ["Subprogram" idlwave-mark-subprogram t]
9155 ["Block" idlwave-mark-block t]
9156 ["Header" idlwave-mark-doclib t])
9157 ("Format"
9158 ["Indent Entire Statement" idlwave-indent-statement
9159 :active t :keys "C-u \\[indent-for-tab-command]" ]
9160 ["Indent Subprogram" idlwave-indent-subprogram t]
9161 ["(Un)Comment Region" idlwave-toggle-comment-region t]
9162 ["Continue/Split line" idlwave-split-line t]
9163 "--"
9164 ["Toggle Auto Fill" idlwave-auto-fill-mode :style toggle
9165 :selected (symbol-value idlwave-fill-function)])
9166 ("Templates"
9167 ["Procedure" idlwave-procedure t]
9168 ["Function" idlwave-function t]
9169 ["Doc Header" idlwave-doc-header t]
9170 ["Log" idlwave-doc-modification t]
9171 "--"
9172 ["Case" idlwave-case t]
9173 ["For" idlwave-for t]
9174 ["Repeat" idlwave-repeat t]
9175 ["While" idlwave-while t]
9176 "--"
9177 ["Close Block" idlwave-close-block t])
9178 ("Completion"
9179 ["Complete" idlwave-complete t]
9180 ("Complete Specific"
9181 ["1 Procedure Name" (idlwave-complete 'procedure) t]
9182 ["2 Procedure Keyword" (idlwave-complete 'procedure-keyword) t]
9183 "--"
9184 ["3 Function Name" (idlwave-complete 'function) t]
9185 ["4 Function Keyword" (idlwave-complete 'function-keyword) t]
9186 "--"
9187 ["5 Procedure Method Name" (idlwave-complete 'procedure-method) t]
9188 ["6 Procedure Method Keyword" (idlwave-complete 'procedure-method-keyword) t]
9189 "--"
9190 ["7 Function Method Name" (idlwave-complete 'function-method) t]
9191 ["8 Function Method Keyword" (idlwave-complete 'function-method-keyword) t]
9192 "--"
9193 ["9 Class Name" idlwave-complete-class t]))
9194 ("Routine Info"
9195 ["Show Routine Info" idlwave-routine-info t]
9196 ["Online Context Help" idlwave-context-help t]
9197 "--"
9198 ["Find Routine Source" idlwave-find-module t]
9199 ["Resolve Routine" idlwave-resolve (featurep 'idlw-shell)]
9200 "--"
9201 ["Update Routine Info" idlwave-update-routine-info t]
9202 ["Rescan XML Help Catalog" idlwave-convert-xml-system-routine-info t]
9203 "--"
9204 "IDL User Catalog"
9205 ["Select Catalog Directories" (idlwave-create-user-catalog-file nil) t]
9206 ["Scan Directories" (idlwave-update-routine-info '(16))
9207 (and idlwave-path-alist (not idlwave-catalog-process))]
9208 ["Scan Directories &" (idlwave-update-routine-info '(64))
9209 (and idlwave-path-alist (not idlwave-catalog-process))]
9210 "--"
9211 "Routine Shadows"
9212 ["Check Current Buffer" idlwave-list-buffer-load-path-shadows t]
9213 ["Check Compiled Routines" idlwave-list-shell-load-path-shadows t]
9214 ["Check Everything" idlwave-list-all-load-path-shadows t])
9215 ("Misc"
9216 ["Kill auto-created buffers" idlwave-kill-autoloaded-buffers t]
9217 "--"
9218 ["Insert TAB character" idlwave-hard-tab t])
9219 "--"
9220 ("External"
9221 ["Start IDL shell" idlwave-shell t]
9222 ["Edit file in IDLDE" idlwave-edit-in-idlde t]
9223 ["Launch IDL Help" idlwave-launch-idlhelp t])
9224 "--"
9225 ("Customize"
9226 ["Browse IDLWAVE Group" idlwave-customize t]
9227 "--"
9228 ["Build Full Customize Menu" idlwave-create-customize-menu
9229 (fboundp 'customize-menu-create)])
9230 ("Documentation"
9231 ["Describe Mode" describe-mode t]
9232 ["Abbreviation List" idlwave-list-abbrevs t]
9233 "--"
9234 ["Commentary in idlwave.el" idlwave-show-commentary t]
9235 ["Commentary in idlw-shell.el" idlwave-shell-show-commentary t]
9236 "--"
9237 ["Info" idlwave-info t]
9238 "--"
9239 ["Help with Topic" idlwave-help-assistant-help-with-topic
9240 idlwave-help-use-assistant]
9241 ["Launch IDL Help" idlwave-launch-idlhelp t])))
9242
9243 (defvar idlwave-mode-debug-menu-def
9244 '("Debug"
9245 ["Start IDL shell" idlwave-shell t]
9246 ["Save and .RUN buffer" idlwave-shell-save-and-run
9247 (and (boundp 'idlwave-shell-automatic-start)
9248 idlwave-shell-automatic-start)]))
9249
9250 (if (or (featurep 'easymenu) (load "easymenu" t))
9251 (progn
9252 (easy-menu-define idlwave-mode-menu idlwave-mode-map
9253 "IDL and WAVE CL editing menu"
9254 idlwave-mode-menu-def)
9255 (easy-menu-define idlwave-mode-debug-menu idlwave-mode-map
9256 "IDL and WAVE CL editing menu"
9257 idlwave-mode-debug-menu-def)))
9258
9259 (defun idlwave-customize ()
9260 "Call the customize function with idlwave as argument."
9261 (interactive)
9262 ;; Try to load the code for the shell, so that we can customize it
9263 ;; as well.
9264 (or (featurep 'idlw-shell)
9265 (load "idlw-shell" t))
9266 (customize-browse 'idlwave))
9267
9268 (defun idlwave-create-customize-menu ()
9269 "Create a full customization menu for IDLWAVE, insert it into the menu."
9270 (interactive)
9271 (if (fboundp 'customize-menu-create)
9272 (progn
9273 ;; Try to load the code for the shell, so that we can customize it
9274 ;; as well.
9275 (or (featurep 'idlw-shell)
9276 (load "idlw-shell" t))
9277 (easy-menu-change
9278 '("IDLWAVE") "Customize"
9279 `(["Browse IDLWAVE group" idlwave-customize t]
9280 "--"
9281 ,(customize-menu-create 'idlwave)
9282 ["Set" Custom-set t]
9283 ["Save" Custom-save t]
9284 ["Reset to Current" Custom-reset-current t]
9285 ["Reset to Saved" Custom-reset-saved t]
9286 ["Reset to Standard Settings" Custom-reset-standard t]))
9287 (message "\"IDLWAVE\"-menu now contains full customization menu"))
9288 (error "Cannot expand menu (outdated version of cus-edit.el)")))
9289
9290 (defun idlwave-show-commentary ()
9291 "Use the finder to view the file documentation from `idlwave.el'."
9292 (interactive)
9293 (require 'finder)
9294 (finder-commentary "idlwave.el"))
9295
9296 (defun idlwave-shell-show-commentary ()
9297 "Use the finder to view the file documentation from `idlw-shell.el'."
9298 (interactive)
9299 (require 'finder)
9300 (finder-commentary "idlw-shell.el"))
9301
9302 (defun idlwave-info ()
9303 "Read documentation for IDLWAVE in the info system."
9304 (interactive)
9305 (require 'info)
9306 (Info-goto-node "(idlwave)"))
9307
9308 (defun idlwave-list-abbrevs (arg)
9309 "Show the code abbreviations define in IDLWAVE mode.
9310 This lists all abbrevs where the replacement text differs from the input text.
9311 These are the ones the users want to learn to speed up their writing.
9312
9313 The function does *not* list abbrevs which replace a word with itself
9314 to call a hook. These hooks are used to change the case of words or
9315 to blink the matching `begin', and the user does not need to know them.
9316
9317 With arg, list all abbrevs with the corresponding hook.
9318
9319 This function was written since `list-abbrevs' looks terrible for IDLWAVE mode."
9320
9321 (interactive "P")
9322 (let ((table (symbol-value 'idlwave-mode-abbrev-table))
9323 abbrevs
9324 str rpl func fmt (len-str 0) (len-rpl 0))
9325 (mapatoms
9326 (lambda (sym)
9327 (if (symbol-value sym)
9328 (progn
9329 (setq str (symbol-name sym)
9330 rpl (symbol-value sym)
9331 func (symbol-function sym))
9332 (if arg
9333 (setq func (prin1-to-string func))
9334 (if (and (listp func) (stringp (nth 2 func)))
9335 (setq rpl (concat "EVAL: " (nth 2 func))
9336 func "")
9337 (setq func "")))
9338 (if (or arg (not (string= rpl str)))
9339 (progn
9340 (setq len-str (max len-str (length str)))
9341 (setq len-rpl (max len-rpl (length rpl)))
9342 (setq abbrevs (cons (list str rpl func) abbrevs)))))))
9343 table)
9344 ;; sort the list
9345 (setq abbrevs (sort abbrevs (lambda (a b) (string< (car a) (car b)))))
9346 ;; Make the format
9347 (setq fmt (format "%%-%ds %%-%ds %%s\n" len-str len-rpl))
9348 (with-output-to-temp-buffer "*Help*"
9349 (if arg
9350 (progn
9351 (princ "Abbreviations and Actions in IDLWAVE-Mode\n")
9352 (princ "=========================================\n\n")
9353 (princ (format fmt "KEY" "REPLACE" "HOOK"))
9354 (princ (format fmt "---" "-------" "----")))
9355 (princ "Code Abbreviations and Templates in IDLWAVE-Mode\n")
9356 (princ "================================================\n\n")
9357 (princ (format fmt "KEY" "ACTION" ""))
9358 (princ (format fmt "---" "------" "")))
9359 (mapcar
9360 (lambda (list)
9361 (setq str (car list)
9362 rpl (nth 1 list)
9363 func (nth 2 list))
9364 (princ (format fmt str rpl func)))
9365 abbrevs)))
9366 ;; Make sure each abbreviation uses only one display line
9367 (save-excursion
9368 (set-buffer "*Help*")
9369 (setq truncate-lines t)))
9370
9371 ;; Add .pro files to speedbar for support, if it's loaded
9372 (eval-after-load "speedbar" '(speedbar-add-supported-extension ".pro"))
9373
9374 ;; Set an idle timer to load the routine info.
9375 ;; Will only work on systems which support this.
9376 (or idlwave-routines (idlwave-start-load-rinfo-timer))
9377
9378 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.[Pp][Rr][Oo]\\'" . idlwave-mode))
9379
9380 ;; Run the hook
9381 (run-hooks 'idlwave-load-hook)
9382
9383 (provide 'idlwave)
9384
9385 ;; arch-tag: f77f3b0c-c37c-424f-a328-0886fd42b6fb
9386 ;;; idlwave.el ends here