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