]> code.delx.au - gnu-emacs/blob - lisp/progmodes/idlwave.el
Merge from emacs--devo--0
[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 ;; Free Software Foundation, Inc.
5
6 ;; Authors: J.D. Smith <jdsmith@as.arizona.edu>
7 ;; Carsten Dominik <dominik@science.uva.nl>
8 ;; Chris Chase <chase@att.com>
9 ;; Maintainer: J.D. Smith <jdsmith@as.arizona.edu>
10 ;; Version: 6.1_em22
11 ;; Keywords: languages
12
13 ;; This file is part of GNU Emacs.
14
15 ;; GNU Emacs is free software; you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; 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; see the file COPYING. If not, write to the
27 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
28 ;; Boston, MA 02110-1301, USA.
29
30 ;;; Commentary:
31
32 ;; IDLWAVE enables feature-rich development and interaction with IDL,
33 ;; the Interactive Data Language. It provides a compelling,
34 ;; full-featured alternative to the IDLDE development environment
35 ;; bundled with IDL.
36
37 ;; In the remotely distant past, based on pascal.el, though bears
38 ;; little resemblance to it now.
39 ;;
40 ;; Incorporates many ideas, such as abbrevs, action routines, and
41 ;; continuation line indenting, from wave.el.
42 ;; wave.el original written by Lubos Pochman, Precision Visuals, Boulder.
43 ;;
44 ;; See the mode description ("C-h m" in idlwave-mode or "C-h f idlwave-mode")
45 ;; for features, key bindings, and info.
46 ;; Also, Info format documentation is available with `M-x idlwave-info'
47 ;;
48 ;; New versions of IDLWAVE, documentation, and more information
49 ;; available from:
50 ;; http://idlwave.org
51 ;;
52 ;; INSTALLATION
53 ;; ============
54 ;;
55 ;; Follow the instructions in the INSTALL file of the distribution.
56 ;; In short, put this file on your load path and add the following
57 ;; lines to your .emacs file:
58 ;;
59 ;; (autoload 'idlwave-mode "idlwave" "IDLWAVE Mode" t)
60 ;; (autoload 'idlwave-shell "idlw-shell" "IDLWAVE Shell" t)
61 ;; (setq auto-mode-alist (cons '("\\.pro\\'" . idlwave-mode) auto-mode-alist))
62 ;;
63 ;;
64 ;; SOURCE
65 ;; ======
66 ;;
67 ;; The newest version of this file is available from the maintainer's
68 ;; Webpage:
69 ;;
70 ;; http://idlwave.org
71 ;;
72 ;; DOCUMENTATION
73 ;; =============
74 ;;
75 ;; IDLWAVE is documented online in info format. A printable version
76 ;; of the documentation is available from the maintainers webpage (see
77 ;; SOURCE).
78 ;;
79 ;;
80 ;; ACKNOWLEDGMENTS
81 ;; ===============
82 ;;
83 ;; Thanks to the following people for their contributions and comments:
84 ;;
85 ;; Ulrik Dickow <dickow_at_nbi.dk>
86 ;; Eric E. Dors <edors_at_lanl.gov>
87 ;; Stein Vidar H. Haugan <s.v.h.haugan_at_astro.uio.no>
88 ;; David Huenemoerder <dph_at_space.mit.edu>
89 ;; Kevin Ivory <Kevin.Ivory_at_linmpi.mpg.de>
90 ;; Dick Jackson <dick_at_d-jackson.com>
91 ;; Xuyong Liu <liu_at_stsci.edu>
92 ;; Simon Marshall <Simon.Marshall_at_esrin.esa.it>
93 ;; Laurent Mugnier <mugnier_at_onera.fr>
94 ;; Lubos Pochman <lubos_at_rsinc.com>
95 ;; Bob Portmann <portmann_at_al.noaa.gov>
96 ;; Patrick M. Ryan <pat_at_jaameri.gsfc.nasa.gov>
97 ;; Marty Ryba <ryba_at_ll.mit.edu>
98 ;; Paul Sorenson <aardvark62_at_msn.com>
99 ;; Phil Sterne <sterne_at_dublin.llnl.gov>
100 ;; Phil Williams <williams_at_irc.chmcc.org>
101 ;;
102 ;; CUSTOMIZATION:
103 ;; =============
104 ;;
105 ;; IDLWAVE has extensive customize support; to learn about the
106 ;; variables which control the mode's behavior, use `M-x
107 ;; idlwave-customize'.
108 ;;
109 ;; You can set your own preferred values with Customize, or with Lisp
110 ;; code in .emacs. For an example of what to put into .emacs, check
111 ;; the TexInfo documentation or see a complete .emacs available at the
112 ;; website.
113 ;;
114 ;; KNOWN PROBLEMS:
115 ;; ==============
116 ;;
117 ;; IDLWAVE support for the IDL-derived PV-WAVE CL language of Visual
118 ;; Numerics, Inc. is growing less and less complete as the two
119 ;; languages grow increasingly apart. The mode probably shouldn't
120 ;; even have "WAVE" in its title, but it's catchy, and was required
121 ;; to avoid conflict with the CORBA idl.el mode. Caveat WAVEor.
122 ;;
123 ;; Moving the point backwards in conjunction with abbrev expansion
124 ;; does not work as I would like it, but this is a problem with
125 ;; emacs abbrev expansion done by the self-insert-command. It ends
126 ;; up inserting the character that expanded the abbrev after moving
127 ;; point backward, e.g., "\cl" expanded with a space becomes
128 ;; "LONG( )" with point before the close paren. This is solved by
129 ;; using a temporary function in `post-command-hook' - not pretty,
130 ;; but it works.
131 ;;
132 ;; Tabs and spaces are treated equally as whitespace when filling a
133 ;; comment paragraph. To accomplish this, tabs are permanently
134 ;; replaced by spaces in the text surrounding the paragraph, which
135 ;; may be an undesirable side-effect. Replacing tabs with spaces is
136 ;; limited to comments only and occurs only when a comment
137 ;; paragraph is filled via `idlwave-fill-paragraph'.
138 ;;
139 ;; Muti-statement lines (using "&") on block begin and end lines can
140 ;; ruin the formatting. For example, multiple end statements on a
141 ;; line: endif & endif. Using "&" outside of block begin/end lines
142 ;; should be okay.
143 ;;
144 ;; Determining the expression at point for printing and other
145 ;; examination commands is somewhat rough: currently only fairly
146 ;; simple entities are found. You can always drag-select or examine
147 ;; a pre-selected region.
148 ;;
149 ;; When forcing completion of method keywords, the initial
150 ;; query for a method has multiple entries for some methods. Would
151 ;; be too difficult to fix this hardly used case.
152 ;;
153 \f
154 ;;; Code:
155
156
157 (eval-when-compile (require 'cl))
158 (require 'idlw-help)
159
160 ;; For XEmacs
161 (unless (fboundp 'line-beginning-position)
162 (defalias 'line-beginning-position 'point-at-bol))
163 (unless (fboundp 'line-end-position)
164 (defalias 'line-end-position 'point-at-eol))
165 (unless (fboundp 'char-valid-p)
166 (defalias 'char-valid-p 'characterp))
167 (unless (fboundp 'match-string-no-properties)
168 (defalias 'match-string-no-properties 'match-string))
169
170 (if (not (fboundp 'cancel-timer))
171 (condition-case nil
172 (require 'timer)
173 (error nil)))
174
175 (eval-and-compile
176 ;; Kludge to allow `defcustom' for Emacs 19.
177 (condition-case () (require 'custom) (error nil))
178 (if (and (featurep 'custom) (fboundp 'custom-declare-variable))
179 nil ;; We've got what we needed
180 ;; We have the old or no custom-library, hack around it!
181 (defmacro defgroup (&rest args) nil)
182 (defmacro defcustom (var value doc &rest args)
183 `(defvar ,var ,value ,doc))))
184
185 (defgroup idlwave nil
186 "Major mode for editing IDL .pro files."
187 :tag "IDLWAVE"
188 :link '(url-link :tag "Home Page"
189 "http://idlwave.org")
190 :link '(emacs-commentary-link :tag "Commentary in idlw-shell.el"
191 "idlw-shell.el")
192 :link '(emacs-commentary-link :tag "Commentary in idlwave.el" "idlwave.el")
193 :link '(custom-manual "(idlwave)Top")
194 :prefix "idlwave"
195 :group 'languages)
196
197
198 ;;; Variables for indentation behavior ---------------------------------------
199
200 (defgroup idlwave-code-formatting nil
201 "Indentation and formatting options for IDLWAVE mode."
202 :group 'idlwave)
203
204 (defcustom idlwave-main-block-indent 2
205 "*Extra indentation for the main block of code.
206 That is the block between the FUNCTION/PRO statement and the END
207 statement for that program unit."
208 :group 'idlwave-code-formatting
209 :type 'integer)
210
211 (defcustom idlwave-block-indent 3
212 "*Extra indentation applied to block lines.
213 If you change this, you probably also want to change `idlwave-end-offset'."
214 :group 'idlwave-code-formatting
215 :type 'integer)
216
217 (defcustom idlwave-end-offset -3
218 "*Extra indentation applied to block END lines.
219 A value equal to negative `idlwave-block-indent' will make END lines
220 line up with the block BEGIN lines."
221 :group 'idlwave-code-formatting
222 :type 'integer)
223
224 (defcustom idlwave-continuation-indent 3
225 "*Extra indentation applied to continuation lines.
226 This extra offset applies to the first of a set of continuation lines.
227 The following lines receive the same indentation as the first."
228 :group 'idlwave-code-formatting
229 :type 'integer)
230
231 (defcustom idlwave-max-extra-continuation-indent 40
232 "*Maximum additional indentation for special continuation indent.
233 Several special indentations are tried to help line up continuation
234 lines in routine calls or definitions, other statements with
235 parentheses, or assignment statements. This variable specifies a
236 maximum amount by which this special indentation can exceed the
237 standard continuation indentation, otherwise defaulting to a fixed
238 offset. Set to 0 to effectively disable all special continuation
239 indentation, or to a large number (like 100) to enable it in all
240 cases. See also `idlwave-indent-to-open-paren', which can override
241 this variable."
242 :group 'idlwave-code-formatting
243 :type 'integer)
244
245 (defcustom idlwave-indent-to-open-paren t
246 "*Non-nil means, indent continuation lines to innermost open
247 parenthesis. This indentation occurs even if otherwise disallowed by
248 `idlwave-max-extra-continuation-indent'. Matching parens and the
249 interleaving args are lined up. Example:
250
251 x = function_a(function_b(function_c( a, b, [1,2,3, $
252 4,5,6 $
253 ], $
254 c, d $
255 )))
256
257 When this variable is nil, paren alignment may still occur, based on
258 the value of `max-extra-continuation-indent', which, if zero, would
259 yield:
260
261 x = function_a(function_b(function_c( a, b, [1,2,3, $
262 4,5,6 $
263 ], $
264 c, d $
265 )))"
266 :group 'idlwave-code-formatting
267 :type 'boolean)
268
269 (defcustom idlwave-indent-parens-nested nil
270 "*Non-nil means, indent continuation lines with parens by nesting
271 lines at consecutively deeper levels."
272 :group 'idlwave-code-formatting
273 :type 'boolean)
274
275
276 (defcustom idlwave-hanging-indent t
277 "*If set non-nil then comment paragraphs are indented under the
278 hanging indent given by `idlwave-hang-indent-regexp' match in the first line
279 of the paragraph."
280 :group 'idlwave-code-formatting
281 :type 'boolean)
282
283 (defcustom idlwave-hang-indent-regexp "- "
284 "*Regular expression matching the position of the hanging indent
285 in the first line of a comment paragraph. The size of the indent
286 extends to the end of the match for the regular expression."
287 :group 'idlwave-code-formatting
288 :type 'regexp)
289
290 (defcustom idlwave-use-last-hang-indent nil
291 "*If non-nil then use last match on line for `idlwave-indent-regexp'."
292 :group 'idlwave-code-formatting
293 :type 'boolean)
294
295 (defcustom idlwave-fill-comment-line-only t
296 "*If non-nil then auto fill will only operate on comment lines."
297 :group 'idlwave-code-formatting
298 :type 'boolean)
299
300 (defcustom idlwave-auto-fill-split-string t
301 "*If non-nil then auto fill will split strings with the IDL `+' operator.
302 When the line end falls within a string, string concatenation with the
303 '+' operator will be used to distribute a long string over lines.
304 If nil and a string is split then a terminal beep and warning are issued.
305
306 This variable is ignored when `idlwave-fill-comment-line-only' is
307 non-nil, since in this case code is not auto-filled."
308 :group 'idlwave-code-formatting
309 :type 'boolean)
310
311 (defcustom idlwave-split-line-string t
312 "*If non-nil then `idlwave-split-line' will split strings with `+'.
313 When the splitting point of a line falls inside a string, split the string
314 using the `+' string concatenation operator. If nil and a string is
315 split then a terminal beep and warning are issued."
316 :group 'idlwave-code-formatting
317 :type 'boolean)
318
319 (defcustom idlwave-no-change-comment ";;;"
320 "*The indentation of a comment that starts with this regular
321 expression will not be changed. Note that the indentation of a comment
322 at the beginning of a line is never changed."
323 :group 'idlwave-code-formatting
324 :type 'string)
325
326 (defcustom idlwave-begin-line-comment nil
327 "*A comment anchored at the beginning of line.
328 A comment matching this regular expression will not have its
329 indentation changed. If nil the default is \"^;\", i.e., any line
330 beginning with a \";\". Expressions for comments at the beginning of
331 the line should begin with \"^\"."
332 :group 'idlwave-code-formatting
333 :type '(choice (const :tag "Any line beginning with `;'" nil)
334 'regexp))
335
336 (defcustom idlwave-code-comment ";;[^;]"
337 "*A comment that starts with this regular expression on a line by
338 itself is indented as if it is a part of IDL code. As a result if
339 the comment is not preceded by whitespace it is unchanged."
340 :group 'idlwave-code-formatting
341 :type 'regexp)
342
343 ;; Comments not matching any of the above will be indented as a
344 ;; right-margin comment, i.e., to a minimum of `comment-column'.
345
346 ;;; Routine Info and Completion ---------------------------------------
347
348 (defgroup idlwave-routine-info nil
349 "Routine Info options for IDLWAVE mode."
350 :group 'idlwave)
351
352 (defcustom idlwave-use-library-catalogs t
353 "*Non-nil means search the IDL path for library catalog files.
354
355 These files, named .idlwave_catalog, document routine information for
356 individual directories and libraries of IDL .pro files. Many popular
357 libraries come with catalog files by default, so leaving this on is a
358 usually a good idea.."
359 :group 'idlwave-routine-info
360 :type 'boolean)
361
362 (defcustom idlwave-init-rinfo-when-idle-after 10
363 "*Seconds of idle time before routine info is automatically
364 initialized. Initializing the routine info can take a long time, in
365 particular if a large number of library catalogs are involved. When
366 Emacs is idle for more than the number of seconds specified by this
367 variable, it starts the initialization. The process is split into
368 five steps, in order to keep work interruption as short as possible.
369 If one of the steps finishes, and no user input has arrived in the
370 mean time, initialization proceeds immediately to the next step. A
371 good value for this variable is about 1/3 of the time initialization
372 take in your setup. So if you have a fast machine and no problems
373 with a slow network connection, don't hesitate to set this to 2
374 seconds. A Value of 0 means, don't initialize automatically, but
375 instead wait until routine information is needed, and initialize
376 then."
377 :group 'idlwave-routine-info
378 :type 'number)
379
380 (defcustom idlwave-scan-all-buffers-for-routine-info t
381 "*Non-nil means, scan buffers for IDL programs when updating info.
382 The scanning is done by the command `idlwave-update-routine-info'.
383 The following values are allowed:
384
385 nil Don't scan any buffers.
386 t Scan all idlwave-mode buffers in the current editing session.
387 current Scan only the current buffer, but no other buffers."
388 :group 'idlwave-routine-info
389 :type '(choice
390 (const :tag "No buffer" nil)
391 (const :tag "All buffers" t)
392 (const :tag "Current buffer only" 'current)))
393
394 (defcustom idlwave-query-shell-for-routine-info t
395 "*Non-nil means query the shell for info about compiled routines.
396 Querying the shell is useful to get information about compiled modules,
397 and it is turned on by default. However, when you have a complete library
398 scan, this is not necessary."
399 :group 'idlwave-routine-info
400 :type 'boolean)
401
402 (defcustom idlwave-auto-routine-info-updates
403 '(find-file save-buffer kill-buffer compile-buffer)
404 "*Controls under what circumstances routine info is updated automatically.
405 Possible values:
406 nil Never
407 t All available
408 \(...) A list of circumstances. Allowed members are:
409 find-file Add info for new IDLWAVE buffers.
410 save-buffer Update buffer info when buffer is saved
411 kill-buffer Remove buffer info when buffer gets killed
412 compile-buffer Update shell info after `idlwave-shell-save-and...'"
413 :group 'idlwave-routine-info
414 :type '(choice
415 (const :tag "Never" nil)
416 (const :tag "As often as possible" t)
417 (set :tag "Checklist" :greedy t
418 (const :tag "When visiting a file" find-file)
419 (const :tag "When saving a buffer" save-buffer)
420 (const :tag "After a buffer was killed" kill-buffer)
421 (const :tag "After a buffer was compiled successfully, update shell info" compile-buffer))))
422
423 (defcustom idlwave-rinfo-max-source-lines 5
424 "*Maximum number of source files displayed in the Routine Info window.
425 When an integer, it is the maximum number of source files displayed.
426 t means to show all source files."
427 :group 'idlwave-routine-info
428 :type 'integer)
429
430 (defcustom idlwave-library-path nil
431 "Library path for Windows and MacOS (OS9). Not needed under UNIX.
432 When selecting the directories to scan for IDL user catalog routine
433 info, IDLWAVE can, under UNIX, query the shell for the exact search
434 path \(the value of !PATH). However, under Windows and MacOS
435 \(pre-OSX), the IDLWAVE shell does not work. In this case, this
436 variable can be set to specify the paths where IDLWAVE can find PRO
437 files. The shell will only be asked for a list of paths when this
438 variable is nil. The value is a list of directories. A directory
439 preceeded by a `+' will be searched recursively. If you set this
440 variable on a UNIX system, the shell will not be queried. See also
441 `idlwave-system-directory'."
442 :group 'idlwave-routine-info
443 :type '(repeat (directory)))
444
445 (defcustom idlwave-system-directory ""
446 "The IDL system directory for Windows and MacOS. Not needed under
447 UNIX. Set this to the value of the `!DIR' system variable in IDL.
448 IDLWAVE uses this to find out which of the library routines belong to
449 the official system library. All files inside the `lib' subdirectory
450 are considered system library files - so don't install private stuff
451 in this directory. On UNIX systems, IDLWAVE queries the shell for the
452 value of `!DIR'. See also `idlwave-library-path'."
453 :group 'idlwave-routine-info
454 :type 'directory)
455
456 ;; Configuration files
457 (defcustom idlwave-config-directory
458 (convert-standard-filename "~/.idlwave")
459 "*Directory for configuration files and user-library catalog."
460 :group 'idlwave-routine-info
461 :type 'file)
462
463 (defvar idlwave-user-catalog-file "idlusercat.el")
464 (defvar idlwave-xml-system-rinfo-converted-file "idl_xml_rinfo.el")
465 (defvar idlwave-path-file "idlpath.el")
466
467 (defvar idlwave-libinfo-file nil
468 "*Obsolete variable, no longer used.")
469
470 (defcustom idlwave-special-lib-alist nil
471 "Alist of regular expressions matching special library directories.
472 When listing routine source locations, IDLWAVE gives a short hint where
473 the file defining the routine is located. By default it lists `SystemLib'
474 for routines in the system library `!DIR/lib' and `Library' for anything
475 else. This variable can define additional types. The car of each entry
476 is a regular expression matching the file name (they normally will match
477 on the path). The cdr is the string to be used as identifier. Max 10
478 chars are allowed."
479 :group 'idlwave-routine-info
480 :type '(repeat
481 (cons regexp string)))
482
483 (defcustom idlwave-auto-write-paths t
484 "Write out path (!PATH) and system directory (!DIR) info automatically.
485 Path info is needed to locate library catalog files. If non-nil,
486 whenever the path-list changes as a result of shell-query, etc., it is
487 written to file. Otherwise, the menu option \"Write Paths\" can be
488 used to force a write."
489 :group 'idlwave-routine-info
490 :type 'boolean)
491
492 (defgroup idlwave-completion nil
493 "Completion options for IDLWAVE mode."
494 :prefix "idlwave"
495 :group 'idlwave)
496
497 (eval-and-compile
498 (defconst idlwave-tmp
499 '(choice :tag "by applying the function"
500 (const upcase)
501 (const downcase)
502 (const capitalize)
503 (const preserve)
504 (symbol :tag "Other"))))
505
506 (defcustom idlwave-completion-case '((routine . upcase)
507 (keyword . upcase)
508 (class . preserve)
509 (method . preserve))
510 "Association list setting the case of completed words.
511
512 This variable determines the case (UPPER/lower/Capitalized...) of
513 words inserted into the buffer by completion. The preferred case can
514 be specified separately for routine names, keywords, classes and
515 methods.
516 This alist should therefore have entries for `routine' (normal
517 functions and procedures, i.e. non-methods), `keyword', `class', and
518 `method'. Plausible values are
519
520 upcase upcase whole word, like `BOX_CURSOR'
521 downcase downcase whole word, like `read_ppm'
522 capitalize capitalize each part, like `Widget_Control'
523 preserve preserve case as is, like `IDLgrView'
524
525 The value can also be any Emacs Lisp function which transforms the
526 case of characters in a string.
527
528 A value of `preserve' means that the case of the completed word is
529 identical to the way it was written in the definition statement of the
530 routine. This was implemented to allow for mixed-case completion, in
531 particular of object classes and methods.
532 If a completable word is defined in multiple locations, the meaning of
533 `preserve' is not unique since the different definitions might be
534 cased differently. Therefore IDLWAVE always takes the case of the
535 *first* definition it encounters during routine info collection and
536 uses the case derived from it consistently.
537
538 Note that a lowercase-only string in the buffer will always be completed in
539 lower case (but see the variable `idlwave-completion-force-default-case').
540
541 After changing this variable, you need to either restart Emacs or press
542 `C-u C-c C-i' to update the internal lists."
543 :group 'idlwave-completion
544 :type `(repeat
545 (cons (symbol :tag "Derive completion case for")
546 ,idlwave-tmp)))
547
548 (defcustom idlwave-completion-force-default-case nil
549 "*Non-nil means, completion will always honor `idlwave-completion-case'.
550 When nil, only the completion of a mixed case or upper case string
551 will honor the default settings in `idlwave-completion-case', while
552 the completion of lower case strings will be completed entirely in
553 lower case."
554 :group 'idlwave-completion
555 :type 'boolean)
556
557 (defcustom idlwave-complete-empty-string-as-lower-case nil
558 "*Non-nil means, the empty string is considered downcase for completion.
559 The case of what is already in the buffer determines the case of completions.
560 When this variable is non-nil, the empty string is considered to be downcase.
561 Completing on the empty string then offers downcase versions of the possible
562 completions."
563 :group 'idlwave-completion
564 :type 'boolean)
565
566 (defvar idlwave-default-completion-case-is-down nil
567 "Obsolete variable. See `idlwave-complete-empty-string-as-lower-case' and
568 `idlwave-completion-case'.")
569
570 (defcustom idlwave-buffer-case-takes-precedence nil
571 "*Non-nil means, the case of tokens in buffers dominates over system stuff.
572 To make this possible, we need to re-case everything each time we update
573 the routine info from the buffers. This is slow.
574 The default is to consider the case given in the system and library files
575 first which makes updating much faster."
576 :group 'idlwave-completion
577 :type 'boolean)
578
579 (defcustom idlwave-highlight-help-links-in-completion t
580 "*Non-nil means, highlight completions for which system help is available.
581 Help can then be accessed with mouse-3.
582 This option is only effective when the online help system is installed."
583 :group 'idlwave-completion
584 :type 'boolean)
585
586 (defcustom idlwave-support-inheritance t
587 "Non-nil means, treat inheritance with completion, online help etc.
588 When nil, IDLWAVE only knows about the native methods and tags of a class,
589 not about inherited ones."
590 :group 'idlwave-routine-info
591 :type 'boolean)
592
593 (defcustom idlwave-keyword-class-inheritance '("^[gs]etproperty$" "^init$")
594 "List of regular expressions for class-driven keyword inheritance.
595 Keyword inheritance is often tied to class inheritance by \"chaining\"
596 up the class tree. While it cannot be assumed that the presence of an
597 _EXTRA or _REF_EXTRA symbol guarantees such chaining will occur, for
598 certain methods this assumption is almost always true. The methods
599 for which to assume this can be set here."
600 :group 'idlwave-routine-info
601 :type '(repeat (regexp :tag "Match method:")))
602
603
604 (defcustom idlwave-completion-show-classes 1
605 "*Number of classes to show when completing object methods and keywords.
606 When completing methods or keywords for an object with unknown class,
607 the *Completions* buffer will show the valid classes for each completion
608 like this:
609
610 MyMethod <Class1,Class2,Class3>
611
612 The value of this variable may be nil to inhibit display, or an integer to
613 indicate the maximum number of classes to display.
614
615 On XEmacs, a full list of classes will also be placed into a `help-echo'
616 property on the competion items, so that the list of classes for the current
617 item is displayed in the echo area. If the value of this variable is a
618 negative integer, the `help-echo' property will be suppressed."
619 :group 'idlwave-completion
620 :type '(choice (const :tag "Don't show" nil)
621 (integer :tag "Number of classes shown" 1)))
622
623 (defcustom idlwave-completion-fontify-classes t
624 "*Non-nil means, fontify the classes in completions buffer.
625 This makes it easier to distinguish the completion items from the extra
626 class info listed. See `idlwave-completion-show-classes'."
627 :group 'idlwave-completion
628 :type 'boolean)
629
630 (defcustom idlwave-query-class '((method-default . nil)
631 (keyword-default . nil))
632 "Association list governing specification of object classes for completion.
633
634 When IDLWAVE tries to complete object-oriented methods, it usually
635 cannot determine the class of a given object from context. In order
636 to provide the user with a correct list of methods or keywords, it
637 needs to determine the appropriate class. IDLWAVE has two ways of
638 doing this (well, three ways if you count the shell... see
639 `idlwave-shell-query-for-class'):
640
641 1. Combine the items of all available classes which contain this
642 method for the purpose of completion. So when completing a method,
643 all methods of all known classes are available, and when completing
644 a keyword, all keywords allowed for this method in any class are
645 shown. This behavior is very much like normal completion and is
646 therefore the default. It works much better than one might think -
647 only for the INIT, GETPROPERTY and SETPROPERTY the keyword lists
648 become uncomfortably long. See also
649 `idlwave-completion-show-classes'.
650
651 2. The second possibility is to ask the user on each occasion. To
652 make this less interruptive, IDLWAVE can store the class as a text
653 property on the object operator `->'. For a given object in the
654 source code, class selection will then be needed only once
655 - for example to complete the method. Keywords to the method can
656 then be completed directly, because the class is already known.
657 You will have to turn on the storage of the selected class
658 explicitly with the variable `idlwave-store-inquired-class'.
659
660 This variable allows you to configure IDLWAVE's method and
661 method-keyword completion behavior. Its value is an alist, which
662 should contain at least two elements: (method-default . VALUE) and
663 \(keyword-default . VALUE), where VALUE is either t or nil. These
664 specify if the class should be found during method and keyword
665 completion, respectively.
666
667 The alist may have additional entries specifying exceptions from the
668 keyword completion rule for specific methods, like INIT or
669 GETPROPERTY. In order to turn on class specification for the INIT
670 method, add an entry (\"INIT\" . t). The method name must be ALL-CAPS."
671 :group 'idlwave-completion
672 :type '(list
673 (cons (const method-default)
674 (boolean :tag "Determine class when completing METHODS "))
675 (cons (const keyword-default)
676 (boolean :tag "Determine class when completing KEYWORDS "))
677 (repeat
678 :tag "Exceptions to defaults"
679 :inline t
680 (cons (string :tag "MODULE" :value "")
681 (boolean :tag "Determine class for this method")))))
682
683 (defcustom idlwave-store-inquired-class t
684 "*Non-nil means, store class of a method call as text property on `->'.
685 IDLWAVE sometimes has to ask the user for the class associated with a
686 particular object method call. This happens during the commands
687 `idlwave-routine-info' and `idlwave-complete', depending upon the
688 value of the variable `idlwave-query-class'.
689
690 When you specify a class, this information can be stored as a text
691 property on the `->' arrow in the source code, so that during the same
692 editing session, IDLWAVE will not have to ask again. When this
693 variable is non-nil, IDLWAVE will store and reuse the class information.
694 The class stored can be checked and removed with `\\[idlwave-routine-info]'
695 on the arrow.
696
697 The default of this variable is nil, since the result of commands then
698 is more predictable. However, if you know what you are doing, it can
699 be nice to turn this on.
700
701 An arrow which knows the class will be highlighted with
702 `idlwave-class-arrow-face'. The command \\[idlwave-routine-info]
703 displays (with prefix arg: deletes) the class stored on the arrow
704 at point."
705 :group 'idlwave-completion
706 :type 'boolean)
707
708 (defcustom idlwave-class-arrow-face 'bold
709 "*Face to highlight object operator arrows `->' which carry a class property.
710 When IDLWAVE stores a class name as text property on an object arrow
711 \(see variable `idlwave-store-inquired-class', it highlights the arrow
712 with this font in order to remind the user that this arrow is special."
713 :group 'idlwave-completion
714 :type 'symbol)
715
716 (defcustom idlwave-resize-routine-help-window t
717 "*Non-nil means, resize the Routine-info *Help* window to fit the content."
718 :group 'idlwave-completion
719 :type 'boolean)
720
721 (defcustom idlwave-keyword-completion-adds-equal t
722 "*Non-nil means, completion automatically adds `=' after completed keywords."
723 :group 'idlwave-completion
724 :type 'boolean)
725
726 (defcustom idlwave-function-completion-adds-paren t
727 "*Non-nil means, completion automatically adds `(' after completed function.
728 nil means, don't add anything.
729 A value of `2' means, also add the closing parenthesis and position cursor
730 between the two."
731 :group 'idlwave-completion
732 :type '(choice (const :tag "Nothing" nil)
733 (const :tag "(" t)
734 (const :tag "()" 2)))
735
736 (defcustom idlwave-completion-restore-window-configuration t
737 "*Non-nil means, try to restore the window configuration after completion.
738 When completion is not unique, Emacs displays a list of completions.
739 This messes up your window configuration. With this variable set, IDLWAVE
740 restores the old configuration after successful completion."
741 :group 'idlwave-completion
742 :type 'boolean)
743
744 ;;; Variables for abbrev and action behavior -----------------------------
745
746 (defgroup idlwave-abbrev-and-indent-action nil
747 "IDLWAVE performs actions when expanding abbreviations or indenting lines.
748 The variables in this group govern this."
749 :group 'idlwave)
750
751 (defcustom idlwave-do-actions nil
752 "*Non-nil means performs actions when indenting.
753 The actions that can be performed are listed in `idlwave-indent-action-table'."
754 :group 'idlwave-abbrev-and-indent-action
755 :type 'boolean)
756
757 (defcustom idlwave-abbrev-start-char "\\"
758 "*A single character string used to start abbreviations in abbrev mode.
759 Possible characters to chose from: ~`\%
760 or even '?'. '.' is not a good choice because it can make structure
761 field names act like abbrevs in certain circumstances.
762
763 Changes to this in `idlwave-mode-hook' will have no effect. Instead a user
764 must set it directly using `setq' in the .emacs file before idlwave.el
765 is loaded."
766 :group 'idlwave-abbrev-and-indent-action
767 :type 'string)
768
769 (defcustom idlwave-surround-by-blank nil
770 "*Non-nil means, enable `idlwave-surround'.
771 If non-nil, `=',`<',`>',`&',`,', `->' are surrounded with spaces by
772 `idlwave-surround'.
773 See help for `idlwave-indent-action-table' for symbols using `idlwave-surround'.
774
775 Also see the default key bindings for keys using `idlwave-surround'.
776 Keys are bound and made into actions calling `idlwave-surround' with
777 `idlwave-action-and-binding'.
778 See help for `idlwave-action-and-binding' for examples.
779
780 Also see help for `idlwave-surround'."
781 :group 'idlwave-abbrev-and-indent-action
782 :type 'boolean)
783
784 (defcustom idlwave-pad-keyword t
785 "*Non-nil means pad '=' in keywords (routine calls or defs) like assignment.
786 Whenever `idlwave-surround' is non-nil then this affects how '=' is
787 padded for keywords and for variables. If t, pad the same as for
788 assignments. If nil then spaces are removed. With any other value,
789 spaces are left unchanged."
790 :group 'idlwave-abbrev-and-indent-action
791 :type '(choice
792 (const :tag "Pad like assignments" t)
793 (const :tag "Remove space near `='" nil)
794 (const :tag "Keep space near `='" 'keep)))
795
796 (defcustom idlwave-show-block t
797 "*Non-nil means point blinks to block beginning for `idlwave-show-begin'."
798 :group 'idlwave-abbrev-and-indent-action
799 :type 'boolean)
800
801 (defcustom idlwave-expand-generic-end nil
802 "*Non-nil means expand generic END to ENDIF/ENDELSE/ENDWHILE etc."
803 :group 'idlwave-abbrev-and-indent-action
804 :type 'boolean)
805
806 (defcustom idlwave-reindent-end t
807 "*Non-nil means re-indent line after END was typed."
808 :group 'idlwave-abbrev-and-indent-action
809 :type 'boolean)
810
811 (defcustom idlwave-abbrev-move t
812 "*Non-nil means the abbrev hook can move point.
813 Set to nil by `idlwave-expand-region-abbrevs'. To see the abbrev
814 definitions, use the command `list-abbrevs', for abbrevs that move
815 point. Moving point is useful, for example, to place point between
816 parentheses of expanded functions.
817
818 See `idlwave-check-abbrev'."
819 :group 'idlwave-abbrev-and-indent-action
820 :type 'boolean)
821
822 (defcustom idlwave-abbrev-change-case nil
823 "*Non-nil means all abbrevs will be forced to either upper or lower case.
824 If the value t, all expanded abbrevs will be upper case.
825 If the value is 'down then abbrevs will be forced to lower case.
826 If nil, the case will not change.
827 If `idlwave-reserved-word-upcase' is non-nil, reserved words will always be
828 upper case, regardless of this variable."
829 :group 'idlwave-abbrev-and-indent-action
830 :type 'boolean)
831
832 (defcustom idlwave-reserved-word-upcase nil
833 "*Non-nil means, reserved words will be made upper case via abbrev expansion.
834 If nil case of reserved words is controlled by `idlwave-abbrev-change-case'.
835 Has effect only if in abbrev-mode."
836 :group 'idlwave-abbrev-and-indent-action
837 :type 'boolean)
838
839 ;;; Action/Expand Tables.
840 ;;
841 ;; The average user may have difficulty modifying this directly. It
842 ;; can be modified/set in idlwave-mode-hook, but it is easier to use
843 ;; idlwave-action-and-binding. See help for idlwave-action-and-binding for
844 ;; examples of how to add an action.
845 ;;
846 ;; The action table is used by `idlwave-indent-line' whereas both the
847 ;; action and expand tables are used by `idlwave-indent-and-action'. In
848 ;; general, the expand table is only used when a line is explicitly
849 ;; indented. Whereas, in addition to being used when the expand table
850 ;; is used, the action table is used when a line is indirectly
851 ;; indented via line splitting, auto-filling or a new line creation.
852 ;;
853 ;; Example actions:
854 ;;
855 ;; Capitalize system vars
856 ;; (idlwave-action-and-binding idlwave-sysvar '(capitalize-word 1) t)
857 ;;
858 ;; Capitalize procedure name
859 ;; (idlwave-action-and-binding "\\<\\(pro\\|function\\)\\>[ \t]*\\<"
860 ;; '(capitalize-word 1) t)
861 ;;
862 ;; Capitalize common block name
863 ;; (idlwave-action-and-binding "\\<common\\>[ \t]+\\<"
864 ;; '(capitalize-word 1) t)
865 ;; Capitalize label
866 ;; (idlwave-action-and-binding (concat "^[ \t]*" idlwave-label)
867 ;; '(capitalize-word -1) t)
868
869 (defvar idlwave-indent-action-table nil
870 "*Associated array containing action lists of search string (car),
871 and function as a cdr. This table is used by `idlwave-indent-line'.
872 See documentation for `idlwave-do-action' for a complete description of
873 the action lists.
874
875 Additions to the table are made with `idlwave-action-and-binding' when a
876 binding is not requested.
877 See help on `idlwave-action-and-binding' for examples.")
878
879 (defvar idlwave-indent-expand-table nil
880 "*Associated array containing action lists of search string (car),
881 and function as a cdr. The table is used by the
882 `idlwave-indent-and-action' function. See documentation for
883 `idlwave-do-action' for a complete description of the action lists.
884
885 Additions to the table are made with `idlwave-action-and-binding' when a
886 binding is requested.
887 See help on `idlwave-action-and-binding' for examples.")
888
889 ;;; Documentation header and history keyword ---------------------------------
890
891 (defgroup idlwave-documentation nil
892 "Options for documenting IDLWAVE files."
893 :group 'idlwave)
894
895 ;; FIXME: make defcustom?
896 (defvar idlwave-file-header
897 (list nil
898 ";+
899 ; NAME:
900 ;
901 ;
902 ;
903 ; PURPOSE:
904 ;
905 ;
906 ;
907 ; CATEGORY:
908 ;
909 ;
910 ;
911 ; CALLING SEQUENCE:
912 ;
913 ;
914 ;
915 ; INPUTS:
916 ;
917 ;
918 ;
919 ; OPTIONAL INPUTS:
920 ;
921 ;
922 ;
923 ; KEYWORD PARAMETERS:
924 ;
925 ;
926 ;
927 ; OUTPUTS:
928 ;
929 ;
930 ;
931 ; OPTIONAL OUTPUTS:
932 ;
933 ;
934 ;
935 ; COMMON BLOCKS:
936 ;
937 ;
938 ;
939 ; SIDE EFFECTS:
940 ;
941 ;
942 ;
943 ; RESTRICTIONS:
944 ;
945 ;
946 ;
947 ; PROCEDURE:
948 ;
949 ;
950 ;
951 ; EXAMPLE:
952 ;
953 ;
954 ;
955 ; MODIFICATION HISTORY:
956 ;
957 ;-
958 ")
959 "*A list (PATHNAME STRING) specifying the doc-header template to use for
960 summarizing a file. If PATHNAME is non-nil then this file will be included.
961 Otherwise STRING is used. If nil, the file summary will be omitted.
962 For example you might set PATHNAME to the path for the
963 lib_template.pro file included in the IDL distribution.")
964
965 (defcustom idlwave-header-to-beginning-of-file t
966 "*Non-nil means, the documentation header will always be at start of file.
967 When nil, the header is positioned between the PRO/FUNCTION line of
968 the current routine and the code, allowing several routine headers in
969 a file."
970 :group 'idlwave-documentation
971 :type 'boolean)
972
973 (defcustom idlwave-timestamp-hook 'idlwave-default-insert-timestamp
974 "*The hook function used to update the timestamp of a function."
975 :group 'idlwave-documentation
976 :type 'function)
977
978 (defcustom idlwave-doc-modifications-keyword "HISTORY"
979 "*The modifications keyword to use with the log documentation commands.
980 A ':' is added to the keyword end.
981 Inserted by doc-header and used to position logs by doc-modification.
982 If nil it will not be inserted."
983 :group 'idlwave-documentation
984 :type 'string)
985
986 (defcustom idlwave-doclib-start "^;+\\+"
987 "*Regexp matching the start of a document library header."
988 :group 'idlwave-documentation
989 :type 'regexp)
990
991 (defcustom idlwave-doclib-end "^;+-"
992 "*Regexp matching the end of a document library header."
993 :group 'idlwave-documentation
994 :type 'regexp)
995
996 ;;; External Programs -------------------------------------------------------
997
998 (defgroup idlwave-external-programs nil
999 "Path locations of external commands used by IDLWAVE."
1000 :group 'idlwave)
1001
1002 (defcustom idlwave-shell-explicit-file-name "idl"
1003 "*If non-nil, this is the command to run IDL.
1004 Should be an absolute file path or path relative to the current environment
1005 execution search path. If you want to specify command line switches
1006 for the idl program, use `idlwave-shell-command-line-options'.
1007
1008 I know the name of this variable is badly chosen, but I cannot change
1009 it without compromizing backwards-compatibility."
1010 :group 'idlwave-external-programs
1011 :type 'string)
1012
1013 (defcustom idlwave-shell-command-line-options nil
1014 "*A list of command line options for calling the IDL program.
1015 Since IDL is executed directly without going through a shell like /bin/sh,
1016 this should be a list of strings like '(\"-rt=file\" \"-nw\") with a separate
1017 string for each argument. But you may also give a single string which
1018 contains the options whitespace-separated. Emacs will be kind enough to
1019 split it for you."
1020 :type '(choice
1021 string
1022 (repeat (string :value "")))
1023 :group 'idlwave-external-programs)
1024
1025 (defcustom idlwave-help-application "idlhelp"
1026 "*The external application providing reference help for programming.
1027 Obsolete, if the IDL Assistant is being used for help."
1028 :group 'idlwave-external-programs
1029 :type 'string)
1030
1031 ;;; Some Shell variables which must be defined here.-----------------------
1032
1033 (defcustom idlwave-shell-debug-modifiers '()
1034 "List of modifiers to be used for the debugging commands.
1035 Will be used to bind debugging commands in the shell buffer and in all
1036 source buffers. These are additional convenience bindings, the debugging
1037 commands are always available with the `C-c C-d' prefix.
1038 If you set this to '(control shift), this means setting a breakpoint will
1039 be on `C-S-b', compiling a source file on `C-S-c' etc. Possible modifiers
1040 are `control', `meta', `super', `hyper', `alt', and `shift'."
1041 :group 'idlwave-shell-general-setup
1042 :type '(set :tag "Specify modifiers"
1043 (const control)
1044 (const meta)
1045 (const super)
1046 (const hyper)
1047 (const alt)
1048 (const shift)))
1049
1050 (defcustom idlwave-shell-automatic-start nil
1051 "*If non-nil attempt invoke idlwave-shell if not already running.
1052 This is checked when an attempt to send a command to an
1053 IDL process is made."
1054 :group 'idlwave-shell-general-setup
1055 :type 'boolean)
1056
1057 ;;; Miscellaneous variables -------------------------------------------------
1058
1059 (defgroup idlwave-misc nil
1060 "Miscellaneous options for IDLWAVE mode."
1061 :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
1062 :group 'idlwave)
1063
1064 (defcustom idlwave-startup-message t
1065 "*Non-nil displays a startup message when `idlwave-mode' is first called."
1066 :group 'idlwave-misc
1067 :type 'boolean)
1068
1069 (defcustom idlwave-default-font-lock-items
1070 '(pros-and-functions batch-files idlwave-idl-keywords label goto
1071 common-blocks class-arrows)
1072 "Items which should be fontified on the default fontification level 2.
1073 IDLWAVE defines 3 levels of fontification. Level 1 is very little, level 3
1074 is everything and level 2 is specified by this list.
1075 This variable must be set before IDLWAVE gets loaded. It is
1076 a list of symbols, the following symbols are allowed.
1077
1078 pros-and-functions Procedure and Function definitions
1079 batch-files Batch Files
1080 idlwave-idl-keywords IDL Keywords
1081 label Statement Labels
1082 goto Goto Statements
1083 common-blocks Common Blocks
1084 keyword-parameters Keyword Parameters in routine definitions and calls
1085 system-variables System Variables
1086 fixme FIXME: Warning in comments (on XEmacs only v. 21.0 and up)
1087 class-arrows Object Arrows with class property"
1088 :group 'idlwave-misc
1089 :type '(set
1090 :inline t :greedy t
1091 (const :tag "Procedure and Function definitions" pros-and-functions)
1092 (const :tag "Batch Files" batch-files)
1093 (const :tag "IDL Keywords (reserved words)" idlwave-idl-keywords)
1094 (const :tag "Statement Labels" label)
1095 (const :tag "Goto Statements" goto)
1096 (const :tag "Tags in Structure Definition" structtag)
1097 (const :tag "Structure Name" structname)
1098 (const :tag "Common Blocks" common-blocks)
1099 (const :tag "Keyword Parameters" keyword-parameters)
1100 (const :tag "System Variables" system-variables)
1101 (const :tag "FIXME: Warning" fixme)
1102 (const :tag "Object Arrows with class property " class-arrows)))
1103
1104 (defcustom idlwave-mode-hook nil
1105 "Normal hook. Executed when a buffer is put into `idlwave-mode'."
1106 :group 'idlwave-misc
1107 :type 'hook)
1108
1109 (defcustom idlwave-load-hook nil
1110 "Normal hook. Executed when idlwave.el is loaded."
1111 :group 'idlwave-misc
1112 :type 'hook)
1113
1114 (defvar idlwave-experimental nil
1115 "Non-nil means turn on a few experimental features.
1116 This variable is only for the maintainer, to test difficult stuff,
1117 while still distributing stable releases.
1118 As a user, you should not set this to t.")
1119
1120 ;;;
1121 ;;; End customization variables section
1122 ;;;
1123
1124 ;;; Non customization variables
1125
1126 ;;; font-lock mode - Additions by Phil Williams, Ulrik Dickow and
1127 ;;; Simon Marshall <simon_at_gnu.ai.mit.edu>
1128 ;;; and Carsten Dominik...
1129
1130 ;; The following are the reserved words in IDL. Maybe we should
1131 ;; highlight some more stuff as well?
1132 ;; Procedure declarations. Fontify keyword plus procedure name.
1133 (defvar idlwave-idl-keywords
1134 ;; To update this regexp, update the list of keywords and
1135 ;; evaluate the form.
1136 ;; (insert
1137 ;; (prin1-to-string
1138 ;; (concat
1139 ;; "\\<\\("
1140 ;; (regexp-opt
1141 ;; '("||" "&&" "and" "or" "xor" "not"
1142 ;; "eq" "ge" "gt" "le" "lt" "ne"
1143 ;; "for" "do" "endfor"
1144 ;; "if" "then" "endif" "else" "endelse"
1145 ;; "case" "of" "endcase"
1146 ;; "switch" "break" "continue" "endswitch"
1147 ;; "begin" "end"
1148 ;; "repeat" "until" "endrep"
1149 ;; "while" "endwhile"
1150 ;; "goto" "return"
1151 ;; "inherits" "mod"
1152 ;; "compile_opt" "forward_function"
1153 ;; "on_error" "on_ioerror")) ; on_error is not officially reserved
1154 ;; "\\)\\>")))
1155 "\\<\\(&&\\|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\\|||\\)\\>")
1156
1157
1158 (let* (;; Procedure declarations. Fontify keyword plus procedure name.
1159 ;; Function declarations. Fontify keyword plus function name.
1160 (pros-and-functions
1161 '("\\<\\(function\\|pro\\)\\>[ \t]+\\(\\sw+\\(::\\sw+\\)?\\)"
1162 (1 font-lock-keyword-face)
1163 (2 font-lock-function-name-face nil t)))
1164
1165 ;; Common blocks
1166 (common-blocks
1167 '("\\<\\(common\\)\\>[ \t]*\\(\\sw+\\)?[ \t]*,?"
1168 (1 font-lock-keyword-face) ; "common"
1169 (2 font-lock-reference-face nil t) ; block name
1170 ("[ \t]*\\(\\sw+\\)[ ,]*"
1171 ;; Start with point after block name and comma
1172 (goto-char (match-end 0)) ; needed for XEmacs, could be nil
1173 nil
1174 (1 font-lock-variable-name-face) ; variable names
1175 )))
1176
1177 ;; Batch files
1178 (batch-files
1179 '("^[ \t]*\\(@[^ \t\n]+\\)" (1 font-lock-string-face)))
1180
1181 ;; FIXME warning.
1182 (fixme
1183 '("\\<FIXME:" (0 font-lock-warning-face t)))
1184
1185 ;; Labels
1186 (label
1187 '("^[ \t]*\\([a-zA-Z]\\sw*:\\)" (1 font-lock-reference-face)))
1188
1189 ;; The goto statement and its label
1190 (goto
1191 '("\\(goto\\)[ \t]*,[ \t]*\\([a-zA-Z]\\sw*\\)"
1192 (1 font-lock-keyword-face)
1193 (2 font-lock-reference-face)))
1194
1195 ;; Tags in structure definitions. Note that this definition
1196 ;; actually collides with labels, so we have to use the same
1197 ;; face. It also matches named subscript ranges,
1198 ;; e.g. vec{bottom:top]. No good way around this.
1199 (structtag
1200 '("\\<\\([a-zA-Z][a-zA-Z0-9_]*:\\)[^:]" (1 font-lock-reference-face)))
1201
1202 ;; Structure names
1203 (structname
1204 '("\\({\\|\\<inherits\\s-\\)\\s-*\\([a-zA-Z][a-zA-Z0-9_]*\\)[},\t \n]"
1205 (2 font-lock-function-name-face)))
1206
1207 ;; Keyword parameters, like /xlog or ,xrange=[]
1208 ;; This is anchored to the comma preceeding the keyword.
1209 ;; Treats continuation lines, works only during whole buffer
1210 ;; fontification. Slow, use it only in fancy fontification.
1211 (keyword-parameters
1212 '("\\(,\\|[a-zA-Z0-9_](\\)[ \t]*\\(\\$[ \t]*\\(;.*\\)?\n\\([ \t]*\\(;.*\\)?\n\\)*[ \t]*\\)?\\(/[a-zA-Z_]\\sw*\\|[a-zA-Z_]\\sw*[ \t]*=\\)"
1213 (6 font-lock-reference-face)))
1214
1215 ;; System variables start with a bang.
1216 (system-variables
1217 '("\\(![a-zA-Z_0-9]+\\(\\.\\sw+\\)?\\)"
1218 (1 font-lock-variable-name-face)))
1219
1220 ;; Special and unusual operators (not used because too noisy)
1221 (special-operators
1222 '("[<>#]" (0 font-lock-keyword-face)))
1223
1224 ;; All operators (not used because too noisy)
1225 (all-operators
1226 '("[-*^#+<>/]" (0 font-lock-keyword-face)))
1227
1228 ;; Arrows with text property `idlwave-class'
1229 (class-arrows
1230 '(idlwave-match-class-arrows (0 idlwave-class-arrow-face))))
1231
1232 (defconst idlwave-font-lock-keywords-1
1233 (list pros-and-functions batch-files)
1234 "Subdued level highlighting for IDLWAVE mode.")
1235
1236 (defconst idlwave-font-lock-keywords-2
1237 (mapcar 'symbol-value idlwave-default-font-lock-items)
1238 "Medium level highlighting for IDLWAVE mode.")
1239
1240 (defconst idlwave-font-lock-keywords-3
1241 (list pros-and-functions
1242 batch-files
1243 idlwave-idl-keywords
1244 label goto
1245 structtag
1246 structname
1247 common-blocks
1248 keyword-parameters
1249 system-variables
1250 class-arrows)
1251 "Gaudy level highlighting for IDLWAVE mode."))
1252
1253 (defun idlwave-match-class-arrows (limit)
1254 ;; Match an object arrow with class property
1255 (and idlwave-store-inquired-class
1256 (re-search-forward "->" limit 'limit)
1257 (get-text-property (match-beginning 0) 'idlwave-class)))
1258
1259 (defvar idlwave-font-lock-keywords idlwave-font-lock-keywords-2
1260 "Default expressions to highlight in IDLWAVE mode.")
1261
1262 (defvar idlwave-font-lock-defaults
1263 '((idlwave-font-lock-keywords
1264 idlwave-font-lock-keywords-1
1265 idlwave-font-lock-keywords-2
1266 idlwave-font-lock-keywords-3)
1267 nil t
1268 ((?$ . "w") (?_ . "w") (?. . "w") (?| . "w") (?& . "w"))
1269 beginning-of-line))
1270
1271 (put 'idlwave-mode 'font-lock-defaults
1272 idlwave-font-lock-defaults) ; XEmacs
1273
1274 (defconst idlwave-comment-line-start-skip "^[ \t]*;"
1275 "Regexp to match the start of a full-line comment.
1276 That is the _beginning_ of a line containing a comment delimiter `;' preceded
1277 only by whitespace.")
1278
1279 (defconst idlwave-begin-block-reg
1280 "\\<\\(pro\\|function\\|begin\\|case\\|switch\\)\\>"
1281 "Regular expression to find the beginning of a block. The case does
1282 not matter. The search skips matches in comments.")
1283
1284 (defconst idlwave-begin-unit-reg "^\\s-*\\(pro\\|function\\)\\>\\|\\`"
1285 "Regular expression to find the beginning of a unit. The case does
1286 not matter.")
1287
1288 (defconst idlwave-end-unit-reg "^\\s-*\\(pro\\|function\\)\\>\\|\\'"
1289 "Regular expression to find the line that indicates the end of unit.
1290 This line is the end of buffer or the start of another unit. The case does
1291 not matter. The search skips matches in comments.")
1292
1293 (defconst idlwave-continue-line-reg "\\<\\$"
1294 "Regular expression to match a continued line.")
1295
1296 (defconst idlwave-end-block-reg
1297 "\\<end\\(\\|case\\|switch\\|else\\|for\\|if\\|rep\\|while\\)\\>"
1298 "Regular expression to find the end of a block. The case does
1299 not matter. The search skips matches found in comments.")
1300
1301 (defconst idlwave-block-matches
1302 '(("pro" . "end")
1303 ("function" . "end")
1304 ("case" . "endcase")
1305 ("else" . "endelse")
1306 ("for" . "endfor")
1307 ("then" . "endif")
1308 ("repeat" . "endrep")
1309 ("switch" . "endswitch")
1310 ("while" . "endwhile"))
1311 "Matches between statements and the corresponding END variant.
1312 The cars are the reserved words starting a block. If the block really
1313 begins with BEGIN, the cars are the reserved words before the begin
1314 which can be used to identify the block type.
1315 This is used to check for the correct END type, to close blocks and
1316 to expand generic end statements to their detailed form.")
1317
1318 (defconst idlwave-block-match-regexp
1319 "\\<\\(else\\|for\\|then\\|repeat\\|while\\)\\>"
1320 "Regular expression matching reserved words which can stand before
1321 blocks starting with a BEGIN statement. The matches must have associations
1322 `idlwave-block-matches'")
1323
1324 (defconst idlwave-identifier "[a-zA-Z_][a-zA-Z0-9$_]*"
1325 "Regular expression matching an IDL identifier.")
1326
1327 (defconst idlwave-sysvar (concat "!" idlwave-identifier)
1328 "Regular expression matching IDL system variables.")
1329
1330 (defconst idlwave-variable (concat idlwave-identifier "\\|" idlwave-sysvar)
1331 "Regular expression matching IDL variable names.")
1332
1333 (defconst idlwave-label (concat idlwave-identifier ":")
1334 "Regular expression matching IDL labels.")
1335
1336 (defconst idlwave-method-call (concat idlwave-identifier "\\s *->"
1337 "\\(\\s *" idlwave-identifier "::\\)?"
1338 ))
1339
1340 (defconst idlwave-statement-match
1341 (list
1342 ;; "endif else" is the only possible "end" that can be
1343 ;; followed by a statement on the same line.
1344 '(endelse . ("end\\(\\|if\\)\\s +else" "end\\(\\|if\\)\\s +else"))
1345 ;; all other "end"s can not be followed by a statement.
1346 (cons 'end (list idlwave-end-block-reg nil))
1347 '(if . ("if\\>" "then"))
1348 '(for . ("for\\>" "do"))
1349 '(begin . ("begin\\>" nil))
1350 '(pdef . ("pro\\>\\|function\\>" nil))
1351 '(while . ("while\\>" "do"))
1352 '(repeat . ("repeat\\>" "repeat"))
1353 '(goto . ("goto\\>" nil))
1354 '(case . ("case\\>" nil))
1355 '(switch . ("switch\\>" nil))
1356 (cons 'call (list (concat "\\(" idlwave-variable "\\) *= *"
1357 "\\(" idlwave-method-call "\\s *\\)?"
1358 idlwave-identifier
1359 "\\s *(") nil))
1360 (cons 'call (list (concat
1361 "\\(" idlwave-method-call "\\s *\\)?"
1362 idlwave-identifier
1363 "\\( *\\($\\|\\$\\)\\|\\s *,\\)") nil))
1364 (cons 'assign (list (concat
1365 "\\(" idlwave-variable "\\) *=") nil)))
1366
1367 "Associated list of statement matching regular expressions.
1368 Each regular expression matches the start of an IDL statement. The
1369 first element of each association is a symbol giving the statement
1370 type. The associated value is a list. The first element of this list
1371 is a regular expression matching the start of an IDL statement for
1372 identifying the statement type. The second element of this list is a
1373 regular expression for finding a substatement for the type. The
1374 substatement starts after the end of the found match modulo
1375 whitespace. If it is nil then the statement has no substatement. The
1376 list order matters since matching an assignment statement exactly is
1377 not possible without parsing. Thus assignment statement become just
1378 the leftover unidentified statements containing an equal sign." )
1379
1380 (defvar idlwave-fill-function 'auto-fill-function
1381 "IDL mode auto fill function.")
1382
1383 (defvar idlwave-comment-indent-function 'comment-indent-function
1384 "IDL mode comment indent function.")
1385
1386 ;; Note that this is documented in the v18 manuals as being a string
1387 ;; of length one rather than a single character.
1388 ;; The code in this file accepts either format for compatibility.
1389 (defvar idlwave-comment-indent-char ?\
1390 "Character to be inserted for IDL comment indentation.
1391 Normally a space.")
1392
1393 (defconst idlwave-continuation-char ?$
1394 "Character which is inserted as a last character on previous line by
1395 \\[idlwave-split-line] to begin a continuation line. Normally $.")
1396
1397 (defconst idlwave-mode-version "6.1_em22")
1398
1399 (defmacro idlwave-keyword-abbrev (&rest args)
1400 "Creates a function for abbrev hooks to call `idlwave-check-abbrev' with args."
1401 `(quote (lambda ()
1402 ,(append '(idlwave-check-abbrev) args))))
1403
1404 ;; If I take the time I can replace idlwave-keyword-abbrev with
1405 ;; idlwave-code-abbrev and remove the quoted abbrev check from
1406 ;; idlwave-check-abbrev. Then, e.g, (idlwave-keyword-abbrev 0 t) becomes
1407 ;; (idlwave-code-abbrev idlwave-check-abbrev 0 t). In fact I should change
1408 ;; the name of idlwave-check-abbrev to something like idlwave-modify-abbrev.
1409
1410 (defmacro idlwave-code-abbrev (&rest args)
1411 "Creates a function for abbrev hooks that ensures abbrevs are not quoted.
1412 Specifically, if the abbrev is in a comment or string it is unexpanded.
1413 Otherwise ARGS forms a list that is evaluated."
1414 `(quote (lambda ()
1415 ,(prin1-to-string args) ;; Puts the code in the doc string
1416 (if (idlwave-quoted)
1417 (progn (unexpand-abbrev) nil)
1418 ,(append args)))))
1419
1420 (defvar idlwave-mode-map (make-sparse-keymap)
1421 "Keymap used in IDL mode.")
1422
1423 (defvar idlwave-mode-syntax-table (make-syntax-table)
1424 "Syntax table in use in `idlwave-mode' buffers.")
1425
1426 (modify-syntax-entry ?+ "." idlwave-mode-syntax-table)
1427 (modify-syntax-entry ?- "." idlwave-mode-syntax-table)
1428 (modify-syntax-entry ?* "." idlwave-mode-syntax-table)
1429 (modify-syntax-entry ?/ "." idlwave-mode-syntax-table)
1430 (modify-syntax-entry ?^ "." idlwave-mode-syntax-table)
1431 (modify-syntax-entry ?# "." idlwave-mode-syntax-table)
1432 (modify-syntax-entry ?= "." idlwave-mode-syntax-table)
1433 (modify-syntax-entry ?% "." idlwave-mode-syntax-table)
1434 (modify-syntax-entry ?< "." idlwave-mode-syntax-table)
1435 (modify-syntax-entry ?> "." idlwave-mode-syntax-table)
1436 (modify-syntax-entry ?\' "\"" idlwave-mode-syntax-table)
1437 (modify-syntax-entry ?\" "\"" idlwave-mode-syntax-table)
1438 (modify-syntax-entry ?\\ "." idlwave-mode-syntax-table)
1439 (modify-syntax-entry ?_ "_" idlwave-mode-syntax-table)
1440 (modify-syntax-entry ?{ "(}" idlwave-mode-syntax-table)
1441 (modify-syntax-entry ?} "){" idlwave-mode-syntax-table)
1442 (modify-syntax-entry ?$ "_" idlwave-mode-syntax-table)
1443 (modify-syntax-entry ?. "." idlwave-mode-syntax-table)
1444 (modify-syntax-entry ?\; "<" idlwave-mode-syntax-table)
1445 (modify-syntax-entry ?\n ">" idlwave-mode-syntax-table)
1446 (modify-syntax-entry ?\f ">" idlwave-mode-syntax-table)
1447
1448 (defvar idlwave-find-symbol-syntax-table
1449 (copy-syntax-table idlwave-mode-syntax-table)
1450 "Syntax table that treats symbol characters as word characters.")
1451
1452 (modify-syntax-entry ?$ "w" idlwave-find-symbol-syntax-table)
1453 (modify-syntax-entry ?_ "w" idlwave-find-symbol-syntax-table)
1454 (modify-syntax-entry ?! "w" idlwave-find-symbol-syntax-table)
1455 (modify-syntax-entry ?. "w" idlwave-find-symbol-syntax-table)
1456
1457 (defmacro idlwave-with-special-syntax (&rest body)
1458 "Execute BODY with a different syntax table."
1459 `(let ((saved-syntax (syntax-table)))
1460 (unwind-protect
1461 (progn
1462 (set-syntax-table idlwave-find-symbol-syntax-table)
1463 ,@body)
1464 (set-syntax-table saved-syntax))))
1465
1466 ;(defmacro idlwave-with-special-syntax1 (&rest body)
1467 ; "Execute BODY with a different syntax table."
1468 ; `(let ((saved-syntax (syntax-table)))
1469 ; (unwind-protect
1470 ; (progn
1471 ; (set-syntax-table idlwave-find-symbol-syntax-table)
1472 ; ,@body)
1473 ; (set-syntax-table saved-syntax))))
1474
1475 (defun idlwave-action-and-binding (key cmd &optional select)
1476 "KEY and CMD are made into a key binding and an indent action.
1477 KEY is a string - same as for the `define-key' function. CMD is a
1478 function of no arguments or a list to be evaluated. CMD is bound to
1479 KEY in `idlwave-mode-map' by defining an anonymous function calling
1480 `self-insert-command' followed by CMD. If KEY contains more than one
1481 character a binding will only be set if SELECT is 'both.
1482
1483 \(KEY . CMD\) is also placed in the `idlwave-indent-expand-table',
1484 replacing any previous value for KEY. If a binding is not set then it
1485 will instead be placed in `idlwave-indent-action-table'.
1486
1487 If the optional argument SELECT is nil then an action and binding are
1488 created. If SELECT is 'noaction, then a binding is always set and no
1489 action is created. If SELECT is 'both then an action and binding
1490 will both be created even if KEY contains more than one character.
1491 Otherwise, if SELECT is non-nil then only an action is created.
1492
1493 Some examples:
1494 No spaces before and 1 after a comma
1495 (idlwave-action-and-binding \",\" '(idlwave-surround 0 1))
1496 A minimum of 1 space before and after `=' (see `idlwave-expand-equal').
1497 (idlwave-action-and-binding \"=\" '(idlwave-expand-equal -1 -1))
1498 Capitalize system variables - action only
1499 (idlwave-action-and-binding idlwave-sysvar '(capitalize-word 1) t)"
1500 (if (not (equal select 'noaction))
1501 ;; Add action
1502 (let* ((table (if select 'idlwave-indent-action-table
1503 'idlwave-indent-expand-table))
1504 (table-key (regexp-quote key))
1505 (cell (assoc table-key (eval table))))
1506 (if cell
1507 ;; Replace action command
1508 (setcdr cell cmd)
1509 ;; New action
1510 (set table (append (eval table) (list (cons table-key cmd)))))))
1511 ;; Make key binding for action
1512 (if (or (and (null select) (= (length key) 1))
1513 (equal select 'noaction)
1514 (equal select 'both))
1515 (define-key idlwave-mode-map key
1516 (append '(lambda ()
1517 (interactive)
1518 (self-insert-command 1))
1519 (list (if (listp cmd)
1520 cmd
1521 (list cmd)))))))
1522
1523 (fset 'idlwave-debug-map (make-sparse-keymap))
1524
1525 (define-key idlwave-mode-map "\C-c " 'idlwave-hard-tab)
1526 (define-key idlwave-mode-map [(control tab)] 'idlwave-hard-tab)
1527 ;(define-key idlwave-mode-map "\C-c\C- " 'idlwave-hard-tab)
1528 (define-key idlwave-mode-map "'" 'idlwave-show-matching-quote)
1529 (define-key idlwave-mode-map "\"" 'idlwave-show-matching-quote)
1530 (define-key idlwave-mode-map "\C-g" 'idlwave-keyboard-quit)
1531 (define-key idlwave-mode-map "\C-c;" 'idlwave-toggle-comment-region)
1532 (define-key idlwave-mode-map "\C-\M-a" 'idlwave-beginning-of-subprogram)
1533 (define-key idlwave-mode-map "\C-\M-e" 'idlwave-end-of-subprogram)
1534 (define-key idlwave-mode-map "\C-c{" 'idlwave-beginning-of-block)
1535 (define-key idlwave-mode-map "\C-c}" 'idlwave-end-of-block)
1536 (define-key idlwave-mode-map "\C-c]" 'idlwave-close-block)
1537 (define-key idlwave-mode-map [(meta control h)] 'idlwave-mark-subprogram)
1538 (define-key idlwave-mode-map "\M-\C-n" 'idlwave-forward-block)
1539 (define-key idlwave-mode-map "\M-\C-p" 'idlwave-backward-block)
1540 (define-key idlwave-mode-map "\M-\C-d" 'idlwave-down-block)
1541 (define-key idlwave-mode-map "\M-\C-u" 'idlwave-backward-up-block)
1542 (define-key idlwave-mode-map "\M-\r" 'idlwave-split-line)
1543 (define-key idlwave-mode-map "\M-\C-q" 'idlwave-indent-subprogram)
1544 (define-key idlwave-mode-map "\C-c\C-p" 'idlwave-previous-statement)
1545 (define-key idlwave-mode-map "\C-c\C-n" 'idlwave-next-statement)
1546 ;; (define-key idlwave-mode-map "\r" 'idlwave-newline)
1547 ;; (define-key idlwave-mode-map "\t" 'idlwave-indent-line)
1548 (define-key idlwave-mode-map [(shift iso-lefttab)] 'idlwave-indent-statement)
1549 (define-key idlwave-mode-map "\C-c\C-a" 'idlwave-auto-fill-mode)
1550 (define-key idlwave-mode-map "\M-q" 'idlwave-fill-paragraph)
1551 (define-key idlwave-mode-map "\M-s" 'idlwave-edit-in-idlde)
1552 (define-key idlwave-mode-map "\C-c\C-h" 'idlwave-doc-header)
1553 (define-key idlwave-mode-map "\C-c\C-m" 'idlwave-doc-modification)
1554 (define-key idlwave-mode-map "\C-c\C-c" 'idlwave-case)
1555 (define-key idlwave-mode-map "\C-c\C-d" 'idlwave-debug-map)
1556 (when (and (boundp 'idlwave-shell-debug-modifiers)
1557 (listp idlwave-shell-debug-modifiers)
1558 (not (equal idlwave-shell-debug-modifiers '())))
1559 ;; Bind the debug commands also with the special modifiers.
1560 (let ((shift (memq 'shift idlwave-shell-debug-modifiers))
1561 (mods-noshift (delq 'shift
1562 (copy-sequence idlwave-shell-debug-modifiers))))
1563 (define-key idlwave-mode-map
1564 (vector (append mods-noshift (list (if shift ?C ?c))))
1565 'idlwave-shell-save-and-run)
1566 (define-key idlwave-mode-map
1567 (vector (append mods-noshift (list (if shift ?B ?b))))
1568 'idlwave-shell-break-here)
1569 (define-key idlwave-mode-map
1570 (vector (append mods-noshift (list (if shift ?E ?e))))
1571 'idlwave-shell-run-region)))
1572 (define-key idlwave-mode-map "\C-c\C-d\C-c" 'idlwave-shell-save-and-run)
1573 (define-key idlwave-mode-map "\C-c\C-d\C-b" 'idlwave-shell-break-here)
1574 (define-key idlwave-mode-map "\C-c\C-d\C-e" 'idlwave-shell-run-region)
1575 (define-key idlwave-mode-map "\C-c\C-f" 'idlwave-for)
1576 ;; (define-key idlwave-mode-map "\C-c\C-f" 'idlwave-function)
1577 ;; (define-key idlwave-mode-map "\C-c\C-p" 'idlwave-procedure)
1578 (define-key idlwave-mode-map "\C-c\C-r" 'idlwave-repeat)
1579 (define-key idlwave-mode-map "\C-c\C-w" 'idlwave-while)
1580 (define-key idlwave-mode-map "\C-c\C-k" 'idlwave-kill-autoloaded-buffers)
1581 (define-key idlwave-mode-map "\C-c\C-s" 'idlwave-shell)
1582 (define-key idlwave-mode-map "\C-c\C-l" 'idlwave-shell-recenter-shell-window)
1583 (define-key idlwave-mode-map "\C-c\C-b" 'idlwave-list-buffer-load-path-shadows)
1584 (autoload 'idlwave-shell "idlw-shell"
1585 "Run an inferior IDL, with I/O through buffer `(idlwave-shell-buffer)'." t)
1586 (autoload 'idlwave-shell-send-command "idlw-shell")
1587 (autoload 'idlwave-shell-recenter-shell-window "idlw-shell"
1588 "Run `idlwave-shell' and switch back to current window" t)
1589 (autoload 'idlwave-shell-save-and-run "idlw-shell"
1590 "Save and run buffer under the shell." t)
1591 (autoload 'idlwave-shell-break-here "idlw-shell"
1592 "Set breakpoint in current line." t)
1593 (autoload 'idlwave-shell-run-region "idlw-shell"
1594 "Compile and run the region." t)
1595 (define-key idlwave-mode-map "\C-c\C-v" 'idlwave-find-module)
1596 (define-key idlwave-mode-map "\C-c\C-t" 'idlwave-find-module-this-file)
1597 (define-key idlwave-mode-map "\C-c?" 'idlwave-routine-info)
1598 (define-key idlwave-mode-map "\M-?" 'idlwave-context-help)
1599 (define-key idlwave-mode-map [(control meta ?\?)]
1600 'idlwave-help-assistant-help-with-topic)
1601 ;; Pickup both forms of Esc/Meta binding
1602 (define-key idlwave-mode-map [(meta tab)] 'idlwave-complete)
1603 (define-key idlwave-mode-map [?\e?\t] 'idlwave-complete)
1604 (define-key idlwave-mode-map "\M-\C-i" 'idlwave-complete)
1605 (define-key idlwave-mode-map "\C-c\C-i" 'idlwave-update-routine-info)
1606 (define-key idlwave-mode-map "\C-c=" 'idlwave-resolve)
1607 (define-key idlwave-mode-map
1608 (if (featurep 'xemacs) [(shift button3)] [(shift mouse-3)])
1609 'idlwave-mouse-context-help)
1610
1611 ;; Set action and key bindings.
1612 ;; See description of the function `idlwave-action-and-binding'.
1613 ;; Automatically add spaces for the following characters
1614
1615 ;; Actions for & are complicated by &&
1616 (idlwave-action-and-binding "&" 'idlwave-custom-ampersand-surround)
1617
1618 ;; Automatically add spaces to equal sign if not keyword. This needs
1619 ;; to go ahead of > and <, so >= and <= will be treated correctly
1620 (idlwave-action-and-binding "=" '(idlwave-expand-equal -1 -1))
1621
1622 ;; Actions for > and < are complicated by >=, <=, and ->...
1623 (idlwave-action-and-binding "<" '(idlwave-custom-ltgtr-surround nil))
1624 (idlwave-action-and-binding ">" '(idlwave-custom-ltgtr-surround 'gtr))
1625
1626 (idlwave-action-and-binding "," '(idlwave-surround 0 -1 1))
1627
1628
1629 ;;;
1630 ;;; Abbrev Section
1631 ;;;
1632 ;;; When expanding abbrevs and the abbrev hook moves backward, an extra
1633 ;;; space is inserted (this is the space typed by the user to expanded
1634 ;;; the abbrev).
1635 ;;;
1636 (defvar idlwave-mode-abbrev-table nil
1637 "Abbreviation table used for IDLWAVE mode")
1638 (define-abbrev-table 'idlwave-mode-abbrev-table ())
1639
1640 (defun idlwave-define-abbrev (name expansion hook &optional noprefix table)
1641 "Define-abbrev with backward compatibility.
1642
1643 If NOPREFIX is non-nil, don't prepend prefix character. Installs into
1644 idlwave-mode-abbrev-table unless TABLE is non-nil."
1645 (let ((abbrevs-changed nil) ;; mask the current value to avoid save
1646 (args (list (or table idlwave-mode-abbrev-table)
1647 (if noprefix name (concat idlwave-abbrev-start-char name))
1648 expansion
1649 hook)))
1650 (condition-case nil
1651 (apply 'define-abbrev (append args '(0 t)))
1652 (error (apply 'define-abbrev args)))))
1653
1654 (condition-case nil
1655 (modify-syntax-entry (string-to-char idlwave-abbrev-start-char)
1656 "w" idlwave-mode-syntax-table)
1657 (error nil))
1658
1659 ;;
1660 ;; Templates
1661 ;;
1662 (idlwave-define-abbrev "c" "" (idlwave-code-abbrev idlwave-case))
1663 (idlwave-define-abbrev "sw" "" (idlwave-code-abbrev idlwave-switch))
1664 (idlwave-define-abbrev "f" "" (idlwave-code-abbrev idlwave-for))
1665 (idlwave-define-abbrev "fu" "" (idlwave-code-abbrev idlwave-function))
1666 (idlwave-define-abbrev "pr" "" (idlwave-code-abbrev idlwave-procedure))
1667 (idlwave-define-abbrev "r" "" (idlwave-code-abbrev idlwave-repeat))
1668 (idlwave-define-abbrev "w" "" (idlwave-code-abbrev idlwave-while))
1669 (idlwave-define-abbrev "i" "" (idlwave-code-abbrev idlwave-if))
1670 (idlwave-define-abbrev "elif" "" (idlwave-code-abbrev idlwave-elif))
1671 ;;
1672 ;; Keywords, system functions, conversion routines
1673 ;;
1674 (idlwave-define-abbrev "ap" "arg_present()" (idlwave-keyword-abbrev 1))
1675 (idlwave-define-abbrev "b" "begin" (idlwave-keyword-abbrev 0 t))
1676 (idlwave-define-abbrev "co" "common" (idlwave-keyword-abbrev 0 t))
1677 (idlwave-define-abbrev "cb" "byte()" (idlwave-keyword-abbrev 1))
1678 (idlwave-define-abbrev "cx" "fix()" (idlwave-keyword-abbrev 1))
1679 (idlwave-define-abbrev "cl" "long()" (idlwave-keyword-abbrev 1))
1680 (idlwave-define-abbrev "cf" "float()" (idlwave-keyword-abbrev 1))
1681 (idlwave-define-abbrev "cs" "string()" (idlwave-keyword-abbrev 1))
1682 (idlwave-define-abbrev "cc" "complex()" (idlwave-keyword-abbrev 1))
1683 (idlwave-define-abbrev "cd" "double()" (idlwave-keyword-abbrev 1))
1684 (idlwave-define-abbrev "e" "else" (idlwave-keyword-abbrev 0 t))
1685 (idlwave-define-abbrev "ec" "endcase" 'idlwave-show-begin)
1686 (idlwave-define-abbrev "es" "endswitch" 'idlwave-show-begin)
1687 (idlwave-define-abbrev "ee" "endelse" 'idlwave-show-begin)
1688 (idlwave-define-abbrev "ef" "endfor" 'idlwave-show-begin)
1689 (idlwave-define-abbrev "ei" "endif else if" 'idlwave-show-begin)
1690 (idlwave-define-abbrev "el" "endif else" 'idlwave-show-begin)
1691 (idlwave-define-abbrev "en" "endif" 'idlwave-show-begin)
1692 (idlwave-define-abbrev "er" "endrep" 'idlwave-show-begin)
1693 (idlwave-define-abbrev "ew" "endwhile" 'idlwave-show-begin)
1694 (idlwave-define-abbrev "g" "goto," (idlwave-keyword-abbrev 0 t))
1695 (idlwave-define-abbrev "h" "help," (idlwave-keyword-abbrev 0))
1696 (idlwave-define-abbrev "k" "keyword_set()" (idlwave-keyword-abbrev 1))
1697 (idlwave-define-abbrev "n" "n_elements()" (idlwave-keyword-abbrev 1))
1698 (idlwave-define-abbrev "on" "on_error," (idlwave-keyword-abbrev 0))
1699 (idlwave-define-abbrev "oi" "on_ioerror," (idlwave-keyword-abbrev 0 1))
1700 (idlwave-define-abbrev "ow" "openw," (idlwave-keyword-abbrev 0))
1701 (idlwave-define-abbrev "or" "openr," (idlwave-keyword-abbrev 0))
1702 (idlwave-define-abbrev "ou" "openu," (idlwave-keyword-abbrev 0))
1703 (idlwave-define-abbrev "p" "print," (idlwave-keyword-abbrev 0))
1704 (idlwave-define-abbrev "pt" "plot," (idlwave-keyword-abbrev 0))
1705 (idlwave-define-abbrev "re" "read," (idlwave-keyword-abbrev 0))
1706 (idlwave-define-abbrev "rf" "readf," (idlwave-keyword-abbrev 0))
1707 (idlwave-define-abbrev "ru" "readu," (idlwave-keyword-abbrev 0))
1708 (idlwave-define-abbrev "rt" "return" (idlwave-keyword-abbrev 0))
1709 (idlwave-define-abbrev "sc" "strcompress()" (idlwave-keyword-abbrev 1))
1710 (idlwave-define-abbrev "sn" "strlen()" (idlwave-keyword-abbrev 1))
1711 (idlwave-define-abbrev "sl" "strlowcase()" (idlwave-keyword-abbrev 1))
1712 (idlwave-define-abbrev "su" "strupcase()" (idlwave-keyword-abbrev 1))
1713 (idlwave-define-abbrev "sm" "strmid()" (idlwave-keyword-abbrev 1))
1714 (idlwave-define-abbrev "sp" "strpos()" (idlwave-keyword-abbrev 1))
1715 (idlwave-define-abbrev "st" "strput()" (idlwave-keyword-abbrev 1))
1716 (idlwave-define-abbrev "sr" "strtrim()" (idlwave-keyword-abbrev 1))
1717 (idlwave-define-abbrev "t" "then" (idlwave-keyword-abbrev 0 t))
1718 (idlwave-define-abbrev "u" "until" (idlwave-keyword-abbrev 0 t))
1719 (idlwave-define-abbrev "wu" "writeu," (idlwave-keyword-abbrev 0))
1720 (idlwave-define-abbrev "iap" "if arg_present() then" (idlwave-keyword-abbrev 6))
1721 (idlwave-define-abbrev "ik" "if keyword_set() then" (idlwave-keyword-abbrev 6))
1722 (idlwave-define-abbrev "ine" "if n_elements() eq 0 then" (idlwave-keyword-abbrev 11))
1723 (idlwave-define-abbrev "inn" "if n_elements() ne 0 then" (idlwave-keyword-abbrev 11))
1724 (idlwave-define-abbrev "np" "n_params()" (idlwave-keyword-abbrev 0))
1725 (idlwave-define-abbrev "s" "size()" (idlwave-keyword-abbrev 1))
1726 (idlwave-define-abbrev "wi" "widget_info()" (idlwave-keyword-abbrev 1))
1727 (idlwave-define-abbrev "wc" "widget_control," (idlwave-keyword-abbrev 0))
1728 (idlwave-define-abbrev "pv" "ptr_valid()" (idlwave-keyword-abbrev 1))
1729 (idlwave-define-abbrev "ipv" "if ptr_valid() then" (idlwave-keyword-abbrev 6))
1730
1731 ;; This section is reserved words only. (From IDL user manual)
1732 ;;
1733 (idlwave-define-abbrev "and" "and" (idlwave-keyword-abbrev 0 t) t)
1734 (idlwave-define-abbrev "begin" "begin" (idlwave-keyword-abbrev 0 t) t)
1735 (idlwave-define-abbrev "break" "break" (idlwave-keyword-abbrev 0 t) t)
1736 (idlwave-define-abbrev "case" "case" (idlwave-keyword-abbrev 0 t) t)
1737 (idlwave-define-abbrev "common" "common" (idlwave-keyword-abbrev 0 t) t)
1738 (idlwave-define-abbrev "continue" "continue" (idlwave-keyword-abbrev 0 t) t)
1739 (idlwave-define-abbrev "do" "do" (idlwave-keyword-abbrev 0 t) t)
1740 (idlwave-define-abbrev "else" "else" (idlwave-keyword-abbrev 0 t) t)
1741 (idlwave-define-abbrev "end" "end" 'idlwave-show-begin-check t)
1742 (idlwave-define-abbrev "endcase" "endcase" 'idlwave-show-begin-check t)
1743 (idlwave-define-abbrev "endelse" "endelse" 'idlwave-show-begin-check t)
1744 (idlwave-define-abbrev "endfor" "endfor" 'idlwave-show-begin-check t)
1745 (idlwave-define-abbrev "endif" "endif" 'idlwave-show-begin-check t)
1746 (idlwave-define-abbrev "endrep" "endrep" 'idlwave-show-begin-check t)
1747 (idlwave-define-abbrev "endswitch" "endswitch" 'idlwave-show-begin-check t)
1748 (idlwave-define-abbrev "endwhi" "endwhi" 'idlwave-show-begin-check t)
1749 (idlwave-define-abbrev "endwhile" "endwhile" 'idlwave-show-begin-check t)
1750 (idlwave-define-abbrev "eq" "eq" (idlwave-keyword-abbrev 0 t) t)
1751 (idlwave-define-abbrev "for" "for" (idlwave-keyword-abbrev 0 t) t)
1752 (idlwave-define-abbrev "function" "function" (idlwave-keyword-abbrev 0 t) t)
1753 (idlwave-define-abbrev "ge" "ge" (idlwave-keyword-abbrev 0 t) t)
1754 (idlwave-define-abbrev "goto" "goto" (idlwave-keyword-abbrev 0 t) t)
1755 (idlwave-define-abbrev "gt" "gt" (idlwave-keyword-abbrev 0 t) t)
1756 (idlwave-define-abbrev "if" "if" (idlwave-keyword-abbrev 0 t) t)
1757 (idlwave-define-abbrev "le" "le" (idlwave-keyword-abbrev 0 t) t)
1758 (idlwave-define-abbrev "lt" "lt" (idlwave-keyword-abbrev 0 t) t)
1759 (idlwave-define-abbrev "mod" "mod" (idlwave-keyword-abbrev 0 t) t)
1760 (idlwave-define-abbrev "ne" "ne" (idlwave-keyword-abbrev 0 t) t)
1761 (idlwave-define-abbrev "not" "not" (idlwave-keyword-abbrev 0 t) t)
1762 (idlwave-define-abbrev "of" "of" (idlwave-keyword-abbrev 0 t) t)
1763 (idlwave-define-abbrev "on_ioerror" "on_ioerror" (idlwave-keyword-abbrev 0 t) t)
1764 (idlwave-define-abbrev "or" "or" (idlwave-keyword-abbrev 0 t) t)
1765 (idlwave-define-abbrev "pro" "pro" (idlwave-keyword-abbrev 0 t) t)
1766 (idlwave-define-abbrev "repeat" "repeat" (idlwave-keyword-abbrev 0 t) t)
1767 (idlwave-define-abbrev "switch" "switch" (idlwave-keyword-abbrev 0 t) t)
1768 (idlwave-define-abbrev "then" "then" (idlwave-keyword-abbrev 0 t) t)
1769 (idlwave-define-abbrev "until" "until" (idlwave-keyword-abbrev 0 t) t)
1770 (idlwave-define-abbrev "while" "while" (idlwave-keyword-abbrev 0 t) t)
1771 (idlwave-define-abbrev "xor" "xor" (idlwave-keyword-abbrev 0 t) t)
1772
1773 (defvar imenu-create-index-function)
1774 (defvar extract-index-name-function)
1775 (defvar prev-index-position-function)
1776 (defvar imenu-extract-index-name-function)
1777 (defvar imenu-prev-index-position-function)
1778 ;; defined later - so just make the compiler hush
1779 (defvar idlwave-mode-menu)
1780 (defvar idlwave-mode-debug-menu)
1781
1782 ;;;###autoload
1783 (defun idlwave-mode ()
1784 "Major mode for editing IDL source files (version 6.1_em22).
1785
1786 The main features of this mode are
1787
1788 1. Indentation and Formatting
1789 --------------------------
1790 Like other Emacs programming modes, C-j inserts a newline and indents.
1791 TAB is used for explicit indentation of the current line.
1792
1793 To start a continuation line, use \\[idlwave-split-line]. This
1794 function can also be used in the middle of a line to split the line
1795 at that point. When used inside a long constant string, the string
1796 is split at that point with the `+' concatenation operator.
1797
1798 Comments are indented as follows:
1799
1800 `;;;' Indentation remains unchanged.
1801 `;;' Indent like the surrounding code
1802 `;' Indent to a minimum column.
1803
1804 The indentation of comments starting in column 0 is never changed.
1805
1806 Use \\[idlwave-fill-paragraph] to refill a paragraph inside a
1807 comment. The indentation of the second line of the paragraph
1808 relative to the first will be retained. Use
1809 \\[idlwave-auto-fill-mode] to toggle auto-fill mode for these
1810 comments. When the variable `idlwave-fill-comment-line-only' is
1811 nil, code can also be auto-filled and auto-indented.
1812
1813 To convert pre-existing IDL code to your formatting style, mark the
1814 entire buffer with \\[mark-whole-buffer] and execute
1815 \\[idlwave-expand-region-abbrevs]. Then mark the entire buffer
1816 again followed by \\[indent-region] (`indent-region').
1817
1818 2. Routine Info
1819 ------------
1820 IDLWAVE displays information about the calling sequence and the
1821 accepted keyword parameters of a procedure or function with
1822 \\[idlwave-routine-info]. \\[idlwave-find-module] jumps to the
1823 source file of a module. These commands know about system
1824 routines, all routines in idlwave-mode buffers and (when the
1825 idlwave-shell is active) about all modules currently compiled under
1826 this shell. It also makes use of pre-compiled or custom-scanned
1827 user and library catalogs many popular libraries ship with by
1828 default. Use \\[idlwave-update-routine-info] to update this
1829 information, which is also used for completion (see item 4).
1830
1831 3. Online IDL Help
1832 ---------------
1833
1834 \\[idlwave-context-help] displays the IDL documentation relevant
1835 for the system variable, keyword, or routines at point. A single
1836 key stroke gets you directly to the right place in the docs. See
1837 the manual to configure where and how the HTML help is displayed.
1838
1839 4. Completion
1840 ----------
1841 \\[idlwave-complete] completes the names of procedures, functions
1842 class names, keyword parameters, system variables and tags, class
1843 tags, structure tags, filenames and much more. It is context
1844 sensitive and figures out what is expected at point. Lower case
1845 strings are completed in lower case, other strings in mixed or
1846 upper case.
1847
1848 5. Code Templates and Abbreviations
1849 --------------------------------
1850 Many Abbreviations are predefined to expand to code fragments and templates.
1851 The abbreviations start generally with a `\\`. Some examples
1852
1853 \\pr PROCEDURE template
1854 \\fu FUNCTION template
1855 \\c CASE statement template
1856 \\sw SWITCH statement template
1857 \\f FOR loop template
1858 \\r REPEAT Loop template
1859 \\w WHILE loop template
1860 \\i IF statement template
1861 \\elif IF-ELSE statement template
1862 \\b BEGIN
1863
1864 For a full list, use \\[idlwave-list-abbrevs]. Some templates also
1865 have direct keybindings - see the list of keybindings below.
1866
1867 \\[idlwave-doc-header] inserts a documentation header at the
1868 beginning of the current program unit (pro, function or main).
1869 Change log entries can be added to the current program unit with
1870 \\[idlwave-doc-modification].
1871
1872 6. Automatic Case Conversion
1873 -------------------------
1874 The case of reserved words and some abbrevs is controlled by
1875 `idlwave-reserved-word-upcase' and `idlwave-abbrev-change-case'.
1876
1877 7. Automatic END completion
1878 ------------------------
1879 If the variable `idlwave-expand-generic-end' is non-nil, each END typed
1880 will be converted to the specific version, like ENDIF, ENDFOR, etc.
1881
1882 8. Hooks
1883 -----
1884 Loading idlwave.el runs `idlwave-load-hook'.
1885 Turning on `idlwave-mode' runs `idlwave-mode-hook'.
1886
1887 9. Documentation and Customization
1888 -------------------------------
1889 Info documentation for this package is available. Use
1890 \\[idlwave-info] to display (complain to your sysadmin if that does
1891 not work). For Postscript, PDF, and HTML versions of the
1892 documentation, check IDLWAVE's homepage at `http://idlwave.org'.
1893 IDLWAVE has customize support - see the group `idlwave'.
1894
1895 10.Keybindings
1896 -----------
1897 Here is a list of all keybindings of this mode.
1898 If some of the key bindings below show with ??, use \\[describe-key]
1899 followed by the key sequence to see what the key sequence does.
1900
1901 \\{idlwave-mode-map}"
1902
1903 (interactive)
1904 (kill-all-local-variables)
1905
1906 (if idlwave-startup-message
1907 (message "Emacs IDLWAVE mode version %s." idlwave-mode-version))
1908 (setq idlwave-startup-message nil)
1909
1910 (setq local-abbrev-table idlwave-mode-abbrev-table)
1911 (set-syntax-table idlwave-mode-syntax-table)
1912
1913 (set (make-local-variable 'indent-line-function) 'idlwave-indent-and-action)
1914
1915 (make-local-variable idlwave-comment-indent-function)
1916 (set idlwave-comment-indent-function 'idlwave-comment-hook)
1917
1918 (set (make-local-variable 'comment-start-skip) ";+[ \t]*")
1919 (set (make-local-variable 'comment-start) ";")
1920 (set (make-local-variable 'comment-add) 1) ; ";;" for new and regions
1921 (set (make-local-variable 'require-final-newline) t)
1922 (set (make-local-variable 'abbrev-all-caps) t)
1923 (set (make-local-variable 'indent-tabs-mode) nil)
1924 (set (make-local-variable 'completion-ignore-case) t)
1925
1926 (use-local-map idlwave-mode-map)
1927
1928 (when (featurep 'easymenu)
1929 (easy-menu-add idlwave-mode-menu idlwave-mode-map)
1930 (easy-menu-add idlwave-mode-debug-menu idlwave-mode-map))
1931
1932 (setq mode-name "IDLWAVE")
1933 (setq major-mode 'idlwave-mode)
1934 (setq abbrev-mode t)
1935
1936 (set (make-local-variable idlwave-fill-function) 'idlwave-auto-fill)
1937 (setq comment-end "")
1938 (set (make-local-variable 'comment-multi-line) nil)
1939 (set (make-local-variable 'paragraph-separate)
1940 "[ \t\f]*$\\|[ \t]*;+[ \t]*$\\|;+[+=-_*]+$")
1941 (set (make-local-variable 'paragraph-start) "[ \t\f]\\|[ \t]*;+[ \t]")
1942 (set (make-local-variable 'paragraph-ignore-fill-prefix) nil)
1943 (set (make-local-variable 'parse-sexp-ignore-comments) t)
1944
1945 ;; ChangeLog
1946 (set (make-local-variable 'add-log-current-defun-function)
1947 'idlwave-current-routine-fullname)
1948
1949 ;; Set tag table list to use IDLTAGS as file name.
1950 (if (boundp 'tag-table-alist)
1951 (add-to-list 'tag-table-alist '("\\.pro$" . "IDLTAGS")))
1952
1953 ;; Font-lock additions
1954 ;; Following line is for Emacs - XEmacs uses the corresponding property
1955 ;; on the `idlwave-mode' symbol.
1956 (set (make-local-variable 'font-lock-defaults) idlwave-font-lock-defaults)
1957 (set (make-local-variable 'font-lock-mark-block-function)
1958 'idlwave-mark-subprogram)
1959 (set (make-local-variable 'font-lock-fontify-region-function)
1960 'idlwave-font-lock-fontify-region)
1961
1962 ;; Imenu setup
1963 (set (make-local-variable 'imenu-create-index-function)
1964 'imenu-default-create-index-function)
1965 (set (make-local-variable 'imenu-extract-index-name-function)
1966 'idlwave-unit-name)
1967 (set (make-local-variable 'imenu-prev-index-position-function)
1968 'idlwave-prev-index-position)
1969
1970 ;; HideShow setup
1971 (add-to-list 'hs-special-modes-alist
1972 (list 'idlwave-mode
1973 idlwave-begin-block-reg
1974 idlwave-end-block-reg
1975 ";"
1976 'idlwave-forward-block nil))
1977
1978 ;; Make a local post-command-hook and add our hook to it
1979 ;; NB: `make-local-hook' needed for older/alternative Emacs compatibility
1980 ;; (make-local-hook 'post-command-hook)
1981 (add-hook 'post-command-hook 'idlwave-command-hook nil 'local)
1982
1983 ;; Make local hooks for buffer updates
1984 ;; NB: `make-local-hook' needed for older/alternative Emacs compatibility
1985 ;; (make-local-hook 'kill-buffer-hook)
1986 (add-hook 'kill-buffer-hook 'idlwave-kill-buffer-update nil 'local)
1987 ;; (make-local-hook 'after-save-hook)
1988 (add-hook 'after-save-hook 'idlwave-save-buffer-update nil 'local)
1989 (add-hook 'after-save-hook 'idlwave-revoke-license-to-kill nil 'local)
1990
1991 ;; Setup directories and file, if necessary
1992 (idlwave-setup)
1993
1994 ;; Update the routine info with info about current buffer?
1995 (idlwave-new-buffer-update)
1996
1997 ;; Check help location
1998 (idlwave-help-check-locations)
1999
2000 ;; Run the mode hook
2001 (run-mode-hooks 'idlwave-mode-hook))
2002
2003 (defvar idlwave-setup-done nil)
2004 (defun idlwave-setup ()
2005 (unless idlwave-setup-done
2006 (if (not (file-directory-p idlwave-config-directory))
2007 (make-directory idlwave-config-directory))
2008 (setq
2009 idlwave-user-catalog-file (expand-file-name
2010 idlwave-user-catalog-file
2011 idlwave-config-directory)
2012 idlwave-xml-system-rinfo-converted-file
2013 (expand-file-name
2014 idlwave-xml-system-rinfo-converted-file
2015 idlwave-config-directory)
2016 idlwave-path-file (expand-file-name
2017 idlwave-path-file
2018 idlwave-config-directory))
2019 (idlwave-read-paths) ; we may need these early
2020 (setq idlwave-setup-done t)))
2021
2022 (defun idlwave-font-lock-fontify-region (beg end &optional verbose)
2023 "Fontify continuation lines correctly."
2024 (let (pos)
2025 (save-excursion
2026 (goto-char beg)
2027 (forward-line -1)
2028 (when (setq pos (idlwave-is-continuation-line))
2029 (goto-char pos)
2030 (idlwave-beginning-of-statement)
2031 (setq beg (point)))))
2032 (font-lock-default-fontify-region beg end verbose))
2033
2034 ;;
2035 ;; Code Formatting ----------------------------------------------------
2036 ;;
2037
2038 (defun idlwave-hard-tab ()
2039 "Inserts TAB in buffer in current position."
2040 (interactive)
2041 (insert "\t"))
2042
2043 ;;; This stuff is experimental
2044
2045 (defvar idlwave-command-hook nil
2046 "If non-nil, a list that can be evaluated using `eval'.
2047 It is evaluated in the lisp function `idlwave-command-hook' which is
2048 placed in `post-command-hook'.")
2049
2050 (defun idlwave-command-hook ()
2051 "Command run after every command.
2052 Evaluates a non-nil value of the *variable* `idlwave-command-hook' and
2053 sets the variable to zero afterwards."
2054 (and idlwave-command-hook
2055 (listp idlwave-command-hook)
2056 (condition-case nil
2057 (eval idlwave-command-hook)
2058 (error nil)))
2059 (setq idlwave-command-hook nil))
2060
2061 ;;; End experiment
2062
2063 ;; It would be better to use expand.el for better abbrev handling and
2064 ;; versatility.
2065
2066 (defun idlwave-check-abbrev (arg &optional reserved)
2067 "Reverses abbrev expansion if in comment or string.
2068 Argument ARG is the number of characters to move point
2069 backward if `idlwave-abbrev-move' is non-nil.
2070 If optional argument RESERVED is non-nil then the expansion
2071 consists of reserved words, which will be capitalized if
2072 `idlwave-reserved-word-upcase' is non-nil.
2073 Otherwise, the abbrev will be capitalized if `idlwave-abbrev-change-case'
2074 is non-nil, unless its value is \`down in which case the abbrev will be
2075 made into all lowercase.
2076 Returns non-nil if abbrev is left expanded."
2077 (if (idlwave-quoted)
2078 (progn (unexpand-abbrev)
2079 nil)
2080 (if (and reserved idlwave-reserved-word-upcase)
2081 (upcase-region last-abbrev-location (point))
2082 (cond
2083 ((equal idlwave-abbrev-change-case 'down)
2084 (downcase-region last-abbrev-location (point)))
2085 (idlwave-abbrev-change-case
2086 (upcase-region last-abbrev-location (point)))))
2087 (if (and idlwave-abbrev-move (> arg 0))
2088 (if (boundp 'post-command-hook)
2089 (setq idlwave-command-hook (list 'backward-char (1+ arg)))
2090 (backward-char arg)))
2091 t))
2092
2093 (defun idlwave-in-comment ()
2094 "Returns t if point is inside a comment, nil otherwise."
2095 (save-excursion
2096 (let ((here (point)))
2097 (and (idlwave-goto-comment) (> here (point))))))
2098
2099 (defun idlwave-goto-comment ()
2100 "Move to start of comment delimiter on current line.
2101 Moves to end of line if there is no comment delimiter.
2102 Ignores comment delimiters in strings.
2103 Returns point if comment found and nil otherwise."
2104 (let ((eos (progn (end-of-line) (point)))
2105 (data (match-data))
2106 found)
2107 ;; Look for first comment delimiter not in a string
2108 (beginning-of-line)
2109 (setq found (search-forward comment-start eos 'lim))
2110 (while (and found (idlwave-in-quote))
2111 (setq found (search-forward comment-start eos 'lim)))
2112 (store-match-data data)
2113 (and found (not (idlwave-in-quote))
2114 (progn
2115 (backward-char 1)
2116 (point)))))
2117
2118 (defvar transient-mark-mode)
2119 (defvar zmacs-regions)
2120 (defvar mark-active)
2121 (defun idlwave-region-active-p ()
2122 "Is transient-mark-mode on and the region active?
2123 Works on both Emacs and XEmacs."
2124 (if (featurep 'xemacs)
2125 (and zmacs-regions (region-active-p))
2126 (and transient-mark-mode mark-active)))
2127
2128 (defun idlwave-show-matching-quote ()
2129 "Insert quote and show matching quote if this is end of a string."
2130 (interactive)
2131 (let ((bq (idlwave-in-quote))
2132 (inq last-command-char))
2133 (if (and bq (not (idlwave-in-comment)))
2134 (let ((delim (char-after bq)))
2135 (insert inq)
2136 (if (eq inq delim)
2137 (save-excursion
2138 (goto-char bq)
2139 (sit-for 1))))
2140 ;; Not the end of a string
2141 (insert inq))))
2142
2143 (defun idlwave-show-begin-check ()
2144 "Ensure that the previous word was a token before `idlwave-show-begin'.
2145 An END token must be preceded by whitespace."
2146 (if (not (idlwave-quoted))
2147 (if
2148 (save-excursion
2149 (backward-word 1)
2150 (backward-char 1)
2151 (looking-at "[ \t\n\f]"))
2152 (idlwave-show-begin))))
2153
2154 (defun idlwave-show-begin ()
2155 "Finds the start of current block and blinks to it for a second.
2156 Also checks if the correct end statement has been used."
2157 ;; All end statements are reserved words
2158 ;; Re-indent end line
2159 ;;(insert-char ?\ 1) ;; So indent, etc. work well
2160 ;;(backward-char 1)
2161 (let* ((pos (point-marker))
2162 (last-abbrev-marker (copy-marker last-abbrev-location))
2163 (eol-pos (save-excursion (end-of-line) (point)))
2164 begin-pos end-pos end end1 )
2165 (if idlwave-reindent-end (idlwave-indent-line))
2166 (setq last-abbrev-location (marker-position last-abbrev-marker))
2167 (when (and (idlwave-check-abbrev 0 t)
2168 idlwave-show-block)
2169 (save-excursion
2170 ;; Move inside current block
2171 (goto-char last-abbrev-marker)
2172 (idlwave-block-jump-out -1 'nomark)
2173 (setq begin-pos (point))
2174 (idlwave-block-jump-out 1 'nomark)
2175 (setq end-pos (point))
2176 (if (> end-pos eol-pos)
2177 (setq end-pos pos))
2178 (goto-char end-pos)
2179 (setq end (buffer-substring
2180 (progn
2181 (skip-chars-backward "a-zA-Z")
2182 (point))
2183 end-pos))
2184 (goto-char begin-pos)
2185 (when (setq end1 (cdr (idlwave-block-master)))
2186 (cond
2187 ((null end1)) ; no-operation
2188 ((string= (downcase end) (downcase end1))
2189 (sit-for 1))
2190 ((string= (downcase end) "end")
2191 ;; A generic end
2192 (if idlwave-expand-generic-end
2193 (save-excursion
2194 (goto-char pos)
2195 (backward-char 3)
2196 (insert (if (string= end "END") (upcase end1) end1))
2197 (delete-char 3)))
2198 (sit-for 1))
2199 (t
2200 (beep)
2201 (message "Warning: Shouldn't this be \"%s\" instead of \"%s\"?"
2202 end1 end)
2203 (sit-for 1))))))))
2204 ;;(delete-char 1))
2205
2206 (defun idlwave-block-master ()
2207 (let ((case-fold-search t))
2208 (save-excursion
2209 (cond
2210 ((looking-at "pro\\|case\\|switch\\|function\\>")
2211 (assoc (downcase (match-string 0)) idlwave-block-matches))
2212 ((looking-at "begin\\>")
2213 (let ((limit (save-excursion
2214 (idlwave-beginning-of-statement)
2215 (point))))
2216 (cond
2217 ((re-search-backward ":[ \t]*\\=" limit t)
2218 ;; seems to be a case thing
2219 '("begin" . "end"))
2220 ((re-search-backward idlwave-block-match-regexp limit t)
2221 (assoc (downcase (match-string 1))
2222 idlwave-block-matches))
2223 (t
2224 ;; Just a normal block
2225 '("begin" . "end")))))
2226 (t nil)))))
2227
2228 (defun idlwave-close-block ()
2229 "Terminate the current block with the correct END statement."
2230 (interactive)
2231 ;; Start new line if we are not in a new line
2232 (unless (save-excursion
2233 (skip-chars-backward " \t")
2234 (bolp))
2235 (let ((idlwave-show-block nil))
2236 (newline-and-indent)))
2237 (let ((last-abbrev-location (point))) ; for upcasing
2238 (insert "end")
2239 (idlwave-show-begin)))
2240
2241 (defun idlwave-custom-ampersand-surround (&optional is-action)
2242 "Surround &, leaving room for && (which surrround as well)."
2243 (let* ((prev-char (char-after (- (point) 2)))
2244 (next-char (char-after (point)))
2245 (amp-left (eq prev-char ?&))
2246 (amp-right (eq next-char ?&))
2247 (len (if amp-left 2 1)))
2248 (unless amp-right ;no need to do it twice, amp-left will catch it.
2249 (idlwave-surround -1 (if (or is-action amp-left) -1) len))))
2250
2251 (defun idlwave-custom-ltgtr-surround (gtr &optional is-action)
2252 "Surround > and < by blanks, leaving room for >= and <=, and considering ->."
2253 (let* ((prev-char (char-after (- (point) 2)))
2254 (next-char (char-after (point)))
2255 (method-invoke (and gtr (eq prev-char ?-)))
2256 (len (if method-invoke 2 1)))
2257 (unless (eq next-char ?=)
2258 ;; Key binding: pad only on left, to save for possible >=/<=
2259 (idlwave-surround -1 (if (or is-action method-invoke) -1) len))))
2260
2261 (defun idlwave-surround (&optional before after length is-action)
2262 "Surround the LENGTH characters before point with blanks.
2263 LENGTH defaults to 1.
2264 Optional arguments BEFORE and AFTER affect the behavior before and
2265 after the characters (see also description of `idlwave-make-space'):
2266
2267 nil do nothing
2268 0 force no spaces
2269 integer > 0 force exactly n spaces
2270 integer < 0 at least |n| spaces
2271
2272 The function does nothing if any of the following conditions is true:
2273 - `idlwave-surround-by-blank' is nil
2274 - the character before point is inside a string or comment"
2275 (when (and idlwave-surround-by-blank (not (idlwave-quoted)))
2276 (let ((length (or length 1))) ; establish a default for LENGTH
2277 (backward-char length)
2278 (save-restriction
2279 (let ((here (point)))
2280 (skip-chars-backward " \t")
2281 (if (bolp)
2282 ;; avoid clobbering indent
2283 (progn
2284 (move-to-column (idlwave-calculate-indent))
2285 (if (<= (point) here)
2286 (narrow-to-region (point) here))
2287 (goto-char here)))
2288 (idlwave-make-space before))
2289 (skip-chars-forward " \t"))
2290 (forward-char length)
2291 (idlwave-make-space after)
2292 ;; Check to see if the line should auto wrap
2293 (if (and (equal (char-after (1- (point))) ?\ )
2294 (> (current-column) fill-column))
2295 (funcall auto-fill-function)))))
2296
2297 (defun idlwave-make-space (n)
2298 "Make space at point.
2299 The space affected is all the spaces and tabs around point.
2300 If n is non-nil then point is left abs(n) spaces from the beginning of
2301 the contiguous space.
2302 The amount of space at point is determined by N.
2303 If the value of N is:
2304 nil - do nothing.
2305 > 0 - exactly N spaces.
2306 < 0 - a minimum of -N spaces, i.e., do not change if there are
2307 already -N spaces.
2308 0 - no spaces (i.e. remove any existing space)."
2309 (if (integerp n)
2310 (let
2311 ((start-col (progn (skip-chars-backward " \t") (current-column)))
2312 (left (point))
2313 (end-col (progn (skip-chars-forward " \t") (current-column))))
2314 (delete-horizontal-space)
2315 (cond
2316 ((> n 0)
2317 (idlwave-indent-to (+ start-col n))
2318 (goto-char (+ left n)))
2319 ((< n 0)
2320 (idlwave-indent-to end-col (- n))
2321 (goto-char (- left n)))
2322 ;; n = 0, done
2323 ))))
2324
2325 (defun idlwave-newline ()
2326 "Inserts a newline and indents the current and previous line."
2327 (interactive)
2328 ;;
2329 ;; Handle unterminated single and double quotes
2330 ;; If not in a comment and in a string then insertion of a newline
2331 ;; will mean unbalanced quotes.
2332 ;;
2333 (if (and (not (idlwave-in-comment)) (idlwave-in-quote))
2334 (progn (beep)
2335 (message "Warning: unbalanced quotes?")))
2336 (newline)
2337 ;;
2338 ;; The current line is being split, the cursor should be at the
2339 ;; beginning of the new line skipping the leading indentation.
2340 ;;
2341 ;; The reason we insert the new line before indenting is that the
2342 ;; indenting could be confused by keywords (e.g. END) on the line
2343 ;; after the split point. This prevents us from just using
2344 ;; `indent-for-tab-command' followed by `newline-and-indent'.
2345 ;;
2346 (beginning-of-line 0)
2347 (idlwave-indent-line)
2348 (forward-line)
2349 (idlwave-indent-line))
2350
2351 ;;
2352 ;; Use global variable 'comment-column' to set parallel comment
2353 ;;
2354 ;; Modeled on lisp.el
2355 ;; Emacs Lisp and IDL (Wave CL) have identical comment syntax
2356 (defun idlwave-comment-hook ()
2357 "Compute indent for the beginning of the IDL comment delimiter."
2358 (if (or (looking-at idlwave-no-change-comment)
2359 (if idlwave-begin-line-comment
2360 (looking-at idlwave-begin-line-comment)
2361 (looking-at "^;")))
2362 (current-column)
2363 (if (looking-at idlwave-code-comment)
2364 (if (save-excursion (skip-chars-backward " \t") (bolp))
2365 ;; On line by itself, indent as code
2366 (let ((tem (idlwave-calculate-indent)))
2367 (if (listp tem) (car tem) tem))
2368 ;; after code - do not change
2369 (current-column))
2370 (skip-chars-backward " \t")
2371 (max (if (bolp) 0 (1+ (current-column)))
2372 comment-column))))
2373
2374 (defun idlwave-split-line ()
2375 "Continue line by breaking line at point and indent the lines.
2376 For a code line insert continuation marker. If the line is a line comment
2377 then the new line will contain a comment with the same indentation.
2378 Splits strings with the IDL operator `+' if `idlwave-split-line-string' is
2379 non-nil."
2380 (interactive)
2381 ;; Expand abbreviation, just like normal RET would.
2382 (and abbrev-mode (expand-abbrev))
2383 (let (beg)
2384 (if (not (idlwave-in-comment))
2385 ;; For code line add continuation.
2386 ;; Check if splitting a string.
2387 (progn
2388 (if (setq beg (idlwave-in-quote))
2389 (if idlwave-split-line-string
2390 ;; Split the string.
2391 (progn (insert (setq beg (char-after beg)) " + "
2392 idlwave-continuation-char beg)
2393 (backward-char 1)
2394 (newline-and-indent)
2395 (forward-char 1))
2396 ;; Do not split the string.
2397 (beep)
2398 (message "Warning: continuation inside string!!")
2399 (insert " " idlwave-continuation-char))
2400 ;; Not splitting a string.
2401 (if (not (member (char-before) '(?\ ?\t)))
2402 (insert " "))
2403 (insert idlwave-continuation-char)
2404 (newline-and-indent)))
2405 (indent-new-comment-line))
2406 ;; Indent previous line
2407 (setq beg (- (point-max) (point)))
2408 (forward-line -1)
2409 (idlwave-indent-line)
2410 (goto-char (- (point-max) beg))
2411 ;; Reindent new line
2412 (idlwave-indent-line)))
2413
2414 (defun idlwave-beginning-of-subprogram ()
2415 "Moves point to the beginning of the current program unit."
2416 (interactive)
2417 (idlwave-find-key idlwave-begin-unit-reg -1))
2418
2419 (defun idlwave-end-of-subprogram ()
2420 "Moves point to the start of the next program unit."
2421 (interactive)
2422 (idlwave-end-of-statement)
2423 (idlwave-find-key idlwave-end-unit-reg 1))
2424
2425 (defun idlwave-mark-statement ()
2426 "Mark current IDL statement."
2427 (interactive)
2428 (idlwave-end-of-statement)
2429 (let ((end (point)))
2430 (idlwave-beginning-of-statement)
2431 (push-mark end nil t)))
2432
2433 (defun idlwave-mark-block ()
2434 "Mark containing block."
2435 (interactive)
2436 (idlwave-end-of-statement)
2437 (idlwave-backward-up-block -1)
2438 (idlwave-end-of-statement)
2439 (let ((end (point)))
2440 (idlwave-backward-block)
2441 (idlwave-beginning-of-statement)
2442 (push-mark end nil t)))
2443
2444
2445 (defun idlwave-mark-subprogram ()
2446 "Put mark at beginning of program, point at end.
2447 The marks are pushed."
2448 (interactive)
2449 (idlwave-end-of-statement)
2450 (idlwave-beginning-of-subprogram)
2451 (let ((beg (point)))
2452 (idlwave-forward-block)
2453 (push-mark beg nil t))
2454 (exchange-point-and-mark))
2455
2456 (defun idlwave-backward-up-block (&optional arg)
2457 "Move to beginning of enclosing block if prefix ARG >= 0.
2458 If prefix ARG < 0 then move forward to enclosing block end."
2459 (interactive "p")
2460 (idlwave-block-jump-out (- arg) 'nomark))
2461
2462 (defun idlwave-beginning-of-block ()
2463 "Go to the beginning of the current block."
2464 (interactive)
2465 (idlwave-block-jump-out -1 'nomark)
2466 (forward-word 1))
2467
2468 (defun idlwave-end-of-block ()
2469 "Go to the beginning of the current block."
2470 (interactive)
2471 (idlwave-block-jump-out 1 'nomark)
2472 (backward-word 1))
2473
2474 (defun idlwave-forward-block (&optional arg)
2475 "Move across next nested block."
2476 (interactive)
2477 (let ((arg (or arg 1)))
2478 (if (idlwave-down-block arg)
2479 (idlwave-block-jump-out arg 'nomark))))
2480
2481 (defun idlwave-backward-block ()
2482 "Move backward across previous nested block."
2483 (interactive)
2484 (if (idlwave-down-block -1)
2485 (idlwave-block-jump-out -1 'nomark)))
2486
2487 (defun idlwave-down-block (&optional arg)
2488 "Go down a block.
2489 With ARG: ARG >= 0 go forwards, ARG < 0 go backwards.
2490 Returns non-nil if successfull."
2491 (interactive "p")
2492 (let (status)
2493 (if (< arg 0)
2494 ;; Backward
2495 (let ((eos (save-excursion
2496 (idlwave-block-jump-out -1 'nomark)
2497 (point))))
2498 (if (setq status (idlwave-find-key
2499 idlwave-end-block-reg -1 'nomark eos))
2500 (idlwave-beginning-of-statement)
2501 (message "No nested block before beginning of containing block.")))
2502 ;; Forward
2503 (let ((eos (save-excursion
2504 (idlwave-block-jump-out 1 'nomark)
2505 (point))))
2506 (if (setq status (idlwave-find-key
2507 idlwave-begin-block-reg 1 'nomark eos))
2508 (idlwave-end-of-statement)
2509 (message "No nested block before end of containing block."))))
2510 status))
2511
2512 (defun idlwave-mark-doclib ()
2513 "Put point at beginning of doc library header, mark at end.
2514 The marks are pushed."
2515 (interactive)
2516 (let (beg
2517 (here (point)))
2518 (goto-char (point-max))
2519 (if (re-search-backward idlwave-doclib-start nil t)
2520 (progn
2521 (setq beg (progn (beginning-of-line) (point)))
2522 (if (re-search-forward idlwave-doclib-end nil t)
2523 (progn
2524 (forward-line 1)
2525 (push-mark beg nil t)
2526 (message "Could not find end of doc library header.")))
2527 (message "Could not find doc library header start.")
2528 (goto-char here)))))
2529
2530 (defun idlwave-current-routine-fullname ()
2531 (let ((name (idlwave-current-routine)))
2532 (idlwave-make-full-name (nth 2 name) (car name))))
2533
2534 (defun idlwave-current-routine ()
2535 "Return (NAME TYPE CLASS) of current routine."
2536 (idlwave-routines)
2537 (save-excursion
2538 (idlwave-beginning-of-subprogram)
2539 (if (looking-at "[ \t]*\\<\\(pro\\|function\\)\\>\\s-+\\(\\([a-zA-Z0-9$_]+\\)::\\)?\\([a-zA-Z0-9$_]+\\)")
2540 (let* ((type (if (string= (downcase (match-string 1)) "pro")
2541 'pro 'function))
2542 (class (idlwave-sintern-class (match-string 3)))
2543 (name (idlwave-sintern-routine-or-method (match-string 4) class)))
2544 (list name type class)))))
2545
2546 (defvar idlwave-shell-prompt-pattern)
2547 (defun idlwave-beginning-of-statement ()
2548 "Move to beginning of the current statement.
2549 Skips back past statement continuations.
2550 Point is placed at the beginning of the line whether or not this is an
2551 actual statement."
2552 (interactive)
2553 (cond
2554 ((eq major-mode 'idlwave-shell-mode)
2555 (if (re-search-backward idlwave-shell-prompt-pattern nil t)
2556 (goto-char (match-end 0))))
2557 (t
2558 (if (save-excursion (forward-line -1) (idlwave-is-continuation-line))
2559 (idlwave-previous-statement)
2560 (beginning-of-line)))))
2561
2562 (defun idlwave-previous-statement ()
2563 "Moves point to beginning of the previous statement.
2564 Returns t if the current line before moving is the beginning of
2565 the first non-comment statement in the file, and nil otherwise."
2566 (interactive)
2567 (let (first-statement)
2568 (if (not (= (forward-line -1) 0))
2569 ;; first line in file
2570 t
2571 ;; skip blank lines, label lines, include lines and line comments
2572 (while (and
2573 ;; The current statement is the first statement until we
2574 ;; reach another statement.
2575 (setq first-statement
2576 (or
2577 (looking-at idlwave-comment-line-start-skip)
2578 (looking-at "[ \t]*$")
2579 (looking-at (concat "[ \t]*" idlwave-label "[ \t]*$"))
2580 (looking-at "^@")))
2581 (= (forward-line -1) 0)))
2582 ;; skip continuation lines
2583 (while (and
2584 (save-excursion
2585 (forward-line -1)
2586 (idlwave-is-continuation-line))
2587 (= (forward-line -1) 0)))
2588 first-statement)))
2589
2590 (defun idlwave-end-of-statement ()
2591 "Moves point to the end of the current IDL statement.
2592 If not in a statement just moves to end of line. Returns position."
2593 (interactive)
2594 (while (and (idlwave-is-continuation-line)
2595 (= (forward-line 1) 0))
2596 (while (and (idlwave-is-comment-or-empty-line)
2597 (= (forward-line 1) 0))))
2598 (end-of-line)
2599 (point))
2600
2601 (defun idlwave-end-of-statement0 ()
2602 "Moves point to the end of the current IDL statement.
2603 If not in a statement just moves to end of line. Returns position."
2604 (interactive)
2605 (while (and (idlwave-is-continuation-line)
2606 (= (forward-line 1) 0)))
2607 (end-of-line)
2608 (point))
2609
2610 (defun idlwave-next-statement ()
2611 "Moves point to beginning of the next IDL statement.
2612 Returns t if that statement is the last
2613 non-comment IDL statement in the file, and nil otherwise."
2614 (interactive)
2615 (let (last-statement)
2616 (idlwave-end-of-statement)
2617 ;; skip blank lines, label lines, include lines and line comments
2618 (while (and (= (forward-line 1) 0)
2619 ;; The current statement is the last statement until
2620 ;; we reach a new statement.
2621 (setq last-statement
2622 (or
2623 (looking-at idlwave-comment-line-start-skip)
2624 (looking-at "[ \t]*$")
2625 (looking-at (concat "[ \t]*" idlwave-label "[ \t]*$"))
2626 (looking-at "^@")))))
2627 last-statement))
2628
2629 (defun idlwave-skip-multi-commands (&optional lim)
2630 "Skip past multiple commands on a line (with `&')."
2631 (let ((save-point (point)))
2632 (when (re-search-forward ".*&" lim t)
2633 (goto-char (match-end 0))
2634 (if (idlwave-quoted)
2635 (goto-char save-point)
2636 (if (eq (char-after (- (point) 2)) ?&) (goto-char save-point))))
2637 (point)))
2638
2639 (defun idlwave-skip-label-or-case ()
2640 "Skip label or case statement element.
2641 Returns position after label.
2642 If there is no label point is not moved and nil is returned."
2643 ;; Case expressions and labels are terminated by a colon.
2644 ;; So we find the first colon in the line and make sure
2645 ;; - no `?' is before it (might be a ? b : c)
2646 ;; - it is not in a comment
2647 ;; - not in a string constant
2648 ;; - not in parenthesis (like a[0:3])
2649 ;; - not followed by another ":" in explicit class, ala a->b::c
2650 ;; As many in this mode, this function is heuristic and not an exact
2651 ;; parser.
2652 (let* ((start (point))
2653 (eos (save-excursion (idlwave-end-of-statement) (point)))
2654 (end (idlwave-find-key ":" 1 'nomark eos)))
2655 (if (and end
2656 (= (nth 0 (parse-partial-sexp start end)) 0)
2657 (not (string-match "\\?" (buffer-substring start end)))
2658 (not (string-match "^::" (buffer-substring end eos))))
2659 (progn
2660 (forward-char)
2661 (point))
2662 (goto-char start)
2663 nil)))
2664
2665 (defun idlwave-start-of-substatement (&optional pre)
2666 "Move to start of next IDL substatement after point.
2667 Uses the type of the current IDL statement to determine if the next
2668 statement is on a new line or is a subpart of the current statement.
2669 Returns point at start of substatement modulo whitespace.
2670 If optional argument is non-nil move to beginning of current
2671 substatement."
2672 (let ((orig (point))
2673 (eos (idlwave-end-of-statement))
2674 (ifnest 0)
2675 st nst last)
2676 (idlwave-beginning-of-statement)
2677 (idlwave-skip-label-or-case)
2678 (if (< (point) orig)
2679 (idlwave-skip-multi-commands orig))
2680 (setq last (point))
2681 ;; Continue looking for substatements until we are past orig
2682 (while (and (<= (point) orig) (not (eobp)))
2683 (setq last (point))
2684 (setq nst (nth 1 (cdr (setq st (car (idlwave-statement-type))))))
2685 (if (equal (car st) 'if) (setq ifnest (1+ ifnest)))
2686 (cond ((and nst
2687 (idlwave-find-key nst 1 'nomark eos))
2688 (goto-char (match-end 0)))
2689 ((and (> ifnest 0) (idlwave-find-key "\\<else\\>" 1 'nomark eos))
2690 (setq ifnest (1- ifnest))
2691 (goto-char (match-end 0)))
2692 (t (setq ifnest 0)
2693 (idlwave-next-statement))))
2694 (if pre (goto-char last))
2695 ;; If a continuation line starts here, move to next line
2696 (if (looking-at "[ \t]*\\$\\([ \t]*\\(;\\|$\\)\\)")
2697 (beginning-of-line 2))
2698 (point)))
2699
2700 (defun idlwave-statement-type ()
2701 "Return the type of the current IDL statement.
2702 Uses `idlwave-statement-match' to return a cons of (type . point) with
2703 point the ending position where the type was determined. Type is the
2704 association from `idlwave-statement-match', i.e. the cons cell from the
2705 list not just the type symbol. Returns nil if not an identifiable
2706 statement."
2707 (save-excursion
2708 ;; Skip whitespace within a statement which is spaces, tabs, continuations
2709 ;; and possibly comments
2710 (while (looking-at "[ \t]*\\$")
2711 (forward-line 1))
2712 (skip-chars-forward " \t")
2713 (let ((st idlwave-statement-match)
2714 (case-fold-search t))
2715 (while (and (not (looking-at (nth 0 (cdr (car st)))))
2716 (setq st (cdr st))))
2717 (if st
2718 (append st (match-end 0))))))
2719
2720 (defun idlwave-expand-equal (&optional before after is-action)
2721 "Pad '=' with spaces. Two cases: Assignment statement, and keyword
2722 assignment. Which case is determined using
2723 `idlwave-start-of-substatement' and `idlwave-statement-type'. The
2724 equal sign will be surrounded by BEFORE and AFTER blanks. If
2725 `idlwave-pad-keyword' is t then keyword assignment is treated just
2726 like assignment statements. When nil, spaces are removed for keyword
2727 assignment. Any other value keeps the current space around the `='.
2728 Limits in for loops are treated as keyword assignment.
2729
2730 Starting with IDL 6.0, a number of op= assignments are available.
2731 Since ambiguities of the form:
2732
2733 r and= b
2734 rand= b
2735
2736 can occur, alphanumeric operator assignment will never be pre-padded,
2737 only post-padded. You must use a space before these to disambiguate
2738 \(not just for padding, but for proper parsing by IDL too!). Other
2739 operators, such as ##=, ^=, etc., will be pre-padded.
2740
2741 IS-ACTION is ignored.
2742
2743 See `idlwave-surround'."
2744 (if idlwave-surround-by-blank
2745 (let
2746 ((non-an-ops "\\(##\\|\\*\\|\\+\\|-\\|/\\|<\\|>\\|\\^\\)\\=")
2747 (an-ops
2748 "\\s-\\(AND\\|EQ\\|GE\\|GT\\|LE\\|LT\\|MOD\\|NE\\|OR\\|XOR\\)\\=")
2749 (len 1))
2750
2751 (save-excursion
2752 (let ((case-fold-search t))
2753 (backward-char)
2754 (if (or
2755 (re-search-backward non-an-ops nil t)
2756 ;; Why doesn't ##? work for both?
2757 (re-search-backward "\\(#\\)\\=" nil t))
2758 (setq len (1+ (length (match-string 1))))
2759 (when (re-search-backward an-ops nil t)
2760 ;(setq begin nil) ; won't modify begin
2761 (setq len (1+ (length (match-string 1))))))))
2762
2763 (if (eq t idlwave-pad-keyword)
2764 ;; Everything gets padded equally
2765 (idlwave-surround before after len)
2766 ;; Treating keywords/for variables specially...
2767 (let ((st (save-excursion ; To catch "for" variables
2768 (idlwave-start-of-substatement t)
2769 (idlwave-statement-type)))
2770 (what (save-excursion ; To catch keywords
2771 (skip-chars-backward "= \t")
2772 (nth 2 (idlwave-where)))))
2773 (cond ((or (memq what '(function-keyword procedure-keyword))
2774 (memq (caar st) '(for pdef)))
2775 (cond
2776 ((null idlwave-pad-keyword)
2777 (idlwave-surround 0 0)
2778 ) ; remove space
2779 (t))) ; leave any spaces alone
2780 (t (idlwave-surround before after len))))))))
2781
2782
2783 (defun idlwave-indent-and-action (&optional arg)
2784 "Call `idlwave-indent-line' and do expand actions.
2785 With prefix ARG non-nil, indent the entire sub-statement."
2786 (interactive "p")
2787 (save-excursion
2788 (if (and idlwave-expand-generic-end
2789 (re-search-backward "\\<\\(end\\)\\s-*\\="
2790 (max 0 (- (point) 10)) t)
2791 (looking-at "\\(end\\)\\([ \n\t]\\|\\'\\)"))
2792 (progn (goto-char (match-end 1))
2793 ;;Expand the END abbreviation, just as RET or Space would have.
2794 (if abbrev-mode (expand-abbrev)
2795 (idlwave-show-begin)))))
2796 (when (and (not arg) current-prefix-arg)
2797 (setq arg current-prefix-arg)
2798 (setq current-prefix-arg nil))
2799 (if arg
2800 (idlwave-indent-statement)
2801 (idlwave-indent-line t)))
2802
2803 (defun idlwave-indent-line (&optional expand)
2804 "Indents current IDL line as code or as a comment.
2805 The actions in `idlwave-indent-action-table' are performed.
2806 If the optional argument EXPAND is non-nil then the actions in
2807 `idlwave-indent-expand-table' are performed."
2808 (interactive)
2809 ;; Move point out of left margin.
2810 (if (save-excursion
2811 (skip-chars-backward " \t")
2812 (bolp))
2813 (skip-chars-forward " \t"))
2814 (let ((mloc (point-marker)))
2815 (save-excursion
2816 (beginning-of-line)
2817 (if (looking-at idlwave-comment-line-start-skip)
2818 ;; Indentation for a line comment
2819 (progn
2820 (skip-chars-forward " \t")
2821 (idlwave-indent-left-margin (idlwave-comment-hook)))
2822 ;;
2823 ;; Code Line
2824 ;;
2825 ;; Before indenting, run action routines.
2826 ;;
2827 (if (and expand idlwave-do-actions)
2828 (mapcar 'idlwave-do-action idlwave-indent-expand-table))
2829 ;;
2830 (if idlwave-do-actions
2831 (mapcar 'idlwave-do-action idlwave-indent-action-table))
2832 ;;
2833 ;; No longer expand abbrevs on the line. The user can do this
2834 ;; manually using expand-region-abbrevs.
2835 ;;
2836 ;; Indent for code line
2837 ;;
2838 (beginning-of-line)
2839 (if (or
2840 ;; a label line
2841 (looking-at (concat "^" idlwave-label "[ \t]*$"))
2842 ;; a batch command
2843 (looking-at "^[ \t]*@"))
2844 ;; leave flush left
2845 nil
2846 ;; indent the line
2847 (idlwave-indent-left-margin (idlwave-calculate-indent)))
2848 ;; Adjust parallel comment
2849 (end-of-line)
2850 (if (idlwave-in-comment)
2851 ;; Emacs 21 is too smart with fill-column on comment indent
2852 (let ((fill-column (if (fboundp 'comment-indent-new-line)
2853 (1- (frame-width))
2854 fill-column)))
2855 (indent-for-comment)))))
2856 (goto-char mloc)
2857 ;; Get rid of marker
2858 (set-marker mloc nil)))
2859
2860 (defun idlwave-do-action (action)
2861 "Perform an action repeatedly on a line. ACTION is a list (REG
2862 . FUNC). REG is a regular expression. FUNC is either a function name
2863 to be called with `funcall' or a list to be evaluated with `eval'.
2864 The action performed by FUNC should leave point after the match for
2865 REG - otherwise an infinite loop may be entered. FUNC is always
2866 passed a final argument of 'is-action, so it can discriminate between
2867 being run as an action, or a key binding"
2868 (let ((action-key (car action))
2869 (action-routine (cdr action)))
2870 (beginning-of-line)
2871 (while (idlwave-look-at action-key)
2872 (if (listp action-routine)
2873 (eval (append action-routine '('is-action)))
2874 (funcall action-routine 'is-action)))))
2875
2876 (defun idlwave-indent-to (col &optional min)
2877 "Indent from point with spaces until column COL.
2878 Inserts space before markers at point."
2879 (if (not min) (setq min 0))
2880 (insert-before-markers
2881 (make-string (max min (- col (current-column))) ?\ )))
2882
2883 (defun idlwave-indent-left-margin (col)
2884 "Indent the current line to column COL.
2885 Indents such that first non-whitespace character is at column COL
2886 Inserts spaces before markers at point."
2887 (save-excursion
2888 (beginning-of-line)
2889 (delete-horizontal-space)
2890 (idlwave-indent-to col)))
2891
2892 (defun idlwave-indent-subprogram ()
2893 "Indents program unit which contains point."
2894 (interactive)
2895 (save-excursion
2896 (idlwave-end-of-statement)
2897 (idlwave-beginning-of-subprogram)
2898 (let ((beg (point)))
2899 (idlwave-forward-block)
2900 (message "Indenting subprogram...")
2901 (indent-region beg (point) nil))
2902 (message "Indenting subprogram...done.")))
2903
2904 (defun idlwave-indent-statement ()
2905 "Indent current statement, including all continuation lines."
2906 (interactive)
2907 (save-excursion
2908 (idlwave-beginning-of-statement)
2909 (let ((beg (point)))
2910 (idlwave-end-of-statement)
2911 (indent-region beg (point) nil))))
2912
2913 (defun idlwave-calculate-indent ()
2914 "Return appropriate indentation for current line as IDL code."
2915 (save-excursion
2916 (beginning-of-line)
2917 (cond
2918 ;; Check for beginning of unit - main (beginning of buffer), pro, or
2919 ;; function
2920 ((idlwave-look-at idlwave-begin-unit-reg)
2921 0)
2922 ;; Check for continuation line
2923 ((save-excursion
2924 (and (= (forward-line -1) 0)
2925 (idlwave-is-continuation-line)))
2926 (idlwave-calculate-cont-indent))
2927 ;; calculate indent based on previous and current statements
2928 (t (let* (beg-prev-pos
2929 (the-indent
2930 ;; calculate indent based on previous statement
2931 (save-excursion
2932 (cond
2933 ;; Beginning of file
2934 ((prog1
2935 (idlwave-previous-statement)
2936 (setq beg-prev-pos (point)))
2937 0)
2938 ;; Main block
2939 ((idlwave-look-at idlwave-begin-unit-reg t)
2940 (+ (idlwave-current-statement-indent)
2941 idlwave-main-block-indent))
2942 ;; Begin block
2943 ((idlwave-look-at idlwave-begin-block-reg t)
2944 (+ (idlwave-min-current-statement-indent)
2945 idlwave-block-indent))
2946 ;; End Block
2947 ((idlwave-look-at idlwave-end-block-reg t)
2948 (progn
2949 ;; Match to the *beginning* of the block opener
2950 (goto-char beg-prev-pos)
2951 (idlwave-block-jump-out -1 'nomark) ; go to begin block
2952 (idlwave-min-current-statement-indent)))
2953 ;; idlwave-end-offset
2954 ;; idlwave-block-indent))
2955
2956 ;; Default to current indent
2957 ((idlwave-current-statement-indent))))))
2958 ;; adjust the indentation based on the current statement
2959 (cond
2960 ;; End block
2961 ((idlwave-look-at idlwave-end-block-reg)
2962 (+ the-indent idlwave-end-offset))
2963 (the-indent)))))))
2964
2965 ;;
2966 ;; Parentheses indent
2967 ;;
2968
2969 (defun idlwave-calculate-paren-indent (beg-reg end-reg close-exp)
2970 "Calculate the continuation indent inside a paren group.
2971 Returns a cons-cell with (open . indent), where open is the
2972 location of the open paren"
2973 (let ((open (nth 1 (parse-partial-sexp beg-reg end-reg))))
2974 ;; Found an innermost open paren.
2975 (when open
2976 (goto-char open)
2977 ;; Line up with next word unless this is a closing paren.
2978 (cons open
2979 (cond
2980 ;; Plain Kernighan-style nested indent
2981 (idlwave-indent-parens-nested
2982 (+ idlwave-continuation-indent (idlwave-current-indent)))
2983
2984 ;; This is a closed paren - line up under open paren.
2985 (close-exp
2986 (current-column))
2987
2988 ;; Empty (or just comment) follows -- revert to basic indent
2989 ((progn
2990 ;; Skip paren
2991 (forward-char 1)
2992 (looking-at "[ \t$]*\\(;.*\\)?$"))
2993 nil)
2994
2995 ;; Line up with first word after any blank space
2996 ((progn
2997 (skip-chars-forward " \t")
2998 (current-column))))))))
2999
3000 (defun idlwave-calculate-cont-indent ()
3001 "Calculates the IDL continuation indent column from the previous
3002 statement. Note that here previous statement usually means the
3003 beginning of the current statement if this statement is a continuation
3004 of the previous line. Various special types of continuations,
3005 including assignments, routine definitions, and parenthetical
3006 groupings, are treated separately."
3007 (save-excursion
3008 (let* ((case-fold-search t)
3009 (end-reg (progn (beginning-of-line) (point)))
3010 (beg-last-statement (save-excursion (idlwave-previous-statement)
3011 (point)))
3012 (beg-reg (progn (idlwave-start-of-substatement 'pre)
3013 (if (eq (line-beginning-position) end-reg)
3014 (goto-char beg-last-statement)
3015 (point))))
3016 (basic-indent (+ (idlwave-min-current-statement-indent end-reg)
3017 idlwave-continuation-indent))
3018 fancy-nonparen-indent fancy-paren-indent)
3019 (cond
3020 ;; Align then with its matching if, etc.
3021 ((let ((matchers '(("\\<if\\>" . "[ \t]*then")
3022 ("\\<\\(if\\|end\\(if\\)?\\)\\>" . "[ \t]*else")
3023 ("\\<\\(for\\|while\\)\\>" . "[ \t]*do")
3024 ("\\<\\(repeat\\|end\\(rep\\)?\\)\\>" .
3025 "[ \t]*until")
3026 ("\\<case\\>" . "[ \t]*of")))
3027 match cont-re)
3028 (goto-char end-reg)
3029 (and
3030 (setq cont-re
3031 (catch 'exit
3032 (while (setq match (car matchers))
3033 (if (looking-at (cdr match))
3034 (throw 'exit (car match)))
3035 (setq matchers (cdr matchers)))))
3036 (idlwave-find-key cont-re -1 'nomark beg-last-statement)))
3037 (if (looking-at "end") ;; that one's special
3038 (- (idlwave-current-indent)
3039 (+ idlwave-block-indent idlwave-end-offset))
3040 (idlwave-current-indent)))
3041
3042 ;; Indent in from the previous line for continuing statements
3043 ((let ((matchers '("\\<then\\>"
3044 "\\<do\\>"
3045 "\\<repeat\\>"
3046 "\\<else\\>"))
3047 match)
3048 (catch 'exit
3049 (goto-char end-reg)
3050 (if (/= (forward-line -1) 0)
3051 (throw 'exit nil))
3052 (while (setq match (car matchers))
3053 (if (looking-at (concat ".*" match "[ \t]*\\$[ \t]*"
3054 "\\(;.*\\)?$"))
3055 (throw 'exit t))
3056 (setq matchers (cdr matchers)))))
3057 (+ idlwave-continuation-indent (idlwave-current-indent)))
3058
3059 ;; Parenthetical indent, either traditional or Kernighan style
3060 ((setq fancy-paren-indent
3061 (let* ((end-reg end-reg)
3062 (close-exp (progn
3063 (goto-char end-reg)
3064 (skip-chars-forward " \t")
3065 (looking-at "\\s)")))
3066 indent-cons)
3067 (catch 'loop
3068 (while (setq indent-cons (idlwave-calculate-paren-indent
3069 beg-reg end-reg close-exp))
3070 ;; First permitted containing paren
3071 (if (or
3072 idlwave-indent-to-open-paren
3073 idlwave-indent-parens-nested
3074 (null (cdr indent-cons))
3075 (< (- (cdr indent-cons) basic-indent)
3076 idlwave-max-extra-continuation-indent))
3077 (throw 'loop (cdr indent-cons)))
3078 (setq end-reg (car indent-cons))))))
3079 fancy-paren-indent)
3080
3081 ;; A continued assignment, or procedure call/definition
3082 ((and
3083 (> idlwave-max-extra-continuation-indent 0)
3084 (setq fancy-nonparen-indent
3085 (progn
3086 (goto-char beg-reg)
3087 (while (idlwave-look-at "&")) ; skip continued statements
3088 (cond
3089 ;; A continued Procedure call or definition
3090 ((progn
3091 (idlwave-look-at "^[ \t]*\\(pro\\|function\\)") ;skip over
3092 (looking-at "[ \t]*\\([a-zA-Z0-9.$_]+[ \t]*->[ \t]*\\)?[a-zA-Z][:a-zA-Z0-9$_]*[ \t]*\\(,\\)[ \t]*"))
3093 (goto-char (match-end 0))
3094 ;; Comment only, or blank line with "$"? Basic indent.
3095 (if (save-match-data (looking-at "[ \t$]*\\(;.*\\)?$"))
3096 nil
3097 (current-column)))
3098
3099 ;; Continued assignment (with =):
3100 ((catch 'assign ;
3101 (while (looking-at "[^=\n\r]*\\(=\\)[ \t]*")
3102 (goto-char (match-end 0))
3103 (if (null (idlwave-what-function beg-reg))
3104 (throw 'assign t))))
3105 (unless (or
3106 (idlwave-in-quote)
3107 (looking-at "[ \t$]*\\(;.*\\)?$") ; use basic
3108 (save-excursion
3109 (goto-char beg-last-statement)
3110 (eq (caar (idlwave-statement-type)) 'for)))
3111 (current-column))))))
3112 (< (- fancy-nonparen-indent basic-indent)
3113 idlwave-max-extra-continuation-indent))
3114 (if fancy-paren-indent ;calculated but disallowed paren indent
3115 (+ fancy-nonparen-indent idlwave-continuation-indent)
3116 fancy-nonparen-indent))
3117
3118 ;; Basic indent, by default
3119 (t basic-indent)))))
3120
3121
3122
3123 (defun idlwave-find-key (key-re &optional dir nomark limit)
3124 "Move to next match of the regular expression KEY-RE.
3125 Matches inside comments or string constants will be ignored.
3126 If DIR is negative, the search will be backwards.
3127 At a successful match, the mark is pushed unless NOMARK is non-nil.
3128 Searches are limited to LIMIT.
3129 Searches are case-insensitive and use a special syntax table which
3130 treats `$' and `_' as word characters.
3131 Return value is the beginning of the match or (in case of failure) nil."
3132 (setq dir (or dir 0))
3133 (let ((case-fold-search t)
3134 (search-func (if (> dir 0) 're-search-forward 're-search-backward))
3135 found)
3136 (idlwave-with-special-syntax
3137 (save-excursion
3138 (catch 'exit
3139 (while (funcall search-func key-re limit t)
3140 (if (not (idlwave-quoted))
3141 (throw 'exit (setq found (match-beginning 0)))
3142 (if (or (and (> dir 0) (eobp))
3143 (and (< dir 0) (bobp)))
3144 (throw 'exit nil)))))))
3145 (if found
3146 (progn
3147 (if (not nomark) (push-mark))
3148 (goto-char found)
3149 found)
3150 nil)))
3151
3152 (defun idlwave-block-jump-out (&optional dir nomark)
3153 "When optional argument DIR is non-negative, move forward to end of
3154 current block using the `idlwave-begin-block-reg' and `idlwave-end-block-reg'
3155 regular expressions. When DIR is negative, move backwards to block beginning.
3156 Recursively calls itself to skip over nested blocks. DIR defaults to
3157 forward. Calls `push-mark' unless the optional argument NOMARK is
3158 non-nil. Movement is limited by the start of program units because of
3159 possibility of unbalanced blocks."
3160 (interactive "P")
3161 (or dir (setq dir 0))
3162 (let* ((here (point))
3163 (case-fold-search t)
3164 (limit (if (>= dir 0) (point-max) (point-min)))
3165 (block-limit (if (>= dir 0)
3166 idlwave-begin-block-reg
3167 idlwave-end-block-reg))
3168 found
3169 (block-reg (concat idlwave-begin-block-reg "\\|"
3170 idlwave-end-block-reg))
3171 (unit-limit (or (save-excursion
3172 (if (< dir 0)
3173 (idlwave-find-key
3174 idlwave-begin-unit-reg dir t limit)
3175 (end-of-line)
3176 (idlwave-find-key
3177 idlwave-end-unit-reg dir t limit)))
3178 limit)))
3179 (if (>= dir 0) (end-of-line)) ;Make sure we are in current block
3180 (if (setq found (idlwave-find-key block-reg dir t unit-limit))
3181 (while (and found (looking-at block-limit))
3182 (if (>= dir 0) (forward-word 1))
3183 (idlwave-block-jump-out dir t)
3184 (setq found (idlwave-find-key block-reg dir t unit-limit))))
3185 (if (not nomark) (push-mark here))
3186 (if (not found) (goto-char unit-limit)
3187 (if (>= dir 0) (forward-word 1)))))
3188
3189 (defun idlwave-min-current-statement-indent (&optional end-reg)
3190 "The minimum indent in the current statement."
3191 (idlwave-beginning-of-statement)
3192 (if (not (idlwave-is-continuation-line))
3193 (idlwave-current-indent)
3194 (let ((min (idlwave-current-indent)) comm-or-empty)
3195 (while (and (= (forward-line 1) 0)
3196 (or (setq comm-or-empty (idlwave-is-comment-or-empty-line))
3197 (idlwave-is-continuation-line))
3198 (or (null end-reg) (< (point) end-reg)))
3199 (unless comm-or-empty (setq min (min min (idlwave-current-indent)))))
3200 (if (or comm-or-empty (and end-reg (>= (point) end-reg)))
3201 min
3202 (min min (idlwave-current-indent))))))
3203
3204 (defun idlwave-current-statement-indent (&optional last-line)
3205 "Return indentation of the current statement.
3206 If in a statement, moves to beginning of statement before finding indent."
3207 (if last-line
3208 (idlwave-end-of-statement)
3209 (idlwave-beginning-of-statement))
3210 (idlwave-current-indent))
3211
3212 (defun idlwave-current-indent ()
3213 "Return the column of the indentation of the current line.
3214 Skips any whitespace. Returns 0 if the end-of-line follows the whitespace."
3215 (save-excursion
3216 (beginning-of-line)
3217 (skip-chars-forward " \t")
3218 ;; if we are at the end of blank line return 0
3219 (cond ((eolp) 0)
3220 ((current-column)))))
3221
3222 (defun idlwave-is-continuation-line ()
3223 "Tests if current line is continuation line.
3224 Blank or comment-only lines following regular continuation lines (with
3225 `$') count as continuations too."
3226 (let (p)
3227 (save-excursion
3228 (or
3229 (idlwave-look-at "\\<\\$")
3230 (catch 'loop
3231 (while (and (looking-at "^[ \t]*\\(;.*\\)?$")
3232 (eq (forward-line -1) 0))
3233 (if (setq p (idlwave-look-at "\\<\\$")) (throw 'loop p))))))))
3234
3235 (defun idlwave-is-comment-line ()
3236 "Tests if the current line is a comment line."
3237 (save-excursion
3238 (beginning-of-line 1)
3239 (looking-at "[ \t]*;")))
3240
3241 (defun idlwave-is-comment-or-empty-line ()
3242 "Tests if the current line is a comment line."
3243 (save-excursion
3244 (beginning-of-line 1)
3245 (looking-at "[ \t]*[;\n]")))
3246
3247 (defun idlwave-look-at (regexp &optional cont beg)
3248 "Searches current line from current point for REGEXP.
3249 If optional argument CONT is non-nil, searches to the end of
3250 the current statement.
3251 If optional arg BEG is non-nil, search starts from the beginning of the
3252 current statement.
3253 Ignores matches that end in a comment or inside a string expression.
3254 Returns point if successful, nil otherwise.
3255 This function produces unexpected results if REGEXP contains quotes or
3256 a comment delimiter. The search is case insensitive.
3257 If successful leaves point after the match, otherwise, does not move point."
3258 (let ((here (point))
3259 (case-fold-search t)
3260 (eos (save-excursion
3261 (if cont (idlwave-end-of-statement) (end-of-line))
3262 (point)))
3263 found)
3264 (idlwave-with-special-syntax
3265 (if beg (idlwave-beginning-of-statement))
3266 (while (and (setq found (re-search-forward regexp eos t))
3267 (idlwave-quoted))))
3268 (if (not found) (goto-char here))
3269 found))
3270
3271 (defun idlwave-fill-paragraph (&optional nohang)
3272 "Fills paragraphs in comments.
3273 A paragraph is made up of all contiguous lines having the same comment
3274 leader (the leading whitespace before the comment delimiter and the
3275 comment delimiter). In addition, paragraphs are separated by blank
3276 line comments. The indentation is given by the hanging indent of the
3277 first line, otherwise by the minimum indentation of the lines after
3278 the first line. The indentation of the first line does not change.
3279 Does not effect code lines. Does not fill comments on the same line
3280 with code. The hanging indent is given by the end of the first match
3281 matching `idlwave-hang-indent-regexp' on the paragraph's first line . If the
3282 optional argument NOHANG is non-nil then the hanging indent is
3283 ignored."
3284 (interactive "P")
3285 ;; check if this is a line comment
3286 (if (save-excursion
3287 (beginning-of-line)
3288 (skip-chars-forward " \t")
3289 (looking-at comment-start))
3290 (let
3291 ((indent 999)
3292 pre here diff fill-prefix-reg bcl first-indent
3293 hang start end)
3294 ;; Change tabs to spaces in the surrounding paragraph.
3295 ;; The surrounding paragraph will be the largest containing block of
3296 ;; contiguous line comments. Thus, we may be changing tabs in
3297 ;; a much larger area than is needed, but this is the easiest
3298 ;; brute force way to do it.
3299 ;;
3300 ;; This has the undesirable side effect of replacing the tabs
3301 ;; permanently without the user's request or knowledge.
3302 (save-excursion
3303 (backward-paragraph)
3304 (setq start (point)))
3305 (save-excursion
3306 (forward-paragraph)
3307 (setq end (point)))
3308 (untabify start end)
3309 ;;
3310 (setq here (point))
3311 (beginning-of-line)
3312 (setq bcl (point))
3313 (re-search-forward
3314 (concat "^[ \t]*" comment-start "+")
3315 (save-excursion (end-of-line) (point))
3316 t)
3317 ;; Get the comment leader on the line and its length
3318 (setq pre (current-column))
3319 ;; the comment leader is the indentation plus exactly the
3320 ;; number of consecutive ";".
3321 (setq fill-prefix-reg
3322 (concat
3323 (setq fill-prefix
3324 (regexp-quote
3325 (buffer-substring (save-excursion
3326 (beginning-of-line) (point))
3327 (point))))
3328 "[^;]"))
3329
3330 ;; Mark the beginning and end of the paragraph
3331 (goto-char bcl)
3332 (while (and (looking-at fill-prefix-reg)
3333 (not (looking-at paragraph-separate))
3334 (not (bobp)))
3335 (forward-line -1))
3336 ;; Move to first line of paragraph
3337 (if (/= (point) bcl)
3338 (forward-line 1))
3339 (setq start (point))
3340 (goto-char bcl)
3341 (while (and (looking-at fill-prefix-reg)
3342 (not (looking-at paragraph-separate))
3343 (not (eobp)))
3344 (forward-line 1))
3345 (beginning-of-line)
3346 (if (or (not (looking-at fill-prefix-reg))
3347 (looking-at paragraph-separate))
3348 (forward-line -1))
3349 (end-of-line)
3350 ;; if at end of buffer add a newline (need this because
3351 ;; fill-region needs END to be at the beginning of line after
3352 ;; the paragraph or it will add a line).
3353 (if (eobp)
3354 (progn (insert ?\n) (backward-char 1)))
3355 ;; Set END to the beginning of line after the paragraph
3356 ;; END is calculated as distance from end of buffer
3357 (setq end (- (point-max) (point) 1))
3358 ;;
3359 ;; Calculate the indentation for the paragraph.
3360 ;;
3361 ;; In the following while statements, after one iteration
3362 ;; point will be at the beginning of a line in which case
3363 ;; the while will not be executed for the
3364 ;; the first paragraph line and thus will not affect the
3365 ;; indentation.
3366 ;;
3367 ;; First check to see if indentation is based on hanging indent.
3368 (if (and (not nohang) idlwave-hanging-indent
3369 (setq hang
3370 (save-excursion
3371 (goto-char start)
3372 (idlwave-calc-hanging-indent))))
3373 ;; Adjust lines of paragraph by inserting spaces so that
3374 ;; each line's indent is at least as great as the hanging
3375 ;; indent. This is needed for fill-paragraph to work with
3376 ;; a fill-prefix.
3377 (progn
3378 (setq indent hang)
3379 (beginning-of-line)
3380 (while (> (point) start)
3381 (re-search-forward comment-start-skip
3382 (save-excursion (end-of-line) (point))
3383 t)
3384 (if (> (setq diff (- indent (current-column))) 0)
3385 (progn
3386 (if (>= here (point))
3387 ;; adjust the original location for the
3388 ;; inserted text.
3389 (setq here (+ here diff)))
3390 (insert (make-string diff ?\ ))))
3391 (forward-line -1))
3392 )
3393
3394 ;; No hang. Instead find minimum indentation of paragraph
3395 ;; after first line.
3396 ;; For the following while statement, since START is at the
3397 ;; beginning of line and END is at the end of line
3398 ;; point is greater than START at least once (which would
3399 ;; be the case for a single line paragraph).
3400 (while (> (point) start)
3401 (beginning-of-line)
3402 (setq indent
3403 (min indent
3404 (progn
3405 (re-search-forward
3406 comment-start-skip
3407 (save-excursion (end-of-line) (point))
3408 t)
3409 (current-column))))
3410 (forward-line -1))
3411 )
3412 (setq fill-prefix (concat fill-prefix
3413 (make-string (- indent pre)
3414 ?\ )))
3415 ;; first-line indent
3416 (setq first-indent
3417 (max
3418 (progn
3419 (re-search-forward
3420 comment-start-skip
3421 (save-excursion (end-of-line) (point))
3422 t)
3423 (current-column))
3424 indent))
3425
3426 ;; try to keep point at its original place
3427 (goto-char here)
3428
3429 ;; In place of the more modern fill-region-as-paragraph, a hack
3430 ;; to keep whitespace untouched on the first line within the
3431 ;; indent length and to preserve any indent on the first line
3432 ;; (first indent).
3433 (save-excursion
3434 (setq diff
3435 (buffer-substring start (+ start first-indent -1)))
3436 (subst-char-in-region start (+ start first-indent -1) ?\ ?~ nil)
3437 (fill-region-as-paragraph
3438 start
3439 (- (point-max) end)
3440 (current-justification)
3441 nil)
3442 (delete-region start (+ start first-indent -1))
3443 (goto-char start)
3444 (insert diff))
3445 ;; When we want the point at the beginning of the comment
3446 ;; body fill-region will put it at the beginning of the line.
3447 (if (bolp) (skip-chars-forward (concat " \t" comment-start)))
3448 (setq fill-prefix nil))))
3449
3450 (defun idlwave-calc-hanging-indent ()
3451 "Calculate the position of the hanging indent for the comment
3452 paragraph. The hanging indent position is given by the first match
3453 with the `idlwave-hang-indent-regexp'. If `idlwave-use-last-hang-indent' is
3454 non-nil then use last occurrence matching `idlwave-hang-indent-regexp' on
3455 the line.
3456 If not found returns nil."
3457 (if idlwave-use-last-hang-indent
3458 (save-excursion
3459 (end-of-line)
3460 (if (re-search-backward
3461 idlwave-hang-indent-regexp
3462 (save-excursion (beginning-of-line) (point))
3463 t)
3464 (+ (current-column) (length idlwave-hang-indent-regexp))))
3465 (save-excursion
3466 (beginning-of-line)
3467 (if (re-search-forward
3468 idlwave-hang-indent-regexp
3469 (save-excursion (end-of-line) (point))
3470 t)
3471 (current-column)))))
3472
3473 (defun idlwave-auto-fill ()
3474 "Called to break lines in auto fill mode.
3475 Only fills non-comment lines if `idlwave-fill-comment-line-only' is
3476 non-nil. Places a continuation character at the end of the line if
3477 not in a comment. Splits strings with IDL concatenation operator `+'
3478 if `idlwave-auto-fill-split-string' is non-nil."
3479 (if (<= (current-column) fill-column)
3480 nil ; do not to fill
3481 (if (or (not idlwave-fill-comment-line-only)
3482 (save-excursion
3483 ;; Check for comment line
3484 (beginning-of-line)
3485 (looking-at idlwave-comment-line-start-skip)))
3486 (let (beg)
3487 (idlwave-indent-line)
3488 ;; Prevent actions do-auto-fill which calls indent-line-function.
3489 (let (idlwave-do-actions
3490 (fill-nobreak-predicate
3491 (if (and (idlwave-in-quote)
3492 idlwave-auto-fill-split-string)
3493 (lambda () ;; We'll need 5 spaces for " ' + $"
3494 (<= (- fill-column (current-column)) 5)
3495 ))))
3496 (do-auto-fill))
3497 (save-excursion
3498 (end-of-line 0)
3499 ;; Indent the split line
3500 (idlwave-indent-line))
3501 (if (save-excursion
3502 (beginning-of-line)
3503 (looking-at idlwave-comment-line-start-skip))
3504 ;; A continued line comment
3505 ;; We treat continued line comments as part of a comment
3506 ;; paragraph. So we check for a hanging indent.
3507 (if idlwave-hanging-indent
3508 (let ((here (- (point-max) (point)))
3509 (indent
3510 (save-excursion
3511 (forward-line -1)
3512 (idlwave-calc-hanging-indent))))
3513 (if indent
3514 (progn
3515 ;; Remove whitespace between comment delimiter and
3516 ;; text, insert spaces for appropriate indentation.
3517 (beginning-of-line)
3518 (re-search-forward
3519 comment-start-skip
3520 (save-excursion (end-of-line) (point)) t)
3521 (delete-horizontal-space)
3522 (idlwave-indent-to indent)
3523 (goto-char (- (point-max) here)))
3524 )))
3525 ;; Split code or comment?
3526 (if (save-excursion
3527 (end-of-line 0)
3528 (idlwave-in-comment))
3529 ;; Splitting a non-full-line comment.
3530 ;; Insert the comment delimiter from split line
3531 (progn
3532 (save-excursion
3533 (beginning-of-line)
3534 (skip-chars-forward " \t")
3535 ;; Insert blank to keep off beginning of line
3536 (insert " "
3537 (save-excursion
3538 (forward-line -1)
3539 (buffer-substring (idlwave-goto-comment)
3540 (progn
3541 (skip-chars-forward "; ")
3542 (point))))))
3543 (idlwave-indent-line))
3544 ;; Split code line - add continuation character
3545 (save-excursion
3546 (end-of-line 0)
3547 ;; Check to see if we split a string
3548 (if (and (setq beg (idlwave-in-quote))
3549 idlwave-auto-fill-split-string)
3550 ;; Split the string and concatenate.
3551 ;; The first extra space is for the space
3552 ;; the line was split. That space was removed.
3553 (insert " " (char-after beg) " +"))
3554 (insert " $"))
3555 (if beg
3556 (if idlwave-auto-fill-split-string
3557 ;; Make the second part of continued string
3558 (save-excursion
3559 (beginning-of-line)
3560 (skip-chars-forward " \t")
3561 (insert (char-after beg)))
3562 ;; Warning
3563 (beep)
3564 (message "Warning: continuation inside a string.")))
3565 ;; Although do-auto-fill (via indent-new-comment-line) calls
3566 ;; idlwave-indent-line for the new line, re-indent again
3567 ;; because of the addition of the continuation character.
3568 (idlwave-indent-line))
3569 )))))
3570
3571 (defun idlwave-auto-fill-mode (arg)
3572 "Toggle auto-fill mode for IDL mode.
3573 With arg, turn auto-fill mode on if arg is positive.
3574 In auto-fill mode, inserting a space at a column beyond `fill-column'
3575 automatically breaks the line at a previous space."
3576 (interactive "P")
3577 (prog1 (set idlwave-fill-function
3578 (if (if (null arg)
3579 (not (symbol-value idlwave-fill-function))
3580 (> (prefix-numeric-value arg) 0))
3581 'idlwave-auto-fill
3582 nil))
3583 ;; update mode-line
3584 (set-buffer-modified-p (buffer-modified-p))))
3585
3586 ;(defun idlwave-fill-routine-call ()
3587 ; "Fill a routine definition or statement, indenting appropriately."
3588 ; (let ((where (idlwave-where)))))
3589
3590
3591 (defun idlwave-doc-header (&optional nomark )
3592 "Insert a documentation header at the beginning of the unit.
3593 Inserts the value of the variable idlwave-file-header. Sets mark before
3594 moving to do insertion unless the optional prefix argument NOMARK
3595 is non-nil."
3596 (interactive "P")
3597 (or nomark (push-mark))
3598 ;; make sure we catch the current line if it begins the unit
3599 (if idlwave-header-to-beginning-of-file
3600 (goto-char (point-min))
3601 (end-of-line)
3602 (idlwave-beginning-of-subprogram)
3603 (beginning-of-line)
3604 ;; skip function or procedure line
3605 (if (idlwave-look-at "\\<\\(pro\\|function\\)\\>")
3606 (progn
3607 (idlwave-end-of-statement)
3608 (if (> (forward-line 1) 0) (insert "\n")))))
3609 (let ((pos (point)))
3610 (if idlwave-file-header
3611 (cond ((car idlwave-file-header)
3612 (insert-file-contents (car idlwave-file-header)))
3613 ((stringp (car (cdr idlwave-file-header)))
3614 (insert (car (cdr idlwave-file-header))))))
3615 (goto-char pos)))
3616
3617 (defun idlwave-default-insert-timestamp ()
3618 "Default timestamp insertion function"
3619 (insert (current-time-string))
3620 (insert ", " (user-full-name))
3621 (if (boundp 'user-mail-address)
3622 (insert " <" user-mail-address ">")
3623 (insert " <" (user-login-name) "@" (system-name) ">"))
3624 ;; Remove extra spaces from line
3625 (idlwave-fill-paragraph)
3626 ;; Insert a blank line comment to separate from the date entry -
3627 ;; will keep the entry from flowing onto date line if re-filled.
3628 (insert "\n;\n;\t\t"))
3629
3630 (defun idlwave-doc-modification ()
3631 "Insert a brief modification log at the beginning of the current program.
3632 Looks for an occurrence of the value of user variable
3633 `idlwave-doc-modifications-keyword' if non-nil. Inserts time and user name
3634 and places the point for the user to add a log. Before moving, saves
3635 location on mark ring so that the user can return to previous point."
3636 (interactive)
3637 (push-mark)
3638 (let* (beg end)
3639 (if (and (or (re-search-backward idlwave-doclib-start nil t)
3640 (progn
3641 (goto-char (point-min))
3642 (re-search-forward idlwave-doclib-start nil t)))
3643 (setq beg (match-beginning 0))
3644 (re-search-forward idlwave-doclib-end nil t)
3645 (setq end (match-end 0)))
3646 (progn
3647 (goto-char beg)
3648 (if (re-search-forward
3649 (concat idlwave-doc-modifications-keyword ":")
3650 end t)
3651 (end-of-line)
3652 (goto-char end)
3653 (end-of-line -1)
3654 (insert "\n" comment-start "\n")
3655 (insert comment-start " " idlwave-doc-modifications-keyword ":"))
3656 (insert "\n;\n;\t")
3657 (run-hooks 'idlwave-timestamp-hook))
3658 (error "No valid DOCLIB header"))))
3659
3660
3661 ;;; CJC 3/16/93
3662 ;;; Interface to expand-region-abbrevs which did not work when the
3663 ;;; abbrev hook associated with an abbrev moves point backwards
3664 ;;; after abbrev expansion, e.g., as with the abbrev '.n'.
3665 ;;; The original would enter an infinite loop in attempting to expand
3666 ;;; .n (it would continually expand and unexpand the abbrev without expanding
3667 ;;; because the point would keep going back to the beginning of the
3668 ;;; abbrev instead of to the end of the abbrev). We now keep the
3669 ;;; abbrev hook from moving backwards.
3670 ;;;
3671 (defun idlwave-expand-region-abbrevs (start end)
3672 "Expand each abbrev occurrence in the region.
3673 Calling from a program, arguments are START END."
3674 (interactive "r")
3675 (save-excursion
3676 (goto-char (min start end))
3677 (let ((idlwave-show-block nil) ;Do not blink
3678 (idlwave-abbrev-move nil)) ;Do not move
3679 (expand-region-abbrevs start end 'noquery))))
3680
3681 (defun idlwave-quoted ()
3682 "Returns t if point is in a comment or quoted string.
3683 nil otherwise."
3684 (or (idlwave-in-comment) (idlwave-in-quote)))
3685
3686 (defun idlwave-in-quote ()
3687 "Returns location of the opening quote
3688 if point is in a IDL string constant, nil otherwise.
3689 Ignores comment delimiters on the current line.
3690 Properly handles nested quotation marks and octal
3691 constants - a double quote followed by an octal digit."
3692 ;;; Treat an octal inside an apostrophe to be a normal string. Treat a
3693 ;;; double quote followed by an octal digit to be an octal constant
3694 ;;; rather than a string. Therefore, there is no terminating double
3695 ;;; quote.
3696 (save-excursion
3697 ;; Because single and double quotes can quote each other we must
3698 ;; search for the string start from the beginning of line.
3699 (let* ((start (point))
3700 (eol (progn (end-of-line) (point)))
3701 (bq (progn (beginning-of-line) (point)))
3702 (endq (point))
3703 (data (match-data))
3704 delim
3705 found)
3706 (while (< endq start)
3707 ;; Find string start
3708 ;; Don't find an octal constant beginning with a double quote
3709 (if (re-search-forward "[\"']" eol 'lim)
3710 ;; Find the string end.
3711 ;; In IDL, two consecutive delimiters after the start of a
3712 ;; string act as an
3713 ;; escape for the delimiter in the string.
3714 ;; Two consecutive delimiters alone (i.e., not after the
3715 ;; start of a string) is the null string.
3716 (progn
3717 ;; Move to position after quote
3718 (goto-char (1+ (match-beginning 0)))
3719 (setq bq (1- (point)))
3720 ;; Get the string delimiter
3721 (setq delim (char-to-string (preceding-char)))
3722 ;; Check for null string
3723 (if (looking-at delim)
3724 (progn (setq endq (point)) (forward-char 1))
3725 ;; Look for next unpaired delimiter
3726 (setq found (search-forward delim eol 'lim))
3727 (while (looking-at delim)
3728 (forward-char 1)
3729 (setq found (search-forward delim eol 'lim)))
3730 (if found
3731 (setq endq (- (point) 1))
3732 (setq endq (point)))
3733 ))
3734 (progn (setq bq (point)) (setq endq (point)))))
3735 (store-match-data data)
3736 ;; return string beginning position or nil
3737 (if (> start bq) bq))))
3738
3739 (defun idlwave-is-pointer-dereference (&optional limit)
3740 "Determines if the character after point is a pointer dereference *."
3741 (let ((pos (point)))
3742 (and
3743 (eq (char-after) ?\*)
3744 (not (idlwave-in-quote))
3745 (save-excursion
3746 (forward-char)
3747 (re-search-backward (concat "\\(" idlwave-idl-keywords
3748 "\\|[[(*+-/=,^><]\\)\\s-*\\*") limit t)))))
3749
3750
3751 ;; Statement templates
3752
3753 ;; Replace these with a general template function, something like
3754 ;; expand.el (I think there was also something with a name similar to
3755 ;; dmacro.el)
3756
3757 (defun idlwave-template (s1 s2 &optional prompt noindent)
3758 "Build a template with optional prompt expression.
3759
3760 Opens a line if point is not followed by a newline modulo intervening
3761 whitespace. S1 and S2 are strings. S1 is inserted at point followed
3762 by S2. Point is inserted between S1 and S2. The case of S1 and S2 is
3763 adjusted according to `idlwave-abbrev-change-case'. If optional argument
3764 PROMPT is a string then it is displayed as a message in the
3765 minibuffer. The PROMPT serves as a reminder to the user of an
3766 expression to enter.
3767
3768 The lines containing S1 and S2 are reindented using `indent-region'
3769 unless the optional second argument NOINDENT is non-nil."
3770 (if (eq major-mode 'idlwave-shell-mode)
3771 ;; This is a gross hack to avoit template abbrev expansion
3772 ;; in the shell. FIXME: This is a dirty hack.
3773 (if (and (eq this-command 'self-insert-command)
3774 (equal last-abbrev-location (point)))
3775 (insert last-abbrev-text)
3776 (error "No templates in idlwave-shell"))
3777 (cond ((eq idlwave-abbrev-change-case 'down)
3778 (setq s1 (downcase s1) s2 (downcase s2)))
3779 (idlwave-abbrev-change-case
3780 (setq s1 (upcase s1) s2 (upcase s2))))
3781 (let ((beg (save-excursion (beginning-of-line) (point)))
3782 end)
3783 (if (not (looking-at "\\s-*\n"))
3784 (open-line 1))
3785 (insert s1)
3786 (save-excursion
3787 (insert s2)
3788 (setq end (point)))
3789 (if (not noindent)
3790 (indent-region beg end nil))
3791 (if (stringp prompt)
3792 (message prompt)))))
3793
3794 (defun idlwave-rw-case (string)
3795 "Make STRING have the case required by `idlwave-reserved-word-upcase'."
3796 (if idlwave-reserved-word-upcase
3797 (upcase string)
3798 string))
3799
3800 (defun idlwave-elif ()
3801 "Build skeleton IDL if-else block."
3802 (interactive)
3803 (idlwave-template
3804 (idlwave-rw-case "if")
3805 (idlwave-rw-case " then begin\n\nendif else begin\n\nendelse")
3806 "Condition expression"))
3807
3808 (defun idlwave-case ()
3809 "Build skeleton IDL case statement."
3810 (interactive)
3811 (idlwave-template
3812 (idlwave-rw-case "case")
3813 (idlwave-rw-case " of\n\nendcase")
3814 "Selector expression"))
3815
3816 (defun idlwave-switch ()
3817 "Build skeleton IDL switch statement."
3818 (interactive)
3819 (idlwave-template
3820 (idlwave-rw-case "switch")
3821 (idlwave-rw-case " of\n\nendswitch")
3822 "Selector expression"))
3823
3824 (defun idlwave-for ()
3825 "Build skeleton for loop statment."
3826 (interactive)
3827 (idlwave-template
3828 (idlwave-rw-case "for")
3829 (idlwave-rw-case " do begin\n\nendfor")
3830 "Loop expression"))
3831
3832 (defun idlwave-if ()
3833 "Build skeleton for loop statment."
3834 (interactive)
3835 (idlwave-template
3836 (idlwave-rw-case "if")
3837 (idlwave-rw-case " then begin\n\nendif")
3838 "Scalar logical expression"))
3839
3840 (defun idlwave-procedure ()
3841 (interactive)
3842 (idlwave-template
3843 (idlwave-rw-case "pro")
3844 (idlwave-rw-case "\n\nreturn\nend")
3845 "Procedure name"))
3846
3847 (defun idlwave-function ()
3848 (interactive)
3849 (idlwave-template
3850 (idlwave-rw-case "function")
3851 (idlwave-rw-case "\n\nreturn\nend")
3852 "Function name"))
3853
3854 (defun idlwave-repeat ()
3855 (interactive)
3856 (idlwave-template
3857 (idlwave-rw-case "repeat begin\n\nendrep until")
3858 (idlwave-rw-case "")
3859 "Exit condition"))
3860
3861 (defun idlwave-while ()
3862 (interactive)
3863 (idlwave-template
3864 (idlwave-rw-case "while")
3865 (idlwave-rw-case " do begin\n\nendwhile")
3866 "Entry condition"))
3867
3868 (defun idlwave-split-string (string &optional pattern)
3869 "Return a list of substrings of STRING which are separated by PATTERN.
3870 If PATTERN is omitted, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
3871 (or pattern
3872 (setq pattern "[ \f\t\n\r\v]+"))
3873 (let (parts (start 0))
3874 (while (string-match pattern string start)
3875 (setq parts (cons (substring string start (match-beginning 0)) parts)
3876 start (match-end 0)))
3877 (nreverse (cons (substring string start) parts))))
3878
3879 (defun idlwave-replace-string (string replace_string replace_with)
3880 (let* ((start 0)
3881 (last (length string))
3882 (ret_string "")
3883 end)
3884 (while (setq end (string-match replace_string string start))
3885 (setq ret_string
3886 (concat ret_string (substring string start end) replace_with))
3887 (setq start (match-end 0)))
3888 (setq ret_string (concat ret_string (substring string start last)))))
3889
3890 (defun idlwave-get-buffer-visiting (file)
3891 ;; Return the buffer currently visiting FILE
3892 (cond
3893 ((boundp 'find-file-compare-truenames) ; XEmacs
3894 (let ((find-file-compare-truenames t))
3895 (get-file-buffer file)))
3896 ((fboundp 'find-buffer-visiting) ; Emacs
3897 (find-buffer-visiting file))
3898 (t (error "This should not happen (idlwave-get-buffer-visiting)"))))
3899
3900 (defvar idlwave-outlawed-buffers nil
3901 "List of buffer pulled up by idlwave for special reasons.
3902 Buffers in this list may be killed by `idlwave-kill-autoloaded-buffers'.")
3903
3904 (defun idlwave-find-file-noselect (file &optional why)
3905 ;; Return a buffer visiting file.
3906 (or (idlwave-get-buffer-visiting file)
3907 (let ((buf (find-file-noselect file)))
3908 (if why (add-to-list 'idlwave-outlawed-buffers (cons buf why)))
3909 buf)))
3910
3911 (defun idlwave-kill-autoloaded-buffers ()
3912 "Kill buffers created automatically by IDLWAVE.
3913 Function prompts for a letter to identify the buffers to kill.
3914 Possible letters are:
3915
3916 f Buffers created by the command \\[idlwave-find-module] or mouse
3917 clicks in the routine info window.
3918 s Buffers created by the IDLWAVE Shell to display where execution
3919 stopped or an error was found.
3920 a Both of the above.
3921
3922 Buffer containing unsaved changes require confirmation before they are killed."
3923 (interactive)
3924 (if (null idlwave-outlawed-buffers)
3925 (error "No IDLWAVE-created buffers available")
3926 (princ (format "Kill IDLWAVE-created buffers: [f]ind source(%d), [s]hell display(%d), [a]ll ? "
3927 (idlwave-count-outlawed-buffers 'find)
3928 (idlwave-count-outlawed-buffers 'shell)))
3929 (let ((c (read-char)))
3930 (cond
3931 ((member c '(?f ?\C-f))
3932 (idlwave-do-kill-autoloaded-buffers 'find))
3933 ((member c '(?s ?\C-s))
3934 (idlwave-do-kill-autoloaded-buffers 'shell))
3935 ((member c '(?a ?\C-a))
3936 (idlwave-do-kill-autoloaded-buffers t))
3937 (t (error "Abort"))))))
3938
3939 (defun idlwave-count-outlawed-buffers (tag)
3940 "How many outlawed buffers have tag TAG?"
3941 (length (delq nil
3942 (mapcar
3943 (lambda (x) (eq (cdr x) tag))
3944 idlwave-outlawed-buffers))))
3945
3946 (defun idlwave-do-kill-autoloaded-buffers (&rest reasons)
3947 "Kill all buffers pulled up by IDLWAVE matching REASONS."
3948 (let* ((list (copy-sequence idlwave-outlawed-buffers))
3949 (cnt 0)
3950 entry)
3951 (while (setq entry (pop list))
3952 (if (buffer-live-p (car entry))
3953 (and (or (memq t reasons)
3954 (memq (cdr entry) reasons))
3955 (kill-buffer (car entry))
3956 (incf cnt)
3957 (setq idlwave-outlawed-buffers
3958 (delq entry idlwave-outlawed-buffers)))
3959 (setq idlwave-outlawed-buffers
3960 (delq entry idlwave-outlawed-buffers))))
3961 (message "%d buffer%s killed" cnt (if (= cnt 1) "" "s"))))
3962
3963 (defun idlwave-revoke-license-to-kill ()
3964 "Remove BUFFER from the buffers which may be killed.
3965 Killing would be done by `idlwave-do-kill-autoloaded-buffers'.
3966 Intended for `after-save-hook'."
3967 (let* ((buf (current-buffer))
3968 (entry (assq buf idlwave-outlawed-buffers)))
3969 ;; Revoke license
3970 (if entry
3971 (setq idlwave-outlawed-buffers
3972 (delq entry idlwave-outlawed-buffers)))
3973 ;; Remove this function from the hook.
3974 (remove-hook 'after-save-hook 'idlwave-revoke-license-to-kill 'local)))
3975
3976 (defvar idlwave-path-alist)
3977 (defun idlwave-locate-lib-file (file)
3978 ;; Find FILE on the scanned lib path and return a buffer visiting it
3979 (let* ((dirs idlwave-path-alist)
3980 dir efile)
3981 (catch 'exit
3982 (while (setq dir (car (pop dirs)))
3983 (if (file-regular-p
3984 (setq efile (expand-file-name file dir)))
3985 (throw 'exit efile))))))
3986
3987 (defun idlwave-expand-lib-file-name (file)
3988 ;; Find FILE on the scanned lib path and return a buffer visiting it
3989 ;; This is for, e.g., finding source with no user catalog
3990 (cond
3991 ((null file) nil)
3992 ((file-name-absolute-p file) file)
3993 (t (idlwave-locate-lib-file file))))
3994
3995 (defun idlwave-make-tags ()
3996 "Creates the IDL tags file IDLTAGS in the current directory from
3997 the list of directories specified in the minibuffer. Directories may be
3998 for example: . /usr/local/rsi/idl/lib. All the subdirectories of the
3999 specified top directories are searched if the directory name is prefixed
4000 by @. Specify @ directories with care, it may take a long, long time if
4001 you specify /."
4002 (interactive)
4003 (let (directory directories cmd append status numdirs dir getsubdirs
4004 buffer save_buffer files numfiles item errbuf)
4005
4006 ;;
4007 ;; Read list of directories
4008 (setq directory (read-string "Tag Directories: " "."))
4009 (setq directories (idlwave-split-string directory "[ \t]+"))
4010 ;;
4011 ;; Set etags command, vars
4012 (setq cmd "etags --output=IDLTAGS --language=none --regex='/[
4013 \\t]*[pP][Rr][Oo][ \\t]+\\([^ \\t,]+\\)/' --regex='/[
4014 \\t]*[Ff][Uu][Nn][Cc][Tt][Ii][Oo][Nn][ \\t]+\\([^ \\t,]+\\)/' ")
4015 (setq append " ")
4016 (setq status 0)
4017 ;;
4018 ;; For each directory
4019 (setq numdirs 0)
4020 (setq dir (nth numdirs directories))
4021 (while (and dir)
4022 ;;
4023 ;; Find the subdirectories
4024 (if (string-match "^[@]\\(.+\\)$" dir)
4025 (setq getsubdirs t) (setq getsubdirs nil))
4026 (if (and getsubdirs) (setq dir (substring dir 1 (length dir))))
4027 (setq dir (expand-file-name dir))
4028 (if (file-directory-p dir)
4029 (progn
4030 (if (and getsubdirs)
4031 (progn
4032 (setq buffer (get-buffer-create "*idltags*"))
4033 (call-process "sh" nil buffer nil "-c"
4034 (concat "find " dir " -type d -print"))
4035 (setq save_buffer (current-buffer))
4036 (set-buffer buffer)
4037 (setq files (idlwave-split-string
4038 (idlwave-replace-string
4039 (buffer-substring 1 (point-max))
4040 "\n" "/*.pro ")
4041 "[ \t]+"))
4042 (set-buffer save_buffer)
4043 (kill-buffer buffer))
4044 (setq files (list (concat dir "/*.pro"))))
4045 ;;
4046 ;; For each subdirectory
4047 (setq numfiles 0)
4048 (setq item (nth numfiles files))
4049 (while (and item)
4050 ;;
4051 ;; Call etags
4052 (if (not (string-match "^[ \\t]*$" item))
4053 (progn
4054 (message "%s" (concat "Tagging " item "..."))
4055 (setq errbuf (get-buffer-create "*idltags-error*"))
4056 (setq status (+ status
4057 (if (eq 0 (call-process
4058 "sh" nil errbuf nil "-c"
4059 (concat cmd append item)))
4060 0
4061 1)))
4062 ;;
4063 ;; Append additional tags
4064 (setq append " --append ")
4065 (setq numfiles (1+ numfiles))
4066 (setq item (nth numfiles files)))
4067 (progn
4068 (setq numfiles (1+ numfiles))
4069 (setq item (nth numfiles files))
4070 )))
4071
4072 (setq numdirs (1+ numdirs))
4073 (setq dir (nth numdirs directories)))
4074 (progn
4075 (setq numdirs (1+ numdirs))
4076 (setq dir (nth numdirs directories)))))
4077
4078 (setq errbuf (get-buffer-create "*idltags-error*"))
4079 (if (= status 0)
4080 (kill-buffer errbuf))
4081 (message "")
4082 ))
4083
4084 (defun idlwave-toggle-comment-region (beg end &optional n)
4085 "Comment the lines in the region if the first non-blank line is
4086 commented, and conversely, uncomment region. If optional prefix arg
4087 N is non-nil, then for N positive, add N comment delimiters or for N
4088 negative, remove N comment delimiters.
4089 Uses `comment-region' which does not place comment delimiters on
4090 blank lines."
4091 (interactive "r\nP")
4092 (if n
4093 (comment-region beg end (prefix-numeric-value n))
4094 (save-excursion
4095 (goto-char beg)
4096 (beginning-of-line)
4097 ;; skip blank lines
4098 (skip-chars-forward " \t\n")
4099 (if (looking-at (concat "[ \t]*\\(" comment-start "+\\)"))
4100 (if (fboundp 'uncomment-region)
4101 (uncomment-region beg end)
4102 (comment-region beg end
4103 (- (length (buffer-substring
4104 (match-beginning 1)
4105 (match-end 1))))))
4106 (comment-region beg end)))))
4107
4108
4109 ;; ----------------------------------------------------------------------------
4110 ;; ----------------------------------------------------------------------------
4111 ;; ----------------------------------------------------------------------------
4112 ;; ----------------------------------------------------------------------------
4113 ;;
4114 ;; Completion and Routine Info
4115 ;;
4116
4117 ;; String "intern" functions
4118
4119 ;; For the completion and routine info function, we want to normalize
4120 ;; the case of procedure names etc. We do this by "interning" these
4121 ;; string is a hand-crafted way. Hashes are used to map the downcase
4122 ;; version of the strings to the cased versions. Most *-sint-*
4123 ;; variables consist of *two* hashes, a buffer+shell, followed by a
4124 ;; system hash. The former is re-scanned, and the latter takes case
4125 ;; precedence.
4126 ;;
4127 ;; Since these cased versions are really lisp objects, we can use `eq'
4128 ;; to search, which is a large performance boost. All new strings
4129 ;; need to be "sinterned". We do this as early as possible after
4130 ;; getting these strings from completion or buffer substrings. So
4131 ;; most of the code can simply assume to deal with "sinterned"
4132 ;; strings. The only exception is that the functions which scan whole
4133 ;; buffers for routine information do not intern the grabbed strings.
4134 ;; This is only done afterwards. Therefore in these functions it is
4135 ;; *not* safe to assume the strings can be compared with `eq' and be
4136 ;; fed into the routine assq functions.
4137
4138 ;; Here we define the hashing functions.
4139
4140 ;; The variables which hold the hashes.
4141 (defvar idlwave-sint-routines '(nil))
4142 (defvar idlwave-sint-keywords '(nil))
4143 (defvar idlwave-sint-methods '(nil))
4144 (defvar idlwave-sint-classes '(nil))
4145 (defvar idlwave-sint-dirs '(nil))
4146 (defvar idlwave-sint-libnames '(nil))
4147
4148 (defun idlwave-reset-sintern (&optional what)
4149 "Reset all sintern hashes."
4150 ;; Make sure the hash functions are accessible.
4151 (if (or (not (fboundp 'gethash))
4152 (not (fboundp 'puthash)))
4153 (progn
4154 (require 'cl)
4155 (or (fboundp 'puthash)
4156 (defalias 'puthash 'cl-puthash))))
4157 (let ((entries '((idlwave-sint-routines 1000 10)
4158 (idlwave-sint-keywords 1000 10)
4159 (idlwave-sint-methods 100 10)
4160 (idlwave-sint-classes 10 10))))
4161
4162 ;; Make sure these are lists
4163 (loop for entry in entries
4164 for var = (car entry)
4165 do (if (not (consp (symbol-value var))) (set var (list nil))))
4166
4167 ;; Reset the system & library hash
4168 (when (or (eq what t) (eq what 'syslib)
4169 (null (cdr idlwave-sint-routines)))
4170 (loop for entry in entries
4171 for var = (car entry) for size = (nth 1 entry)
4172 do (setcdr (symbol-value var)
4173 (make-hash-table ':size size ':test 'equal)))
4174 (setq idlwave-sint-dirs nil
4175 idlwave-sint-libnames nil))
4176
4177 ;; Reset the buffer & shell hash
4178 (when (or (eq what t) (eq what 'bufsh)
4179 (null (car idlwave-sint-routines)))
4180 (loop for entry in entries
4181 for var = (car entry) for size = (nth 1 entry)
4182 do (setcar (symbol-value var)
4183 (make-hash-table ':size size ':test 'equal))))))
4184
4185 (defun idlwave-sintern-routine-or-method (name &optional class set)
4186 (if class
4187 (idlwave-sintern-method name set)
4188 (idlwave-sintern-routine name set)))
4189
4190 (defun idlwave-sintern (stype &rest args)
4191 (apply (intern (concat "idlwave-sintern-" (symbol-name stype))) args))
4192
4193 ;;(defmacro idlwave-sintern (type var)
4194 ;; `(cond ((not (stringp name)) name)
4195 ;; ((gethash (downcase name) (cdr ,var)))
4196 ;; ((gethash (downcase name) (car ,var)))
4197 ;; (set (idlwave-sintern-set name ,type ,var set))
4198 ;; (name)))
4199
4200 (defun idlwave-sintern-routine (name &optional set)
4201 (cond ((not (stringp name)) name)
4202 ((gethash (downcase name) (cdr idlwave-sint-routines)))
4203 ((gethash (downcase name) (car idlwave-sint-routines)))
4204 (set (idlwave-sintern-set name 'routine idlwave-sint-routines set))
4205 (name)))
4206 (defun idlwave-sintern-keyword (name &optional set)
4207 (cond ((not (stringp name)) name)
4208 ((gethash (downcase name) (cdr idlwave-sint-keywords)))
4209 ((gethash (downcase name) (car idlwave-sint-keywords)))
4210 (set (idlwave-sintern-set name 'keyword idlwave-sint-keywords set))
4211 (name)))
4212 (defun idlwave-sintern-method (name &optional set)
4213 (cond ((not (stringp name)) name)
4214 ((gethash (downcase name) (cdr idlwave-sint-methods)))
4215 ((gethash (downcase name) (car idlwave-sint-methods)))
4216 (set (idlwave-sintern-set name 'method idlwave-sint-methods set))
4217 (name)))
4218 (defun idlwave-sintern-class (name &optional set)
4219 (cond ((not (stringp name)) name)
4220 ((gethash (downcase name) (cdr idlwave-sint-classes)))
4221 ((gethash (downcase name) (car idlwave-sint-classes)))
4222 (set (idlwave-sintern-set name 'class idlwave-sint-classes set))
4223 (name)))
4224
4225 (defun idlwave-sintern-dir (dir &optional set)
4226 (car (or (member dir idlwave-sint-dirs)
4227 (setq idlwave-sint-dirs (cons dir idlwave-sint-dirs)))))
4228 (defun idlwave-sintern-libname (name &optional set)
4229 (car (or (member name idlwave-sint-libnames)
4230 (setq idlwave-sint-libnames (cons name idlwave-sint-libnames)))))
4231
4232 (defun idlwave-sintern-set (name type tables set)
4233 (let* ((func (or (cdr (assq type idlwave-completion-case))
4234 'identity))
4235 (iname (funcall (if (eq func 'preserve) 'identity func) name))
4236 (table (if (eq set 'sys) (cdr tables) (car tables))))
4237 (puthash (downcase name) iname table)
4238 iname))
4239
4240 (defun idlwave-sintern-keyword-list (kwd-list &optional set)
4241 "Sintern a set of keywords (file (key . link) (key2 . link2) ...)"
4242 (mapcar (lambda(x)
4243 (setcar x (idlwave-sintern-keyword (car x) set)))
4244 (cdr kwd-list))
4245 kwd-list)
4246
4247 (defun idlwave-sintern-rinfo-list (list &optional set default-dir)
4248 "Sintern all strings in the rinfo LIST. With optional parameter
4249 SET: also set new patterns. Probably this will always have to be t.
4250 If DEFAULT-DIR is passed, it is used as the base of the directory"
4251 (let (entry name type class kwds res source call new)
4252 (while list
4253 (setq entry (car list)
4254 list (cdr list)
4255 name (car entry)
4256 type (nth 1 entry)
4257 class (nth 2 entry)
4258 source (nth 3 entry)
4259 call (nth 4 entry)
4260 kwds (nthcdr 5 entry))
4261
4262 ;; The class and name
4263 (if class
4264 (progn
4265 (if (symbolp class) (setq class (symbol-name class)))
4266 (setq class (idlwave-sintern-class class set))
4267 (setq name (idlwave-sintern-method name set)))
4268 (setq name (idlwave-sintern-routine name set)))
4269
4270 ;; The source
4271 (let ((source-type (car source))
4272 (source-file (nth 1 source))
4273 (source-dir (if default-dir
4274 (file-name-as-directory default-dir)
4275 (nth 2 source)))
4276 (source-lib (nth 3 source)))
4277 (if (stringp source-dir)
4278 (setq source-dir (idlwave-sintern-dir source-dir set)))
4279 (if (stringp source-lib)
4280 (setq source-lib (idlwave-sintern-libname source-lib set)))
4281 (setq source (list source-type source-file source-dir source-lib)))
4282
4283 ;; The keywords
4284 (setq kwds (mapcar (lambda (x)
4285 (idlwave-sintern-keyword-list x set))
4286 kwds))
4287
4288 ;; Build a canonicalized list
4289 (setq new (nconc (list name type class source call) kwds)
4290 res (cons new res)))
4291 (nreverse res)))
4292
4293 ;; Creating new sintern tables
4294
4295 (defun idlwave-new-sintern-type (tag)
4296 "Define a variable and a function to sintern the new type TAG.
4297 This defines the function `idlwave-sintern-TAG' and the variable
4298 `idlwave-sint-TAGs'."
4299 (let* ((name (symbol-name tag))
4300 (names (concat name "s"))
4301 (var (intern (concat "idlwave-sint-" names)))
4302 (func (intern (concat "idlwave-sintern-" name))))
4303 (set var nil) ; initial value of the association list
4304 (fset func ; set the function
4305 `(lambda (name &optional set)
4306 (cond ((not (stringp name)) name)
4307 ((cdr (assoc (downcase name) ,var)))
4308 (set
4309 (setq ,var (cons (cons (downcase name) name) ,var))
4310 name)
4311 (name))))))
4312
4313 (defun idlwave-reset-sintern-type (tag)
4314 "Reset the sintern variable associated with TAG."
4315 (set (intern (concat "idlwave-sint-" (symbol-name tag) "s")) nil))
4316
4317 ;;---------------------------------------------------------------------------
4318
4319
4320 ;; The variables which hold the information
4321 (defvar idlwave-system-routines nil
4322 "Holds the routine-info obtained by scanning buffers.")
4323 (defvar idlwave-buffer-routines nil
4324 "Holds the routine-info obtained by scanning buffers.")
4325 (defvar idlwave-compiled-routines nil
4326 "Holds the routine-info obtained by asking the shell.")
4327 (defvar idlwave-unresolved-routines nil
4328 "Holds the unresolved routine-info obtained by asking the shell.")
4329 (defvar idlwave-user-catalog-routines nil
4330 "Holds the procedure routine-info from the user scan.")
4331 (defvar idlwave-library-catalog-routines nil
4332 "Holds the procedure routine-info from the .idlwave_catalog library files.")
4333 (defvar idlwave-library-catalog-libname nil
4334 "Name of library catalog loaded from .idlwave_catalog files.")
4335 (defvar idlwave-path-alist nil
4336 "Alist with !PATH directories and zero or more flags if the dir has
4337 been scanned in a user catalog ('user) or discovered in a library
4338 catalog \('lib).")
4339 (defvar idlwave-true-path-alist nil
4340 "Like `idlwave-path-alist', but with true filenames.")
4341 (defvar idlwave-routines nil
4342 "Holds the combinded procedure/function/method routine-info.")
4343 (defvar idlwave-class-alist nil
4344 "Holds the class names known to IDLWAVE.")
4345 (defvar idlwave-class-history nil
4346 "The history of classes selected with the minibuffer.")
4347 (defvar idlwave-force-class-query nil)
4348 (defvar idlwave-before-completion-wconf nil
4349 "The window configuration just before the completion buffer was displayed.")
4350 (defvar idlwave-last-system-routine-info-cons-cell nil
4351 "The last cons cell in the system routine info.")
4352
4353 ;;
4354 ;; The code to get routine info from different sources.
4355
4356 (defvar idlwave-system-routines)
4357 (defvar idlwave-catalog-process nil
4358 "The background process currently updating the catalog.")
4359
4360 (defun idlwave-routines ()
4361 "Provide a list of IDL routines.
4362 This routine loads the builtin routines on the first call. Later it
4363 only returns the value of the variable."
4364 (if (and idlwave-catalog-process
4365 (processp idlwave-catalog-process))
4366 (progn
4367 (cond
4368 ((equal (process-status idlwave-catalog-process) 'exit)
4369 (message "updating........")
4370 (setq idlwave-catalog-process nil)
4371 (idlwave-update-routine-info '(4)))
4372 ((equal (process-status idlwave-catalog-process) 'run)
4373 ;; Keep it running...
4374 )
4375 (t
4376 ;; Something is wrong, get rid of the process
4377 (message "Problem with catalog process") (beep)
4378 (condition-case nil
4379 (kill-process idlwave-catalog-process)
4380 (error nil))
4381 (setq idlwave-catalog-process nil)))))
4382 (or idlwave-routines
4383 (progn
4384 (idlwave-update-routine-info)
4385 ;; return the current value
4386 idlwave-routines)))
4387
4388 (defvar idlwave-update-rinfo-hook nil
4389 "List of functions which should run after a global rinfo update.
4390 Does not run after automatic updates of buffer or the shell.")
4391
4392 (defun idlwave-rescan-catalog-directories ()
4393 "Rescan the previously selected directories. For batch processing."
4394 (idlwave-update-routine-info '(16)))
4395
4396 (defun idlwave-rescan-asynchronously ()
4397 "Dispatch another Emacs instance to update the idlwave catalog.
4398 After the process finishes normally, the first access to routine info
4399 will re-read the catalog."
4400 (interactive)
4401 (if (processp idlwave-catalog-process)
4402 (if (eq (process-status idlwave-catalog-process) 'run)
4403 (if (yes-or-no-p "A catalog-updating process is running. Kill it? ")
4404 (progn
4405 (condition-case nil
4406 (kill-process idlwave-catalog-process)
4407 (error nil))
4408 (error "Process killed, no new process started"))
4409 (error "Quit"))
4410 (condition-case nil
4411 (kill-process idlwave-catalog-process)
4412 (error nil))))
4413 (if (or (not idlwave-user-catalog-file)
4414 (not (stringp idlwave-user-catalog-file))
4415 (not (file-regular-p idlwave-user-catalog-file)))
4416 (error "No catalog has been produced yet"))
4417 (let* ((emacs (concat invocation-directory invocation-name))
4418 (args (list "-batch"
4419 "-l" (expand-file-name "~/.emacs")
4420 "-l" "idlwave"
4421 "-f" "idlwave-rescan-catalog-directories"))
4422 (process (apply 'start-process "idlcat"
4423 nil emacs args)))
4424 (setq idlwave-catalog-process process)
4425 (set-process-sentinel
4426 process
4427 (lambda (pro why)
4428 (when (string-match "finished" why)
4429 (setq idlwave-routines nil
4430 idlwave-system-routines nil
4431 idlwave-catalog-process nil)
4432 (or (idlwave-start-load-rinfo-timer)
4433 (idlwave-update-routine-info '(4))))))
4434 (message "Background job started to update catalog file")))
4435
4436
4437 ;; Format for all routine info user catalog, library catalogs, etc.:
4438 ;;
4439 ;; ("ROUTINE" type class
4440 ;; (system) | (lib pro_file dir "LIBNAME") | (user pro_file dir "USERLIB") |
4441 ;; (buffer pro_file dir) | (compiled pro_file dir)
4442 ;; "calling_string" ("HELPFILE" (("KWD1" . link1) ...))
4443 ;; ("HELPFILE2" (("KWD2" . link) ...)) ...)
4444 ;;
4445 ;; DIR will be supplied dynamically while loading library catalogs,
4446 ;; and is sinterned to save space, as is LIBNAME. PRO_FILE can be a
4447 ;; complete filepath, in which case DIR is unnecessary. HELPFILE can
4448 ;; be nil, as can LINK1, etc., if no HTML help is available.
4449
4450
4451 (defvar idlwave-load-rinfo-idle-timer)
4452 (defvar idlwave-shell-path-query)
4453
4454 (defun idlwave-update-routine-info (&optional arg no-concatenate)
4455 "Update the internal routine-info lists.
4456 These lists are used by `idlwave-routine-info' (\\[idlwave-routine-info])
4457 and by `idlwave-complete' (\\[idlwave-complete]) to provide information
4458 about individual routines.
4459
4460 The information can come from 4 sources:
4461 1. IDL programs in the current editing session
4462 2. Compiled modules in an IDL shell running as Emacs subprocess
4463 3. A list which covers the IDL system routines.
4464 4. A list which covers the prescanned library files.
4465
4466 Scans all IDLWAVE-mode buffers of the current editing session (see
4467 `idlwave-scan-all-buffers-for-routine-info').
4468 When an IDL shell is running, this command also queries the IDL program
4469 for currently compiled routines.
4470
4471 With prefix ARG, also reload the system and library lists.
4472 With two prefix ARG's, also rescans the chosen user catalog tree.
4473 With three prefix args, dispatch asynchronous process to do the update.
4474
4475 If NO-CONCATENATE is non-nil, don't pre-concatenate the routine info
4476 lists, but instead wait for the shell query to complete and
4477 asynchronously finish updating routine info. This is set
4478 automatically when called interactively. When you need routine
4479 information updated immediately, leave NO-CONCATENATE nil."
4480 (interactive "P\np")
4481 ;; Stop any idle processing
4482 (if (or (and (fboundp 'itimerp)
4483 (itimerp idlwave-load-rinfo-idle-timer))
4484 (and (fboundp 'timerp)
4485 (timerp idlwave-load-rinfo-idle-timer)))
4486 (cancel-timer idlwave-load-rinfo-idle-timer))
4487 (cond
4488 ((equal arg '(64))
4489 ;; Start a background process which updates the catalog.
4490 (idlwave-rescan-asynchronously))
4491 ((equal arg '(16))
4492 ;; Update the user catalog now, and wait for them.
4493 (idlwave-create-user-catalog-file t))
4494 (t
4495 (let* ((load (or arg
4496 idlwave-buffer-case-takes-precedence
4497 (null idlwave-routines)))
4498 ;; The override-idle means, even if the idle timer has done some
4499 ;; preparing work, load and renormalize everything anyway.
4500 (override-idle (or arg idlwave-buffer-case-takes-precedence)))
4501
4502 (setq idlwave-buffer-routines nil
4503 idlwave-compiled-routines nil
4504 idlwave-unresolved-routines nil)
4505 ;; Reset the appropriate hashes
4506 (if (get 'idlwave-reset-sintern 'done-by-idle)
4507 ;; reset was already done in idle time, so skip this step now once
4508 (put 'idlwave-reset-sintern 'done-by-idle nil)
4509 (idlwave-reset-sintern (cond (load t)
4510 ((null idlwave-system-routines) t)
4511 (t 'bufsh))))
4512
4513 (if idlwave-buffer-case-takes-precedence
4514 ;; We can safely scan the buffer stuff first
4515 (progn
4516 (idlwave-update-buffer-routine-info)
4517 (and load (idlwave-load-all-rinfo override-idle)))
4518 ;; We first do the system info, and then the buffers
4519 (and load (idlwave-load-all-rinfo override-idle))
4520 (idlwave-update-buffer-routine-info))
4521
4522 ;; Let's see if there is a shell
4523 (let* ((shell-is-running (and (fboundp 'idlwave-shell-is-running)
4524 (idlwave-shell-is-running)))
4525 (ask-shell (and shell-is-running
4526 idlwave-query-shell-for-routine-info)))
4527
4528 ;; Load the library catalogs again, first re-scanning the path
4529 (when arg
4530 (if shell-is-running
4531 (idlwave-shell-send-command idlwave-shell-path-query
4532 '(progn
4533 (idlwave-shell-get-path-info)
4534 (idlwave-scan-library-catalogs))
4535 'hide)
4536 (idlwave-scan-library-catalogs)))
4537
4538 (if (or (not ask-shell)
4539 (not no-concatenate))
4540 ;; 1. If we are not going to ask the shell, we need to do the
4541 ;; concatenation now.
4542 ;; 2. When this function is called non-interactively, it
4543 ;; means that someone needs routine info *now*. The
4544 ;; shell update causes the concatenation to be
4545 ;; *delayed*, so not in time for the current command.
4546 ;; Therefore, we do a concatenation now, even though
4547 ;; the shell might do it again.
4548 (idlwave-concatenate-rinfo-lists nil 'run-hooks))
4549
4550 (when ask-shell
4551 ;; Ask the shell about the routines it knows of.
4552 (message "Querying the shell")
4553 (idlwave-shell-update-routine-info nil t)))))))
4554
4555
4556 (defvar idlwave-load-rinfo-steps-done (make-vector 6 nil))
4557 (defvar idlwave-load-rinfo-idle-timer nil)
4558 (defun idlwave-start-load-rinfo-timer ()
4559 (if (or (and (fboundp 'itimerp)
4560 (itimerp idlwave-load-rinfo-idle-timer))
4561 (and (fboundp 'timerp)
4562 (timerp idlwave-load-rinfo-idle-timer)))
4563 (cancel-timer idlwave-load-rinfo-idle-timer))
4564 (setq idlwave-load-rinfo-steps-done (make-vector 6 nil))
4565 (setq idlwave-load-rinfo-idle-timer nil)
4566 (if (and idlwave-init-rinfo-when-idle-after
4567 (numberp idlwave-init-rinfo-when-idle-after)
4568 (not (equal 0 idlwave-init-rinfo-when-idle-after))
4569 (not idlwave-routines))
4570 (condition-case nil
4571 (progn
4572 (setq idlwave-load-rinfo-idle-timer
4573 (run-with-idle-timer
4574 idlwave-init-rinfo-when-idle-after
4575 nil 'idlwave-load-rinfo-next-step)))
4576 (error nil))))
4577
4578 (defvar idlwave-library-routines nil "Obsolete variable.")
4579
4580 ;;------ XML Help routine info system
4581 (defun idlwave-load-system-routine-info ()
4582 ;; Load the system routine info from the cached routine info file,
4583 ;; which, if necessary, will be re-created from the XML file on
4584 ;; disk. As a last fallback, load the (likely outdated) idlw-rinfo
4585 ;; file distributed with older IDLWAVE versions (<6.0)
4586 (unless (and (load idlwave-xml-system-rinfo-converted-file
4587 'noerror 'nomessage)
4588 (idlwave-xml-system-routine-info-up-to-date))
4589 ;; See if we can create it from XML source
4590 (condition-case nil
4591 (idlwave-convert-xml-system-routine-info)
4592 (error
4593 (unless (load idlwave-xml-system-rinfo-converted-file
4594 'noerror 'nomessage)
4595 (if idlwave-system-routines
4596 (message
4597 "Failed to load converted routine info, using old conversion.")
4598 (message
4599 "Failed to convert XML routine info, falling back on idlw-rinfo.")
4600 (if (not (load "idlw-rinfo" 'noerror 'nomessage))
4601 (message
4602 "Could not locate any system routine information."))))))))
4603
4604 (defun idlwave-xml-system-routine-info-up-to-date()
4605 (let* ((dir (file-name-as-directory
4606 (expand-file-name "help/online_help" (idlwave-sys-dir))))
4607 (catalog-file (expand-file-name "idl_catalog.xml" dir)))
4608 (file-newer-than-file-p ;converted file is newer than catalog
4609 idlwave-xml-system-rinfo-converted-file
4610 catalog-file)))
4611
4612 (defvar idlwave-system-class-info nil) ; Gathered from idlw-rinfo
4613 (defvar idlwave-system-variables-alist nil
4614 "Alist of system variables and the associated structure tags.
4615 Gets set in cached XML rinfo, or `idlw-rinfo.el'.")
4616 (defvar idlwave-executive-commands-alist nil
4617 "Alist of system variables and their help files.")
4618 (defvar idlwave-help-special-topic-words nil)
4619
4620
4621 (defun idlwave-shorten-syntax (syntax name &optional class)
4622 ;; From a list of syntax statments, shorten with %s and group with "or"
4623 (let ((case-fold-search t))
4624 (mapconcat
4625 (lambda (x)
4626 (while (string-match name x)
4627 (setq x (replace-match "%s" t t x)))
4628 (if class
4629 (while (string-match class x)
4630 (setq x (replace-match "%s" t t x))))
4631 x)
4632 (nreverse syntax)
4633 " or ")))
4634
4635 (defun idlwave-xml-create-class-method-lists (xml-entry)
4636 ;; Create a class list entry from the xml parsed list., returning a
4637 ;; cons of form (class-entry method-entries).
4638 (let* ((nameblock (nth 1 xml-entry))
4639 (class (cdr (assq 'name nameblock)))
4640 (link (cdr (assq 'link nameblock)))
4641 (params (cddr xml-entry))
4642 (case-fold-search t)
4643 class-entry
4644 method methods-entry extra-kwds
4645 props get-props set-props init-props inherits
4646 pelem ptype)
4647 (while params
4648 (setq pelem (car params))
4649 (when (listp pelem)
4650 (setq ptype (car pelem)
4651 props (car (cdr pelem)))
4652 (cond
4653 ((eq ptype 'SUPERCLASS)
4654 (let ((pname (cdr (assq 'name props)))
4655 (plink (cdr (assq 'link props))))
4656 (unless (and (string= pname "None")
4657 (string= plink "None"))
4658 (push pname inherits))))
4659
4660 ((eq ptype 'PROPERTY)
4661 (let ((pname (cdr (assq 'name props)))
4662 (plink (cdr (assq 'link props)))
4663 (get (string= (cdr (assq 'get props)) "Yes"))
4664 (set (string= (cdr (assq 'set props)) "Yes"))
4665 (init (string= (cdr (assq 'init props)) "Yes")))
4666 (if get (push (list pname plink) get-props))
4667 (if set (push (list pname plink) set-props))
4668 (if init (push (list pname plink) init-props))))
4669
4670 ((eq ptype 'METHOD)
4671 (setq method (cdr (assq 'name props)))
4672 (setq extra-kwds ;;Assume all property keywords are gathered already
4673 (cond
4674 ((string-match (concat class "::Init") method)
4675 (put 'init-props 'matched t)
4676 init-props)
4677 ((string-match (concat class "::GetProperty") method)
4678 (put 'get-props 'matched t)
4679 get-props)
4680 ((string-match (concat class "::SetProperty") method)
4681 (put 'set-props 'matched t)
4682 set-props)
4683 (t nil)))
4684 (setq methods-entry
4685 (nconc (idlwave-xml-create-rinfo-list pelem class extra-kwds)
4686 methods-entry)))
4687 (t)))
4688 (setq params (cdr params)))
4689 ;(unless (get 'init-props 'matched)
4690 ; (message "Failed to match Init in class %s" class))
4691 ;(unless (get 'get-props 'matched)
4692 ; (message "Failed to match GetProperty in class %s" class))
4693 ;(unless (get 'set-props 'matched)
4694 ; (message "Failed to match SetProperty in class %s" class))
4695 (setq class-entry
4696 (if inherits
4697 (list class (append '(inherits) inherits) (list 'link link))
4698 (list class (list 'link link))))
4699 (cons class-entry methods-entry)))
4700
4701 (defun idlwave-xml-create-rinfo-list (xml-entry &optional class extra-kws)
4702 ;; Create correctly structured list elements from ROUTINE or METHOD
4703 ;; XML list structures. Return a list of list elements, with more
4704 ;; than one sub-list possible if a routine can serve as both
4705 ;; procedure and function (e.g. call_method).
4706 (let* ((nameblock (nth 1 xml-entry))
4707 (name (cdr (assq 'name nameblock)))
4708 (link (cdr (assq 'link nameblock)))
4709 (params (cddr xml-entry))
4710 (syntax-vec (make-vector 3 nil)) ; procedure, function, exec command
4711 (case-fold-search t)
4712 syntax kwd klink pref-list kwds pelem ptype entry props result type)
4713 (if class ;; strip out class name from class method name string
4714 (if (string-match (concat class "::") name)
4715 (setq name (substring name (match-end 0)))))
4716 (while params
4717 (setq pelem (car params))
4718 (when (listp pelem)
4719 (setq ptype (car pelem)
4720 props (car (cdr pelem)))
4721 (cond
4722 ((eq ptype 'SYNTAX)
4723 (setq syntax (cdr (assq 'name props)))
4724 (if (string-match "-&gt;" syntax)
4725 (setq syntax (replace-match "->" t nil syntax)))
4726 (setq type (cdr (assq 'type props)))
4727 (push syntax
4728 (aref syntax-vec (cond
4729 ((string-match "^pro" type) 0)
4730 ((string-match "^fun" type) 1)
4731 ((string-match "^exec" type) 2)))))
4732 ((eq ptype 'KEYWORD)
4733 (setq kwd (cdr (assq 'name props))
4734 klink (cdr (assq 'link props)))
4735 (if (string-match "^\\[XY\\(Z?\\)\\]" kwd)
4736 (progn
4737 (setq pref-list
4738 (if (match-string 1 kwd) '("X" "Y" "Z") '("X" "Y"))
4739 kwd (substring kwd (match-end 0)))
4740 (loop for x in pref-list do
4741 (push (list (concat x kwd) klink) kwds)))
4742 (push (list kwd klink) kwds)))
4743
4744 (t))); Do nothing for the others
4745 (setq params (cdr params)))
4746
4747 ;; Debug
4748 ; (if (and (null (aref syntax-vec 0))
4749 ; (null (aref syntax-vec 1))
4750 ; (null (aref syntax-vec 2)))
4751 ; (with-current-buffer (get-buffer-create "IDL_XML_catalog_complaints")
4752 ; (if class
4753 ; (insert (format "Missing SYNTAX entry for %s::%s\n" class name))
4754 ; (insert (message "Missing SYNTAX entry for %s\n" name)))))
4755
4756 ;; Executive commands are treated specially
4757 (if (aref syntax-vec 2)
4758 (cons (substring name 1) link)
4759 (if extra-kws (setq kwds (nconc kwds extra-kws)))
4760 (setq kwds (idlwave-rinfo-group-keywords kwds link))
4761 (loop for idx from 0 to 1 do
4762 (if (aref syntax-vec idx)
4763 (push (append (list name (if (eq idx 0) 'pro 'fun)
4764 class '(system)
4765 (idlwave-shorten-syntax
4766 (aref syntax-vec idx) name class))
4767 kwds) result)))
4768 result)))
4769
4770
4771 (defun idlwave-rinfo-group-keywords (kwds master-link)
4772 ;; Group keywords by link file, as a list with elements
4773 ;; (linkfile ( ("KWD1" . link1) ("KWD2" . link2))
4774 (let (kwd link anchor linkfiles block master-elt)
4775 (while kwds
4776 (setq kwd (car kwds)
4777 link (idlwave-split-link-target (nth 1 kwd))
4778 anchor (cdr link)
4779 link (car link)
4780 kwd (car kwd))
4781 (if (setq block (assoc link linkfiles))
4782 (push (cons kwd anchor) (cdr block))
4783 (push (list link (cons kwd anchor)) linkfiles))
4784 (setq kwds (cdr kwds)))
4785 ;; Ensure the master link is there
4786 (if (setq master-elt (assoc master-link linkfiles))
4787 (if (eq (car linkfiles) master-elt)
4788 linkfiles
4789 (cons master-elt (delq master-elt linkfiles)))
4790 (push (list master-link) linkfiles))))
4791
4792 (defun idlwave-convert-xml-clean-statement-aliases (aliases)
4793 ;; Clean up the syntax of routines which are actually aliases by
4794 ;; removing the "OR" from the statements
4795 (let (syntax entry)
4796 (loop for x in aliases do
4797 (setq entry (assoc x idlwave-system-routines))
4798 (when entry
4799 (while (string-match " +or +" (setq syntax (nth 4 entry)))
4800 (setf (nth 4 entry) (replace-match ", " t t syntax)))))))
4801
4802 (defun idlwave-convert-xml-clean-routine-aliases (aliases)
4803 ;; Duplicate and trim original routine aliases from rinfo list
4804 ;; This if for, e.g. OPENR/OPENW/OPENU
4805 (let (alias remove-list new parts all-parts)
4806 (loop for x in aliases do
4807 (when (setq parts (split-string (cdr x) "/"))
4808 (setq new (assoc (cdr x) all-parts))
4809 (unless new
4810 (setq new (cons (cdr x) parts))
4811 (push new all-parts))
4812 (setcdr new (delete (car x) (cdr new)))))
4813
4814 ;; Add any missing aliases (separate by slashes)
4815 (loop for x in all-parts do
4816 (if (cdr x)
4817 (push (cons (nth 1 x) (car x)) aliases)))
4818
4819 (loop for x in aliases do
4820 (when (setq alias (assoc (cdr x) idlwave-system-routines))
4821 (unless (memq alias remove-list) (push alias remove-list))
4822 (setq alias (copy-sequence alias))
4823 (setcar alias (car x))
4824 (push alias idlwave-system-routines)))
4825 (loop for x in remove-list do
4826 (delq x idlwave-system-routines))))
4827
4828 (defun idlwave-convert-xml-clean-sysvar-aliases (aliases)
4829 ;; Duplicate and trim original routine aliases from rinfo list
4830 ;; This if for, e.g. !X, !Y, !Z.
4831 (let (alias remove-list new parts all-parts)
4832 (loop for x in aliases do
4833 (when (setq alias (assoc (cdr x) idlwave-system-variables-alist))
4834 (unless (memq alias remove-list) (push alias remove-list))
4835 (setq alias (copy-sequence alias))
4836 (setcar alias (car x))
4837 (push alias idlwave-system-variables-alist)))
4838 (loop for x in remove-list do
4839 (delq x idlwave-system-variables-alist))))
4840
4841
4842 (defun idlwave-xml-create-sysvar-alist (xml-entry)
4843 ;; Create a sysvar list entry from the xml parsed list.
4844 (let* ((nameblock (nth 1 xml-entry))
4845 (name (cdr (assq 'name nameblock)))
4846 (sysvar (substring name (progn (string-match "^ *!" name)
4847 (match-end 0))))
4848 (link (cdr (assq 'link nameblock)))
4849 (params (cddr xml-entry))
4850 (case-fold-search t)
4851 pelem ptype props fields tags)
4852 (while params
4853 (setq pelem (car params))
4854 (when (listp pelem)
4855 (setq ptype (car pelem)
4856 props (car (cdr pelem)))
4857 (cond
4858 ((eq ptype 'FIELD)
4859 (push (cons (cdr (assq 'name props))
4860 (cdr
4861 (idlwave-split-link-target (cdr (assq 'link props)))))
4862 tags))))
4863 (setq params (cdr params)))
4864 (delq nil
4865 (list sysvar (if tags (cons 'tags tags)) (list 'link link)))))
4866
4867
4868 (defvar idlwave-xml-routine-info-file nil)
4869
4870 (defun idlwave-save-routine-info ()
4871 (if idlwave-xml-routine-info-file
4872 (with-temp-file idlwave-xml-system-rinfo-converted-file
4873 (insert
4874 (concat ";; *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
4875 ;; IDLWAVE Routine Information File (IDLWAVE version " idlwave-mode-version ")
4876 ;; Automatically generated from source file:
4877 ;; " idlwave-xml-routine-info-file "
4878 ;; on " (current-time-string) "
4879 ;; Do not edit."))
4880 (insert (format "\n(setq idlwave-xml-routine-info-file \n \"%s\")"
4881 idlwave-xml-routine-info-file))
4882 (insert "\n(setq idlwave-system-routines\n '")
4883 (prin1 idlwave-system-routines (current-buffer))
4884 (insert ")")
4885 (insert "\n(setq idlwave-system-variables-alist\n '")
4886 (prin1 idlwave-system-variables-alist (current-buffer))
4887 (insert ")")
4888 (insert "\n(setq idlwave-system-class-info\n '")
4889 (prin1 idlwave-system-class-info (current-buffer))
4890 (insert ")")
4891 (insert "\n(setq idlwave-executive-commands-alist\n '")
4892 (prin1 idlwave-executive-commands-alist (current-buffer))
4893 (insert ")")
4894 (insert "\n(setq idlwave-help-special-topic-words\n '")
4895 (prin1 idlwave-help-special-topic-words (current-buffer))
4896 (insert ")"))))
4897
4898 (defun idlwave-convert-xml-system-routine-info ()
4899 "Convert XML supplied IDL routine info into internal form.
4900 Cache to disk for quick recovery."
4901 (interactive)
4902 (let* ((dir (file-name-as-directory
4903 (expand-file-name "help/online_help" (idlwave-sys-dir))))
4904 (catalog-file (expand-file-name "idl_catalog.xml" dir))
4905 (elem-cnt 0)
4906 props rinfo msg-cnt elem type nelem class-result alias
4907 routines routine-aliases statement-aliases sysvar-aliases
4908 version-string)
4909 (if (not (file-exists-p catalog-file))
4910 (error "No such XML routine info file: %s" catalog-file)
4911 (if (not (file-readable-p catalog-file))
4912 (error "Cannot read XML routine info file: %s" catalog-file)))
4913 (require 'xml)
4914 (message "Reading XML routine info...")
4915 (setq rinfo (xml-parse-file catalog-file))
4916 (message "Reading XML routine info...done")
4917 (setq rinfo (assq 'CATALOG rinfo))
4918 (unless rinfo (error "Failed to parse XML routine info"))
4919 ;;(setq rinfo (car rinfo)) ; Skip the catalog stuff.
4920
4921 (setq version-string (cdr (assq 'version (nth 1 rinfo)))
4922 rinfo (cddr rinfo))
4923
4924 (setq nelem (length rinfo)
4925 msg-cnt (/ nelem 20))
4926
4927 (setq idlwave-xml-routine-info-file nil)
4928 (message "Converting XML routine info...")
4929 (setq idlwave-system-routines nil
4930 idlwave-system-variables-alist nil
4931 idlwave-system-class-info nil
4932 idlwave-executive-commands-alist nil
4933 idlwave-help-special-topic-words nil)
4934
4935 (while rinfo
4936 (setq elem (car rinfo)
4937 rinfo (cdr rinfo))
4938 (incf elem-cnt)
4939 (when (listp elem)
4940 (setq type (car elem)
4941 props (car (cdr elem)))
4942 (if (= (mod elem-cnt msg-cnt) 0)
4943 (message "Converting XML routine info...%2d%%"
4944 (/ (* elem-cnt 100) nelem)))
4945 (cond
4946 ((eq type 'ROUTINE)
4947 (if (setq alias (assq 'alias_to props))
4948 (push (cons (cdr (assq 'name props)) (cdr alias))
4949 routine-aliases)
4950 (setq routines (idlwave-xml-create-rinfo-list elem))
4951 (if (listp (cdr routines))
4952 (setq idlwave-system-routines
4953 (nconc idlwave-system-routines routines))
4954 ;; a cons cell is an executive commands
4955 (push routines idlwave-executive-commands-alist))))
4956
4957 ((eq type 'CLASS)
4958 (setq class-result (idlwave-xml-create-class-method-lists elem))
4959 (push (car class-result) idlwave-system-class-info)
4960 (setq idlwave-system-routines
4961 (nconc idlwave-system-routines (cdr class-result))))
4962
4963 ((eq type 'STATEMENT)
4964 (push (cons (cdr (assq 'name props))
4965 (cdr (assq 'link props)))
4966 idlwave-help-special-topic-words)
4967 ;; Save the links to those which are statement aliases (not routines)
4968 (if (setq alias (assq 'alias_to props))
4969 (unless (member (cdr alias) statement-aliases)
4970 (push (cdr alias) statement-aliases))))
4971
4972 ((eq type 'SYSVAR)
4973 (if (setq alias (cdr (assq 'alias_to props)))
4974 (push (cons (substring (cdr (assq 'name props)) 1)
4975 (substring alias 1))
4976 sysvar-aliases)
4977 (push (idlwave-xml-create-sysvar-alist elem)
4978 idlwave-system-variables-alist)))
4979 (t))))
4980 (idlwave-convert-xml-clean-routine-aliases routine-aliases)
4981 (idlwave-convert-xml-clean-statement-aliases statement-aliases)
4982 (idlwave-convert-xml-clean-sysvar-aliases sysvar-aliases)
4983
4984 (setq idlwave-xml-routine-info-file catalog-file)
4985 (idlwave-save-routine-info)
4986 (message "Converting XML routine info...done")))
4987
4988
4989 ;; ("ROUTINE" type class
4990 ;; (system) | (lib pro_file dir "LIBNAME") | (user pro_file dir "USERLIB") |
4991 ;; (buffer pro_file dir) | (compiled pro_file dir)
4992 ;; "calling_string" ("HELPFILE" (("KWD1" . link1) ...))
4993 ;; ("HELPFILE2" (("KWD2" . link) ...)) ...)
4994
4995
4996 (defun idlwave-load-rinfo-next-step ()
4997 (let ((inhibit-quit t)
4998 (arr idlwave-load-rinfo-steps-done))
4999 (if (catch 'exit
5000 (when (not (aref arr 0))
5001 (message "Loading system routine info in idle time...")
5002 (idlwave-load-system-routine-info)
5003 ;;(load "idlw-rinfo" 'noerror 'nomessage)
5004 (message "Loading system routine info in idle time...done")
5005 (aset arr 0 t)
5006 (throw 'exit t))
5007
5008 (when (not (aref arr 1))
5009 (message "Normalizing idlwave-system-routines in idle time...")
5010 (idlwave-reset-sintern t)
5011 (put 'idlwave-reset-sintern 'done-by-idle t)
5012 (setq idlwave-system-routines
5013 (idlwave-sintern-rinfo-list idlwave-system-routines 'sys))
5014 (message "Normalizing idlwave-system-routines in idle time...done")
5015 (aset arr 1 t)
5016 (throw 'exit t))
5017
5018 (when (not (aref arr 2))
5019 (when (and (stringp idlwave-user-catalog-file)
5020 (file-regular-p idlwave-user-catalog-file))
5021 (message "Loading user catalog in idle time...")
5022 (condition-case nil
5023 (load-file idlwave-user-catalog-file)
5024 (error (throw 'exit nil)))
5025 ;; Check for the old style catalog and warn
5026 (if (and
5027 (boundp 'idlwave-library-routines)
5028 idlwave-library-routines)
5029 (progn
5030 (setq idlwave-library-routines nil)
5031 (ding)
5032 (message "Outdated user catalog: %s... recreate"
5033 idlwave-user-catalog-file))
5034 (message "Loading user catalog in idle time...done")))
5035 (aset arr 2 t)
5036 (throw 'exit t))
5037
5038 (when (not (aref arr 3))
5039 (when idlwave-user-catalog-routines
5040 (message "Normalizing user catalog routines in idle time...")
5041 (setq idlwave-user-catalog-routines
5042 (idlwave-sintern-rinfo-list
5043 idlwave-user-catalog-routines 'sys))
5044 (message
5045 "Normalizing user catalog routines in idle time...done"))
5046 (aset arr 3 t)
5047 (throw 'exit t))
5048
5049 (when (not (aref arr 4))
5050 (idlwave-scan-library-catalogs
5051 "Loading and normalizing library catalogs in idle time...")
5052 (aset arr 4 t)
5053 (throw 'exit t))
5054 (when (not (aref arr 5))
5055 (message "Finishing initialization in idle time...")
5056 (idlwave-routines)
5057 (message "Finishing initialization in idle time...done")
5058 (aset arr 5 t)
5059 (throw 'exit nil)))
5060 ;; restart the timer
5061 (if (sit-for 1)
5062 (idlwave-load-rinfo-next-step)
5063 (setq idlwave-load-rinfo-idle-timer
5064 (run-with-idle-timer
5065 idlwave-init-rinfo-when-idle-after
5066 nil 'idlwave-load-rinfo-next-step))))))
5067
5068 (defun idlwave-load-all-rinfo (&optional force)
5069 ;; Load and case-treat the system, user catalog, and library routine
5070 ;; info files.
5071
5072 ;; System
5073 (when (or force (not (aref idlwave-load-rinfo-steps-done 0)))
5074 ;;(load "idlw-rinfo" 'noerror 'nomessage))
5075 (idlwave-load-system-routine-info))
5076 (when (or force (not (aref idlwave-load-rinfo-steps-done 1)))
5077 (message "Normalizing idlwave-system-routines...")
5078 (setq idlwave-system-routines
5079 (idlwave-sintern-rinfo-list idlwave-system-routines 'sys))
5080 (message "Normalizing idlwave-system-routines...done"))
5081 (when idlwave-system-routines
5082 (setq idlwave-routines (copy-sequence idlwave-system-routines))
5083 (setq idlwave-last-system-routine-info-cons-cell
5084 (nthcdr (1- (length idlwave-routines)) idlwave-routines)))
5085
5086 ;; User catalog
5087 (when (and (stringp idlwave-user-catalog-file)
5088 (file-regular-p idlwave-user-catalog-file))
5089 (condition-case nil
5090 (when (or force (not (aref idlwave-load-rinfo-steps-done 2)))
5091 (load-file idlwave-user-catalog-file))
5092 (error nil))
5093 (when (and
5094 (boundp 'idlwave-library-routines)
5095 idlwave-library-routines)
5096 (setq idlwave-library-routines nil)
5097 (error "Outdated user catalog: %s... recreate"
5098 idlwave-user-catalog-file))
5099 (setq idlwave-true-path-alist nil)
5100 (when (or force (not (aref idlwave-load-rinfo-steps-done 3)))
5101 (message "Normalizing user catalog routines...")
5102 (setq idlwave-user-catalog-routines
5103 (idlwave-sintern-rinfo-list
5104 idlwave-user-catalog-routines 'sys))
5105 (message "Normalizing user catalog routines...done")))
5106
5107 ;; Library catalog
5108 (when (or force (not (aref idlwave-load-rinfo-steps-done 4)))
5109 (idlwave-scan-library-catalogs
5110 "Loading and normalizing library catalogs..."))
5111 (run-hooks 'idlwave-after-load-rinfo-hook))
5112
5113
5114 (defun idlwave-update-buffer-routine-info ()
5115 (let (res)
5116 (cond
5117 ((eq idlwave-scan-all-buffers-for-routine-info t)
5118 ;; Scan all buffers, current buffer last
5119 (message "Scanning all buffers...")
5120 (setq res (idlwave-get-routine-info-from-buffers
5121 (reverse (buffer-list)))))
5122 ((null idlwave-scan-all-buffers-for-routine-info)
5123 ;; Don't scan any buffers
5124 (setq res nil))
5125 (t
5126 ;; Just scan this buffer
5127 (if (eq major-mode 'idlwave-mode)
5128 (progn
5129 (message "Scanning current buffer...")
5130 (setq res (idlwave-get-routine-info-from-buffers
5131 (list (current-buffer))))))))
5132 ;; Put the result into the correct variable
5133 (setq idlwave-buffer-routines
5134 (idlwave-sintern-rinfo-list res 'set))))
5135
5136 (defun idlwave-concatenate-rinfo-lists (&optional quiet run-hook)
5137 "Put the different sources for routine information together."
5138 ;; The sequence here is important because earlier definitions shadow
5139 ;; later ones. We assume that if things in the buffers are newer
5140 ;; then in the shell of the system, they are meant to be different.
5141 (setcdr idlwave-last-system-routine-info-cons-cell
5142 (append idlwave-buffer-routines
5143 idlwave-compiled-routines
5144 idlwave-library-catalog-routines
5145 idlwave-user-catalog-routines))
5146 (setq idlwave-class-alist nil)
5147
5148 ;; Give a message with information about the number of routines we have.
5149 (unless quiet
5150 (message
5151 "Routines Found: buffer(%d) compiled(%d) library(%d) user(%d) system(%d)"
5152 (length idlwave-buffer-routines)
5153 (length idlwave-compiled-routines)
5154 (length idlwave-library-catalog-routines)
5155 (length idlwave-user-catalog-routines)
5156 (length idlwave-system-routines)))
5157 (if run-hook
5158 (run-hooks 'idlwave-update-rinfo-hook)))
5159
5160 (defun idlwave-class-alist ()
5161 "Return the class alist - make it if necessary."
5162 (or idlwave-class-alist
5163 (let (class)
5164 (loop for x in idlwave-routines do
5165 (when (and (setq class (nth 2 x))
5166 (not (assq class idlwave-class-alist)))
5167 (push (list class) idlwave-class-alist)))
5168 idlwave-class-alist)))
5169
5170 ;; Three functions for the hooks
5171 (defun idlwave-save-buffer-update ()
5172 (idlwave-update-current-buffer-info 'save-buffer))
5173 (defun idlwave-kill-buffer-update ()
5174 (idlwave-update-current-buffer-info 'kill-buffer))
5175 (defun idlwave-new-buffer-update ()
5176 (idlwave-update-current-buffer-info 'find-file))
5177
5178 (defun idlwave-update-current-buffer-info (why)
5179 "Update idlwave-routines for current buffer. Can run from after-save-hook."
5180 (when (and (eq major-mode 'idlwave-mode)
5181 (or (eq t idlwave-auto-routine-info-updates)
5182 (memq why idlwave-auto-routine-info-updates))
5183 idlwave-scan-all-buffers-for-routine-info
5184 idlwave-routines)
5185 (condition-case nil
5186 (let (routines)
5187 (idlwave-replace-buffer-routine-info
5188 (buffer-file-name)
5189 (if (eq why 'kill-buffer)
5190 nil
5191 (setq routines
5192 (idlwave-sintern-rinfo-list
5193 (idlwave-get-routine-info-from-buffers
5194 (list (current-buffer))) 'set))))
5195 (idlwave-concatenate-rinfo-lists 'quiet)
5196 routines)
5197 (error nil))))
5198
5199 (defun idlwave-replace-buffer-routine-info (file new)
5200 "Cut the part from FILE out of `idlwave-buffer-routines' and add NEW."
5201 (let ((list idlwave-buffer-routines)
5202 found)
5203 (while list
5204 ;; The following test uses eq to make sure it works correctly
5205 ;; when two buffers visit the same file. Then the file names
5206 ;; will be equal, but not eq.
5207 (if (eq (idlwave-routine-source-file (nth 3 (car list))) file)
5208 (progn
5209 (setcar list nil)
5210 (setq found t))
5211 (if found
5212 ;; End of that section reached. Jump.
5213 (setq list nil)))
5214 (setq list (cdr list)))
5215 (setq idlwave-buffer-routines
5216 (append new (delq nil idlwave-buffer-routines)))))
5217
5218 ;;----- Scanning buffers -------------------
5219
5220 (defun idlwave-get-routine-info-from-buffers (buffers)
5221 "Call `idlwave-get-buffer-routine-info' on idlwave-mode buffers in BUFFERS."
5222 (let (buf routine-lists res)
5223 (save-excursion
5224 (while (setq buf (pop buffers))
5225 (set-buffer buf)
5226 (if (and (eq major-mode 'idlwave-mode)
5227 buffer-file-name)
5228 ;; yes, this buffer has the right mode.
5229 (progn (setq res (condition-case nil
5230 (idlwave-get-buffer-routine-info)
5231 (error nil)))
5232 (push res routine-lists)))))
5233 ;; Concatenate the individual lists and return the result
5234 (apply 'nconc routine-lists)))
5235
5236 (defun idlwave-get-buffer-routine-info ()
5237 "Scan the current buffer for routine info. Return (PRO-LIST FUNC-LIST)."
5238 (let* ((case-fold-search t)
5239 routine-list string entry)
5240 (save-excursion
5241 (save-restriction
5242 (widen)
5243 (goto-char (point-min))
5244 (while (re-search-forward
5245 "^[ \t]*\\(pro\\|function\\)[ \t]" nil t)
5246 (setq string (buffer-substring-no-properties
5247 (match-beginning 0)
5248 (progn
5249 (idlwave-end-of-statement)
5250 (point))))
5251 (setq entry (idlwave-parse-definition string))
5252 (push entry routine-list))))
5253 routine-list))
5254
5255 (defvar idlwave-scanning-lib-dir)
5256 (defun idlwave-parse-definition (string)
5257 "Parse a module definition."
5258 (let ((case-fold-search t)
5259 start name args type keywords class)
5260 ;; Remove comments
5261 (while (string-match ";.*" string)
5262 (setq string (replace-match "" t t string)))
5263 ;; Remove the continuation line stuff
5264 (while (string-match "\\([^a-zA-Z0-9$_]\\)\\$[ \t]*\n" string)
5265 (setq string (replace-match "\\1 " t nil string)))
5266 (while (string-match "\n" string)
5267 (setq string (replace-match " " t nil string)))
5268 ;; Match the name and type.
5269 (when (string-match
5270 "\\<\\(pro\\|function\\)\\>\\s-+\\(\\([a-zA-Z0-9$_]+\\)::\\)?\\([a-zA-Z0-9$_]+\\)" string)
5271 (setq start (match-end 0))
5272 (setq type (downcase (match-string 1 string)))
5273 (if (match-beginning 3)
5274 (setq class (match-string 3 string)))
5275 (setq name (match-string 4 string)))
5276 ;; Match normal args and keyword args
5277 (while (string-match
5278 ",\\s-*\\([a-zA-Z][a-zA-Z0-9$_]*\\|\\(_ref\\)?_extra\\)\\s-*\\(=\\)?"
5279 string start)
5280 (setq start (match-end 0))
5281 (if (match-beginning 3)
5282 (push (match-string 1 string) keywords)
5283 (push (match-string 1 string) args)))
5284 ;; Normalize and sort.
5285 (setq args (nreverse args))
5286 (setq keywords (sort keywords (lambda (a b)
5287 (string< (downcase a) (downcase b)))))
5288 ;; Make and return the entry
5289 ;; We don't know which argument are optional, so this information
5290 ;; will not be contained in the calling sequence.
5291 (list name
5292 (if (equal type "pro") 'pro 'fun)
5293 class
5294 (cond ((not (boundp 'idlwave-scanning-lib))
5295 (list 'buffer (buffer-file-name)))
5296 ; ((string= (downcase
5297 ; (file-name-sans-extension
5298 ; (file-name-nondirectory (buffer-file-name))))
5299 ; (downcase name))
5300 ; (list 'lib))
5301 ; (t (cons 'lib (file-name-nondirectory (buffer-file-name))))
5302 (t (list 'user (file-name-nondirectory (buffer-file-name))
5303 idlwave-scanning-lib-dir "UserLib")))
5304 (concat
5305 (if (string= type "function") "Result = " "")
5306 (if class "Obj ->[%s::]" "")
5307 "%s"
5308 (if args
5309 (concat
5310 (if (string= type "function") "(" ", ")
5311 (mapconcat 'identity args ", ")
5312 (if (string= type "function") ")" ""))))
5313 (if keywords
5314 (cons nil (mapcar 'list keywords)) ;No help file
5315 nil))))
5316
5317
5318 ;;----- Scanning the user catalog -------------------
5319
5320 (defun idlwave-sys-dir ()
5321 "Return the syslib directory, or a dummy that never matches."
5322 (cond
5323 ((and idlwave-system-directory
5324 (not (string= idlwave-system-directory "")))
5325 idlwave-system-directory)
5326 ((getenv "IDL_DIR"))
5327 (t "@@@@@@@@")))
5328
5329
5330 (defun idlwave-create-user-catalog-file (&optional arg)
5331 "Scan all files on selected dirs of IDL search path for routine information.
5332
5333 A widget checklist will allow you to choose the directories. Write
5334 the result as a file `idlwave-user-catalog-file'. When this file
5335 exists, will be automatically loaded to give routine information about
5336 library routines. With ARG, just rescan the same directories as last
5337 time - so no widget will pop up."
5338 (interactive "P")
5339 ;; Make sure the file is loaded if it exists.
5340 (if (and (stringp idlwave-user-catalog-file)
5341 (file-regular-p idlwave-user-catalog-file))
5342 (condition-case nil
5343 (load-file idlwave-user-catalog-file)
5344 (error nil)))
5345 ;; Make sure the file name makes sense
5346 (unless (and (stringp idlwave-user-catalog-file)
5347 (> (length idlwave-user-catalog-file) 0)
5348 (file-accessible-directory-p
5349 (file-name-directory idlwave-user-catalog-file))
5350 (not (string= "" (file-name-nondirectory
5351 idlwave-user-catalog-file))))
5352 (error "`idlwave-user-catalog-file' does not point to a file in an accessible directory"))
5353
5354 (cond
5355 ;; Rescan the known directories
5356 ((and arg idlwave-path-alist
5357 (consp (car idlwave-path-alist)))
5358 (idlwave-scan-user-lib-files idlwave-path-alist))
5359
5360 ;; Expand the directories from library-path and run the widget
5361 (idlwave-library-path
5362 (idlwave-display-user-catalog-widget
5363 (if idlwave-true-path-alist
5364 ;; Propagate any flags on the existing path-alist
5365 (mapcar (lambda (x)
5366 (let ((path-entry (assoc (file-truename x)
5367 idlwave-true-path-alist)))
5368 (if path-entry
5369 (cons x (cdr path-entry))
5370 (list x))))
5371 (idlwave-expand-path idlwave-library-path))
5372 (mapcar 'list (idlwave-expand-path idlwave-library-path)))))
5373
5374 ;; Ask the shell for the path and then run the widget
5375 (t
5376 (message "Asking the shell for IDL path...")
5377 (require 'idlw-shell)
5378 (idlwave-shell-send-command idlwave-shell-path-query
5379 '(idlwave-user-catalog-command-hook nil)
5380 'hide))))
5381
5382
5383 ;; Parse shell path information and select among it.
5384 (defun idlwave-user-catalog-command-hook (&optional arg)
5385 ;; Command hook used by `idlwave-create-user-catalog-file'.
5386 (if arg
5387 ;; Scan immediately
5388 (idlwave-scan-user-lib-files idlwave-path-alist)
5389 ;; Set the path and display the widget
5390 (idlwave-shell-get-path-info 'no-write) ; set to something path-alist
5391 (idlwave-scan-library-catalogs "Locating library catalogs..." 'no-load)
5392 (idlwave-display-user-catalog-widget idlwave-path-alist)))
5393
5394 (defconst idlwave-user-catalog-widget-help-string
5395 "This is the front-end to the creation of the IDLWAVE user catalog.
5396 Please select the directories on IDL's search path from which you
5397 would like to extract routine information, to be stored in the file:
5398
5399 %s
5400
5401 If this is not the correct file, first set variable
5402 `idlwave-user-catalog-file', and call this command again.
5403
5404 N.B. Many libraries include pre-scanned catalog files
5405 \(\".idlwave_catalog\"). These are marked with \"[LIB]\", and need
5406 not be scanned. You can scan your own libraries off-line using the
5407 perl script `idlwave_catalog'.
5408
5409 After selecting the directories, choose [Scan & Save] to scan the library
5410 directories and save the routine info.
5411 \n")
5412
5413 (defvar idlwave-widget)
5414 (defvar widget-keymap)
5415 (defun idlwave-display-user-catalog-widget (dirs-list)
5416 "Create the widget to select IDL search path directories for scanning."
5417 (interactive)
5418 (require 'widget)
5419 (require 'wid-edit)
5420 (unless dirs-list
5421 (error "Don't know IDL's search path"))
5422
5423 (kill-buffer (get-buffer-create "*IDLWAVE Widget*"))
5424 (switch-to-buffer (get-buffer-create "*IDLWAVE Widget*"))
5425 (kill-all-local-variables)
5426 (make-local-variable 'idlwave-widget)
5427 (widget-insert (format idlwave-user-catalog-widget-help-string
5428 idlwave-user-catalog-file))
5429
5430 (widget-create 'push-button
5431 :notify 'idlwave-widget-scan-user-lib-files
5432 "Scan & Save")
5433 (widget-insert " ")
5434 (widget-create 'push-button
5435 :notify 'idlwave-delete-user-catalog-file
5436 "Delete File")
5437 (widget-insert " ")
5438 (widget-create 'push-button
5439 :notify
5440 '(lambda (&rest ignore)
5441 (let ((path-list (widget-get idlwave-widget :path-dirs)))
5442 (mapcar (lambda (x)
5443 (unless (memq 'lib (cdr x))
5444 (idlwave-path-alist-add-flag x 'user)))
5445 path-list)
5446 (idlwave-display-user-catalog-widget path-list)))
5447 "Select All Non-Lib")
5448 (widget-insert " ")
5449 (widget-create 'push-button
5450 :notify
5451 '(lambda (&rest ignore)
5452 (let ((path-list (widget-get idlwave-widget :path-dirs)))
5453 (mapcar (lambda (x)
5454 (idlwave-path-alist-remove-flag x 'user))
5455 path-list)
5456 (idlwave-display-user-catalog-widget path-list)))
5457 "Deselect All")
5458 (widget-insert " ")
5459 (widget-create 'push-button
5460 :notify (lambda (&rest ignore)
5461 (kill-buffer (current-buffer)))
5462 "Quit")
5463 (widget-insert "\n\n")
5464
5465 (widget-insert "Select Directories: \n")
5466
5467 (setq idlwave-widget
5468 (apply 'widget-create
5469 'checklist
5470 :value (delq nil (mapcar (lambda (x)
5471 (if (memq 'user (cdr x))
5472 (car x)))
5473 dirs-list))
5474 :greedy t
5475 :tag "List of directories"
5476 (mapcar (lambda (x)
5477 (list 'item
5478 (if (memq 'lib (cdr x))
5479 (concat "[LIB] " (car x) )
5480 (car x)))) dirs-list)))
5481 (widget-put idlwave-widget :path-dirs dirs-list)
5482 (widget-insert "\n")
5483 (use-local-map widget-keymap)
5484 (widget-setup)
5485 (goto-char (point-min))
5486 (delete-other-windows))
5487
5488 (defun idlwave-delete-user-catalog-file (&rest ignore)
5489 (if (yes-or-no-p
5490 (format "Delete file %s " idlwave-user-catalog-file))
5491 (progn
5492 (delete-file idlwave-user-catalog-file)
5493 (message "%s has been deleted" idlwave-user-catalog-file))))
5494
5495 (defun idlwave-widget-scan-user-lib-files (&rest ignore)
5496 ;; Call `idlwave-scan-user-lib-files' with data taken from the widget.
5497 (let* ((widget idlwave-widget)
5498 (selected-dirs (widget-value widget))
5499 (path-alist (widget-get widget :path-dirs))
5500 (this-path-alist path-alist)
5501 dir-entry)
5502 (while (setq dir-entry (pop this-path-alist))
5503 (if (member
5504 (if (memq 'lib (cdr dir-entry))
5505 (concat "[LIB] " (car dir-entry))
5506 (car dir-entry))
5507 selected-dirs)
5508 (idlwave-path-alist-add-flag dir-entry 'user)
5509 (idlwave-path-alist-remove-flag dir-entry 'user)))
5510 (idlwave-scan-user-lib-files path-alist)))
5511
5512 (defvar font-lock-mode)
5513 (defun idlwave-scan-user-lib-files (path-alist)
5514 ;; Scan the PRO files in PATH-ALIST and store the info in the user catalog
5515 (let* ((idlwave-scanning-lib t)
5516 (idlwave-scanning-lib-dir "")
5517 (idlwave-completion-case nil)
5518 dirs-alist dir files file)
5519 (setq idlwave-user-catalog-routines nil
5520 idlwave-path-alist path-alist ; for library-path instead
5521 idlwave-true-path-alist nil)
5522 (if idlwave-auto-write-paths (idlwave-write-paths))
5523 (save-excursion
5524 (set-buffer (get-buffer-create "*idlwave-scan.pro*"))
5525 (idlwave-mode)
5526 (setq dirs-alist (reverse path-alist))
5527 (while (setq dir (pop dirs-alist))
5528 (when (memq 'user (cdr dir)) ; Has it marked for scan?
5529 (setq dir (car dir))
5530 (setq idlwave-scanning-lib-dir dir)
5531 (when (file-directory-p dir)
5532 (setq files (directory-files dir 'full "\\.[pP][rR][oO]\\'"))
5533 (while (setq file (pop files))
5534 (when (file-regular-p file)
5535 (if (not (file-readable-p file))
5536 (message "Skipping %s (no read permission)" file)
5537 (message "Scanning %s..." file)
5538 (erase-buffer)
5539 (insert-file-contents file 'visit)
5540 (setq idlwave-user-catalog-routines
5541 (append (idlwave-get-routine-info-from-buffers
5542 (list (current-buffer)))
5543 idlwave-user-catalog-routines)))))))))
5544 (message "Creating user catalog file...")
5545 (kill-buffer "*idlwave-scan.pro*")
5546 (kill-buffer (get-buffer-create "*IDLWAVE Widget*"))
5547 (let ((font-lock-maximum-size 0)
5548 (auto-mode-alist nil))
5549 (find-file idlwave-user-catalog-file))
5550 (if (and (boundp 'font-lock-mode)
5551 font-lock-mode)
5552 (font-lock-mode 0))
5553 (erase-buffer)
5554 (insert ";; IDLWAVE user catalog file\n")
5555 (insert (format ";; Created %s\n\n" (current-time-string)))
5556
5557 ;; Define the routine info list
5558 (insert "\n(setq idlwave-user-catalog-routines\n '(")
5559 (let ((standard-output (current-buffer)))
5560 (mapcar (lambda (x)
5561 (insert "\n ")
5562 (prin1 x)
5563 (goto-char (point-max)))
5564 idlwave-user-catalog-routines))
5565 (insert (format "))\n\n;;; %s ends here\n"
5566 (file-name-nondirectory idlwave-user-catalog-file)))
5567 (goto-char (point-min))
5568 ;; Save the buffer
5569 (save-buffer 0)
5570 (kill-buffer (current-buffer)))
5571 (message "Creating user catalog file...done")
5572 (message "Info for %d routines saved in %s"
5573 (length idlwave-user-catalog-routines)
5574 idlwave-user-catalog-file)
5575 (sit-for 2)
5576 (idlwave-update-routine-info t))
5577
5578 (defun idlwave-read-paths ()
5579 (if (and (stringp idlwave-path-file)
5580 (file-regular-p idlwave-path-file))
5581 (condition-case nil
5582 (load idlwave-path-file t t t)
5583 (error nil))))
5584
5585 (defun idlwave-write-paths ()
5586 (interactive)
5587 (when (and idlwave-path-alist idlwave-system-directory)
5588 (let ((font-lock-maximum-size 0)
5589 (auto-mode-alist nil))
5590 (find-file idlwave-path-file))
5591 (if (and (boundp 'font-lock-mode)
5592 font-lock-mode)
5593 (font-lock-mode 0))
5594 (erase-buffer)
5595 (insert ";; IDLWAVE paths\n")
5596 (insert (format ";; Created %s\n\n" (current-time-string)))
5597 ;; Define the variable which knows the value of "!DIR"
5598 (insert (format "\n(setq idlwave-system-directory \"%s\")\n"
5599 idlwave-system-directory))
5600
5601 ;; Define the variable which contains a list of all scanned directories
5602 (insert "\n(setq idlwave-path-alist\n '(")
5603 (let ((standard-output (current-buffer)))
5604 (mapcar (lambda (x)
5605 (insert "\n ")
5606 (prin1 x)
5607 (goto-char (point-max)))
5608 idlwave-path-alist))
5609 (insert "))\n")
5610 (save-buffer 0)
5611 (kill-buffer (current-buffer))))
5612
5613
5614 (defun idlwave-expand-path (path &optional default-dir)
5615 ;; Expand parts of path starting with '+' recursively into directory list.
5616 ;; Relative recursive path elements are expanded relative to DEFAULT-DIR.
5617 (message "Expanding path...")
5618 (let (path1 dir recursive)
5619 (while (setq dir (pop path))
5620 (if (setq recursive (string= (substring dir 0 1) "+"))
5621 (setq dir (substring dir 1)))
5622 (if (and recursive
5623 (not (file-name-absolute-p dir)))
5624 (setq dir (expand-file-name dir default-dir)))
5625 (if recursive
5626 ;; Expand recursively
5627 (setq path1 (append (idlwave-recursive-directory-list dir) path1))
5628 ;; Keep unchanged
5629 (push dir path1)))
5630 (message "Expanding path...done")
5631 (nreverse path1)))
5632
5633 (defun idlwave-recursive-directory-list (dir)
5634 ;; Return a list of all directories below DIR, including DIR itself
5635 (let ((path (list dir)) path1 file files)
5636 (while (setq dir (pop path))
5637 (when (file-directory-p dir)
5638 (setq files (nreverse (directory-files dir t "[^.]")))
5639 (while (setq file (pop files))
5640 (if (file-directory-p file)
5641 (push (file-name-as-directory file) path)))
5642 (push dir path1)))
5643 path1))
5644
5645
5646 ;;----- Scanning the library catalogs ------------------
5647
5648
5649
5650
5651 (defun idlwave-scan-library-catalogs (&optional message-base no-load)
5652 "Scan for library catalog files (.idlwave_catalog) and ingest.
5653
5654 All directories on `idlwave-path-alist' (or `idlwave-library-path'
5655 instead, if present) are searched. Print MESSAGE-BASE along with the
5656 libraries being loaded, if passed, and skip loading/normalizing if
5657 NO-LOAD is non-nil. The variable `idlwave-use-library-catalogs' can
5658 be set to nil to disable library catalog scanning."
5659 (when idlwave-use-library-catalogs
5660 (let ((dirs
5661 (if idlwave-library-path
5662 (idlwave-expand-path idlwave-library-path)
5663 (mapcar 'car idlwave-path-alist)))
5664 (old-libname "")
5665 dir-entry dir flags catalog all-routines)
5666 (if message-base (message message-base))
5667 (while (setq dir (pop dirs))
5668 (catch 'continue
5669 (when (file-readable-p
5670 (setq catalog (expand-file-name ".idlwave_catalog" dir)))
5671 (unless no-load
5672 (setq idlwave-library-catalog-routines nil)
5673 ;; Load the catalog file
5674 (condition-case nil
5675 (load catalog t t t)
5676 (error (throw 'continue t)))
5677 (when (and
5678 message-base
5679 (not (string= idlwave-library-catalog-libname
5680 old-libname)))
5681 (message "%s" (concat message-base
5682 idlwave-library-catalog-libname))
5683 (setq old-libname idlwave-library-catalog-libname))
5684 (when idlwave-library-catalog-routines
5685 (setq all-routines
5686 (append
5687 (idlwave-sintern-rinfo-list
5688 idlwave-library-catalog-routines 'sys dir)
5689 all-routines))))
5690
5691 ;; Add a 'lib flag if on path-alist
5692 (when (and idlwave-path-alist
5693 (setq dir-entry (assoc dir idlwave-path-alist)))
5694 (idlwave-path-alist-add-flag dir-entry 'lib)))))
5695 (unless no-load (setq idlwave-library-catalog-routines all-routines))
5696 (if message-base (message (concat message-base "done"))))))
5697
5698 ;;----- Communicating with the Shell -------------------
5699
5700 ;; First, here is the idl program which can be used to query IDL for
5701 ;; defined routines.
5702 (defconst idlwave-routine-info.pro
5703 "
5704 ;; START OF IDLWAVE SUPPORT ROUTINES
5705 pro idlwave_print_safe,item,limit
5706 catch,err
5707 if err ne 0 then begin
5708 print,'Could not print item.'
5709 return
5710 endif
5711 if n_elements(item) gt limit then $
5712 print,item[0:limit-1],'<... truncated at ',strtrim(limit,2),' elements>' $
5713 else print,item
5714 end
5715
5716 pro idlwave_print_info_entry,name,func=func,separator=sep
5717 ;; See if it's an object method
5718 if name eq '' then return
5719 func = keyword_set(func)
5720 methsep = strpos(name,'::')
5721 meth = methsep ne -1
5722
5723 ;; Get routine info
5724 pars = routine_info(name,/parameters,functions=func)
5725 source = routine_info(name,/source,functions=func)
5726 nargs = pars.num_args
5727 nkw = pars.num_kw_args
5728 if nargs gt 0 then args = pars.args
5729 if nkw gt 0 then kwargs = pars.kw_args
5730
5731 ;; Trim the class, and make the name
5732 if meth then begin
5733 class = strmid(name,0,methsep)
5734 name = strmid(name,methsep+2,strlen(name)-1)
5735 if nargs gt 0 then begin
5736 ;; remove the self argument
5737 wh = where(args ne 'SELF',nargs)
5738 if nargs gt 0 then args = args[wh]
5739 endif
5740 endif else begin
5741 ;; No class, just a normal routine.
5742 class = \"\"
5743 endelse
5744
5745 ;; Calling sequence
5746 cs = \"\"
5747 if func then cs = 'Result = '
5748 if meth then cs = cs + 'Obj -> [' + '%s' + '::]'
5749 cs = cs + '%s'
5750 if func then cs = cs + '(' else if nargs gt 0 then cs = cs + ', '
5751 if nargs gt 0 then begin
5752 for j=0,nargs-1 do begin
5753 cs = cs + args[j]
5754 if j lt nargs-1 then cs = cs + ', '
5755 endfor
5756 end
5757 if func then cs = cs + ')'
5758 ;; Keyword arguments
5759 kwstring = ''
5760 if nkw gt 0 then begin
5761 for j=0,nkw-1 do begin
5762 kwstring = kwstring + ' ' + kwargs[j]
5763 endfor
5764 endif
5765
5766 ret=(['IDLWAVE-PRO','IDLWAVE-FUN'])[func]
5767
5768 print,ret + ': ' + name + sep + class + sep + source[0].path $
5769 + sep + cs + sep + kwstring
5770 end
5771
5772 pro idlwave_routine_info,file
5773 on_error,1
5774 sep = '<@>'
5775 print,'>>>BEGIN OF IDLWAVE ROUTINE INFO (\"' + sep + '\" IS THE SEPARATOR)'
5776 all = routine_info()
5777 fileQ=n_elements(file) ne 0
5778 if fileQ then file=strtrim(file,2)
5779 for i=0L,n_elements(all)-1L do begin
5780 if fileQ then begin
5781 if (routine_info(all[i],/SOURCE)).path eq file then $
5782 idlwave_print_info_entry,all[i],separator=sep
5783 endif else idlwave_print_info_entry,all[i],separator=sep
5784 endfor
5785 all = routine_info(/functions)
5786 for i=0L,n_elements(all)-1L do begin
5787 if fileQ then begin
5788 if (routine_info(all[i],/FUNCTIONS,/SOURCE)).path eq file then $
5789 idlwave_print_info_entry,all[i],separator=sep,/FUNC
5790 endif else idlwave_print_info_entry,all[i],separator=sep,/FUNC
5791 endfor
5792 print,'>>>END OF IDLWAVE ROUTINE INFO'
5793 end
5794
5795 pro idlwave_get_sysvars
5796 on_error,1
5797 catch,error_status
5798 if error_status ne 0 then begin
5799 print, 'Cannot get info about system variables'
5800 endif else begin
5801 help,/brief,output=s,/system_variables ; ? unsafe use of OUTPUT=
5802 s = strtrim(strjoin(s,' ',/single),2) ; make one line
5803 v = strsplit(s,' +',/regex,/extract) ; get variables
5804 for i=0L,n_elements(v)-1 do begin
5805 t = [''] ; get tag list
5806 a=execute('if n_tags('+v[i]+') gt 0 then t=tag_names('+v[i]+')')
5807 print, 'IDLWAVE-SYSVAR: '+v[i]+' '+strjoin(t,' ',/single)
5808 endfor
5809 endelse
5810 end
5811
5812 pro idlwave_get_class_tags, class
5813 res = execute('tags=tag_names({'+class+'})')
5814 if res then print,'IDLWAVE-CLASS-TAGS: '+class+' '+strjoin(tags,' ',/single)
5815 end
5816 ;; END OF IDLWAVE SUPPORT ROUTINES
5817 "
5818 "The idl programs to get info from the shell.")
5819
5820 (defvar idlwave-idlwave_routine_info-compiled nil
5821 "Remembers if the routine info procedure is already compiled.")
5822
5823 (defvar idlwave-shell-temp-pro-file)
5824 (defvar idlwave-shell-temp-rinfo-save-file)
5825
5826 (defun idlwave-shell-compile-helper-routines (&optional wait)
5827 (unless (and idlwave-idlwave_routine_info-compiled
5828 (file-readable-p (idlwave-shell-temp-file 'rinfo)))
5829 (save-excursion
5830 (set-buffer (idlwave-find-file-noselect
5831 (idlwave-shell-temp-file 'pro)))
5832 (erase-buffer)
5833 (insert idlwave-routine-info.pro)
5834 (save-buffer 0))
5835 (idlwave-shell-send-command
5836 (concat ".run \"" idlwave-shell-temp-pro-file "\"")
5837 nil 'hide wait)
5838 (idlwave-shell-send-command
5839 (format "save,'idlwave_print_safe','idlwave_routine_info','idlwave_print_info_entry','idlwave_get_class_tags','idlwave_get_sysvars',FILE='%s',/ROUTINES"
5840 (idlwave-shell-temp-file 'rinfo))
5841 nil 'hide)
5842 (setq idlwave-idlwave_routine_info-compiled t))
5843
5844 ;; Restore if necessary. Must use execute to hide lame routine_info
5845 ;; errors on undefinded routine
5846 (idlwave-shell-send-command
5847 (format "if execute(\"_v=routine_info('idlwave_routine_info',/SOURCE)\") eq 0 then restore,'%s' else if _v.path eq '' then restore,'%s'"
5848 idlwave-shell-temp-rinfo-save-file
5849 idlwave-shell-temp-rinfo-save-file)
5850 nil 'hide))
5851
5852
5853 (defun idlwave-shell-update-routine-info (&optional quiet run-hooks wait file)
5854 "Query the shell for routine_info of compiled modules and update the lists."
5855 ;; Save and compile the procedure. The compiled procedure is then
5856 ;; saved into an IDL SAVE file, to allow for fast RESTORE. We may
5857 ;; need to test for and possibly RESTORE the procedure each time we
5858 ;; use it, since the user may have killed or redefined it. In
5859 ;; particular, .RESET_SESSION will kill all user procedures. If
5860 ;; FILE is set, only update routine info for routines in that file.
5861
5862 (idlwave-shell-compile-helper-routines wait)
5863 ; execute the routine_info procedure, and analyze the output
5864 (idlwave-shell-send-command
5865 (format "idlwave_routine_info%s" (if file (concat ",'" file "'") ""))
5866 `(progn
5867 (idlwave-shell-routine-info-filter)
5868 (idlwave-concatenate-rinfo-lists ,quiet ,run-hooks))
5869 'hide wait))
5870
5871 ;; ---------------------------------------------------------------------------
5872 ;;
5873 ;; Completion and displaying routine calling sequences
5874
5875 (defvar idlwave-completion-help-info nil)
5876 (defvar idlwave-completion-help-links nil)
5877 (defvar idlwave-current-obj_new-class nil)
5878 (defvar idlwave-complete-special nil)
5879
5880 (defun idlwave-complete (&optional arg module class)
5881 "Complete a function, procedure or keyword name at point.
5882 This function is smart and figures out what can be completed
5883 at this point.
5884 - At the beginning of a statement it completes procedure names.
5885 - In the middle of a statement it completes function names.
5886 - after a `(' or `,' in the argument list of a function or procedure,
5887 it completes a keyword of the relevant function or procedure.
5888 - In the first arg of `OBJ_NEW', it completes a class name.
5889
5890 When several completions are possible, a list will be displayed in the
5891 *Completions* buffer. If this list is too long to fit into the
5892 window, scrolling can be achieved by repeatedly pressing
5893 \\[idlwave-complete].
5894
5895 The function also knows about object methods. When it needs a class
5896 name, the action depends upon `idlwave-query-class', which see. You
5897 can force IDLWAVE to ask you for a class name with a
5898 \\[universal-argument] prefix argument to this command.
5899
5900 See also the variables `idlwave-keyword-completion-adds-equal' and
5901 `idlwave-function-completion-adds-paren'.
5902
5903 The optional ARG can be used to specify the completion type in order
5904 to override IDLWAVE's idea of what should be completed at point.
5905 Possible values are:
5906
5907 0 <=> query for the completion type
5908 1 <=> 'procedure
5909 2 <=> 'procedure-keyword
5910 3 <=> 'function
5911 4 <=> 'function-keyword
5912 5 <=> 'procedure-method
5913 6 <=> 'procedure-method-keyword
5914 7 <=> 'function-method
5915 8 <=> 'function-method-keyword
5916 9 <=> 'class
5917
5918 As a special case, the universal argument C-u forces completion of
5919 function names in places where the default would be a keyword.
5920
5921 Two prefix argument, C-u C-u, prompts for a regexp by which to limit
5922 completion.
5923
5924 For Lisp programmers only:
5925 When we force a keyword, optional argument MODULE can contain the module name.
5926 When we force a method or a method keyword, CLASS can specify the class."
5927 (interactive "P")
5928 (idlwave-routines)
5929 (let* ((where-list
5930 (if (and arg
5931 (or (and (integerp arg) (not (equal arg '(16))))
5932 (symbolp arg)))
5933 (idlwave-make-force-complete-where-list arg module class)
5934 (idlwave-where)))
5935 (what (nth 2 where-list))
5936 (idlwave-force-class-query (equal arg '(4)))
5937 (completion-regexp-list
5938 (if (equal arg '(16))
5939 (list (read-string (concat "Completion Regexp: "))))))
5940
5941 (if (and module (string-match "::" module))
5942 (setq class (substring module 0 (match-beginning 0))
5943 module (substring module (match-end 0))))
5944
5945 (cond
5946
5947 ((and (null arg)
5948 (eq (car-safe last-command) 'idlwave-display-completion-list)
5949 (get-buffer-window "*Completions*"))
5950 (setq this-command last-command)
5951 (idlwave-scroll-completions))
5952
5953 ;; Complete a filename in quotes
5954 ((and (idlwave-in-quote)
5955 (not (eq what 'class)))
5956 (idlwave-complete-filename))
5957
5958 ;; Check for any special completion functions
5959 ((and idlwave-complete-special
5960 (idlwave-call-special idlwave-complete-special)))
5961
5962 ((null what)
5963 (error "Nothing to complete here"))
5964
5965 ;; Complete a class
5966 ((eq what 'class)
5967 (setq idlwave-completion-help-info '(class))
5968 (idlwave-complete-class))
5969
5970 ((eq what 'procedure)
5971 ;; Complete a procedure name
5972 (let* ((cw-list (nth 3 where-list))
5973 (class-selector (idlwave-determine-class cw-list 'pro))
5974 (super-classes (unless (idlwave-explicit-class-listed cw-list)
5975 (idlwave-all-class-inherits class-selector)))
5976 (isa (concat "procedure" (if class-selector "-method" "")))
5977 (type-selector 'pro))
5978 (setq idlwave-completion-help-info
5979 (list 'routine nil type-selector class-selector nil super-classes))
5980 (idlwave-complete-in-buffer
5981 'procedure (if class-selector 'method 'routine)
5982 (idlwave-routines) 'idlwave-selector
5983 (format "Select a %s name%s"
5984 isa
5985 (if class-selector
5986 (format " (class is %s)"
5987 (if (eq class-selector t)
5988 "unknown" class-selector))
5989 ""))
5990 isa
5991 'idlwave-attach-method-classes 'idlwave-add-file-link-selector)))
5992
5993 ((eq what 'function)
5994 ;; Complete a function name
5995 (let* ((cw-list (nth 3 where-list))
5996 (class-selector (idlwave-determine-class cw-list 'fun))
5997 (super-classes (unless (idlwave-explicit-class-listed cw-list)
5998 (idlwave-all-class-inherits class-selector)))
5999 (isa (concat "function" (if class-selector "-method" "")))
6000 (type-selector 'fun))
6001 (setq idlwave-completion-help-info
6002 (list 'routine nil type-selector class-selector nil super-classes))
6003 (idlwave-complete-in-buffer
6004 'function (if class-selector 'method 'routine)
6005 (idlwave-routines) 'idlwave-selector
6006 (format "Select a %s name%s"
6007 isa
6008 (if class-selector
6009 (format " (class is %s)"
6010 (if (eq class-selector t)
6011 "unknown" class-selector))
6012 ""))
6013 isa
6014 'idlwave-attach-method-classes 'idlwave-add-file-link-selector)))
6015
6016 ((and (memq what '(procedure-keyword function-keyword)) ; Special Case
6017 (equal arg '(4)))
6018 (idlwave-complete 3))
6019
6020 ((eq what 'procedure-keyword)
6021 ;; Complete a procedure keyword
6022 (let* ((where (nth 3 where-list))
6023 (name (car where))
6024 (method-selector name)
6025 (type-selector 'pro)
6026 (class (idlwave-determine-class where 'pro))
6027 (class-selector class)
6028 (super-classes (idlwave-all-class-inherits class-selector))
6029 (isa (format "procedure%s-keyword" (if class "-method" "")))
6030 (entry (idlwave-best-rinfo-assq
6031 name 'pro class (idlwave-routines)))
6032 (system (if entry (eq (car (nth 3 entry)) 'system)))
6033 (list (idlwave-entry-keywords entry 'do-link)))
6034 (unless (or entry (eq class t))
6035 (error "Nothing known about procedure %s"
6036 (idlwave-make-full-name class name)))
6037 (setq list (idlwave-fix-keywords name 'pro class list
6038 super-classes system))
6039 (unless list (error "No keywords available for procedure %s"
6040 (idlwave-make-full-name class name)))
6041 (setq idlwave-completion-help-info
6042 (list 'keyword name type-selector class-selector entry super-classes))
6043 (idlwave-complete-in-buffer
6044 'keyword 'keyword list nil
6045 (format "Select keyword for procedure %s%s"
6046 (idlwave-make-full-name class name)
6047 (if (or (member '("_EXTRA") list)
6048 (member '("_REF_EXTRA") list))
6049 " (note _EXTRA)" ""))
6050 isa
6051 'idlwave-attach-keyword-classes)))
6052
6053 ((eq what 'function-keyword)
6054 ;; Complete a function keyword
6055 (let* ((where (nth 3 where-list))
6056 (name (car where))
6057 (method-selector name)
6058 (type-selector 'fun)
6059 (class (idlwave-determine-class where 'fun))
6060 (class-selector class)
6061 (super-classes (idlwave-all-class-inherits class-selector))
6062 (isa (format "function%s-keyword" (if class "-method" "")))
6063 (entry (idlwave-best-rinfo-assq
6064 name 'fun class (idlwave-routines)))
6065 (system (if entry (eq (car (nth 3 entry)) 'system)))
6066 (list (idlwave-entry-keywords entry 'do-link))
6067 msg-name)
6068 (unless (or entry (eq class t))
6069 (error "Nothing known about function %s"
6070 (idlwave-make-full-name class name)))
6071 (setq list (idlwave-fix-keywords name 'fun class list
6072 super-classes system))
6073 ;; OBJ_NEW: Messages mention the proper Init method
6074 (setq msg-name (if (and (null class)
6075 (string= (upcase name) "OBJ_NEW"))
6076 (concat idlwave-current-obj_new-class
6077 "::Init (via OBJ_NEW)")
6078 (idlwave-make-full-name class name)))
6079 (unless list (error "No keywords available for function %s"
6080 msg-name))
6081 (setq idlwave-completion-help-info
6082 (list 'keyword name type-selector class-selector nil super-classes))
6083 (idlwave-complete-in-buffer
6084 'keyword 'keyword list nil
6085 (format "Select keyword for function %s%s" msg-name
6086 (if (or (member '("_EXTRA") list)
6087 (member '("_REF_EXTRA") list))
6088 " (note _EXTRA)" ""))
6089 isa
6090 'idlwave-attach-keyword-classes)))
6091
6092 (t (error "This should not happen (idlwave-complete)")))))
6093
6094 (defvar idlwave-complete-special nil
6095 "List of special completion functions.
6096 These functions are called for each completion. Each function must
6097 check if its own special completion context is present. If yes, it
6098 should use `idlwave-complete-in-buffer' to do some completion and
6099 return t. If such a function returns t, *no further* attempts to
6100 complete other contexts will be done. If the function returns nil,
6101 other completions will be tried.")
6102
6103 (defun idlwave-call-special (functions &rest args)
6104 (let ((funcs functions)
6105 fun ret)
6106 (catch 'exit
6107 (while (setq fun (pop funcs))
6108 (if (setq ret (apply fun args))
6109 (throw 'exit ret)))
6110 nil)))
6111
6112 (defun idlwave-make-force-complete-where-list (what &optional module class)
6113 ;; Return an artificial WHERE specification to force the completion
6114 ;; routine to complete a specific item independent of context.
6115 ;; WHAT is the prefix arg of `idlwave-complete', see there for details.
6116 ;; MODULE and CLASS can be used to specify the routine name and class.
6117 ;; The class name will also be found in MODULE if that is like "class::mod".
6118 (let* ((what-list '(("procedure") ("procedure-keyword")
6119 ("function") ("function-keyword")
6120 ("procedure-method") ("procedure-method-keyword")
6121 ("function-method") ("function-method-keyword")
6122 ("class")))
6123 (module (idlwave-sintern-routine-or-method module class))
6124 (class (idlwave-sintern-class class))
6125 (what (cond
6126 ((equal what 0)
6127 (setq what
6128 (intern (completing-read
6129 "Complete what? " what-list nil t))))
6130 ((integerp what)
6131 (setq what (intern (car (nth (1- what) what-list)))))
6132 ((and what
6133 (symbolp what)
6134 (assoc (symbol-name what) what-list))
6135 what)
6136 (t (error "Invalid WHAT"))))
6137 (nil-list '(nil nil nil nil))
6138 (class-list (list nil nil (or class t) nil)))
6139
6140 (cond
6141
6142 ((eq what 'procedure)
6143 (list nil-list nil-list 'procedure nil-list nil))
6144
6145 ((eq what 'procedure-keyword)
6146 (let* ((class-selector nil)
6147 (super-classes nil)
6148 (type-selector 'pro)
6149 (pro (or module
6150 (idlwave-completing-read
6151 "Procedure: " (idlwave-routines) 'idlwave-selector))))
6152 (setq pro (idlwave-sintern-routine pro))
6153 (list nil-list nil-list 'procedure-keyword
6154 (list pro nil nil nil) nil)))
6155
6156 ((eq what 'function)
6157 (list nil-list nil-list 'function nil-list nil))
6158
6159 ((eq what 'function-keyword)
6160 (let* ((class-selector nil)
6161 (super-classes nil)
6162 (type-selector 'fun)
6163 (func (or module
6164 (idlwave-completing-read
6165 "Function: " (idlwave-routines) 'idlwave-selector))))
6166 (setq func (idlwave-sintern-routine func))
6167 (list nil-list nil-list 'function-keyword
6168 (list func nil nil nil) nil)))
6169
6170 ((eq what 'procedure-method)
6171 (list nil-list nil-list 'procedure class-list nil))
6172
6173 ((eq what 'procedure-method-keyword)
6174 (let* ((class (idlwave-determine-class class-list 'pro))
6175 (class-selector class)
6176 (super-classes (idlwave-all-class-inherits class-selector))
6177 (type-selector 'pro)
6178 (pro (or module
6179 (idlwave-completing-read
6180 (format "Procedure in %s class: " class-selector)
6181 (idlwave-routines) 'idlwave-selector))))
6182 (setq pro (idlwave-sintern-method pro))
6183 (list nil-list nil-list 'procedure-keyword
6184 (list pro nil class nil) nil)))
6185
6186 ((eq what 'function-method)
6187 (list nil-list nil-list 'function class-list nil))
6188
6189 ((eq what 'function-method-keyword)
6190 (let* ((class (idlwave-determine-class class-list 'fun))
6191 (class-selector class)
6192 (super-classes (idlwave-all-class-inherits class-selector))
6193 (type-selector 'fun)
6194 (func (or module
6195 (idlwave-completing-read
6196 (format "Function in %s class: " class-selector)
6197 (idlwave-routines) 'idlwave-selector))))
6198 (setq func (idlwave-sintern-method func))
6199 (list nil-list nil-list 'function-keyword
6200 (list func nil class nil) nil)))
6201
6202 ((eq what 'class)
6203 (list nil-list nil-list 'class nil-list nil))
6204
6205 (t (error "Invalid value for WHAT")))))
6206
6207 (defun idlwave-completing-read (&rest args)
6208 ;; Completing read, case insensitive
6209 (let ((old-value (default-value 'completion-ignore-case)))
6210 (unwind-protect
6211 (progn
6212 (setq-default completion-ignore-case t)
6213 (apply 'completing-read args))
6214 (setq-default completion-ignore-case old-value))))
6215
6216 (defvar idlwave-shell-default-directory)
6217 (defun idlwave-complete-filename ()
6218 "Use the comint stuff to complete a file name."
6219 (require 'comint)
6220 (let* ((comint-file-name-chars "~/A-Za-z0-9+@:_.$#%={}\\-")
6221 (comint-completion-addsuffix nil)
6222 (default-directory
6223 (if (and (boundp 'idlwave-shell-default-directory)
6224 (stringp idlwave-shell-default-directory)
6225 (file-directory-p idlwave-shell-default-directory))
6226 idlwave-shell-default-directory
6227 default-directory)))
6228 (comint-dynamic-complete-filename)))
6229
6230 (defun idlwave-make-full-name (class name)
6231 ;; Make a fully qualified module name including the class name
6232 (concat (if class (format "%s::" class) "") name))
6233
6234 (defun idlwave-rinfo-assoc (name type class list)
6235 "Like `idlwave-rinfo-assq', but sintern strings first."
6236 (idlwave-rinfo-assq
6237 (idlwave-sintern-routine-or-method name class)
6238 type (idlwave-sintern-class class) list))
6239
6240 (defun idlwave-rinfo-assq (name type class list)
6241 ;; Works like assq, but also checks type and class
6242 (catch 'exit
6243 (let (match)
6244 (while (setq match (assq name list))
6245 (and (or (eq type t)
6246 (eq (nth 1 match) type))
6247 (eq (nth 2 match) class)
6248 (throw 'exit match))
6249 (setq list (cdr (memq match list)))))))
6250
6251 (defun idlwave-rinfo-assq-any-class (name type class list)
6252 ;; Return the first matching method on the inheritance list
6253 (let* ((classes (cons class (idlwave-all-class-inherits class)))
6254 class rtn)
6255 (while classes
6256 (if (setq rtn (idlwave-rinfo-assq name type (pop classes) list))
6257 (setq classes nil)))
6258 rtn))
6259
6260 (defun idlwave-best-rinfo-assq (name type class list &optional with-file
6261 keep-system)
6262 "Like `idlwave-rinfo-assq', but get all twins and sort, then return first.
6263 If WITH-FILE is passed, find the best rinfo entry with a file
6264 included. If KEEP-SYSTEM is set, don't prune system for compiled
6265 syslib files."
6266 (let ((twins (idlwave-routine-twins
6267 (idlwave-rinfo-assq-any-class name type class list)
6268 list))
6269 syslibp)
6270 (when (> (length twins) 1)
6271 (setq twins (sort twins 'idlwave-routine-entry-compare-twins))
6272 (if (and (null keep-system)
6273 (eq 'system (car (nth 3 (car twins))))
6274 (setq syslibp (idlwave-any-syslib (cdr twins)))
6275 (not (equal 1 syslibp)))
6276 ;; Its a compiled syslib, so we need to remove the system entry
6277 (setq twins (cdr twins)))
6278 (if with-file
6279 (setq twins (delq nil
6280 (mapcar (lambda (x)
6281 (if (nth 1 (nth 3 x)) x))
6282 twins)))))
6283 (car twins)))
6284
6285 (defun idlwave-best-rinfo-assoc (name type class list &optional with-file
6286 keep-system)
6287 "Like `idlwave-best-rinfo-assq', but sintern strings first."
6288 (idlwave-best-rinfo-assq
6289 (idlwave-sintern-routine-or-method name class)
6290 type (idlwave-sintern-class class) list with-file keep-system))
6291
6292 (defun idlwave-any-syslib (entries)
6293 "Does the entry list ENTRIES contain a syslib entry?
6294 If yes, return the index (>=1)."
6295 (let (file (cnt 0))
6296 (catch 'exit
6297 (while entries
6298 (incf cnt)
6299 (setq file (idlwave-routine-source-file (nth 3 (car entries))))
6300 (if (and file (idlwave-syslib-p file))
6301 (throw 'exit cnt)
6302 (setq entries (cdr entries))))
6303 nil)))
6304
6305 (defun idlwave-all-assq (key list)
6306 "Return a list of all associations of Key in LIST."
6307 (let (rtn elt)
6308 (while (setq elt (assq key list))
6309 (push elt rtn)
6310 (setq list (cdr (memq elt list))))
6311 (nreverse rtn)))
6312
6313 (defun idlwave-all-method-classes (method &optional type)
6314 "Return all classes which have a method METHOD. TYPE is 'fun or 'pro.
6315 When TYPE is not specified, both procedures and functions will be considered."
6316 (if (null method)
6317 (mapcar 'car (idlwave-class-alist))
6318 (let (rtn)
6319 (mapcar (lambda (x)
6320 (and (nth 2 x)
6321 (or (not type)
6322 (eq type (nth 1 x)))
6323 (push (nth 2 x) rtn)))
6324 (idlwave-all-assq method (idlwave-routines)))
6325 (idlwave-uniquify rtn))))
6326
6327 (defun idlwave-all-method-keyword-classes (method keyword &optional type)
6328 "Return all classes which have a method METHOD with keyword KEYWORD.
6329 TYPE is 'fun or 'pro.
6330 When TYPE is not specified, both procedures and functions will be considered."
6331 (if (or (null method)
6332 (null keyword))
6333 nil
6334 (let (rtn)
6335 (mapcar (lambda (x)
6336 (and (nth 2 x) ; non-nil class
6337 (or (not type) ; correct or unspecified type
6338 (eq type (nth 1 x)))
6339 (assoc keyword (idlwave-entry-keywords x))
6340 (push (nth 2 x) rtn)))
6341 (idlwave-all-assq method (idlwave-routines)))
6342 (idlwave-uniquify rtn))))
6343
6344 (defun idlwave-members-only (list club)
6345 "Return list of all elements in LIST which are also in CLUB."
6346 (let (rtn)
6347 (while list
6348 (if (member (car list) club)
6349 (setq rtn (cons (car list) rtn)))
6350 (setq list (cdr list)))
6351 (nreverse rtn)))
6352
6353 (defun idlwave-nonmembers-only (list club)
6354 "Return list of all elements in LIST which are not in CLUB."
6355 (let (rtn)
6356 (while list
6357 (if (member (car list) club)
6358 nil
6359 (setq rtn (cons (car list) rtn)))
6360 (setq list (cdr list)))
6361 (nreverse rtn)))
6362
6363 (defun idlwave-explicit-class-listed (info)
6364 "Return whether or not the class is listed explicitly, ala a->b::c.
6365 INFO is as returned by idlwave-what-function or -procedure."
6366 (let ((apos (nth 3 info)))
6367 (if apos
6368 (save-excursion (goto-char apos)
6369 (looking-at "->[a-zA-Z][a-zA-Z0-9$_]*::")))))
6370
6371 (defvar idlwave-determine-class-special nil
6372 "List of special functions for determining class.
6373 Must accept two arguments: `apos' and `info'")
6374
6375 (defun idlwave-determine-class (info type)
6376 ;; Determine the class of a routine call.
6377 ;; INFO is the `cw-list' structure as returned by idlwave-where.
6378 ;; The second element in this structure is the class. When nil, we
6379 ;; return nil. When t, try to get the class from text properties at
6380 ;; the arrow. When the object is "self", we use the class of the
6381 ;; current routine. otherwise prompt the user for a class name.
6382 ;; Also stores the selected class as a text property at the arrow.
6383 ;; TYPE is 'fun or 'pro.
6384 (let* ((class (nth 2 info))
6385 (apos (nth 3 info))
6386 (nassoc (assoc (if (stringp (car info))
6387 (upcase (car info))
6388 (car info))
6389 idlwave-query-class))
6390 (dassoc (assq (if (car info) 'keyword-default 'method-default)
6391 idlwave-query-class))
6392 (query (cond (nassoc (cdr nassoc))
6393 (dassoc (cdr dassoc))
6394 (t t)))
6395 (arrow (and apos (string= (buffer-substring apos (+ 2 apos)) "->")))
6396 (is-self
6397 (and arrow
6398 (save-excursion (goto-char apos)
6399 (forward-word -1)
6400 (let ((case-fold-search t))
6401 (looking-at "self\\>")))))
6402 (force-query idlwave-force-class-query)
6403 store special-class class-alist)
6404 (cond
6405 ((null class) nil)
6406 ((eq t class)
6407 ;; There is an object which would like to know its class
6408 (if (and arrow (get-text-property apos 'idlwave-class)
6409 idlwave-store-inquired-class
6410 (not force-query))
6411 (setq class (get-text-property apos 'idlwave-class)
6412 class (idlwave-sintern-class class)))
6413 (if (and (eq t class) is-self)
6414 (setq class (or (nth 2 (idlwave-current-routine)) class)))
6415
6416 ;; Before prompting, try any special class determination routines
6417 (when (and (eq t class)
6418 idlwave-determine-class-special
6419 (not force-query))
6420 (setq special-class
6421 (idlwave-call-special idlwave-determine-class-special apos))
6422 (if special-class
6423 (setq class (idlwave-sintern-class special-class)
6424 store idlwave-store-inquired-class)))
6425
6426 ;; Prompt for a class, if we need to
6427 (when (and (eq class t)
6428 (or force-query query))
6429 (setq class-alist
6430 (mapcar 'list (idlwave-all-method-classes (car info) type)))
6431 (setq class
6432 (idlwave-sintern-class
6433 (cond
6434 ((and (= (length class-alist) 0) (not force-query))
6435 (error "No classes available with method %s" (car info)))
6436 ((and (= (length class-alist) 1) (not force-query))
6437 (car (car class-alist)))
6438 (t
6439 (setq store idlwave-store-inquired-class)
6440 (idlwave-completing-read
6441 (format "Class%s: " (if (stringp (car info))
6442 (format " for %s method %s"
6443 type (car info))
6444 ""))
6445 class-alist nil nil nil 'idlwave-class-history))))))
6446
6447 ;; Store it, if requested
6448 (when (and class (not (eq t class)))
6449 ;; We have a real class here
6450 (when (and store arrow)
6451 (condition-case ()
6452 (add-text-properties
6453 apos (+ apos 2)
6454 `(idlwave-class ,class face ,idlwave-class-arrow-face
6455 rear-nonsticky t))
6456 (error nil)))
6457 (setf (nth 2 info) class))
6458 ;; Return the class
6459 class)
6460 ;; Default as fallback
6461 (t class))))
6462
6463 (defvar type-selector)
6464 (defvar class-selector)
6465 (defvar method-selector)
6466 (defvar super-classes)
6467 (defun idlwave-selector (a)
6468 (and (eq (nth 1 a) type-selector)
6469 (or (and (nth 2 a) (eq class-selector t))
6470 (eq (nth 2 a) class-selector)
6471 (memq (nth 2 a) super-classes))))
6472
6473 (defun idlwave-add-file-link-selector (a)
6474 ;; Record a file link, if any, for the tested names during selection.
6475 (let ((sel (idlwave-selector a)) file)
6476 (if (and sel (setq file (idlwave-entry-has-help a)))
6477 (push (cons (car a) file) idlwave-completion-help-links))
6478 sel))
6479
6480
6481 (defun idlwave-where ()
6482 "Find out where we are.
6483 The return value is a list with the following stuff:
6484 \(PRO-LIST FUNC-LIST COMPLETE-WHAT CW-LIST LAST-CHAR)
6485
6486 PRO-LIST (PRO POINT CLASS ARROW)
6487 FUNC-LIST (FUNC POINT CLASS ARROW)
6488 COMPLETE-WHAT a symbol indicating what kind of completion makes sense here
6489 CW-LIST (PRO-OR-FUNC POINT CLASS ARROW) Like PRO-LIST, for what can
6490 be completed here.
6491 LAST-CHAR last relevant character before point (non-white non-comment,
6492 not part of current identifier or leading slash).
6493
6494 In the lists, we have these meanings:
6495 PRO: Procedure name
6496 FUNC: Function name
6497 POINT: Where is this
6498 CLASS: What class has the routine (nil=no, t=is method, but class unknown)
6499 ARROW: Location of the arrow"
6500 (idlwave-routines)
6501 (let* (;(bos (save-excursion (idlwave-beginning-of-statement) (point)))
6502 (bos (save-excursion (idlwave-start-of-substatement 'pre) (point)))
6503 (func-entry (idlwave-what-function bos))
6504 (func (car func-entry))
6505 (func-class (nth 1 func-entry))
6506 (func-arrow (nth 2 func-entry))
6507 (func-point (or (nth 3 func-entry) 0))
6508 (func-level (or (nth 4 func-entry) 0))
6509 (pro-entry (idlwave-what-procedure bos))
6510 (pro (car pro-entry))
6511 (pro-class (nth 1 pro-entry))
6512 (pro-arrow (nth 2 pro-entry))
6513 (pro-point (or (nth 3 pro-entry) 0))
6514 (last-char (idlwave-last-valid-char))
6515 (case-fold-search t)
6516 (match-string (buffer-substring bos (point)))
6517 cw cw-mod cw-arrow cw-class cw-point)
6518 (if (< func-point pro-point) (setq func nil))
6519 (cond
6520 ((string-match "\\`[ \t]*\\(pro\\|function\\)[ \t]+[a-zA-Z0-9_]*\\'"
6521 match-string)
6522 (setq cw 'class))
6523 ((string-match
6524 "\\`[ \t]*\\([a-zA-Z][a-zA-Z0-9$_]*\\)?\\'"
6525 (if (> pro-point 0)
6526 (buffer-substring pro-point (point))
6527 match-string))
6528 (setq cw 'procedure cw-class pro-class cw-point pro-point
6529 cw-arrow pro-arrow))
6530 ((string-match "\\`[ \t]*\\(pro\\|function\\)\\>"
6531 match-string)
6532 nil)
6533 ((string-match "OBJ_NEW([ \t]*['\"]\\([a-zA-Z0-9$_]*\\)?\\'"
6534 match-string)
6535 (setq cw 'class))
6536 ((string-match "\\<inherits\\s-+\\([a-zA-Z0-9$_]*\\)?\\'"
6537 match-string)
6538 (setq cw 'class))
6539 ((and func
6540 (> func-point pro-point)
6541 (= func-level 1)
6542 (memq last-char '(?\( ?,)))
6543 (setq cw 'function-keyword cw-mod func cw-point func-point
6544 cw-class func-class cw-arrow func-arrow))
6545 ((and pro (eq last-char ?,))
6546 (setq cw 'procedure-keyword cw-mod pro cw-point pro-point
6547 cw-class pro-class cw-arrow pro-arrow))
6548 ; ((member last-char '(?\' ?\) ?\] ?!))
6549 ; ;; after these chars, a function makes no sense
6550 ; ;; FIXME: I am sure there can be more in this list
6551 ; ;; FIXME: Do we want to do this at all?
6552 ; nil)
6553 ;; Everywhere else we try a function.
6554 (t
6555 (setq cw 'function)
6556 (save-excursion
6557 (if (re-search-backward "->[ \t]*\\(\\$[ \t]*\\(;.*\\)?\n\\s-*\\)?\\(\\([$a-zA-Z0-9_]+\\)::\\)?[$a-zA-Z0-9_]*\\=" bos t)
6558 (setq cw-arrow (copy-marker (match-beginning 0))
6559 cw-class (if (match-end 4)
6560 (idlwave-sintern-class (match-string 4))
6561 t))))))
6562 (list (list pro pro-point pro-class pro-arrow)
6563 (list func func-point func-class func-arrow)
6564 cw
6565 (list cw-mod cw-point cw-class cw-arrow)
6566 last-char)))
6567
6568 (defun idlwave-this-word (&optional class)
6569 ;; Grab the word around point. CLASS is for the `skip-chars=...' functions
6570 (setq class (or class "a-zA-Z0-9$_."))
6571 (save-excursion
6572 (buffer-substring
6573 (progn (skip-chars-backward class) (point))
6574 (progn (skip-chars-forward class) (point)))))
6575
6576 (defun idlwave-what-function (&optional bound)
6577 ;; Find out if point is within the argument list of a function.
6578 ;; The return value is ("function-name" class arrow-start (point) level).
6579 ;; Level is 1 on the top level parentheses, higher further down.
6580
6581 ;; If the optional BOUND is an integer, bound backwards directed
6582 ;; searches to this point.
6583
6584 (catch 'exit
6585 (let (pos
6586 func-point
6587 (cnt 0)
6588 func arrow-start class)
6589 (idlwave-with-special-syntax
6590 (save-restriction
6591 (save-excursion
6592 (narrow-to-region (max 1 (or bound 0)) (point-max))
6593 ;; move back out of the current parenthesis
6594 (while (condition-case nil
6595 (progn (up-list -1) t)
6596 (error nil))
6597 (setq pos (point))
6598 (incf cnt)
6599 (when (and (= (following-char) ?\()
6600 (re-search-backward
6601 "\\(::\\|\\<\\)\\([a-zA-Z][a-zA-Z0-9$_]*\\)[ \t]*\\="
6602 bound t))
6603 (setq func (match-string 2)
6604 func-point (goto-char (match-beginning 2))
6605 pos func-point)
6606 (if (re-search-backward
6607 "->[ \t]*\\(\\([a-zA-Z][a-zA-Z0-9$_]*\\)::\\)?\\=" bound t)
6608 (setq arrow-start (copy-marker (match-beginning 0))
6609 class (or (match-string 2) t)))
6610 (throw
6611 'exit
6612 (list
6613 (idlwave-sintern-routine-or-method func class)
6614 (idlwave-sintern-class class)
6615 arrow-start func-point cnt)))
6616 (goto-char pos))
6617 (throw 'exit nil)))))))
6618
6619 (defun idlwave-what-procedure (&optional bound)
6620 ;; Find out if point is within the argument list of a procedure.
6621 ;; The return value is ("procedure-name" class arrow-pos (point)).
6622
6623 ;; If the optional BOUND is an integer, bound backwards directed
6624 ;; searches to this point.
6625 (let ((pos (point)) pro-point
6626 pro class arrow-start string)
6627 (save-excursion
6628 ;;(idlwave-beginning-of-statement)
6629 (idlwave-start-of-substatement 'pre)
6630 (setq string (buffer-substring (point) pos))
6631 (if (string-match
6632 "\\`[ \t]*\\([a-zA-Z][a-zA-Z0-9$_]*\\)[ \t]*\\(,\\|\\'\\)" string)
6633 (setq pro (match-string 1 string)
6634 pro-point (+ (point) (match-beginning 1)))
6635 (if (and (idlwave-skip-object)
6636 (setq string (buffer-substring (point) pos))
6637 (string-match
6638 "\\`[ \t]*\\(->\\)[ \t]*\\(\\([a-zA-Z][a-zA-Z0-9$_]*\\)::\\)?\\([a-zA-Z][a-zA-Z0-9$_]*\\)?[ \t]*\\(,\\|\\(\\$\\s *\\(;.*\\)?\\)?$\\)"
6639 string))
6640 (setq pro (if (match-beginning 4)
6641 (match-string 4 string))
6642 pro-point (if (match-beginning 4)
6643 (+ (point) (match-beginning 4))
6644 pos)
6645 arrow-start (copy-marker (+ (point) (match-beginning 1)))
6646 class (or (match-string 3 string) t)))))
6647 (list (idlwave-sintern-routine-or-method pro class)
6648 (idlwave-sintern-class class)
6649 arrow-start
6650 pro-point)))
6651
6652 (defun idlwave-skip-object ()
6653 ;; If there is an object at point, move over it and return t.
6654 (let ((pos (point)))
6655 (if (catch 'exit
6656 (save-excursion
6657 (skip-chars-forward " ") ; white space
6658 (skip-chars-forward "*") ; de-reference
6659 (cond
6660 ((looking-at idlwave-identifier)
6661 (goto-char (match-end 0)))
6662 ((eq (following-char) ?\()
6663 nil)
6664 (t (throw 'exit nil)))
6665 (catch 'endwhile
6666 (while t
6667 (cond ((eq (following-char) ?.)
6668 (forward-char 1)
6669 (if (not (looking-at idlwave-identifier))
6670 (throw 'exit nil))
6671 (goto-char (match-end 0)))
6672 ((memq (following-char) '(?\( ?\[))
6673 (condition-case nil
6674 (forward-list 1)
6675 (error (throw 'exit nil))))
6676 (t (throw 'endwhile t)))))
6677 (if (looking-at "[ \t]*->")
6678 (throw 'exit (setq pos (match-beginning 0)))
6679 (throw 'exit nil))))
6680 (goto-char pos)
6681 nil)))
6682
6683 (defun idlwave-last-valid-char ()
6684 "Return the last character before point which is not white or a comment
6685 and also not part of the current identifier. Since we do this in
6686 order to identify places where keywords are, we consider the initial
6687 `/' of a keyword as part of the identifier.
6688 This function is not general, can only be used for completion stuff."
6689 (catch 'exit
6690 (save-excursion
6691 ;; skip the current identifier
6692 (skip-chars-backward "a-zA-Z0-9_$")
6693 ;; also skip a leading slash which might be belong to the keyword
6694 (if (eq (preceding-char) ?/)
6695 (backward-char 1))
6696 ;; FIXME: does not check if this is a valid identifier
6697 (while t
6698 (skip-chars-backward " \t")
6699 (cond
6700 ((memq (preceding-char) '(?\; ?\$)) (throw 'exit nil))
6701 ((eq (preceding-char) ?\n)
6702 (beginning-of-line 0)
6703 (if (looking-at "\\([^\n]*\\)\\$[ \t]*\\(;[^\n]*\\)?\n")
6704 ;; continuation line
6705 (goto-char (match-end 1))
6706 (throw 'exit nil)))
6707 (t (throw 'exit (preceding-char))))))))
6708
6709 (defvar idlwave-complete-after-success-form nil
6710 "A form to evaluate after successful completion.")
6711 (defvar idlwave-complete-after-success-form-force nil
6712 "A form to evaluate after completion selection in *Completions* buffer.")
6713 (defconst idlwave-completion-mark (make-marker)
6714 "A mark pointing to the beginning of the completion string.")
6715
6716 (defun idlwave-complete-in-buffer (type stype list selector prompt isa
6717 &optional prepare-display-function
6718 special-selector)
6719 "Perform TYPE completion of word before point against LIST.
6720 SELECTOR is the PREDICATE argument for the completion function. Show
6721 PROMPT in echo area. TYPE is one of the intern types, e.g. 'function,
6722 'procedure, 'class-tag, 'keyword, 'sysvar, etc.. SPECIAL-SELECTOR is
6723 used only once, for `all-completions', and can be used to, e.g.,
6724 accumulate information on matching completions."
6725 (let* ((completion-ignore-case t)
6726 beg (end (point)) slash part spart completion all-completions
6727 dpart dcompletion)
6728
6729 (unless list
6730 (error (concat prompt ": No completions available")))
6731
6732 ;; What is already in the buffer?
6733 (save-excursion
6734 (skip-chars-backward "a-zA-Z0-9_$")
6735 (setq slash (eq (preceding-char) ?/)
6736 beg (point)
6737 idlwave-complete-after-success-form
6738 (list 'idlwave-after-successful-completion
6739 (list 'quote type) slash beg)
6740 idlwave-complete-after-success-form-force
6741 (list 'idlwave-after-successful-completion
6742 (list 'quote type) slash (list 'quote 'force))))
6743
6744 ;; Try a completion
6745 (setq part (buffer-substring beg end)
6746 dpart (downcase part)
6747 spart (idlwave-sintern stype part)
6748 completion (try-completion part list selector)
6749 dcompletion (if (stringp completion) (downcase completion))
6750 idlwave-completion-help-links nil)
6751 (cond
6752 ((null completion)
6753 ;; nothing available.
6754 (error (concat prompt ": no completion for \"%s\"") part))
6755 ((and (not (equal dpart dcompletion))
6756 (not (eq t completion)))
6757 ;; We can add something
6758 (delete-region beg end)
6759 (if (and (string= part dpart)
6760 (or (not (string= part ""))
6761 idlwave-complete-empty-string-as-lower-case)
6762 (not idlwave-completion-force-default-case))
6763 (insert dcompletion)
6764 (insert completion))
6765 (if (eq t (try-completion completion list selector))
6766 ;; Now this is a unique match
6767 (idlwave-after-successful-completion type slash beg))
6768 t)
6769 ((or (eq completion t)
6770 (and (= 1 (length (setq all-completions
6771 (idlwave-uniquify
6772 (all-completions part list
6773 (or special-selector
6774 selector))))))
6775 (equal dpart dcompletion)))
6776 ;; This is already complete
6777 (idlwave-after-successful-completion type slash beg)
6778 (message "%s is already the complete %s" part isa)
6779 nil)
6780 (t
6781 ;; We cannot add something - offer a list.
6782 (message "Making completion list...")
6783
6784 (unless idlwave-completion-help-links ; already set somewhere?
6785 (mapcar (lambda (x) ; Pass link prop through to highlight-linked
6786 (let ((link (get-text-property 0 'link (car x))))
6787 (if link
6788 (push (cons (car x) link)
6789 idlwave-completion-help-links))))
6790 list))
6791 (let* ((list all-completions)
6792 ;; "complete" means, this is already a valid completion
6793 (complete (memq spart all-completions))
6794 (completion-highlight-first-word-only t)) ; XEmacs
6795 ; (completion-fixup-function ; Emacs
6796 ; (lambda () (and (eq (preceding-char) ?>)
6797 ; (re-search-backward " <" beg t)))))
6798
6799 (setq list (sort list (lambda (a b)
6800 (string< (downcase a) (downcase b)))))
6801 (if prepare-display-function
6802 (setq list (funcall prepare-display-function list)))
6803 (if (and (string= part dpart)
6804 (or (not (string= part ""))
6805 idlwave-complete-empty-string-as-lower-case)
6806 (not idlwave-completion-force-default-case))
6807 (setq list (mapcar (lambda (x)
6808 (if (listp x)
6809 (setcar x (downcase (car x)))
6810 (setq x (downcase x)))
6811 x)
6812 list)))
6813 (idlwave-display-completion-list list prompt beg complete))
6814 t))))
6815
6816 (defun idlwave-complete-class ()
6817 "Complete a class at point."
6818 (interactive)
6819 ;; Call `idlwave-routines' to make sure the class list will be available
6820 (idlwave-routines)
6821 ;; Check for the special case of completing empty string after pro/function
6822 (if (let ((case-fold-search t))
6823 (save-excursion
6824 (and
6825 (re-search-backward "\\<\\(pro\\|function\\)[ \t]+\\="
6826 (- (point) 15) t)
6827 (goto-char (point-min))
6828 (re-search-forward
6829 "^[ \t]*\\(pro\\|function\\)[ \t]+\\([a-zA-Z0-9_]+::\\)" nil t))))
6830 ;; Yank the full class specification
6831 (insert (match-string 2))
6832 ;; Do the completion, using list gathered from `idlwave-routines'
6833 (idlwave-complete-in-buffer
6834 'class 'class (idlwave-class-alist) nil
6835 "Select a class" "class"
6836 '(lambda (list) ;; Push it to help-links if system help available
6837 (mapcar (lambda (x)
6838 (let* ((entry (idlwave-class-info x))
6839 (link (nth 1 (assq 'link entry))))
6840 (if link (push (cons x link)
6841 idlwave-completion-help-links))
6842 x))
6843 list)))))
6844
6845 (defun idlwave-attach-classes (list type show-classes)
6846 ;; Attach the proper class list to a LIST of completion items.
6847 ;; TYPE, when 'kwd, shows classes for method keywords, when
6848 ;; 'class-tag, for class tags, and otherwise for methods.
6849 ;; SHOW-CLASSES is the value of `idlwave-completion-show-classes'.
6850 (if (or (null show-classes) ; don't want to see classes
6851 (null class-selector) ; not a method call
6852 (and
6853 (stringp class-selector) ; the class is already known
6854 (not super-classes))) ; no possibilities for inheritance
6855 ;; In these cases, we do not have to do anything
6856 list
6857 (let* ((do-prop (and (>= show-classes 0)
6858 (>= emacs-major-version 21)))
6859 (do-buf (not (= show-classes 0)))
6860 ;; (do-dots (featurep 'xemacs))
6861 (do-dots t)
6862 (inherit (if (and (not (eq type 'class-tag)) super-classes)
6863 (cons class-selector super-classes)))
6864 (max (abs show-classes))
6865 (lmax (if do-dots (apply 'max (mapcar 'length list))))
6866 classes nclasses class-info space)
6867 (mapcar
6868 (lambda (x)
6869 ;; get the classes
6870 (if (eq type 'class-tag)
6871 ;; Just one class for tags
6872 (setq classes
6873 (list
6874 (idlwave-class-or-superclass-with-tag class-selector x)))
6875 ;; Multiple classes for method or method-keyword
6876 (setq classes
6877 (if (eq type 'kwd)
6878 (idlwave-all-method-keyword-classes
6879 method-selector x type-selector)
6880 (idlwave-all-method-classes x type-selector)))
6881 (if inherit
6882 (setq classes
6883 (delq nil
6884 (mapcar (lambda (x) (if (memq x inherit) x nil))
6885 classes)))))
6886 (setq nclasses (length classes))
6887 ;; Make the separator between item and class-info
6888 (if do-dots
6889 (setq space (concat " " (make-string (- lmax (length x)) ?.)))
6890 (setq space " "))
6891 (if do-buf
6892 ;; We do want info in the buffer
6893 (if (<= nclasses max)
6894 (setq class-info (concat
6895 space
6896 "<" (mapconcat 'identity classes ",") ">"))
6897 (setq class-info (format "%s<%d classes>" space nclasses)))
6898 (setq class-info nil))
6899 (when do-prop
6900 ;; We do want properties
6901 (setq x (copy-sequence x))
6902 (put-text-property 0 (length x)
6903 'help-echo (mapconcat 'identity classes " ")
6904 x))
6905 (if class-info
6906 (list x class-info)
6907 x))
6908 list))))
6909
6910 (defun idlwave-attach-method-classes (list)
6911 ;; Call idlwave-attach-classes with method parameters
6912 (idlwave-attach-classes list 'method idlwave-completion-show-classes))
6913 (defun idlwave-attach-keyword-classes (list)
6914 ;; Call idlwave-attach-classes with keyword parameters
6915 (idlwave-attach-classes list 'kwd idlwave-completion-show-classes))
6916 (defun idlwave-attach-class-tag-classes (list)
6917 ;; Call idlwave-attach-classes with class structure tags
6918 (idlwave-attach-classes list 'class-tag idlwave-completion-show-classes))
6919
6920
6921 ;;----------------------------------------------------------------------
6922 ;;----------------------------------------------------------------------
6923 ;;----------------------------------------------------------------------
6924 ;;----------------------------------------------------------------------
6925 ;;----------------------------------------------------------------------
6926 (defvar rtn)
6927 (defun idlwave-pset (item)
6928 (set 'rtn item))
6929
6930 (defun idlwave-popup-select (ev list title &optional sort)
6931 "Select an item in LIST with a popup menu.
6932 TITLE is the title to put atop the popup. If SORT is non-nil,
6933 sort the list before displaying"
6934 (let ((maxpopup idlwave-max-popup-menu-items)
6935 rtn menu resp)
6936 (cond ((null list))
6937 ((= 1 (length list))
6938 (setq rtn (car list)))
6939 ((featurep 'xemacs)
6940 (if sort (setq list (sort list (lambda (a b)
6941 (string< (upcase a) (upcase b))))))
6942 (setq menu
6943 (append (list title)
6944 (mapcar (lambda (x) (vector x (list 'idlwave-pset
6945 x)))
6946 list)))
6947 (setq menu (idlwave-split-menu-xemacs menu maxpopup))
6948 (setq resp (get-popup-menu-response menu))
6949 (funcall (event-function resp) (event-object resp)))
6950 (t
6951 (if sort (setq list (sort list (lambda (a b)
6952 (string< (upcase a) (upcase b))))))
6953 (setq menu (cons title
6954 (list
6955 (append (list "")
6956 (mapcar (lambda(x) (cons x x)) list)))))
6957 (setq menu (idlwave-split-menu-emacs menu maxpopup))
6958 (setq rtn (x-popup-menu ev menu))))
6959 rtn))
6960
6961 (defun idlwave-split-menu-xemacs (menu N)
6962 "Split the MENU into submenus of maximum length N."
6963 (if (<= (length menu) (1+ N))
6964 ;; No splitting needed
6965 menu
6966 (let* ((title (car menu))
6967 (entries (cdr menu))
6968 (menu (list title))
6969 (cnt 0)
6970 (nextmenu nil))
6971 (while entries
6972 (while (and entries (< cnt N))
6973 (setq cnt (1+ cnt)
6974 nextmenu (cons (car entries) nextmenu)
6975 entries (cdr entries)))
6976 (setq nextmenu (nreverse nextmenu))
6977 (setq nextmenu (cons (format "%s...%s"
6978 (aref (car nextmenu) 0)
6979 (aref (nth (1- cnt) nextmenu) 0))
6980 nextmenu))
6981 (setq menu (cons nextmenu menu)
6982 nextmenu nil
6983 cnt 0))
6984 (nreverse menu))))
6985
6986 (defun idlwave-split-menu-emacs (menu N)
6987 "Split the MENU into submenus of maximum length N."
6988 (if (<= (length (nth 1 menu)) (1+ N))
6989 ;; No splitting needed
6990 menu
6991 (let* ((title (car menu))
6992 (entries (cdr (nth 1 menu)))
6993 (menu nil)
6994 (cnt 0)
6995 (nextmenu nil))
6996 (while entries
6997 (while (and entries (< cnt N))
6998 (setq cnt (1+ cnt)
6999 nextmenu (cons (car entries) nextmenu)
7000 entries (cdr entries)))
7001 (setq nextmenu (nreverse nextmenu))
7002 (prin1 nextmenu)
7003 (setq nextmenu (cons (format "%s...%s"
7004 (car (car nextmenu))
7005 (car (nth (1- cnt) nextmenu)))
7006 nextmenu))
7007 (setq menu (cons nextmenu menu)
7008 nextmenu nil
7009 cnt 0))
7010 (setq menu (nreverse menu))
7011 (setq menu (cons title menu))
7012 menu)))
7013
7014 (defvar idlwave-completion-setup-hook nil)
7015
7016 (defun idlwave-scroll-completions (&optional message)
7017 "Scroll the completion window on this frame."
7018 (let ((cwin (get-buffer-window "*Completions*" 'visible))
7019 (win (selected-window)))
7020 (unwind-protect
7021 (progn
7022 (select-window cwin)
7023 (condition-case nil
7024 (scroll-up)
7025 (error (if (and (listp last-command)
7026 (nth 2 last-command))
7027 (progn
7028 (select-window win)
7029 (eval idlwave-complete-after-success-form))
7030 (set-window-start cwin (point-min)))))
7031 (and message (message message)))
7032 (select-window win))))
7033
7034 (defun idlwave-display-completion-list (list &optional message beg complete)
7035 "Display the completions in LIST in the completions buffer and echo MESSAGE."
7036 (unless (and (get-buffer-window "*Completions*")
7037 (idlwave-local-value 'idlwave-completion-p "*Completions*"))
7038 (move-marker idlwave-completion-mark beg)
7039 (setq idlwave-before-completion-wconf (current-window-configuration)))
7040
7041 (if (featurep 'xemacs)
7042 (idlwave-display-completion-list-xemacs
7043 list)
7044 (idlwave-display-completion-list-emacs list))
7045
7046 ;; Store a special value in `this-command'. When `idlwave-complete'
7047 ;; finds this in `last-command', it will scroll the *Completions* buffer.
7048 (setq this-command (list 'idlwave-display-completion-list message complete))
7049
7050 ;; Mark the completions buffer as created by cib
7051 (idlwave-set-local 'idlwave-completion-p t "*Completions*")
7052
7053 ;; Fontify the classes
7054 (if (and idlwave-completion-fontify-classes
7055 (consp (car list)))
7056 (idlwave-completion-fontify-classes))
7057
7058 ;; Run the hook
7059 (run-hooks 'idlwave-completion-setup-hook)
7060
7061 ;; Display the message
7062 (message (or message "Making completion list...done")))
7063
7064 (defun idlwave-choose (function &rest args)
7065 "Call FUNCTION as a completion chooser and pass ARGS to it."
7066 (let ((completion-ignore-case t)) ; install correct value
7067 (apply function args))
7068 (if (and (eq major-mode 'idlwave-shell-mode)
7069 (boundp 'font-lock-mode)
7070 (not font-lock-mode))
7071 ;; For the shell, remove the fontification of the word before point
7072 (let ((beg (save-excursion
7073 (skip-chars-backward "a-zA-Z0-9_")
7074 (point))))
7075 (remove-text-properties beg (point) '(face nil))))
7076 (eval idlwave-complete-after-success-form-force))
7077
7078 (defun idlwave-keyboard-quit ()
7079 (interactive)
7080 (unwind-protect
7081 (if (eq (car-safe last-command) 'idlwave-display-completion-list)
7082 (idlwave-restore-wconf-after-completion))
7083 (keyboard-quit)))
7084
7085 (defun idlwave-restore-wconf-after-completion ()
7086 "Restore the old (before completion) window configuration."
7087 (and idlwave-completion-restore-window-configuration
7088 idlwave-before-completion-wconf
7089 (set-window-configuration idlwave-before-completion-wconf)))
7090
7091 (defun idlwave-one-key-select (sym prompt delay)
7092 "Make the user select an element from the alist in the variable SYM.
7093 The keys of the alist are expected to be strings. The function returns the
7094 car of the selected association.
7095 To do this, PROMPT is displayed and and the user must hit a letter key to
7096 select an entry. If the user does not reply within DELAY seconds, a help
7097 window with the options is displayed automatically.
7098 The key which is associated with each option is generated automatically.
7099 First, the strings are checked for preselected keys, like in \"[P]rint\".
7100 If these don't exist, a letter in the string is automatically selected."
7101 (let* ((alist (symbol-value sym))
7102 (temp-buffer-show-hook (if (fboundp 'fit-window-to-buffer)
7103 '(fit-window-to-buffer)))
7104 keys-alist char)
7105 ;; First check the cache
7106 (if (and (eq (symbol-value sym) (get sym :one-key-alist-last)))
7107 (setq keys-alist (get sym :one-key-alist-cache))
7108 ;; Need to make new list
7109 (setq keys-alist (idlwave-make-one-key-alist alist))
7110 (put sym :one-key-alist-cache keys-alist)
7111 (put sym :one-key-alist-last alist))
7112 ;; Display prompt and wait for quick reply
7113 (message "%s[%s]" prompt
7114 (mapconcat (lambda(x) (char-to-string (car x)))
7115 keys-alist ""))
7116 (if (sit-for delay)
7117 ;; No quick reply: Show help
7118 (save-window-excursion
7119 (with-output-to-temp-buffer "*Completions*"
7120 (mapcar (lambda(x)
7121 (princ (nth 1 x))
7122 (princ "\n"))
7123 keys-alist))
7124 (setq char (read-char)))
7125 (setq char (read-char)))
7126 (message nil)
7127 ;; Return the selected result
7128 (nth 2 (assoc char keys-alist))))
7129
7130 ;; Used for, e.g., electric debug super-examine.
7131 (defun idlwave-make-one-key-alist (alist)
7132 "Make an alist for single key selection."
7133 (let ((l alist) keys-alist name start char help
7134 (cnt 0)
7135 (case-fold-search nil))
7136 (while l
7137 (setq name (car (car l))
7138 l (cdr l))
7139 (catch 'exit
7140 ;; First check if the configuration predetermined a key
7141 (if (string-match "\\[\\(.\\)\\]" name)
7142 (progn
7143 (setq char (string-to-char (downcase (match-string 1 name)))
7144 help (format "%c: %s" char name)
7145 keys-alist (cons (list char help name) keys-alist))
7146 (throw 'exit t)))
7147 ;; Then check for capital letters
7148 (setq start 0)
7149 (while (string-match "[A-Z]" name start)
7150 (setq start (match-end 0)
7151 char (string-to-char (downcase (match-string 0 name))))
7152 (if (not (assoc char keys-alist))
7153 (progn
7154 (setq help (format "%c: %s" char
7155 (replace-match
7156 (concat "[" (match-string 0 name) "]")
7157 t t name))
7158 keys-alist (cons (list char help name) keys-alist))
7159 (throw 'exit t))))
7160 ;; Now check for lowercase letters
7161 (setq start 0)
7162 (while (string-match "[a-z]" name start)
7163 (setq start (match-end 0)
7164 char (string-to-char (match-string 0 name)))
7165 (if (not (assoc char keys-alist))
7166 (progn
7167 (setq help (format "%c: %s" char
7168 (replace-match
7169 (concat "[" (match-string 0 name) "]")
7170 t t name))
7171 keys-alist (cons (list char help name) keys-alist))
7172 (throw 'exit t))))
7173 ;; Bummer, nothing found! Use a stupid number
7174 (setq char (string-to-char (int-to-string (setq cnt (1+ cnt))))
7175 help (format "%c: %s" char name)
7176 keys-alist (cons (list char help name) keys-alist))))
7177 (nreverse keys-alist)))
7178
7179 (defun idlwave-set-local (var value &optional buffer)
7180 "Set the buffer-local value of VAR in BUFFER to VALUE."
7181 (save-excursion
7182 (set-buffer (or buffer (current-buffer)))
7183 (set (make-local-variable var) value)))
7184
7185 (defun idlwave-local-value (var &optional buffer)
7186 "Return the value of VAR in BUFFER, but only if VAR is local to BUFFER."
7187 (save-excursion
7188 (set-buffer (or buffer (current-buffer)))
7189 (and (local-variable-p var (current-buffer))
7190 (symbol-value var))))
7191
7192 ;; In XEmacs, we can use :activate-callback directly to advice the
7193 ;; choose functions. We use the private keymap only for the online
7194 ;; help feature.
7195
7196 (defvar idlwave-completion-map nil
7197 "Keymap for completion-list-mode with idlwave-complete.")
7198
7199 (defun idlwave-display-completion-list-xemacs (list &rest cl-args)
7200 (with-output-to-temp-buffer "*Completions*"
7201 (apply 'display-completion-list list
7202 ':activate-callback 'idlwave-default-choose-completion
7203 cl-args))
7204 (save-excursion
7205 (set-buffer "*Completions*")
7206 (use-local-map
7207 (or idlwave-completion-map
7208 (setq idlwave-completion-map
7209 (idlwave-make-modified-completion-map-xemacs
7210 (current-local-map)))))))
7211
7212 (defun idlwave-default-choose-completion (&rest args)
7213 "Execute `default-choose-completion' and then restore the win-conf."
7214 (apply 'idlwave-choose 'default-choose-completion args))
7215
7216 (defun idlwave-make-modified-completion-map-xemacs (old-map)
7217 "Replace `choose-completion' and `mouse-choose-completion' in OLD-MAP."
7218 (let ((new-map (copy-keymap old-map)))
7219 (define-key new-map [button3up] 'idlwave-mouse-completion-help)
7220 (define-key new-map [button3] (lambda ()
7221 (interactive)
7222 (setq this-command last-command)))
7223 new-map))
7224
7225 ;; In Emacs we also replace keybindings in the completion
7226 ;; map in order to install our wrappers.
7227
7228 (defun idlwave-display-completion-list-emacs (list)
7229 "Display completion list and install the choose wrappers."
7230 (with-output-to-temp-buffer "*Completions*"
7231 (display-completion-list list))
7232 (save-excursion
7233 (set-buffer "*Completions*")
7234 (use-local-map
7235 (or idlwave-completion-map
7236 (setq idlwave-completion-map
7237 (idlwave-make-modified-completion-map-emacs
7238 (current-local-map)))))))
7239
7240 (defun idlwave-make-modified-completion-map-emacs (old-map)
7241 "Replace `choose-completion' and `mouse-choose-completion' in OLD-MAP."
7242 (let ((new-map (copy-keymap old-map)))
7243 (substitute-key-definition
7244 'choose-completion 'idlwave-choose-completion new-map)
7245 (substitute-key-definition
7246 'mouse-choose-completion 'idlwave-mouse-choose-completion new-map)
7247 (define-key new-map [mouse-3] 'idlwave-mouse-completion-help)
7248 new-map))
7249
7250 (defun idlwave-choose-completion (&rest args)
7251 "Choose the completion that point is in or next to."
7252 (interactive)
7253 (apply 'idlwave-choose 'choose-completion args))
7254
7255 (defun idlwave-mouse-choose-completion (&rest args)
7256 "Click on an alternative in the `*Completions*' buffer to choose it."
7257 (interactive "e")
7258 (apply 'idlwave-choose 'mouse-choose-completion args))
7259
7260 ;;----------------------------------------------------------------------
7261 ;;----------------------------------------------------------------------
7262
7263 ;;; ------------------------------------------------------------------------
7264 ;;; Stucture parsing code, and code to manage class info
7265
7266 ;;
7267 ;; - Go again over the documentation how to write a completion
7268 ;; plugin. It is in self.el, but currently still very bad.
7269 ;; This could be in a separate file in the distribution, or
7270 ;; in an appendix for the manual.
7271
7272 (defvar idlwave-struct-skip
7273 "[ \t]*\\(\\$.*\n\\(^[ \t]*\\(\\$[ \t]*\\)?\\(;.*\\)?\n\\)*\\)?[ \t]*"
7274 "Regexp for skipping continued blank or comment-only lines in
7275 structures")
7276
7277 (defvar idlwave-struct-tag-regexp
7278 (concat "[{,]" ;leading comma/brace
7279 idlwave-struct-skip ; 4 groups
7280 "\\([a-zA-Z][a-zA-Z0-9_]*\\)" ;the tag itself, group 5
7281 "[ \t]*:") ; the final colon
7282 "Regexp for structure tags.")
7283
7284 (defun idlwave-struct-tags ()
7285 "Return a list of all tags in the structure defined at point.
7286 Point is expected just before the opening `{' of the struct definition."
7287 (save-excursion
7288 (let* ((borders (idlwave-struct-borders))
7289 (beg (car borders))
7290 (end (cdr borders))
7291 tags)
7292 (goto-char beg)
7293 (save-restriction
7294 (narrow-to-region beg end)
7295 (while (re-search-forward idlwave-struct-tag-regexp end t)
7296 ;; Check if we are still on the top level of the structure.
7297 (if (and (condition-case nil (progn (up-list -1) t) (error nil))
7298 (= (point) beg))
7299 (push (match-string-no-properties 5) tags))
7300 (goto-char (match-end 0))))
7301 (nreverse tags))))
7302
7303 (defun idlwave-find-struct-tag (tag)
7304 "Find a given TAG in the structure defined at point."
7305 (let* ((borders (idlwave-struct-borders))
7306 (beg (car borders))
7307 (end (cdr borders))
7308 (case-fold-search t))
7309 (re-search-forward (concat "\\(^[ \t]*\\|[,{][ \t]*\\)" tag "[ \t]*:")
7310 end t)))
7311
7312 (defun idlwave-struct-inherits ()
7313 "Return a list of all `inherits' names in the struct at point.
7314 Point is expected just before the opening `{' of the struct definition."
7315 (save-excursion
7316 (let* ((borders (idlwave-struct-borders))
7317 (beg (car borders))
7318 (end (cdr borders))
7319 (case-fold-search t)
7320 names)
7321 (goto-char beg)
7322 (save-restriction
7323 (narrow-to-region beg end)
7324 (while (re-search-forward
7325 (concat "[{,]" ;leading comma/brace
7326 idlwave-struct-skip ; 4 groups
7327 "inherits" ; The INHERITS tag
7328 idlwave-struct-skip ; 4 more
7329 "\\([a-zA-Z][a-zA-Z0-9_]*\\)") ; The super-group, #9
7330 end t)
7331 ;; Check if we are still on the top level of the structure.
7332 (if (and (condition-case nil (progn (up-list -1) t) (error nil))
7333 (= (point) beg))
7334 (push (match-string-no-properties 9) names))
7335 (goto-char (match-end 0))))
7336 (nreverse names))))
7337
7338 (defun idlwave-in-structure ()
7339 "Return t if point is inside an IDL structure definition."
7340 (let ((beg (point)))
7341 (save-excursion
7342 (if (not (or (idlwave-in-comment) (idlwave-in-quote)))
7343 (if (idlwave-find-structure-definition nil nil 'back)
7344 (let ((borders (idlwave-struct-borders)))
7345 (or (= (car borders) (cdr borders)) ;; struct not yet closed...
7346 (and (> beg (car borders)) (< beg (cdr borders))))))))))
7347
7348 (defun idlwave-struct-borders ()
7349 "Return the borders of the {...} after point as a cons cell."
7350 (let (beg)
7351 (save-excursion
7352 (skip-chars-forward "^{")
7353 (setq beg (point))
7354 (condition-case nil (forward-list 1)
7355 (error (goto-char beg)))
7356 (cons beg (point)))))
7357
7358 (defun idlwave-find-structure-definition (&optional var name bound)
7359 "Search forward for a structure definition. If VAR is non-nil,
7360 search for a structure assigned to variable VAR. If NAME is non-nil,
7361 search for a named structure NAME, if a string, or a generic named
7362 structure otherwise. If BOUND is an integer, limit the search. If
7363 BOUND is the symbol `all', we search first back and then forward
7364 through the entire file. If BOUND is the symbol `back' we search only
7365 backward."
7366 (let* ((ws "[ \t]*\\(\\$.*\n[ \t]*\\)*")
7367 (case-fold-search t)
7368 (lim (if (integerp bound) bound nil))
7369 (re (concat
7370 (if var
7371 (concat "\\<" (regexp-quote (downcase var)) "\\>" ws)
7372 "\\(\\)")
7373 "=" ws "\\({\\)"
7374 (if name
7375 (if (stringp name)
7376 (concat ws "\\(\\<" (downcase name) "\\)[^a-zA-Z0-9_$]")
7377 ;; Just a generic name
7378 (concat ws "\\<\\([a-zA-Z_0-9$]+\\)" ws ","))
7379 ""))))
7380 (if (or (and (or (eq bound 'all) (eq bound 'back))
7381 (re-search-backward re nil t))
7382 (and (not (eq bound 'back)) (re-search-forward re lim t)))
7383 (progn
7384 (goto-char (match-beginning 3))
7385 (match-string-no-properties 5)))))
7386
7387 (defvar idlwave-class-info nil)
7388 (defvar idlwave-class-reset nil) ; to reset buffer-local classes
7389
7390 (add-hook 'idlwave-update-rinfo-hook
7391 (lambda () (setq idlwave-class-reset t)))
7392 (add-hook 'idlwave-after-load-rinfo-hook
7393 (lambda () (setq idlwave-class-info nil)))
7394
7395 (defun idlwave-class-info (class)
7396 (let (list entry)
7397 (if idlwave-class-info
7398 (if idlwave-class-reset
7399 (setq
7400 idlwave-class-reset nil
7401 idlwave-class-info ; Remove any visited in a buffer
7402 (delq nil (mapcar
7403 (lambda (x)
7404 (let ((filebuf
7405 (idlwave-class-file-or-buffer
7406 (or (cdr (assq 'found-in x)) (car x)))))
7407 (if (cdr filebuf)
7408 nil
7409 x)))
7410 idlwave-class-info))))
7411 ;; Info is nil, put in the system stuff to start.
7412 (setq idlwave-class-info idlwave-system-class-info)
7413 (setq list idlwave-class-info)
7414 (while (setq entry (pop list))
7415 (idlwave-sintern-class-info entry)))
7416 (setq class (idlwave-sintern-class class))
7417 (or (assq class idlwave-class-info)
7418 (progn (idlwave-scan-class-info class)
7419 (assq class idlwave-class-info)))))
7420
7421 (defun idlwave-sintern-class-info (entry)
7422 "Sintern the class names in a class-info entry."
7423 (let ((taglist (assq 'tags entry))
7424 (inherits (assq 'inherits entry)))
7425 (setcar entry (idlwave-sintern-class (car entry) 'set))
7426 (if inherits
7427 (setcdr inherits (mapcar (lambda (x) (idlwave-sintern-class x 'set))
7428 (cdr inherits))))))
7429
7430 (defun idlwave-find-class-definition (class &optional all-hook alt-class)
7431 "Find class structure definition(s)
7432 If ALL-HOOK is set, find all named structure definitions in a given
7433 class__define routine, on which ALL-HOOK will be run. If ALT-CLASS is
7434 set, look for the name__define pro, and inside of it, for the ALT-CLASS
7435 class/struct definition"
7436 (let ((case-fold-search t) end-lim list name)
7437 (when (re-search-forward
7438 (concat "^[ \t]*pro[ \t]+" (downcase class) "__define" "\\>") nil t)
7439 (if all-hook
7440 (progn
7441 ;; For everything there
7442 (setq end-lim (save-excursion (idlwave-end-of-subprogram) (point)))
7443 (while (setq name
7444 (idlwave-find-structure-definition nil t end-lim))
7445 (funcall all-hook name)))
7446 (idlwave-find-structure-definition nil (or alt-class class))))))
7447
7448
7449 (defun idlwave-class-file-or-buffer (class)
7450 "Find buffer visiting CLASS definition"
7451 (let* ((pro (concat (downcase class) "__define"))
7452 (file (idlwave-routine-source-file
7453 (nth 3 (idlwave-rinfo-assoc pro 'pro nil
7454 (idlwave-routines))))))
7455 (cons file (if file (idlwave-get-buffer-visiting file)))))
7456
7457
7458 (defun idlwave-scan-class-info (class)
7459 "Scan all class and named structure info in the class__define pro"
7460 (let* ((idlwave-auto-routine-info-updates nil)
7461 (filebuf (idlwave-class-file-or-buffer class))
7462 (file (car filebuf))
7463 (buf (cdr filebuf))
7464 (class (idlwave-sintern-class class)))
7465 (if (or
7466 (not file)
7467 (and ;; neither a regular file nor a visited buffer
7468 (not buf)
7469 (not (file-regular-p file))))
7470 nil ; Cannot find the file/buffer to get any info
7471 (save-excursion
7472 (if buf (set-buffer buf)
7473 ;; Read the file in temporarily
7474 (set-buffer (get-buffer-create " *IDLWAVE-tmp*"))
7475 (erase-buffer)
7476 (unless (eq major-mode 'idlwave-mode)
7477 (idlwave-mode))
7478 (insert-file-contents file))
7479 (save-excursion
7480 (goto-char 1)
7481 (idlwave-find-class-definition class
7482 ;; Scan all of the structures found there
7483 (lambda (name)
7484 (let* ((this-class (idlwave-sintern-class name))
7485 (entry
7486 (list this-class
7487 (cons 'tags (idlwave-struct-tags))
7488 (cons 'inherits (idlwave-struct-inherits)))))
7489 (if (not (eq this-class class))
7490 (setq entry (nconc entry (list (cons 'found-in class)))))
7491 (idlwave-sintern-class-info entry)
7492 (push entry idlwave-class-info)))))))))
7493
7494 (defun idlwave-class-found-in (class)
7495 "Return the FOUND-IN property of the class."
7496 (cdr (assq 'found-in (idlwave-class-info class))))
7497 (defun idlwave-class-tags (class)
7498 "Return the native tags in CLASS."
7499 (cdr (assq 'tags (idlwave-class-info class))))
7500 (defun idlwave-class-inherits (class)
7501 "Return the direct superclasses of CLASS."
7502 (cdr (assq 'inherits (idlwave-class-info class))))
7503
7504
7505 (defun idlwave-all-class-tags (class)
7506 "Return a list of native and inherited tags in CLASS."
7507 (condition-case err
7508 (apply 'append (mapcar 'idlwave-class-tags
7509 (cons class (idlwave-all-class-inherits class))))
7510 (error
7511 (idlwave-class-tag-reset)
7512 (error "%s" (error-message-string err)))))
7513
7514
7515 (defun idlwave-all-class-inherits (class)
7516 "Return a list of all superclasses of CLASS (recursively expanded).
7517 The list is cached in `idlwave-class-info' for faster access."
7518 (cond
7519 ((not idlwave-support-inheritance) nil)
7520 ((eq class nil) nil)
7521 ((eq class t) nil)
7522 (t
7523 (let ((info (idlwave-class-info class))
7524 entry)
7525 (if (setq entry (assq 'all-inherits info))
7526 (cdr entry)
7527 ;; Save the depth of inheritance scan to check for circular references
7528 (let ((inherits (mapcar (lambda (x) (cons x 0))
7529 (idlwave-class-inherits class)))
7530 rtn all-inherits cl)
7531 (while inherits
7532 (setq cl (pop inherits)
7533 rtn (cons (car cl) rtn)
7534 inherits (append (mapcar (lambda (x)
7535 (cons x (1+ (cdr cl))))
7536 (idlwave-class-inherits (car cl)))
7537 inherits))
7538 (if (> (cdr cl) 999)
7539 (error
7540 "Class scan: inheritance depth exceeded. Circular inheritance?")
7541 ))
7542 (setq all-inherits (nreverse rtn))
7543 (nconc info (list (cons 'all-inherits all-inherits)))
7544 all-inherits))))))
7545
7546 (defun idlwave-entry-keywords (entry &optional record-link)
7547 "Return the flat entry keywords alist from routine-info entry.
7548 If RECORD-LINK is non-nil, the keyword text is copied and a text
7549 property indicating the link is added."
7550 (let (kwds)
7551 (mapcar
7552 (lambda (key-list)
7553 (let ((file (car key-list)))
7554 (mapcar (lambda (key-cons)
7555 (let ((key (car key-cons))
7556 (link (cdr key-cons)))
7557 (when (and record-link file)
7558 (setq key (copy-sequence key))
7559 (put-text-property
7560 0 (length key)
7561 'link
7562 (concat
7563 file
7564 (if link
7565 (concat idlwave-html-link-sep
7566 (number-to-string link))))
7567 key))
7568 (push (list key) kwds)))
7569 (cdr key-list))))
7570 (nthcdr 5 entry))
7571 (nreverse kwds)))
7572
7573 (defun idlwave-entry-find-keyword (entry keyword)
7574 "Find keyword KEYWORD in entry ENTRY, and return (with link) if set"
7575 (catch 'exit
7576 (mapc
7577 (lambda (key-list)
7578 (let ((file (car key-list))
7579 (kwd (assoc keyword (cdr key-list))))
7580 (when kwd
7581 (setq kwd (cons (car kwd)
7582 (if (and file (cdr kwd))
7583 (concat file
7584 idlwave-html-link-sep
7585 (number-to-string (cdr kwd)))
7586 (cdr kwd))))
7587 (throw 'exit kwd))))
7588 (nthcdr 5 entry))))
7589
7590 ;;==========================================================================
7591 ;;
7592 ;; Completing class structure tags. This is a completion plugin.
7593 ;; The necessary taglist is constructed dynamically
7594
7595 (defvar idlwave-current-tags-class nil)
7596 (defvar idlwave-current-class-tags nil)
7597 (defvar idlwave-current-native-class-tags nil)
7598 (defvar idlwave-sint-class-tags nil)
7599 (idlwave-new-sintern-type 'class-tag)
7600 (add-to-list 'idlwave-complete-special 'idlwave-complete-class-structure-tag)
7601 (add-hook 'idlwave-update-rinfo-hook 'idlwave-class-tag-reset)
7602
7603 (defun idlwave-complete-class-structure-tag ()
7604 "Complete a structure tag on a `self' argument in an object method."
7605 (interactive)
7606 (let ((pos (point))
7607 (case-fold-search t))
7608 (if (save-excursion
7609 ;; Check if the context is right
7610 (skip-chars-backward "a-zA-Z0-9._$")
7611 (and (< (point) (- pos 4))
7612 (looking-at "self\\.")))
7613 (let* ((class-selector (nth 2 (idlwave-current-routine)))
7614 (super-classes (idlwave-all-class-inherits class-selector)))
7615 ;; Check if we are in a class routine
7616 (unless class-selector
7617 (error "Not in a method procedure or function"))
7618 ;; Check if we need to update the "current" class
7619 (if (not (equal class-selector idlwave-current-tags-class))
7620 (idlwave-prepare-class-tag-completion class-selector))
7621 (setq idlwave-completion-help-info
7622 (list 'idlwave-complete-class-structure-tag-help
7623 (idlwave-sintern-routine
7624 (concat class-selector "__define"))
7625 nil))
7626 (let ((idlwave-cpl-bold idlwave-current-native-class-tags))
7627 (idlwave-complete-in-buffer
7628 'class-tag 'class-tag
7629 idlwave-current-class-tags nil
7630 (format "Select a tag of class %s" class-selector)
7631 "class tag"
7632 'idlwave-attach-class-tag-classes))
7633 t) ; return t to skip other completions
7634 nil)))
7635
7636 (defun idlwave-class-tag-reset ()
7637 (setq idlwave-current-tags-class nil))
7638
7639 (defun idlwave-prepare-class-tag-completion (class)
7640 "Find and parse the necessary class definitions for class structure tags."
7641 (setq idlwave-sint-class-tags nil)
7642 (setq idlwave-current-tags-class class)
7643 (setq idlwave-current-class-tags
7644 (mapcar (lambda (x)
7645 (list (idlwave-sintern-class-tag x 'set)))
7646 (idlwave-all-class-tags class)))
7647 (setq idlwave-current-native-class-tags
7648 (mapcar 'downcase (idlwave-class-tags class))))
7649
7650 ;===========================================================================
7651 ;;
7652 ;; Completing system variables and their structure fields
7653 ;; This is also a plugin.
7654
7655 (defvar idlwave-sint-sysvars nil)
7656 (defvar idlwave-sint-sysvartags nil)
7657 (idlwave-new-sintern-type 'sysvar)
7658 (idlwave-new-sintern-type 'sysvartag)
7659 (add-to-list 'idlwave-complete-special 'idlwave-complete-sysvar-or-tag)
7660 (add-hook 'idlwave-update-rinfo-hook 'idlwave-sysvars-reset)
7661 (add-hook 'idlwave-after-load-rinfo-hook 'idlwave-sintern-sysvar-alist)
7662
7663
7664 (defun idlwave-complete-sysvar-or-tag ()
7665 "Complete a system variable."
7666 (interactive)
7667 (let ((pos (point))
7668 (case-fold-search t))
7669 (cond ((save-excursion
7670 ;; Check if the context is right for system variable
7671 (skip-chars-backward "[a-zA-Z0-9_$]")
7672 (equal (char-before) ?!))
7673 (setq idlwave-completion-help-info '(idlwave-complete-sysvar-help))
7674 (idlwave-complete-in-buffer 'sysvar 'sysvar
7675 idlwave-system-variables-alist nil
7676 "Select a system variable"
7677 "system variable")
7678 t) ; return t to skip other completions
7679 ((save-excursion
7680 ;; Check if the context is right for sysvar tag
7681 (skip-chars-backward "a-zA-Z0-9_$.")
7682 (and (equal (char-before) ?!)
7683 (looking-at "\\([a-zA-Z][a-zA-Z0-9_$]*\\)\\.")
7684 (<= (match-end 0) pos)))
7685 ;; Complete a system variable tag
7686 (let* ((var (idlwave-sintern-sysvar (match-string 1)))
7687 (entry (assq var idlwave-system-variables-alist))
7688 (tags (cdr (assq 'tags entry))))
7689 (or entry (error "!%s is not a known system variable" var))
7690 (or tags (error "System variable !%s is not a structure" var))
7691 (setq idlwave-completion-help-info
7692 (list 'idlwave-complete-sysvar-tag-help var))
7693 (idlwave-complete-in-buffer 'sysvartag 'sysvartag
7694 tags nil
7695 "Select a system variable tag"
7696 "system variable tag")
7697 t)) ; return t to skip other completions
7698 (t nil))))
7699
7700 (defvar link) ;dynamic variables set by help callback
7701 (defvar props)
7702 (defun idlwave-complete-sysvar-help (mode word)
7703 (let ((word (or (nth 1 idlwave-completion-help-info) word))
7704 (entry (assoc word idlwave-system-variables-alist)))
7705 (cond
7706 ((eq mode 'test)
7707 (and (stringp word) entry (nth 1 (assq 'link entry))))
7708 ((eq mode 'set)
7709 (if entry (setq link (nth 1 (assq 'link entry))))) ;; setting dynamic!!!
7710 (t (error "This should not happen")))))
7711
7712 (defun idlwave-complete-sysvar-tag-help (mode word)
7713 (let* ((var (nth 1 idlwave-completion-help-info))
7714 (entry (assoc var idlwave-system-variables-alist))
7715 (tags (cdr (assq 'tags entry)))
7716 (main (nth 1 (assq 'link entry)))
7717 target main-base)
7718 (cond
7719 ((eq mode 'test) ; we can at least link the main
7720 (and (stringp word) entry main))
7721 ((eq mode 'set)
7722 (if entry
7723 (setq link
7724 (if (setq target (cdr (assoc-string word tags t)))
7725 (idlwave-substitute-link-target main target)
7726 main)))) ;; setting dynamic!!!
7727 (t (error "This should not happen")))))
7728
7729 (defun idlwave-split-link-target (link)
7730 "Split a given link into link file and anchor."
7731 (if (string-match idlwave-html-link-sep link)
7732 (cons (substring link 0 (match-beginning 0))
7733 (string-to-number (substring link (match-end 0))))))
7734
7735 (defun idlwave-substitute-link-target (link target)
7736 "Substitute the target anchor for the given link."
7737 (let (main-base)
7738 (setq main-base (if (string-match "#" link)
7739 (substring link 0 (match-beginning 0))
7740 link))
7741 (if target
7742 (concat main-base idlwave-html-link-sep (number-to-string target))
7743 link)))
7744
7745 ;; Fake help in the source buffer for class structure tags.
7746 ;; KWD AND NAME ARE GLOBAL-VARIABLES HERE.
7747 (defvar name)
7748 (defvar kwd)
7749 (defvar idlwave-help-do-class-struct-tag nil)
7750 (defun idlwave-complete-class-structure-tag-help (mode word)
7751 (cond
7752 ((eq mode 'test) ; nothing gets fontified for class tags
7753 nil)
7754 ((eq mode 'set)
7755 (let (class-with found-in)
7756 (when (setq class-with
7757 (idlwave-class-or-superclass-with-tag
7758 idlwave-current-tags-class
7759 word))
7760 (if (assq (idlwave-sintern-class class-with)
7761 idlwave-system-class-info)
7762 (error "No help available for system class tags"))
7763 (if (setq found-in (idlwave-class-found-in class-with))
7764 (setq name (cons (concat found-in "__define") class-with))
7765 (setq name (concat class-with "__define")))))
7766 (setq kwd word
7767 idlwave-help-do-class-struct-tag t))
7768 (t (error "This should not happen"))))
7769
7770 (defun idlwave-class-or-superclass-with-tag (class tag)
7771 "Find and return the CLASS or one of its superclass with the
7772 associated TAG, if any."
7773 (let ((sclasses (cons class (idlwave-all-class-inherits class)))
7774 cl)
7775 (catch 'exit
7776 (while sclasses
7777 (setq cl (pop sclasses))
7778 (let ((tags (idlwave-class-tags cl)))
7779 (while tags
7780 (if (eq t (compare-strings tag 0 nil (car tags) 0 nil t))
7781 (throw 'exit cl))
7782 (setq tags (cdr tags))))))))
7783
7784
7785 (defun idlwave-sysvars-reset ()
7786 (if (and (fboundp 'idlwave-shell-is-running)
7787 (idlwave-shell-is-running)
7788 idlwave-idlwave_routine_info-compiled)
7789 (idlwave-shell-send-command "idlwave_get_sysvars"
7790 'idlwave-process-sysvars 'hide)))
7791
7792 (defun idlwave-process-sysvars ()
7793 (idlwave-shell-filter-sysvars)
7794 (setq idlwave-sint-sysvars nil
7795 idlwave-sint-sysvartags nil)
7796 (idlwave-sintern-sysvar-alist))
7797
7798 (defun idlwave-sintern-sysvar-alist ()
7799 (let ((list idlwave-system-variables-alist) entry tags)
7800 (while (setq entry (pop list))
7801 (setcar entry (idlwave-sintern-sysvar (car entry) 'set))
7802 (setq tags (assq 'tags entry))
7803 (if tags
7804 (setcdr tags
7805 (mapcar (lambda (x)
7806 (cons (idlwave-sintern-sysvartag (car x) 'set)
7807 (cdr x)))
7808 (cdr tags)))))))
7809
7810 (defvar idlwave-shell-command-output)
7811 (defun idlwave-shell-filter-sysvars ()
7812 "Get any new system variables and tags."
7813 (let ((text idlwave-shell-command-output)
7814 (start 0)
7815 (old idlwave-system-variables-alist)
7816 var tags type name class link old-entry)
7817 (setq idlwave-system-variables-alist nil)
7818 (while (string-match "^IDLWAVE-SYSVAR: !\\([a-zA-Z0-9_$]+\\)\\( \\(.*\\)\\)?"
7819 text start)
7820 (setq start (match-end 0)
7821 var (match-string 1 text)
7822 tags (if (match-end 3)
7823 (idlwave-split-string (match-string 3 text))))
7824 ;; Maintain old links, if present
7825 (setq old-entry (assq (idlwave-sintern-sysvar var) old))
7826 (setq link (assq 'link old-entry))
7827 (setq idlwave-system-variables-alist
7828 (cons (list var
7829 (cons
7830 'tags
7831 (mapcar (lambda (x)
7832 (cons x
7833 (cdr (assq
7834 (idlwave-sintern-sysvartag x)
7835 (cdr (assq 'tags old-entry))))))
7836 tags)) link)
7837 idlwave-system-variables-alist)))
7838 ;; Keep the old value if query was not successful
7839 (setq idlwave-system-variables-alist
7840 (or idlwave-system-variables-alist old))))
7841
7842 (defun idlwave-completion-fontify-classes ()
7843 "Goto the *Completions* buffer and fontify the class info."
7844 (when (featurep 'font-lock)
7845 (save-excursion
7846 (set-buffer "*Completions*")
7847 (save-excursion
7848 (goto-char (point-min))
7849 (let ((buffer-read-only nil))
7850 (while (re-search-forward "\\.*<[^>]+>" nil t)
7851 (put-text-property (match-beginning 0) (match-end 0)
7852 'face 'font-lock-string-face)))))))
7853
7854 (defun idlwave-uniquify (list)
7855 (let ((ht (make-hash-table :size (length list) :test 'equal)))
7856 (delq nil
7857 (mapcar (lambda (x)
7858 (unless (gethash x ht)
7859 (puthash x t ht)
7860 x))
7861 list))))
7862
7863 (defun idlwave-after-successful-completion (type slash &optional verify)
7864 "Add `=' or `(' after successful completion of keyword and function.
7865 Restore the pre-completion window configuration if possible."
7866 (cond
7867 ((eq type 'procedure)
7868 nil)
7869 ((eq type 'function)
7870 (cond
7871 ((equal idlwave-function-completion-adds-paren nil) nil)
7872 ((or (equal idlwave-function-completion-adds-paren t)
7873 (equal idlwave-function-completion-adds-paren 1))
7874 (insert "("))
7875 ((equal idlwave-function-completion-adds-paren 2)
7876 (insert "()")
7877 (backward-char 1))
7878 (t nil)))
7879 ((eq type 'keyword)
7880 (if (and idlwave-keyword-completion-adds-equal
7881 (not slash))
7882 (progn (insert "=") t)
7883 nil)))
7884
7885 ;; Restore the pre-completion window configuration if this is safe.
7886
7887 (if (or (eq verify 'force) ; force
7888 (and
7889 (get-buffer-window "*Completions*") ; visible
7890 (idlwave-local-value 'idlwave-completion-p
7891 "*Completions*") ; cib-buffer
7892 (eq (marker-buffer idlwave-completion-mark)
7893 (current-buffer)) ; buffer OK
7894 (equal (marker-position idlwave-completion-mark)
7895 verify))) ; pos OK
7896 (idlwave-restore-wconf-after-completion))
7897 (move-marker idlwave-completion-mark nil)
7898 (setq idlwave-before-completion-wconf nil))
7899
7900 (defun idlwave-mouse-context-help (ev &optional arg)
7901 "Call `idlwave-context-help' on the clicked location."
7902 (interactive "eP")
7903 (mouse-set-point ev)
7904 (idlwave-context-help arg))
7905
7906 (defvar idlwave-last-context-help-pos nil)
7907 (defun idlwave-context-help (&optional arg)
7908 "Display IDL Online Help on context.
7909 If point is on a keyword, help for that keyword will be shown. If
7910 point is on a routine name or in the argument list of a routine, help
7911 for that routine will be displayed. Works for system routines and
7912 keywords, it pulls up text help. For other routies and keywords,
7913 visits the source file, finding help in the header (if
7914 `idlwave-help-source-try-header' is non-nil) or the routine definition
7915 itself."
7916 (interactive "P")
7917 (idlwave-do-context-help arg))
7918
7919 (defun idlwave-mouse-completion-help (ev)
7920 "Display online help about the completion at point."
7921 (interactive "eP")
7922 ;; Restore last-command for next command, to make
7923 ;; scrolling/cancelling of completions work.
7924 (setq this-command last-command)
7925 (idlwave-do-mouse-completion-help ev))
7926
7927 (defun idlwave-routine-info (&optional arg external)
7928 "Display a routines calling sequence and list of keywords. When
7929 point is on the name a function or procedure, or in the argument list
7930 of a function or procedure, this command displays a help buffer with
7931 the information. When called with prefix arg, enforce class query.
7932
7933 When point is on an object operator `->', display the class stored in
7934 this arrow, if any (see `idlwave-store-inquired-class'). With a
7935 prefix arg, the class property is cleared out."
7936
7937 (interactive "P")
7938 (idlwave-routines)
7939 (if (string-match "->" (buffer-substring
7940 (max (point-min) (1- (point)))
7941 (min (+ 2 (point)) (point-max))))
7942 ;; Cursor is on an arrow
7943 (if (get-text-property (point) 'idlwave-class)
7944 ;; arrow has class property
7945 (if arg
7946 ;; Remove property
7947 (save-excursion
7948 (backward-char 1)
7949 (when (looking-at ".?\\(->\\)")
7950 (remove-text-properties (match-beginning 1) (match-end 1)
7951 '(idlwave-class nil face nil))
7952 (message "Class property removed from arrow")))
7953 ;; Echo class property
7954 (message "Arrow has text property identifying object to be class %s"
7955 (get-text-property (point) 'idlwave-class)))
7956 ;; No property found
7957 (message "Arrow has no class text property"))
7958
7959 ;; Not on an arrow...
7960 (let* ((idlwave-query-class nil)
7961 (idlwave-force-class-query (equal arg '(4)))
7962 (module (idlwave-what-module)))
7963 (if (car module)
7964 (apply 'idlwave-display-calling-sequence
7965 (idlwave-fix-module-if-obj_new module))
7966 (error "Don't know which calling sequence to show")))))
7967
7968 (defun idlwave-resolve (&optional arg)
7969 "Call RESOLVE_ROUTINE on the module name at point.
7970 Like `idlwave-routine-info', this looks for a routine call at point.
7971 After confirmation in the minibuffer, it will use the shell to issue
7972 a RESOLVE call for this routine, to attempt to make it defined and its
7973 routine info available for IDLWAVE. If the routine is a method call,
7974 both `class__method' and `class__define' will be tried.
7975 With ARG, enforce query for the class of object methods."
7976 (interactive "P")
7977 (let* ((idlwave-query-class nil)
7978 (idlwave-force-class-query (equal arg '(4)))
7979 (module (idlwave-what-module))
7980 (name (idlwave-make-full-name (nth 2 module) (car module)))
7981 (type (if (eq (nth 1 module) 'pro) "pro" "function"))
7982 (resolve (read-string "Resolve: " (format "%s %s" type name)))
7983 (kwd "")
7984 class)
7985 (if (string-match "\\(pro\\|function\\)[ \t]+\\(\\(.*\\)::\\)?\\(.*\\)"
7986 resolve)
7987 (setq type (match-string 1 resolve)
7988 class (if (match-beginning 2)
7989 (match-string 3 resolve)
7990 nil)
7991 name (match-string 4 resolve)))
7992 (if (string= (downcase type) "function")
7993 (setq kwd ",/is_function"))
7994
7995 (cond
7996 ((null class)
7997 (idlwave-shell-send-command
7998 (format "resolve_routine,'%s'%s" (downcase name) kwd)
7999 'idlwave-update-routine-info
8000 nil t))
8001 (t
8002 (idlwave-shell-send-command
8003 (format "resolve_routine,'%s__define'%s" (downcase class) kwd)
8004 (list 'idlwave-shell-send-command
8005 (format "resolve_routine,'%s__%s'%s"
8006 (downcase class) (downcase name) kwd)
8007 '(idlwave-update-routine-info)
8008 nil t))))))
8009
8010 (defun idlwave-find-module-this-file ()
8011 (interactive)
8012 (idlwave-find-module '(4)))
8013
8014 (defun idlwave-find-module (&optional arg)
8015 "Find the source code of an IDL module.
8016 Works for modules for which IDLWAVE has routine info available. The
8017 function offers as default the module name `idlwave-routine-info'
8018 would use. With ARG limit to this buffer. With two prefix ARG's
8019 force class query for object methods."
8020 (interactive "P")
8021 (let* ((idlwave-query-class nil)
8022 (idlwave-force-class-query (equal arg '(16)))
8023 (this-buffer (equal arg '(4)))
8024 (module (idlwave-fix-module-if-obj_new (idlwave-what-module)))
8025 (default (if module
8026 (concat (idlwave-make-full-name
8027 (nth 2 module) (car module))
8028 (if (eq (nth 1 module) 'pro) "<p>" "<f>"))
8029 "none"))
8030 (list
8031 (idlwave-uniquify
8032 (delq nil
8033 (mapcar (lambda (x)
8034 (if (eq 'system (car-safe (nth 3 x)))
8035 ;; Take out system routines with no source.
8036 nil
8037 (list
8038 (concat (idlwave-make-full-name
8039 (nth 2 x) (car x))
8040 (if (eq (nth 1 x) 'pro) "<p>" "<f>")))))
8041 (if this-buffer
8042 (idlwave-save-buffer-update)
8043 (idlwave-routines))))))
8044 (name (idlwave-completing-read
8045 (if (or (not this-buffer)
8046 (assoc default list))
8047 (format "Module (Default %s): " default)
8048 (format "Module in this file: "))
8049 list))
8050 type class)
8051 (if (string-match "\\`\\s-*\\'" name)
8052 ;; Nothing, use the default.
8053 (setq name default))
8054 (if (string-match "<[fp]>" name)
8055 (setq type (substring name -2 -1)
8056 name (substring name 0 -3)))
8057 (if (string-match "\\(.*\\)::\\(.*\\)" name)
8058 (setq class (match-string 1 name)
8059 name (match-string 2 name)))
8060 (setq name (idlwave-sintern-routine-or-method name class)
8061 class (idlwave-sintern-class class)
8062 type (cond ((equal type "f") 'fun)
8063 ((equal type "p") 'pro)
8064 (t t)))
8065 (idlwave-do-find-module name type class nil this-buffer)))
8066
8067 (defun idlwave-do-find-module (name type class
8068 &optional force-source this-buffer)
8069 (let ((name1 (idlwave-make-full-name class name))
8070 source buf1 entry
8071 (buf (current-buffer))
8072 (pos (point))
8073 file name2)
8074 (setq entry (idlwave-best-rinfo-assq name type class (idlwave-routines)
8075 'WITH-FILE)
8076 source (or force-source (nth 3 entry))
8077 name2 (if (nth 2 entry)
8078 (idlwave-make-full-name (nth 2 entry) name)
8079 name1))
8080 (if source
8081 (setq file (idlwave-routine-source-file source)))
8082 (unless file ; Try to find it on the path.
8083 (setq file
8084 (idlwave-expand-lib-file-name
8085 (if class
8086 (format "%s__define.pro" (downcase class))
8087 (format "%s.pro" (downcase name))))))
8088 (cond
8089 ((or (null name) (equal name ""))
8090 (error "Abort"))
8091 ((eq (car source) 'system)
8092 (error "Source code for system routine %s is not available"
8093 name2))
8094 ((or (not file) (not (file-regular-p file)))
8095 (error "Source code for routine %s is not available"
8096 name2))
8097 (t
8098 (when (not this-buffer)
8099 (setq buf1
8100 (idlwave-find-file-noselect file 'find))
8101 (pop-to-buffer buf1 t))
8102 (goto-char (point-max))
8103 (let ((case-fold-search t))
8104 (if (re-search-backward
8105 (concat "^[ \t]*\\<"
8106 (cond ((eq type 'fun) "function")
8107 ((eq type 'pro) "pro")
8108 (t "\\(pro\\|function\\)"))
8109 "\\>[ \t]+"
8110 (regexp-quote (downcase name2))
8111 "[^a-zA-Z0-9_$]")
8112 nil t)
8113 (goto-char (match-beginning 0))
8114 (pop-to-buffer buf)
8115 (goto-char pos)
8116 (error "Could not find routine %s" name2)))))))
8117
8118 (defun idlwave-what-module ()
8119 "Return a default module for stuff near point.
8120 Used by `idlwave-routine-info' and `idlwave-find-module'."
8121 (idlwave-routines)
8122 (if (let ((case-fold-search t))
8123 (save-excursion
8124 (idlwave-beginning-of-statement)
8125 (looking-at "[ \t]*\\(pro\\|function\\)[ \t]+\\(\\([a-zA-Z0-9_$]+\\)::\\)?\\([a-zA-Z0-9$_]+\\)\\([, \t\n]\\|$\\)")))
8126 ;; This is a function or procedure definition statement
8127 ;; We return the defined routine as module.
8128 (list
8129 (idlwave-sintern-routine-or-method (match-string-no-properties 4)
8130 (match-string-no-properties 2))
8131 (if (equal (downcase (match-string 1)) "pro") 'pro 'fun)
8132 (idlwave-sintern-class (match-string 3)))
8133
8134 ;; Not a definition statement - analyze precise position.
8135 (let* ((where (idlwave-where))
8136 (cw (nth 2 where))
8137 (pro (car (nth 0 where)))
8138 (func (car (nth 1 where)))
8139 (this-word (idlwave-this-word "a-zA-Z0-9$_"))
8140 (next-char (save-excursion (skip-chars-forward "a-zA-Z0-9$_")
8141 (following-char)))
8142 )
8143 (cond
8144 ((and (eq cw 'procedure)
8145 (not (equal this-word "")))
8146 (setq this-word (idlwave-sintern-routine-or-method
8147 this-word (nth 2 (nth 3 where))))
8148 (list this-word 'pro
8149 (idlwave-determine-class
8150 (cons this-word (cdr (nth 3 where)))
8151 'pro)))
8152 ((and (eq cw 'function)
8153 (not (equal this-word ""))
8154 (or (eq next-char ?\() ; exclude arrays, vars.
8155 (looking-at "[a-zA-Z0-9_]*[ \t]*(")))
8156 (setq this-word (idlwave-sintern-routine-or-method
8157 this-word (nth 2 (nth 3 where))))
8158 (list this-word 'fun
8159 (idlwave-determine-class
8160 (cons this-word (cdr (nth 3 where)))
8161 'fun)))
8162 ((and (memq cw '(function-keyword procedure-keyword))
8163 (not (equal this-word ""))
8164 (eq next-char ?\()) ; A function!
8165 (setq this-word (idlwave-sintern-routine this-word))
8166 (list this-word 'fun nil))
8167 (func
8168 (list func 'fun (idlwave-determine-class (nth 1 where) 'fun)))
8169 (pro
8170 (list pro 'pro (idlwave-determine-class (nth 0 where) 'pro)))
8171 (t nil)))))
8172
8173 (defun idlwave-what-module-find-class ()
8174 "Call idlwave-what-module and find the inherited class if necessary."
8175 (let* ((module (idlwave-what-module))
8176 (class (nth 2 module))
8177 classes)
8178 (if (and (= (length module) 3)
8179 (stringp class))
8180 (list (car module)
8181 (nth 1 module)
8182 (apply 'idlwave-find-inherited-class module))
8183 module)))
8184
8185 (defun idlwave-find-inherited-class (name type class)
8186 "Find the class which defines TYPE NAME and is CLASS or inherited by CLASS."
8187 (let ((entry (idlwave-best-rinfo-assoc name type class (idlwave-routines))))
8188 (if entry
8189 (nth 2 entry)
8190 class)))
8191
8192 (defun idlwave-fix-module-if-obj_new (module)
8193 "Check if MODULE points to obj_new.
8194 If yes, and if the cursor is in the keyword region, change to the
8195 appropriate Init method."
8196 (let* ((name (car module))
8197 (pos (point))
8198 (case-fold-search t)
8199 string)
8200 (if (and (stringp name)
8201 (equal (downcase name) "obj_new")
8202 (save-excursion
8203 (idlwave-beginning-of-statement)
8204 (setq string (buffer-substring (point) pos))
8205 (string-match "obj_new([^'\"]*['\"]\\([a-zA-Z0-9_]+\\)"
8206 string)))
8207 (let ((name "Init")
8208 (class (match-string 1 string)))
8209 (setq module (list (idlwave-sintern-method "Init")
8210 'fun
8211 (idlwave-sintern-class class)))))
8212 module))
8213
8214 (defun idlwave-fix-keywords (name type class keywords
8215 &optional super-classes system)
8216 "Update a list of keywords.
8217 Translate OBJ_NEW, adding all super-class keywords, or all keywords
8218 from all classes if class equals t. If SYSTEM is non-nil, don't
8219 demand _EXTRA in the keyword list."
8220 (let ((case-fold-search t))
8221
8222 ;; If this is the OBJ_NEW function, try to figure out the class and use
8223 ;; the keywords from the corresponding INIT method.
8224 (if (and (equal (upcase name) "OBJ_NEW")
8225 (or (eq major-mode 'idlwave-mode)
8226 (eq major-mode 'idlwave-shell-mode)))
8227 (let* ((bos (save-excursion (idlwave-beginning-of-statement) (point)))
8228 (string (buffer-substring bos (point)))
8229 (case-fold-search t)
8230 class)
8231 (and (string-match "obj_new([^'\"]*['\"]\\([a-zA-Z0-9_]+\\)"
8232 string)
8233 (setq class (idlwave-sintern-class (match-string 1 string)))
8234 (setq idlwave-current-obj_new-class class)
8235 (setq keywords
8236 (append keywords
8237 (idlwave-entry-keywords
8238 (idlwave-rinfo-assq
8239 (idlwave-sintern-method "INIT")
8240 'fun
8241 class
8242 (idlwave-routines)) 'do-link))))))
8243
8244 ;; If the class is `t', combine all keywords of all methods NAME
8245 (when (eq class t)
8246 (mapc (lambda (entry)
8247 (and
8248 (nth 2 entry) ; non-nil class
8249 (eq (nth 1 entry) type) ; correct type
8250 (setq keywords
8251 (append keywords
8252 (idlwave-entry-keywords entry 'do-link)))))
8253 (idlwave-all-assq name (idlwave-routines)))
8254 (setq keywords (idlwave-uniquify keywords)))
8255
8256 ;; If we have inheritance, add all keywords from superclasses, if
8257 ;; the user indicated that method in `idlwave-keyword-class-inheritance'
8258 (when (and
8259 super-classes
8260 idlwave-keyword-class-inheritance
8261 (stringp class)
8262 (or
8263 system
8264 (assq (idlwave-sintern-keyword "_extra") keywords)
8265 (assq (idlwave-sintern-keyword "_ref_extra") keywords))
8266 ;; Check if one of the keyword-class regexps matches the name
8267 (let ((regexps idlwave-keyword-class-inheritance) re)
8268 (catch 'exit
8269 (while (setq re (pop regexps))
8270 (if (string-match re name) (throw 'exit t))))))
8271
8272 (loop for entry in (idlwave-routines) do
8273 (and (nth 2 entry) ; non-nil class
8274 (memq (nth 2 entry) super-classes) ; an inherited class
8275 (eq (nth 1 entry) type) ; correct type
8276 (eq (car entry) name) ; correct name
8277 (mapcar (lambda (k) (add-to-list 'keywords k))
8278 (idlwave-entry-keywords entry 'do-link))))
8279 (setq keywords (idlwave-uniquify keywords)))
8280
8281 ;; Return the final list
8282 keywords))
8283
8284 (defun idlwave-expand-keyword (keyword module)
8285 "Expand KEYWORD to one of the valid keyword parameters of MODULE.
8286 KEYWORD may be an exact match or an abbreviation of a keyword.
8287 If the match is exact, KEYWORD itself is returned, even if there may be other
8288 keywords of which KEYWORD is an abbreviation. This is necessary because some
8289 system routines have keywords which are prefixes of other keywords.
8290 If KEYWORD is an abbreviation of several keywords, a list of all possible
8291 completions is returned.
8292 If the abbreviation was unique, the correct keyword is returned.
8293 If it cannot be a keyword, the function return nil.
8294 If we do not know about MODULE, just return KEYWORD literally."
8295 (let* ((name (car module))
8296 (type (nth 1 module))
8297 (class (nth 2 module))
8298 (kwd (idlwave-sintern-keyword keyword))
8299 (entry (idlwave-best-rinfo-assoc name type class (idlwave-routines)))
8300 (kwd-alist (idlwave-entry-keywords entry))
8301 (extra (or (assq (idlwave-sintern-keyword "_EXTRA") kwd-alist)
8302 (assq (idlwave-sintern-keyword "_REF_EXTRA") kwd-alist)))
8303 (completion-ignore-case t)
8304 candidates)
8305 (cond ((assq kwd kwd-alist)
8306 kwd)
8307 ((setq candidates (all-completions kwd kwd-alist))
8308 (if (= (length candidates) 1)
8309 (car candidates)
8310 candidates))
8311 ((and entry extra)
8312 ;; Inheritance may cause this keyword to be correct
8313 keyword)
8314 (entry
8315 ;; We do know the function, which does not have the keyword.
8316 nil)
8317 (t
8318 ;; We do not know the function, so this just might be a correct
8319 ;; keyword - return it as it is.
8320 keyword))))
8321
8322 (defvar idlwave-rinfo-mouse-map (make-sparse-keymap))
8323 (defvar idlwave-rinfo-map (make-sparse-keymap))
8324 (define-key idlwave-rinfo-mouse-map
8325 (if (featurep 'xemacs) [button2] [mouse-2])
8326 'idlwave-mouse-active-rinfo)
8327 (define-key idlwave-rinfo-mouse-map
8328 (if (featurep 'xemacs) [(shift button2)] [(shift mouse-2)])
8329 'idlwave-mouse-active-rinfo-shift)
8330 (define-key idlwave-rinfo-mouse-map
8331 (if (featurep 'xemacs) [button3] [mouse-3])
8332 'idlwave-mouse-active-rinfo-right)
8333 (define-key idlwave-rinfo-mouse-map " " 'idlwave-active-rinfo-space)
8334 (define-key idlwave-rinfo-map "q" 'idlwave-quit-help)
8335 (define-key idlwave-rinfo-mouse-map "q" 'idlwave-quit-help)
8336 (defvar idlwave-popup-source nil)
8337 (defvar idlwave-rinfo-marker (make-marker))
8338
8339 (defun idlwave-quit-help ()
8340 (interactive)
8341 (let ((ri-window (get-buffer-window "*Help*"))
8342 (olh-window (get-buffer-window "*IDLWAVE Help*")))
8343 (when (and olh-window
8344 (fboundp 'idlwave-help-quit))
8345 (select-window olh-window)
8346 (idlwave-help-quit))
8347 (when (window-live-p ri-window)
8348 (delete-window ri-window))))
8349
8350 (defun idlwave-display-calling-sequence (name type class
8351 &optional initial-class)
8352 ;; Display the calling sequence of module NAME, type TYPE in class CLASS.
8353 (let* ((initial-class (or initial-class class))
8354 (entry (or (idlwave-best-rinfo-assq name type class
8355 (idlwave-routines))
8356 (idlwave-rinfo-assq name type class
8357 idlwave-unresolved-routines)))
8358 (name (or (car entry) name))
8359 (class (or (nth 2 entry) class))
8360 (superclasses (idlwave-all-class-inherits initial-class))
8361 (twins (idlwave-routine-twins entry))
8362 (dtwins (idlwave-study-twins twins))
8363 (all dtwins)
8364 (system (eq (car (nth 3 entry)) 'system))
8365 (calling-seq (nth 4 entry))
8366 (keywords (idlwave-entry-keywords entry 'do-link))
8367 (html-file (car (nth 5 entry)))
8368 (help-echo-kwd
8369 "Button2: Insert KEYWORD (SHIFT=`/KEYWORD') | Button3: Online Help ")
8370 (help-echo-use
8371 "Button2/3: Online Help")
8372 (help-echo-src
8373 "Button2: Jump to source and back | Button3: Source in Help window.")
8374 (help-echo-class
8375 "Button2: Display info about same method in superclass")
8376 (col 0)
8377 (data (list name type class (current-buffer) nil initial-class))
8378 (km-prop (if (featurep 'xemacs) 'keymap 'local-map))
8379 (face 'idlwave-help-link)
8380 beg props win cnt total)
8381 ;; Fix keywords, but don't add chained super-classes, since these
8382 ;; are shown separately for that super-class
8383 (setq keywords (idlwave-fix-keywords name type class keywords))
8384 (cond
8385 ((null entry)
8386 (error "No %s %s known %s" type name
8387 (if initial-class (concat "in class " initial-class) "")))
8388 ((or (null name) (equal name ""))
8389 (error "No function or procedure call at point"))
8390 ((null calling-seq)
8391 (error "Calling sequence of %s %s not available" type name))
8392 (t
8393 (save-excursion
8394 (move-marker idlwave-rinfo-marker (point))
8395 (set-buffer (get-buffer-create "*Help*"))
8396 (use-local-map idlwave-rinfo-map)
8397 (setq buffer-read-only nil)
8398 (erase-buffer)
8399 (set (make-local-variable 'idlwave-popup-source) nil)
8400 (set (make-local-variable 'idlwave-current-obj_new-class)
8401 idlwave-current-obj_new-class)
8402 (when superclasses
8403 (setq props (list 'mouse-face 'highlight
8404 km-prop idlwave-rinfo-mouse-map
8405 'help-echo help-echo-class
8406 'data (cons 'class data)))
8407 (let ((classes (cons initial-class superclasses)) c)
8408 (insert "Classes: ")
8409 (while (setq c (pop classes))
8410 (insert " ")
8411 (setq beg (point))
8412 (insert c)
8413 (if (equal (downcase c) (downcase class))
8414 (add-text-properties beg (point) (list 'face 'bold))
8415 ;; If Method exists in a different class link it
8416 (if (idlwave-rinfo-assq name type c (idlwave-routines))
8417 (add-text-properties beg (point) props))))
8418 (insert "\n")))
8419 (setq props (list 'mouse-face 'highlight
8420 km-prop idlwave-rinfo-mouse-map
8421 'help-echo help-echo-use
8422 'data (cons 'usage data)))
8423 (if html-file (setq props (append (list 'face face 'link html-file)
8424 props)))
8425 (insert "Usage: ")
8426 (setq beg (point))
8427 (insert (if class
8428 (format calling-seq class name class name class name)
8429 (format calling-seq name name name name))
8430 "\n")
8431 (add-text-properties beg (point) props)
8432
8433 (insert "Keywords:")
8434 (if (null keywords)
8435 (insert " No keywords accepted.")
8436 (setq col 9)
8437 (mapcar
8438 (lambda (x)
8439 (if (>= (+ col 1 (length (car x)))
8440 (window-width))
8441 (progn
8442 (insert "\n ")
8443 (setq col 9)))
8444 (insert " ")
8445 (setq beg (point)
8446 ;; Relevant keywords already have link property attached
8447 props (list 'mouse-face 'highlight
8448 km-prop idlwave-rinfo-mouse-map
8449 'data (cons 'keyword data)
8450 'help-echo help-echo-kwd
8451 'keyword (car x)))
8452 (if system (setq props (append (list 'face face) props)))
8453 (insert (car x))
8454 (add-text-properties beg (point) props)
8455 (setq col (+ col 1 (length (car x)))))
8456 keywords))
8457
8458 (setq cnt 1 total (length all))
8459 ;; Here entry is (key file (list of type-conses))
8460 (while (setq entry (pop all))
8461 (setq props (list 'mouse-face 'highlight
8462 km-prop idlwave-rinfo-mouse-map
8463 'help-echo help-echo-src
8464 'source (list (car (car (nth 2 entry))) ;type
8465 (nth 1 entry)
8466 nil
8467 (cdr (car (nth 2 entry))))
8468 'data (cons 'source data)))
8469 (idlwave-insert-source-location
8470 (format "\n%-8s %s"
8471 (if (equal cnt 1)
8472 (if (> total 1) "Sources:" "Source:")
8473 "")
8474 (if (> total 1) "- " ""))
8475 entry props)
8476 (incf cnt)
8477 (when (and all (> cnt idlwave-rinfo-max-source-lines))
8478 ;; No more source lines, please
8479 (insert (format
8480 "\n Source information truncated to %d entries."
8481 idlwave-rinfo-max-source-lines))
8482 (setq all nil)))
8483 (goto-char (point-min))
8484 (setq buffer-read-only t))
8485 (display-buffer "*Help*")
8486 (if (and (setq win (get-buffer-window "*Help*"))
8487 idlwave-resize-routine-help-window)
8488 (progn
8489 (let ((ww (selected-window)))
8490 (unwind-protect
8491 (progn
8492 (select-window win)
8493 (enlarge-window (- (/ (frame-height) 2)
8494 (window-height)))
8495 (shrink-window-if-larger-than-buffer))
8496 (select-window ww)))))))))
8497
8498 (defun idlwave-insert-source-location (prefix entry &optional file-props)
8499 "Insert a source location into the routine info buffer.
8500 Start line with PREFIX. If a file name is inserted, add FILE-PROPS to
8501 it."
8502 (let* ((key (car entry))
8503 (file (nth 1 entry))
8504 (types (nth 2 entry))
8505 (shell-flag (assq 'compiled types))
8506 (buffer-flag (assq 'buffer types))
8507 (user-flag (assq 'user types))
8508 (lib-flag (assq 'lib types))
8509 (ndupl (or (and buffer-flag (idlwave-count-memq 'buffer types))
8510 (and user-flag (idlwave-count-memq 'user types))
8511 (and lib-flag (idlwave-count-memq 'lib types))
8512 1))
8513 (doflags t)
8514 beg special)
8515
8516 (insert prefix)
8517
8518 (cond
8519 ((eq key 'system)
8520 (setq doflags nil)
8521 (insert "System "))
8522
8523 ((eq key 'builtin)
8524 (setq doflags nil)
8525 (insert "Builtin "))
8526
8527 ((and (not file) shell-flag)
8528 (insert "Unresolved"))
8529
8530 ((null file)
8531 (insert "ERROR"))
8532
8533 ((idlwave-syslib-p file)
8534 (if (string-match "obsolete" (file-name-directory file))
8535 (insert "Obsolete ")
8536 (insert "SystemLib ")))
8537
8538 ;; New special syntax: taken directly from routine-info for
8539 ;; library catalog routines
8540 ((setq special (or (cdr lib-flag) (cdr user-flag)))
8541 (insert (format "%-10s" special)))
8542
8543 ;; Old special syntax: a matching regexp
8544 ((setq special (idlwave-special-lib-test file))
8545 (insert (format "%-10s" special)))
8546
8547 ;; Catch-all with file
8548 ((idlwave-lib-p file) (insert "Library "))
8549
8550 ;; Sanity catch all
8551 (t (insert "Other ")))
8552
8553 (when doflags
8554 (insert (concat
8555 " ["
8556 (if lib-flag "L" "-")
8557 (if user-flag "C" "-")
8558 (if shell-flag "S" "-")
8559 (if buffer-flag "B" "-")
8560 "] ")))
8561 (when (> ndupl 1)
8562 (setq beg (point))
8563 (insert (format "(%dx) " ndupl))
8564 (add-text-properties beg (point) (list 'face 'bold)))
8565 (when (and file (not (equal file "")))
8566 (setq beg (point))
8567 (insert (apply 'abbreviate-file-name
8568 (if (featurep 'xemacs) (list file t) (list file))))
8569 (if file-props
8570 (add-text-properties beg (point) file-props)))))
8571
8572 (defun idlwave-special-lib-test (file)
8573 "Check the path of FILE against the regexps which define special libs.
8574 Return the name of the special lib if there is a match."
8575 (let ((alist idlwave-special-lib-alist)
8576 entry rtn)
8577 (cond
8578 ((stringp file)
8579 (while (setq entry (pop alist))
8580 (if (string-match (car entry) file)
8581 (setq rtn (cdr entry)
8582 alist nil)))
8583 rtn)
8584 (t nil))))
8585
8586 (defun idlwave-mouse-active-rinfo-right (ev)
8587 (interactive "e")
8588 (idlwave-mouse-active-rinfo ev 'right))
8589
8590 (defun idlwave-mouse-active-rinfo-shift (ev)
8591 (interactive "e")
8592 (idlwave-mouse-active-rinfo ev nil 'shift))
8593
8594 (defun idlwave-active-rinfo-space ()
8595 (interactive)
8596 (idlwave-mouse-active-rinfo nil 'right))
8597
8598 (defun idlwave-mouse-active-rinfo (ev &optional right shift)
8599 "Does the mouse actions in the routine info buffer.
8600 Optional args RIGHT and SHIFT indicate, if mouse-3 was used, and if SHIFT
8601 was pressed."
8602 (interactive "e")
8603 (if ev (mouse-set-point ev))
8604 (let (data id name type class buf bufwin source link keyword
8605 word initial-class)
8606 (setq data (get-text-property (point) 'data)
8607 source (get-text-property (point) 'source)
8608 keyword (get-text-property (point) 'keyword)
8609 link (get-text-property (point) 'link)
8610 id (car data)
8611 name (nth 1 data) type (nth 2 data) class (nth 3 data)
8612 buf (nth 4 data)
8613 initial-class (nth 6 data)
8614 word (idlwave-this-word)
8615 bufwin (get-buffer-window buf t))
8616
8617 (cond ((eq id 'class) ; Switch class being displayed
8618 (if (window-live-p bufwin) (select-window bufwin))
8619 (idlwave-display-calling-sequence
8620 (idlwave-sintern-method name)
8621 type (idlwave-sintern-class word)
8622 initial-class))
8623 ((eq id 'usage) ; Online help on this routine
8624 (idlwave-online-help link name type class))
8625 ((eq id 'source) ; Source in help or buffer
8626 (if right ; In help
8627 (let ((idlwave-extra-help-function 'idlwave-help-with-source)
8628 (idlwave-help-source-try-header nil)
8629 ;; Fake idlwave-routines so help will find the right entry
8630 (idlwave-routines
8631 (list (list name type class source ""))))
8632 (idlwave-help-get-special-help name type class nil))
8633 ;; Otherwise just pop to the source
8634 (setq idlwave-popup-source (not idlwave-popup-source))
8635 (if idlwave-popup-source
8636 (condition-case err
8637 (idlwave-do-find-module name type class source)
8638 (error
8639 (setq idlwave-popup-source nil)
8640 (if (window-live-p bufwin) (select-window bufwin))
8641 (error (nth 1 err))))
8642 (if bufwin
8643 (select-window bufwin)
8644 (pop-to-buffer buf))
8645 (goto-char (marker-position idlwave-rinfo-marker)))))
8646 ((eq id 'keyword)
8647 (if right
8648 (idlwave-online-help link name type class keyword)
8649 (idlwave-rinfo-insert-keyword keyword buf shift))))))
8650
8651 (defun idlwave-rinfo-insert-keyword (keyword buffer &optional shift)
8652 "Insert KEYWORD in BUFFER. Make sure buffer is displayed in a window."
8653 (let ((bwin (get-buffer-window buffer)))
8654 (if idlwave-complete-empty-string-as-lower-case
8655 (setq keyword (downcase keyword)))
8656 (if bwin
8657 (select-window bwin)
8658 (pop-to-buffer buffer)
8659 (setq bwin (get-buffer-window buffer)))
8660 (if (eq (preceding-char) ?/)
8661 (insert keyword)
8662 (unless (save-excursion
8663 (re-search-backward
8664 "[(,][ \t]*\\(\\$[ \t]*\\(;.*\\)?\n\\)?[ \t]*\\="
8665 (min (- (point) 100) (point-min)) t))
8666 (insert ", "))
8667 (if shift (insert "/"))
8668 (insert keyword)
8669 (if (and (not shift)
8670 idlwave-keyword-completion-adds-equal)
8671 (insert "=")))))
8672
8673 (defun idlwave-list-buffer-load-path-shadows (&optional arg)
8674 "List the load path shadows of all routines defined in current buffer."
8675 (interactive "P")
8676 (idlwave-routines)
8677 (if (eq major-mode 'idlwave-mode)
8678 (idlwave-list-load-path-shadows
8679 nil (idlwave-update-current-buffer-info 'save-buffer)
8680 "in current buffer")
8681 (error "Current buffer is not in idlwave-mode")))
8682
8683 (defun idlwave-list-shell-load-path-shadows (&optional arg)
8684 "List the load path shadows of all routines compiled under the shell.
8685 This is very useful for checking an IDL application. Just compile the
8686 application, do RESOLVE_ALL, and `C-c C-i' to compile all referenced
8687 routines and update IDLWAVE internal info. Then check for shadowing
8688 with this command."
8689 (interactive "P")
8690 (cond
8691 ((or (not (fboundp 'idlwave-shell-is-running))
8692 (not (idlwave-shell-is-running)))
8693 (error "Shell is not running"))
8694 ((null idlwave-compiled-routines)
8695 (error "No compiled routines. Maybe you need to update with `C-c C-i'"))
8696 (t
8697 (idlwave-list-load-path-shadows nil idlwave-compiled-routines
8698 "in the shell"))))
8699
8700 (defun idlwave-list-all-load-path-shadows (&optional arg)
8701 "List the load path shadows of all routines known to IDLWAVE."
8702 (interactive "P")
8703 (idlwave-list-load-path-shadows nil nil "globally"))
8704
8705 (defun idlwave-list-load-path-shadows (arg &optional special-routines loc)
8706 "List the routines which are defined multiple times.
8707 Search the information IDLWAVE has about IDL routines for multiple
8708 definitions.
8709 When SPECIAL-ROUTINES in non-nil, only look for shadows of these routines.
8710
8711 When IDL hits a routine call which is not defined, it will search on
8712 the load path in order to find a definition. The output of this
8713 command can be used to detect possible name clashes during this process."
8714 (idlwave-routines) ; Make sure everything is loaded.
8715 (unless (or idlwave-user-catalog-routines idlwave-library-catalog-routines)
8716 (or (y-or-n-p
8717 "You don't have any user or library catalogs. Continue anyway? ")
8718 (error "Abort")))
8719 (let* ((routines (append idlwave-system-routines
8720 idlwave-compiled-routines
8721 idlwave-library-catalog-routines
8722 idlwave-user-catalog-routines
8723 idlwave-buffer-routines
8724 nil))
8725 (km-prop (if (featurep 'xemacs) 'keymap 'local-map))
8726 (keymap (make-sparse-keymap))
8727 (props (list 'mouse-face 'highlight
8728 km-prop keymap
8729 'help-echo "Mouse2: Find source"))
8730 (nroutines (length (or special-routines routines)))
8731 (step (/ nroutines 100))
8732 (n 0)
8733 (cnt 0)
8734 (idlwave-sort-prefer-buffer-info nil)
8735 routine twins dtwins twin done props1 lroutines)
8736
8737 (if special-routines
8738 ;; Just looking for shadows of a few special routines
8739 (setq lroutines routines
8740 routines special-routines))
8741
8742 (message "Sorting routines...")
8743 (setq routines (sort routines
8744 (lambda (a b)
8745 (string< (downcase (idlwave-make-full-name
8746 (nth 2 a) (car a)))
8747 (downcase (idlwave-make-full-name
8748 (nth 2 b) (car b)))))))
8749 (message "Sorting routines...done")
8750
8751 (define-key keymap (if (featurep 'xemacs) [(button2)] [(mouse-2)])
8752 (lambda (ev)
8753 (interactive "e")
8754 (mouse-set-point ev)
8755 (apply 'idlwave-do-find-module
8756 (get-text-property (point) 'find-args))))
8757 (define-key keymap [(return)]
8758 (lambda ()
8759 (interactive)
8760 (apply 'idlwave-do-find-module
8761 (get-text-property (point) 'find-args))))
8762 (message "Compiling list...( 0%%)")
8763 (save-excursion
8764 (set-buffer (get-buffer-create "*Shadows*"))
8765 (setq buffer-read-only nil)
8766 (erase-buffer)
8767 (while (setq routine (pop routines))
8768 (if (= (mod (setq n (1+ n)) step) 0)
8769 (message "Compiling list...(%2d%%)" (/ (* n 100) nroutines)))
8770
8771 ;; Get a list of all twins
8772 (setq twins (idlwave-routine-twins routine (or lroutines routines)))
8773 (if (memq routine done)
8774 (setq dtwins nil)
8775 (setq dtwins (idlwave-study-twins twins)))
8776 ;; Mark all twins as dealt with
8777 (setq done (append twins done))
8778 (when (or (> (length dtwins) 1)
8779 (> (idlwave-count-memq 'lib (nth 2 (car dtwins))) 1)
8780 (> (idlwave-count-memq 'user (nth 2 (car dtwins))) 1)
8781 (> (idlwave-count-memq 'buffer (nth 2 (car dtwins))) 1))
8782 (incf cnt)
8783 (insert (format "\n%s%s"
8784 (idlwave-make-full-name (nth 2 routine)
8785 (car routine))
8786 (if (eq (nth 1 routine) 'fun) "()" "")))
8787 (while (setq twin (pop dtwins))
8788 (setq props1 (append (list 'find-args
8789 (list (nth 0 routine)
8790 (nth 1 routine)
8791 (nth 2 routine)))
8792 props))
8793 (idlwave-insert-source-location "\n - " twin props1))))
8794 (goto-char (point-min))
8795 (setq buffer-read-only t))
8796 (setq loc (or loc ""))
8797 (if (> cnt 0)
8798 (progn
8799 (display-buffer (get-buffer "*Shadows*"))
8800 (message "%d case%s of shadowing found %s"
8801 cnt (if (= cnt 1) "" "s") loc))
8802 (message "No shadowing conflicts found %s" loc))))
8803
8804 (defun idlwave-print-source (routine)
8805 (let* ((source (nth 3 routine))
8806 (stype (car source))
8807 (sfile (idlwave-routine-source-file source)))
8808 (if (idlwave-syslib-p sfile) (setq stype 'syslib))
8809 (if (and (eq stype 'compiled)
8810 (or (not (stringp sfile))
8811 (not (string-match "\\S-" sfile))))
8812 (setq stype 'unresolved))
8813 (princ (format " %-10s %s\n"
8814 stype
8815 (if sfile sfile "No source code available")))))
8816
8817 (defun idlwave-routine-twins (entry &optional list)
8818 "Return all twin entries of ENTRY in LIST.
8819 LIST defaults to `idlwave-routines'.
8820 Twin entries are those which have the same name, type, and class.
8821 ENTRY will also be returned, as the first item of this list."
8822 (let* ((name (car entry))
8823 (type (nth 1 entry))
8824 (class (nth 2 entry))
8825 (candidates (idlwave-all-assq name (or list (idlwave-routines))))
8826 twins candidate)
8827 (while (setq candidate (pop candidates))
8828 (if (and (not (eq candidate entry))
8829 (eq type (nth 1 candidate))
8830 (eq class (nth 2 candidate)))
8831 (push candidate twins)))
8832 (if (setq candidate (idlwave-rinfo-assq name type class
8833 idlwave-unresolved-routines))
8834 (push candidate twins))
8835 (cons entry (nreverse twins))))
8836
8837 (defun idlwave-study-twins (entries)
8838 "Return dangerous twins of first entry in ENTRIES.
8839 Dangerous twins are routines with same name, but in different files on
8840 the load path. If a file is in the system library and has an entry in
8841 the `idlwave-system-routines' list, we omit the latter as
8842 non-dangerous because many IDL routines are implemented as library
8843 routines, and may have been scanned."
8844 (let* ((entry (car entries))
8845 (name (car entry)) ;
8846 (type (nth 1 entry)) ; Must be bound for
8847 (class (nth 2 entry)) ; idlwave-routine-twin-compare
8848 (cnt 0)
8849 source type type-cons file alist syslibp key)
8850 (while (setq entry (pop entries))
8851 (incf cnt)
8852 (setq source (nth 3 entry)
8853 type (car source)
8854 type-cons (cons type (nth 3 source))
8855 file (idlwave-routine-source-file source))
8856
8857 ;; Make KEY to index entry properly
8858 (setq key (cond ((eq type 'system) type)
8859 (file (file-truename file))
8860 (t 'unresolved)))
8861
8862 ;; Check for an entry in the system library
8863 (if (and file
8864 (not syslibp)
8865 (idlwave-syslib-p file))
8866 (setq syslibp t))
8867
8868 ;; If there's more than one matching entry for the same file, just
8869 ;; append the type-cons to the type list.
8870 (if (setq entry (assoc key alist))
8871 (push type-cons (nth 2 entry))
8872 (push (list key file (list type-cons)) alist)))
8873
8874 (setq alist (nreverse alist))
8875
8876 (when syslibp
8877 ;; File is in system *library* - remove any 'system entry
8878 (setq alist (delq (assq 'system alist) alist)))
8879
8880 ;; If 'system remains and we've scanned the syslib, it's a builtin
8881 ;; (rather than a !DIR/lib/.pro file bundled as source).
8882 (when (and (idlwave-syslib-scanned-p)
8883 (setq entry (assoc 'system alist)))
8884 (setcar entry 'builtin))
8885 (sort alist 'idlwave-routine-twin-compare)))
8886
8887 (defvar type)
8888 (defvar class)
8889 (defvar idlwave-sort-prefer-buffer-info t
8890 "Internal variable used to influence `idlwave-routine-twin-compare'.")
8891
8892 (defmacro idlwave-xor (a b)
8893 `(and (or ,a ,b)
8894 (not (and ,a ,b))))
8895
8896 (defun idlwave-routine-entry-compare (a b)
8897 "Compare two routine info entries for sortiung. This is the general case.
8898 It first compates class, names, and type. If it turns out that A and B
8899 are twins (same name, class, and type), calls another routine which
8900 compares twins on the basis of their file names and path locations."
8901 (let ((name (car a)) (type (nth 1 a)) (class (nth 2 a)))
8902 (cond
8903 ((not (equal (idlwave-downcase-safe class)
8904 (idlwave-downcase-safe (nth 2 b))))
8905 ;; Class decides
8906 (cond ((null (nth 2 b)) nil)
8907 ((null class) t)
8908 (t (string< (downcase class) (downcase (nth 2 b))))))
8909 ((not (equal (downcase name) (downcase (car b))))
8910 ;; Name decides
8911 (string< (downcase name) (downcase (car b))))
8912 ((not (eq type (nth 1 b)))
8913 ;; Type decides
8914 (< (if (eq type 'fun) 1 0) (if (eq (nth 1 b) 'fun) 1 0)))
8915 (t
8916 ;; A and B are twins - so the decision is more complicated.
8917 ;; Call twin-compare with the proper arguments.
8918 (idlwave-routine-entry-compare-twins a b)))))
8919
8920 (defun idlwave-routine-entry-compare-twins (a b)
8921 "Compare two routine entries, under the assumption that they are
8922 twins. This basically calls `idlwave-routine-twin-compare' with the
8923 correct args."
8924 (let* ((name (car a)) (type (nth 1 a)) (class (nth 2 a)) ; needed outside
8925 (asrc (nth 3 a))
8926 (atype (car asrc))
8927 (bsrc (nth 3 b))
8928 (btype (car bsrc))
8929 (afile (idlwave-routine-source-file asrc))
8930 (bfile (idlwave-routine-source-file bsrc)))
8931 (idlwave-routine-twin-compare
8932 (if (stringp afile)
8933 (list (file-truename afile) afile (list atype))
8934 (list atype afile (list atype)))
8935 (if (stringp bfile)
8936 (list (file-truename bfile) bfile (list btype))
8937 (list btype bfile (list btype))))
8938 ))
8939
8940 (defun idlwave-routine-twin-compare (a b)
8941 "Compare two routine twin entries for sorting.
8942 In here, A and B are not normal routine info entries, but special
8943 lists (KEY FILENAME (TYPES...)).
8944 This expects NAME TYPE CLASS to be bound to the right values."
8945 (let* (;; Dis-assemble entries
8946 (akey (car a)) (bkey (car b))
8947 (afile (nth 1 a)) (bfile (nth 1 b))
8948 (atypes (nth 2 a)) (btypes (nth 2 b))
8949 ;; System routines?
8950 (asysp (memq akey '(builtin system)))
8951 (bsysp (memq bkey '(builtin system)))
8952 ;; Compiled routines?
8953 (acompp (memq 'compiled atypes))
8954 (bcompp (memq 'compiled btypes))
8955 ;; Unresolved?
8956 (aunresp (or (eq akey 'unresolved)
8957 (and acompp (not afile))))
8958 (bunresp (or (eq bkey 'unresolved)
8959 (and bcompp (not bfile))))
8960 ;; Buffer info available?
8961 (abufp (memq 'buffer atypes))
8962 (bbufp (memq 'buffer btypes))
8963 ;; On search path?
8964 (tpath-alist (idlwave-true-path-alist))
8965 (apathp (and (stringp akey)
8966 (assoc (file-name-directory akey) tpath-alist)))
8967 (bpathp (and (stringp bkey)
8968 (assoc (file-name-directory bkey) tpath-alist)))
8969 ;; How early on search path? High number means early since we
8970 ;; measure the tail of the path list
8971 (anpath (length (memq apathp tpath-alist)))
8972 (bnpath (length (memq bpathp tpath-alist)))
8973 ;; Look at file names
8974 (aname (if (stringp afile) (downcase (file-name-nondirectory afile)) ""))
8975 (bname (if (stringp bfile) (downcase (file-name-nondirectory bfile)) ""))
8976 (fname-re (if class (format "\\`%s__\\(%s\\|define\\)\\.pro\\'"
8977 (regexp-quote (downcase class))
8978 (regexp-quote (downcase name)))
8979 (format "\\`%s\\.pro" (regexp-quote (downcase name)))))
8980 ;; Is file name derived from the routine name?
8981 ;; Method file or class definition file?
8982 (anamep (string-match fname-re aname))
8983 (adefp (and class anamep (string= "define" (match-string 1 aname))))
8984 (bnamep (string-match fname-re bname))
8985 (bdefp (and class bnamep (string= "define" (match-string 1 bname)))))
8986
8987 ;; Now: follow JD's ideas about sorting. Looks really simple now,
8988 ;; doesn't it? The difficult stuff is hidden above...
8989 (cond
8990 ((idlwave-xor asysp bsysp) asysp) ; System entries first
8991 ((idlwave-xor aunresp bunresp) bunresp) ; Unresolved last
8992 ((and idlwave-sort-prefer-buffer-info
8993 (idlwave-xor abufp bbufp)) abufp) ; Buffers before non-buffers
8994 ((idlwave-xor acompp bcompp) acompp) ; Compiled entries
8995 ((idlwave-xor apathp bpathp) apathp) ; Library before non-library
8996 ((idlwave-xor anamep bnamep) anamep) ; Correct file names first
8997 ((and class anamep bnamep ; both file names match ->
8998 (idlwave-xor adefp bdefp)) bdefp) ; __define after __method
8999 ((> anpath bnpath) t) ; Who is first on path?
9000 (t nil)))) ; Default
9001
9002 (defun idlwave-routine-source-file (source)
9003 (if (nth 2 source)
9004 (expand-file-name (nth 1 source) (nth 2 source))
9005 (nth 1 source)))
9006
9007 (defun idlwave-downcase-safe (string)
9008 "Donwcase if string, else return unchanged."
9009 (if (stringp string)
9010 (downcase string)
9011 string))
9012
9013 (defun idlwave-count-eq (elt list)
9014 "How often is ELT in LIST?"
9015 (length (delq nil (mapcar (lambda (x) (eq x elt)) list))))
9016
9017 (defun idlwave-count-memq (elt alist)
9018 "How often is ELT a key in ALIST?"
9019 (length (delq nil (mapcar (lambda (x) (eq (car x) elt)) alist))))
9020
9021 (defun idlwave-syslib-p (file)
9022 "Non-nil if FILE is in the system library."
9023 (let* ((true-syslib (file-name-as-directory
9024 (file-truename
9025 (expand-file-name "lib" (idlwave-sys-dir)))))
9026 (true-file (file-truename file)))
9027 (string-match (concat "^" (regexp-quote true-syslib)) true-file)))
9028
9029 (defun idlwave-lib-p (file)
9030 "Non-nil if file is in the library"
9031 (let ((true-dir (file-name-directory (file-truename file))))
9032 (assoc true-dir (idlwave-true-path-alist))))
9033
9034 (defun idlwave-path-alist-add-flag (list-entry flag)
9035 "Add a flag to the path list entry, if not set."
9036 (let ((flags (cdr list-entry)))
9037 (add-to-list 'flags flag)
9038 (setcdr list-entry flags)))
9039
9040 (defun idlwave-path-alist-remove-flag (list-entry flag)
9041 "Remove a flag to the path list entry, if set."
9042 (let ((flags (delq flag (cdr list-entry))))
9043 (setcdr list-entry flags)))
9044
9045 (defun idlwave-true-path-alist ()
9046 "Return `idlwave-path-alist' alist with true-names.
9047 Info is cached, but relies on the functions setting `idlwave-path-alist'
9048 to reset the variable `idlwave-true-path-alist' to nil."
9049 (or idlwave-true-path-alist
9050 (setq idlwave-true-path-alist
9051 (mapcar (lambda(x) (cons
9052 (file-name-as-directory
9053 (file-truename
9054 (directory-file-name
9055 (car x))))
9056 (cdr x)))
9057 idlwave-path-alist))))
9058
9059 (defun idlwave-syslib-scanned-p ()
9060 "Non-nil if the system lib file !DIR/lib has been scanned."
9061 (let* ((true-syslib (file-name-as-directory
9062 (file-truename
9063 (expand-file-name "lib" (idlwave-sys-dir))))))
9064 (cdr (assoc true-syslib (idlwave-true-path-alist)))))
9065
9066 ;; ----------------------------------------------------------------------------
9067 ;;
9068 ;; Online Help display
9069
9070
9071 ;; ----------------------------------------------------------------------------
9072 ;;
9073 ;; Additions for use with imenu.el and func-menu.el
9074 ;; (pop-up a list of IDL units in the current file).
9075 ;;
9076
9077 (defun idlwave-prev-index-position ()
9078 "Search for the previous procedure or function.
9079 Return nil if not found. For use with imenu.el."
9080 (save-match-data
9081 (cond
9082 ((idlwave-find-key "\\<\\(pro\\|function\\)\\>" -1 'nomark))
9083 ;; ((idlwave-find-key idlwave-begin-unit-reg 1 'nomark)
9084 (t nil))))
9085
9086 (defun idlwave-unit-name ()
9087 "Return the unit name.
9088 Assumes that point is at the beginning of the unit as found by
9089 `idlwave-prev-index-position'."
9090 (forward-sexp 2)
9091 (forward-sexp -1)
9092 (let ((begin (point)))
9093 (re-search-forward
9094 "[a-zA-Z_][a-zA-Z0-9$_]+\\(::[a-zA-Z_][a-zA-Z0-9$_]+\\)?")
9095 (if (fboundp 'buffer-substring-no-properties)
9096 (buffer-substring-no-properties begin (point))
9097 (buffer-substring begin (point)))))
9098
9099 (defalias 'idlwave-function-menu
9100 (condition-case nil
9101 (progn
9102 (require 'func-menu)
9103 'function-menu)
9104 (error (condition-case nil
9105 (progn
9106 (require 'imenu)
9107 'imenu)
9108 (error nil)))))
9109
9110 ;; Here we hack func-menu.el in order to support this new mode.
9111 ;; The latest versions of func-menu.el already have this stuff in, so
9112 ;; we hack only if it is not already there.
9113 (when (fboundp 'eval-after-load)
9114 (eval-after-load "func-menu"
9115 '(progn
9116 (or (assq 'idlwave-mode fume-function-name-regexp-alist)
9117 (not (boundp 'fume-function-name-regexp-idl)) ; avoid problems
9118 (setq fume-function-name-regexp-alist
9119 (cons '(idlwave-mode . fume-function-name-regexp-idl)
9120 fume-function-name-regexp-alist)))
9121 (or (assq 'idlwave-mode fume-find-function-name-method-alist)
9122 (not (fboundp 'fume-find-next-idl-function-name)) ; avoid problems
9123 (setq fume-find-function-name-method-alist
9124 (cons '(idlwave-mode . fume-find-next-idl-function-name)
9125 fume-find-function-name-method-alist))))))
9126
9127 (defun idlwave-edit-in-idlde ()
9128 "Edit the current file in IDL Development environment."
9129 (interactive)
9130 (start-process "idldeclient" nil
9131 idlwave-shell-explicit-file-name "-c" "-e"
9132 (buffer-file-name)))
9133
9134 (defvar idlwave-help-use-assistant)
9135 (defun idlwave-launch-idlhelp ()
9136 "Start the IDLhelp application."
9137 (interactive)
9138 (if idlwave-help-use-assistant
9139 (idlwave-help-assistant-raise)
9140 (start-process "idlhelp" nil idlwave-help-application)))
9141
9142 ;; Menus - using easymenu.el
9143 (defvar idlwave-mode-menu-def
9144 `("IDLWAVE"
9145 ["PRO/FUNC menu" idlwave-function-menu t]
9146 ("Motion"
9147 ["Subprogram Start" idlwave-beginning-of-subprogram t]
9148 ["Subprogram End" idlwave-end-of-subprogram t]
9149 ["Block Start" idlwave-beginning-of-block t]
9150 ["Block End" idlwave-end-of-block t]
9151 ["Up Block" idlwave-backward-up-block t]
9152 ["Down Block" idlwave-down-block t]
9153 ["Skip Block Backward" idlwave-backward-block t]
9154 ["Skip Block Forward" idlwave-forward-block t])
9155 ("Mark"
9156 ["Subprogram" idlwave-mark-subprogram t]
9157 ["Block" idlwave-mark-block t]
9158 ["Header" idlwave-mark-doclib t])
9159 ("Format"
9160 ["Indent Entire Statement" idlwave-indent-statement
9161 :active t :keys "C-u \\[indent-for-tab-command]" ]
9162 ["Indent Subprogram" idlwave-indent-subprogram t]
9163 ["(Un)Comment Region" idlwave-toggle-comment-region t]
9164 ["Continue/Split line" idlwave-split-line t]
9165 "--"
9166 ["Toggle Auto Fill" idlwave-auto-fill-mode :style toggle
9167 :selected (symbol-value idlwave-fill-function)])
9168 ("Templates"
9169 ["Procedure" idlwave-procedure t]
9170 ["Function" idlwave-function t]
9171 ["Doc Header" idlwave-doc-header t]
9172 ["Log" idlwave-doc-modification t]
9173 "--"
9174 ["Case" idlwave-case t]
9175 ["For" idlwave-for t]
9176 ["Repeat" idlwave-repeat t]
9177 ["While" idlwave-while t]
9178 "--"
9179 ["Close Block" idlwave-close-block t])
9180 ("Completion"
9181 ["Complete" idlwave-complete t]
9182 ("Complete Specific"
9183 ["1 Procedure Name" (idlwave-complete 'procedure) t]
9184 ["2 Procedure Keyword" (idlwave-complete 'procedure-keyword) t]
9185 "--"
9186 ["3 Function Name" (idlwave-complete 'function) t]
9187 ["4 Function Keyword" (idlwave-complete 'function-keyword) t]
9188 "--"
9189 ["5 Procedure Method Name" (idlwave-complete 'procedure-method) t]
9190 ["6 Procedure Method Keyword" (idlwave-complete 'procedure-method-keyword) t]
9191 "--"
9192 ["7 Function Method Name" (idlwave-complete 'function-method) t]
9193 ["8 Function Method Keyword" (idlwave-complete 'function-method-keyword) t]
9194 "--"
9195 ["9 Class Name" idlwave-complete-class t]))
9196 ("Routine Info"
9197 ["Show Routine Info" idlwave-routine-info t]
9198 ["Online Context Help" idlwave-context-help t]
9199 "--"
9200 ["Find Routine Source" idlwave-find-module t]
9201 ["Resolve Routine" idlwave-resolve (featurep 'idlw-shell)]
9202 "--"
9203 ["Update Routine Info" idlwave-update-routine-info t]
9204 ["Rescan XML Help Catalog" idlwave-convert-xml-system-routine-info t]
9205 "--"
9206 "IDL User Catalog"
9207 ["Select Catalog Directories" (idlwave-create-user-catalog-file nil) t]
9208 ["Scan Directories" (idlwave-update-routine-info '(16))
9209 (and idlwave-path-alist (not idlwave-catalog-process))]
9210 ["Scan Directories &" (idlwave-update-routine-info '(64))
9211 (and idlwave-path-alist (not idlwave-catalog-process))]
9212 "--"
9213 "Routine Shadows"
9214 ["Check Current Buffer" idlwave-list-buffer-load-path-shadows t]
9215 ["Check Compiled Routines" idlwave-list-shell-load-path-shadows t]
9216 ["Check Everything" idlwave-list-all-load-path-shadows t])
9217 ("Misc"
9218 ["Kill auto-created buffers" idlwave-kill-autoloaded-buffers t]
9219 "--"
9220 ["Insert TAB character" idlwave-hard-tab t])
9221 "--"
9222 ("External"
9223 ["Start IDL shell" idlwave-shell t]
9224 ["Edit file in IDLDE" idlwave-edit-in-idlde t]
9225 ["Launch IDL Help" idlwave-launch-idlhelp t])
9226 "--"
9227 ("Customize"
9228 ["Browse IDLWAVE Group" idlwave-customize t]
9229 "--"
9230 ["Build Full Customize Menu" idlwave-create-customize-menu
9231 (fboundp 'customize-menu-create)])
9232 ("Documentation"
9233 ["Describe Mode" describe-mode t]
9234 ["Abbreviation List" idlwave-list-abbrevs t]
9235 "--"
9236 ["Commentary in idlwave.el" idlwave-show-commentary t]
9237 ["Commentary in idlw-shell.el" idlwave-shell-show-commentary t]
9238 "--"
9239 ["Info" idlwave-info t]
9240 "--"
9241 ["Help with Topic" idlwave-help-assistant-help-with-topic
9242 idlwave-help-use-assistant]
9243 ["Launch IDL Help" idlwave-launch-idlhelp t])))
9244
9245 (defvar idlwave-mode-debug-menu-def
9246 '("Debug"
9247 ["Start IDL shell" idlwave-shell t]
9248 ["Save and .RUN buffer" idlwave-shell-save-and-run
9249 (and (boundp 'idlwave-shell-automatic-start)
9250 idlwave-shell-automatic-start)]))
9251
9252 (if (or (featurep 'easymenu) (load "easymenu" t))
9253 (progn
9254 (easy-menu-define idlwave-mode-menu idlwave-mode-map
9255 "IDL and WAVE CL editing menu"
9256 idlwave-mode-menu-def)
9257 (easy-menu-define idlwave-mode-debug-menu idlwave-mode-map
9258 "IDL and WAVE CL editing menu"
9259 idlwave-mode-debug-menu-def)))
9260
9261 (defun idlwave-customize ()
9262 "Call the customize function with idlwave as argument."
9263 (interactive)
9264 ;; Try to load the code for the shell, so that we can customize it
9265 ;; as well.
9266 (or (featurep 'idlw-shell)
9267 (load "idlw-shell" t))
9268 (customize-browse 'idlwave))
9269
9270 (defun idlwave-create-customize-menu ()
9271 "Create a full customization menu for IDLWAVE, insert it into the menu."
9272 (interactive)
9273 (if (fboundp 'customize-menu-create)
9274 (progn
9275 ;; Try to load the code for the shell, so that we can customize it
9276 ;; as well.
9277 (or (featurep 'idlw-shell)
9278 (load "idlw-shell" t))
9279 (easy-menu-change
9280 '("IDLWAVE") "Customize"
9281 `(["Browse IDLWAVE group" idlwave-customize t]
9282 "--"
9283 ,(customize-menu-create 'idlwave)
9284 ["Set" Custom-set t]
9285 ["Save" Custom-save t]
9286 ["Reset to Current" Custom-reset-current t]
9287 ["Reset to Saved" Custom-reset-saved t]
9288 ["Reset to Standard Settings" Custom-reset-standard t]))
9289 (message "\"IDLWAVE\"-menu now contains full customization menu"))
9290 (error "Cannot expand menu (outdated version of cus-edit.el)")))
9291
9292 (defun idlwave-show-commentary ()
9293 "Use the finder to view the file documentation from `idlwave.el'."
9294 (interactive)
9295 (require 'finder)
9296 (finder-commentary "idlwave.el"))
9297
9298 (defun idlwave-shell-show-commentary ()
9299 "Use the finder to view the file documentation from `idlw-shell.el'."
9300 (interactive)
9301 (require 'finder)
9302 (finder-commentary "idlw-shell.el"))
9303
9304 (defun idlwave-info ()
9305 "Read documentation for IDLWAVE in the info system."
9306 (interactive)
9307 (require 'info)
9308 (Info-goto-node "(idlwave)"))
9309
9310 (defun idlwave-list-abbrevs (arg)
9311 "Show the code abbreviations define in IDLWAVE mode.
9312 This lists all abbrevs where the replacement text differs from the input text.
9313 These are the ones the users want to learn to speed up their writing.
9314
9315 The function does *not* list abbrevs which replace a word with itself
9316 to call a hook. These hooks are used to change the case of words or
9317 to blink the matching `begin', and the user does not need to know them.
9318
9319 With arg, list all abbrevs with the corresponding hook.
9320
9321 This function was written since `list-abbrevs' looks terrible for IDLWAVE mode."
9322
9323 (interactive "P")
9324 (let ((table (symbol-value 'idlwave-mode-abbrev-table))
9325 abbrevs
9326 str rpl func fmt (len-str 0) (len-rpl 0))
9327 (mapatoms
9328 (lambda (sym)
9329 (if (symbol-value sym)
9330 (progn
9331 (setq str (symbol-name sym)
9332 rpl (symbol-value sym)
9333 func (symbol-function sym))
9334 (if arg
9335 (setq func (prin1-to-string func))
9336 (if (and (listp func) (stringp (nth 2 func)))
9337 (setq rpl (concat "EVAL: " (nth 2 func))
9338 func "")
9339 (setq func "")))
9340 (if (or arg (not (string= rpl str)))
9341 (progn
9342 (setq len-str (max len-str (length str)))
9343 (setq len-rpl (max len-rpl (length rpl)))
9344 (setq abbrevs (cons (list str rpl func) abbrevs)))))))
9345 table)
9346 ;; sort the list
9347 (setq abbrevs (sort abbrevs (lambda (a b) (string< (car a) (car b)))))
9348 ;; Make the format
9349 (setq fmt (format "%%-%ds %%-%ds %%s\n" len-str len-rpl))
9350 (with-output-to-temp-buffer "*Help*"
9351 (if arg
9352 (progn
9353 (princ "Abbreviations and Actions in IDLWAVE-Mode\n")
9354 (princ "=========================================\n\n")
9355 (princ (format fmt "KEY" "REPLACE" "HOOK"))
9356 (princ (format fmt "---" "-------" "----")))
9357 (princ "Code Abbreviations and Templates in IDLWAVE-Mode\n")
9358 (princ "================================================\n\n")
9359 (princ (format fmt "KEY" "ACTION" ""))
9360 (princ (format fmt "---" "------" "")))
9361 (mapcar
9362 (lambda (list)
9363 (setq str (car list)
9364 rpl (nth 1 list)
9365 func (nth 2 list))
9366 (princ (format fmt str rpl func)))
9367 abbrevs)))
9368 ;; Make sure each abbreviation uses only one display line
9369 (save-excursion
9370 (set-buffer "*Help*")
9371 (setq truncate-lines t)))
9372
9373 ;; Add .pro files to speedbar for support, if it's loaded
9374 (eval-after-load "speedbar" '(speedbar-add-supported-extension ".pro"))
9375
9376 ;; Set an idle timer to load the routine info.
9377 ;; Will only work on systems which support this.
9378 (or idlwave-routines (idlwave-start-load-rinfo-timer))
9379
9380 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.[Pp][Rr][Oo]\\'" . idlwave-mode))
9381
9382 ;; Run the hook
9383 (run-hooks 'idlwave-load-hook)
9384
9385 (provide 'idlwave)
9386
9387 ;; arch-tag: f77f3b0c-c37c-424f-a328-0886fd42b6fb
9388 ;;; idlwave.el ends here