]> code.delx.au - gnu-emacs/blob - lisp/progmodes/idlwave.el
Fix typos.
[gnu-emacs] / lisp / progmodes / idlwave.el
1 ;; idlwave.el --- IDL editing mode for GNU Emacs
2
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 ;; 2008, 2009 Free Software Foundation, Inc.
5
6 ;; Authors: J.D. Smith <jdsmith@as.arizona.edu>
7 ;; Carsten Dominik <dominik@science.uva.nl>
8 ;; Chris Chase <chase@att.com>
9 ;; Maintainer: J.D. Smith <jdsmith@as.arizona.edu>
10 ;; Version: 6.1_em22
11 ;; Keywords: languages
12
13 ;; This file is part of GNU Emacs.
14
15 ;; GNU Emacs is free software: you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation, either version 3 of the License, or
18 ;; (at your option) any later version.
19
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
24
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27
28 ;;; Commentary:
29
30 ;; IDLWAVE enables feature-rich development and interaction with IDL,
31 ;; the Interactive Data Language. It provides a compelling,
32 ;; full-featured alternative to the IDLDE development environment
33 ;; bundled with IDL.
34
35 ;; In the remotely distant past, based on pascal.el, though bears
36 ;; little resemblance to it now.
37 ;;
38 ;; Incorporates many ideas, such as abbrevs, action routines, and
39 ;; continuation line indenting, from wave.el.
40 ;; wave.el original written by Lubos Pochman, Precision Visuals, Boulder.
41 ;;
42 ;; See the mode description ("C-h m" in idlwave-mode or "C-h f idlwave-mode")
43 ;; for features, key bindings, and info.
44 ;; Also, Info format documentation is available with `M-x idlwave-info'
45 ;;
46 ;; New versions of IDLWAVE, documentation, and more information
47 ;; available from:
48 ;; http://idlwave.org
49 ;;
50 ;; INSTALLATION
51 ;; ============
52 ;;
53 ;; Follow the instructions in the INSTALL file of the distribution.
54 ;; In short, put this file on your load path and add the following
55 ;; lines to your .emacs file:
56 ;;
57 ;; (autoload 'idlwave-mode "idlwave" "IDLWAVE Mode" t)
58 ;; (autoload 'idlwave-shell "idlw-shell" "IDLWAVE Shell" t)
59 ;; (setq auto-mode-alist (cons '("\\.pro\\'" . idlwave-mode) auto-mode-alist))
60 ;;
61 ;;
62 ;; SOURCE
63 ;; ======
64 ;;
65 ;; The newest version of this file is available from the maintainer's
66 ;; Webpage:
67 ;;
68 ;; http://idlwave.org
69 ;;
70 ;; DOCUMENTATION
71 ;; =============
72 ;;
73 ;; IDLWAVE is documented online in info format. A printable version
74 ;; of the documentation is available from the maintainers webpage (see
75 ;; SOURCE).
76 ;;
77 ;;
78 ;; ACKNOWLEDGMENTS
79 ;; ===============
80 ;;
81 ;; Thanks to the following people for their contributions and comments:
82 ;;
83 ;; Ulrik Dickow <dickow_at_nbi.dk>
84 ;; Eric E. Dors <edors_at_lanl.gov>
85 ;; Stein Vidar H. Haugan <s.v.h.haugan_at_astro.uio.no>
86 ;; David Huenemoerder <dph_at_space.mit.edu>
87 ;; Kevin Ivory <Kevin.Ivory_at_linmpi.mpg.de>
88 ;; Dick Jackson <dick_at_d-jackson.com>
89 ;; Xuyong Liu <liu_at_stsci.edu>
90 ;; Simon Marshall <Simon.Marshall_at_esrin.esa.it>
91 ;; Laurent Mugnier <mugnier_at_onera.fr>
92 ;; Lubos Pochman <lubos_at_rsinc.com>
93 ;; Bob Portmann <portmann_at_al.noaa.gov>
94 ;; Patrick M. Ryan <pat_at_jaameri.gsfc.nasa.gov>
95 ;; Marty Ryba <ryba_at_ll.mit.edu>
96 ;; Paul Sorenson <aardvark62_at_msn.com>
97 ;; Phil Sterne <sterne_at_dublin.llnl.gov>
98 ;; Phil Williams <williams_at_irc.chmcc.org>
99 ;;
100 ;; CUSTOMIZATION:
101 ;; =============
102 ;;
103 ;; IDLWAVE has extensive customize support; to learn about the
104 ;; variables which control the mode's behavior, use `M-x
105 ;; idlwave-customize'.
106 ;;
107 ;; You can set your own preferred values with Customize, or with Lisp
108 ;; code in .emacs. For an example of what to put into .emacs, check
109 ;; the TexInfo documentation or see a complete .emacs available at the
110 ;; website.
111 ;;
112 ;; KNOWN PROBLEMS:
113 ;; ==============
114 ;;
115 ;; IDLWAVE support for the IDL-derived PV-WAVE CL language of Visual
116 ;; Numerics, Inc. is growing less and less complete as the two
117 ;; languages grow increasingly apart. The mode probably shouldn't
118 ;; even have "WAVE" in its title, but it's catchy, and was required
119 ;; to avoid conflict with the CORBA idl.el mode. Caveat WAVEor.
120 ;;
121 ;; Moving the point backwards in conjunction with abbrev expansion
122 ;; does not work as I would like it, but this is a problem with
123 ;; emacs abbrev expansion done by the self-insert-command. It ends
124 ;; up inserting the character that expanded the abbrev after moving
125 ;; point backward, e.g., "\cl" expanded with a space becomes
126 ;; "LONG( )" with point before the close paren. This is solved by
127 ;; using a temporary function in `post-command-hook' - not pretty,
128 ;; but it works.
129 ;;
130 ;; Tabs and spaces are treated equally as whitespace when filling a
131 ;; comment paragraph. To accomplish this, tabs are permanently
132 ;; replaced by spaces in the text surrounding the paragraph, which
133 ;; may be an undesirable side-effect. Replacing tabs with spaces is
134 ;; limited to comments only and occurs only when a comment
135 ;; paragraph is filled via `idlwave-fill-paragraph'.
136 ;;
137 ;; Muti-statement lines (using "&") on block begin and end lines can
138 ;; ruin the formatting. For example, multiple end statements on a
139 ;; line: endif & endif. Using "&" outside of block begin/end lines
140 ;; should be okay.
141 ;;
142 ;; Determining the expression at point for printing and other
143 ;; examination commands is somewhat rough: currently only fairly
144 ;; simple entities are found. You can always drag-select or examine
145 ;; a pre-selected region.
146 ;;
147 ;; When forcing completion of method keywords, the initial
148 ;; query for a method has multiple entries for some methods. Would
149 ;; be too difficult to fix this hardly used case.
150 ;;
151 \f
152 ;;; Code:
153
154
155 (eval-when-compile (require 'cl))
156 (require 'idlw-help)
157
158 ;; For XEmacs
159 (unless (fboundp 'line-beginning-position)
160 (defalias 'line-beginning-position 'point-at-bol))
161 (unless (fboundp 'line-end-position)
162 (defalias 'line-end-position 'point-at-eol))
163 (unless (fboundp 'char-valid-p)
164 (defalias 'char-valid-p 'characterp))
165 (unless (fboundp 'match-string-no-properties)
166 (defalias 'match-string-no-properties 'match-string))
167
168 (if (not (fboundp 'cancel-timer))
169 (condition-case nil
170 (require 'timer)
171 (error nil)))
172
173 (declare-function idlwave-shell-get-path-info "idlw-shell")
174 (declare-function idlwave-shell-temp-file "idlw-shell")
175 (declare-function idlwave-shell-is-running "idlw-shell")
176 (declare-function widget-value "wid-edit" (widget))
177 (declare-function comint-dynamic-complete-filename "comint" ())
178
179 (defgroup idlwave nil
180 "Major mode for editing IDL .pro files."
181 :tag "IDLWAVE"
182 :link '(url-link :tag "Home Page"
183 "http://idlwave.org")
184 :link '(emacs-commentary-link :tag "Commentary in idlw-shell.el"
185 "idlw-shell.el")
186 :link '(emacs-commentary-link :tag "Commentary in idlwave.el" "idlwave.el")
187 :link '(custom-manual "(idlwave)Top")
188 :prefix "idlwave"
189 :group 'languages)
190
191
192 ;;; Variables for indentation behavior ---------------------------------------
193
194 (defgroup idlwave-code-formatting nil
195 "Indentation and formatting options for IDLWAVE mode."
196 :group 'idlwave)
197
198 (defcustom idlwave-main-block-indent 2
199 "*Extra indentation for the main block of code.
200 That is the block between the FUNCTION/PRO statement and the END
201 statement for that program unit."
202 :group 'idlwave-code-formatting
203 :type 'integer)
204
205 (defcustom idlwave-block-indent 3
206 "*Extra indentation applied to block lines.
207 If you change this, you probably also want to change `idlwave-end-offset'."
208 :group 'idlwave-code-formatting
209 :type 'integer)
210
211 (defcustom idlwave-end-offset -3
212 "*Extra indentation applied to block END lines.
213 A value equal to negative `idlwave-block-indent' will make END lines
214 line up with the block BEGIN lines."
215 :group 'idlwave-code-formatting
216 :type 'integer)
217
218 (defcustom idlwave-continuation-indent 3
219 "*Extra indentation applied to continuation lines.
220 This extra offset applies to the first of a set of continuation lines.
221 The following lines receive the same indentation as the first."
222 :group 'idlwave-code-formatting
223 :type 'integer)
224
225 (defcustom idlwave-max-extra-continuation-indent 40
226 "*Maximum additional indentation for special continuation indent.
227 Several special indentations are tried to help line up continuation
228 lines in routine calls or definitions, other statements with
229 parentheses, or assignment statements. This variable specifies a
230 maximum amount by which this special indentation can exceed the
231 standard continuation indentation, otherwise defaulting to a fixed
232 offset. Set to 0 to effectively disable all special continuation
233 indentation, or to a large number (like 100) to enable it in all
234 cases. See also `idlwave-indent-to-open-paren', which can override
235 this variable."
236 :group 'idlwave-code-formatting
237 :type 'integer)
238
239 (defcustom idlwave-indent-to-open-paren t
240 "*Non-nil means, indent continuation lines to innermost open parenthesis.
241 This indentation occurs even if otherwise disallowed by
242 `idlwave-max-extra-continuation-indent'. Matching parens and the
243 interleaving args are lined up. Example:
244
245 x = function_a(function_b(function_c( a, b, [1,2,3, $
246 4,5,6 $
247 ], $
248 c, d $
249 )))
250
251 When this variable is nil, paren alignment may still occur, based on
252 the value of `idlwave-max-extra-continuation-indent', which, if zero,
253 would yield:
254
255 x = function_a(function_b(function_c( a, b, [1,2,3, $
256 4,5,6 $
257 ], $
258 c, d $
259 )))"
260 :group 'idlwave-code-formatting
261 :type 'boolean)
262
263 (defcustom idlwave-indent-parens-nested nil
264 "*Non-nil means, indent continuation lines with parens by nesting
265 lines at consecutively deeper levels."
266 :group 'idlwave-code-formatting
267 :type 'boolean)
268
269
270 (defcustom idlwave-hanging-indent t
271 "*If set non-nil then comment paragraphs are indented under the
272 hanging indent given by `idlwave-hang-indent-regexp' match in the first line
273 of the paragraph."
274 :group 'idlwave-code-formatting
275 :type 'boolean)
276
277 (defcustom idlwave-hang-indent-regexp "- "
278 "*Regular expression matching the position of the hanging indent
279 in the first line of a comment paragraph. The size of the indent
280 extends to the end of the match for the regular expression."
281 :group 'idlwave-code-formatting
282 :type 'regexp)
283
284 (defcustom idlwave-use-last-hang-indent nil
285 "*If non-nil then use last match on line for `idlwave-indent-regexp'."
286 :group 'idlwave-code-formatting
287 :type 'boolean)
288
289 (defcustom idlwave-fill-comment-line-only t
290 "*If non-nil then auto fill will only operate on comment lines."
291 :group 'idlwave-code-formatting
292 :type 'boolean)
293
294 (defcustom idlwave-auto-fill-split-string t
295 "*If non-nil then auto fill will split strings with the IDL `+' operator.
296 When the line end falls within a string, string concatenation with the
297 '+' operator will be used to distribute a long string over lines.
298 If nil and a string is split then a terminal beep and warning are issued.
299
300 This variable is ignored when `idlwave-fill-comment-line-only' is
301 non-nil, since in this case code is not auto-filled."
302 :group 'idlwave-code-formatting
303 :type 'boolean)
304
305 (defcustom idlwave-split-line-string t
306 "*If non-nil then `idlwave-split-line' will split strings with `+'.
307 When the splitting point of a line falls inside a string, split the string
308 using the `+' string concatenation operator. If nil and a string is
309 split then a terminal beep and warning are issued."
310 :group 'idlwave-code-formatting
311 :type 'boolean)
312
313 (defcustom idlwave-no-change-comment ";;;"
314 "*The indentation of a comment that starts with this regular
315 expression will not be changed. Note that the indentation of a comment
316 at the beginning of a line is never changed."
317 :group 'idlwave-code-formatting
318 :type 'string)
319
320 (defcustom idlwave-begin-line-comment nil
321 "*A comment anchored at the beginning of line.
322 A comment matching this regular expression will not have its
323 indentation changed. If nil the default is \"^;\", i.e., any line
324 beginning with a \";\". Expressions for comments at the beginning of
325 the line should begin with \"^\"."
326 :group 'idlwave-code-formatting
327 :type '(choice (const :tag "Any line beginning with `;'" nil)
328 'regexp))
329
330 (defcustom idlwave-code-comment ";;[^;]"
331 "*A comment that starts with this regular expression on a line by
332 itself is indented as if it is a part of IDL code. As a result if
333 the comment is not preceded by whitespace it is unchanged."
334 :group 'idlwave-code-formatting
335 :type 'regexp)
336
337 ;; Comments not matching any of the above will be indented as a
338 ;; right-margin comment, i.e., to a minimum of `comment-column'.
339
340 ;;; Routine Info and Completion ---------------------------------------
341
342 (defgroup idlwave-routine-info nil
343 "Routine Info options for IDLWAVE mode."
344 :group 'idlwave)
345
346 (defcustom idlwave-use-library-catalogs t
347 "*Non-nil means search the IDL path for library catalog files.
348
349 These files, named .idlwave_catalog, document routine information for
350 individual directories and libraries of IDL .pro files. Many popular
351 libraries come with catalog files by default, so leaving this on is
352 usually a good idea."
353 :group 'idlwave-routine-info
354 :type 'boolean)
355
356 (defcustom idlwave-init-rinfo-when-idle-after 10
357 "*Seconds of idle time before routine info is automatically initialized.
358 Initializing the routine info can take a long time, in particular if a
359 large number of library catalogs are involved. When Emacs is idle for
360 more than the number of seconds specified by this variable, it starts
361 the initialization. The process is split into five steps, in order to
362 keep work interruption as short as possible. If one of the steps
363 finishes, and no user input has arrived in the mean time, initialization
364 proceeds immediately to the next step. A good value for this variable
365 is about 1/3 of the time initialization take in your setup. So if you
366 have a fast machine and no problems with a slow network connection,
367 don't hesitate to set this to 2 seconds. A value of 0 means, don't
368 initialize automatically, but instead wait until routine information is
369 needed, and initialize then."
370 :group 'idlwave-routine-info
371 :type 'number)
372
373 (defcustom idlwave-scan-all-buffers-for-routine-info t
374 "*Non-nil means, scan buffers for IDL programs when updating info.
375 The scanning is done by the command `idlwave-update-routine-info'.
376 The following values are allowed:
377
378 nil Don't scan any buffers.
379 t Scan all `idlwave-mode' buffers in the current editing session.
380 current Scan only the current buffer, but no other buffers."
381 :group 'idlwave-routine-info
382 :type '(choice
383 (const :tag "No buffer" nil)
384 (const :tag "All buffers" t)
385 (const :tag "Current buffer only" 'current)))
386
387 (defcustom idlwave-query-shell-for-routine-info t
388 "*Non-nil means query the shell for info about compiled routines.
389 Querying the shell is useful to get information about compiled modules,
390 and it is turned on by default. However, when you have a complete library
391 scan, this is not necessary."
392 :group 'idlwave-routine-info
393 :type 'boolean)
394
395 (defcustom idlwave-auto-routine-info-updates
396 '(find-file save-buffer kill-buffer compile-buffer)
397 "*Controls under what circumstances routine info is updated automatically.
398 Possible values:
399 nil Never
400 t All available
401 \(...) A list of circumstances. Allowed members are:
402 find-file Add info for new IDLWAVE buffers.
403 save-buffer Update buffer info when buffer is saved
404 kill-buffer Remove buffer info when buffer gets killed
405 compile-buffer Update shell info after `idlwave-shell-save-and...'"
406 :group 'idlwave-routine-info
407 :type '(choice
408 (const :tag "Never" nil)
409 (const :tag "As often as possible" t)
410 (set :tag "Checklist" :greedy t
411 (const :tag "When visiting a file" find-file)
412 (const :tag "When saving a buffer" save-buffer)
413 (const :tag "After a buffer was killed" kill-buffer)
414 (const :tag "After a buffer was compiled successfully, update shell info" compile-buffer))))
415
416 (defcustom idlwave-rinfo-max-source-lines 5
417 "*Maximum number of source files displayed in the Routine Info window.
418 When an integer, it is the maximum number of source files displayed.
419 A value of t means to show all source files."
420 :group 'idlwave-routine-info
421 :type 'integer)
422
423 (defcustom idlwave-library-path nil
424 "Library path for Windows and MacOS (OS9). Not needed under UNIX.
425 When selecting the directories to scan for IDL user catalog routine
426 info, IDLWAVE can, under UNIX, query the shell for the exact search
427 path \(the value of !PATH). However, under Windows and MacOS
428 \(pre-OSX), the IDLWAVE shell does not work. In this case, this
429 variable can be set to specify the paths where IDLWAVE can find PRO
430 files. The shell will only be asked for a list of paths when this
431 variable is nil. The value is a list of directories. A directory
432 preceeded by a `+' will be searched recursively. If you set this
433 variable on a UNIX system, the shell will not be queried. See also
434 `idlwave-system-directory'."
435 :group 'idlwave-routine-info
436 :type '(repeat (directory)))
437
438 (defcustom idlwave-system-directory ""
439 "The IDL system directory for Windows and MacOS. Not needed under
440 UNIX. Set this to the value of the `!DIR' system variable in IDL.
441 IDLWAVE uses this to find out which of the library routines belong to
442 the official system library. All files inside the `lib' subdirectory
443 are considered system library files - so don't install private stuff
444 in this directory. On UNIX systems, IDLWAVE queries the shell for the
445 value of `!DIR'. See also `idlwave-library-path'."
446 :group 'idlwave-routine-info
447 :type 'directory)
448
449 ;; Configuration files
450 (defcustom idlwave-config-directory
451 (convert-standard-filename "~/.idlwave")
452 "*Directory for configuration files and user-library catalog."
453 :group 'idlwave-routine-info
454 :type 'file)
455
456 (defvar idlwave-user-catalog-file "idlusercat.el")
457 (defvar idlwave-xml-system-rinfo-converted-file "idl_xml_rinfo.el")
458 (defvar idlwave-path-file "idlpath.el")
459
460 (defvar idlwave-libinfo-file nil
461 "*Obsolete variable, no longer used.")
462
463 (defcustom idlwave-special-lib-alist nil
464 "Alist of regular expressions matching special library directories.
465 When listing routine source locations, IDLWAVE gives a short hint where
466 the file defining the routine is located. By default it lists `SystemLib'
467 for routines in the system library `!DIR/lib' and `Library' for anything
468 else. This variable can define additional types. The car of each entry
469 is a regular expression matching the file name (they normally will match
470 on the path). The cdr is the string to be used as identifier. Max 10
471 chars are allowed."
472 :group 'idlwave-routine-info
473 :type '(repeat
474 (cons regexp string)))
475
476 (defcustom idlwave-auto-write-paths t
477 "Write out path (!PATH) and system directory (!DIR) info automatically.
478 Path info is needed to locate library catalog files. If non-nil,
479 whenever the path-list changes as a result of shell-query, etc., it is
480 written to file. Otherwise, the menu option \"Write Paths\" can be
481 used to force a write."
482 :group 'idlwave-routine-info
483 :type 'boolean)
484
485 (defgroup idlwave-completion nil
486 "Completion options for IDLWAVE mode."
487 :prefix "idlwave"
488 :group 'idlwave)
489
490 (eval-and-compile
491 (defconst idlwave-tmp
492 '(choice :tag "by applying the function"
493 (const upcase)
494 (const downcase)
495 (const capitalize)
496 (const preserve)
497 (symbol :tag "Other"))))
498
499 (defcustom idlwave-completion-case '((routine . upcase)
500 (keyword . upcase)
501 (class . preserve)
502 (method . preserve))
503 "Association list setting the case of completed words.
504
505 This variable determines the case (UPPER/lower/Capitalized...) of
506 words inserted into the buffer by completion. The preferred case can
507 be specified separately for routine names, keywords, classes and
508 methods.
509 This alist should therefore have entries for `routine' (normal
510 functions and procedures, i.e. non-methods), `keyword', `class', and
511 `method'. Plausible values are
512
513 upcase upcase whole word, like `BOX_CURSOR'
514 downcase downcase whole word, like `read_ppm'
515 capitalize capitalize each part, like `Widget_Control'
516 preserve preserve case as is, like `IDLgrView'
517
518 The value can also be any Emacs Lisp function which transforms the
519 case of characters in a string.
520
521 A value of `preserve' means that the case of the completed word is
522 identical to the way it was written in the definition statement of the
523 routine. This was implemented to allow for mixed-case completion, in
524 particular of object classes and methods.
525 If a completable word is defined in multiple locations, the meaning of
526 `preserve' is not unique since the different definitions might be
527 cased differently. Therefore IDLWAVE always takes the case of the
528 *first* definition it encounters during routine info collection and
529 uses the case derived from it consistently.
530
531 Note that a lowercase-only string in the buffer will always be completed in
532 lower case (but see the variable `idlwave-completion-force-default-case').
533
534 After changing this variable, you need to either restart Emacs or press
535 `C-u C-c C-i' to update the internal lists."
536 :group 'idlwave-completion
537 :type `(repeat
538 (cons (symbol :tag "Derive completion case for")
539 ,idlwave-tmp)))
540
541 (defcustom idlwave-completion-force-default-case nil
542 "*Non-nil means, completion will always honor `idlwave-completion-case'.
543 When nil, only the completion of a mixed case or upper case string
544 will honor the default settings in `idlwave-completion-case', while
545 the completion of lower case strings will be completed entirely in
546 lower case."
547 :group 'idlwave-completion
548 :type 'boolean)
549
550 (defcustom idlwave-complete-empty-string-as-lower-case nil
551 "*Non-nil means, the empty string is considered downcase for completion.
552 The case of what is already in the buffer determines the case of completions.
553 When this variable is non-nil, the empty string is considered to be downcase.
554 Completing on the empty string then offers downcase versions of the possible
555 completions."
556 :group 'idlwave-completion
557 :type 'boolean)
558
559 (defvar idlwave-default-completion-case-is-down nil
560 "Obsolete variable. See `idlwave-complete-empty-string-as-lower-case' and
561 `idlwave-completion-case'.")
562
563 (defcustom idlwave-buffer-case-takes-precedence nil
564 "*Non-nil means, the case of tokens in buffers dominates over system stuff.
565 To make this possible, we need to re-case everything each time we update
566 the routine info from the buffers. This is slow.
567 The default is to consider the case given in the system and library files
568 first which makes updating much faster."
569 :group 'idlwave-completion
570 :type 'boolean)
571
572 (defcustom idlwave-highlight-help-links-in-completion t
573 "*Non-nil means, highlight completions for which system help is available.
574 Help can then be accessed with mouse-3.
575 This option is only effective when the online help system is installed."
576 :group 'idlwave-completion
577 :type 'boolean)
578
579 (defcustom idlwave-support-inheritance t
580 "Non-nil means, treat inheritance with completion, online help etc.
581 When nil, IDLWAVE only knows about the native methods and tags of a class,
582 not about inherited ones."
583 :group 'idlwave-routine-info
584 :type 'boolean)
585
586 (defcustom idlwave-keyword-class-inheritance '("^[gs]etproperty$" "^init$")
587 "List of regular expressions for class-driven keyword inheritance.
588 Keyword inheritance is often tied to class inheritance by \"chaining\"
589 up the class tree. While it cannot be assumed that the presence of an
590 _EXTRA or _REF_EXTRA symbol guarantees such chaining will occur, for
591 certain methods this assumption is almost always true. The methods
592 for which to assume this can be set here."
593 :group 'idlwave-routine-info
594 :type '(repeat (regexp :tag "Match method:")))
595
596
597 (defcustom idlwave-completion-show-classes 1
598 "*Number of classes to show when completing object methods and keywords.
599 When completing methods or keywords for an object with unknown class,
600 the *Completions* buffer will show the valid classes for each completion
601 like this:
602
603 MyMethod <Class1,Class2,Class3>
604
605 The value of this variable may be nil to inhibit display, or an integer to
606 indicate the maximum number of classes to display.
607
608 On XEmacs, a full list of classes will also be placed into a `help-echo'
609 property on the competion items, so that the list of classes for the current
610 item is displayed in the echo area. If the value of this variable is a
611 negative integer, the `help-echo' property will be suppressed."
612 :group 'idlwave-completion
613 :type '(choice (const :tag "Don't show" nil)
614 (integer :tag "Number of classes shown" 1)))
615
616 (defcustom idlwave-completion-fontify-classes t
617 "*Non-nil means, fontify the classes in completions buffer.
618 This makes it easier to distinguish the completion items from the extra
619 class info listed. See `idlwave-completion-show-classes'."
620 :group 'idlwave-completion
621 :type 'boolean)
622
623 (defcustom idlwave-query-class '((method-default . nil)
624 (keyword-default . nil))
625 "Association list governing specification of object classes for completion.
626
627 When IDLWAVE tries to complete object-oriented methods, it usually
628 cannot determine the class of a given object from context. In order
629 to provide the user with a correct list of methods or keywords, it
630 needs to determine the appropriate class. IDLWAVE has two ways of
631 doing this (well, three ways if you count the shell... see
632 `idlwave-shell-query-for-class'):
633
634 1. Combine the items of all available classes which contain this
635 method for the purpose of completion. So when completing a method,
636 all methods of all known classes are available, and when completing
637 a keyword, all keywords allowed for this method in any class are
638 shown. This behavior is very much like normal completion and is
639 therefore the default. It works much better than one might think -
640 only for the INIT, GETPROPERTY and SETPROPERTY the keyword lists
641 become uncomfortably long. See also
642 `idlwave-completion-show-classes'.
643
644 2. The second possibility is to ask the user on each occasion. To
645 make this less interruptive, IDLWAVE can store the class as a text
646 property on the object operator `->'. For a given object in the
647 source code, class selection will then be needed only once
648 - for example to complete the method. Keywords to the method can
649 then be completed directly, because the class is already known.
650 You will have to turn on the storage of the selected class
651 explicitly with the variable `idlwave-store-inquired-class'.
652
653 This variable allows you to configure IDLWAVE's method and
654 method-keyword completion behavior. Its value is an alist, which
655 should contain at least two elements: (method-default . VALUE) and
656 \(keyword-default . VALUE), where VALUE is either t or nil. These
657 specify if the class should be found during method and keyword
658 completion, respectively.
659
660 The alist may have additional entries specifying exceptions from the
661 keyword completion rule for specific methods, like INIT or
662 GETPROPERTY. In order to turn on class specification for the INIT
663 method, add an entry (\"INIT\" . t). The method name must be ALL-CAPS."
664 :group 'idlwave-completion
665 :type '(list
666 (cons (const method-default)
667 (boolean :tag "Determine class when completing METHODS "))
668 (cons (const keyword-default)
669 (boolean :tag "Determine class when completing KEYWORDS "))
670 (repeat
671 :tag "Exceptions to defaults"
672 :inline t
673 (cons (string :tag "MODULE" :value "")
674 (boolean :tag "Determine class for this method")))))
675
676 (defcustom idlwave-store-inquired-class t
677 "*Non-nil means, store class of a method call as text property on `->'.
678 IDLWAVE sometimes has to ask the user for the class associated with a
679 particular object method call. This happens during the commands
680 `idlwave-routine-info' and `idlwave-complete', depending upon the
681 value of the variable `idlwave-query-class'.
682
683 When you specify a class, this information can be stored as a text
684 property on the `->' arrow in the source code, so that during the same
685 editing session, IDLWAVE will not have to ask again. When this
686 variable is non-nil, IDLWAVE will store and reuse the class information.
687 The class stored can be checked and removed with `\\[idlwave-routine-info]'
688 on the arrow.
689
690 The default of this variable is nil, since the result of commands then
691 is more predictable. However, if you know what you are doing, it can
692 be nice to turn this on.
693
694 An arrow which knows the class will be highlighted with
695 `idlwave-class-arrow-face'. The command \\[idlwave-routine-info]
696 displays (with prefix arg: deletes) the class stored on the arrow
697 at point."
698 :group 'idlwave-completion
699 :type 'boolean)
700
701 (defcustom idlwave-class-arrow-face 'bold
702 "*Face to highlight object operator arrows `->' which carry a class property.
703 When IDLWAVE stores a class name as text property on an object arrow
704 \(see variable `idlwave-store-inquired-class', it highlights the arrow
705 with this font in order to remind the user that this arrow is special."
706 :group 'idlwave-completion
707 :type 'symbol)
708
709 (defcustom idlwave-resize-routine-help-window t
710 "*Non-nil means, resize the Routine-info *Help* window to fit the content."
711 :group 'idlwave-completion
712 :type 'boolean)
713
714 (defcustom idlwave-keyword-completion-adds-equal t
715 "*Non-nil means, completion automatically adds `=' after completed keywords."
716 :group 'idlwave-completion
717 :type 'boolean)
718
719 (defcustom idlwave-function-completion-adds-paren t
720 "*Non-nil means, completion automatically adds `(' after completed function.
721 nil means, don't add anything.
722 A value of `2' means, also add the closing parenthesis and position cursor
723 between the two."
724 :group 'idlwave-completion
725 :type '(choice (const :tag "Nothing" nil)
726 (const :tag "(" t)
727 (const :tag "()" 2)))
728
729 (defcustom idlwave-completion-restore-window-configuration t
730 "*Non-nil means, try to restore the window configuration after completion.
731 When completion is not unique, Emacs displays a list of completions.
732 This messes up your window configuration. With this variable set, IDLWAVE
733 restores the old configuration after successful completion."
734 :group 'idlwave-completion
735 :type 'boolean)
736
737 ;;; Variables for abbrev and action behavior -----------------------------
738
739 (defgroup idlwave-abbrev-and-indent-action nil
740 "IDLWAVE performs actions when expanding abbreviations or indenting lines.
741 The variables in this group govern this."
742 :group 'idlwave)
743
744 (defcustom idlwave-do-actions nil
745 "*Non-nil means performs actions when indenting.
746 The actions that can be performed are listed in `idlwave-indent-action-table'."
747 :group 'idlwave-abbrev-and-indent-action
748 :type 'boolean)
749
750 (defcustom idlwave-abbrev-start-char "\\"
751 "*A single character string used to start abbreviations in abbrev mode.
752 Possible characters to chose from: ~`\%
753 or even '?'. '.' is not a good choice because it can make structure
754 field names act like abbrevs in certain circumstances.
755
756 Changes to this in `idlwave-mode-hook' will have no effect. Instead a user
757 must set it directly using `setq' in the .emacs file before idlwave.el
758 is loaded."
759 :group 'idlwave-abbrev-and-indent-action
760 :type 'string)
761
762 (defcustom idlwave-surround-by-blank nil
763 "*Non-nil means, enable `idlwave-surround'.
764 If non-nil, `=',`<',`>',`&',`,', `->' are surrounded with spaces by
765 `idlwave-surround'.
766 See help for `idlwave-indent-action-table' for symbols using `idlwave-surround'.
767
768 Also see the default key bindings for keys using `idlwave-surround'.
769 Keys are bound and made into actions calling `idlwave-surround' with
770 `idlwave-action-and-binding'.
771 See help for `idlwave-action-and-binding' for examples.
772
773 Also see help for `idlwave-surround'."
774 :group 'idlwave-abbrev-and-indent-action
775 :type 'boolean)
776
777 (defcustom idlwave-pad-keyword t
778 "*Non-nil means pad '=' in keywords (routine calls or defs) like assignment.
779 Whenever `idlwave-surround' is non-nil then this affects how '=' is
780 padded for keywords and for variables. If t, pad the same as for
781 assignments. If nil then spaces are removed. With any other value,
782 spaces are left unchanged."
783 :group 'idlwave-abbrev-and-indent-action
784 :type '(choice
785 (const :tag "Pad like assignments" t)
786 (const :tag "Remove space near `='" nil)
787 (const :tag "Keep space near `='" 'keep)))
788
789 (defcustom idlwave-show-block t
790 "*Non-nil means point blinks to block beginning for `idlwave-show-begin'."
791 :group 'idlwave-abbrev-and-indent-action
792 :type 'boolean)
793
794 (defcustom idlwave-expand-generic-end nil
795 "*Non-nil means expand generic END to ENDIF/ENDELSE/ENDWHILE etc."
796 :group 'idlwave-abbrev-and-indent-action
797 :type 'boolean)
798
799 (defcustom idlwave-reindent-end t
800 "*Non-nil means re-indent line after END was typed."
801 :group 'idlwave-abbrev-and-indent-action
802 :type 'boolean)
803
804 (defcustom idlwave-abbrev-move t
805 "*Non-nil means the abbrev hook can move point.
806 Set to nil by `idlwave-expand-region-abbrevs'. To see the abbrev
807 definitions, use the command `list-abbrevs', for abbrevs that move
808 point. Moving point is useful, for example, to place point between
809 parentheses of expanded functions.
810
811 See `idlwave-check-abbrev'."
812 :group 'idlwave-abbrev-and-indent-action
813 :type 'boolean)
814
815 (defcustom idlwave-abbrev-change-case nil
816 "*Non-nil means all abbrevs will be forced to either upper or lower case.
817 If the value t, all expanded abbrevs will be upper case.
818 If the value is 'down then abbrevs will be forced to lower case.
819 If nil, the case will not change.
820 If `idlwave-reserved-word-upcase' is non-nil, reserved words will always be
821 upper case, regardless of this variable."
822 :group 'idlwave-abbrev-and-indent-action
823 :type 'boolean)
824
825 (defcustom idlwave-reserved-word-upcase nil
826 "*Non-nil means, reserved words will be made upper case via abbrev expansion.
827 If nil case of reserved words is controlled by `idlwave-abbrev-change-case'.
828 Has effect only if in abbrev-mode."
829 :group 'idlwave-abbrev-and-indent-action
830 :type 'boolean)
831
832 ;;; Action/Expand Tables.
833 ;;
834 ;; The average user may have difficulty modifying this directly. It
835 ;; can be modified/set in idlwave-mode-hook, but it is easier to use
836 ;; idlwave-action-and-binding. See help for idlwave-action-and-binding for
837 ;; examples of how to add an action.
838 ;;
839 ;; The action table is used by `idlwave-indent-line' whereas both the
840 ;; action and expand tables are used by `idlwave-indent-and-action'. In
841 ;; general, the expand table is only used when a line is explicitly
842 ;; indented. Whereas, in addition to being used when the expand table
843 ;; is used, the action table is used when a line is indirectly
844 ;; indented via line splitting, auto-filling or a new line creation.
845 ;;
846 ;; Example actions:
847 ;;
848 ;; Capitalize system vars
849 ;; (idlwave-action-and-binding idlwave-sysvar '(capitalize-word 1) t)
850 ;;
851 ;; Capitalize procedure name
852 ;; (idlwave-action-and-binding "\\<\\(pro\\|function\\)\\>[ \t]*\\<"
853 ;; '(capitalize-word 1) t)
854 ;;
855 ;; Capitalize common block name
856 ;; (idlwave-action-and-binding "\\<common\\>[ \t]+\\<"
857 ;; '(capitalize-word 1) t)
858 ;; Capitalize label
859 ;; (idlwave-action-and-binding (concat "^[ \t]*" idlwave-label)
860 ;; '(capitalize-word -1) t)
861
862 (defvar idlwave-indent-action-table nil
863 "*Associated array containing action lists of search string (car),
864 and function as a cdr. This table is used by `idlwave-indent-line'.
865 See documentation for `idlwave-do-action' for a complete description of
866 the action lists.
867
868 Additions to the table are made with `idlwave-action-and-binding' when a
869 binding is not requested.
870 See help on `idlwave-action-and-binding' for examples.")
871
872 (defvar idlwave-indent-expand-table nil
873 "*Associated array containing action lists of search string (car),
874 and function as a cdr. The table is used by the
875 `idlwave-indent-and-action' function. See documentation for
876 `idlwave-do-action' for a complete description of the action lists.
877
878 Additions to the table are made with `idlwave-action-and-binding' when a
879 binding is requested.
880 See help on `idlwave-action-and-binding' for examples.")
881
882 ;;; Documentation header and history keyword ---------------------------------
883
884 (defgroup idlwave-documentation nil
885 "Options for documenting IDLWAVE files."
886 :group 'idlwave)
887
888 ;; FIXME: make defcustom?
889 (defvar idlwave-file-header
890 (list nil
891 ";+
892 ; NAME:
893 ;
894 ;
895 ;
896 ; PURPOSE:
897 ;
898 ;
899 ;
900 ; CATEGORY:
901 ;
902 ;
903 ;
904 ; CALLING SEQUENCE:
905 ;
906 ;
907 ;
908 ; INPUTS:
909 ;
910 ;
911 ;
912 ; OPTIONAL INPUTS:
913 ;
914 ;
915 ;
916 ; KEYWORD PARAMETERS:
917 ;
918 ;
919 ;
920 ; OUTPUTS:
921 ;
922 ;
923 ;
924 ; OPTIONAL OUTPUTS:
925 ;
926 ;
927 ;
928 ; COMMON BLOCKS:
929 ;
930 ;
931 ;
932 ; SIDE EFFECTS:
933 ;
934 ;
935 ;
936 ; RESTRICTIONS:
937 ;
938 ;
939 ;
940 ; PROCEDURE:
941 ;
942 ;
943 ;
944 ; EXAMPLE:
945 ;
946 ;
947 ;
948 ; MODIFICATION HISTORY:
949 ;
950 ;-
951 ")
952 "*A list (PATHNAME STRING) specifying the doc-header template to use for
953 summarizing a file. If PATHNAME is non-nil then this file will be included.
954 Otherwise STRING is used. If nil, the file summary will be omitted.
955 For example you might set PATHNAME to the path for the
956 lib_template.pro file included in the IDL distribution.")
957
958 (defcustom idlwave-header-to-beginning-of-file t
959 "*Non-nil means, the documentation header will always be at start of file.
960 When nil, the header is positioned between the PRO/FUNCTION line of
961 the current routine and the code, allowing several routine headers in
962 a file."
963 :group 'idlwave-documentation
964 :type 'boolean)
965
966 (defcustom idlwave-timestamp-hook 'idlwave-default-insert-timestamp
967 "*The hook function used to update the timestamp of a function."
968 :group 'idlwave-documentation
969 :type 'function)
970
971 (defcustom idlwave-doc-modifications-keyword "HISTORY"
972 "*The modifications keyword to use with the log documentation commands.
973 A ':' is added to the keyword end.
974 Inserted by doc-header and used to position logs by doc-modification.
975 If nil it will not be inserted."
976 :group 'idlwave-documentation
977 :type 'string)
978
979 (defcustom idlwave-doclib-start "^;+\\+"
980 "*Regexp matching the start of a document library header."
981 :group 'idlwave-documentation
982 :type 'regexp)
983
984 (defcustom idlwave-doclib-end "^;+-"
985 "*Regexp matching the end of a document library header."
986 :group 'idlwave-documentation
987 :type 'regexp)
988
989 ;;; External Programs -------------------------------------------------------
990
991 (defgroup idlwave-external-programs nil
992 "Path locations of external commands used by IDLWAVE."
993 :group 'idlwave)
994
995 (defcustom idlwave-shell-explicit-file-name "idl"
996 "*If non-nil, this is the command to run IDL.
997 Should be an absolute file path or path relative to the current environment
998 execution search path. If you want to specify command line switches
999 for the IDL program, use `idlwave-shell-command-line-options'.
1000
1001 I know the name of this variable is badly chosen, but I cannot change
1002 it without compromising backwards-compatibility."
1003 :group 'idlwave-external-programs
1004 :type 'string)
1005
1006 (defcustom idlwave-shell-command-line-options nil
1007 "*A list of command line options for calling the IDL program.
1008 Since IDL is executed directly without going through a shell like /bin/sh,
1009 this should be a list of strings like '(\"-rt=file\" \"-nw\") with a separate
1010 string for each argument. But you may also give a single string which
1011 contains the options whitespace-separated. Emacs will be kind enough to
1012 split it for you."
1013 :type '(choice
1014 string
1015 (repeat (string :value "")))
1016 :group 'idlwave-external-programs)
1017
1018 (defcustom idlwave-help-application "idlhelp"
1019 "*The external application providing reference help for programming.
1020 Obsolete, if the IDL Assistant is being used for help."
1021 :group 'idlwave-external-programs
1022 :type 'string)
1023
1024 ;;; Some Shell variables which must be defined here.-----------------------
1025
1026 (defcustom idlwave-shell-debug-modifiers '()
1027 "List of modifiers to be used for the debugging commands.
1028 Will be used to bind debugging commands in the shell buffer and in all
1029 source buffers. These are additional convenience bindings, the debugging
1030 commands are always available with the `C-c C-d' prefix.
1031 If you set this to '(control shift), this means setting a breakpoint will
1032 be on `C-S-b', compiling a source file on `C-S-c' etc. Possible modifiers
1033 are `control', `meta', `super', `hyper', `alt', and `shift'."
1034 :group 'idlwave-shell-general-setup
1035 :type '(set :tag "Specify modifiers"
1036 (const control)
1037 (const meta)
1038 (const super)
1039 (const hyper)
1040 (const alt)
1041 (const shift)))
1042
1043 (defcustom idlwave-shell-automatic-start nil
1044 "*If non-nil attempt invoke `idlwave-shell' if not already running.
1045 This is checked when an attempt to send a command to an
1046 IDL process is made."
1047 :group 'idlwave-shell-general-setup
1048 :type 'boolean)
1049
1050 ;;; Miscellaneous variables -------------------------------------------------
1051
1052 (defgroup idlwave-misc nil
1053 "Miscellaneous options for IDLWAVE mode."
1054 :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
1055 :group 'idlwave)
1056
1057 (defcustom idlwave-startup-message t
1058 "*Non-nil displays a startup message when `idlwave-mode' is first called."
1059 :group 'idlwave-misc
1060 :type 'boolean)
1061
1062 (defcustom idlwave-default-font-lock-items
1063 '(pros-and-functions batch-files idlwave-idl-keywords label goto
1064 common-blocks class-arrows)
1065 "Items which should be fontified on the default fontification level 2.
1066 IDLWAVE defines 3 levels of fontification. Level 1 is very little, level 3
1067 is everything and level 2 is specified by this list.
1068 This variable must be set before IDLWAVE gets loaded.
1069 It is a list of symbols; the following symbols are allowed:
1070
1071 pros-and-functions Procedure and Function definitions
1072 batch-files Batch Files
1073 idlwave-idl-keywords IDL Keywords
1074 label Statement Labels
1075 goto Goto Statements
1076 common-blocks Common Blocks
1077 keyword-parameters Keyword Parameters in routine definitions and calls
1078 system-variables System Variables
1079 fixme FIXME: Warning in comments (on XEmacs only v. 21.0 and up)
1080 class-arrows Object Arrows with class property"
1081 :group 'idlwave-misc
1082 :type '(set
1083 :inline t :greedy t
1084 (const :tag "Procedure and Function definitions" pros-and-functions)
1085 (const :tag "Batch Files" batch-files)
1086 (const :tag "IDL Keywords (reserved words)" idlwave-idl-keywords)
1087 (const :tag "Statement Labels" label)
1088 (const :tag "Goto Statements" goto)
1089 (const :tag "Tags in Structure Definition" structtag)
1090 (const :tag "Structure Name" structname)
1091 (const :tag "Common Blocks" common-blocks)
1092 (const :tag "Keyword Parameters" keyword-parameters)
1093 (const :tag "System Variables" system-variables)
1094 (const :tag "FIXME: Warning" fixme)
1095 (const :tag "Object Arrows with class property " class-arrows)))
1096
1097 (defcustom idlwave-mode-hook nil
1098 "Normal hook. Executed when a buffer is put into `idlwave-mode'."
1099 :group 'idlwave-misc
1100 :type 'hook)
1101
1102 (defcustom idlwave-load-hook nil
1103 "Normal hook. Executed when idlwave.el is loaded."
1104 :group 'idlwave-misc
1105 :type 'hook)
1106
1107 (defvar idlwave-experimental nil
1108 "Non-nil means turn on a few experimental features.
1109 This variable is only for the maintainer, to test difficult stuff,
1110 while still distributing stable releases.
1111 As a user, you should not set this to t.")
1112
1113 ;;;
1114 ;;; End customization variables section
1115 ;;;
1116
1117 ;;; Non customization variables
1118
1119 ;;; font-lock mode - Additions by Phil Williams, Ulrik Dickow and
1120 ;;; Simon Marshall <simon_at_gnu.ai.mit.edu>
1121 ;;; and Carsten Dominik...
1122
1123 ;; The following are the reserved words in IDL. Maybe we should
1124 ;; highlight some more stuff as well?
1125 ;; Procedure declarations. Fontify keyword plus procedure name.
1126 (defvar idlwave-idl-keywords
1127 ;; To update this regexp, update the list of keywords and
1128 ;; evaluate the form.
1129 ;; (insert
1130 ;; (prin1-to-string
1131 ;; (concat
1132 ;; "\\<\\("
1133 ;; (regexp-opt
1134 ;; '("||" "&&" "and" "or" "xor" "not"
1135 ;; "eq" "ge" "gt" "le" "lt" "ne"
1136 ;; "for" "do" "endfor"
1137 ;; "if" "then" "endif" "else" "endelse"
1138 ;; "case" "of" "endcase"
1139 ;; "switch" "break" "continue" "endswitch"
1140 ;; "begin" "end"
1141 ;; "repeat" "until" "endrep"
1142 ;; "while" "endwhile"
1143 ;; "goto" "return"
1144 ;; "inherits" "mod"
1145 ;; "compile_opt" "forward_function"
1146 ;; "on_error" "on_ioerror")) ; on_error is not officially reserved
1147 ;; "\\)\\>")))
1148 "\\<\\(&&\\|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\\|||\\)\\>")
1149
1150
1151 (let* (;; Procedure declarations. Fontify keyword plus procedure name.
1152 ;; Function declarations. Fontify keyword plus function name.
1153 (pros-and-functions
1154 '("\\<\\(function\\|pro\\)\\>[ \t]+\\(\\sw+\\(::\\sw+\\)?\\)"
1155 (1 font-lock-keyword-face)
1156 (2 font-lock-function-name-face nil t)))
1157
1158 ;; Common blocks
1159 (common-blocks
1160 '("\\<\\(common\\)\\>[ \t]*\\(\\sw+\\)?[ \t]*,?"
1161 (1 font-lock-keyword-face) ; "common"
1162 (2 font-lock-reference-face nil t) ; block name
1163 ("[ \t]*\\(\\sw+\\)[ ,]*"
1164 ;; Start with point after block name and comma
1165 (goto-char (match-end 0)) ; needed for XEmacs, could be nil
1166 nil
1167 (1 font-lock-variable-name-face) ; variable names
1168 )))
1169
1170 ;; Batch files
1171 (batch-files
1172 '("^[ \t]*\\(@[^ \t\n]+\\)" (1 font-lock-string-face)))
1173
1174 ;; FIXME warning.
1175 (fixme
1176 '("\\<FIXME:" (0 font-lock-warning-face t)))
1177
1178 ;; Labels
1179 (label
1180 '("^[ \t]*\\([a-zA-Z]\\sw*:\\)" (1 font-lock-reference-face)))
1181
1182 ;; The goto statement and its label
1183 (goto
1184 '("\\(goto\\)[ \t]*,[ \t]*\\([a-zA-Z]\\sw*\\)"
1185 (1 font-lock-keyword-face)
1186 (2 font-lock-reference-face)))
1187
1188 ;; Tags in structure definitions. Note that this definition
1189 ;; actually collides with labels, so we have to use the same
1190 ;; face. It also matches named subscript ranges,
1191 ;; e.g. vec{bottom:top]. No good way around this.
1192 (structtag
1193 '("\\<\\([a-zA-Z][a-zA-Z0-9_]*:\\)[^:]" (1 font-lock-reference-face)))
1194
1195 ;; Structure names
1196 (structname
1197 '("\\({\\|\\<inherits\\s-\\)\\s-*\\([a-zA-Z][a-zA-Z0-9_]*\\)[},\t \n]"
1198 (2 font-lock-function-name-face)))
1199
1200 ;; Keyword parameters, like /xlog or ,xrange=[]
1201 ;; This is anchored to the comma preceeding the keyword.
1202 ;; Treats continuation lines, works only during whole buffer
1203 ;; fontification. Slow, use it only in fancy fontification.
1204 (keyword-parameters
1205 '("\\(,\\|[a-zA-Z0-9_](\\)[ \t]*\\(\\$[ \t]*\\(;.*\\)?\n\\([ \t]*\\(;.*\\)?\n\\)*[ \t]*\\)?\\(/[a-zA-Z_]\\sw*\\|[a-zA-Z_]\\sw*[ \t]*=\\)"
1206 (6 font-lock-reference-face)))
1207
1208 ;; System variables start with a bang.
1209 (system-variables
1210 '("\\(![a-zA-Z_0-9]+\\(\\.\\sw+\\)?\\)"
1211 (1 font-lock-variable-name-face)))
1212
1213 ;; Special and unusual operators (not used because too noisy)
1214 (special-operators
1215 '("[<>#]" (0 font-lock-keyword-face)))
1216
1217 ;; All operators (not used because too noisy)
1218 (all-operators
1219 '("[-*^#+<>/]" (0 font-lock-keyword-face)))
1220
1221 ;; Arrows with text property `idlwave-class'
1222 (class-arrows
1223 '(idlwave-match-class-arrows (0 idlwave-class-arrow-face))))
1224
1225 (defconst idlwave-font-lock-keywords-1
1226 (list pros-and-functions batch-files)
1227 "Subdued level highlighting for IDLWAVE mode.")
1228
1229 (defconst idlwave-font-lock-keywords-2
1230 (mapcar 'symbol-value idlwave-default-font-lock-items)
1231 "Medium level highlighting for IDLWAVE mode.")
1232
1233 (defconst idlwave-font-lock-keywords-3
1234 (list pros-and-functions
1235 batch-files
1236 idlwave-idl-keywords
1237 label goto
1238 structtag
1239 structname
1240 common-blocks
1241 keyword-parameters
1242 system-variables
1243 class-arrows)
1244 "Gaudy level highlighting for IDLWAVE mode."))
1245
1246 (defun idlwave-match-class-arrows (limit)
1247 ;; Match an object arrow with class property
1248 (and idlwave-store-inquired-class
1249 (re-search-forward "->" limit 'limit)
1250 (get-text-property (match-beginning 0) 'idlwave-class)))
1251
1252 (defvar idlwave-font-lock-keywords idlwave-font-lock-keywords-2
1253 "Default expressions to highlight in IDLWAVE mode.")
1254
1255 (defvar idlwave-font-lock-defaults
1256 '((idlwave-font-lock-keywords
1257 idlwave-font-lock-keywords-1
1258 idlwave-font-lock-keywords-2
1259 idlwave-font-lock-keywords-3)
1260 nil t
1261 ((?$ . "w") (?_ . "w") (?. . "w") (?| . "w") (?& . "w"))
1262 beginning-of-line))
1263
1264 (put 'idlwave-mode 'font-lock-defaults
1265 idlwave-font-lock-defaults) ; XEmacs
1266
1267 (defconst idlwave-comment-line-start-skip "^[ \t]*;"
1268 "Regexp to match the start of a full-line comment.
1269 That is the _beginning_ of a line containing a comment delimiter `;' preceded
1270 only by whitespace.")
1271
1272 (defconst idlwave-begin-block-reg
1273 "\\<\\(pro\\|function\\|begin\\|case\\|switch\\)\\>"
1274 "Regular expression to find the beginning of a block.
1275 The case does not matter. The search skips matches in comments.")
1276
1277 (defconst idlwave-begin-unit-reg "^\\s-*\\(pro\\|function\\)\\>\\|\\`"
1278 "Regular expression to find the beginning of a unit.
1279 The case does not matter.")
1280
1281 (defconst idlwave-end-unit-reg "^\\s-*\\(pro\\|function\\)\\>\\|\\'"
1282 "Regular expression to find the line that indicates the end of unit.
1283 This line is the end of buffer or the start of another unit.
1284 The case does not matter. The search skips matches in comments.")
1285
1286 (defconst idlwave-continue-line-reg "\\<\\$"
1287 "Regular expression to match a continued line.")
1288
1289 (defconst idlwave-end-block-reg
1290 "\\<end\\(\\|case\\|switch\\|else\\|for\\|if\\|rep\\|while\\)\\>"
1291 "Regular expression to find the end of a block.
1292 The case does not matter. The search skips matches in comments.")
1293
1294 (defconst idlwave-block-matches
1295 '(("pro" . "end")
1296 ("function" . "end")
1297 ("case" . "endcase")
1298 ("else" . "endelse")
1299 ("for" . "endfor")
1300 ("then" . "endif")
1301 ("repeat" . "endrep")
1302 ("switch" . "endswitch")
1303 ("while" . "endwhile"))
1304 "Matches between statements and the corresponding END variant.
1305 The cars are the reserved words starting a block. If the block really
1306 begins with BEGIN, the cars are the reserved words before the begin
1307 which can be used to identify the block type.
1308 This is used to check for the correct END type, to close blocks and
1309 to expand generic end statements to their detailed form.")
1310
1311 (defconst idlwave-block-match-regexp
1312 "\\<\\(else\\|for\\|then\\|repeat\\|while\\)\\>"
1313 "Regular expression matching reserved words which can stand before
1314 blocks starting with a BEGIN statement. The matches must have associations
1315 `idlwave-block-matches'.")
1316
1317 (defconst idlwave-identifier "[a-zA-Z_][a-zA-Z0-9$_]*"
1318 "Regular expression matching an IDL identifier.")
1319
1320 (defconst idlwave-sysvar (concat "!" idlwave-identifier)
1321 "Regular expression matching IDL system variables.")
1322
1323 (defconst idlwave-variable (concat idlwave-identifier "\\|" idlwave-sysvar)
1324 "Regular expression matching IDL variable names.")
1325
1326 (defconst idlwave-label (concat idlwave-identifier ":")
1327 "Regular expression matching IDL labels.")
1328
1329 (defconst idlwave-method-call (concat idlwave-identifier "\\s *->"
1330 "\\(\\s *" idlwave-identifier "::\\)?"
1331 ))
1332
1333 (defconst idlwave-statement-match
1334 (list
1335 ;; "endif else" is the only possible "end" that can be
1336 ;; followed by a statement on the same line.
1337 '(endelse . ("end\\(\\|if\\)\\s +else" "end\\(\\|if\\)\\s +else"))
1338 ;; all other "end"s can not be followed by a statement.
1339 (cons 'end (list idlwave-end-block-reg nil))
1340 '(if . ("if\\>" "then"))
1341 '(for . ("for\\>" "do"))
1342 '(begin . ("begin\\>" nil))
1343 '(pdef . ("pro\\>\\|function\\>" nil))
1344 '(while . ("while\\>" "do"))
1345 '(repeat . ("repeat\\>" "repeat"))
1346 '(goto . ("goto\\>" nil))
1347 '(case . ("case\\>" nil))
1348 '(switch . ("switch\\>" nil))
1349 (cons 'call (list (concat "\\(" idlwave-variable "\\) *= *"
1350 "\\(" idlwave-method-call "\\s *\\)?"
1351 idlwave-identifier
1352 "\\s *(") nil))
1353 (cons 'call (list (concat
1354 "\\(" idlwave-method-call "\\s *\\)?"
1355 idlwave-identifier
1356 "\\( *\\($\\|\\$\\)\\|\\s *,\\)") nil))
1357 (cons 'assign (list (concat
1358 "\\(" idlwave-variable "\\) *=") nil)))
1359
1360 "Associated list of statement matching regular expressions.
1361 Each regular expression matches the start of an IDL statement.
1362 The first element of each association is a symbol giving the statement
1363 type. The associated value is a list. The first element of this list
1364 is a regular expression matching the start of an IDL statement for
1365 identifying the statement type. The second element of this list is a
1366 regular expression for finding a substatement for the type. The
1367 substatement starts after the end of the found match modulo
1368 whitespace. If it is nil then the statement has no substatement. The
1369 list order matters since matching an assignment statement exactly is
1370 not possible without parsing. Thus assignment statement become just
1371 the leftover unidentified statements containing an equal sign.")
1372
1373 (defvar idlwave-fill-function 'auto-fill-function
1374 "IDL mode auto fill function.")
1375
1376 (defvar idlwave-comment-indent-function 'comment-indent-function
1377 "IDL mode comment indent function.")
1378
1379 ;; Note that this is documented in the v18 manuals as being a string
1380 ;; of length one rather than a single character.
1381 ;; The code in this file accepts either format for compatibility.
1382 (defvar idlwave-comment-indent-char ?\
1383 "Character to be inserted for IDL comment indentation.
1384 Normally a space.")
1385
1386 (defconst idlwave-continuation-char ?$
1387 "Character which is inserted as a last character on previous line by
1388 \\[idlwave-split-line] to begin a continuation line. Normally $.")
1389
1390 (defconst idlwave-mode-version "6.1_em22")
1391
1392 (defmacro idlwave-keyword-abbrev (&rest args)
1393 "Creates a function for abbrev hooks to call `idlwave-check-abbrev' with args."
1394 `(quote (lambda ()
1395 ,(append '(idlwave-check-abbrev) args))))
1396
1397 ;; If I take the time I can replace idlwave-keyword-abbrev with
1398 ;; idlwave-code-abbrev and remove the quoted abbrev check from
1399 ;; idlwave-check-abbrev. Then, e.g, (idlwave-keyword-abbrev 0 t) becomes
1400 ;; (idlwave-code-abbrev idlwave-check-abbrev 0 t). In fact I should change
1401 ;; the name of idlwave-check-abbrev to something like idlwave-modify-abbrev.
1402
1403 (defmacro idlwave-code-abbrev (&rest args)
1404 "Creates a function for abbrev hooks that ensures abbrevs are not quoted.
1405 Specifically, if the abbrev is in a comment or string it is unexpanded.
1406 Otherwise ARGS forms a list that is evaluated."
1407 `(quote (lambda ()
1408 ,(prin1-to-string args) ;; Puts the code in the doc string
1409 (if (idlwave-quoted)
1410 (progn (unexpand-abbrev) nil)
1411 ,(append args)))))
1412
1413 (defvar idlwave-mode-map (make-sparse-keymap)
1414 "Keymap used in IDL mode.")
1415
1416 (defvar idlwave-mode-syntax-table (make-syntax-table)
1417 "Syntax table in use in `idlwave-mode' buffers.")
1418
1419 (modify-syntax-entry ?+ "." idlwave-mode-syntax-table)
1420 (modify-syntax-entry ?- "." idlwave-mode-syntax-table)
1421 (modify-syntax-entry ?* "." idlwave-mode-syntax-table)
1422 (modify-syntax-entry ?/ "." idlwave-mode-syntax-table)
1423 (modify-syntax-entry ?^ "." idlwave-mode-syntax-table)
1424 (modify-syntax-entry ?# "." idlwave-mode-syntax-table)
1425 (modify-syntax-entry ?= "." idlwave-mode-syntax-table)
1426 (modify-syntax-entry ?% "." idlwave-mode-syntax-table)
1427 (modify-syntax-entry ?< "." 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 ?\n ">" idlwave-mode-syntax-table)
1439 (modify-syntax-entry ?\f ">" idlwave-mode-syntax-table)
1440
1441 (defvar idlwave-find-symbol-syntax-table
1442 (copy-syntax-table idlwave-mode-syntax-table)
1443 "Syntax table that treats symbol characters as word characters.")
1444
1445 (modify-syntax-entry ?$ "w" idlwave-find-symbol-syntax-table)
1446 (modify-syntax-entry ?_ "w" idlwave-find-symbol-syntax-table)
1447 (modify-syntax-entry ?! "w" idlwave-find-symbol-syntax-table)
1448 (modify-syntax-entry ?. "w" idlwave-find-symbol-syntax-table)
1449
1450 (defmacro idlwave-with-special-syntax (&rest body)
1451 "Execute BODY with a different syntax table."
1452 `(let ((saved-syntax (syntax-table)))
1453 (unwind-protect
1454 (progn
1455 (set-syntax-table idlwave-find-symbol-syntax-table)
1456 ,@body)
1457 (set-syntax-table saved-syntax))))
1458
1459 ;(defmacro idlwave-with-special-syntax1 (&rest body)
1460 ; "Execute BODY with a different syntax table."
1461 ; `(let ((saved-syntax (syntax-table)))
1462 ; (unwind-protect
1463 ; (progn
1464 ; (set-syntax-table idlwave-find-symbol-syntax-table)
1465 ; ,@body)
1466 ; (set-syntax-table saved-syntax))))
1467
1468 (defun idlwave-action-and-binding (key cmd &optional select)
1469 "KEY and CMD are made into a key binding and an indent action.
1470 KEY is a string - same as for the `define-key' function. CMD is a
1471 function of no arguments or a list to be evaluated. CMD is bound to
1472 KEY in `idlwave-mode-map' by defining an anonymous function calling
1473 `self-insert-command' followed by CMD. If KEY contains more than one
1474 character a binding will only be set if SELECT is 'both.
1475
1476 \(KEY . CMD\) is also placed in the `idlwave-indent-expand-table',
1477 replacing any previous value for KEY. If a binding is not set then it
1478 will instead be placed in `idlwave-indent-action-table'.
1479
1480 If the optional argument SELECT is nil then an action and binding are
1481 created. If SELECT is 'noaction, then a binding is always set and no
1482 action is created. If SELECT is 'both then an action and binding
1483 will both be created even if KEY contains more than one character.
1484 Otherwise, if SELECT is non-nil then only an action is created.
1485
1486 Some examples:
1487 No spaces before and 1 after a comma
1488 (idlwave-action-and-binding \",\" '(idlwave-surround 0 1))
1489 A minimum of 1 space before and after `=' (see `idlwave-expand-equal').
1490 (idlwave-action-and-binding \"=\" '(idlwave-expand-equal -1 -1))
1491 Capitalize system variables - action only
1492 (idlwave-action-and-binding idlwave-sysvar '(capitalize-word 1) t)"
1493 (if (not (equal select 'noaction))
1494 ;; Add action
1495 (let* ((table (if select 'idlwave-indent-action-table
1496 'idlwave-indent-expand-table))
1497 (table-key (regexp-quote key))
1498 (cell (assoc table-key (eval table))))
1499 (if cell
1500 ;; Replace action command
1501 (setcdr cell cmd)
1502 ;; New action
1503 (set table (append (eval table) (list (cons table-key cmd)))))))
1504 ;; Make key binding for action
1505 (if (or (and (null select) (= (length key) 1))
1506 (equal select 'noaction)
1507 (equal select 'both))
1508 (define-key idlwave-mode-map key
1509 (append '(lambda ()
1510 (interactive)
1511 (self-insert-command 1))
1512 (list (if (listp cmd)
1513 cmd
1514 (list cmd)))))))
1515
1516 (fset 'idlwave-debug-map (make-sparse-keymap))
1517
1518 (define-key idlwave-mode-map "\C-c " 'idlwave-hard-tab)
1519 (define-key idlwave-mode-map [(control tab)] 'idlwave-hard-tab)
1520 ;(define-key idlwave-mode-map "\C-c\C- " 'idlwave-hard-tab)
1521 (define-key idlwave-mode-map "'" 'idlwave-show-matching-quote)
1522 (define-key idlwave-mode-map "\"" 'idlwave-show-matching-quote)
1523 (define-key idlwave-mode-map "\C-g" 'idlwave-keyboard-quit)
1524 (define-key idlwave-mode-map "\C-c;" 'idlwave-toggle-comment-region)
1525 (define-key idlwave-mode-map "\C-\M-a" 'idlwave-beginning-of-subprogram)
1526 (define-key idlwave-mode-map "\C-\M-e" 'idlwave-end-of-subprogram)
1527 (define-key idlwave-mode-map "\C-c{" 'idlwave-beginning-of-block)
1528 (define-key idlwave-mode-map "\C-c}" 'idlwave-end-of-block)
1529 (define-key idlwave-mode-map "\C-c]" 'idlwave-close-block)
1530 (define-key idlwave-mode-map [(meta control h)] 'idlwave-mark-subprogram)
1531 (define-key idlwave-mode-map "\M-\C-n" 'idlwave-forward-block)
1532 (define-key idlwave-mode-map "\M-\C-p" 'idlwave-backward-block)
1533 (define-key idlwave-mode-map "\M-\C-d" 'idlwave-down-block)
1534 (define-key idlwave-mode-map "\M-\C-u" 'idlwave-backward-up-block)
1535 (define-key idlwave-mode-map "\M-\r" 'idlwave-split-line)
1536 (define-key idlwave-mode-map "\M-\C-q" 'idlwave-indent-subprogram)
1537 (define-key idlwave-mode-map "\C-c\C-p" 'idlwave-previous-statement)
1538 (define-key idlwave-mode-map "\C-c\C-n" 'idlwave-next-statement)
1539 ;; (define-key idlwave-mode-map "\r" 'idlwave-newline)
1540 ;; (define-key idlwave-mode-map "\t" 'idlwave-indent-line)
1541 (define-key idlwave-mode-map [(shift iso-lefttab)] 'idlwave-indent-statement)
1542 (define-key idlwave-mode-map "\C-c\C-a" 'idlwave-auto-fill-mode)
1543 (define-key idlwave-mode-map "\M-q" 'idlwave-fill-paragraph)
1544 (define-key idlwave-mode-map "\M-s" 'idlwave-edit-in-idlde)
1545 (define-key idlwave-mode-map "\C-c\C-h" 'idlwave-doc-header)
1546 (define-key idlwave-mode-map "\C-c\C-m" 'idlwave-doc-modification)
1547 (define-key idlwave-mode-map "\C-c\C-c" 'idlwave-case)
1548 (define-key idlwave-mode-map "\C-c\C-d" 'idlwave-debug-map)
1549 (when (and (boundp 'idlwave-shell-debug-modifiers)
1550 (listp idlwave-shell-debug-modifiers)
1551 (not (equal idlwave-shell-debug-modifiers '())))
1552 ;; Bind the debug commands also with the special modifiers.
1553 (let ((shift (memq 'shift idlwave-shell-debug-modifiers))
1554 (mods-noshift (delq 'shift
1555 (copy-sequence idlwave-shell-debug-modifiers))))
1556 (define-key idlwave-mode-map
1557 (vector (append mods-noshift (list (if shift ?C ?c))))
1558 'idlwave-shell-save-and-run)
1559 (define-key idlwave-mode-map
1560 (vector (append mods-noshift (list (if shift ?B ?b))))
1561 'idlwave-shell-break-here)
1562 (define-key idlwave-mode-map
1563 (vector (append mods-noshift (list (if shift ?E ?e))))
1564 'idlwave-shell-run-region)))
1565 (define-key idlwave-mode-map "\C-c\C-d\C-c" 'idlwave-shell-save-and-run)
1566 (define-key idlwave-mode-map "\C-c\C-d\C-b" 'idlwave-shell-break-here)
1567 (define-key idlwave-mode-map "\C-c\C-d\C-e" 'idlwave-shell-run-region)
1568 (define-key idlwave-mode-map "\C-c\C-f" 'idlwave-for)
1569 ;; (define-key idlwave-mode-map "\C-c\C-f" 'idlwave-function)
1570 ;; (define-key idlwave-mode-map "\C-c\C-p" 'idlwave-procedure)
1571 (define-key idlwave-mode-map "\C-c\C-r" 'idlwave-repeat)
1572 (define-key idlwave-mode-map "\C-c\C-w" 'idlwave-while)
1573 (define-key idlwave-mode-map "\C-c\C-k" 'idlwave-kill-autoloaded-buffers)
1574 (define-key idlwave-mode-map "\C-c\C-s" 'idlwave-shell)
1575 (define-key idlwave-mode-map "\C-c\C-l" 'idlwave-shell-recenter-shell-window)
1576 (define-key idlwave-mode-map "\C-c\C-b" 'idlwave-list-buffer-load-path-shadows)
1577 (autoload 'idlwave-shell "idlw-shell"
1578 "Run an inferior IDL, with I/O through buffer `(idlwave-shell-buffer)'." t)
1579 (autoload 'idlwave-shell-send-command "idlw-shell")
1580 (autoload 'idlwave-shell-recenter-shell-window "idlw-shell"
1581 "Run `idlwave-shell' and switch back to current window" t)
1582 (autoload 'idlwave-shell-save-and-run "idlw-shell"
1583 "Save and run buffer under the shell." t)
1584 (autoload 'idlwave-shell-break-here "idlw-shell"
1585 "Set breakpoint in current line." t)
1586 (autoload 'idlwave-shell-run-region "idlw-shell"
1587 "Compile and run the region." t)
1588 (define-key idlwave-mode-map "\C-c\C-v" 'idlwave-find-module)
1589 (define-key idlwave-mode-map "\C-c\C-t" 'idlwave-find-module-this-file)
1590 (define-key idlwave-mode-map "\C-c?" 'idlwave-routine-info)
1591 (define-key idlwave-mode-map "\M-?" 'idlwave-context-help)
1592 (define-key idlwave-mode-map [(control meta ?\?)]
1593 'idlwave-help-assistant-help-with-topic)
1594 ;; Pickup both forms of Esc/Meta binding
1595 (define-key idlwave-mode-map [(meta tab)] 'idlwave-complete)
1596 (define-key idlwave-mode-map [?\e?\t] 'idlwave-complete)
1597 (define-key idlwave-mode-map "\M-\C-i" 'idlwave-complete)
1598 (define-key idlwave-mode-map "\C-c\C-i" 'idlwave-update-routine-info)
1599 (define-key idlwave-mode-map "\C-c=" 'idlwave-resolve)
1600 (define-key idlwave-mode-map
1601 (if (featurep 'xemacs) [(shift button3)] [(shift mouse-3)])
1602 'idlwave-mouse-context-help)
1603
1604 ;; Set action and key bindings.
1605 ;; See description of the function `idlwave-action-and-binding'.
1606 ;; Automatically add spaces for the following characters
1607
1608 ;; Actions for & are complicated by &&
1609 (idlwave-action-and-binding "&" 'idlwave-custom-ampersand-surround)
1610
1611 ;; Automatically add spaces to equal sign if not keyword. This needs
1612 ;; to go ahead of > and <, so >= and <= will be treated correctly
1613 (idlwave-action-and-binding "=" '(idlwave-expand-equal -1 -1))
1614
1615 ;; Actions for > and < are complicated by >=, <=, and ->...
1616 (idlwave-action-and-binding "<" '(idlwave-custom-ltgtr-surround nil))
1617 (idlwave-action-and-binding ">" '(idlwave-custom-ltgtr-surround 'gtr))
1618
1619 (idlwave-action-and-binding "," '(idlwave-surround 0 -1 1))
1620
1621
1622 ;;;
1623 ;;; Abbrev Section
1624 ;;;
1625 ;;; When expanding abbrevs and the abbrev hook moves backward, an extra
1626 ;;; space is inserted (this is the space typed by the user to expanded
1627 ;;; the abbrev).
1628 ;;;
1629 (defvar idlwave-mode-abbrev-table nil
1630 "Abbreviation table used for IDLWAVE mode.")
1631 (define-abbrev-table 'idlwave-mode-abbrev-table ())
1632
1633 (defun idlwave-define-abbrev (name expansion hook &optional noprefix table)
1634 "Define-abbrev with backward compatibility.
1635
1636 If NOPREFIX is non-nil, don't prepend prefix character. Installs into
1637 `idlwave-mode-abbrev-table' unless TABLE is non-nil."
1638 (let ((abbrevs-changed nil) ;; mask the current value to avoid save
1639 (args (list (or table idlwave-mode-abbrev-table)
1640 (if noprefix name (concat idlwave-abbrev-start-char name))
1641 expansion
1642 hook)))
1643 (condition-case nil
1644 (apply 'define-abbrev (append args '(0 t)))
1645 (error (apply 'define-abbrev args)))))
1646
1647 (condition-case nil
1648 (modify-syntax-entry (string-to-char idlwave-abbrev-start-char)
1649 "w" idlwave-mode-syntax-table)
1650 (error nil))
1651
1652 ;;
1653 ;; Templates
1654 ;;
1655 (idlwave-define-abbrev "c" "" (idlwave-code-abbrev idlwave-case))
1656 (idlwave-define-abbrev "sw" "" (idlwave-code-abbrev idlwave-switch))
1657 (idlwave-define-abbrev "f" "" (idlwave-code-abbrev idlwave-for))
1658 (idlwave-define-abbrev "fu" "" (idlwave-code-abbrev idlwave-function))
1659 (idlwave-define-abbrev "pr" "" (idlwave-code-abbrev idlwave-procedure))
1660 (idlwave-define-abbrev "r" "" (idlwave-code-abbrev idlwave-repeat))
1661 (idlwave-define-abbrev "w" "" (idlwave-code-abbrev idlwave-while))
1662 (idlwave-define-abbrev "i" "" (idlwave-code-abbrev idlwave-if))
1663 (idlwave-define-abbrev "elif" "" (idlwave-code-abbrev idlwave-elif))
1664 ;;
1665 ;; Keywords, system functions, conversion routines
1666 ;;
1667 (idlwave-define-abbrev "ap" "arg_present()" (idlwave-keyword-abbrev 1))
1668 (idlwave-define-abbrev "b" "begin" (idlwave-keyword-abbrev 0 t))
1669 (idlwave-define-abbrev "co" "common" (idlwave-keyword-abbrev 0 t))
1670 (idlwave-define-abbrev "cb" "byte()" (idlwave-keyword-abbrev 1))
1671 (idlwave-define-abbrev "cx" "fix()" (idlwave-keyword-abbrev 1))
1672 (idlwave-define-abbrev "cl" "long()" (idlwave-keyword-abbrev 1))
1673 (idlwave-define-abbrev "cf" "float()" (idlwave-keyword-abbrev 1))
1674 (idlwave-define-abbrev "cs" "string()" (idlwave-keyword-abbrev 1))
1675 (idlwave-define-abbrev "cc" "complex()" (idlwave-keyword-abbrev 1))
1676 (idlwave-define-abbrev "cd" "double()" (idlwave-keyword-abbrev 1))
1677 (idlwave-define-abbrev "e" "else" (idlwave-keyword-abbrev 0 t))
1678 (idlwave-define-abbrev "ec" "endcase" 'idlwave-show-begin)
1679 (idlwave-define-abbrev "es" "endswitch" 'idlwave-show-begin)
1680 (idlwave-define-abbrev "ee" "endelse" 'idlwave-show-begin)
1681 (idlwave-define-abbrev "ef" "endfor" 'idlwave-show-begin)
1682 (idlwave-define-abbrev "ei" "endif else if" 'idlwave-show-begin)
1683 (idlwave-define-abbrev "el" "endif else" 'idlwave-show-begin)
1684 (idlwave-define-abbrev "en" "endif" 'idlwave-show-begin)
1685 (idlwave-define-abbrev "er" "endrep" 'idlwave-show-begin)
1686 (idlwave-define-abbrev "ew" "endwhile" 'idlwave-show-begin)
1687 (idlwave-define-abbrev "g" "goto," (idlwave-keyword-abbrev 0 t))
1688 (idlwave-define-abbrev "h" "help," (idlwave-keyword-abbrev 0))
1689 (idlwave-define-abbrev "k" "keyword_set()" (idlwave-keyword-abbrev 1))
1690 (idlwave-define-abbrev "n" "n_elements()" (idlwave-keyword-abbrev 1))
1691 (idlwave-define-abbrev "on" "on_error," (idlwave-keyword-abbrev 0))
1692 (idlwave-define-abbrev "oi" "on_ioerror," (idlwave-keyword-abbrev 0 1))
1693 (idlwave-define-abbrev "ow" "openw," (idlwave-keyword-abbrev 0))
1694 (idlwave-define-abbrev "or" "openr," (idlwave-keyword-abbrev 0))
1695 (idlwave-define-abbrev "ou" "openu," (idlwave-keyword-abbrev 0))
1696 (idlwave-define-abbrev "p" "print," (idlwave-keyword-abbrev 0))
1697 (idlwave-define-abbrev "pt" "plot," (idlwave-keyword-abbrev 0))
1698 (idlwave-define-abbrev "re" "read," (idlwave-keyword-abbrev 0))
1699 (idlwave-define-abbrev "rf" "readf," (idlwave-keyword-abbrev 0))
1700 (idlwave-define-abbrev "ru" "readu," (idlwave-keyword-abbrev 0))
1701 (idlwave-define-abbrev "rt" "return" (idlwave-keyword-abbrev 0))
1702 (idlwave-define-abbrev "sc" "strcompress()" (idlwave-keyword-abbrev 1))
1703 (idlwave-define-abbrev "sn" "strlen()" (idlwave-keyword-abbrev 1))
1704 (idlwave-define-abbrev "sl" "strlowcase()" (idlwave-keyword-abbrev 1))
1705 (idlwave-define-abbrev "su" "strupcase()" (idlwave-keyword-abbrev 1))
1706 (idlwave-define-abbrev "sm" "strmid()" (idlwave-keyword-abbrev 1))
1707 (idlwave-define-abbrev "sp" "strpos()" (idlwave-keyword-abbrev 1))
1708 (idlwave-define-abbrev "st" "strput()" (idlwave-keyword-abbrev 1))
1709 (idlwave-define-abbrev "sr" "strtrim()" (idlwave-keyword-abbrev 1))
1710 (idlwave-define-abbrev "t" "then" (idlwave-keyword-abbrev 0 t))
1711 (idlwave-define-abbrev "u" "until" (idlwave-keyword-abbrev 0 t))
1712 (idlwave-define-abbrev "wu" "writeu," (idlwave-keyword-abbrev 0))
1713 (idlwave-define-abbrev "iap" "if arg_present() then" (idlwave-keyword-abbrev 6))
1714 (idlwave-define-abbrev "ik" "if keyword_set() then" (idlwave-keyword-abbrev 6))
1715 (idlwave-define-abbrev "ine" "if n_elements() eq 0 then" (idlwave-keyword-abbrev 11))
1716 (idlwave-define-abbrev "inn" "if n_elements() ne 0 then" (idlwave-keyword-abbrev 11))
1717 (idlwave-define-abbrev "np" "n_params()" (idlwave-keyword-abbrev 0))
1718 (idlwave-define-abbrev "s" "size()" (idlwave-keyword-abbrev 1))
1719 (idlwave-define-abbrev "wi" "widget_info()" (idlwave-keyword-abbrev 1))
1720 (idlwave-define-abbrev "wc" "widget_control," (idlwave-keyword-abbrev 0))
1721 (idlwave-define-abbrev "pv" "ptr_valid()" (idlwave-keyword-abbrev 1))
1722 (idlwave-define-abbrev "ipv" "if ptr_valid() then" (idlwave-keyword-abbrev 6))
1723
1724 ;; This section is reserved words only. (From IDL user manual)
1725 ;;
1726 (idlwave-define-abbrev "and" "and" (idlwave-keyword-abbrev 0 t) t)
1727 (idlwave-define-abbrev "begin" "begin" (idlwave-keyword-abbrev 0 t) t)
1728 (idlwave-define-abbrev "break" "break" (idlwave-keyword-abbrev 0 t) t)
1729 (idlwave-define-abbrev "case" "case" (idlwave-keyword-abbrev 0 t) t)
1730 (idlwave-define-abbrev "common" "common" (idlwave-keyword-abbrev 0 t) t)
1731 (idlwave-define-abbrev "continue" "continue" (idlwave-keyword-abbrev 0 t) t)
1732 (idlwave-define-abbrev "do" "do" (idlwave-keyword-abbrev 0 t) t)
1733 (idlwave-define-abbrev "else" "else" (idlwave-keyword-abbrev 0 t) t)
1734 (idlwave-define-abbrev "end" "end" 'idlwave-show-begin-check t)
1735 (idlwave-define-abbrev "endcase" "endcase" 'idlwave-show-begin-check t)
1736 (idlwave-define-abbrev "endelse" "endelse" 'idlwave-show-begin-check t)
1737 (idlwave-define-abbrev "endfor" "endfor" 'idlwave-show-begin-check t)
1738 (idlwave-define-abbrev "endif" "endif" 'idlwave-show-begin-check t)
1739 (idlwave-define-abbrev "endrep" "endrep" 'idlwave-show-begin-check t)
1740 (idlwave-define-abbrev "endswitch" "endswitch" 'idlwave-show-begin-check t)
1741 (idlwave-define-abbrev "endwhi" "endwhi" 'idlwave-show-begin-check t)
1742 (idlwave-define-abbrev "endwhile" "endwhile" 'idlwave-show-begin-check t)
1743 (idlwave-define-abbrev "eq" "eq" (idlwave-keyword-abbrev 0 t) t)
1744 (idlwave-define-abbrev "for" "for" (idlwave-keyword-abbrev 0 t) t)
1745 (idlwave-define-abbrev "function" "function" (idlwave-keyword-abbrev 0 t) t)
1746 (idlwave-define-abbrev "ge" "ge" (idlwave-keyword-abbrev 0 t) t)
1747 (idlwave-define-abbrev "goto" "goto" (idlwave-keyword-abbrev 0 t) t)
1748 (idlwave-define-abbrev "gt" "gt" (idlwave-keyword-abbrev 0 t) t)
1749 (idlwave-define-abbrev "if" "if" (idlwave-keyword-abbrev 0 t) t)
1750 (idlwave-define-abbrev "le" "le" (idlwave-keyword-abbrev 0 t) t)
1751 (idlwave-define-abbrev "lt" "lt" (idlwave-keyword-abbrev 0 t) t)
1752 (idlwave-define-abbrev "mod" "mod" (idlwave-keyword-abbrev 0 t) t)
1753 (idlwave-define-abbrev "ne" "ne" (idlwave-keyword-abbrev 0 t) t)
1754 (idlwave-define-abbrev "not" "not" (idlwave-keyword-abbrev 0 t) t)
1755 (idlwave-define-abbrev "of" "of" (idlwave-keyword-abbrev 0 t) t)
1756 (idlwave-define-abbrev "on_ioerror" "on_ioerror" (idlwave-keyword-abbrev 0 t) t)
1757 (idlwave-define-abbrev "or" "or" (idlwave-keyword-abbrev 0 t) t)
1758 (idlwave-define-abbrev "pro" "pro" (idlwave-keyword-abbrev 0 t) t)
1759 (idlwave-define-abbrev "repeat" "repeat" (idlwave-keyword-abbrev 0 t) t)
1760 (idlwave-define-abbrev "switch" "switch" (idlwave-keyword-abbrev 0 t) t)
1761 (idlwave-define-abbrev "then" "then" (idlwave-keyword-abbrev 0 t) t)
1762 (idlwave-define-abbrev "until" "until" (idlwave-keyword-abbrev 0 t) t)
1763 (idlwave-define-abbrev "while" "while" (idlwave-keyword-abbrev 0 t) t)
1764 (idlwave-define-abbrev "xor" "xor" (idlwave-keyword-abbrev 0 t) t)
1765
1766 (defvar imenu-create-index-function)
1767 (defvar extract-index-name-function)
1768 (defvar prev-index-position-function)
1769 (defvar imenu-extract-index-name-function)
1770 (defvar imenu-prev-index-position-function)
1771 ;; defined later - so just make the compiler hush
1772 (defvar idlwave-mode-menu)
1773 (defvar idlwave-mode-debug-menu)
1774
1775 ;;;###autoload
1776 (defun idlwave-mode ()
1777 "Major mode for editing IDL source files (version 6.1_em22).
1778
1779 The main features of this mode are
1780
1781 1. Indentation and Formatting
1782 --------------------------
1783 Like other Emacs programming modes, C-j inserts a newline and indents.
1784 TAB is used for explicit indentation of the current line.
1785
1786 To start a continuation line, use \\[idlwave-split-line]. This
1787 function can also be used in the middle of a line to split the line
1788 at that point. When used inside a long constant string, the string
1789 is split at that point with the `+' concatenation operator.
1790
1791 Comments are indented as follows:
1792
1793 `;;;' Indentation remains unchanged.
1794 `;;' Indent like the surrounding code
1795 `;' Indent to a minimum column.
1796
1797 The indentation of comments starting in column 0 is never changed.
1798
1799 Use \\[idlwave-fill-paragraph] to refill a paragraph inside a
1800 comment. The indentation of the second line of the paragraph
1801 relative to the first will be retained. Use
1802 \\[idlwave-auto-fill-mode] to toggle auto-fill mode for these
1803 comments. When the variable `idlwave-fill-comment-line-only' is
1804 nil, code can also be auto-filled and auto-indented.
1805
1806 To convert pre-existing IDL code to your formatting style, mark the
1807 entire buffer with \\[mark-whole-buffer] and execute
1808 \\[idlwave-expand-region-abbrevs]. Then mark the entire buffer
1809 again followed by \\[indent-region] (`indent-region').
1810
1811 2. Routine Info
1812 ------------
1813 IDLWAVE displays information about the calling sequence and the
1814 accepted keyword parameters of a procedure or function with
1815 \\[idlwave-routine-info]. \\[idlwave-find-module] jumps to the
1816 source file of a module. These commands know about system
1817 routines, all routines in idlwave-mode buffers and (when the
1818 idlwave-shell is active) about all modules currently compiled under
1819 this shell. It also makes use of pre-compiled or custom-scanned
1820 user and library catalogs many popular libraries ship with by
1821 default. Use \\[idlwave-update-routine-info] to update this
1822 information, which is also used for completion (see item 4).
1823
1824 3. Online IDL Help
1825 ---------------
1826
1827 \\[idlwave-context-help] displays the IDL documentation relevant
1828 for the system variable, keyword, or routines at point. A single
1829 key stroke gets you directly to the right place in the docs. See
1830 the manual to configure where and how the HTML help is displayed.
1831
1832 4. Completion
1833 ----------
1834 \\[idlwave-complete] completes the names of procedures, functions
1835 class names, keyword parameters, system variables and tags, class
1836 tags, structure tags, filenames and much more. It is context
1837 sensitive and figures out what is expected at point. Lower case
1838 strings are completed in lower case, other strings in mixed or
1839 upper case.
1840
1841 5. Code Templates and Abbreviations
1842 --------------------------------
1843 Many Abbreviations are predefined to expand to code fragments and templates.
1844 The abbreviations start generally with a `\\`. Some examples:
1845
1846 \\pr PROCEDURE template
1847 \\fu FUNCTION template
1848 \\c CASE statement template
1849 \\sw SWITCH statement template
1850 \\f FOR loop template
1851 \\r REPEAT Loop template
1852 \\w WHILE loop template
1853 \\i IF statement template
1854 \\elif IF-ELSE statement template
1855 \\b BEGIN
1856
1857 For a full list, use \\[idlwave-list-abbrevs]. Some templates also
1858 have direct keybindings - see the list of keybindings below.
1859
1860 \\[idlwave-doc-header] inserts a documentation header at the
1861 beginning of the current program unit (pro, function or main).
1862 Change log entries can be added to the current program unit with
1863 \\[idlwave-doc-modification].
1864
1865 6. Automatic Case Conversion
1866 -------------------------
1867 The case of reserved words and some abbrevs is controlled by
1868 `idlwave-reserved-word-upcase' and `idlwave-abbrev-change-case'.
1869
1870 7. Automatic END completion
1871 ------------------------
1872 If the variable `idlwave-expand-generic-end' is non-nil, each END typed
1873 will be converted to the specific version, like ENDIF, ENDFOR, etc.
1874
1875 8. Hooks
1876 -----
1877 Loading idlwave.el runs `idlwave-load-hook'.
1878 Turning on `idlwave-mode' runs `idlwave-mode-hook'.
1879
1880 9. Documentation and Customization
1881 -------------------------------
1882 Info documentation for this package is available. Use
1883 \\[idlwave-info] to display (complain to your sysadmin if that does
1884 not work). For Postscript, PDF, and HTML versions of the
1885 documentation, check IDLWAVE's homepage at URL `http://idlwave.org'.
1886 IDLWAVE has customize support - see the group `idlwave'.
1887
1888 10.Keybindings
1889 -----------
1890 Here is a list of all keybindings of this mode.
1891 If some of the key bindings below show with ??, use \\[describe-key]
1892 followed by the key sequence to see what the key sequence does.
1893
1894 \\{idlwave-mode-map}"
1895
1896 (interactive)
1897 (kill-all-local-variables)
1898
1899 (if idlwave-startup-message
1900 (message "Emacs IDLWAVE mode version %s." idlwave-mode-version))
1901 (setq idlwave-startup-message nil)
1902
1903 (setq local-abbrev-table idlwave-mode-abbrev-table)
1904 (set-syntax-table idlwave-mode-syntax-table)
1905
1906 (set (make-local-variable 'indent-line-function) 'idlwave-indent-and-action)
1907
1908 (make-local-variable idlwave-comment-indent-function)
1909 (set idlwave-comment-indent-function 'idlwave-comment-hook)
1910
1911 (set (make-local-variable 'comment-start-skip) ";+[ \t]*")
1912 (set (make-local-variable 'comment-start) ";")
1913 (set (make-local-variable 'comment-add) 1) ; ";;" for new and regions
1914 (set (make-local-variable 'require-final-newline) t)
1915 (set (make-local-variable 'abbrev-all-caps) t)
1916 (set (make-local-variable 'indent-tabs-mode) nil)
1917 (set (make-local-variable 'completion-ignore-case) t)
1918
1919 (use-local-map idlwave-mode-map)
1920
1921 (when (featurep 'easymenu)
1922 (easy-menu-add idlwave-mode-menu idlwave-mode-map)
1923 (easy-menu-add idlwave-mode-debug-menu idlwave-mode-map))
1924
1925 (setq mode-name "IDLWAVE")
1926 (setq major-mode 'idlwave-mode)
1927 (setq abbrev-mode t)
1928
1929 (set (make-local-variable idlwave-fill-function) 'idlwave-auto-fill)
1930 (setq comment-end "")
1931 (set (make-local-variable 'comment-multi-line) nil)
1932 (set (make-local-variable 'paragraph-separate)
1933 "[ \t\f]*$\\|[ \t]*;+[ \t]*$\\|;+[+=-_*]+$")
1934 (set (make-local-variable 'paragraph-start) "[ \t\f]\\|[ \t]*;+[ \t]")
1935 (set (make-local-variable 'paragraph-ignore-fill-prefix) nil)
1936 (set (make-local-variable 'parse-sexp-ignore-comments) t)
1937
1938 ;; ChangeLog
1939 (set (make-local-variable 'add-log-current-defun-function)
1940 'idlwave-current-routine-fullname)
1941
1942 ;; Set tag table list to use IDLTAGS as file name.
1943 (if (boundp 'tag-table-alist)
1944 (add-to-list 'tag-table-alist '("\\.pro$" . "IDLTAGS")))
1945
1946 ;; Font-lock additions
1947 ;; Following line is for Emacs - XEmacs uses the corresponding property
1948 ;; on the `idlwave-mode' symbol.
1949 (set (make-local-variable 'font-lock-defaults) idlwave-font-lock-defaults)
1950 (set (make-local-variable 'font-lock-mark-block-function)
1951 'idlwave-mark-subprogram)
1952 (set (make-local-variable 'font-lock-fontify-region-function)
1953 'idlwave-font-lock-fontify-region)
1954
1955 ;; Imenu setup
1956 (set (make-local-variable 'imenu-create-index-function)
1957 'imenu-default-create-index-function)
1958 (set (make-local-variable 'imenu-extract-index-name-function)
1959 'idlwave-unit-name)
1960 (set (make-local-variable 'imenu-prev-index-position-function)
1961 'idlwave-prev-index-position)
1962
1963 ;; HideShow setup
1964 (add-to-list 'hs-special-modes-alist
1965 (list 'idlwave-mode
1966 idlwave-begin-block-reg
1967 idlwave-end-block-reg
1968 ";"
1969 'idlwave-forward-block nil))
1970
1971 ;; Make a local post-command-hook and add our hook to it
1972 ;; NB: `make-local-hook' needed for older/alternative Emacs compatibility
1973 ;; (make-local-hook 'post-command-hook)
1974 (add-hook 'post-command-hook 'idlwave-command-hook nil 'local)
1975
1976 ;; Make local hooks for buffer updates
1977 ;; NB: `make-local-hook' needed for older/alternative Emacs compatibility
1978 ;; (make-local-hook 'kill-buffer-hook)
1979 (add-hook 'kill-buffer-hook 'idlwave-kill-buffer-update nil 'local)
1980 ;; (make-local-hook 'after-save-hook)
1981 (add-hook 'after-save-hook 'idlwave-save-buffer-update nil 'local)
1982 (add-hook 'after-save-hook 'idlwave-revoke-license-to-kill nil 'local)
1983
1984 ;; Setup directories and file, if necessary
1985 (idlwave-setup)
1986
1987 ;; Update the routine info with info about current buffer?
1988 (idlwave-new-buffer-update)
1989
1990 ;; Check help location
1991 (idlwave-help-check-locations)
1992
1993 ;; Run the mode hook
1994 (run-mode-hooks 'idlwave-mode-hook))
1995
1996 (defvar idlwave-setup-done nil)
1997 (defun idlwave-setup ()
1998 (unless idlwave-setup-done
1999 (if (not (file-directory-p idlwave-config-directory))
2000 (make-directory idlwave-config-directory))
2001 (setq
2002 idlwave-user-catalog-file (expand-file-name
2003 idlwave-user-catalog-file
2004 idlwave-config-directory)
2005 idlwave-xml-system-rinfo-converted-file
2006 (expand-file-name
2007 idlwave-xml-system-rinfo-converted-file
2008 idlwave-config-directory)
2009 idlwave-path-file (expand-file-name
2010 idlwave-path-file
2011 idlwave-config-directory))
2012 (idlwave-read-paths) ; we may need these early
2013 (setq idlwave-setup-done t)))
2014
2015 (defun idlwave-font-lock-fontify-region (beg end &optional verbose)
2016 "Fontify continuation lines correctly."
2017 (let (pos)
2018 (save-excursion
2019 (goto-char beg)
2020 (forward-line -1)
2021 (when (setq pos (idlwave-is-continuation-line))
2022 (goto-char pos)
2023 (idlwave-beginning-of-statement)
2024 (setq beg (point)))))
2025 (font-lock-default-fontify-region beg end verbose))
2026
2027 ;;
2028 ;; Code Formatting ----------------------------------------------------
2029 ;;
2030
2031 (defun idlwave-hard-tab ()
2032 "Insert TAB in buffer in current position."
2033 (interactive)
2034 (insert "\t"))
2035
2036 ;;; This stuff is experimental
2037
2038 (defvar idlwave-command-hook nil
2039 "If non-nil, a list that can be evaluated using `eval'.
2040 It is evaluated in the lisp function `idlwave-command-hook' which is
2041 placed in `post-command-hook'.")
2042
2043 (defun idlwave-command-hook ()
2044 "Command run after every command.
2045 Evaluates a non-nil value of the *variable* `idlwave-command-hook' and
2046 sets the variable to zero afterwards."
2047 (and idlwave-command-hook
2048 (listp idlwave-command-hook)
2049 (condition-case nil
2050 (eval idlwave-command-hook)
2051 (error nil)))
2052 (setq idlwave-command-hook nil))
2053
2054 ;;; End experiment
2055
2056 ;; It would be better to use expand.el for better abbrev handling and
2057 ;; versatility.
2058
2059 (defun idlwave-check-abbrev (arg &optional reserved)
2060 "Reverse abbrev expansion if in comment or string.
2061 Argument ARG is the number of characters to move point
2062 backward if `idlwave-abbrev-move' is non-nil.
2063 If optional argument RESERVED is non-nil then the expansion
2064 consists of reserved words, which will be capitalized if
2065 `idlwave-reserved-word-upcase' is non-nil.
2066 Otherwise, the abbrev will be capitalized if `idlwave-abbrev-change-case'
2067 is non-nil, unless its value is \`down in which case the abbrev will be
2068 made into all lowercase.
2069 Returns non-nil if abbrev is left expanded."
2070 (if (idlwave-quoted)
2071 (progn (unexpand-abbrev)
2072 nil)
2073 (if (and reserved idlwave-reserved-word-upcase)
2074 (upcase-region last-abbrev-location (point))
2075 (cond
2076 ((equal idlwave-abbrev-change-case 'down)
2077 (downcase-region last-abbrev-location (point)))
2078 (idlwave-abbrev-change-case
2079 (upcase-region last-abbrev-location (point)))))
2080 (if (and idlwave-abbrev-move (> arg 0))
2081 (if (boundp 'post-command-hook)
2082 (setq idlwave-command-hook (list 'backward-char (1+ arg)))
2083 (backward-char arg)))
2084 t))
2085
2086 (defun idlwave-in-comment ()
2087 "Return t if point is inside a comment, nil otherwise."
2088 (save-excursion
2089 (let ((here (point)))
2090 (and (idlwave-goto-comment) (> here (point))))))
2091
2092 (defun idlwave-goto-comment ()
2093 "Move to start of comment delimiter on current line.
2094 Moves to end of line if there is no comment delimiter.
2095 Ignores comment delimiters in strings.
2096 Returns point if comment found and nil otherwise."
2097 (let ((eos (progn (end-of-line) (point)))
2098 (data (match-data))
2099 found)
2100 ;; Look for first comment delimiter not in a string
2101 (beginning-of-line)
2102 (setq found (search-forward comment-start eos 'lim))
2103 (while (and found (idlwave-in-quote))
2104 (setq found (search-forward comment-start eos 'lim)))
2105 (store-match-data data)
2106 (and found (not (idlwave-in-quote))
2107 (progn
2108 (backward-char 1)
2109 (point)))))
2110
2111 (defun idlwave-region-active-p ()
2112 "Should we operate on an active region?"
2113 (if (fboundp 'use-region-p)
2114 (use-region-p)
2115 (region-active-p)))
2116
2117 (defun idlwave-show-matching-quote ()
2118 "Insert quote and show matching quote if this is end of a string."
2119 (interactive)
2120 (let ((bq (idlwave-in-quote))
2121 (inq last-command-event))
2122 (if (and bq (not (idlwave-in-comment)))
2123 (let ((delim (char-after bq)))
2124 (insert inq)
2125 (if (eq inq delim)
2126 (save-excursion
2127 (goto-char bq)
2128 (sit-for 1))))
2129 ;; Not the end of a string
2130 (insert inq))))
2131
2132 (defun idlwave-show-begin-check ()
2133 "Ensure that the previous word was a token before `idlwave-show-begin'.
2134 An END token must be preceded by whitespace."
2135 (if (not (idlwave-quoted))
2136 (if
2137 (save-excursion
2138 (backward-word 1)
2139 (backward-char 1)
2140 (looking-at "[ \t\n\f]"))
2141 (idlwave-show-begin))))
2142
2143 (defun idlwave-show-begin ()
2144 "Find the start of current block and blinks to it for a second.
2145 Also checks if the correct END statement has been used."
2146 ;; All end statements are reserved words
2147 ;; Re-indent end line
2148 ;;(insert-char ?\ 1) ;; So indent, etc. work well
2149 ;;(backward-char 1)
2150 (let* ((pos (point-marker))
2151 (last-abbrev-marker (copy-marker last-abbrev-location))
2152 (eol-pos (save-excursion (end-of-line) (point)))
2153 begin-pos end-pos end end1 )
2154 (if idlwave-reindent-end (idlwave-indent-line))
2155 (setq last-abbrev-location (marker-position last-abbrev-marker))
2156 (when (and (idlwave-check-abbrev 0 t)
2157 idlwave-show-block)
2158 (save-excursion
2159 ;; Move inside current block
2160 (goto-char last-abbrev-marker)
2161 (idlwave-block-jump-out -1 'nomark)
2162 (setq begin-pos (point))
2163 (idlwave-block-jump-out 1 'nomark)
2164 (setq end-pos (point))
2165 (if (> end-pos eol-pos)
2166 (setq end-pos pos))
2167 (goto-char end-pos)
2168 (setq end (buffer-substring
2169 (progn
2170 (skip-chars-backward "a-zA-Z")
2171 (point))
2172 end-pos))
2173 (goto-char begin-pos)
2174 (when (setq end1 (cdr (idlwave-block-master)))
2175 (cond
2176 ((null end1)) ; no-operation
2177 ((string= (downcase end) (downcase end1))
2178 (sit-for 1))
2179 ((string= (downcase end) "end")
2180 ;; A generic end
2181 (if idlwave-expand-generic-end
2182 (save-excursion
2183 (goto-char pos)
2184 (backward-char 3)
2185 (insert (if (string= end "END") (upcase end1) end1))
2186 (delete-char 3)))
2187 (sit-for 1))
2188 (t
2189 (beep)
2190 (message "Warning: Shouldn't this be \"%s\" instead of \"%s\"?"
2191 end1 end)
2192 (sit-for 1))))))))
2193 ;;(delete-char 1))
2194
2195 (defun idlwave-block-master ()
2196 (let ((case-fold-search t))
2197 (save-excursion
2198 (cond
2199 ((looking-at "pro\\|case\\|switch\\|function\\>")
2200 (assoc (downcase (match-string 0)) idlwave-block-matches))
2201 ((looking-at "begin\\>")
2202 (let ((limit (save-excursion
2203 (idlwave-beginning-of-statement)
2204 (point))))
2205 (cond
2206 ((re-search-backward ":[ \t]*\\=" limit t)
2207 ;; seems to be a case thing
2208 '("begin" . "end"))
2209 ((re-search-backward idlwave-block-match-regexp limit t)
2210 (assoc (downcase (match-string 1))
2211 idlwave-block-matches))
2212 (t
2213 ;; Just a normal block
2214 '("begin" . "end")))))
2215 (t nil)))))
2216
2217 (defun idlwave-close-block ()
2218 "Terminate the current block with the correct END statement."
2219 (interactive)
2220 ;; Start new line if we are not in a new line
2221 (unless (save-excursion
2222 (skip-chars-backward " \t")
2223 (bolp))
2224 (let ((idlwave-show-block nil))
2225 (newline-and-indent)))
2226 (let ((last-abbrev-location (point))) ; for upcasing
2227 (insert "end")
2228 (idlwave-show-begin)))
2229
2230 (defun idlwave-custom-ampersand-surround (&optional is-action)
2231 "Surround &, leaving room for && (which surround as well)."
2232 (let* ((prev-char (char-after (- (point) 2)))
2233 (next-char (char-after (point)))
2234 (amp-left (eq prev-char ?&))
2235 (amp-right (eq next-char ?&))
2236 (len (if amp-left 2 1)))
2237 (unless amp-right ;no need to do it twice, amp-left will catch it.
2238 (idlwave-surround -1 (if (or is-action amp-left) -1) len))))
2239
2240 (defun idlwave-custom-ltgtr-surround (gtr &optional is-action)
2241 "Surround > and < by blanks, leaving room for >= and <=, and considering ->."
2242 (let* ((prev-char (char-after (- (point) 2)))
2243 (next-char (char-after (point)))
2244 (method-invoke (and gtr (eq prev-char ?-)))
2245 (len (if method-invoke 2 1)))
2246 (unless (eq next-char ?=)
2247 ;; Key binding: pad only on left, to save for possible >=/<=
2248 (idlwave-surround -1 (if (or is-action method-invoke) -1) len))))
2249
2250 (defun idlwave-surround (&optional before after length is-action)
2251 "Surround the LENGTH characters before point with blanks.
2252 LENGTH defaults to 1.
2253 Optional arguments BEFORE and AFTER affect the behavior before and
2254 after the characters (see also description of `idlwave-make-space'):
2255
2256 nil do nothing
2257 0 force no spaces
2258 integer > 0 force exactly n spaces
2259 integer < 0 at least |n| spaces
2260
2261 The function does nothing if any of the following conditions is true:
2262 - `idlwave-surround-by-blank' is nil
2263 - the character before point is inside a string or comment"
2264 (when (and idlwave-surround-by-blank (not (idlwave-quoted)))
2265 (let ((length (or length 1))) ; establish a default for LENGTH
2266 (backward-char length)
2267 (save-restriction
2268 (let ((here (point)))
2269 (skip-chars-backward " \t")
2270 (if (bolp)
2271 ;; avoid clobbering indent
2272 (progn
2273 (move-to-column (idlwave-calculate-indent))
2274 (if (<= (point) here)
2275 (narrow-to-region (point) here))
2276 (goto-char here)))
2277 (idlwave-make-space before))
2278 (skip-chars-forward " \t"))
2279 (forward-char length)
2280 (idlwave-make-space after)
2281 ;; Check to see if the line should auto wrap
2282 (if (and (equal (char-after (1- (point))) ?\ )
2283 (> (current-column) fill-column))
2284 (funcall auto-fill-function)))))
2285
2286 (defun idlwave-make-space (n)
2287 "Make space at point.
2288 The space affected is all the spaces and tabs around point.
2289 If n is non-nil then point is left abs(n) spaces from the beginning of
2290 the contiguous space.
2291 The amount of space at point is determined by N.
2292 If the value of N is:
2293 nil - do nothing.
2294 > 0 - exactly N spaces.
2295 < 0 - a minimum of -N spaces, i.e., do not change if there are
2296 already -N spaces.
2297 0 - no spaces (i.e. remove any existing space)."
2298 (if (integerp n)
2299 (let
2300 ((start-col (progn (skip-chars-backward " \t") (current-column)))
2301 (left (point))
2302 (end-col (progn (skip-chars-forward " \t") (current-column))))
2303 (delete-horizontal-space)
2304 (cond
2305 ((> n 0)
2306 (idlwave-indent-to (+ start-col n))
2307 (goto-char (+ left n)))
2308 ((< n 0)
2309 (idlwave-indent-to end-col (- n))
2310 (goto-char (- left n)))
2311 ;; n = 0, done
2312 ))))
2313
2314 (defun idlwave-newline ()
2315 "Insert a newline and indent the current and previous line."
2316 (interactive)
2317 ;;
2318 ;; Handle unterminated single and double quotes
2319 ;; If not in a comment and in a string then insertion of a newline
2320 ;; will mean unbalanced quotes.
2321 ;;
2322 (if (and (not (idlwave-in-comment)) (idlwave-in-quote))
2323 (progn (beep)
2324 (message "Warning: unbalanced quotes?")))
2325 (newline)
2326 ;;
2327 ;; The current line is being split, the cursor should be at the
2328 ;; beginning of the new line skipping the leading indentation.
2329 ;;
2330 ;; The reason we insert the new line before indenting is that the
2331 ;; indenting could be confused by keywords (e.g. END) on the line
2332 ;; after the split point. This prevents us from just using
2333 ;; `indent-for-tab-command' followed by `newline-and-indent'.
2334 ;;
2335 (beginning-of-line 0)
2336 (idlwave-indent-line)
2337 (forward-line)
2338 (idlwave-indent-line))
2339
2340 ;;
2341 ;; Use global variable 'comment-column' to set parallel comment
2342 ;;
2343 ;; Modeled on lisp.el
2344 ;; Emacs Lisp and IDL (Wave CL) have identical comment syntax
2345 (defun idlwave-comment-hook ()
2346 "Compute indent for the beginning of the IDL comment delimiter."
2347 (if (or (looking-at idlwave-no-change-comment)
2348 (if idlwave-begin-line-comment
2349 (looking-at idlwave-begin-line-comment)
2350 (looking-at "^;")))
2351 (current-column)
2352 (if (looking-at idlwave-code-comment)
2353 (if (save-excursion (skip-chars-backward " \t") (bolp))
2354 ;; On line by itself, indent as code
2355 (let ((tem (idlwave-calculate-indent)))
2356 (if (listp tem) (car tem) tem))
2357 ;; after code - do not change
2358 (current-column))
2359 (skip-chars-backward " \t")
2360 (max (if (bolp) 0 (1+ (current-column)))
2361 comment-column))))
2362
2363 (defun idlwave-split-line ()
2364 "Continue line by breaking line at point and indent the lines.
2365 For a code line insert continuation marker. If the line is a line comment
2366 then the new line will contain a comment with the same indentation.
2367 Splits strings with the IDL operator `+' if `idlwave-split-line-string' is
2368 non-nil."
2369 (interactive)
2370 ;; Expand abbreviation, just like normal RET would.
2371 (and abbrev-mode (expand-abbrev))
2372 (let (beg)
2373 (if (not (idlwave-in-comment))
2374 ;; For code line add continuation.
2375 ;; Check if splitting a string.
2376 (progn
2377 (if (setq beg (idlwave-in-quote))
2378 (if idlwave-split-line-string
2379 ;; Split the string.
2380 (progn (insert (setq beg (char-after beg)) " + "
2381 idlwave-continuation-char beg)
2382 (backward-char 1)
2383 (newline-and-indent)
2384 (forward-char 1))
2385 ;; Do not split the string.
2386 (beep)
2387 (message "Warning: continuation inside string!!")
2388 (insert " " idlwave-continuation-char))
2389 ;; Not splitting a string.
2390 (if (not (member (char-before) '(?\ ?\t)))
2391 (insert " "))
2392 (insert idlwave-continuation-char)
2393 (newline-and-indent)))
2394 (indent-new-comment-line))
2395 ;; Indent previous line
2396 (setq beg (- (point-max) (point)))
2397 (forward-line -1)
2398 (idlwave-indent-line)
2399 (goto-char (- (point-max) beg))
2400 ;; Reindent new line
2401 (idlwave-indent-line)))
2402
2403 (defun idlwave-beginning-of-subprogram (&optional nomark)
2404 "Move point to the beginning of the current program unit.
2405 If NOMARK is non-nil, do not push mark."
2406 (interactive)
2407 (idlwave-find-key idlwave-begin-unit-reg -1 nomark))
2408
2409 (defun idlwave-end-of-subprogram (&optional nomark)
2410 "Move point to the start of the next program unit.
2411 If NOMARK is non-nil, do not push mark."
2412 (interactive)
2413 (idlwave-end-of-statement)
2414 (idlwave-find-key idlwave-end-unit-reg 1 nomark))
2415
2416 (defun idlwave-mark-statement ()
2417 "Mark current IDL statement."
2418 (interactive)
2419 (idlwave-end-of-statement)
2420 (let ((end (point)))
2421 (idlwave-beginning-of-statement)
2422 (push-mark end nil t)))
2423
2424 (defun idlwave-mark-block ()
2425 "Mark containing block."
2426 (interactive)
2427 (idlwave-end-of-statement)
2428 (idlwave-backward-up-block -1)
2429 (idlwave-end-of-statement)
2430 (let ((end (point)))
2431 (idlwave-backward-block)
2432 (idlwave-beginning-of-statement)
2433 (push-mark end nil t)))
2434
2435
2436 (defun idlwave-mark-subprogram ()
2437 "Put mark at beginning of program, point at end.
2438 The marks are pushed."
2439 (interactive)
2440 (idlwave-end-of-statement)
2441 (idlwave-beginning-of-subprogram)
2442 (let ((beg (point)))
2443 (idlwave-forward-block)
2444 (push-mark beg nil t))
2445 (exchange-point-and-mark))
2446
2447 (defun idlwave-backward-up-block (&optional arg)
2448 "Move to beginning of enclosing block if prefix ARG >= 0.
2449 If prefix ARG < 0 then move forward to enclosing block end."
2450 (interactive "p")
2451 (idlwave-block-jump-out (- arg) 'nomark))
2452
2453 (defun idlwave-beginning-of-block ()
2454 "Go to the beginning of the current block."
2455 (interactive)
2456 (idlwave-block-jump-out -1 'nomark)
2457 (forward-word 1))
2458
2459 (defun idlwave-end-of-block ()
2460 "Go to the beginning of the current block."
2461 (interactive)
2462 (idlwave-block-jump-out 1 'nomark)
2463 (backward-word 1))
2464
2465 (defun idlwave-forward-block (&optional arg)
2466 "Move across next nested block."
2467 (interactive)
2468 (let ((arg (or arg 1)))
2469 (if (idlwave-down-block arg)
2470 (idlwave-block-jump-out arg 'nomark))))
2471
2472 (defun idlwave-backward-block ()
2473 "Move backward across previous nested block."
2474 (interactive)
2475 (if (idlwave-down-block -1)
2476 (idlwave-block-jump-out -1 'nomark)))
2477
2478 (defun idlwave-down-block (&optional arg)
2479 "Go down a block.
2480 With ARG: ARG >= 0 go forwards, ARG < 0 go backwards.
2481 Returns non-nil if successfull."
2482 (interactive "p")
2483 (let (status)
2484 (if (< arg 0)
2485 ;; Backward
2486 (let ((eos (save-excursion
2487 (idlwave-block-jump-out -1 'nomark)
2488 (point))))
2489 (if (setq status (idlwave-find-key
2490 idlwave-end-block-reg -1 'nomark eos))
2491 (idlwave-beginning-of-statement)
2492 (message "No nested block before beginning of containing block.")))
2493 ;; Forward
2494 (let ((eos (save-excursion
2495 (idlwave-block-jump-out 1 'nomark)
2496 (point))))
2497 (if (setq status (idlwave-find-key
2498 idlwave-begin-block-reg 1 'nomark eos))
2499 (idlwave-end-of-statement)
2500 (message "No nested block before end of containing block."))))
2501 status))
2502
2503 (defun idlwave-mark-doclib ()
2504 "Put point at beginning of doc library header, mark at end.
2505 The marks are pushed."
2506 (interactive)
2507 (let (beg
2508 (here (point)))
2509 (goto-char (point-max))
2510 (if (re-search-backward idlwave-doclib-start nil t)
2511 (progn
2512 (setq beg (progn (beginning-of-line) (point)))
2513 (if (re-search-forward idlwave-doclib-end nil t)
2514 (progn
2515 (forward-line 1)
2516 (push-mark beg nil t)
2517 (message "Could not find end of doc library header.")))
2518 (message "Could not find doc library header start.")
2519 (goto-char here)))))
2520
2521 (defun idlwave-current-routine-fullname ()
2522 (let ((name (idlwave-current-routine)))
2523 (idlwave-make-full-name (nth 2 name) (car name))))
2524
2525 (defun idlwave-current-routine ()
2526 "Return (NAME TYPE CLASS) of current routine."
2527 (idlwave-routines)
2528 (save-excursion
2529 (idlwave-beginning-of-subprogram 'nomark)
2530 (if (looking-at "[ \t]*\\<\\(pro\\|function\\)\\>\\s-+\\(\\([a-zA-Z0-9$_]+\\)::\\)?\\([a-zA-Z0-9$_]+\\)")
2531 (let* ((type (if (string= (downcase (match-string 1)) "pro")
2532 'pro 'function))
2533 (class (idlwave-sintern-class (match-string 3)))
2534 (name (idlwave-sintern-routine-or-method (match-string 4) class)))
2535 (list name type class)))))
2536
2537 (defvar idlwave-shell-prompt-pattern)
2538 (defun idlwave-beginning-of-statement ()
2539 "Move to beginning of the current statement.
2540 Skips back past statement continuations.
2541 Point is placed at the beginning of the line whether or not this is an
2542 actual statement."
2543 (interactive)
2544 (cond
2545 ((eq major-mode 'idlwave-shell-mode)
2546 (if (re-search-backward idlwave-shell-prompt-pattern nil t)
2547 (goto-char (match-end 0))))
2548 (t
2549 (if (save-excursion (forward-line -1) (idlwave-is-continuation-line))
2550 (idlwave-previous-statement)
2551 (beginning-of-line)))))
2552
2553 (defun idlwave-previous-statement ()
2554 "Move point to beginning of the previous statement.
2555 Returns t if the current line before moving is the beginning of
2556 the first non-comment statement in the file, and nil otherwise."
2557 (interactive)
2558 (let (first-statement)
2559 (if (not (= (forward-line -1) 0))
2560 ;; first line in file
2561 t
2562 ;; skip blank lines, label lines, include lines and line comments
2563 (while (and
2564 ;; The current statement is the first statement until we
2565 ;; reach another statement.
2566 (setq first-statement
2567 (or
2568 (looking-at idlwave-comment-line-start-skip)
2569 (looking-at "[ \t]*$")
2570 (looking-at (concat "[ \t]*" idlwave-label "[ \t]*$"))
2571 (looking-at "^@")))
2572 (= (forward-line -1) 0)))
2573 ;; skip continuation lines
2574 (while (and
2575 (save-excursion
2576 (forward-line -1)
2577 (idlwave-is-continuation-line))
2578 (= (forward-line -1) 0)))
2579 first-statement)))
2580
2581 (defun idlwave-end-of-statement ()
2582 "Move point to the end of the current IDL statement.
2583 If not in a statement just moves to end of line. Returns position."
2584 (interactive)
2585 (while (and (idlwave-is-continuation-line)
2586 (= (forward-line 1) 0))
2587 (while (and (idlwave-is-comment-or-empty-line)
2588 (= (forward-line 1) 0))))
2589 (end-of-line)
2590 (point))
2591
2592 (defun idlwave-end-of-statement0 ()
2593 "Move point to the end of the current IDL statement.
2594 If not in a statement just moves to end of line. Returns position."
2595 (interactive)
2596 (while (and (idlwave-is-continuation-line)
2597 (= (forward-line 1) 0)))
2598 (end-of-line)
2599 (point))
2600
2601 (defun idlwave-next-statement ()
2602 "Move point to beginning of the next IDL statement.
2603 Returns t if that statement is the last non-comment IDL statement
2604 in the file, and nil otherwise."
2605 (interactive)
2606 (let (last-statement)
2607 (idlwave-end-of-statement)
2608 ;; skip blank lines, label lines, include lines and line comments
2609 (while (and (= (forward-line 1) 0)
2610 ;; The current statement is the last statement until
2611 ;; we reach a new statement.
2612 (setq last-statement
2613 (or
2614 (looking-at idlwave-comment-line-start-skip)
2615 (looking-at "[ \t]*$")
2616 (looking-at (concat "[ \t]*" idlwave-label "[ \t]*$"))
2617 (looking-at "^@")))))
2618 last-statement))
2619
2620 (defun idlwave-skip-multi-commands (&optional lim)
2621 "Skip past multiple commands on a line (with `&')."
2622 (let ((save-point (point)))
2623 (when (re-search-forward ".*&" lim t)
2624 (goto-char (match-end 0))
2625 (if (idlwave-quoted)
2626 (goto-char save-point)
2627 (if (eq (char-after (- (point) 2)) ?&) (goto-char save-point))))
2628 (point)))
2629
2630 (defun idlwave-skip-label-or-case ()
2631 "Skip label or case statement element.
2632 Returns position after label.
2633 If there is no label point is not moved and nil is returned."
2634 ;; Case expressions and labels are terminated by a colon.
2635 ;; So we find the first colon in the line and make sure
2636 ;; - no `?' is before it (might be a ? b : c)
2637 ;; - it is not in a comment
2638 ;; - not in a string constant
2639 ;; - not in parenthesis (like a[0:3])
2640 ;; - not followed by another ":" in explicit class, ala a->b::c
2641 ;; As many in this mode, this function is heuristic and not an exact
2642 ;; parser.
2643 (let* ((start (point))
2644 (eos (save-excursion (idlwave-end-of-statement) (point)))
2645 (end (idlwave-find-key ":" 1 'nomark eos)))
2646 (if (and end
2647 (= (nth 0 (parse-partial-sexp start end)) 0)
2648 (not (string-match "\\?" (buffer-substring start end)))
2649 (not (string-match "^::" (buffer-substring end eos))))
2650 (progn
2651 (forward-char)
2652 (point))
2653 (goto-char start)
2654 nil)))
2655
2656 (defun idlwave-start-of-substatement (&optional pre)
2657 "Move to start of next IDL substatement after point.
2658 Uses the type of the current IDL statement to determine if the next
2659 statement is on a new line or is a subpart of the current statement.
2660 Returns point at start of substatement modulo whitespace.
2661 If optional argument is non-nil move to beginning of current
2662 substatement."
2663 (let ((orig (point))
2664 (eos (idlwave-end-of-statement))
2665 (ifnest 0)
2666 st nst last)
2667 (idlwave-beginning-of-statement)
2668 (idlwave-skip-label-or-case)
2669 (if (< (point) orig)
2670 (idlwave-skip-multi-commands orig))
2671 (setq last (point))
2672 ;; Continue looking for substatements until we are past orig
2673 (while (and (<= (point) orig) (not (eobp)))
2674 (setq last (point))
2675 (setq nst (nth 1 (cdr (setq st (car (idlwave-statement-type))))))
2676 (if (equal (car st) 'if) (setq ifnest (1+ ifnest)))
2677 (cond ((and nst
2678 (idlwave-find-key nst 1 'nomark eos))
2679 (goto-char (match-end 0)))
2680 ((and (> ifnest 0) (idlwave-find-key "\\<else\\>" 1 'nomark eos))
2681 (setq ifnest (1- ifnest))
2682 (goto-char (match-end 0)))
2683 (t (setq ifnest 0)
2684 (idlwave-next-statement))))
2685 (if pre (goto-char last))
2686 ;; If a continuation line starts here, move to next line
2687 (if (looking-at "[ \t]*\\$\\([ \t]*\\(;\\|$\\)\\)")
2688 (beginning-of-line 2))
2689 (point)))
2690
2691 (defun idlwave-statement-type ()
2692 "Return the type of the current IDL statement.
2693 Uses `idlwave-statement-match' to return a cons of (type . point) with
2694 point the ending position where the type was determined. Type is the
2695 association from `idlwave-statement-match', i.e. the cons cell from the
2696 list not just the type symbol. Returns nil if not an identifiable
2697 statement."
2698 (save-excursion
2699 ;; Skip whitespace within a statement which is spaces, tabs, continuations
2700 ;; and possibly comments
2701 (while (looking-at "[ \t]*\\$")
2702 (forward-line 1))
2703 (skip-chars-forward " \t")
2704 (let ((st idlwave-statement-match)
2705 (case-fold-search t))
2706 (while (and (not (looking-at (nth 0 (cdr (car st)))))
2707 (setq st (cdr st))))
2708 (if st
2709 (append st (match-end 0))))))
2710
2711 (defun idlwave-expand-equal (&optional before after is-action)
2712 "Pad '=' with spaces.
2713 Two cases: Assignment statement, and keyword assignment.
2714 Which case is determined using `idlwave-start-of-substatement' and
2715 `idlwave-statement-type'. The equal sign will be surrounded by BEFORE
2716 and AFTER blanks. If `idlwave-pad-keyword' is t then keyword assignment
2717 is treated just like assignment statements. When nil, spaces are
2718 removed for keyword assignment. Any other value keeps the current space
2719 around the `='. Limits in for loops are treated as keyword assignment.
2720
2721 Starting with IDL 6.0, a number of op= assignments are available.
2722 Since ambiguities of the form:
2723
2724 r and= b
2725 rand= b
2726
2727 can occur, alphanumeric operator assignment will never be pre-padded,
2728 only post-padded. You must use a space before these to disambiguate
2729 \(not just for padding, but for proper parsing by IDL too!). Other
2730 operators, such as ##=, ^=, etc., will be pre-padded.
2731
2732 IS-ACTION is ignored.
2733
2734 See `idlwave-surround'."
2735 (if idlwave-surround-by-blank
2736 (let
2737 ((non-an-ops "\\(##\\|\\*\\|\\+\\|-\\|/\\|<\\|>\\|\\^\\)\\=")
2738 (an-ops
2739 "\\s-\\(AND\\|EQ\\|GE\\|GT\\|LE\\|LT\\|MOD\\|NE\\|OR\\|XOR\\)\\=")
2740 (len 1))
2741
2742 (save-excursion
2743 (let ((case-fold-search t))
2744 (backward-char)
2745 (if (or
2746 (re-search-backward non-an-ops nil t)
2747 ;; Why doesn't ##? work for both?
2748 (re-search-backward "\\(#\\)\\=" nil t))
2749 (setq len (1+ (length (match-string 1))))
2750 (when (re-search-backward an-ops nil t)
2751 ;(setq begin nil) ; won't modify begin
2752 (setq len (1+ (length (match-string 1))))))))
2753
2754 (if (eq t idlwave-pad-keyword)
2755 ;; Everything gets padded equally
2756 (idlwave-surround before after len)
2757 ;; Treating keywords/for variables specially...
2758 (let ((st (save-excursion ; To catch "for" variables
2759 (idlwave-start-of-substatement t)
2760 (idlwave-statement-type)))
2761 (what (save-excursion ; To catch keywords
2762 (skip-chars-backward "= \t")
2763 (nth 2 (idlwave-where)))))
2764 (cond ((or (memq what '(function-keyword procedure-keyword))
2765 (memq (caar st) '(for pdef)))
2766 (cond
2767 ((null idlwave-pad-keyword)
2768 (idlwave-surround 0 0)
2769 ) ; remove space
2770 (t))) ; leave any spaces alone
2771 (t (idlwave-surround before after len))))))))
2772
2773
2774 (defun idlwave-indent-and-action (&optional arg)
2775 "Call `idlwave-indent-line' and do expand actions.
2776 With prefix ARG non-nil, indent the entire sub-statement."
2777 (interactive "p")
2778 (save-excursion
2779 (if (and idlwave-expand-generic-end
2780 (re-search-backward "\\<\\(end\\)\\s-*\\="
2781 (max 0 (- (point) 10)) t)
2782 (looking-at "\\(end\\)\\([ \n\t]\\|\\'\\)"))
2783 (progn (goto-char (match-end 1))
2784 ;;Expand the END abbreviation, just as RET or Space would have.
2785 (if abbrev-mode (expand-abbrev)
2786 (idlwave-show-begin)))))
2787 (when (and (not arg) current-prefix-arg)
2788 (setq arg current-prefix-arg)
2789 (setq current-prefix-arg nil))
2790 (if arg
2791 (idlwave-indent-statement)
2792 (idlwave-indent-line t)))
2793
2794 (defun idlwave-indent-line (&optional expand)
2795 "Indent current IDL line as code or as a comment.
2796 The actions in `idlwave-indent-action-table' are performed.
2797 If the optional argument EXPAND is non-nil then the actions in
2798 `idlwave-indent-expand-table' are performed."
2799 (interactive)
2800 ;; Move point out of left margin.
2801 (if (save-excursion
2802 (skip-chars-backward " \t")
2803 (bolp))
2804 (skip-chars-forward " \t"))
2805 (let ((mloc (point-marker)))
2806 (save-excursion
2807 (beginning-of-line)
2808 (if (looking-at idlwave-comment-line-start-skip)
2809 ;; Indentation for a line comment
2810 (progn
2811 (skip-chars-forward " \t")
2812 (idlwave-indent-left-margin (idlwave-comment-hook)))
2813 ;;
2814 ;; Code Line
2815 ;;
2816 ;; Before indenting, run action routines.
2817 ;;
2818 (if (and expand idlwave-do-actions)
2819 (mapc 'idlwave-do-action idlwave-indent-expand-table))
2820 ;;
2821 (if idlwave-do-actions
2822 (mapc 'idlwave-do-action idlwave-indent-action-table))
2823 ;;
2824 ;; No longer expand abbrevs on the line. The user can do this
2825 ;; manually using expand-region-abbrevs.
2826 ;;
2827 ;; Indent for code line
2828 ;;
2829 (beginning-of-line)
2830 (if (or
2831 ;; a label line
2832 (looking-at (concat "^" idlwave-label "[ \t]*$"))
2833 ;; a batch command
2834 (looking-at "^[ \t]*@"))
2835 ;; leave flush left
2836 nil
2837 ;; indent the line
2838 (idlwave-indent-left-margin (idlwave-calculate-indent)))
2839 ;; Adjust parallel comment
2840 (end-of-line)
2841 (if (idlwave-in-comment)
2842 ;; Emacs 21 is too smart with fill-column on comment indent
2843 (let ((fill-column (if (fboundp 'comment-indent-new-line)
2844 (1- (frame-width))
2845 fill-column)))
2846 (indent-for-comment)))))
2847 (goto-char mloc)
2848 ;; Get rid of marker
2849 (set-marker mloc nil)))
2850
2851 (defun idlwave-do-action (action)
2852 "Perform an action repeatedly on a line.
2853 ACTION is a list (REG . FUNC). REG is a regular expression. FUNC is
2854 either a function name to be called with `funcall' or a list to be
2855 evaluated with `eval'. The action performed by FUNC should leave
2856 point after the match for REG - otherwise an infinite loop may be
2857 entered. FUNC is always passed a final argument of 'is-action, so it
2858 can discriminate between being run as an action, or a key binding."
2859 (let ((action-key (car action))
2860 (action-routine (cdr action)))
2861 (beginning-of-line)
2862 (while (idlwave-look-at action-key)
2863 (if (listp action-routine)
2864 (eval (append action-routine '('is-action)))
2865 (funcall action-routine 'is-action)))))
2866
2867 (defun idlwave-indent-to (col &optional min)
2868 "Indent from point with spaces until column COL.
2869 Inserts space before markers at point."
2870 (if (not min) (setq min 0))
2871 (insert-before-markers
2872 (make-string (max min (- col (current-column))) ?\ )))
2873
2874 (defun idlwave-indent-left-margin (col)
2875 "Indent the current line to column COL.
2876 Indents such that first non-whitespace character is at column COL
2877 Inserts spaces before markers at point."
2878 (save-excursion
2879 (beginning-of-line)
2880 (delete-horizontal-space)
2881 (idlwave-indent-to col)))
2882
2883 (defun idlwave-indent-subprogram ()
2884 "Indent program unit which contains point."
2885 (interactive)
2886 (save-excursion
2887 (idlwave-end-of-statement)
2888 (idlwave-beginning-of-subprogram)
2889 (let ((beg (point)))
2890 (idlwave-forward-block)
2891 (message "Indenting subprogram...")
2892 (indent-region beg (point) nil))
2893 (message "Indenting subprogram...done.")))
2894
2895 (defun idlwave-indent-statement ()
2896 "Indent current statement, including all continuation lines."
2897 (interactive)
2898 (save-excursion
2899 (idlwave-beginning-of-statement)
2900 (let ((beg (point)))
2901 (idlwave-end-of-statement)
2902 (indent-region beg (point) nil))))
2903
2904 (defun idlwave-calculate-indent ()
2905 "Return appropriate indentation for current line as IDL code."
2906 (save-excursion
2907 (beginning-of-line)
2908 (cond
2909 ;; Check for beginning of unit - main (beginning of buffer), pro, or
2910 ;; function
2911 ((idlwave-look-at idlwave-begin-unit-reg)
2912 0)
2913 ;; Check for continuation line
2914 ((save-excursion
2915 (and (= (forward-line -1) 0)
2916 (idlwave-is-continuation-line)))
2917 (idlwave-calculate-cont-indent))
2918 ;; calculate indent based on previous and current statements
2919 (t (let* (beg-prev-pos
2920 (the-indent
2921 ;; calculate indent based on previous statement
2922 (save-excursion
2923 (cond
2924 ;; Beginning of file
2925 ((prog1
2926 (idlwave-previous-statement)
2927 (setq beg-prev-pos (point)))
2928 0)
2929 ;; Main block
2930 ((idlwave-look-at idlwave-begin-unit-reg t)
2931 (+ (idlwave-current-statement-indent)
2932 idlwave-main-block-indent))
2933 ;; Begin block
2934 ((idlwave-look-at idlwave-begin-block-reg t)
2935 (+ (idlwave-min-current-statement-indent)
2936 idlwave-block-indent))
2937 ;; End Block
2938 ((idlwave-look-at idlwave-end-block-reg t)
2939 (progn
2940 ;; Match to the *beginning* of the block opener
2941 (goto-char beg-prev-pos)
2942 (idlwave-block-jump-out -1 'nomark) ; go to begin block
2943 (idlwave-min-current-statement-indent)))
2944 ;; idlwave-end-offset
2945 ;; idlwave-block-indent))
2946
2947 ;; Default to current indent
2948 ((idlwave-current-statement-indent))))))
2949 ;; adjust the indentation based on the current statement
2950 (cond
2951 ;; End block
2952 ((idlwave-look-at idlwave-end-block-reg)
2953 (+ the-indent idlwave-end-offset))
2954 (the-indent)))))))
2955
2956 ;;
2957 ;; Parentheses indent
2958 ;;
2959
2960 (defun idlwave-calculate-paren-indent (beg-reg end-reg close-exp)
2961 "Calculate the continuation indent inside a paren group.
2962 Returns a cons-cell with (open . indent), where open is the
2963 location of the open paren."
2964 (let ((open (nth 1 (parse-partial-sexp beg-reg end-reg))))
2965 ;; Found an innermost open paren.
2966 (when open
2967 (goto-char open)
2968 ;; Line up with next word unless this is a closing paren.
2969 (cons open
2970 (cond
2971 ;; Plain Kernighan-style nested indent
2972 (idlwave-indent-parens-nested
2973 (+ idlwave-continuation-indent (idlwave-current-indent)))
2974
2975 ;; This is a closed paren - line up under open paren.
2976 (close-exp
2977 (current-column))
2978
2979 ;; Empty (or just comment) follows -- revert to basic indent
2980 ((progn
2981 ;; Skip paren
2982 (forward-char 1)
2983 (looking-at "[ \t$]*\\(;.*\\)?$"))
2984 nil)
2985
2986 ;; Line up with first word after any blank space
2987 ((progn
2988 (skip-chars-forward " \t")
2989 (current-column))))))))
2990
2991 (defun idlwave-calculate-cont-indent ()
2992 "Calculates the IDL continuation indent column from the previous statement.
2993 Note that here previous statement usually means the beginning of the
2994 current statement if this statement is a continuation of the previous
2995 line. Various special types of continuations, including assignments,
2996 routine definitions, and parenthetical groupings, are treated separately."
2997 (save-excursion
2998 (let* ((case-fold-search t)
2999 (end-reg (progn (beginning-of-line) (point)))
3000 (beg-last-statement (save-excursion (idlwave-previous-statement)
3001 (point)))
3002 (beg-reg (progn (idlwave-start-of-substatement 'pre)
3003 (if (eq (line-beginning-position) end-reg)
3004 (goto-char beg-last-statement)
3005 (point))))
3006 (basic-indent (+ (idlwave-min-current-statement-indent end-reg)
3007 idlwave-continuation-indent))
3008 fancy-nonparen-indent fancy-paren-indent)
3009 (cond
3010 ;; Align then with its matching if, etc.
3011 ((let ((matchers '(("\\<if\\>" . "[ \t]*then")
3012 ("\\<\\(if\\|end\\(if\\)?\\)\\>" . "[ \t]*else")
3013 ("\\<\\(for\\|while\\)\\>" . "[ \t]*do")
3014 ("\\<\\(repeat\\|end\\(rep\\)?\\)\\>" .
3015 "[ \t]*until")
3016 ("\\<case\\>" . "[ \t]*of")))
3017 match cont-re)
3018 (goto-char end-reg)
3019 (and
3020 (setq cont-re
3021 (catch 'exit
3022 (while (setq match (car matchers))
3023 (if (looking-at (cdr match))
3024 (throw 'exit (car match)))
3025 (setq matchers (cdr matchers)))))
3026 (idlwave-find-key cont-re -1 'nomark beg-last-statement)))
3027 (if (looking-at "end") ;; that one's special
3028 (- (idlwave-current-indent)
3029 (+ idlwave-block-indent idlwave-end-offset))
3030 (idlwave-current-indent)))
3031
3032 ;; Indent in from the previous line for continuing statements
3033 ((let ((matchers '("\\<then\\>"
3034 "\\<do\\>"
3035 "\\<repeat\\>"
3036 "\\<else\\>"))
3037 match)
3038 (catch 'exit
3039 (goto-char end-reg)
3040 (if (/= (forward-line -1) 0)
3041 (throw 'exit nil))
3042 (while (setq match (car matchers))
3043 (if (looking-at (concat ".*" match "[ \t]*\\$[ \t]*"
3044 "\\(;.*\\)?$"))
3045 (throw 'exit t))
3046 (setq matchers (cdr matchers)))))
3047 (+ idlwave-continuation-indent (idlwave-current-indent)))
3048
3049 ;; Parenthetical indent, either traditional or Kernighan style
3050 ((setq fancy-paren-indent
3051 (let* ((end-reg end-reg)
3052 (close-exp (progn
3053 (goto-char end-reg)
3054 (skip-chars-forward " \t")
3055 (looking-at "\\s)")))
3056 indent-cons)
3057 (catch 'loop
3058 (while (setq indent-cons (idlwave-calculate-paren-indent
3059 beg-reg end-reg close-exp))
3060 ;; First permitted containing paren
3061 (if (or
3062 idlwave-indent-to-open-paren
3063 idlwave-indent-parens-nested
3064 (null (cdr indent-cons))
3065 (< (- (cdr indent-cons) basic-indent)
3066 idlwave-max-extra-continuation-indent))
3067 (throw 'loop (cdr indent-cons)))
3068 (setq end-reg (car indent-cons))))))
3069 fancy-paren-indent)
3070
3071 ;; A continued assignment, or procedure call/definition
3072 ((and
3073 (> idlwave-max-extra-continuation-indent 0)
3074 (setq fancy-nonparen-indent
3075 (progn
3076 (goto-char beg-reg)
3077 (while (idlwave-look-at "&")) ; skip continued statements
3078 (cond
3079 ;; A continued Procedure call or definition
3080 ((progn
3081 (idlwave-look-at "^[ \t]*\\(pro\\|function\\)") ;skip over
3082 (looking-at "[ \t]*\\([a-zA-Z0-9.$_]+[ \t]*->[ \t]*\\)?[a-zA-Z][:a-zA-Z0-9$_]*[ \t]*\\(,\\)[ \t]*"))
3083 (goto-char (match-end 0))
3084 ;; Comment only, or blank line with "$"? Basic indent.
3085 (if (save-match-data (looking-at "[ \t$]*\\(;.*\\)?$"))
3086 nil
3087 (current-column)))
3088
3089 ;; Continued assignment (with =):
3090 ((catch 'assign ;
3091 (while (looking-at "[^=\n\r]*\\(=\\)[ \t]*")
3092 (goto-char (match-end 0))
3093 (if (null (idlwave-what-function beg-reg))
3094 (throw 'assign t))))
3095 (unless (or
3096 (idlwave-in-quote)
3097 (looking-at "[ \t$]*\\(;.*\\)?$") ; use basic
3098 (save-excursion
3099 (goto-char beg-last-statement)
3100 (eq (caar (idlwave-statement-type)) 'for)))
3101 (current-column))))))
3102 (< (- fancy-nonparen-indent basic-indent)
3103 idlwave-max-extra-continuation-indent))
3104 (if fancy-paren-indent ;calculated but disallowed paren indent
3105 (+ fancy-nonparen-indent idlwave-continuation-indent)
3106 fancy-nonparen-indent))
3107
3108 ;; Basic indent, by default
3109 (t basic-indent)))))
3110
3111
3112
3113 (defun idlwave-find-key (key-re &optional dir nomark limit)
3114 "Move to next match of the regular expression KEY-RE.
3115 Matches inside comments or string constants will be ignored.
3116 If DIR is negative, the search will be backwards.
3117 At a successful match, the mark is pushed unless NOMARK is non-nil.
3118 Searches are limited to LIMIT.
3119 Searches are case-insensitive and use a special syntax table which
3120 treats `$' and `_' as word characters.
3121 Return value is the beginning of the match or (in case of failure) nil."
3122 (setq dir (or dir 0))
3123 (let ((case-fold-search t)
3124 (search-func (if (> dir 0) 're-search-forward 're-search-backward))
3125 found)
3126 (idlwave-with-special-syntax
3127 (save-excursion
3128 (catch 'exit
3129 (while (funcall search-func key-re limit t)
3130 (if (not (idlwave-quoted))
3131 (throw 'exit (setq found (match-beginning 0)))
3132 (if (or (and (> dir 0) (eobp))
3133 (and (< dir 0) (bobp)))
3134 (throw 'exit nil)))))))
3135 (if found
3136 (progn
3137 (if (not nomark) (push-mark))
3138 (goto-char found)
3139 found)
3140 nil)))
3141
3142 (defun idlwave-block-jump-out (&optional dir nomark)
3143 "When optional argument DIR is non-negative, move forward to end of
3144 current block using the `idlwave-begin-block-reg' and `idlwave-end-block-reg'
3145 regular expressions. When DIR is negative, move backwards to block beginning.
3146 Recursively calls itself to skip over nested blocks. DIR defaults to
3147 forward. Calls `push-mark' unless the optional argument NOMARK is
3148 non-nil. Movement is limited by the start of program units because of
3149 possibility of unbalanced blocks."
3150 (interactive "P")
3151 (or dir (setq dir 0))
3152 (let* ((here (point))
3153 (case-fold-search t)
3154 (limit (if (>= dir 0) (point-max) (point-min)))
3155 (block-limit (if (>= dir 0)
3156 idlwave-begin-block-reg
3157 idlwave-end-block-reg))
3158 found
3159 (block-reg (concat idlwave-begin-block-reg "\\|"
3160 idlwave-end-block-reg))
3161 (unit-limit (or (save-excursion
3162 (if (< dir 0)
3163 (idlwave-find-key
3164 idlwave-begin-unit-reg dir t limit)
3165 (end-of-line)
3166 (idlwave-find-key
3167 idlwave-end-unit-reg dir t limit)))
3168 limit)))
3169 (if (>= dir 0) (end-of-line)) ;Make sure we are in current block
3170 (if (setq found (idlwave-find-key block-reg dir t unit-limit))
3171 (while (and found (looking-at block-limit))
3172 (if (>= dir 0) (forward-word 1))
3173 (idlwave-block-jump-out dir t)
3174 (setq found (idlwave-find-key block-reg dir t unit-limit))))
3175 (if (not nomark) (push-mark here))
3176 (if (not found) (goto-char unit-limit)
3177 (if (>= dir 0) (forward-word 1)))))
3178
3179 (defun idlwave-min-current-statement-indent (&optional end-reg)
3180 "The minimum indent in the current statement."
3181 (idlwave-beginning-of-statement)
3182 (if (not (idlwave-is-continuation-line))
3183 (idlwave-current-indent)
3184 (let ((min (idlwave-current-indent)) comm-or-empty)
3185 (while (and (= (forward-line 1) 0)
3186 (or (setq comm-or-empty (idlwave-is-comment-or-empty-line))
3187 (idlwave-is-continuation-line))
3188 (or (null end-reg) (< (point) end-reg)))
3189 (unless comm-or-empty (setq min (min min (idlwave-current-indent)))))
3190 (if (or comm-or-empty (and end-reg (>= (point) end-reg)))
3191 min
3192 (min min (idlwave-current-indent))))))
3193
3194 (defun idlwave-current-statement-indent (&optional last-line)
3195 "Return indentation of the current statement.
3196 If in a statement, moves to beginning of statement before finding indent."
3197 (if last-line
3198 (idlwave-end-of-statement)
3199 (idlwave-beginning-of-statement))
3200 (idlwave-current-indent))
3201
3202 (defun idlwave-current-indent ()
3203 "Return the column of the indentation of the current line.
3204 Skips any whitespace. Returns 0 if the end-of-line follows the whitespace."
3205 (save-excursion
3206 (beginning-of-line)
3207 (skip-chars-forward " \t")
3208 ;; if we are at the end of blank line return 0
3209 (cond ((eolp) 0)
3210 ((current-column)))))
3211
3212 (defun idlwave-is-continuation-line ()
3213 "Test if current line is continuation line.
3214 Blank or comment-only lines following regular continuation lines (with
3215 `$') count as continuations too."
3216 (let (p)
3217 (save-excursion
3218 (or
3219 (idlwave-look-at "\\<\\$")
3220 (catch 'loop
3221 (while (and (looking-at "^[ \t]*\\(;.*\\)?$")
3222 (eq (forward-line -1) 0))
3223 (if (setq p (idlwave-look-at "\\<\\$")) (throw 'loop p))))))))
3224
3225 (defun idlwave-is-comment-line ()
3226 "Test if the current line is a comment line."
3227 (save-excursion
3228 (beginning-of-line 1)
3229 (looking-at "[ \t]*;")))
3230
3231 (defun idlwave-is-comment-or-empty-line ()
3232 "Test if the current line is a comment line."
3233 (save-excursion
3234 (beginning-of-line 1)
3235 (looking-at "[ \t]*[;\n]")))
3236
3237 (defun idlwave-look-at (regexp &optional cont beg)
3238 "Search current line from current point for REGEXP.
3239 If optional argument CONT is non-nil, searches to the end of
3240 the current statement.
3241 If optional arg BEG is non-nil, search starts from the beginning of the
3242 current statement.
3243 Ignores matches that end in a comment or inside a string expression.
3244 Returns point if successful, nil otherwise.
3245 This function produces unexpected results if REGEXP contains quotes or
3246 a comment delimiter. The search is case insensitive.
3247 If successful leaves point after the match, otherwise, does not move point."
3248 (let ((here (point))
3249 (case-fold-search t)
3250 (eos (save-excursion
3251 (if cont (idlwave-end-of-statement) (end-of-line))
3252 (point)))
3253 found)
3254 (idlwave-with-special-syntax
3255 (if beg (idlwave-beginning-of-statement))
3256 (while (and (setq found (re-search-forward regexp eos t))
3257 (idlwave-quoted))))
3258 (if (not found) (goto-char here))
3259 found))
3260
3261 (defun idlwave-fill-paragraph (&optional nohang)
3262 "Fill paragraphs in comments.
3263 A paragraph is made up of all contiguous lines having the same comment
3264 leader (the leading whitespace before the comment delimiter and the
3265 comment delimiter). In addition, paragraphs are separated by blank
3266 line comments. The indentation is given by the hanging indent of the
3267 first line, otherwise by the minimum indentation of the lines after
3268 the first line. The indentation of the first line does not change.
3269 Does not effect code lines. Does not fill comments on the same line
3270 with code. The hanging indent is given by the end of the first match
3271 matching `idlwave-hang-indent-regexp' on the paragraph's first line.
3272 If the optional argument NOHANG is non-nil then the hanging indent is
3273 ignored."
3274 (interactive "P")
3275 ;; check if this is a line comment
3276 (if (save-excursion
3277 (beginning-of-line)
3278 (skip-chars-forward " \t")
3279 (looking-at comment-start))
3280 (let
3281 ((indent 999)
3282 pre here diff fill-prefix-reg bcl first-indent
3283 hang start end)
3284 ;; Change tabs to spaces in the surrounding paragraph.
3285 ;; The surrounding paragraph will be the largest containing block of
3286 ;; contiguous line comments. Thus, we may be changing tabs in
3287 ;; a much larger area than is needed, but this is the easiest
3288 ;; brute force way to do it.
3289 ;;
3290 ;; This has the undesirable side effect of replacing the tabs
3291 ;; permanently without the user's request or knowledge.
3292 (save-excursion
3293 (backward-paragraph)
3294 (setq start (point)))
3295 (save-excursion
3296 (forward-paragraph)
3297 (setq end (point)))
3298 (untabify start end)
3299 ;;
3300 (setq here (point))
3301 (beginning-of-line)
3302 (setq bcl (point))
3303 (re-search-forward
3304 (concat "^[ \t]*" comment-start "+")
3305 (save-excursion (end-of-line) (point))
3306 t)
3307 ;; Get the comment leader on the line and its length
3308 (setq pre (current-column))
3309 ;; the comment leader is the indentation plus exactly the
3310 ;; number of consecutive ";".
3311 (setq fill-prefix-reg
3312 (concat
3313 (setq fill-prefix
3314 (regexp-quote
3315 (buffer-substring (save-excursion
3316 (beginning-of-line) (point))
3317 (point))))
3318 "[^;]"))
3319
3320 ;; Mark the beginning and end of the paragraph
3321 (goto-char bcl)
3322 (while (and (looking-at fill-prefix-reg)
3323 (not (looking-at paragraph-separate))
3324 (not (bobp)))
3325 (forward-line -1))
3326 ;; Move to first line of paragraph
3327 (if (/= (point) bcl)
3328 (forward-line 1))
3329 (setq start (point))
3330 (goto-char bcl)
3331 (while (and (looking-at fill-prefix-reg)
3332 (not (looking-at paragraph-separate))
3333 (not (eobp)))
3334 (forward-line 1))
3335 (beginning-of-line)
3336 (if (or (not (looking-at fill-prefix-reg))
3337 (looking-at paragraph-separate))
3338 (forward-line -1))
3339 (end-of-line)
3340 ;; if at end of buffer add a newline (need this because
3341 ;; fill-region needs END to be at the beginning of line after
3342 ;; the paragraph or it will add a line).
3343 (if (eobp)
3344 (progn (insert ?\n) (backward-char 1)))
3345 ;; Set END to the beginning of line after the paragraph
3346 ;; END is calculated as distance from end of buffer
3347 (setq end (- (point-max) (point) 1))
3348 ;;
3349 ;; Calculate the indentation for the paragraph.
3350 ;;
3351 ;; In the following while statements, after one iteration
3352 ;; point will be at the beginning of a line in which case
3353 ;; the while will not be executed for the
3354 ;; the first paragraph line and thus will not affect the
3355 ;; indentation.
3356 ;;
3357 ;; First check to see if indentation is based on hanging indent.
3358 (if (and (not nohang) idlwave-hanging-indent
3359 (setq hang
3360 (save-excursion
3361 (goto-char start)
3362 (idlwave-calc-hanging-indent))))
3363 ;; Adjust lines of paragraph by inserting spaces so that
3364 ;; each line's indent is at least as great as the hanging
3365 ;; indent. This is needed for fill-paragraph to work with
3366 ;; a fill-prefix.
3367 (progn
3368 (setq indent hang)
3369 (beginning-of-line)
3370 (while (> (point) start)
3371 (re-search-forward comment-start-skip
3372 (save-excursion (end-of-line) (point))
3373 t)
3374 (if (> (setq diff (- indent (current-column))) 0)
3375 (progn
3376 (if (>= here (point))
3377 ;; adjust the original location for the
3378 ;; inserted text.
3379 (setq here (+ here diff)))
3380 (insert (make-string diff ?\ ))))
3381 (forward-line -1))
3382 )
3383
3384 ;; No hang. Instead find minimum indentation of paragraph
3385 ;; after first line.
3386 ;; For the following while statement, since START is at the
3387 ;; beginning of line and END is at the end of line
3388 ;; point is greater than START at least once (which would
3389 ;; be the case for a single line paragraph).
3390 (while (> (point) start)
3391 (beginning-of-line)
3392 (setq indent
3393 (min indent
3394 (progn
3395 (re-search-forward
3396 comment-start-skip
3397 (save-excursion (end-of-line) (point))
3398 t)
3399 (current-column))))
3400 (forward-line -1))
3401 )
3402 (setq fill-prefix (concat fill-prefix
3403 (make-string (- indent pre)
3404 ?\ )))
3405 ;; first-line indent
3406 (setq first-indent
3407 (max
3408 (progn
3409 (re-search-forward
3410 comment-start-skip
3411 (save-excursion (end-of-line) (point))
3412 t)
3413 (current-column))
3414 indent))
3415
3416 ;; try to keep point at its original place
3417 (goto-char here)
3418
3419 ;; In place of the more modern fill-region-as-paragraph, a hack
3420 ;; to keep whitespace untouched on the first line within the
3421 ;; indent length and to preserve any indent on the first line
3422 ;; (first indent).
3423 (save-excursion
3424 (setq diff
3425 (buffer-substring start (+ start first-indent -1)))
3426 (subst-char-in-region start (+ start first-indent -1) ?\ ?~ nil)
3427 (fill-region-as-paragraph
3428 start
3429 (- (point-max) end)
3430 (current-justification)
3431 nil)
3432 (delete-region start (+ start first-indent -1))
3433 (goto-char start)
3434 (insert diff))
3435 ;; When we want the point at the beginning of the comment
3436 ;; body fill-region will put it at the beginning of the line.
3437 (if (bolp) (skip-chars-forward (concat " \t" comment-start)))
3438 (setq fill-prefix nil))))
3439
3440 (defun idlwave-calc-hanging-indent ()
3441 "Calculate the position of the hanging indent for the comment paragraph.
3442 The hanging indent position is given by the first match with the
3443 `idlwave-hang-indent-regexp'. If `idlwave-use-last-hang-indent' is
3444 non-nil then use last occurrence matching `idlwave-hang-indent-regexp'
3445 on the line.
3446 If not found returns nil."
3447 (if idlwave-use-last-hang-indent
3448 (save-excursion
3449 (end-of-line)
3450 (if (re-search-backward
3451 idlwave-hang-indent-regexp
3452 (save-excursion (beginning-of-line) (point))
3453 t)
3454 (+ (current-column) (length idlwave-hang-indent-regexp))))
3455 (save-excursion
3456 (beginning-of-line)
3457 (if (re-search-forward
3458 idlwave-hang-indent-regexp
3459 (save-excursion (end-of-line) (point))
3460 t)
3461 (current-column)))))
3462
3463 (defun idlwave-auto-fill ()
3464 "Called to break lines in auto fill mode.
3465 Only fills non-comment lines if `idlwave-fill-comment-line-only' is
3466 non-nil. Places a continuation character at the end of the line if
3467 not in a comment. Splits strings with IDL concatenation operator `+'
3468 if `idlwave-auto-fill-split-string' is non-nil."
3469 (if (<= (current-column) fill-column)
3470 nil ; do not to fill
3471 (if (or (not idlwave-fill-comment-line-only)
3472 (save-excursion
3473 ;; Check for comment line
3474 (beginning-of-line)
3475 (looking-at idlwave-comment-line-start-skip)))
3476 (let (beg)
3477 (idlwave-indent-line)
3478 ;; Prevent actions do-auto-fill which calls indent-line-function.
3479 (let (idlwave-do-actions
3480 (paragraph-separate ".")
3481 (fill-nobreak-predicate
3482 (if (and (idlwave-in-quote)
3483 idlwave-auto-fill-split-string)
3484 (lambda () ;; We'll need 5 spaces for " ' + $"
3485 (<= (- fill-column (current-column)) 5)
3486 ))))
3487 (do-auto-fill))
3488 (save-excursion
3489 (end-of-line 0)
3490 ;; Indent the split line
3491 (idlwave-indent-line))
3492 (if (save-excursion
3493 (beginning-of-line)
3494 (looking-at idlwave-comment-line-start-skip))
3495 ;; A continued line comment
3496 ;; We treat continued line comments as part of a comment
3497 ;; paragraph. So we check for a hanging indent.
3498 (if idlwave-hanging-indent
3499 (let ((here (- (point-max) (point)))
3500 (indent
3501 (save-excursion
3502 (forward-line -1)
3503 (idlwave-calc-hanging-indent))))
3504 (if indent
3505 (progn
3506 ;; Remove whitespace between comment delimiter and
3507 ;; text, insert spaces for appropriate indentation.
3508 (beginning-of-line)
3509 (re-search-forward
3510 comment-start-skip
3511 (save-excursion (end-of-line) (point)) t)
3512 (delete-horizontal-space)
3513 (idlwave-indent-to indent)
3514 (goto-char (- (point-max) here)))
3515 )))
3516 ;; Split code or comment?
3517 (if (save-excursion
3518 (end-of-line 0)
3519 (idlwave-in-comment))
3520 ;; Splitting a non-full-line comment.
3521 ;; Insert the comment delimiter from split line
3522 (progn
3523 (save-excursion
3524 (beginning-of-line)
3525 (skip-chars-forward " \t")
3526 ;; Insert blank to keep off beginning of line
3527 (insert " "
3528 (save-excursion
3529 (forward-line -1)
3530 (buffer-substring (idlwave-goto-comment)
3531 (progn
3532 (skip-chars-forward "; ")
3533 (point))))))
3534 (idlwave-indent-line))
3535 ;; Split code line - add continuation character
3536 (save-excursion
3537 (end-of-line 0)
3538 ;; Check to see if we split a string
3539 (if (and (setq beg (idlwave-in-quote))
3540 idlwave-auto-fill-split-string)
3541 ;; Split the string and concatenate.
3542 ;; The first extra space is for the space
3543 ;; the line was split. That space was removed.
3544 (insert " " (char-after beg) " +"))
3545 (insert " $"))
3546 (if beg
3547 (if idlwave-auto-fill-split-string
3548 ;; Make the second part of continued string
3549 (save-excursion
3550 (beginning-of-line)
3551 (skip-chars-forward " \t")
3552 (insert (char-after beg)))
3553 ;; Warning
3554 (beep)
3555 (message "Warning: continuation inside a string.")))
3556 ;; Although do-auto-fill (via indent-new-comment-line) calls
3557 ;; idlwave-indent-line for the new line, re-indent again
3558 ;; because of the addition of the continuation character.
3559 (idlwave-indent-line))
3560 )))))
3561
3562 (defun idlwave-auto-fill-mode (arg)
3563 "Toggle auto-fill mode for IDL mode.
3564 With arg, turn auto-fill mode on if arg is positive.
3565 In auto-fill mode, inserting a space at a column beyond `fill-column'
3566 automatically breaks the line at a previous space."
3567 (interactive "P")
3568 (prog1 (set idlwave-fill-function
3569 (if (if (null arg)
3570 (not (symbol-value idlwave-fill-function))
3571 (> (prefix-numeric-value arg) 0))
3572 'idlwave-auto-fill
3573 nil))
3574 ;; update mode-line
3575 (set-buffer-modified-p (buffer-modified-p))))
3576
3577 ;(defun idlwave-fill-routine-call ()
3578 ; "Fill a routine definition or statement, indenting appropriately."
3579 ; (let ((where (idlwave-where)))))
3580
3581
3582 (defun idlwave-doc-header (&optional nomark)
3583 "Insert a documentation header at the beginning of the unit.
3584 Inserts the value of the variable `idlwave-file-header'. Sets mark
3585 before moving to do insertion unless the optional prefix argument
3586 NOMARK is non-nil."
3587 (interactive "P")
3588 (or nomark (push-mark))
3589 ;; make sure we catch the current line if it begins the unit
3590 (if idlwave-header-to-beginning-of-file
3591 (goto-char (point-min))
3592 (end-of-line)
3593 (idlwave-beginning-of-subprogram)
3594 (beginning-of-line)
3595 ;; skip function or procedure line
3596 (if (idlwave-look-at "\\<\\(pro\\|function\\)\\>")
3597 (progn
3598 (idlwave-end-of-statement)
3599 (if (> (forward-line 1) 0) (insert "\n")))))
3600 (let ((pos (point)))
3601 (if idlwave-file-header
3602 (cond ((car idlwave-file-header)
3603 (insert-file-contents (car idlwave-file-header)))
3604 ((stringp (car (cdr idlwave-file-header)))
3605 (insert (car (cdr idlwave-file-header))))))
3606 (goto-char pos)))
3607
3608 (defun idlwave-default-insert-timestamp ()
3609 "Default timestamp insertion function."
3610 (insert (current-time-string))
3611 (insert ", " (user-full-name))
3612 (if (boundp 'user-mail-address)
3613 (insert " <" user-mail-address ">")
3614 (insert " <" (user-login-name) "@" (system-name) ">"))
3615 ;; Remove extra spaces from line
3616 (idlwave-fill-paragraph)
3617 ;; Insert a blank line comment to separate from the date entry -
3618 ;; will keep the entry from flowing onto date line if re-filled.
3619 (insert "\n;\n;\t\t"))
3620
3621 (defun idlwave-doc-modification ()
3622 "Insert a brief modification log at the beginning of the current program.
3623 Looks for an occurrence of the value of user variable
3624 `idlwave-doc-modifications-keyword' if non-nil. Inserts time and user
3625 name and places the point for the user to add a log. Before moving, saves
3626 location on mark ring so that the user can return to previous point."
3627 (interactive)
3628 (push-mark)
3629 (let* (beg end)
3630 (if (and (or (re-search-backward idlwave-doclib-start nil t)
3631 (progn
3632 (goto-char (point-min))
3633 (re-search-forward idlwave-doclib-start nil t)))
3634 (setq beg (match-beginning 0))
3635 (re-search-forward idlwave-doclib-end nil t)
3636 (setq end (match-end 0)))
3637 (progn
3638 (goto-char beg)
3639 (if (re-search-forward
3640 (concat idlwave-doc-modifications-keyword ":")
3641 end t)
3642 (end-of-line)
3643 (goto-char end)
3644 (end-of-line -1)
3645 (insert "\n" comment-start "\n")
3646 (insert comment-start " " idlwave-doc-modifications-keyword ":"))
3647 (insert "\n;\n;\t")
3648 (run-hooks 'idlwave-timestamp-hook))
3649 (error "No valid DOCLIB header"))))
3650
3651
3652 ;;; CJC 3/16/93
3653 ;;; Interface to expand-region-abbrevs which did not work when the
3654 ;;; abbrev hook associated with an abbrev moves point backwards
3655 ;;; after abbrev expansion, e.g., as with the abbrev '.n'.
3656 ;;; The original would enter an infinite loop in attempting to expand
3657 ;;; .n (it would continually expand and unexpand the abbrev without expanding
3658 ;;; because the point would keep going back to the beginning of the
3659 ;;; abbrev instead of to the end of the abbrev). We now keep the
3660 ;;; abbrev hook from moving backwards.
3661 ;;;
3662 (defun idlwave-expand-region-abbrevs (start end)
3663 "Expand each abbrev occurrence in the region.
3664 Calling from a program, arguments are START END."
3665 (interactive "r")
3666 (save-excursion
3667 (goto-char (min start end))
3668 (let ((idlwave-show-block nil) ;Do not blink
3669 (idlwave-abbrev-move nil)) ;Do not move
3670 (expand-region-abbrevs start end 'noquery))))
3671
3672 (defun idlwave-quoted ()
3673 "Return t if point is in a comment or quoted string.
3674 Returns nil otherwise."
3675 (or (idlwave-in-comment) (idlwave-in-quote)))
3676
3677 (defun idlwave-in-quote ()
3678 "Return location of the opening quote
3679 if point is in a IDL string constant, nil otherwise.
3680 Ignores comment delimiters on the current line.
3681 Properly handles nested quotation marks and octal
3682 constants - a double quote followed by an octal digit."
3683 ;;; Treat an octal inside an apostrophe to be a normal string. Treat a
3684 ;;; double quote followed by an octal digit to be an octal constant
3685 ;;; rather than a string. Therefore, there is no terminating double
3686 ;;; quote.
3687 (save-excursion
3688 ;; Because single and double quotes can quote each other we must
3689 ;; search for the string start from the beginning of line.
3690 (let* ((start (point))
3691 (eol (progn (end-of-line) (point)))
3692 (bq (progn (beginning-of-line) (point)))
3693 (endq (point))
3694 (data (match-data))
3695 delim
3696 found)
3697 (while (< endq start)
3698 ;; Find string start
3699 ;; Don't find an octal constant beginning with a double quote
3700 (if (re-search-forward "[\"']" eol 'lim)
3701 ;; Find the string end.
3702 ;; In IDL, two consecutive delimiters after the start of a
3703 ;; string act as an
3704 ;; escape for the delimiter in the string.
3705 ;; Two consecutive delimiters alone (i.e., not after the
3706 ;; start of a string) is the null string.
3707 (progn
3708 ;; Move to position after quote
3709 (goto-char (1+ (match-beginning 0)))
3710 (setq bq (1- (point)))
3711 ;; Get the string delimiter
3712 (setq delim (char-to-string (preceding-char)))
3713 ;; Check for null string
3714 (if (looking-at delim)
3715 (progn (setq endq (point)) (forward-char 1))
3716 ;; Look for next unpaired delimiter
3717 (setq found (search-forward delim eol 'lim))
3718 (while (looking-at delim)
3719 (forward-char 1)
3720 (setq found (search-forward delim eol 'lim)))
3721 (if found
3722 (setq endq (- (point) 1))
3723 (setq endq (point)))
3724 ))
3725 (progn (setq bq (point)) (setq endq (point)))))
3726 (store-match-data data)
3727 ;; return string beginning position or nil
3728 (if (> start bq) bq))))
3729
3730 (defun idlwave-is-pointer-dereference (&optional limit)
3731 "Determine if the character after point is a pointer dereference *."
3732 (let ((pos (point)))
3733 (and
3734 (eq (char-after) ?\*)
3735 (not (idlwave-in-quote))
3736 (save-excursion
3737 (forward-char)
3738 (re-search-backward (concat "\\(" idlwave-idl-keywords
3739 "\\|[[(*+-/=,^><]\\)\\s-*\\*") limit t)))))
3740
3741
3742 ;; Statement templates
3743
3744 ;; Replace these with a general template function, something like
3745 ;; expand.el (I think there was also something with a name similar to
3746 ;; dmacro.el)
3747
3748 (defun idlwave-template (s1 s2 &optional prompt noindent)
3749 "Build a template with optional prompt expression.
3750
3751 Opens a line if point is not followed by a newline modulo intervening
3752 whitespace. S1 and S2 are strings. S1 is inserted at point followed
3753 by S2. Point is inserted between S1 and S2. The case of S1 and S2 is
3754 adjusted according to `idlwave-abbrev-change-case'. If optional
3755 argument PROMPT is a string then it is displayed as a message in the
3756 minibuffer. The PROMPT serves as a reminder to the user of an
3757 expression to enter.
3758
3759 The lines containing S1 and S2 are reindented using `indent-region'
3760 unless the optional second argument NOINDENT is non-nil."
3761 (if (eq major-mode 'idlwave-shell-mode)
3762 ;; This is a gross hack to avoit template abbrev expansion
3763 ;; in the shell. FIXME: This is a dirty hack.
3764 (if (and (eq this-command 'self-insert-command)
3765 (equal last-abbrev-location (point)))
3766 (insert last-abbrev-text)
3767 (error "No templates in idlwave-shell"))
3768 (cond ((eq idlwave-abbrev-change-case 'down)
3769 (setq s1 (downcase s1) s2 (downcase s2)))
3770 (idlwave-abbrev-change-case
3771 (setq s1 (upcase s1) s2 (upcase s2))))
3772 (let ((beg (save-excursion (beginning-of-line) (point)))
3773 end)
3774 (if (not (looking-at "\\s-*\n"))
3775 (open-line 1))
3776 (insert s1)
3777 (save-excursion
3778 (insert s2)
3779 (setq end (point)))
3780 (if (not noindent)
3781 (indent-region beg end nil))
3782 (if (stringp prompt)
3783 (message "%s" prompt)))))
3784
3785 (defun idlwave-rw-case (string)
3786 "Make STRING have the case required by `idlwave-reserved-word-upcase'."
3787 (if idlwave-reserved-word-upcase
3788 (upcase string)
3789 string))
3790
3791 (defun idlwave-elif ()
3792 "Build skeleton IDL if-else block."
3793 (interactive)
3794 (idlwave-template
3795 (idlwave-rw-case "if")
3796 (idlwave-rw-case " then begin\n\nendif else begin\n\nendelse")
3797 "Condition expression"))
3798
3799 (defun idlwave-case ()
3800 "Build skeleton IDL case statement."
3801 (interactive)
3802 (idlwave-template
3803 (idlwave-rw-case "case")
3804 (idlwave-rw-case " of\n\nendcase")
3805 "Selector expression"))
3806
3807 (defun idlwave-switch ()
3808 "Build skeleton IDL switch statement."
3809 (interactive)
3810 (idlwave-template
3811 (idlwave-rw-case "switch")
3812 (idlwave-rw-case " of\n\nendswitch")
3813 "Selector expression"))
3814
3815 (defun idlwave-for ()
3816 "Build skeleton IDL loop statement."
3817 (interactive)
3818 (idlwave-template
3819 (idlwave-rw-case "for")
3820 (idlwave-rw-case " do begin\n\nendfor")
3821 "Loop expression"))
3822
3823 (defun idlwave-if ()
3824 "Build skeleton IDL if statement."
3825 (interactive)
3826 (idlwave-template
3827 (idlwave-rw-case "if")
3828 (idlwave-rw-case " then begin\n\nendif")
3829 "Scalar logical expression"))
3830
3831 (defun idlwave-procedure ()
3832 (interactive)
3833 (idlwave-template
3834 (idlwave-rw-case "pro")
3835 (idlwave-rw-case "\n\nreturn\nend")
3836 "Procedure name"))
3837
3838 (defun idlwave-function ()
3839 (interactive)
3840 (idlwave-template
3841 (idlwave-rw-case "function")
3842 (idlwave-rw-case "\n\nreturn\nend")
3843 "Function name"))
3844
3845 (defun idlwave-repeat ()
3846 (interactive)
3847 (idlwave-template
3848 (idlwave-rw-case "repeat begin\n\nendrep until")
3849 (idlwave-rw-case "")
3850 "Exit condition"))
3851
3852 (defun idlwave-while ()
3853 (interactive)
3854 (idlwave-template
3855 (idlwave-rw-case "while")
3856 (idlwave-rw-case " do begin\n\nendwhile")
3857 "Entry condition"))
3858
3859 (defun idlwave-split-string (string &optional pattern)
3860 "Return a list of substrings of STRING which are separated by PATTERN.
3861 If PATTERN is omitted, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
3862 (or pattern
3863 (setq pattern "[ \f\t\n\r\v]+"))
3864 (let (parts (start 0))
3865 (while (string-match pattern string start)
3866 (setq parts (cons (substring string start (match-beginning 0)) parts)
3867 start (match-end 0)))
3868 (nreverse (cons (substring string start) parts))))
3869
3870 (defun idlwave-replace-string (string replace_string replace_with)
3871 (let* ((start 0)
3872 (last (length string))
3873 (ret_string "")
3874 end)
3875 (while (setq end (string-match replace_string string start))
3876 (setq ret_string
3877 (concat ret_string (substring string start end) replace_with))
3878 (setq start (match-end 0)))
3879 (setq ret_string (concat ret_string (substring string start last)))))
3880
3881 (defun idlwave-get-buffer-visiting (file)
3882 ;; Return the buffer currently visiting FILE
3883 (cond
3884 ((boundp 'find-file-compare-truenames) ; XEmacs
3885 (let ((find-file-compare-truenames t))
3886 (get-file-buffer file)))
3887 ((fboundp 'find-buffer-visiting) ; Emacs
3888 (find-buffer-visiting file))
3889 (t (error "This should not happen (idlwave-get-buffer-visiting)"))))
3890
3891 (defvar idlwave-outlawed-buffers nil
3892 "List of buffers pulled up by IDLWAVE for special reasons.
3893 Buffers in this list may be killed by `idlwave-kill-autoloaded-buffers'.")
3894
3895 (defun idlwave-find-file-noselect (file &optional why)
3896 ;; Return a buffer visiting file.
3897 (or (idlwave-get-buffer-visiting file)
3898 (let ((buf (find-file-noselect file)))
3899 (if why (add-to-list 'idlwave-outlawed-buffers (cons buf why)))
3900 buf)))
3901
3902 (defun idlwave-kill-autoloaded-buffers ()
3903 "Kill buffers created automatically by IDLWAVE.
3904 Function prompts for a letter to identify the buffers to kill.
3905 Possible letters are:
3906
3907 f Buffers created by the command \\[idlwave-find-module] or mouse
3908 clicks in the routine info window.
3909 s Buffers created by the IDLWAVE Shell to display where execution
3910 stopped or an error was found.
3911 a Both of the above.
3912
3913 Buffers containing unsaved changes require confirmation before they are killed."
3914 (interactive)
3915 (if (null idlwave-outlawed-buffers)
3916 (error "No IDLWAVE-created buffers available")
3917 (princ (format "Kill IDLWAVE-created buffers: [f]ind source(%d), [s]hell display(%d), [a]ll ? "
3918 (idlwave-count-outlawed-buffers 'find)
3919 (idlwave-count-outlawed-buffers 'shell)))
3920 (let ((c (read-char)))
3921 (cond
3922 ((member c '(?f ?\C-f))
3923 (idlwave-do-kill-autoloaded-buffers 'find))
3924 ((member c '(?s ?\C-s))
3925 (idlwave-do-kill-autoloaded-buffers 'shell))
3926 ((member c '(?a ?\C-a))
3927 (idlwave-do-kill-autoloaded-buffers t))
3928 (t (error "Abort"))))))
3929
3930 (defun idlwave-count-outlawed-buffers (tag)
3931 "How many outlawed buffers have tag TAG?"
3932 (length (delq nil
3933 (mapcar
3934 (lambda (x) (eq (cdr x) tag))
3935 idlwave-outlawed-buffers))))
3936
3937 (defun idlwave-do-kill-autoloaded-buffers (&rest reasons)
3938 "Kill all buffers pulled up by IDLWAVE matching REASONS."
3939 (let* ((list (copy-sequence idlwave-outlawed-buffers))
3940 (cnt 0)
3941 entry)
3942 (while (setq entry (pop list))
3943 (if (buffer-live-p (car entry))
3944 (and (or (memq t reasons)
3945 (memq (cdr entry) reasons))
3946 (kill-buffer (car entry))
3947 (incf cnt)
3948 (setq idlwave-outlawed-buffers
3949 (delq entry idlwave-outlawed-buffers)))
3950 (setq idlwave-outlawed-buffers
3951 (delq entry idlwave-outlawed-buffers))))
3952 (message "%d buffer%s killed" cnt (if (= cnt 1) "" "s"))))
3953
3954 (defun idlwave-revoke-license-to-kill ()
3955 "Remove BUFFER from the buffers which may be killed.
3956 Killing would be done by `idlwave-do-kill-autoloaded-buffers'.
3957 Intended for `after-save-hook'."
3958 (let* ((buf (current-buffer))
3959 (entry (assq buf idlwave-outlawed-buffers)))
3960 ;; Revoke license
3961 (if entry
3962 (setq idlwave-outlawed-buffers
3963 (delq entry idlwave-outlawed-buffers)))
3964 ;; Remove this function from the hook.
3965 (remove-hook 'after-save-hook 'idlwave-revoke-license-to-kill 'local)))
3966
3967 (defvar idlwave-path-alist)
3968 (defun idlwave-locate-lib-file (file)
3969 ;; Find FILE on the scanned lib path and return a buffer visiting it
3970 (let* ((dirs idlwave-path-alist)
3971 dir efile)
3972 (catch 'exit
3973 (while (setq dir (car (pop dirs)))
3974 (if (file-regular-p
3975 (setq efile (expand-file-name file dir)))
3976 (throw 'exit efile))))))
3977
3978 (defun idlwave-expand-lib-file-name (file)
3979 ;; Find FILE on the scanned lib path and return a buffer visiting it
3980 ;; This is for, e.g., finding source with no user catalog
3981 (cond
3982 ((null file) nil)
3983 ((file-name-absolute-p file) file)
3984 (t (idlwave-locate-lib-file file))))
3985
3986 (defun idlwave-make-tags ()
3987 "Create the IDL tags file IDLTAGS in the current directory from
3988 the list of directories specified in the minibuffer. Directories may be
3989 for example: . /usr/local/rsi/idl/lib. All the subdirectories of the
3990 specified top directories are searched if the directory name is prefixed
3991 by @. Specify @ directories with care, it may take a long, long time if
3992 you specify /."
3993 (interactive)
3994 (let (directory directories cmd append status numdirs dir getsubdirs
3995 buffer save_buffer files numfiles item errbuf)
3996
3997 ;;
3998 ;; Read list of directories
3999 (setq directory (read-string "Tag Directories: " "."))
4000 (setq directories (idlwave-split-string directory "[ \t]+"))
4001 ;;
4002 ;; Set etags command, vars
4003 (setq cmd "etags --output=IDLTAGS --language=none --regex='/[
4004 \\t]*[pP][Rr][Oo][ \\t]+\\([^ \\t,]+\\)/' --regex='/[
4005 \\t]*[Ff][Uu][Nn][Cc][Tt][Ii][Oo][Nn][ \\t]+\\([^ \\t,]+\\)/' ")
4006 (setq append " ")
4007 (setq status 0)
4008 ;;
4009 ;; For each directory
4010 (setq numdirs 0)
4011 (setq dir (nth numdirs directories))
4012 (while (and dir)
4013 ;;
4014 ;; Find the subdirectories
4015 (if (string-match "^[@]\\(.+\\)$" dir)
4016 (setq getsubdirs t) (setq getsubdirs nil))
4017 (if (and getsubdirs) (setq dir (substring dir 1 (length dir))))
4018 (setq dir (expand-file-name dir))
4019 (if (file-directory-p dir)
4020 (progn
4021 (if (and getsubdirs)
4022 (progn
4023 (setq buffer (get-buffer-create "*idltags*"))
4024 (call-process "sh" nil buffer nil "-c"
4025 (concat "find " dir " -type d -print"))
4026 (setq save_buffer (current-buffer))
4027 (set-buffer buffer)
4028 (setq files (idlwave-split-string
4029 (idlwave-replace-string
4030 (buffer-substring 1 (point-max))
4031 "\n" "/*.pro ")
4032 "[ \t]+"))
4033 (set-buffer save_buffer)
4034 (kill-buffer buffer))
4035 (setq files (list (concat dir "/*.pro"))))
4036 ;;
4037 ;; For each subdirectory
4038 (setq numfiles 0)
4039 (setq item (nth numfiles files))
4040 (while (and item)
4041 ;;
4042 ;; Call etags
4043 (if (not (string-match "^[ \\t]*$" item))
4044 (progn
4045 (message "%s" (concat "Tagging " item "..."))
4046 (setq errbuf (get-buffer-create "*idltags-error*"))
4047 (setq status (+ status
4048 (if (eq 0 (call-process
4049 "sh" nil errbuf nil "-c"
4050 (concat cmd append item)))
4051 0
4052 1)))
4053 ;;
4054 ;; Append additional tags
4055 (setq append " --append ")
4056 (setq numfiles (1+ numfiles))
4057 (setq item (nth numfiles files)))
4058 (progn
4059 (setq numfiles (1+ numfiles))
4060 (setq item (nth numfiles files))
4061 )))
4062
4063 (setq numdirs (1+ numdirs))
4064 (setq dir (nth numdirs directories)))
4065 (progn
4066 (setq numdirs (1+ numdirs))
4067 (setq dir (nth numdirs directories)))))
4068
4069 (setq errbuf (get-buffer-create "*idltags-error*"))
4070 (if (= status 0)
4071 (kill-buffer errbuf))
4072 (message "")
4073 ))
4074
4075 (defun idlwave-toggle-comment-region (beg end &optional n)
4076 "Comment the lines in the region if the first non-blank line is
4077 commented, and conversely, uncomment region. If optional prefix arg
4078 N is non-nil, then for N positive, add N comment delimiters or for N
4079 negative, remove N comment delimiters.
4080 Uses `comment-region' which does not place comment delimiters on
4081 blank lines."
4082 (interactive "r\nP")
4083 (if n
4084 (comment-region beg end (prefix-numeric-value n))
4085 (save-excursion
4086 (goto-char beg)
4087 (beginning-of-line)
4088 ;; skip blank lines
4089 (skip-chars-forward " \t\n")
4090 (if (looking-at (concat "[ \t]*\\(" comment-start "+\\)"))
4091 (if (fboundp 'uncomment-region)
4092 (uncomment-region beg end)
4093 (comment-region beg end
4094 (- (length (buffer-substring
4095 (match-beginning 1)
4096 (match-end 1))))))
4097 (comment-region beg end)))))
4098
4099
4100 ;; ----------------------------------------------------------------------------
4101 ;; ----------------------------------------------------------------------------
4102 ;; ----------------------------------------------------------------------------
4103 ;; ----------------------------------------------------------------------------
4104 ;;
4105 ;; Completion and Routine Info
4106 ;;
4107
4108 ;; String "intern" functions
4109
4110 ;; For the completion and routine info function, we want to normalize
4111 ;; the case of procedure names etc. We do this by "interning" these
4112 ;; string is a hand-crafted way. Hashes are used to map the downcase
4113 ;; version of the strings to the cased versions. Most *-sint-*
4114 ;; variables consist of *two* hashes, a buffer+shell, followed by a
4115 ;; system hash. The former is re-scanned, and the latter takes case
4116 ;; precedence.
4117 ;;
4118 ;; Since these cased versions are really lisp objects, we can use `eq'
4119 ;; to search, which is a large performance boost. All new strings
4120 ;; need to be "sinterned". We do this as early as possible after
4121 ;; getting these strings from completion or buffer substrings. So
4122 ;; most of the code can simply assume to deal with "sinterned"
4123 ;; strings. The only exception is that the functions which scan whole
4124 ;; buffers for routine information do not intern the grabbed strings.
4125 ;; This is only done afterwards. Therefore in these functions it is
4126 ;; *not* safe to assume the strings can be compared with `eq' and be
4127 ;; fed into the routine assq functions.
4128
4129 ;; Here we define the hashing functions.
4130
4131 ;; The variables which hold the hashes.
4132 (defvar idlwave-sint-routines '(nil))
4133 (defvar idlwave-sint-keywords '(nil))
4134 (defvar idlwave-sint-methods '(nil))
4135 (defvar idlwave-sint-classes '(nil))
4136 (defvar idlwave-sint-dirs '(nil))
4137 (defvar idlwave-sint-libnames '(nil))
4138
4139 (defun idlwave-reset-sintern (&optional what)
4140 "Reset all sintern hashes."
4141 ;; Make sure the hash functions are accessible.
4142 (if (or (not (fboundp 'gethash))
4143 (not (fboundp 'puthash)))
4144 (progn
4145 (require 'cl)
4146 (or (fboundp 'puthash)
4147 (defalias 'puthash 'cl-puthash))))
4148 (let ((entries '((idlwave-sint-routines 1000 10)
4149 (idlwave-sint-keywords 1000 10)
4150 (idlwave-sint-methods 100 10)
4151 (idlwave-sint-classes 10 10))))
4152
4153 ;; Make sure these are lists
4154 (loop for entry in entries
4155 for var = (car entry)
4156 do (if (not (consp (symbol-value var))) (set var (list nil))))
4157
4158 ;; Reset the system & library hash
4159 (when (or (eq what t) (eq what 'syslib)
4160 (null (cdr idlwave-sint-routines)))
4161 (loop for entry in entries
4162 for var = (car entry) for size = (nth 1 entry)
4163 do (setcdr (symbol-value var)
4164 (make-hash-table ':size size ':test 'equal)))
4165 (setq idlwave-sint-dirs nil
4166 idlwave-sint-libnames nil))
4167
4168 ;; Reset the buffer & shell hash
4169 (when (or (eq what t) (eq what 'bufsh)
4170 (null (car idlwave-sint-routines)))
4171 (loop for entry in entries
4172 for var = (car entry) for size = (nth 1 entry)
4173 do (setcar (symbol-value var)
4174 (make-hash-table ':size size ':test 'equal))))))
4175
4176 (defun idlwave-sintern-routine-or-method (name &optional class set)
4177 (if class
4178 (idlwave-sintern-method name set)
4179 (idlwave-sintern-routine name set)))
4180
4181 (defun idlwave-sintern (stype &rest args)
4182 (apply (intern (concat "idlwave-sintern-" (symbol-name stype))) args))
4183
4184 ;;(defmacro idlwave-sintern (type var)
4185 ;; `(cond ((not (stringp name)) name)
4186 ;; ((gethash (downcase name) (cdr ,var)))
4187 ;; ((gethash (downcase name) (car ,var)))
4188 ;; (set (idlwave-sintern-set name ,type ,var set))
4189 ;; (name)))
4190
4191 (defun idlwave-sintern-routine (name &optional set)
4192 (cond ((not (stringp name)) name)
4193 ((gethash (downcase name) (cdr idlwave-sint-routines)))
4194 ((gethash (downcase name) (car idlwave-sint-routines)))
4195 (set (idlwave-sintern-set name 'routine idlwave-sint-routines set))
4196 (name)))
4197 (defun idlwave-sintern-keyword (name &optional set)
4198 (cond ((not (stringp name)) name)
4199 ((gethash (downcase name) (cdr idlwave-sint-keywords)))
4200 ((gethash (downcase name) (car idlwave-sint-keywords)))
4201 (set (idlwave-sintern-set name 'keyword idlwave-sint-keywords set))
4202 (name)))
4203 (defun idlwave-sintern-method (name &optional set)
4204 (cond ((not (stringp name)) name)
4205 ((gethash (downcase name) (cdr idlwave-sint-methods)))
4206 ((gethash (downcase name) (car idlwave-sint-methods)))
4207 (set (idlwave-sintern-set name 'method idlwave-sint-methods set))
4208 (name)))
4209 (defun idlwave-sintern-class (name &optional set)
4210 (cond ((not (stringp name)) name)
4211 ((gethash (downcase name) (cdr idlwave-sint-classes)))
4212 ((gethash (downcase name) (car idlwave-sint-classes)))
4213 (set (idlwave-sintern-set name 'class idlwave-sint-classes set))
4214 (name)))
4215
4216 (defun idlwave-sintern-dir (dir &optional set)
4217 (car (or (member dir idlwave-sint-dirs)
4218 (setq idlwave-sint-dirs (cons dir idlwave-sint-dirs)))))
4219 (defun idlwave-sintern-libname (name &optional set)
4220 (car (or (member name idlwave-sint-libnames)
4221 (setq idlwave-sint-libnames (cons name idlwave-sint-libnames)))))
4222
4223 (defun idlwave-sintern-set (name type tables set)
4224 (let* ((func (or (cdr (assq type idlwave-completion-case))
4225 'identity))
4226 (iname (funcall (if (eq func 'preserve) 'identity func) name))
4227 (table (if (eq set 'sys) (cdr tables) (car tables))))
4228 (puthash (downcase name) iname table)
4229 iname))
4230
4231 (defun idlwave-sintern-keyword-list (kwd-list &optional set)
4232 "Sintern a set of keywords (file (key . link) (key2 . link2) ...)"
4233 (mapc (lambda(x)
4234 (setcar x (idlwave-sintern-keyword (car x) set)))
4235 (cdr kwd-list))
4236 kwd-list)
4237
4238 (defun idlwave-sintern-rinfo-list (list &optional set default-dir)
4239 "Sintern all strings in the rinfo LIST.
4240 With optional parameter SET: also set new patterns. Probably this
4241 will always have to be t. If DEFAULT-DIR is passed, it is used as
4242 the base of the directory."
4243 (let (entry name type class kwds res source call new)
4244 (while list
4245 (setq entry (car list)
4246 list (cdr list)
4247 name (car entry)
4248 type (nth 1 entry)
4249 class (nth 2 entry)
4250 source (nth 3 entry)
4251 call (nth 4 entry)
4252 kwds (nthcdr 5 entry))
4253
4254 ;; The class and name
4255 (if class
4256 (progn
4257 (if (symbolp class) (setq class (symbol-name class)))
4258 (setq class (idlwave-sintern-class class set))
4259 (setq name (idlwave-sintern-method name set)))
4260 (setq name (idlwave-sintern-routine name set)))
4261
4262 ;; The source
4263 (let ((source-type (car source))
4264 (source-file (nth 1 source))
4265 (source-dir (if default-dir
4266 (file-name-as-directory default-dir)
4267 (nth 2 source)))
4268 (source-lib (nth 3 source)))
4269 (if (stringp source-dir)
4270 (setq source-dir (idlwave-sintern-dir source-dir set)))
4271 (if (stringp source-lib)
4272 (setq source-lib (idlwave-sintern-libname source-lib set)))
4273 (setq source (list source-type source-file source-dir source-lib)))
4274
4275 ;; The keywords
4276 (setq kwds (mapcar (lambda (x)
4277 (idlwave-sintern-keyword-list x set))
4278 kwds))
4279
4280 ;; Build a canonicalized list
4281 (setq new (nconc (list name type class source call) kwds)
4282 res (cons new res)))
4283 (nreverse res)))
4284
4285 ;; Creating new sintern tables
4286
4287 (defun idlwave-new-sintern-type (tag)
4288 "Define a variable and a function to sintern the new type TAG.
4289 This defines the function `idlwave-sintern-TAG' and the variable
4290 `idlwave-sint-TAGs'."
4291 (let* ((name (symbol-name tag))
4292 (names (concat name "s"))
4293 (var (intern (concat "idlwave-sint-" names)))
4294 (func (intern (concat "idlwave-sintern-" name))))
4295 (set var nil) ; initial value of the association list
4296 (fset func ; set the function
4297 `(lambda (name &optional set)
4298 (cond ((not (stringp name)) name)
4299 ((cdr (assoc (downcase name) ,var)))
4300 (set
4301 (setq ,var (cons (cons (downcase name) name) ,var))
4302 name)
4303 (name))))))
4304
4305 (defun idlwave-reset-sintern-type (tag)
4306 "Reset the sintern variable associated with TAG."
4307 (set (intern (concat "idlwave-sint-" (symbol-name tag) "s")) nil))
4308
4309 ;;---------------------------------------------------------------------------
4310
4311
4312 ;; The variables which hold the information
4313 (defvar idlwave-system-routines nil
4314 "Holds the routine-info obtained by scanning buffers.")
4315 (defvar idlwave-buffer-routines nil
4316 "Holds the routine-info obtained by scanning buffers.")
4317 (defvar idlwave-compiled-routines nil
4318 "Holds the routine-info obtained by asking the shell.")
4319 (defvar idlwave-unresolved-routines nil
4320 "Holds the unresolved routine-info obtained by asking the shell.")
4321 (defvar idlwave-user-catalog-routines nil
4322 "Holds the procedure routine-info from the user scan.")
4323 (defvar idlwave-library-catalog-routines nil
4324 "Holds the procedure routine-info from the .idlwave_catalog library files.")
4325 (defvar idlwave-library-catalog-libname nil
4326 "Name of library catalog loaded from .idlwave_catalog files.")
4327 (defvar idlwave-path-alist nil
4328 "Alist with !PATH directories and zero or more flags if the dir has
4329 been scanned in a user catalog ('user) or discovered in a library
4330 catalog \('lib).")
4331 (defvar idlwave-true-path-alist nil
4332 "Like `idlwave-path-alist', but with true filenames.")
4333 (defvar idlwave-routines nil
4334 "Holds the combined procedure/function/method routine-info.")
4335 (defvar idlwave-class-alist nil
4336 "Holds the class names known to IDLWAVE.")
4337 (defvar idlwave-class-history nil
4338 "The history of classes selected with the minibuffer.")
4339 (defvar idlwave-force-class-query nil)
4340 (defvar idlwave-before-completion-wconf nil
4341 "The window configuration just before the completion buffer was displayed.")
4342 (defvar idlwave-last-system-routine-info-cons-cell nil
4343 "The last cons cell in the system routine info.")
4344
4345 ;;
4346 ;; The code to get routine info from different sources.
4347
4348 (defvar idlwave-system-routines)
4349 (defvar idlwave-catalog-process nil
4350 "The background process currently updating the catalog.")
4351
4352 (defun idlwave-routines ()
4353 "Provide a list of IDL routines.
4354 This routine loads the builtin routines on the first call.
4355 Later it only returns the value of the variable."
4356 (if (and idlwave-catalog-process
4357 (processp idlwave-catalog-process))
4358 (progn
4359 (cond
4360 ((equal (process-status idlwave-catalog-process) 'exit)
4361 (message "updating........")
4362 (setq idlwave-catalog-process nil)
4363 (idlwave-update-routine-info '(4)))
4364 ((equal (process-status idlwave-catalog-process) 'run)
4365 ;; Keep it running...
4366 )
4367 (t
4368 ;; Something is wrong, get rid of the process
4369 (message "Problem with catalog process") (beep)
4370 (condition-case nil
4371 (kill-process idlwave-catalog-process)
4372 (error nil))
4373 (setq idlwave-catalog-process nil)))))
4374 (or idlwave-routines
4375 (progn
4376 (idlwave-update-routine-info)
4377 ;; return the current value
4378 idlwave-routines)))
4379
4380 (defvar idlwave-update-rinfo-hook nil
4381 "List of functions which should run after a global rinfo update.
4382 Does not run after automatic updates of buffer or the shell.")
4383
4384 (defun idlwave-rescan-catalog-directories ()
4385 "Rescan the previously selected directories. For batch processing."
4386 (idlwave-update-routine-info '(16)))
4387
4388 (defun idlwave-rescan-asynchronously ()
4389 "Dispatch another Emacs instance to update the idlwave catalog.
4390 After the process finishes normally, the first access to routine info
4391 will re-read the catalog."
4392 (interactive)
4393 (if (processp idlwave-catalog-process)
4394 (if (eq (process-status idlwave-catalog-process) 'run)
4395 (if (yes-or-no-p "A catalog-updating process is running. Kill it? ")
4396 (progn
4397 (condition-case nil
4398 (kill-process idlwave-catalog-process)
4399 (error nil))
4400 (error "Process killed, no new process started"))
4401 (error "Quit"))
4402 (condition-case nil
4403 (kill-process idlwave-catalog-process)
4404 (error nil))))
4405 (if (or (not idlwave-user-catalog-file)
4406 (not (stringp idlwave-user-catalog-file))
4407 (not (file-regular-p idlwave-user-catalog-file)))
4408 (error "No catalog has been produced yet"))
4409 (let* ((emacs (concat invocation-directory invocation-name))
4410 (args (list "-batch"
4411 "-l" (expand-file-name "~/.emacs")
4412 "-l" "idlwave"
4413 "-f" "idlwave-rescan-catalog-directories"))
4414 (process (apply 'start-process "idlcat"
4415 nil emacs args)))
4416 (setq idlwave-catalog-process process)
4417 (set-process-sentinel
4418 process
4419 (lambda (pro why)
4420 (when (string-match "finished" why)
4421 (setq idlwave-routines nil
4422 idlwave-system-routines nil
4423 idlwave-catalog-process nil)
4424 (or (idlwave-start-load-rinfo-timer)
4425 (idlwave-update-routine-info '(4))))))
4426 (message "Background job started to update catalog file")))
4427
4428
4429 ;; Format for all routine info user catalog, library catalogs, etc.:
4430 ;;
4431 ;; ("ROUTINE" type class
4432 ;; (system) | (lib pro_file dir "LIBNAME") | (user pro_file dir "USERLIB") |
4433 ;; (buffer pro_file dir) | (compiled pro_file dir)
4434 ;; "calling_string" ("HELPFILE" (("KWD1" . link1) ...))
4435 ;; ("HELPFILE2" (("KWD2" . link) ...)) ...)
4436 ;;
4437 ;; DIR will be supplied dynamically while loading library catalogs,
4438 ;; and is sinterned to save space, as is LIBNAME. PRO_FILE can be a
4439 ;; complete filepath, in which case DIR is unnecessary. HELPFILE can
4440 ;; be nil, as can LINK1, etc., if no HTML help is available.
4441
4442
4443 (defvar idlwave-load-rinfo-idle-timer)
4444 (defvar idlwave-shell-path-query)
4445
4446 (defun idlwave-update-routine-info (&optional arg no-concatenate)
4447 "Update the internal routine-info lists.
4448 These lists are used by `idlwave-routine-info' (\\[idlwave-routine-info])
4449 and by `idlwave-complete' (\\[idlwave-complete]) to provide information
4450 about individual routines.
4451
4452 The information can come from 4 sources:
4453 1. IDL programs in the current editing session
4454 2. Compiled modules in an IDL shell running as Emacs subprocess
4455 3. A list which covers the IDL system routines.
4456 4. A list which covers the prescanned library files.
4457
4458 Scans all IDLWAVE-mode buffers of the current editing session (see
4459 `idlwave-scan-all-buffers-for-routine-info').
4460 When an IDL shell is running, this command also queries the IDL program
4461 for currently compiled routines.
4462
4463 With prefix ARG, also reload the system and library lists.
4464 With two prefix ARG's, also rescans the chosen user catalog tree.
4465 With three prefix args, dispatch asynchronous process to do the update.
4466
4467 If NO-CONCATENATE is non-nil, don't pre-concatenate the routine info
4468 lists, but instead wait for the shell query to complete and
4469 asynchronously finish updating routine info. This is set
4470 automatically when called interactively. When you need routine
4471 information updated immediately, leave NO-CONCATENATE nil."
4472 (interactive "P\np")
4473 ;; Stop any idle processing
4474 (if (or (and (fboundp 'itimerp)
4475 (itimerp idlwave-load-rinfo-idle-timer))
4476 (and (fboundp 'timerp)
4477 (timerp idlwave-load-rinfo-idle-timer)))
4478 (cancel-timer idlwave-load-rinfo-idle-timer))
4479 (cond
4480 ((equal arg '(64))
4481 ;; Start a background process which updates the catalog.
4482 (idlwave-rescan-asynchronously))
4483 ((equal arg '(16))
4484 ;; Update the user catalog now, and wait for them.
4485 (idlwave-create-user-catalog-file t))
4486 (t
4487 (let* ((load (or arg
4488 idlwave-buffer-case-takes-precedence
4489 (null idlwave-routines)))
4490 ;; The override-idle means, even if the idle timer has done some
4491 ;; preparing work, load and renormalize everything anyway.
4492 (override-idle (or arg idlwave-buffer-case-takes-precedence)))
4493
4494 (setq idlwave-buffer-routines nil
4495 idlwave-compiled-routines nil
4496 idlwave-unresolved-routines nil)
4497 ;; Reset the appropriate hashes
4498 (if (get 'idlwave-reset-sintern 'done-by-idle)
4499 ;; reset was already done in idle time, so skip this step now once
4500 (put 'idlwave-reset-sintern 'done-by-idle nil)
4501 (idlwave-reset-sintern (cond (load t)
4502 ((null idlwave-system-routines) t)
4503 (t 'bufsh))))
4504
4505 (if idlwave-buffer-case-takes-precedence
4506 ;; We can safely scan the buffer stuff first
4507 (progn
4508 (idlwave-update-buffer-routine-info)
4509 (and load (idlwave-load-all-rinfo override-idle)))
4510 ;; We first do the system info, and then the buffers
4511 (and load (idlwave-load-all-rinfo override-idle))
4512 (idlwave-update-buffer-routine-info))
4513
4514 ;; Let's see if there is a shell
4515 (let* ((shell-is-running (and (fboundp 'idlwave-shell-is-running)
4516 (idlwave-shell-is-running)))
4517 (ask-shell (and shell-is-running
4518 idlwave-query-shell-for-routine-info)))
4519
4520 ;; Load the library catalogs again, first re-scanning the path
4521 (when arg
4522 (if shell-is-running
4523 (idlwave-shell-send-command idlwave-shell-path-query
4524 '(progn
4525 (idlwave-shell-get-path-info)
4526 (idlwave-scan-library-catalogs))
4527 'hide)
4528 (idlwave-scan-library-catalogs)))
4529
4530 (if (or (not ask-shell)
4531 (not no-concatenate))
4532 ;; 1. If we are not going to ask the shell, we need to do the
4533 ;; concatenation now.
4534 ;; 2. When this function is called non-interactively, it
4535 ;; means that someone needs routine info *now*. The
4536 ;; shell update causes the concatenation to be
4537 ;; *delayed*, so not in time for the current command.
4538 ;; Therefore, we do a concatenation now, even though
4539 ;; the shell might do it again.
4540 (idlwave-concatenate-rinfo-lists nil 'run-hooks))
4541
4542 (when ask-shell
4543 ;; Ask the shell about the routines it knows of.
4544 (message "Querying the shell")
4545 (idlwave-shell-update-routine-info nil t)))))))
4546
4547
4548 (defvar idlwave-load-rinfo-steps-done (make-vector 6 nil))
4549 (defvar idlwave-load-rinfo-idle-timer nil)
4550 (defun idlwave-start-load-rinfo-timer ()
4551 (if (or (and (fboundp 'itimerp)
4552 (itimerp idlwave-load-rinfo-idle-timer))
4553 (and (fboundp 'timerp)
4554 (timerp idlwave-load-rinfo-idle-timer)))
4555 (cancel-timer idlwave-load-rinfo-idle-timer))
4556 (setq idlwave-load-rinfo-steps-done (make-vector 6 nil))
4557 (setq idlwave-load-rinfo-idle-timer nil)
4558 (if (and idlwave-init-rinfo-when-idle-after
4559 (numberp idlwave-init-rinfo-when-idle-after)
4560 (not (equal 0 idlwave-init-rinfo-when-idle-after))
4561 (not idlwave-routines))
4562 (condition-case nil
4563 (progn
4564 (setq idlwave-load-rinfo-idle-timer
4565 (run-with-idle-timer
4566 idlwave-init-rinfo-when-idle-after
4567 nil 'idlwave-load-rinfo-next-step)))
4568 (error nil))))
4569
4570 (defvar idlwave-library-routines nil "Obsolete variable.")
4571
4572 ;;------ XML Help routine info system
4573 (defun idlwave-load-system-routine-info ()
4574 ;; Load the system routine info from the cached routine info file,
4575 ;; which, if necessary, will be re-created from the XML file on
4576 ;; disk. As a last fallback, load the (likely outdated) idlw-rinfo
4577 ;; file distributed with older IDLWAVE versions (<6.0)
4578 (unless (and (load idlwave-xml-system-rinfo-converted-file
4579 'noerror 'nomessage)
4580 (idlwave-xml-system-routine-info-up-to-date))
4581 ;; See if we can create it from XML source
4582 (condition-case nil
4583 (idlwave-convert-xml-system-routine-info)
4584 (error
4585 (unless (load idlwave-xml-system-rinfo-converted-file
4586 'noerror 'nomessage)
4587 (if idlwave-system-routines
4588 (message
4589 "Failed to load converted routine info, using old conversion.")
4590 (message
4591 "Failed to convert XML routine info, falling back on idlw-rinfo.")
4592 (if (not (load "idlw-rinfo" 'noerror 'nomessage))
4593 (message
4594 "Could not locate any system routine information."))))))))
4595
4596 (defun idlwave-xml-system-routine-info-up-to-date()
4597 (let* ((dir (file-name-as-directory
4598 (expand-file-name "help/online_help" (idlwave-sys-dir))))
4599 (catalog-file (expand-file-name "idl_catalog.xml" dir)))
4600 (file-newer-than-file-p ;converted file is newer than catalog
4601 idlwave-xml-system-rinfo-converted-file
4602 catalog-file)))
4603
4604 (defvar idlwave-system-class-info nil) ; Gathered from idlw-rinfo
4605 (defvar idlwave-system-variables-alist nil
4606 "Alist of system variables and the associated structure tags.
4607 Gets set in cached XML rinfo, or `idlw-rinfo.el'.")
4608 (defvar idlwave-executive-commands-alist nil
4609 "Alist of system variables and their help files.")
4610 (defvar idlwave-help-special-topic-words nil)
4611
4612
4613 (defun idlwave-shorten-syntax (syntax name &optional class)
4614 ;; From a list of syntax statments, shorten with %s and group with "or"
4615 (let ((case-fold-search t))
4616 (mapconcat
4617 (lambda (x)
4618 (while (string-match name x)
4619 (setq x (replace-match "%s" t t x)))
4620 (if class
4621 (while (string-match class x)
4622 (setq x (replace-match "%s" t t x))))
4623 x)
4624 (nreverse syntax)
4625 " or ")))
4626
4627 (defun idlwave-xml-create-class-method-lists (xml-entry)
4628 ;; Create a class list entry from the xml parsed list., returning a
4629 ;; cons of form (class-entry method-entries).
4630 (let* ((nameblock (nth 1 xml-entry))
4631 (class (cdr (assq 'name nameblock)))
4632 (link (cdr (assq 'link nameblock)))
4633 (params (cddr xml-entry))
4634 (case-fold-search t)
4635 class-entry
4636 method methods-entry extra-kwds
4637 props get-props set-props init-props inherits
4638 pelem ptype)
4639 (while params
4640 (setq pelem (car params))
4641 (when (listp pelem)
4642 (setq ptype (car pelem)
4643 props (car (cdr pelem)))
4644 (cond
4645 ((eq ptype 'SUPERCLASS)
4646 (let ((pname (cdr (assq 'name props)))
4647 (plink (cdr (assq 'link props))))
4648 (unless (and (string= pname "None")
4649 (string= plink "None"))
4650 (push pname inherits))))
4651
4652 ((eq ptype 'PROPERTY)
4653 (let ((pname (cdr (assq 'name props)))
4654 (plink (cdr (assq 'link props)))
4655 (get (string= (cdr (assq 'get props)) "Yes"))
4656 (set (string= (cdr (assq 'set props)) "Yes"))
4657 (init (string= (cdr (assq 'init props)) "Yes")))
4658 (if get (push (list pname plink) get-props))
4659 (if set (push (list pname plink) set-props))
4660 (if init (push (list pname plink) init-props))))
4661
4662 ((eq ptype 'METHOD)
4663 (setq method (cdr (assq 'name props)))
4664 (setq extra-kwds ;;Assume all property keywords are gathered already
4665 (cond
4666 ((string-match (concat class "::Init") method)
4667 (put 'init-props 'matched t)
4668 init-props)
4669 ((string-match (concat class "::GetProperty") method)
4670 (put 'get-props 'matched t)
4671 get-props)
4672 ((string-match (concat class "::SetProperty") method)
4673 (put 'set-props 'matched t)
4674 set-props)
4675 (t nil)))
4676 (setq methods-entry
4677 (nconc (idlwave-xml-create-rinfo-list pelem class extra-kwds)
4678 methods-entry)))
4679 (t)))
4680 (setq params (cdr params)))
4681 ;(unless (get 'init-props 'matched)
4682 ; (message "Failed to match Init in class %s" class))
4683 ;(unless (get 'get-props 'matched)
4684 ; (message "Failed to match GetProperty in class %s" class))
4685 ;(unless (get 'set-props 'matched)
4686 ; (message "Failed to match SetProperty in class %s" class))
4687 (setq class-entry
4688 (if inherits
4689 (list class (append '(inherits) inherits) (list 'link link))
4690 (list class (list 'link link))))
4691 (cons class-entry methods-entry)))
4692
4693 (defun idlwave-xml-create-rinfo-list (xml-entry &optional class extra-kws)
4694 ;; Create correctly structured list elements from ROUTINE or METHOD
4695 ;; XML list structures. Return a list of list elements, with more
4696 ;; than one sub-list possible if a routine can serve as both
4697 ;; procedure and function (e.g. call_method).
4698 (let* ((nameblock (nth 1 xml-entry))
4699 (name (cdr (assq 'name nameblock)))
4700 (link (cdr (assq 'link nameblock)))
4701 (params (cddr xml-entry))
4702 (syntax-vec (make-vector 3 nil)) ; procedure, function, exec command
4703 (case-fold-search t)
4704 syntax kwd klink pref-list kwds pelem ptype entry props result type)
4705 (if class ;; strip out class name from class method name string
4706 (if (string-match (concat class "::") name)
4707 (setq name (substring name (match-end 0)))))
4708 (while params
4709 (setq pelem (car params))
4710 (when (listp pelem)
4711 (setq ptype (car pelem)
4712 props (car (cdr pelem)))
4713 (cond
4714 ((eq ptype 'SYNTAX)
4715 (setq syntax (cdr (assq 'name props)))
4716 (if (string-match "-&gt;" syntax)
4717 (setq syntax (replace-match "->" t nil syntax)))
4718 (setq type (cdr (assq 'type props)))
4719 (push syntax
4720 (aref syntax-vec (cond
4721 ((string-match "^pro" type) 0)
4722 ((string-match "^fun" type) 1)
4723 ((string-match "^exec" type) 2)))))
4724 ((eq ptype 'KEYWORD)
4725 (setq kwd (cdr (assq 'name props))
4726 klink (cdr (assq 'link props)))
4727 (if (string-match "^\\[XY\\(Z?\\)\\]" kwd)
4728 (progn
4729 (setq pref-list
4730 (if (match-string 1 kwd) '("X" "Y" "Z") '("X" "Y"))
4731 kwd (substring kwd (match-end 0)))
4732 (loop for x in pref-list do
4733 (push (list (concat x kwd) klink) kwds)))
4734 (push (list kwd klink) kwds)))
4735
4736 (t))); Do nothing for the others
4737 (setq params (cdr params)))
4738
4739 ;; Debug
4740 ; (if (and (null (aref syntax-vec 0))
4741 ; (null (aref syntax-vec 1))
4742 ; (null (aref syntax-vec 2)))
4743 ; (with-current-buffer (get-buffer-create "IDL_XML_catalog_complaints")
4744 ; (if class
4745 ; (insert (format "Missing SYNTAX entry for %s::%s\n" class name))
4746 ; (insert (message "Missing SYNTAX entry for %s\n" name)))))
4747
4748 ;; Executive commands are treated specially
4749 (if (aref syntax-vec 2)
4750 (cons (substring name 1) link)
4751 (if extra-kws (setq kwds (nconc kwds extra-kws)))
4752 (setq kwds (idlwave-rinfo-group-keywords kwds link))
4753 (loop for idx from 0 to 1 do
4754 (if (aref syntax-vec idx)
4755 (push (append (list name (if (eq idx 0) 'pro 'fun)
4756 class '(system)
4757 (idlwave-shorten-syntax
4758 (aref syntax-vec idx) name class))
4759 kwds) result)))
4760 result)))
4761
4762
4763 (defun idlwave-rinfo-group-keywords (kwds master-link)
4764 ;; Group keywords by link file, as a list with elements
4765 ;; (linkfile ( ("KWD1" . link1) ("KWD2" . link2))
4766 (let (kwd link anchor linkfiles block master-elt)
4767 (while kwds
4768 (setq kwd (car kwds)
4769 link (idlwave-split-link-target (nth 1 kwd))
4770 anchor (cdr link)
4771 link (car link)
4772 kwd (car kwd))
4773 (if (setq block (assoc link linkfiles))
4774 (push (cons kwd anchor) (cdr block))
4775 (push (list link (cons kwd anchor)) linkfiles))
4776 (setq kwds (cdr kwds)))
4777 ;; Ensure the master link is there
4778 (if (setq master-elt (assoc master-link linkfiles))
4779 (if (eq (car linkfiles) master-elt)
4780 linkfiles
4781 (cons master-elt (delq master-elt linkfiles)))
4782 (push (list master-link) linkfiles))))
4783
4784 (defun idlwave-convert-xml-clean-statement-aliases (aliases)
4785 ;; Clean up the syntax of routines which are actually aliases by
4786 ;; removing the "OR" from the statements
4787 (let (syntax entry)
4788 (loop for x in aliases do
4789 (setq entry (assoc x idlwave-system-routines))
4790 (when entry
4791 (while (string-match " +or +" (setq syntax (nth 4 entry)))
4792 (setf (nth 4 entry) (replace-match ", " t t syntax)))))))
4793
4794 (defun idlwave-convert-xml-clean-routine-aliases (aliases)
4795 ;; Duplicate and trim original routine aliases from rinfo list
4796 ;; This if for, e.g. OPENR/OPENW/OPENU
4797 (let (alias remove-list new parts all-parts)
4798 (loop for x in aliases do
4799 (when (setq parts (split-string (cdr x) "/"))
4800 (setq new (assoc (cdr x) all-parts))
4801 (unless new
4802 (setq new (cons (cdr x) parts))
4803 (push new all-parts))
4804 (setcdr new (delete (car x) (cdr new)))))
4805
4806 ;; Add any missing aliases (separate by slashes)
4807 (loop for x in all-parts do
4808 (if (cdr x)
4809 (push (cons (nth 1 x) (car x)) aliases)))
4810
4811 (loop for x in aliases do
4812 (when (setq alias (assoc (cdr x) idlwave-system-routines))
4813 (unless (memq alias remove-list) (push alias remove-list))
4814 (setq alias (copy-sequence alias))
4815 (setcar alias (car x))
4816 (push alias idlwave-system-routines)))
4817 (loop for x in remove-list do
4818 (delq x idlwave-system-routines))))
4819
4820 (defun idlwave-convert-xml-clean-sysvar-aliases (aliases)
4821 ;; Duplicate and trim original routine aliases from rinfo list
4822 ;; This if for, e.g. !X, !Y, !Z.
4823 (let (alias remove-list new parts all-parts)
4824 (loop for x in aliases do
4825 (when (setq alias (assoc (cdr x) idlwave-system-variables-alist))
4826 (unless (memq alias remove-list) (push alias remove-list))
4827 (setq alias (copy-sequence alias))
4828 (setcar alias (car x))
4829 (push alias idlwave-system-variables-alist)))
4830 (loop for x in remove-list do
4831 (delq x idlwave-system-variables-alist))))
4832
4833
4834 (defun idlwave-xml-create-sysvar-alist (xml-entry)
4835 ;; Create a sysvar list entry from the xml parsed list.
4836 (let* ((nameblock (nth 1 xml-entry))
4837 (name (cdr (assq 'name nameblock)))
4838 (sysvar (substring name (progn (string-match "^ *!" name)
4839 (match-end 0))))
4840 (link (cdr (assq 'link nameblock)))
4841 (params (cddr xml-entry))
4842 (case-fold-search t)
4843 pelem ptype props fields tags)
4844 (while params
4845 (setq pelem (car params))
4846 (when (listp pelem)
4847 (setq ptype (car pelem)
4848 props (car (cdr pelem)))
4849 (cond
4850 ((eq ptype 'FIELD)
4851 (push (cons (cdr (assq 'name props))
4852 (cdr
4853 (idlwave-split-link-target (cdr (assq 'link props)))))
4854 tags))))
4855 (setq params (cdr params)))
4856 (delq nil
4857 (list sysvar (if tags (cons 'tags tags)) (list 'link link)))))
4858
4859
4860 (defvar idlwave-xml-routine-info-file nil)
4861
4862 (defun idlwave-save-routine-info ()
4863 (if idlwave-xml-routine-info-file
4864 (with-temp-file idlwave-xml-system-rinfo-converted-file
4865 (insert
4866 (concat ";; *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
4867 ;; IDLWAVE Routine Information File (IDLWAVE version " idlwave-mode-version ")
4868 ;; Automatically generated from source file:
4869 ;; " idlwave-xml-routine-info-file "
4870 ;; on " (current-time-string) "
4871 ;; Do not edit."))
4872 (insert (format "\n(setq idlwave-xml-routine-info-file \n \"%s\")"
4873 idlwave-xml-routine-info-file))
4874 (insert "\n(setq idlwave-system-routines\n '")
4875 (prin1 idlwave-system-routines (current-buffer))
4876 (insert ")")
4877 (insert "\n(setq idlwave-system-variables-alist\n '")
4878 (prin1 idlwave-system-variables-alist (current-buffer))
4879 (insert ")")
4880 (insert "\n(setq idlwave-system-class-info\n '")
4881 (prin1 idlwave-system-class-info (current-buffer))
4882 (insert ")")
4883 (insert "\n(setq idlwave-executive-commands-alist\n '")
4884 (prin1 idlwave-executive-commands-alist (current-buffer))
4885 (insert ")")
4886 (insert "\n(setq idlwave-help-special-topic-words\n '")
4887 (prin1 idlwave-help-special-topic-words (current-buffer))
4888 (insert ")"))))
4889
4890 (defun idlwave-convert-xml-system-routine-info ()
4891 "Convert XML supplied IDL routine info into internal form.
4892 Cache to disk for quick recovery."
4893 (interactive)
4894 (let* ((dir (file-name-as-directory
4895 (expand-file-name "help/online_help" (idlwave-sys-dir))))
4896 (catalog-file (expand-file-name "idl_catalog.xml" dir))
4897 (elem-cnt 0)
4898 props rinfo msg-cnt elem type nelem class-result alias
4899 routines routine-aliases statement-aliases sysvar-aliases
4900 version-string)
4901 (if (not (file-exists-p catalog-file))
4902 (error "No such XML routine info file: %s" catalog-file)
4903 (if (not (file-readable-p catalog-file))
4904 (error "Cannot read XML routine info file: %s" catalog-file)))
4905 (message "Reading XML routine info...")
4906 (setq rinfo (xml-parse-file catalog-file))
4907 (message "Reading XML routine info...done")
4908 (setq rinfo (assq 'CATALOG rinfo))
4909 (unless rinfo (error "Failed to parse XML routine info"))
4910 ;;(setq rinfo (car rinfo)) ; Skip the catalog stuff.
4911
4912 (setq version-string (cdr (assq 'version (nth 1 rinfo)))
4913 rinfo (cddr rinfo))
4914
4915 (setq nelem (length rinfo)
4916 msg-cnt (/ nelem 20))
4917
4918 (setq idlwave-xml-routine-info-file nil)
4919 (message "Converting XML routine info...")
4920 (setq idlwave-system-routines nil
4921 idlwave-system-variables-alist nil
4922 idlwave-system-class-info nil
4923 idlwave-executive-commands-alist nil
4924 idlwave-help-special-topic-words nil)
4925
4926 (while rinfo
4927 (setq elem (car rinfo)
4928 rinfo (cdr rinfo))
4929 (incf elem-cnt)
4930 (when (listp elem)
4931 (setq type (car elem)
4932 props (car (cdr elem)))
4933 (if (= (mod elem-cnt msg-cnt) 0)
4934 (message "Converting XML routine info...%2d%%"
4935 (/ (* elem-cnt 100) nelem)))
4936 (cond
4937 ((eq type 'ROUTINE)
4938 (if (setq alias (assq 'alias_to props))
4939 (push (cons (cdr (assq 'name props)) (cdr alias))
4940 routine-aliases)
4941 (setq routines (idlwave-xml-create-rinfo-list elem))
4942 (if (listp (cdr routines))
4943 (setq idlwave-system-routines
4944 (nconc idlwave-system-routines routines))
4945 ;; a cons cell is an executive commands
4946 (push routines idlwave-executive-commands-alist))))
4947
4948 ((eq type 'CLASS)
4949 (setq class-result (idlwave-xml-create-class-method-lists elem))
4950 (push (car class-result) idlwave-system-class-info)
4951 (setq idlwave-system-routines
4952 (nconc idlwave-system-routines (cdr class-result))))
4953
4954 ((eq type 'STATEMENT)
4955 (push (cons (cdr (assq 'name props))
4956 (cdr (assq 'link props)))
4957 idlwave-help-special-topic-words)
4958 ;; Save the links to those which are statement aliases (not routines)
4959 (if (setq alias (assq 'alias_to props))
4960 (unless (member (cdr alias) statement-aliases)
4961 (push (cdr alias) statement-aliases))))
4962
4963 ((eq type 'SYSVAR)
4964 (if (setq alias (cdr (assq 'alias_to props)))
4965 (push (cons (substring (cdr (assq 'name props)) 1)
4966 (substring alias 1))
4967 sysvar-aliases)
4968 (push (idlwave-xml-create-sysvar-alist elem)
4969 idlwave-system-variables-alist)))
4970 (t))))
4971 (idlwave-convert-xml-clean-routine-aliases routine-aliases)
4972 (idlwave-convert-xml-clean-statement-aliases statement-aliases)
4973 (idlwave-convert-xml-clean-sysvar-aliases sysvar-aliases)
4974
4975 (setq idlwave-xml-routine-info-file catalog-file)
4976 (idlwave-save-routine-info)
4977 (message "Converting XML routine info...done")))
4978
4979
4980 ;; ("ROUTINE" type class
4981 ;; (system) | (lib pro_file dir "LIBNAME") | (user pro_file dir "USERLIB") |
4982 ;; (buffer pro_file dir) | (compiled pro_file dir)
4983 ;; "calling_string" ("HELPFILE" (("KWD1" . link1) ...))
4984 ;; ("HELPFILE2" (("KWD2" . link) ...)) ...)
4985
4986
4987 (defun idlwave-load-rinfo-next-step ()
4988 (let ((inhibit-quit t)
4989 (arr idlwave-load-rinfo-steps-done))
4990 (if (catch 'exit
4991 (when (not (aref arr 0))
4992 (message "Loading system routine info in idle time...")
4993 (idlwave-load-system-routine-info)
4994 ;;(load "idlw-rinfo" 'noerror 'nomessage)
4995 (message "Loading system routine info in idle time...done")
4996 (aset arr 0 t)
4997 (throw 'exit t))
4998
4999 (when (not (aref arr 1))
5000 (message "Normalizing idlwave-system-routines in idle time...")
5001 (idlwave-reset-sintern t)
5002 (put 'idlwave-reset-sintern 'done-by-idle t)
5003 (setq idlwave-system-routines
5004 (idlwave-sintern-rinfo-list idlwave-system-routines 'sys))
5005 (message "Normalizing idlwave-system-routines in idle time...done")
5006 (aset arr 1 t)
5007 (throw 'exit t))
5008
5009 (when (not (aref arr 2))
5010 (when (and (stringp idlwave-user-catalog-file)
5011 (file-regular-p idlwave-user-catalog-file))
5012 (message "Loading user catalog in idle time...")
5013 (condition-case nil
5014 (load-file idlwave-user-catalog-file)
5015 (error (throw 'exit nil)))
5016 ;; Check for the old style catalog and warn
5017 (if (and
5018 (boundp 'idlwave-library-routines)
5019 idlwave-library-routines)
5020 (progn
5021 (setq idlwave-library-routines nil)
5022 (ding)
5023 (message "Outdated user catalog: %s... recreate"
5024 idlwave-user-catalog-file))
5025 (message "Loading user catalog in idle time...done")))
5026 (aset arr 2 t)
5027 (throw 'exit t))
5028
5029 (when (not (aref arr 3))
5030 (when idlwave-user-catalog-routines
5031 (message "Normalizing user catalog routines in idle time...")
5032 (setq idlwave-user-catalog-routines
5033 (idlwave-sintern-rinfo-list
5034 idlwave-user-catalog-routines 'sys))
5035 (message
5036 "Normalizing user catalog routines in idle time...done"))
5037 (aset arr 3 t)
5038 (throw 'exit t))
5039
5040 (when (not (aref arr 4))
5041 (idlwave-scan-library-catalogs
5042 "Loading and normalizing library catalogs in idle time...")
5043 (aset arr 4 t)
5044 (throw 'exit t))
5045 (when (not (aref arr 5))
5046 (message "Finishing initialization in idle time...")
5047 (idlwave-routines)
5048 (message "Finishing initialization in idle time...done")
5049 (aset arr 5 t)
5050 (throw 'exit nil)))
5051 ;; restart the timer
5052 (if (sit-for 1)
5053 (idlwave-load-rinfo-next-step)
5054 (setq idlwave-load-rinfo-idle-timer
5055 (run-with-idle-timer
5056 idlwave-init-rinfo-when-idle-after
5057 nil 'idlwave-load-rinfo-next-step))))))
5058
5059 (defun idlwave-load-all-rinfo (&optional force)
5060 ;; Load and case-treat the system, user catalog, and library routine
5061 ;; info files.
5062
5063 ;; System
5064 (when (or force (not (aref idlwave-load-rinfo-steps-done 0)))
5065 ;;(load "idlw-rinfo" 'noerror 'nomessage))
5066 (idlwave-load-system-routine-info))
5067 (when (or force (not (aref idlwave-load-rinfo-steps-done 1)))
5068 (message "Normalizing idlwave-system-routines...")
5069 (setq idlwave-system-routines
5070 (idlwave-sintern-rinfo-list idlwave-system-routines 'sys))
5071 (message "Normalizing idlwave-system-routines...done"))
5072 (when idlwave-system-routines
5073 (setq idlwave-routines (copy-sequence idlwave-system-routines))
5074 (setq idlwave-last-system-routine-info-cons-cell
5075 (nthcdr (1- (length idlwave-routines)) idlwave-routines)))
5076
5077 ;; User catalog
5078 (when (and (stringp idlwave-user-catalog-file)
5079 (file-regular-p idlwave-user-catalog-file))
5080 (condition-case nil
5081 (when (or force (not (aref idlwave-load-rinfo-steps-done 2)))
5082 (load-file idlwave-user-catalog-file))
5083 (error nil))
5084 (when (and
5085 (boundp 'idlwave-library-routines)
5086 idlwave-library-routines)
5087 (setq idlwave-library-routines nil)
5088 (error "Outdated user catalog: %s... recreate"
5089 idlwave-user-catalog-file))
5090 (setq idlwave-true-path-alist nil)
5091 (when (or force (not (aref idlwave-load-rinfo-steps-done 3)))
5092 (message "Normalizing user catalog routines...")
5093 (setq idlwave-user-catalog-routines
5094 (idlwave-sintern-rinfo-list
5095 idlwave-user-catalog-routines 'sys))
5096 (message "Normalizing user catalog routines...done")))
5097
5098 ;; Library catalog
5099 (when (or force (not (aref idlwave-load-rinfo-steps-done 4)))
5100 (idlwave-scan-library-catalogs
5101 "Loading and normalizing library catalogs..."))
5102 (run-hooks 'idlwave-after-load-rinfo-hook))
5103
5104
5105 (defun idlwave-update-buffer-routine-info ()
5106 (let (res)
5107 (cond
5108 ((eq idlwave-scan-all-buffers-for-routine-info t)
5109 ;; Scan all buffers, current buffer last
5110 (message "Scanning all buffers...")
5111 (setq res (idlwave-get-routine-info-from-buffers
5112 (reverse (buffer-list)))))
5113 ((null idlwave-scan-all-buffers-for-routine-info)
5114 ;; Don't scan any buffers
5115 (setq res nil))
5116 (t
5117 ;; Just scan this buffer
5118 (if (eq major-mode 'idlwave-mode)
5119 (progn
5120 (message "Scanning current buffer...")
5121 (setq res (idlwave-get-routine-info-from-buffers
5122 (list (current-buffer))))))))
5123 ;; Put the result into the correct variable
5124 (setq idlwave-buffer-routines
5125 (idlwave-sintern-rinfo-list res 'set))))
5126
5127 (defun idlwave-concatenate-rinfo-lists (&optional quiet run-hook)
5128 "Put the different sources for routine information together."
5129 ;; The sequence here is important because earlier definitions shadow
5130 ;; later ones. We assume that if things in the buffers are newer
5131 ;; then in the shell of the system, they are meant to be different.
5132 (setcdr idlwave-last-system-routine-info-cons-cell
5133 (append idlwave-buffer-routines
5134 idlwave-compiled-routines
5135 idlwave-library-catalog-routines
5136 idlwave-user-catalog-routines))
5137 (setq idlwave-class-alist nil)
5138
5139 ;; Give a message with information about the number of routines we have.
5140 (unless quiet
5141 (message
5142 "Routines Found: buffer(%d) compiled(%d) library(%d) user(%d) system(%d)"
5143 (length idlwave-buffer-routines)
5144 (length idlwave-compiled-routines)
5145 (length idlwave-library-catalog-routines)
5146 (length idlwave-user-catalog-routines)
5147 (length idlwave-system-routines)))
5148 (if run-hook
5149 (run-hooks 'idlwave-update-rinfo-hook)))
5150
5151 (defun idlwave-class-alist ()
5152 "Return the class alist - make it if necessary."
5153 (or idlwave-class-alist
5154 (let (class)
5155 (loop for x in idlwave-routines do
5156 (when (and (setq class (nth 2 x))
5157 (not (assq class idlwave-class-alist)))
5158 (push (list class) idlwave-class-alist)))
5159 idlwave-class-alist)))
5160
5161 ;; Three functions for the hooks
5162 (defun idlwave-save-buffer-update ()
5163 (idlwave-update-current-buffer-info 'save-buffer))
5164 (defun idlwave-kill-buffer-update ()
5165 (idlwave-update-current-buffer-info 'kill-buffer))
5166 (defun idlwave-new-buffer-update ()
5167 (idlwave-update-current-buffer-info 'find-file))
5168
5169 (defun idlwave-update-current-buffer-info (why)
5170 "Update `idlwave-routines' for current buffer.
5171 Can run from `after-save-hook'."
5172 (when (and (eq major-mode 'idlwave-mode)
5173 (or (eq t idlwave-auto-routine-info-updates)
5174 (memq why idlwave-auto-routine-info-updates))
5175 idlwave-scan-all-buffers-for-routine-info
5176 idlwave-routines)
5177 (condition-case nil
5178 (let (routines)
5179 (idlwave-replace-buffer-routine-info
5180 (buffer-file-name)
5181 (if (eq why 'kill-buffer)
5182 nil
5183 (setq routines
5184 (idlwave-sintern-rinfo-list
5185 (idlwave-get-routine-info-from-buffers
5186 (list (current-buffer))) 'set))))
5187 (idlwave-concatenate-rinfo-lists 'quiet)
5188 routines)
5189 (error nil))))
5190
5191 (defun idlwave-replace-buffer-routine-info (file new)
5192 "Cut the part from FILE out of `idlwave-buffer-routines' and add NEW."
5193 (let ((list idlwave-buffer-routines)
5194 found)
5195 (while list
5196 ;; The following test uses eq to make sure it works correctly
5197 ;; when two buffers visit the same file. Then the file names
5198 ;; will be equal, but not eq.
5199 (if (eq (idlwave-routine-source-file (nth 3 (car list))) file)
5200 (progn
5201 (setcar list nil)
5202 (setq found t))
5203 (if found
5204 ;; End of that section reached. Jump.
5205 (setq list nil)))
5206 (setq list (cdr list)))
5207 (setq idlwave-buffer-routines
5208 (append new (delq nil idlwave-buffer-routines)))))
5209
5210 ;;----- Scanning buffers -------------------
5211
5212 (defun idlwave-get-routine-info-from-buffers (buffers)
5213 "Call `idlwave-get-buffer-routine-info' on idlwave-mode buffers in BUFFERS."
5214 (let (buf routine-lists res)
5215 (save-excursion
5216 (while (setq buf (pop buffers))
5217 (set-buffer buf)
5218 (if (and (eq major-mode 'idlwave-mode)
5219 buffer-file-name)
5220 ;; yes, this buffer has the right mode.
5221 (progn (setq res (condition-case nil
5222 (idlwave-get-buffer-routine-info)
5223 (error nil)))
5224 (push res routine-lists)))))
5225 ;; Concatenate the individual lists and return the result
5226 (apply 'nconc routine-lists)))
5227
5228 (defun idlwave-get-buffer-routine-info ()
5229 "Scan the current buffer for routine info. Return (PRO-LIST FUNC-LIST)."
5230 (let* ((case-fold-search t)
5231 routine-list string entry)
5232 (save-excursion
5233 (save-restriction
5234 (widen)
5235 (goto-char (point-min))
5236 (while (re-search-forward
5237 "^[ \t]*\\(pro\\|function\\)[ \t]" nil t)
5238 (setq string (buffer-substring-no-properties
5239 (match-beginning 0)
5240 (progn
5241 (idlwave-end-of-statement)
5242 (point))))
5243 (setq entry (idlwave-parse-definition string))
5244 (push entry routine-list))))
5245 routine-list))
5246
5247 (defvar idlwave-scanning-lib-dir)
5248 (defun idlwave-parse-definition (string)
5249 "Parse a module definition."
5250 (let ((case-fold-search t)
5251 start name args type keywords class)
5252 ;; Remove comments
5253 (while (string-match ";.*" string)
5254 (setq string (replace-match "" t t string)))
5255 ;; Remove the continuation line stuff
5256 (while (string-match "\\([^a-zA-Z0-9$_]\\)\\$[ \t]*\n" string)
5257 (setq string (replace-match "\\1 " t nil string)))
5258 (while (string-match "\n" string)
5259 (setq string (replace-match " " t nil string)))
5260 ;; Match the name and type.
5261 (when (string-match
5262 "\\<\\(pro\\|function\\)\\>\\s-+\\(\\([a-zA-Z0-9$_]+\\)::\\)?\\([a-zA-Z0-9$_]+\\)" string)
5263 (setq start (match-end 0))
5264 (setq type (downcase (match-string 1 string)))
5265 (if (match-beginning 3)
5266 (setq class (match-string 3 string)))
5267 (setq name (match-string 4 string)))
5268 ;; Match normal args and keyword args
5269 (while (string-match
5270 ",\\s-*\\([a-zA-Z][a-zA-Z0-9$_]*\\|\\(_ref\\)?_extra\\)\\s-*\\(=\\)?"
5271 string start)
5272 (setq start (match-end 0))
5273 (if (match-beginning 3)
5274 (push (match-string 1 string) keywords)
5275 (push (match-string 1 string) args)))
5276 ;; Normalize and sort.
5277 (setq args (nreverse args))
5278 (setq keywords (sort keywords (lambda (a b)
5279 (string< (downcase a) (downcase b)))))
5280 ;; Make and return the entry
5281 ;; We don't know which argument are optional, so this information
5282 ;; will not be contained in the calling sequence.
5283 (list name
5284 (if (equal type "pro") 'pro 'fun)
5285 class
5286 (cond ((not (boundp 'idlwave-scanning-lib))
5287 (list 'buffer (buffer-file-name)))
5288 ; ((string= (downcase
5289 ; (file-name-sans-extension
5290 ; (file-name-nondirectory (buffer-file-name))))
5291 ; (downcase name))
5292 ; (list 'lib))
5293 ; (t (cons 'lib (file-name-nondirectory (buffer-file-name))))
5294 (t (list 'user (file-name-nondirectory (buffer-file-name))
5295 idlwave-scanning-lib-dir "UserLib")))
5296 (concat
5297 (if (string= type "function") "Result = " "")
5298 (if class "Obj ->[%s::]" "")
5299 "%s"
5300 (if args
5301 (concat
5302 (if (string= type "function") "(" ", ")
5303 (mapconcat 'identity args ", ")
5304 (if (string= type "function") ")" ""))))
5305 (if keywords
5306 (cons nil (mapcar 'list keywords)) ;No help file
5307 nil))))
5308
5309
5310 ;;----- Scanning the user catalog -------------------
5311
5312 (defun idlwave-sys-dir ()
5313 "Return the syslib directory, or a dummy that never matches."
5314 (cond
5315 ((and idlwave-system-directory
5316 (not (string= idlwave-system-directory "")))
5317 idlwave-system-directory)
5318 ((getenv "IDL_DIR"))
5319 (t "@@@@@@@@")))
5320
5321
5322 (defun idlwave-create-user-catalog-file (&optional arg)
5323 "Scan all files on selected dirs of IDL search path for routine information.
5324
5325 A widget checklist will allow you to choose the directories. Write
5326 the result as a file `idlwave-user-catalog-file'. When this file
5327 exists, it will be automatically loaded to give routine information
5328 about library routines. With ARG, just rescan the same directories
5329 as last time - so no widget will pop up."
5330 (interactive "P")
5331 ;; Make sure the file is loaded if it exists.
5332 (if (and (stringp idlwave-user-catalog-file)
5333 (file-regular-p idlwave-user-catalog-file))
5334 (condition-case nil
5335 (load-file idlwave-user-catalog-file)
5336 (error nil)))
5337 ;; Make sure the file name makes sense
5338 (unless (and (stringp idlwave-user-catalog-file)
5339 (> (length idlwave-user-catalog-file) 0)
5340 (file-accessible-directory-p
5341 (file-name-directory idlwave-user-catalog-file))
5342 (not (string= "" (file-name-nondirectory
5343 idlwave-user-catalog-file))))
5344 (error "`idlwave-user-catalog-file' does not point to a file in an accessible directory"))
5345
5346 (cond
5347 ;; Rescan the known directories
5348 ((and arg idlwave-path-alist
5349 (consp (car idlwave-path-alist)))
5350 (idlwave-scan-user-lib-files idlwave-path-alist))
5351
5352 ;; Expand the directories from library-path and run the widget
5353 (idlwave-library-path
5354 (idlwave-display-user-catalog-widget
5355 (if idlwave-true-path-alist
5356 ;; Propagate any flags on the existing path-alist
5357 (mapcar (lambda (x)
5358 (let ((path-entry (assoc (file-truename x)
5359 idlwave-true-path-alist)))
5360 (if path-entry
5361 (cons x (cdr path-entry))
5362 (list x))))
5363 (idlwave-expand-path idlwave-library-path))
5364 (mapcar 'list (idlwave-expand-path idlwave-library-path)))))
5365
5366 ;; Ask the shell for the path and then run the widget
5367 (t
5368 (message "Asking the shell for IDL path...")
5369 (require 'idlw-shell)
5370 (idlwave-shell-send-command idlwave-shell-path-query
5371 '(idlwave-user-catalog-command-hook nil)
5372 'hide))))
5373
5374
5375 ;; Parse shell path information and select among it.
5376 (defun idlwave-user-catalog-command-hook (&optional arg)
5377 ;; Command hook used by `idlwave-create-user-catalog-file'.
5378 (if arg
5379 ;; Scan immediately
5380 (idlwave-scan-user-lib-files idlwave-path-alist)
5381 ;; Set the path and display the widget
5382 (idlwave-shell-get-path-info 'no-write) ; set to something path-alist
5383 (idlwave-scan-library-catalogs "Locating library catalogs..." 'no-load)
5384 (idlwave-display-user-catalog-widget idlwave-path-alist)))
5385
5386 (defconst idlwave-user-catalog-widget-help-string
5387 "This is the front-end to the creation of the IDLWAVE user catalog.
5388 Please select the directories on IDL's search path from which you
5389 would like to extract routine information, to be stored in the file:
5390
5391 %s
5392
5393 If this is not the correct file, first set variable
5394 `idlwave-user-catalog-file', and call this command again.
5395
5396 N.B. Many libraries include pre-scanned catalog files
5397 \(\".idlwave_catalog\"). These are marked with \"[LIB]\", and need
5398 not be scanned. You can scan your own libraries off-line using the
5399 perl script `idlwave_catalog'.
5400
5401 After selecting the directories, choose [Scan & Save] to scan the library
5402 directories and save the routine info.
5403 \n")
5404
5405 (defvar idlwave-widget)
5406 (defvar widget-keymap)
5407 (defun idlwave-display-user-catalog-widget (dirs-list)
5408 "Create the widget to select IDL search path directories for scanning."
5409 (interactive)
5410 (require 'widget)
5411 (require 'wid-edit)
5412 (unless dirs-list
5413 (error "Don't know IDL's search path"))
5414
5415 (kill-buffer (get-buffer-create "*IDLWAVE Widget*"))
5416 (switch-to-buffer (get-buffer-create "*IDLWAVE Widget*"))
5417 (kill-all-local-variables)
5418 (make-local-variable 'idlwave-widget)
5419 (widget-insert (format idlwave-user-catalog-widget-help-string
5420 idlwave-user-catalog-file))
5421
5422 (widget-create 'push-button
5423 :notify 'idlwave-widget-scan-user-lib-files
5424 "Scan & Save")
5425 (widget-insert " ")
5426 (widget-create 'push-button
5427 :notify 'idlwave-delete-user-catalog-file
5428 "Delete File")
5429 (widget-insert " ")
5430 (widget-create 'push-button
5431 :notify
5432 '(lambda (&rest ignore)
5433 (let ((path-list (widget-get idlwave-widget :path-dirs)))
5434 (mapcar (lambda (x)
5435 (unless (memq 'lib (cdr x))
5436 (idlwave-path-alist-add-flag x 'user)))
5437 path-list)
5438 (idlwave-display-user-catalog-widget path-list)))
5439 "Select All Non-Lib")
5440 (widget-insert " ")
5441 (widget-create 'push-button
5442 :notify
5443 '(lambda (&rest ignore)
5444 (let ((path-list (widget-get idlwave-widget :path-dirs)))
5445 (mapcar (lambda (x)
5446 (idlwave-path-alist-remove-flag x 'user))
5447 path-list)
5448 (idlwave-display-user-catalog-widget path-list)))
5449 "Deselect All")
5450 (widget-insert " ")
5451 (widget-create 'push-button
5452 :notify (lambda (&rest ignore)
5453 (kill-buffer (current-buffer)))
5454 "Quit")
5455 (widget-insert "\n\n")
5456
5457 (widget-insert "Select Directories: \n")
5458
5459 (setq idlwave-widget
5460 (apply 'widget-create
5461 'checklist
5462 :value (delq nil (mapcar (lambda (x)
5463 (if (memq 'user (cdr x))
5464 (car x)))
5465 dirs-list))
5466 :greedy t
5467 :tag "List of directories"
5468 (mapcar (lambda (x)
5469 (list 'item
5470 (if (memq 'lib (cdr x))
5471 (concat "[LIB] " (car x) )
5472 (car x)))) dirs-list)))
5473 (widget-put idlwave-widget :path-dirs dirs-list)
5474 (widget-insert "\n")
5475 (use-local-map widget-keymap)
5476 (widget-setup)
5477 (goto-char (point-min))
5478 (delete-other-windows))
5479
5480 (defun idlwave-delete-user-catalog-file (&rest ignore)
5481 (if (yes-or-no-p
5482 (format "Delete file %s " idlwave-user-catalog-file))
5483 (progn
5484 (delete-file idlwave-user-catalog-file)
5485 (message "%s has been deleted" idlwave-user-catalog-file))))
5486
5487 (defun idlwave-widget-scan-user-lib-files (&rest ignore)
5488 ;; Call `idlwave-scan-user-lib-files' with data taken from the widget.
5489 (let* ((widget idlwave-widget)
5490 (selected-dirs (widget-value widget))
5491 (path-alist (widget-get widget :path-dirs))
5492 (this-path-alist path-alist)
5493 dir-entry)
5494 (while (setq dir-entry (pop this-path-alist))
5495 (if (member
5496 (if (memq 'lib (cdr dir-entry))
5497 (concat "[LIB] " (car dir-entry))
5498 (car dir-entry))
5499 selected-dirs)
5500 (idlwave-path-alist-add-flag dir-entry 'user)
5501 (idlwave-path-alist-remove-flag dir-entry 'user)))
5502 (idlwave-scan-user-lib-files path-alist)))
5503
5504 (defvar font-lock-mode)
5505 (defun idlwave-scan-user-lib-files (path-alist)
5506 ;; Scan the PRO files in PATH-ALIST and store the info in the user catalog
5507 (let* ((idlwave-scanning-lib t)
5508 (idlwave-scanning-lib-dir "")
5509 (idlwave-completion-case nil)
5510 dirs-alist dir files file)
5511 (setq idlwave-user-catalog-routines nil
5512 idlwave-path-alist path-alist ; for library-path instead
5513 idlwave-true-path-alist nil)
5514 (if idlwave-auto-write-paths (idlwave-write-paths))
5515 (save-excursion
5516 (set-buffer (get-buffer-create "*idlwave-scan.pro*"))
5517 (idlwave-mode)
5518 (setq dirs-alist (reverse path-alist))
5519 (while (setq dir (pop dirs-alist))
5520 (when (memq 'user (cdr dir)) ; Has it marked for scan?
5521 (setq dir (car dir))
5522 (setq idlwave-scanning-lib-dir dir)
5523 (when (file-directory-p dir)
5524 (setq files (directory-files dir 'full "\\.[pP][rR][oO]\\'"))
5525 (while (setq file (pop files))
5526 (when (file-regular-p file)
5527 (if (not (file-readable-p file))
5528 (message "Skipping %s (no read permission)" file)
5529 (message "Scanning %s..." file)
5530 (erase-buffer)
5531 (insert-file-contents file 'visit)
5532 (setq idlwave-user-catalog-routines
5533 (append (idlwave-get-routine-info-from-buffers
5534 (list (current-buffer)))
5535 idlwave-user-catalog-routines)))))))))
5536 (message "Creating user catalog file...")
5537 (kill-buffer "*idlwave-scan.pro*")
5538 (kill-buffer (get-buffer-create "*IDLWAVE Widget*"))
5539 (let ((font-lock-maximum-size 0)
5540 (auto-mode-alist nil))
5541 (find-file idlwave-user-catalog-file))
5542 (if (and (boundp 'font-lock-mode)
5543 font-lock-mode)
5544 (font-lock-mode 0))
5545 (erase-buffer)
5546 (insert ";; IDLWAVE user catalog file\n")
5547 (insert (format ";; Created %s\n\n" (current-time-string)))
5548
5549 ;; Define the routine info list
5550 (insert "\n(setq idlwave-user-catalog-routines\n '(")
5551 (let ((standard-output (current-buffer)))
5552 (mapc (lambda (x)
5553 (insert "\n ")
5554 (prin1 x)
5555 (goto-char (point-max)))
5556 idlwave-user-catalog-routines))
5557 (insert (format "))\n\n;;; %s ends here\n"
5558 (file-name-nondirectory idlwave-user-catalog-file)))
5559 (goto-char (point-min))
5560 ;; Save the buffer
5561 (save-buffer 0)
5562 (kill-buffer (current-buffer)))
5563 (message "Creating user catalog file...done")
5564 (message "Info for %d routines saved in %s"
5565 (length idlwave-user-catalog-routines)
5566 idlwave-user-catalog-file)
5567 (sit-for 2)
5568 (idlwave-update-routine-info t))
5569
5570 (defun idlwave-read-paths ()
5571 (if (and (stringp idlwave-path-file)
5572 (file-regular-p idlwave-path-file))
5573 (condition-case nil
5574 (load idlwave-path-file t t t)
5575 (error nil))))
5576
5577 (defun idlwave-write-paths ()
5578 (interactive)
5579 (when (and idlwave-path-alist idlwave-system-directory)
5580 (let ((font-lock-maximum-size 0)
5581 (auto-mode-alist nil))
5582 (find-file idlwave-path-file))
5583 (if (and (boundp 'font-lock-mode)
5584 font-lock-mode)
5585 (font-lock-mode 0))
5586 (erase-buffer)
5587 (insert ";; IDLWAVE paths\n")
5588 (insert (format ";; Created %s\n\n" (current-time-string)))
5589 ;; Define the variable which knows the value of "!DIR"
5590 (insert (format "\n(setq idlwave-system-directory \"%s\")\n"
5591 idlwave-system-directory))
5592
5593 ;; Define the variable which contains a list of all scanned directories
5594 (insert "\n(setq idlwave-path-alist\n '(")
5595 (let ((standard-output (current-buffer)))
5596 (mapc (lambda (x)
5597 (insert "\n ")
5598 (prin1 x)
5599 (goto-char (point-max)))
5600 idlwave-path-alist))
5601 (insert "))\n")
5602 (save-buffer 0)
5603 (kill-buffer (current-buffer))))
5604
5605
5606 (defun idlwave-expand-path (path &optional default-dir)
5607 ;; Expand parts of path starting with '+' recursively into directory list.
5608 ;; Relative recursive path elements are expanded relative to DEFAULT-DIR.
5609 (message "Expanding path...")
5610 (let (path1 dir recursive)
5611 (while (setq dir (pop path))
5612 (if (setq recursive (string= (substring dir 0 1) "+"))
5613 (setq dir (substring dir 1)))
5614 (if (and recursive
5615 (not (file-name-absolute-p dir)))
5616 (setq dir (expand-file-name dir default-dir)))
5617 (if recursive
5618 ;; Expand recursively
5619 (setq path1 (append (idlwave-recursive-directory-list dir) path1))
5620 ;; Keep unchanged
5621 (push dir path1)))
5622 (message "Expanding path...done")
5623 (nreverse path1)))
5624
5625 (defun idlwave-recursive-directory-list (dir)
5626 ;; Return a list of all directories below DIR, including DIR itself
5627 (let ((path (list dir)) path1 file files)
5628 (while (setq dir (pop path))
5629 (when (file-directory-p dir)
5630 (setq files (nreverse (directory-files dir t "[^.]")))
5631 (while (setq file (pop files))
5632 (if (file-directory-p file)
5633 (push (file-name-as-directory file) path)))
5634 (push dir path1)))
5635 path1))
5636
5637
5638 ;;----- Scanning the library catalogs ------------------
5639
5640
5641
5642
5643 (defun idlwave-scan-library-catalogs (&optional message-base no-load)
5644 "Scan for library catalog files (.idlwave_catalog) and ingest.
5645
5646 All directories on `idlwave-path-alist' (or `idlwave-library-path'
5647 instead, if present) are searched. Print MESSAGE-BASE along with the
5648 libraries being loaded, if passed, and skip loading/normalizing if
5649 NO-LOAD is non-nil. The variable `idlwave-use-library-catalogs' can
5650 be set to nil to disable library catalog scanning."
5651 (when idlwave-use-library-catalogs
5652 (let ((dirs
5653 (if idlwave-library-path
5654 (idlwave-expand-path idlwave-library-path)
5655 (mapcar 'car idlwave-path-alist)))
5656 (old-libname "")
5657 dir-entry dir flags catalog all-routines)
5658 (if message-base (message message-base))
5659 (while (setq dir (pop dirs))
5660 (catch 'continue
5661 (when (file-readable-p
5662 (setq catalog (expand-file-name ".idlwave_catalog" dir)))
5663 (unless no-load
5664 (setq idlwave-library-catalog-routines nil)
5665 ;; Load the catalog file
5666 (condition-case nil
5667 (load catalog t t t)
5668 (error (throw 'continue t)))
5669 (when (and
5670 message-base
5671 (not (string= idlwave-library-catalog-libname
5672 old-libname)))
5673 (message "%s" (concat message-base
5674 idlwave-library-catalog-libname))
5675 (setq old-libname idlwave-library-catalog-libname))
5676 (when idlwave-library-catalog-routines
5677 (setq all-routines
5678 (append
5679 (idlwave-sintern-rinfo-list
5680 idlwave-library-catalog-routines 'sys dir)
5681 all-routines))))
5682
5683 ;; Add a 'lib flag if on path-alist
5684 (when (and idlwave-path-alist
5685 (setq dir-entry (assoc dir idlwave-path-alist)))
5686 (idlwave-path-alist-add-flag dir-entry 'lib)))))
5687 (unless no-load (setq idlwave-library-catalog-routines all-routines))
5688 (if message-base (message (concat message-base "done"))))))
5689
5690 ;;----- Communicating with the Shell -------------------
5691
5692 ;; First, here is the idl program which can be used to query IDL for
5693 ;; defined routines.
5694 (defconst idlwave-routine-info.pro
5695 "
5696 ;; START OF IDLWAVE SUPPORT ROUTINES
5697 pro idlwave_print_safe,item,limit
5698 catch,err
5699 if err ne 0 then begin
5700 print,'Could not print item.'
5701 return
5702 endif
5703 if n_elements(item) gt limit then $
5704 print,item[0:limit-1],'<... truncated at ',strtrim(limit,2),' elements>' $
5705 else print,item
5706 end
5707
5708 pro idlwave_print_info_entry,name,func=func,separator=sep
5709 ;; See if it's an object method
5710 if name eq '' then return
5711 func = keyword_set(func)
5712 methsep = strpos(name,'::')
5713 meth = methsep ne -1
5714
5715 ;; Get routine info
5716 pars = routine_info(name,/parameters,functions=func)
5717 source = routine_info(name,/source,functions=func)
5718 nargs = pars.num_args
5719 nkw = pars.num_kw_args
5720 if nargs gt 0 then args = pars.args
5721 if nkw gt 0 then kwargs = pars.kw_args
5722
5723 ;; Trim the class, and make the name
5724 if meth then begin
5725 class = strmid(name,0,methsep)
5726 name = strmid(name,methsep+2,strlen(name)-1)
5727 if nargs gt 0 then begin
5728 ;; remove the self argument
5729 wh = where(args ne 'SELF',nargs)
5730 if nargs gt 0 then args = args[wh]
5731 endif
5732 endif else begin
5733 ;; No class, just a normal routine.
5734 class = \"\"
5735 endelse
5736
5737 ;; Calling sequence
5738 cs = \"\"
5739 if func then cs = 'Result = '
5740 if meth then cs = cs + 'Obj -> [' + '%s' + '::]'
5741 cs = cs + '%s'
5742 if func then cs = cs + '(' else if nargs gt 0 then cs = cs + ', '
5743 if nargs gt 0 then begin
5744 for j=0,nargs-1 do begin
5745 cs = cs + args[j]
5746 if j lt nargs-1 then cs = cs + ', '
5747 endfor
5748 end
5749 if func then cs = cs + ')'
5750 ;; Keyword arguments
5751 kwstring = ''
5752 if nkw gt 0 then begin
5753 for j=0,nkw-1 do begin
5754 kwstring = kwstring + ' ' + kwargs[j]
5755 endfor
5756 endif
5757
5758 ret=(['IDLWAVE-PRO','IDLWAVE-FUN'])[func]
5759
5760 print,ret + ': ' + name + sep + class + sep + source[0].path $
5761 + sep + cs + sep + kwstring
5762 end
5763
5764 pro idlwave_routine_info,file
5765 on_error,1
5766 sep = '<@>'
5767 print,'>>>BEGIN OF IDLWAVE ROUTINE INFO (\"' + sep + '\" IS THE SEPARATOR)'
5768 all = routine_info()
5769 fileQ=n_elements(file) ne 0
5770 if fileQ then file=strtrim(file,2)
5771 for i=0L,n_elements(all)-1L do begin
5772 if fileQ then begin
5773 if (routine_info(all[i],/SOURCE)).path eq file then $
5774 idlwave_print_info_entry,all[i],separator=sep
5775 endif else idlwave_print_info_entry,all[i],separator=sep
5776 endfor
5777 all = routine_info(/functions)
5778 for i=0L,n_elements(all)-1L do begin
5779 if fileQ then begin
5780 if (routine_info(all[i],/FUNCTIONS,/SOURCE)).path eq file then $
5781 idlwave_print_info_entry,all[i],separator=sep,/FUNC
5782 endif else idlwave_print_info_entry,all[i],separator=sep,/FUNC
5783 endfor
5784 print,'>>>END OF IDLWAVE ROUTINE INFO'
5785 end
5786
5787 pro idlwave_get_sysvars
5788 on_error,1
5789 catch,error_status
5790 if error_status ne 0 then begin
5791 print, 'Cannot get info about system variables'
5792 endif else begin
5793 help,/brief,output=s,/system_variables ; ? unsafe use of OUTPUT=
5794 s = strtrim(strjoin(s,' ',/single),2) ; make one line
5795 v = strsplit(s,' +',/regex,/extract) ; get variables
5796 for i=0L,n_elements(v)-1 do begin
5797 t = [''] ; get tag list
5798 a=execute('if n_tags('+v[i]+') gt 0 then t=tag_names('+v[i]+')')
5799 print, 'IDLWAVE-SYSVAR: '+v[i]+' '+strjoin(t,' ',/single)
5800 endfor
5801 endelse
5802 end
5803
5804 pro idlwave_get_class_tags, class
5805 res = execute('tags=tag_names({'+class+'})')
5806 if res then print,'IDLWAVE-CLASS-TAGS: '+class+' '+strjoin(tags,' ',/single)
5807 end
5808 ;; END OF IDLWAVE SUPPORT ROUTINES
5809 "
5810 "The IDL programs to get info from the shell.")
5811
5812 (defvar idlwave-idlwave_routine_info-compiled nil
5813 "Remember if the routine info procedure is already compiled.")
5814
5815 (defvar idlwave-shell-temp-pro-file)
5816 (defvar idlwave-shell-temp-rinfo-save-file)
5817
5818 (defun idlwave-shell-compile-helper-routines (&optional wait)
5819 (unless (and idlwave-idlwave_routine_info-compiled
5820 (file-readable-p (idlwave-shell-temp-file 'rinfo)))
5821 (save-excursion
5822 (set-buffer (idlwave-find-file-noselect
5823 (idlwave-shell-temp-file 'pro)))
5824 (erase-buffer)
5825 (insert idlwave-routine-info.pro)
5826 (save-buffer 0))
5827 (idlwave-shell-send-command
5828 (concat ".run \"" idlwave-shell-temp-pro-file "\"")
5829 nil 'hide wait)
5830 (idlwave-shell-send-command
5831 (format "save,'idlwave_print_safe','idlwave_routine_info','idlwave_print_info_entry','idlwave_get_class_tags','idlwave_get_sysvars',FILE='%s',/ROUTINES"
5832 (idlwave-shell-temp-file 'rinfo))
5833 nil 'hide)
5834 (setq idlwave-idlwave_routine_info-compiled t))
5835
5836 ;; Restore if necessary. Must use execute to hide lame routine_info
5837 ;; errors on undefinded routine
5838 (idlwave-shell-send-command
5839 (format "if execute(\"_v=routine_info('idlwave_routine_info',/SOURCE)\") eq 0 then restore,'%s' else if _v.path eq '' then restore,'%s'"
5840 idlwave-shell-temp-rinfo-save-file
5841 idlwave-shell-temp-rinfo-save-file)
5842 nil 'hide))
5843
5844
5845 (defun idlwave-shell-update-routine-info (&optional quiet run-hooks wait file)
5846 "Query the shell for routine_info of compiled modules and update the lists."
5847 ;; Save and compile the procedure. The compiled procedure is then
5848 ;; saved into an IDL SAVE file, to allow for fast RESTORE. We may
5849 ;; need to test for and possibly RESTORE the procedure each time we
5850 ;; use it, since the user may have killed or redefined it. In
5851 ;; particular, .RESET_SESSION will kill all user procedures. If
5852 ;; FILE is set, only update routine info for routines in that file.
5853
5854 (idlwave-shell-compile-helper-routines wait)
5855 ; execute the routine_info procedure, and analyze the output
5856 (idlwave-shell-send-command
5857 (format "idlwave_routine_info%s" (if file (concat ",'" file "'") ""))
5858 `(progn
5859 (idlwave-shell-routine-info-filter)
5860 (idlwave-concatenate-rinfo-lists ,quiet ,run-hooks))
5861 'hide wait))
5862
5863 ;; ---------------------------------------------------------------------------
5864 ;;
5865 ;; Completion and displaying routine calling sequences
5866
5867 (defvar idlwave-completion-help-info nil)
5868 (defvar idlwave-completion-help-links nil)
5869 (defvar idlwave-current-obj_new-class nil)
5870 (defvar idlwave-complete-special nil)
5871
5872 (defun idlwave-complete (&optional arg module class)
5873 "Complete a function, procedure or keyword name at point.
5874 This function is smart and figures out what can be completed
5875 at this point.
5876 - At the beginning of a statement it completes procedure names.
5877 - In the middle of a statement it completes function names.
5878 - After a `(' or `,' in the argument list of a function or procedure,
5879 it completes a keyword of the relevant function or procedure.
5880 - In the first arg of `OBJ_NEW', it completes a class name.
5881
5882 When several completions are possible, a list will be displayed in
5883 the *Completions* buffer. If this list is too long to fit into the
5884 window, scrolling can be achieved by repeatedly pressing
5885 \\[idlwave-complete].
5886
5887 The function also knows about object methods. When it needs a class
5888 name, the action depends upon `idlwave-query-class', which see. You
5889 can force IDLWAVE to ask you for a class name with a
5890 \\[universal-argument] prefix argument to this command.
5891
5892 See also the variables `idlwave-keyword-completion-adds-equal' and
5893 `idlwave-function-completion-adds-paren'.
5894
5895 The optional ARG can be used to specify the completion type in order
5896 to override IDLWAVE's idea of what should be completed at point.
5897 Possible values are:
5898
5899 0 <=> query for the completion type
5900 1 <=> 'procedure
5901 2 <=> 'procedure-keyword
5902 3 <=> 'function
5903 4 <=> 'function-keyword
5904 5 <=> 'procedure-method
5905 6 <=> 'procedure-method-keyword
5906 7 <=> 'function-method
5907 8 <=> 'function-method-keyword
5908 9 <=> 'class
5909
5910 As a special case, the universal argument C-u forces completion of
5911 function names in places where the default would be a keyword.
5912
5913 Two prefix argument, C-u C-u, prompts for a regexp by which to limit
5914 completion.
5915
5916 For Lisp programmers only:
5917 When we force a keyword, optional argument MODULE can contain the module name.
5918 When we force a method or a method keyword, CLASS can specify the class."
5919 (interactive "P")
5920 (idlwave-routines)
5921 (let* ((where-list
5922 (if (and arg
5923 (or (and (integerp arg) (not (equal arg '(16))))
5924 (symbolp arg)))
5925 (idlwave-make-force-complete-where-list arg module class)
5926 (idlwave-where)))
5927 (what (nth 2 where-list))
5928 (idlwave-force-class-query (equal arg '(4)))
5929 (completion-regexp-list
5930 (if (equal arg '(16))
5931 (list (read-string (concat "Completion Regexp: "))))))
5932
5933 (if (and module (string-match "::" module))
5934 (setq class (substring module 0 (match-beginning 0))
5935 module (substring module (match-end 0))))
5936
5937 (cond
5938
5939 ((and (null arg)
5940 (eq (car-safe last-command) 'idlwave-display-completion-list)
5941 (get-buffer-window "*Completions*"))
5942 (setq this-command last-command)
5943 (idlwave-scroll-completions))
5944
5945 ;; Complete a filename in quotes
5946 ((and (idlwave-in-quote)
5947 (not (eq what 'class)))
5948 (idlwave-complete-filename))
5949
5950 ;; Check for any special completion functions
5951 ((and idlwave-complete-special
5952 (idlwave-call-special idlwave-complete-special)))
5953
5954 ((null what)
5955 (error "Nothing to complete here"))
5956
5957 ;; Complete a class
5958 ((eq what 'class)
5959 (setq idlwave-completion-help-info '(class))
5960 (idlwave-complete-class))
5961
5962 ((eq what 'procedure)
5963 ;; Complete a procedure name
5964 (let* ((cw-list (nth 3 where-list))
5965 (class-selector (idlwave-determine-class cw-list 'pro))
5966 (super-classes (unless (idlwave-explicit-class-listed cw-list)
5967 (idlwave-all-class-inherits class-selector)))
5968 (isa (concat "procedure" (if class-selector "-method" "")))
5969 (type-selector 'pro))
5970 (setq idlwave-completion-help-info
5971 (list 'routine nil type-selector class-selector nil super-classes))
5972 (idlwave-complete-in-buffer
5973 'procedure (if class-selector 'method 'routine)
5974 (idlwave-routines) 'idlwave-selector
5975 (format "Select a %s name%s"
5976 isa
5977 (if class-selector
5978 (format " (class is %s)"
5979 (if (eq class-selector t)
5980 "unknown" class-selector))
5981 ""))
5982 isa
5983 'idlwave-attach-method-classes 'idlwave-add-file-link-selector)))
5984
5985 ((eq what 'function)
5986 ;; Complete a function name
5987 (let* ((cw-list (nth 3 where-list))
5988 (class-selector (idlwave-determine-class cw-list 'fun))
5989 (super-classes (unless (idlwave-explicit-class-listed cw-list)
5990 (idlwave-all-class-inherits class-selector)))
5991 (isa (concat "function" (if class-selector "-method" "")))
5992 (type-selector 'fun))
5993 (setq idlwave-completion-help-info
5994 (list 'routine nil type-selector class-selector nil super-classes))
5995 (idlwave-complete-in-buffer
5996 'function (if class-selector 'method 'routine)
5997 (idlwave-routines) 'idlwave-selector
5998 (format "Select a %s name%s"
5999 isa
6000 (if class-selector
6001 (format " (class is %s)"
6002 (if (eq class-selector t)
6003 "unknown" class-selector))
6004 ""))
6005 isa
6006 'idlwave-attach-method-classes 'idlwave-add-file-link-selector)))
6007
6008 ((and (memq what '(procedure-keyword function-keyword)) ; Special Case
6009 (equal arg '(4)))
6010 (idlwave-complete 3))
6011
6012 ((eq what 'procedure-keyword)
6013 ;; Complete a procedure keyword
6014 (let* ((where (nth 3 where-list))
6015 (name (car where))
6016 (method-selector name)
6017 (type-selector 'pro)
6018 (class (idlwave-determine-class where 'pro))
6019 (class-selector class)
6020 (super-classes (idlwave-all-class-inherits class-selector))
6021 (isa (format "procedure%s-keyword" (if class "-method" "")))
6022 (entry (idlwave-best-rinfo-assq
6023 name 'pro class (idlwave-routines)))
6024 (system (if entry (eq (car (nth 3 entry)) 'system)))
6025 (list (idlwave-entry-keywords entry 'do-link)))
6026 (unless (or entry (eq class t))
6027 (error "Nothing known about procedure %s"
6028 (idlwave-make-full-name class name)))
6029 (setq list (idlwave-fix-keywords name 'pro class list
6030 super-classes system))
6031 (unless list (error "No keywords available for procedure %s"
6032 (idlwave-make-full-name class name)))
6033 (setq idlwave-completion-help-info
6034 (list 'keyword name type-selector class-selector entry super-classes))
6035 (idlwave-complete-in-buffer
6036 'keyword 'keyword list nil
6037 (format "Select keyword for procedure %s%s"
6038 (idlwave-make-full-name class name)
6039 (if (or (member '("_EXTRA") list)
6040 (member '("_REF_EXTRA") list))
6041 " (note _EXTRA)" ""))
6042 isa
6043 'idlwave-attach-keyword-classes)))
6044
6045 ((eq what 'function-keyword)
6046 ;; Complete a function keyword
6047 (let* ((where (nth 3 where-list))
6048 (name (car where))
6049 (method-selector name)
6050 (type-selector 'fun)
6051 (class (idlwave-determine-class where 'fun))
6052 (class-selector class)
6053 (super-classes (idlwave-all-class-inherits class-selector))
6054 (isa (format "function%s-keyword" (if class "-method" "")))
6055 (entry (idlwave-best-rinfo-assq
6056 name 'fun class (idlwave-routines)))
6057 (system (if entry (eq (car (nth 3 entry)) 'system)))
6058 (list (idlwave-entry-keywords entry 'do-link))
6059 msg-name)
6060 (unless (or entry (eq class t))
6061 (error "Nothing known about function %s"
6062 (idlwave-make-full-name class name)))
6063 (setq list (idlwave-fix-keywords name 'fun class list
6064 super-classes system))
6065 ;; OBJ_NEW: Messages mention the proper Init method
6066 (setq msg-name (if (and (null class)
6067 (string= (upcase name) "OBJ_NEW"))
6068 (concat idlwave-current-obj_new-class
6069 "::Init (via OBJ_NEW)")
6070 (idlwave-make-full-name class name)))
6071 (unless list (error "No keywords available for function %s"
6072 msg-name))
6073 (setq idlwave-completion-help-info
6074 (list 'keyword name type-selector class-selector nil super-classes))
6075 (idlwave-complete-in-buffer
6076 'keyword 'keyword list nil
6077 (format "Select keyword for function %s%s" msg-name
6078 (if (or (member '("_EXTRA") list)
6079 (member '("_REF_EXTRA") list))
6080 " (note _EXTRA)" ""))
6081 isa
6082 'idlwave-attach-keyword-classes)))
6083
6084 (t (error "This should not happen (idlwave-complete)")))))
6085
6086 (defvar idlwave-complete-special nil
6087 "List of special completion functions.
6088 These functions are called for each completion. Each function must
6089 check if its own special completion context is present. If yes, it
6090 should use `idlwave-complete-in-buffer' to do some completion and
6091 return t. If such a function returns t, *no further* attempts to
6092 complete other contexts will be done. If the function returns nil,
6093 other completions will be tried.")
6094
6095 (defun idlwave-call-special (functions &rest args)
6096 (let ((funcs functions)
6097 fun ret)
6098 (catch 'exit
6099 (while (setq fun (pop funcs))
6100 (if (setq ret (apply fun args))
6101 (throw 'exit ret)))
6102 nil)))
6103
6104 (defun idlwave-make-force-complete-where-list (what &optional module class)
6105 ;; Return an artificial WHERE specification to force the completion
6106 ;; routine to complete a specific item independent of context.
6107 ;; WHAT is the prefix arg of `idlwave-complete', see there for details.
6108 ;; MODULE and CLASS can be used to specify the routine name and class.
6109 ;; The class name will also be found in MODULE if that is like "class::mod".
6110 (let* ((what-list '(("procedure") ("procedure-keyword")
6111 ("function") ("function-keyword")
6112 ("procedure-method") ("procedure-method-keyword")
6113 ("function-method") ("function-method-keyword")
6114 ("class")))
6115 (module (idlwave-sintern-routine-or-method module class))
6116 (class (idlwave-sintern-class class))
6117 (what (cond
6118 ((equal what 0)
6119 (setq what
6120 (intern (completing-read
6121 "Complete what? " what-list nil t))))
6122 ((integerp what)
6123 (setq what (intern (car (nth (1- what) what-list)))))
6124 ((and what
6125 (symbolp what)
6126 (assoc (symbol-name what) what-list))
6127 what)
6128 (t (error "Invalid WHAT"))))
6129 (nil-list '(nil nil nil nil))
6130 (class-list (list nil nil (or class t) nil)))
6131
6132 (cond
6133
6134 ((eq what 'procedure)
6135 (list nil-list nil-list 'procedure nil-list nil))
6136
6137 ((eq what 'procedure-keyword)
6138 (let* ((class-selector nil)
6139 (super-classes nil)
6140 (type-selector 'pro)
6141 (pro (or module
6142 (idlwave-completing-read
6143 "Procedure: " (idlwave-routines) 'idlwave-selector))))
6144 (setq pro (idlwave-sintern-routine pro))
6145 (list nil-list nil-list 'procedure-keyword
6146 (list pro nil nil nil) nil)))
6147
6148 ((eq what 'function)
6149 (list nil-list nil-list 'function nil-list nil))
6150
6151 ((eq what 'function-keyword)
6152 (let* ((class-selector nil)
6153 (super-classes nil)
6154 (type-selector 'fun)
6155 (func (or module
6156 (idlwave-completing-read
6157 "Function: " (idlwave-routines) 'idlwave-selector))))
6158 (setq func (idlwave-sintern-routine func))
6159 (list nil-list nil-list 'function-keyword
6160 (list func nil nil nil) nil)))
6161
6162 ((eq what 'procedure-method)
6163 (list nil-list nil-list 'procedure class-list nil))
6164
6165 ((eq what 'procedure-method-keyword)
6166 (let* ((class (idlwave-determine-class class-list 'pro))
6167 (class-selector class)
6168 (super-classes (idlwave-all-class-inherits class-selector))
6169 (type-selector 'pro)
6170 (pro (or module
6171 (idlwave-completing-read
6172 (format "Procedure in %s class: " class-selector)
6173 (idlwave-routines) 'idlwave-selector))))
6174 (setq pro (idlwave-sintern-method pro))
6175 (list nil-list nil-list 'procedure-keyword
6176 (list pro nil class nil) nil)))
6177
6178 ((eq what 'function-method)
6179 (list nil-list nil-list 'function class-list nil))
6180
6181 ((eq what 'function-method-keyword)
6182 (let* ((class (idlwave-determine-class class-list 'fun))
6183 (class-selector class)
6184 (super-classes (idlwave-all-class-inherits class-selector))
6185 (type-selector 'fun)
6186 (func (or module
6187 (idlwave-completing-read
6188 (format "Function in %s class: " class-selector)
6189 (idlwave-routines) 'idlwave-selector))))
6190 (setq func (idlwave-sintern-method func))
6191 (list nil-list nil-list 'function-keyword
6192 (list func nil class nil) nil)))
6193
6194 ((eq what 'class)
6195 (list nil-list nil-list 'class nil-list nil))
6196
6197 (t (error "Invalid value for WHAT")))))
6198
6199 (defun idlwave-completing-read (&rest args)
6200 ;; Completing read, case insensitive
6201 (let ((old-value (default-value 'completion-ignore-case)))
6202 (unwind-protect
6203 (progn
6204 (setq-default completion-ignore-case t)
6205 (apply 'completing-read args))
6206 (setq-default completion-ignore-case old-value))))
6207
6208 (defvar idlwave-shell-default-directory)
6209 (defun idlwave-complete-filename ()
6210 "Use the comint stuff to complete a file name."
6211 (require 'comint)
6212 (let* ((comint-file-name-chars "~/A-Za-z0-9+@:_.$#%={}\\-")
6213 (comint-completion-addsuffix nil)
6214 (default-directory
6215 (if (and (boundp 'idlwave-shell-default-directory)
6216 (stringp idlwave-shell-default-directory)
6217 (file-directory-p idlwave-shell-default-directory))
6218 idlwave-shell-default-directory
6219 default-directory)))
6220 (comint-dynamic-complete-filename)))
6221
6222 (defun idlwave-make-full-name (class name)
6223 ;; Make a fully qualified module name including the class name
6224 (concat (if class (format "%s::" class) "") name))
6225
6226 (defun idlwave-rinfo-assoc (name type class list)
6227 "Like `idlwave-rinfo-assq', but sintern strings first."
6228 (idlwave-rinfo-assq
6229 (idlwave-sintern-routine-or-method name class)
6230 type (idlwave-sintern-class class) list))
6231
6232 (defun idlwave-rinfo-assq (name type class list)
6233 ;; Works like assq, but also checks type and class
6234 (catch 'exit
6235 (let (match)
6236 (while (setq match (assq name list))
6237 (and (or (eq type t)
6238 (eq (nth 1 match) type))
6239 (eq (nth 2 match) class)
6240 (throw 'exit match))
6241 (setq list (cdr (memq match list)))))))
6242
6243 (defun idlwave-rinfo-assq-any-class (name type class list)
6244 ;; Return the first matching method on the inheritance list
6245 (let* ((classes (cons class (idlwave-all-class-inherits class)))
6246 class rtn)
6247 (while classes
6248 (if (setq rtn (idlwave-rinfo-assq name type (pop classes) list))
6249 (setq classes nil)))
6250 rtn))
6251
6252 (defun idlwave-best-rinfo-assq (name type class list &optional with-file
6253 keep-system)
6254 "Like `idlwave-rinfo-assq', but get all twins and sort, then return first.
6255 If WITH-FILE is passed, find the best rinfo entry with a file
6256 included. If KEEP-SYSTEM is set, don't prune system for compiled
6257 syslib files."
6258 (let ((twins (idlwave-routine-twins
6259 (idlwave-rinfo-assq-any-class name type class list)
6260 list))
6261 syslibp)
6262 (when (> (length twins) 1)
6263 (setq twins (sort twins 'idlwave-routine-entry-compare-twins))
6264 (if (and (null keep-system)
6265 (eq 'system (car (nth 3 (car twins))))
6266 (setq syslibp (idlwave-any-syslib (cdr twins)))
6267 (not (equal 1 syslibp)))
6268 ;; Its a compiled syslib, so we need to remove the system entry
6269 (setq twins (cdr twins)))
6270 (if with-file
6271 (setq twins (delq nil
6272 (mapcar (lambda (x)
6273 (if (nth 1 (nth 3 x)) x))
6274 twins)))))
6275 (car twins)))
6276
6277 (defun idlwave-best-rinfo-assoc (name type class list &optional with-file
6278 keep-system)
6279 "Like `idlwave-best-rinfo-assq', but sintern strings first."
6280 (idlwave-best-rinfo-assq
6281 (idlwave-sintern-routine-or-method name class)
6282 type (idlwave-sintern-class class) list with-file keep-system))
6283
6284 (defun idlwave-any-syslib (entries)
6285 "Does the entry list ENTRIES contain a syslib entry?
6286 If yes, return the index (>=1)."
6287 (let (file (cnt 0))
6288 (catch 'exit
6289 (while entries
6290 (incf cnt)
6291 (setq file (idlwave-routine-source-file (nth 3 (car entries))))
6292 (if (and file (idlwave-syslib-p file))
6293 (throw 'exit cnt)
6294 (setq entries (cdr entries))))
6295 nil)))
6296
6297 (defun idlwave-all-assq (key list)
6298 "Return a list of all associations of Key in LIST."
6299 (let (rtn elt)
6300 (while (setq elt (assq key list))
6301 (push elt rtn)
6302 (setq list (cdr (memq elt list))))
6303 (nreverse rtn)))
6304
6305 (defun idlwave-all-method-classes (method &optional type)
6306 "Return all classes which have a method METHOD.
6307 TYPE is 'fun or 'pro.
6308 When TYPE is not specified, both procedures and functions will be considered."
6309 (if (null method)
6310 (mapcar 'car (idlwave-class-alist))
6311 (let (rtn)
6312 (mapc (lambda (x)
6313 (and (nth 2 x)
6314 (or (not type)
6315 (eq type (nth 1 x)))
6316 (push (nth 2 x) rtn)))
6317 (idlwave-all-assq method (idlwave-routines)))
6318 (idlwave-uniquify rtn))))
6319
6320 (defun idlwave-all-method-keyword-classes (method keyword &optional type)
6321 "Return all classes which have a method METHOD with keyword KEYWORD.
6322 TYPE is 'fun or 'pro.
6323 When TYPE is not specified, both procedures and functions will be considered."
6324 (if (or (null method)
6325 (null keyword))
6326 nil
6327 (let (rtn)
6328 (mapc (lambda (x)
6329 (and (nth 2 x) ; non-nil class
6330 (or (not type) ; correct or unspecified type
6331 (eq type (nth 1 x)))
6332 (assoc keyword (idlwave-entry-keywords x))
6333 (push (nth 2 x) rtn)))
6334 (idlwave-all-assq method (idlwave-routines)))
6335 (idlwave-uniquify rtn))))
6336
6337 (defun idlwave-members-only (list club)
6338 "Return list of all elements in LIST which are also in CLUB."
6339 (let (rtn)
6340 (while list
6341 (if (member (car list) club)
6342 (setq rtn (cons (car list) rtn)))
6343 (setq list (cdr list)))
6344 (nreverse rtn)))
6345
6346 (defun idlwave-nonmembers-only (list club)
6347 "Return list of all elements in LIST which are not in CLUB."
6348 (let (rtn)
6349 (while list
6350 (if (member (car list) club)
6351 nil
6352 (setq rtn (cons (car list) rtn)))
6353 (setq list (cdr list)))
6354 (nreverse rtn)))
6355
6356 (defun idlwave-explicit-class-listed (info)
6357 "Return whether or not the class is listed explicitly, ala a->b::c.
6358 INFO is as returned by `idlwave-what-function' or `-procedure'."
6359 (let ((apos (nth 3 info)))
6360 (if apos
6361 (save-excursion (goto-char apos)
6362 (looking-at "->[a-zA-Z][a-zA-Z0-9$_]*::")))))
6363
6364 (defvar idlwave-determine-class-special nil
6365 "List of special functions for determining class.
6366 Must accept two arguments: `apos' and `info'.")
6367
6368 (defun idlwave-determine-class (info type)
6369 ;; Determine the class of a routine call.
6370 ;; INFO is the `cw-list' structure as returned by idlwave-where.
6371 ;; The second element in this structure is the class. When nil, we
6372 ;; return nil. When t, try to get the class from text properties at
6373 ;; the arrow. When the object is "self", we use the class of the
6374 ;; current routine. otherwise prompt the user for a class name.
6375 ;; Also stores the selected class as a text property at the arrow.
6376 ;; TYPE is 'fun or 'pro.
6377 (let* ((class (nth 2 info))
6378 (apos (nth 3 info))
6379 (nassoc (assoc (if (stringp (car info))
6380 (upcase (car info))
6381 (car info))
6382 idlwave-query-class))
6383 (dassoc (assq (if (car info) 'keyword-default 'method-default)
6384 idlwave-query-class))
6385 (query (cond (nassoc (cdr nassoc))
6386 (dassoc (cdr dassoc))
6387 (t t)))
6388 (arrow (and apos (string= (buffer-substring apos (+ 2 apos)) "->")))
6389 (is-self
6390 (and arrow
6391 (save-excursion (goto-char apos)
6392 (forward-word -1)
6393 (let ((case-fold-search t))
6394 (looking-at "self\\>")))))
6395 (force-query idlwave-force-class-query)
6396 store special-class class-alist)
6397 (cond
6398 ((null class) nil)
6399 ((eq t class)
6400 ;; There is an object which would like to know its class
6401 (if (and arrow (get-text-property apos 'idlwave-class)
6402 idlwave-store-inquired-class
6403 (not force-query))
6404 (setq class (get-text-property apos 'idlwave-class)
6405 class (idlwave-sintern-class class)))
6406 (if (and (eq t class) is-self)
6407 (setq class (or (nth 2 (idlwave-current-routine)) class)))
6408
6409 ;; Before prompting, try any special class determination routines
6410 (when (and (eq t class)
6411 idlwave-determine-class-special
6412 (not force-query))
6413 (setq special-class
6414 (idlwave-call-special idlwave-determine-class-special apos))
6415 (if special-class
6416 (setq class (idlwave-sintern-class special-class)
6417 store idlwave-store-inquired-class)))
6418
6419 ;; Prompt for a class, if we need to
6420 (when (and (eq class t)
6421 (or force-query query))
6422 (setq class-alist
6423 (mapcar 'list (idlwave-all-method-classes (car info) type)))
6424 (setq class
6425 (idlwave-sintern-class
6426 (cond
6427 ((and (= (length class-alist) 0) (not force-query))
6428 (error "No classes available with method %s" (car info)))
6429 ((and (= (length class-alist) 1) (not force-query))
6430 (car (car class-alist)))
6431 (t
6432 (setq store idlwave-store-inquired-class)
6433 (idlwave-completing-read
6434 (format "Class%s: " (if (stringp (car info))
6435 (format " for %s method %s"
6436 type (car info))
6437 ""))
6438 class-alist nil nil nil 'idlwave-class-history))))))
6439
6440 ;; Store it, if requested
6441 (when (and class (not (eq t class)))
6442 ;; We have a real class here
6443 (when (and store arrow)
6444 (condition-case ()
6445 (add-text-properties
6446 apos (+ apos 2)
6447 `(idlwave-class ,class face ,idlwave-class-arrow-face
6448 rear-nonsticky t))
6449 (error nil)))
6450 (setf (nth 2 info) class))
6451 ;; Return the class
6452 class)
6453 ;; Default as fallback
6454 (t class))))
6455
6456 (defvar type-selector)
6457 (defvar class-selector)
6458 (defvar method-selector)
6459 (defvar super-classes)
6460 (defun idlwave-selector (a)
6461 (and (eq (nth 1 a) type-selector)
6462 (or (and (nth 2 a) (eq class-selector t))
6463 (eq (nth 2 a) class-selector)
6464 (memq (nth 2 a) super-classes))))
6465
6466 (defun idlwave-add-file-link-selector (a)
6467 ;; Record a file link, if any, for the tested names during selection.
6468 (let ((sel (idlwave-selector a)) file)
6469 (if (and sel (setq file (idlwave-entry-has-help a)))
6470 (push (cons (car a) file) idlwave-completion-help-links))
6471 sel))
6472
6473
6474 (defun idlwave-where ()
6475 "Find out where we are.
6476 The return value is a list with the following stuff:
6477 \(PRO-LIST FUNC-LIST COMPLETE-WHAT CW-LIST LAST-CHAR)
6478
6479 PRO-LIST (PRO POINT CLASS ARROW)
6480 FUNC-LIST (FUNC POINT CLASS ARROW)
6481 COMPLETE-WHAT a symbol indicating what kind of completion makes sense here
6482 CW-LIST (PRO-OR-FUNC POINT CLASS ARROW) Like PRO-LIST, for what can
6483 be completed here.
6484 LAST-CHAR last relevant character before point (non-white non-comment,
6485 not part of current identifier or leading slash).
6486
6487 In the lists, we have these meanings:
6488 PRO: Procedure name
6489 FUNC: Function name
6490 POINT: Where is this
6491 CLASS: What class has the routine (nil=no, t=is method, but class unknown)
6492 ARROW: Location of the arrow"
6493 (idlwave-routines)
6494 (let* (;(bos (save-excursion (idlwave-beginning-of-statement) (point)))
6495 (bos (save-excursion (idlwave-start-of-substatement 'pre) (point)))
6496 (func-entry (idlwave-what-function bos))
6497 (func (car func-entry))
6498 (func-class (nth 1 func-entry))
6499 (func-arrow (nth 2 func-entry))
6500 (func-point (or (nth 3 func-entry) 0))
6501 (func-level (or (nth 4 func-entry) 0))
6502 (pro-entry (idlwave-what-procedure bos))
6503 (pro (car pro-entry))
6504 (pro-class (nth 1 pro-entry))
6505 (pro-arrow (nth 2 pro-entry))
6506 (pro-point (or (nth 3 pro-entry) 0))
6507 (last-char (idlwave-last-valid-char))
6508 (case-fold-search t)
6509 (match-string (buffer-substring bos (point)))
6510 cw cw-mod cw-arrow cw-class cw-point)
6511 (if (< func-point pro-point) (setq func nil))
6512 (cond
6513 ((string-match "\\`[ \t]*\\(pro\\|function\\)[ \t]+[a-zA-Z0-9_]*\\'"
6514 match-string)
6515 (setq cw 'class))
6516 ((string-match
6517 "\\`[ \t]*\\([a-zA-Z][a-zA-Z0-9$_]*\\)?\\'"
6518 (if (> pro-point 0)
6519 (buffer-substring pro-point (point))
6520 match-string))
6521 (setq cw 'procedure cw-class pro-class cw-point pro-point
6522 cw-arrow pro-arrow))
6523 ((string-match "\\`[ \t]*\\(pro\\|function\\)\\>"
6524 match-string)
6525 nil)
6526 ((string-match "OBJ_NEW([ \t]*['\"]\\([a-zA-Z0-9$_]*\\)?\\'"
6527 match-string)
6528 (setq cw 'class))
6529 ((string-match "\\<inherits\\s-+\\([a-zA-Z0-9$_]*\\)?\\'"
6530 match-string)
6531 (setq cw 'class))
6532 ((and func
6533 (> func-point pro-point)
6534 (= func-level 1)
6535 (memq last-char '(?\( ?,)))
6536 (setq cw 'function-keyword cw-mod func cw-point func-point
6537 cw-class func-class cw-arrow func-arrow))
6538 ((and pro (eq last-char ?,))
6539 (setq cw 'procedure-keyword cw-mod pro cw-point pro-point
6540 cw-class pro-class cw-arrow pro-arrow))
6541 ; ((member last-char '(?\' ?\) ?\] ?!))
6542 ; ;; after these chars, a function makes no sense
6543 ; ;; FIXME: I am sure there can be more in this list
6544 ; ;; FIXME: Do we want to do this at all?
6545 ; nil)
6546 ;; Everywhere else we try a function.
6547 (t
6548 (setq cw 'function)
6549 (save-excursion
6550 (if (re-search-backward "->[ \t]*\\(\\$[ \t]*\\(;.*\\)?\n\\s-*\\)?\\(\\([$a-zA-Z0-9_]+\\)::\\)?[$a-zA-Z0-9_]*\\=" bos t)
6551 (setq cw-arrow (copy-marker (match-beginning 0))
6552 cw-class (if (match-end 4)
6553 (idlwave-sintern-class (match-string 4))
6554 t))))))
6555 (list (list pro pro-point pro-class pro-arrow)
6556 (list func func-point func-class func-arrow)
6557 cw
6558 (list cw-mod cw-point cw-class cw-arrow)
6559 last-char)))
6560
6561 (defun idlwave-this-word (&optional class)
6562 ;; Grab the word around point. CLASS is for the `skip-chars=...' functions
6563 (setq class (or class "a-zA-Z0-9$_."))
6564 (save-excursion
6565 (buffer-substring
6566 (progn (skip-chars-backward class) (point))
6567 (progn (skip-chars-forward class) (point)))))
6568
6569 (defun idlwave-what-function (&optional bound)
6570 ;; Find out if point is within the argument list of a function.
6571 ;; The return value is ("function-name" class arrow-start (point) level).
6572 ;; Level is 1 on the top level parentheses, higher further down.
6573
6574 ;; If the optional BOUND is an integer, bound backwards directed
6575 ;; searches to this point.
6576
6577 (catch 'exit
6578 (let (pos
6579 func-point
6580 (cnt 0)
6581 func arrow-start class)
6582 (idlwave-with-special-syntax
6583 (save-restriction
6584 (save-excursion
6585 (narrow-to-region (max 1 (or bound 0)) (point-max))
6586 ;; move back out of the current parenthesis
6587 (while (condition-case nil
6588 (progn (up-list -1) t)
6589 (error nil))
6590 (setq pos (point))
6591 (incf cnt)
6592 (when (and (= (following-char) ?\()
6593 (re-search-backward
6594 "\\(::\\|\\<\\)\\([a-zA-Z][a-zA-Z0-9$_]*\\)[ \t]*\\="
6595 bound t))
6596 (setq func (match-string 2)
6597 func-point (goto-char (match-beginning 2))
6598 pos func-point)
6599 (if (re-search-backward
6600 "->[ \t]*\\(\\([a-zA-Z][a-zA-Z0-9$_]*\\)::\\)?\\=" bound t)
6601 (setq arrow-start (copy-marker (match-beginning 0))
6602 class (or (match-string 2) t)))
6603 (throw
6604 'exit
6605 (list
6606 (idlwave-sintern-routine-or-method func class)
6607 (idlwave-sintern-class class)
6608 arrow-start func-point cnt)))
6609 (goto-char pos))
6610 (throw 'exit nil)))))))
6611
6612 (defun idlwave-what-procedure (&optional bound)
6613 ;; Find out if point is within the argument list of a procedure.
6614 ;; The return value is ("procedure-name" class arrow-pos (point)).
6615
6616 ;; If the optional BOUND is an integer, bound backwards directed
6617 ;; searches to this point.
6618 (let ((pos (point)) pro-point
6619 pro class arrow-start string)
6620 (save-excursion
6621 ;;(idlwave-beginning-of-statement)
6622 (idlwave-start-of-substatement 'pre)
6623 (setq string (buffer-substring (point) pos))
6624 (if (string-match
6625 "\\`[ \t]*\\([a-zA-Z][a-zA-Z0-9$_]*\\)[ \t]*\\(,\\|\\'\\)" string)
6626 (setq pro (match-string 1 string)
6627 pro-point (+ (point) (match-beginning 1)))
6628 (if (and (idlwave-skip-object)
6629 (setq string (buffer-substring (point) pos))
6630 (string-match
6631 "\\`[ \t]*\\(->\\)[ \t]*\\(\\([a-zA-Z][a-zA-Z0-9$_]*\\)::\\)?\\([a-zA-Z][a-zA-Z0-9$_]*\\)?[ \t]*\\(,\\|\\(\\$\\s *\\(;.*\\)?\\)?$\\)"
6632 string))
6633 (setq pro (if (match-beginning 4)
6634 (match-string 4 string))
6635 pro-point (if (match-beginning 4)
6636 (+ (point) (match-beginning 4))
6637 pos)
6638 arrow-start (copy-marker (+ (point) (match-beginning 1)))
6639 class (or (match-string 3 string) t)))))
6640 (list (idlwave-sintern-routine-or-method pro class)
6641 (idlwave-sintern-class class)
6642 arrow-start
6643 pro-point)))
6644
6645 (defun idlwave-skip-object ()
6646 ;; If there is an object at point, move over it and return t.
6647 (let ((pos (point)))
6648 (if (catch 'exit
6649 (save-excursion
6650 (skip-chars-forward " ") ; white space
6651 (skip-chars-forward "*") ; de-reference
6652 (cond
6653 ((looking-at idlwave-identifier)
6654 (goto-char (match-end 0)))
6655 ((eq (following-char) ?\()
6656 nil)
6657 (t (throw 'exit nil)))
6658 (catch 'endwhile
6659 (while t
6660 (cond ((eq (following-char) ?.)
6661 (forward-char 1)
6662 (if (not (looking-at idlwave-identifier))
6663 (throw 'exit nil))
6664 (goto-char (match-end 0)))
6665 ((memq (following-char) '(?\( ?\[))
6666 (condition-case nil
6667 (forward-list 1)
6668 (error (throw 'exit nil))))
6669 (t (throw 'endwhile t)))))
6670 (if (looking-at "[ \t]*->")
6671 (throw 'exit (setq pos (match-beginning 0)))
6672 (throw 'exit nil))))
6673 (goto-char pos)
6674 nil)))
6675
6676 (defun idlwave-last-valid-char ()
6677 "Return the last character before point which is not white or a comment
6678 and also not part of the current identifier. Since we do this in
6679 order to identify places where keywords are, we consider the initial
6680 `/' of a keyword as part of the identifier.
6681 This function is not general, can only be used for completion stuff."
6682 (catch 'exit
6683 (save-excursion
6684 ;; skip the current identifier
6685 (skip-chars-backward "a-zA-Z0-9_$")
6686 ;; also skip a leading slash which might be belong to the keyword
6687 (if (eq (preceding-char) ?/)
6688 (backward-char 1))
6689 ;; FIXME: does not check if this is a valid identifier
6690 (while t
6691 (skip-chars-backward " \t")
6692 (cond
6693 ((memq (preceding-char) '(?\; ?\$)) (throw 'exit nil))
6694 ((eq (preceding-char) ?\n)
6695 (beginning-of-line 0)
6696 (if (looking-at "\\([^\n]*\\)\\$[ \t]*\\(;[^\n]*\\)?\n")
6697 ;; continuation line
6698 (goto-char (match-end 1))
6699 (throw 'exit nil)))
6700 (t (throw 'exit (preceding-char))))))))
6701
6702 (defvar idlwave-complete-after-success-form nil
6703 "A form to evaluate after successful completion.")
6704 (defvar idlwave-complete-after-success-form-force nil
6705 "A form to evaluate after completion selection in *Completions* buffer.")
6706 (defconst idlwave-completion-mark (make-marker)
6707 "A mark pointing to the beginning of the completion string.")
6708
6709 (defun idlwave-complete-in-buffer (type stype list selector prompt isa
6710 &optional prepare-display-function
6711 special-selector)
6712 "Perform TYPE completion of word before point against LIST.
6713 SELECTOR is the PREDICATE argument for the completion function. Show
6714 PROMPT in echo area. TYPE is one of the intern types, e.g. 'function,
6715 'procedure, 'class-tag, 'keyword, 'sysvar, etc. SPECIAL-SELECTOR is
6716 used only once, for `all-completions', and can be used to, e.g.,
6717 accumulate information on matching completions."
6718 (let* ((completion-ignore-case t)
6719 beg (end (point)) slash part spart completion all-completions
6720 dpart dcompletion)
6721
6722 (unless list
6723 (error (concat prompt ": No completions available")))
6724
6725 ;; What is already in the buffer?
6726 (save-excursion
6727 (skip-chars-backward "a-zA-Z0-9_$")
6728 (setq slash (eq (preceding-char) ?/)
6729 beg (point)
6730 idlwave-complete-after-success-form
6731 (list 'idlwave-after-successful-completion
6732 (list 'quote type) slash beg)
6733 idlwave-complete-after-success-form-force
6734 (list 'idlwave-after-successful-completion
6735 (list 'quote type) slash (list 'quote 'force))))
6736
6737 ;; Try a completion
6738 (setq part (buffer-substring beg end)
6739 dpart (downcase part)
6740 spart (idlwave-sintern stype part)
6741 completion (try-completion part list selector)
6742 dcompletion (if (stringp completion) (downcase completion))
6743 idlwave-completion-help-links nil)
6744 (cond
6745 ((null completion)
6746 ;; nothing available.
6747 (error (concat prompt ": no completion for \"%s\"") part))
6748 ((and (not (equal dpart dcompletion))
6749 (not (eq t completion)))
6750 ;; We can add something
6751 (delete-region beg end)
6752 (if (and (string= part dpart)
6753 (or (not (string= part ""))
6754 idlwave-complete-empty-string-as-lower-case)
6755 (not idlwave-completion-force-default-case))
6756 (insert dcompletion)
6757 (insert completion))
6758 (if (eq t (try-completion completion list selector))
6759 ;; Now this is a unique match
6760 (idlwave-after-successful-completion type slash beg))
6761 t)
6762 ((or (eq completion t)
6763 (and (= 1 (length (setq all-completions
6764 (idlwave-uniquify
6765 (all-completions part list
6766 (or special-selector
6767 selector))))))
6768 (equal dpart dcompletion)))
6769 ;; This is already complete
6770 (idlwave-after-successful-completion type slash beg)
6771 (message "%s is already the complete %s" part isa)
6772 nil)
6773 (t
6774 ;; We cannot add something - offer a list.
6775 (message "Making completion list...")
6776
6777 (unless idlwave-completion-help-links ; already set somewhere?
6778 (mapc (lambda (x) ; Pass link prop through to highlight-linked
6779 (let ((link (get-text-property 0 'link (car x))))
6780 (if link
6781 (push (cons (car x) link)
6782 idlwave-completion-help-links))))
6783 list))
6784 (let* ((list all-completions)
6785 ;; "complete" means, this is already a valid completion
6786 (complete (memq spart all-completions))
6787 (completion-highlight-first-word-only t)) ; XEmacs
6788 ; (completion-fixup-function ; Emacs
6789 ; (lambda () (and (eq (preceding-char) ?>)
6790 ; (re-search-backward " <" beg t)))))
6791
6792 (setq list (sort list (lambda (a b)
6793 (string< (downcase a) (downcase b)))))
6794 (if prepare-display-function
6795 (setq list (funcall prepare-display-function list)))
6796 (if (and (string= part dpart)
6797 (or (not (string= part ""))
6798 idlwave-complete-empty-string-as-lower-case)
6799 (not idlwave-completion-force-default-case))
6800 (setq list (mapcar (lambda (x)
6801 (if (listp x)
6802 (setcar x (downcase (car x)))
6803 (setq x (downcase x)))
6804 x)
6805 list)))
6806 (idlwave-display-completion-list list prompt beg complete))
6807 t))))
6808
6809 (defun idlwave-complete-class ()
6810 "Complete a class at point."
6811 (interactive)
6812 ;; Call `idlwave-routines' to make sure the class list will be available
6813 (idlwave-routines)
6814 ;; Check for the special case of completing empty string after pro/function
6815 (if (let ((case-fold-search t))
6816 (save-excursion
6817 (and
6818 (re-search-backward "\\<\\(pro\\|function\\)[ \t]+\\="
6819 (- (point) 15) t)
6820 (goto-char (point-min))
6821 (re-search-forward
6822 "^[ \t]*\\(pro\\|function\\)[ \t]+\\([a-zA-Z0-9_]+::\\)" nil t))))
6823 ;; Yank the full class specification
6824 (insert (match-string 2))
6825 ;; Do the completion, using list gathered from `idlwave-routines'
6826 (idlwave-complete-in-buffer
6827 'class 'class (idlwave-class-alist) nil
6828 "Select a class" "class"
6829 '(lambda (list) ;; Push it to help-links if system help available
6830 (mapcar (lambda (x)
6831 (let* ((entry (idlwave-class-info x))
6832 (link (nth 1 (assq 'link entry))))
6833 (if link (push (cons x link)
6834 idlwave-completion-help-links))
6835 x))
6836 list)))))
6837
6838 (defun idlwave-attach-classes (list type show-classes)
6839 ;; Attach the proper class list to a LIST of completion items.
6840 ;; TYPE, when 'kwd, shows classes for method keywords, when
6841 ;; 'class-tag, for class tags, and otherwise for methods.
6842 ;; SHOW-CLASSES is the value of `idlwave-completion-show-classes'.
6843 (if (or (null show-classes) ; don't want to see classes
6844 (null class-selector) ; not a method call
6845 (and
6846 (stringp class-selector) ; the class is already known
6847 (not super-classes))) ; no possibilities for inheritance
6848 ;; In these cases, we do not have to do anything
6849 list
6850 (let* ((do-prop (and (>= show-classes 0)
6851 (>= emacs-major-version 21)))
6852 (do-buf (not (= show-classes 0)))
6853 ;; (do-dots (featurep 'xemacs))
6854 (do-dots t)
6855 (inherit (if (and (not (eq type 'class-tag)) super-classes)
6856 (cons class-selector super-classes)))
6857 (max (abs show-classes))
6858 (lmax (if do-dots (apply 'max (mapcar 'length list))))
6859 classes nclasses class-info space)
6860 (mapcar
6861 (lambda (x)
6862 ;; get the classes
6863 (if (eq type 'class-tag)
6864 ;; Just one class for tags
6865 (setq classes
6866 (list
6867 (idlwave-class-or-superclass-with-tag class-selector x)))
6868 ;; Multiple classes for method or method-keyword
6869 (setq classes
6870 (if (eq type 'kwd)
6871 (idlwave-all-method-keyword-classes
6872 method-selector x type-selector)
6873 (idlwave-all-method-classes x type-selector)))
6874 (if inherit
6875 (setq classes
6876 (delq nil
6877 (mapcar (lambda (x) (if (memq x inherit) x nil))
6878 classes)))))
6879 (setq nclasses (length classes))
6880 ;; Make the separator between item and class-info
6881 (if do-dots
6882 (setq space (concat " " (make-string (- lmax (length x)) ?.)))
6883 (setq space " "))
6884 (if do-buf
6885 ;; We do want info in the buffer
6886 (if (<= nclasses max)
6887 (setq class-info (concat
6888 space
6889 "<" (mapconcat 'identity classes ",") ">"))
6890 (setq class-info (format "%s<%d classes>" space nclasses)))
6891 (setq class-info nil))
6892 (when do-prop
6893 ;; We do want properties
6894 (setq x (copy-sequence x))
6895 (put-text-property 0 (length x)
6896 'help-echo (mapconcat 'identity classes " ")
6897 x))
6898 (if class-info
6899 (list x class-info)
6900 x))
6901 list))))
6902
6903 (defun idlwave-attach-method-classes (list)
6904 ;; Call idlwave-attach-classes with method parameters
6905 (idlwave-attach-classes list 'method idlwave-completion-show-classes))
6906 (defun idlwave-attach-keyword-classes (list)
6907 ;; Call idlwave-attach-classes with keyword parameters
6908 (idlwave-attach-classes list 'kwd idlwave-completion-show-classes))
6909 (defun idlwave-attach-class-tag-classes (list)
6910 ;; Call idlwave-attach-classes with class structure tags
6911 (idlwave-attach-classes list 'class-tag idlwave-completion-show-classes))
6912
6913
6914 ;;----------------------------------------------------------------------
6915 ;;----------------------------------------------------------------------
6916 ;;----------------------------------------------------------------------
6917 ;;----------------------------------------------------------------------
6918 ;;----------------------------------------------------------------------
6919 (defvar rtn)
6920 (defun idlwave-pset (item)
6921 (set 'rtn item))
6922
6923 (defun idlwave-popup-select (ev list title &optional sort)
6924 "Select an item in LIST with a popup menu.
6925 TITLE is the title to put atop the popup. If SORT is non-nil,
6926 sort the list before displaying."
6927 (let ((maxpopup idlwave-max-popup-menu-items)
6928 rtn menu resp)
6929 (cond ((null list))
6930 ((= 1 (length list))
6931 (setq rtn (car list)))
6932 ((featurep 'xemacs)
6933 (if sort (setq list (sort list (lambda (a b)
6934 (string< (upcase a) (upcase b))))))
6935 (setq menu
6936 (append (list title)
6937 (mapcar (lambda (x) (vector x (list 'idlwave-pset
6938 x)))
6939 list)))
6940 (setq menu (idlwave-split-menu-xemacs menu maxpopup))
6941 (setq resp (get-popup-menu-response menu))
6942 (funcall (event-function resp) (event-object resp)))
6943 (t
6944 (if sort (setq list (sort list (lambda (a b)
6945 (string< (upcase a) (upcase b))))))
6946 (setq menu (cons title
6947 (list
6948 (append (list "")
6949 (mapcar (lambda(x) (cons x x)) list)))))
6950 (setq menu (idlwave-split-menu-emacs menu maxpopup))
6951 (setq rtn (x-popup-menu ev menu))))
6952 rtn))
6953
6954 (defun idlwave-split-menu-xemacs (menu N)
6955 "Split the MENU into submenus of maximum length N."
6956 (if (<= (length menu) (1+ N))
6957 ;; No splitting needed
6958 menu
6959 (let* ((title (car menu))
6960 (entries (cdr menu))
6961 (menu (list title))
6962 (cnt 0)
6963 (nextmenu nil))
6964 (while entries
6965 (while (and entries (< cnt N))
6966 (setq cnt (1+ cnt)
6967 nextmenu (cons (car entries) nextmenu)
6968 entries (cdr entries)))
6969 (setq nextmenu (nreverse nextmenu))
6970 (setq nextmenu (cons (format "%s...%s"
6971 (aref (car nextmenu) 0)
6972 (aref (nth (1- cnt) nextmenu) 0))
6973 nextmenu))
6974 (setq menu (cons nextmenu menu)
6975 nextmenu nil
6976 cnt 0))
6977 (nreverse menu))))
6978
6979 (defun idlwave-split-menu-emacs (menu N)
6980 "Split the MENU into submenus of maximum length N."
6981 (if (<= (length (nth 1 menu)) (1+ N))
6982 ;; No splitting needed
6983 menu
6984 (let* ((title (car menu))
6985 (entries (cdr (nth 1 menu)))
6986 (menu nil)
6987 (cnt 0)
6988 (nextmenu nil))
6989 (while entries
6990 (while (and entries (< cnt N))
6991 (setq cnt (1+ cnt)
6992 nextmenu (cons (car entries) nextmenu)
6993 entries (cdr entries)))
6994 (setq nextmenu (nreverse nextmenu))
6995 (prin1 nextmenu)
6996 (setq nextmenu (cons (format "%s...%s"
6997 (car (car nextmenu))
6998 (car (nth (1- cnt) nextmenu)))
6999 nextmenu))
7000 (setq menu (cons nextmenu menu)
7001 nextmenu nil
7002 cnt 0))
7003 (setq menu (nreverse menu))
7004 (setq menu (cons title menu))
7005 menu)))
7006
7007 (defvar idlwave-completion-setup-hook nil)
7008
7009 (defun idlwave-scroll-completions (&optional message)
7010 "Scroll the completion window on this frame."
7011 (let ((cwin (get-buffer-window "*Completions*" 'visible))
7012 (win (selected-window)))
7013 (unwind-protect
7014 (progn
7015 (select-window cwin)
7016 (condition-case nil
7017 (scroll-up)
7018 (error (if (and (listp last-command)
7019 (nth 2 last-command))
7020 (progn
7021 (select-window win)
7022 (eval idlwave-complete-after-success-form))
7023 (set-window-start cwin (point-min)))))
7024 (and message (message "%s" message)))
7025 (select-window win))))
7026
7027 (defun idlwave-display-completion-list (list &optional message beg complete)
7028 "Display the completions in LIST in the completions buffer and echo MESSAGE."
7029 (unless (and (get-buffer-window "*Completions*")
7030 (idlwave-local-value 'idlwave-completion-p "*Completions*"))
7031 (move-marker idlwave-completion-mark beg)
7032 (setq idlwave-before-completion-wconf (current-window-configuration)))
7033
7034 (if (featurep 'xemacs)
7035 (idlwave-display-completion-list-xemacs
7036 list)
7037 (idlwave-display-completion-list-emacs list))
7038
7039 ;; Store a special value in `this-command'. When `idlwave-complete'
7040 ;; finds this in `last-command', it will scroll the *Completions* buffer.
7041 (setq this-command (list 'idlwave-display-completion-list message complete))
7042
7043 ;; Mark the completions buffer as created by cib
7044 (idlwave-set-local 'idlwave-completion-p t "*Completions*")
7045
7046 ;; Fontify the classes
7047 (if (and idlwave-completion-fontify-classes
7048 (consp (car list)))
7049 (idlwave-completion-fontify-classes))
7050
7051 ;; Run the hook
7052 (run-hooks 'idlwave-completion-setup-hook)
7053
7054 ;; Display the message
7055 (message "%s" (or message "Making completion list...done")))
7056
7057 (defun idlwave-choose (function &rest args)
7058 "Call FUNCTION as a completion chooser and pass ARGS to it."
7059 (let ((completion-ignore-case t)) ; install correct value
7060 (apply function args))
7061 (if (and (eq major-mode 'idlwave-shell-mode)
7062 (boundp 'font-lock-mode)
7063 (not font-lock-mode))
7064 ;; For the shell, remove the fontification of the word before point
7065 (let ((beg (save-excursion
7066 (skip-chars-backward "a-zA-Z0-9_")
7067 (point))))
7068 (remove-text-properties beg (point) '(face nil))))
7069 (eval idlwave-complete-after-success-form-force))
7070
7071 (defun idlwave-keyboard-quit ()
7072 (interactive)
7073 (unwind-protect
7074 (if (eq (car-safe last-command) 'idlwave-display-completion-list)
7075 (idlwave-restore-wconf-after-completion))
7076 (keyboard-quit)))
7077
7078 (defun idlwave-restore-wconf-after-completion ()
7079 "Restore the old (before completion) window configuration."
7080 (and idlwave-completion-restore-window-configuration
7081 idlwave-before-completion-wconf
7082 (set-window-configuration idlwave-before-completion-wconf)))
7083
7084 (defun idlwave-one-key-select (sym prompt delay)
7085 "Make the user select an element from the alist in the variable SYM.
7086 The keys of the alist are expected to be strings. The function returns the
7087 car of the selected association.
7088 To do this, PROMPT is displayed and the user must hit a letter key to
7089 select an entry. If the user does not reply within DELAY seconds, a help
7090 window with the options is displayed automatically.
7091 The key which is associated with each option is generated automatically.
7092 First, the strings are checked for preselected keys, like in \"[P]rint\".
7093 If these don't exist, a letter in the string is automatically selected."
7094 (let* ((alist (symbol-value sym))
7095 (temp-buffer-show-hook (if (fboundp 'fit-window-to-buffer)
7096 '(fit-window-to-buffer)))
7097 keys-alist char)
7098 ;; First check the cache
7099 (if (and (eq (symbol-value sym) (get sym :one-key-alist-last)))
7100 (setq keys-alist (get sym :one-key-alist-cache))
7101 ;; Need to make new list
7102 (setq keys-alist (idlwave-make-one-key-alist alist))
7103 (put sym :one-key-alist-cache keys-alist)
7104 (put sym :one-key-alist-last alist))
7105 ;; Display prompt and wait for quick reply
7106 (message "%s[%s]" prompt
7107 (mapconcat (lambda(x) (char-to-string (car x)))
7108 keys-alist ""))
7109 (if (sit-for delay)
7110 ;; No quick reply: Show help
7111 (save-window-excursion
7112 (with-output-to-temp-buffer "*Completions*"
7113 (mapcar (lambda(x)
7114 (princ (nth 1 x))
7115 (princ "\n"))
7116 keys-alist))
7117 (setq char (read-char)))
7118 (setq char (read-char)))
7119 (message nil)
7120 ;; Return the selected result
7121 (nth 2 (assoc char keys-alist))))
7122
7123 ;; Used for, e.g., electric debug super-examine.
7124 (defun idlwave-make-one-key-alist (alist)
7125 "Make an alist for single key selection."
7126 (let ((l alist) keys-alist name start char help
7127 (cnt 0)
7128 (case-fold-search nil))
7129 (while l
7130 (setq name (car (car l))
7131 l (cdr l))
7132 (catch 'exit
7133 ;; First check if the configuration predetermined a key
7134 (if (string-match "\\[\\(.\\)\\]" name)
7135 (progn
7136 (setq char (string-to-char (downcase (match-string 1 name)))
7137 help (format "%c: %s" char name)
7138 keys-alist (cons (list char help name) keys-alist))
7139 (throw 'exit t)))
7140 ;; Then check for capital letters
7141 (setq start 0)
7142 (while (string-match "[A-Z]" name start)
7143 (setq start (match-end 0)
7144 char (string-to-char (downcase (match-string 0 name))))
7145 (if (not (assoc char keys-alist))
7146 (progn
7147 (setq help (format "%c: %s" char
7148 (replace-match
7149 (concat "[" (match-string 0 name) "]")
7150 t t name))
7151 keys-alist (cons (list char help name) keys-alist))
7152 (throw 'exit t))))
7153 ;; Now check for lowercase letters
7154 (setq start 0)
7155 (while (string-match "[a-z]" name start)
7156 (setq start (match-end 0)
7157 char (string-to-char (match-string 0 name)))
7158 (if (not (assoc char keys-alist))
7159 (progn
7160 (setq help (format "%c: %s" char
7161 (replace-match
7162 (concat "[" (match-string 0 name) "]")
7163 t t name))
7164 keys-alist (cons (list char help name) keys-alist))
7165 (throw 'exit t))))
7166 ;; Bummer, nothing found! Use a stupid number
7167 (setq char (string-to-char (int-to-string (setq cnt (1+ cnt))))
7168 help (format "%c: %s" char name)
7169 keys-alist (cons (list char help name) keys-alist))))
7170 (nreverse keys-alist)))
7171
7172 (defun idlwave-set-local (var value &optional buffer)
7173 "Set the buffer-local value of VAR in BUFFER to VALUE."
7174 (save-excursion
7175 (set-buffer (or buffer (current-buffer)))
7176 (set (make-local-variable var) value)))
7177
7178 (defun idlwave-local-value (var &optional buffer)
7179 "Return the value of VAR in BUFFER, but only if VAR is local to BUFFER."
7180 (save-excursion
7181 (set-buffer (or buffer (current-buffer)))
7182 (and (local-variable-p var (current-buffer))
7183 (symbol-value var))))
7184
7185 ;; In XEmacs, we can use :activate-callback directly to advice the
7186 ;; choose functions. We use the private keymap only for the online
7187 ;; help feature.
7188
7189 (defvar idlwave-completion-map nil
7190 "Keymap for `completion-list-mode' with `idlwave-complete'.")
7191
7192 (defun idlwave-display-completion-list-xemacs (list &rest cl-args)
7193 (with-output-to-temp-buffer "*Completions*"
7194 (apply 'display-completion-list list
7195 ':activate-callback 'idlwave-default-choose-completion
7196 cl-args))
7197 (save-excursion
7198 (set-buffer "*Completions*")
7199 (use-local-map
7200 (or idlwave-completion-map
7201 (setq idlwave-completion-map
7202 (idlwave-make-modified-completion-map-xemacs
7203 (current-local-map)))))))
7204
7205 (defun idlwave-default-choose-completion (&rest args)
7206 "Execute `default-choose-completion' and then restore the win-conf."
7207 (apply 'idlwave-choose 'default-choose-completion args))
7208
7209 (defun idlwave-make-modified-completion-map-xemacs (old-map)
7210 "Replace `choose-completion' and `mouse-choose-completion' in OLD-MAP."
7211 (let ((new-map (copy-keymap old-map)))
7212 (define-key new-map [button3up] 'idlwave-mouse-completion-help)
7213 (define-key new-map [button3] (lambda ()
7214 (interactive)
7215 (setq this-command last-command)))
7216 new-map))
7217
7218 ;; In Emacs we also replace keybindings in the completion
7219 ;; map in order to install our wrappers.
7220
7221 (defun idlwave-display-completion-list-emacs (list)
7222 "Display completion list and install the choose wrappers."
7223 (with-output-to-temp-buffer "*Completions*"
7224 (display-completion-list list))
7225 (save-excursion
7226 (set-buffer "*Completions*")
7227 (use-local-map
7228 (or idlwave-completion-map
7229 (setq idlwave-completion-map
7230 (idlwave-make-modified-completion-map-emacs
7231 (current-local-map)))))))
7232
7233 (defun idlwave-make-modified-completion-map-emacs (old-map)
7234 "Replace `choose-completion' and `mouse-choose-completion' in OLD-MAP."
7235 (let ((new-map (copy-keymap old-map)))
7236 (substitute-key-definition
7237 'choose-completion 'idlwave-choose-completion new-map)
7238 (substitute-key-definition
7239 'mouse-choose-completion 'idlwave-mouse-choose-completion new-map)
7240 (define-key new-map [mouse-3] 'idlwave-mouse-completion-help)
7241 new-map))
7242
7243 (defun idlwave-choose-completion (&rest args)
7244 "Choose the completion that point is in or next to."
7245 (interactive)
7246 (apply 'idlwave-choose 'choose-completion args))
7247
7248 (defun idlwave-mouse-choose-completion (&rest args)
7249 "Click on an alternative in the `*Completions*' buffer to choose it."
7250 (interactive "e")
7251 (apply 'idlwave-choose 'mouse-choose-completion args))
7252
7253 ;;----------------------------------------------------------------------
7254 ;;----------------------------------------------------------------------
7255
7256 ;;; ------------------------------------------------------------------------
7257 ;;; Stucture parsing code, and code to manage class info
7258
7259 ;;
7260 ;; - Go again over the documentation how to write a completion
7261 ;; plugin. It is in self.el, but currently still very bad.
7262 ;; This could be in a separate file in the distribution, or
7263 ;; in an appendix for the manual.
7264
7265 (defvar idlwave-struct-skip
7266 "[ \t]*\\(\\$.*\n\\(^[ \t]*\\(\\$[ \t]*\\)?\\(;.*\\)?\n\\)*\\)?[ \t]*"
7267 "Regexp for skipping continued blank or comment-only lines in structures.")
7268
7269 (defvar idlwave-struct-tag-regexp
7270 (concat "[{,]" ;leading comma/brace
7271 idlwave-struct-skip ; 4 groups
7272 "\\([a-zA-Z][a-zA-Z0-9_]*\\)" ;the tag itself, group 5
7273 "[ \t]*:") ; the final colon
7274 "Regexp for structure tags.")
7275
7276 (defun idlwave-struct-tags ()
7277 "Return a list of all tags in the structure defined at point.
7278 Point is expected just before the opening `{' of the struct definition."
7279 (save-excursion
7280 (let* ((borders (idlwave-struct-borders))
7281 (beg (car borders))
7282 (end (cdr borders))
7283 tags)
7284 (goto-char beg)
7285 (save-restriction
7286 (narrow-to-region beg end)
7287 (while (re-search-forward idlwave-struct-tag-regexp end t)
7288 ;; Check if we are still on the top level of the structure.
7289 (if (and (condition-case nil (progn (up-list -1) t) (error nil))
7290 (= (point) beg))
7291 (push (match-string-no-properties 5) tags))
7292 (goto-char (match-end 0))))
7293 (nreverse tags))))
7294
7295 (defun idlwave-find-struct-tag (tag)
7296 "Find a given TAG in the structure defined at point."
7297 (let* ((borders (idlwave-struct-borders))
7298 (beg (car borders))
7299 (end (cdr borders))
7300 (case-fold-search t))
7301 (re-search-forward (concat "\\(^[ \t]*\\|[,{][ \t]*\\)" tag "[ \t]*:")
7302 end t)))
7303
7304 (defun idlwave-struct-inherits ()
7305 "Return a list of all `inherits' names in the struct at point.
7306 Point is expected just before the opening `{' of the struct definition."
7307 (save-excursion
7308 (let* ((borders (idlwave-struct-borders))
7309 (beg (car borders))
7310 (end (cdr borders))
7311 (case-fold-search t)
7312 names)
7313 (goto-char beg)
7314 (save-restriction
7315 (narrow-to-region beg end)
7316 (while (re-search-forward
7317 (concat "[{,]" ;leading comma/brace
7318 idlwave-struct-skip ; 4 groups
7319 "inherits" ; The INHERITS tag
7320 idlwave-struct-skip ; 4 more
7321 "\\([a-zA-Z][a-zA-Z0-9_]*\\)") ; The super-group, #9
7322 end t)
7323 ;; Check if we are still on the top level of the structure.
7324 (if (and (condition-case nil (progn (up-list -1) t) (error nil))
7325 (= (point) beg))
7326 (push (match-string-no-properties 9) names))
7327 (goto-char (match-end 0))))
7328 (nreverse names))))
7329
7330 (defun idlwave-in-structure ()
7331 "Return t if point is inside an IDL structure definition."
7332 (let ((beg (point)))
7333 (save-excursion
7334 (if (not (or (idlwave-in-comment) (idlwave-in-quote)))
7335 (if (idlwave-find-structure-definition nil nil 'back)
7336 (let ((borders (idlwave-struct-borders)))
7337 (or (= (car borders) (cdr borders)) ;; struct not yet closed...
7338 (and (> beg (car borders)) (< beg (cdr borders))))))))))
7339
7340 (defun idlwave-struct-borders ()
7341 "Return the borders of the {...} after point as a cons cell."
7342 (let (beg)
7343 (save-excursion
7344 (skip-chars-forward "^{")
7345 (setq beg (point))
7346 (condition-case nil (forward-list 1)
7347 (error (goto-char beg)))
7348 (cons beg (point)))))
7349
7350 (defun idlwave-find-structure-definition (&optional var name bound)
7351 "Search forward for a structure definition.
7352 If VAR is non-nil, search for a structure assigned to variable VAR.
7353 If NAME is non-nil, search for a named structure NAME, if a string,
7354 or a generic named structure otherwise. If BOUND is an integer, limit
7355 the search. If BOUND is the symbol `all', we search first back and
7356 then forward through the entire file. If BOUND is the symbol `back'
7357 we search only backward."
7358 (let* ((ws "[ \t]*\\(\\$.*\n[ \t]*\\)*")
7359 (case-fold-search t)
7360 (lim (if (integerp bound) bound nil))
7361 (re (concat
7362 (if var
7363 (concat "\\<" (regexp-quote (downcase var)) "\\>" ws)
7364 "\\(\\)")
7365 "=" ws "\\({\\)"
7366 (if name
7367 (if (stringp name)
7368 (concat ws "\\(\\<" (downcase name) "\\)[^a-zA-Z0-9_$]")
7369 ;; Just a generic name
7370 (concat ws "\\<\\([a-zA-Z_0-9$]+\\)" ws ","))
7371 ""))))
7372 (if (or (and (or (eq bound 'all) (eq bound 'back))
7373 (re-search-backward re nil t))
7374 (and (not (eq bound 'back)) (re-search-forward re lim t)))
7375 (progn
7376 (goto-char (match-beginning 3))
7377 (match-string-no-properties 5)))))
7378
7379 (defvar idlwave-class-info nil)
7380 (defvar idlwave-class-reset nil) ; to reset buffer-local classes
7381
7382 (add-hook 'idlwave-update-rinfo-hook
7383 (lambda () (setq idlwave-class-reset t)))
7384 (add-hook 'idlwave-after-load-rinfo-hook
7385 (lambda () (setq idlwave-class-info nil)))
7386
7387 (defun idlwave-class-info (class)
7388 (let (list entry)
7389 (if idlwave-class-info
7390 (if idlwave-class-reset
7391 (setq
7392 idlwave-class-reset nil
7393 idlwave-class-info ; Remove any visited in a buffer
7394 (delq nil (mapcar
7395 (lambda (x)
7396 (let ((filebuf
7397 (idlwave-class-file-or-buffer
7398 (or (cdr (assq 'found-in x)) (car x)))))
7399 (if (cdr filebuf)
7400 nil
7401 x)))
7402 idlwave-class-info))))
7403 ;; Info is nil, put in the system stuff to start.
7404 (setq idlwave-class-info idlwave-system-class-info)
7405 (setq list idlwave-class-info)
7406 (while (setq entry (pop list))
7407 (idlwave-sintern-class-info entry)))
7408 (setq class (idlwave-sintern-class class))
7409 (or (assq class idlwave-class-info)
7410 (progn (idlwave-scan-class-info class)
7411 (assq class idlwave-class-info)))))
7412
7413 (defun idlwave-sintern-class-info (entry)
7414 "Sintern the class names in a class-info entry."
7415 (let ((taglist (assq 'tags entry))
7416 (inherits (assq 'inherits entry)))
7417 (setcar entry (idlwave-sintern-class (car entry) 'set))
7418 (if inherits
7419 (setcdr inherits (mapcar (lambda (x) (idlwave-sintern-class x 'set))
7420 (cdr inherits))))))
7421
7422 (defun idlwave-find-class-definition (class &optional all-hook alt-class)
7423 "Find class structure definition(s).
7424 If ALL-HOOK is set, find all named structure definitions in a given
7425 class__define routine, on which ALL-HOOK will be run. If ALT-CLASS is
7426 set, look for the name__define pro, and inside of it, for the ALT-CLASS
7427 class/struct definition."
7428 (let ((case-fold-search t) end-lim list name)
7429 (when (re-search-forward
7430 (concat "^[ \t]*pro[ \t]+" (downcase class) "__define" "\\>") nil t)
7431 (if all-hook
7432 (progn
7433 ;; For everything there
7434 (setq end-lim (save-excursion (idlwave-end-of-subprogram) (point)))
7435 (while (setq name
7436 (idlwave-find-structure-definition nil t end-lim))
7437 (funcall all-hook name)))
7438 (idlwave-find-structure-definition nil (or alt-class class))))))
7439
7440
7441 (defun idlwave-class-file-or-buffer (class)
7442 "Find buffer visiting CLASS definition."
7443 (let* ((pro (concat (downcase class) "__define"))
7444 (file (idlwave-routine-source-file
7445 (nth 3 (idlwave-rinfo-assoc pro 'pro nil
7446 (idlwave-routines))))))
7447 (cons file (if file (idlwave-get-buffer-visiting file)))))
7448
7449
7450 (defun idlwave-scan-class-info (class)
7451 "Scan all class and named structure info in the class__define pro."
7452 (let* ((idlwave-auto-routine-info-updates nil)
7453 (filebuf (idlwave-class-file-or-buffer class))
7454 (file (car filebuf))
7455 (buf (cdr filebuf))
7456 (class (idlwave-sintern-class class)))
7457 (if (or
7458 (not file)
7459 (and ;; neither a regular file nor a visited buffer
7460 (not buf)
7461 (not (file-regular-p file))))
7462 nil ; Cannot find the file/buffer to get any info
7463 (save-excursion
7464 (if buf (set-buffer buf)
7465 ;; Read the file in temporarily
7466 (set-buffer (get-buffer-create " *IDLWAVE-tmp*"))
7467 (erase-buffer)
7468 (unless (eq major-mode 'idlwave-mode)
7469 (idlwave-mode))
7470 (insert-file-contents file))
7471 (save-excursion
7472 (goto-char 1)
7473 (idlwave-find-class-definition class
7474 ;; Scan all of the structures found there
7475 (lambda (name)
7476 (let* ((this-class (idlwave-sintern-class name))
7477 (entry
7478 (list this-class
7479 (cons 'tags (idlwave-struct-tags))
7480 (cons 'inherits (idlwave-struct-inherits)))))
7481 (if (not (eq this-class class))
7482 (setq entry (nconc entry (list (cons 'found-in class)))))
7483 (idlwave-sintern-class-info entry)
7484 (push entry idlwave-class-info)))))))))
7485
7486 (defun idlwave-class-found-in (class)
7487 "Return the FOUND-IN property of the CLASS."
7488 (cdr (assq 'found-in (idlwave-class-info class))))
7489 (defun idlwave-class-tags (class)
7490 "Return the native tags in CLASS."
7491 (cdr (assq 'tags (idlwave-class-info class))))
7492 (defun idlwave-class-inherits (class)
7493 "Return the direct superclasses of CLASS."
7494 (cdr (assq 'inherits (idlwave-class-info class))))
7495
7496
7497 (defun idlwave-all-class-tags (class)
7498 "Return a list of native and inherited tags in CLASS."
7499 (condition-case err
7500 (apply 'append (mapcar 'idlwave-class-tags
7501 (cons class (idlwave-all-class-inherits class))))
7502 (error
7503 (idlwave-class-tag-reset)
7504 (error "%s" (error-message-string err)))))
7505
7506
7507 (defun idlwave-all-class-inherits (class)
7508 "Return a list of all superclasses of CLASS (recursively expanded).
7509 The list is cached in `idlwave-class-info' for faster access."
7510 (cond
7511 ((not idlwave-support-inheritance) nil)
7512 ((eq class nil) nil)
7513 ((eq class t) nil)
7514 (t
7515 (let ((info (idlwave-class-info class))
7516 entry)
7517 (if (setq entry (assq 'all-inherits info))
7518 (cdr entry)
7519 ;; Save the depth of inheritance scan to check for circular references
7520 (let ((inherits (mapcar (lambda (x) (cons x 0))
7521 (idlwave-class-inherits class)))
7522 rtn all-inherits cl)
7523 (while inherits
7524 (setq cl (pop inherits)
7525 rtn (cons (car cl) rtn)
7526 inherits (append (mapcar (lambda (x)
7527 (cons x (1+ (cdr cl))))
7528 (idlwave-class-inherits (car cl)))
7529 inherits))
7530 (if (> (cdr cl) 999)
7531 (error
7532 "Class scan: inheritance depth exceeded. Circular inheritance?")
7533 ))
7534 (setq all-inherits (nreverse rtn))
7535 (nconc info (list (cons 'all-inherits all-inherits)))
7536 all-inherits))))))
7537
7538 (defun idlwave-entry-keywords (entry &optional record-link)
7539 "Return the flat entry keywords alist from routine-info entry.
7540 If RECORD-LINK is non-nil, the keyword text is copied and a text
7541 property indicating the link is added."
7542 (let (kwds)
7543 (mapc
7544 (lambda (key-list)
7545 (let ((file (car key-list)))
7546 (mapcar (lambda (key-cons)
7547 (let ((key (car key-cons))
7548 (link (cdr key-cons)))
7549 (when (and record-link file)
7550 (setq key (copy-sequence key))
7551 (put-text-property
7552 0 (length key)
7553 'link
7554 (concat
7555 file
7556 (if link
7557 (concat idlwave-html-link-sep
7558 (number-to-string link))))
7559 key))
7560 (push (list key) kwds)))
7561 (cdr key-list))))
7562 (nthcdr 5 entry))
7563 (nreverse kwds)))
7564
7565 (defun idlwave-entry-find-keyword (entry keyword)
7566 "Find keyword KEYWORD in entry ENTRY, and return (with link) if set."
7567 (catch 'exit
7568 (mapc
7569 (lambda (key-list)
7570 (let ((file (car key-list))
7571 (kwd (assoc keyword (cdr key-list))))
7572 (when kwd
7573 (setq kwd (cons (car kwd)
7574 (if (and file (cdr kwd))
7575 (concat file
7576 idlwave-html-link-sep
7577 (number-to-string (cdr kwd)))
7578 (cdr kwd))))
7579 (throw 'exit kwd))))
7580 (nthcdr 5 entry))))
7581
7582 ;;==========================================================================
7583 ;;
7584 ;; Completing class structure tags. This is a completion plugin.
7585 ;; The necessary taglist is constructed dynamically
7586
7587 (defvar idlwave-current-tags-class nil)
7588 (defvar idlwave-current-class-tags nil)
7589 (defvar idlwave-current-native-class-tags nil)
7590 (defvar idlwave-sint-class-tags nil)
7591 (declare-function idlwave-sintern-class-tag "idlwave" t t)
7592 (idlwave-new-sintern-type 'class-tag)
7593 (add-to-list 'idlwave-complete-special 'idlwave-complete-class-structure-tag)
7594 (add-hook 'idlwave-update-rinfo-hook 'idlwave-class-tag-reset)
7595
7596 (defun idlwave-complete-class-structure-tag ()
7597 "Complete a structure tag on a `self' argument in an object method."
7598 (interactive)
7599 (let ((pos (point))
7600 (case-fold-search t))
7601 (if (save-excursion
7602 ;; Check if the context is right
7603 (skip-chars-backward "a-zA-Z0-9._$")
7604 (and (< (point) (- pos 4))
7605 (looking-at "self\\.")))
7606 (let* ((class-selector (nth 2 (idlwave-current-routine)))
7607 (super-classes (idlwave-all-class-inherits class-selector)))
7608 ;; Check if we are in a class routine
7609 (unless class-selector
7610 (error "Not in a method procedure or function"))
7611 ;; Check if we need to update the "current" class
7612 (if (not (equal class-selector idlwave-current-tags-class))
7613 (idlwave-prepare-class-tag-completion class-selector))
7614 (setq idlwave-completion-help-info
7615 (list 'idlwave-complete-class-structure-tag-help
7616 (idlwave-sintern-routine
7617 (concat class-selector "__define"))
7618 nil))
7619 (let ((idlwave-cpl-bold idlwave-current-native-class-tags))
7620 (idlwave-complete-in-buffer
7621 'class-tag 'class-tag
7622 idlwave-current-class-tags nil
7623 (format "Select a tag of class %s" class-selector)
7624 "class tag"
7625 'idlwave-attach-class-tag-classes))
7626 t) ; return t to skip other completions
7627 nil)))
7628
7629 (defun idlwave-class-tag-reset ()
7630 (setq idlwave-current-tags-class nil))
7631
7632 (defun idlwave-prepare-class-tag-completion (class)
7633 "Find and parse the necessary class definitions for class structure tags."
7634 (setq idlwave-sint-class-tags nil)
7635 (setq idlwave-current-tags-class class)
7636 (setq idlwave-current-class-tags
7637 (mapcar (lambda (x)
7638 (list (idlwave-sintern-class-tag x 'set)))
7639 (idlwave-all-class-tags class)))
7640 (setq idlwave-current-native-class-tags
7641 (mapcar 'downcase (idlwave-class-tags class))))
7642
7643 ;===========================================================================
7644 ;;
7645 ;; Completing system variables and their structure fields
7646 ;; This is also a plugin.
7647
7648 (defvar idlwave-sint-sysvars nil)
7649 (defvar idlwave-sint-sysvartags nil)
7650 (declare-function idlwave-sintern-sysvar "idlwave" t t)
7651 (declare-function idlwave-sintern-sysvartag "idlwave" t t)
7652 (idlwave-new-sintern-type 'sysvar)
7653 (idlwave-new-sintern-type 'sysvartag)
7654 (add-to-list 'idlwave-complete-special 'idlwave-complete-sysvar-or-tag)
7655 (add-hook 'idlwave-update-rinfo-hook 'idlwave-sysvars-reset)
7656 (add-hook 'idlwave-after-load-rinfo-hook 'idlwave-sintern-sysvar-alist)
7657
7658
7659 (defun idlwave-complete-sysvar-or-tag ()
7660 "Complete a system variable."
7661 (interactive)
7662 (let ((pos (point))
7663 (case-fold-search t))
7664 (cond ((save-excursion
7665 ;; Check if the context is right for system variable
7666 (skip-chars-backward "[a-zA-Z0-9_$]")
7667 (equal (char-before) ?!))
7668 (setq idlwave-completion-help-info '(idlwave-complete-sysvar-help))
7669 (idlwave-complete-in-buffer 'sysvar 'sysvar
7670 idlwave-system-variables-alist nil
7671 "Select a system variable"
7672 "system variable")
7673 t) ; return t to skip other completions
7674 ((save-excursion
7675 ;; Check if the context is right for sysvar tag
7676 (skip-chars-backward "a-zA-Z0-9_$.")
7677 (and (equal (char-before) ?!)
7678 (looking-at "\\([a-zA-Z][a-zA-Z0-9_$]*\\)\\.")
7679 (<= (match-end 0) pos)))
7680 ;; Complete a system variable tag
7681 (let* ((var (idlwave-sintern-sysvar (match-string 1)))
7682 (entry (assq var idlwave-system-variables-alist))
7683 (tags (cdr (assq 'tags entry))))
7684 (or entry (error "!%s is not a known system variable" var))
7685 (or tags (error "System variable !%s is not a structure" var))
7686 (setq idlwave-completion-help-info
7687 (list 'idlwave-complete-sysvar-tag-help var))
7688 (idlwave-complete-in-buffer 'sysvartag 'sysvartag
7689 tags nil
7690 "Select a system variable tag"
7691 "system variable tag")
7692 t)) ; return t to skip other completions
7693 (t nil))))
7694
7695 (defvar link) ;dynamic variables set by help callback
7696 (defvar props)
7697 (defun idlwave-complete-sysvar-help (mode word)
7698 (let ((word (or (nth 1 idlwave-completion-help-info) word))
7699 (entry (assoc word idlwave-system-variables-alist)))
7700 (cond
7701 ((eq mode 'test)
7702 (and (stringp word) entry (nth 1 (assq 'link entry))))
7703 ((eq mode 'set)
7704 (if entry (setq link (nth 1 (assq 'link entry))))) ;; setting dynamic!!!
7705 (t (error "This should not happen")))))
7706
7707 (defun idlwave-complete-sysvar-tag-help (mode word)
7708 (let* ((var (nth 1 idlwave-completion-help-info))
7709 (entry (assoc var idlwave-system-variables-alist))
7710 (tags (cdr (assq 'tags entry)))
7711 (main (nth 1 (assq 'link entry)))
7712 target main-base)
7713 (cond
7714 ((eq mode 'test) ; we can at least link the main
7715 (and (stringp word) entry main))
7716 ((eq mode 'set)
7717 (if entry
7718 (setq link
7719 (if (setq target (cdr (assoc-string word tags t)))
7720 (idlwave-substitute-link-target main target)
7721 main)))) ;; setting dynamic!!!
7722 (t (error "This should not happen")))))
7723
7724 (defun idlwave-split-link-target (link)
7725 "Split a given LINK into link file and anchor."
7726 (if (string-match idlwave-html-link-sep link)
7727 (cons (substring link 0 (match-beginning 0))
7728 (string-to-number (substring link (match-end 0))))))
7729
7730 (defun idlwave-substitute-link-target (link target)
7731 "Substitute the TARGET anchor for the given LINK."
7732 (let (main-base)
7733 (setq main-base (if (string-match "#" link)
7734 (substring link 0 (match-beginning 0))
7735 link))
7736 (if target
7737 (concat main-base idlwave-html-link-sep (number-to-string target))
7738 link)))
7739
7740 ;; Fake help in the source buffer for class structure tags.
7741 ;; KWD AND NAME ARE GLOBAL-VARIABLES HERE.
7742 (defvar name)
7743 (defvar kwd)
7744 (defvar idlwave-help-do-class-struct-tag nil)
7745 (defun idlwave-complete-class-structure-tag-help (mode word)
7746 (cond
7747 ((eq mode 'test) ; nothing gets fontified for class tags
7748 nil)
7749 ((eq mode 'set)
7750 (let (class-with found-in)
7751 (when (setq class-with
7752 (idlwave-class-or-superclass-with-tag
7753 idlwave-current-tags-class
7754 word))
7755 (if (assq (idlwave-sintern-class class-with)
7756 idlwave-system-class-info)
7757 (error "No help available for system class tags"))
7758 (if (setq found-in (idlwave-class-found-in class-with))
7759 (setq name (cons (concat found-in "__define") class-with))
7760 (setq name (concat class-with "__define")))))
7761 (setq kwd word
7762 idlwave-help-do-class-struct-tag t))
7763 (t (error "This should not happen"))))
7764
7765 (defun idlwave-class-or-superclass-with-tag (class tag)
7766 "Find and return the CLASS or one of its superclass with the
7767 associated TAG, if any."
7768 (let ((sclasses (cons class (idlwave-all-class-inherits class)))
7769 cl)
7770 (catch 'exit
7771 (while sclasses
7772 (setq cl (pop sclasses))
7773 (let ((tags (idlwave-class-tags cl)))
7774 (while tags
7775 (if (eq t (compare-strings tag 0 nil (car tags) 0 nil t))
7776 (throw 'exit cl))
7777 (setq tags (cdr tags))))))))
7778
7779
7780 (defun idlwave-sysvars-reset ()
7781 (if (and (fboundp 'idlwave-shell-is-running)
7782 (idlwave-shell-is-running)
7783 idlwave-idlwave_routine_info-compiled)
7784 (idlwave-shell-send-command "idlwave_get_sysvars"
7785 'idlwave-process-sysvars 'hide)))
7786
7787 (defun idlwave-process-sysvars ()
7788 (idlwave-shell-filter-sysvars)
7789 (setq idlwave-sint-sysvars nil
7790 idlwave-sint-sysvartags nil)
7791 (idlwave-sintern-sysvar-alist))
7792
7793 (defun idlwave-sintern-sysvar-alist ()
7794 (let ((list idlwave-system-variables-alist) entry tags)
7795 (while (setq entry (pop list))
7796 (setcar entry (idlwave-sintern-sysvar (car entry) 'set))
7797 (setq tags (assq 'tags entry))
7798 (if tags
7799 (setcdr tags
7800 (mapcar (lambda (x)
7801 (cons (idlwave-sintern-sysvartag (car x) 'set)
7802 (cdr x)))
7803 (cdr tags)))))))
7804
7805 (defvar idlwave-shell-command-output)
7806 (defun idlwave-shell-filter-sysvars ()
7807 "Get any new system variables and tags."
7808 (let ((text idlwave-shell-command-output)
7809 (start 0)
7810 (old idlwave-system-variables-alist)
7811 var tags type name class link old-entry)
7812 (setq idlwave-system-variables-alist nil)
7813 (while (string-match "^IDLWAVE-SYSVAR: !\\([a-zA-Z0-9_$]+\\)\\( \\(.*\\)\\)?"
7814 text start)
7815 (setq start (match-end 0)
7816 var (match-string 1 text)
7817 tags (if (match-end 3)
7818 (idlwave-split-string (match-string 3 text))))
7819 ;; Maintain old links, if present
7820 (setq old-entry (assq (idlwave-sintern-sysvar var) old))
7821 (setq link (assq 'link old-entry))
7822 (setq idlwave-system-variables-alist
7823 (cons (list var
7824 (cons
7825 'tags
7826 (mapcar (lambda (x)
7827 (cons x
7828 (cdr (assq
7829 (idlwave-sintern-sysvartag x)
7830 (cdr (assq 'tags old-entry))))))
7831 tags)) link)
7832 idlwave-system-variables-alist)))
7833 ;; Keep the old value if query was not successful
7834 (setq idlwave-system-variables-alist
7835 (or idlwave-system-variables-alist old))))
7836
7837 (defun idlwave-completion-fontify-classes ()
7838 "Goto the *Completions* buffer and fontify the class info."
7839 (when (featurep 'font-lock)
7840 (save-excursion
7841 (set-buffer "*Completions*")
7842 (save-excursion
7843 (goto-char (point-min))
7844 (let ((buffer-read-only nil))
7845 (while (re-search-forward "\\.*<[^>]+>" nil t)
7846 (put-text-property (match-beginning 0) (match-end 0)
7847 'face 'font-lock-string-face)))))))
7848
7849 (defun idlwave-uniquify (list)
7850 (let ((ht (make-hash-table :size (length list) :test 'equal)))
7851 (delq nil
7852 (mapcar (lambda (x)
7853 (unless (gethash x ht)
7854 (puthash x t ht)
7855 x))
7856 list))))
7857
7858 (defun idlwave-after-successful-completion (type slash &optional verify)
7859 "Add `=' or `(' after successful completion of keyword and function.
7860 Restore the pre-completion window configuration if possible."
7861 (cond
7862 ((eq type 'procedure)
7863 nil)
7864 ((eq type 'function)
7865 (cond
7866 ((equal idlwave-function-completion-adds-paren nil) nil)
7867 ((or (equal idlwave-function-completion-adds-paren t)
7868 (equal idlwave-function-completion-adds-paren 1))
7869 (insert "("))
7870 ((equal idlwave-function-completion-adds-paren 2)
7871 (insert "()")
7872 (backward-char 1))
7873 (t nil)))
7874 ((eq type 'keyword)
7875 (if (and idlwave-keyword-completion-adds-equal
7876 (not slash))
7877 (progn (insert "=") t)
7878 nil)))
7879
7880 ;; Restore the pre-completion window configuration if this is safe.
7881
7882 (if (or (eq verify 'force) ; force
7883 (and
7884 (get-buffer-window "*Completions*") ; visible
7885 (idlwave-local-value 'idlwave-completion-p
7886 "*Completions*") ; cib-buffer
7887 (eq (marker-buffer idlwave-completion-mark)
7888 (current-buffer)) ; buffer OK
7889 (equal (marker-position idlwave-completion-mark)
7890 verify))) ; pos OK
7891 (idlwave-restore-wconf-after-completion))
7892 (move-marker idlwave-completion-mark nil)
7893 (setq idlwave-before-completion-wconf nil))
7894
7895 (defun idlwave-mouse-context-help (ev &optional arg)
7896 "Call `idlwave-context-help' on the clicked location."
7897 (interactive "eP")
7898 (mouse-set-point ev)
7899 (idlwave-context-help arg))
7900
7901 (defvar idlwave-last-context-help-pos nil)
7902 (defun idlwave-context-help (&optional arg)
7903 "Display IDL Online Help on context.
7904 If point is on a keyword, help for that keyword will be shown. If
7905 point is on a routine name or in the argument list of a routine, help
7906 for that routine will be displayed. Works for system routines and
7907 keywords, it pulls up text help. For other routies and keywords,
7908 visits the source file, finding help in the header (if
7909 `idlwave-help-source-try-header' is non-nil) or the routine definition
7910 itself."
7911 (interactive "P")
7912 (idlwave-do-context-help arg))
7913
7914 (defun idlwave-mouse-completion-help (ev)
7915 "Display online help about the completion at point."
7916 (interactive "eP")
7917 ;; Restore last-command for next command, to make
7918 ;; scrolling/cancelling of completions work.
7919 (setq this-command last-command)
7920 (idlwave-do-mouse-completion-help ev))
7921
7922 (defun idlwave-routine-info (&optional arg external)
7923 "Display a routines calling sequence and list of keywords.
7924 When point is on the name a function or procedure, or in the argument
7925 list of a function or procedure, this command displays a help buffer with
7926 the information. When called with prefix arg, enforce class query.
7927
7928 When point is on an object operator `->', display the class stored in
7929 this arrow, if any (see `idlwave-store-inquired-class'). With a prefix
7930 arg, the class property is cleared out."
7931
7932 (interactive "P")
7933 (idlwave-routines)
7934 (if (string-match "->" (buffer-substring
7935 (max (point-min) (1- (point)))
7936 (min (+ 2 (point)) (point-max))))
7937 ;; Cursor is on an arrow
7938 (if (get-text-property (point) 'idlwave-class)
7939 ;; arrow has class property
7940 (if arg
7941 ;; Remove property
7942 (save-excursion
7943 (backward-char 1)
7944 (when (looking-at ".?\\(->\\)")
7945 (remove-text-properties (match-beginning 1) (match-end 1)
7946 '(idlwave-class nil face nil))
7947 (message "Class property removed from arrow")))
7948 ;; Echo class property
7949 (message "Arrow has text property identifying object to be class %s"
7950 (get-text-property (point) 'idlwave-class)))
7951 ;; No property found
7952 (message "Arrow has no class text property"))
7953
7954 ;; Not on an arrow...
7955 (let* ((idlwave-query-class nil)
7956 (idlwave-force-class-query (equal arg '(4)))
7957 (module (idlwave-what-module)))
7958 (if (car module)
7959 (apply 'idlwave-display-calling-sequence
7960 (idlwave-fix-module-if-obj_new module))
7961 (error "Don't know which calling sequence to show")))))
7962
7963 (defun idlwave-resolve (&optional arg)
7964 "Call RESOLVE_ROUTINE on the module name at point.
7965 Like `idlwave-routine-info', this looks for a routine call at point.
7966 After confirmation in the minibuffer, it will use the shell to issue
7967 a RESOLVE call for this routine, to attempt to make it defined and its
7968 routine info available for IDLWAVE. If the routine is a method call,
7969 both `class__method' and `class__define' will be tried.
7970 With ARG, enforce query for the class of object methods."
7971 (interactive "P")
7972 (let* ((idlwave-query-class nil)
7973 (idlwave-force-class-query (equal arg '(4)))
7974 (module (idlwave-what-module))
7975 (name (idlwave-make-full-name (nth 2 module) (car module)))
7976 (type (if (eq (nth 1 module) 'pro) "pro" "function"))
7977 (resolve (read-string "Resolve: " (format "%s %s" type name)))
7978 (kwd "")
7979 class)
7980 (if (string-match "\\(pro\\|function\\)[ \t]+\\(\\(.*\\)::\\)?\\(.*\\)"
7981 resolve)
7982 (setq type (match-string 1 resolve)
7983 class (if (match-beginning 2)
7984 (match-string 3 resolve)
7985 nil)
7986 name (match-string 4 resolve)))
7987 (if (string= (downcase type) "function")
7988 (setq kwd ",/is_function"))
7989
7990 (cond
7991 ((null class)
7992 (idlwave-shell-send-command
7993 (format "resolve_routine,'%s'%s" (downcase name) kwd)
7994 'idlwave-update-routine-info
7995 nil t))
7996 (t
7997 (idlwave-shell-send-command
7998 (format "resolve_routine,'%s__define'%s" (downcase class) kwd)
7999 (list 'idlwave-shell-send-command
8000 (format "resolve_routine,'%s__%s'%s"
8001 (downcase class) (downcase name) kwd)
8002 '(idlwave-update-routine-info)
8003 nil t))))))
8004
8005 (defun idlwave-find-module-this-file ()
8006 (interactive)
8007 (idlwave-find-module '(4)))
8008
8009 (defun idlwave-find-module (&optional arg)
8010 "Find the source code of an IDL module.
8011 Works for modules for which IDLWAVE has routine info available.
8012 The function offers as default the module name `idlwave-routine-info'
8013 would use. With ARG limit to this buffer. With two prefix ARG's
8014 force class query for object methods."
8015 (interactive "P")
8016 (let* ((idlwave-query-class nil)
8017 (idlwave-force-class-query (equal arg '(16)))
8018 (this-buffer (equal arg '(4)))
8019 (module (idlwave-fix-module-if-obj_new (idlwave-what-module)))
8020 (default (if module
8021 (concat (idlwave-make-full-name
8022 (nth 2 module) (car module))
8023 (if (eq (nth 1 module) 'pro) "<p>" "<f>"))
8024 "none"))
8025 (list
8026 (idlwave-uniquify
8027 (delq nil
8028 (mapcar (lambda (x)
8029 (if (eq 'system (car-safe (nth 3 x)))
8030 ;; Take out system routines with no source.
8031 nil
8032 (list
8033 (concat (idlwave-make-full-name
8034 (nth 2 x) (car x))
8035 (if (eq (nth 1 x) 'pro) "<p>" "<f>")))))
8036 (if this-buffer
8037 (idlwave-save-buffer-update)
8038 (idlwave-routines))))))
8039 (name (idlwave-completing-read
8040 (if (or (not this-buffer)
8041 (assoc default list))
8042 (format "Module (Default %s): " default)
8043 (format "Module in this file: "))
8044 list))
8045 type class)
8046 (if (string-match "\\`\\s-*\\'" name)
8047 ;; Nothing, use the default.
8048 (setq name default))
8049 (if (string-match "<[fp]>" name)
8050 (setq type (substring name -2 -1)
8051 name (substring name 0 -3)))
8052 (if (string-match "\\(.*\\)::\\(.*\\)" name)
8053 (setq class (match-string 1 name)
8054 name (match-string 2 name)))
8055 (setq name (idlwave-sintern-routine-or-method name class)
8056 class (idlwave-sintern-class class)
8057 type (cond ((equal type "f") 'fun)
8058 ((equal type "p") 'pro)
8059 (t t)))
8060 (idlwave-do-find-module name type class nil this-buffer)))
8061
8062 (defun idlwave-do-find-module (name type class
8063 &optional force-source this-buffer)
8064 (let ((name1 (idlwave-make-full-name class name))
8065 source buf1 entry
8066 (buf (current-buffer))
8067 (pos (point))
8068 file name2)
8069 (setq entry (idlwave-best-rinfo-assq name type class (idlwave-routines)
8070 'WITH-FILE)
8071 source (or force-source (nth 3 entry))
8072 name2 (if (nth 2 entry)
8073 (idlwave-make-full-name (nth 2 entry) name)
8074 name1))
8075 (if source
8076 (setq file (idlwave-routine-source-file source)))
8077 (unless file ; Try to find it on the path.
8078 (setq file
8079 (idlwave-expand-lib-file-name
8080 (if class
8081 (format "%s__define.pro" (downcase class))
8082 (format "%s.pro" (downcase name))))))
8083 (cond
8084 ((or (null name) (equal name ""))
8085 (error "Abort"))
8086 ((eq (car source) 'system)
8087 (error "Source code for system routine %s is not available"
8088 name2))
8089 ((or (not file) (not (file-regular-p file)))
8090 (error "Source code for routine %s is not available"
8091 name2))
8092 (t
8093 (when (not this-buffer)
8094 (setq buf1
8095 (idlwave-find-file-noselect file 'find))
8096 (pop-to-buffer buf1 t))
8097 (goto-char (point-max))
8098 (let ((case-fold-search t))
8099 (if (re-search-backward
8100 (concat "^[ \t]*\\<"
8101 (cond ((eq type 'fun) "function")
8102 ((eq type 'pro) "pro")
8103 (t "\\(pro\\|function\\)"))
8104 "\\>[ \t]+"
8105 (regexp-quote (downcase name2))
8106 "[^a-zA-Z0-9_$]")
8107 nil t)
8108 (goto-char (match-beginning 0))
8109 (pop-to-buffer buf)
8110 (goto-char pos)
8111 (error "Could not find routine %s" name2)))))))
8112
8113 (defun idlwave-what-module ()
8114 "Return a default module for stuff near point.
8115 Used by `idlwave-routine-info' and `idlwave-find-module'."
8116 (idlwave-routines)
8117 (if (let ((case-fold-search t))
8118 (save-excursion
8119 (idlwave-beginning-of-statement)
8120 (looking-at "[ \t]*\\(pro\\|function\\)[ \t]+\\(\\([a-zA-Z0-9_$]+\\)::\\)?\\([a-zA-Z0-9$_]+\\)\\([, \t\n]\\|$\\)")))
8121 ;; This is a function or procedure definition statement
8122 ;; We return the defined routine as module.
8123 (list
8124 (idlwave-sintern-routine-or-method (match-string-no-properties 4)
8125 (match-string-no-properties 2))
8126 (if (equal (downcase (match-string 1)) "pro") 'pro 'fun)
8127 (idlwave-sintern-class (match-string 3)))
8128
8129 ;; Not a definition statement - analyze precise position.
8130 (let* ((where (idlwave-where))
8131 (cw (nth 2 where))
8132 (pro (car (nth 0 where)))
8133 (func (car (nth 1 where)))
8134 (this-word (idlwave-this-word "a-zA-Z0-9$_"))
8135 (next-char (save-excursion (skip-chars-forward "a-zA-Z0-9$_")
8136 (following-char)))
8137 )
8138 (cond
8139 ((and (eq cw 'procedure)
8140 (not (equal this-word "")))
8141 (setq this-word (idlwave-sintern-routine-or-method
8142 this-word (nth 2 (nth 3 where))))
8143 (list this-word 'pro
8144 (idlwave-determine-class
8145 (cons this-word (cdr (nth 3 where)))
8146 'pro)))
8147 ((and (eq cw 'function)
8148 (not (equal this-word ""))
8149 (or (eq next-char ?\() ; exclude arrays, vars.
8150 (looking-at "[a-zA-Z0-9_]*[ \t]*(")))
8151 (setq this-word (idlwave-sintern-routine-or-method
8152 this-word (nth 2 (nth 3 where))))
8153 (list this-word 'fun
8154 (idlwave-determine-class
8155 (cons this-word (cdr (nth 3 where)))
8156 'fun)))
8157 ((and (memq cw '(function-keyword procedure-keyword))
8158 (not (equal this-word ""))
8159 (eq next-char ?\()) ; A function!
8160 (setq this-word (idlwave-sintern-routine this-word))
8161 (list this-word 'fun nil))
8162 (func
8163 (list func 'fun (idlwave-determine-class (nth 1 where) 'fun)))
8164 (pro
8165 (list pro 'pro (idlwave-determine-class (nth 0 where) 'pro)))
8166 (t nil)))))
8167
8168 (defun idlwave-what-module-find-class ()
8169 "Call `idlwave-what-module' and find the inherited class if necessary."
8170 (let* ((module (idlwave-what-module))
8171 (class (nth 2 module))
8172 classes)
8173 (if (and (= (length module) 3)
8174 (stringp class))
8175 (list (car module)
8176 (nth 1 module)
8177 (apply 'idlwave-find-inherited-class module))
8178 module)))
8179
8180 (defun idlwave-find-inherited-class (name type class)
8181 "Find the class which defines TYPE NAME and is CLASS or inherited by CLASS."
8182 (let ((entry (idlwave-best-rinfo-assoc name type class (idlwave-routines))))
8183 (if entry
8184 (nth 2 entry)
8185 class)))
8186
8187 (defun idlwave-fix-module-if-obj_new (module)
8188 "Check if MODULE points to obj_new.
8189 If yes, and if the cursor is in the keyword region, change to the
8190 appropriate Init method."
8191 (let* ((name (car module))
8192 (pos (point))
8193 (case-fold-search t)
8194 string)
8195 (if (and (stringp name)
8196 (equal (downcase name) "obj_new")
8197 (save-excursion
8198 (idlwave-beginning-of-statement)
8199 (setq string (buffer-substring (point) pos))
8200 (string-match "obj_new([^'\"]*['\"]\\([a-zA-Z0-9_]+\\)"
8201 string)))
8202 (let ((name "Init")
8203 (class (match-string 1 string)))
8204 (setq module (list (idlwave-sintern-method "Init")
8205 'fun
8206 (idlwave-sintern-class class)))))
8207 module))
8208
8209 (defun idlwave-fix-keywords (name type class keywords
8210 &optional super-classes system)
8211 "Update a list of keywords.
8212 Translate OBJ_NEW, adding all super-class keywords, or all keywords
8213 from all classes if CLASS equals t. If SYSTEM is non-nil, don't
8214 demand _EXTRA in the keyword list."
8215 (let ((case-fold-search t))
8216
8217 ;; If this is the OBJ_NEW function, try to figure out the class and use
8218 ;; the keywords from the corresponding INIT method.
8219 (if (and (equal (upcase name) "OBJ_NEW")
8220 (or (eq major-mode 'idlwave-mode)
8221 (eq major-mode 'idlwave-shell-mode)))
8222 (let* ((bos (save-excursion (idlwave-beginning-of-statement) (point)))
8223 (string (buffer-substring bos (point)))
8224 (case-fold-search t)
8225 class)
8226 (and (string-match "obj_new([^'\"]*['\"]\\([a-zA-Z0-9_]+\\)"
8227 string)
8228 (setq class (idlwave-sintern-class (match-string 1 string)))
8229 (setq idlwave-current-obj_new-class class)
8230 (setq keywords
8231 (append keywords
8232 (idlwave-entry-keywords
8233 (idlwave-rinfo-assq
8234 (idlwave-sintern-method "INIT")
8235 'fun
8236 class
8237 (idlwave-routines)) 'do-link))))))
8238
8239 ;; If the class is `t', combine all keywords of all methods NAME
8240 (when (eq class t)
8241 (mapc (lambda (entry)
8242 (and
8243 (nth 2 entry) ; non-nil class
8244 (eq (nth 1 entry) type) ; correct type
8245 (setq keywords
8246 (append keywords
8247 (idlwave-entry-keywords entry 'do-link)))))
8248 (idlwave-all-assq name (idlwave-routines)))
8249 (setq keywords (idlwave-uniquify keywords)))
8250
8251 ;; If we have inheritance, add all keywords from superclasses, if
8252 ;; the user indicated that method in `idlwave-keyword-class-inheritance'
8253 (when (and
8254 super-classes
8255 idlwave-keyword-class-inheritance
8256 (stringp class)
8257 (or
8258 system
8259 (assq (idlwave-sintern-keyword "_extra") keywords)
8260 (assq (idlwave-sintern-keyword "_ref_extra") keywords))
8261 ;; Check if one of the keyword-class regexps matches the name
8262 (let ((regexps idlwave-keyword-class-inheritance) re)
8263 (catch 'exit
8264 (while (setq re (pop regexps))
8265 (if (string-match re name) (throw 'exit t))))))
8266
8267 (loop for entry in (idlwave-routines) do
8268 (and (nth 2 entry) ; non-nil class
8269 (memq (nth 2 entry) super-classes) ; an inherited class
8270 (eq (nth 1 entry) type) ; correct type
8271 (eq (car entry) name) ; correct name
8272 (mapc (lambda (k) (add-to-list 'keywords k))
8273 (idlwave-entry-keywords entry 'do-link))))
8274 (setq keywords (idlwave-uniquify keywords)))
8275
8276 ;; Return the final list
8277 keywords))
8278
8279 (defun idlwave-expand-keyword (keyword module)
8280 "Expand KEYWORD to one of the valid keyword parameters of MODULE.
8281 KEYWORD may be an exact match or an abbreviation of a keyword.
8282 If the match is exact, KEYWORD itself is returned, even if there may be other
8283 keywords of which KEYWORD is an abbreviation. This is necessary because some
8284 system routines have keywords which are prefixes of other keywords.
8285 If KEYWORD is an abbreviation of several keywords, a list of all possible
8286 completions is returned.
8287 If the abbreviation was unique, the correct keyword is returned.
8288 If it cannot be a keyword, the function return nil.
8289 If we do not know about MODULE, just return KEYWORD literally."
8290 (let* ((name (car module))
8291 (type (nth 1 module))
8292 (class (nth 2 module))
8293 (kwd (idlwave-sintern-keyword keyword))
8294 (entry (idlwave-best-rinfo-assoc name type class (idlwave-routines)))
8295 (kwd-alist (idlwave-entry-keywords entry))
8296 (extra (or (assq (idlwave-sintern-keyword "_EXTRA") kwd-alist)
8297 (assq (idlwave-sintern-keyword "_REF_EXTRA") kwd-alist)))
8298 (completion-ignore-case t)
8299 candidates)
8300 (cond ((assq kwd kwd-alist)
8301 kwd)
8302 ((setq candidates (all-completions kwd kwd-alist))
8303 (if (= (length candidates) 1)
8304 (car candidates)
8305 candidates))
8306 ((and entry extra)
8307 ;; Inheritance may cause this keyword to be correct
8308 keyword)
8309 (entry
8310 ;; We do know the function, which does not have the keyword.
8311 nil)
8312 (t
8313 ;; We do not know the function, so this just might be a correct
8314 ;; keyword - return it as it is.
8315 keyword))))
8316
8317 (defvar idlwave-rinfo-mouse-map (make-sparse-keymap))
8318 (defvar idlwave-rinfo-map (make-sparse-keymap))
8319 (define-key idlwave-rinfo-mouse-map
8320 (if (featurep 'xemacs) [button2] [mouse-2])
8321 'idlwave-mouse-active-rinfo)
8322 (define-key idlwave-rinfo-mouse-map
8323 (if (featurep 'xemacs) [(shift button2)] [(shift mouse-2)])
8324 'idlwave-mouse-active-rinfo-shift)
8325 (define-key idlwave-rinfo-mouse-map
8326 (if (featurep 'xemacs) [button3] [mouse-3])
8327 'idlwave-mouse-active-rinfo-right)
8328 (define-key idlwave-rinfo-mouse-map " " 'idlwave-active-rinfo-space)
8329 (define-key idlwave-rinfo-map "q" 'idlwave-quit-help)
8330 (define-key idlwave-rinfo-mouse-map "q" 'idlwave-quit-help)
8331 (defvar idlwave-popup-source nil)
8332 (defvar idlwave-rinfo-marker (make-marker))
8333
8334 (defun idlwave-quit-help ()
8335 (interactive)
8336 (let ((ri-window (get-buffer-window "*Help*"))
8337 (olh-window (get-buffer-window "*IDLWAVE Help*")))
8338 (when (and olh-window
8339 (fboundp 'idlwave-help-quit))
8340 (select-window olh-window)
8341 (idlwave-help-quit))
8342 (when (window-live-p ri-window)
8343 (delete-window ri-window))))
8344
8345 (defun idlwave-display-calling-sequence (name type class
8346 &optional initial-class)
8347 ;; Display the calling sequence of module NAME, type TYPE in class CLASS.
8348 (let* ((initial-class (or initial-class class))
8349 (entry (or (idlwave-best-rinfo-assq name type class
8350 (idlwave-routines))
8351 (idlwave-rinfo-assq name type class
8352 idlwave-unresolved-routines)))
8353 (name (or (car entry) name))
8354 (class (or (nth 2 entry) class))
8355 (superclasses (idlwave-all-class-inherits initial-class))
8356 (twins (idlwave-routine-twins entry))
8357 (dtwins (idlwave-study-twins twins))
8358 (all dtwins)
8359 (system (eq (car (nth 3 entry)) 'system))
8360 (calling-seq (nth 4 entry))
8361 (keywords (idlwave-entry-keywords entry 'do-link))
8362 (html-file (car (nth 5 entry)))
8363 (help-echo-kwd
8364 "Button2: Insert KEYWORD (SHIFT=`/KEYWORD') | Button3: Online Help ")
8365 (help-echo-use
8366 "Button2/3: Online Help")
8367 (help-echo-src
8368 "Button2: Jump to source and back | Button3: Source in Help window.")
8369 (help-echo-class
8370 "Button2: Display info about same method in superclass")
8371 (col 0)
8372 (data (list name type class (current-buffer) nil initial-class))
8373 (km-prop (if (featurep 'xemacs) 'keymap 'local-map))
8374 (face 'idlwave-help-link)
8375 beg props win cnt total)
8376 ;; Fix keywords, but don't add chained super-classes, since these
8377 ;; are shown separately for that super-class
8378 (setq keywords (idlwave-fix-keywords name type class keywords))
8379 (cond
8380 ((null entry)
8381 (error "No %s %s known %s" type name
8382 (if initial-class (concat "in class " initial-class) "")))
8383 ((or (null name) (equal name ""))
8384 (error "No function or procedure call at point"))
8385 ((null calling-seq)
8386 (error "Calling sequence of %s %s not available" type name))
8387 (t
8388 (save-excursion
8389 (move-marker idlwave-rinfo-marker (point))
8390 (set-buffer (get-buffer-create "*Help*"))
8391 (use-local-map idlwave-rinfo-map)
8392 (setq buffer-read-only nil)
8393 (erase-buffer)
8394 (set (make-local-variable 'idlwave-popup-source) nil)
8395 (set (make-local-variable 'idlwave-current-obj_new-class)
8396 idlwave-current-obj_new-class)
8397 (when superclasses
8398 (setq props (list 'mouse-face 'highlight
8399 km-prop idlwave-rinfo-mouse-map
8400 'help-echo help-echo-class
8401 'data (cons 'class data)))
8402 (let ((classes (cons initial-class superclasses)) c)
8403 (insert "Classes: ")
8404 (while (setq c (pop classes))
8405 (insert " ")
8406 (setq beg (point))
8407 (insert c)
8408 (if (equal (downcase c) (downcase class))
8409 (add-text-properties beg (point) (list 'face 'bold))
8410 ;; If Method exists in a different class link it
8411 (if (idlwave-rinfo-assq name type c (idlwave-routines))
8412 (add-text-properties beg (point) props))))
8413 (insert "\n")))
8414 (setq props (list 'mouse-face 'highlight
8415 km-prop idlwave-rinfo-mouse-map
8416 'help-echo help-echo-use
8417 'data (cons 'usage data)))
8418 (if html-file (setq props (append (list 'face face 'link html-file)
8419 props)))
8420 (insert "Usage: ")
8421 (setq beg (point))
8422 (insert (if class
8423 (format calling-seq class name class name class name)
8424 (format calling-seq name name name name))
8425 "\n")
8426 (add-text-properties beg (point) props)
8427
8428 (insert "Keywords:")
8429 (if (null keywords)
8430 (insert " No keywords accepted.")
8431 (setq col 9)
8432 (mapc
8433 (lambda (x)
8434 (if (>= (+ col 1 (length (car x)))
8435 (window-width))
8436 (progn
8437 (insert "\n ")
8438 (setq col 9)))
8439 (insert " ")
8440 (setq beg (point)
8441 ;; Relevant keywords already have link property attached
8442 props (list 'mouse-face 'highlight
8443 km-prop idlwave-rinfo-mouse-map
8444 'data (cons 'keyword data)
8445 'help-echo help-echo-kwd
8446 'keyword (car x)))
8447 (if system (setq props (append (list 'face face) props)))
8448 (insert (car x))
8449 (add-text-properties beg (point) props)
8450 (setq col (+ col 1 (length (car x)))))
8451 keywords))
8452
8453 (setq cnt 1 total (length all))
8454 ;; Here entry is (key file (list of type-conses))
8455 (while (setq entry (pop all))
8456 (setq props (list 'mouse-face 'highlight
8457 km-prop idlwave-rinfo-mouse-map
8458 'help-echo help-echo-src
8459 'source (list (car (car (nth 2 entry))) ;type
8460 (nth 1 entry)
8461 nil
8462 (cdr (car (nth 2 entry))))
8463 'data (cons 'source data)))
8464 (idlwave-insert-source-location
8465 (format "\n%-8s %s"
8466 (if (equal cnt 1)
8467 (if (> total 1) "Sources:" "Source:")
8468 "")
8469 (if (> total 1) "- " ""))
8470 entry props)
8471 (incf cnt)
8472 (when (and all (> cnt idlwave-rinfo-max-source-lines))
8473 ;; No more source lines, please
8474 (insert (format
8475 "\n Source information truncated to %d entries."
8476 idlwave-rinfo-max-source-lines))
8477 (setq all nil)))
8478 (goto-char (point-min))
8479 (setq buffer-read-only t))
8480 (display-buffer "*Help*")
8481 (if (and (setq win (get-buffer-window "*Help*"))
8482 idlwave-resize-routine-help-window)
8483 (progn
8484 (let ((ww (selected-window)))
8485 (unwind-protect
8486 (progn
8487 (select-window win)
8488 (enlarge-window (- (/ (frame-height) 2)
8489 (window-height)))
8490 (shrink-window-if-larger-than-buffer))
8491 (select-window ww)))))))))
8492
8493 (defun idlwave-insert-source-location (prefix entry &optional file-props)
8494 "Insert a source location into the routine info buffer.
8495 Start line with PREFIX. If a file name is inserted, add FILE-PROPS
8496 to it."
8497 (let* ((key (car entry))
8498 (file (nth 1 entry))
8499 (types (nth 2 entry))
8500 (shell-flag (assq 'compiled types))
8501 (buffer-flag (assq 'buffer types))
8502 (user-flag (assq 'user types))
8503 (lib-flag (assq 'lib types))
8504 (ndupl (or (and buffer-flag (idlwave-count-memq 'buffer types))
8505 (and user-flag (idlwave-count-memq 'user types))
8506 (and lib-flag (idlwave-count-memq 'lib types))
8507 1))
8508 (doflags t)
8509 beg special)
8510
8511 (insert prefix)
8512
8513 (cond
8514 ((eq key 'system)
8515 (setq doflags nil)
8516 (insert "System "))
8517
8518 ((eq key 'builtin)
8519 (setq doflags nil)
8520 (insert "Builtin "))
8521
8522 ((and (not file) shell-flag)
8523 (insert "Unresolved"))
8524
8525 ((null file)
8526 (insert "ERROR"))
8527
8528 ((idlwave-syslib-p file)
8529 (if (string-match "obsolete" (file-name-directory file))
8530 (insert "Obsolete ")
8531 (insert "SystemLib ")))
8532
8533 ;; New special syntax: taken directly from routine-info for
8534 ;; library catalog routines
8535 ((setq special (or (cdr lib-flag) (cdr user-flag)))
8536 (insert (format "%-10s" special)))
8537
8538 ;; Old special syntax: a matching regexp
8539 ((setq special (idlwave-special-lib-test file))
8540 (insert (format "%-10s" special)))
8541
8542 ;; Catch-all with file
8543 ((idlwave-lib-p file) (insert "Library "))
8544
8545 ;; Sanity catch all
8546 (t (insert "Other ")))
8547
8548 (when doflags
8549 (insert (concat
8550 " ["
8551 (if lib-flag "L" "-")
8552 (if user-flag "C" "-")
8553 (if shell-flag "S" "-")
8554 (if buffer-flag "B" "-")
8555 "] ")))
8556 (when (> ndupl 1)
8557 (setq beg (point))
8558 (insert (format "(%dx) " ndupl))
8559 (add-text-properties beg (point) (list 'face 'bold)))
8560 (when (and file (not (equal file "")))
8561 (setq beg (point))
8562 (insert (apply 'abbreviate-file-name
8563 (if (featurep 'xemacs) (list file t) (list file))))
8564 (if file-props
8565 (add-text-properties beg (point) file-props)))))
8566
8567 (defun idlwave-special-lib-test (file)
8568 "Check the path of FILE against the regexps which define special libs.
8569 Return the name of the special lib if there is a match."
8570 (let ((alist idlwave-special-lib-alist)
8571 entry rtn)
8572 (cond
8573 ((stringp file)
8574 (while (setq entry (pop alist))
8575 (if (string-match (car entry) file)
8576 (setq rtn (cdr entry)
8577 alist nil)))
8578 rtn)
8579 (t nil))))
8580
8581 (defun idlwave-mouse-active-rinfo-right (ev)
8582 (interactive "e")
8583 (idlwave-mouse-active-rinfo ev 'right))
8584
8585 (defun idlwave-mouse-active-rinfo-shift (ev)
8586 (interactive "e")
8587 (idlwave-mouse-active-rinfo ev nil 'shift))
8588
8589 (defun idlwave-active-rinfo-space ()
8590 (interactive)
8591 (idlwave-mouse-active-rinfo nil 'right))
8592
8593 (defun idlwave-mouse-active-rinfo (ev &optional right shift)
8594 "Do the mouse actions in the routine info buffer.
8595 Optional args RIGHT and SHIFT indicate, if mouse-3 was used, and if SHIFT
8596 was pressed."
8597 (interactive "e")
8598 (if ev (mouse-set-point ev))
8599 (let (data id name type class buf bufwin source link keyword
8600 word initial-class)
8601 (setq data (get-text-property (point) 'data)
8602 source (get-text-property (point) 'source)
8603 keyword (get-text-property (point) 'keyword)
8604 link (get-text-property (point) 'link)
8605 id (car data)
8606 name (nth 1 data) type (nth 2 data) class (nth 3 data)
8607 buf (nth 4 data)
8608 initial-class (nth 6 data)
8609 word (idlwave-this-word)
8610 bufwin (get-buffer-window buf t))
8611
8612 (cond ((eq id 'class) ; Switch class being displayed
8613 (if (window-live-p bufwin) (select-window bufwin))
8614 (idlwave-display-calling-sequence
8615 (idlwave-sintern-method name)
8616 type (idlwave-sintern-class word)
8617 initial-class))
8618 ((eq id 'usage) ; Online help on this routine
8619 (idlwave-online-help link name type class))
8620 ((eq id 'source) ; Source in help or buffer
8621 (if right ; In help
8622 (let ((idlwave-extra-help-function 'idlwave-help-with-source)
8623 (idlwave-help-source-try-header nil)
8624 ;; Fake idlwave-routines so help will find the right entry
8625 (idlwave-routines
8626 (list (list name type class source ""))))
8627 (idlwave-help-get-special-help name type class nil))
8628 ;; Otherwise just pop to the source
8629 (setq idlwave-popup-source (not idlwave-popup-source))
8630 (if idlwave-popup-source
8631 (condition-case err
8632 (idlwave-do-find-module name type class source)
8633 (error
8634 (setq idlwave-popup-source nil)
8635 (if (window-live-p bufwin) (select-window bufwin))
8636 (error (nth 1 err))))
8637 (if bufwin
8638 (select-window bufwin)
8639 (pop-to-buffer buf))
8640 (goto-char (marker-position idlwave-rinfo-marker)))))
8641 ((eq id 'keyword)
8642 (if right
8643 (idlwave-online-help link name type class keyword)
8644 (idlwave-rinfo-insert-keyword keyword buf shift))))))
8645
8646 (defun idlwave-rinfo-insert-keyword (keyword buffer &optional shift)
8647 "Insert KEYWORD in BUFFER. Make sure buffer is displayed in a window."
8648 (let ((bwin (get-buffer-window buffer)))
8649 (if idlwave-complete-empty-string-as-lower-case
8650 (setq keyword (downcase keyword)))
8651 (if bwin
8652 (select-window bwin)
8653 (pop-to-buffer buffer)
8654 (setq bwin (get-buffer-window buffer)))
8655 (if (eq (preceding-char) ?/)
8656 (insert keyword)
8657 (unless (save-excursion
8658 (re-search-backward
8659 "[(,][ \t]*\\(\\$[ \t]*\\(;.*\\)?\n\\)?[ \t]*\\="
8660 (min (- (point) 100) (point-min)) t))
8661 (insert ", "))
8662 (if shift (insert "/"))
8663 (insert keyword)
8664 (if (and (not shift)
8665 idlwave-keyword-completion-adds-equal)
8666 (insert "=")))))
8667
8668 (defun idlwave-list-buffer-load-path-shadows (&optional arg)
8669 "List the load path shadows of all routines defined in current buffer."
8670 (interactive "P")
8671 (idlwave-routines)
8672 (if (eq major-mode 'idlwave-mode)
8673 (idlwave-list-load-path-shadows
8674 nil (idlwave-update-current-buffer-info 'save-buffer)
8675 "in current buffer")
8676 (error "Current buffer is not in idlwave-mode")))
8677
8678 (defun idlwave-list-shell-load-path-shadows (&optional arg)
8679 "List the load path shadows of all routines compiled under the shell.
8680 This is very useful for checking an IDL application. Just compile the
8681 application, do RESOLVE_ALL, and `C-c C-i' to compile all referenced
8682 routines and update IDLWAVE internal info. Then check for shadowing
8683 with this command."
8684 (interactive "P")
8685 (cond
8686 ((or (not (fboundp 'idlwave-shell-is-running))
8687 (not (idlwave-shell-is-running)))
8688 (error "Shell is not running"))
8689 ((null idlwave-compiled-routines)
8690 (error "No compiled routines. Maybe you need to update with `C-c C-i'"))
8691 (t
8692 (idlwave-list-load-path-shadows nil idlwave-compiled-routines
8693 "in the shell"))))
8694
8695 (defun idlwave-list-all-load-path-shadows (&optional arg)
8696 "List the load path shadows of all routines known to IDLWAVE."
8697 (interactive "P")
8698 (idlwave-list-load-path-shadows nil nil "globally"))
8699
8700 (defun idlwave-list-load-path-shadows (arg &optional special-routines loc)
8701 "List the routines which are defined multiple times.
8702 Search the information IDLWAVE has about IDL routines for multiple
8703 definitions.
8704 When SPECIAL-ROUTINES in non-nil, only look for shadows of these routines.
8705
8706 When IDL hits a routine call which is not defined, it will search on
8707 the load path in order to find a definition. The output of this command
8708 can be used to detect possible name clashes during this process."
8709 (idlwave-routines) ; Make sure everything is loaded.
8710 (unless (or idlwave-user-catalog-routines idlwave-library-catalog-routines)
8711 (or (y-or-n-p
8712 "You don't have any user or library catalogs. Continue anyway? ")
8713 (error "Abort")))
8714 (let* ((routines (append idlwave-system-routines
8715 idlwave-compiled-routines
8716 idlwave-library-catalog-routines
8717 idlwave-user-catalog-routines
8718 idlwave-buffer-routines
8719 nil))
8720 (km-prop (if (featurep 'xemacs) 'keymap 'local-map))
8721 (keymap (make-sparse-keymap))
8722 (props (list 'mouse-face 'highlight
8723 km-prop keymap
8724 'help-echo "Mouse2: Find source"))
8725 (nroutines (length (or special-routines routines)))
8726 (step (/ nroutines 100))
8727 (n 0)
8728 (cnt 0)
8729 (idlwave-sort-prefer-buffer-info nil)
8730 routine twins dtwins twin done props1 lroutines)
8731
8732 (if special-routines
8733 ;; Just looking for shadows of a few special routines
8734 (setq lroutines routines
8735 routines special-routines))
8736
8737 (message "Sorting routines...")
8738 (setq routines (sort routines
8739 (lambda (a b)
8740 (string< (downcase (idlwave-make-full-name
8741 (nth 2 a) (car a)))
8742 (downcase (idlwave-make-full-name
8743 (nth 2 b) (car b)))))))
8744 (message "Sorting routines...done")
8745
8746 (define-key keymap (if (featurep 'xemacs) [(button2)] [(mouse-2)])
8747 (lambda (ev)
8748 (interactive "e")
8749 (mouse-set-point ev)
8750 (apply 'idlwave-do-find-module
8751 (get-text-property (point) 'find-args))))
8752 (define-key keymap [(return)]
8753 (lambda ()
8754 (interactive)
8755 (apply 'idlwave-do-find-module
8756 (get-text-property (point) 'find-args))))
8757 (message "Compiling list...( 0%%)")
8758 (save-excursion
8759 (set-buffer (get-buffer-create "*Shadows*"))
8760 (setq buffer-read-only nil)
8761 (erase-buffer)
8762 (while (setq routine (pop routines))
8763 (if (= (mod (setq n (1+ n)) step) 0)
8764 (message "Compiling list...(%2d%%)" (/ (* n 100) nroutines)))
8765
8766 ;; Get a list of all twins
8767 (setq twins (idlwave-routine-twins routine (or lroutines routines)))
8768 (if (memq routine done)
8769 (setq dtwins nil)
8770 (setq dtwins (idlwave-study-twins twins)))
8771 ;; Mark all twins as dealt with
8772 (setq done (append twins done))
8773 (when (or (> (length dtwins) 1)
8774 (> (idlwave-count-memq 'lib (nth 2 (car dtwins))) 1)
8775 (> (idlwave-count-memq 'user (nth 2 (car dtwins))) 1)
8776 (> (idlwave-count-memq 'buffer (nth 2 (car dtwins))) 1))
8777 (incf cnt)
8778 (insert (format "\n%s%s"
8779 (idlwave-make-full-name (nth 2 routine)
8780 (car routine))
8781 (if (eq (nth 1 routine) 'fun) "()" "")))
8782 (while (setq twin (pop dtwins))
8783 (setq props1 (append (list 'find-args
8784 (list (nth 0 routine)
8785 (nth 1 routine)
8786 (nth 2 routine)))
8787 props))
8788 (idlwave-insert-source-location "\n - " twin props1))))
8789 (goto-char (point-min))
8790 (setq buffer-read-only t))
8791 (setq loc (or loc ""))
8792 (if (> cnt 0)
8793 (progn
8794 (display-buffer (get-buffer "*Shadows*"))
8795 (message "%d case%s of shadowing found %s"
8796 cnt (if (= cnt 1) "" "s") loc))
8797 (message "No shadowing conflicts found %s" loc))))
8798
8799 (defun idlwave-print-source (routine)
8800 (let* ((source (nth 3 routine))
8801 (stype (car source))
8802 (sfile (idlwave-routine-source-file source)))
8803 (if (idlwave-syslib-p sfile) (setq stype 'syslib))
8804 (if (and (eq stype 'compiled)
8805 (or (not (stringp sfile))
8806 (not (string-match "\\S-" sfile))))
8807 (setq stype 'unresolved))
8808 (princ (format " %-10s %s\n"
8809 stype
8810 (if sfile sfile "No source code available")))))
8811
8812 (defun idlwave-routine-twins (entry &optional list)
8813 "Return all twin entries of ENTRY in LIST.
8814 LIST defaults to `idlwave-routines'.
8815 Twin entries are those which have the same name, type, and class.
8816 ENTRY will also be returned, as the first item of this list."
8817 (let* ((name (car entry))
8818 (type (nth 1 entry))
8819 (class (nth 2 entry))
8820 (candidates (idlwave-all-assq name (or list (idlwave-routines))))
8821 twins candidate)
8822 (while (setq candidate (pop candidates))
8823 (if (and (not (eq candidate entry))
8824 (eq type (nth 1 candidate))
8825 (eq class (nth 2 candidate)))
8826 (push candidate twins)))
8827 (if (setq candidate (idlwave-rinfo-assq name type class
8828 idlwave-unresolved-routines))
8829 (push candidate twins))
8830 (cons entry (nreverse twins))))
8831
8832 (defun idlwave-study-twins (entries)
8833 "Return dangerous twins of first entry in ENTRIES.
8834 Dangerous twins are routines with same name, but in different files on
8835 the load path. If a file is in the system library and has an entry in
8836 the `idlwave-system-routines' list, we omit the latter as
8837 non-dangerous because many IDL routines are implemented as library
8838 routines, and may have been scanned."
8839 (let* ((entry (car entries))
8840 (name (car entry)) ;
8841 (type (nth 1 entry)) ; Must be bound for
8842 (class (nth 2 entry)) ; idlwave-routine-twin-compare
8843 (cnt 0)
8844 source type type-cons file alist syslibp key)
8845 (while (setq entry (pop entries))
8846 (incf cnt)
8847 (setq source (nth 3 entry)
8848 type (car source)
8849 type-cons (cons type (nth 3 source))
8850 file (idlwave-routine-source-file source))
8851
8852 ;; Make KEY to index entry properly
8853 (setq key (cond ((eq type 'system) type)
8854 (file (file-truename file))
8855 (t 'unresolved)))
8856
8857 ;; Check for an entry in the system library
8858 (if (and file
8859 (not syslibp)
8860 (idlwave-syslib-p file))
8861 (setq syslibp t))
8862
8863 ;; If there's more than one matching entry for the same file, just
8864 ;; append the type-cons to the type list.
8865 (if (setq entry (assoc key alist))
8866 (push type-cons (nth 2 entry))
8867 (push (list key file (list type-cons)) alist)))
8868
8869 (setq alist (nreverse alist))
8870
8871 (when syslibp
8872 ;; File is in system *library* - remove any 'system entry
8873 (setq alist (delq (assq 'system alist) alist)))
8874
8875 ;; If 'system remains and we've scanned the syslib, it's a builtin
8876 ;; (rather than a !DIR/lib/.pro file bundled as source).
8877 (when (and (idlwave-syslib-scanned-p)
8878 (setq entry (assoc 'system alist)))
8879 (setcar entry 'builtin))
8880 (sort alist 'idlwave-routine-twin-compare)))
8881
8882 (defvar type)
8883 (defvar class)
8884 (defvar idlwave-sort-prefer-buffer-info t
8885 "Internal variable used to influence `idlwave-routine-twin-compare'.")
8886
8887 (defmacro idlwave-xor (a b)
8888 `(and (or ,a ,b)
8889 (not (and ,a ,b))))
8890
8891 (defun idlwave-routine-entry-compare (a b)
8892 "Compare two routine info entries for sorting.
8893 This is the general case. It first compares class, names, and type.
8894 If it turns out that A and B are twins (same name, class, and type),
8895 calls another routine which compares twins on the basis of their file
8896 names and path locations."
8897 (let ((name (car a)) (type (nth 1 a)) (class (nth 2 a)))
8898 (cond
8899 ((not (equal (idlwave-downcase-safe class)
8900 (idlwave-downcase-safe (nth 2 b))))
8901 ;; Class decides
8902 (cond ((null (nth 2 b)) nil)
8903 ((null class) t)
8904 (t (string< (downcase class) (downcase (nth 2 b))))))
8905 ((not (equal (downcase name) (downcase (car b))))
8906 ;; Name decides
8907 (string< (downcase name) (downcase (car b))))
8908 ((not (eq type (nth 1 b)))
8909 ;; Type decides
8910 (< (if (eq type 'fun) 1 0) (if (eq (nth 1 b) 'fun) 1 0)))
8911 (t
8912 ;; A and B are twins - so the decision is more complicated.
8913 ;; Call twin-compare with the proper arguments.
8914 (idlwave-routine-entry-compare-twins a b)))))
8915
8916 (defun idlwave-routine-entry-compare-twins (a b)
8917 "Compare two routine entries, under the assumption that they are twins.
8918 This basically calls `idlwave-routine-twin-compare' with the correct args."
8919 (let* ((name (car a)) (type (nth 1 a)) (class (nth 2 a)) ; needed outside
8920 (asrc (nth 3 a))
8921 (atype (car asrc))
8922 (bsrc (nth 3 b))
8923 (btype (car bsrc))
8924 (afile (idlwave-routine-source-file asrc))
8925 (bfile (idlwave-routine-source-file bsrc)))
8926 (idlwave-routine-twin-compare
8927 (if (stringp afile)
8928 (list (file-truename afile) afile (list atype))
8929 (list atype afile (list atype)))
8930 (if (stringp bfile)
8931 (list (file-truename bfile) bfile (list btype))
8932 (list btype bfile (list btype))))
8933 ))
8934
8935 (defun idlwave-routine-twin-compare (a b)
8936 "Compare two routine twin entries for sorting.
8937 In here, A and B are not normal routine info entries, but special
8938 lists (KEY FILENAME (TYPES...)).
8939 This expects NAME TYPE CLASS to be bound to the right values."
8940 (let* (;; Dis-assemble entries
8941 (akey (car a)) (bkey (car b))
8942 (afile (nth 1 a)) (bfile (nth 1 b))
8943 (atypes (nth 2 a)) (btypes (nth 2 b))
8944 ;; System routines?
8945 (asysp (memq akey '(builtin system)))
8946 (bsysp (memq bkey '(builtin system)))
8947 ;; Compiled routines?
8948 (acompp (memq 'compiled atypes))
8949 (bcompp (memq 'compiled btypes))
8950 ;; Unresolved?
8951 (aunresp (or (eq akey 'unresolved)
8952 (and acompp (not afile))))
8953 (bunresp (or (eq bkey 'unresolved)
8954 (and bcompp (not bfile))))
8955 ;; Buffer info available?
8956 (abufp (memq 'buffer atypes))
8957 (bbufp (memq 'buffer btypes))
8958 ;; On search path?
8959 (tpath-alist (idlwave-true-path-alist))
8960 (apathp (and (stringp akey)
8961 (assoc (file-name-directory akey) tpath-alist)))
8962 (bpathp (and (stringp bkey)
8963 (assoc (file-name-directory bkey) tpath-alist)))
8964 ;; How early on search path? High number means early since we
8965 ;; measure the tail of the path list
8966 (anpath (length (memq apathp tpath-alist)))
8967 (bnpath (length (memq bpathp tpath-alist)))
8968 ;; Look at file names
8969 (aname (if (stringp afile) (downcase (file-name-nondirectory afile)) ""))
8970 (bname (if (stringp bfile) (downcase (file-name-nondirectory bfile)) ""))
8971 (fname-re (if class (format "\\`%s__\\(%s\\|define\\)\\.pro\\'"
8972 (regexp-quote (downcase class))
8973 (regexp-quote (downcase name)))
8974 (format "\\`%s\\.pro" (regexp-quote (downcase name)))))
8975 ;; Is file name derived from the routine name?
8976 ;; Method file or class definition file?
8977 (anamep (string-match fname-re aname))
8978 (adefp (and class anamep (string= "define" (match-string 1 aname))))
8979 (bnamep (string-match fname-re bname))
8980 (bdefp (and class bnamep (string= "define" (match-string 1 bname)))))
8981
8982 ;; Now: follow JD's ideas about sorting. Looks really simple now,
8983 ;; doesn't it? The difficult stuff is hidden above...
8984 (cond
8985 ((idlwave-xor asysp bsysp) asysp) ; System entries first
8986 ((idlwave-xor aunresp bunresp) bunresp) ; Unresolved last
8987 ((and idlwave-sort-prefer-buffer-info
8988 (idlwave-xor abufp bbufp)) abufp) ; Buffers before non-buffers
8989 ((idlwave-xor acompp bcompp) acompp) ; Compiled entries
8990 ((idlwave-xor apathp bpathp) apathp) ; Library before non-library
8991 ((idlwave-xor anamep bnamep) anamep) ; Correct file names first
8992 ((and class anamep bnamep ; both file names match ->
8993 (idlwave-xor adefp bdefp)) bdefp) ; __define after __method
8994 ((> anpath bnpath) t) ; Who is first on path?
8995 (t nil)))) ; Default
8996
8997 (defun idlwave-routine-source-file (source)
8998 (if (nth 2 source)
8999 (expand-file-name (nth 1 source) (nth 2 source))
9000 (nth 1 source)))
9001
9002 (defun idlwave-downcase-safe (string)
9003 "Donwcase if string, else return unchanged."
9004 (if (stringp string)
9005 (downcase string)
9006 string))
9007
9008 (defun idlwave-count-eq (elt list)
9009 "How often is ELT in LIST?"
9010 (length (delq nil (mapcar (lambda (x) (eq x elt)) list))))
9011
9012 (defun idlwave-count-memq (elt alist)
9013 "How often is ELT a key in ALIST?"
9014 (length (delq nil (mapcar (lambda (x) (eq (car x) elt)) alist))))
9015
9016 (defun idlwave-syslib-p (file)
9017 "Non-nil if FILE is in the system library."
9018 (let* ((true-syslib (file-name-as-directory
9019 (file-truename
9020 (expand-file-name "lib" (idlwave-sys-dir)))))
9021 (true-file (file-truename file)))
9022 (string-match (concat "^" (regexp-quote true-syslib)) true-file)))
9023
9024 (defun idlwave-lib-p (file)
9025 "Non-nil if FILE is in the library."
9026 (let ((true-dir (file-name-directory (file-truename file))))
9027 (assoc true-dir (idlwave-true-path-alist))))
9028
9029 (defun idlwave-path-alist-add-flag (list-entry flag)
9030 "Add a flag to the path list entry, if not set."
9031 (let ((flags (cdr list-entry)))
9032 (add-to-list 'flags flag)
9033 (setcdr list-entry flags)))
9034
9035 (defun idlwave-path-alist-remove-flag (list-entry flag)
9036 "Remove a flag to the path list entry, if set."
9037 (let ((flags (delq flag (cdr list-entry))))
9038 (setcdr list-entry flags)))
9039
9040 (defun idlwave-true-path-alist ()
9041 "Return `idlwave-path-alist' alist with true-names.
9042 Info is cached, but relies on the functions setting `idlwave-path-alist'
9043 to reset the variable `idlwave-true-path-alist' to nil."
9044 (or idlwave-true-path-alist
9045 (setq idlwave-true-path-alist
9046 (mapcar (lambda(x) (cons
9047 (file-name-as-directory
9048 (file-truename
9049 (directory-file-name
9050 (car x))))
9051 (cdr x)))
9052 idlwave-path-alist))))
9053
9054 (defun idlwave-syslib-scanned-p ()
9055 "Non-nil if the system lib file !DIR/lib has been scanned."
9056 (let* ((true-syslib (file-name-as-directory
9057 (file-truename
9058 (expand-file-name "lib" (idlwave-sys-dir))))))
9059 (cdr (assoc true-syslib (idlwave-true-path-alist)))))
9060
9061 ;; ----------------------------------------------------------------------------
9062 ;;
9063 ;; Online Help display
9064
9065
9066 ;; ----------------------------------------------------------------------------
9067 ;;
9068 ;; Additions for use with imenu.el and func-menu.el
9069 ;; (pop-up a list of IDL units in the current file).
9070 ;;
9071
9072 (defun idlwave-prev-index-position ()
9073 "Search for the previous procedure or function.
9074 Return nil if not found. For use with imenu.el."
9075 (save-match-data
9076 (cond
9077 ((idlwave-find-key "\\<\\(pro\\|function\\)\\>" -1 'nomark))
9078 ;; ((idlwave-find-key idlwave-begin-unit-reg 1 'nomark)
9079 (t nil))))
9080
9081 (defun idlwave-unit-name ()
9082 "Return the unit name.
9083 Assumes that point is at the beginning of the unit as found by
9084 `idlwave-prev-index-position'."
9085 (forward-sexp 2)
9086 (forward-sexp -1)
9087 (let ((begin (point)))
9088 (re-search-forward
9089 "[a-zA-Z_][a-zA-Z0-9$_]+\\(::[a-zA-Z_][a-zA-Z0-9$_]+\\)?")
9090 (if (fboundp 'buffer-substring-no-properties)
9091 (buffer-substring-no-properties begin (point))
9092 (buffer-substring begin (point)))))
9093
9094 (defalias 'idlwave-function-menu
9095 (condition-case nil
9096 (progn
9097 (require 'func-menu)
9098 'function-menu)
9099 (error (condition-case nil
9100 (progn
9101 (require 'imenu)
9102 'imenu)
9103 (error nil)))))
9104
9105 ;; Here we hack func-menu.el in order to support this new mode.
9106 ;; The latest versions of func-menu.el already have this stuff in, so
9107 ;; we hack only if it is not already there.
9108 (when (fboundp 'eval-after-load)
9109 (eval-after-load "func-menu"
9110 '(progn
9111 (or (assq 'idlwave-mode fume-function-name-regexp-alist)
9112 (not (boundp 'fume-function-name-regexp-idl)) ; avoid problems
9113 (setq fume-function-name-regexp-alist
9114 (cons '(idlwave-mode . fume-function-name-regexp-idl)
9115 fume-function-name-regexp-alist)))
9116 (or (assq 'idlwave-mode fume-find-function-name-method-alist)
9117 (not (fboundp 'fume-find-next-idl-function-name)) ; avoid problems
9118 (setq fume-find-function-name-method-alist
9119 (cons '(idlwave-mode . fume-find-next-idl-function-name)
9120 fume-find-function-name-method-alist))))))
9121
9122 (defun idlwave-edit-in-idlde ()
9123 "Edit the current file in IDL Development environment."
9124 (interactive)
9125 (start-process "idldeclient" nil
9126 idlwave-shell-explicit-file-name "-c" "-e"
9127 (buffer-file-name)))
9128
9129 (defvar idlwave-help-use-assistant)
9130 (defun idlwave-launch-idlhelp ()
9131 "Start the IDLhelp application."
9132 (interactive)
9133 (if idlwave-help-use-assistant
9134 (idlwave-help-assistant-raise)
9135 (start-process "idlhelp" nil idlwave-help-application)))
9136
9137 ;; Menus - using easymenu.el
9138 (defvar idlwave-mode-menu-def
9139 `("IDLWAVE"
9140 ["PRO/FUNC menu" idlwave-function-menu t]
9141 ("Motion"
9142 ["Subprogram Start" idlwave-beginning-of-subprogram t]
9143 ["Subprogram End" idlwave-end-of-subprogram t]
9144 ["Block Start" idlwave-beginning-of-block t]
9145 ["Block End" idlwave-end-of-block t]
9146 ["Up Block" idlwave-backward-up-block t]
9147 ["Down Block" idlwave-down-block t]
9148 ["Skip Block Backward" idlwave-backward-block t]
9149 ["Skip Block Forward" idlwave-forward-block t])
9150 ("Mark"
9151 ["Subprogram" idlwave-mark-subprogram t]
9152 ["Block" idlwave-mark-block t]
9153 ["Header" idlwave-mark-doclib t])
9154 ("Format"
9155 ["Indent Entire Statement" idlwave-indent-statement
9156 :active t :keys "C-u \\[indent-for-tab-command]" ]
9157 ["Indent Subprogram" idlwave-indent-subprogram t]
9158 ["(Un)Comment Region" idlwave-toggle-comment-region t]
9159 ["Continue/Split line" idlwave-split-line t]
9160 "--"
9161 ["Toggle Auto Fill" idlwave-auto-fill-mode :style toggle
9162 :selected (symbol-value idlwave-fill-function)])
9163 ("Templates"
9164 ["Procedure" idlwave-procedure t]
9165 ["Function" idlwave-function t]
9166 ["Doc Header" idlwave-doc-header t]
9167 ["Log" idlwave-doc-modification t]
9168 "--"
9169 ["Case" idlwave-case t]
9170 ["For" idlwave-for t]
9171 ["Repeat" idlwave-repeat t]
9172 ["While" idlwave-while t]
9173 "--"
9174 ["Close Block" idlwave-close-block t])
9175 ("Completion"
9176 ["Complete" idlwave-complete t]
9177 ("Complete Specific"
9178 ["1 Procedure Name" (idlwave-complete 'procedure) t]
9179 ["2 Procedure Keyword" (idlwave-complete 'procedure-keyword) t]
9180 "--"
9181 ["3 Function Name" (idlwave-complete 'function) t]
9182 ["4 Function Keyword" (idlwave-complete 'function-keyword) t]
9183 "--"
9184 ["5 Procedure Method Name" (idlwave-complete 'procedure-method) t]
9185 ["6 Procedure Method Keyword" (idlwave-complete 'procedure-method-keyword) t]
9186 "--"
9187 ["7 Function Method Name" (idlwave-complete 'function-method) t]
9188 ["8 Function Method Keyword" (idlwave-complete 'function-method-keyword) t]
9189 "--"
9190 ["9 Class Name" idlwave-complete-class t]))
9191 ("Routine Info"
9192 ["Show Routine Info" idlwave-routine-info t]
9193 ["Online Context Help" idlwave-context-help t]
9194 "--"
9195 ["Find Routine Source" idlwave-find-module t]
9196 ["Resolve Routine" idlwave-resolve (featurep 'idlw-shell)]
9197 "--"
9198 ["Update Routine Info" idlwave-update-routine-info t]
9199 ["Rescan XML Help Catalog" idlwave-convert-xml-system-routine-info t]
9200 "--"
9201 "IDL User Catalog"
9202 ["Select Catalog Directories" (idlwave-create-user-catalog-file nil) t]
9203 ["Scan Directories" (idlwave-update-routine-info '(16))
9204 (and idlwave-path-alist (not idlwave-catalog-process))]
9205 ["Scan Directories &" (idlwave-update-routine-info '(64))
9206 (and idlwave-path-alist (not idlwave-catalog-process))]
9207 "--"
9208 "Routine Shadows"
9209 ["Check Current Buffer" idlwave-list-buffer-load-path-shadows t]
9210 ["Check Compiled Routines" idlwave-list-shell-load-path-shadows t]
9211 ["Check Everything" idlwave-list-all-load-path-shadows t])
9212 ("Misc"
9213 ["Kill auto-created buffers" idlwave-kill-autoloaded-buffers t]
9214 "--"
9215 ["Insert TAB character" idlwave-hard-tab t])
9216 "--"
9217 ("External"
9218 ["Start IDL shell" idlwave-shell t]
9219 ["Edit file in IDLDE" idlwave-edit-in-idlde t]
9220 ["Launch IDL Help" idlwave-launch-idlhelp t])
9221 "--"
9222 ("Customize"
9223 ["Browse IDLWAVE Group" idlwave-customize t]
9224 "--"
9225 ["Build Full Customize Menu" idlwave-create-customize-menu
9226 (fboundp 'customize-menu-create)])
9227 ("Documentation"
9228 ["Describe Mode" describe-mode t]
9229 ["Abbreviation List" idlwave-list-abbrevs t]
9230 "--"
9231 ["Commentary in idlwave.el" idlwave-show-commentary t]
9232 ["Commentary in idlw-shell.el" idlwave-shell-show-commentary t]
9233 "--"
9234 ["Info" idlwave-info t]
9235 "--"
9236 ["Help with Topic" idlwave-help-assistant-help-with-topic
9237 idlwave-help-use-assistant]
9238 ["Launch IDL Help" idlwave-launch-idlhelp t])))
9239
9240 (defvar idlwave-mode-debug-menu-def
9241 '("Debug"
9242 ["Start IDL shell" idlwave-shell t]
9243 ["Save and .RUN buffer" idlwave-shell-save-and-run
9244 (and (boundp 'idlwave-shell-automatic-start)
9245 idlwave-shell-automatic-start)]))
9246
9247 (if (or (featurep 'easymenu) (load "easymenu" t))
9248 (progn
9249 (easy-menu-define idlwave-mode-menu idlwave-mode-map
9250 "IDL and WAVE CL editing menu"
9251 idlwave-mode-menu-def)
9252 (easy-menu-define idlwave-mode-debug-menu idlwave-mode-map
9253 "IDL and WAVE CL editing menu"
9254 idlwave-mode-debug-menu-def)))
9255
9256 (defun idlwave-customize ()
9257 "Call the customize function with `idlwave' as argument."
9258 (interactive)
9259 ;; Try to load the code for the shell, so that we can customize it
9260 ;; as well.
9261 (or (featurep 'idlw-shell)
9262 (load "idlw-shell" t))
9263 (customize-browse 'idlwave))
9264
9265 (defun idlwave-create-customize-menu ()
9266 "Create a full customization menu for IDLWAVE, insert it into the menu."
9267 (interactive)
9268 (if (fboundp 'customize-menu-create)
9269 (progn
9270 ;; Try to load the code for the shell, so that we can customize it
9271 ;; as well.
9272 (or (featurep 'idlw-shell)
9273 (load "idlw-shell" t))
9274 (easy-menu-change
9275 '("IDLWAVE") "Customize"
9276 `(["Browse IDLWAVE group" idlwave-customize t]
9277 "--"
9278 ,(customize-menu-create 'idlwave)
9279 ["Set" Custom-set t]
9280 ["Save" Custom-save t]
9281 ["Reset to Current" Custom-reset-current t]
9282 ["Reset to Saved" Custom-reset-saved t]
9283 ["Reset to Standard Settings" Custom-reset-standard t]))
9284 (message "\"IDLWAVE\"-menu now contains full customization menu"))
9285 (error "Cannot expand menu (outdated version of cus-edit.el)")))
9286
9287 (defun idlwave-show-commentary ()
9288 "Use the finder to view the file documentation from `idlwave.el'."
9289 (interactive)
9290 (finder-commentary "idlwave.el"))
9291
9292 (defun idlwave-shell-show-commentary ()
9293 "Use the finder to view the file documentation from `idlw-shell.el'."
9294 (interactive)
9295 (finder-commentary "idlw-shell.el"))
9296
9297 (defun idlwave-info ()
9298 "Read documentation for IDLWAVE in the info system."
9299 (interactive)
9300 (info "idlwave"))
9301
9302 (defun idlwave-list-abbrevs (arg)
9303 "Show the code abbreviations define in IDLWAVE mode.
9304 This lists all abbrevs where the replacement text differs from the input text.
9305 These are the ones the users want to learn to speed up their writing.
9306
9307 The function does *not* list abbrevs which replace a word with itself
9308 to call a hook. These hooks are used to change the case of words or
9309 to blink the matching `begin', and the user does not need to know them.
9310
9311 With arg, list all abbrevs with the corresponding hook.
9312
9313 This function was written since `list-abbrevs' looks terrible for IDLWAVE mode."
9314
9315 (interactive "P")
9316 (let ((table (symbol-value 'idlwave-mode-abbrev-table))
9317 abbrevs
9318 str rpl func fmt (len-str 0) (len-rpl 0))
9319 (mapatoms
9320 (lambda (sym)
9321 (if (symbol-value sym)
9322 (progn
9323 (setq str (symbol-name sym)
9324 rpl (symbol-value sym)
9325 func (symbol-function sym))
9326 (if arg
9327 (setq func (prin1-to-string func))
9328 (if (and (listp func) (stringp (nth 2 func)))
9329 (setq rpl (concat "EVAL: " (nth 2 func))
9330 func "")
9331 (setq func "")))
9332 (if (or arg (not (string= rpl str)))
9333 (progn
9334 (setq len-str (max len-str (length str)))
9335 (setq len-rpl (max len-rpl (length rpl)))
9336 (setq abbrevs (cons (list str rpl func) abbrevs)))))))
9337 table)
9338 ;; sort the list
9339 (setq abbrevs (sort abbrevs (lambda (a b) (string< (car a) (car b)))))
9340 ;; Make the format
9341 (setq fmt (format "%%-%ds %%-%ds %%s\n" len-str len-rpl))
9342 (with-output-to-temp-buffer "*Help*"
9343 (if arg
9344 (progn
9345 (princ "Abbreviations and Actions in IDLWAVE-Mode\n")
9346 (princ "=========================================\n\n")
9347 (princ (format fmt "KEY" "REPLACE" "HOOK"))
9348 (princ (format fmt "---" "-------" "----")))
9349 (princ "Code Abbreviations and Templates in IDLWAVE-Mode\n")
9350 (princ "================================================\n\n")
9351 (princ (format fmt "KEY" "ACTION" ""))
9352 (princ (format fmt "---" "------" "")))
9353 (mapcar
9354 (lambda (list)
9355 (setq str (car list)
9356 rpl (nth 1 list)
9357 func (nth 2 list))
9358 (princ (format fmt str rpl func)))
9359 abbrevs)))
9360 ;; Make sure each abbreviation uses only one display line
9361 (save-excursion
9362 (set-buffer "*Help*")
9363 (setq truncate-lines t)))
9364
9365 ;; Add .pro files to speedbar for support, if it's loaded
9366 (eval-after-load "speedbar" '(speedbar-add-supported-extension ".pro"))
9367
9368 ;; Set an idle timer to load the routine info.
9369 ;; Will only work on systems which support this.
9370 (or idlwave-routines (idlwave-start-load-rinfo-timer))
9371
9372 ;; Run the hook
9373 (run-hooks 'idlwave-load-hook)
9374
9375 (provide 'idlwave)
9376
9377 ;; arch-tag: f77f3b0c-c37c-424f-a328-0886fd42b6fb
9378 ;;; idlwave.el ends here