Merge frome mainline.
[bpt/emacs.git] / lisp / progmodes / idlwave.el
CommitLineData
52a244eb 1;; idlwave.el --- IDL editing mode for GNU Emacs
d7a0267c 2
1ba983e8 3;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
114f9c96 4;; 2008, 2009, 2010 Free Software Foundation, Inc.
f32b3b91 5
52a244eb 6;; Authors: J.D. Smith <jdsmith@as.arizona.edu>
65363a4e 7;; Carsten Dominik <dominik@science.uva.nl>
52a244eb 8;; Chris Chase <chase@att.com>
5e72c6b2 9;; Maintainer: J.D. Smith <jdsmith@as.arizona.edu>
e08734e2 10;; Version: 6.1_em22
f32b3b91
CD
11;; Keywords: languages
12
e8af40ee 13;; This file is part of GNU Emacs.
f32b3b91 14
b1fc2b50 15;; GNU Emacs is free software: you can redistribute it and/or modify
f32b3b91 16;; it under the terms of the GNU General Public License as published by
b1fc2b50
GM
17;; the Free Software Foundation, either version 3 of the License, or
18;; (at your option) any later version.
f32b3b91
CD
19
20;; GNU Emacs is distributed in the hope that it will be useful,
21;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23;; GNU General Public License for more details.
24
25;; You should have received a copy of the GNU General Public License
b1fc2b50 26;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
f32b3b91
CD
27
28;;; Commentary:
29
f66f03de
S
30;; IDLWAVE enables feature-rich development and interaction with IDL,
31;; the Interactive Data Language. It provides a compelling,
32;; full-featured alternative to the IDLDE development environment
33;; bundled with IDL.
3938cb82 34
52a244eb
S
35;; In the remotely distant past, based on pascal.el, though bears
36;; little resemblance to it now.
f32b3b91
CD
37;;
38;; Incorporates many ideas, such as abbrevs, action routines, and
39;; continuation line indenting, from wave.el.
40;; wave.el original written by Lubos Pochman, Precision Visuals, Boulder.
41;;
42;; See the mode description ("C-h m" in idlwave-mode or "C-h f idlwave-mode")
43;; for features, key bindings, and info.
44;; Also, Info format documentation is available with `M-x idlwave-info'
45;;
5e72c6b2
S
46;; New versions of IDLWAVE, documentation, and more information
47;; available from:
48;; http://idlwave.org
f32b3b91
CD
49;;
50;; INSTALLATION
51;; ============
52;;
53;; Follow the instructions in the INSTALL file of the distribution.
54;; In short, put this file on your load path and add the following
55;; lines to your .emacs file:
56;;
57;; (autoload 'idlwave-mode "idlwave" "IDLWAVE Mode" t)
8c7b4ec8 58;; (autoload 'idlwave-shell "idlw-shell" "IDLWAVE Shell" t)
f32b3b91
CD
59;; (setq auto-mode-alist (cons '("\\.pro\\'" . idlwave-mode) auto-mode-alist))
60;;
61;;
62;; SOURCE
63;; ======
64;;
76959b77 65;; The newest version of this file is available from the maintainer's
52a244eb 66;; Webpage:
f32b3b91 67;;
5e72c6b2 68;; http://idlwave.org
f32b3b91
CD
69;;
70;; DOCUMENTATION
71;; =============
72;;
52a244eb
S
73;; IDLWAVE is documented online in info format. A printable version
74;; of the documentation is available from the maintainers webpage (see
75;; SOURCE).
775591f7 76;;
4b1aaa8b 77;;
f32b3b91
CD
78;; ACKNOWLEDGMENTS
79;; ===============
80;;
81;; Thanks to the following people for their contributions and comments:
82;;
52a244eb
S
83;; Ulrik Dickow <dickow_at_nbi.dk>
84;; Eric E. Dors <edors_at_lanl.gov>
85;; Stein Vidar H. Haugan <s.v.h.haugan_at_astro.uio.no>
86;; David Huenemoerder <dph_at_space.mit.edu>
87;; Kevin Ivory <Kevin.Ivory_at_linmpi.mpg.de>
88;; Dick Jackson <dick_at_d-jackson.com>
89;; Xuyong Liu <liu_at_stsci.edu>
90;; Simon Marshall <Simon.Marshall_at_esrin.esa.it>
91;; Laurent Mugnier <mugnier_at_onera.fr>
92;; Lubos Pochman <lubos_at_rsinc.com>
93;; Bob Portmann <portmann_at_al.noaa.gov>
94;; Patrick M. Ryan <pat_at_jaameri.gsfc.nasa.gov>
95;; Marty Ryba <ryba_at_ll.mit.edu>
96;; Paul Sorenson <aardvark62_at_msn.com>
97;; Phil Sterne <sterne_at_dublin.llnl.gov>
98;; Phil Williams <williams_at_irc.chmcc.org>
f32b3b91
CD
99;;
100;; CUSTOMIZATION:
101;; =============
102;;
52a244eb
S
103;; IDLWAVE has extensive customize support; to learn about the
104;; variables which control the mode's behavior, use `M-x
105;; idlwave-customize'.
f32b3b91
CD
106;;
107;; You can set your own preferred values with Customize, or with Lisp
108;; code in .emacs. For an example of what to put into .emacs, check
52a244eb
S
109;; the TexInfo documentation or see a complete .emacs available at the
110;; website.
f32b3b91
CD
111;;
112;; KNOWN PROBLEMS:
113;; ==============
114;;
76959b77
S
115;; IDLWAVE support for the IDL-derived PV-WAVE CL language of Visual
116;; Numerics, Inc. is growing less and less complete as the two
117;; languages grow increasingly apart. The mode probably shouldn't
3938cb82 118;; even have "WAVE" in its title, but it's catchy, and was required
52a244eb 119;; to avoid conflict with the CORBA idl.el mode. Caveat WAVEor.
76959b77 120;;
f32b3b91
CD
121;; Moving the point backwards in conjunction with abbrev expansion
122;; does not work as I would like it, but this is a problem with
123;; emacs abbrev expansion done by the self-insert-command. It ends
124;; up inserting the character that expanded the abbrev after moving
125;; point backward, e.g., "\cl" expanded with a space becomes
126;; "LONG( )" with point before the close paren. This is solved by
4b1aaa8b 127;; using a temporary function in `post-command-hook' - not pretty,
595ab50b 128;; but it works.
f32b3b91
CD
129;;
130;; Tabs and spaces are treated equally as whitespace when filling a
131;; comment paragraph. To accomplish this, tabs are permanently
132;; replaced by spaces in the text surrounding the paragraph, which
133;; may be an undesirable side-effect. Replacing tabs with spaces is
134;; limited to comments only and occurs only when a comment
135;; paragraph is filled via `idlwave-fill-paragraph'.
136;;
52a244eb
S
137;; Muti-statement lines (using "&") on block begin and end lines can
138;; ruin the formatting. For example, multiple end statements on a
139;; line: endif & endif. Using "&" outside of block begin/end lines
140;; should be okay.
f32b3b91 141;;
76959b77
S
142;; Determining the expression at point for printing and other
143;; examination commands is somewhat rough: currently only fairly
144;; simple entities are found. You can always drag-select or examine
52a244eb 145;; a pre-selected region.
f32b3b91 146;;
f32b3b91
CD
147;; When forcing completion of method keywords, the initial
148;; query for a method has multiple entries for some methods. Would
595ab50b 149;; be too difficult to fix this hardly used case.
f32b3b91
CD
150;;
151\f
152;;; Code:
153
52a244eb 154
f32b3b91 155(eval-when-compile (require 'cl))
52a244eb
S
156(require 'idlw-help)
157
158;; For XEmacs
159(unless (fboundp 'line-beginning-position)
160 (defalias 'line-beginning-position 'point-at-bol))
161(unless (fboundp 'line-end-position)
162 (defalias 'line-end-position 'point-at-eol))
163(unless (fboundp 'char-valid-p)
164 (defalias 'char-valid-p 'characterp))
f66f03de
S
165(unless (fboundp 'match-string-no-properties)
166 (defalias 'match-string-no-properties 'match-string))
f32b3b91 167
3938cb82
S
168(if (not (fboundp 'cancel-timer))
169 (condition-case nil
170 (require 'timer)
171 (error nil)))
172
73e72da4
DN
173(declare-function idlwave-shell-get-path-info "idlw-shell")
174(declare-function idlwave-shell-temp-file "idlw-shell")
175(declare-function idlwave-shell-is-running "idlw-shell")
176(declare-function widget-value "wid-edit" (widget))
177(declare-function comint-dynamic-complete-filename "comint" ())
73e72da4 178
f32b3b91 179(defgroup idlwave nil
31b58798 180 "Major mode for editing IDL .pro files."
f32b3b91 181 :tag "IDLWAVE"
4b1aaa8b 182 :link '(url-link :tag "Home Page"
5e72c6b2 183 "http://idlwave.org")
595ab50b
CD
184 :link '(emacs-commentary-link :tag "Commentary in idlw-shell.el"
185 "idlw-shell.el")
f32b3b91
CD
186 :link '(emacs-commentary-link :tag "Commentary in idlwave.el" "idlwave.el")
187 :link '(custom-manual "(idlwave)Top")
188 :prefix "idlwave"
189 :group 'languages)
190
52a244eb 191
f32b3b91
CD
192;;; Variables for indentation behavior ---------------------------------------
193
194(defgroup idlwave-code-formatting nil
195 "Indentation and formatting options for IDLWAVE mode."
196 :group 'idlwave)
197
f66f03de 198(defcustom idlwave-main-block-indent 2
f32b3b91
CD
199 "*Extra indentation for the main block of code.
200That is the block between the FUNCTION/PRO statement and the END
201statement for that program unit."
202 :group 'idlwave-code-formatting
203 :type 'integer)
204
f66f03de 205(defcustom idlwave-block-indent 3
f32b3b91
CD
206 "*Extra indentation applied to block lines.
207If you change this, you probably also want to change `idlwave-end-offset'."
208 :group 'idlwave-code-formatting
209 :type 'integer)
210
f66f03de 211(defcustom idlwave-end-offset -3
f32b3b91
CD
212 "*Extra indentation applied to block END lines.
213A value equal to negative `idlwave-block-indent' will make END lines
214line up with the block BEGIN lines."
215 :group 'idlwave-code-formatting
216 :type 'integer)
217
f66f03de 218(defcustom idlwave-continuation-indent 3
f32b3b91
CD
219 "*Extra indentation applied to continuation lines.
220This extra offset applies to the first of a set of continuation lines.
5e72c6b2
S
221The following lines receive the same indentation as the first."
222 :group 'idlwave-code-formatting
223 :type 'integer)
224
f66f03de 225(defcustom idlwave-max-extra-continuation-indent 40
5e72c6b2
S
226 "*Maximum additional indentation for special continuation indent.
227Several special indentations are tried to help line up continuation
228lines in routine calls or definitions, other statements with
134b6671 229parentheses, or assignment statements. This variable specifies a
5e72c6b2
S
230maximum amount by which this special indentation can exceed the
231standard continuation indentation, otherwise defaulting to a fixed
232offset. Set to 0 to effectively disable all special continuation
233indentation, or to a large number (like 100) to enable it in all
52a244eb 234cases. See also `idlwave-indent-to-open-paren', which can override
5e72c6b2 235this variable."
f32b3b91
CD
236 :group 'idlwave-code-formatting
237 :type 'integer)
238
5e72c6b2 239(defcustom idlwave-indent-to-open-paren t
5a0c3f56
JB
240 "*Non-nil means, indent continuation lines to innermost open parenthesis.
241This indentation occurs even if otherwise disallowed by
5e72c6b2
S
242`idlwave-max-extra-continuation-indent'. Matching parens and the
243interleaving args are lined up. Example:
244
245 x = function_a(function_b(function_c( a, b, [1,2,3, $
246 4,5,6 $
247 ], $
248 c, d $
249 )))
250
251When this variable is nil, paren alignment may still occur, based on
5a0c3f56
JB
252the value of `idlwave-max-extra-continuation-indent', which, if zero,
253would yield:
5e72c6b2
S
254
255 x = function_a(function_b(function_c( a, b, [1,2,3, $
256 4,5,6 $
257 ], $
258 c, d $
259 )))"
5a0c3f56 260 :group 'idlwave-code-formatting
5e72c6b2
S
261 :type 'boolean)
262
52a244eb
S
263(defcustom idlwave-indent-parens-nested nil
264 "*Non-nil means, indent continuation lines with parens by nesting
265lines at consecutively deeper levels."
266 :group 'idlwave-code-formatting
267 :type 'boolean)
268
269
f32b3b91
CD
270(defcustom idlwave-hanging-indent t
271 "*If set non-nil then comment paragraphs are indented under the
272hanging indent given by `idlwave-hang-indent-regexp' match in the first line
273of the paragraph."
274 :group 'idlwave-code-formatting
275 :type 'boolean)
276
277(defcustom idlwave-hang-indent-regexp "- "
278 "*Regular expression matching the position of the hanging indent
5a0c3f56 279in the first line of a comment paragraph. The size of the indent
f32b3b91
CD
280extends to the end of the match for the regular expression."
281 :group 'idlwave-code-formatting
282 :type 'regexp)
283
284(defcustom idlwave-use-last-hang-indent nil
285 "*If non-nil then use last match on line for `idlwave-indent-regexp'."
286 :group 'idlwave-code-formatting
287 :type 'boolean)
288
289(defcustom idlwave-fill-comment-line-only t
290 "*If non-nil then auto fill will only operate on comment lines."
291 :group 'idlwave-code-formatting
292 :type 'boolean)
293
294(defcustom idlwave-auto-fill-split-string t
295 "*If non-nil then auto fill will split strings with the IDL `+' operator.
4b1aaa8b
PE
296When the line end falls within a string, string concatenation with the
297'+' operator will be used to distribute a long string over lines.
f32b3b91
CD
298If nil and a string is split then a terminal beep and warning are issued.
299
300This variable is ignored when `idlwave-fill-comment-line-only' is
301non-nil, since in this case code is not auto-filled."
302 :group 'idlwave-code-formatting
303 :type 'boolean)
304
305(defcustom idlwave-split-line-string t
306 "*If non-nil then `idlwave-split-line' will split strings with `+'.
307When the splitting point of a line falls inside a string, split the string
308using the `+' string concatenation operator. If nil and a string is
309split then a terminal beep and warning are issued."
310 :group 'idlwave-code-formatting
311 :type 'boolean)
312
313(defcustom idlwave-no-change-comment ";;;"
314 "*The indentation of a comment that starts with this regular
5a0c3f56 315expression will not be changed. Note that the indentation of a comment
f32b3b91
CD
316at the beginning of a line is never changed."
317 :group 'idlwave-code-formatting
318 :type 'string)
319
320(defcustom idlwave-begin-line-comment nil
321 "*A comment anchored at the beginning of line.
322A comment matching this regular expression will not have its
323indentation changed. If nil the default is \"^;\", i.e., any line
324beginning with a \";\". Expressions for comments at the beginning of
325the line should begin with \"^\"."
326 :group 'idlwave-code-formatting
327 :type '(choice (const :tag "Any line beginning with `;'" nil)
328 'regexp))
329
330(defcustom idlwave-code-comment ";;[^;]"
331 "*A comment that starts with this regular expression on a line by
332itself is indented as if it is a part of IDL code. As a result if
333the comment is not preceded by whitespace it is unchanged."
334 :group 'idlwave-code-formatting
335 :type 'regexp)
336
337;; Comments not matching any of the above will be indented as a
338;; right-margin comment, i.e., to a minimum of `comment-column'.
339
f32b3b91
CD
340;;; Routine Info and Completion ---------------------------------------
341
15e42531
CD
342(defgroup idlwave-routine-info nil
343 "Routine Info options for IDLWAVE mode."
f32b3b91
CD
344 :group 'idlwave)
345
52a244eb
S
346(defcustom idlwave-use-library-catalogs t
347 "*Non-nil means search the IDL path for library catalog files.
348
349These files, named .idlwave_catalog, document routine information for
350individual directories and libraries of IDL .pro files. Many popular
5a0c3f56
JB
351libraries come with catalog files by default, so leaving this on is
352usually a good idea."
52a244eb
S
353 :group 'idlwave-routine-info
354 :type 'boolean)
5e72c6b2
S
355
356(defcustom idlwave-init-rinfo-when-idle-after 10
5a0c3f56
JB
357 "*Seconds of idle time before routine info is automatically initialized.
358Initializing the routine info can take a long time, in particular if a
359large number of library catalogs are involved. When Emacs is idle for
360more than the number of seconds specified by this variable, it starts
361the initialization. The process is split into five steps, in order to
362keep work interruption as short as possible. If one of the steps
363finishes, and no user input has arrived in the mean time, initialization
364proceeds immediately to the next step. A good value for this variable
365is about 1/3 of the time initialization take in your setup. So if you
366have a fast machine and no problems with a slow network connection,
367don't hesitate to set this to 2 seconds. A value of 0 means, don't
368initialize automatically, but instead wait until routine information is
369needed, and initialize then."
5e72c6b2
S
370 :group 'idlwave-routine-info
371 :type 'number)
372
f32b3b91 373(defcustom idlwave-scan-all-buffers-for-routine-info t
15e42531
CD
374 "*Non-nil means, scan buffers for IDL programs when updating info.
375The scanning is done by the command `idlwave-update-routine-info'.
376The following values are allowed:
377
378nil Don't scan any buffers.
5a0c3f56 379t Scan all `idlwave-mode' buffers in the current editing session.
15e42531
CD
380current Scan only the current buffer, but no other buffers."
381 :group 'idlwave-routine-info
382 :type '(choice
383 (const :tag "No buffer" nil)
384 (const :tag "All buffers" t)
385 (const :tag "Current buffer only" 'current)))
f32b3b91
CD
386
387(defcustom idlwave-query-shell-for-routine-info t
388 "*Non-nil means query the shell for info about compiled routines.
389Querying the shell is useful to get information about compiled modules,
390and it is turned on by default. However, when you have a complete library
391scan, this is not necessary."
15e42531 392 :group 'idlwave-routine-info
f32b3b91
CD
393 :type 'boolean)
394
15e42531
CD
395(defcustom idlwave-auto-routine-info-updates
396 '(find-file save-buffer kill-buffer compile-buffer)
397 "*Controls under what circumstances routine info is updated automatically.
398Possible values:
399nil Never
400t All available
5a0c3f56 401\(...) A list of circumstances. Allowed members are:
15e42531
CD
402 find-file Add info for new IDLWAVE buffers.
403 save-buffer Update buffer info when buffer is saved
404 kill-buffer Remove buffer info when buffer gets killed
405 compile-buffer Update shell info after `idlwave-shell-save-and...'"
406 :group 'idlwave-routine-info
407 :type '(choice
408 (const :tag "Never" nil)
409 (const :tag "As often as possible" t)
410 (set :tag "Checklist" :greedy t
411 (const :tag "When visiting a file" find-file)
412 (const :tag "When saving a buffer" save-buffer)
413 (const :tag "After a buffer was killed" kill-buffer)
414 (const :tag "After a buffer was compiled successfully, update shell info" compile-buffer))))
4b1aaa8b 415
15e42531
CD
416(defcustom idlwave-rinfo-max-source-lines 5
417 "*Maximum number of source files displayed in the Routine Info window.
418When an integer, it is the maximum number of source files displayed.
5a0c3f56 419A value of t means to show all source files."
15e42531
CD
420 :group 'idlwave-routine-info
421 :type 'integer)
422
f32b3b91 423(defcustom idlwave-library-path nil
8c43762b 424 "Library path for Windows and MacOS (OS9). Not needed under UNIX.
f66f03de
S
425When selecting the directories to scan for IDL user catalog routine
426info, IDLWAVE can, under UNIX, query the shell for the exact search
427path \(the value of !PATH). However, under Windows and MacOS
8c43762b 428\(pre-OSX), the IDLWAVE shell does not work. In this case, this
f66f03de
S
429variable can be set to specify the paths where IDLWAVE can find PRO
430files. The shell will only be asked for a list of paths when this
431variable is nil. The value is a list of directories. A directory
432preceeded by a `+' will be searched recursively. If you set this
433variable on a UNIX system, the shell will not be queried. See also
434`idlwave-system-directory'."
15e42531 435 :group 'idlwave-routine-info
f32b3b91
CD
436 :type '(repeat (directory)))
437
15e42531 438(defcustom idlwave-system-directory ""
52a244eb
S
439 "The IDL system directory for Windows and MacOS. Not needed under
440UNIX. Set this to the value of the `!DIR' system variable in IDL.
441IDLWAVE uses this to find out which of the library routines belong to
442the official system library. All files inside the `lib' subdirectory
443are considered system library files - so don't install private stuff
444in this directory. On UNIX systems, IDLWAVE queries the shell for the
445value of `!DIR'. See also `idlwave-library-path'."
15e42531
CD
446 :group 'idlwave-routine-info
447 :type 'directory)
448
f66f03de 449;; Configuration files
4b1aaa8b 450(defcustom idlwave-config-directory
52a244eb
S
451 (convert-standard-filename "~/.idlwave")
452 "*Directory for configuration files and user-library catalog."
15e42531 453 :group 'idlwave-routine-info
f32b3b91
CD
454 :type 'file)
455
52a244eb 456(defvar idlwave-user-catalog-file "idlusercat.el")
f66f03de 457(defvar idlwave-xml-system-rinfo-converted-file "idl_xml_rinfo.el")
52a244eb
S
458(defvar idlwave-path-file "idlpath.el")
459
460(defvar idlwave-libinfo-file nil
461 "*Obsolete variable, no longer used.")
462
15e42531
CD
463(defcustom idlwave-special-lib-alist nil
464 "Alist of regular expressions matching special library directories.
465When listing routine source locations, IDLWAVE gives a short hint where
4b1aaa8b 466the file defining the routine is located. By default it lists `SystemLib'
15e42531
CD
467for routines in the system library `!DIR/lib' and `Library' for anything
468else. This variable can define additional types. The car of each entry
469is a regular expression matching the file name (they normally will match
470on the path). The cdr is the string to be used as identifier. Max 10
471chars are allowed."
472 :group 'idlwave-routine-info
473 :type '(repeat
474 (cons regexp string)))
475
52a244eb 476(defcustom idlwave-auto-write-paths t
4b1aaa8b 477 "Write out path (!PATH) and system directory (!DIR) info automatically.
52a244eb
S
478Path info is needed to locate library catalog files. If non-nil,
479whenever the path-list changes as a result of shell-query, etc., it is
480written to file. Otherwise, the menu option \"Write Paths\" can be
481used to force a write."
482 :group 'idlwave-routine-info
05a1abfc 483 :type 'boolean)
775591f7 484
15e42531
CD
485(defgroup idlwave-completion nil
486 "Completion options for IDLWAVE mode."
487 :prefix "idlwave"
488 :group 'idlwave)
489
f32b3b91
CD
490(eval-and-compile
491 (defconst idlwave-tmp
492 '(choice :tag "by applying the function"
493 (const upcase)
494 (const downcase)
495 (const capitalize)
496 (const preserve)
497 (symbol :tag "Other"))))
498
f32b3b91
CD
499(defcustom idlwave-completion-case '((routine . upcase)
500 (keyword . upcase)
501 (class . preserve)
502 (method . preserve))
503 "Association list setting the case of completed words.
504
505This variable determines the case (UPPER/lower/Capitalized...) of
506words inserted into the buffer by completion. The preferred case can
507be specified separately for routine names, keywords, classes and
4b1aaa8b 508methods.
f32b3b91
CD
509This alist should therefore have entries for `routine' (normal
510functions and procedures, i.e. non-methods), `keyword', `class', and
511`method'. Plausible values are
512
513upcase upcase whole word, like `BOX_CURSOR'
514downcase downcase whole word, like `read_ppm'
515capitalize capitalize each part, like `Widget_Control'
516preserve preserve case as is, like `IDLgrView'
517
518The value can also be any Emacs Lisp function which transforms the
519case of characters in a string.
520
521A value of `preserve' means that the case of the completed word is
522identical to the way it was written in the definition statement of the
523routine. This was implemented to allow for mixed-case completion, in
524particular of object classes and methods.
525If a completable word is defined in multiple locations, the meaning of
526`preserve' is not unique since the different definitions might be
527cased differently. Therefore IDLWAVE always takes the case of the
528*first* definition it encounters during routine info collection and
529uses the case derived from it consistently.
530
531Note that a lowercase-only string in the buffer will always be completed in
532lower case (but see the variable `idlwave-completion-force-default-case').
533
534After changing this variable, you need to either restart Emacs or press
535`C-u C-c C-i' to update the internal lists."
15e42531 536 :group 'idlwave-completion
f32b3b91
CD
537 :type `(repeat
538 (cons (symbol :tag "Derive completion case for")
539 ,idlwave-tmp)))
540
541(defcustom idlwave-completion-force-default-case nil
542 "*Non-nil means, completion will always honor `idlwave-completion-case'.
543When nil, only the completion of a mixed case or upper case string
544will honor the default settings in `idlwave-completion-case', while
545the completion of lower case strings will be completed entirely in
546lower case."
15e42531 547 :group 'idlwave-completion
f32b3b91
CD
548 :type 'boolean)
549
550(defcustom idlwave-complete-empty-string-as-lower-case nil
551 "*Non-nil means, the empty string is considered downcase for completion.
552The case of what is already in the buffer determines the case of completions.
553When this variable is non-nil, the empty string is considered to be downcase.
554Completing on the empty string then offers downcase versions of the possible
555completions."
15e42531 556 :group 'idlwave-completion
f32b3b91
CD
557 :type 'boolean)
558
559(defvar idlwave-default-completion-case-is-down nil
560 "Obsolete variable. See `idlwave-complete-empty-string-as-lower-case' and
561`idlwave-completion-case'.")
562
563(defcustom idlwave-buffer-case-takes-precedence nil
564 "*Non-nil means, the case of tokens in buffers dominates over system stuff.
565To make this possible, we need to re-case everything each time we update
566the routine info from the buffers. This is slow.
567The default is to consider the case given in the system and library files
568first which makes updating much faster."
15e42531
CD
569 :group 'idlwave-completion
570 :type 'boolean)
571
572(defcustom idlwave-highlight-help-links-in-completion t
573 "*Non-nil means, highlight completions for which system help is available.
574Help can then be accessed with mouse-3.
575This option is only effective when the online help system is installed."
576 :group 'idlwave-completion
f32b3b91
CD
577 :type 'boolean)
578
05a1abfc
CD
579(defcustom idlwave-support-inheritance t
580 "Non-nil means, treat inheritance with completion, online help etc.
cef6cafe 581When nil, IDLWAVE only knows about the native methods and tags of a class,
05a1abfc
CD
582not about inherited ones."
583 :group 'idlwave-routine-info
584 :type 'boolean)
585
5e72c6b2
S
586(defcustom idlwave-keyword-class-inheritance '("^[gs]etproperty$" "^init$")
587 "List of regular expressions for class-driven keyword inheritance.
588Keyword inheritance is often tied to class inheritance by \"chaining\"
589up the class tree. While it cannot be assumed that the presence of an
590_EXTRA or _REF_EXTRA symbol guarantees such chaining will occur, for
591certain methods this assumption is almost always true. The methods
592for which to assume this can be set here."
593 :group 'idlwave-routine-info
594 :type '(repeat (regexp :tag "Match method:")))
4b1aaa8b 595
5e72c6b2 596
f32b3b91
CD
597(defcustom idlwave-completion-show-classes 1
598 "*Number of classes to show when completing object methods and keywords.
599When completing methods or keywords for an object with unknown class,
2e8b9c7d 600the *Completions* buffer will show the valid classes for each completion
f32b3b91
CD
601like this:
602
603MyMethod <Class1,Class2,Class3>
604
605The value of this variable may be nil to inhibit display, or an integer to
606indicate the maximum number of classes to display.
607
608On XEmacs, a full list of classes will also be placed into a `help-echo'
609property on the competion items, so that the list of classes for the current
610item is displayed in the echo area. If the value of this variable is a
611negative integer, the `help-echo' property will be suppressed."
15e42531 612 :group 'idlwave-completion
f32b3b91
CD
613 :type '(choice (const :tag "Don't show" nil)
614 (integer :tag "Number of classes shown" 1)))
615
616(defcustom idlwave-completion-fontify-classes t
617 "*Non-nil means, fontify the classes in completions buffer.
618This makes it easier to distinguish the completion items from the extra
619class info listed. See `idlwave-completion-show-classes'."
15e42531 620 :group 'idlwave-completion
f32b3b91
CD
621 :type 'boolean)
622
623(defcustom idlwave-query-class '((method-default . nil)
624 (keyword-default . nil))
625 "Association list governing specification of object classes for completion.
626
5e72c6b2
S
627When IDLWAVE tries to complete object-oriented methods, it usually
628cannot determine the class of a given object from context. In order
629to provide the user with a correct list of methods or keywords, it
76959b77
S
630needs to determine the appropriate class. IDLWAVE has two ways of
631doing this (well, three ways if you count the shell... see
632`idlwave-shell-query-for-class'):
633
6341. Combine the items of all available classes which contain this
635 method for the purpose of completion. So when completing a method,
636 all methods of all known classes are available, and when completing
637 a keyword, all keywords allowed for this method in any class are
638 shown. This behavior is very much like normal completion and is
639 therefore the default. It works much better than one might think -
640 only for the INIT, GETPROPERTY and SETPROPERTY the keyword lists
641 become uncomfortably long. See also
5e72c6b2 642 `idlwave-completion-show-classes'.
f32b3b91
CD
643
6442. The second possibility is to ask the user on each occasion. To
645 make this less interruptive, IDLWAVE can store the class as a text
646 property on the object operator `->'. For a given object in the
647 source code, class selection will then be needed only once
648 - for example to complete the method. Keywords to the method can
649 then be completed directly, because the class is already known.
650 You will have to turn on the storage of the selected class
651 explicitly with the variable `idlwave-store-inquired-class'.
652
5e72c6b2
S
653This variable allows you to configure IDLWAVE's method and
654method-keyword completion behavior. Its value is an alist, which
655should contain at least two elements: (method-default . VALUE) and
facebc7b 656\(keyword-default . VALUE), where VALUE is either t or nil. These
5e72c6b2
S
657specify if the class should be found during method and keyword
658completion, respectively.
f32b3b91 659
4b1aaa8b 660The alist may have additional entries specifying exceptions from the
f32b3b91
CD
661keyword completion rule for specific methods, like INIT or
662GETPROPERTY. In order to turn on class specification for the INIT
663method, add an entry (\"INIT\" . t). The method name must be ALL-CAPS."
15e42531 664 :group 'idlwave-completion
f32b3b91
CD
665 :type '(list
666 (cons (const method-default)
667 (boolean :tag "Determine class when completing METHODS "))
668 (cons (const keyword-default)
669 (boolean :tag "Determine class when completing KEYWORDS "))
670 (repeat
671 :tag "Exceptions to defaults"
672 :inline t
673 (cons (string :tag "MODULE" :value "")
674 (boolean :tag "Determine class for this method")))))
675
f66f03de 676(defcustom idlwave-store-inquired-class t
f32b3b91
CD
677 "*Non-nil means, store class of a method call as text property on `->'.
678IDLWAVE sometimes has to ask the user for the class associated with a
679particular object method call. This happens during the commands
680`idlwave-routine-info' and `idlwave-complete', depending upon the
681value of the variable `idlwave-query-class'.
682
683When you specify a class, this information can be stored as a text
4b1aaa8b 684property on the `->' arrow in the source code, so that during the same
f32b3b91
CD
685editing session, IDLWAVE will not have to ask again. When this
686variable is non-nil, IDLWAVE will store and reuse the class information.
687The class stored can be checked and removed with `\\[idlwave-routine-info]'
688on the arrow.
689
690The default of this variable is nil, since the result of commands then
691is more predictable. However, if you know what you are doing, it can
692be nice to turn this on.
693
694An arrow which knows the class will be highlighted with
695`idlwave-class-arrow-face'. The command \\[idlwave-routine-info]
696displays (with prefix arg: deletes) the class stored on the arrow
697at point."
15e42531 698 :group 'idlwave-completion
f32b3b91
CD
699 :type 'boolean)
700
701(defcustom idlwave-class-arrow-face 'bold
702 "*Face to highlight object operator arrows `->' which carry a class property.
703When IDLWAVE stores a class name as text property on an object arrow
facebc7b 704\(see variable `idlwave-store-inquired-class', it highlights the arrow
f32b3b91 705with this font in order to remind the user that this arrow is special."
15e42531 706 :group 'idlwave-completion
f32b3b91
CD
707 :type 'symbol)
708
709(defcustom idlwave-resize-routine-help-window t
710 "*Non-nil means, resize the Routine-info *Help* window to fit the content."
15e42531 711 :group 'idlwave-completion
f32b3b91
CD
712 :type 'boolean)
713
714(defcustom idlwave-keyword-completion-adds-equal t
715 "*Non-nil means, completion automatically adds `=' after completed keywords."
15e42531 716 :group 'idlwave-completion
f32b3b91
CD
717 :type 'boolean)
718
719(defcustom idlwave-function-completion-adds-paren t
720 "*Non-nil means, completion automatically adds `(' after completed function.
0ff9b955 721nil means, don't add anything.
f32b3b91
CD
722A value of `2' means, also add the closing parenthesis and position cursor
723between the two."
15e42531 724 :group 'idlwave-completion
f32b3b91
CD
725 :type '(choice (const :tag "Nothing" nil)
726 (const :tag "(" t)
727 (const :tag "()" 2)))
728
729(defcustom idlwave-completion-restore-window-configuration t
730 "*Non-nil means, try to restore the window configuration after completion.
731When completion is not unique, Emacs displays a list of completions.
732This messes up your window configuration. With this variable set, IDLWAVE
733restores the old configuration after successful completion."
15e42531 734 :group 'idlwave-completion
f32b3b91
CD
735 :type 'boolean)
736
737;;; Variables for abbrev and action behavior -----------------------------
738
739(defgroup idlwave-abbrev-and-indent-action nil
740 "IDLWAVE performs actions when expanding abbreviations or indenting lines.
741The variables in this group govern this."
742 :group 'idlwave)
743
744(defcustom idlwave-do-actions nil
745 "*Non-nil means performs actions when indenting.
746The actions that can be performed are listed in `idlwave-indent-action-table'."
747 :group 'idlwave-abbrev-and-indent-action
748 :type 'boolean)
749
750(defcustom idlwave-abbrev-start-char "\\"
751 "*A single character string used to start abbreviations in abbrev mode.
752Possible characters to chose from: ~`\%
753or even '?'. '.' is not a good choice because it can make structure
754field names act like abbrevs in certain circumstances.
755
756Changes to this in `idlwave-mode-hook' will have no effect. Instead a user
757must set it directly using `setq' in the .emacs file before idlwave.el
758is loaded."
759 :group 'idlwave-abbrev-and-indent-action
760 :type 'string)
761
762(defcustom idlwave-surround-by-blank nil
763 "*Non-nil means, enable `idlwave-surround'.
595ab50b 764If non-nil, `=',`<',`>',`&',`,', `->' are surrounded with spaces by
f32b3b91
CD
765`idlwave-surround'.
766See help for `idlwave-indent-action-table' for symbols using `idlwave-surround'.
767
768Also see the default key bindings for keys using `idlwave-surround'.
769Keys are bound and made into actions calling `idlwave-surround' with
770`idlwave-action-and-binding'.
771See help for `idlwave-action-and-binding' for examples.
772
773Also see help for `idlwave-surround'."
774 :group 'idlwave-abbrev-and-indent-action
775 :type 'boolean)
776
777(defcustom idlwave-pad-keyword t
52a244eb
S
778 "*Non-nil means pad '=' in keywords (routine calls or defs) like assignment.
779Whenever `idlwave-surround' is non-nil then this affects how '=' is
780padded for keywords and for variables. If t, pad the same as for
781assignments. If nil then spaces are removed. With any other value,
782spaces are left unchanged."
f32b3b91 783 :group 'idlwave-abbrev-and-indent-action
15e42531
CD
784 :type '(choice
785 (const :tag "Pad like assignments" t)
786 (const :tag "Remove space near `='" nil)
787 (const :tag "Keep space near `='" 'keep)))
f32b3b91
CD
788
789(defcustom idlwave-show-block t
790 "*Non-nil means point blinks to block beginning for `idlwave-show-begin'."
791 :group 'idlwave-abbrev-and-indent-action
792 :type 'boolean)
793
794(defcustom idlwave-expand-generic-end nil
795 "*Non-nil means expand generic END to ENDIF/ENDELSE/ENDWHILE etc."
796 :group 'idlwave-abbrev-and-indent-action
797 :type 'boolean)
798
15e42531
CD
799(defcustom idlwave-reindent-end t
800 "*Non-nil means re-indent line after END was typed."
801 :group 'idlwave-abbrev-and-indent-action
802 :type 'boolean)
803
f32b3b91
CD
804(defcustom idlwave-abbrev-move t
805 "*Non-nil means the abbrev hook can move point.
5a0c3f56 806Set to nil by `idlwave-expand-region-abbrevs'. To see the abbrev
f32b3b91 807definitions, use the command `list-abbrevs', for abbrevs that move
5a0c3f56 808point. Moving point is useful, for example, to place point between
f32b3b91
CD
809parentheses of expanded functions.
810
811See `idlwave-check-abbrev'."
812 :group 'idlwave-abbrev-and-indent-action
813 :type 'boolean)
814
815(defcustom idlwave-abbrev-change-case nil
816 "*Non-nil means all abbrevs will be forced to either upper or lower case.
817If the value t, all expanded abbrevs will be upper case.
818If the value is 'down then abbrevs will be forced to lower case.
819If nil, the case will not change.
820If `idlwave-reserved-word-upcase' is non-nil, reserved words will always be
821upper case, regardless of this variable."
822 :group 'idlwave-abbrev-and-indent-action
823 :type 'boolean)
824
825(defcustom idlwave-reserved-word-upcase nil
826 "*Non-nil means, reserved words will be made upper case via abbrev expansion.
827If nil case of reserved words is controlled by `idlwave-abbrev-change-case'.
828Has effect only if in abbrev-mode."
829 :group 'idlwave-abbrev-and-indent-action
830 :type 'boolean)
831
832;;; Action/Expand Tables.
833;;
834;; The average user may have difficulty modifying this directly. It
835;; can be modified/set in idlwave-mode-hook, but it is easier to use
836;; idlwave-action-and-binding. See help for idlwave-action-and-binding for
837;; examples of how to add an action.
838;;
839;; The action table is used by `idlwave-indent-line' whereas both the
840;; action and expand tables are used by `idlwave-indent-and-action'. In
841;; general, the expand table is only used when a line is explicitly
842;; indented. Whereas, in addition to being used when the expand table
843;; is used, the action table is used when a line is indirectly
844;; indented via line splitting, auto-filling or a new line creation.
845;;
846;; Example actions:
847;;
848;; Capitalize system vars
849;; (idlwave-action-and-binding idlwave-sysvar '(capitalize-word 1) t)
850;;
851;; Capitalize procedure name
852;; (idlwave-action-and-binding "\\<\\(pro\\|function\\)\\>[ \t]*\\<"
853;; '(capitalize-word 1) t)
854;;
855;; Capitalize common block name
856;; (idlwave-action-and-binding "\\<common\\>[ \t]+\\<"
857;; '(capitalize-word 1) t)
858;; Capitalize label
859;; (idlwave-action-and-binding (concat "^[ \t]*" idlwave-label)
860;; '(capitalize-word -1) t)
861
862(defvar idlwave-indent-action-table nil
863 "*Associated array containing action lists of search string (car),
5a0c3f56 864and function as a cdr. This table is used by `idlwave-indent-line'.
f32b3b91
CD
865See documentation for `idlwave-do-action' for a complete description of
866the action lists.
867
868Additions to the table are made with `idlwave-action-and-binding' when a
869binding is not requested.
870See help on `idlwave-action-and-binding' for examples.")
871
872(defvar idlwave-indent-expand-table nil
873 "*Associated array containing action lists of search string (car),
5a0c3f56
JB
874and function as a cdr. The table is used by the
875`idlwave-indent-and-action' function. See documentation for
f32b3b91
CD
876`idlwave-do-action' for a complete description of the action lists.
877
878Additions to the table are made with `idlwave-action-and-binding' when a
879binding is requested.
880See help on `idlwave-action-and-binding' for examples.")
881
882;;; Documentation header and history keyword ---------------------------------
883
884(defgroup idlwave-documentation nil
885 "Options for documenting IDLWAVE files."
886 :group 'idlwave)
887
888;; FIXME: make defcustom?
889(defvar idlwave-file-header
890 (list nil
891 ";+
892; NAME:
893;
894;
895;
896; PURPOSE:
897;
898;
899;
900; CATEGORY:
901;
902;
903;
904; CALLING SEQUENCE:
905;
906;
907;
908; INPUTS:
909;
910;
911;
912; OPTIONAL INPUTS:
913;
914;
915;
916; KEYWORD PARAMETERS:
917;
918;
919;
920; OUTPUTS:
921;
922;
923;
924; OPTIONAL OUTPUTS:
925;
926;
927;
928; COMMON BLOCKS:
929;
930;
931;
932; SIDE EFFECTS:
933;
934;
935;
936; RESTRICTIONS:
937;
938;
939;
940; PROCEDURE:
941;
942;
943;
944; EXAMPLE:
945;
946;
947;
948; MODIFICATION HISTORY:
949;
950;-
951")
952 "*A list (PATHNAME STRING) specifying the doc-header template to use for
5a0c3f56
JB
953summarizing a file. If PATHNAME is non-nil then this file will be included.
954Otherwise STRING is used. If nil, the file summary will be omitted.
f32b3b91
CD
955For example you might set PATHNAME to the path for the
956lib_template.pro file included in the IDL distribution.")
957
f66f03de 958(defcustom idlwave-header-to-beginning-of-file t
5e72c6b2
S
959 "*Non-nil means, the documentation header will always be at start of file.
960When nil, the header is positioned between the PRO/FUNCTION line of
961the current routine and the code, allowing several routine headers in
962a file."
963 :group 'idlwave-documentation
964 :type 'boolean)
965
f32b3b91
CD
966(defcustom idlwave-timestamp-hook 'idlwave-default-insert-timestamp
967 "*The hook function used to update the timestamp of a function."
968 :group 'idlwave-documentation
969 :type 'function)
970
971(defcustom idlwave-doc-modifications-keyword "HISTORY"
972 "*The modifications keyword to use with the log documentation commands.
973A ':' is added to the keyword end.
974Inserted by doc-header and used to position logs by doc-modification.
975If nil it will not be inserted."
976 :group 'idlwave-documentation
977 :type 'string)
978
979(defcustom idlwave-doclib-start "^;+\\+"
980 "*Regexp matching the start of a document library header."
981 :group 'idlwave-documentation
982 :type 'regexp)
983
984(defcustom idlwave-doclib-end "^;+-"
985 "*Regexp matching the end of a document library header."
986 :group 'idlwave-documentation
987 :type 'regexp)
988
989;;; External Programs -------------------------------------------------------
990
991(defgroup idlwave-external-programs nil
05a1abfc 992 "Path locations of external commands used by IDLWAVE."
f32b3b91
CD
993 :group 'idlwave)
994
f32b3b91 995(defcustom idlwave-shell-explicit-file-name "idl"
5e72c6b2 996 "*If non-nil, this is the command to run IDL.
f32b3b91 997Should be an absolute file path or path relative to the current environment
5e72c6b2 998execution search path. If you want to specify command line switches
5a0c3f56 999for the IDL program, use `idlwave-shell-command-line-options'.
5e72c6b2
S
1000
1001I know the name of this variable is badly chosen, but I cannot change
5a0c3f56 1002it without compromising backwards-compatibility."
f32b3b91
CD
1003 :group 'idlwave-external-programs
1004 :type 'string)
1005
f32b3b91 1006(defcustom idlwave-shell-command-line-options nil
5e72c6b2
S
1007 "*A list of command line options for calling the IDL program.
1008Since IDL is executed directly without going through a shell like /bin/sh,
1009this should be a list of strings like '(\"-rt=file\" \"-nw\") with a separate
1010string for each argument. But you may also give a single string which
1011contains the options whitespace-separated. Emacs will be kind enough to
1012split it for you."
1013 :type '(choice
1014 string
1015 (repeat (string :value "")))
f32b3b91
CD
1016 :group 'idlwave-external-programs)
1017
1018(defcustom idlwave-help-application "idlhelp"
f66f03de
S
1019 "*The external application providing reference help for programming.
1020Obsolete, if the IDL Assistant is being used for help."
f32b3b91
CD
1021 :group 'idlwave-external-programs
1022 :type 'string)
1023
05a1abfc
CD
1024;;; Some Shell variables which must be defined here.-----------------------
1025
1026(defcustom idlwave-shell-debug-modifiers '()
1027 "List of modifiers to be used for the debugging commands.
1028Will be used to bind debugging commands in the shell buffer and in all
1029source buffers. These are additional convenience bindings, the debugging
1030commands are always available with the `C-c C-d' prefix.
1031If you set this to '(control shift), this means setting a breakpoint will
1032be on `C-S-b', compiling a source file on `C-S-c' etc. Possible modifiers
1033are `control', `meta', `super', `hyper', `alt', and `shift'."
1034 :group 'idlwave-shell-general-setup
1035 :type '(set :tag "Specify modifiers"
1036 (const control)
1037 (const meta)
1038 (const super)
1039 (const hyper)
1040 (const alt)
1041 (const shift)))
1042
1043(defcustom idlwave-shell-automatic-start nil
5a0c3f56 1044 "*If non-nil attempt invoke `idlwave-shell' if not already running.
05a1abfc
CD
1045This is checked when an attempt to send a command to an
1046IDL process is made."
1047 :group 'idlwave-shell-general-setup
1048 :type 'boolean)
1049
f32b3b91
CD
1050;;; Miscellaneous variables -------------------------------------------------
1051
1052(defgroup idlwave-misc nil
1053 "Miscellaneous options for IDLWAVE mode."
8ec3bce0 1054 :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
f32b3b91
CD
1055 :group 'idlwave)
1056
1057(defcustom idlwave-startup-message t
1058 "*Non-nil displays a startup message when `idlwave-mode' is first called."
1059 :group 'idlwave-misc
1060 :type 'boolean)
1061
4b1aaa8b 1062(defcustom idlwave-default-font-lock-items
facebc7b 1063 '(pros-and-functions batch-files idlwave-idl-keywords label goto
f32b3b91
CD
1064 common-blocks class-arrows)
1065 "Items which should be fontified on the default fontification level 2.
1066IDLWAVE defines 3 levels of fontification. Level 1 is very little, level 3
1067is everything and level 2 is specified by this list.
5a0c3f56
JB
1068This variable must be set before IDLWAVE gets loaded.
1069It is a list of symbols; the following symbols are allowed:
f32b3b91
CD
1070
1071pros-and-functions Procedure and Function definitions
1072batch-files Batch Files
facebc7b 1073idlwave-idl-keywords IDL Keywords
f32b3b91
CD
1074label Statement Labels
1075goto Goto Statements
1076common-blocks Common Blocks
1077keyword-parameters Keyword Parameters in routine definitions and calls
1078system-variables System Variables
1079fixme FIXME: Warning in comments (on XEmacs only v. 21.0 and up)
1080class-arrows Object Arrows with class property"
1081 :group 'idlwave-misc
1082 :type '(set
1083 :inline t :greedy t
1084 (const :tag "Procedure and Function definitions" pros-and-functions)
facebc7b
S
1085 (const :tag "Batch Files" batch-files)
1086 (const :tag "IDL Keywords (reserved words)" idlwave-idl-keywords)
1087 (const :tag "Statement Labels" label)
1088 (const :tag "Goto Statements" goto)
1089 (const :tag "Tags in Structure Definition" structtag)
1090 (const :tag "Structure Name" structname)
1091 (const :tag "Common Blocks" common-blocks)
1092 (const :tag "Keyword Parameters" keyword-parameters)
1093 (const :tag "System Variables" system-variables)
1094 (const :tag "FIXME: Warning" fixme)
f32b3b91
CD
1095 (const :tag "Object Arrows with class property " class-arrows)))
1096
1097(defcustom idlwave-mode-hook nil
1098 "Normal hook. Executed when a buffer is put into `idlwave-mode'."
1099 :group 'idlwave-misc
1100 :type 'hook)
1101
1102(defcustom idlwave-load-hook nil
1103 "Normal hook. Executed when idlwave.el is loaded."
1104 :group 'idlwave-misc
1105 :type 'hook)
1106
15e42531
CD
1107(defvar idlwave-experimental nil
1108 "Non-nil means turn on a few experimental features.
1109This variable is only for the maintainer, to test difficult stuff,
1110while still distributing stable releases.
1111As a user, you should not set this to t.")
1112
f32b3b91
CD
1113;;;
1114;;; End customization variables section
1115;;;
1116
1117;;; Non customization variables
1118
1119;;; font-lock mode - Additions by Phil Williams, Ulrik Dickow and
52a244eb 1120;;; Simon Marshall <simon_at_gnu.ai.mit.edu>
f32b3b91
CD
1121;;; and Carsten Dominik...
1122
76959b77 1123;; The following are the reserved words in IDL. Maybe we should
4b1aaa8b 1124;; highlight some more stuff as well?
76959b77
S
1125;; Procedure declarations. Fontify keyword plus procedure name.
1126(defvar idlwave-idl-keywords
4b1aaa8b 1127 ;; To update this regexp, update the list of keywords and
76959b77 1128 ;; evaluate the form.
4b1aaa8b 1129 ;; (insert
76959b77 1130 ;; (prin1-to-string
4b1aaa8b 1131 ;; (concat
76959b77 1132 ;; "\\<\\("
4b1aaa8b 1133 ;; (regexp-opt
52a244eb 1134 ;; '("||" "&&" "and" "or" "xor" "not"
4b1aaa8b 1135 ;; "eq" "ge" "gt" "le" "lt" "ne"
76959b77 1136 ;; "for" "do" "endfor"
4b1aaa8b 1137 ;; "if" "then" "endif" "else" "endelse"
76959b77
S
1138 ;; "case" "of" "endcase"
1139 ;; "switch" "break" "continue" "endswitch"
1140 ;; "begin" "end"
1141 ;; "repeat" "until" "endrep"
4b1aaa8b 1142 ;; "while" "endwhile"
76959b77
S
1143 ;; "goto" "return"
1144 ;; "inherits" "mod"
1145 ;; "compile_opt" "forward_function"
1146 ;; "on_error" "on_ioerror")) ; on_error is not officially reserved
1147 ;; "\\)\\>")))
52a244eb
S
1148 "\\<\\(&&\\|and\\|b\\(egin\\|reak\\)\\|c\\(ase\\|o\\(mpile_opt\\|ntinue\\)\\)\\|do\\|e\\(lse\\|nd\\(case\\|else\\|for\\|if\\|rep\\|switch\\|while\\)?\\|q\\)\\|for\\(ward_function\\)?\\|g\\(oto\\|[et]\\)\\|i\\(f\\|nherits\\)\\|l[et]\\|mod\\|n\\(e\\|ot\\)\\|o\\(n_\\(error\\|ioerror\\)\\|[fr]\\)\\|re\\(peat\\|turn\\)\\|switch\\|then\\|until\\|while\\|xor\\|||\\)\\>")
1149
76959b77 1150
facebc7b 1151(let* (;; Procedure declarations. Fontify keyword plus procedure name.
f32b3b91
CD
1152 ;; Function declarations. Fontify keyword plus function name.
1153 (pros-and-functions
1154 '("\\<\\(function\\|pro\\)\\>[ \t]+\\(\\sw+\\(::\\sw+\\)?\\)"
1155 (1 font-lock-keyword-face)
1156 (2 font-lock-function-name-face nil t)))
1157
1158 ;; Common blocks
1159 (common-blocks
1160 '("\\<\\(common\\)\\>[ \t]*\\(\\sw+\\)?[ \t]*,?"
1161 (1 font-lock-keyword-face) ; "common"
1162 (2 font-lock-reference-face nil t) ; block name
f66f03de 1163 ("[ \t]*\\(\\sw+\\)[ ,]*"
f32b3b91 1164 ;; Start with point after block name and comma
4b1aaa8b 1165 (goto-char (match-end 0)) ; needed for XEmacs, could be nil
f32b3b91
CD
1166 nil
1167 (1 font-lock-variable-name-face) ; variable names
1168 )))
1169
1170 ;; Batch files
1171 (batch-files
1172 '("^[ \t]*\\(@[^ \t\n]+\\)" (1 font-lock-string-face)))
1173
1174 ;; FIXME warning.
1175 (fixme
1176 '("\\<FIXME:" (0 font-lock-warning-face t)))
1177
1178 ;; Labels
1179 (label
1180 '("^[ \t]*\\([a-zA-Z]\\sw*:\\)" (1 font-lock-reference-face)))
1181
1182 ;; The goto statement and its label
1183 (goto
1184 '("\\(goto\\)[ \t]*,[ \t]*\\([a-zA-Z]\\sw*\\)"
1185 (1 font-lock-keyword-face)
1186 (2 font-lock-reference-face)))
1187
52a244eb
S
1188 ;; Tags in structure definitions. Note that this definition
1189 ;; actually collides with labels, so we have to use the same
1190 ;; face. It also matches named subscript ranges,
1191 ;; e.g. vec{bottom:top]. No good way around this.
05a1abfc
CD
1192 (structtag
1193 '("\\<\\([a-zA-Z][a-zA-Z0-9_]*:\\)[^:]" (1 font-lock-reference-face)))
1194
1195 ;; Structure names
1196 (structname
1197 '("\\({\\|\\<inherits\\s-\\)\\s-*\\([a-zA-Z][a-zA-Z0-9_]*\\)[},\t \n]"
1198 (2 font-lock-function-name-face)))
1199
52a244eb 1200 ;; Keyword parameters, like /xlog or ,xrange=[]
f32b3b91 1201 ;; This is anchored to the comma preceeding the keyword.
595ab50b
CD
1202 ;; Treats continuation lines, works only during whole buffer
1203 ;; fontification. Slow, use it only in fancy fontification.
f32b3b91 1204 (keyword-parameters
0dc2be2f
S
1205 '("\\(,\\|[a-zA-Z0-9_](\\)[ \t]*\\(\\$[ \t]*\\(;.*\\)?\n\\([ \t]*\\(;.*\\)?\n\\)*[ \t]*\\)?\\(/[a-zA-Z_]\\sw*\\|[a-zA-Z_]\\sw*[ \t]*=\\)"
1206 (6 font-lock-reference-face)))
f32b3b91 1207
595ab50b 1208 ;; System variables start with a bang.
f32b3b91 1209 (system-variables
15e42531 1210 '("\\(![a-zA-Z_0-9]+\\(\\.\\sw+\\)?\\)"
f32b3b91
CD
1211 (1 font-lock-variable-name-face)))
1212
1213 ;; Special and unusual operators (not used because too noisy)
8d222148
SM
1214 ;; (special-operators
1215 ;; '("[<>#]" (0 font-lock-keyword-face)))
f32b3b91
CD
1216
1217 ;; All operators (not used because too noisy)
8d222148
SM
1218 ;; (all-operators
1219 ;; '("[-*^#+<>/]" (0 font-lock-keyword-face)))
4b1aaa8b 1220
f32b3b91
CD
1221 ;; Arrows with text property `idlwave-class'
1222 (class-arrows
facebc7b
S
1223 '(idlwave-match-class-arrows (0 idlwave-class-arrow-face))))
1224
1225 (defconst idlwave-font-lock-keywords-1
1226 (list pros-and-functions batch-files)
1227 "Subdued level highlighting for IDLWAVE mode.")
f32b3b91 1228
facebc7b
S
1229 (defconst idlwave-font-lock-keywords-2
1230 (mapcar 'symbol-value idlwave-default-font-lock-items)
1231 "Medium level highlighting for IDLWAVE mode.")
f32b3b91 1232
facebc7b 1233 (defconst idlwave-font-lock-keywords-3
f32b3b91
CD
1234 (list pros-and-functions
1235 batch-files
76959b77 1236 idlwave-idl-keywords
f32b3b91 1237 label goto
05a1abfc
CD
1238 structtag
1239 structname
f32b3b91
CD
1240 common-blocks
1241 keyword-parameters
1242 system-variables
facebc7b
S
1243 class-arrows)
1244 "Gaudy level highlighting for IDLWAVE mode."))
f32b3b91
CD
1245
1246(defun idlwave-match-class-arrows (limit)
1247 ;; Match an object arrow with class property
1248 (and idlwave-store-inquired-class
1249 (re-search-forward "->" limit 'limit)
1250 (get-text-property (match-beginning 0) 'idlwave-class)))
1251
1252(defvar idlwave-font-lock-keywords idlwave-font-lock-keywords-2
1253 "Default expressions to highlight in IDLWAVE mode.")
1254
1255(defvar idlwave-font-lock-defaults
1256 '((idlwave-font-lock-keywords
4b1aaa8b 1257 idlwave-font-lock-keywords-1
f32b3b91
CD
1258 idlwave-font-lock-keywords-2
1259 idlwave-font-lock-keywords-3)
4b1aaa8b
PE
1260 nil t
1261 ((?$ . "w") (?_ . "w") (?. . "w") (?| . "w") (?& . "w"))
f32b3b91
CD
1262 beginning-of-line))
1263
4b1aaa8b 1264(put 'idlwave-mode 'font-lock-defaults
f32b3b91
CD
1265 idlwave-font-lock-defaults) ; XEmacs
1266
1267(defconst idlwave-comment-line-start-skip "^[ \t]*;"
1268 "Regexp to match the start of a full-line comment.
1269That is the _beginning_ of a line containing a comment delimiter `;' preceded
1270only by whitespace.")
1271
4b1aaa8b 1272(defconst idlwave-begin-block-reg
05a1abfc 1273 "\\<\\(pro\\|function\\|begin\\|case\\|switch\\)\\>"
5a0c3f56
JB
1274 "Regular expression to find the beginning of a block.
1275The case does not matter. The search skips matches in comments.")
f32b3b91 1276
52a244eb 1277(defconst idlwave-begin-unit-reg "^\\s-*\\(pro\\|function\\)\\>\\|\\`"
5a0c3f56
JB
1278 "Regular expression to find the beginning of a unit.
1279The case does not matter.")
f32b3b91 1280
52a244eb 1281(defconst idlwave-end-unit-reg "^\\s-*\\(pro\\|function\\)\\>\\|\\'"
f32b3b91 1282 "Regular expression to find the line that indicates the end of unit.
5a0c3f56
JB
1283This line is the end of buffer or the start of another unit.
1284The case does not matter. The search skips matches in comments.")
f32b3b91
CD
1285
1286(defconst idlwave-continue-line-reg "\\<\\$"
1287 "Regular expression to match a continued line.")
1288
1289(defconst idlwave-end-block-reg
05a1abfc 1290 "\\<end\\(\\|case\\|switch\\|else\\|for\\|if\\|rep\\|while\\)\\>"
5a0c3f56
JB
1291 "Regular expression to find the end of a block.
1292The case does not matter. The search skips matches in comments.")
f32b3b91
CD
1293
1294(defconst idlwave-block-matches
1295 '(("pro" . "end")
1296 ("function" . "end")
1297 ("case" . "endcase")
1298 ("else" . "endelse")
1299 ("for" . "endfor")
1300 ("then" . "endif")
1301 ("repeat" . "endrep")
05a1abfc 1302 ("switch" . "endswitch")
f32b3b91
CD
1303 ("while" . "endwhile"))
1304 "Matches between statements and the corresponding END variant.
1305The cars are the reserved words starting a block. If the block really
1306begins with BEGIN, the cars are the reserved words before the begin
1307which can be used to identify the block type.
1308This is used to check for the correct END type, to close blocks and
1309to expand generic end statements to their detailed form.")
1310
1311(defconst idlwave-block-match-regexp
1312 "\\<\\(else\\|for\\|then\\|repeat\\|while\\)\\>"
1313"Regular expression matching reserved words which can stand before
1314blocks starting with a BEGIN statement. The matches must have associations
5a0c3f56 1315`idlwave-block-matches'.")
f32b3b91 1316
52a244eb 1317(defconst idlwave-identifier "[a-zA-Z_][a-zA-Z0-9$_]*"
f32b3b91
CD
1318 "Regular expression matching an IDL identifier.")
1319
1320(defconst idlwave-sysvar (concat "!" idlwave-identifier)
1321 "Regular expression matching IDL system variables.")
1322
1323(defconst idlwave-variable (concat idlwave-identifier "\\|" idlwave-sysvar)
1324 "Regular expression matching IDL variable names.")
1325
1326(defconst idlwave-label (concat idlwave-identifier ":")
1327 "Regular expression matching IDL labels.")
1328
52a244eb
S
1329(defconst idlwave-method-call (concat idlwave-identifier "\\s *->"
1330 "\\(\\s *" idlwave-identifier "::\\)?"
1331))
1332
f32b3b91
CD
1333(defconst idlwave-statement-match
1334 (list
aa87aafc 1335 ;; "endif else" is the only possible "end" that can be
f32b3b91
CD
1336 ;; followed by a statement on the same line.
1337 '(endelse . ("end\\(\\|if\\)\\s +else" "end\\(\\|if\\)\\s +else"))
1338 ;; all other "end"s can not be followed by a statement.
1339 (cons 'end (list idlwave-end-block-reg nil))
1340 '(if . ("if\\>" "then"))
1341 '(for . ("for\\>" "do"))
1342 '(begin . ("begin\\>" nil))
1343 '(pdef . ("pro\\>\\|function\\>" nil))
1344 '(while . ("while\\>" "do"))
1345 '(repeat . ("repeat\\>" "repeat"))
1346 '(goto . ("goto\\>" nil))
1347 '(case . ("case\\>" nil))
05a1abfc 1348 '(switch . ("switch\\>" nil))
4b1aaa8b 1349 (cons 'call (list (concat "\\(" idlwave-variable "\\) *= *"
52a244eb
S
1350 "\\(" idlwave-method-call "\\s *\\)?"
1351 idlwave-identifier
1352 "\\s *(") nil))
4b1aaa8b 1353 (cons 'call (list (concat
52a244eb 1354 "\\(" idlwave-method-call "\\s *\\)?"
4b1aaa8b 1355 idlwave-identifier
52a244eb 1356 "\\( *\\($\\|\\$\\)\\|\\s *,\\)") nil))
4b1aaa8b 1357 (cons 'assign (list (concat
52a244eb 1358 "\\(" idlwave-variable "\\) *=") nil)))
4b1aaa8b 1359
f32b3b91 1360 "Associated list of statement matching regular expressions.
5a0c3f56
JB
1361Each regular expression matches the start of an IDL statement.
1362The first element of each association is a symbol giving the statement
f32b3b91
CD
1363type. The associated value is a list. The first element of this list
1364is a regular expression matching the start of an IDL statement for
1365identifying the statement type. The second element of this list is a
1366regular expression for finding a substatement for the type. The
1367substatement starts after the end of the found match modulo
1368whitespace. If it is nil then the statement has no substatement. The
1369list order matters since matching an assignment statement exactly is
1370not possible without parsing. Thus assignment statement become just
5a0c3f56 1371the leftover unidentified statements containing an equal sign.")
f32b3b91
CD
1372
1373(defvar idlwave-fill-function 'auto-fill-function
1374 "IDL mode auto fill function.")
1375
1376(defvar idlwave-comment-indent-function 'comment-indent-function
1377 "IDL mode comment indent function.")
1378
1379;; Note that this is documented in the v18 manuals as being a string
1380;; of length one rather than a single character.
1381;; The code in this file accepts either format for compatibility.
4b1aaa8b 1382(defvar idlwave-comment-indent-char ?\
f32b3b91
CD
1383 "Character to be inserted for IDL comment indentation.
1384Normally a space.")
1385
1386(defconst idlwave-continuation-char ?$
1387 "Character which is inserted as a last character on previous line by
1388 \\[idlwave-split-line] to begin a continuation line. Normally $.")
1389
e08734e2 1390(defconst idlwave-mode-version "6.1_em22")
f32b3b91
CD
1391
1392(defmacro idlwave-keyword-abbrev (&rest args)
1393 "Creates a function for abbrev hooks to call `idlwave-check-abbrev' with args."
8a946354 1394 `(quote (lambda ()
5e72c6b2 1395 ,(append '(idlwave-check-abbrev) args))))
f32b3b91
CD
1396
1397;; If I take the time I can replace idlwave-keyword-abbrev with
1398;; idlwave-code-abbrev and remove the quoted abbrev check from
1399;; idlwave-check-abbrev. Then, e.g, (idlwave-keyword-abbrev 0 t) becomes
1400;; (idlwave-code-abbrev idlwave-check-abbrev 0 t). In fact I should change
1401;; the name of idlwave-check-abbrev to something like idlwave-modify-abbrev.
1402
1403(defmacro idlwave-code-abbrev (&rest args)
1404 "Creates a function for abbrev hooks that ensures abbrevs are not quoted.
1405Specifically, if the abbrev is in a comment or string it is unexpanded.
1406Otherwise ARGS forms a list that is evaluated."
8d222148
SM
1407 ;; FIXME: it would probably be better to rely on the new :enable-function
1408 ;; to enforce the "don't expand in comments or strings".
1409 `(lambda ()
1410 ,(prin1-to-string args) ;; Puts the code in the doc string
1411 (if (idlwave-quoted)
1412 (progn (unexpand-abbrev) nil)
1413 ,(append args))))
1414
1415(autoload 'idlwave-shell "idlw-shell"
1416 "Run an inferior IDL, with I/O through buffer `(idlwave-shell-buffer)'." t)
1417(autoload 'idlwave-shell-send-command "idlw-shell")
1418(autoload 'idlwave-shell-recenter-shell-window "idlw-shell"
1419 "Run `idlwave-shell' and switch back to current window" t)
1420(autoload 'idlwave-shell-save-and-run "idlw-shell"
1421 "Save and run buffer under the shell." t)
1422(autoload 'idlwave-shell-break-here "idlw-shell"
1423 "Set breakpoint in current line." t)
1424(autoload 'idlwave-shell-run-region "idlw-shell"
1425 "Compile and run the region." t)
f32b3b91 1426
8d222148
SM
1427(fset 'idlwave-debug-map (make-sparse-keymap))
1428
1429(defvar idlwave-mode-map
1430 (let ((map (make-sparse-keymap)))
1431 (define-key map "\C-c " 'idlwave-hard-tab)
1432 (define-key map [(control tab)] 'idlwave-hard-tab)
1433 ;;(define-key map "\C-c\C- " 'idlwave-hard-tab)
1434 (define-key map "'" 'idlwave-show-matching-quote)
1435 (define-key map "\"" 'idlwave-show-matching-quote)
1436 (define-key map "\C-g" 'idlwave-keyboard-quit)
1437 (define-key map "\C-c;" 'idlwave-toggle-comment-region)
1438 (define-key map "\C-\M-a" 'idlwave-beginning-of-subprogram)
1439 (define-key map "\C-\M-e" 'idlwave-end-of-subprogram)
1440 (define-key map "\C-c{" 'idlwave-beginning-of-block)
1441 (define-key map "\C-c}" 'idlwave-end-of-block)
1442 (define-key map "\C-c]" 'idlwave-close-block)
1443 (define-key map [(meta control h)] 'idlwave-mark-subprogram)
1444 (define-key map "\M-\C-n" 'idlwave-forward-block)
1445 (define-key map "\M-\C-p" 'idlwave-backward-block)
1446 (define-key map "\M-\C-d" 'idlwave-down-block)
1447 (define-key map "\M-\C-u" 'idlwave-backward-up-block)
1448 (define-key map "\M-\r" 'idlwave-split-line)
1449 (define-key map "\M-\C-q" 'idlwave-indent-subprogram)
1450 (define-key map "\C-c\C-p" 'idlwave-previous-statement)
1451 (define-key map "\C-c\C-n" 'idlwave-next-statement)
1452 ;; (define-key map "\r" 'idlwave-newline)
1453 ;; (define-key map "\t" 'idlwave-indent-line)
1454 (define-key map [(shift iso-lefttab)] 'idlwave-indent-statement)
1455 (define-key map "\C-c\C-a" 'idlwave-auto-fill-mode)
1456 (define-key map "\M-q" 'idlwave-fill-paragraph)
1457 (define-key map "\M-s" 'idlwave-edit-in-idlde)
1458 (define-key map "\C-c\C-h" 'idlwave-doc-header)
1459 (define-key map "\C-c\C-m" 'idlwave-doc-modification)
1460 (define-key map "\C-c\C-c" 'idlwave-case)
1461 (define-key map "\C-c\C-d" 'idlwave-debug-map)
1462 (when (and (listp idlwave-shell-debug-modifiers)
1463 (not (equal idlwave-shell-debug-modifiers '())))
1464 ;; Bind the debug commands also with the special modifiers.
1465 (let ((shift (memq 'shift idlwave-shell-debug-modifiers))
1466 (mods-noshift
1467 (delq 'shift (copy-sequence idlwave-shell-debug-modifiers))))
1468 (define-key map
1469 (vector (append mods-noshift (list (if shift ?C ?c))))
1470 'idlwave-shell-save-and-run)
1471 (define-key map
1472 (vector (append mods-noshift (list (if shift ?B ?b))))
1473 'idlwave-shell-break-here)
1474 (define-key map
1475 (vector (append mods-noshift (list (if shift ?E ?e))))
1476 'idlwave-shell-run-region)))
1477 (define-key map "\C-c\C-d\C-c" 'idlwave-shell-save-and-run)
1478 (define-key map "\C-c\C-d\C-b" 'idlwave-shell-break-here)
1479 (define-key map "\C-c\C-d\C-e" 'idlwave-shell-run-region)
1480 (define-key map "\C-c\C-f" 'idlwave-for)
1481 ;; (define-key map "\C-c\C-f" 'idlwave-function)
1482 ;; (define-key map "\C-c\C-p" 'idlwave-procedure)
1483 (define-key map "\C-c\C-r" 'idlwave-repeat)
1484 (define-key map "\C-c\C-w" 'idlwave-while)
1485 (define-key map "\C-c\C-k" 'idlwave-kill-autoloaded-buffers)
1486 (define-key map "\C-c\C-s" 'idlwave-shell)
1487 (define-key map "\C-c\C-l" 'idlwave-shell-recenter-shell-window)
1488 (define-key map "\C-c\C-b" 'idlwave-list-buffer-load-path-shadows)
1489 (define-key map "\C-c\C-v" 'idlwave-find-module)
1490 (define-key map "\C-c\C-t" 'idlwave-find-module-this-file)
1491 (define-key map "\C-c?" 'idlwave-routine-info)
1492 (define-key map "\M-?" 'idlwave-context-help)
1493 (define-key map [(control meta ?\?)]
1494 'idlwave-help-assistant-help-with-topic)
1495 ;; Pickup both forms of Esc/Meta binding
1496 (define-key map [(meta tab)] 'idlwave-complete)
1497 (define-key map [?\e?\t] 'idlwave-complete)
1498 (define-key map "\M-\C-i" 'idlwave-complete)
1499 (define-key map "\C-c\C-i" 'idlwave-update-routine-info)
1500 (define-key map "\C-c=" 'idlwave-resolve)
1501 (define-key map
1502 (if (featurep 'xemacs) [(shift button3)] [(shift mouse-3)])
1503 'idlwave-mouse-context-help)
1504 map)
f32b3b91
CD
1505 "Keymap used in IDL mode.")
1506
8d222148
SM
1507(defvar idlwave-mode-syntax-table
1508 (let ((st (make-syntax-table)))
1509 (modify-syntax-entry ?+ "." st)
1510 (modify-syntax-entry ?- "." st)
1511 (modify-syntax-entry ?* "." st)
1512 (modify-syntax-entry ?/ "." st)
1513 (modify-syntax-entry ?^ "." st)
1514 (modify-syntax-entry ?# "." st)
1515 (modify-syntax-entry ?= "." st)
1516 (modify-syntax-entry ?% "." st)
1517 (modify-syntax-entry ?< "." st)
1518 (modify-syntax-entry ?> "." st)
1519 (modify-syntax-entry ?\' "\"" st)
1520 (modify-syntax-entry ?\" "\"" st)
1521 (modify-syntax-entry ?\\ "." st)
1522 (modify-syntax-entry ?_ "_" st)
1523 (modify-syntax-entry ?{ "(}" st)
1524 (modify-syntax-entry ?} "){" st)
1525 (modify-syntax-entry ?$ "_" st)
1526 (modify-syntax-entry ?. "." st)
1527 (modify-syntax-entry ?\; "<" st)
1528 (modify-syntax-entry ?\n ">" st)
1529 (modify-syntax-entry ?\f ">" st)
1530 st)
f32b3b91
CD
1531 "Syntax table in use in `idlwave-mode' buffers.")
1532
f32b3b91 1533(defvar idlwave-find-symbol-syntax-table
8d222148
SM
1534 (let ((st (copy-syntax-table idlwave-mode-syntax-table)))
1535 (modify-syntax-entry ?$ "w" st)
1536 (modify-syntax-entry ?_ "w" st)
1537 (modify-syntax-entry ?! "w" st)
1538 (modify-syntax-entry ?. "w" st)
1539 st)
f32b3b91
CD
1540 "Syntax table that treats symbol characters as word characters.")
1541
76959b77
S
1542(defmacro idlwave-with-special-syntax (&rest body)
1543 "Execute BODY with a different syntax table."
05a1abfc
CD
1544 `(let ((saved-syntax (syntax-table)))
1545 (unwind-protect
1546 (progn
1547 (set-syntax-table idlwave-find-symbol-syntax-table)
1548 ,@body)
1549 (set-syntax-table saved-syntax))))
1550
76959b77
S
1551;(defmacro idlwave-with-special-syntax1 (&rest body)
1552; "Execute BODY with a different syntax table."
1553; `(let ((saved-syntax (syntax-table)))
1554; (unwind-protect
1555; (progn
1556; (set-syntax-table idlwave-find-symbol-syntax-table)
1557; ,@body)
1558; (set-syntax-table saved-syntax))))
1559
f32b3b91
CD
1560(defun idlwave-action-and-binding (key cmd &optional select)
1561 "KEY and CMD are made into a key binding and an indent action.
1562KEY is a string - same as for the `define-key' function. CMD is a
1563function of no arguments or a list to be evaluated. CMD is bound to
1564KEY in `idlwave-mode-map' by defining an anonymous function calling
1565`self-insert-command' followed by CMD. If KEY contains more than one
1566character a binding will only be set if SELECT is 'both.
1567
5e72c6b2 1568\(KEY . CMD\) is also placed in the `idlwave-indent-expand-table',
f32b3b91
CD
1569replacing any previous value for KEY. If a binding is not set then it
1570will instead be placed in `idlwave-indent-action-table'.
1571
1572If the optional argument SELECT is nil then an action and binding are
1573created. If SELECT is 'noaction, then a binding is always set and no
1574action is created. If SELECT is 'both then an action and binding
1575will both be created even if KEY contains more than one character.
1576Otherwise, if SELECT is non-nil then only an action is created.
1577
1578Some examples:
1579No spaces before and 1 after a comma
1580 (idlwave-action-and-binding \",\" '(idlwave-surround 0 1))
1581A minimum of 1 space before and after `=' (see `idlwave-expand-equal').
1582 (idlwave-action-and-binding \"=\" '(idlwave-expand-equal -1 -1))
1583Capitalize system variables - action only
1584 (idlwave-action-and-binding idlwave-sysvar '(capitalize-word 1) t)"
1585 (if (not (equal select 'noaction))
1586 ;; Add action
1587 (let* ((table (if select 'idlwave-indent-action-table
1588 'idlwave-indent-expand-table))
3938cb82
S
1589 (table-key (regexp-quote key))
1590 (cell (assoc table-key (eval table))))
f32b3b91
CD
1591 (if cell
1592 ;; Replace action command
1593 (setcdr cell cmd)
1594 ;; New action
3938cb82 1595 (set table (append (eval table) (list (cons table-key cmd)))))))
f32b3b91
CD
1596 ;; Make key binding for action
1597 (if (or (and (null select) (= (length key) 1))
1598 (equal select 'noaction)
1599 (equal select 'both))
1600 (define-key idlwave-mode-map key
8d222148
SM
1601 `(lambda ()
1602 (interactive)
1603 (self-insert-command 1)
1604 ,@(if (listp cmd) cmd (list cmd))))))
f32b3b91
CD
1605
1606;; Set action and key bindings.
1607;; See description of the function `idlwave-action-and-binding'.
1608;; Automatically add spaces for the following characters
f66f03de
S
1609
1610;; Actions for & are complicated by &&
1611(idlwave-action-and-binding "&" 'idlwave-custom-ampersand-surround)
1612
1613;; Automatically add spaces to equal sign if not keyword. This needs
1614;; to go ahead of > and <, so >= and <= will be treated correctly
f32b3b91
CD
1615(idlwave-action-and-binding "=" '(idlwave-expand-equal -1 -1))
1616
4b1aaa8b 1617;; Actions for > and < are complicated by >=, <=, and ->...
f66f03de
S
1618(idlwave-action-and-binding "<" '(idlwave-custom-ltgtr-surround nil))
1619(idlwave-action-and-binding ">" '(idlwave-custom-ltgtr-surround 'gtr))
1620
1621(idlwave-action-and-binding "," '(idlwave-surround 0 -1 1))
1622
1623
f32b3b91
CD
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;;;
5e72c6b2 1631(defvar idlwave-mode-abbrev-table nil
5a0c3f56 1632 "Abbreviation table used for IDLWAVE mode.")
5e72c6b2
S
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
1638If NOPREFIX is non-nil, don't prepend prefix character. Installs into
5a0c3f56 1639`idlwave-mode-abbrev-table' unless TABLE is non-nil."
5e72c6b2
S
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)))))
f32b3b91
CD
1648
1649(condition-case nil
4b1aaa8b 1650 (modify-syntax-entry (string-to-char idlwave-abbrev-start-char)
f32b3b91
CD
1651 "w" idlwave-mode-syntax-table)
1652 (error nil))
1653
5e72c6b2
S
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))
3938cb82
S
1723(idlwave-define-abbrev "pv" "ptr_valid()" (idlwave-keyword-abbrev 1))
1724(idlwave-define-abbrev "ipv" "if ptr_valid() then" (idlwave-keyword-abbrev 6))
ff689efd 1725
5e72c6b2
S
1726;; This section is reserved words only. (From IDL user manual)
1727;;
1728(idlwave-define-abbrev "and" "and" (idlwave-keyword-abbrev 0 t) t)
1729(idlwave-define-abbrev "begin" "begin" (idlwave-keyword-abbrev 0 t) t)
1730(idlwave-define-abbrev "break" "break" (idlwave-keyword-abbrev 0 t) t)
1731(idlwave-define-abbrev "case" "case" (idlwave-keyword-abbrev 0 t) t)
1732(idlwave-define-abbrev "common" "common" (idlwave-keyword-abbrev 0 t) t)
1733(idlwave-define-abbrev "continue" "continue" (idlwave-keyword-abbrev 0 t) t)
1734(idlwave-define-abbrev "do" "do" (idlwave-keyword-abbrev 0 t) t)
1735(idlwave-define-abbrev "else" "else" (idlwave-keyword-abbrev 0 t) t)
1736(idlwave-define-abbrev "end" "end" 'idlwave-show-begin-check t)
1737(idlwave-define-abbrev "endcase" "endcase" 'idlwave-show-begin-check t)
1738(idlwave-define-abbrev "endelse" "endelse" 'idlwave-show-begin-check t)
1739(idlwave-define-abbrev "endfor" "endfor" 'idlwave-show-begin-check t)
1740(idlwave-define-abbrev "endif" "endif" 'idlwave-show-begin-check t)
1741(idlwave-define-abbrev "endrep" "endrep" 'idlwave-show-begin-check t)
1742(idlwave-define-abbrev "endswitch" "endswitch" 'idlwave-show-begin-check t)
1743(idlwave-define-abbrev "endwhi" "endwhi" 'idlwave-show-begin-check t)
1744(idlwave-define-abbrev "endwhile" "endwhile" 'idlwave-show-begin-check t)
1745(idlwave-define-abbrev "eq" "eq" (idlwave-keyword-abbrev 0 t) t)
1746(idlwave-define-abbrev "for" "for" (idlwave-keyword-abbrev 0 t) t)
1747(idlwave-define-abbrev "function" "function" (idlwave-keyword-abbrev 0 t) t)
1748(idlwave-define-abbrev "ge" "ge" (idlwave-keyword-abbrev 0 t) t)
1749(idlwave-define-abbrev "goto" "goto" (idlwave-keyword-abbrev 0 t) t)
1750(idlwave-define-abbrev "gt" "gt" (idlwave-keyword-abbrev 0 t) t)
1751(idlwave-define-abbrev "if" "if" (idlwave-keyword-abbrev 0 t) t)
1752(idlwave-define-abbrev "le" "le" (idlwave-keyword-abbrev 0 t) t)
1753(idlwave-define-abbrev "lt" "lt" (idlwave-keyword-abbrev 0 t) t)
1754(idlwave-define-abbrev "mod" "mod" (idlwave-keyword-abbrev 0 t) t)
1755(idlwave-define-abbrev "ne" "ne" (idlwave-keyword-abbrev 0 t) t)
1756(idlwave-define-abbrev "not" "not" (idlwave-keyword-abbrev 0 t) t)
1757(idlwave-define-abbrev "of" "of" (idlwave-keyword-abbrev 0 t) t)
1758(idlwave-define-abbrev "on_ioerror" "on_ioerror" (idlwave-keyword-abbrev 0 t) t)
1759(idlwave-define-abbrev "or" "or" (idlwave-keyword-abbrev 0 t) t)
1760(idlwave-define-abbrev "pro" "pro" (idlwave-keyword-abbrev 0 t) t)
1761(idlwave-define-abbrev "repeat" "repeat" (idlwave-keyword-abbrev 0 t) t)
1762(idlwave-define-abbrev "switch" "switch" (idlwave-keyword-abbrev 0 t) t)
1763(idlwave-define-abbrev "then" "then" (idlwave-keyword-abbrev 0 t) t)
1764(idlwave-define-abbrev "until" "until" (idlwave-keyword-abbrev 0 t) t)
1765(idlwave-define-abbrev "while" "while" (idlwave-keyword-abbrev 0 t) t)
1766(idlwave-define-abbrev "xor" "xor" (idlwave-keyword-abbrev 0 t) t)
f32b3b91
CD
1767
1768(defvar imenu-create-index-function)
1769(defvar extract-index-name-function)
1770(defvar prev-index-position-function)
1771(defvar imenu-extract-index-name-function)
1772(defvar imenu-prev-index-position-function)
5e72c6b2 1773;; defined later - so just make the compiler hush
4b1aaa8b 1774(defvar idlwave-mode-menu)
f32b3b91
CD
1775(defvar idlwave-mode-debug-menu)
1776
1777;;;###autoload
1778(defun idlwave-mode ()
e08734e2 1779 "Major mode for editing IDL source files (version 6.1_em22).
f32b3b91
CD
1780
1781The main features of this mode are
1782
17831. Indentation and Formatting
1784 --------------------------
1785 Like other Emacs programming modes, C-j inserts a newline and indents.
1786 TAB is used for explicit indentation of the current line.
1787
5e72c6b2
S
1788 To start a continuation line, use \\[idlwave-split-line]. This
1789 function can also be used in the middle of a line to split the line
1790 at that point. When used inside a long constant string, the string
1791 is split at that point with the `+' concatenation operator.
f32b3b91
CD
1792
1793 Comments are indented as follows:
1794
1795 `;;;' Indentation remains unchanged.
1796 `;;' Indent like the surrounding code
1797 `;' Indent to a minimum column.
1798
1799 The indentation of comments starting in column 0 is never changed.
1800
5e72c6b2
S
1801 Use \\[idlwave-fill-paragraph] to refill a paragraph inside a
1802 comment. The indentation of the second line of the paragraph
1803 relative to the first will be retained. Use
1804 \\[idlwave-auto-fill-mode] to toggle auto-fill mode for these
1805 comments. When the variable `idlwave-fill-comment-line-only' is
52a244eb 1806 nil, code can also be auto-filled and auto-indented.
f32b3b91
CD
1807
1808 To convert pre-existing IDL code to your formatting style, mark the
5e72c6b2
S
1809 entire buffer with \\[mark-whole-buffer] and execute
1810 \\[idlwave-expand-region-abbrevs]. Then mark the entire buffer
1811 again followed by \\[indent-region] (`indent-region').
f32b3b91
CD
1812
18132. Routine Info
1814 ------------
5e72c6b2
S
1815 IDLWAVE displays information about the calling sequence and the
1816 accepted keyword parameters of a procedure or function with
1817 \\[idlwave-routine-info]. \\[idlwave-find-module] jumps to the
1818 source file of a module. These commands know about system
1819 routines, all routines in idlwave-mode buffers and (when the
1820 idlwave-shell is active) about all modules currently compiled under
52a244eb
S
1821 this shell. It also makes use of pre-compiled or custom-scanned
1822 user and library catalogs many popular libraries ship with by
1823 default. Use \\[idlwave-update-routine-info] to update this
15e42531
CD
1824 information, which is also used for completion (see item 4).
1825
18263. Online IDL Help
1827 ---------------
f66f03de 1828
15e42531 1829 \\[idlwave-context-help] displays the IDL documentation relevant
f66f03de
S
1830 for the system variable, keyword, or routines at point. A single
1831 key stroke gets you directly to the right place in the docs. See
52a244eb 1832 the manual to configure where and how the HTML help is displayed.
f32b3b91 1833
15e42531 18344. Completion
f32b3b91 1835 ----------
15e42531 1836 \\[idlwave-complete] completes the names of procedures, functions
52a244eb
S
1837 class names, keyword parameters, system variables and tags, class
1838 tags, structure tags, filenames and much more. It is context
1839 sensitive and figures out what is expected at point. Lower case
1840 strings are completed in lower case, other strings in mixed or
1841 upper case.
f32b3b91 1842
15e42531 18435. Code Templates and Abbreviations
f32b3b91
CD
1844 --------------------------------
1845 Many Abbreviations are predefined to expand to code fragments and templates.
5a0c3f56 1846 The abbreviations start generally with a `\\`. Some examples:
f32b3b91
CD
1847
1848 \\pr PROCEDURE template
1849 \\fu FUNCTION template
1850 \\c CASE statement template
05a1abfc 1851 \\sw SWITCH statement template
f32b3b91
CD
1852 \\f FOR loop template
1853 \\r REPEAT Loop template
1854 \\w WHILE loop template
1855 \\i IF statement template
1856 \\elif IF-ELSE statement template
1857 \\b BEGIN
4b1aaa8b 1858
52a244eb
S
1859 For a full list, use \\[idlwave-list-abbrevs]. Some templates also
1860 have direct keybindings - see the list of keybindings below.
775591f7 1861
52a244eb
S
1862 \\[idlwave-doc-header] inserts a documentation header at the
1863 beginning of the current program unit (pro, function or main).
1864 Change log entries can be added to the current program unit with
1865 \\[idlwave-doc-modification].
f32b3b91 1866
15e42531 18676. Automatic Case Conversion
f32b3b91
CD
1868 -------------------------
1869 The case of reserved words and some abbrevs is controlled by
1870 `idlwave-reserved-word-upcase' and `idlwave-abbrev-change-case'.
1871
15e42531 18727. Automatic END completion
f32b3b91
CD
1873 ------------------------
1874 If the variable `idlwave-expand-generic-end' is non-nil, each END typed
1875 will be converted to the specific version, like ENDIF, ENDFOR, etc.
1876
15e42531 18778. Hooks
f32b3b91
CD
1878 -----
1879 Loading idlwave.el runs `idlwave-load-hook'.
1880 Turning on `idlwave-mode' runs `idlwave-mode-hook'.
1881
15e42531 18829. Documentation and Customization
f32b3b91 1883 -------------------------------
5e72c6b2
S
1884 Info documentation for this package is available. Use
1885 \\[idlwave-info] to display (complain to your sysadmin if that does
1886 not work). For Postscript, PDF, and HTML versions of the
855b42a2 1887 documentation, check IDLWAVE's homepage at URL `http://idlwave.org'.
f32b3b91
CD
1888 IDLWAVE has customize support - see the group `idlwave'.
1889
15e42531 189010.Keybindings
f32b3b91
CD
1891 -----------
1892 Here is a list of all keybindings of this mode.
1893 If some of the key bindings below show with ??, use \\[describe-key]
1894 followed by the key sequence to see what the key sequence does.
1895
1896\\{idlwave-mode-map}"
1897
1898 (interactive)
1899 (kill-all-local-variables)
4b1aaa8b 1900
f32b3b91
CD
1901 (if idlwave-startup-message
1902 (message "Emacs IDLWAVE mode version %s." idlwave-mode-version))
1903 (setq idlwave-startup-message nil)
4b1aaa8b 1904
f32b3b91
CD
1905 (setq local-abbrev-table idlwave-mode-abbrev-table)
1906 (set-syntax-table idlwave-mode-syntax-table)
4b1aaa8b 1907
f32b3b91 1908 (set (make-local-variable 'indent-line-function) 'idlwave-indent-and-action)
4b1aaa8b 1909
f32b3b91
CD
1910 (make-local-variable idlwave-comment-indent-function)
1911 (set idlwave-comment-indent-function 'idlwave-comment-hook)
4b1aaa8b 1912
f32b3b91
CD
1913 (set (make-local-variable 'comment-start-skip) ";+[ \t]*")
1914 (set (make-local-variable 'comment-start) ";")
0dc2be2f 1915 (set (make-local-variable 'comment-add) 1) ; ";;" for new and regions
f66f03de 1916 (set (make-local-variable 'require-final-newline) t)
f32b3b91
CD
1917 (set (make-local-variable 'abbrev-all-caps) t)
1918 (set (make-local-variable 'indent-tabs-mode) nil)
1919 (set (make-local-variable 'completion-ignore-case) t)
4b1aaa8b 1920
f32b3b91
CD
1921 (use-local-map idlwave-mode-map)
1922
1923 (when (featurep 'easymenu)
1924 (easy-menu-add idlwave-mode-menu idlwave-mode-map)
1925 (easy-menu-add idlwave-mode-debug-menu idlwave-mode-map))
1926
1927 (setq mode-name "IDLWAVE")
1928 (setq major-mode 'idlwave-mode)
1929 (setq abbrev-mode t)
4b1aaa8b 1930
f32b3b91
CD
1931 (set (make-local-variable idlwave-fill-function) 'idlwave-auto-fill)
1932 (setq comment-end "")
1933 (set (make-local-variable 'comment-multi-line) nil)
4b1aaa8b 1934 (set (make-local-variable 'paragraph-separate)
5e72c6b2 1935 "[ \t\f]*$\\|[ \t]*;+[ \t]*$\\|;+[+=-_*]+$")
f32b3b91
CD
1936 (set (make-local-variable 'paragraph-start) "[ \t\f]\\|[ \t]*;+[ \t]")
1937 (set (make-local-variable 'paragraph-ignore-fill-prefix) nil)
76959b77 1938 (set (make-local-variable 'parse-sexp-ignore-comments) t)
775591f7 1939
e08734e2 1940 ;; ChangeLog
8c43762b 1941 (set (make-local-variable 'add-log-current-defun-function)
e08734e2
S
1942 'idlwave-current-routine-fullname)
1943
f32b3b91
CD
1944 ;; Set tag table list to use IDLTAGS as file name.
1945 (if (boundp 'tag-table-alist)
1946 (add-to-list 'tag-table-alist '("\\.pro$" . "IDLTAGS")))
4b1aaa8b 1947
e08734e2 1948 ;; Font-lock additions
52a244eb 1949 ;; Following line is for Emacs - XEmacs uses the corresponding property
f32b3b91
CD
1950 ;; on the `idlwave-mode' symbol.
1951 (set (make-local-variable 'font-lock-defaults) idlwave-font-lock-defaults)
0dc2be2f
S
1952 (set (make-local-variable 'font-lock-mark-block-function)
1953 'idlwave-mark-subprogram)
1954 (set (make-local-variable 'font-lock-fontify-region-function)
1955 'idlwave-font-lock-fontify-region)
f32b3b91
CD
1956
1957 ;; Imenu setup
1958 (set (make-local-variable 'imenu-create-index-function)
1959 'imenu-default-create-index-function)
1960 (set (make-local-variable 'imenu-extract-index-name-function)
1961 'idlwave-unit-name)
1962 (set (make-local-variable 'imenu-prev-index-position-function)
1963 'idlwave-prev-index-position)
1964
0dc2be2f
S
1965 ;; HideShow setup
1966 (add-to-list 'hs-special-modes-alist
1967 (list 'idlwave-mode
1968 idlwave-begin-block-reg
1969 idlwave-end-block-reg
1970 ";"
1971 'idlwave-forward-block nil))
4b1aaa8b 1972
f32b3b91 1973 ;; Make a local post-command-hook and add our hook to it
f66f03de
S
1974 ;; NB: `make-local-hook' needed for older/alternative Emacs compatibility
1975 ;; (make-local-hook 'post-command-hook)
15e42531
CD
1976 (add-hook 'post-command-hook 'idlwave-command-hook nil 'local)
1977
1978 ;; Make local hooks for buffer updates
f66f03de
S
1979 ;; NB: `make-local-hook' needed for older/alternative Emacs compatibility
1980 ;; (make-local-hook 'kill-buffer-hook)
15e42531 1981 (add-hook 'kill-buffer-hook 'idlwave-kill-buffer-update nil 'local)
f66f03de 1982 ;; (make-local-hook 'after-save-hook)
e08734e2 1983 (add-hook 'after-save-hook 'idlwave-save-buffer-update nil 'local)
15e42531
CD
1984 (add-hook 'after-save-hook 'idlwave-revoke-license-to-kill nil 'local)
1985
52a244eb
S
1986 ;; Setup directories and file, if necessary
1987 (idlwave-setup)
1988
15e42531
CD
1989 ;; Update the routine info with info about current buffer?
1990 (idlwave-new-buffer-update)
f32b3b91 1991
f66f03de
S
1992 ;; Check help location
1993 (idlwave-help-check-locations)
1994
f32b3b91 1995 ;; Run the mode hook
9a969196 1996 (run-mode-hooks 'idlwave-mode-hook))
f32b3b91 1997
52a244eb
S
1998(defvar idlwave-setup-done nil)
1999(defun idlwave-setup ()
2000 (unless idlwave-setup-done
2001 (if (not (file-directory-p idlwave-config-directory))
2002 (make-directory idlwave-config-directory))
4b1aaa8b
PE
2003 (setq
2004 idlwave-user-catalog-file (expand-file-name
2005 idlwave-user-catalog-file
f66f03de 2006 idlwave-config-directory)
4b1aaa8b
PE
2007 idlwave-xml-system-rinfo-converted-file
2008 (expand-file-name
f66f03de
S
2009 idlwave-xml-system-rinfo-converted-file
2010 idlwave-config-directory)
4b1aaa8b
PE
2011 idlwave-path-file (expand-file-name
2012 idlwave-path-file
f66f03de 2013 idlwave-config-directory))
52a244eb
S
2014 (idlwave-read-paths) ; we may need these early
2015 (setq idlwave-setup-done t)))
2016
0dc2be2f
S
2017(defun idlwave-font-lock-fontify-region (beg end &optional verbose)
2018 "Fontify continuation lines correctly."
2019 (let (pos)
2020 (save-excursion
2021 (goto-char beg)
2022 (forward-line -1)
2023 (when (setq pos (idlwave-is-continuation-line))
2024 (goto-char pos)
2025 (idlwave-beginning-of-statement)
2026 (setq beg (point)))))
2027 (font-lock-default-fontify-region beg end verbose))
2028
f32b3b91 2029;;
52a244eb 2030;; Code Formatting ----------------------------------------------------
4b1aaa8b 2031;;
f32b3b91 2032
f32b3b91 2033(defun idlwave-hard-tab ()
5a0c3f56 2034 "Insert TAB in buffer in current position."
f32b3b91
CD
2035 (interactive)
2036 (insert "\t"))
2037
2038;;; This stuff is experimental
2039
2040(defvar idlwave-command-hook nil
2041 "If non-nil, a list that can be evaluated using `eval'.
2042It is evaluated in the lisp function `idlwave-command-hook' which is
2043placed in `post-command-hook'.")
2044
2045(defun idlwave-command-hook ()
2046 "Command run after every command.
2047Evaluates a non-nil value of the *variable* `idlwave-command-hook' and
2048sets the variable to zero afterwards."
2049 (and idlwave-command-hook
2050 (listp idlwave-command-hook)
2051 (condition-case nil
2052 (eval idlwave-command-hook)
2053 (error nil)))
2054 (setq idlwave-command-hook nil))
2055
2056;;; End experiment
2057
2058;; It would be better to use expand.el for better abbrev handling and
2059;; versatility.
2060
2061(defun idlwave-check-abbrev (arg &optional reserved)
5a0c3f56 2062 "Reverse abbrev expansion if in comment or string.
f32b3b91
CD
2063Argument ARG is the number of characters to move point
2064backward if `idlwave-abbrev-move' is non-nil.
2065If optional argument RESERVED is non-nil then the expansion
2066consists of reserved words, which will be capitalized if
2067`idlwave-reserved-word-upcase' is non-nil.
2068Otherwise, the abbrev will be capitalized if `idlwave-abbrev-change-case'
2069is non-nil, unless its value is \`down in which case the abbrev will be
2070made into all lowercase.
2071Returns non-nil if abbrev is left expanded."
2072 (if (idlwave-quoted)
2073 (progn (unexpand-abbrev)
2074 nil)
2075 (if (and reserved idlwave-reserved-word-upcase)
2076 (upcase-region last-abbrev-location (point))
2077 (cond
2078 ((equal idlwave-abbrev-change-case 'down)
2079 (downcase-region last-abbrev-location (point)))
2080 (idlwave-abbrev-change-case
2081 (upcase-region last-abbrev-location (point)))))
2082 (if (and idlwave-abbrev-move (> arg 0))
2083 (if (boundp 'post-command-hook)
2084 (setq idlwave-command-hook (list 'backward-char (1+ arg)))
2085 (backward-char arg)))
2086 t))
2087
2088(defun idlwave-in-comment ()
5a0c3f56 2089 "Return t if point is inside a comment, nil otherwise."
f32b3b91
CD
2090 (save-excursion
2091 (let ((here (point)))
2092 (and (idlwave-goto-comment) (> here (point))))))
2093
2094(defun idlwave-goto-comment ()
2095 "Move to start of comment delimiter on current line.
2096Moves to end of line if there is no comment delimiter.
2097Ignores comment delimiters in strings.
2098Returns point if comment found and nil otherwise."
2099 (let ((eos (progn (end-of-line) (point)))
2100 (data (match-data))
2101 found)
2102 ;; Look for first comment delimiter not in a string
2103 (beginning-of-line)
2104 (setq found (search-forward comment-start eos 'lim))
2105 (while (and found (idlwave-in-quote))
2106 (setq found (search-forward comment-start eos 'lim)))
2107 (store-match-data data)
2108 (and found (not (idlwave-in-quote))
2109 (progn
2110 (backward-char 1)
2111 (point)))))
2112
5e72c6b2 2113(defun idlwave-region-active-p ()
a00e54f7
RS
2114 "Should we operate on an active region?"
2115 (if (fboundp 'use-region-p)
2116 (use-region-p)
2117 (region-active-p)))
5e72c6b2 2118
f32b3b91
CD
2119(defun idlwave-show-matching-quote ()
2120 "Insert quote and show matching quote if this is end of a string."
2121 (interactive)
2122 (let ((bq (idlwave-in-quote))
1ba983e8 2123 (inq last-command-event))
f32b3b91
CD
2124 (if (and bq (not (idlwave-in-comment)))
2125 (let ((delim (char-after bq)))
2126 (insert inq)
2127 (if (eq inq delim)
2128 (save-excursion
2129 (goto-char bq)
2130 (sit-for 1))))
2131 ;; Not the end of a string
2132 (insert inq))))
2133
2134(defun idlwave-show-begin-check ()
2135 "Ensure that the previous word was a token before `idlwave-show-begin'.
2136An END token must be preceded by whitespace."
5e72c6b2
S
2137 (if (not (idlwave-quoted))
2138 (if
2139 (save-excursion
2140 (backward-word 1)
2141 (backward-char 1)
2142 (looking-at "[ \t\n\f]"))
2143 (idlwave-show-begin))))
f32b3b91
CD
2144
2145(defun idlwave-show-begin ()
5a0c3f56
JB
2146 "Find the start of current block and blinks to it for a second.
2147Also checks if the correct END statement has been used."
f32b3b91 2148 ;; All end statements are reserved words
76959b77 2149 ;; Re-indent end line
52a244eb
S
2150 ;;(insert-char ?\ 1) ;; So indent, etc. work well
2151 ;;(backward-char 1)
76959b77
S
2152 (let* ((pos (point-marker))
2153 (last-abbrev-marker (copy-marker last-abbrev-location))
2154 (eol-pos (save-excursion (end-of-line) (point)))
2155 begin-pos end-pos end end1 )
2156 (if idlwave-reindent-end (idlwave-indent-line))
52a244eb 2157 (setq last-abbrev-location (marker-position last-abbrev-marker))
f32b3b91
CD
2158 (when (and (idlwave-check-abbrev 0 t)
2159 idlwave-show-block)
2160 (save-excursion
2161 ;; Move inside current block
76959b77 2162 (goto-char last-abbrev-marker)
f32b3b91 2163 (idlwave-block-jump-out -1 'nomark)
76959b77
S
2164 (setq begin-pos (point))
2165 (idlwave-block-jump-out 1 'nomark)
2166 (setq end-pos (point))
2167 (if (> end-pos eol-pos)
2168 (setq end-pos pos))
2169 (goto-char end-pos)
4b1aaa8b 2170 (setq end (buffer-substring
76959b77
S
2171 (progn
2172 (skip-chars-backward "a-zA-Z")
2173 (point))
2174 end-pos))
2175 (goto-char begin-pos)
f32b3b91
CD
2176 (when (setq end1 (cdr (idlwave-block-master)))
2177 (cond
5e72c6b2 2178 ((null end1)) ; no-operation
f32b3b91
CD
2179 ((string= (downcase end) (downcase end1))
2180 (sit-for 1))
2181 ((string= (downcase end) "end")
2182 ;; A generic end
2183 (if idlwave-expand-generic-end
2184 (save-excursion
2185 (goto-char pos)
2186 (backward-char 3)
2187 (insert (if (string= end "END") (upcase end1) end1))
2188 (delete-char 3)))
2189 (sit-for 1))
2190 (t
2191 (beep)
4b1aaa8b 2192 (message "Warning: Shouldn't this be \"%s\" instead of \"%s\"?"
f32b3b91 2193 end1 end)
52a244eb
S
2194 (sit-for 1))))))))
2195 ;;(delete-char 1))
f32b3b91
CD
2196
2197(defun idlwave-block-master ()
2198 (let ((case-fold-search t))
2199 (save-excursion
2200 (cond
05a1abfc 2201 ((looking-at "pro\\|case\\|switch\\|function\\>")
f32b3b91
CD
2202 (assoc (downcase (match-string 0)) idlwave-block-matches))
2203 ((looking-at "begin\\>")
4b1aaa8b
PE
2204 (let ((limit (save-excursion
2205 (idlwave-beginning-of-statement)
f32b3b91
CD
2206 (point))))
2207 (cond
52a244eb
S
2208 ((re-search-backward ":[ \t]*\\=" limit t)
2209 ;; seems to be a case thing
2210 '("begin" . "end"))
f32b3b91
CD
2211 ((re-search-backward idlwave-block-match-regexp limit t)
2212 (assoc (downcase (match-string 1))
2213 idlwave-block-matches))
f32b3b91 2214 (t
52a244eb 2215 ;; Just a normal block
f32b3b91
CD
2216 '("begin" . "end")))))
2217 (t nil)))))
2218
2219(defun idlwave-close-block ()
2220 "Terminate the current block with the correct END statement."
2221 (interactive)
f32b3b91
CD
2222 ;; Start new line if we are not in a new line
2223 (unless (save-excursion
2224 (skip-chars-backward " \t")
2225 (bolp))
2226 (let ((idlwave-show-block nil))
2227 (newline-and-indent)))
5e72c6b2
S
2228 (let ((last-abbrev-location (point))) ; for upcasing
2229 (insert "end")
2230 (idlwave-show-begin)))
2231
f66f03de 2232(defun idlwave-custom-ampersand-surround (&optional is-action)
5a0c3f56 2233 "Surround &, leaving room for && (which surround as well)."
f66f03de
S
2234 (let* ((prev-char (char-after (- (point) 2)))
2235 (next-char (char-after (point)))
2236 (amp-left (eq prev-char ?&))
2237 (amp-right (eq next-char ?&))
2238 (len (if amp-left 2 1)))
2239 (unless amp-right ;no need to do it twice, amp-left will catch it.
2240 (idlwave-surround -1 (if (or is-action amp-left) -1) len))))
2241
2242(defun idlwave-custom-ltgtr-surround (gtr &optional is-action)
2243 "Surround > and < by blanks, leaving room for >= and <=, and considering ->."
2244 (let* ((prev-char (char-after (- (point) 2)))
2245 (next-char (char-after (point)))
2246 (method-invoke (and gtr (eq prev-char ?-)))
2247 (len (if method-invoke 2 1)))
2248 (unless (eq next-char ?=)
2249 ;; Key binding: pad only on left, to save for possible >=/<=
2250 (idlwave-surround -1 (if (or is-action method-invoke) -1) len))))
2251
2252(defun idlwave-surround (&optional before after length is-action)
595ab50b
CD
2253 "Surround the LENGTH characters before point with blanks.
2254LENGTH defaults to 1.
f32b3b91 2255Optional arguments BEFORE and AFTER affect the behavior before and
595ab50b
CD
2256after the characters (see also description of `idlwave-make-space'):
2257
2258nil do nothing
22590 force no spaces
2260integer > 0 force exactly n spaces
2261integer < 0 at least |n| spaces
f32b3b91
CD
2262
2263The function does nothing if any of the following conditions is true:
2264- `idlwave-surround-by-blank' is nil
f66f03de 2265- the character before point is inside a string or comment"
5e72c6b2 2266 (when (and idlwave-surround-by-blank (not (idlwave-quoted)))
f66f03de
S
2267 (let ((length (or length 1))) ; establish a default for LENGTH
2268 (backward-char length)
2269 (save-restriction
2270 (let ((here (point)))
2271 (skip-chars-backward " \t")
2272 (if (bolp)
2273 ;; avoid clobbering indent
2274 (progn
2275 (move-to-column (idlwave-calculate-indent))
2276 (if (<= (point) here)
2277 (narrow-to-region (point) here))
2278 (goto-char here)))
2279 (idlwave-make-space before))
2280 (skip-chars-forward " \t"))
2281 (forward-char length)
2282 (idlwave-make-space after)
2283 ;; Check to see if the line should auto wrap
2284 (if (and (equal (char-after (1- (point))) ?\ )
2285 (> (current-column) fill-column))
2286 (funcall auto-fill-function)))))
f32b3b91
CD
2287
2288(defun idlwave-make-space (n)
2289 "Make space at point.
2290The space affected is all the spaces and tabs around point.
2291If n is non-nil then point is left abs(n) spaces from the beginning of
2292the contiguous space.
2293The amount of space at point is determined by N.
2294If the value of N is:
2295nil - do nothing.
595ab50b
CD
2296> 0 - exactly N spaces.
2297< 0 - a minimum of -N spaces, i.e., do not change if there are
2298 already -N spaces.
22990 - no spaces (i.e. remove any existing space)."
f32b3b91
CD
2300 (if (integerp n)
2301 (let
2302 ((start-col (progn (skip-chars-backward " \t") (current-column)))
2303 (left (point))
2304 (end-col (progn (skip-chars-forward " \t") (current-column))))
2305 (delete-horizontal-space)
2306 (cond
2307 ((> n 0)
2308 (idlwave-indent-to (+ start-col n))
2309 (goto-char (+ left n)))
2310 ((< n 0)
2311 (idlwave-indent-to end-col (- n))
2312 (goto-char (- left n)))
2313 ;; n = 0, done
2314 ))))
2315
2316(defun idlwave-newline ()
5a0c3f56 2317 "Insert a newline and indent the current and previous line."
f32b3b91
CD
2318 (interactive)
2319 ;;
2320 ;; Handle unterminated single and double quotes
2321 ;; If not in a comment and in a string then insertion of a newline
2322 ;; will mean unbalanced quotes.
2323 ;;
2324 (if (and (not (idlwave-in-comment)) (idlwave-in-quote))
2325 (progn (beep)
2326 (message "Warning: unbalanced quotes?")))
2327 (newline)
2328 ;;
2329 ;; The current line is being split, the cursor should be at the
2330 ;; beginning of the new line skipping the leading indentation.
2331 ;;
2332 ;; The reason we insert the new line before indenting is that the
2333 ;; indenting could be confused by keywords (e.g. END) on the line
2334 ;; after the split point. This prevents us from just using
2335 ;; `indent-for-tab-command' followed by `newline-and-indent'.
2336 ;;
2337 (beginning-of-line 0)
2338 (idlwave-indent-line)
2339 (forward-line)
2340 (idlwave-indent-line))
2341
2342;;
2343;; Use global variable 'comment-column' to set parallel comment
2344;;
2345;; Modeled on lisp.el
2346;; Emacs Lisp and IDL (Wave CL) have identical comment syntax
2347(defun idlwave-comment-hook ()
2348 "Compute indent for the beginning of the IDL comment delimiter."
2349 (if (or (looking-at idlwave-no-change-comment)
8d222148 2350 (looking-at (or idlwave-begin-line-comment "^;")))
f32b3b91
CD
2351 (current-column)
2352 (if (looking-at idlwave-code-comment)
2353 (if (save-excursion (skip-chars-backward " \t") (bolp))
2354 ;; On line by itself, indent as code
2355 (let ((tem (idlwave-calculate-indent)))
2356 (if (listp tem) (car tem) tem))
2357 ;; after code - do not change
2358 (current-column))
2359 (skip-chars-backward " \t")
2360 (max (if (bolp) 0 (1+ (current-column)))
2361 comment-column))))
2362
2363(defun idlwave-split-line ()
2364 "Continue line by breaking line at point and indent the lines.
5a0c3f56 2365For a code line insert continuation marker. If the line is a line comment
f32b3b91
CD
2366then the new line will contain a comment with the same indentation.
2367Splits strings with the IDL operator `+' if `idlwave-split-line-string' is
2368non-nil."
2369 (interactive)
15e42531
CD
2370 ;; Expand abbreviation, just like normal RET would.
2371 (and abbrev-mode (expand-abbrev))
f32b3b91
CD
2372 (let (beg)
2373 (if (not (idlwave-in-comment))
2374 ;; For code line add continuation.
2375 ;; Check if splitting a string.
2376 (progn
2377 (if (setq beg (idlwave-in-quote))
2378 (if idlwave-split-line-string
2379 ;; Split the string.
2380 (progn (insert (setq beg (char-after beg)) " + "
2381 idlwave-continuation-char beg)
5e72c6b2
S
2382 (backward-char 1)
2383 (newline-and-indent)
2384 (forward-char 1))
f32b3b91
CD
2385 ;; Do not split the string.
2386 (beep)
2387 (message "Warning: continuation inside string!!")
2388 (insert " " idlwave-continuation-char))
2389 ;; Not splitting a string.
15e42531
CD
2390 (if (not (member (char-before) '(?\ ?\t)))
2391 (insert " "))
5e72c6b2
S
2392 (insert idlwave-continuation-char)
2393 (newline-and-indent)))
f32b3b91
CD
2394 (indent-new-comment-line))
2395 ;; Indent previous line
2396 (setq beg (- (point-max) (point)))
2397 (forward-line -1)
2398 (idlwave-indent-line)
2399 (goto-char (- (point-max) beg))
2400 ;; Reindent new line
2401 (idlwave-indent-line)))
2402
cca13260 2403(defun idlwave-beginning-of-subprogram (&optional nomark)
5a0c3f56 2404 "Move point to the beginning of the current program unit.
cca13260 2405If NOMARK is non-nil, do not push mark."
f32b3b91 2406 (interactive)
cca13260 2407 (idlwave-find-key idlwave-begin-unit-reg -1 nomark))
f32b3b91 2408
cca13260 2409(defun idlwave-end-of-subprogram (&optional nomark)
5a0c3f56 2410 "Move point to the start of the next program unit.
cca13260 2411If NOMARK is non-nil, do not push mark."
f32b3b91
CD
2412 (interactive)
2413 (idlwave-end-of-statement)
cca13260 2414 (idlwave-find-key idlwave-end-unit-reg 1 nomark))
f32b3b91
CD
2415
2416(defun idlwave-mark-statement ()
2417 "Mark current IDL statement."
2418 (interactive)
2419 (idlwave-end-of-statement)
2420 (let ((end (point)))
2421 (idlwave-beginning-of-statement)
0dc2be2f 2422 (push-mark end nil t)))
f32b3b91
CD
2423
2424(defun idlwave-mark-block ()
2425 "Mark containing block."
2426 (interactive)
2427 (idlwave-end-of-statement)
2428 (idlwave-backward-up-block -1)
2429 (idlwave-end-of-statement)
2430 (let ((end (point)))
2431 (idlwave-backward-block)
2432 (idlwave-beginning-of-statement)
0dc2be2f 2433 (push-mark end nil t)))
f32b3b91
CD
2434
2435
2436(defun idlwave-mark-subprogram ()
2437 "Put mark at beginning of program, point at end.
2438The marks are pushed."
2439 (interactive)
2440 (idlwave-end-of-statement)
2441 (idlwave-beginning-of-subprogram)
2442 (let ((beg (point)))
2443 (idlwave-forward-block)
0dc2be2f 2444 (push-mark beg nil t))
f32b3b91
CD
2445 (exchange-point-and-mark))
2446
2447(defun idlwave-backward-up-block (&optional arg)
2448 "Move to beginning of enclosing block if prefix ARG >= 0.
2449If prefix ARG < 0 then move forward to enclosing block end."
2450 (interactive "p")
2451 (idlwave-block-jump-out (- arg) 'nomark))
2452
2453(defun idlwave-beginning-of-block ()
2454 "Go to the beginning of the current block."
2455 (interactive)
2456 (idlwave-block-jump-out -1 'nomark)
2457 (forward-word 1))
2458
2459(defun idlwave-end-of-block ()
2460 "Go to the beginning of the current block."
2461 (interactive)
2462 (idlwave-block-jump-out 1 'nomark)
2463 (backward-word 1))
2464
0dc2be2f 2465(defun idlwave-forward-block (&optional arg)
f32b3b91
CD
2466 "Move across next nested block."
2467 (interactive)
0dc2be2f
S
2468 (let ((arg (or arg 1)))
2469 (if (idlwave-down-block arg)
2470 (idlwave-block-jump-out arg 'nomark))))
f32b3b91
CD
2471
2472(defun idlwave-backward-block ()
2473 "Move backward across previous nested block."
2474 (interactive)
2475 (if (idlwave-down-block -1)
2476 (idlwave-block-jump-out -1 'nomark)))
2477
2478(defun idlwave-down-block (&optional arg)
2479 "Go down a block.
2480With ARG: ARG >= 0 go forwards, ARG < 0 go backwards.
2481Returns non-nil if successfull."
2482 (interactive "p")
2483 (let (status)
2484 (if (< arg 0)
2485 ;; Backward
2486 (let ((eos (save-excursion
2487 (idlwave-block-jump-out -1 'nomark)
2488 (point))))
4b1aaa8b 2489 (if (setq status (idlwave-find-key
f32b3b91
CD
2490 idlwave-end-block-reg -1 'nomark eos))
2491 (idlwave-beginning-of-statement)
2492 (message "No nested block before beginning of containing block.")))
2493 ;; Forward
2494 (let ((eos (save-excursion
2495 (idlwave-block-jump-out 1 'nomark)
2496 (point))))
4b1aaa8b 2497 (if (setq status (idlwave-find-key
f32b3b91
CD
2498 idlwave-begin-block-reg 1 'nomark eos))
2499 (idlwave-end-of-statement)
2500 (message "No nested block before end of containing block."))))
2501 status))
2502
2503(defun idlwave-mark-doclib ()
2504 "Put point at beginning of doc library header, mark at end.
2505The marks are pushed."
2506 (interactive)
2507 (let (beg
2508 (here (point)))
2509 (goto-char (point-max))
2510 (if (re-search-backward idlwave-doclib-start nil t)
4b1aaa8b 2511 (progn
f32b3b91
CD
2512 (setq beg (progn (beginning-of-line) (point)))
2513 (if (re-search-forward idlwave-doclib-end nil t)
2514 (progn
2515 (forward-line 1)
0dc2be2f 2516 (push-mark beg nil t)
f32b3b91
CD
2517 (message "Could not find end of doc library header.")))
2518 (message "Could not find doc library header start.")
2519 (goto-char here)))))
2520
e08734e2
S
2521(defun idlwave-current-routine-fullname ()
2522 (let ((name (idlwave-current-routine)))
2523 (idlwave-make-full-name (nth 2 name) (car name))))
2524
15e42531
CD
2525(defun idlwave-current-routine ()
2526 "Return (NAME TYPE CLASS) of current routine."
2527 (idlwave-routines)
2528 (save-excursion
cca13260 2529 (idlwave-beginning-of-subprogram 'nomark)
15e42531
CD
2530 (if (looking-at "[ \t]*\\<\\(pro\\|function\\)\\>\\s-+\\(\\([a-zA-Z0-9$_]+\\)::\\)?\\([a-zA-Z0-9$_]+\\)")
2531 (let* ((type (if (string= (downcase (match-string 1)) "pro")
2532 'pro 'function))
2533 (class (idlwave-sintern-class (match-string 3)))
2534 (name (idlwave-sintern-routine-or-method (match-string 4) class)))
2535 (list name type class)))))
2536
f32b3b91
CD
2537(defvar idlwave-shell-prompt-pattern)
2538(defun idlwave-beginning-of-statement ()
2539 "Move to beginning of the current statement.
2540Skips back past statement continuations.
2541Point is placed at the beginning of the line whether or not this is an
2542actual statement."
2543 (interactive)
2544 (cond
2545 ((eq major-mode 'idlwave-shell-mode)
2546 (if (re-search-backward idlwave-shell-prompt-pattern nil t)
2547 (goto-char (match-end 0))))
4b1aaa8b 2548 (t
f32b3b91
CD
2549 (if (save-excursion (forward-line -1) (idlwave-is-continuation-line))
2550 (idlwave-previous-statement)
2551 (beginning-of-line)))))
2552
2553(defun idlwave-previous-statement ()
5a0c3f56 2554 "Move point to beginning of the previous statement.
f32b3b91
CD
2555Returns t if the current line before moving is the beginning of
2556the first non-comment statement in the file, and nil otherwise."
2557 (interactive)
2558 (let (first-statement)
2559 (if (not (= (forward-line -1) 0))
2560 ;; first line in file
2561 t
2562 ;; skip blank lines, label lines, include lines and line comments
2563 (while (and
2564 ;; The current statement is the first statement until we
2565 ;; reach another statement.
2566 (setq first-statement
2567 (or
2568 (looking-at idlwave-comment-line-start-skip)
2569 (looking-at "[ \t]*$")
2570 (looking-at (concat "[ \t]*" idlwave-label "[ \t]*$"))
2571 (looking-at "^@")))
2572 (= (forward-line -1) 0)))
2573 ;; skip continuation lines
2574 (while (and
2575 (save-excursion
2576 (forward-line -1)
2577 (idlwave-is-continuation-line))
2578 (= (forward-line -1) 0)))
2579 first-statement)))
2580
f32b3b91 2581(defun idlwave-end-of-statement ()
5a0c3f56 2582 "Move point to the end of the current IDL statement.
05a1abfc
CD
2583If not in a statement just moves to end of line. Returns position."
2584 (interactive)
2585 (while (and (idlwave-is-continuation-line)
2586 (= (forward-line 1) 0))
2587 (while (and (idlwave-is-comment-or-empty-line)
2588 (= (forward-line 1) 0))))
2589 (end-of-line)
2590 (point))
2591
2592(defun idlwave-end-of-statement0 ()
5a0c3f56
JB
2593 "Move point to the end of the current IDL statement.
2594If not in a statement just moves to end of line. Returns position."
f32b3b91
CD
2595 (interactive)
2596 (while (and (idlwave-is-continuation-line)
2597 (= (forward-line 1) 0)))
2598 (end-of-line)
2599 (point))
2600
2601(defun idlwave-next-statement ()
5a0c3f56
JB
2602 "Move point to beginning of the next IDL statement.
2603Returns t if that statement is the last non-comment IDL statement
2604in the file, and nil otherwise."
f32b3b91
CD
2605 (interactive)
2606 (let (last-statement)
2607 (idlwave-end-of-statement)
2608 ;; skip blank lines, label lines, include lines and line comments
2609 (while (and (= (forward-line 1) 0)
2610 ;; The current statement is the last statement until
2611 ;; we reach a new statement.
2612 (setq last-statement
2613 (or
2614 (looking-at idlwave-comment-line-start-skip)
2615 (looking-at "[ \t]*$")
2616 (looking-at (concat "[ \t]*" idlwave-label "[ \t]*$"))
2617 (looking-at "^@")))))
2618 last-statement))
2619
76959b77
S
2620(defun idlwave-skip-multi-commands (&optional lim)
2621 "Skip past multiple commands on a line (with `&')."
2622 (let ((save-point (point)))
2623 (when (re-search-forward ".*&" lim t)
2624 (goto-char (match-end 0))
4b1aaa8b 2625 (if (idlwave-quoted)
6b75c9af
S
2626 (goto-char save-point)
2627 (if (eq (char-after (- (point) 2)) ?&) (goto-char save-point))))
76959b77
S
2628 (point)))
2629
15e42531 2630(defun idlwave-skip-label-or-case ()
f32b3b91
CD
2631 "Skip label or case statement element.
2632Returns position after label.
2633If there is no label point is not moved and nil is returned."
15e42531
CD
2634 ;; Case expressions and labels are terminated by a colon.
2635 ;; So we find the first colon in the line and make sure
2636 ;; - no `?' is before it (might be a ? b : c)
2637 ;; - it is not in a comment
2638 ;; - not in a string constant
2639 ;; - not in parenthesis (like a[0:3])
5e72c6b2 2640 ;; - not followed by another ":" in explicit class, ala a->b::c
15e42531 2641 ;; As many in this mode, this function is heuristic and not an exact
4b1aaa8b 2642 ;; parser.
5e72c6b2
S
2643 (let* ((start (point))
2644 (eos (save-excursion (idlwave-end-of-statement) (point)))
2645 (end (idlwave-find-key ":" 1 'nomark eos)))
f32b3b91 2646 (if (and end
15e42531 2647 (= (nth 0 (parse-partial-sexp start end)) 0)
5e72c6b2
S
2648 (not (string-match "\\?" (buffer-substring start end)))
2649 (not (string-match "^::" (buffer-substring end eos))))
f32b3b91
CD
2650 (progn
2651 (forward-char)
2652 (point))
2653 (goto-char start)
2654 nil)))
2655
2656(defun idlwave-start-of-substatement (&optional pre)
2657 "Move to start of next IDL substatement after point.
2658Uses the type of the current IDL statement to determine if the next
2659statement is on a new line or is a subpart of the current statement.
2660Returns point at start of substatement modulo whitespace.
2661If optional argument is non-nil move to beginning of current
15e42531 2662substatement."
f32b3b91
CD
2663 (let ((orig (point))
2664 (eos (idlwave-end-of-statement))
2665 (ifnest 0)
2666 st nst last)
2667 (idlwave-beginning-of-statement)
15e42531 2668 (idlwave-skip-label-or-case)
52a244eb
S
2669 (if (< (point) orig)
2670 (idlwave-skip-multi-commands orig))
f32b3b91
CD
2671 (setq last (point))
2672 ;; Continue looking for substatements until we are past orig
2673 (while (and (<= (point) orig) (not (eobp)))
2674 (setq last (point))
2675 (setq nst (nth 1 (cdr (setq st (car (idlwave-statement-type))))))
2676 (if (equal (car st) 'if) (setq ifnest (1+ ifnest)))
2677 (cond ((and nst
2678 (idlwave-find-key nst 1 'nomark eos))
2679 (goto-char (match-end 0)))
2680 ((and (> ifnest 0) (idlwave-find-key "\\<else\\>" 1 'nomark eos))
2681 (setq ifnest (1- ifnest))
2682 (goto-char (match-end 0)))
2683 (t (setq ifnest 0)
2684 (idlwave-next-statement))))
2685 (if pre (goto-char last))
15e42531
CD
2686 ;; If a continuation line starts here, move to next line
2687 (if (looking-at "[ \t]*\\$\\([ \t]*\\(;\\|$\\)\\)")
2688 (beginning-of-line 2))
f32b3b91
CD
2689 (point)))
2690
2691(defun idlwave-statement-type ()
2692 "Return the type of the current IDL statement.
2693Uses `idlwave-statement-match' to return a cons of (type . point) with
5a0c3f56 2694point the ending position where the type was determined. Type is the
f32b3b91 2695association from `idlwave-statement-match', i.e. the cons cell from the
5a0c3f56 2696list not just the type symbol. Returns nil if not an identifiable
f32b3b91
CD
2697statement."
2698 (save-excursion
2699 ;; Skip whitespace within a statement which is spaces, tabs, continuations
76959b77
S
2700 ;; and possibly comments
2701 (while (looking-at "[ \t]*\\$")
f32b3b91
CD
2702 (forward-line 1))
2703 (skip-chars-forward " \t")
2704 (let ((st idlwave-statement-match)
2705 (case-fold-search t))
2706 (while (and (not (looking-at (nth 0 (cdr (car st)))))
2707 (setq st (cdr st))))
2708 (if st
2709 (append st (match-end 0))))))
2710
f66f03de 2711(defun idlwave-expand-equal (&optional before after is-action)
5a0c3f56
JB
2712 "Pad '=' with spaces.
2713Two cases: Assignment statement, and keyword assignment.
2714Which case is determined using `idlwave-start-of-substatement' and
2715`idlwave-statement-type'. The equal sign will be surrounded by BEFORE
2716and AFTER blanks. If `idlwave-pad-keyword' is t then keyword assignment
2717is treated just like assignment statements. When nil, spaces are
2718removed for keyword assignment. Any other value keeps the current space
2719around the `='. Limits in for loops are treated as keyword assignment.
52a244eb
S
2720
2721Starting with IDL 6.0, a number of op= assignments are available.
2722Since ambiguities of the form:
2723
2724r and= b
2725rand= b
2726
2727can occur, alphanumeric operator assignment will never be pre-padded,
2728only post-padded. You must use a space before these to disambiguate
2729\(not just for padding, but for proper parsing by IDL too!). Other
2730operators, such as ##=, ^=, etc., will be pre-padded.
2731
f66f03de
S
2732IS-ACTION is ignored.
2733
52a244eb 2734See `idlwave-surround'."
f32b3b91 2735 (if idlwave-surround-by-blank
4b1aaa8b 2736 (let
52a244eb 2737 ((non-an-ops "\\(##\\|\\*\\|\\+\\|-\\|/\\|<\\|>\\|\\^\\)\\=")
4b1aaa8b 2738 (an-ops
52a244eb
S
2739 "\\s-\\(AND\\|EQ\\|GE\\|GT\\|LE\\|LT\\|MOD\\|NE\\|OR\\|XOR\\)\\=")
2740 (len 1))
4b1aaa8b
PE
2741
2742 (save-excursion
52a244eb
S
2743 (let ((case-fold-search t))
2744 (backward-char)
4b1aaa8b 2745 (if (or
52a244eb
S
2746 (re-search-backward non-an-ops nil t)
2747 ;; Why doesn't ##? work for both?
4b1aaa8b 2748 (re-search-backward "\\(#\\)\\=" nil t))
52a244eb
S
2749 (setq len (1+ (length (match-string 1))))
2750 (when (re-search-backward an-ops nil t)
3938cb82 2751 ;(setq begin nil) ; won't modify begin
52a244eb 2752 (setq len (1+ (length (match-string 1))))))))
4b1aaa8b
PE
2753
2754 (if (eq t idlwave-pad-keyword)
52a244eb 2755 ;; Everything gets padded equally
f66f03de 2756 (idlwave-surround before after len)
52a244eb
S
2757 ;; Treating keywords/for variables specially...
2758 (let ((st (save-excursion ; To catch "for" variables
2759 (idlwave-start-of-substatement t)
2760 (idlwave-statement-type)))
2761 (what (save-excursion ; To catch keywords
2762 (skip-chars-backward "= \t")
2763 (nth 2 (idlwave-where)))))
2764 (cond ((or (memq what '(function-keyword procedure-keyword))
4b1aaa8b
PE
2765 (memq (caar st) '(for pdef)))
2766 (cond
52a244eb
S
2767 ((null idlwave-pad-keyword)
2768 (idlwave-surround 0 0)
2769 ) ; remove space
2770 (t))) ; leave any spaces alone
f66f03de 2771 (t (idlwave-surround before after len))))))))
4b1aaa8b 2772
f32b3b91 2773
5e72c6b2
S
2774(defun idlwave-indent-and-action (&optional arg)
2775 "Call `idlwave-indent-line' and do expand actions.
2776With prefix ARG non-nil, indent the entire sub-statement."
2777 (interactive "p")
05a1abfc 2778 (save-excursion
4b1aaa8b
PE
2779 (if (and idlwave-expand-generic-end
2780 (re-search-backward "\\<\\(end\\)\\s-*\\="
05a1abfc
CD
2781 (max 0 (- (point) 10)) t)
2782 (looking-at "\\(end\\)\\([ \n\t]\\|\\'\\)"))
2783 (progn (goto-char (match-end 1))
5e72c6b2
S
2784 ;;Expand the END abbreviation, just as RET or Space would have.
2785 (if abbrev-mode (expand-abbrev)
2786 (idlwave-show-begin)))))
52a244eb
S
2787 (when (and (not arg) current-prefix-arg)
2788 (setq arg current-prefix-arg)
2789 (setq current-prefix-arg nil))
4b1aaa8b 2790 (if arg
5e72c6b2
S
2791 (idlwave-indent-statement)
2792 (idlwave-indent-line t)))
f32b3b91
CD
2793
2794(defun idlwave-indent-line (&optional expand)
5a0c3f56 2795 "Indent current IDL line as code or as a comment.
f32b3b91
CD
2796The actions in `idlwave-indent-action-table' are performed.
2797If the optional argument EXPAND is non-nil then the actions in
2798`idlwave-indent-expand-table' are performed."
2799 (interactive)
2800 ;; Move point out of left margin.
2801 (if (save-excursion
2802 (skip-chars-backward " \t")
2803 (bolp))
2804 (skip-chars-forward " \t"))
2805 (let ((mloc (point-marker)))
2806 (save-excursion
2807 (beginning-of-line)
2808 (if (looking-at idlwave-comment-line-start-skip)
2809 ;; Indentation for a line comment
2810 (progn
2811 (skip-chars-forward " \t")
2812 (idlwave-indent-left-margin (idlwave-comment-hook)))
2813 ;;
2814 ;; Code Line
2815 ;;
2816 ;; Before indenting, run action routines.
2817 ;;
2818 (if (and expand idlwave-do-actions)
8ffcfb27 2819 (mapc 'idlwave-do-action idlwave-indent-expand-table))
f32b3b91
CD
2820 ;;
2821 (if idlwave-do-actions
8ffcfb27 2822 (mapc 'idlwave-do-action idlwave-indent-action-table))
f32b3b91
CD
2823 ;;
2824 ;; No longer expand abbrevs on the line. The user can do this
2825 ;; manually using expand-region-abbrevs.
2826 ;;
2827 ;; Indent for code line
2828 ;;
2829 (beginning-of-line)
2830 (if (or
2831 ;; a label line
2832 (looking-at (concat "^" idlwave-label "[ \t]*$"))
2833 ;; a batch command
2834 (looking-at "^[ \t]*@"))
2835 ;; leave flush left
2836 nil
2837 ;; indent the line
2838 (idlwave-indent-left-margin (idlwave-calculate-indent)))
2839 ;; Adjust parallel comment
76959b77
S
2840 (end-of-line)
2841 (if (idlwave-in-comment)
2842 ;; Emacs 21 is too smart with fill-column on comment indent
2843 (let ((fill-column (if (fboundp 'comment-indent-new-line)
2844 (1- (frame-width))
2845 fill-column)))
2846 (indent-for-comment)))))
f32b3b91
CD
2847 (goto-char mloc)
2848 ;; Get rid of marker
76959b77 2849 (set-marker mloc nil)))
f32b3b91
CD
2850
2851(defun idlwave-do-action (action)
5a0c3f56
JB
2852 "Perform an action repeatedly on a line.
2853ACTION is a list (REG . FUNC). REG is a regular expression. FUNC is
2854either a function name to be called with `funcall' or a list to be
2855evaluated with `eval'. The action performed by FUNC should leave
2856point after the match for REG - otherwise an infinite loop may be
2857entered. FUNC is always passed a final argument of 'is-action, so it
2858can discriminate between being run as an action, or a key binding."
f32b3b91
CD
2859 (let ((action-key (car action))
2860 (action-routine (cdr action)))
2861 (beginning-of-line)
2862 (while (idlwave-look-at action-key)
2863 (if (listp action-routine)
f66f03de
S
2864 (eval (append action-routine '('is-action)))
2865 (funcall action-routine 'is-action)))))
f32b3b91
CD
2866
2867(defun idlwave-indent-to (col &optional min)
2868 "Indent from point with spaces until column COL.
2869Inserts space before markers at point."
2870 (if (not min) (setq min 0))
2871 (insert-before-markers
15e42531 2872 (make-string (max min (- col (current-column))) ?\ )))
f32b3b91
CD
2873
2874(defun idlwave-indent-left-margin (col)
2875 "Indent the current line to column COL.
2876Indents such that first non-whitespace character is at column COL
2877Inserts spaces before markers at point."
2878 (save-excursion
2879 (beginning-of-line)
2880 (delete-horizontal-space)
2881 (idlwave-indent-to col)))
2882
2883(defun idlwave-indent-subprogram ()
5a0c3f56 2884 "Indent program unit which contains point."
f32b3b91
CD
2885 (interactive)
2886 (save-excursion
2887 (idlwave-end-of-statement)
2888 (idlwave-beginning-of-subprogram)
2889 (let ((beg (point)))
2890 (idlwave-forward-block)
2891 (message "Indenting subprogram...")
2892 (indent-region beg (point) nil))
2893 (message "Indenting subprogram...done.")))
2894
5e72c6b2
S
2895(defun idlwave-indent-statement ()
2896 "Indent current statement, including all continuation lines."
2897 (interactive)
2898 (save-excursion
2899 (idlwave-beginning-of-statement)
2900 (let ((beg (point)))
2901 (idlwave-end-of-statement)
2902 (indent-region beg (point) nil))))
2903
f32b3b91
CD
2904(defun idlwave-calculate-indent ()
2905 "Return appropriate indentation for current line as IDL code."
2906 (save-excursion
2907 (beginning-of-line)
2908 (cond
2909 ;; Check for beginning of unit - main (beginning of buffer), pro, or
2910 ;; function
2911 ((idlwave-look-at idlwave-begin-unit-reg)
2912 0)
2913 ;; Check for continuation line
2914 ((save-excursion
2915 (and (= (forward-line -1) 0)
2916 (idlwave-is-continuation-line)))
2917 (idlwave-calculate-cont-indent))
2918 ;; calculate indent based on previous and current statements
52a244eb
S
2919 (t (let* (beg-prev-pos
2920 (the-indent
2921 ;; calculate indent based on previous statement
2922 (save-excursion
2923 (cond
2924 ;; Beginning of file
4b1aaa8b 2925 ((prog1
52a244eb
S
2926 (idlwave-previous-statement)
2927 (setq beg-prev-pos (point)))
2928 0)
2929 ;; Main block
2930 ((idlwave-look-at idlwave-begin-unit-reg t)
2931 (+ (idlwave-current-statement-indent)
2932 idlwave-main-block-indent))
2933 ;; Begin block
2934 ((idlwave-look-at idlwave-begin-block-reg t)
4b1aaa8b 2935 (+ (idlwave-min-current-statement-indent)
52a244eb
S
2936 idlwave-block-indent))
2937 ;; End Block
2938 ((idlwave-look-at idlwave-end-block-reg t)
2939 (progn
2940 ;; Match to the *beginning* of the block opener
2941 (goto-char beg-prev-pos)
2942 (idlwave-block-jump-out -1 'nomark) ; go to begin block
2943 (idlwave-min-current-statement-indent)))
2944 ;; idlwave-end-offset
2945 ;; idlwave-block-indent))
4b1aaa8b 2946
52a244eb
S
2947 ;; Default to current indent
2948 ((idlwave-current-statement-indent))))))
f32b3b91
CD
2949 ;; adjust the indentation based on the current statement
2950 (cond
2951 ;; End block
5e72c6b2
S
2952 ((idlwave-look-at idlwave-end-block-reg)
2953 (+ the-indent idlwave-end-offset))
f32b3b91
CD
2954 (the-indent)))))))
2955
2956;;
52a244eb 2957;; Parentheses indent
f32b3b91
CD
2958;;
2959
5e72c6b2
S
2960(defun idlwave-calculate-paren-indent (beg-reg end-reg close-exp)
2961 "Calculate the continuation indent inside a paren group.
4b1aaa8b 2962Returns a cons-cell with (open . indent), where open is the
5a0c3f56 2963location of the open paren."
5e72c6b2
S
2964 (let ((open (nth 1 (parse-partial-sexp beg-reg end-reg))))
2965 ;; Found an innermost open paren.
2966 (when open
2967 (goto-char open)
2968 ;; Line up with next word unless this is a closing paren.
2969 (cons open
2970 (cond
52a244eb
S
2971 ;; Plain Kernighan-style nested indent
2972 (idlwave-indent-parens-nested
2973 (+ idlwave-continuation-indent (idlwave-current-indent)))
2974
5e72c6b2
S
2975 ;; This is a closed paren - line up under open paren.
2976 (close-exp
2977 (current-column))
52a244eb
S
2978
2979 ;; Empty (or just comment) follows -- revert to basic indent
5e72c6b2
S
2980 ((progn
2981 ;; Skip paren
2982 (forward-char 1)
2983 (looking-at "[ \t$]*\\(;.*\\)?$"))
52a244eb
S
2984 nil)
2985
2986 ;; Line up with first word after any blank space
5e72c6b2
S
2987 ((progn
2988 (skip-chars-forward " \t")
2989 (current-column))))))))
2990
f32b3b91 2991(defun idlwave-calculate-cont-indent ()
5a0c3f56
JB
2992 "Calculates the IDL continuation indent column from the previous statement.
2993Note that here previous statement usually means the beginning of the
2994current statement if this statement is a continuation of the previous
2995line. Various special types of continuations, including assignments,
2996routine definitions, and parenthetical groupings, are treated separately."
f32b3b91 2997 (save-excursion
52a244eb 2998 (let* ((case-fold-search t)
f32b3b91 2999 (end-reg (progn (beginning-of-line) (point)))
52a244eb
S
3000 (beg-last-statement (save-excursion (idlwave-previous-statement)
3001 (point)))
4b1aaa8b 3002 (beg-reg (progn (idlwave-start-of-substatement 'pre)
52a244eb
S
3003 (if (eq (line-beginning-position) end-reg)
3004 (goto-char beg-last-statement)
3005 (point))))
3006 (basic-indent (+ (idlwave-min-current-statement-indent end-reg)
3007 idlwave-continuation-indent))
3008 fancy-nonparen-indent fancy-paren-indent)
4b1aaa8b 3009 (cond
52a244eb
S
3010 ;; Align then with its matching if, etc.
3011 ((let ((matchers '(("\\<if\\>" . "[ \t]*then")
3012 ("\\<\\(if\\|end\\(if\\)?\\)\\>" . "[ \t]*else")
3013 ("\\<\\(for\\|while\\)\\>" . "[ \t]*do")
4b1aaa8b 3014 ("\\<\\(repeat\\|end\\(rep\\)?\\)\\>" .
52a244eb
S
3015 "[ \t]*until")
3016 ("\\<case\\>" . "[ \t]*of")))
3017 match cont-re)
3018 (goto-char end-reg)
4b1aaa8b 3019 (and
52a244eb
S
3020 (setq cont-re
3021 (catch 'exit
3022 (while (setq match (car matchers))
3023 (if (looking-at (cdr match))
3024 (throw 'exit (car match)))
3025 (setq matchers (cdr matchers)))))
3026 (idlwave-find-key cont-re -1 'nomark beg-last-statement)))
3027 (if (looking-at "end") ;; that one's special
4b1aaa8b 3028 (- (idlwave-current-indent)
52a244eb
S
3029 (+ idlwave-block-indent idlwave-end-offset))
3030 (idlwave-current-indent)))
3031
3032 ;; Indent in from the previous line for continuing statements
3033 ((let ((matchers '("\\<then\\>"
3034 "\\<do\\>"
3035 "\\<repeat\\>"
3036 "\\<else\\>"))
3037 match)
3038 (catch 'exit
3039 (goto-char end-reg)
3040 (if (/= (forward-line -1) 0)
3041 (throw 'exit nil))
3042 (while (setq match (car matchers))
3043 (if (looking-at (concat ".*" match "[ \t]*\\$[ \t]*"
3044 "\\(;.*\\)?$"))
3045 (throw 'exit t))
3046 (setq matchers (cdr matchers)))))
3047 (+ idlwave-continuation-indent (idlwave-current-indent)))
3048
3049 ;; Parenthetical indent, either traditional or Kernighan style
3050 ((setq fancy-paren-indent
3051 (let* ((end-reg end-reg)
3052 (close-exp (progn
3053 (goto-char end-reg)
4b1aaa8b 3054 (skip-chars-forward " \t")
52a244eb
S
3055 (looking-at "\\s)")))
3056 indent-cons)
3057 (catch 'loop
3058 (while (setq indent-cons (idlwave-calculate-paren-indent
3059 beg-reg end-reg close-exp))
3060 ;; First permitted containing paren
3061 (if (or
3062 idlwave-indent-to-open-paren
3063 idlwave-indent-parens-nested
3064 (null (cdr indent-cons))
3065 (< (- (cdr indent-cons) basic-indent)
3066 idlwave-max-extra-continuation-indent))
3067 (throw 'loop (cdr indent-cons)))
3068 (setq end-reg (car indent-cons))))))
5e72c6b2
S
3069 fancy-paren-indent)
3070
52a244eb
S
3071 ;; A continued assignment, or procedure call/definition
3072 ((and
3073 (> idlwave-max-extra-continuation-indent 0)
3074 (setq fancy-nonparen-indent
3075 (progn
3076 (goto-char beg-reg)
3077 (while (idlwave-look-at "&")) ; skip continued statements
3078 (cond
3079 ;; A continued Procedure call or definition
3080 ((progn
3081 (idlwave-look-at "^[ \t]*\\(pro\\|function\\)") ;skip over
3082 (looking-at "[ \t]*\\([a-zA-Z0-9.$_]+[ \t]*->[ \t]*\\)?[a-zA-Z][:a-zA-Z0-9$_]*[ \t]*\\(,\\)[ \t]*"))
3083 (goto-char (match-end 0))
3084 ;; Comment only, or blank line with "$"? Basic indent.
3085 (if (save-match-data (looking-at "[ \t$]*\\(;.*\\)?$"))
3086 nil
3087 (current-column)))
4b1aaa8b 3088
52a244eb
S
3089 ;; Continued assignment (with =):
3090 ((catch 'assign ;
3091 (while (looking-at "[^=\n\r]*\\(=\\)[ \t]*")
3092 (goto-char (match-end 0))
4b1aaa8b 3093 (if (null (idlwave-what-function beg-reg))
52a244eb
S
3094 (throw 'assign t))))
3095 (unless (or
3096 (idlwave-in-quote)
3097 (looking-at "[ \t$]*\\(;.*\\)?$") ; use basic
3098 (save-excursion
3099 (goto-char beg-last-statement)
3100 (eq (caar (idlwave-statement-type)) 'for)))
3101 (current-column))))))
3102 (< (- fancy-nonparen-indent basic-indent)
3103 idlwave-max-extra-continuation-indent))
3104 (if fancy-paren-indent ;calculated but disallowed paren indent
3105 (+ fancy-nonparen-indent idlwave-continuation-indent)
3106 fancy-nonparen-indent))
3107
3108 ;; Basic indent, by default
3109 (t basic-indent)))))
3110
3111
f32b3b91 3112
15e42531
CD
3113(defun idlwave-find-key (key-re &optional dir nomark limit)
3114 "Move to next match of the regular expression KEY-RE.
3115Matches inside comments or string constants will be ignored.
3116If DIR is negative, the search will be backwards.
3117At a successful match, the mark is pushed unless NOMARK is non-nil.
3118Searches are limited to LIMIT.
3119Searches are case-insensitive and use a special syntax table which
3120treats `$' and `_' as word characters.
3121Return value is the beginning of the match or (in case of failure) nil."
3122 (setq dir (or dir 0))
3123 (let ((case-fold-search t)
3124 (search-func (if (> dir 0) 're-search-forward 're-search-backward))
3125 found)
3126 (idlwave-with-special-syntax
3127 (save-excursion
3128 (catch 'exit
3129 (while (funcall search-func key-re limit t)
3130 (if (not (idlwave-quoted))
52a244eb
S
3131 (throw 'exit (setq found (match-beginning 0)))
3132 (if (or (and (> dir 0) (eobp))
3133 (and (< dir 0) (bobp)))
3134 (throw 'exit nil)))))))
15e42531
CD
3135 (if found
3136 (progn
3137 (if (not nomark) (push-mark))
3138 (goto-char found)
3139 found)
3140 nil)))
3141
f32b3b91
CD
3142(defun idlwave-block-jump-out (&optional dir nomark)
3143 "When optional argument DIR is non-negative, move forward to end of
3144current block using the `idlwave-begin-block-reg' and `idlwave-end-block-reg'
5a0c3f56
JB
3145regular expressions. When DIR is negative, move backwards to block beginning.
3146Recursively calls itself to skip over nested blocks. DIR defaults to
3147forward. Calls `push-mark' unless the optional argument NOMARK is
3148non-nil. Movement is limited by the start of program units because of
f32b3b91
CD
3149possibility of unbalanced blocks."
3150 (interactive "P")
3151 (or dir (setq dir 0))
3152 (let* ((here (point))
3153 (case-fold-search t)
3154 (limit (if (>= dir 0) (point-max) (point-min)))
4b1aaa8b 3155 (block-limit (if (>= dir 0)
f32b3b91
CD
3156 idlwave-begin-block-reg
3157 idlwave-end-block-reg))
3158 found
3159 (block-reg (concat idlwave-begin-block-reg "\\|"
3160 idlwave-end-block-reg))
3161 (unit-limit (or (save-excursion
3162 (if (< dir 0)
3163 (idlwave-find-key
3164 idlwave-begin-unit-reg dir t limit)
3165 (end-of-line)
4b1aaa8b 3166 (idlwave-find-key
f32b3b91
CD
3167 idlwave-end-unit-reg dir t limit)))
3168 limit)))
3169 (if (>= dir 0) (end-of-line)) ;Make sure we are in current block
3170 (if (setq found (idlwave-find-key block-reg dir t unit-limit))
3171 (while (and found (looking-at block-limit))
3172 (if (>= dir 0) (forward-word 1))
3173 (idlwave-block-jump-out dir t)
3174 (setq found (idlwave-find-key block-reg dir t unit-limit))))
3175 (if (not nomark) (push-mark here))
3176 (if (not found) (goto-char unit-limit)
3177 (if (>= dir 0) (forward-word 1)))))
3178
52a244eb
S
3179(defun idlwave-min-current-statement-indent (&optional end-reg)
3180 "The minimum indent in the current statement."
3181 (idlwave-beginning-of-statement)
3182 (if (not (idlwave-is-continuation-line))
3183 (idlwave-current-indent)
3184 (let ((min (idlwave-current-indent)) comm-or-empty)
3185 (while (and (= (forward-line 1) 0)
3186 (or (setq comm-or-empty (idlwave-is-comment-or-empty-line))
3187 (idlwave-is-continuation-line))
3188 (or (null end-reg) (< (point) end-reg)))
3189 (unless comm-or-empty (setq min (min min (idlwave-current-indent)))))
3190 (if (or comm-or-empty (and end-reg (>= (point) end-reg)))
4b1aaa8b 3191 min
52a244eb
S
3192 (min min (idlwave-current-indent))))))
3193
3194(defun idlwave-current-statement-indent (&optional last-line)
f32b3b91
CD
3195 "Return indentation of the current statement.
3196If in a statement, moves to beginning of statement before finding indent."
52a244eb
S
3197 (if last-line
3198 (idlwave-end-of-statement)
3199 (idlwave-beginning-of-statement))
f32b3b91
CD
3200 (idlwave-current-indent))
3201
3202(defun idlwave-current-indent ()
3203 "Return the column of the indentation of the current line.
5a0c3f56 3204Skips any whitespace. Returns 0 if the end-of-line follows the whitespace."
f32b3b91
CD
3205 (save-excursion
3206 (beginning-of-line)
3207 (skip-chars-forward " \t")
3208 ;; if we are at the end of blank line return 0
3209 (cond ((eolp) 0)
3210 ((current-column)))))
3211
3212(defun idlwave-is-continuation-line ()
5a0c3f56 3213 "Test if current line is continuation line.
5e72c6b2
S
3214Blank or comment-only lines following regular continuation lines (with
3215`$') count as continuations too."
0dc2be2f
S
3216 (let (p)
3217 (save-excursion
4b1aaa8b 3218 (or
0dc2be2f
S
3219 (idlwave-look-at "\\<\\$")
3220 (catch 'loop
4b1aaa8b 3221 (while (and (looking-at "^[ \t]*\\(;.*\\)?$")
0dc2be2f
S
3222 (eq (forward-line -1) 0))
3223 (if (setq p (idlwave-look-at "\\<\\$")) (throw 'loop p))))))))
f32b3b91
CD
3224
3225(defun idlwave-is-comment-line ()
5a0c3f56 3226 "Test if the current line is a comment line."
f32b3b91
CD
3227 (save-excursion
3228 (beginning-of-line 1)
3229 (looking-at "[ \t]*;")))
3230
05a1abfc 3231(defun idlwave-is-comment-or-empty-line ()
5a0c3f56 3232 "Test if the current line is a comment line."
05a1abfc
CD
3233 (save-excursion
3234 (beginning-of-line 1)
3235 (looking-at "[ \t]*[;\n]")))
3236
f32b3b91 3237(defun idlwave-look-at (regexp &optional cont beg)
5a0c3f56 3238 "Search current line from current point for REGEXP.
15e42531
CD
3239If optional argument CONT is non-nil, searches to the end of
3240the current statement.
3241If optional arg BEG is non-nil, search starts from the beginning of the
3242current statement.
3243Ignores matches that end in a comment or inside a string expression.
3244Returns point if successful, nil otherwise.
3245This function produces unexpected results if REGEXP contains quotes or
5a0c3f56 3246a comment delimiter. The search is case insensitive.
15e42531 3247If successful leaves point after the match, otherwise, does not move point."
f32b3b91 3248 (let ((here (point))
f32b3b91 3249 (case-fold-search t)
15e42531
CD
3250 (eos (save-excursion
3251 (if cont (idlwave-end-of-statement) (end-of-line))
3252 (point)))
f32b3b91 3253 found)
15e42531
CD
3254 (idlwave-with-special-syntax
3255 (if beg (idlwave-beginning-of-statement))
3256 (while (and (setq found (re-search-forward regexp eos t))
3257 (idlwave-quoted))))
f32b3b91
CD
3258 (if (not found) (goto-char here))
3259 found))
3260
3261(defun idlwave-fill-paragraph (&optional nohang)
5a0c3f56 3262 "Fill paragraphs in comments.
f32b3b91
CD
3263A paragraph is made up of all contiguous lines having the same comment
3264leader (the leading whitespace before the comment delimiter and the
3265comment delimiter). In addition, paragraphs are separated by blank
5a0c3f56 3266line comments. The indentation is given by the hanging indent of the
f32b3b91 3267first line, otherwise by the minimum indentation of the lines after
5a0c3f56
JB
3268the first line. The indentation of the first line does not change.
3269Does not effect code lines. Does not fill comments on the same line
f32b3b91 3270with code. The hanging indent is given by the end of the first match
5a0c3f56
JB
3271matching `idlwave-hang-indent-regexp' on the paragraph's first line.
3272If the optional argument NOHANG is non-nil then the hanging indent is
f32b3b91
CD
3273ignored."
3274 (interactive "P")
3275 ;; check if this is a line comment
3276 (if (save-excursion
3277 (beginning-of-line)
3278 (skip-chars-forward " \t")
3279 (looking-at comment-start))
3280 (let
3281 ((indent 999)
3282 pre here diff fill-prefix-reg bcl first-indent
3283 hang start end)
3284 ;; Change tabs to spaces in the surrounding paragraph.
3285 ;; The surrounding paragraph will be the largest containing block of
3286 ;; contiguous line comments. Thus, we may be changing tabs in
3287 ;; a much larger area than is needed, but this is the easiest
3288 ;; brute force way to do it.
3289 ;;
3290 ;; This has the undesirable side effect of replacing the tabs
3291 ;; permanently without the user's request or knowledge.
3292 (save-excursion
3293 (backward-paragraph)
3294 (setq start (point)))
3295 (save-excursion
3296 (forward-paragraph)
3297 (setq end (point)))
3298 (untabify start end)
3299 ;;
3300 (setq here (point))
3301 (beginning-of-line)
3302 (setq bcl (point))
3303 (re-search-forward
3304 (concat "^[ \t]*" comment-start "+")
3305 (save-excursion (end-of-line) (point))
3306 t)
3307 ;; Get the comment leader on the line and its length
3308 (setq pre (current-column))
3309 ;; the comment leader is the indentation plus exactly the
3310 ;; number of consecutive ";".
3311 (setq fill-prefix-reg
3312 (concat
3313 (setq fill-prefix
3314 (regexp-quote
3315 (buffer-substring (save-excursion
3316 (beginning-of-line) (point))
3317 (point))))
3318 "[^;]"))
4b1aaa8b 3319
f32b3b91
CD
3320 ;; Mark the beginning and end of the paragraph
3321 (goto-char bcl)
3322 (while (and (looking-at fill-prefix-reg)
3323 (not (looking-at paragraph-separate))
3324 (not (bobp)))
3325 (forward-line -1))
3326 ;; Move to first line of paragraph
3327 (if (/= (point) bcl)
3328 (forward-line 1))
3329 (setq start (point))
3330 (goto-char bcl)
3331 (while (and (looking-at fill-prefix-reg)
3332 (not (looking-at paragraph-separate))
3333 (not (eobp)))
3334 (forward-line 1))
3335 (beginning-of-line)
3336 (if (or (not (looking-at fill-prefix-reg))
3337 (looking-at paragraph-separate))
3338 (forward-line -1))
3339 (end-of-line)
3340 ;; if at end of buffer add a newline (need this because
3341 ;; fill-region needs END to be at the beginning of line after
3342 ;; the paragraph or it will add a line).
3343 (if (eobp)
3344 (progn (insert ?\n) (backward-char 1)))
3345 ;; Set END to the beginning of line after the paragraph
3346 ;; END is calculated as distance from end of buffer
3347 (setq end (- (point-max) (point) 1))
3348 ;;
3349 ;; Calculate the indentation for the paragraph.
3350 ;;
3351 ;; In the following while statements, after one iteration
3352 ;; point will be at the beginning of a line in which case
3353 ;; the while will not be executed for the
3354 ;; the first paragraph line and thus will not affect the
3355 ;; indentation.
3356 ;;
3357 ;; First check to see if indentation is based on hanging indent.
3358 (if (and (not nohang) idlwave-hanging-indent
3359 (setq hang
3360 (save-excursion
3361 (goto-char start)
3362 (idlwave-calc-hanging-indent))))
3363 ;; Adjust lines of paragraph by inserting spaces so that
3364 ;; each line's indent is at least as great as the hanging
3365 ;; indent. This is needed for fill-paragraph to work with
3366 ;; a fill-prefix.
3367 (progn
3368 (setq indent hang)
3369 (beginning-of-line)
3370 (while (> (point) start)
3371 (re-search-forward comment-start-skip
3372 (save-excursion (end-of-line) (point))
3373 t)
3374 (if (> (setq diff (- indent (current-column))) 0)
3375 (progn
3376 (if (>= here (point))
3377 ;; adjust the original location for the
3378 ;; inserted text.
3379 (setq here (+ here diff)))
15e42531 3380 (insert (make-string diff ?\ ))))
f32b3b91
CD
3381 (forward-line -1))
3382 )
4b1aaa8b 3383
f32b3b91
CD
3384 ;; No hang. Instead find minimum indentation of paragraph
3385 ;; after first line.
3386 ;; For the following while statement, since START is at the
aa87aafc 3387 ;; beginning of line and END is at the end of line
f32b3b91
CD
3388 ;; point is greater than START at least once (which would
3389 ;; be the case for a single line paragraph).
3390 (while (> (point) start)
3391 (beginning-of-line)
3392 (setq indent
3393 (min indent
3394 (progn
3395 (re-search-forward
3396 comment-start-skip
3397 (save-excursion (end-of-line) (point))
3398 t)
3399 (current-column))))
3400 (forward-line -1))
3401 )
3402 (setq fill-prefix (concat fill-prefix
3403 (make-string (- indent pre)
15e42531 3404 ?\ )))
f32b3b91
CD
3405 ;; first-line indent
3406 (setq first-indent
3407 (max
3408 (progn
3409 (re-search-forward
3410 comment-start-skip
3411 (save-excursion (end-of-line) (point))
3412 t)
3413 (current-column))
3414 indent))
4b1aaa8b 3415
f32b3b91
CD
3416 ;; try to keep point at its original place
3417 (goto-char here)
3418
3419 ;; In place of the more modern fill-region-as-paragraph, a hack
3420 ;; to keep whitespace untouched on the first line within the
3421 ;; indent length and to preserve any indent on the first line
3422 ;; (first indent).
3423 (save-excursion
3424 (setq diff
3425 (buffer-substring start (+ start first-indent -1)))
15e42531 3426 (subst-char-in-region start (+ start first-indent -1) ?\ ?~ nil)
f32b3b91
CD
3427 (fill-region-as-paragraph
3428 start
3429 (- (point-max) end)
3430 (current-justification)
3431 nil)
3432 (delete-region start (+ start first-indent -1))
3433 (goto-char start)
3434 (insert diff))
3435 ;; When we want the point at the beginning of the comment
3436 ;; body fill-region will put it at the beginning of the line.
3437 (if (bolp) (skip-chars-forward (concat " \t" comment-start)))
3438 (setq fill-prefix nil))))
3439
3440(defun idlwave-calc-hanging-indent ()
5a0c3f56
JB
3441 "Calculate the position of the hanging indent for the comment paragraph.
3442The hanging indent position is given by the first match with the
3443`idlwave-hang-indent-regexp'. If `idlwave-use-last-hang-indent' is
3444non-nil then use last occurrence matching `idlwave-hang-indent-regexp'
3445on the line.
f32b3b91
CD
3446If not found returns nil."
3447 (if idlwave-use-last-hang-indent
3448 (save-excursion
3449 (end-of-line)
3450 (if (re-search-backward
3451 idlwave-hang-indent-regexp
3452 (save-excursion (beginning-of-line) (point))
3453 t)
3454 (+ (current-column) (length idlwave-hang-indent-regexp))))
3455 (save-excursion
3456 (beginning-of-line)
3457 (if (re-search-forward
3458 idlwave-hang-indent-regexp
3459 (save-excursion (end-of-line) (point))
3460 t)
3461 (current-column)))))
3462
3463(defun idlwave-auto-fill ()
4b1aaa8b 3464 "Called to break lines in auto fill mode.
52a244eb
S
3465Only fills non-comment lines if `idlwave-fill-comment-line-only' is
3466non-nil. Places a continuation character at the end of the line if
3467not in a comment. Splits strings with IDL concatenation operator `+'
3468if `idlwave-auto-fill-split-string' is non-nil."
f32b3b91
CD
3469 (if (<= (current-column) fill-column)
3470 nil ; do not to fill
3471 (if (or (not idlwave-fill-comment-line-only)
3472 (save-excursion
3473 ;; Check for comment line
3474 (beginning-of-line)
3475 (looking-at idlwave-comment-line-start-skip)))
3476 (let (beg)
3477 (idlwave-indent-line)
3478 ;; Prevent actions do-auto-fill which calls indent-line-function.
3479 (let (idlwave-do-actions
d6aac72d 3480 (paragraph-separate ".")
52a244eb
S
3481 (fill-nobreak-predicate
3482 (if (and (idlwave-in-quote)
3483 idlwave-auto-fill-split-string)
3484 (lambda () ;; We'll need 5 spaces for " ' + $"
3485 (<= (- fill-column (current-column)) 5)
3486 ))))
f32b3b91
CD
3487 (do-auto-fill))
3488 (save-excursion
3489 (end-of-line 0)
3490 ;; Indent the split line
a86bd650 3491 (idlwave-indent-line))
f32b3b91
CD
3492 (if (save-excursion
3493 (beginning-of-line)
3494 (looking-at idlwave-comment-line-start-skip))
3495 ;; A continued line comment
3496 ;; We treat continued line comments as part of a comment
3497 ;; paragraph. So we check for a hanging indent.
3498 (if idlwave-hanging-indent
3499 (let ((here (- (point-max) (point)))
3500 (indent
3501 (save-excursion
3502 (forward-line -1)
3503 (idlwave-calc-hanging-indent))))
3504 (if indent
3505 (progn
3506 ;; Remove whitespace between comment delimiter and
3507 ;; text, insert spaces for appropriate indentation.
3508 (beginning-of-line)
3509 (re-search-forward
3510 comment-start-skip
3511 (save-excursion (end-of-line) (point)) t)
3512 (delete-horizontal-space)
3513 (idlwave-indent-to indent)
3514 (goto-char (- (point-max) here)))
3515 )))
3516 ;; Split code or comment?
3517 (if (save-excursion
3518 (end-of-line 0)
3519 (idlwave-in-comment))
52a244eb 3520 ;; Splitting a non-full-line comment.
f32b3b91
CD
3521 ;; Insert the comment delimiter from split line
3522 (progn
3523 (save-excursion
3524 (beginning-of-line)
3525 (skip-chars-forward " \t")
3526 ;; Insert blank to keep off beginning of line
3527 (insert " "
3528 (save-excursion
3529 (forward-line -1)
3530 (buffer-substring (idlwave-goto-comment)
3531 (progn
3532 (skip-chars-forward "; ")
3533 (point))))))
3534 (idlwave-indent-line))
3535 ;; Split code line - add continuation character
3536 (save-excursion
3537 (end-of-line 0)
3538 ;; Check to see if we split a string
3539 (if (and (setq beg (idlwave-in-quote))
3540 idlwave-auto-fill-split-string)
3541 ;; Split the string and concatenate.
3542 ;; The first extra space is for the space
3543 ;; the line was split. That space was removed.
3544 (insert " " (char-after beg) " +"))
3545 (insert " $"))
3546 (if beg
3547 (if idlwave-auto-fill-split-string
3548 ;; Make the second part of continued string
3549 (save-excursion
3550 (beginning-of-line)
3551 (skip-chars-forward " \t")
3552 (insert (char-after beg)))
3553 ;; Warning
3554 (beep)
3555 (message "Warning: continuation inside a string.")))
3556 ;; Although do-auto-fill (via indent-new-comment-line) calls
3557 ;; idlwave-indent-line for the new line, re-indent again
3558 ;; because of the addition of the continuation character.
3559 (idlwave-indent-line))
3560 )))))
3561
3562(defun idlwave-auto-fill-mode (arg)
3563 "Toggle auto-fill mode for IDL mode.
3564With arg, turn auto-fill mode on if arg is positive.
3565In auto-fill mode, inserting a space at a column beyond `fill-column'
3566automatically breaks the line at a previous space."
3567 (interactive "P")
3568 (prog1 (set idlwave-fill-function
3569 (if (if (null arg)
3570 (not (symbol-value idlwave-fill-function))
3571 (> (prefix-numeric-value arg) 0))
3572 'idlwave-auto-fill
3573 nil))
3574 ;; update mode-line
3575 (set-buffer-modified-p (buffer-modified-p))))
3576
52a244eb
S
3577;(defun idlwave-fill-routine-call ()
3578; "Fill a routine definition or statement, indenting appropriately."
3579; (let ((where (idlwave-where)))))
3580
3581
5a0c3f56 3582(defun idlwave-doc-header (&optional nomark)
f32b3b91 3583 "Insert a documentation header at the beginning of the unit.
5a0c3f56
JB
3584Inserts the value of the variable `idlwave-file-header'. Sets mark
3585before moving to do insertion unless the optional prefix argument
3586NOMARK is non-nil."
f32b3b91
CD
3587 (interactive "P")
3588 (or nomark (push-mark))
3589 ;; make sure we catch the current line if it begins the unit
5e72c6b2
S
3590 (if idlwave-header-to-beginning-of-file
3591 (goto-char (point-min))
3592 (end-of-line)
3593 (idlwave-beginning-of-subprogram)
3594 (beginning-of-line)
3595 ;; skip function or procedure line
3596 (if (idlwave-look-at "\\<\\(pro\\|function\\)\\>")
3597 (progn
3598 (idlwave-end-of-statement)
3599 (if (> (forward-line 1) 0) (insert "\n")))))
3600 (let ((pos (point)))
3601 (if idlwave-file-header
3602 (cond ((car idlwave-file-header)
a527b753 3603 (insert-file-contents (car idlwave-file-header)))
5e72c6b2
S
3604 ((stringp (car (cdr idlwave-file-header)))
3605 (insert (car (cdr idlwave-file-header))))))
3606 (goto-char pos)))
f32b3b91
CD
3607
3608(defun idlwave-default-insert-timestamp ()
5a0c3f56 3609 "Default timestamp insertion function."
f32b3b91
CD
3610 (insert (current-time-string))
3611 (insert ", " (user-full-name))
5e72c6b2 3612 (if (boundp 'user-mail-address)
4b1aaa8b 3613 (insert " <" user-mail-address ">")
5e72c6b2 3614 (insert " <" (user-login-name) "@" (system-name) ">"))
f32b3b91
CD
3615 ;; Remove extra spaces from line
3616 (idlwave-fill-paragraph)
3617 ;; Insert a blank line comment to separate from the date entry -
3618 ;; will keep the entry from flowing onto date line if re-filled.
5e72c6b2 3619 (insert "\n;\n;\t\t"))
f32b3b91
CD
3620
3621(defun idlwave-doc-modification ()
3622 "Insert a brief modification log at the beginning of the current program.
3623Looks for an occurrence of the value of user variable
5a0c3f56
JB
3624`idlwave-doc-modifications-keyword' if non-nil. Inserts time and user
3625name and places the point for the user to add a log. Before moving, saves
f32b3b91
CD
3626location on mark ring so that the user can return to previous point."
3627 (interactive)
3628 (push-mark)
05a1abfc
CD
3629 (let* (beg end)
3630 (if (and (or (re-search-backward idlwave-doclib-start nil t)
3631 (progn
3632 (goto-char (point-min))
3633 (re-search-forward idlwave-doclib-start nil t)))
3634 (setq beg (match-beginning 0))
3635 (re-search-forward idlwave-doclib-end nil t)
3636 (setq end (match-end 0)))
3637 (progn
3638 (goto-char beg)
4b1aaa8b 3639 (if (re-search-forward
05a1abfc
CD
3640 (concat idlwave-doc-modifications-keyword ":")
3641 end t)
3642 (end-of-line)
3643 (goto-char end)
3644 (end-of-line -1)
3645 (insert "\n" comment-start "\n")
3646 (insert comment-start " " idlwave-doc-modifications-keyword ":"))
3647 (insert "\n;\n;\t")
3648 (run-hooks 'idlwave-timestamp-hook))
3649 (error "No valid DOCLIB header"))))
f32b3b91 3650
e08734e2 3651
8d222148
SM
3652;; CJC 3/16/93
3653;; Interface to expand-region-abbrevs which did not work when the
3654;; abbrev hook associated with an abbrev moves point backwards
3655;; after abbrev expansion, e.g., as with the abbrev '.n'.
3656;; The original would enter an infinite loop in attempting to expand
3657;; .n (it would continually expand and unexpand the abbrev without expanding
3658;; because the point would keep going back to the beginning of the
3659;; abbrev instead of to the end of the abbrev). We now keep the
3660;; abbrev hook from moving backwards.
f32b3b91
CD
3661;;;
3662(defun idlwave-expand-region-abbrevs (start end)
3663 "Expand each abbrev occurrence in the region.
3664Calling from a program, arguments are START END."
3665 (interactive "r")
3666 (save-excursion
3667 (goto-char (min start end))
3668 (let ((idlwave-show-block nil) ;Do not blink
3669 (idlwave-abbrev-move nil)) ;Do not move
3670 (expand-region-abbrevs start end 'noquery))))
3671
3672(defun idlwave-quoted ()
5a0c3f56
JB
3673 "Return t if point is in a comment or quoted string.
3674Returns nil otherwise."
f32b3b91
CD
3675 (or (idlwave-in-comment) (idlwave-in-quote)))
3676
3677(defun idlwave-in-quote ()
5a0c3f56 3678 "Return location of the opening quote
f32b3b91
CD
3679if point is in a IDL string constant, nil otherwise.
3680Ignores comment delimiters on the current line.
3681Properly handles nested quotation marks and octal
3682constants - a double quote followed by an octal digit."
8d222148
SM
3683;; Treat an octal inside an apostrophe to be a normal string. Treat a
3684;; double quote followed by an octal digit to be an octal constant
3685;; rather than a string. Therefore, there is no terminating double
3686;; quote.
f32b3b91
CD
3687 (save-excursion
3688 ;; Because single and double quotes can quote each other we must
3689 ;; search for the string start from the beginning of line.
3690 (let* ((start (point))
3691 (eol (progn (end-of-line) (point)))
3692 (bq (progn (beginning-of-line) (point)))
3693 (endq (point))
3694 (data (match-data))
3695 delim
3696 found)
3697 (while (< endq start)
3698 ;; Find string start
3699 ;; Don't find an octal constant beginning with a double quote
52a244eb 3700 (if (re-search-forward "[\"']" eol 'lim)
f32b3b91
CD
3701 ;; Find the string end.
3702 ;; In IDL, two consecutive delimiters after the start of a
3703 ;; string act as an
3704 ;; escape for the delimiter in the string.
3705 ;; Two consecutive delimiters alone (i.e., not after the
aa87aafc 3706 ;; start of a string) is the null string.
f32b3b91
CD
3707 (progn
3708 ;; Move to position after quote
3709 (goto-char (1+ (match-beginning 0)))
3710 (setq bq (1- (point)))
3711 ;; Get the string delimiter
3712 (setq delim (char-to-string (preceding-char)))
3713 ;; Check for null string
3714 (if (looking-at delim)
3715 (progn (setq endq (point)) (forward-char 1))
3716 ;; Look for next unpaired delimiter
3717 (setq found (search-forward delim eol 'lim))
3718 (while (looking-at delim)
3719 (forward-char 1)
3720 (setq found (search-forward delim eol 'lim)))
8d222148 3721 (setq endq (if found (1- (point)) (point)))
f32b3b91
CD
3722 ))
3723 (progn (setq bq (point)) (setq endq (point)))))
3724 (store-match-data data)
3725 ;; return string beginning position or nil
3726 (if (> start bq) bq))))
3727
76959b77 3728(defun idlwave-is-pointer-dereference (&optional limit)
5a0c3f56 3729 "Determine if the character after point is a pointer dereference *."
8d222148
SM
3730 (and
3731 (eq (char-after) ?\*)
3732 (not (idlwave-in-quote))
3733 (save-excursion
3734 (forward-char)
3735 (re-search-backward (concat "\\(" idlwave-idl-keywords
3736 "\\|[[(*+-/=,^><]\\)\\s-*\\*") limit t))))
76959b77
S
3737
3738
f32b3b91
CD
3739;; Statement templates
3740
3741;; Replace these with a general template function, something like
3742;; expand.el (I think there was also something with a name similar to
3743;; dmacro.el)
3744
3745(defun idlwave-template (s1 s2 &optional prompt noindent)
3746 "Build a template with optional prompt expression.
3747
3748Opens a line if point is not followed by a newline modulo intervening
3749whitespace. S1 and S2 are strings. S1 is inserted at point followed
595ab50b 3750by S2. Point is inserted between S1 and S2. The case of S1 and S2 is
5a0c3f56
JB
3751adjusted according to `idlwave-abbrev-change-case'. If optional
3752argument PROMPT is a string then it is displayed as a message in the
f32b3b91
CD
3753minibuffer. The PROMPT serves as a reminder to the user of an
3754expression to enter.
3755
3756The lines containing S1 and S2 are reindented using `indent-region'
3757unless the optional second argument NOINDENT is non-nil."
15e42531 3758 (if (eq major-mode 'idlwave-shell-mode)
05a1abfc 3759 ;; This is a gross hack to avoit template abbrev expansion
15e42531
CD
3760 ;; in the shell. FIXME: This is a dirty hack.
3761 (if (and (eq this-command 'self-insert-command)
3762 (equal last-abbrev-location (point)))
3763 (insert last-abbrev-text)
3764 (error "No templates in idlwave-shell"))
3765 (cond ((eq idlwave-abbrev-change-case 'down)
3766 (setq s1 (downcase s1) s2 (downcase s2)))
3767 (idlwave-abbrev-change-case
3768 (setq s1 (upcase s1) s2 (upcase s2))))
3769 (let ((beg (save-excursion (beginning-of-line) (point)))
3770 end)
3771 (if (not (looking-at "\\s-*\n"))
3772 (open-line 1))
3773 (insert s1)
3774 (save-excursion
3775 (insert s2)
3776 (setq end (point)))
3777 (if (not noindent)
3778 (indent-region beg end nil))
3779 (if (stringp prompt)
274f1353 3780 (message "%s" prompt)))))
4b1aaa8b 3781
595ab50b
CD
3782(defun idlwave-rw-case (string)
3783 "Make STRING have the case required by `idlwave-reserved-word-upcase'."
3784 (if idlwave-reserved-word-upcase
3785 (upcase string)
3786 string))
3787
f32b3b91
CD
3788(defun idlwave-elif ()
3789 "Build skeleton IDL if-else block."
3790 (interactive)
595ab50b
CD
3791 (idlwave-template
3792 (idlwave-rw-case "if")
3793 (idlwave-rw-case " then begin\n\nendif else begin\n\nendelse")
3794 "Condition expression"))
f32b3b91
CD
3795
3796(defun idlwave-case ()
3797 "Build skeleton IDL case statement."
3798 (interactive)
4b1aaa8b 3799 (idlwave-template
595ab50b
CD
3800 (idlwave-rw-case "case")
3801 (idlwave-rw-case " of\n\nendcase")
3802 "Selector expression"))
f32b3b91 3803
05a1abfc
CD
3804(defun idlwave-switch ()
3805 "Build skeleton IDL switch statement."
3806 (interactive)
4b1aaa8b 3807 (idlwave-template
05a1abfc
CD
3808 (idlwave-rw-case "switch")
3809 (idlwave-rw-case " of\n\nendswitch")
3810 "Selector expression"))
3811
f32b3b91 3812(defun idlwave-for ()
5a0c3f56 3813 "Build skeleton IDL loop statement."
f32b3b91 3814 (interactive)
4b1aaa8b 3815 (idlwave-template
595ab50b
CD
3816 (idlwave-rw-case "for")
3817 (idlwave-rw-case " do begin\n\nendfor")
3818 "Loop expression"))
f32b3b91
CD
3819
3820(defun idlwave-if ()
5a0c3f56 3821 "Build skeleton IDL if statement."
f32b3b91 3822 (interactive)
595ab50b
CD
3823 (idlwave-template
3824 (idlwave-rw-case "if")
3825 (idlwave-rw-case " then begin\n\nendif")
3826 "Scalar logical expression"))
f32b3b91
CD
3827
3828(defun idlwave-procedure ()
3829 (interactive)
4b1aaa8b 3830 (idlwave-template
595ab50b
CD
3831 (idlwave-rw-case "pro")
3832 (idlwave-rw-case "\n\nreturn\nend")
3833 "Procedure name"))
f32b3b91
CD
3834
3835(defun idlwave-function ()
3836 (interactive)
4b1aaa8b 3837 (idlwave-template
595ab50b
CD
3838 (idlwave-rw-case "function")
3839 (idlwave-rw-case "\n\nreturn\nend")
3840 "Function name"))
f32b3b91
CD
3841
3842(defun idlwave-repeat ()
3843 (interactive)
595ab50b
CD
3844 (idlwave-template
3845 (idlwave-rw-case "repeat begin\n\nendrep until")
3846 (idlwave-rw-case "")
3847 "Exit condition"))
f32b3b91
CD
3848
3849(defun idlwave-while ()
3850 (interactive)
4b1aaa8b 3851 (idlwave-template
595ab50b
CD
3852 (idlwave-rw-case "while")
3853 (idlwave-rw-case " do begin\n\nendwhile")
3854 "Entry condition"))
f32b3b91
CD
3855
3856(defun idlwave-split-string (string &optional pattern)
3857 "Return a list of substrings of STRING which are separated by PATTERN.
3858If PATTERN is omitted, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
3859 (or pattern
3860 (setq pattern "[ \f\t\n\r\v]+"))
3861 (let (parts (start 0))
3862 (while (string-match pattern string start)
3863 (setq parts (cons (substring string start (match-beginning 0)) parts)
3864 start (match-end 0)))
3865 (nreverse (cons (substring string start) parts))))
3866
3867(defun idlwave-replace-string (string replace_string replace_with)
3868 (let* ((start 0)
3869 (last (length string))
3870 (ret_string "")
3871 end)
3872 (while (setq end (string-match replace_string string start))
3873 (setq ret_string
3874 (concat ret_string (substring string start end) replace_with))
3875 (setq start (match-end 0)))
3876 (setq ret_string (concat ret_string (substring string start last)))))
3877
3878(defun idlwave-get-buffer-visiting (file)
3879 ;; Return the buffer currently visiting FILE
3880 (cond
3881 ((boundp 'find-file-compare-truenames) ; XEmacs
3882 (let ((find-file-compare-truenames t))
3883 (get-file-buffer file)))
3884 ((fboundp 'find-buffer-visiting) ; Emacs
3885 (find-buffer-visiting file))
3886 (t (error "This should not happen (idlwave-get-buffer-visiting)"))))
3887
15e42531 3888(defvar idlwave-outlawed-buffers nil
5a0c3f56 3889 "List of buffers pulled up by IDLWAVE for special reasons.
15e42531
CD
3890Buffers in this list may be killed by `idlwave-kill-autoloaded-buffers'.")
3891
3892(defun idlwave-find-file-noselect (file &optional why)
f32b3b91
CD
3893 ;; Return a buffer visiting file.
3894 (or (idlwave-get-buffer-visiting file)
15e42531
CD
3895 (let ((buf (find-file-noselect file)))
3896 (if why (add-to-list 'idlwave-outlawed-buffers (cons buf why)))
3897 buf)))
3898
3899(defun idlwave-kill-autoloaded-buffers ()
52a244eb 3900 "Kill buffers created automatically by IDLWAVE.
15e42531
CD
3901Function prompts for a letter to identify the buffers to kill.
3902Possible letters are:
3903
3904f Buffers created by the command \\[idlwave-find-module] or mouse
3905 clicks in the routine info window.
3906s Buffers created by the IDLWAVE Shell to display where execution
3907 stopped or an error was found.
3908a Both of the above.
3909
5a0c3f56 3910Buffers containing unsaved changes require confirmation before they are killed."
15e42531
CD
3911 (interactive)
3912 (if (null idlwave-outlawed-buffers)
3913 (error "No IDLWAVE-created buffers available")
3914 (princ (format "Kill IDLWAVE-created buffers: [f]ind source(%d), [s]hell display(%d), [a]ll ? "
3915 (idlwave-count-outlawed-buffers 'find)
3916 (idlwave-count-outlawed-buffers 'shell)))
3917 (let ((c (read-char)))
3918 (cond
3919 ((member c '(?f ?\C-f))
3920 (idlwave-do-kill-autoloaded-buffers 'find))
3921 ((member c '(?s ?\C-s))
3922 (idlwave-do-kill-autoloaded-buffers 'shell))
3923 ((member c '(?a ?\C-a))
3924 (idlwave-do-kill-autoloaded-buffers t))
3925 (t (error "Abort"))))))
3926
3927(defun idlwave-count-outlawed-buffers (tag)
3928 "How many outlawed buffers have tag TAG?"
3929 (length (delq nil
4b1aaa8b
PE
3930 (mapcar
3931 (lambda (x) (eq (cdr x) tag))
15e42531
CD
3932 idlwave-outlawed-buffers))))
3933
3934(defun idlwave-do-kill-autoloaded-buffers (&rest reasons)
3935 "Kill all buffers pulled up by IDLWAVE matching REASONS."
3936 (let* ((list (copy-sequence idlwave-outlawed-buffers))
3937 (cnt 0)
3938 entry)
3939 (while (setq entry (pop list))
3940 (if (buffer-live-p (car entry))
3941 (and (or (memq t reasons)
3942 (memq (cdr entry) reasons))
3943 (kill-buffer (car entry))
3944 (incf cnt)
4b1aaa8b 3945 (setq idlwave-outlawed-buffers
15e42531 3946 (delq entry idlwave-outlawed-buffers)))
4b1aaa8b 3947 (setq idlwave-outlawed-buffers
15e42531
CD
3948 (delq entry idlwave-outlawed-buffers))))
3949 (message "%d buffer%s killed" cnt (if (= cnt 1) "" "s"))))
3950
3951(defun idlwave-revoke-license-to-kill ()
3952 "Remove BUFFER from the buffers which may be killed.
3953Killing would be done by `idlwave-do-kill-autoloaded-buffers'.
3954Intended for `after-save-hook'."
3955 (let* ((buf (current-buffer))
3956 (entry (assq buf idlwave-outlawed-buffers)))
3957 ;; Revoke license
3958 (if entry
4b1aaa8b 3959 (setq idlwave-outlawed-buffers
15e42531
CD
3960 (delq entry idlwave-outlawed-buffers)))
3961 ;; Remove this function from the hook.
3962 (remove-hook 'after-save-hook 'idlwave-revoke-license-to-kill 'local)))
3963
3964(defvar idlwave-path-alist)
3965(defun idlwave-locate-lib-file (file)
f32b3b91 3966 ;; Find FILE on the scanned lib path and return a buffer visiting it
15e42531 3967 (let* ((dirs idlwave-path-alist)
f32b3b91
CD
3968 dir efile)
3969 (catch 'exit
15e42531 3970 (while (setq dir (car (pop dirs)))
f32b3b91
CD
3971 (if (file-regular-p
3972 (setq efile (expand-file-name file dir)))
15e42531 3973 (throw 'exit efile))))))
52a244eb 3974
15e42531
CD
3975(defun idlwave-expand-lib-file-name (file)
3976 ;; Find FILE on the scanned lib path and return a buffer visiting it
52a244eb 3977 ;; This is for, e.g., finding source with no user catalog
4b1aaa8b 3978 (cond
15e42531 3979 ((null file) nil)
15e42531
CD
3980 ((file-name-absolute-p file) file)
3981 (t (idlwave-locate-lib-file file))))
f32b3b91
CD
3982
3983(defun idlwave-make-tags ()
5a0c3f56
JB
3984 "Create the IDL tags file IDLTAGS in the current directory from
3985the list of directories specified in the minibuffer. Directories may be
3986for example: . /usr/local/rsi/idl/lib. All the subdirectories of the
f32b3b91 3987specified top directories are searched if the directory name is prefixed
5a0c3f56 3988by @. Specify @ directories with care, it may take a long, long time if
f32b3b91
CD
3989you specify /."
3990 (interactive)
3991 (let (directory directories cmd append status numdirs dir getsubdirs
3992 buffer save_buffer files numfiles item errbuf)
4b1aaa8b 3993
f32b3b91
CD
3994 ;;
3995 ;; Read list of directories
3996 (setq directory (read-string "Tag Directories: " "."))
3997 (setq directories (idlwave-split-string directory "[ \t]+"))
3998 ;;
3999 ;; Set etags command, vars
4000 (setq cmd "etags --output=IDLTAGS --language=none --regex='/[
4001\\t]*[pP][Rr][Oo][ \\t]+\\([^ \\t,]+\\)/' --regex='/[
4002\\t]*[Ff][Uu][Nn][Cc][Tt][Ii][Oo][Nn][ \\t]+\\([^ \\t,]+\\)/' ")
4003 (setq append " ")
4004 (setq status 0)
4005 ;;
4006 ;; For each directory
4007 (setq numdirs 0)
4008 (setq dir (nth numdirs directories))
4009 (while (and dir)
4010 ;;
4011 ;; Find the subdirectories
4012 (if (string-match "^[@]\\(.+\\)$" dir)
4013 (setq getsubdirs t) (setq getsubdirs nil))
4014 (if (and getsubdirs) (setq dir (substring dir 1 (length dir))))
4015 (setq dir (expand-file-name dir))
4016 (if (file-directory-p dir)
4017 (progn
4018 (if (and getsubdirs)
4019 (progn
4020 (setq buffer (get-buffer-create "*idltags*"))
4021 (call-process "sh" nil buffer nil "-c"
4022 (concat "find " dir " -type d -print"))
4023 (setq save_buffer (current-buffer))
4024 (set-buffer buffer)
4025 (setq files (idlwave-split-string
4026 (idlwave-replace-string
4027 (buffer-substring 1 (point-max))
4028 "\n" "/*.pro ")
4029 "[ \t]+"))
4030 (set-buffer save_buffer)
4031 (kill-buffer buffer))
4032 (setq files (list (concat dir "/*.pro"))))
4033 ;;
4034 ;; For each subdirectory
4035 (setq numfiles 0)
4036 (setq item (nth numfiles files))
4037 (while (and item)
4038 ;;
4039 ;; Call etags
4040 (if (not (string-match "^[ \\t]*$" item))
4041 (progn
29a4e67d 4042 (message "%s" (concat "Tagging " item "..."))
f32b3b91 4043 (setq errbuf (get-buffer-create "*idltags-error*"))
52a244eb 4044 (setq status (+ status
4b1aaa8b 4045 (if (eq 0 (call-process
52a244eb
S
4046 "sh" nil errbuf nil "-c"
4047 (concat cmd append item)))
4048 0
4049 1)))
f32b3b91
CD
4050 ;;
4051 ;; Append additional tags
4052 (setq append " --append ")
4053 (setq numfiles (1+ numfiles))
4054 (setq item (nth numfiles files)))
4055 (progn
4056 (setq numfiles (1+ numfiles))
4057 (setq item (nth numfiles files))
4058 )))
4b1aaa8b 4059
f32b3b91
CD
4060 (setq numdirs (1+ numdirs))
4061 (setq dir (nth numdirs directories)))
4062 (progn
4063 (setq numdirs (1+ numdirs))
4064 (setq dir (nth numdirs directories)))))
4b1aaa8b 4065
f32b3b91
CD
4066 (setq errbuf (get-buffer-create "*idltags-error*"))
4067 (if (= status 0)
4068 (kill-buffer errbuf))
4069 (message "")
4070 ))
4071
4072(defun idlwave-toggle-comment-region (beg end &optional n)
4073 "Comment the lines in the region if the first non-blank line is
5a0c3f56 4074commented, and conversely, uncomment region. If optional prefix arg
f32b3b91
CD
4075N is non-nil, then for N positive, add N comment delimiters or for N
4076negative, remove N comment delimiters.
4077Uses `comment-region' which does not place comment delimiters on
4078blank lines."
4079 (interactive "r\nP")
4080 (if n
4081 (comment-region beg end (prefix-numeric-value n))
4082 (save-excursion
4083 (goto-char beg)
4084 (beginning-of-line)
4085 ;; skip blank lines
4086 (skip-chars-forward " \t\n")
4087 (if (looking-at (concat "[ \t]*\\(" comment-start "+\\)"))
52a244eb
S
4088 (if (fboundp 'uncomment-region)
4089 (uncomment-region beg end)
4090 (comment-region beg end
4091 (- (length (buffer-substring
4092 (match-beginning 1)
4093 (match-end 1))))))
4094 (comment-region beg end)))))
f32b3b91
CD
4095
4096
4097;; ----------------------------------------------------------------------------
4098;; ----------------------------------------------------------------------------
4099;; ----------------------------------------------------------------------------
4100;; ----------------------------------------------------------------------------
4101;;
4102;; Completion and Routine Info
4103;;
4104
4105;; String "intern" functions
4106
4107;; For the completion and routine info function, we want to normalize
4108;; the case of procedure names etc. We do this by "interning" these
4109;; string is a hand-crafted way. Hashes are used to map the downcase
52a244eb
S
4110;; version of the strings to the cased versions. Most *-sint-*
4111;; variables consist of *two* hashes, a buffer+shell, followed by a
4112;; system hash. The former is re-scanned, and the latter takes case
4113;; precedence.
4114;;
4115;; Since these cased versions are really lisp objects, we can use `eq'
4116;; to search, which is a large performance boost. All new strings
4117;; need to be "sinterned". We do this as early as possible after
4118;; getting these strings from completion or buffer substrings. So
4119;; most of the code can simply assume to deal with "sinterned"
4120;; strings. The only exception is that the functions which scan whole
4121;; buffers for routine information do not intern the grabbed strings.
4122;; This is only done afterwards. Therefore in these functions it is
4123;; *not* safe to assume the strings can be compared with `eq' and be
4124;; fed into the routine assq functions.
f32b3b91
CD
4125
4126;; Here we define the hashing functions.
4127
4128;; The variables which hold the hashes.
4129(defvar idlwave-sint-routines '(nil))
4130(defvar idlwave-sint-keywords '(nil))
4131(defvar idlwave-sint-methods '(nil))
4132(defvar idlwave-sint-classes '(nil))
52a244eb
S
4133(defvar idlwave-sint-dirs '(nil))
4134(defvar idlwave-sint-libnames '(nil))
f32b3b91
CD
4135
4136(defun idlwave-reset-sintern (&optional what)
4137 "Reset all sintern hashes."
4138 ;; Make sure the hash functions are accessible.
8d222148
SM
4139 (unless (and (fboundp 'gethash)
4140 (fboundp 'puthash))
4141 (require 'cl)
4142 (or (fboundp 'puthash)
4143 (defalias 'puthash 'cl-puthash)))
f32b3b91
CD
4144 (let ((entries '((idlwave-sint-routines 1000 10)
4145 (idlwave-sint-keywords 1000 10)
4146 (idlwave-sint-methods 100 10)
4147 (idlwave-sint-classes 10 10))))
4148
4149 ;; Make sure these are lists
4150 (loop for entry in entries
4151 for var = (car entry)
4152 do (if (not (consp (symbol-value var))) (set var (list nil))))
4153
f66f03de 4154 ;; Reset the system & library hash
f32b3b91
CD
4155 (when (or (eq what t) (eq what 'syslib)
4156 (null (cdr idlwave-sint-routines)))
f32b3b91
CD
4157 (loop for entry in entries
4158 for var = (car entry) for size = (nth 1 entry)
4b1aaa8b 4159 do (setcdr (symbol-value var)
f32b3b91 4160 (make-hash-table ':size size ':test 'equal)))
52a244eb
S
4161 (setq idlwave-sint-dirs nil
4162 idlwave-sint-libnames nil))
f32b3b91 4163
f66f03de 4164 ;; Reset the buffer & shell hash
f32b3b91
CD
4165 (when (or (eq what t) (eq what 'bufsh)
4166 (null (car idlwave-sint-routines)))
f32b3b91
CD
4167 (loop for entry in entries
4168 for var = (car entry) for size = (nth 1 entry)
4b1aaa8b 4169 do (setcar (symbol-value var)
f32b3b91
CD
4170 (make-hash-table ':size size ':test 'equal))))))
4171
4172(defun idlwave-sintern-routine-or-method (name &optional class set)
4173 (if class
4174 (idlwave-sintern-method name set)
4175 (idlwave-sintern-routine name set)))
4176
4177(defun idlwave-sintern (stype &rest args)
4178 (apply (intern (concat "idlwave-sintern-" (symbol-name stype))) args))
4179
4180;;(defmacro idlwave-sintern (type var)
4181;; `(cond ((not (stringp name)) name)
4182;; ((gethash (downcase name) (cdr ,var)))
4183;; ((gethash (downcase name) (car ,var)))
4184;; (set (idlwave-sintern-set name ,type ,var set))
4185;; (name)))
4186
4187(defun idlwave-sintern-routine (name &optional set)
4188 (cond ((not (stringp name)) name)
4189 ((gethash (downcase name) (cdr idlwave-sint-routines)))
4190 ((gethash (downcase name) (car idlwave-sint-routines)))
4191 (set (idlwave-sintern-set name 'routine idlwave-sint-routines set))
4192 (name)))
4193(defun idlwave-sintern-keyword (name &optional set)
4194 (cond ((not (stringp name)) name)
4195 ((gethash (downcase name) (cdr idlwave-sint-keywords)))
4196 ((gethash (downcase name) (car idlwave-sint-keywords)))
4197 (set (idlwave-sintern-set name 'keyword idlwave-sint-keywords set))
4198 (name)))
4199(defun idlwave-sintern-method (name &optional set)
4200 (cond ((not (stringp name)) name)
4201 ((gethash (downcase name) (cdr idlwave-sint-methods)))
4202 ((gethash (downcase name) (car idlwave-sint-methods)))
4203 (set (idlwave-sintern-set name 'method idlwave-sint-methods set))
4204 (name)))
4205(defun idlwave-sintern-class (name &optional set)
4206 (cond ((not (stringp name)) name)
4207 ((gethash (downcase name) (cdr idlwave-sint-classes)))
4208 ((gethash (downcase name) (car idlwave-sint-classes)))
4209 (set (idlwave-sintern-set name 'class idlwave-sint-classes set))
4210 (name)))
4211
52a244eb
S
4212(defun idlwave-sintern-dir (dir &optional set)
4213 (car (or (member dir idlwave-sint-dirs)
4214 (setq idlwave-sint-dirs (cons dir idlwave-sint-dirs)))))
4215(defun idlwave-sintern-libname (name &optional set)
4216 (car (or (member name idlwave-sint-libnames)
4217 (setq idlwave-sint-libnames (cons name idlwave-sint-libnames)))))
f32b3b91
CD
4218
4219(defun idlwave-sintern-set (name type tables set)
4220 (let* ((func (or (cdr (assq type idlwave-completion-case))
4221 'identity))
4222 (iname (funcall (if (eq func 'preserve) 'identity func) name))
4223 (table (if (eq set 'sys) (cdr tables) (car tables))))
4224 (puthash (downcase name) iname table)
4225 iname))
4226
52a244eb
S
4227(defun idlwave-sintern-keyword-list (kwd-list &optional set)
4228 "Sintern a set of keywords (file (key . link) (key2 . link2) ...)"
8ffcfb27
GM
4229 (mapc (lambda(x)
4230 (setcar x (idlwave-sintern-keyword (car x) set)))
4231 (cdr kwd-list))
52a244eb
S
4232 kwd-list)
4233
4234(defun idlwave-sintern-rinfo-list (list &optional set default-dir)
5a0c3f56
JB
4235 "Sintern all strings in the rinfo LIST.
4236With optional parameter SET: also set new patterns. Probably this
4237will always have to be t. If DEFAULT-DIR is passed, it is used as
4238the base of the directory."
52a244eb 4239 (let (entry name type class kwds res source call new)
f32b3b91
CD
4240 (while list
4241 (setq entry (car list)
4242 list (cdr list)
4243 name (car entry)
4244 type (nth 1 entry)
4245 class (nth 2 entry)
4246 source (nth 3 entry)
4247 call (nth 4 entry)
52a244eb
S
4248 kwds (nthcdr 5 entry))
4249
4250 ;; The class and name
f32b3b91
CD
4251 (if class
4252 (progn
4253 (if (symbolp class) (setq class (symbol-name class)))
4254 (setq class (idlwave-sintern-class class set))
4255 (setq name (idlwave-sintern-method name set)))
4256 (setq name (idlwave-sintern-routine name set)))
4b1aaa8b 4257
52a244eb
S
4258 ;; The source
4259 (let ((source-type (car source))
4260 (source-file (nth 1 source))
4b1aaa8b 4261 (source-dir (if default-dir
52a244eb
S
4262 (file-name-as-directory default-dir)
4263 (nth 2 source)))
4264 (source-lib (nth 3 source)))
4265 (if (stringp source-dir)
4266 (setq source-dir (idlwave-sintern-dir source-dir set)))
4267 (if (stringp source-lib)
4268 (setq source-lib (idlwave-sintern-libname source-lib set)))
4269 (setq source (list source-type source-file source-dir source-lib)))
4b1aaa8b 4270
52a244eb
S
4271 ;; The keywords
4272 (setq kwds (mapcar (lambda (x)
4273 (idlwave-sintern-keyword-list x set))
4274 kwds))
4275
4276 ;; Build a canonicalized list
4277 (setq new (nconc (list name type class source call) kwds)
4278 res (cons new res)))
f32b3b91
CD
4279 (nreverse res)))
4280
05a1abfc
CD
4281;; Creating new sintern tables
4282
4283(defun idlwave-new-sintern-type (tag)
4284 "Define a variable and a function to sintern the new type TAG.
4285This defines the function `idlwave-sintern-TAG' and the variable
4286`idlwave-sint-TAGs'."
4287 (let* ((name (symbol-name tag))
4288 (names (concat name "s"))
4289 (var (intern (concat "idlwave-sint-" names)))
4290 (func (intern (concat "idlwave-sintern-" name))))
4291 (set var nil) ; initial value of the association list
4292 (fset func ; set the function
4293 `(lambda (name &optional set)
4294 (cond ((not (stringp name)) name)
4295 ((cdr (assoc (downcase name) ,var)))
4296 (set
4297 (setq ,var (cons (cons (downcase name) name) ,var))
4298 name)
4299 (name))))))
4300
4301(defun idlwave-reset-sintern-type (tag)
4302 "Reset the sintern variable associated with TAG."
4303 (set (intern (concat "idlwave-sint-" (symbol-name tag) "s")) nil))
4304
f32b3b91
CD
4305;;---------------------------------------------------------------------------
4306
4307
4308;; The variables which hold the information
15e42531 4309(defvar idlwave-system-routines nil
f32b3b91
CD
4310 "Holds the routine-info obtained by scanning buffers.")
4311(defvar idlwave-buffer-routines nil
4312 "Holds the routine-info obtained by scanning buffers.")
4313(defvar idlwave-compiled-routines nil
15e42531
CD
4314 "Holds the routine-info obtained by asking the shell.")
4315(defvar idlwave-unresolved-routines nil
4316 "Holds the unresolved routine-info obtained by asking the shell.")
52a244eb
S
4317(defvar idlwave-user-catalog-routines nil
4318 "Holds the procedure routine-info from the user scan.")
4319(defvar idlwave-library-catalog-routines nil
3938cb82
S
4320 "Holds the procedure routine-info from the .idlwave_catalog library files.")
4321(defvar idlwave-library-catalog-libname nil
4322 "Name of library catalog loaded from .idlwave_catalog files.")
15e42531 4323(defvar idlwave-path-alist nil
52a244eb
S
4324 "Alist with !PATH directories and zero or more flags if the dir has
4325been scanned in a user catalog ('user) or discovered in a library
4326catalog \('lib).")
15e42531
CD
4327(defvar idlwave-true-path-alist nil
4328 "Like `idlwave-path-alist', but with true filenames.")
f32b3b91 4329(defvar idlwave-routines nil
b9e4fbd3 4330 "Holds the combined procedure/function/method routine-info.")
f32b3b91
CD
4331(defvar idlwave-class-alist nil
4332 "Holds the class names known to IDLWAVE.")
4333(defvar idlwave-class-history nil
4334 "The history of classes selected with the minibuffer.")
4335(defvar idlwave-force-class-query nil)
4336(defvar idlwave-before-completion-wconf nil
4337 "The window configuration just before the completion buffer was displayed.")
15e42531
CD
4338(defvar idlwave-last-system-routine-info-cons-cell nil
4339 "The last cons cell in the system routine info.")
f32b3b91
CD
4340
4341;;
4342;; The code to get routine info from different sources.
4343
15e42531 4344(defvar idlwave-system-routines)
5e72c6b2
S
4345(defvar idlwave-catalog-process nil
4346 "The background process currently updating the catalog.")
4347
f32b3b91
CD
4348(defun idlwave-routines ()
4349 "Provide a list of IDL routines.
5a0c3f56
JB
4350This routine loads the builtin routines on the first call.
4351Later it only returns the value of the variable."
5e72c6b2
S
4352 (if (and idlwave-catalog-process
4353 (processp idlwave-catalog-process))
4354 (progn
4355 (cond
4356 ((equal (process-status idlwave-catalog-process) 'exit)
4357 (message "updating........")
4358 (setq idlwave-catalog-process nil)
4359 (idlwave-update-routine-info '(4)))
4360 ((equal (process-status idlwave-catalog-process) 'run)
4361 ;; Keep it running...
4362 )
4363 (t
4364 ;; Something is wrong, get rid of the process
4365 (message "Problem with catalog process") (beep)
4366 (condition-case nil
4367 (kill-process idlwave-catalog-process)
4368 (error nil))
4369 (setq idlwave-catalog-process nil)))))
f32b3b91
CD
4370 (or idlwave-routines
4371 (progn
4372 (idlwave-update-routine-info)
4373 ;; return the current value
4374 idlwave-routines)))
4375
05a1abfc
CD
4376(defvar idlwave-update-rinfo-hook nil
4377 "List of functions which should run after a global rinfo update.
4378Does not run after automatic updates of buffer or the shell.")
4379
5e72c6b2 4380(defun idlwave-rescan-catalog-directories ()
5a0c3f56 4381 "Rescan the previously selected directories. For batch processing."
5e72c6b2
S
4382 (idlwave-update-routine-info '(16)))
4383
4384(defun idlwave-rescan-asynchronously ()
8a6a28ac 4385 "Dispatch another Emacs instance to update the idlwave catalog.
5e72c6b2
S
4386After the process finishes normally, the first access to routine info
4387will re-read the catalog."
4388 (interactive)
4389 (if (processp idlwave-catalog-process)
4390 (if (eq (process-status idlwave-catalog-process) 'run)
4391 (if (yes-or-no-p "A catalog-updating process is running. Kill it? ")
4392 (progn
4393 (condition-case nil
4394 (kill-process idlwave-catalog-process)
4395 (error nil))
4396 (error "Process killed, no new process started"))
4397 (error "Quit"))
4398 (condition-case nil
4399 (kill-process idlwave-catalog-process)
4400 (error nil))))
52a244eb
S
4401 (if (or (not idlwave-user-catalog-file)
4402 (not (stringp idlwave-user-catalog-file))
4403 (not (file-regular-p idlwave-user-catalog-file)))
5e72c6b2 4404 (error "No catalog has been produced yet"))
4b1aaa8b 4405 (let* ((emacs (concat invocation-directory invocation-name))
5e72c6b2
S
4406 (args (list "-batch"
4407 "-l" (expand-file-name "~/.emacs")
4408 "-l" "idlwave"
4409 "-f" "idlwave-rescan-catalog-directories"))
4b1aaa8b 4410 (process (apply 'start-process "idlcat"
5e72c6b2
S
4411 nil emacs args)))
4412 (setq idlwave-catalog-process process)
4b1aaa8b 4413 (set-process-sentinel
5e72c6b2
S
4414 process
4415 (lambda (pro why)
4416 (when (string-match "finished" why)
4417 (setq idlwave-routines nil
4418 idlwave-system-routines nil
4419 idlwave-catalog-process nil)
4420 (or (idlwave-start-load-rinfo-timer)
4421 (idlwave-update-routine-info '(4))))))
4422 (message "Background job started to update catalog file")))
4423
4424
52a244eb
S
4425;; Format for all routine info user catalog, library catalogs, etc.:
4426;;
4427;; ("ROUTINE" type class
4428;; (system) | (lib pro_file dir "LIBNAME") | (user pro_file dir "USERLIB") |
4429;; (buffer pro_file dir) | (compiled pro_file dir)
4b1aaa8b 4430;; "calling_string" ("HELPFILE" (("KWD1" . link1) ...))
f66f03de 4431;; ("HELPFILE2" (("KWD2" . link) ...)) ...)
52a244eb
S
4432;;
4433;; DIR will be supplied dynamically while loading library catalogs,
4434;; and is sinterned to save space, as is LIBNAME. PRO_FILE can be a
4435;; complete filepath, in which case DIR is unnecessary. HELPFILE can
4436;; be nil, as can LINK1, etc., if no HTML help is available.
4437
4438
5e72c6b2 4439(defvar idlwave-load-rinfo-idle-timer)
3938cb82
S
4440(defvar idlwave-shell-path-query)
4441
52a244eb 4442(defun idlwave-update-routine-info (&optional arg no-concatenate)
f32b3b91
CD
4443 "Update the internal routine-info lists.
4444These lists are used by `idlwave-routine-info' (\\[idlwave-routine-info])
4445and by `idlwave-complete' (\\[idlwave-complete]) to provide information
4446about individual routines.
4447
4448The information can come from 4 sources:
44491. IDL programs in the current editing session
44502. Compiled modules in an IDL shell running as Emacs subprocess
44513. A list which covers the IDL system routines.
44524. A list which covers the prescanned library files.
4453
4454Scans all IDLWAVE-mode buffers of the current editing session (see
4455`idlwave-scan-all-buffers-for-routine-info').
4456When an IDL shell is running, this command also queries the IDL program
4457for currently compiled routines.
4458
4459With prefix ARG, also reload the system and library lists.
52a244eb
S
4460With two prefix ARG's, also rescans the chosen user catalog tree.
4461With three prefix args, dispatch asynchronous process to do the update.
4462
4463If NO-CONCATENATE is non-nil, don't pre-concatenate the routine info
4464lists, but instead wait for the shell query to complete and
4465asynchronously finish updating routine info. This is set
4466automatically when called interactively. When you need routine
4467information updated immediately, leave NO-CONCATENATE nil."
751adbde 4468 (interactive "P\np")
5e72c6b2
S
4469 ;; Stop any idle processing
4470 (if (or (and (fboundp 'itimerp)
4471 (itimerp idlwave-load-rinfo-idle-timer))
4472 (and (fboundp 'timerp)
4473 (timerp idlwave-load-rinfo-idle-timer)))
4474 (cancel-timer idlwave-load-rinfo-idle-timer))
4475 (cond
4476 ((equal arg '(64))
4477 ;; Start a background process which updates the catalog.
4478 (idlwave-rescan-asynchronously))
4479 ((equal arg '(16))
52a244eb
S
4480 ;; Update the user catalog now, and wait for them.
4481 (idlwave-create-user-catalog-file t))
5e72c6b2
S
4482 (t
4483 (let* ((load (or arg
4484 idlwave-buffer-case-takes-precedence
4485 (null idlwave-routines)))
4486 ;; The override-idle means, even if the idle timer has done some
4487 ;; preparing work, load and renormalize everything anyway.
4488 (override-idle (or arg idlwave-buffer-case-takes-precedence)))
4b1aaa8b 4489
f32b3b91 4490 (setq idlwave-buffer-routines nil
15e42531
CD
4491 idlwave-compiled-routines nil
4492 idlwave-unresolved-routines nil)
f32b3b91 4493 ;; Reset the appropriate hashes
5e72c6b2
S
4494 (if (get 'idlwave-reset-sintern 'done-by-idle)
4495 ;; reset was already done in idle time, so skip this step now once
4496 (put 'idlwave-reset-sintern 'done-by-idle nil)
4497 (idlwave-reset-sintern (cond (load t)
4498 ((null idlwave-system-routines) t)
4499 (t 'bufsh))))
4b1aaa8b 4500
f32b3b91
CD
4501 (if idlwave-buffer-case-takes-precedence
4502 ;; We can safely scan the buffer stuff first
4503 (progn
4504 (idlwave-update-buffer-routine-info)
f66f03de 4505 (and load (idlwave-load-all-rinfo override-idle)))
f32b3b91 4506 ;; We first do the system info, and then the buffers
f66f03de 4507 (and load (idlwave-load-all-rinfo override-idle))
f32b3b91
CD
4508 (idlwave-update-buffer-routine-info))
4509
4510 ;; Let's see if there is a shell
4511 (let* ((shell-is-running (and (fboundp 'idlwave-shell-is-running)
4512 (idlwave-shell-is-running)))
4513 (ask-shell (and shell-is-running
4514 idlwave-query-shell-for-routine-info)))
4b1aaa8b 4515
52a244eb 4516 ;; Load the library catalogs again, first re-scanning the path
4b1aaa8b 4517 (when arg
52a244eb
S
4518 (if shell-is-running
4519 (idlwave-shell-send-command idlwave-shell-path-query
4520 '(progn
4521 (idlwave-shell-get-path-info)
4522 (idlwave-scan-library-catalogs))
4523 'hide)
4524 (idlwave-scan-library-catalogs)))
775591f7 4525
f32b3b91 4526 (if (or (not ask-shell)
52a244eb 4527 (not no-concatenate))
f32b3b91
CD
4528 ;; 1. If we are not going to ask the shell, we need to do the
4529 ;; concatenation now.
52a244eb
S
4530 ;; 2. When this function is called non-interactively, it
4531 ;; means that someone needs routine info *now*. The
4532 ;; shell update causes the concatenation to be
4533 ;; *delayed*, so not in time for the current command.
4534 ;; Therefore, we do a concatenation now, even though
4535 ;; the shell might do it again.
4536 (idlwave-concatenate-rinfo-lists nil 'run-hooks))
4b1aaa8b 4537
f32b3b91 4538 (when ask-shell
52a244eb 4539 ;; Ask the shell about the routines it knows of.
f32b3b91 4540 (message "Querying the shell")
5e72c6b2
S
4541 (idlwave-shell-update-routine-info nil t)))))))
4542
52a244eb
S
4543
4544(defvar idlwave-load-rinfo-steps-done (make-vector 6 nil))
5e72c6b2
S
4545(defvar idlwave-load-rinfo-idle-timer nil)
4546(defun idlwave-start-load-rinfo-timer ()
4547 (if (or (and (fboundp 'itimerp)
4548 (itimerp idlwave-load-rinfo-idle-timer))
4549 (and (fboundp 'timerp)
4550 (timerp idlwave-load-rinfo-idle-timer)))
4551 (cancel-timer idlwave-load-rinfo-idle-timer))
52a244eb 4552 (setq idlwave-load-rinfo-steps-done (make-vector 6 nil))
5e72c6b2
S
4553 (setq idlwave-load-rinfo-idle-timer nil)
4554 (if (and idlwave-init-rinfo-when-idle-after
4555 (numberp idlwave-init-rinfo-when-idle-after)
4556 (not (equal 0 idlwave-init-rinfo-when-idle-after))
4557 (not idlwave-routines))
4558 (condition-case nil
4559 (progn
4560 (setq idlwave-load-rinfo-idle-timer
4561 (run-with-idle-timer
4562 idlwave-init-rinfo-when-idle-after
4563 nil 'idlwave-load-rinfo-next-step)))
4564 (error nil))))
4565
3938cb82
S
4566(defvar idlwave-library-routines nil "Obsolete variable.")
4567
f66f03de
S
4568;;------ XML Help routine info system
4569(defun idlwave-load-system-routine-info ()
4570 ;; Load the system routine info from the cached routine info file,
4571 ;; which, if necessary, will be re-created from the XML file on
4572 ;; disk. As a last fallback, load the (likely outdated) idlw-rinfo
4573 ;; file distributed with older IDLWAVE versions (<6.0)
4b1aaa8b 4574 (unless (and (load idlwave-xml-system-rinfo-converted-file
f66f03de
S
4575 'noerror 'nomessage)
4576 (idlwave-xml-system-routine-info-up-to-date))
4577 ;; See if we can create it from XML source
4578 (condition-case nil
4579 (idlwave-convert-xml-system-routine-info)
4b1aaa8b
PE
4580 (error
4581 (unless (load idlwave-xml-system-rinfo-converted-file
f66f03de
S
4582 'noerror 'nomessage)
4583 (if idlwave-system-routines
4b1aaa8b 4584 (message
f66f03de 4585 "Failed to load converted routine info, using old conversion.")
4b1aaa8b 4586 (message
f66f03de
S
4587 "Failed to convert XML routine info, falling back on idlw-rinfo.")
4588 (if (not (load "idlw-rinfo" 'noerror 'nomessage))
4b1aaa8b 4589 (message
f66f03de
S
4590 "Could not locate any system routine information."))))))))
4591
4592(defun idlwave-xml-system-routine-info-up-to-date()
4b1aaa8b 4593 (let* ((dir (file-name-as-directory
f66f03de
S
4594 (expand-file-name "help/online_help" (idlwave-sys-dir))))
4595 (catalog-file (expand-file-name "idl_catalog.xml" dir)))
4596 (file-newer-than-file-p ;converted file is newer than catalog
4597 idlwave-xml-system-rinfo-converted-file
4598 catalog-file)))
4599
4600(defvar idlwave-system-class-info nil) ; Gathered from idlw-rinfo
4601(defvar idlwave-system-variables-alist nil
4602 "Alist of system variables and the associated structure tags.
4603Gets set in cached XML rinfo, or `idlw-rinfo.el'.")
4604(defvar idlwave-executive-commands-alist nil
4605 "Alist of system variables and their help files.")
4606(defvar idlwave-help-special-topic-words nil)
4607
4b1aaa8b 4608
f66f03de 4609(defun idlwave-shorten-syntax (syntax name &optional class)
5a89f0a7 4610 ;; From a list of syntax statements, shorten with %s and group with "or"
f66f03de 4611 (let ((case-fold-search t))
4b1aaa8b 4612 (mapconcat
f66f03de
S
4613 (lambda (x)
4614 (while (string-match name x)
4615 (setq x (replace-match "%s" t t x)))
4b1aaa8b 4616 (if class
f66f03de
S
4617 (while (string-match class x)
4618 (setq x (replace-match "%s" t t x))))
4619 x)
4620 (nreverse syntax)
4621 " or ")))
4622
4623(defun idlwave-xml-create-class-method-lists (xml-entry)
4624 ;; Create a class list entry from the xml parsed list., returning a
4625 ;; cons of form (class-entry method-entries).
4626 (let* ((nameblock (nth 1 xml-entry))
4627 (class (cdr (assq 'name nameblock)))
4628 (link (cdr (assq 'link nameblock)))
4629 (params (cddr xml-entry))
4630 (case-fold-search t)
4631 class-entry
4632 method methods-entry extra-kwds
4633 props get-props set-props init-props inherits
4634 pelem ptype)
4635 (while params
4636 (setq pelem (car params))
4637 (when (listp pelem)
4638 (setq ptype (car pelem)
4639 props (car (cdr pelem)))
4640 (cond
4641 ((eq ptype 'SUPERCLASS)
58c8f915
S
4642 (let ((pname (cdr (assq 'name props)))
4643 (plink (cdr (assq 'link props))))
4644 (unless (and (string= pname "None")
4645 (string= plink "None"))
4646 (push pname inherits))))
f66f03de
S
4647
4648 ((eq ptype 'PROPERTY)
4649 (let ((pname (cdr (assq 'name props)))
4650 (plink (cdr (assq 'link props)))
4651 (get (string= (cdr (assq 'get props)) "Yes"))
4652 (set (string= (cdr (assq 'set props)) "Yes"))
4653 (init (string= (cdr (assq 'init props)) "Yes")))
4654 (if get (push (list pname plink) get-props))
4655 (if set (push (list pname plink) set-props))
4656 (if init (push (list pname plink) init-props))))
4657
4658 ((eq ptype 'METHOD)
4659 (setq method (cdr (assq 'name props)))
4660 (setq extra-kwds ;;Assume all property keywords are gathered already
4661 (cond
4662 ((string-match (concat class "::Init") method)
4663 (put 'init-props 'matched t)
4664 init-props)
4665 ((string-match (concat class "::GetProperty") method)
4666 (put 'get-props 'matched t)
4667 get-props)
4668 ((string-match (concat class "::SetProperty") method)
4669 (put 'set-props 'matched t)
4670 set-props)
4671 (t nil)))
4b1aaa8b
PE
4672 (setq methods-entry
4673 (nconc (idlwave-xml-create-rinfo-list pelem class extra-kwds)
f66f03de
S
4674 methods-entry)))
4675 (t)))
4676 (setq params (cdr params)))
8d222148
SM
4677 ;;(unless (get 'init-props 'matched)
4678 ;; (message "Failed to match Init in class %s" class))
4679 ;;(unless (get 'get-props 'matched)
4680 ;; (message "Failed to match GetProperty in class %s" class))
4681 ;;(unless (get 'set-props 'matched)
4682 ;; (message "Failed to match SetProperty in class %s" class))
4b1aaa8b
PE
4683 (setq class-entry
4684 (if inherits
f66f03de
S
4685 (list class (append '(inherits) inherits) (list 'link link))
4686 (list class (list 'link link))))
4687 (cons class-entry methods-entry)))
4b1aaa8b 4688
f66f03de
S
4689(defun idlwave-xml-create-rinfo-list (xml-entry &optional class extra-kws)
4690 ;; Create correctly structured list elements from ROUTINE or METHOD
4691 ;; XML list structures. Return a list of list elements, with more
4692 ;; than one sub-list possible if a routine can serve as both
4693 ;; procedure and function (e.g. call_method).
4694 (let* ((nameblock (nth 1 xml-entry))
4695 (name (cdr (assq 'name nameblock)))
4696 (link (cdr (assq 'link nameblock)))
4697 (params (cddr xml-entry))
4698 (syntax-vec (make-vector 3 nil)) ; procedure, function, exec command
4699 (case-fold-search t)
8d222148 4700 syntax kwd klink pref-list kwds pelem ptype props result type)
f66f03de
S
4701 (if class ;; strip out class name from class method name string
4702 (if (string-match (concat class "::") name)
4703 (setq name (substring name (match-end 0)))))
4704 (while params
4705 (setq pelem (car params))
4706 (when (listp pelem)
4707 (setq ptype (car pelem)
4708 props (car (cdr pelem)))
4709 (cond
4710 ((eq ptype 'SYNTAX)
4711 (setq syntax (cdr (assq 'name props)))
4712 (if (string-match "-&gt;" syntax)
4713 (setq syntax (replace-match "->" t nil syntax)))
4714 (setq type (cdr (assq 'type props)))
4715 (push syntax
4716 (aref syntax-vec (cond
4717 ((string-match "^pro" type) 0)
4718 ((string-match "^fun" type) 1)
4719 ((string-match "^exec" type) 2)))))
4720 ((eq ptype 'KEYWORD)
4721 (setq kwd (cdr (assq 'name props))
4722 klink (cdr (assq 'link props)))
4723 (if (string-match "^\\[XY\\(Z?\\)\\]" kwd)
4b1aaa8b
PE
4724 (progn
4725 (setq pref-list
f66f03de
S
4726 (if (match-string 1 kwd) '("X" "Y" "Z") '("X" "Y"))
4727 kwd (substring kwd (match-end 0)))
4728 (loop for x in pref-list do
4729 (push (list (concat x kwd) klink) kwds)))
4730 (push (list kwd klink) kwds)))
4731
4732 (t))); Do nothing for the others
4733 (setq params (cdr params)))
4b1aaa8b 4734
f66f03de 4735 ;; Debug
8d222148
SM
4736 ;; (if (and (null (aref syntax-vec 0))
4737 ;; (null (aref syntax-vec 1))
4738 ;; (null (aref syntax-vec 2)))
4739 ;; (with-current-buffer (get-buffer-create "IDL_XML_catalog_complaints")
4740 ;; (if class
4741 ;; (insert (format "Missing SYNTAX entry for %s::%s\n" class name))
4742 ;; (insert (message "Missing SYNTAX entry for %s\n" name)))))
f66f03de
S
4743
4744 ;; Executive commands are treated specially
4745 (if (aref syntax-vec 2)
4746 (cons (substring name 1) link)
4747 (if extra-kws (setq kwds (nconc kwds extra-kws)))
4748 (setq kwds (idlwave-rinfo-group-keywords kwds link))
4749 (loop for idx from 0 to 1 do
4750 (if (aref syntax-vec idx)
4b1aaa8b 4751 (push (append (list name (if (eq idx 0) 'pro 'fun)
f66f03de 4752 class '(system)
4b1aaa8b 4753 (idlwave-shorten-syntax
f66f03de
S
4754 (aref syntax-vec idx) name class))
4755 kwds) result)))
4756 result)))
4757
4758
4759(defun idlwave-rinfo-group-keywords (kwds master-link)
4b1aaa8b 4760 ;; Group keywords by link file, as a list with elements
f66f03de
S
4761 ;; (linkfile ( ("KWD1" . link1) ("KWD2" . link2))
4762 (let (kwd link anchor linkfiles block master-elt)
4763 (while kwds
4764 (setq kwd (car kwds)
4765 link (idlwave-split-link-target (nth 1 kwd))
4766 anchor (cdr link)
4767 link (car link)
4768 kwd (car kwd))
4769 (if (setq block (assoc link linkfiles))
4770 (push (cons kwd anchor) (cdr block))
4771 (push (list link (cons kwd anchor)) linkfiles))
4772 (setq kwds (cdr kwds)))
4773 ;; Ensure the master link is there
4774 (if (setq master-elt (assoc master-link linkfiles))
4775 (if (eq (car linkfiles) master-elt)
4776 linkfiles
4777 (cons master-elt (delq master-elt linkfiles)))
4778 (push (list master-link) linkfiles))))
4b1aaa8b 4779
f66f03de
S
4780(defun idlwave-convert-xml-clean-statement-aliases (aliases)
4781 ;; Clean up the syntax of routines which are actually aliases by
4782 ;; removing the "OR" from the statements
4783 (let (syntax entry)
4784 (loop for x in aliases do
4785 (setq entry (assoc x idlwave-system-routines))
4786 (when entry
4787 (while (string-match " +or +" (setq syntax (nth 4 entry)))
4788 (setf (nth 4 entry) (replace-match ", " t t syntax)))))))
4789
4790(defun idlwave-convert-xml-clean-routine-aliases (aliases)
4791 ;; Duplicate and trim original routine aliases from rinfo list
4b1aaa8b 4792 ;; This if for, e.g. OPENR/OPENW/OPENU
f66f03de
S
4793 (let (alias remove-list new parts all-parts)
4794 (loop for x in aliases do
4795 (when (setq parts (split-string (cdr x) "/"))
4796 (setq new (assoc (cdr x) all-parts))
4797 (unless new
4798 (setq new (cons (cdr x) parts))
4799 (push new all-parts))
4800 (setcdr new (delete (car x) (cdr new)))))
4b1aaa8b 4801
f66f03de
S
4802 ;; Add any missing aliases (separate by slashes)
4803 (loop for x in all-parts do
4804 (if (cdr x)
4805 (push (cons (nth 1 x) (car x)) aliases)))
4806
4807 (loop for x in aliases do
4808 (when (setq alias (assoc (cdr x) idlwave-system-routines))
4809 (unless (memq alias remove-list) (push alias remove-list))
4810 (setq alias (copy-sequence alias))
4811 (setcar alias (car x))
4812 (push alias idlwave-system-routines)))
4813 (loop for x in remove-list do
4814 (delq x idlwave-system-routines))))
4815
4816(defun idlwave-convert-xml-clean-sysvar-aliases (aliases)
4817 ;; Duplicate and trim original routine aliases from rinfo list
4818 ;; This if for, e.g. !X, !Y, !Z.
8d222148 4819 (let (alias remove-list)
f66f03de
S
4820 (loop for x in aliases do
4821 (when (setq alias (assoc (cdr x) idlwave-system-variables-alist))
4822 (unless (memq alias remove-list) (push alias remove-list))
4823 (setq alias (copy-sequence alias))
4824 (setcar alias (car x))
4825 (push alias idlwave-system-variables-alist)))
4826 (loop for x in remove-list do
4827 (delq x idlwave-system-variables-alist))))
4828
4829
4830(defun idlwave-xml-create-sysvar-alist (xml-entry)
4831 ;; Create a sysvar list entry from the xml parsed list.
4832 (let* ((nameblock (nth 1 xml-entry))
a86bd650 4833 (name (cdr (assq 'name nameblock)))
b9e4fbd3 4834 (sysvar (substring name (progn (string-match "^ *!" name)
a86bd650 4835 (match-end 0))))
f66f03de
S
4836 (link (cdr (assq 'link nameblock)))
4837 (params (cddr xml-entry))
4838 (case-fold-search t)
8d222148 4839 pelem ptype props tags)
f66f03de
S
4840 (while params
4841 (setq pelem (car params))
4842 (when (listp pelem)
4843 (setq ptype (car pelem)
4844 props (car (cdr pelem)))
4845 (cond
4846 ((eq ptype 'FIELD)
4b1aaa8b 4847 (push (cons (cdr (assq 'name props))
f66f03de
S
4848 (cdr
4849 (idlwave-split-link-target (cdr (assq 'link props)))))
4850 tags))))
4851 (setq params (cdr params)))
4852 (delq nil
4853 (list sysvar (if tags (cons 'tags tags)) (list 'link link)))))
4854
4855
4856(defvar idlwave-xml-routine-info-file nil)
4857
4858(defun idlwave-save-routine-info ()
4859 (if idlwave-xml-routine-info-file
4860 (with-temp-file idlwave-xml-system-rinfo-converted-file
4b1aaa8b 4861 (insert
f66f03de 4862 (concat ";; *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
4b1aaa8b
PE
4863;; IDLWAVE Routine Information File (IDLWAVE version " idlwave-mode-version ")
4864;; Automatically generated from source file:
f66f03de
S
4865;; " idlwave-xml-routine-info-file "
4866;; on " (current-time-string) "
4867;; Do not edit."))
4868 (insert (format "\n(setq idlwave-xml-routine-info-file \n \"%s\")"
4869 idlwave-xml-routine-info-file))
4870 (insert "\n(setq idlwave-system-routines\n '")
4871 (prin1 idlwave-system-routines (current-buffer))
4872 (insert ")")
4873 (insert "\n(setq idlwave-system-variables-alist\n '")
4874 (prin1 idlwave-system-variables-alist (current-buffer))
4875 (insert ")")
4876 (insert "\n(setq idlwave-system-class-info\n '")
4877 (prin1 idlwave-system-class-info (current-buffer))
4878 (insert ")")
4879 (insert "\n(setq idlwave-executive-commands-alist\n '")
4880 (prin1 idlwave-executive-commands-alist (current-buffer))
4881 (insert ")")
4882 (insert "\n(setq idlwave-help-special-topic-words\n '")
4883 (prin1 idlwave-help-special-topic-words (current-buffer))
4884 (insert ")"))))
4885
4886(defun idlwave-convert-xml-system-routine-info ()
4887 "Convert XML supplied IDL routine info into internal form.
4888Cache to disk for quick recovery."
4889 (interactive)
4b1aaa8b 4890 (let* ((dir (file-name-as-directory
f66f03de
S
4891 (expand-file-name "help/online_help" (idlwave-sys-dir))))
4892 (catalog-file (expand-file-name "idl_catalog.xml" dir))
4893 (elem-cnt 0)
4b1aaa8b 4894 props rinfo msg-cnt elem type nelem class-result alias
8d222148 4895 routines routine-aliases statement-aliases sysvar-aliases)
f66f03de
S
4896 (if (not (file-exists-p catalog-file))
4897 (error "No such XML routine info file: %s" catalog-file)
4898 (if (not (file-readable-p catalog-file))
4899 (error "Cannot read XML routine info file: %s" catalog-file)))
4b1aaa8b 4900 (message "Reading XML routine info...")
e08734e2 4901 (setq rinfo (xml-parse-file catalog-file))
f66f03de
S
4902 (message "Reading XML routine info...done")
4903 (setq rinfo (assq 'CATALOG rinfo))
4904 (unless rinfo (error "Failed to parse XML routine info"))
4905 ;;(setq rinfo (car rinfo)) ; Skip the catalog stuff.
4b1aaa8b 4906
8d222148 4907 (setq rinfo (cddr rinfo))
f66f03de
S
4908
4909 (setq nelem (length rinfo)
4910 msg-cnt (/ nelem 20))
4b1aaa8b 4911
f66f03de
S
4912 (setq idlwave-xml-routine-info-file nil)
4913 (message "Converting XML routine info...")
4914 (setq idlwave-system-routines nil
4915 idlwave-system-variables-alist nil
4916 idlwave-system-class-info nil
4917 idlwave-executive-commands-alist nil
4918 idlwave-help-special-topic-words nil)
4919
4920 (while rinfo
4921 (setq elem (car rinfo)
4922 rinfo (cdr rinfo))
4923 (incf elem-cnt)
4924 (when (listp elem)
4925 (setq type (car elem)
4926 props (car (cdr elem)))
4927 (if (= (mod elem-cnt msg-cnt) 0)
4b1aaa8b 4928 (message "Converting XML routine info...%2d%%"
f66f03de 4929 (/ (* elem-cnt 100) nelem)))
4b1aaa8b 4930 (cond
f66f03de
S
4931 ((eq type 'ROUTINE)
4932 (if (setq alias (assq 'alias_to props))
4b1aaa8b 4933 (push (cons (cdr (assq 'name props)) (cdr alias))
f66f03de
S
4934 routine-aliases)
4935 (setq routines (idlwave-xml-create-rinfo-list elem))
4936 (if (listp (cdr routines))
4937 (setq idlwave-system-routines
4938 (nconc idlwave-system-routines routines))
4939 ;; a cons cell is an executive commands
4940 (push routines idlwave-executive-commands-alist))))
4b1aaa8b 4941
f66f03de
S
4942 ((eq type 'CLASS)
4943 (setq class-result (idlwave-xml-create-class-method-lists elem))
4944 (push (car class-result) idlwave-system-class-info)
4945 (setq idlwave-system-routines
4946 (nconc idlwave-system-routines (cdr class-result))))
4947
4948 ((eq type 'STATEMENT)
4949 (push (cons (cdr (assq 'name props))
4950 (cdr (assq 'link props)))
4951 idlwave-help-special-topic-words)
4952 ;; Save the links to those which are statement aliases (not routines)
4953 (if (setq alias (assq 'alias_to props))
4954 (unless (member (cdr alias) statement-aliases)
4955 (push (cdr alias) statement-aliases))))
4956
4957 ((eq type 'SYSVAR)
4958 (if (setq alias (cdr (assq 'alias_to props)))
4b1aaa8b 4959 (push (cons (substring (cdr (assq 'name props)) 1)
f66f03de
S
4960 (substring alias 1))
4961 sysvar-aliases)
4b1aaa8b 4962 (push (idlwave-xml-create-sysvar-alist elem)
f66f03de
S
4963 idlwave-system-variables-alist)))
4964 (t))))
4965 (idlwave-convert-xml-clean-routine-aliases routine-aliases)
4966 (idlwave-convert-xml-clean-statement-aliases statement-aliases)
4967 (idlwave-convert-xml-clean-sysvar-aliases sysvar-aliases)
4968
4969 (setq idlwave-xml-routine-info-file catalog-file)
4970 (idlwave-save-routine-info)
4971 (message "Converting XML routine info...done")))
4b1aaa8b
PE
4972
4973
f66f03de
S
4974;; ("ROUTINE" type class
4975;; (system) | (lib pro_file dir "LIBNAME") | (user pro_file dir "USERLIB") |
4976;; (buffer pro_file dir) | (compiled pro_file dir)
4b1aaa8b 4977;; "calling_string" ("HELPFILE" (("KWD1" . link1) ...))
f66f03de
S
4978;; ("HELPFILE2" (("KWD2" . link) ...)) ...)
4979
4980
5e72c6b2
S
4981(defun idlwave-load-rinfo-next-step ()
4982 (let ((inhibit-quit t)
4983 (arr idlwave-load-rinfo-steps-done))
f66f03de 4984 (if (catch 'exit
5e72c6b2 4985 (when (not (aref arr 0))
f66f03de
S
4986 (message "Loading system routine info in idle time...")
4987 (idlwave-load-system-routine-info)
4988 ;;(load "idlw-rinfo" 'noerror 'nomessage)
4989 (message "Loading system routine info in idle time...done")
5e72c6b2
S
4990 (aset arr 0 t)
4991 (throw 'exit t))
4b1aaa8b 4992
5e72c6b2
S
4993 (when (not (aref arr 1))
4994 (message "Normalizing idlwave-system-routines in idle time...")
4995 (idlwave-reset-sintern t)
4996 (put 'idlwave-reset-sintern 'done-by-idle t)
4997 (setq idlwave-system-routines
4998 (idlwave-sintern-rinfo-list idlwave-system-routines 'sys))
4999 (message "Normalizing idlwave-system-routines in idle time...done")
5000 (aset arr 1 t)
5001 (throw 'exit t))
f66f03de 5002
5e72c6b2 5003 (when (not (aref arr 2))
52a244eb
S
5004 (when (and (stringp idlwave-user-catalog-file)
5005 (file-regular-p idlwave-user-catalog-file))
5006 (message "Loading user catalog in idle time...")
5e72c6b2 5007 (condition-case nil
52a244eb
S
5008 (load-file idlwave-user-catalog-file)
5009 (error (throw 'exit nil)))
5010 ;; Check for the old style catalog and warn
5011 (if (and
5012 (boundp 'idlwave-library-routines)
5013 idlwave-library-routines)
775591f7 5014 (progn
52a244eb
S
5015 (setq idlwave-library-routines nil)
5016 (ding)
4b1aaa8b 5017 (message "Outdated user catalog: %s... recreate"
52a244eb 5018 idlwave-user-catalog-file))
f66f03de
S
5019 (message "Loading user catalog in idle time...done")))
5020 (aset arr 2 t)
5021 (throw 'exit t))
5022
5e72c6b2 5023 (when (not (aref arr 3))
52a244eb
S
5024 (when idlwave-user-catalog-routines
5025 (message "Normalizing user catalog routines in idle time...")
4b1aaa8b 5026 (setq idlwave-user-catalog-routines
52a244eb
S
5027 (idlwave-sintern-rinfo-list
5028 idlwave-user-catalog-routines 'sys))
4b1aaa8b 5029 (message
52a244eb 5030 "Normalizing user catalog routines in idle time...done"))
5e72c6b2
S
5031 (aset arr 3 t)
5032 (throw 'exit t))
f66f03de 5033
5e72c6b2 5034 (when (not (aref arr 4))
4b1aaa8b 5035 (idlwave-scan-library-catalogs
52a244eb
S
5036 "Loading and normalizing library catalogs in idle time...")
5037 (aset arr 4 t)
5038 (throw 'exit t))
5039 (when (not (aref arr 5))
5e72c6b2
S
5040 (message "Finishing initialization in idle time...")
5041 (idlwave-routines)
5042 (message "Finishing initialization in idle time...done")
4b1aaa8b 5043 (aset arr 5 t)
5e72c6b2 5044 (throw 'exit nil)))
52a244eb
S
5045 ;; restart the timer
5046 (if (sit-for 1)
5047 (idlwave-load-rinfo-next-step)
5048 (setq idlwave-load-rinfo-idle-timer
5049 (run-with-idle-timer
5050 idlwave-init-rinfo-when-idle-after
5051 nil 'idlwave-load-rinfo-next-step))))))
5e72c6b2 5052
8d222148
SM
5053(defvar idlwave-after-load-rinfo-hook nil)
5054
f66f03de
S
5055(defun idlwave-load-all-rinfo (&optional force)
5056 ;; Load and case-treat the system, user catalog, and library routine
5057 ;; info files.
5058
5059 ;; System
5e72c6b2 5060 (when (or force (not (aref idlwave-load-rinfo-steps-done 0)))
f66f03de
S
5061 ;;(load "idlw-rinfo" 'noerror 'nomessage))
5062 (idlwave-load-system-routine-info))
5e72c6b2
S
5063 (when (or force (not (aref idlwave-load-rinfo-steps-done 1)))
5064 (message "Normalizing idlwave-system-routines...")
5065 (setq idlwave-system-routines
5066 (idlwave-sintern-rinfo-list idlwave-system-routines 'sys))
5067 (message "Normalizing idlwave-system-routines...done"))
f66f03de
S
5068 (when idlwave-system-routines
5069 (setq idlwave-routines (copy-sequence idlwave-system-routines))
5070 (setq idlwave-last-system-routine-info-cons-cell
5071 (nthcdr (1- (length idlwave-routines)) idlwave-routines)))
5072
5073 ;; User catalog
52a244eb
S
5074 (when (and (stringp idlwave-user-catalog-file)
5075 (file-regular-p idlwave-user-catalog-file))
f32b3b91 5076 (condition-case nil
52a244eb
S
5077 (when (or force (not (aref idlwave-load-rinfo-steps-done 2)))
5078 (load-file idlwave-user-catalog-file))
5079 (error nil))
4b1aaa8b 5080 (when (and
f66f03de
S
5081 (boundp 'idlwave-library-routines)
5082 idlwave-library-routines)
52a244eb 5083 (setq idlwave-library-routines nil)
4b1aaa8b 5084 (error "Outdated user catalog: %s... recreate"
f66f03de 5085 idlwave-user-catalog-file))
52a244eb
S
5086 (setq idlwave-true-path-alist nil)
5087 (when (or force (not (aref idlwave-load-rinfo-steps-done 3)))
5088 (message "Normalizing user catalog routines...")
4b1aaa8b
PE
5089 (setq idlwave-user-catalog-routines
5090 (idlwave-sintern-rinfo-list
52a244eb
S
5091 idlwave-user-catalog-routines 'sys))
5092 (message "Normalizing user catalog routines...done")))
f66f03de
S
5093
5094 ;; Library catalog
52a244eb
S
5095 (when (or force (not (aref idlwave-load-rinfo-steps-done 4)))
5096 (idlwave-scan-library-catalogs
5097 "Loading and normalizing library catalogs..."))
05a1abfc
CD
5098 (run-hooks 'idlwave-after-load-rinfo-hook))
5099
f32b3b91
CD
5100
5101(defun idlwave-update-buffer-routine-info ()
5102 (let (res)
4b1aaa8b 5103 (cond
15e42531
CD
5104 ((eq idlwave-scan-all-buffers-for-routine-info t)
5105 ;; Scan all buffers, current buffer last
5106 (message "Scanning all buffers...")
4b1aaa8b 5107 (setq res (idlwave-get-routine-info-from-buffers
15e42531
CD
5108 (reverse (buffer-list)))))
5109 ((null idlwave-scan-all-buffers-for-routine-info)
5110 ;; Don't scan any buffers
5111 (setq res nil))
5112 (t
f32b3b91
CD
5113 ;; Just scan this buffer
5114 (if (eq major-mode 'idlwave-mode)
5115 (progn
5116 (message "Scanning current buffer...")
5117 (setq res (idlwave-get-routine-info-from-buffers
15e42531 5118 (list (current-buffer))))))))
f32b3b91 5119 ;; Put the result into the correct variable
4b1aaa8b 5120 (setq idlwave-buffer-routines
52a244eb 5121 (idlwave-sintern-rinfo-list res 'set))))
f32b3b91 5122
05a1abfc 5123(defun idlwave-concatenate-rinfo-lists (&optional quiet run-hook)
f32b3b91 5124 "Put the different sources for routine information together."
4b1aaa8b 5125 ;; The sequence here is important because earlier definitions shadow
f32b3b91 5126 ;; later ones. We assume that if things in the buffers are newer
52a244eb 5127 ;; then in the shell of the system, they are meant to be different.
15e42531
CD
5128 (setcdr idlwave-last-system-routine-info-cons-cell
5129 (append idlwave-buffer-routines
5130 idlwave-compiled-routines
52a244eb
S
5131 idlwave-library-catalog-routines
5132 idlwave-user-catalog-routines))
f32b3b91 5133 (setq idlwave-class-alist nil)
15e42531 5134
f32b3b91 5135 ;; Give a message with information about the number of routines we have.
15e42531 5136 (unless quiet
4b1aaa8b 5137 (message
52a244eb 5138 "Routines Found: buffer(%d) compiled(%d) library(%d) user(%d) system(%d)"
15e42531
CD
5139 (length idlwave-buffer-routines)
5140 (length idlwave-compiled-routines)
52a244eb
S
5141 (length idlwave-library-catalog-routines)
5142 (length idlwave-user-catalog-routines)
05a1abfc
CD
5143 (length idlwave-system-routines)))
5144 (if run-hook
5145 (run-hooks 'idlwave-update-rinfo-hook)))
15e42531
CD
5146
5147(defun idlwave-class-alist ()
5148 "Return the class alist - make it if necessary."
5149 (or idlwave-class-alist
5150 (let (class)
5151 (loop for x in idlwave-routines do
5152 (when (and (setq class (nth 2 x))
5153 (not (assq class idlwave-class-alist)))
5154 (push (list class) idlwave-class-alist)))
4b1aaa8b 5155 idlwave-class-alist)))
15e42531
CD
5156
5157;; Three functions for the hooks
5158(defun idlwave-save-buffer-update ()
5159 (idlwave-update-current-buffer-info 'save-buffer))
5160(defun idlwave-kill-buffer-update ()
5161 (idlwave-update-current-buffer-info 'kill-buffer))
5162(defun idlwave-new-buffer-update ()
5163 (idlwave-update-current-buffer-info 'find-file))
5164
5165(defun idlwave-update-current-buffer-info (why)
5a0c3f56
JB
5166 "Update `idlwave-routines' for current buffer.
5167Can run from `after-save-hook'."
15e42531
CD
5168 (when (and (eq major-mode 'idlwave-mode)
5169 (or (eq t idlwave-auto-routine-info-updates)
5170 (memq why idlwave-auto-routine-info-updates))
5171 idlwave-scan-all-buffers-for-routine-info
5172 idlwave-routines)
5173 (condition-case nil
5174 (let (routines)
5175 (idlwave-replace-buffer-routine-info
5176 (buffer-file-name)
5177 (if (eq why 'kill-buffer)
5178 nil
5179 (setq routines
5180 (idlwave-sintern-rinfo-list
5181 (idlwave-get-routine-info-from-buffers
5182 (list (current-buffer))) 'set))))
5183 (idlwave-concatenate-rinfo-lists 'quiet)
5184 routines)
5185 (error nil))))
5186
5187(defun idlwave-replace-buffer-routine-info (file new)
5188 "Cut the part from FILE out of `idlwave-buffer-routines' and add NEW."
4b1aaa8b 5189 (let ((list idlwave-buffer-routines)
15e42531
CD
5190 found)
5191 (while list
5192 ;; The following test uses eq to make sure it works correctly
5193 ;; when two buffers visit the same file. Then the file names
5194 ;; will be equal, but not eq.
52a244eb 5195 (if (eq (idlwave-routine-source-file (nth 3 (car list))) file)
15e42531
CD
5196 (progn
5197 (setcar list nil)
5198 (setq found t))
5199 (if found
4b1aaa8b 5200 ;; End of that section reached. Jump.
15e42531
CD
5201 (setq list nil)))
5202 (setq list (cdr list)))
5203 (setq idlwave-buffer-routines
5204 (append new (delq nil idlwave-buffer-routines)))))
f32b3b91
CD
5205
5206;;----- Scanning buffers -------------------
5207
5208(defun idlwave-get-routine-info-from-buffers (buffers)
5209 "Call `idlwave-get-buffer-routine-info' on idlwave-mode buffers in BUFFERS."
5210 (let (buf routine-lists res)
5211 (save-excursion
5212 (while (setq buf (pop buffers))
5213 (set-buffer buf)
05a1abfc
CD
5214 (if (and (eq major-mode 'idlwave-mode)
5215 buffer-file-name)
f32b3b91
CD
5216 ;; yes, this buffer has the right mode.
5217 (progn (setq res (condition-case nil
5218 (idlwave-get-buffer-routine-info)
5219 (error nil)))
5220 (push res routine-lists)))))
5221 ;; Concatenate the individual lists and return the result
5222 (apply 'nconc routine-lists)))
5223
5224(defun idlwave-get-buffer-routine-info ()
5225 "Scan the current buffer for routine info. Return (PRO-LIST FUNC-LIST)."
5226 (let* ((case-fold-search t)
5227 routine-list string entry)
5228 (save-excursion
5229 (save-restriction
5230 (widen)
5231 (goto-char (point-min))
4b1aaa8b 5232 (while (re-search-forward
15e42531 5233 "^[ \t]*\\(pro\\|function\\)[ \t]" nil t)
76959b77 5234 (setq string (buffer-substring-no-properties
f32b3b91 5235 (match-beginning 0)
4b1aaa8b 5236 (progn
f32b3b91
CD
5237 (idlwave-end-of-statement)
5238 (point))))
5239 (setq entry (idlwave-parse-definition string))
5240 (push entry routine-list))))
5241 routine-list))
5242
15e42531 5243(defvar idlwave-scanning-lib-dir)
8d222148 5244(defvar idlwave-scanning-lib)
f32b3b91
CD
5245(defun idlwave-parse-definition (string)
5246 "Parse a module definition."
5247 (let ((case-fold-search t)
5248 start name args type keywords class)
5249 ;; Remove comments
5250 (while (string-match ";.*" string)
5251 (setq string (replace-match "" t t string)))
5252 ;; Remove the continuation line stuff
5253 (while (string-match "\\([^a-zA-Z0-9$_]\\)\\$[ \t]*\n" string)
5254 (setq string (replace-match "\\1 " t nil string)))
05a1abfc
CD
5255 (while (string-match "\n" string)
5256 (setq string (replace-match " " t nil string)))
f32b3b91
CD
5257 ;; Match the name and type.
5258 (when (string-match
5259 "\\<\\(pro\\|function\\)\\>\\s-+\\(\\([a-zA-Z0-9$_]+\\)::\\)?\\([a-zA-Z0-9$_]+\\)" string)
5260 (setq start (match-end 0))
5261 (setq type (downcase (match-string 1 string)))
5262 (if (match-beginning 3)
5263 (setq class (match-string 3 string)))
5264 (setq name (match-string 4 string)))
5265 ;; Match normal args and keyword args
5266 (while (string-match
15e42531 5267 ",\\s-*\\([a-zA-Z][a-zA-Z0-9$_]*\\|\\(_ref\\)?_extra\\)\\s-*\\(=\\)?"
f32b3b91
CD
5268 string start)
5269 (setq start (match-end 0))
15e42531 5270 (if (match-beginning 3)
f32b3b91
CD
5271 (push (match-string 1 string) keywords)
5272 (push (match-string 1 string) args)))
5273 ;; Normalize and sort.
5274 (setq args (nreverse args))
4b1aaa8b 5275 (setq keywords (sort keywords (lambda (a b)
f32b3b91
CD
5276 (string< (downcase a) (downcase b)))))
5277 ;; Make and return the entry
5278 ;; We don't know which argument are optional, so this information
5279 ;; will not be contained in the calling sequence.
5280 (list name
5281 (if (equal type "pro") 'pro 'fun)
5282 class
5283 (cond ((not (boundp 'idlwave-scanning-lib))
52a244eb 5284 (list 'buffer (buffer-file-name)))
4b1aaa8b 5285; ((string= (downcase
15e42531
CD
5286; (file-name-sans-extension
5287; (file-name-nondirectory (buffer-file-name))))
5288; (downcase name))
5289; (list 'lib))
5290; (t (cons 'lib (file-name-nondirectory (buffer-file-name))))
52a244eb
S
5291 (t (list 'user (file-name-nondirectory (buffer-file-name))
5292 idlwave-scanning-lib-dir "UserLib")))
4b1aaa8b 5293 (concat
f32b3b91
CD
5294 (if (string= type "function") "Result = " "")
5295 (if class "Obj ->[%s::]" "")
5296 "%s"
5297 (if args
5298 (concat
5299 (if (string= type "function") "(" ", ")
5300 (mapconcat 'identity args ", ")
5301 (if (string= type "function") ")" ""))))
5302 (if keywords
52a244eb 5303 (cons nil (mapcar 'list keywords)) ;No help file
f32b3b91
CD
5304 nil))))
5305
f32b3b91 5306
52a244eb 5307;;----- Scanning the user catalog -------------------
15e42531
CD
5308
5309(defun idlwave-sys-dir ()
5310 "Return the syslib directory, or a dummy that never matches."
3938cb82
S
5311 (cond
5312 ((and idlwave-system-directory
5313 (not (string= idlwave-system-directory "")))
5314 idlwave-system-directory)
5315 ((getenv "IDL_DIR"))
5316 (t "@@@@@@@@")))
5317
52a244eb 5318
52a244eb 5319(defun idlwave-create-user-catalog-file (&optional arg)
f32b3b91 5320 "Scan all files on selected dirs of IDL search path for routine information.
52a244eb
S
5321
5322A widget checklist will allow you to choose the directories. Write
5323the result as a file `idlwave-user-catalog-file'. When this file
5a0c3f56
JB
5324exists, it will be automatically loaded to give routine information
5325about library routines. With ARG, just rescan the same directories
5326as last time - so no widget will pop up."
f32b3b91
CD
5327 (interactive "P")
5328 ;; Make sure the file is loaded if it exists.
52a244eb
S
5329 (if (and (stringp idlwave-user-catalog-file)
5330 (file-regular-p idlwave-user-catalog-file))
f32b3b91 5331 (condition-case nil
52a244eb 5332 (load-file idlwave-user-catalog-file)
f32b3b91
CD
5333 (error nil)))
5334 ;; Make sure the file name makes sense
52a244eb
S
5335 (unless (and (stringp idlwave-user-catalog-file)
5336 (> (length idlwave-user-catalog-file) 0)
f32b3b91 5337 (file-accessible-directory-p
52a244eb 5338 (file-name-directory idlwave-user-catalog-file))
4b1aaa8b 5339 (not (string= "" (file-name-nondirectory
52a244eb
S
5340 idlwave-user-catalog-file))))
5341 (error "`idlwave-user-catalog-file' does not point to a file in an accessible directory"))
4b1aaa8b 5342
f32b3b91 5343 (cond
f32b3b91 5344 ;; Rescan the known directories
52a244eb
S
5345 ((and arg idlwave-path-alist
5346 (consp (car idlwave-path-alist)))
5347 (idlwave-scan-user-lib-files idlwave-path-alist))
5348
5349 ;; Expand the directories from library-path and run the widget
f32b3b91 5350 (idlwave-library-path
52a244eb 5351 (idlwave-display-user-catalog-widget
4b1aaa8b 5352 (if idlwave-true-path-alist
52a244eb
S
5353 ;; Propagate any flags on the existing path-alist
5354 (mapcar (lambda (x)
5355 (let ((path-entry (assoc (file-truename x)
5356 idlwave-true-path-alist)))
5357 (if path-entry
4b1aaa8b 5358 (cons x (cdr path-entry))
52a244eb
S
5359 (list x))))
5360 (idlwave-expand-path idlwave-library-path))
5361 (mapcar 'list (idlwave-expand-path idlwave-library-path)))))
5362
5363 ;; Ask the shell for the path and then run the widget
f32b3b91 5364 (t
f32b3b91 5365 (message "Asking the shell for IDL path...")
15e42531
CD
5366 (require 'idlw-shell)
5367 (idlwave-shell-send-command idlwave-shell-path-query
52a244eb 5368 '(idlwave-user-catalog-command-hook nil)
15e42531 5369 'hide))))
f32b3b91 5370
52a244eb
S
5371
5372;; Parse shell path information and select among it.
5373(defun idlwave-user-catalog-command-hook (&optional arg)
5374 ;; Command hook used by `idlwave-create-user-catalog-file'.
f32b3b91
CD
5375 (if arg
5376 ;; Scan immediately
52a244eb
S
5377 (idlwave-scan-user-lib-files idlwave-path-alist)
5378 ;; Set the path and display the widget
5379 (idlwave-shell-get-path-info 'no-write) ; set to something path-alist
5380 (idlwave-scan-library-catalogs "Locating library catalogs..." 'no-load)
5381 (idlwave-display-user-catalog-widget idlwave-path-alist)))
5382
4b1aaa8b 5383(defconst idlwave-user-catalog-widget-help-string
52a244eb
S
5384 "This is the front-end to the creation of the IDLWAVE user catalog.
5385Please select the directories on IDL's search path from which you
5386would like to extract routine information, to be stored in the file:
f32b3b91
CD
5387
5388 %s
5389
52a244eb
S
5390If this is not the correct file, first set variable
5391`idlwave-user-catalog-file', and call this command again.
15e42531 5392
52a244eb
S
5393N.B. Many libraries include pre-scanned catalog files
5394\(\".idlwave_catalog\"). These are marked with \"[LIB]\", and need
5395not be scanned. You can scan your own libraries off-line using the
5396perl script `idlwave_catalog'.
15e42531 5397
f32b3b91
CD
5398After selecting the directories, choose [Scan & Save] to scan the library
5399directories and save the routine info.
5400\n")
5401
5402(defvar idlwave-widget)
5403(defvar widget-keymap)
52a244eb 5404(defun idlwave-display-user-catalog-widget (dirs-list)
f32b3b91
CD
5405 "Create the widget to select IDL search path directories for scanning."
5406 (interactive)
5407 (require 'widget)
5408 (require 'wid-edit)
52a244eb 5409 (unless dirs-list
f32b3b91
CD
5410 (error "Don't know IDL's search path"))
5411
f32b3b91
CD
5412 (kill-buffer (get-buffer-create "*IDLWAVE Widget*"))
5413 (switch-to-buffer (get-buffer-create "*IDLWAVE Widget*"))
5414 (kill-all-local-variables)
5415 (make-local-variable 'idlwave-widget)
52a244eb
S
5416 (widget-insert (format idlwave-user-catalog-widget-help-string
5417 idlwave-user-catalog-file))
4b1aaa8b 5418
f32b3b91 5419 (widget-create 'push-button
52a244eb 5420 :notify 'idlwave-widget-scan-user-lib-files
f32b3b91
CD
5421 "Scan & Save")
5422 (widget-insert " ")
5423 (widget-create 'push-button
52a244eb 5424 :notify 'idlwave-delete-user-catalog-file
f32b3b91
CD
5425 "Delete File")
5426 (widget-insert " ")
5427 (widget-create 'push-button
4b1aaa8b 5428 :notify
8d222148
SM
5429 (lambda (&rest ignore)
5430 (let ((path-list (widget-get idlwave-widget :path-dirs)))
5431 (dolist (x path-list)
5432 (unless (memq 'lib (cdr x))
5433 (idlwave-path-alist-add-flag x 'user)))
5434 (idlwave-display-user-catalog-widget path-list)))
52a244eb 5435 "Select All Non-Lib")
f32b3b91
CD
5436 (widget-insert " ")
5437 (widget-create 'push-button
4b1aaa8b 5438 :notify
8d222148
SM
5439 (lambda (&rest ignore)
5440 (let ((path-list (widget-get idlwave-widget :path-dirs)))
5441 (dolist (x path-list)
5442 (idlwave-path-alist-remove-flag x 'user))
5443 (idlwave-display-user-catalog-widget path-list)))
f32b3b91 5444 "Deselect All")
52a244eb
S
5445 (widget-insert " ")
5446 (widget-create 'push-button
5447 :notify (lambda (&rest ignore)
5448 (kill-buffer (current-buffer)))
5449 "Quit")
f32b3b91
CD
5450 (widget-insert "\n\n")
5451
52a244eb 5452 (widget-insert "Select Directories: \n")
4b1aaa8b 5453
f32b3b91
CD
5454 (setq idlwave-widget
5455 (apply 'widget-create
5456 'checklist
4b1aaa8b
PE
5457 :value (delq nil (mapcar (lambda (x)
5458 (if (memq 'user (cdr x))
52a244eb
S
5459 (car x)))
5460 dirs-list))
f32b3b91
CD
5461 :greedy t
5462 :tag "List of directories"
4b1aaa8b
PE
5463 (mapcar (lambda (x)
5464 (list 'item
52a244eb
S
5465 (if (memq 'lib (cdr x))
5466 (concat "[LIB] " (car x) )
5467 (car x)))) dirs-list)))
5468 (widget-put idlwave-widget :path-dirs dirs-list)
f32b3b91
CD
5469 (widget-insert "\n")
5470 (use-local-map widget-keymap)
5471 (widget-setup)
5472 (goto-char (point-min))
5473 (delete-other-windows))
4b1aaa8b 5474
52a244eb 5475(defun idlwave-delete-user-catalog-file (&rest ignore)
f32b3b91 5476 (if (yes-or-no-p
52a244eb 5477 (format "Delete file %s " idlwave-user-catalog-file))
f32b3b91 5478 (progn
52a244eb
S
5479 (delete-file idlwave-user-catalog-file)
5480 (message "%s has been deleted" idlwave-user-catalog-file))))
f32b3b91 5481
52a244eb
S
5482(defun idlwave-widget-scan-user-lib-files (&rest ignore)
5483 ;; Call `idlwave-scan-user-lib-files' with data taken from the widget.
f32b3b91 5484 (let* ((widget idlwave-widget)
15e42531 5485 (selected-dirs (widget-value widget))
52a244eb
S
5486 (path-alist (widget-get widget :path-dirs))
5487 (this-path-alist path-alist)
5488 dir-entry)
5489 (while (setq dir-entry (pop this-path-alist))
4b1aaa8b 5490 (if (member
52a244eb
S
5491 (if (memq 'lib (cdr dir-entry))
5492 (concat "[LIB] " (car dir-entry))
5493 (car dir-entry))
5494 selected-dirs)
5495 (idlwave-path-alist-add-flag dir-entry 'user)
5496 (idlwave-path-alist-remove-flag dir-entry 'user)))
5497 (idlwave-scan-user-lib-files path-alist)))
f32b3b91
CD
5498
5499(defvar font-lock-mode)
52a244eb
S
5500(defun idlwave-scan-user-lib-files (path-alist)
5501 ;; Scan the PRO files in PATH-ALIST and store the info in the user catalog
f32b3b91 5502 (let* ((idlwave-scanning-lib t)
15e42531 5503 (idlwave-scanning-lib-dir "")
f32b3b91 5504 (idlwave-completion-case nil)
15e42531 5505 dirs-alist dir files file)
52a244eb
S
5506 (setq idlwave-user-catalog-routines nil
5507 idlwave-path-alist path-alist ; for library-path instead
5508 idlwave-true-path-alist nil)
5509 (if idlwave-auto-write-paths (idlwave-write-paths))
9a529312 5510 (with-current-buffer (get-buffer-create "*idlwave-scan.pro*")
f32b3b91 5511 (idlwave-mode)
15e42531
CD
5512 (setq dirs-alist (reverse path-alist))
5513 (while (setq dir (pop dirs-alist))
52a244eb 5514 (when (memq 'user (cdr dir)) ; Has it marked for scan?
15e42531 5515 (setq dir (car dir))
52a244eb 5516 (setq idlwave-scanning-lib-dir dir)
15e42531
CD
5517 (when (file-directory-p dir)
5518 (setq files (directory-files dir 'full "\\.[pP][rR][oO]\\'"))
5519 (while (setq file (pop files))
5520 (when (file-regular-p file)
5521 (if (not (file-readable-p file))
5522 (message "Skipping %s (no read permission)" file)
5523 (message "Scanning %s..." file)
5524 (erase-buffer)
5525 (insert-file-contents file 'visit)
52a244eb 5526 (setq idlwave-user-catalog-routines
15e42531
CD
5527 (append (idlwave-get-routine-info-from-buffers
5528 (list (current-buffer)))
52a244eb
S
5529 idlwave-user-catalog-routines)))))))))
5530 (message "Creating user catalog file...")
f32b3b91
CD
5531 (kill-buffer "*idlwave-scan.pro*")
5532 (kill-buffer (get-buffer-create "*IDLWAVE Widget*"))
15e42531
CD
5533 (let ((font-lock-maximum-size 0)
5534 (auto-mode-alist nil))
52a244eb 5535 (find-file idlwave-user-catalog-file))
f32b3b91
CD
5536 (if (and (boundp 'font-lock-mode)
5537 font-lock-mode)
5538 (font-lock-mode 0))
5539 (erase-buffer)
52a244eb 5540 (insert ";; IDLWAVE user catalog file\n")
f32b3b91
CD
5541 (insert (format ";; Created %s\n\n" (current-time-string)))
5542
f32b3b91 5543 ;; Define the routine info list
52a244eb 5544 (insert "\n(setq idlwave-user-catalog-routines\n '(")
5e72c6b2 5545 (let ((standard-output (current-buffer)))
8ffcfb27
GM
5546 (mapc (lambda (x)
5547 (insert "\n ")
5548 (prin1 x)
5549 (goto-char (point-max)))
5550 idlwave-user-catalog-routines))
f32b3b91 5551 (insert (format "))\n\n;;; %s ends here\n"
52a244eb 5552 (file-name-nondirectory idlwave-user-catalog-file)))
f32b3b91
CD
5553 (goto-char (point-min))
5554 ;; Save the buffer
5555 (save-buffer 0)
5556 (kill-buffer (current-buffer)))
52a244eb 5557 (message "Creating user catalog file...done")
f32b3b91 5558 (message "Info for %d routines saved in %s"
52a244eb
S
5559 (length idlwave-user-catalog-routines)
5560 idlwave-user-catalog-file)
f32b3b91
CD
5561 (sit-for 2)
5562 (idlwave-update-routine-info t))
5563
52a244eb
S
5564(defun idlwave-read-paths ()
5565 (if (and (stringp idlwave-path-file)
5566 (file-regular-p idlwave-path-file))
5567 (condition-case nil
5568 (load idlwave-path-file t t t)
5569 (error nil))))
5570
5571(defun idlwave-write-paths ()
5572 (interactive)
5573 (when (and idlwave-path-alist idlwave-system-directory)
5574 (let ((font-lock-maximum-size 0)
5575 (auto-mode-alist nil))
5576 (find-file idlwave-path-file))
5577 (if (and (boundp 'font-lock-mode)
5578 font-lock-mode)
5579 (font-lock-mode 0))
5580 (erase-buffer)
5581 (insert ";; IDLWAVE paths\n")
5582 (insert (format ";; Created %s\n\n" (current-time-string)))
5583 ;; Define the variable which knows the value of "!DIR"
5584 (insert (format "\n(setq idlwave-system-directory \"%s\")\n"
5585 idlwave-system-directory))
4b1aaa8b 5586
52a244eb
S
5587 ;; Define the variable which contains a list of all scanned directories
5588 (insert "\n(setq idlwave-path-alist\n '(")
5589 (let ((standard-output (current-buffer)))
8ffcfb27
GM
5590 (mapc (lambda (x)
5591 (insert "\n ")
5592 (prin1 x)
5593 (goto-char (point-max)))
5594 idlwave-path-alist))
52a244eb
S
5595 (insert "))\n")
5596 (save-buffer 0)
5597 (kill-buffer (current-buffer))))
5598
5599
f32b3b91
CD
5600(defun idlwave-expand-path (path &optional default-dir)
5601 ;; Expand parts of path starting with '+' recursively into directory list.
5602 ;; Relative recursive path elements are expanded relative to DEFAULT-DIR.
5603 (message "Expanding path...")
5604 (let (path1 dir recursive)
5605 (while (setq dir (pop path))
5606 (if (setq recursive (string= (substring dir 0 1) "+"))
5607 (setq dir (substring dir 1)))
5608 (if (and recursive
5609 (not (file-name-absolute-p dir)))
5610 (setq dir (expand-file-name dir default-dir)))
5611 (if recursive
5612 ;; Expand recursively
5613 (setq path1 (append (idlwave-recursive-directory-list dir) path1))
5614 ;; Keep unchanged
5615 (push dir path1)))
5616 (message "Expanding path...done")
5617 (nreverse path1)))
5618
5619(defun idlwave-recursive-directory-list (dir)
5620 ;; Return a list of all directories below DIR, including DIR itself
5621 (let ((path (list dir)) path1 file files)
5622 (while (setq dir (pop path))
5623 (when (file-directory-p dir)
5624 (setq files (nreverse (directory-files dir t "[^.]")))
5625 (while (setq file (pop files))
4b1aaa8b 5626 (if (file-directory-p file)
f32b3b91
CD
5627 (push (file-name-as-directory file) path)))
5628 (push dir path1)))
5629 path1))
5630
52a244eb
S
5631
5632;;----- Scanning the library catalogs ------------------
5633
3938cb82
S
5634
5635
5636
52a244eb 5637(defun idlwave-scan-library-catalogs (&optional message-base no-load)
4b1aaa8b 5638 "Scan for library catalog files (.idlwave_catalog) and ingest.
52a244eb
S
5639
5640All directories on `idlwave-path-alist' (or `idlwave-library-path'
5641instead, if present) are searched. Print MESSAGE-BASE along with the
5642libraries being loaded, if passed, and skip loading/normalizing if
5643NO-LOAD is non-nil. The variable `idlwave-use-library-catalogs' can
5644be set to nil to disable library catalog scanning."
5645 (when idlwave-use-library-catalogs
4b1aaa8b 5646 (let ((dirs
52a244eb
S
5647 (if idlwave-library-path
5648 (idlwave-expand-path idlwave-library-path)
5649 (mapcar 'car idlwave-path-alist)))
5650 (old-libname "")
8d222148 5651 dir-entry dir catalog all-routines)
52a244eb
S
5652 (if message-base (message message-base))
5653 (while (setq dir (pop dirs))
5654 (catch 'continue
4b1aaa8b 5655 (when (file-readable-p
52a244eb
S
5656 (setq catalog (expand-file-name ".idlwave_catalog" dir)))
5657 (unless no-load
5658 (setq idlwave-library-catalog-routines nil)
5659 ;; Load the catalog file
5660 (condition-case nil
5661 (load catalog t t t)
5662 (error (throw 'continue t)))
4b1aaa8b
PE
5663 (when (and
5664 message-base
5665 (not (string= idlwave-library-catalog-libname
52a244eb 5666 old-libname)))
4b1aaa8b 5667 (message "%s" (concat message-base
f66f03de 5668 idlwave-library-catalog-libname))
52a244eb
S
5669 (setq old-libname idlwave-library-catalog-libname))
5670 (when idlwave-library-catalog-routines
5671 (setq all-routines
4b1aaa8b 5672 (append
52a244eb
S
5673 (idlwave-sintern-rinfo-list
5674 idlwave-library-catalog-routines 'sys dir)
5675 all-routines))))
4b1aaa8b 5676
52a244eb
S
5677 ;; Add a 'lib flag if on path-alist
5678 (when (and idlwave-path-alist
5679 (setq dir-entry (assoc dir idlwave-path-alist)))
5680 (idlwave-path-alist-add-flag dir-entry 'lib)))))
5681 (unless no-load (setq idlwave-library-catalog-routines all-routines))
5682 (if message-base (message (concat message-base "done"))))))
5683
5684;;----- Communicating with the Shell -------------------
f32b3b91
CD
5685
5686;; First, here is the idl program which can be used to query IDL for
4b1aaa8b 5687;; defined routines.
f32b3b91
CD
5688(defconst idlwave-routine-info.pro
5689 "
05a1abfc 5690;; START OF IDLWAVE SUPPORT ROUTINES
f66f03de
S
5691pro idlwave_print_safe,item,limit
5692 catch,err
5693 if err ne 0 then begin
5694 print,'Could not print item.'
5695 return
5696 endif
5697 if n_elements(item) gt limit then $
5698 print,item[0:limit-1],'<... truncated at ',strtrim(limit,2),' elements>' $
5699 else print,item
5700end
5701
15e42531 5702pro idlwave_print_info_entry,name,func=func,separator=sep
f32b3b91 5703 ;; See if it's an object method
15e42531 5704 if name eq '' then return
4b1aaa8b 5705 func = keyword_set(func)
f32b3b91
CD
5706 methsep = strpos(name,'::')
5707 meth = methsep ne -1
4b1aaa8b 5708
f32b3b91
CD
5709 ;; Get routine info
5710 pars = routine_info(name,/parameters,functions=func)
5711 source = routine_info(name,/source,functions=func)
5712 nargs = pars.num_args
5713 nkw = pars.num_kw_args
5714 if nargs gt 0 then args = pars.args
5715 if nkw gt 0 then kwargs = pars.kw_args
4b1aaa8b 5716
f32b3b91 5717 ;; Trim the class, and make the name
4b1aaa8b 5718 if meth then begin
f32b3b91
CD
5719 class = strmid(name,0,methsep)
5720 name = strmid(name,methsep+2,strlen(name)-1)
4b1aaa8b 5721 if nargs gt 0 then begin
f32b3b91
CD
5722 ;; remove the self argument
5723 wh = where(args ne 'SELF',nargs)
52a244eb 5724 if nargs gt 0 then args = args[wh]
f32b3b91
CD
5725 endif
5726 endif else begin
5727 ;; No class, just a normal routine.
5728 class = \"\"
5729 endelse
4b1aaa8b 5730
f32b3b91
CD
5731 ;; Calling sequence
5732 cs = \"\"
5733 if func then cs = 'Result = '
5734 if meth then cs = cs + 'Obj -> [' + '%s' + '::]'
5735 cs = cs + '%s'
5736 if func then cs = cs + '(' else if nargs gt 0 then cs = cs + ', '
5737 if nargs gt 0 then begin
5738 for j=0,nargs-1 do begin
52a244eb 5739 cs = cs + args[j]
f32b3b91
CD
5740 if j lt nargs-1 then cs = cs + ', '
5741 endfor
5742 end
5743 if func then cs = cs + ')'
5744 ;; Keyword arguments
5745 kwstring = ''
5746 if nkw gt 0 then begin
5747 for j=0,nkw-1 do begin
52a244eb 5748 kwstring = kwstring + ' ' + kwargs[j]
f32b3b91
CD
5749 endfor
5750 endif
4b1aaa8b 5751
52a244eb 5752 ret=(['IDLWAVE-PRO','IDLWAVE-FUN'])[func]
4b1aaa8b 5753
52a244eb 5754 print,ret + ': ' + name + sep + class + sep + source[0].path $
f32b3b91
CD
5755 + sep + cs + sep + kwstring
5756end
5757
f66f03de 5758pro idlwave_routine_info,file
52a244eb 5759 on_error,1
f32b3b91
CD
5760 sep = '<@>'
5761 print,'>>>BEGIN OF IDLWAVE ROUTINE INFO (\"' + sep + '\" IS THE SEPARATOR)'
5762 all = routine_info()
f66f03de
S
5763 fileQ=n_elements(file) ne 0
5764 if fileQ then file=strtrim(file,2)
4b1aaa8b
PE
5765 for i=0L,n_elements(all)-1L do begin
5766 if fileQ then begin
f66f03de
S
5767 if (routine_info(all[i],/SOURCE)).path eq file then $
5768 idlwave_print_info_entry,all[i],separator=sep
5769 endif else idlwave_print_info_entry,all[i],separator=sep
4b1aaa8b 5770 endfor
f32b3b91 5771 all = routine_info(/functions)
4b1aaa8b
PE
5772 for i=0L,n_elements(all)-1L do begin
5773 if fileQ then begin
f66f03de
S
5774 if (routine_info(all[i],/FUNCTIONS,/SOURCE)).path eq file then $
5775 idlwave_print_info_entry,all[i],separator=sep,/FUNC
5776 endif else idlwave_print_info_entry,all[i],separator=sep,/FUNC
4b1aaa8b 5777 endfor
f32b3b91
CD
5778 print,'>>>END OF IDLWAVE ROUTINE INFO'
5779end
05a1abfc
CD
5780
5781pro idlwave_get_sysvars
52a244eb 5782 on_error,1
05a1abfc
CD
5783 catch,error_status
5784 if error_status ne 0 then begin
5785 print, 'Cannot get info about system variables'
5786 endif else begin
5787 help,/brief,output=s,/system_variables ; ? unsafe use of OUTPUT=
5788 s = strtrim(strjoin(s,' ',/single),2) ; make one line
5789 v = strsplit(s,' +',/regex,/extract) ; get variables
f66f03de 5790 for i=0L,n_elements(v)-1 do begin
05a1abfc
CD
5791 t = [''] ; get tag list
5792 a=execute('if n_tags('+v[i]+') gt 0 then t=tag_names('+v[i]+')')
5793 print, 'IDLWAVE-SYSVAR: '+v[i]+' '+strjoin(t,' ',/single)
5794 endfor
5795 endelse
5796end
5797
5798pro idlwave_get_class_tags, class
5799 res = execute('tags=tag_names({'+class+'})')
5e72c6b2 5800 if res then print,'IDLWAVE-CLASS-TAGS: '+class+' '+strjoin(tags,' ',/single)
05a1abfc
CD
5801end
5802;; END OF IDLWAVE SUPPORT ROUTINES
4b1aaa8b 5803"
5a0c3f56 5804 "The IDL programs to get info from the shell.")
f32b3b91 5805
15e42531 5806(defvar idlwave-idlwave_routine_info-compiled nil
5a0c3f56 5807 "Remember if the routine info procedure is already compiled.")
f32b3b91
CD
5808
5809(defvar idlwave-shell-temp-pro-file)
15e42531 5810(defvar idlwave-shell-temp-rinfo-save-file)
f66f03de
S
5811
5812(defun idlwave-shell-compile-helper-routines (&optional wait)
15e42531 5813 (unless (and idlwave-idlwave_routine_info-compiled
5e72c6b2 5814 (file-readable-p (idlwave-shell-temp-file 'rinfo)))
9a529312
SM
5815 (with-current-buffer (idlwave-find-file-noselect
5816 (idlwave-shell-temp-file 'pro))
15e42531
CD
5817 (erase-buffer)
5818 (insert idlwave-routine-info.pro)
5819 (save-buffer 0))
4b1aaa8b 5820 (idlwave-shell-send-command
f66f03de 5821 (concat ".run \"" idlwave-shell-temp-pro-file "\"")
52a244eb 5822 nil 'hide wait)
15e42531 5823 (idlwave-shell-send-command
4b1aaa8b 5824 (format "save,'idlwave_print_safe','idlwave_routine_info','idlwave_print_info_entry','idlwave_get_class_tags','idlwave_get_sysvars',FILE='%s',/ROUTINES"
5e72c6b2 5825 (idlwave-shell-temp-file 'rinfo))
f66f03de
S
5826 nil 'hide)
5827 (setq idlwave-idlwave_routine_info-compiled t))
15e42531 5828
f66f03de
S
5829 ;; Restore if necessary. Must use execute to hide lame routine_info
5830 ;; errors on undefinded routine
15e42531 5831 (idlwave-shell-send-command
f66f03de
S
5832 (format "if execute(\"_v=routine_info('idlwave_routine_info',/SOURCE)\") eq 0 then restore,'%s' else if _v.path eq '' then restore,'%s'"
5833 idlwave-shell-temp-rinfo-save-file
15e42531 5834 idlwave-shell-temp-rinfo-save-file)
f66f03de
S
5835 nil 'hide))
5836
5837
5838(defun idlwave-shell-update-routine-info (&optional quiet run-hooks wait file)
5839 "Query the shell for routine_info of compiled modules and update the lists."
5840 ;; Save and compile the procedure. The compiled procedure is then
5841 ;; saved into an IDL SAVE file, to allow for fast RESTORE. We may
5842 ;; need to test for and possibly RESTORE the procedure each time we
5843 ;; use it, since the user may have killed or redefined it. In
5844 ;; particular, .RESET_SESSION will kill all user procedures. If
5845 ;; FILE is set, only update routine info for routines in that file.
5846
5847 (idlwave-shell-compile-helper-routines wait)
5848 ; execute the routine_info procedure, and analyze the output
5849 (idlwave-shell-send-command
5850 (format "idlwave_routine_info%s" (if file (concat ",'" file "'") ""))
15e42531
CD
5851 `(progn
5852 (idlwave-shell-routine-info-filter)
05a1abfc 5853 (idlwave-concatenate-rinfo-lists ,quiet ,run-hooks))
52a244eb 5854 'hide wait))
f32b3b91
CD
5855
5856;; ---------------------------------------------------------------------------
5857;;
5858;; Completion and displaying routine calling sequences
5859
15e42531 5860(defvar idlwave-completion-help-info nil)
52a244eb 5861(defvar idlwave-completion-help-links nil)
15e42531 5862(defvar idlwave-current-obj_new-class nil)
05a1abfc 5863(defvar idlwave-complete-special nil)
8d222148
SM
5864(defvar method-selector)
5865(defvar class-selector)
5866(defvar type-selector)
5867(defvar super-classes)
15e42531 5868
f32b3b91
CD
5869(defun idlwave-complete (&optional arg module class)
5870 "Complete a function, procedure or keyword name at point.
2e8b9c7d 5871This function is smart and figures out what can be completed
f32b3b91
CD
5872at this point.
5873- At the beginning of a statement it completes procedure names.
5874- In the middle of a statement it completes function names.
5a0c3f56 5875- After a `(' or `,' in the argument list of a function or procedure,
f32b3b91
CD
5876 it completes a keyword of the relevant function or procedure.
5877- In the first arg of `OBJ_NEW', it completes a class name.
5878
5a0c3f56
JB
5879When several completions are possible, a list will be displayed in
5880the *Completions* buffer. If this list is too long to fit into the
5e72c6b2
S
5881window, scrolling can be achieved by repeatedly pressing
5882\\[idlwave-complete].
f32b3b91
CD
5883
5884The function also knows about object methods. When it needs a class
5885name, the action depends upon `idlwave-query-class', which see. You
5e72c6b2
S
5886can force IDLWAVE to ask you for a class name with a
5887\\[universal-argument] prefix argument to this command.
f32b3b91
CD
5888
5889See also the variables `idlwave-keyword-completion-adds-equal' and
5890`idlwave-function-completion-adds-paren'.
5891
5892The optional ARG can be used to specify the completion type in order
5893to override IDLWAVE's idea of what should be completed at point.
5894Possible values are:
5895
58960 <=> query for the completion type
58971 <=> 'procedure
58982 <=> 'procedure-keyword
58993 <=> 'function
59004 <=> 'function-keyword
59015 <=> 'procedure-method
59026 <=> 'procedure-method-keyword
59037 <=> 'function-method
59048 <=> 'function-method-keyword
59059 <=> 'class
5906
5e72c6b2
S
5907As a special case, the universal argument C-u forces completion of
5908function names in places where the default would be a keyword.
5909
52a244eb
S
5910Two prefix argument, C-u C-u, prompts for a regexp by which to limit
5911completion.
5912
f32b3b91
CD
5913For Lisp programmers only:
5914When we force a keyword, optional argument MODULE can contain the module name.
5915When we force a method or a method keyword, CLASS can specify the class."
5916 (interactive "P")
5917 (idlwave-routines)
5918 (let* ((where-list
5919 (if (and arg
52a244eb 5920 (or (and (integerp arg) (not (equal arg '(16))))
f32b3b91
CD
5921 (symbolp arg)))
5922 (idlwave-make-force-complete-where-list arg module class)
5923 (idlwave-where)))
5924 (what (nth 2 where-list))
52a244eb
S
5925 (idlwave-force-class-query (equal arg '(4)))
5926 (completion-regexp-list
5927 (if (equal arg '(16))
5928 (list (read-string (concat "Completion Regexp: "))))))
4b1aaa8b 5929
f32b3b91
CD
5930 (if (and module (string-match "::" module))
5931 (setq class (substring module 0 (match-beginning 0))
5932 module (substring module (match-end 0))))
5933
5934 (cond
5935
5936 ((and (null arg)
5937 (eq (car-safe last-command) 'idlwave-display-completion-list)
595ab50b 5938 (get-buffer-window "*Completions*"))
f32b3b91
CD
5939 (setq this-command last-command)
5940 (idlwave-scroll-completions))
5941
52a244eb 5942 ;; Complete a filename in quotes
05a1abfc
CD
5943 ((and (idlwave-in-quote)
5944 (not (eq what 'class)))
5945 (idlwave-complete-filename))
5946
52a244eb
S
5947 ;; Check for any special completion functions
5948 ((and idlwave-complete-special
5949 (idlwave-call-special idlwave-complete-special)))
4b1aaa8b 5950
f32b3b91
CD
5951 ((null what)
5952 (error "Nothing to complete here"))
5953
52a244eb 5954 ;; Complete a class
f32b3b91 5955 ((eq what 'class)
15e42531 5956 (setq idlwave-completion-help-info '(class))
f32b3b91
CD
5957 (idlwave-complete-class))
5958
5959 ((eq what 'procedure)
5960 ;; Complete a procedure name
5e72c6b2
S
5961 (let* ((cw-list (nth 3 where-list))
5962 (class-selector (idlwave-determine-class cw-list 'pro))
5963 (super-classes (unless (idlwave-explicit-class-listed cw-list)
5964 (idlwave-all-class-inherits class-selector)))
f32b3b91
CD
5965 (isa (concat "procedure" (if class-selector "-method" "")))
5966 (type-selector 'pro))
4b1aaa8b 5967 (setq idlwave-completion-help-info
05a1abfc 5968 (list 'routine nil type-selector class-selector nil super-classes))
f32b3b91
CD
5969 (idlwave-complete-in-buffer
5970 'procedure (if class-selector 'method 'routine)
5971 (idlwave-routines) 'idlwave-selector
5972 (format "Select a %s name%s"
5973 isa
5974 (if class-selector
4b1aaa8b
PE
5975 (format " (class is %s)"
5976 (if (eq class-selector t)
76959b77 5977 "unknown" class-selector))
f32b3b91
CD
5978 ""))
5979 isa
52a244eb 5980 'idlwave-attach-method-classes 'idlwave-add-file-link-selector)))
f32b3b91
CD
5981
5982 ((eq what 'function)
5983 ;; Complete a function name
5e72c6b2
S
5984 (let* ((cw-list (nth 3 where-list))
5985 (class-selector (idlwave-determine-class cw-list 'fun))
5986 (super-classes (unless (idlwave-explicit-class-listed cw-list)
5987 (idlwave-all-class-inherits class-selector)))
f32b3b91
CD
5988 (isa (concat "function" (if class-selector "-method" "")))
5989 (type-selector 'fun))
4b1aaa8b 5990 (setq idlwave-completion-help-info
05a1abfc 5991 (list 'routine nil type-selector class-selector nil super-classes))
f32b3b91
CD
5992 (idlwave-complete-in-buffer
5993 'function (if class-selector 'method 'routine)
5994 (idlwave-routines) 'idlwave-selector
5995 (format "Select a %s name%s"
5996 isa
5997 (if class-selector
4b1aaa8b 5998 (format " (class is %s)"
76959b77
S
5999 (if (eq class-selector t)
6000 "unknown" class-selector))
f32b3b91
CD
6001 ""))
6002 isa
52a244eb 6003 'idlwave-attach-method-classes 'idlwave-add-file-link-selector)))
f32b3b91 6004
52a244eb 6005 ((and (memq what '(procedure-keyword function-keyword)) ; Special Case
5e72c6b2
S
6006 (equal arg '(4)))
6007 (idlwave-complete 3))
6008
f32b3b91
CD
6009 ((eq what 'procedure-keyword)
6010 ;; Complete a procedure keyword
6011 (let* ((where (nth 3 where-list))
6012 (name (car where))
6013 (method-selector name)
6014 (type-selector 'pro)
6015 (class (idlwave-determine-class where 'pro))
6016 (class-selector class)
05a1abfc 6017 (super-classes (idlwave-all-class-inherits class-selector))
f32b3b91 6018 (isa (format "procedure%s-keyword" (if class "-method" "")))
15e42531 6019 (entry (idlwave-best-rinfo-assq
f32b3b91 6020 name 'pro class (idlwave-routines)))
3938cb82 6021 (system (if entry (eq (car (nth 3 entry)) 'system)))
52a244eb 6022 (list (idlwave-entry-keywords entry 'do-link)))
f32b3b91
CD
6023 (unless (or entry (eq class t))
6024 (error "Nothing known about procedure %s"
6025 (idlwave-make-full-name class name)))
4b1aaa8b 6026 (setq list (idlwave-fix-keywords name 'pro class list
3938cb82 6027 super-classes system))
b6a97790 6028 (unless list (error "No keywords available for procedure %s"
3938cb82 6029 (idlwave-make-full-name class name)))
4b1aaa8b 6030 (setq idlwave-completion-help-info
52a244eb 6031 (list 'keyword name type-selector class-selector entry super-classes))
f32b3b91
CD
6032 (idlwave-complete-in-buffer
6033 'keyword 'keyword list nil
6034 (format "Select keyword for procedure %s%s"
6035 (idlwave-make-full-name class name)
15e42531 6036 (if (or (member '("_EXTRA") list)
4b1aaa8b 6037 (member '("_REF_EXTRA") list))
15e42531 6038 " (note _EXTRA)" ""))
f32b3b91
CD
6039 isa
6040 'idlwave-attach-keyword-classes)))
6041
6042 ((eq what 'function-keyword)
6043 ;; Complete a function keyword
6044 (let* ((where (nth 3 where-list))
6045 (name (car where))
6046 (method-selector name)
6047 (type-selector 'fun)
6048 (class (idlwave-determine-class where 'fun))
6049 (class-selector class)
05a1abfc 6050 (super-classes (idlwave-all-class-inherits class-selector))
f32b3b91 6051 (isa (format "function%s-keyword" (if class "-method" "")))
15e42531 6052 (entry (idlwave-best-rinfo-assq
f32b3b91 6053 name 'fun class (idlwave-routines)))
3938cb82 6054 (system (if entry (eq (car (nth 3 entry)) 'system)))
52a244eb 6055 (list (idlwave-entry-keywords entry 'do-link))
15e42531 6056 msg-name)
f32b3b91
CD
6057 (unless (or entry (eq class t))
6058 (error "Nothing known about function %s"
6059 (idlwave-make-full-name class name)))
4b1aaa8b 6060 (setq list (idlwave-fix-keywords name 'fun class list
3938cb82 6061 super-classes system))
15e42531
CD
6062 ;; OBJ_NEW: Messages mention the proper Init method
6063 (setq msg-name (if (and (null class)
6064 (string= (upcase name) "OBJ_NEW"))
6065 (concat idlwave-current-obj_new-class
6066 "::Init (via OBJ_NEW)")
6067 (idlwave-make-full-name class name)))
b6a97790 6068 (unless list (error "No keywords available for function %s"
3938cb82 6069 msg-name))
4b1aaa8b 6070 (setq idlwave-completion-help-info
05a1abfc 6071 (list 'keyword name type-selector class-selector nil super-classes))
f32b3b91
CD
6072 (idlwave-complete-in-buffer
6073 'keyword 'keyword list nil
15e42531
CD
6074 (format "Select keyword for function %s%s" msg-name
6075 (if (or (member '("_EXTRA") list)
4b1aaa8b 6076 (member '("_REF_EXTRA") list))
15e42531 6077 " (note _EXTRA)" ""))
f32b3b91
CD
6078 isa
6079 'idlwave-attach-keyword-classes)))
15e42531 6080
f32b3b91
CD
6081 (t (error "This should not happen (idlwave-complete)")))))
6082
05a1abfc
CD
6083(defvar idlwave-complete-special nil
6084 "List of special completion functions.
52a244eb
S
6085These functions are called for each completion. Each function must
6086check if its own special completion context is present. If yes, it
6087should use `idlwave-complete-in-buffer' to do some completion and
6088return t. If such a function returns t, *no further* attempts to
6089complete other contexts will be done. If the function returns nil,
6090other completions will be tried.")
76959b77
S
6091
6092(defun idlwave-call-special (functions &rest args)
6093 (let ((funcs functions)
6094 fun ret)
05a1abfc 6095 (catch 'exit
76959b77
S
6096 (while (setq fun (pop funcs))
6097 (if (setq ret (apply fun args))
6098 (throw 'exit ret)))
05a1abfc
CD
6099 nil)))
6100
f32b3b91
CD
6101(defun idlwave-make-force-complete-where-list (what &optional module class)
6102 ;; Return an artificial WHERE specification to force the completion
6103 ;; routine to complete a specific item independent of context.
6104 ;; WHAT is the prefix arg of `idlwave-complete', see there for details.
6105 ;; MODULE and CLASS can be used to specify the routine name and class.
6106 ;; The class name will also be found in MODULE if that is like "class::mod".
6107 (let* ((what-list '(("procedure") ("procedure-keyword")
6108 ("function") ("function-keyword")
6109 ("procedure-method") ("procedure-method-keyword")
6110 ("function-method") ("function-method-keyword")
6111 ("class")))
6112 (module (idlwave-sintern-routine-or-method module class))
6113 (class (idlwave-sintern-class class))
4b1aaa8b 6114 (what (cond
f32b3b91
CD
6115 ((equal what 0)
6116 (setq what
4b1aaa8b 6117 (intern (completing-read
f32b3b91
CD
6118 "Complete what? " what-list nil t))))
6119 ((integerp what)
6120 (setq what (intern (car (nth (1- what) what-list)))))
6121 ((and what
6122 (symbolp what)
6123 (assoc (symbol-name what) what-list))
6124 what)
eac9c0ef 6125 (t (error "Invalid WHAT"))))
f32b3b91
CD
6126 (nil-list '(nil nil nil nil))
6127 (class-list (list nil nil (or class t) nil)))
6128
6129 (cond
6130
6131 ((eq what 'procedure)
6132 (list nil-list nil-list 'procedure nil-list nil))
6133
6134 ((eq what 'procedure-keyword)
6135 (let* ((class-selector nil)
05a1abfc 6136 (super-classes nil)
f32b3b91
CD
6137 (type-selector 'pro)
6138 (pro (or module
4b1aaa8b 6139 (idlwave-completing-read
f32b3b91
CD
6140 "Procedure: " (idlwave-routines) 'idlwave-selector))))
6141 (setq pro (idlwave-sintern-routine pro))
6142 (list nil-list nil-list 'procedure-keyword
6143 (list pro nil nil nil) nil)))
6144
6145 ((eq what 'function)
6146 (list nil-list nil-list 'function nil-list nil))
6147
6148 ((eq what 'function-keyword)
6149 (let* ((class-selector nil)
05a1abfc 6150 (super-classes nil)
f32b3b91
CD
6151 (type-selector 'fun)
6152 (func (or module
4b1aaa8b 6153 (idlwave-completing-read
f32b3b91
CD
6154 "Function: " (idlwave-routines) 'idlwave-selector))))
6155 (setq func (idlwave-sintern-routine func))
6156 (list nil-list nil-list 'function-keyword
6157 (list func nil nil nil) nil)))
6158
6159 ((eq what 'procedure-method)
6160 (list nil-list nil-list 'procedure class-list nil))
6161
6162 ((eq what 'procedure-method-keyword)
6163 (let* ((class (idlwave-determine-class class-list 'pro))
6164 (class-selector class)
05a1abfc 6165 (super-classes (idlwave-all-class-inherits class-selector))
f32b3b91
CD
6166 (type-selector 'pro)
6167 (pro (or module
6168 (idlwave-completing-read
6169 (format "Procedure in %s class: " class-selector)
6170 (idlwave-routines) 'idlwave-selector))))
6171 (setq pro (idlwave-sintern-method pro))
6172 (list nil-list nil-list 'procedure-keyword
6173 (list pro nil class nil) nil)))
6174
6175 ((eq what 'function-method)
6176 (list nil-list nil-list 'function class-list nil))
6177
6178 ((eq what 'function-method-keyword)
6179 (let* ((class (idlwave-determine-class class-list 'fun))
6180 (class-selector class)
05a1abfc 6181 (super-classes (idlwave-all-class-inherits class-selector))
f32b3b91
CD
6182 (type-selector 'fun)
6183 (func (or module
6184 (idlwave-completing-read
6185 (format "Function in %s class: " class-selector)
6186 (idlwave-routines) 'idlwave-selector))))
6187 (setq func (idlwave-sintern-method func))
6188 (list nil-list nil-list 'function-keyword
6189 (list func nil class nil) nil)))
6190
6191 ((eq what 'class)
6192 (list nil-list nil-list 'class nil-list nil))
4b1aaa8b 6193
eac9c0ef 6194 (t (error "Invalid value for WHAT")))))
f32b3b91
CD
6195
6196(defun idlwave-completing-read (&rest args)
6197 ;; Completing read, case insensitive
6198 (let ((old-value (default-value 'completion-ignore-case)))
6199 (unwind-protect
6200 (progn
6201 (setq-default completion-ignore-case t)
6202 (apply 'completing-read args))
6203 (setq-default completion-ignore-case old-value))))
6204
05a1abfc
CD
6205(defvar idlwave-shell-default-directory)
6206(defun idlwave-complete-filename ()
6207 "Use the comint stuff to complete a file name."
6208 (require 'comint)
6209 (let* ((comint-file-name-chars "~/A-Za-z0-9+@:_.$#%={}\\-")
6210 (comint-completion-addsuffix nil)
6211 (default-directory
6212 (if (and (boundp 'idlwave-shell-default-directory)
6213 (stringp idlwave-shell-default-directory)
6214 (file-directory-p idlwave-shell-default-directory))
6215 idlwave-shell-default-directory
4b1aaa8b 6216 default-directory)))
05a1abfc
CD
6217 (comint-dynamic-complete-filename)))
6218
f32b3b91
CD
6219(defun idlwave-make-full-name (class name)
6220 ;; Make a fully qualified module name including the class name
6221 (concat (if class (format "%s::" class) "") name))
6222
15e42531
CD
6223(defun idlwave-rinfo-assoc (name type class list)
6224 "Like `idlwave-rinfo-assq', but sintern strings first."
4b1aaa8b 6225 (idlwave-rinfo-assq
15e42531
CD
6226 (idlwave-sintern-routine-or-method name class)
6227 type (idlwave-sintern-class class) list))
6228
f32b3b91
CD
6229(defun idlwave-rinfo-assq (name type class list)
6230 ;; Works like assq, but also checks type and class
6231 (catch 'exit
6232 (let (match)
6233 (while (setq match (assq name list))
6234 (and (or (eq type t)
6235 (eq (nth 1 match) type))
6236 (eq (nth 2 match) class)
6237 (throw 'exit match))
6238 (setq list (cdr (memq match list)))))))
6239
05a1abfc 6240(defun idlwave-rinfo-assq-any-class (name type class list)
52a244eb 6241 ;; Return the first matching method on the inheritance list
05a1abfc
CD
6242 (let* ((classes (cons class (idlwave-all-class-inherits class)))
6243 class rtn)
6244 (while classes
6245 (if (setq rtn (idlwave-rinfo-assq name type (pop classes) list))
6246 (setq classes nil)))
6247 rtn))
6248
4b1aaa8b 6249(defun idlwave-best-rinfo-assq (name type class list &optional with-file
52a244eb
S
6250 keep-system)
6251 "Like `idlwave-rinfo-assq', but get all twins and sort, then return first.
6252If WITH-FILE is passed, find the best rinfo entry with a file
6253included. If KEEP-SYSTEM is set, don't prune system for compiled
6254syslib files."
15e42531 6255 (let ((twins (idlwave-routine-twins
05a1abfc 6256 (idlwave-rinfo-assq-any-class name type class list)
15e42531
CD
6257 list))
6258 syslibp)
6259 (when (> (length twins) 1)
6260 (setq twins (sort twins 'idlwave-routine-entry-compare-twins))
52a244eb
S
6261 (if (and (null keep-system)
6262 (eq 'system (car (nth 3 (car twins))))
15e42531
CD
6263 (setq syslibp (idlwave-any-syslib (cdr twins)))
6264 (not (equal 1 syslibp)))
52a244eb
S
6265 ;; Its a compiled syslib, so we need to remove the system entry
6266 (setq twins (cdr twins)))
6267 (if with-file
6268 (setq twins (delq nil
6269 (mapcar (lambda (x)
6270 (if (nth 1 (nth 3 x)) x))
6271 twins)))))
15e42531
CD
6272 (car twins)))
6273
4b1aaa8b 6274(defun idlwave-best-rinfo-assoc (name type class list &optional with-file
52a244eb 6275 keep-system)
15e42531
CD
6276 "Like `idlwave-best-rinfo-assq', but sintern strings first."
6277 (idlwave-best-rinfo-assq
6278 (idlwave-sintern-routine-or-method name class)
52a244eb 6279 type (idlwave-sintern-class class) list with-file keep-system))
15e42531
CD
6280
6281(defun idlwave-any-syslib (entries)
6282 "Does the entry list ENTRIES contain a syslib entry?
6283If yes, return the index (>=1)."
6284 (let (file (cnt 0))
6285 (catch 'exit
6286 (while entries
6287 (incf cnt)
52a244eb
S
6288 (setq file (idlwave-routine-source-file (nth 3 (car entries))))
6289 (if (and file (idlwave-syslib-p file))
15e42531
CD
6290 (throw 'exit cnt)
6291 (setq entries (cdr entries))))
6292 nil)))
6293
f32b3b91
CD
6294(defun idlwave-all-assq (key list)
6295 "Return a list of all associations of Key in LIST."
6296 (let (rtn elt)
6297 (while (setq elt (assq key list))
6298 (push elt rtn)
6299 (setq list (cdr (memq elt list))))
6300 (nreverse rtn)))
6301
6302(defun idlwave-all-method-classes (method &optional type)
5a0c3f56
JB
6303 "Return all classes which have a method METHOD.
6304TYPE is 'fun or 'pro.
f32b3b91
CD
6305When TYPE is not specified, both procedures and functions will be considered."
6306 (if (null method)
15e42531 6307 (mapcar 'car (idlwave-class-alist))
f32b3b91 6308 (let (rtn)
8ffcfb27
GM
6309 (mapc (lambda (x)
6310 (and (nth 2 x)
6311 (or (not type)
6312 (eq type (nth 1 x)))
6313 (push (nth 2 x) rtn)))
6314 (idlwave-all-assq method (idlwave-routines)))
f32b3b91
CD
6315 (idlwave-uniquify rtn))))
6316
6317(defun idlwave-all-method-keyword-classes (method keyword &optional type)
6318 "Return all classes which have a method METHOD with keyword KEYWORD.
6319TYPE is 'fun or 'pro.
6320When TYPE is not specified, both procedures and functions will be considered."
6321 (if (or (null method)
6322 (null keyword))
6323 nil
6324 (let (rtn)
8ffcfb27
GM
6325 (mapc (lambda (x)
6326 (and (nth 2 x) ; non-nil class
6327 (or (not type) ; correct or unspecified type
6328 (eq type (nth 1 x)))
6329 (assoc keyword (idlwave-entry-keywords x))
6330 (push (nth 2 x) rtn)))
6331 (idlwave-all-assq method (idlwave-routines)))
f32b3b91
CD
6332 (idlwave-uniquify rtn))))
6333
05a1abfc
CD
6334(defun idlwave-members-only (list club)
6335 "Return list of all elements in LIST which are also in CLUB."
6336 (let (rtn)
6337 (while list
6338 (if (member (car list) club)
6339 (setq rtn (cons (car list) rtn)))
6340 (setq list (cdr list)))
6341 (nreverse rtn)))
6342
6343(defun idlwave-nonmembers-only (list club)
6344 "Return list of all elements in LIST which are not in CLUB."
6345 (let (rtn)
6346 (while list
6347 (if (member (car list) club)
6348 nil
6349 (setq rtn (cons (car list) rtn)))
6350 (setq list (cdr list)))
6351 (nreverse rtn)))
6352
5e72c6b2
S
6353(defun idlwave-explicit-class-listed (info)
6354 "Return whether or not the class is listed explicitly, ala a->b::c.
5a0c3f56 6355INFO is as returned by `idlwave-what-function' or `-procedure'."
5e72c6b2
S
6356 (let ((apos (nth 3 info)))
6357 (if apos
6358 (save-excursion (goto-char apos)
6359 (looking-at "->[a-zA-Z][a-zA-Z0-9$_]*::")))))
6360
76959b77
S
6361(defvar idlwave-determine-class-special nil
6362 "List of special functions for determining class.
5a0c3f56 6363Must accept two arguments: `apos' and `info'.")
76959b77 6364
f32b3b91 6365(defun idlwave-determine-class (info type)
4b1aaa8b 6366 ;; Determine the class of a routine call.
76959b77
S
6367 ;; INFO is the `cw-list' structure as returned by idlwave-where.
6368 ;; The second element in this structure is the class. When nil, we
6369 ;; return nil. When t, try to get the class from text properties at
6370 ;; the arrow. When the object is "self", we use the class of the
6371 ;; current routine. otherwise prompt the user for a class name.
6372 ;; Also stores the selected class as a text property at the arrow.
f32b3b91
CD
6373 ;; TYPE is 'fun or 'pro.
6374 (let* ((class (nth 2 info))
6375 (apos (nth 3 info))
6376 (nassoc (assoc (if (stringp (car info))
6377 (upcase (car info))
6378 (car info))
6379 idlwave-query-class))
6380 (dassoc (assq (if (car info) 'keyword-default 'method-default)
6381 idlwave-query-class))
6382 (query (cond (nassoc (cdr nassoc))
6383 (dassoc (cdr dassoc))
6384 (t t)))
6385 (arrow (and apos (string= (buffer-substring apos (+ 2 apos)) "->")))
4b1aaa8b 6386 (is-self
15e42531
CD
6387 (and arrow
6388 (save-excursion (goto-char apos)
6389 (forward-word -1)
6390 (let ((case-fold-search t))
6391 (looking-at "self\\>")))))
f32b3b91 6392 (force-query idlwave-force-class-query)
76959b77 6393 store special-class class-alist)
f32b3b91
CD
6394 (cond
6395 ((null class) nil)
6396 ((eq t class)
6397 ;; There is an object which would like to know its class
6398 (if (and arrow (get-text-property apos 'idlwave-class)
6399 idlwave-store-inquired-class
6400 (not force-query))
6401 (setq class (get-text-property apos 'idlwave-class)
6402 class (idlwave-sintern-class class)))
76959b77
S
6403 (if (and (eq t class) is-self)
6404 (setq class (or (nth 2 (idlwave-current-routine)) class)))
6405
6406 ;; Before prompting, try any special class determination routines
4b1aaa8b 6407 (when (and (eq t class)
76959b77
S
6408 idlwave-determine-class-special
6409 (not force-query))
4b1aaa8b 6410 (setq special-class
76959b77 6411 (idlwave-call-special idlwave-determine-class-special apos))
4b1aaa8b 6412 (if special-class
76959b77
S
6413 (setq class (idlwave-sintern-class special-class)
6414 store idlwave-store-inquired-class)))
4b1aaa8b 6415
76959b77 6416 ;; Prompt for a class, if we need to
f32b3b91
CD
6417 (when (and (eq class t)
6418 (or force-query query))
4b1aaa8b 6419 (setq class-alist
f32b3b91
CD
6420 (mapcar 'list (idlwave-all-method-classes (car info) type)))
6421 (setq class
6422 (idlwave-sintern-class
6423 (cond
6424 ((and (= (length class-alist) 0) (not force-query))
6425 (error "No classes available with method %s" (car info)))
6426 ((and (= (length class-alist) 1) (not force-query))
6427 (car (car class-alist)))
4b1aaa8b 6428 (t
f32b3b91 6429 (setq store idlwave-store-inquired-class)
4b1aaa8b 6430 (idlwave-completing-read
f32b3b91
CD
6431 (format "Class%s: " (if (stringp (car info))
6432 (format " for %s method %s"
6433 type (car info))
6434 ""))
6435 class-alist nil nil nil 'idlwave-class-history))))))
76959b77
S
6436
6437 ;; Store it, if requested
f32b3b91
CD
6438 (when (and class (not (eq t class)))
6439 ;; We have a real class here
6440 (when (and store arrow)
76959b77 6441 (condition-case ()
4b1aaa8b
PE
6442 (add-text-properties
6443 apos (+ apos 2)
6444 `(idlwave-class ,class face ,idlwave-class-arrow-face
76959b77
S
6445 rear-nonsticky t))
6446 (error nil)))
f32b3b91
CD
6447 (setf (nth 2 info) class))
6448 ;; Return the class
6449 class)
6450 ;; Default as fallback
6451 (t class))))
6452
f32b3b91
CD
6453(defun idlwave-selector (a)
6454 (and (eq (nth 1 a) type-selector)
6455 (or (and (nth 2 a) (eq class-selector t))
05a1abfc 6456 (eq (nth 2 a) class-selector)
52a244eb
S
6457 (memq (nth 2 a) super-classes))))
6458
6459(defun idlwave-add-file-link-selector (a)
6460 ;; Record a file link, if any, for the tested names during selection.
6461 (let ((sel (idlwave-selector a)) file)
6462 (if (and sel (setq file (idlwave-entry-has-help a)))
6463 (push (cons (car a) file) idlwave-completion-help-links))
6464 sel))
6465
f32b3b91
CD
6466
6467(defun idlwave-where ()
4b1aaa8b 6468 "Find out where we are.
f32b3b91 6469The return value is a list with the following stuff:
5e72c6b2 6470\(PRO-LIST FUNC-LIST COMPLETE-WHAT CW-LIST LAST-CHAR)
f32b3b91
CD
6471
6472PRO-LIST (PRO POINT CLASS ARROW)
6473FUNC-LIST (FUNC POINT CLASS ARROW)
6474COMPLETE-WHAT a symbol indicating what kind of completion makes sense here
4b1aaa8b 6475CW-LIST (PRO-OR-FUNC POINT CLASS ARROW) Like PRO-LIST, for what can
5e72c6b2 6476 be completed here.
f32b3b91
CD
6477LAST-CHAR last relevant character before point (non-white non-comment,
6478 not part of current identifier or leading slash).
6479
6480In the lists, we have these meanings:
6481PRO: Procedure name
6482FUNC: Function name
6483POINT: Where is this
6484CLASS: What class has the routine (nil=no, t=is method, but class unknown)
5e72c6b2 6485ARROW: Location of the arrow"
f32b3b91 6486 (idlwave-routines)
4b1aaa8b 6487 (let* (;(bos (save-excursion (idlwave-beginning-of-statement) (point)))
15e42531 6488 (bos (save-excursion (idlwave-start-of-substatement 'pre) (point)))
f32b3b91
CD
6489 (func-entry (idlwave-what-function bos))
6490 (func (car func-entry))
6491 (func-class (nth 1 func-entry))
6492 (func-arrow (nth 2 func-entry))
6493 (func-point (or (nth 3 func-entry) 0))
6494 (func-level (or (nth 4 func-entry) 0))
6495 (pro-entry (idlwave-what-procedure bos))
6496 (pro (car pro-entry))
6497 (pro-class (nth 1 pro-entry))
6498 (pro-arrow (nth 2 pro-entry))
6499 (pro-point (or (nth 3 pro-entry) 0))
6500 (last-char (idlwave-last-valid-char))
6501 (case-fold-search t)
52a244eb 6502 (match-string (buffer-substring bos (point)))
f32b3b91
CD
6503 cw cw-mod cw-arrow cw-class cw-point)
6504 (if (< func-point pro-point) (setq func nil))
6505 (cond
15e42531 6506 ((string-match "\\`[ \t]*\\(pro\\|function\\)[ \t]+[a-zA-Z0-9_]*\\'"
52a244eb 6507 match-string)
15e42531 6508 (setq cw 'class))
4b1aaa8b
PE
6509 ((string-match
6510 "\\`[ \t]*\\([a-zA-Z][a-zA-Z0-9$_]*\\)?\\'"
52a244eb
S
6511 (if (> pro-point 0)
6512 (buffer-substring pro-point (point))
6513 match-string))
f32b3b91
CD
6514 (setq cw 'procedure cw-class pro-class cw-point pro-point
6515 cw-arrow pro-arrow))
6516 ((string-match "\\`[ \t]*\\(pro\\|function\\)\\>"
52a244eb 6517 match-string)
f32b3b91 6518 nil)
05a1abfc 6519 ((string-match "OBJ_NEW([ \t]*['\"]\\([a-zA-Z0-9$_]*\\)?\\'"
52a244eb 6520 match-string)
4b1aaa8b 6521 (setq cw 'class))
05a1abfc 6522 ((string-match "\\<inherits\\s-+\\([a-zA-Z0-9$_]*\\)?\\'"
52a244eb 6523 match-string)
4b1aaa8b
PE
6524 (setq cw 'class))
6525 ((and func
f32b3b91
CD
6526 (> func-point pro-point)
6527 (= func-level 1)
6528 (memq last-char '(?\( ?,)))
6529 (setq cw 'function-keyword cw-mod func cw-point func-point
6530 cw-class func-class cw-arrow func-arrow))
6531 ((and pro (eq last-char ?,))
6532 (setq cw 'procedure-keyword cw-mod pro cw-point pro-point
6533 cw-class pro-class cw-arrow pro-arrow))
6534; ((member last-char '(?\' ?\) ?\] ?!))
6535; ;; after these chars, a function makes no sense
6536; ;; FIXME: I am sure there can be more in this list
6537; ;; FIXME: Do we want to do this at all?
6538; nil)
6539 ;; Everywhere else we try a function.
6540 (t
6541 (setq cw 'function)
6542 (save-excursion
52a244eb 6543 (if (re-search-backward "->[ \t]*\\(\\$[ \t]*\\(;.*\\)?\n\\s-*\\)?\\(\\([$a-zA-Z0-9_]+\\)::\\)?[$a-zA-Z0-9_]*\\=" bos t)
76959b77 6544 (setq cw-arrow (copy-marker (match-beginning 0))
52a244eb
S
6545 cw-class (if (match-end 4)
6546 (idlwave-sintern-class (match-string 4))
5e72c6b2 6547 t))))))
f32b3b91
CD
6548 (list (list pro pro-point pro-class pro-arrow)
6549 (list func func-point func-class func-arrow)
6550 cw
6551 (list cw-mod cw-point cw-class cw-arrow)
6552 last-char)))
6553
6554(defun idlwave-this-word (&optional class)
6555 ;; Grab the word around point. CLASS is for the `skip-chars=...' functions
52a244eb 6556 (setq class (or class "a-zA-Z0-9$_."))
f32b3b91 6557 (save-excursion
52a244eb 6558 (buffer-substring
f32b3b91
CD
6559 (progn (skip-chars-backward class) (point))
6560 (progn (skip-chars-forward class) (point)))))
6561
f32b3b91
CD
6562(defun idlwave-what-function (&optional bound)
6563 ;; Find out if point is within the argument list of a function.
76959b77
S
6564 ;; The return value is ("function-name" class arrow-start (point) level).
6565 ;; Level is 1 on the top level parentheses, higher further down.
f32b3b91
CD
6566
6567 ;; If the optional BOUND is an integer, bound backwards directed
6568 ;; searches to this point.
6569
6570 (catch 'exit
4b1aaa8b 6571 (let (pos
f32b3b91 6572 func-point
f32b3b91
CD
6573 (cnt 0)
6574 func arrow-start class)
15e42531
CD
6575 (idlwave-with-special-syntax
6576 (save-restriction
6577 (save-excursion
6578 (narrow-to-region (max 1 (or bound 0)) (point-max))
6579 ;; move back out of the current parenthesis
6580 (while (condition-case nil
6581 (progn (up-list -1) t)
6582 (error nil))
6583 (setq pos (point))
6584 (incf cnt)
6585 (when (and (= (following-char) ?\()
4b1aaa8b 6586 (re-search-backward
15e42531
CD
6587 "\\(::\\|\\<\\)\\([a-zA-Z][a-zA-Z0-9$_]*\\)[ \t]*\\="
6588 bound t))
6589 (setq func (match-string 2)
6590 func-point (goto-char (match-beginning 2))
6591 pos func-point)
4b1aaa8b 6592 (if (re-search-backward
15e42531 6593 "->[ \t]*\\(\\([a-zA-Z][a-zA-Z0-9$_]*\\)::\\)?\\=" bound t)
76959b77 6594 (setq arrow-start (copy-marker (match-beginning 0))
15e42531 6595 class (or (match-string 2) t)))
4b1aaa8b
PE
6596 (throw
6597 'exit
15e42531
CD
6598 (list
6599 (idlwave-sintern-routine-or-method func class)
6600 (idlwave-sintern-class class)
6601 arrow-start func-point cnt)))
6602 (goto-char pos))
6603 (throw 'exit nil)))))))
f32b3b91
CD
6604
6605(defun idlwave-what-procedure (&optional bound)
6606 ;; Find out if point is within the argument list of a procedure.
6607 ;; The return value is ("procedure-name" class arrow-pos (point)).
6608
6609 ;; If the optional BOUND is an integer, bound backwards directed
6610 ;; searches to this point.
6611 (let ((pos (point)) pro-point
6612 pro class arrow-start string)
4b1aaa8b 6613 (save-excursion
05a1abfc 6614 ;;(idlwave-beginning-of-statement)
15e42531 6615 (idlwave-start-of-substatement 'pre)
f32b3b91 6616 (setq string (buffer-substring (point) pos))
4b1aaa8b 6617 (if (string-match
76959b77
S
6618 "\\`[ \t]*\\([a-zA-Z][a-zA-Z0-9$_]*\\)[ \t]*\\(,\\|\\'\\)" string)
6619 (setq pro (match-string 1 string)
6620 pro-point (+ (point) (match-beginning 1)))
f32b3b91
CD
6621 (if (and (idlwave-skip-object)
6622 (setq string (buffer-substring (point) pos))
4b1aaa8b
PE
6623 (string-match
6624 "\\`[ \t]*\\(->\\)[ \t]*\\(\\([a-zA-Z][a-zA-Z0-9$_]*\\)::\\)?\\([a-zA-Z][a-zA-Z0-9$_]*\\)?[ \t]*\\(,\\|\\(\\$\\s *\\(;.*\\)?\\)?$\\)"
52a244eb 6625 string))
f32b3b91
CD
6626 (setq pro (if (match-beginning 4)
6627 (match-string 4 string))
6628 pro-point (if (match-beginning 4)
6629 (+ (point) (match-beginning 4))
6630 pos)
76959b77 6631 arrow-start (copy-marker (+ (point) (match-beginning 1)))
f32b3b91
CD
6632 class (or (match-string 3 string) t)))))
6633 (list (idlwave-sintern-routine-or-method pro class)
6634 (idlwave-sintern-class class)
6635 arrow-start
6636 pro-point)))
6637
6638(defun idlwave-skip-object ()
6639 ;; If there is an object at point, move over it and return t.
6640 (let ((pos (point)))
6641 (if (catch 'exit
6642 (save-excursion
6643 (skip-chars-forward " ") ; white space
6644 (skip-chars-forward "*") ; de-reference
6645 (cond
6646 ((looking-at idlwave-identifier)
6647 (goto-char (match-end 0)))
6648 ((eq (following-char) ?\()
6649 nil)
6650 (t (throw 'exit nil)))
6651 (catch 'endwhile
6652 (while t
6653 (cond ((eq (following-char) ?.)
6654 (forward-char 1)
6655 (if (not (looking-at idlwave-identifier))
6656 (throw 'exit nil))
6657 (goto-char (match-end 0)))
6658 ((memq (following-char) '(?\( ?\[))
6659 (condition-case nil
6660 (forward-list 1)
6661 (error (throw 'exit nil))))
6662 (t (throw 'endwhile t)))))
6663 (if (looking-at "[ \t]*->")
6664 (throw 'exit (setq pos (match-beginning 0)))
6665 (throw 'exit nil))))
6666 (goto-char pos)
6667 nil)))
4b1aaa8b 6668
f32b3b91
CD
6669(defun idlwave-last-valid-char ()
6670 "Return the last character before point which is not white or a comment
6671and also not part of the current identifier. Since we do this in
6672order to identify places where keywords are, we consider the initial
6673`/' of a keyword as part of the identifier.
6674This function is not general, can only be used for completion stuff."
6675 (catch 'exit
6676 (save-excursion
6677 ;; skip the current identifier
6678 (skip-chars-backward "a-zA-Z0-9_$")
6679 ;; also skip a leading slash which might be belong to the keyword
6680 (if (eq (preceding-char) ?/)
6681 (backward-char 1))
6682 ;; FIXME: does not check if this is a valid identifier
6683 (while t
6684 (skip-chars-backward " \t")
6685 (cond
6686 ((memq (preceding-char) '(?\; ?\$)) (throw 'exit nil))
6687 ((eq (preceding-char) ?\n)
6688 (beginning-of-line 0)
3938cb82 6689 (if (looking-at "\\([^\n]*\\)\\$[ \t]*\\(;[^\n]*\\)?\n")
f32b3b91
CD
6690 ;; continuation line
6691 (goto-char (match-end 1))
6692 (throw 'exit nil)))
6693 (t (throw 'exit (preceding-char))))))))
6694
6695(defvar idlwave-complete-after-success-form nil
6696 "A form to evaluate after successful completion.")
6697(defvar idlwave-complete-after-success-form-force nil
6698 "A form to evaluate after completion selection in *Completions* buffer.")
6699(defconst idlwave-completion-mark (make-marker)
6700 "A mark pointing to the beginning of the completion string.")
8d222148 6701(defvar completion-highlight-first-word-only) ;XEmacs.
f32b3b91
CD
6702
6703(defun idlwave-complete-in-buffer (type stype list selector prompt isa
52a244eb
S
6704 &optional prepare-display-function
6705 special-selector)
f32b3b91 6706 "Perform TYPE completion of word before point against LIST.
76959b77 6707SELECTOR is the PREDICATE argument for the completion function. Show
52a244eb 6708PROMPT in echo area. TYPE is one of the intern types, e.g. 'function,
5a0c3f56 6709'procedure, 'class-tag, 'keyword, 'sysvar, etc. SPECIAL-SELECTOR is
52a244eb
S
6710used only once, for `all-completions', and can be used to, e.g.,
6711accumulate information on matching completions."
f32b3b91
CD
6712 (let* ((completion-ignore-case t)
6713 beg (end (point)) slash part spart completion all-completions
6714 dpart dcompletion)
6715
6716 (unless list
6717 (error (concat prompt ": No completions available")))
6718
6719 ;; What is already in the buffer?
6720 (save-excursion
6721 (skip-chars-backward "a-zA-Z0-9_$")
6722 (setq slash (eq (preceding-char) ?/)
6723 beg (point)
6724 idlwave-complete-after-success-form
6725 (list 'idlwave-after-successful-completion
6726 (list 'quote type) slash beg)
6727 idlwave-complete-after-success-form-force
6728 (list 'idlwave-after-successful-completion
6729 (list 'quote type) slash (list 'quote 'force))))
6730
6731 ;; Try a completion
6732 (setq part (buffer-substring beg end)
6733 dpart (downcase part)
6734 spart (idlwave-sintern stype part)
6735 completion (try-completion part list selector)
52a244eb
S
6736 dcompletion (if (stringp completion) (downcase completion))
6737 idlwave-completion-help-links nil)
f32b3b91
CD
6738 (cond
6739 ((null completion)
6740 ;; nothing available.
76959b77 6741 (error (concat prompt ": no completion for \"%s\"") part))
f32b3b91
CD
6742 ((and (not (equal dpart dcompletion))
6743 (not (eq t completion)))
6744 ;; We can add something
6745 (delete-region beg end)
8d222148
SM
6746 (insert (if (and (string= part dpart)
6747 (or (not (string= part ""))
6748 idlwave-complete-empty-string-as-lower-case)
6749 (not idlwave-completion-force-default-case))
6750 dcompletion
6751 completion))
f32b3b91
CD
6752 (if (eq t (try-completion completion list selector))
6753 ;; Now this is a unique match
6754 (idlwave-after-successful-completion type slash beg))
6755 t)
6756 ((or (eq completion t)
52a244eb 6757 (and (= 1 (length (setq all-completions
f32b3b91 6758 (idlwave-uniquify
4b1aaa8b
PE
6759 (all-completions part list
6760 (or special-selector
52a244eb
S
6761 selector))))))
6762 (equal dpart dcompletion)))
f32b3b91
CD
6763 ;; This is already complete
6764 (idlwave-after-successful-completion type slash beg)
6765 (message "%s is already the complete %s" part isa)
6766 nil)
4b1aaa8b 6767 (t
f32b3b91
CD
6768 ;; We cannot add something - offer a list.
6769 (message "Making completion list...")
4b1aaa8b 6770
52a244eb 6771 (unless idlwave-completion-help-links ; already set somewhere?
9001c33f
GM
6772 (mapc (lambda (x) ; Pass link prop through to highlight-linked
6773 (let ((link (get-text-property 0 'link (car x))))
6774 (if link
6775 (push (cons (car x) link)
6776 idlwave-completion-help-links))))
6777 list))
f32b3b91 6778 (let* ((list all-completions)
05a1abfc 6779 ;; "complete" means, this is already a valid completion
f32b3b91 6780 (complete (memq spart all-completions))
52a244eb 6781 (completion-highlight-first-word-only t)) ; XEmacs
8d222148
SM
6782 ;; (completion-fixup-function ; Emacs
6783 ;; (lambda () (and (eq (preceding-char) ?>)
6784 ;; (re-search-backward " <" beg t)))))
4b1aaa8b 6785
f32b3b91
CD
6786 (setq list (sort list (lambda (a b)
6787 (string< (downcase a) (downcase b)))))
6788 (if prepare-display-function
6789 (setq list (funcall prepare-display-function list)))
6790 (if (and (string= part dpart)
6791 (or (not (string= part ""))
6792 idlwave-complete-empty-string-as-lower-case)
6793 (not idlwave-completion-force-default-case))
6794 (setq list (mapcar (lambda (x)
4b1aaa8b 6795 (if (listp x)
f32b3b91
CD
6796 (setcar x (downcase (car x)))
6797 (setq x (downcase x)))
6798 x)
6799 list)))
6800 (idlwave-display-completion-list list prompt beg complete))
6801 t))))
6802
6803(defun idlwave-complete-class ()
6804 "Complete a class at point."
6805 (interactive)
6806 ;; Call `idlwave-routines' to make sure the class list will be available
6807 (idlwave-routines)
15e42531
CD
6808 ;; Check for the special case of completing empty string after pro/function
6809 (if (let ((case-fold-search t))
6810 (save-excursion
6811 (and
6812 (re-search-backward "\\<\\(pro\\|function\\)[ \t]+\\="
6813 (- (point) 15) t)
6814 (goto-char (point-min))
4b1aaa8b 6815 (re-search-forward
15e42531
CD
6816 "^[ \t]*\\(pro\\|function\\)[ \t]+\\([a-zA-Z0-9_]+::\\)" nil t))))
6817 ;; Yank the full class specification
6818 (insert (match-string 2))
52a244eb 6819 ;; Do the completion, using list gathered from `idlwave-routines'
4b1aaa8b
PE
6820 (idlwave-complete-in-buffer
6821 'class 'class (idlwave-class-alist) nil
52a244eb 6822 "Select a class" "class"
8d222148
SM
6823 (lambda (list) ;; Push it to help-links if system help available
6824 (mapcar (lambda (x)
6825 (let* ((entry (idlwave-class-info x))
6826 (link (nth 1 (assq 'link entry))))
6827 (if link (push (cons x link)
6828 idlwave-completion-help-links))
6829 x))
6830 list)))))
f32b3b91 6831
76959b77 6832(defun idlwave-attach-classes (list type show-classes)
05a1abfc 6833 ;; Attach the proper class list to a LIST of completion items.
76959b77
S
6834 ;; TYPE, when 'kwd, shows classes for method keywords, when
6835 ;; 'class-tag, for class tags, and otherwise for methods.
f32b3b91 6836 ;; SHOW-CLASSES is the value of `idlwave-completion-show-classes'.
76959b77
S
6837 (if (or (null show-classes) ; don't want to see classes
6838 (null class-selector) ; not a method call
4b1aaa8b 6839 (and
76959b77
S
6840 (stringp class-selector) ; the class is already known
6841 (not super-classes))) ; no possibilities for inheritance
6842 ;; In these cases, we do not have to do anything
6843 list
05a1abfc
CD
6844 (let* ((do-prop (and (>= show-classes 0)
6845 (>= emacs-major-version 21)))
f32b3b91 6846 (do-buf (not (= show-classes 0)))
76959b77 6847 ;; (do-dots (featurep 'xemacs))
05a1abfc 6848 (do-dots t)
76959b77 6849 (inherit (if (and (not (eq type 'class-tag)) super-classes)
05a1abfc 6850 (cons class-selector super-classes)))
f32b3b91
CD
6851 (max (abs show-classes))
6852 (lmax (if do-dots (apply 'max (mapcar 'length list))))
6853 classes nclasses class-info space)
4b1aaa8b 6854 (mapcar
f32b3b91
CD
6855 (lambda (x)
6856 ;; get the classes
76959b77
S
6857 (if (eq type 'class-tag)
6858 ;; Just one class for tags
6859 (setq classes
4b1aaa8b 6860 (list
76959b77 6861 (idlwave-class-or-superclass-with-tag class-selector x)))
52a244eb 6862 ;; Multiple classes for method or method-keyword
76959b77
S
6863 (setq classes
6864 (if (eq type 'kwd)
6865 (idlwave-all-method-keyword-classes
6866 method-selector x type-selector)
6867 (idlwave-all-method-classes x type-selector)))
6868 (if inherit
4b1aaa8b 6869 (setq classes
76959b77
S
6870 (delq nil
6871 (mapcar (lambda (x) (if (memq x inherit) x nil))
6872 classes)))))
f32b3b91
CD
6873 (setq nclasses (length classes))
6874 ;; Make the separator between item and class-info
6875 (if do-dots
6876 (setq space (concat " " (make-string (- lmax (length x)) ?.)))
6877 (setq space " "))
6878 (if do-buf
6879 ;; We do want info in the buffer
6880 (if (<= nclasses max)
6881 (setq class-info (concat
6882 space
6883 "<" (mapconcat 'identity classes ",") ">"))
6884 (setq class-info (format "%s<%d classes>" space nclasses)))
6885 (setq class-info nil))
6886 (when do-prop
6887 ;; We do want properties
6888 (setq x (copy-sequence x))
6889 (put-text-property 0 (length x)
52a244eb
S
6890 'help-echo (mapconcat 'identity classes " ")
6891 x))
f32b3b91
CD
6892 (if class-info
6893 (list x class-info)
6894 x))
6895 list))))
6896
6897(defun idlwave-attach-method-classes (list)
6898 ;; Call idlwave-attach-classes with method parameters
76959b77 6899 (idlwave-attach-classes list 'method idlwave-completion-show-classes))
f32b3b91
CD
6900(defun idlwave-attach-keyword-classes (list)
6901 ;; Call idlwave-attach-classes with keyword parameters
76959b77
S
6902 (idlwave-attach-classes list 'kwd idlwave-completion-show-classes))
6903(defun idlwave-attach-class-tag-classes (list)
6904 ;; Call idlwave-attach-classes with class structure tags
6905 (idlwave-attach-classes list 'class-tag idlwave-completion-show-classes))
4b1aaa8b 6906
f32b3b91
CD
6907
6908;;----------------------------------------------------------------------
6909;;----------------------------------------------------------------------
6910;;----------------------------------------------------------------------
6911;;----------------------------------------------------------------------
6912;;----------------------------------------------------------------------
5e72c6b2
S
6913(defvar rtn)
6914(defun idlwave-pset (item)
6915 (set 'rtn item))
6916
6917(defun idlwave-popup-select (ev list title &optional sort)
6918 "Select an item in LIST with a popup menu.
6919TITLE is the title to put atop the popup. If SORT is non-nil,
5a0c3f56 6920sort the list before displaying."
5e72c6b2 6921 (let ((maxpopup idlwave-max-popup-menu-items)
8d222148 6922 rtn menu)
5e72c6b2
S
6923 (cond ((null list))
6924 ((= 1 (length list))
6925 (setq rtn (car list)))
6926 ((featurep 'xemacs)
4b1aaa8b 6927 (if sort (setq list (sort list (lambda (a b)
5e72c6b2
S
6928 (string< (upcase a) (upcase b))))))
6929 (setq menu
6930 (append (list title)
6931 (mapcar (lambda (x) (vector x (list 'idlwave-pset
6932 x)))
6933 list)))
6934 (setq menu (idlwave-split-menu-xemacs menu maxpopup))
8d222148
SM
6935 (let ((resp (get-popup-menu-response menu)))
6936 (funcall (event-function resp) (event-object resp))))
5e72c6b2 6937 (t
4b1aaa8b 6938 (if sort (setq list (sort list (lambda (a b)
5e72c6b2
S
6939 (string< (upcase a) (upcase b))))))
6940 (setq menu (cons title
6941 (list
6942 (append (list "")
6943 (mapcar (lambda(x) (cons x x)) list)))))
6944 (setq menu (idlwave-split-menu-emacs menu maxpopup))
6945 (setq rtn (x-popup-menu ev menu))))
6946 rtn))
6947
6948(defun idlwave-split-menu-xemacs (menu N)
6949 "Split the MENU into submenus of maximum length N."
6950 (if (<= (length menu) (1+ N))
6951 ;; No splitting needed
6952 menu
6953 (let* ((title (car menu))
6954 (entries (cdr menu))
6955 (menu (list title))
6956 (cnt 0)
6957 (nextmenu nil))
6958 (while entries
6959 (while (and entries (< cnt N))
6960 (setq cnt (1+ cnt)
6961 nextmenu (cons (car entries) nextmenu)
6962 entries (cdr entries)))
6963 (setq nextmenu (nreverse nextmenu))
6964 (setq nextmenu (cons (format "%s...%s"
6965 (aref (car nextmenu) 0)
6966 (aref (nth (1- cnt) nextmenu) 0))
6967 nextmenu))
6968 (setq menu (cons nextmenu menu)
6969 nextmenu nil
6970 cnt 0))
6971 (nreverse menu))))
6972
6973(defun idlwave-split-menu-emacs (menu N)
6974 "Split the MENU into submenus of maximum length N."
6975 (if (<= (length (nth 1 menu)) (1+ N))
6976 ;; No splitting needed
6977 menu
6978 (let* ((title (car menu))
6979 (entries (cdr (nth 1 menu)))
6980 (menu nil)
6981 (cnt 0)
6982 (nextmenu nil))
6983 (while entries
6984 (while (and entries (< cnt N))
6985 (setq cnt (1+ cnt)
6986 nextmenu (cons (car entries) nextmenu)
6987 entries (cdr entries)))
6988 (setq nextmenu (nreverse nextmenu))
6989 (prin1 nextmenu)
6990 (setq nextmenu (cons (format "%s...%s"
6991 (car (car nextmenu))
6992 (car (nth (1- cnt) nextmenu)))
6993 nextmenu))
6994 (setq menu (cons nextmenu menu)
6995 nextmenu nil
6996 cnt 0))
6997 (setq menu (nreverse menu))
6998 (setq menu (cons title menu))
6999 menu)))
f32b3b91 7000
15e42531
CD
7001(defvar idlwave-completion-setup-hook nil)
7002
f32b3b91
CD
7003(defun idlwave-scroll-completions (&optional message)
7004 "Scroll the completion window on this frame."
7005 (let ((cwin (get-buffer-window "*Completions*" 'visible))
7006 (win (selected-window)))
7007 (unwind-protect
7008 (progn
7009 (select-window cwin)
7010 (condition-case nil
7011 (scroll-up)
7012 (error (if (and (listp last-command)
7013 (nth 2 last-command))
7014 (progn
7015 (select-window win)
7016 (eval idlwave-complete-after-success-form))
7017 (set-window-start cwin (point-min)))))
274f1353 7018 (and message (message "%s" message)))
f32b3b91
CD
7019 (select-window win))))
7020
7021(defun idlwave-display-completion-list (list &optional message beg complete)
7022 "Display the completions in LIST in the completions buffer and echo MESSAGE."
7023 (unless (and (get-buffer-window "*Completions*")
7024 (idlwave-local-value 'idlwave-completion-p "*Completions*"))
7025 (move-marker idlwave-completion-mark beg)
7026 (setq idlwave-before-completion-wconf (current-window-configuration)))
7027
7028 (if (featurep 'xemacs)
4b1aaa8b 7029 (idlwave-display-completion-list-xemacs
15e42531 7030 list)
f32b3b91
CD
7031 (idlwave-display-completion-list-emacs list))
7032
7033 ;; Store a special value in `this-command'. When `idlwave-complete'
7034 ;; finds this in `last-command', it will scroll the *Completions* buffer.
7035 (setq this-command (list 'idlwave-display-completion-list message complete))
7036
7037 ;; Mark the completions buffer as created by cib
7038 (idlwave-set-local 'idlwave-completion-p t "*Completions*")
7039
7040 ;; Fontify the classes
7041 (if (and idlwave-completion-fontify-classes
7042 (consp (car list)))
7043 (idlwave-completion-fontify-classes))
7044
15e42531
CD
7045 ;; Run the hook
7046 (run-hooks 'idlwave-completion-setup-hook)
7047
f32b3b91 7048 ;; Display the message
274f1353 7049 (message "%s" (or message "Making completion list...done")))
f32b3b91
CD
7050
7051(defun idlwave-choose (function &rest args)
7052 "Call FUNCTION as a completion chooser and pass ARGS to it."
7053 (let ((completion-ignore-case t)) ; install correct value
7054 (apply function args))
15e42531
CD
7055 (if (and (eq major-mode 'idlwave-shell-mode)
7056 (boundp 'font-lock-mode)
7057 (not font-lock-mode))
52a244eb 7058 ;; For the shell, remove the fontification of the word before point
15e42531
CD
7059 (let ((beg (save-excursion
7060 (skip-chars-backward "a-zA-Z0-9_")
7061 (point))))
7062 (remove-text-properties beg (point) '(face nil))))
f32b3b91
CD
7063 (eval idlwave-complete-after-success-form-force))
7064
76959b77
S
7065(defun idlwave-keyboard-quit ()
7066 (interactive)
7067 (unwind-protect
7068 (if (eq (car-safe last-command) 'idlwave-display-completion-list)
7069 (idlwave-restore-wconf-after-completion))
7070 (keyboard-quit)))
7071
f32b3b91
CD
7072(defun idlwave-restore-wconf-after-completion ()
7073 "Restore the old (before completion) window configuration."
7074 (and idlwave-completion-restore-window-configuration
7075 idlwave-before-completion-wconf
7076 (set-window-configuration idlwave-before-completion-wconf)))
7077
52a244eb
S
7078(defun idlwave-one-key-select (sym prompt delay)
7079 "Make the user select an element from the alist in the variable SYM.
7080The keys of the alist are expected to be strings. The function returns the
7081car of the selected association.
d9271f41 7082To do this, PROMPT is displayed and the user must hit a letter key to
52a244eb
S
7083select an entry. If the user does not reply within DELAY seconds, a help
7084window with the options is displayed automatically.
7085The key which is associated with each option is generated automatically.
7086First, the strings are checked for preselected keys, like in \"[P]rint\".
7087If these don't exist, a letter in the string is automatically selected."
7088 (let* ((alist (symbol-value sym))
7089 (temp-buffer-show-hook (if (fboundp 'fit-window-to-buffer)
7090 '(fit-window-to-buffer)))
7091 keys-alist char)
7092 ;; First check the cache
7093 (if (and (eq (symbol-value sym) (get sym :one-key-alist-last)))
7094 (setq keys-alist (get sym :one-key-alist-cache))
7095 ;; Need to make new list
7096 (setq keys-alist (idlwave-make-one-key-alist alist))
7097 (put sym :one-key-alist-cache keys-alist)
7098 (put sym :one-key-alist-last alist))
7099 ;; Display prompt and wait for quick reply
7100 (message "%s[%s]" prompt
7101 (mapconcat (lambda(x) (char-to-string (car x)))
7102 keys-alist ""))
7103 (if (sit-for delay)
7104 ;; No quick reply: Show help
7105 (save-window-excursion
7106 (with-output-to-temp-buffer "*Completions*"
7107 (mapcar (lambda(x)
7108 (princ (nth 1 x))
7109 (princ "\n"))
4b1aaa8b 7110 keys-alist))
52a244eb
S
7111 (setq char (read-char)))
7112 (setq char (read-char)))
7113 (message nil)
7114 ;; Return the selected result
7115 (nth 2 (assoc char keys-alist))))
7116
7117;; Used for, e.g., electric debug super-examine.
7118(defun idlwave-make-one-key-alist (alist)
7119 "Make an alist for single key selection."
7120 (let ((l alist) keys-alist name start char help
7121 (cnt 0)
7122 (case-fold-search nil))
7123 (while l
7124 (setq name (car (car l))
7125 l (cdr l))
7126 (catch 'exit
7127 ;; First check if the configuration predetermined a key
7128 (if (string-match "\\[\\(.\\)\\]" name)
7129 (progn
7130 (setq char (string-to-char (downcase (match-string 1 name)))
7131 help (format "%c: %s" char name)
7132 keys-alist (cons (list char help name) keys-alist))
7133 (throw 'exit t)))
7134 ;; Then check for capital letters
7135 (setq start 0)
7136 (while (string-match "[A-Z]" name start)
7137 (setq start (match-end 0)
7138 char (string-to-char (downcase (match-string 0 name))))
7139 (if (not (assoc char keys-alist))
7140 (progn
7141 (setq help (format "%c: %s" char
7142 (replace-match
7143 (concat "[" (match-string 0 name) "]")
7144 t t name))
7145 keys-alist (cons (list char help name) keys-alist))
7146 (throw 'exit t))))
7147 ;; Now check for lowercase letters
7148 (setq start 0)
7149 (while (string-match "[a-z]" name start)
7150 (setq start (match-end 0)
7151 char (string-to-char (match-string 0 name)))
7152 (if (not (assoc char keys-alist))
7153 (progn
7154 (setq help (format "%c: %s" char
7155 (replace-match
7156 (concat "[" (match-string 0 name) "]")
7157 t t name))
7158 keys-alist (cons (list char help name) keys-alist))
7159 (throw 'exit t))))
7160 ;; Bummer, nothing found! Use a stupid number
7161 (setq char (string-to-char (int-to-string (setq cnt (1+ cnt))))
7162 help (format "%c: %s" char name)
7163 keys-alist (cons (list char help name) keys-alist))))
7164 (nreverse keys-alist)))
7165
f32b3b91
CD
7166(defun idlwave-set-local (var value &optional buffer)
7167 "Set the buffer-local value of VAR in BUFFER to VALUE."
9a529312 7168 (with-current-buffer (or buffer (current-buffer))
f32b3b91
CD
7169 (set (make-local-variable var) value)))
7170
7171(defun idlwave-local-value (var &optional buffer)
7172 "Return the value of VAR in BUFFER, but only if VAR is local to BUFFER."
9a529312 7173 (with-current-buffer (or buffer (current-buffer))
f32b3b91
CD
7174 (and (local-variable-p var (current-buffer))
7175 (symbol-value var))))
7176
15e42531
CD
7177;; In XEmacs, we can use :activate-callback directly to advice the
7178;; choose functions. We use the private keymap only for the online
7179;; help feature.
f32b3b91 7180
15e42531 7181(defvar idlwave-completion-map nil
5a0c3f56 7182 "Keymap for `completion-list-mode' with `idlwave-complete'.")
15e42531
CD
7183
7184(defun idlwave-display-completion-list-xemacs (list &rest cl-args)
f32b3b91 7185 (with-output-to-temp-buffer "*Completions*"
15e42531
CD
7186 (apply 'display-completion-list list
7187 ':activate-callback 'idlwave-default-choose-completion
7188 cl-args))
9a529312 7189 (with-current-buffer "*Completions*"
15e42531
CD
7190 (use-local-map
7191 (or idlwave-completion-map
7192 (setq idlwave-completion-map
7193 (idlwave-make-modified-completion-map-xemacs
7194 (current-local-map)))))))
f32b3b91
CD
7195
7196(defun idlwave-default-choose-completion (&rest args)
7197 "Execute `default-choose-completion' and then restore the win-conf."
7198 (apply 'idlwave-choose 'default-choose-completion args))
7199
15e42531
CD
7200(defun idlwave-make-modified-completion-map-xemacs (old-map)
7201 "Replace `choose-completion' and `mouse-choose-completion' in OLD-MAP."
7202 (let ((new-map (copy-keymap old-map)))
7203 (define-key new-map [button3up] 'idlwave-mouse-completion-help)
7204 (define-key new-map [button3] (lambda ()
7205 (interactive)
7206 (setq this-command last-command)))
7207 new-map))
f32b3b91 7208
76959b77 7209;; In Emacs we also replace keybindings in the completion
15e42531 7210;; map in order to install our wrappers.
f32b3b91
CD
7211
7212(defun idlwave-display-completion-list-emacs (list)
7213 "Display completion list and install the choose wrappers."
7214 (with-output-to-temp-buffer "*Completions*"
7215 (display-completion-list list))
9a529312 7216 (with-current-buffer "*Completions*"
f32b3b91
CD
7217 (use-local-map
7218 (or idlwave-completion-map
7219 (setq idlwave-completion-map
15e42531
CD
7220 (idlwave-make-modified-completion-map-emacs
7221 (current-local-map)))))))
7222
7223(defun idlwave-make-modified-completion-map-emacs (old-map)
f32b3b91
CD
7224 "Replace `choose-completion' and `mouse-choose-completion' in OLD-MAP."
7225 (let ((new-map (copy-keymap old-map)))
4b1aaa8b 7226 (substitute-key-definition
f32b3b91
CD
7227 'choose-completion 'idlwave-choose-completion new-map)
7228 (substitute-key-definition
7229 'mouse-choose-completion 'idlwave-mouse-choose-completion new-map)
15e42531 7230 (define-key new-map [mouse-3] 'idlwave-mouse-completion-help)
f32b3b91
CD
7231 new-map))
7232
7233(defun idlwave-choose-completion (&rest args)
7234 "Choose the completion that point is in or next to."
7235 (interactive)
7236 (apply 'idlwave-choose 'choose-completion args))
7237
7238(defun idlwave-mouse-choose-completion (&rest args)
7239 "Click on an alternative in the `*Completions*' buffer to choose it."
7240 (interactive "e")
7241 (apply 'idlwave-choose 'mouse-choose-completion args))
7242
7243;;----------------------------------------------------------------------
7244;;----------------------------------------------------------------------
7245
05a1abfc 7246;;; ------------------------------------------------------------------------
52a244eb 7247;;; Stucture parsing code, and code to manage class info
05a1abfc
CD
7248
7249;;
7250;; - Go again over the documentation how to write a completion
7251;; plugin. It is in self.el, but currently still very bad.
4b1aaa8b
PE
7252;; This could be in a separate file in the distribution, or
7253;; in an appendix for the manual.
52a244eb
S
7254
7255(defvar idlwave-struct-skip
7256 "[ \t]*\\(\\$.*\n\\(^[ \t]*\\(\\$[ \t]*\\)?\\(;.*\\)?\n\\)*\\)?[ \t]*"
5a0c3f56 7257 "Regexp for skipping continued blank or comment-only lines in structures.")
52a244eb
S
7258
7259(defvar idlwave-struct-tag-regexp
7260 (concat "[{,]" ;leading comma/brace
7261 idlwave-struct-skip ; 4 groups
7262 "\\([a-zA-Z][a-zA-Z0-9_]*\\)" ;the tag itself, group 5
7263 "[ \t]*:") ; the final colon
7264 "Regexp for structure tags.")
05a1abfc
CD
7265
7266(defun idlwave-struct-tags ()
7267 "Return a list of all tags in the structure defined at point.
7268Point is expected just before the opening `{' of the struct definition."
7269 (save-excursion
7270 (let* ((borders (idlwave-struct-borders))
7271 (beg (car borders))
7272 (end (cdr borders))
7273 tags)
7274 (goto-char beg)
52a244eb
S
7275 (save-restriction
7276 (narrow-to-region beg end)
7277 (while (re-search-forward idlwave-struct-tag-regexp end t)
7278 ;; Check if we are still on the top level of the structure.
7279 (if (and (condition-case nil (progn (up-list -1) t) (error nil))
7280 (= (point) beg))
7281 (push (match-string-no-properties 5) tags))
7282 (goto-char (match-end 0))))
7283 (nreverse tags))))
05a1abfc 7284
76959b77
S
7285(defun idlwave-find-struct-tag (tag)
7286 "Find a given TAG in the structure defined at point."
7287 (let* ((borders (idlwave-struct-borders))
76959b77
S
7288 (end (cdr borders))
7289 (case-fold-search t))
4b1aaa8b 7290 (re-search-forward (concat "\\(^[ \t]*\\|[,{][ \t]*\\)" tag "[ \t]*:")
76959b77
S
7291 end t)))
7292
05a1abfc
CD
7293(defun idlwave-struct-inherits ()
7294 "Return a list of all `inherits' names in the struct at point.
7295Point is expected just before the opening `{' of the struct definition."
7296 (save-excursion
7297 (let* ((borders (idlwave-struct-borders))
7298 (beg (car borders))
7299 (end (cdr borders))
7300 (case-fold-search t)
7301 names)
7302 (goto-char beg)
52a244eb
S
7303 (save-restriction
7304 (narrow-to-region beg end)
4b1aaa8b 7305 (while (re-search-forward
52a244eb
S
7306 (concat "[{,]" ;leading comma/brace
7307 idlwave-struct-skip ; 4 groups
7308 "inherits" ; The INHERITS tag
7309 idlwave-struct-skip ; 4 more
7310 "\\([a-zA-Z][a-zA-Z0-9_]*\\)") ; The super-group, #9
7311 end t)
7312 ;; Check if we are still on the top level of the structure.
7313 (if (and (condition-case nil (progn (up-list -1) t) (error nil))
7314 (= (point) beg))
7315 (push (match-string-no-properties 9) names))
7316 (goto-char (match-end 0))))
05a1abfc
CD
7317 (nreverse names))))
7318
5e72c6b2 7319(defun idlwave-in-structure ()
52a244eb 7320 "Return t if point is inside an IDL structure definition."
5e72c6b2
S
7321 (let ((beg (point)))
7322 (save-excursion
7323 (if (not (or (idlwave-in-comment) (idlwave-in-quote)))
7324 (if (idlwave-find-structure-definition nil nil 'back)
7325 (let ((borders (idlwave-struct-borders)))
7326 (or (= (car borders) (cdr borders)) ;; struct not yet closed...
7327 (and (> beg (car borders)) (< beg (cdr borders))))))))))
05a1abfc
CD
7328
7329(defun idlwave-struct-borders ()
7330 "Return the borders of the {...} after point as a cons cell."
7331 (let (beg)
7332 (save-excursion
7333 (skip-chars-forward "^{")
7334 (setq beg (point))
7335 (condition-case nil (forward-list 1)
7336 (error (goto-char beg)))
7337 (cons beg (point)))))
7338
7339(defun idlwave-find-structure-definition (&optional var name bound)
5a0c3f56
JB
7340 "Search forward for a structure definition.
7341If VAR is non-nil, search for a structure assigned to variable VAR.
7342If NAME is non-nil, search for a named structure NAME, if a string,
7343or a generic named structure otherwise. If BOUND is an integer, limit
7344the search. If BOUND is the symbol `all', we search first back and
7345then forward through the entire file. If BOUND is the symbol `back'
7346we search only backward."
76959b77 7347 (let* ((ws "[ \t]*\\(\\$.*\n[ \t]*\\)*")
05a1abfc
CD
7348 (case-fold-search t)
7349 (lim (if (integerp bound) bound nil))
7350 (re (concat
7351 (if var
7352 (concat "\\<" (regexp-quote (downcase var)) "\\>" ws)
7353 "\\(\\)")
7354 "=" ws "\\({\\)"
4b1aaa8b 7355 (if name
52a244eb 7356 (if (stringp name)
4b1aaa8b 7357 (concat ws "\\(\\<" (downcase name) "\\)[^a-zA-Z0-9_$]")
52a244eb
S
7358 ;; Just a generic name
7359 (concat ws "\\<\\([a-zA-Z_0-9$]+\\)" ws ","))
7360 ""))))
5e72c6b2 7361 (if (or (and (or (eq bound 'all) (eq bound 'back))
05a1abfc 7362 (re-search-backward re nil t))
5e72c6b2 7363 (and (not (eq bound 'back)) (re-search-forward re lim t)))
52a244eb
S
7364 (progn
7365 (goto-char (match-beginning 3))
7366 (match-string-no-properties 5)))))
7367
4b1aaa8b 7368(defvar idlwave-class-info nil)
52a244eb 7369(defvar idlwave-class-reset nil) ; to reset buffer-local classes
05a1abfc 7370
05a1abfc 7371(add-hook 'idlwave-update-rinfo-hook
52a244eb 7372 (lambda () (setq idlwave-class-reset t)))
05a1abfc
CD
7373(add-hook 'idlwave-after-load-rinfo-hook
7374 (lambda () (setq idlwave-class-info nil)))
7375
7376(defun idlwave-class-info (class)
7377 (let (list entry)
52a244eb
S
7378 (if idlwave-class-info
7379 (if idlwave-class-reset
4b1aaa8b 7380 (setq
52a244eb
S
7381 idlwave-class-reset nil
7382 idlwave-class-info ; Remove any visited in a buffer
4b1aaa8b
PE
7383 (delq nil (mapcar
7384 (lambda (x)
7385 (let ((filebuf
7386 (idlwave-class-file-or-buffer
52a244eb
S
7387 (or (cdr (assq 'found-in x)) (car x)))))
7388 (if (cdr filebuf)
7389 nil
7390 x)))
7391 idlwave-class-info))))
7392 ;; Info is nil, put in the system stuff to start.
05a1abfc
CD
7393 (setq idlwave-class-info idlwave-system-class-info)
7394 (setq list idlwave-class-info)
7395 (while (setq entry (pop list))
7396 (idlwave-sintern-class-info entry)))
7397 (setq class (idlwave-sintern-class class))
52a244eb
S
7398 (or (assq class idlwave-class-info)
7399 (progn (idlwave-scan-class-info class)
7400 (assq class idlwave-class-info)))))
05a1abfc
CD
7401
7402(defun idlwave-sintern-class-info (entry)
7403 "Sintern the class names in a class-info entry."
8d222148 7404 (let ((inherits (assq 'inherits entry)))
05a1abfc
CD
7405 (setcar entry (idlwave-sintern-class (car entry) 'set))
7406 (if inherits
7407 (setcdr inherits (mapcar (lambda (x) (idlwave-sintern-class x 'set))
7408 (cdr inherits))))))
7409
52a244eb 7410(defun idlwave-find-class-definition (class &optional all-hook alt-class)
5a0c3f56 7411 "Find class structure definition(s).
52a244eb
S
7412If ALL-HOOK is set, find all named structure definitions in a given
7413class__define routine, on which ALL-HOOK will be run. If ALT-CLASS is
7414set, look for the name__define pro, and inside of it, for the ALT-CLASS
5a0c3f56 7415class/struct definition."
8d222148 7416 (let ((case-fold-search t) end-lim name)
52a244eb
S
7417 (when (re-search-forward
7418 (concat "^[ \t]*pro[ \t]+" (downcase class) "__define" "\\>") nil t)
7419 (if all-hook
7420 (progn
7421 ;; For everything there
7422 (setq end-lim (save-excursion (idlwave-end-of-subprogram) (point)))
4b1aaa8b 7423 (while (setq name
52a244eb
S
7424 (idlwave-find-structure-definition nil t end-lim))
7425 (funcall all-hook name)))
7426 (idlwave-find-structure-definition nil (or alt-class class))))))
76959b77 7427
52a244eb
S
7428
7429(defun idlwave-class-file-or-buffer (class)
5a0c3f56 7430 "Find buffer visiting CLASS definition."
05a1abfc 7431 (let* ((pro (concat (downcase class) "__define"))
52a244eb
S
7432 (file (idlwave-routine-source-file
7433 (nth 3 (idlwave-rinfo-assoc pro 'pro nil
7434 (idlwave-routines))))))
7435 (cons file (if file (idlwave-get-buffer-visiting file)))))
7436
7437
7438(defun idlwave-scan-class-info (class)
5a0c3f56 7439 "Scan all class and named structure info in the class__define pro."
52a244eb
S
7440 (let* ((idlwave-auto-routine-info-updates nil)
7441 (filebuf (idlwave-class-file-or-buffer class))
7442 (file (car filebuf))
7443 (buf (cdr filebuf))
7444 (class (idlwave-sintern-class class)))
7445 (if (or
7446 (not file)
7447 (and ;; neither a regular file nor a visited buffer
7448 (not buf)
7449 (not (file-regular-p file))))
7450 nil ; Cannot find the file/buffer to get any info
05a1abfc 7451 (save-excursion
52a244eb
S
7452 (if buf (set-buffer buf)
7453 ;; Read the file in temporarily
05a1abfc
CD
7454 (set-buffer (get-buffer-create " *IDLWAVE-tmp*"))
7455 (erase-buffer)
7456 (unless (eq major-mode 'idlwave-mode)
7457 (idlwave-mode))
7458 (insert-file-contents file))
7459 (save-excursion
7460 (goto-char 1)
4b1aaa8b 7461 (idlwave-find-class-definition class
52a244eb
S
7462 ;; Scan all of the structures found there
7463 (lambda (name)
7464 (let* ((this-class (idlwave-sintern-class name))
4b1aaa8b 7465 (entry
52a244eb
S
7466 (list this-class
7467 (cons 'tags (idlwave-struct-tags))
7468 (cons 'inherits (idlwave-struct-inherits)))))
7469 (if (not (eq this-class class))
7470 (setq entry (nconc entry (list (cons 'found-in class)))))
7471 (idlwave-sintern-class-info entry)
7472 (push entry idlwave-class-info)))))))))
7473
7474(defun idlwave-class-found-in (class)
5a0c3f56 7475 "Return the FOUND-IN property of the CLASS."
52a244eb 7476 (cdr (assq 'found-in (idlwave-class-info class))))
05a1abfc
CD
7477(defun idlwave-class-tags (class)
7478 "Return the native tags in CLASS."
7479 (cdr (assq 'tags (idlwave-class-info class))))
7480(defun idlwave-class-inherits (class)
7481 "Return the direct superclasses of CLASS."
7482 (cdr (assq 'inherits (idlwave-class-info class))))
7483
52a244eb 7484
05a1abfc
CD
7485(defun idlwave-all-class-tags (class)
7486 "Return a list of native and inherited tags in CLASS."
76959b77
S
7487 (condition-case err
7488 (apply 'append (mapcar 'idlwave-class-tags
7489 (cons class (idlwave-all-class-inherits class))))
4b1aaa8b 7490 (error
76959b77
S
7491 (idlwave-class-tag-reset)
7492 (error "%s" (error-message-string err)))))
7493
05a1abfc
CD
7494
7495(defun idlwave-all-class-inherits (class)
7496 "Return a list of all superclasses of CLASS (recursively expanded).
5e72c6b2 7497The list is cached in `idlwave-class-info' for faster access."
05a1abfc
CD
7498 (cond
7499 ((not idlwave-support-inheritance) nil)
7500 ((eq class nil) nil)
7501 ((eq class t) nil)
7502 (t
7503 (let ((info (idlwave-class-info class))
7504 entry)
7505 (if (setq entry (assq 'all-inherits info))
7506 (cdr entry)
76959b77
S
7507 ;; Save the depth of inheritance scan to check for circular references
7508 (let ((inherits (mapcar (lambda (x) (cons x 0))
7509 (idlwave-class-inherits class)))
05a1abfc
CD
7510 rtn all-inherits cl)
7511 (while inherits
7512 (setq cl (pop inherits)
76959b77
S
7513 rtn (cons (car cl) rtn)
7514 inherits (append (mapcar (lambda (x)
7515 (cons x (1+ (cdr cl))))
7516 (idlwave-class-inherits (car cl)))
7517 inherits))
7518 (if (> (cdr cl) 999)
7519 (error
7520 "Class scan: inheritance depth exceeded. Circular inheritance?")
7521 ))
05a1abfc
CD
7522 (setq all-inherits (nreverse rtn))
7523 (nconc info (list (cons 'all-inherits all-inherits)))
7524 all-inherits))))))
7525
52a244eb 7526(defun idlwave-entry-keywords (entry &optional record-link)
4b1aaa8b 7527 "Return the flat entry keywords alist from routine-info entry.
52a244eb
S
7528If RECORD-LINK is non-nil, the keyword text is copied and a text
7529property indicating the link is added."
7530 (let (kwds)
8ffcfb27 7531 (mapc
4b1aaa8b 7532 (lambda (key-list)
52a244eb
S
7533 (let ((file (car key-list)))
7534 (mapcar (lambda (key-cons)
7535 (let ((key (car key-cons))
7536 (link (cdr key-cons)))
7537 (when (and record-link file)
7538 (setq key (copy-sequence key))
4b1aaa8b 7539 (put-text-property
52a244eb 7540 0 (length key)
4b1aaa8b
PE
7541 'link
7542 (concat
7543 file
7544 (if link
52a244eb
S
7545 (concat idlwave-html-link-sep
7546 (number-to-string link))))
7547 key))
7548 (push (list key) kwds)))
7549 (cdr key-list))))
7550 (nthcdr 5 entry))
7551 (nreverse kwds)))
7552
7553(defun idlwave-entry-find-keyword (entry keyword)
5a0c3f56 7554 "Find keyword KEYWORD in entry ENTRY, and return (with link) if set."
52a244eb
S
7555 (catch 'exit
7556 (mapc
4b1aaa8b 7557 (lambda (key-list)
52a244eb
S
7558 (let ((file (car key-list))
7559 (kwd (assoc keyword (cdr key-list))))
7560 (when kwd
4b1aaa8b 7561 (setq kwd (cons (car kwd)
52a244eb 7562 (if (and file (cdr kwd))
4b1aaa8b 7563 (concat file
52a244eb
S
7564 idlwave-html-link-sep
7565 (number-to-string (cdr kwd)))
7566 (cdr kwd))))
7567 (throw 'exit kwd))))
7568 (nthcdr 5 entry))))
05a1abfc
CD
7569
7570;;==========================================================================
7571;;
7572;; Completing class structure tags. This is a completion plugin.
7573;; The necessary taglist is constructed dynamically
7574
7575(defvar idlwave-current-tags-class nil)
7576(defvar idlwave-current-class-tags nil)
7577(defvar idlwave-current-native-class-tags nil)
76959b77 7578(defvar idlwave-sint-class-tags nil)
1a717047 7579(declare-function idlwave-sintern-class-tag "idlwave" t t)
76959b77 7580(idlwave-new-sintern-type 'class-tag)
05a1abfc 7581(add-to-list 'idlwave-complete-special 'idlwave-complete-class-structure-tag)
76959b77 7582(add-hook 'idlwave-update-rinfo-hook 'idlwave-class-tag-reset)
05a1abfc
CD
7583
7584(defun idlwave-complete-class-structure-tag ()
7585 "Complete a structure tag on a `self' argument in an object method."
7586 (interactive)
7587 (let ((pos (point))
7588 (case-fold-search t))
7589 (if (save-excursion
7590 ;; Check if the context is right
52a244eb 7591 (skip-chars-backward "a-zA-Z0-9._$")
05a1abfc
CD
7592 (and (< (point) (- pos 4))
7593 (looking-at "self\\.")))
76959b77
S
7594 (let* ((class-selector (nth 2 (idlwave-current-routine)))
7595 (super-classes (idlwave-all-class-inherits class-selector)))
05a1abfc 7596 ;; Check if we are in a class routine
76959b77 7597 (unless class-selector
e8af40ee 7598 (error "Not in a method procedure or function"))
05a1abfc 7599 ;; Check if we need to update the "current" class
76959b77
S
7600 (if (not (equal class-selector idlwave-current-tags-class))
7601 (idlwave-prepare-class-tag-completion class-selector))
4b1aaa8b 7602 (setq idlwave-completion-help-info
76959b77 7603 (list 'idlwave-complete-class-structure-tag-help
4b1aaa8b 7604 (idlwave-sintern-routine
76959b77
S
7605 (concat class-selector "__define"))
7606 nil))
8d222148 7607 ;; FIXME: idlwave-cpl-bold doesn't seem used anywhere.
05a1abfc
CD
7608 (let ((idlwave-cpl-bold idlwave-current-native-class-tags))
7609 (idlwave-complete-in-buffer
4b1aaa8b 7610 'class-tag 'class-tag
05a1abfc 7611 idlwave-current-class-tags nil
76959b77
S
7612 (format "Select a tag of class %s" class-selector)
7613 "class tag"
7614 'idlwave-attach-class-tag-classes))
05a1abfc
CD
7615 t) ; return t to skip other completions
7616 nil)))
7617
76959b77 7618(defun idlwave-class-tag-reset ()
05a1abfc
CD
7619 (setq idlwave-current-tags-class nil))
7620
7621(defun idlwave-prepare-class-tag-completion (class)
7622 "Find and parse the necessary class definitions for class structure tags."
76959b77 7623 (setq idlwave-sint-class-tags nil)
05a1abfc
CD
7624 (setq idlwave-current-tags-class class)
7625 (setq idlwave-current-class-tags
7626 (mapcar (lambda (x)
76959b77 7627 (list (idlwave-sintern-class-tag x 'set)))
05a1abfc
CD
7628 (idlwave-all-class-tags class)))
7629 (setq idlwave-current-native-class-tags
7630 (mapcar 'downcase (idlwave-class-tags class))))
7631
7632;===========================================================================
7633;;
7634;; Completing system variables and their structure fields
52a244eb 7635;; This is also a plugin.
05a1abfc
CD
7636
7637(defvar idlwave-sint-sysvars nil)
7638(defvar idlwave-sint-sysvartags nil)
1a717047
GM
7639(declare-function idlwave-sintern-sysvar "idlwave" t t)
7640(declare-function idlwave-sintern-sysvartag "idlwave" t t)
05a1abfc
CD
7641(idlwave-new-sintern-type 'sysvar)
7642(idlwave-new-sintern-type 'sysvartag)
7643(add-to-list 'idlwave-complete-special 'idlwave-complete-sysvar-or-tag)
7644(add-hook 'idlwave-update-rinfo-hook 'idlwave-sysvars-reset)
05a1abfc
CD
7645(add-hook 'idlwave-after-load-rinfo-hook 'idlwave-sintern-sysvar-alist)
7646
05a1abfc
CD
7647
7648(defun idlwave-complete-sysvar-or-tag ()
7649 "Complete a system variable."
7650 (interactive)
7651 (let ((pos (point))
7652 (case-fold-search t))
7653 (cond ((save-excursion
7654 ;; Check if the context is right for system variable
7655 (skip-chars-backward "[a-zA-Z0-9_$]")
7656 (equal (char-before) ?!))
7657 (setq idlwave-completion-help-info '(idlwave-complete-sysvar-help))
4b1aaa8b 7658 (idlwave-complete-in-buffer 'sysvar 'sysvar
05a1abfc
CD
7659 idlwave-system-variables-alist nil
7660 "Select a system variable"
7661 "system variable")
7662 t) ; return t to skip other completions
7663 ((save-excursion
7664 ;; Check if the context is right for sysvar tag
52a244eb 7665 (skip-chars-backward "a-zA-Z0-9_$.")
05a1abfc
CD
7666 (and (equal (char-before) ?!)
7667 (looking-at "\\([a-zA-Z][a-zA-Z0-9_$]*\\)\\.")
7668 (<= (match-end 0) pos)))
7669 ;; Complete a system variable tag
7670 (let* ((var (idlwave-sintern-sysvar (match-string 1)))
7671 (entry (assq var idlwave-system-variables-alist))
52a244eb
S
7672 (tags (cdr (assq 'tags entry))))
7673 (or entry (error "!%s is not a known system variable" var))
05a1abfc
CD
7674 (or tags (error "System variable !%s is not a structure" var))
7675 (setq idlwave-completion-help-info
52a244eb 7676 (list 'idlwave-complete-sysvar-tag-help var))
4b1aaa8b 7677 (idlwave-complete-in-buffer 'sysvartag 'sysvartag
05a1abfc
CD
7678 tags nil
7679 "Select a system variable tag"
7680 "system variable tag")
7681 t)) ; return t to skip other completions
7682 (t nil))))
7683
f66f03de
S
7684(defvar link) ;dynamic variables set by help callback
7685(defvar props)
05a1abfc 7686(defun idlwave-complete-sysvar-help (mode word)
52a244eb
S
7687 (let ((word (or (nth 1 idlwave-completion-help-info) word))
7688 (entry (assoc word idlwave-system-variables-alist)))
7689 (cond
7690 ((eq mode 'test)
7691 (and (stringp word) entry (nth 1 (assq 'link entry))))
7692 ((eq mode 'set)
7693 (if entry (setq link (nth 1 (assq 'link entry))))) ;; setting dynamic!!!
7694 (t (error "This should not happen")))))
7695
7696(defun idlwave-complete-sysvar-tag-help (mode word)
7697 (let* ((var (nth 1 idlwave-completion-help-info))
7698 (entry (assoc var idlwave-system-variables-alist))
7699 (tags (cdr (assq 'tags entry)))
7700 (main (nth 1 (assq 'link entry)))
8d222148 7701 target)
52a244eb
S
7702 (cond
7703 ((eq mode 'test) ; we can at least link the main
7704 (and (stringp word) entry main))
7705 ((eq mode 'set)
4b1aaa8b
PE
7706 (if entry
7707 (setq link
e08734e2 7708 (if (setq target (cdr (assoc-string word tags t)))
52a244eb
S
7709 (idlwave-substitute-link-target main target)
7710 main)))) ;; setting dynamic!!!
7711 (t (error "This should not happen")))))
7712
f66f03de 7713(defun idlwave-split-link-target (link)
5a0c3f56 7714 "Split a given LINK into link file and anchor."
f66f03de
S
7715 (if (string-match idlwave-html-link-sep link)
7716 (cons (substring link 0 (match-beginning 0))
7717 (string-to-number (substring link (match-end 0))))))
7718
52a244eb 7719(defun idlwave-substitute-link-target (link target)
5a0c3f56 7720 "Substitute the TARGET anchor for the given LINK."
52a244eb
S
7721 (let (main-base)
7722 (setq main-base (if (string-match "#" link)
7723 (substring link 0 (match-beginning 0))
7724 link))
7725 (if target
7726 (concat main-base idlwave-html-link-sep (number-to-string target))
7727 link)))
76959b77
S
7728
7729;; Fake help in the source buffer for class structure tags.
52a244eb 7730;; KWD AND NAME ARE GLOBAL-VARIABLES HERE.
4b1aaa8b 7731(defvar name)
52a244eb 7732(defvar kwd)
76959b77
S
7733(defvar idlwave-help-do-class-struct-tag nil)
7734(defun idlwave-complete-class-structure-tag-help (mode word)
7735 (cond
7736 ((eq mode 'test) ; nothing gets fontified for class tags
7737 nil)
7738 ((eq mode 'set)
52a244eb 7739 (let (class-with found-in)
4b1aaa8b
PE
7740 (when (setq class-with
7741 (idlwave-class-or-superclass-with-tag
76959b77
S
7742 idlwave-current-tags-class
7743 word))
4b1aaa8b 7744 (if (assq (idlwave-sintern-class class-with)
76959b77 7745 idlwave-system-class-info)
ff689efd 7746 (error "No help available for system class tags"))
52a244eb
S
7747 (if (setq found-in (idlwave-class-found-in class-with))
7748 (setq name (cons (concat found-in "__define") class-with))
7749 (setq name (concat class-with "__define")))))
76959b77
S
7750 (setq kwd word
7751 idlwave-help-do-class-struct-tag t))
7752 (t (error "This should not happen"))))
7753
7754(defun idlwave-class-or-superclass-with-tag (class tag)
7755 "Find and return the CLASS or one of its superclass with the
7756associated TAG, if any."
e08734e2 7757 (let ((sclasses (cons class (idlwave-all-class-inherits class)))
76959b77
S
7758 cl)
7759 (catch 'exit
7760 (while sclasses
7761 (setq cl (pop sclasses))
7762 (let ((tags (idlwave-class-tags cl)))
7763 (while tags
7764 (if (eq t (compare-strings tag 0 nil (car tags) 0 nil t))
4b1aaa8b 7765 (throw 'exit cl))
76959b77
S
7766 (setq tags (cdr tags))))))))
7767
05a1abfc
CD
7768
7769(defun idlwave-sysvars-reset ()
7770 (if (and (fboundp 'idlwave-shell-is-running)
52a244eb
S
7771 (idlwave-shell-is-running)
7772 idlwave-idlwave_routine_info-compiled)
05a1abfc
CD
7773 (idlwave-shell-send-command "idlwave_get_sysvars"
7774 'idlwave-process-sysvars 'hide)))
7775
7776(defun idlwave-process-sysvars ()
7777 (idlwave-shell-filter-sysvars)
7778 (setq idlwave-sint-sysvars nil
7779 idlwave-sint-sysvartags nil)
7780 (idlwave-sintern-sysvar-alist))
7781
05a1abfc 7782(defun idlwave-sintern-sysvar-alist ()
52a244eb 7783 (let ((list idlwave-system-variables-alist) entry tags)
05a1abfc
CD
7784 (while (setq entry (pop list))
7785 (setcar entry (idlwave-sintern-sysvar (car entry) 'set))
52a244eb
S
7786 (setq tags (assq 'tags entry))
7787 (if tags
4b1aaa8b
PE
7788 (setcdr tags
7789 (mapcar (lambda (x)
52a244eb
S
7790 (cons (idlwave-sintern-sysvartag (car x) 'set)
7791 (cdr x)))
7792 (cdr tags)))))))
05a1abfc
CD
7793
7794(defvar idlwave-shell-command-output)
7795(defun idlwave-shell-filter-sysvars ()
52a244eb 7796 "Get any new system variables and tags."
05a1abfc
CD
7797 (let ((text idlwave-shell-command-output)
7798 (start 0)
7799 (old idlwave-system-variables-alist)
52a244eb 7800 var tags type name class link old-entry)
05a1abfc
CD
7801 (setq idlwave-system-variables-alist nil)
7802 (while (string-match "^IDLWAVE-SYSVAR: !\\([a-zA-Z0-9_$]+\\)\\( \\(.*\\)\\)?"
7803 text start)
7804 (setq start (match-end 0)
7805 var (match-string 1 text)
4b1aaa8b 7806 tags (if (match-end 3)
52a244eb
S
7807 (idlwave-split-string (match-string 3 text))))
7808 ;; Maintain old links, if present
7809 (setq old-entry (assq (idlwave-sintern-sysvar var) old))
7810 (setq link (assq 'link old-entry))
05a1abfc 7811 (setq idlwave-system-variables-alist
4b1aaa8b
PE
7812 (cons (list var
7813 (cons
7814 'tags
7815 (mapcar (lambda (x)
7816 (cons x
7817 (cdr (assq
7818 (idlwave-sintern-sysvartag x)
52a244eb
S
7819 (cdr (assq 'tags old-entry))))))
7820 tags)) link)
05a1abfc
CD
7821 idlwave-system-variables-alist)))
7822 ;; Keep the old value if query was not successful
7823 (setq idlwave-system-variables-alist
7824 (or idlwave-system-variables-alist old))))
7825
f32b3b91
CD
7826(defun idlwave-completion-fontify-classes ()
7827 "Goto the *Completions* buffer and fontify the class info."
7828 (when (featurep 'font-lock)
9a529312 7829 (with-current-buffer "*Completions*"
f32b3b91
CD
7830 (save-excursion
7831 (goto-char (point-min))
76959b77
S
7832 (let ((buffer-read-only nil))
7833 (while (re-search-forward "\\.*<[^>]+>" nil t)
7834 (put-text-property (match-beginning 0) (match-end 0)
7835 'face 'font-lock-string-face)))))))
f32b3b91
CD
7836
7837(defun idlwave-uniquify (list)
52a244eb 7838 (let ((ht (make-hash-table :size (length list) :test 'equal)))
4b1aaa8b 7839 (delq nil
52a244eb 7840 (mapcar (lambda (x)
4b1aaa8b 7841 (unless (gethash x ht)
52a244eb
S
7842 (puthash x t ht)
7843 x))
7844 list))))
f32b3b91
CD
7845
7846(defun idlwave-after-successful-completion (type slash &optional verify)
7847 "Add `=' or `(' after successful completion of keyword and function.
7848Restore the pre-completion window configuration if possible."
7849 (cond
7850 ((eq type 'procedure)
7851 nil)
7852 ((eq type 'function)
7853 (cond
7854 ((equal idlwave-function-completion-adds-paren nil) nil)
7855 ((or (equal idlwave-function-completion-adds-paren t)
7856 (equal idlwave-function-completion-adds-paren 1))
7857 (insert "("))
7858 ((equal idlwave-function-completion-adds-paren 2)
7859 (insert "()")
7860 (backward-char 1))
7861 (t nil)))
7862 ((eq type 'keyword)
7863 (if (and idlwave-keyword-completion-adds-equal
7864 (not slash))
7865 (progn (insert "=") t)
7866 nil)))
7867
7868 ;; Restore the pre-completion window configuration if this is safe.
4b1aaa8b
PE
7869
7870 (if (or (eq verify 'force) ; force
7871 (and
f32b3b91 7872 (get-buffer-window "*Completions*") ; visible
4b1aaa8b 7873 (idlwave-local-value 'idlwave-completion-p
f32b3b91
CD
7874 "*Completions*") ; cib-buffer
7875 (eq (marker-buffer idlwave-completion-mark)
7876 (current-buffer)) ; buffer OK
7877 (equal (marker-position idlwave-completion-mark)
7878 verify))) ; pos OK
7879 (idlwave-restore-wconf-after-completion))
7880 (move-marker idlwave-completion-mark nil)
7881 (setq idlwave-before-completion-wconf nil))
7882
15e42531
CD
7883(defun idlwave-mouse-context-help (ev &optional arg)
7884 "Call `idlwave-context-help' on the clicked location."
7885 (interactive "eP")
7886 (mouse-set-point ev)
7887 (idlwave-context-help arg))
7888
7889(defvar idlwave-last-context-help-pos nil)
7890(defun idlwave-context-help (&optional arg)
7891 "Display IDL Online Help on context.
76959b77
S
7892If point is on a keyword, help for that keyword will be shown. If
7893point is on a routine name or in the argument list of a routine, help
7894for that routine will be displayed. Works for system routines and
7895keywords, it pulls up text help. For other routies and keywords,
7896visits the source file, finding help in the header (if
7897`idlwave-help-source-try-header' is non-nil) or the routine definition
7898itself."
f32b3b91 7899 (interactive "P")
15e42531
CD
7900 (idlwave-do-context-help arg))
7901
7902(defun idlwave-mouse-completion-help (ev)
7903 "Display online help about the completion at point."
7904 (interactive "eP")
52a244eb
S
7905 ;; Restore last-command for next command, to make
7906 ;; scrolling/cancelling of completions work.
15e42531
CD
7907 (setq this-command last-command)
7908 (idlwave-do-mouse-completion-help ev))
15e42531 7909
f32b3b91 7910(defun idlwave-routine-info (&optional arg external)
5a0c3f56
JB
7911 "Display a routines calling sequence and list of keywords.
7912When point is on the name a function or procedure, or in the argument
7913list of a function or procedure, this command displays a help buffer with
52a244eb 7914the information. When called with prefix arg, enforce class query.
f32b3b91
CD
7915
7916When point is on an object operator `->', display the class stored in
5a0c3f56
JB
7917this arrow, if any (see `idlwave-store-inquired-class'). With a prefix
7918arg, the class property is cleared out."
f32b3b91
CD
7919
7920 (interactive "P")
7921 (idlwave-routines)
7922 (if (string-match "->" (buffer-substring
7923 (max (point-min) (1- (point)))
7924 (min (+ 2 (point)) (point-max))))
7925 ;; Cursor is on an arrow
7926 (if (get-text-property (point) 'idlwave-class)
7927 ;; arrow has class property
7928 (if arg
7929 ;; Remove property
7930 (save-excursion
7931 (backward-char 1)
7932 (when (looking-at ".?\\(->\\)")
7933 (remove-text-properties (match-beginning 1) (match-end 1)
7934 '(idlwave-class nil face nil))
7935 (message "Class property removed from arrow")))
7936 ;; Echo class property
7937 (message "Arrow has text property identifying object to be class %s"
7938 (get-text-property (point) 'idlwave-class)))
7939 ;; No property found
7940 (message "Arrow has no class text property"))
7941
7942 ;; Not on an arrow...
7943 (let* ((idlwave-query-class nil)
7944 (idlwave-force-class-query (equal arg '(4)))
7945 (module (idlwave-what-module)))
15e42531 7946 (if (car module)
05a1abfc
CD
7947 (apply 'idlwave-display-calling-sequence
7948 (idlwave-fix-module-if-obj_new module))
e8af40ee 7949 (error "Don't know which calling sequence to show")))))
f32b3b91
CD
7950
7951(defun idlwave-resolve (&optional arg)
52a244eb 7952 "Call RESOLVE_ROUTINE on the module name at point.
f32b3b91
CD
7953Like `idlwave-routine-info', this looks for a routine call at point.
7954After confirmation in the minibuffer, it will use the shell to issue
7955a RESOLVE call for this routine, to attempt to make it defined and its
7956routine info available for IDLWAVE. If the routine is a method call,
7957both `class__method' and `class__define' will be tried.
7958With ARG, enforce query for the class of object methods."
7959 (interactive "P")
7960 (let* ((idlwave-query-class nil)
7961 (idlwave-force-class-query (equal arg '(4)))
7962 (module (idlwave-what-module))
7963 (name (idlwave-make-full-name (nth 2 module) (car module)))
7964 (type (if (eq (nth 1 module) 'pro) "pro" "function"))
7965 (resolve (read-string "Resolve: " (format "%s %s" type name)))
7966 (kwd "")
7967 class)
7968 (if (string-match "\\(pro\\|function\\)[ \t]+\\(\\(.*\\)::\\)?\\(.*\\)"
7969 resolve)
7970 (setq type (match-string 1 resolve)
4b1aaa8b 7971 class (if (match-beginning 2)
f32b3b91
CD
7972 (match-string 3 resolve)
7973 nil)
7974 name (match-string 4 resolve)))
7975 (if (string= (downcase type) "function")
7976 (setq kwd ",/is_function"))
7977
7978 (cond
7979 ((null class)
4b1aaa8b 7980 (idlwave-shell-send-command
f32b3b91
CD
7981 (format "resolve_routine,'%s'%s" (downcase name) kwd)
7982 'idlwave-update-routine-info
7983 nil t))
7984 (t
4b1aaa8b 7985 (idlwave-shell-send-command
f32b3b91 7986 (format "resolve_routine,'%s__define'%s" (downcase class) kwd)
4b1aaa8b
PE
7987 (list 'idlwave-shell-send-command
7988 (format "resolve_routine,'%s__%s'%s"
f32b3b91
CD
7989 (downcase class) (downcase name) kwd)
7990 '(idlwave-update-routine-info)
7991 nil t))))))
7992
3938cb82
S
7993(defun idlwave-find-module-this-file ()
7994 (interactive)
7995 (idlwave-find-module '(4)))
7996
f32b3b91
CD
7997(defun idlwave-find-module (&optional arg)
7998 "Find the source code of an IDL module.
5a0c3f56
JB
7999Works for modules for which IDLWAVE has routine info available.
8000The function offers as default the module name `idlwave-routine-info'
52a244eb
S
8001would use. With ARG limit to this buffer. With two prefix ARG's
8002force class query for object methods."
f32b3b91
CD
8003 (interactive "P")
8004 (let* ((idlwave-query-class nil)
52a244eb
S
8005 (idlwave-force-class-query (equal arg '(16)))
8006 (this-buffer (equal arg '(4)))
05a1abfc 8007 (module (idlwave-fix-module-if-obj_new (idlwave-what-module)))
52a244eb 8008 (default (if module
4b1aaa8b 8009 (concat (idlwave-make-full-name
52a244eb
S
8010 (nth 2 module) (car module))
8011 (if (eq (nth 1 module) 'pro) "<p>" "<f>"))
8012 "none"))
4b1aaa8b 8013 (list
52a244eb
S
8014 (idlwave-uniquify
8015 (delq nil
4b1aaa8b 8016 (mapcar (lambda (x)
52a244eb
S
8017 (if (eq 'system (car-safe (nth 3 x)))
8018 ;; Take out system routines with no source.
8019 nil
8020 (list
4b1aaa8b 8021 (concat (idlwave-make-full-name
52a244eb
S
8022 (nth 2 x) (car x))
8023 (if (eq (nth 1 x) 'pro) "<p>" "<f>")))))
8024 (if this-buffer
8025 (idlwave-save-buffer-update)
8026 (idlwave-routines))))))
f32b3b91 8027 (name (idlwave-completing-read
52a244eb
S
8028 (if (or (not this-buffer)
8029 (assoc default list))
8030 (format "Module (Default %s): " default)
8031 (format "Module in this file: "))
f32b3b91
CD
8032 list))
8033 type class)
8034 (if (string-match "\\`\\s-*\\'" name)
8035 ;; Nothing, use the default.
8036 (setq name default))
8037 (if (string-match "<[fp]>" name)
8038 (setq type (substring name -2 -1)
8039 name (substring name 0 -3)))
8040 (if (string-match "\\(.*\\)::\\(.*\\)" name)
8041 (setq class (match-string 1 name)
8042 name (match-string 2 name)))
8043 (setq name (idlwave-sintern-routine-or-method name class)
8044 class (idlwave-sintern-class class)
8045 type (cond ((equal type "f") 'fun)
8046 ((equal type "p") 'pro)
8047 (t t)))
52a244eb 8048 (idlwave-do-find-module name type class nil this-buffer)))
f32b3b91 8049
4b1aaa8b 8050(defun idlwave-do-find-module (name type class
52a244eb 8051 &optional force-source this-buffer)
f32b3b91 8052 (let ((name1 (idlwave-make-full-name class name))
4b1aaa8b 8053 source buf1 entry
f32b3b91 8054 (buf (current-buffer))
05a1abfc 8055 (pos (point))
52a244eb
S
8056 file name2)
8057 (setq entry (idlwave-best-rinfo-assq name type class (idlwave-routines)
8058 'WITH-FILE)
05a1abfc
CD
8059 source (or force-source (nth 3 entry))
8060 name2 (if (nth 2 entry)
8061 (idlwave-make-full-name (nth 2 entry) name)
775591f7 8062 name1))
4b1aaa8b 8063 (if source
52a244eb
S
8064 (setq file (idlwave-routine-source-file source)))
8065 (unless file ; Try to find it on the path.
4b1aaa8b
PE
8066 (setq file
8067 (idlwave-expand-lib-file-name
52a244eb
S
8068 (if class
8069 (format "%s__define.pro" (downcase class))
8070 (format "%s.pro" (downcase name))))))
f32b3b91
CD
8071 (cond
8072 ((or (null name) (equal name ""))
8073 (error "Abort"))
f32b3b91 8074 ((eq (car source) 'system)
4b1aaa8b 8075 (error "Source code for system routine %s is not available"
05a1abfc 8076 name2))
52a244eb 8077 ((or (not file) (not (file-regular-p file)))
e8af40ee 8078 (error "Source code for routine %s is not available"
05a1abfc 8079 name2))
52a244eb
S
8080 (t
8081 (when (not this-buffer)
4b1aaa8b 8082 (setq buf1
52a244eb
S
8083 (idlwave-find-file-noselect file 'find))
8084 (pop-to-buffer buf1 t))
15e42531 8085 (goto-char (point-max))
f32b3b91 8086 (let ((case-fold-search t))
15e42531 8087 (if (re-search-backward
f32b3b91 8088 (concat "^[ \t]*\\<"
52a244eb
S
8089 (cond ((eq type 'fun) "function")
8090 ((eq type 'pro) "pro")
f32b3b91 8091 (t "\\(pro\\|function\\)"))
4b1aaa8b 8092 "\\>[ \t]+"
05a1abfc 8093 (regexp-quote (downcase name2))
f32b3b91
CD
8094 "[^a-zA-Z0-9_$]")
8095 nil t)
8096 (goto-char (match-beginning 0))
8097 (pop-to-buffer buf)
8098 (goto-char pos)
05a1abfc 8099 (error "Could not find routine %s" name2)))))))
f32b3b91
CD
8100
8101(defun idlwave-what-module ()
8102 "Return a default module for stuff near point.
8103Used by `idlwave-routine-info' and `idlwave-find-module'."
8104 (idlwave-routines)
15e42531
CD
8105 (if (let ((case-fold-search t))
8106 (save-excursion
8107 (idlwave-beginning-of-statement)
8108 (looking-at "[ \t]*\\(pro\\|function\\)[ \t]+\\(\\([a-zA-Z0-9_$]+\\)::\\)?\\([a-zA-Z0-9$_]+\\)\\([, \t\n]\\|$\\)")))
8109 ;; This is a function or procedure definition statement
8110 ;; We return the defined routine as module.
8111 (list
52a244eb
S
8112 (idlwave-sintern-routine-or-method (match-string-no-properties 4)
8113 (match-string-no-properties 2))
15e42531
CD
8114 (if (equal (downcase (match-string 1)) "pro") 'pro 'fun)
8115 (idlwave-sintern-class (match-string 3)))
8116
52a244eb 8117 ;; Not a definition statement - analyze precise position.
15e42531
CD
8118 (let* ((where (idlwave-where))
8119 (cw (nth 2 where))
8120 (pro (car (nth 0 where)))
8121 (func (car (nth 1 where)))
8122 (this-word (idlwave-this-word "a-zA-Z0-9$_"))
8123 (next-char (save-excursion (skip-chars-forward "a-zA-Z0-9$_")
8124 (following-char)))
8125 )
8126 (cond
8127 ((and (eq cw 'procedure)
8128 (not (equal this-word "")))
4b1aaa8b 8129 (setq this-word (idlwave-sintern-routine-or-method
15e42531
CD
8130 this-word (nth 2 (nth 3 where))))
8131 (list this-word 'pro
4b1aaa8b 8132 (idlwave-determine-class
15e42531
CD
8133 (cons this-word (cdr (nth 3 where)))
8134 'pro)))
4b1aaa8b 8135 ((and (eq cw 'function)
15e42531
CD
8136 (not (equal this-word ""))
8137 (or (eq next-char ?\() ; exclude arrays, vars.
8138 (looking-at "[a-zA-Z0-9_]*[ \t]*(")))
4b1aaa8b 8139 (setq this-word (idlwave-sintern-routine-or-method
15e42531
CD
8140 this-word (nth 2 (nth 3 where))))
8141 (list this-word 'fun
8142 (idlwave-determine-class
8143 (cons this-word (cdr (nth 3 where)))
8144 'fun)))
8145 ((and (memq cw '(function-keyword procedure-keyword))
8146 (not (equal this-word ""))
8147 (eq next-char ?\()) ; A function!
8148 (setq this-word (idlwave-sintern-routine this-word))
8149 (list this-word 'fun nil))
8150 (func
8151 (list func 'fun (idlwave-determine-class (nth 1 where) 'fun)))
8152 (pro
8153 (list pro 'pro (idlwave-determine-class (nth 0 where) 'pro)))
8154 (t nil)))))
f32b3b91 8155
05a1abfc 8156(defun idlwave-what-module-find-class ()
5a0c3f56 8157 "Call `idlwave-what-module' and find the inherited class if necessary."
05a1abfc 8158 (let* ((module (idlwave-what-module))
8d222148 8159 (class (nth 2 module)))
05a1abfc
CD
8160 (if (and (= (length module) 3)
8161 (stringp class))
8162 (list (car module)
8163 (nth 1 module)
8164 (apply 'idlwave-find-inherited-class module))
8165 module)))
8166
8167(defun idlwave-find-inherited-class (name type class)
8168 "Find the class which defines TYPE NAME and is CLASS or inherited by CLASS."
8169 (let ((entry (idlwave-best-rinfo-assoc name type class (idlwave-routines))))
8170 (if entry
8171 (nth 2 entry)
8172 class)))
8173
8174(defun idlwave-fix-module-if-obj_new (module)
4b1aaa8b 8175 "Check if MODULE points to obj_new.
52a244eb
S
8176If yes, and if the cursor is in the keyword region, change to the
8177appropriate Init method."
05a1abfc
CD
8178 (let* ((name (car module))
8179 (pos (point))
8180 (case-fold-search t)
8181 string)
8182 (if (and (stringp name)
8183 (equal (downcase name) "obj_new")
8184 (save-excursion
8185 (idlwave-beginning-of-statement)
8186 (setq string (buffer-substring (point) pos))
8187 (string-match "obj_new([^'\"]*['\"]\\([a-zA-Z0-9_]+\\)"
8188 string)))
8189 (let ((name "Init")
8190 (class (match-string 1 string)))
8191 (setq module (list (idlwave-sintern-method "Init")
8192 'fun
8193 (idlwave-sintern-class class)))))
8194 module))
8195
4b1aaa8b 8196(defun idlwave-fix-keywords (name type class keywords
3938cb82 8197 &optional super-classes system)
52a244eb
S
8198 "Update a list of keywords.
8199Translate OBJ_NEW, adding all super-class keywords, or all keywords
5a0c3f56 8200from all classes if CLASS equals t. If SYSTEM is non-nil, don't
3938cb82 8201demand _EXTRA in the keyword list."
5e72c6b2 8202 (let ((case-fold-search t))
f32b3b91
CD
8203
8204 ;; If this is the OBJ_NEW function, try to figure out the class and use
8205 ;; the keywords from the corresponding INIT method.
5e72c6b2 8206 (if (and (equal (upcase name) "OBJ_NEW")
05a1abfc
CD
8207 (or (eq major-mode 'idlwave-mode)
8208 (eq major-mode 'idlwave-shell-mode)))
f32b3b91
CD
8209 (let* ((bos (save-excursion (idlwave-beginning-of-statement) (point)))
8210 (string (buffer-substring bos (point)))
8211 (case-fold-search t)
8212 class)
8213 (and (string-match "obj_new([^'\"]*['\"]\\([a-zA-Z0-9_]+\\)"
8214 string)
8215 (setq class (idlwave-sintern-class (match-string 1 string)))
15e42531 8216 (setq idlwave-current-obj_new-class class)
4b1aaa8b
PE
8217 (setq keywords
8218 (append keywords
52a244eb
S
8219 (idlwave-entry-keywords
8220 (idlwave-rinfo-assq
8221 (idlwave-sintern-method "INIT")
8222 'fun
8223 class
8224 (idlwave-routines)) 'do-link))))))
4b1aaa8b 8225
f32b3b91
CD
8226 ;; If the class is `t', combine all keywords of all methods NAME
8227 (when (eq class t)
52a244eb
S
8228 (mapc (lambda (entry)
8229 (and
8230 (nth 2 entry) ; non-nil class
8231 (eq (nth 1 entry) type) ; correct type
4b1aaa8b
PE
8232 (setq keywords
8233 (append keywords
52a244eb
S
8234 (idlwave-entry-keywords entry 'do-link)))))
8235 (idlwave-all-assq name (idlwave-routines)))
5e72c6b2 8236 (setq keywords (idlwave-uniquify keywords)))
4b1aaa8b 8237
5e72c6b2 8238 ;; If we have inheritance, add all keywords from superclasses, if
52a244eb 8239 ;; the user indicated that method in `idlwave-keyword-class-inheritance'
4b1aaa8b 8240 (when (and
52a244eb 8241 super-classes
5e72c6b2
S
8242 idlwave-keyword-class-inheritance
8243 (stringp class)
4b1aaa8b 8244 (or
3938cb82
S
8245 system
8246 (assq (idlwave-sintern-keyword "_extra") keywords)
8247 (assq (idlwave-sintern-keyword "_ref_extra") keywords))
5e72c6b2
S
8248 ;; Check if one of the keyword-class regexps matches the name
8249 (let ((regexps idlwave-keyword-class-inheritance) re)
8250 (catch 'exit
8251 (while (setq re (pop regexps))
8252 (if (string-match re name) (throw 'exit t))))))
52a244eb
S
8253
8254 (loop for entry in (idlwave-routines) do
8255 (and (nth 2 entry) ; non-nil class
8256 (memq (nth 2 entry) super-classes) ; an inherited class
8257 (eq (nth 1 entry) type) ; correct type
8258 (eq (car entry) name) ; correct name
8ffcfb27
GM
8259 (mapc (lambda (k) (add-to-list 'keywords k))
8260 (idlwave-entry-keywords entry 'do-link))))
f32b3b91 8261 (setq keywords (idlwave-uniquify keywords)))
4b1aaa8b 8262
f32b3b91
CD
8263 ;; Return the final list
8264 keywords))
8265
15e42531 8266(defun idlwave-expand-keyword (keyword module)
2e8b9c7d 8267 "Expand KEYWORD to one of the valid keyword parameters of MODULE.
15e42531
CD
8268KEYWORD may be an exact match or an abbreviation of a keyword.
8269If the match is exact, KEYWORD itself is returned, even if there may be other
8270keywords of which KEYWORD is an abbreviation. This is necessary because some
8271system routines have keywords which are prefixes of other keywords.
8272If KEYWORD is an abbreviation of several keywords, a list of all possible
8273completions is returned.
8274If the abbreviation was unique, the correct keyword is returned.
8275If it cannot be a keyword, the function return nil.
8276If we do not know about MODULE, just return KEYWORD literally."
8277 (let* ((name (car module))
8278 (type (nth 1 module))
8279 (class (nth 2 module))
8280 (kwd (idlwave-sintern-keyword keyword))
8281 (entry (idlwave-best-rinfo-assoc name type class (idlwave-routines)))
52a244eb 8282 (kwd-alist (idlwave-entry-keywords entry))
15e42531
CD
8283 (extra (or (assq (idlwave-sintern-keyword "_EXTRA") kwd-alist)
8284 (assq (idlwave-sintern-keyword "_REF_EXTRA") kwd-alist)))
8285 (completion-ignore-case t)
8286 candidates)
4b1aaa8b 8287 (cond ((assq kwd kwd-alist)
15e42531
CD
8288 kwd)
8289 ((setq candidates (all-completions kwd kwd-alist))
8290 (if (= (length candidates) 1)
8291 (car candidates)
8292 candidates))
8293 ((and entry extra)
4b1aaa8b 8294 ;; Inheritance may cause this keyword to be correct
15e42531
CD
8295 keyword)
8296 (entry
8297 ;; We do know the function, which does not have the keyword.
8298 nil)
8299 (t
8300 ;; We do not know the function, so this just might be a correct
8301 ;; keyword - return it as it is.
8302 keyword))))
8303
8304(defvar idlwave-rinfo-mouse-map (make-sparse-keymap))
f32b3b91 8305(defvar idlwave-rinfo-map (make-sparse-keymap))
4b1aaa8b 8306(define-key idlwave-rinfo-mouse-map
f32b3b91
CD
8307 (if (featurep 'xemacs) [button2] [mouse-2])
8308 'idlwave-mouse-active-rinfo)
4b1aaa8b 8309(define-key idlwave-rinfo-mouse-map
15e42531
CD
8310 (if (featurep 'xemacs) [(shift button2)] [(shift mouse-2)])
8311 'idlwave-mouse-active-rinfo-shift)
4b1aaa8b 8312(define-key idlwave-rinfo-mouse-map
f32b3b91
CD
8313 (if (featurep 'xemacs) [button3] [mouse-3])
8314 'idlwave-mouse-active-rinfo-right)
15e42531
CD
8315(define-key idlwave-rinfo-mouse-map " " 'idlwave-active-rinfo-space)
8316(define-key idlwave-rinfo-map "q" 'idlwave-quit-help)
8317(define-key idlwave-rinfo-mouse-map "q" 'idlwave-quit-help)
8318(defvar idlwave-popup-source nil)
8319(defvar idlwave-rinfo-marker (make-marker))
8320
8321(defun idlwave-quit-help ()
8322 (interactive)
8323 (let ((ri-window (get-buffer-window "*Help*"))
8324 (olh-window (get-buffer-window "*IDLWAVE Help*")))
8325 (when (and olh-window
8326 (fboundp 'idlwave-help-quit))
8327 (select-window olh-window)
8328 (idlwave-help-quit))
8329 (when (window-live-p ri-window)
8330 (delete-window ri-window))))
f32b3b91 8331
05a1abfc
CD
8332(defun idlwave-display-calling-sequence (name type class
8333 &optional initial-class)
f32b3b91 8334 ;; Display the calling sequence of module NAME, type TYPE in class CLASS.
05a1abfc
CD
8335 (let* ((initial-class (or initial-class class))
8336 (entry (or (idlwave-best-rinfo-assq name type class
15e42531 8337 (idlwave-routines))
4b1aaa8b 8338 (idlwave-rinfo-assq name type class
15e42531 8339 idlwave-unresolved-routines)))
f32b3b91
CD
8340 (name (or (car entry) name))
8341 (class (or (nth 2 entry) class))
05a1abfc 8342 (superclasses (idlwave-all-class-inherits initial-class))
15e42531
CD
8343 (twins (idlwave-routine-twins entry))
8344 (dtwins (idlwave-study-twins twins))
8345 (all dtwins)
52a244eb 8346 (system (eq (car (nth 3 entry)) 'system))
f32b3b91 8347 (calling-seq (nth 4 entry))
52a244eb
S
8348 (keywords (idlwave-entry-keywords entry 'do-link))
8349 (html-file (car (nth 5 entry)))
15e42531 8350 (help-echo-kwd
52a244eb 8351 "Button2: Insert KEYWORD (SHIFT=`/KEYWORD') | Button3: Online Help ")
15e42531 8352 (help-echo-use
52a244eb 8353 "Button2/3: Online Help")
15e42531 8354 (help-echo-src
52a244eb 8355 "Button2: Jump to source and back | Button3: Source in Help window.")
05a1abfc
CD
8356 (help-echo-class
8357 "Button2: Display info about same method in superclass")
f32b3b91 8358 (col 0)
52a244eb 8359 (data (list name type class (current-buffer) nil initial-class))
f32b3b91 8360 (km-prop (if (featurep 'xemacs) 'keymap 'local-map))
f66f03de 8361 (face 'idlwave-help-link)
15e42531 8362 beg props win cnt total)
4b1aaa8b 8363 ;; Fix keywords, but don't add chained super-classes, since these
52a244eb 8364 ;; are shown separately for that super-class
f32b3b91
CD
8365 (setq keywords (idlwave-fix-keywords name type class keywords))
8366 (cond
8367 ((null entry)
05a1abfc
CD
8368 (error "No %s %s known %s" type name
8369 (if initial-class (concat "in class " initial-class) "")))
f32b3b91 8370 ((or (null name) (equal name ""))
e8af40ee 8371 (error "No function or procedure call at point"))
f32b3b91 8372 ((null calling-seq)
52a244eb 8373 (error "Calling sequence of %s %s not available" type name))
f32b3b91 8374 (t
9a529312
SM
8375 (move-marker idlwave-rinfo-marker (point))
8376 (with-current-buffer (get-buffer-create "*Help*")
15e42531 8377 (use-local-map idlwave-rinfo-map)
f32b3b91
CD
8378 (setq buffer-read-only nil)
8379 (erase-buffer)
8380 (set (make-local-variable 'idlwave-popup-source) nil)
15e42531
CD
8381 (set (make-local-variable 'idlwave-current-obj_new-class)
8382 idlwave-current-obj_new-class)
05a1abfc
CD
8383 (when superclasses
8384 (setq props (list 'mouse-face 'highlight
8385 km-prop idlwave-rinfo-mouse-map
8386 'help-echo help-echo-class
8387 'data (cons 'class data)))
8388 (let ((classes (cons initial-class superclasses)) c)
8389 (insert "Classes: ")
8390 (while (setq c (pop classes))
8391 (insert " ")
8392 (setq beg (point))
8393 (insert c)
8394 (if (equal (downcase c) (downcase class))
8395 (add-text-properties beg (point) (list 'face 'bold))
52a244eb 8396 ;; If Method exists in a different class link it
05a1abfc
CD
8397 (if (idlwave-rinfo-assq name type c (idlwave-routines))
8398 (add-text-properties beg (point) props))))
8399 (insert "\n")))
52a244eb
S
8400 (setq props (list 'mouse-face 'highlight
8401 km-prop idlwave-rinfo-mouse-map
8402 'help-echo help-echo-use
8403 'data (cons 'usage data)))
4b1aaa8b 8404 (if html-file (setq props (append (list 'face face 'link html-file)
52a244eb 8405 props)))
f32b3b91
CD
8406 (insert "Usage: ")
8407 (setq beg (point))
8408 (insert (if class
52a244eb
S
8409 (format calling-seq class name class name class name)
8410 (format calling-seq name name name name))
f32b3b91
CD
8411 "\n")
8412 (add-text-properties beg (point) props)
4b1aaa8b 8413
f32b3b91
CD
8414 (insert "Keywords:")
8415 (if (null keywords)
8416 (insert " No keywords accepted.")
8417 (setq col 9)
8ffcfb27 8418 (mapc
f32b3b91 8419 (lambda (x)
4b1aaa8b 8420 (if (>= (+ col 1 (length (car x)))
f32b3b91
CD
8421 (window-width))
8422 (progn
8423 (insert "\n ")
8424 (setq col 9)))
8425 (insert " ")
8426 (setq beg (point)
52a244eb 8427 ;; Relevant keywords already have link property attached
f32b3b91 8428 props (list 'mouse-face 'highlight
15e42531 8429 km-prop idlwave-rinfo-mouse-map
f32b3b91 8430 'data (cons 'keyword data)
15e42531 8431 'help-echo help-echo-kwd
f32b3b91 8432 'keyword (car x)))
52a244eb 8433 (if system (setq props (append (list 'face face) props)))
f32b3b91
CD
8434 (insert (car x))
8435 (add-text-properties beg (point) props)
8436 (setq col (+ col 1 (length (car x)))))
8437 keywords))
4b1aaa8b 8438
15e42531 8439 (setq cnt 1 total (length all))
52a244eb 8440 ;; Here entry is (key file (list of type-conses))
15e42531
CD
8441 (while (setq entry (pop all))
8442 (setq props (list 'mouse-face 'highlight
8443 km-prop idlwave-rinfo-mouse-map
8444 'help-echo help-echo-src
52a244eb
S
8445 'source (list (car (car (nth 2 entry))) ;type
8446 (nth 1 entry)
8447 nil
8448 (cdr (car (nth 2 entry))))
15e42531
CD
8449 'data (cons 'source data)))
8450 (idlwave-insert-source-location
4b1aaa8b 8451 (format "\n%-8s %s"
15e42531
CD
8452 (if (equal cnt 1)
8453 (if (> total 1) "Sources:" "Source:")
8454 "")
8455 (if (> total 1) "- " ""))
8456 entry props)
8457 (incf cnt)
8458 (when (and all (> cnt idlwave-rinfo-max-source-lines))
8459 ;; No more source lines, please
4b1aaa8b 8460 (insert (format
15e42531
CD
8461 "\n Source information truncated to %d entries."
8462 idlwave-rinfo-max-source-lines))
8463 (setq all nil)))
10c8e594 8464 (goto-char (point-min))
f32b3b91
CD
8465 (setq buffer-read-only t))
8466 (display-buffer "*Help*")
8467 (if (and (setq win (get-buffer-window "*Help*"))
8468 idlwave-resize-routine-help-window)
8469 (progn
8470 (let ((ww (selected-window)))
8471 (unwind-protect
8472 (progn
8473 (select-window win)
4b1aaa8b 8474 (enlarge-window (- (/ (frame-height) 2)
f32b3b91
CD
8475 (window-height)))
8476 (shrink-window-if-larger-than-buffer))
8477 (select-window ww)))))))))
8478
15e42531
CD
8479(defun idlwave-insert-source-location (prefix entry &optional file-props)
8480 "Insert a source location into the routine info buffer.
5a0c3f56
JB
8481Start line with PREFIX. If a file name is inserted, add FILE-PROPS
8482to it."
15e42531
CD
8483 (let* ((key (car entry))
8484 (file (nth 1 entry))
8485 (types (nth 2 entry))
52a244eb
S
8486 (shell-flag (assq 'compiled types))
8487 (buffer-flag (assq 'buffer types))
8488 (user-flag (assq 'user types))
8489 (lib-flag (assq 'lib types))
8490 (ndupl (or (and buffer-flag (idlwave-count-memq 'buffer types))
8491 (and user-flag (idlwave-count-memq 'user types))
8492 (and lib-flag (idlwave-count-memq 'lib types))
15e42531
CD
8493 1))
8494 (doflags t)
8495 beg special)
8496
8497 (insert prefix)
8498
8499 (cond
8500 ((eq key 'system)
8501 (setq doflags nil)
52a244eb
S
8502 (insert "System "))
8503
15e42531
CD
8504 ((eq key 'builtin)
8505 (setq doflags nil)
52a244eb
S
8506 (insert "Builtin "))
8507
15e42531 8508 ((and (not file) shell-flag)
52a244eb
S
8509 (insert "Unresolved"))
8510
4b1aaa8b 8511 ((null file)
52a244eb 8512 (insert "ERROR"))
4b1aaa8b 8513
15e42531
CD
8514 ((idlwave-syslib-p file)
8515 (if (string-match "obsolete" (file-name-directory file))
52a244eb
S
8516 (insert "Obsolete ")
8517 (insert "SystemLib ")))
8518
8519 ;; New special syntax: taken directly from routine-info for
8520 ;; library catalog routines
8521 ((setq special (or (cdr lib-flag) (cdr user-flag)))
8522 (insert (format "%-10s" special)))
8523
8524 ;; Old special syntax: a matching regexp
8525 ((setq special (idlwave-special-lib-test file))
8526 (insert (format "%-10s" special)))
4b1aaa8b 8527
52a244eb 8528 ;; Catch-all with file
15e42531 8529 ((idlwave-lib-p file) (insert "Library "))
52a244eb
S
8530
8531 ;; Sanity catch all
15e42531
CD
8532 (t (insert "Other ")))
8533
8534 (when doflags
8535 (insert (concat
8536 " ["
52a244eb
S
8537 (if lib-flag "L" "-")
8538 (if user-flag "C" "-")
15e42531
CD
8539 (if shell-flag "S" "-")
8540 (if buffer-flag "B" "-")
8541 "] ")))
4b1aaa8b 8542 (when (> ndupl 1)
15e42531
CD
8543 (setq beg (point))
8544 (insert (format "(%dx) " ndupl))
8545 (add-text-properties beg (point) (list 'face 'bold)))
8546 (when (and file (not (equal file "")))
8547 (setq beg (point))
8548 (insert (apply 'abbreviate-file-name
8549 (if (featurep 'xemacs) (list file t) (list file))))
8550 (if file-props
8551 (add-text-properties beg (point) file-props)))))
8552
8553(defun idlwave-special-lib-test (file)
8554 "Check the path of FILE against the regexps which define special libs.
8555Return the name of the special lib if there is a match."
8556 (let ((alist idlwave-special-lib-alist)
8557 entry rtn)
8558 (cond
8559 ((stringp file)
8560 (while (setq entry (pop alist))
8561 (if (string-match (car entry) file)
8562 (setq rtn (cdr entry)
8563 alist nil)))
8564 rtn)
8565 (t nil))))
4b1aaa8b 8566
f32b3b91
CD
8567(defun idlwave-mouse-active-rinfo-right (ev)
8568 (interactive "e")
8569 (idlwave-mouse-active-rinfo ev 'right))
8570
15e42531 8571(defun idlwave-mouse-active-rinfo-shift (ev)
f32b3b91 8572 (interactive "e")
15e42531
CD
8573 (idlwave-mouse-active-rinfo ev nil 'shift))
8574
8575(defun idlwave-active-rinfo-space ()
8576 (interactive)
8577 (idlwave-mouse-active-rinfo nil 'right))
8578
8579(defun idlwave-mouse-active-rinfo (ev &optional right shift)
5a0c3f56 8580 "Do the mouse actions in the routine info buffer.
15e42531
CD
8581Optional args RIGHT and SHIFT indicate, if mouse-3 was used, and if SHIFT
8582was pressed."
8583 (interactive "e")
8584 (if ev (mouse-set-point ev))
4b1aaa8b 8585 (let (data id name type class buf bufwin source link keyword
3938cb82 8586 word initial-class)
f32b3b91 8587 (setq data (get-text-property (point) 'data)
15e42531 8588 source (get-text-property (point) 'source)
f32b3b91 8589 keyword (get-text-property (point) 'keyword)
52a244eb 8590 link (get-text-property (point) 'link)
f32b3b91 8591 id (car data)
15e42531 8592 name (nth 1 data) type (nth 2 data) class (nth 3 data)
f32b3b91 8593 buf (nth 4 data)
05a1abfc
CD
8594 initial-class (nth 6 data)
8595 word (idlwave-this-word)
f32b3b91 8596 bufwin (get-buffer-window buf t))
52a244eb
S
8597
8598 (cond ((eq id 'class) ; Switch class being displayed
05a1abfc 8599 (if (window-live-p bufwin) (select-window bufwin))
4b1aaa8b 8600 (idlwave-display-calling-sequence
05a1abfc 8601 (idlwave-sintern-method name)
4b1aaa8b 8602 type (idlwave-sintern-class word)
05a1abfc 8603 initial-class))
52a244eb
S
8604 ((eq id 'usage) ; Online help on this routine
8605 (idlwave-online-help link name type class))
8606 ((eq id 'source) ; Source in help or buffer
8607 (if right ; In help
15e42531
CD
8608 (let ((idlwave-extra-help-function 'idlwave-help-with-source)
8609 (idlwave-help-source-try-header nil)
52a244eb 8610 ;; Fake idlwave-routines so help will find the right entry
15e42531 8611 (idlwave-routines
52a244eb 8612 (list (list name type class source ""))))
15e42531 8613 (idlwave-help-get-special-help name type class nil))
52a244eb 8614 ;; Otherwise just pop to the source
f32b3b91
CD
8615 (setq idlwave-popup-source (not idlwave-popup-source))
8616 (if idlwave-popup-source
8617 (condition-case err
15e42531 8618 (idlwave-do-find-module name type class source)
f32b3b91
CD
8619 (error
8620 (setq idlwave-popup-source nil)
8621 (if (window-live-p bufwin) (select-window bufwin))
8622 (error (nth 1 err))))
8623 (if bufwin
8624 (select-window bufwin)
15e42531
CD
8625 (pop-to-buffer buf))
8626 (goto-char (marker-position idlwave-rinfo-marker)))))
f32b3b91
CD
8627 ((eq id 'keyword)
8628 (if right
52a244eb 8629 (idlwave-online-help link name type class keyword)
15e42531
CD
8630 (idlwave-rinfo-insert-keyword keyword buf shift))))))
8631
8632(defun idlwave-rinfo-insert-keyword (keyword buffer &optional shift)
8633 "Insert KEYWORD in BUFFER. Make sure buffer is displayed in a window."
8634 (let ((bwin (get-buffer-window buffer)))
8635 (if idlwave-complete-empty-string-as-lower-case
8636 (setq keyword (downcase keyword)))
8637 (if bwin
8638 (select-window bwin)
8639 (pop-to-buffer buffer)
8640 (setq bwin (get-buffer-window buffer)))
8641 (if (eq (preceding-char) ?/)
8642 (insert keyword)
4b1aaa8b 8643 (unless (save-excursion
15e42531 8644 (re-search-backward
4b1aaa8b 8645 "[(,][ \t]*\\(\\$[ \t]*\\(;.*\\)?\n\\)?[ \t]*\\="
15e42531
CD
8646 (min (- (point) 100) (point-min)) t))
8647 (insert ", "))
8648 (if shift (insert "/"))
8649 (insert keyword)
8650 (if (and (not shift)
8651 idlwave-keyword-completion-adds-equal)
8652 (insert "=")))))
8653
8654(defun idlwave-list-buffer-load-path-shadows (&optional arg)
8655 "List the load path shadows of all routines defined in current buffer."
8656 (interactive "P")
8657 (idlwave-routines)
8658 (if (eq major-mode 'idlwave-mode)
8659 (idlwave-list-load-path-shadows
8660 nil (idlwave-update-current-buffer-info 'save-buffer)
8661 "in current buffer")
8662 (error "Current buffer is not in idlwave-mode")))
8663
8664(defun idlwave-list-shell-load-path-shadows (&optional arg)
8665 "List the load path shadows of all routines compiled under the shell.
8666This is very useful for checking an IDL application. Just compile the
8667application, do RESOLVE_ALL, and `C-c C-i' to compile all referenced
8668routines and update IDLWAVE internal info. Then check for shadowing
8669with this command."
8670 (interactive "P")
8671 (cond
8672 ((or (not (fboundp 'idlwave-shell-is-running))
8673 (not (idlwave-shell-is-running)))
8674 (error "Shell is not running"))
8675 ((null idlwave-compiled-routines)
e8af40ee 8676 (error "No compiled routines. Maybe you need to update with `C-c C-i'"))
15e42531
CD
8677 (t
8678 (idlwave-list-load-path-shadows nil idlwave-compiled-routines
8679 "in the shell"))))
8680
8681(defun idlwave-list-all-load-path-shadows (&optional arg)
8682 "List the load path shadows of all routines known to IDLWAVE."
8683 (interactive "P")
8684 (idlwave-list-load-path-shadows nil nil "globally"))
8685
8d222148
SM
8686(defvar idlwave-sort-prefer-buffer-info t
8687 "Internal variable used to influence `idlwave-routine-twin-compare'.")
8688
15e42531
CD
8689(defun idlwave-list-load-path-shadows (arg &optional special-routines loc)
8690 "List the routines which are defined multiple times.
8691Search the information IDLWAVE has about IDL routines for multiple
8692definitions.
8693When SPECIAL-ROUTINES in non-nil, only look for shadows of these routines.
8694
8695When IDL hits a routine call which is not defined, it will search on
5a0c3f56
JB
8696the load path in order to find a definition. The output of this command
8697can be used to detect possible name clashes during this process."
15e42531 8698 (idlwave-routines) ; Make sure everything is loaded.
52a244eb 8699 (unless (or idlwave-user-catalog-routines idlwave-library-catalog-routines)
4b1aaa8b 8700 (or (y-or-n-p
52a244eb 8701 "You don't have any user or library catalogs. Continue anyway? ")
15e42531
CD
8702 (error "Abort")))
8703 (let* ((routines (append idlwave-system-routines
8704 idlwave-compiled-routines
52a244eb
S
8705 idlwave-library-catalog-routines
8706 idlwave-user-catalog-routines
15e42531
CD
8707 idlwave-buffer-routines
8708 nil))
8709 (km-prop (if (featurep 'xemacs) 'keymap 'local-map))
8710 (keymap (make-sparse-keymap))
8711 (props (list 'mouse-face 'highlight
8712 km-prop keymap
4b1aaa8b 8713 'help-echo "Mouse2: Find source"))
15e42531 8714 (nroutines (length (or special-routines routines)))
f66f03de 8715 (step (/ nroutines 100))
15e42531 8716 (n 0)
15e42531
CD
8717 (cnt 0)
8718 (idlwave-sort-prefer-buffer-info nil)
8719 routine twins dtwins twin done props1 lroutines)
8720
8721 (if special-routines
8722 ;; Just looking for shadows of a few special routines
8723 (setq lroutines routines
8724 routines special-routines))
8725
8726 (message "Sorting routines...")
8727 (setq routines (sort routines
8728 (lambda (a b)
8729 (string< (downcase (idlwave-make-full-name
8730 (nth 2 a) (car a)))
8731 (downcase (idlwave-make-full-name
8732 (nth 2 b) (car b)))))))
8733 (message "Sorting routines...done")
8734
8735 (define-key keymap (if (featurep 'xemacs) [(button2)] [(mouse-2)])
4b1aaa8b 8736 (lambda (ev)
15e42531
CD
8737 (interactive "e")
8738 (mouse-set-point ev)
8739 (apply 'idlwave-do-find-module
8740 (get-text-property (point) 'find-args))))
8741 (define-key keymap [(return)]
4b1aaa8b 8742 (lambda ()
15e42531
CD
8743 (interactive)
8744 (apply 'idlwave-do-find-module
8745 (get-text-property (point) 'find-args))))
8746 (message "Compiling list...( 0%%)")
9a529312 8747 (with-current-buffer (get-buffer-create "*Shadows*")
15e42531
CD
8748 (setq buffer-read-only nil)
8749 (erase-buffer)
8750 (while (setq routine (pop routines))
f66f03de
S
8751 (if (= (mod (setq n (1+ n)) step) 0)
8752 (message "Compiling list...(%2d%%)" (/ (* n 100) nroutines)))
8753
15e42531
CD
8754 ;; Get a list of all twins
8755 (setq twins (idlwave-routine-twins routine (or lroutines routines)))
8756 (if (memq routine done)
8757 (setq dtwins nil)
8758 (setq dtwins (idlwave-study-twins twins)))
5e72c6b2 8759 ;; Mark all twins as dealt with
15e42531
CD
8760 (setq done (append twins done))
8761 (when (or (> (length dtwins) 1)
52a244eb
S
8762 (> (idlwave-count-memq 'lib (nth 2 (car dtwins))) 1)
8763 (> (idlwave-count-memq 'user (nth 2 (car dtwins))) 1)
8764 (> (idlwave-count-memq 'buffer (nth 2 (car dtwins))) 1))
15e42531
CD
8765 (incf cnt)
8766 (insert (format "\n%s%s"
4b1aaa8b 8767 (idlwave-make-full-name (nth 2 routine)
52a244eb 8768 (car routine))
15e42531
CD
8769 (if (eq (nth 1 routine) 'fun) "()" "")))
8770 (while (setq twin (pop dtwins))
8771 (setq props1 (append (list 'find-args
4b1aaa8b
PE
8772 (list (nth 0 routine)
8773 (nth 1 routine)
52a244eb 8774 (nth 2 routine)))
15e42531
CD
8775 props))
8776 (idlwave-insert-source-location "\n - " twin props1))))
8777 (goto-char (point-min))
8778 (setq buffer-read-only t))
8779 (setq loc (or loc ""))
8780 (if (> cnt 0)
8781 (progn
8782 (display-buffer (get-buffer "*Shadows*"))
8783 (message "%d case%s of shadowing found %s"
8784 cnt (if (= cnt 1) "" "s") loc))
8785 (message "No shadowing conflicts found %s" loc))))
8786
8787(defun idlwave-print-source (routine)
8788 (let* ((source (nth 3 routine))
8789 (stype (car source))
52a244eb
S
8790 (sfile (idlwave-routine-source-file source)))
8791 (if (idlwave-syslib-p sfile) (setq stype 'syslib))
15e42531
CD
8792 (if (and (eq stype 'compiled)
8793 (or (not (stringp sfile))
8794 (not (string-match "\\S-" sfile))))
8795 (setq stype 'unresolved))
4b1aaa8b 8796 (princ (format " %-10s %s\n"
15e42531
CD
8797 stype
8798 (if sfile sfile "No source code available")))))
8799
8800(defun idlwave-routine-twins (entry &optional list)
8801 "Return all twin entries of ENTRY in LIST.
8802LIST defaults to `idlwave-routines'.
8803Twin entries are those which have the same name, type, and class.
8804ENTRY will also be returned, as the first item of this list."
8805 (let* ((name (car entry))
8806 (type (nth 1 entry))
8807 (class (nth 2 entry))
8808 (candidates (idlwave-all-assq name (or list (idlwave-routines))))
8809 twins candidate)
8810 (while (setq candidate (pop candidates))
8811 (if (and (not (eq candidate entry))
8812 (eq type (nth 1 candidate))
8813 (eq class (nth 2 candidate)))
8814 (push candidate twins)))
4b1aaa8b 8815 (if (setq candidate (idlwave-rinfo-assq name type class
15e42531
CD
8816 idlwave-unresolved-routines))
8817 (push candidate twins))
8818 (cons entry (nreverse twins))))
8819
8820(defun idlwave-study-twins (entries)
4b1aaa8b 8821 "Return dangerous twins of first entry in ENTRIES.
52a244eb
S
8822Dangerous twins are routines with same name, but in different files on
8823the load path. If a file is in the system library and has an entry in
8824the `idlwave-system-routines' list, we omit the latter as
8825non-dangerous because many IDL routines are implemented as library
8826routines, and may have been scanned."
15e42531 8827 (let* ((entry (car entries))
4b1aaa8b 8828 (name (car entry)) ;
15e42531
CD
8829 (type (nth 1 entry)) ; Must be bound for
8830 (class (nth 2 entry)) ; idlwave-routine-twin-compare
8831 (cnt 0)
52a244eb 8832 source type type-cons file alist syslibp key)
15e42531
CD
8833 (while (setq entry (pop entries))
8834 (incf cnt)
8835 (setq source (nth 3 entry)
8836 type (car source)
52a244eb
S
8837 type-cons (cons type (nth 3 source))
8838 file (idlwave-routine-source-file source))
8839
15e42531
CD
8840 ;; Make KEY to index entry properly
8841 (setq key (cond ((eq type 'system) type)
8842 (file (file-truename file))
8843 (t 'unresolved)))
52a244eb
S
8844
8845 ;; Check for an entry in the system library
4b1aaa8b 8846 (if (and file
15e42531
CD
8847 (not syslibp)
8848 (idlwave-syslib-p file))
15e42531 8849 (setq syslibp t))
4b1aaa8b 8850
52a244eb
S
8851 ;; If there's more than one matching entry for the same file, just
8852 ;; append the type-cons to the type list.
15e42531 8853 (if (setq entry (assoc key alist))
52a244eb
S
8854 (push type-cons (nth 2 entry))
8855 (push (list key file (list type-cons)) alist)))
4b1aaa8b 8856
15e42531 8857 (setq alist (nreverse alist))
4b1aaa8b 8858
15e42531 8859 (when syslibp
52a244eb
S
8860 ;; File is in system *library* - remove any 'system entry
8861 (setq alist (delq (assq 'system alist) alist)))
4b1aaa8b 8862
52a244eb
S
8863 ;; If 'system remains and we've scanned the syslib, it's a builtin
8864 ;; (rather than a !DIR/lib/.pro file bundled as source).
15e42531
CD
8865 (when (and (idlwave-syslib-scanned-p)
8866 (setq entry (assoc 'system alist)))
8867 (setcar entry 'builtin))
8868 (sort alist 'idlwave-routine-twin-compare)))
8869
8d222148
SM
8870;; FIXME: Dynamically scoped vars need to use the `idlwave-' prefix.
8871;; (defvar type)
8872;; (defvar class)
15e42531
CD
8873(defmacro idlwave-xor (a b)
8874 `(and (or ,a ,b)
8875 (not (and ,a ,b))))
8876
8877(defun idlwave-routine-entry-compare (a b)
5a0c3f56
JB
8878 "Compare two routine info entries for sorting.
8879This is the general case. It first compares class, names, and type.
8880If it turns out that A and B are twins (same name, class, and type),
8881calls another routine which compares twins on the basis of their file
8882names and path locations."
15e42531
CD
8883 (let ((name (car a)) (type (nth 1 a)) (class (nth 2 a)))
8884 (cond
8885 ((not (equal (idlwave-downcase-safe class)
8886 (idlwave-downcase-safe (nth 2 b))))
8887 ;; Class decides
8888 (cond ((null (nth 2 b)) nil)
8889 ((null class) t)
8890 (t (string< (downcase class) (downcase (nth 2 b))))))
8891 ((not (equal (downcase name) (downcase (car b))))
8892 ;; Name decides
8893 (string< (downcase name) (downcase (car b))))
8894 ((not (eq type (nth 1 b)))
8895 ;; Type decides
8896 (< (if (eq type 'fun) 1 0) (if (eq (nth 1 b) 'fun) 1 0)))
4b1aaa8b 8897 (t
15e42531
CD
8898 ;; A and B are twins - so the decision is more complicated.
8899 ;; Call twin-compare with the proper arguments.
8900 (idlwave-routine-entry-compare-twins a b)))))
8901
8902(defun idlwave-routine-entry-compare-twins (a b)
5a0c3f56
JB
8903 "Compare two routine entries, under the assumption that they are twins.
8904This basically calls `idlwave-routine-twin-compare' with the correct args."
52a244eb
S
8905 (let* ((name (car a)) (type (nth 1 a)) (class (nth 2 a)) ; needed outside
8906 (asrc (nth 3 a))
8907 (atype (car asrc))
8908 (bsrc (nth 3 b))
8909 (btype (car bsrc))
8910 (afile (idlwave-routine-source-file asrc))
8911 (bfile (idlwave-routine-source-file bsrc)))
15e42531
CD
8912 (idlwave-routine-twin-compare
8913 (if (stringp afile)
8914 (list (file-truename afile) afile (list atype))
8915 (list atype afile (list atype)))
8916 (if (stringp bfile)
8917 (list (file-truename bfile) bfile (list btype))
8918 (list btype bfile (list btype))))
8919 ))
8920
627e0a14
GM
8921;; Bound in idlwave-study-twins,idlwave-routine-entry-compare-twins.
8922;; FIXME: Dynamically scoped vars need to use the `idlwave-' prefix.
8923(defvar class)
8924
15e42531
CD
8925(defun idlwave-routine-twin-compare (a b)
8926 "Compare two routine twin entries for sorting.
8927In here, A and B are not normal routine info entries, but special
8928lists (KEY FILENAME (TYPES...)).
8929This expects NAME TYPE CLASS to be bound to the right values."
8930 (let* (;; Dis-assemble entries
8931 (akey (car a)) (bkey (car b))
8932 (afile (nth 1 a)) (bfile (nth 1 b))
8933 (atypes (nth 2 a)) (btypes (nth 2 b))
8934 ;; System routines?
8935 (asysp (memq akey '(builtin system)))
8936 (bsysp (memq bkey '(builtin system)))
8937 ;; Compiled routines?
8938 (acompp (memq 'compiled atypes))
8939 (bcompp (memq 'compiled btypes))
8940 ;; Unresolved?
8941 (aunresp (or (eq akey 'unresolved)
8942 (and acompp (not afile))))
8943 (bunresp (or (eq bkey 'unresolved)
8944 (and bcompp (not bfile))))
8945 ;; Buffer info available?
8946 (abufp (memq 'buffer atypes))
8947 (bbufp (memq 'buffer btypes))
8948 ;; On search path?
8949 (tpath-alist (idlwave-true-path-alist))
52a244eb
S
8950 (apathp (and (stringp akey)
8951 (assoc (file-name-directory akey) tpath-alist)))
4b1aaa8b 8952 (bpathp (and (stringp bkey)
52a244eb 8953 (assoc (file-name-directory bkey) tpath-alist)))
15e42531
CD
8954 ;; How early on search path? High number means early since we
8955 ;; measure the tail of the path list
8956 (anpath (length (memq apathp tpath-alist)))
8957 (bnpath (length (memq bpathp tpath-alist)))
8958 ;; Look at file names
8959 (aname (if (stringp afile) (downcase (file-name-nondirectory afile)) ""))
8960 (bname (if (stringp bfile) (downcase (file-name-nondirectory bfile)) ""))
8961 (fname-re (if class (format "\\`%s__\\(%s\\|define\\)\\.pro\\'"
8962 (regexp-quote (downcase class))
8963 (regexp-quote (downcase name)))
8964 (format "\\`%s\\.pro" (regexp-quote (downcase name)))))
8965 ;; Is file name derived from the routine name?
8966 ;; Method file or class definition file?
8967 (anamep (string-match fname-re aname))
8968 (adefp (and class anamep (string= "define" (match-string 1 aname))))
8969 (bnamep (string-match fname-re bname))
8970 (bdefp (and class bnamep (string= "define" (match-string 1 bname)))))
8971
8972 ;; Now: follow JD's ideas about sorting. Looks really simple now,
8973 ;; doesn't it? The difficult stuff is hidden above...
8974 (cond
8975 ((idlwave-xor asysp bsysp) asysp) ; System entries first
8976 ((idlwave-xor aunresp bunresp) bunresp) ; Unresolved last
8977 ((and idlwave-sort-prefer-buffer-info
8978 (idlwave-xor abufp bbufp)) abufp) ; Buffers before non-buffers
8979 ((idlwave-xor acompp bcompp) acompp) ; Compiled entries
8980 ((idlwave-xor apathp bpathp) apathp) ; Library before non-library
8981 ((idlwave-xor anamep bnamep) anamep) ; Correct file names first
8982 ((and class anamep bnamep ; both file names match ->
8983 (idlwave-xor adefp bdefp)) bdefp) ; __define after __method
8984 ((> anpath bnpath) t) ; Who is first on path?
8985 (t nil)))) ; Default
8986
52a244eb 8987(defun idlwave-routine-source-file (source)
4b1aaa8b 8988 (if (nth 2 source)
52a244eb
S
8989 (expand-file-name (nth 1 source) (nth 2 source))
8990 (nth 1 source)))
8991
15e42531
CD
8992(defun idlwave-downcase-safe (string)
8993 "Donwcase if string, else return unchanged."
8994 (if (stringp string)
8995 (downcase string)
8996 string))
8997
8998(defun idlwave-count-eq (elt list)
8999 "How often is ELT in LIST?"
9000 (length (delq nil (mapcar (lambda (x) (eq x elt)) list))))
9001
52a244eb
S
9002(defun idlwave-count-memq (elt alist)
9003 "How often is ELT a key in ALIST?"
9004 (length (delq nil (mapcar (lambda (x) (eq (car x) elt)) alist))))
9005
15e42531 9006(defun idlwave-syslib-p (file)
52a244eb 9007 "Non-nil if FILE is in the system library."
15e42531
CD
9008 (let* ((true-syslib (file-name-as-directory
9009 (file-truename
9010 (expand-file-name "lib" (idlwave-sys-dir)))))
9011 (true-file (file-truename file)))
9012 (string-match (concat "^" (regexp-quote true-syslib)) true-file)))
9013
9014(defun idlwave-lib-p (file)
5a0c3f56 9015 "Non-nil if FILE is in the library."
15e42531
CD
9016 (let ((true-dir (file-name-directory (file-truename file))))
9017 (assoc true-dir (idlwave-true-path-alist))))
9018
52a244eb
S
9019(defun idlwave-path-alist-add-flag (list-entry flag)
9020 "Add a flag to the path list entry, if not set."
9021 (let ((flags (cdr list-entry)))
9022 (add-to-list 'flags flag)
9023 (setcdr list-entry flags)))
9024
9025(defun idlwave-path-alist-remove-flag (list-entry flag)
9026 "Remove a flag to the path list entry, if set."
9027 (let ((flags (delq flag (cdr list-entry))))
9028 (setcdr list-entry flags)))
9029
15e42531
CD
9030(defun idlwave-true-path-alist ()
9031 "Return `idlwave-path-alist' alist with true-names.
52a244eb 9032Info is cached, but relies on the functions setting `idlwave-path-alist'
15e42531
CD
9033to reset the variable `idlwave-true-path-alist' to nil."
9034 (or idlwave-true-path-alist
9035 (setq idlwave-true-path-alist
9036 (mapcar (lambda(x) (cons
9037 (file-name-as-directory
9038 (file-truename
9039 (directory-file-name
9040 (car x))))
9041 (cdr x)))
9042 idlwave-path-alist))))
9043
9044(defun idlwave-syslib-scanned-p ()
9045 "Non-nil if the system lib file !DIR/lib has been scanned."
9046 (let* ((true-syslib (file-name-as-directory
9047 (file-truename
9048 (expand-file-name "lib" (idlwave-sys-dir))))))
9049 (cdr (assoc true-syslib (idlwave-true-path-alist)))))
9050
9051;; ----------------------------------------------------------------------------
9052;;
9053;; Online Help display
9054
f32b3b91
CD
9055
9056;; ----------------------------------------------------------------------------
9057;;
9058;; Additions for use with imenu.el and func-menu.el
9059;; (pop-up a list of IDL units in the current file).
9060;;
9061
9062(defun idlwave-prev-index-position ()
9063 "Search for the previous procedure or function.
9064Return nil if not found. For use with imenu.el."
9065 (save-match-data
9066 (cond
9067 ((idlwave-find-key "\\<\\(pro\\|function\\)\\>" -1 'nomark))
9068 ;; ((idlwave-find-key idlwave-begin-unit-reg 1 'nomark)
9069 (t nil))))
9070
9071(defun idlwave-unit-name ()
9072 "Return the unit name.
9073Assumes that point is at the beginning of the unit as found by
9074`idlwave-prev-index-position'."
9075 (forward-sexp 2)
9076 (forward-sexp -1)
9077 (let ((begin (point)))
4b1aaa8b 9078 (re-search-forward
52a244eb 9079 "[a-zA-Z_][a-zA-Z0-9$_]+\\(::[a-zA-Z_][a-zA-Z0-9$_]+\\)?")
f32b3b91
CD
9080 (if (fboundp 'buffer-substring-no-properties)
9081 (buffer-substring-no-properties begin (point))
9082 (buffer-substring begin (point)))))
9083
facebc7b
S
9084(defalias 'idlwave-function-menu
9085 (condition-case nil
f32b3b91
CD
9086 (progn
9087 (require 'func-menu)
facebc7b
S
9088 'function-menu)
9089 (error (condition-case nil
9090 (progn
9091 (require 'imenu)
9092 'imenu)
9093 (error nil)))))
f32b3b91 9094
52a244eb 9095;; Here we hack func-menu.el in order to support this new mode.
f32b3b91
CD
9096;; The latest versions of func-menu.el already have this stuff in, so
9097;; we hack only if it is not already there.
9098(when (fboundp 'eval-after-load)
9099 (eval-after-load "func-menu"
9100 '(progn
9101 (or (assq 'idlwave-mode fume-function-name-regexp-alist)
9102 (not (boundp 'fume-function-name-regexp-idl)) ; avoid problems
9103 (setq fume-function-name-regexp-alist
9104 (cons '(idlwave-mode . fume-function-name-regexp-idl)
9105 fume-function-name-regexp-alist)))
9106 (or (assq 'idlwave-mode fume-find-function-name-method-alist)
9107 (not (fboundp 'fume-find-next-idl-function-name)) ; avoid problems
9108 (setq fume-find-function-name-method-alist
9109 (cons '(idlwave-mode . fume-find-next-idl-function-name)
9110 fume-find-function-name-method-alist))))))
9111
9112(defun idlwave-edit-in-idlde ()
9113 "Edit the current file in IDL Development environment."
9114 (interactive)
9115 (start-process "idldeclient" nil
9116 idlwave-shell-explicit-file-name "-c" "-e"
f66f03de 9117 (buffer-file-name)))
4b1aaa8b 9118
f66f03de 9119(defvar idlwave-help-use-assistant)
f32b3b91
CD
9120(defun idlwave-launch-idlhelp ()
9121 "Start the IDLhelp application."
9122 (interactive)
f66f03de
S
9123 (if idlwave-help-use-assistant
9124 (idlwave-help-assistant-raise)
9125 (start-process "idlhelp" nil idlwave-help-application)))
4b1aaa8b 9126
f32b3b91
CD
9127;; Menus - using easymenu.el
9128(defvar idlwave-mode-menu-def
9129 `("IDLWAVE"
9130 ["PRO/FUNC menu" idlwave-function-menu t]
9131 ("Motion"
9132 ["Subprogram Start" idlwave-beginning-of-subprogram t]
9133 ["Subprogram End" idlwave-end-of-subprogram t]
9134 ["Block Start" idlwave-beginning-of-block t]
9135 ["Block End" idlwave-end-of-block t]
9136 ["Up Block" idlwave-backward-up-block t]
9137 ["Down Block" idlwave-down-block t]
9138 ["Skip Block Backward" idlwave-backward-block t]
9139 ["Skip Block Forward" idlwave-forward-block t])
9140 ("Mark"
9141 ["Subprogram" idlwave-mark-subprogram t]
9142 ["Block" idlwave-mark-block t]
9143 ["Header" idlwave-mark-doclib t])
9144 ("Format"
4b1aaa8b 9145 ["Indent Entire Statement" idlwave-indent-statement
f66f03de 9146 :active t :keys "C-u \\[indent-for-tab-command]" ]
f32b3b91 9147 ["Indent Subprogram" idlwave-indent-subprogram t]
f66f03de 9148 ["(Un)Comment Region" idlwave-toggle-comment-region t]
f32b3b91
CD
9149 ["Continue/Split line" idlwave-split-line t]
9150 "--"
9151 ["Toggle Auto Fill" idlwave-auto-fill-mode :style toggle
9152 :selected (symbol-value idlwave-fill-function)])
9153 ("Templates"
9154 ["Procedure" idlwave-procedure t]
9155 ["Function" idlwave-function t]
9156 ["Doc Header" idlwave-doc-header t]
9157 ["Log" idlwave-doc-modification t]
9158 "--"
9159 ["Case" idlwave-case t]
9160 ["For" idlwave-for t]
9161 ["Repeat" idlwave-repeat t]
9162 ["While" idlwave-while t]
9163 "--"
9164 ["Close Block" idlwave-close-block t])
15e42531 9165 ("Completion"
f32b3b91 9166 ["Complete" idlwave-complete t]
f66f03de 9167 ("Complete Specific"
f32b3b91
CD
9168 ["1 Procedure Name" (idlwave-complete 'procedure) t]
9169 ["2 Procedure Keyword" (idlwave-complete 'procedure-keyword) t]
9170 "--"
9171 ["3 Function Name" (idlwave-complete 'function) t]
9172 ["4 Function Keyword" (idlwave-complete 'function-keyword) t]
9173 "--"
9174 ["5 Procedure Method Name" (idlwave-complete 'procedure-method) t]
9175 ["6 Procedure Method Keyword" (idlwave-complete 'procedure-method-keyword) t]
9176 "--"
9177 ["7 Function Method Name" (idlwave-complete 'function-method) t]
9178 ["8 Function Method Keyword" (idlwave-complete 'function-method-keyword) t]
9179 "--"
15e42531
CD
9180 ["9 Class Name" idlwave-complete-class t]))
9181 ("Routine Info"
f32b3b91 9182 ["Show Routine Info" idlwave-routine-info t]
52a244eb 9183 ["Online Context Help" idlwave-context-help t]
f32b3b91
CD
9184 "--"
9185 ["Find Routine Source" idlwave-find-module t]
15e42531 9186 ["Resolve Routine" idlwave-resolve (featurep 'idlw-shell)]
f32b3b91
CD
9187 "--"
9188 ["Update Routine Info" idlwave-update-routine-info t]
f66f03de 9189 ["Rescan XML Help Catalog" idlwave-convert-xml-system-routine-info t]
f32b3b91 9190 "--"
52a244eb
S
9191 "IDL User Catalog"
9192 ["Select Catalog Directories" (idlwave-create-user-catalog-file nil) t]
15e42531 9193 ["Scan Directories" (idlwave-update-routine-info '(16))
5e72c6b2
S
9194 (and idlwave-path-alist (not idlwave-catalog-process))]
9195 ["Scan Directories &" (idlwave-update-routine-info '(64))
9196 (and idlwave-path-alist (not idlwave-catalog-process))]
15e42531
CD
9197 "--"
9198 "Routine Shadows"
9199 ["Check Current Buffer" idlwave-list-buffer-load-path-shadows t]
9200 ["Check Compiled Routines" idlwave-list-shell-load-path-shadows t]
9201 ["Check Everything" idlwave-list-all-load-path-shadows t])
9202 ("Misc"
9203 ["Kill auto-created buffers" idlwave-kill-autoloaded-buffers t]
9204 "--"
9205 ["Insert TAB character" idlwave-hard-tab t])
f32b3b91
CD
9206 "--"
9207 ("External"
f32b3b91
CD
9208 ["Start IDL shell" idlwave-shell t]
9209 ["Edit file in IDLDE" idlwave-edit-in-idlde t]
9210 ["Launch IDL Help" idlwave-launch-idlhelp t])
9211 "--"
9212 ("Customize"
9213 ["Browse IDLWAVE Group" idlwave-customize t]
9214 "--"
4b1aaa8b 9215 ["Build Full Customize Menu" idlwave-create-customize-menu
f32b3b91
CD
9216 (fboundp 'customize-menu-create)])
9217 ("Documentation"
9218 ["Describe Mode" describe-mode t]
9219 ["Abbreviation List" idlwave-list-abbrevs t]
9220 "--"
9221 ["Commentary in idlwave.el" idlwave-show-commentary t]
595ab50b 9222 ["Commentary in idlw-shell.el" idlwave-shell-show-commentary t]
f32b3b91
CD
9223 "--"
9224 ["Info" idlwave-info t]
9225 "--"
8c43762b 9226 ["Help with Topic" idlwave-help-assistant-help-with-topic
e08734e2 9227 idlwave-help-use-assistant]
f32b3b91
CD
9228 ["Launch IDL Help" idlwave-launch-idlhelp t])))
9229
9230(defvar idlwave-mode-debug-menu-def
9231 '("Debug"
9232 ["Start IDL shell" idlwave-shell t]
9233 ["Save and .RUN buffer" idlwave-shell-save-and-run
4b1aaa8b 9234 (and (boundp 'idlwave-shell-automatic-start)
f32b3b91
CD
9235 idlwave-shell-automatic-start)]))
9236
9237(if (or (featurep 'easymenu) (load "easymenu" t))
9238 (progn
4b1aaa8b
PE
9239 (easy-menu-define idlwave-mode-menu idlwave-mode-map
9240 "IDL and WAVE CL editing menu"
f32b3b91 9241 idlwave-mode-menu-def)
4b1aaa8b
PE
9242 (easy-menu-define idlwave-mode-debug-menu idlwave-mode-map
9243 "IDL and WAVE CL editing menu"
f32b3b91
CD
9244 idlwave-mode-debug-menu-def)))
9245
9246(defun idlwave-customize ()
5a0c3f56 9247 "Call the customize function with `idlwave' as argument."
f32b3b91 9248 (interactive)
4b1aaa8b 9249 ;; Try to load the code for the shell, so that we can customize it
f32b3b91 9250 ;; as well.
22d5821d
CD
9251 (or (featurep 'idlw-shell)
9252 (load "idlw-shell" t))
f32b3b91
CD
9253 (customize-browse 'idlwave))
9254
9255(defun idlwave-create-customize-menu ()
9256 "Create a full customization menu for IDLWAVE, insert it into the menu."
9257 (interactive)
9258 (if (fboundp 'customize-menu-create)
9259 (progn
4b1aaa8b 9260 ;; Try to load the code for the shell, so that we can customize it
f32b3b91 9261 ;; as well.
22d5821d
CD
9262 (or (featurep 'idlw-shell)
9263 (load "idlw-shell" t))
4b1aaa8b 9264 (easy-menu-change
f32b3b91
CD
9265 '("IDLWAVE") "Customize"
9266 `(["Browse IDLWAVE group" idlwave-customize t]
9267 "--"
9268 ,(customize-menu-create 'idlwave)
9269 ["Set" Custom-set t]
9270 ["Save" Custom-save t]
9271 ["Reset to Current" Custom-reset-current t]
9272 ["Reset to Saved" Custom-reset-saved t]
9273 ["Reset to Standard Settings" Custom-reset-standard t]))
9274 (message "\"IDLWAVE\"-menu now contains full customization menu"))
9275 (error "Cannot expand menu (outdated version of cus-edit.el)")))
9276
9277(defun idlwave-show-commentary ()
9278 "Use the finder to view the file documentation from `idlwave.el'."
9279 (interactive)
f32b3b91
CD
9280 (finder-commentary "idlwave.el"))
9281
9282(defun idlwave-shell-show-commentary ()
595ab50b 9283 "Use the finder to view the file documentation from `idlw-shell.el'."
f32b3b91 9284 (interactive)
595ab50b 9285 (finder-commentary "idlw-shell.el"))
f32b3b91
CD
9286
9287(defun idlwave-info ()
9288 "Read documentation for IDLWAVE in the info system."
9289 (interactive)
d6a277d0 9290 (info "idlwave"))
f32b3b91
CD
9291
9292(defun idlwave-list-abbrevs (arg)
9293 "Show the code abbreviations define in IDLWAVE mode.
9294This lists all abbrevs where the replacement text differs from the input text.
9295These are the ones the users want to learn to speed up their writing.
9296
9297The function does *not* list abbrevs which replace a word with itself
9298to call a hook. These hooks are used to change the case of words or
9299to blink the matching `begin', and the user does not need to know them.
9300
9301With arg, list all abbrevs with the corresponding hook.
9302
9303This function was written since `list-abbrevs' looks terrible for IDLWAVE mode."
9304
9305 (interactive "P")
9306 (let ((table (symbol-value 'idlwave-mode-abbrev-table))
9307 abbrevs
9308 str rpl func fmt (len-str 0) (len-rpl 0))
4b1aaa8b 9309 (mapatoms
f32b3b91
CD
9310 (lambda (sym)
9311 (if (symbol-value sym)
9312 (progn
9313 (setq str (symbol-name sym)
9314 rpl (symbol-value sym)
9315 func (symbol-function sym))
9316 (if arg
9317 (setq func (prin1-to-string func))
9318 (if (and (listp func) (stringp (nth 2 func)))
9319 (setq rpl (concat "EVAL: " (nth 2 func))
9320 func "")
9321 (setq func "")))
9322 (if (or arg (not (string= rpl str)))
9323 (progn
9324 (setq len-str (max len-str (length str)))
9325 (setq len-rpl (max len-rpl (length rpl)))
9326 (setq abbrevs (cons (list str rpl func) abbrevs)))))))
9327 table)
9328 ;; sort the list
9329 (setq abbrevs (sort abbrevs (lambda (a b) (string< (car a) (car b)))))
9330 ;; Make the format
9331 (setq fmt (format "%%-%ds %%-%ds %%s\n" len-str len-rpl))
9332 (with-output-to-temp-buffer "*Help*"
9333 (if arg
9334 (progn
4b1aaa8b 9335 (princ "Abbreviations and Actions in IDLWAVE-Mode\n")
f32b3b91
CD
9336 (princ "=========================================\n\n")
9337 (princ (format fmt "KEY" "REPLACE" "HOOK"))
9338 (princ (format fmt "---" "-------" "----")))
9339 (princ "Code Abbreviations and Templates in IDLWAVE-Mode\n")
9340 (princ "================================================\n\n")
9341 (princ (format fmt "KEY" "ACTION" ""))
9342 (princ (format fmt "---" "------" "")))
9343 (mapcar
9344 (lambda (list)
9345 (setq str (car list)
9346 rpl (nth 1 list)
9347 func (nth 2 list))
9348 (princ (format fmt str rpl func)))
9349 abbrevs)))
9350 ;; Make sure each abbreviation uses only one display line
9a529312 9351 (with-current-buffer "*Help*"
f32b3b91
CD
9352 (setq truncate-lines t)))
9353
5e72c6b2
S
9354;; Add .pro files to speedbar for support, if it's loaded
9355(eval-after-load "speedbar" '(speedbar-add-supported-extension ".pro"))
9356
5e72c6b2
S
9357;; Set an idle timer to load the routine info.
9358;; Will only work on systems which support this.
9359(or idlwave-routines (idlwave-start-load-rinfo-timer))
9360
15e42531 9361;; Run the hook
f32b3b91
CD
9362(run-hooks 'idlwave-load-hook)
9363
9364(provide 'idlwave)
9365
8df608c1 9366;; arch-tag: f77f3b0c-c37c-424f-a328-0886fd42b6fb
f32b3b91 9367;;; idlwave.el ends here