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