Initial checkin of HTML help support module.
[bpt/emacs.git] / lisp / progmodes / idlwave.el
CommitLineData
52a244eb
S
1;; idlwave.el --- IDL editing mode for GNU Emacs
2;; Copyright (c) 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation
f32b3b91 3
52a244eb
S
4;; Authors: J.D. Smith <jdsmith@as.arizona.edu>
5;; Carsten Dominik <dominik@astro.uva.nl>
6;; Chris Chase <chase@att.com>
5e72c6b2 7;; Maintainer: J.D. Smith <jdsmith@as.arizona.edu>
52a244eb
S
8;; Version: 5.5
9;; Date: $Date: 2004/11/17 05:37:18 $
f32b3b91
CD
10;; Keywords: languages
11
e8af40ee 12;; This file is part of GNU Emacs.
f32b3b91
CD
13
14;; GNU Emacs is free software; you can redistribute it and/or modify
15;; it under the terms of the GNU General Public License as published by
16;; the Free Software Foundation; either version 2, or (at your option)
17;; any later version.
18
19;; GNU Emacs is distributed in the hope that it will be useful,
20;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22;; GNU General Public License for more details.
23
24;; You should have received a copy of the GNU General Public License
25;; along with GNU Emacs; see the file COPYING. If not, write to the
26;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27;; Boston, MA 02111-1307, USA.
28
29;;; Commentary:
30
52a244eb
S
31;; In the remotely distant past, based on pascal.el, though bears
32;; little resemblance to it now.
f32b3b91
CD
33;;
34;; Incorporates many ideas, such as abbrevs, action routines, and
35;; continuation line indenting, from wave.el.
36;; wave.el original written by Lubos Pochman, Precision Visuals, Boulder.
37;;
38;; See the mode description ("C-h m" in idlwave-mode or "C-h f idlwave-mode")
39;; for features, key bindings, and info.
40;; Also, Info format documentation is available with `M-x idlwave-info'
41;;
5e72c6b2
S
42;; New versions of IDLWAVE, documentation, and more information
43;; available from:
44;; http://idlwave.org
f32b3b91
CD
45;;
46;; INSTALLATION
47;; ============
48;;
49;; Follow the instructions in the INSTALL file of the distribution.
50;; In short, put this file on your load path and add the following
51;; lines to your .emacs file:
52;;
53;; (autoload 'idlwave-mode "idlwave" "IDLWAVE Mode" t)
8c7b4ec8 54;; (autoload 'idlwave-shell "idlw-shell" "IDLWAVE Shell" t)
f32b3b91
CD
55;; (setq auto-mode-alist (cons '("\\.pro\\'" . idlwave-mode) auto-mode-alist))
56;;
57;;
58;; SOURCE
59;; ======
60;;
76959b77 61;; The newest version of this file is available from the maintainer's
52a244eb 62;; Webpage:
f32b3b91 63;;
5e72c6b2 64;; http://idlwave.org
f32b3b91
CD
65;;
66;; DOCUMENTATION
67;; =============
68;;
52a244eb
S
69;; IDLWAVE is documented online in info format. A printable version
70;; of the documentation is available from the maintainers webpage (see
71;; SOURCE).
775591f7 72;;
52a244eb 73;;
f32b3b91
CD
74;; ACKNOWLEDGMENTS
75;; ===============
76;;
77;; Thanks to the following people for their contributions and comments:
78;;
52a244eb
S
79;; Ulrik Dickow <dickow_at_nbi.dk>
80;; Eric E. Dors <edors_at_lanl.gov>
81;; Stein Vidar H. Haugan <s.v.h.haugan_at_astro.uio.no>
82;; David Huenemoerder <dph_at_space.mit.edu>
83;; Kevin Ivory <Kevin.Ivory_at_linmpi.mpg.de>
84;; Dick Jackson <dick_at_d-jackson.com>
85;; Xuyong Liu <liu_at_stsci.edu>
86;; Simon Marshall <Simon.Marshall_at_esrin.esa.it>
87;; Laurent Mugnier <mugnier_at_onera.fr>
88;; Lubos Pochman <lubos_at_rsinc.com>
89;; Bob Portmann <portmann_at_al.noaa.gov>
90;; Patrick M. Ryan <pat_at_jaameri.gsfc.nasa.gov>
91;; Marty Ryba <ryba_at_ll.mit.edu>
92;; Paul Sorenson <aardvark62_at_msn.com>
93;; Phil Sterne <sterne_at_dublin.llnl.gov>
94;; Phil Williams <williams_at_irc.chmcc.org>
f32b3b91
CD
95;;
96;; CUSTOMIZATION:
97;; =============
98;;
52a244eb
S
99;; IDLWAVE has extensive customize support; to learn about the
100;; variables which control the mode's behavior, use `M-x
101;; idlwave-customize'.
f32b3b91
CD
102;;
103;; You can set your own preferred values with Customize, or with Lisp
104;; code in .emacs. For an example of what to put into .emacs, check
52a244eb
S
105;; the TexInfo documentation or see a complete .emacs available at the
106;; website.
f32b3b91
CD
107;;
108;; KNOWN PROBLEMS:
109;; ==============
110;;
76959b77
S
111;; IDLWAVE support for the IDL-derived PV-WAVE CL language of Visual
112;; Numerics, Inc. is growing less and less complete as the two
113;; languages grow increasingly apart. The mode probably shouldn't
52a244eb
S
114;; even have "WAVE" in it's title, but it's catchy, and was required
115;; to avoid conflict with the CORBA idl.el mode. Caveat WAVEor.
76959b77 116;;
f32b3b91
CD
117;; Moving the point backwards in conjunction with abbrev expansion
118;; does not work as I would like it, but this is a problem with
119;; emacs abbrev expansion done by the self-insert-command. It ends
120;; up inserting the character that expanded the abbrev after moving
121;; point backward, e.g., "\cl" expanded with a space becomes
122;; "LONG( )" with point before the close paren. This is solved by
52a244eb 123;; using a temporary function in `post-command-hook' - not pretty,
595ab50b 124;; but it works.
f32b3b91
CD
125;;
126;; Tabs and spaces are treated equally as whitespace when filling a
127;; comment paragraph. To accomplish this, tabs are permanently
128;; replaced by spaces in the text surrounding the paragraph, which
129;; may be an undesirable side-effect. Replacing tabs with spaces is
130;; limited to comments only and occurs only when a comment
131;; paragraph is filled via `idlwave-fill-paragraph'.
132;;
52a244eb
S
133;; Muti-statement lines (using "&") on block begin and end lines can
134;; ruin the formatting. For example, multiple end statements on a
135;; line: endif & endif. Using "&" outside of block begin/end lines
136;; should be okay.
f32b3b91 137;;
76959b77
S
138;; Determining the expression at point for printing and other
139;; examination commands is somewhat rough: currently only fairly
140;; simple entities are found. You can always drag-select or examine
52a244eb 141;; a pre-selected region.
f32b3b91 142;;
f32b3b91
CD
143;; When forcing completion of method keywords, the initial
144;; query for a method has multiple entries for some methods. Would
595ab50b 145;; be too difficult to fix this hardly used case.
f32b3b91
CD
146;;
147\f
148;;; Code:
149
52a244eb 150
f32b3b91 151(eval-when-compile (require 'cl))
52a244eb
S
152(require 'idlw-help)
153
154;; For XEmacs
155(unless (fboundp 'line-beginning-position)
156 (defalias 'line-beginning-position 'point-at-bol))
157(unless (fboundp 'line-end-position)
158 (defalias 'line-end-position 'point-at-eol))
159(unless (fboundp 'char-valid-p)
160 (defalias 'char-valid-p 'characterp))
f32b3b91
CD
161
162(eval-and-compile
5e72c6b2
S
163 ;; Kludge to allow `defcustom' for Emacs 19.
164 (condition-case () (require 'custom) (error nil))
165 (if (and (featurep 'custom) (fboundp 'custom-declare-variable))
166 nil ;; We've got what we needed
167 ;; We have the old or no custom-library, hack around it!
168 (defmacro defgroup (&rest args) nil)
52a244eb 169 (defmacro defcustom (var value doc &rest args)
5e72c6b2 170 `(defvar ,var ,value ,doc))))
f32b3b91
CD
171
172(defgroup idlwave nil
5e72c6b2 173 "Major mode for editing IDL .pro files"
f32b3b91 174 :tag "IDLWAVE"
52a244eb 175 :link '(url-link :tag "Home Page"
5e72c6b2 176 "http://idlwave.org")
595ab50b
CD
177 :link '(emacs-commentary-link :tag "Commentary in idlw-shell.el"
178 "idlw-shell.el")
f32b3b91
CD
179 :link '(emacs-commentary-link :tag "Commentary in idlwave.el" "idlwave.el")
180 :link '(custom-manual "(idlwave)Top")
181 :prefix "idlwave"
182 :group 'languages)
183
52a244eb 184
f32b3b91
CD
185;;; Variables for indentation behavior ---------------------------------------
186
187(defgroup idlwave-code-formatting nil
188 "Indentation and formatting options for IDLWAVE mode."
189 :group 'idlwave)
190
191(defcustom idlwave-main-block-indent 0
192 "*Extra indentation for the main block of code.
193That is the block between the FUNCTION/PRO statement and the END
194statement for that program unit."
195 :group 'idlwave-code-formatting
196 :type 'integer)
197
198(defcustom idlwave-block-indent 4
199 "*Extra indentation applied to block lines.
200If you change this, you probably also want to change `idlwave-end-offset'."
201 :group 'idlwave-code-formatting
202 :type 'integer)
203
204(defcustom idlwave-end-offset -4
205 "*Extra indentation applied to block END lines.
206A value equal to negative `idlwave-block-indent' will make END lines
207line up with the block BEGIN lines."
208 :group 'idlwave-code-formatting
209 :type 'integer)
210
211(defcustom idlwave-continuation-indent 2
212 "*Extra indentation applied to continuation lines.
213This extra offset applies to the first of a set of continuation lines.
5e72c6b2
S
214The following lines receive the same indentation as the first."
215 :group 'idlwave-code-formatting
216 :type 'integer)
217
218(defcustom idlwave-max-extra-continuation-indent 20
219 "*Maximum additional indentation for special continuation indent.
220Several special indentations are tried to help line up continuation
221lines in routine calls or definitions, other statements with
134b6671 222parentheses, or assignment statements. This variable specifies a
5e72c6b2
S
223maximum amount by which this special indentation can exceed the
224standard continuation indentation, otherwise defaulting to a fixed
225offset. Set to 0 to effectively disable all special continuation
226indentation, or to a large number (like 100) to enable it in all
52a244eb 227cases. See also `idlwave-indent-to-open-paren', which can override
5e72c6b2 228this variable."
f32b3b91
CD
229 :group 'idlwave-code-formatting
230 :type 'integer)
231
5e72c6b2
S
232(defcustom idlwave-indent-to-open-paren t
233 "*Non-nil means, indent continuation lines to innermost open
234parenthesis. This indentation occurs even if otherwise disallowed by
235`idlwave-max-extra-continuation-indent'. Matching parens and the
236interleaving args are lined up. Example:
237
238 x = function_a(function_b(function_c( a, b, [1,2,3, $
239 4,5,6 $
240 ], $
241 c, d $
242 )))
243
244When this variable is nil, paren alignment may still occur, based on
245the value of `max-extra-continuation-indent', which, if zero, would
246yield:
247
248 x = function_a(function_b(function_c( a, b, [1,2,3, $
249 4,5,6 $
250 ], $
251 c, d $
252 )))"
253 :group 'idlwave-code-formatting
254 :type 'boolean)
255
52a244eb
S
256(defcustom idlwave-indent-parens-nested nil
257 "*Non-nil means, indent continuation lines with parens by nesting
258lines at consecutively deeper levels."
259 :group 'idlwave-code-formatting
260 :type 'boolean)
261
262
f32b3b91
CD
263(defcustom idlwave-hanging-indent t
264 "*If set non-nil then comment paragraphs are indented under the
265hanging indent given by `idlwave-hang-indent-regexp' match in the first line
266of the paragraph."
267 :group 'idlwave-code-formatting
268 :type 'boolean)
269
270(defcustom idlwave-hang-indent-regexp "- "
271 "*Regular expression matching the position of the hanging indent
272in the first line of a comment paragraph. The size of the indent
273extends to the end of the match for the regular expression."
274 :group 'idlwave-code-formatting
275 :type 'regexp)
276
277(defcustom idlwave-use-last-hang-indent nil
278 "*If non-nil then use last match on line for `idlwave-indent-regexp'."
279 :group 'idlwave-code-formatting
280 :type 'boolean)
281
282(defcustom idlwave-fill-comment-line-only t
283 "*If non-nil then auto fill will only operate on comment lines."
284 :group 'idlwave-code-formatting
285 :type 'boolean)
286
287(defcustom idlwave-auto-fill-split-string t
288 "*If non-nil then auto fill will split strings with the IDL `+' operator.
52a244eb
S
289When the line end falls within a string, string concatenation with the
290'+' operator will be used to distribute a long string over lines.
f32b3b91
CD
291If nil and a string is split then a terminal beep and warning are issued.
292
293This variable is ignored when `idlwave-fill-comment-line-only' is
294non-nil, since in this case code is not auto-filled."
295 :group 'idlwave-code-formatting
296 :type 'boolean)
297
298(defcustom idlwave-split-line-string t
299 "*If non-nil then `idlwave-split-line' will split strings with `+'.
300When the splitting point of a line falls inside a string, split the string
301using the `+' string concatenation operator. If nil and a string is
302split then a terminal beep and warning are issued."
303 :group 'idlwave-code-formatting
304 :type 'boolean)
305
306(defcustom idlwave-no-change-comment ";;;"
307 "*The indentation of a comment that starts with this regular
308expression will not be changed. Note that the indentation of a comment
309at the beginning of a line is never changed."
310 :group 'idlwave-code-formatting
311 :type 'string)
312
313(defcustom idlwave-begin-line-comment nil
314 "*A comment anchored at the beginning of line.
315A comment matching this regular expression will not have its
316indentation changed. If nil the default is \"^;\", i.e., any line
317beginning with a \";\". Expressions for comments at the beginning of
318the line should begin with \"^\"."
319 :group 'idlwave-code-formatting
320 :type '(choice (const :tag "Any line beginning with `;'" nil)
321 'regexp))
322
323(defcustom idlwave-code-comment ";;[^;]"
324 "*A comment that starts with this regular expression on a line by
325itself is indented as if it is a part of IDL code. As a result if
326the comment is not preceded by whitespace it is unchanged."
327 :group 'idlwave-code-formatting
328 :type 'regexp)
329
330;; Comments not matching any of the above will be indented as a
331;; right-margin comment, i.e., to a minimum of `comment-column'.
332
f32b3b91
CD
333;;; Routine Info and Completion ---------------------------------------
334
15e42531
CD
335(defgroup idlwave-routine-info nil
336 "Routine Info options for IDLWAVE mode."
f32b3b91
CD
337 :group 'idlwave)
338
52a244eb
S
339(defcustom idlwave-use-library-catalogs t
340 "*Non-nil means search the IDL path for library catalog files.
341
342These files, named .idlwave_catalog, document routine information for
343individual directories and libraries of IDL .pro files. Many popular
344libraries come with catalog files by default, so leaving this on is a
345usually a good idea.."
346 :group 'idlwave-routine-info
347 :type 'boolean)
5e72c6b2
S
348
349(defcustom idlwave-init-rinfo-when-idle-after 10
350 "*Seconds of idle time before routine info is automatically initialized.
351Initializing the routine info can take long, in particular if a large
352library catalog is involved. When Emacs is idle for more than the number
353of seconds specified by this variable, it starts the initialization.
52a244eb 354The process is split into five steps, in order to keep possible work
5e72c6b2
S
355interruption as short as possible. If one of the steps finishes, and no
356user input has arrived in the mean time, initialization proceeds immediately
357to the next step.
358A good value for this variable is about 1/3 of the time initialization
359take in you setup. So if you have a fast machine and no problems with a slow network connection, don't hesitate to set this to 2 seconds.
360A Value of 0 means, don't initialize automatically."
361 :group 'idlwave-routine-info
362 :type 'number)
363
f32b3b91 364(defcustom idlwave-scan-all-buffers-for-routine-info t
15e42531
CD
365 "*Non-nil means, scan buffers for IDL programs when updating info.
366The scanning is done by the command `idlwave-update-routine-info'.
367The following values are allowed:
368
369nil Don't scan any buffers.
370t Scan all idlwave-mode buffers in the current editing session.
371current Scan only the current buffer, but no other buffers."
372 :group 'idlwave-routine-info
373 :type '(choice
374 (const :tag "No buffer" nil)
375 (const :tag "All buffers" t)
376 (const :tag "Current buffer only" 'current)))
f32b3b91
CD
377
378(defcustom idlwave-query-shell-for-routine-info t
379 "*Non-nil means query the shell for info about compiled routines.
380Querying the shell is useful to get information about compiled modules,
381and it is turned on by default. However, when you have a complete library
382scan, this is not necessary."
15e42531 383 :group 'idlwave-routine-info
f32b3b91
CD
384 :type 'boolean)
385
15e42531
CD
386(defcustom idlwave-auto-routine-info-updates
387 '(find-file save-buffer kill-buffer compile-buffer)
388 "*Controls under what circumstances routine info is updated automatically.
389Possible values:
390nil Never
391t All available
facebc7b 392\(...) A list of circumstances. Allowed members are:
15e42531
CD
393 find-file Add info for new IDLWAVE buffers.
394 save-buffer Update buffer info when buffer is saved
395 kill-buffer Remove buffer info when buffer gets killed
396 compile-buffer Update shell info after `idlwave-shell-save-and...'"
397 :group 'idlwave-routine-info
398 :type '(choice
399 (const :tag "Never" nil)
400 (const :tag "As often as possible" t)
401 (set :tag "Checklist" :greedy t
402 (const :tag "When visiting a file" find-file)
403 (const :tag "When saving a buffer" save-buffer)
404 (const :tag "After a buffer was killed" kill-buffer)
405 (const :tag "After a buffer was compiled successfully, update shell info" compile-buffer))))
52a244eb 406
15e42531
CD
407(defcustom idlwave-rinfo-max-source-lines 5
408 "*Maximum number of source files displayed in the Routine Info window.
409When an integer, it is the maximum number of source files displayed.
410t means to show all source files."
411 :group 'idlwave-routine-info
412 :type 'integer)
413
f32b3b91 414(defcustom idlwave-library-path nil
52a244eb
S
415 "Library path for Windows and MacOS. Not needed under Unix. When
416selecting the directories to scan for IDL user catalog routine info,
417IDLWAVE can, under UNIX, query the shell for the exact search path
418\(the value of !PATH). However, under Windows and MacOS (pre-OSX),
419the IDLWAVE shell does not work. In this case, this variable can be
420set to specify the paths where IDLWAVE can find PRO files. The shell
421will only be asked for a list of paths when this variable is nil. The
422value is a list of directories. A directory preceeded by a `+' will
423be searched recursively. If you set this variable on a UNIX system,
424the shell will not be queried. See also `idlwave-system-directory'."
15e42531 425 :group 'idlwave-routine-info
f32b3b91
CD
426 :type '(repeat (directory)))
427
15e42531 428(defcustom idlwave-system-directory ""
52a244eb
S
429 "The IDL system directory for Windows and MacOS. Not needed under
430UNIX. Set this to the value of the `!DIR' system variable in IDL.
431IDLWAVE uses this to find out which of the library routines belong to
432the official system library. All files inside the `lib' subdirectory
433are considered system library files - so don't install private stuff
434in this directory. On UNIX systems, IDLWAVE queries the shell for the
435value of `!DIR'. See also `idlwave-library-path'."
15e42531
CD
436 :group 'idlwave-routine-info
437 :type 'directory)
438
52a244eb
S
439(defcustom idlwave-config-directory
440 (convert-standard-filename "~/.idlwave")
441 "*Directory for configuration files and user-library catalog."
15e42531 442 :group 'idlwave-routine-info
f32b3b91
CD
443 :type 'file)
444
52a244eb
S
445(defvar idlwave-user-catalog-file "idlusercat.el")
446(defvar idlwave-path-file "idlpath.el")
447
448(defvar idlwave-libinfo-file nil
449 "*Obsolete variable, no longer used.")
450
15e42531
CD
451(defcustom idlwave-special-lib-alist nil
452 "Alist of regular expressions matching special library directories.
453When listing routine source locations, IDLWAVE gives a short hint where
52a244eb 454the file defining the routine is located. By default it lists `SystemLib'
15e42531
CD
455for routines in the system library `!DIR/lib' and `Library' for anything
456else. This variable can define additional types. The car of each entry
457is a regular expression matching the file name (they normally will match
458on the path). The cdr is the string to be used as identifier. Max 10
459chars are allowed."
460 :group 'idlwave-routine-info
461 :type '(repeat
462 (cons regexp string)))
463
52a244eb
S
464(defcustom idlwave-auto-write-paths t
465 "Write out path (!PATH) and system directory (!DIR) info automatically.
466Path info is needed to locate library catalog files. If non-nil,
467whenever the path-list changes as a result of shell-query, etc., it is
468written to file. Otherwise, the menu option \"Write Paths\" can be
469used to force a write."
470 :group 'idlwave-routine-info
05a1abfc 471 :type 'boolean)
775591f7 472
15e42531
CD
473(defgroup idlwave-completion nil
474 "Completion options for IDLWAVE mode."
475 :prefix "idlwave"
476 :group 'idlwave)
477
f32b3b91
CD
478(eval-and-compile
479 (defconst idlwave-tmp
480 '(choice :tag "by applying the function"
481 (const upcase)
482 (const downcase)
483 (const capitalize)
484 (const preserve)
485 (symbol :tag "Other"))))
486
f32b3b91
CD
487(defcustom idlwave-completion-case '((routine . upcase)
488 (keyword . upcase)
489 (class . preserve)
490 (method . preserve))
491 "Association list setting the case of completed words.
492
493This variable determines the case (UPPER/lower/Capitalized...) of
494words inserted into the buffer by completion. The preferred case can
495be specified separately for routine names, keywords, classes and
52a244eb 496methods.
f32b3b91
CD
497This alist should therefore have entries for `routine' (normal
498functions and procedures, i.e. non-methods), `keyword', `class', and
499`method'. Plausible values are
500
501upcase upcase whole word, like `BOX_CURSOR'
502downcase downcase whole word, like `read_ppm'
503capitalize capitalize each part, like `Widget_Control'
504preserve preserve case as is, like `IDLgrView'
505
506The value can also be any Emacs Lisp function which transforms the
507case of characters in a string.
508
509A value of `preserve' means that the case of the completed word is
510identical to the way it was written in the definition statement of the
511routine. This was implemented to allow for mixed-case completion, in
512particular of object classes and methods.
513If a completable word is defined in multiple locations, the meaning of
514`preserve' is not unique since the different definitions might be
515cased differently. Therefore IDLWAVE always takes the case of the
516*first* definition it encounters during routine info collection and
517uses the case derived from it consistently.
518
519Note that a lowercase-only string in the buffer will always be completed in
520lower case (but see the variable `idlwave-completion-force-default-case').
521
522After changing this variable, you need to either restart Emacs or press
523`C-u C-c C-i' to update the internal lists."
15e42531 524 :group 'idlwave-completion
f32b3b91
CD
525 :type `(repeat
526 (cons (symbol :tag "Derive completion case for")
527 ,idlwave-tmp)))
528
529(defcustom idlwave-completion-force-default-case nil
530 "*Non-nil means, completion will always honor `idlwave-completion-case'.
531When nil, only the completion of a mixed case or upper case string
532will honor the default settings in `idlwave-completion-case', while
533the completion of lower case strings will be completed entirely in
534lower case."
15e42531 535 :group 'idlwave-completion
f32b3b91
CD
536 :type 'boolean)
537
538(defcustom idlwave-complete-empty-string-as-lower-case nil
539 "*Non-nil means, the empty string is considered downcase for completion.
540The case of what is already in the buffer determines the case of completions.
541When this variable is non-nil, the empty string is considered to be downcase.
542Completing on the empty string then offers downcase versions of the possible
543completions."
15e42531 544 :group 'idlwave-completion
f32b3b91
CD
545 :type 'boolean)
546
547(defvar idlwave-default-completion-case-is-down nil
548 "Obsolete variable. See `idlwave-complete-empty-string-as-lower-case' and
549`idlwave-completion-case'.")
550
551(defcustom idlwave-buffer-case-takes-precedence nil
552 "*Non-nil means, the case of tokens in buffers dominates over system stuff.
553To make this possible, we need to re-case everything each time we update
554the routine info from the buffers. This is slow.
555The default is to consider the case given in the system and library files
556first which makes updating much faster."
15e42531
CD
557 :group 'idlwave-completion
558 :type 'boolean)
559
560(defcustom idlwave-highlight-help-links-in-completion t
561 "*Non-nil means, highlight completions for which system help is available.
562Help can then be accessed with mouse-3.
563This option is only effective when the online help system is installed."
564 :group 'idlwave-completion
f32b3b91
CD
565 :type 'boolean)
566
05a1abfc
CD
567(defcustom idlwave-support-inheritance t
568 "Non-nil means, treat inheritance with completion, online help etc.
cef6cafe 569When nil, IDLWAVE only knows about the native methods and tags of a class,
05a1abfc
CD
570not about inherited ones."
571 :group 'idlwave-routine-info
572 :type 'boolean)
573
5e72c6b2
S
574(defcustom idlwave-keyword-class-inheritance '("^[gs]etproperty$" "^init$")
575 "List of regular expressions for class-driven keyword inheritance.
576Keyword inheritance is often tied to class inheritance by \"chaining\"
577up the class tree. While it cannot be assumed that the presence of an
578_EXTRA or _REF_EXTRA symbol guarantees such chaining will occur, for
579certain methods this assumption is almost always true. The methods
580for which to assume this can be set here."
581 :group 'idlwave-routine-info
582 :type '(repeat (regexp :tag "Match method:")))
52a244eb 583
5e72c6b2 584
f32b3b91
CD
585(defcustom idlwave-completion-show-classes 1
586 "*Number of classes to show when completing object methods and keywords.
587When completing methods or keywords for an object with unknown class,
588the *Completions* buffer will show the legal classes for each completion
589like this:
590
591MyMethod <Class1,Class2,Class3>
592
593The value of this variable may be nil to inhibit display, or an integer to
594indicate the maximum number of classes to display.
595
596On XEmacs, a full list of classes will also be placed into a `help-echo'
597property on the competion items, so that the list of classes for the current
598item is displayed in the echo area. If the value of this variable is a
599negative integer, the `help-echo' property will be suppressed."
15e42531 600 :group 'idlwave-completion
f32b3b91
CD
601 :type '(choice (const :tag "Don't show" nil)
602 (integer :tag "Number of classes shown" 1)))
603
604(defcustom idlwave-completion-fontify-classes t
605 "*Non-nil means, fontify the classes in completions buffer.
606This makes it easier to distinguish the completion items from the extra
607class info listed. See `idlwave-completion-show-classes'."
15e42531 608 :group 'idlwave-completion
f32b3b91
CD
609 :type 'boolean)
610
611(defcustom idlwave-query-class '((method-default . nil)
612 (keyword-default . nil))
613 "Association list governing specification of object classes for completion.
614
5e72c6b2
S
615When IDLWAVE tries to complete object-oriented methods, it usually
616cannot determine the class of a given object from context. In order
617to provide the user with a correct list of methods or keywords, it
76959b77
S
618needs to determine the appropriate class. IDLWAVE has two ways of
619doing this (well, three ways if you count the shell... see
620`idlwave-shell-query-for-class'):
621
6221. Combine the items of all available classes which contain this
623 method for the purpose of completion. So when completing a method,
624 all methods of all known classes are available, and when completing
625 a keyword, all keywords allowed for this method in any class are
626 shown. This behavior is very much like normal completion and is
627 therefore the default. It works much better than one might think -
628 only for the INIT, GETPROPERTY and SETPROPERTY the keyword lists
629 become uncomfortably long. See also
5e72c6b2 630 `idlwave-completion-show-classes'.
f32b3b91
CD
631
6322. The second possibility is to ask the user on each occasion. To
633 make this less interruptive, IDLWAVE can store the class as a text
634 property on the object operator `->'. For a given object in the
635 source code, class selection will then be needed only once
636 - for example to complete the method. Keywords to the method can
637 then be completed directly, because the class is already known.
638 You will have to turn on the storage of the selected class
639 explicitly with the variable `idlwave-store-inquired-class'.
640
5e72c6b2
S
641This variable allows you to configure IDLWAVE's method and
642method-keyword completion behavior. Its value is an alist, which
643should contain at least two elements: (method-default . VALUE) and
facebc7b 644\(keyword-default . VALUE), where VALUE is either t or nil. These
5e72c6b2
S
645specify if the class should be found during method and keyword
646completion, respectively.
f32b3b91 647
52a244eb 648The alist may have additional entries specifying exceptions from the
f32b3b91
CD
649keyword completion rule for specific methods, like INIT or
650GETPROPERTY. In order to turn on class specification for the INIT
651method, add an entry (\"INIT\" . t). The method name must be ALL-CAPS."
15e42531 652 :group 'idlwave-completion
f32b3b91
CD
653 :type '(list
654 (cons (const method-default)
655 (boolean :tag "Determine class when completing METHODS "))
656 (cons (const keyword-default)
657 (boolean :tag "Determine class when completing KEYWORDS "))
658 (repeat
659 :tag "Exceptions to defaults"
660 :inline t
661 (cons (string :tag "MODULE" :value "")
662 (boolean :tag "Determine class for this method")))))
663
664(defcustom idlwave-store-inquired-class nil
665 "*Non-nil means, store class of a method call as text property on `->'.
666IDLWAVE sometimes has to ask the user for the class associated with a
667particular object method call. This happens during the commands
668`idlwave-routine-info' and `idlwave-complete', depending upon the
669value of the variable `idlwave-query-class'.
670
671When you specify a class, this information can be stored as a text
52a244eb 672property on the `->' arrow in the source code, so that during the same
f32b3b91
CD
673editing session, IDLWAVE will not have to ask again. When this
674variable is non-nil, IDLWAVE will store and reuse the class information.
675The class stored can be checked and removed with `\\[idlwave-routine-info]'
676on the arrow.
677
678The default of this variable is nil, since the result of commands then
679is more predictable. However, if you know what you are doing, it can
680be nice to turn this on.
681
682An arrow which knows the class will be highlighted with
683`idlwave-class-arrow-face'. The command \\[idlwave-routine-info]
684displays (with prefix arg: deletes) the class stored on the arrow
685at point."
15e42531 686 :group 'idlwave-completion
f32b3b91
CD
687 :type 'boolean)
688
689(defcustom idlwave-class-arrow-face 'bold
690 "*Face to highlight object operator arrows `->' which carry a class property.
691When IDLWAVE stores a class name as text property on an object arrow
facebc7b 692\(see variable `idlwave-store-inquired-class', it highlights the arrow
f32b3b91 693with this font in order to remind the user that this arrow is special."
15e42531 694 :group 'idlwave-completion
f32b3b91
CD
695 :type 'symbol)
696
697(defcustom idlwave-resize-routine-help-window t
698 "*Non-nil means, resize the Routine-info *Help* window to fit the content."
15e42531 699 :group 'idlwave-completion
f32b3b91
CD
700 :type 'boolean)
701
702(defcustom idlwave-keyword-completion-adds-equal t
703 "*Non-nil means, completion automatically adds `=' after completed keywords."
15e42531 704 :group 'idlwave-completion
f32b3b91
CD
705 :type 'boolean)
706
707(defcustom idlwave-function-completion-adds-paren t
708 "*Non-nil means, completion automatically adds `(' after completed function.
0ff9b955 709nil means, don't add anything.
f32b3b91
CD
710A value of `2' means, also add the closing parenthesis and position cursor
711between the two."
15e42531 712 :group 'idlwave-completion
f32b3b91
CD
713 :type '(choice (const :tag "Nothing" nil)
714 (const :tag "(" t)
715 (const :tag "()" 2)))
716
717(defcustom idlwave-completion-restore-window-configuration t
718 "*Non-nil means, try to restore the window configuration after completion.
719When completion is not unique, Emacs displays a list of completions.
720This messes up your window configuration. With this variable set, IDLWAVE
721restores the old configuration after successful completion."
15e42531 722 :group 'idlwave-completion
f32b3b91
CD
723 :type 'boolean)
724
725;;; Variables for abbrev and action behavior -----------------------------
726
727(defgroup idlwave-abbrev-and-indent-action nil
728 "IDLWAVE performs actions when expanding abbreviations or indenting lines.
729The variables in this group govern this."
730 :group 'idlwave)
731
732(defcustom idlwave-do-actions nil
733 "*Non-nil means performs actions when indenting.
734The actions that can be performed are listed in `idlwave-indent-action-table'."
735 :group 'idlwave-abbrev-and-indent-action
736 :type 'boolean)
737
738(defcustom idlwave-abbrev-start-char "\\"
739 "*A single character string used to start abbreviations in abbrev mode.
740Possible characters to chose from: ~`\%
741or even '?'. '.' is not a good choice because it can make structure
742field names act like abbrevs in certain circumstances.
743
744Changes to this in `idlwave-mode-hook' will have no effect. Instead a user
745must set it directly using `setq' in the .emacs file before idlwave.el
746is loaded."
747 :group 'idlwave-abbrev-and-indent-action
748 :type 'string)
749
750(defcustom idlwave-surround-by-blank nil
751 "*Non-nil means, enable `idlwave-surround'.
595ab50b 752If non-nil, `=',`<',`>',`&',`,', `->' are surrounded with spaces by
f32b3b91
CD
753`idlwave-surround'.
754See help for `idlwave-indent-action-table' for symbols using `idlwave-surround'.
755
756Also see the default key bindings for keys using `idlwave-surround'.
757Keys are bound and made into actions calling `idlwave-surround' with
758`idlwave-action-and-binding'.
759See help for `idlwave-action-and-binding' for examples.
760
761Also see help for `idlwave-surround'."
762 :group 'idlwave-abbrev-and-indent-action
763 :type 'boolean)
764
765(defcustom idlwave-pad-keyword t
52a244eb
S
766 "*Non-nil means pad '=' in keywords (routine calls or defs) like assignment.
767Whenever `idlwave-surround' is non-nil then this affects how '=' is
768padded for keywords and for variables. If t, pad the same as for
769assignments. If nil then spaces are removed. With any other value,
770spaces are left unchanged."
f32b3b91 771 :group 'idlwave-abbrev-and-indent-action
15e42531
CD
772 :type '(choice
773 (const :tag "Pad like assignments" t)
774 (const :tag "Remove space near `='" nil)
775 (const :tag "Keep space near `='" 'keep)))
f32b3b91
CD
776
777(defcustom idlwave-show-block t
778 "*Non-nil means point blinks to block beginning for `idlwave-show-begin'."
779 :group 'idlwave-abbrev-and-indent-action
780 :type 'boolean)
781
782(defcustom idlwave-expand-generic-end nil
783 "*Non-nil means expand generic END to ENDIF/ENDELSE/ENDWHILE etc."
784 :group 'idlwave-abbrev-and-indent-action
785 :type 'boolean)
786
15e42531
CD
787(defcustom idlwave-reindent-end t
788 "*Non-nil means re-indent line after END was typed."
789 :group 'idlwave-abbrev-and-indent-action
790 :type 'boolean)
791
f32b3b91
CD
792(defcustom idlwave-abbrev-move t
793 "*Non-nil means the abbrev hook can move point.
794Set to nil by `idlwave-expand-region-abbrevs'. To see the abbrev
795definitions, use the command `list-abbrevs', for abbrevs that move
796point. Moving point is useful, for example, to place point between
797parentheses of expanded functions.
798
799See `idlwave-check-abbrev'."
800 :group 'idlwave-abbrev-and-indent-action
801 :type 'boolean)
802
803(defcustom idlwave-abbrev-change-case nil
804 "*Non-nil means all abbrevs will be forced to either upper or lower case.
805If the value t, all expanded abbrevs will be upper case.
806If the value is 'down then abbrevs will be forced to lower case.
807If nil, the case will not change.
808If `idlwave-reserved-word-upcase' is non-nil, reserved words will always be
809upper case, regardless of this variable."
810 :group 'idlwave-abbrev-and-indent-action
811 :type 'boolean)
812
813(defcustom idlwave-reserved-word-upcase nil
814 "*Non-nil means, reserved words will be made upper case via abbrev expansion.
815If nil case of reserved words is controlled by `idlwave-abbrev-change-case'.
816Has effect only if in abbrev-mode."
817 :group 'idlwave-abbrev-and-indent-action
818 :type 'boolean)
819
820;;; Action/Expand Tables.
821;;
822;; The average user may have difficulty modifying this directly. It
823;; can be modified/set in idlwave-mode-hook, but it is easier to use
824;; idlwave-action-and-binding. See help for idlwave-action-and-binding for
825;; examples of how to add an action.
826;;
827;; The action table is used by `idlwave-indent-line' whereas both the
828;; action and expand tables are used by `idlwave-indent-and-action'. In
829;; general, the expand table is only used when a line is explicitly
830;; indented. Whereas, in addition to being used when the expand table
831;; is used, the action table is used when a line is indirectly
832;; indented via line splitting, auto-filling or a new line creation.
833;;
834;; Example actions:
835;;
836;; Capitalize system vars
837;; (idlwave-action-and-binding idlwave-sysvar '(capitalize-word 1) t)
838;;
839;; Capitalize procedure name
840;; (idlwave-action-and-binding "\\<\\(pro\\|function\\)\\>[ \t]*\\<"
841;; '(capitalize-word 1) t)
842;;
843;; Capitalize common block name
844;; (idlwave-action-and-binding "\\<common\\>[ \t]+\\<"
845;; '(capitalize-word 1) t)
846;; Capitalize label
847;; (idlwave-action-and-binding (concat "^[ \t]*" idlwave-label)
848;; '(capitalize-word -1) t)
849
850(defvar idlwave-indent-action-table nil
851 "*Associated array containing action lists of search string (car),
852and function as a cdr. This table is used by `idlwave-indent-line'.
853See documentation for `idlwave-do-action' for a complete description of
854the action lists.
855
856Additions to the table are made with `idlwave-action-and-binding' when a
857binding is not requested.
858See help on `idlwave-action-and-binding' for examples.")
859
860(defvar idlwave-indent-expand-table nil
861 "*Associated array containing action lists of search string (car),
862and function as a cdr. The table is used by the
863`idlwave-indent-and-action' function. See documentation for
864`idlwave-do-action' for a complete description of the action lists.
865
866Additions to the table are made with `idlwave-action-and-binding' when a
867binding is requested.
868See help on `idlwave-action-and-binding' for examples.")
869
870;;; Documentation header and history keyword ---------------------------------
871
872(defgroup idlwave-documentation nil
873 "Options for documenting IDLWAVE files."
874 :group 'idlwave)
875
876;; FIXME: make defcustom?
877(defvar idlwave-file-header
878 (list nil
879 ";+
880; NAME:
881;
882;
883;
884; PURPOSE:
885;
886;
887;
888; CATEGORY:
889;
890;
891;
892; CALLING SEQUENCE:
893;
894;
895;
896; INPUTS:
897;
898;
899;
900; OPTIONAL INPUTS:
901;
902;
903;
904; KEYWORD PARAMETERS:
905;
906;
907;
908; OUTPUTS:
909;
910;
911;
912; OPTIONAL OUTPUTS:
913;
914;
915;
916; COMMON BLOCKS:
917;
918;
919;
920; SIDE EFFECTS:
921;
922;
923;
924; RESTRICTIONS:
925;
926;
927;
928; PROCEDURE:
929;
930;
931;
932; EXAMPLE:
933;
934;
935;
936; MODIFICATION HISTORY:
937;
938;-
939")
940 "*A list (PATHNAME STRING) specifying the doc-header template to use for
941summarizing a file. If PATHNAME is non-nil then this file will be included.
0ff9b955 942Otherwise STRING is used. If nil, the file summary will be omitted.
f32b3b91
CD
943For example you might set PATHNAME to the path for the
944lib_template.pro file included in the IDL distribution.")
945
5e72c6b2
S
946(defcustom idlwave-header-to-beginning-of-file nil
947 "*Non-nil means, the documentation header will always be at start of file.
948When nil, the header is positioned between the PRO/FUNCTION line of
949the current routine and the code, allowing several routine headers in
950a file."
951 :group 'idlwave-documentation
952 :type 'boolean)
953
f32b3b91
CD
954(defcustom idlwave-timestamp-hook 'idlwave-default-insert-timestamp
955 "*The hook function used to update the timestamp of a function."
956 :group 'idlwave-documentation
957 :type 'function)
958
959(defcustom idlwave-doc-modifications-keyword "HISTORY"
960 "*The modifications keyword to use with the log documentation commands.
961A ':' is added to the keyword end.
962Inserted by doc-header and used to position logs by doc-modification.
963If nil it will not be inserted."
964 :group 'idlwave-documentation
965 :type 'string)
966
967(defcustom idlwave-doclib-start "^;+\\+"
968 "*Regexp matching the start of a document library header."
969 :group 'idlwave-documentation
970 :type 'regexp)
971
972(defcustom idlwave-doclib-end "^;+-"
973 "*Regexp matching the end of a document library header."
974 :group 'idlwave-documentation
975 :type 'regexp)
976
977;;; External Programs -------------------------------------------------------
978
979(defgroup idlwave-external-programs nil
05a1abfc 980 "Path locations of external commands used by IDLWAVE."
f32b3b91
CD
981 :group 'idlwave)
982
983;; WARNING: The following variable has recently been moved from
595ab50b 984;; idlw-shell.el to this file. I hope this does not break
f32b3b91
CD
985;; anything.
986
987(defcustom idlwave-shell-explicit-file-name "idl"
5e72c6b2 988 "*If non-nil, this is the command to run IDL.
f32b3b91 989Should be an absolute file path or path relative to the current environment
5e72c6b2
S
990execution search path. If you want to specify command line switches
991for the idl program, use `idlwave-shell-command-line-options'.
992
993I know the name of this variable is badly chosen, but I cannot change
994it without compromizing backwards-compatibility."
f32b3b91
CD
995 :group 'idlwave-external-programs
996 :type 'string)
997
f32b3b91 998(defcustom idlwave-shell-command-line-options nil
5e72c6b2
S
999 "*A list of command line options for calling the IDL program.
1000Since IDL is executed directly without going through a shell like /bin/sh,
1001this should be a list of strings like '(\"-rt=file\" \"-nw\") with a separate
1002string for each argument. But you may also give a single string which
1003contains the options whitespace-separated. Emacs will be kind enough to
1004split it for you."
1005 :type '(choice
1006 string
1007 (repeat (string :value "")))
f32b3b91
CD
1008 :group 'idlwave-external-programs)
1009
1010(defcustom idlwave-help-application "idlhelp"
1011 "*The external application providing reference help for programming."
1012 :group 'idlwave-external-programs
1013 :type 'string)
1014
05a1abfc
CD
1015;;; Some Shell variables which must be defined here.-----------------------
1016
1017(defcustom idlwave-shell-debug-modifiers '()
1018 "List of modifiers to be used for the debugging commands.
1019Will be used to bind debugging commands in the shell buffer and in all
1020source buffers. These are additional convenience bindings, the debugging
1021commands are always available with the `C-c C-d' prefix.
1022If you set this to '(control shift), this means setting a breakpoint will
1023be on `C-S-b', compiling a source file on `C-S-c' etc. Possible modifiers
1024are `control', `meta', `super', `hyper', `alt', and `shift'."
1025 :group 'idlwave-shell-general-setup
1026 :type '(set :tag "Specify modifiers"
1027 (const control)
1028 (const meta)
1029 (const super)
1030 (const hyper)
1031 (const alt)
1032 (const shift)))
1033
1034(defcustom idlwave-shell-automatic-start nil
1035 "*If non-nil attempt invoke idlwave-shell if not already running.
1036This is checked when an attempt to send a command to an
1037IDL process is made."
1038 :group 'idlwave-shell-general-setup
1039 :type 'boolean)
1040
f32b3b91
CD
1041;;; Miscellaneous variables -------------------------------------------------
1042
1043(defgroup idlwave-misc nil
1044 "Miscellaneous options for IDLWAVE mode."
1045 :group 'idlwave)
1046
1047(defcustom idlwave-startup-message t
1048 "*Non-nil displays a startup message when `idlwave-mode' is first called."
1049 :group 'idlwave-misc
1050 :type 'boolean)
1051
52a244eb 1052(defcustom idlwave-default-font-lock-items
facebc7b 1053 '(pros-and-functions batch-files idlwave-idl-keywords label goto
f32b3b91
CD
1054 common-blocks class-arrows)
1055 "Items which should be fontified on the default fontification level 2.
1056IDLWAVE defines 3 levels of fontification. Level 1 is very little, level 3
1057is everything and level 2 is specified by this list.
1058This variable must be set before IDLWAVE gets loaded. It is
1059a list of symbols, the following symbols are allowed.
1060
1061pros-and-functions Procedure and Function definitions
1062batch-files Batch Files
facebc7b 1063idlwave-idl-keywords IDL Keywords
f32b3b91
CD
1064label Statement Labels
1065goto Goto Statements
1066common-blocks Common Blocks
1067keyword-parameters Keyword Parameters in routine definitions and calls
1068system-variables System Variables
1069fixme FIXME: Warning in comments (on XEmacs only v. 21.0 and up)
1070class-arrows Object Arrows with class property"
1071 :group 'idlwave-misc
1072 :type '(set
1073 :inline t :greedy t
1074 (const :tag "Procedure and Function definitions" pros-and-functions)
facebc7b
S
1075 (const :tag "Batch Files" batch-files)
1076 (const :tag "IDL Keywords (reserved words)" idlwave-idl-keywords)
1077 (const :tag "Statement Labels" label)
1078 (const :tag "Goto Statements" goto)
1079 (const :tag "Tags in Structure Definition" structtag)
1080 (const :tag "Structure Name" structname)
1081 (const :tag "Common Blocks" common-blocks)
1082 (const :tag "Keyword Parameters" keyword-parameters)
1083 (const :tag "System Variables" system-variables)
1084 (const :tag "FIXME: Warning" fixme)
f32b3b91
CD
1085 (const :tag "Object Arrows with class property " class-arrows)))
1086
1087(defcustom idlwave-mode-hook nil
1088 "Normal hook. Executed when a buffer is put into `idlwave-mode'."
1089 :group 'idlwave-misc
1090 :type 'hook)
1091
1092(defcustom idlwave-load-hook nil
1093 "Normal hook. Executed when idlwave.el is loaded."
1094 :group 'idlwave-misc
1095 :type 'hook)
1096
15e42531
CD
1097(defvar idlwave-experimental nil
1098 "Non-nil means turn on a few experimental features.
1099This variable is only for the maintainer, to test difficult stuff,
1100while still distributing stable releases.
1101As a user, you should not set this to t.")
1102
f32b3b91
CD
1103;;;
1104;;; End customization variables section
1105;;;
1106
1107;;; Non customization variables
1108
1109;;; font-lock mode - Additions by Phil Williams, Ulrik Dickow and
52a244eb 1110;;; Simon Marshall <simon_at_gnu.ai.mit.edu>
f32b3b91
CD
1111;;; and Carsten Dominik...
1112
76959b77 1113;; The following are the reserved words in IDL. Maybe we should
52a244eb 1114;; highlight some more stuff as well?
76959b77
S
1115;; Procedure declarations. Fontify keyword plus procedure name.
1116(defvar idlwave-idl-keywords
52a244eb 1117 ;; To update this regexp, update the list of keywords and
76959b77 1118 ;; evaluate the form.
52a244eb 1119 ;; (insert
76959b77 1120 ;; (prin1-to-string
52a244eb 1121 ;; (concat
76959b77 1122 ;; "\\<\\("
52a244eb
S
1123 ;; (regexp-opt
1124 ;; '("||" "&&" "and" "or" "xor" "not"
1125 ;; "eq" "ge" "gt" "le" "lt" "ne"
76959b77 1126 ;; "for" "do" "endfor"
52a244eb 1127 ;; "if" "then" "endif" "else" "endelse"
76959b77
S
1128 ;; "case" "of" "endcase"
1129 ;; "switch" "break" "continue" "endswitch"
1130 ;; "begin" "end"
1131 ;; "repeat" "until" "endrep"
52a244eb 1132 ;; "while" "endwhile"
76959b77
S
1133 ;; "goto" "return"
1134 ;; "inherits" "mod"
1135 ;; "compile_opt" "forward_function"
1136 ;; "on_error" "on_ioerror")) ; on_error is not officially reserved
1137 ;; "\\)\\>")))
52a244eb
S
1138 "\\<\\(&&\\|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\\|||\\)\\>")
1139
76959b77 1140
facebc7b 1141(let* (;; Procedure declarations. Fontify keyword plus procedure name.
f32b3b91
CD
1142 ;; Function declarations. Fontify keyword plus function name.
1143 (pros-and-functions
1144 '("\\<\\(function\\|pro\\)\\>[ \t]+\\(\\sw+\\(::\\sw+\\)?\\)"
1145 (1 font-lock-keyword-face)
1146 (2 font-lock-function-name-face nil t)))
1147
1148 ;; Common blocks
1149 (common-blocks
1150 '("\\<\\(common\\)\\>[ \t]*\\(\\sw+\\)?[ \t]*,?"
1151 (1 font-lock-keyword-face) ; "common"
1152 (2 font-lock-reference-face nil t) ; block name
1153 (font-lock-match-c++-style-declaration-item-and-skip-to-next
1154 ;; Start with point after block name and comma
52a244eb 1155 (goto-char (match-end 0)) ; needed for XEmacs, could be nil
f32b3b91
CD
1156 nil
1157 (1 font-lock-variable-name-face) ; variable names
1158 )))
1159
1160 ;; Batch files
1161 (batch-files
1162 '("^[ \t]*\\(@[^ \t\n]+\\)" (1 font-lock-string-face)))
1163
1164 ;; FIXME warning.
1165 (fixme
1166 '("\\<FIXME:" (0 font-lock-warning-face t)))
1167
1168 ;; Labels
1169 (label
1170 '("^[ \t]*\\([a-zA-Z]\\sw*:\\)" (1 font-lock-reference-face)))
1171
1172 ;; The goto statement and its label
1173 (goto
1174 '("\\(goto\\)[ \t]*,[ \t]*\\([a-zA-Z]\\sw*\\)"
1175 (1 font-lock-keyword-face)
1176 (2 font-lock-reference-face)))
1177
52a244eb
S
1178 ;; Tags in structure definitions. Note that this definition
1179 ;; actually collides with labels, so we have to use the same
1180 ;; face. It also matches named subscript ranges,
1181 ;; e.g. vec{bottom:top]. No good way around this.
05a1abfc
CD
1182 (structtag
1183 '("\\<\\([a-zA-Z][a-zA-Z0-9_]*:\\)[^:]" (1 font-lock-reference-face)))
1184
1185 ;; Structure names
1186 (structname
1187 '("\\({\\|\\<inherits\\s-\\)\\s-*\\([a-zA-Z][a-zA-Z0-9_]*\\)[},\t \n]"
1188 (2 font-lock-function-name-face)))
1189
52a244eb 1190 ;; Keyword parameters, like /xlog or ,xrange=[]
f32b3b91 1191 ;; This is anchored to the comma preceeding the keyword.
595ab50b
CD
1192 ;; Treats continuation lines, works only during whole buffer
1193 ;; fontification. Slow, use it only in fancy fontification.
f32b3b91 1194 (keyword-parameters
15e42531
CD
1195 '("\\(,\\|[a-zA-Z0-9_](\\)[ \t]*\\(\\$[ \t]*\\(;.*\\)?\\(\n[ \t]*;.*\\)*\n[ \t]*\\)?\\(/[a-zA-Z_]\\sw*\\|[a-zA-Z_]\\sw*[ \t]*=\\)"
1196 (5 font-lock-reference-face)))
f32b3b91 1197
595ab50b 1198 ;; System variables start with a bang.
f32b3b91 1199 (system-variables
15e42531 1200 '("\\(![a-zA-Z_0-9]+\\(\\.\\sw+\\)?\\)"
f32b3b91
CD
1201 (1 font-lock-variable-name-face)))
1202
1203 ;; Special and unusual operators (not used because too noisy)
1204 (special-operators
1205 '("[<>#]" (0 font-lock-keyword-face)))
1206
1207 ;; All operators (not used because too noisy)
1208 (all-operators
1209 '("[-*^#+<>/]" (0 font-lock-keyword-face)))
52a244eb 1210
f32b3b91
CD
1211 ;; Arrows with text property `idlwave-class'
1212 (class-arrows
facebc7b
S
1213 '(idlwave-match-class-arrows (0 idlwave-class-arrow-face))))
1214
1215 (defconst idlwave-font-lock-keywords-1
1216 (list pros-and-functions batch-files)
1217 "Subdued level highlighting for IDLWAVE mode.")
f32b3b91 1218
facebc7b
S
1219 (defconst idlwave-font-lock-keywords-2
1220 (mapcar 'symbol-value idlwave-default-font-lock-items)
1221 "Medium level highlighting for IDLWAVE mode.")
f32b3b91 1222
facebc7b 1223 (defconst idlwave-font-lock-keywords-3
f32b3b91
CD
1224 (list pros-and-functions
1225 batch-files
76959b77 1226 idlwave-idl-keywords
f32b3b91 1227 label goto
05a1abfc
CD
1228 structtag
1229 structname
f32b3b91
CD
1230 common-blocks
1231 keyword-parameters
1232 system-variables
facebc7b
S
1233 class-arrows)
1234 "Gaudy level highlighting for IDLWAVE mode."))
f32b3b91
CD
1235
1236(defun idlwave-match-class-arrows (limit)
1237 ;; Match an object arrow with class property
1238 (and idlwave-store-inquired-class
1239 (re-search-forward "->" limit 'limit)
1240 (get-text-property (match-beginning 0) 'idlwave-class)))
1241
1242(defvar idlwave-font-lock-keywords idlwave-font-lock-keywords-2
1243 "Default expressions to highlight in IDLWAVE mode.")
1244
1245(defvar idlwave-font-lock-defaults
1246 '((idlwave-font-lock-keywords
52a244eb 1247 idlwave-font-lock-keywords-1
f32b3b91
CD
1248 idlwave-font-lock-keywords-2
1249 idlwave-font-lock-keywords-3)
52a244eb
S
1250 nil t
1251 ((?$ . "w") (?_ . "w") (?. . "w") (?| . "w") (?& . "w"))
f32b3b91
CD
1252 beginning-of-line))
1253
52a244eb 1254(put 'idlwave-mode 'font-lock-defaults
f32b3b91
CD
1255 idlwave-font-lock-defaults) ; XEmacs
1256
1257(defconst idlwave-comment-line-start-skip "^[ \t]*;"
1258 "Regexp to match the start of a full-line comment.
1259That is the _beginning_ of a line containing a comment delimiter `;' preceded
1260only by whitespace.")
1261
52a244eb 1262(defconst idlwave-begin-block-reg
05a1abfc 1263 "\\<\\(pro\\|function\\|begin\\|case\\|switch\\)\\>"
f32b3b91
CD
1264 "Regular expression to find the beginning of a block. The case does
1265not matter. The search skips matches in comments.")
1266
52a244eb 1267(defconst idlwave-begin-unit-reg "^\\s-*\\(pro\\|function\\)\\>\\|\\`"
f32b3b91
CD
1268 "Regular expression to find the beginning of a unit. The case does
1269not matter.")
1270
52a244eb 1271(defconst idlwave-end-unit-reg "^\\s-*\\(pro\\|function\\)\\>\\|\\'"
f32b3b91
CD
1272 "Regular expression to find the line that indicates the end of unit.
1273This line is the end of buffer or the start of another unit. The case does
1274not matter. The search skips matches in comments.")
1275
1276(defconst idlwave-continue-line-reg "\\<\\$"
1277 "Regular expression to match a continued line.")
1278
1279(defconst idlwave-end-block-reg
05a1abfc 1280 "\\<end\\(\\|case\\|switch\\|else\\|for\\|if\\|rep\\|while\\)\\>"
f32b3b91
CD
1281 "Regular expression to find the end of a block. The case does
1282not matter. The search skips matches found in comments.")
1283
1284(defconst idlwave-block-matches
1285 '(("pro" . "end")
1286 ("function" . "end")
1287 ("case" . "endcase")
1288 ("else" . "endelse")
1289 ("for" . "endfor")
1290 ("then" . "endif")
1291 ("repeat" . "endrep")
05a1abfc 1292 ("switch" . "endswitch")
f32b3b91
CD
1293 ("while" . "endwhile"))
1294 "Matches between statements and the corresponding END variant.
1295The cars are the reserved words starting a block. If the block really
1296begins with BEGIN, the cars are the reserved words before the begin
1297which can be used to identify the block type.
1298This is used to check for the correct END type, to close blocks and
1299to expand generic end statements to their detailed form.")
1300
1301(defconst idlwave-block-match-regexp
1302 "\\<\\(else\\|for\\|then\\|repeat\\|while\\)\\>"
1303"Regular expression matching reserved words which can stand before
1304blocks starting with a BEGIN statement. The matches must have associations
1305`idlwave-block-matches'")
1306
52a244eb 1307(defconst idlwave-identifier "[a-zA-Z_][a-zA-Z0-9$_]*"
f32b3b91
CD
1308 "Regular expression matching an IDL identifier.")
1309
1310(defconst idlwave-sysvar (concat "!" idlwave-identifier)
1311 "Regular expression matching IDL system variables.")
1312
1313(defconst idlwave-variable (concat idlwave-identifier "\\|" idlwave-sysvar)
1314 "Regular expression matching IDL variable names.")
1315
1316(defconst idlwave-label (concat idlwave-identifier ":")
1317 "Regular expression matching IDL labels.")
1318
52a244eb
S
1319(defconst idlwave-method-call (concat idlwave-identifier "\\s *->"
1320 "\\(\\s *" idlwave-identifier "::\\)?"
1321))
1322
f32b3b91
CD
1323(defconst idlwave-statement-match
1324 (list
aa87aafc 1325 ;; "endif else" is the only possible "end" that can be
f32b3b91
CD
1326 ;; followed by a statement on the same line.
1327 '(endelse . ("end\\(\\|if\\)\\s +else" "end\\(\\|if\\)\\s +else"))
1328 ;; all other "end"s can not be followed by a statement.
1329 (cons 'end (list idlwave-end-block-reg nil))
1330 '(if . ("if\\>" "then"))
1331 '(for . ("for\\>" "do"))
1332 '(begin . ("begin\\>" nil))
1333 '(pdef . ("pro\\>\\|function\\>" nil))
1334 '(while . ("while\\>" "do"))
1335 '(repeat . ("repeat\\>" "repeat"))
1336 '(goto . ("goto\\>" nil))
1337 '(case . ("case\\>" nil))
05a1abfc 1338 '(switch . ("switch\\>" nil))
52a244eb
S
1339 (cons 'call (list (concat "\\(" idlwave-variable "\\) *= *"
1340 "\\(" idlwave-method-call "\\s *\\)?"
1341 idlwave-identifier
1342 "\\s *(") nil))
1343 (cons 'call (list (concat
1344 "\\(" idlwave-method-call "\\s *\\)?"
1345 idlwave-identifier
1346 "\\( *\\($\\|\\$\\)\\|\\s *,\\)") nil))
1347 (cons 'assign (list (concat
1348 "\\(" idlwave-variable "\\) *=") nil)))
1349
f32b3b91
CD
1350 "Associated list of statement matching regular expressions.
1351Each regular expression matches the start of an IDL statement. The
1352first element of each association is a symbol giving the statement
1353type. The associated value is a list. The first element of this list
1354is a regular expression matching the start of an IDL statement for
1355identifying the statement type. The second element of this list is a
1356regular expression for finding a substatement for the type. The
1357substatement starts after the end of the found match modulo
1358whitespace. If it is nil then the statement has no substatement. The
1359list order matters since matching an assignment statement exactly is
1360not possible without parsing. Thus assignment statement become just
15e42531 1361the leftover unidentified statements containing an equal sign." )
f32b3b91
CD
1362
1363(defvar idlwave-fill-function 'auto-fill-function
1364 "IDL mode auto fill function.")
1365
1366(defvar idlwave-comment-indent-function 'comment-indent-function
1367 "IDL mode comment indent function.")
1368
1369;; Note that this is documented in the v18 manuals as being a string
1370;; of length one rather than a single character.
1371;; The code in this file accepts either format for compatibility.
52a244eb 1372(defvar idlwave-comment-indent-char ?\
f32b3b91
CD
1373 "Character to be inserted for IDL comment indentation.
1374Normally a space.")
1375
1376(defconst idlwave-continuation-char ?$
1377 "Character which is inserted as a last character on previous line by
1378 \\[idlwave-split-line] to begin a continuation line. Normally $.")
1379
52a244eb 1380(defconst idlwave-mode-version "5.5")
f32b3b91
CD
1381
1382(defmacro idlwave-keyword-abbrev (&rest args)
1383 "Creates a function for abbrev hooks to call `idlwave-check-abbrev' with args."
8a946354 1384 `(quote (lambda ()
5e72c6b2 1385 ,(append '(idlwave-check-abbrev) args))))
f32b3b91
CD
1386
1387;; If I take the time I can replace idlwave-keyword-abbrev with
1388;; idlwave-code-abbrev and remove the quoted abbrev check from
1389;; idlwave-check-abbrev. Then, e.g, (idlwave-keyword-abbrev 0 t) becomes
1390;; (idlwave-code-abbrev idlwave-check-abbrev 0 t). In fact I should change
1391;; the name of idlwave-check-abbrev to something like idlwave-modify-abbrev.
1392
1393(defmacro idlwave-code-abbrev (&rest args)
1394 "Creates a function for abbrev hooks that ensures abbrevs are not quoted.
1395Specifically, if the abbrev is in a comment or string it is unexpanded.
1396Otherwise ARGS forms a list that is evaluated."
8a946354 1397 `(quote (lambda ()
5e72c6b2
S
1398 ,(prin1-to-string args) ;; Puts the code in the doc string
1399 (if (idlwave-quoted)
1400 (progn (unexpand-abbrev) nil)
1401 ,(append args)))))
f32b3b91
CD
1402
1403(defvar idlwave-mode-map (make-sparse-keymap)
1404 "Keymap used in IDL mode.")
1405
1406(defvar idlwave-mode-syntax-table (make-syntax-table)
1407 "Syntax table in use in `idlwave-mode' buffers.")
1408
1409(modify-syntax-entry ?+ "." idlwave-mode-syntax-table)
1410(modify-syntax-entry ?- "." idlwave-mode-syntax-table)
1411(modify-syntax-entry ?* "." idlwave-mode-syntax-table)
1412(modify-syntax-entry ?/ "." idlwave-mode-syntax-table)
1413(modify-syntax-entry ?^ "." idlwave-mode-syntax-table)
1414(modify-syntax-entry ?# "." idlwave-mode-syntax-table)
1415(modify-syntax-entry ?= "." idlwave-mode-syntax-table)
1416(modify-syntax-entry ?% "." idlwave-mode-syntax-table)
1417(modify-syntax-entry ?< "." idlwave-mode-syntax-table)
1418(modify-syntax-entry ?> "." idlwave-mode-syntax-table)
1419(modify-syntax-entry ?\' "\"" idlwave-mode-syntax-table)
1420(modify-syntax-entry ?\" "\"" idlwave-mode-syntax-table)
1421(modify-syntax-entry ?\\ "." idlwave-mode-syntax-table)
1422(modify-syntax-entry ?_ "_" idlwave-mode-syntax-table)
1423(modify-syntax-entry ?{ "(}" idlwave-mode-syntax-table)
1424(modify-syntax-entry ?} "){" idlwave-mode-syntax-table)
1425(modify-syntax-entry ?$ "_" idlwave-mode-syntax-table)
1426(modify-syntax-entry ?. "." idlwave-mode-syntax-table)
1427(modify-syntax-entry ?\; "<" idlwave-mode-syntax-table)
1428(modify-syntax-entry ?\n ">" idlwave-mode-syntax-table)
1429(modify-syntax-entry ?\f ">" idlwave-mode-syntax-table)
1430
1431(defvar idlwave-find-symbol-syntax-table
1432 (copy-syntax-table idlwave-mode-syntax-table)
1433 "Syntax table that treats symbol characters as word characters.")
1434
05a1abfc
CD
1435(modify-syntax-entry ?$ "w" idlwave-find-symbol-syntax-table)
1436(modify-syntax-entry ?_ "w" idlwave-find-symbol-syntax-table)
1437(modify-syntax-entry ?! "w" idlwave-find-symbol-syntax-table)
1438(modify-syntax-entry ?. "w" idlwave-find-symbol-syntax-table)
1439
76959b77
S
1440(defmacro idlwave-with-special-syntax (&rest body)
1441 "Execute BODY with a different syntax table."
05a1abfc
CD
1442 `(let ((saved-syntax (syntax-table)))
1443 (unwind-protect
1444 (progn
1445 (set-syntax-table idlwave-find-symbol-syntax-table)
1446 ,@body)
1447 (set-syntax-table saved-syntax))))
1448
76959b77
S
1449;(defmacro idlwave-with-special-syntax1 (&rest body)
1450; "Execute BODY with a different syntax table."
1451; `(let ((saved-syntax (syntax-table)))
1452; (unwind-protect
1453; (progn
1454; (set-syntax-table idlwave-find-symbol-syntax-table)
1455; ,@body)
1456; (set-syntax-table saved-syntax))))
1457
f32b3b91
CD
1458(defun idlwave-action-and-binding (key cmd &optional select)
1459 "KEY and CMD are made into a key binding and an indent action.
1460KEY is a string - same as for the `define-key' function. CMD is a
1461function of no arguments or a list to be evaluated. CMD is bound to
1462KEY in `idlwave-mode-map' by defining an anonymous function calling
1463`self-insert-command' followed by CMD. If KEY contains more than one
1464character a binding will only be set if SELECT is 'both.
1465
5e72c6b2 1466\(KEY . CMD\) is also placed in the `idlwave-indent-expand-table',
f32b3b91
CD
1467replacing any previous value for KEY. If a binding is not set then it
1468will instead be placed in `idlwave-indent-action-table'.
1469
1470If the optional argument SELECT is nil then an action and binding are
1471created. If SELECT is 'noaction, then a binding is always set and no
1472action is created. If SELECT is 'both then an action and binding
1473will both be created even if KEY contains more than one character.
1474Otherwise, if SELECT is non-nil then only an action is created.
1475
1476Some examples:
1477No spaces before and 1 after a comma
1478 (idlwave-action-and-binding \",\" '(idlwave-surround 0 1))
1479A minimum of 1 space before and after `=' (see `idlwave-expand-equal').
1480 (idlwave-action-and-binding \"=\" '(idlwave-expand-equal -1 -1))
1481Capitalize system variables - action only
1482 (idlwave-action-and-binding idlwave-sysvar '(capitalize-word 1) t)"
1483 (if (not (equal select 'noaction))
1484 ;; Add action
1485 (let* ((table (if select 'idlwave-indent-action-table
1486 'idlwave-indent-expand-table))
1487 (cell (assoc key (eval table))))
1488 (if cell
1489 ;; Replace action command
1490 (setcdr cell cmd)
1491 ;; New action
1492 (set table (append (eval table) (list (cons key cmd)))))))
1493 ;; Make key binding for action
1494 (if (or (and (null select) (= (length key) 1))
1495 (equal select 'noaction)
1496 (equal select 'both))
1497 (define-key idlwave-mode-map key
1498 (append '(lambda ()
1499 (interactive)
1500 (self-insert-command 1))
1501 (list (if (listp cmd)
1502 cmd
1503 (list cmd)))))))
1504
1505(fset 'idlwave-debug-map (make-sparse-keymap))
1506
595ab50b 1507(define-key idlwave-mode-map "\C-c " 'idlwave-hard-tab)
15e42531 1508(define-key idlwave-mode-map [(control tab)] 'idlwave-hard-tab)
595ab50b 1509;(define-key idlwave-mode-map "\C-c\C- " 'idlwave-hard-tab)
f32b3b91
CD
1510(define-key idlwave-mode-map "'" 'idlwave-show-matching-quote)
1511(define-key idlwave-mode-map "\"" 'idlwave-show-matching-quote)
76959b77 1512(define-key idlwave-mode-map "\C-g" 'idlwave-keyboard-quit)
f32b3b91
CD
1513(define-key idlwave-mode-map "\C-c;" 'idlwave-toggle-comment-region)
1514(define-key idlwave-mode-map "\C-\M-a" 'idlwave-beginning-of-subprogram)
1515(define-key idlwave-mode-map "\C-\M-e" 'idlwave-end-of-subprogram)
1516(define-key idlwave-mode-map "\C-c{" 'idlwave-beginning-of-block)
1517(define-key idlwave-mode-map "\C-c}" 'idlwave-end-of-block)
1518(define-key idlwave-mode-map "\C-c]" 'idlwave-close-block)
1519(define-key idlwave-mode-map "\M-\C-h" 'idlwave-mark-subprogram)
1520(define-key idlwave-mode-map "\M-\C-n" 'idlwave-forward-block)
1521(define-key idlwave-mode-map "\M-\C-p" 'idlwave-backward-block)
1522(define-key idlwave-mode-map "\M-\C-d" 'idlwave-down-block)
1523(define-key idlwave-mode-map "\M-\C-u" 'idlwave-backward-up-block)
1524(define-key idlwave-mode-map "\M-\r" 'idlwave-split-line)
1525(define-key idlwave-mode-map "\M-\C-q" 'idlwave-indent-subprogram)
1526(define-key idlwave-mode-map "\C-c\C-p" 'idlwave-previous-statement)
1527(define-key idlwave-mode-map "\C-c\C-n" 'idlwave-next-statement)
1528;; (define-key idlwave-mode-map "\r" 'idlwave-newline)
1529;; (define-key idlwave-mode-map "\t" 'idlwave-indent-line)
76959b77 1530(define-key idlwave-mode-map [(shift tab)] 'idlwave-indent-statement)
f32b3b91
CD
1531(define-key idlwave-mode-map "\C-c\C-a" 'idlwave-auto-fill-mode)
1532(define-key idlwave-mode-map "\M-q" 'idlwave-fill-paragraph)
1533(define-key idlwave-mode-map "\M-s" 'idlwave-edit-in-idlde)
1534(define-key idlwave-mode-map "\C-c\C-h" 'idlwave-doc-header)
1535(define-key idlwave-mode-map "\C-c\C-m" 'idlwave-doc-modification)
1536(define-key idlwave-mode-map "\C-c\C-c" 'idlwave-case)
1537(define-key idlwave-mode-map "\C-c\C-d" 'idlwave-debug-map)
05a1abfc
CD
1538(when (and (boundp 'idlwave-shell-debug-modifiers)
1539 (listp idlwave-shell-debug-modifiers)
1540 (not (equal idlwave-shell-debug-modifiers '())))
1541 ;; Bind the debug commands also with the special modifiers.
1542 (let ((shift (memq 'shift idlwave-shell-debug-modifiers))
52a244eb 1543 (mods-noshift (delq 'shift
05a1abfc 1544 (copy-sequence idlwave-shell-debug-modifiers))))
52a244eb 1545 (define-key idlwave-mode-map
05a1abfc
CD
1546 (vector (append mods-noshift (list (if shift ?C ?c))))
1547 'idlwave-shell-save-and-run)
52a244eb 1548 (define-key idlwave-mode-map
05a1abfc 1549 (vector (append mods-noshift (list (if shift ?B ?b))))
52a244eb
S
1550 'idlwave-shell-break-here)
1551 (define-key idlwave-mode-map
1552 (vector (append mods-noshift (list (if shift ?E ?e))))
1553 'idlwave-shell-run-region)))
1554(define-key idlwave-mode-map "\C-c\C-d\C-c" 'idlwave-shell-save-and-run)
1555(define-key idlwave-mode-map "\C-c\C-d\C-b" 'idlwave-shell-break-here)
1556(define-key idlwave-mode-map "\C-c\C-d\C-e" 'idlwave-shell-run-region)
f32b3b91
CD
1557(define-key idlwave-mode-map "\C-c\C-f" 'idlwave-for)
1558;; (define-key idlwave-mode-map "\C-c\C-f" 'idlwave-function)
1559;; (define-key idlwave-mode-map "\C-c\C-p" 'idlwave-procedure)
1560(define-key idlwave-mode-map "\C-c\C-r" 'idlwave-repeat)
1561(define-key idlwave-mode-map "\C-c\C-w" 'idlwave-while)
15e42531 1562(define-key idlwave-mode-map "\C-c\C-k" 'idlwave-kill-autoloaded-buffers)
f32b3b91
CD
1563(define-key idlwave-mode-map "\C-c\C-s" 'idlwave-shell)
1564(define-key idlwave-mode-map "\C-c\C-l" 'idlwave-shell-recenter-shell-window)
15e42531 1565(define-key idlwave-mode-map "\C-c\C-b" 'idlwave-list-buffer-load-path-shadows)
22d5821d
CD
1566(autoload 'idlwave-shell "idlw-shell"
1567 "Run an inferior IDL, with I/O through buffer `(idlwave-shell-buffer)'." t)
8c7b4ec8
EZ
1568(autoload 'idlwave-shell-send-command "idlw-shell")
1569(autoload 'idlwave-shell-recenter-shell-window "idlw-shell"
f32b3b91 1570 "Run `idlwave-shell' and switch back to current window" t)
8c7b4ec8 1571(autoload 'idlwave-shell-save-and-run "idlw-shell"
f32b3b91 1572 "Save and run buffer under the shell." t)
15e42531
CD
1573(autoload 'idlwave-shell-break-here "idlw-shell"
1574 "Set breakpoint in current line." t)
52a244eb
S
1575(autoload 'idlwave-shell-run-region "idlw-shell"
1576 "Compile and run the region." t)
f32b3b91
CD
1577(define-key idlwave-mode-map "\C-c\C-v" 'idlwave-find-module)
1578(define-key idlwave-mode-map "\C-c?" 'idlwave-routine-info)
15e42531 1579(define-key idlwave-mode-map "\M-?" 'idlwave-context-help)
76959b77 1580(define-key idlwave-mode-map [(control meta ?\?)] 'idlwave-online-help)
52a244eb 1581;; Pickup both forms of Esc/Meta binding
f32b3b91 1582(define-key idlwave-mode-map [(meta tab)] 'idlwave-complete)
52a244eb
S
1583(define-key idlwave-mode-map [?\e?\t] 'idlwave-complete)
1584(define-key idlwave-mode-map "\M-\C-i" 'idlwave-complete)
1585(define-key idlwave-mode-map "\C-c\C-i" 'idlwave-update-routine-info)
1586(define-key idlwave-mode-map "\C-c=" 'idlwave-resolve)
1587(define-key idlwave-mode-map
15e42531
CD
1588 (if (featurep 'xemacs) [(shift button3)] [(shift mouse-3)])
1589 'idlwave-mouse-context-help)
f32b3b91
CD
1590
1591;; Set action and key bindings.
1592;; See description of the function `idlwave-action-and-binding'.
1593;; Automatically add spaces for the following characters
52a244eb
S
1594;(idlwave-action-and-binding "&" '(idlwave-surround -1 -1 '(?&) 1
1595; (lambda (char) 0)))
f32b3b91 1596(idlwave-action-and-binding "<" '(idlwave-surround -1 -1))
5e72c6b2 1597;; Binding works for both > and ->, by changing the length of the token.
52a244eb 1598(idlwave-action-and-binding ">" '(idlwave-surround -1 -1 '(?-) 1
5e72c6b2
S
1599 'idlwave-gtr-pad-hook))
1600(idlwave-action-and-binding "->" '(idlwave-surround -1 -1 nil 2) t)
f32b3b91 1601(idlwave-action-and-binding "," '(idlwave-surround 0 -1))
52a244eb 1602
f32b3b91
CD
1603;; Automatically add spaces to equal sign if not keyword
1604(idlwave-action-and-binding "=" '(idlwave-expand-equal -1 -1))
1605
1606;;;
1607;;; Abbrev Section
1608;;;
1609;;; When expanding abbrevs and the abbrev hook moves backward, an extra
1610;;; space is inserted (this is the space typed by the user to expanded
1611;;; the abbrev).
1612;;;
5e72c6b2
S
1613(defvar idlwave-mode-abbrev-table nil
1614 "Abbreviation table used for IDLWAVE mode")
1615(define-abbrev-table 'idlwave-mode-abbrev-table ())
1616
1617(defun idlwave-define-abbrev (name expansion hook &optional noprefix table)
1618 "Define-abbrev with backward compatibility.
1619
1620If NOPREFIX is non-nil, don't prepend prefix character. Installs into
1621idlwave-mode-abbrev-table unless TABLE is non-nil."
1622 (let ((abbrevs-changed nil) ;; mask the current value to avoid save
1623 (args (list (or table idlwave-mode-abbrev-table)
1624 (if noprefix name (concat idlwave-abbrev-start-char name))
1625 expansion
1626 hook)))
1627 (condition-case nil
1628 (apply 'define-abbrev (append args '(0 t)))
1629 (error (apply 'define-abbrev args)))))
f32b3b91
CD
1630
1631(condition-case nil
52a244eb 1632 (modify-syntax-entry (string-to-char idlwave-abbrev-start-char)
f32b3b91
CD
1633 "w" idlwave-mode-syntax-table)
1634 (error nil))
1635
5e72c6b2
S
1636;;
1637;; Templates
1638;;
1639(idlwave-define-abbrev "c" "" (idlwave-code-abbrev idlwave-case))
1640(idlwave-define-abbrev "sw" "" (idlwave-code-abbrev idlwave-switch))
1641(idlwave-define-abbrev "f" "" (idlwave-code-abbrev idlwave-for))
1642(idlwave-define-abbrev "fu" "" (idlwave-code-abbrev idlwave-function))
1643(idlwave-define-abbrev "pr" "" (idlwave-code-abbrev idlwave-procedure))
1644(idlwave-define-abbrev "r" "" (idlwave-code-abbrev idlwave-repeat))
1645(idlwave-define-abbrev "w" "" (idlwave-code-abbrev idlwave-while))
1646(idlwave-define-abbrev "i" "" (idlwave-code-abbrev idlwave-if))
1647(idlwave-define-abbrev "elif" "" (idlwave-code-abbrev idlwave-elif))
1648;;
1649;; Keywords, system functions, conversion routines
1650;;
1651(idlwave-define-abbrev "ap" "arg_present()" (idlwave-keyword-abbrev 1))
1652(idlwave-define-abbrev "b" "begin" (idlwave-keyword-abbrev 0 t))
1653(idlwave-define-abbrev "co" "common" (idlwave-keyword-abbrev 0 t))
1654(idlwave-define-abbrev "cb" "byte()" (idlwave-keyword-abbrev 1))
1655(idlwave-define-abbrev "cx" "fix()" (idlwave-keyword-abbrev 1))
1656(idlwave-define-abbrev "cl" "long()" (idlwave-keyword-abbrev 1))
1657(idlwave-define-abbrev "cf" "float()" (idlwave-keyword-abbrev 1))
1658(idlwave-define-abbrev "cs" "string()" (idlwave-keyword-abbrev 1))
1659(idlwave-define-abbrev "cc" "complex()" (idlwave-keyword-abbrev 1))
1660(idlwave-define-abbrev "cd" "double()" (idlwave-keyword-abbrev 1))
1661(idlwave-define-abbrev "e" "else" (idlwave-keyword-abbrev 0 t))
1662(idlwave-define-abbrev "ec" "endcase" 'idlwave-show-begin)
1663(idlwave-define-abbrev "es" "endswitch" 'idlwave-show-begin)
1664(idlwave-define-abbrev "ee" "endelse" 'idlwave-show-begin)
1665(idlwave-define-abbrev "ef" "endfor" 'idlwave-show-begin)
1666(idlwave-define-abbrev "ei" "endif else if" 'idlwave-show-begin)
1667(idlwave-define-abbrev "el" "endif else" 'idlwave-show-begin)
1668(idlwave-define-abbrev "en" "endif" 'idlwave-show-begin)
1669(idlwave-define-abbrev "er" "endrep" 'idlwave-show-begin)
1670(idlwave-define-abbrev "ew" "endwhile" 'idlwave-show-begin)
1671(idlwave-define-abbrev "g" "goto," (idlwave-keyword-abbrev 0 t))
1672(idlwave-define-abbrev "h" "help," (idlwave-keyword-abbrev 0))
1673(idlwave-define-abbrev "k" "keyword_set()" (idlwave-keyword-abbrev 1))
1674(idlwave-define-abbrev "n" "n_elements()" (idlwave-keyword-abbrev 1))
1675(idlwave-define-abbrev "on" "on_error," (idlwave-keyword-abbrev 0))
1676(idlwave-define-abbrev "oi" "on_ioerror," (idlwave-keyword-abbrev 0 1))
1677(idlwave-define-abbrev "ow" "openw," (idlwave-keyword-abbrev 0))
1678(idlwave-define-abbrev "or" "openr," (idlwave-keyword-abbrev 0))
1679(idlwave-define-abbrev "ou" "openu," (idlwave-keyword-abbrev 0))
1680(idlwave-define-abbrev "p" "print," (idlwave-keyword-abbrev 0))
1681(idlwave-define-abbrev "pt" "plot," (idlwave-keyword-abbrev 0))
1682(idlwave-define-abbrev "re" "read," (idlwave-keyword-abbrev 0))
1683(idlwave-define-abbrev "rf" "readf," (idlwave-keyword-abbrev 0))
1684(idlwave-define-abbrev "ru" "readu," (idlwave-keyword-abbrev 0))
1685(idlwave-define-abbrev "rt" "return" (idlwave-keyword-abbrev 0))
1686(idlwave-define-abbrev "sc" "strcompress()" (idlwave-keyword-abbrev 1))
1687(idlwave-define-abbrev "sn" "strlen()" (idlwave-keyword-abbrev 1))
1688(idlwave-define-abbrev "sl" "strlowcase()" (idlwave-keyword-abbrev 1))
1689(idlwave-define-abbrev "su" "strupcase()" (idlwave-keyword-abbrev 1))
1690(idlwave-define-abbrev "sm" "strmid()" (idlwave-keyword-abbrev 1))
1691(idlwave-define-abbrev "sp" "strpos()" (idlwave-keyword-abbrev 1))
1692(idlwave-define-abbrev "st" "strput()" (idlwave-keyword-abbrev 1))
1693(idlwave-define-abbrev "sr" "strtrim()" (idlwave-keyword-abbrev 1))
1694(idlwave-define-abbrev "t" "then" (idlwave-keyword-abbrev 0 t))
1695(idlwave-define-abbrev "u" "until" (idlwave-keyword-abbrev 0 t))
1696(idlwave-define-abbrev "wu" "writeu," (idlwave-keyword-abbrev 0))
1697(idlwave-define-abbrev "iap" "if arg_present() then" (idlwave-keyword-abbrev 6))
1698(idlwave-define-abbrev "ik" "if keyword_set() then" (idlwave-keyword-abbrev 6))
1699(idlwave-define-abbrev "ine" "if n_elements() eq 0 then" (idlwave-keyword-abbrev 11))
1700(idlwave-define-abbrev "inn" "if n_elements() ne 0 then" (idlwave-keyword-abbrev 11))
1701(idlwave-define-abbrev "np" "n_params()" (idlwave-keyword-abbrev 0))
1702(idlwave-define-abbrev "s" "size()" (idlwave-keyword-abbrev 1))
1703(idlwave-define-abbrev "wi" "widget_info()" (idlwave-keyword-abbrev 1))
1704(idlwave-define-abbrev "wc" "widget_control," (idlwave-keyword-abbrev 0))
52a244eb 1705
5e72c6b2
S
1706;; This section is reserved words only. (From IDL user manual)
1707;;
1708(idlwave-define-abbrev "and" "and" (idlwave-keyword-abbrev 0 t) t)
1709(idlwave-define-abbrev "begin" "begin" (idlwave-keyword-abbrev 0 t) t)
1710(idlwave-define-abbrev "break" "break" (idlwave-keyword-abbrev 0 t) t)
1711(idlwave-define-abbrev "case" "case" (idlwave-keyword-abbrev 0 t) t)
1712(idlwave-define-abbrev "common" "common" (idlwave-keyword-abbrev 0 t) t)
1713(idlwave-define-abbrev "continue" "continue" (idlwave-keyword-abbrev 0 t) t)
1714(idlwave-define-abbrev "do" "do" (idlwave-keyword-abbrev 0 t) t)
1715(idlwave-define-abbrev "else" "else" (idlwave-keyword-abbrev 0 t) t)
1716(idlwave-define-abbrev "end" "end" 'idlwave-show-begin-check t)
1717(idlwave-define-abbrev "endcase" "endcase" 'idlwave-show-begin-check t)
1718(idlwave-define-abbrev "endelse" "endelse" 'idlwave-show-begin-check t)
1719(idlwave-define-abbrev "endfor" "endfor" 'idlwave-show-begin-check t)
1720(idlwave-define-abbrev "endif" "endif" 'idlwave-show-begin-check t)
1721(idlwave-define-abbrev "endrep" "endrep" 'idlwave-show-begin-check t)
1722(idlwave-define-abbrev "endswitch" "endswitch" 'idlwave-show-begin-check t)
1723(idlwave-define-abbrev "endwhi" "endwhi" 'idlwave-show-begin-check t)
1724(idlwave-define-abbrev "endwhile" "endwhile" 'idlwave-show-begin-check t)
1725(idlwave-define-abbrev "eq" "eq" (idlwave-keyword-abbrev 0 t) t)
1726(idlwave-define-abbrev "for" "for" (idlwave-keyword-abbrev 0 t) t)
1727(idlwave-define-abbrev "function" "function" (idlwave-keyword-abbrev 0 t) t)
1728(idlwave-define-abbrev "ge" "ge" (idlwave-keyword-abbrev 0 t) t)
1729(idlwave-define-abbrev "goto" "goto" (idlwave-keyword-abbrev 0 t) t)
1730(idlwave-define-abbrev "gt" "gt" (idlwave-keyword-abbrev 0 t) t)
1731(idlwave-define-abbrev "if" "if" (idlwave-keyword-abbrev 0 t) t)
1732(idlwave-define-abbrev "le" "le" (idlwave-keyword-abbrev 0 t) t)
1733(idlwave-define-abbrev "lt" "lt" (idlwave-keyword-abbrev 0 t) t)
1734(idlwave-define-abbrev "mod" "mod" (idlwave-keyword-abbrev 0 t) t)
1735(idlwave-define-abbrev "ne" "ne" (idlwave-keyword-abbrev 0 t) t)
1736(idlwave-define-abbrev "not" "not" (idlwave-keyword-abbrev 0 t) t)
1737(idlwave-define-abbrev "of" "of" (idlwave-keyword-abbrev 0 t) t)
1738(idlwave-define-abbrev "on_ioerror" "on_ioerror" (idlwave-keyword-abbrev 0 t) t)
1739(idlwave-define-abbrev "or" "or" (idlwave-keyword-abbrev 0 t) t)
1740(idlwave-define-abbrev "pro" "pro" (idlwave-keyword-abbrev 0 t) t)
1741(idlwave-define-abbrev "repeat" "repeat" (idlwave-keyword-abbrev 0 t) t)
1742(idlwave-define-abbrev "switch" "switch" (idlwave-keyword-abbrev 0 t) t)
1743(idlwave-define-abbrev "then" "then" (idlwave-keyword-abbrev 0 t) t)
1744(idlwave-define-abbrev "until" "until" (idlwave-keyword-abbrev 0 t) t)
1745(idlwave-define-abbrev "while" "while" (idlwave-keyword-abbrev 0 t) t)
1746(idlwave-define-abbrev "xor" "xor" (idlwave-keyword-abbrev 0 t) t)
f32b3b91
CD
1747
1748(defvar imenu-create-index-function)
1749(defvar extract-index-name-function)
1750(defvar prev-index-position-function)
1751(defvar imenu-extract-index-name-function)
1752(defvar imenu-prev-index-position-function)
5e72c6b2 1753;; defined later - so just make the compiler hush
52a244eb 1754(defvar idlwave-mode-menu)
f32b3b91
CD
1755(defvar idlwave-mode-debug-menu)
1756
1757;;;###autoload
1758(defun idlwave-mode ()
52a244eb 1759 "Major mode for editing IDL source files (version 5.5).
f32b3b91
CD
1760
1761The main features of this mode are
1762
17631. Indentation and Formatting
1764 --------------------------
1765 Like other Emacs programming modes, C-j inserts a newline and indents.
1766 TAB is used for explicit indentation of the current line.
1767
5e72c6b2
S
1768 To start a continuation line, use \\[idlwave-split-line]. This
1769 function can also be used in the middle of a line to split the line
1770 at that point. When used inside a long constant string, the string
1771 is split at that point with the `+' concatenation operator.
f32b3b91
CD
1772
1773 Comments are indented as follows:
1774
1775 `;;;' Indentation remains unchanged.
1776 `;;' Indent like the surrounding code
1777 `;' Indent to a minimum column.
1778
1779 The indentation of comments starting in column 0 is never changed.
1780
5e72c6b2
S
1781 Use \\[idlwave-fill-paragraph] to refill a paragraph inside a
1782 comment. The indentation of the second line of the paragraph
1783 relative to the first will be retained. Use
1784 \\[idlwave-auto-fill-mode] to toggle auto-fill mode for these
1785 comments. When the variable `idlwave-fill-comment-line-only' is
52a244eb 1786 nil, code can also be auto-filled and auto-indented.
f32b3b91
CD
1787
1788 To convert pre-existing IDL code to your formatting style, mark the
5e72c6b2
S
1789 entire buffer with \\[mark-whole-buffer] and execute
1790 \\[idlwave-expand-region-abbrevs]. Then mark the entire buffer
1791 again followed by \\[indent-region] (`indent-region').
f32b3b91
CD
1792
17932. Routine Info
1794 ------------
5e72c6b2
S
1795 IDLWAVE displays information about the calling sequence and the
1796 accepted keyword parameters of a procedure or function with
1797 \\[idlwave-routine-info]. \\[idlwave-find-module] jumps to the
1798 source file of a module. These commands know about system
1799 routines, all routines in idlwave-mode buffers and (when the
1800 idlwave-shell is active) about all modules currently compiled under
52a244eb
S
1801 this shell. It also makes use of pre-compiled or custom-scanned
1802 user and library catalogs many popular libraries ship with by
1803 default. Use \\[idlwave-update-routine-info] to update this
15e42531
CD
1804 information, which is also used for completion (see item 4).
1805
18063. Online IDL Help
1807 ---------------
1808 \\[idlwave-context-help] displays the IDL documentation relevant
52a244eb
S
1809 for the system variable, keyword, or routine at point. A single
1810 key stroke gets you directly to the right place in the docs. The
1811 HTML help files package must be installed for this to work -- check
1812 the IDLWAVE webpage for the correct package for your version. See
1813 the manual to configure where and how the HTML help is displayed.
f32b3b91 1814
15e42531 18154. Completion
f32b3b91 1816 ----------
15e42531 1817 \\[idlwave-complete] completes the names of procedures, functions
52a244eb
S
1818 class names, keyword parameters, system variables and tags, class
1819 tags, structure tags, filenames and much more. It is context
1820 sensitive and figures out what is expected at point. Lower case
1821 strings are completed in lower case, other strings in mixed or
1822 upper case.
f32b3b91 1823
15e42531 18245. Code Templates and Abbreviations
f32b3b91
CD
1825 --------------------------------
1826 Many Abbreviations are predefined to expand to code fragments and templates.
1827 The abbreviations start generally with a `\\`. Some examples
1828
1829 \\pr PROCEDURE template
1830 \\fu FUNCTION template
1831 \\c CASE statement template
05a1abfc 1832 \\sw SWITCH statement template
f32b3b91
CD
1833 \\f FOR loop template
1834 \\r REPEAT Loop template
1835 \\w WHILE loop template
1836 \\i IF statement template
1837 \\elif IF-ELSE statement template
1838 \\b BEGIN
52a244eb
S
1839
1840 For a full list, use \\[idlwave-list-abbrevs]. Some templates also
1841 have direct keybindings - see the list of keybindings below.
775591f7 1842
52a244eb
S
1843 \\[idlwave-doc-header] inserts a documentation header at the
1844 beginning of the current program unit (pro, function or main).
1845 Change log entries can be added to the current program unit with
1846 \\[idlwave-doc-modification].
f32b3b91 1847
15e42531 18486. Automatic Case Conversion
f32b3b91
CD
1849 -------------------------
1850 The case of reserved words and some abbrevs is controlled by
1851 `idlwave-reserved-word-upcase' and `idlwave-abbrev-change-case'.
1852
15e42531 18537. Automatic END completion
f32b3b91
CD
1854 ------------------------
1855 If the variable `idlwave-expand-generic-end' is non-nil, each END typed
1856 will be converted to the specific version, like ENDIF, ENDFOR, etc.
1857
15e42531 18588. Hooks
f32b3b91
CD
1859 -----
1860 Loading idlwave.el runs `idlwave-load-hook'.
1861 Turning on `idlwave-mode' runs `idlwave-mode-hook'.
1862
15e42531 18639. Documentation and Customization
f32b3b91 1864 -------------------------------
5e72c6b2
S
1865 Info documentation for this package is available. Use
1866 \\[idlwave-info] to display (complain to your sysadmin if that does
1867 not work). For Postscript, PDF, and HTML versions of the
1868 documentation, check IDLWAVE's homepage at `http://idlwave.org'.
f32b3b91
CD
1869 IDLWAVE has customize support - see the group `idlwave'.
1870
15e42531 187110.Keybindings
f32b3b91
CD
1872 -----------
1873 Here is a list of all keybindings of this mode.
1874 If some of the key bindings below show with ??, use \\[describe-key]
1875 followed by the key sequence to see what the key sequence does.
1876
1877\\{idlwave-mode-map}"
1878
1879 (interactive)
1880 (kill-all-local-variables)
52a244eb 1881
f32b3b91
CD
1882 (if idlwave-startup-message
1883 (message "Emacs IDLWAVE mode version %s." idlwave-mode-version))
1884 (setq idlwave-startup-message nil)
52a244eb 1885
f32b3b91
CD
1886 (setq local-abbrev-table idlwave-mode-abbrev-table)
1887 (set-syntax-table idlwave-mode-syntax-table)
52a244eb 1888
f32b3b91 1889 (set (make-local-variable 'indent-line-function) 'idlwave-indent-and-action)
52a244eb 1890
f32b3b91
CD
1891 (make-local-variable idlwave-comment-indent-function)
1892 (set idlwave-comment-indent-function 'idlwave-comment-hook)
52a244eb 1893
f32b3b91
CD
1894 (set (make-local-variable 'comment-start-skip) ";+[ \t]*")
1895 (set (make-local-variable 'comment-start) ";")
1896 (set (make-local-variable 'require-final-newline) t)
1897 (set (make-local-variable 'abbrev-all-caps) t)
1898 (set (make-local-variable 'indent-tabs-mode) nil)
1899 (set (make-local-variable 'completion-ignore-case) t)
52a244eb 1900
f32b3b91
CD
1901 (use-local-map idlwave-mode-map)
1902
1903 (when (featurep 'easymenu)
1904 (easy-menu-add idlwave-mode-menu idlwave-mode-map)
1905 (easy-menu-add idlwave-mode-debug-menu idlwave-mode-map))
1906
1907 (setq mode-name "IDLWAVE")
1908 (setq major-mode 'idlwave-mode)
1909 (setq abbrev-mode t)
52a244eb 1910
f32b3b91
CD
1911 (set (make-local-variable idlwave-fill-function) 'idlwave-auto-fill)
1912 (setq comment-end "")
1913 (set (make-local-variable 'comment-multi-line) nil)
52a244eb 1914 (set (make-local-variable 'paragraph-separate)
5e72c6b2 1915 "[ \t\f]*$\\|[ \t]*;+[ \t]*$\\|;+[+=-_*]+$")
f32b3b91
CD
1916 (set (make-local-variable 'paragraph-start) "[ \t\f]\\|[ \t]*;+[ \t]")
1917 (set (make-local-variable 'paragraph-ignore-fill-prefix) nil)
76959b77 1918 (set (make-local-variable 'parse-sexp-ignore-comments) t)
775591f7 1919
f32b3b91
CD
1920 ;; Set tag table list to use IDLTAGS as file name.
1921 (if (boundp 'tag-table-alist)
1922 (add-to-list 'tag-table-alist '("\\.pro$" . "IDLTAGS")))
52a244eb 1923
f32b3b91 1924 ;; Font-lock additions - originally Phil Williams, then Ulrik Dickow
52a244eb 1925 ;; Following line is for Emacs - XEmacs uses the corresponding property
f32b3b91
CD
1926 ;; on the `idlwave-mode' symbol.
1927 (set (make-local-variable 'font-lock-defaults) idlwave-font-lock-defaults)
1928
1929 ;; Imenu setup
1930 (set (make-local-variable 'imenu-create-index-function)
1931 'imenu-default-create-index-function)
1932 (set (make-local-variable 'imenu-extract-index-name-function)
1933 'idlwave-unit-name)
1934 (set (make-local-variable 'imenu-prev-index-position-function)
1935 'idlwave-prev-index-position)
1936
1937 ;; Make a local post-command-hook and add our hook to it
5e72c6b2
S
1938 ;; NB: `make-local-hook' needed for older/alternative Emacs compatibility
1939 (make-local-hook 'post-command-hook)
15e42531
CD
1940 (add-hook 'post-command-hook 'idlwave-command-hook nil 'local)
1941
1942 ;; Make local hooks for buffer updates
5e72c6b2
S
1943 ;; NB: `make-local-hook' needed for older/alternative Emacs compatibility
1944 (make-local-hook 'kill-buffer-hook)
15e42531 1945 (add-hook 'kill-buffer-hook 'idlwave-kill-buffer-update nil 'local)
5e72c6b2 1946 (make-local-hook 'after-save-hook)
15e42531
CD
1947 (add-hook 'after-save-hook 'idlwave-save-buffer-update nil 'local)
1948 (add-hook 'after-save-hook 'idlwave-revoke-license-to-kill nil 'local)
1949
52a244eb
S
1950 ;; Setup directories and file, if necessary
1951 (idlwave-setup)
1952
15e42531
CD
1953 ;; Update the routine info with info about current buffer?
1954 (idlwave-new-buffer-update)
f32b3b91
CD
1955
1956 ;; Run the mode hook
1957 (run-hooks 'idlwave-mode-hook))
1958
52a244eb
S
1959(defvar idlwave-setup-done nil)
1960(defun idlwave-setup ()
1961 (unless idlwave-setup-done
1962 (if (not (file-directory-p idlwave-config-directory))
1963 (make-directory idlwave-config-directory))
1964 (setq idlwave-user-catalog-file (expand-file-name
1965 idlwave-user-catalog-file
1966 idlwave-config-directory)
1967 idlwave-path-file (expand-file-name
1968 idlwave-path-file
1969 idlwave-config-directory))
1970 (idlwave-read-paths) ; we may need these early
1971 (setq idlwave-setup-done t)))
1972
f32b3b91 1973;;
52a244eb
S
1974;; Code Formatting ----------------------------------------------------
1975;;
f32b3b91
CD
1976
1977(defun idlwave-push-mark (&rest rest)
1978 "Push mark for compatibility with Emacs 18/19."
1979 (if (fboundp 'iconify-frame)
1980 (apply 'push-mark rest)
1981 (push-mark)))
1982
1983(defun idlwave-hard-tab ()
1984 "Inserts TAB in buffer in current position."
1985 (interactive)
1986 (insert "\t"))
1987
1988;;; This stuff is experimental
1989
1990(defvar idlwave-command-hook nil
1991 "If non-nil, a list that can be evaluated using `eval'.
1992It is evaluated in the lisp function `idlwave-command-hook' which is
1993placed in `post-command-hook'.")
1994
1995(defun idlwave-command-hook ()
1996 "Command run after every command.
1997Evaluates a non-nil value of the *variable* `idlwave-command-hook' and
1998sets the variable to zero afterwards."
1999 (and idlwave-command-hook
2000 (listp idlwave-command-hook)
2001 (condition-case nil
2002 (eval idlwave-command-hook)
2003 (error nil)))
2004 (setq idlwave-command-hook nil))
2005
2006;;; End experiment
2007
2008;; It would be better to use expand.el for better abbrev handling and
2009;; versatility.
2010
2011(defun idlwave-check-abbrev (arg &optional reserved)
2012 "Reverses abbrev expansion if in comment or string.
2013Argument ARG is the number of characters to move point
2014backward if `idlwave-abbrev-move' is non-nil.
2015If optional argument RESERVED is non-nil then the expansion
2016consists of reserved words, which will be capitalized if
2017`idlwave-reserved-word-upcase' is non-nil.
2018Otherwise, the abbrev will be capitalized if `idlwave-abbrev-change-case'
2019is non-nil, unless its value is \`down in which case the abbrev will be
2020made into all lowercase.
2021Returns non-nil if abbrev is left expanded."
2022 (if (idlwave-quoted)
2023 (progn (unexpand-abbrev)
2024 nil)
2025 (if (and reserved idlwave-reserved-word-upcase)
2026 (upcase-region last-abbrev-location (point))
2027 (cond
2028 ((equal idlwave-abbrev-change-case 'down)
2029 (downcase-region last-abbrev-location (point)))
2030 (idlwave-abbrev-change-case
2031 (upcase-region last-abbrev-location (point)))))
2032 (if (and idlwave-abbrev-move (> arg 0))
2033 (if (boundp 'post-command-hook)
2034 (setq idlwave-command-hook (list 'backward-char (1+ arg)))
2035 (backward-char arg)))
2036 t))
2037
2038(defun idlwave-in-comment ()
2039 "Returns t if point is inside a comment, nil otherwise."
2040 (save-excursion
2041 (let ((here (point)))
2042 (and (idlwave-goto-comment) (> here (point))))))
2043
2044(defun idlwave-goto-comment ()
2045 "Move to start of comment delimiter on current line.
2046Moves to end of line if there is no comment delimiter.
2047Ignores comment delimiters in strings.
2048Returns point if comment found and nil otherwise."
2049 (let ((eos (progn (end-of-line) (point)))
2050 (data (match-data))
2051 found)
2052 ;; Look for first comment delimiter not in a string
2053 (beginning-of-line)
2054 (setq found (search-forward comment-start eos 'lim))
2055 (while (and found (idlwave-in-quote))
2056 (setq found (search-forward comment-start eos 'lim)))
2057 (store-match-data data)
2058 (and found (not (idlwave-in-quote))
2059 (progn
2060 (backward-char 1)
2061 (point)))))
2062
5e72c6b2
S
2063(defvar transient-mark-mode)
2064(defvar zmacs-regions)
2065(defvar mark-active)
2066(defun idlwave-region-active-p ()
52a244eb 2067 "Is transient-mark-mode on and the region active?
5e72c6b2
S
2068Works on both Emacs and XEmacs."
2069 (if (featurep 'xemacs)
2070 (and zmacs-regions (region-active-p))
2071 (and transient-mark-mode mark-active)))
2072
f32b3b91
CD
2073(defun idlwave-show-matching-quote ()
2074 "Insert quote and show matching quote if this is end of a string."
2075 (interactive)
2076 (let ((bq (idlwave-in-quote))
2077 (inq last-command-char))
2078 (if (and bq (not (idlwave-in-comment)))
2079 (let ((delim (char-after bq)))
2080 (insert inq)
2081 (if (eq inq delim)
2082 (save-excursion
2083 (goto-char bq)
2084 (sit-for 1))))
2085 ;; Not the end of a string
2086 (insert inq))))
2087
2088(defun idlwave-show-begin-check ()
2089 "Ensure that the previous word was a token before `idlwave-show-begin'.
2090An END token must be preceded by whitespace."
5e72c6b2
S
2091 (if (not (idlwave-quoted))
2092 (if
2093 (save-excursion
2094 (backward-word 1)
2095 (backward-char 1)
2096 (looking-at "[ \t\n\f]"))
2097 (idlwave-show-begin))))
f32b3b91
CD
2098
2099(defun idlwave-show-begin ()
2100 "Finds the start of current block and blinks to it for a second.
2101Also checks if the correct end statement has been used."
2102 ;; All end statements are reserved words
76959b77 2103 ;; Re-indent end line
52a244eb
S
2104 ;;(insert-char ?\ 1) ;; So indent, etc. work well
2105 ;;(backward-char 1)
76959b77
S
2106 (let* ((pos (point-marker))
2107 (last-abbrev-marker (copy-marker last-abbrev-location))
2108 (eol-pos (save-excursion (end-of-line) (point)))
2109 begin-pos end-pos end end1 )
2110 (if idlwave-reindent-end (idlwave-indent-line))
52a244eb 2111 (setq last-abbrev-location (marker-position last-abbrev-marker))
f32b3b91
CD
2112 (when (and (idlwave-check-abbrev 0 t)
2113 idlwave-show-block)
2114 (save-excursion
2115 ;; Move inside current block
76959b77 2116 (goto-char last-abbrev-marker)
f32b3b91 2117 (idlwave-block-jump-out -1 'nomark)
76959b77
S
2118 (setq begin-pos (point))
2119 (idlwave-block-jump-out 1 'nomark)
2120 (setq end-pos (point))
2121 (if (> end-pos eol-pos)
2122 (setq end-pos pos))
2123 (goto-char end-pos)
52a244eb 2124 (setq end (buffer-substring
76959b77
S
2125 (progn
2126 (skip-chars-backward "a-zA-Z")
2127 (point))
2128 end-pos))
2129 (goto-char begin-pos)
f32b3b91
CD
2130 (when (setq end1 (cdr (idlwave-block-master)))
2131 (cond
5e72c6b2 2132 ((null end1)) ; no-operation
f32b3b91
CD
2133 ((string= (downcase end) (downcase end1))
2134 (sit-for 1))
2135 ((string= (downcase end) "end")
2136 ;; A generic end
2137 (if idlwave-expand-generic-end
2138 (save-excursion
2139 (goto-char pos)
2140 (backward-char 3)
2141 (insert (if (string= end "END") (upcase end1) end1))
2142 (delete-char 3)))
2143 (sit-for 1))
2144 (t
2145 (beep)
52a244eb 2146 (message "Warning: Shouldn't this be \"%s\" instead of \"%s\"?"
f32b3b91 2147 end1 end)
52a244eb
S
2148 (sit-for 1))))))))
2149 ;;(delete-char 1))
f32b3b91
CD
2150
2151(defun idlwave-block-master ()
2152 (let ((case-fold-search t))
2153 (save-excursion
2154 (cond
05a1abfc 2155 ((looking-at "pro\\|case\\|switch\\|function\\>")
f32b3b91
CD
2156 (assoc (downcase (match-string 0)) idlwave-block-matches))
2157 ((looking-at "begin\\>")
52a244eb
S
2158 (let ((limit (save-excursion
2159 (idlwave-beginning-of-statement)
f32b3b91
CD
2160 (point))))
2161 (cond
52a244eb
S
2162 ((re-search-backward ":[ \t]*\\=" limit t)
2163 ;; seems to be a case thing
2164 '("begin" . "end"))
f32b3b91
CD
2165 ((re-search-backward idlwave-block-match-regexp limit t)
2166 (assoc (downcase (match-string 1))
2167 idlwave-block-matches))
f32b3b91 2168 (t
52a244eb 2169 ;; Just a normal block
f32b3b91
CD
2170 '("begin" . "end")))))
2171 (t nil)))))
2172
2173(defun idlwave-close-block ()
2174 "Terminate the current block with the correct END statement."
2175 (interactive)
2176
2177 ;; Start new line if we are not in a new line
2178 (unless (save-excursion
2179 (skip-chars-backward " \t")
2180 (bolp))
2181 (let ((idlwave-show-block nil))
2182 (newline-and-indent)))
5e72c6b2
S
2183 (let ((last-abbrev-location (point))) ; for upcasing
2184 (insert "end")
2185 (idlwave-show-begin)))
2186
52a244eb 2187(defun idlwave-gtr-pad-hook (char)
5e72c6b2 2188 "Let the > symbol expand around -> if present. The new token length
52a244eb 2189is returned."
5e72c6b2 2190 2)
f32b3b91 2191
5e72c6b2 2192(defun idlwave-surround (&optional before after escape-chars length ec-hook)
595ab50b
CD
2193 "Surround the LENGTH characters before point with blanks.
2194LENGTH defaults to 1.
f32b3b91 2195Optional arguments BEFORE and AFTER affect the behavior before and
595ab50b
CD
2196after the characters (see also description of `idlwave-make-space'):
2197
2198nil do nothing
21990 force no spaces
2200integer > 0 force exactly n spaces
2201integer < 0 at least |n| spaces
f32b3b91
CD
2202
2203The function does nothing if any of the following conditions is true:
2204- `idlwave-surround-by-blank' is nil
2205- the character before point is inside a string or comment
595ab50b
CD
2206- the char preceeding the string to be surrounded is a member of ESCAPE-CHARS.
2207 This hack is used to avoid padding of `>' when it is part of
5e72c6b2
S
2208 the '->' operator. In this case, ESCAPE-CHARS would be '(?-).
2209
2210If a function is passed in EC-HOOK, and an ESCAPE-CHARS match occurs,
2211the named function will be called with a single argument of the
2212preceeding character. Then idlwave-surround will run as usual if
2213EC-HOOK returns non-nil, and a new length will be taken from the
2214return value."
2215 (when (and idlwave-surround-by-blank (not (idlwave-quoted)))
2216 (let* ((length (or length 1)) ; establish a default for LENGTH
2217 (prev-char (char-after (- (point) (1+ length)))))
2218 (when (or (not (memq prev-char escape-chars))
52a244eb
S
2219 (and (fboundp ec-hook)
2220 (setq length
5e72c6b2
S
2221 (save-excursion (funcall ec-hook prev-char)))))
2222 (backward-char length)
2223 (save-restriction
2224 (let ((here (point)))
2225 (skip-chars-backward " \t")
2226 (if (bolp)
2227 ;; avoid clobbering indent
2228 (progn
2229 (move-to-column (idlwave-calculate-indent))
2230 (if (<= (point) here)
2231 (narrow-to-region (point) here))
2232 (goto-char here)))
2233 (idlwave-make-space before))
2234 (skip-chars-forward " \t"))
2235 (forward-char length)
2236 (idlwave-make-space after)
2237 ;; Check to see if the line should auto wrap
2238 (if (and (equal (char-after (1- (point))) ?\ )
2239 (> (current-column) fill-column))
2240 (funcall auto-fill-function))))))
f32b3b91
CD
2241
2242(defun idlwave-make-space (n)
2243 "Make space at point.
2244The space affected is all the spaces and tabs around point.
2245If n is non-nil then point is left abs(n) spaces from the beginning of
2246the contiguous space.
2247The amount of space at point is determined by N.
2248If the value of N is:
2249nil - do nothing.
595ab50b
CD
2250> 0 - exactly N spaces.
2251< 0 - a minimum of -N spaces, i.e., do not change if there are
2252 already -N spaces.
22530 - no spaces (i.e. remove any existing space)."
f32b3b91
CD
2254 (if (integerp n)
2255 (let
2256 ((start-col (progn (skip-chars-backward " \t") (current-column)))
2257 (left (point))
2258 (end-col (progn (skip-chars-forward " \t") (current-column))))
2259 (delete-horizontal-space)
2260 (cond
2261 ((> n 0)
2262 (idlwave-indent-to (+ start-col n))
2263 (goto-char (+ left n)))
2264 ((< n 0)
2265 (idlwave-indent-to end-col (- n))
2266 (goto-char (- left n)))
2267 ;; n = 0, done
2268 ))))
2269
2270(defun idlwave-newline ()
2271 "Inserts a newline and indents the current and previous line."
2272 (interactive)
2273 ;;
2274 ;; Handle unterminated single and double quotes
2275 ;; If not in a comment and in a string then insertion of a newline
2276 ;; will mean unbalanced quotes.
2277 ;;
2278 (if (and (not (idlwave-in-comment)) (idlwave-in-quote))
2279 (progn (beep)
2280 (message "Warning: unbalanced quotes?")))
2281 (newline)
2282 ;;
2283 ;; The current line is being split, the cursor should be at the
2284 ;; beginning of the new line skipping the leading indentation.
2285 ;;
2286 ;; The reason we insert the new line before indenting is that the
2287 ;; indenting could be confused by keywords (e.g. END) on the line
2288 ;; after the split point. This prevents us from just using
2289 ;; `indent-for-tab-command' followed by `newline-and-indent'.
2290 ;;
2291 (beginning-of-line 0)
2292 (idlwave-indent-line)
2293 (forward-line)
2294 (idlwave-indent-line))
2295
2296;;
2297;; Use global variable 'comment-column' to set parallel comment
2298;;
2299;; Modeled on lisp.el
2300;; Emacs Lisp and IDL (Wave CL) have identical comment syntax
2301(defun idlwave-comment-hook ()
2302 "Compute indent for the beginning of the IDL comment delimiter."
2303 (if (or (looking-at idlwave-no-change-comment)
2304 (if idlwave-begin-line-comment
2305 (looking-at idlwave-begin-line-comment)
2306 (looking-at "^;")))
2307 (current-column)
2308 (if (looking-at idlwave-code-comment)
2309 (if (save-excursion (skip-chars-backward " \t") (bolp))
2310 ;; On line by itself, indent as code
2311 (let ((tem (idlwave-calculate-indent)))
2312 (if (listp tem) (car tem) tem))
2313 ;; after code - do not change
2314 (current-column))
2315 (skip-chars-backward " \t")
2316 (max (if (bolp) 0 (1+ (current-column)))
2317 comment-column))))
2318
2319(defun idlwave-split-line ()
2320 "Continue line by breaking line at point and indent the lines.
2321For a code line insert continuation marker. If the line is a line comment
2322then the new line will contain a comment with the same indentation.
2323Splits strings with the IDL operator `+' if `idlwave-split-line-string' is
2324non-nil."
2325 (interactive)
15e42531
CD
2326 ;; Expand abbreviation, just like normal RET would.
2327 (and abbrev-mode (expand-abbrev))
f32b3b91
CD
2328 (let (beg)
2329 (if (not (idlwave-in-comment))
2330 ;; For code line add continuation.
2331 ;; Check if splitting a string.
2332 (progn
2333 (if (setq beg (idlwave-in-quote))
2334 (if idlwave-split-line-string
2335 ;; Split the string.
2336 (progn (insert (setq beg (char-after beg)) " + "
2337 idlwave-continuation-char beg)
5e72c6b2
S
2338 (backward-char 1)
2339 (newline-and-indent)
2340 (forward-char 1))
f32b3b91
CD
2341 ;; Do not split the string.
2342 (beep)
2343 (message "Warning: continuation inside string!!")
2344 (insert " " idlwave-continuation-char))
2345 ;; Not splitting a string.
15e42531
CD
2346 (if (not (member (char-before) '(?\ ?\t)))
2347 (insert " "))
5e72c6b2
S
2348 (insert idlwave-continuation-char)
2349 (newline-and-indent)))
f32b3b91
CD
2350 (indent-new-comment-line))
2351 ;; Indent previous line
2352 (setq beg (- (point-max) (point)))
2353 (forward-line -1)
2354 (idlwave-indent-line)
2355 (goto-char (- (point-max) beg))
2356 ;; Reindent new line
2357 (idlwave-indent-line)))
2358
2359(defun idlwave-beginning-of-subprogram ()
2360 "Moves point to the beginning of the current program unit."
2361 (interactive)
2362 (idlwave-find-key idlwave-begin-unit-reg -1))
2363
2364(defun idlwave-end-of-subprogram ()
2365 "Moves point to the start of the next program unit."
2366 (interactive)
2367 (idlwave-end-of-statement)
2368 (idlwave-find-key idlwave-end-unit-reg 1))
2369
2370(defun idlwave-mark-statement ()
2371 "Mark current IDL statement."
2372 (interactive)
2373 (idlwave-end-of-statement)
2374 (let ((end (point)))
2375 (idlwave-beginning-of-statement)
2376 (idlwave-push-mark end nil t)))
2377
2378(defun idlwave-mark-block ()
2379 "Mark containing block."
2380 (interactive)
2381 (idlwave-end-of-statement)
2382 (idlwave-backward-up-block -1)
2383 (idlwave-end-of-statement)
2384 (let ((end (point)))
2385 (idlwave-backward-block)
2386 (idlwave-beginning-of-statement)
2387 (idlwave-push-mark end nil t)))
2388
2389
2390(defun idlwave-mark-subprogram ()
2391 "Put mark at beginning of program, point at end.
2392The marks are pushed."
2393 (interactive)
2394 (idlwave-end-of-statement)
2395 (idlwave-beginning-of-subprogram)
2396 (let ((beg (point)))
2397 (idlwave-forward-block)
2398 (idlwave-push-mark beg nil t))
2399 (exchange-point-and-mark))
2400
2401(defun idlwave-backward-up-block (&optional arg)
2402 "Move to beginning of enclosing block if prefix ARG >= 0.
2403If prefix ARG < 0 then move forward to enclosing block end."
2404 (interactive "p")
2405 (idlwave-block-jump-out (- arg) 'nomark))
2406
2407(defun idlwave-beginning-of-block ()
2408 "Go to the beginning of the current block."
2409 (interactive)
2410 (idlwave-block-jump-out -1 'nomark)
2411 (forward-word 1))
2412
2413(defun idlwave-end-of-block ()
2414 "Go to the beginning of the current block."
2415 (interactive)
2416 (idlwave-block-jump-out 1 'nomark)
2417 (backward-word 1))
2418
2419(defun idlwave-forward-block ()
2420 "Move across next nested block."
2421 (interactive)
2422 (if (idlwave-down-block 1)
2423 (idlwave-block-jump-out 1 'nomark)))
2424
2425(defun idlwave-backward-block ()
2426 "Move backward across previous nested block."
2427 (interactive)
2428 (if (idlwave-down-block -1)
2429 (idlwave-block-jump-out -1 'nomark)))
2430
2431(defun idlwave-down-block (&optional arg)
2432 "Go down a block.
2433With ARG: ARG >= 0 go forwards, ARG < 0 go backwards.
2434Returns non-nil if successfull."
2435 (interactive "p")
2436 (let (status)
2437 (if (< arg 0)
2438 ;; Backward
2439 (let ((eos (save-excursion
2440 (idlwave-block-jump-out -1 'nomark)
2441 (point))))
52a244eb 2442 (if (setq status (idlwave-find-key
f32b3b91
CD
2443 idlwave-end-block-reg -1 'nomark eos))
2444 (idlwave-beginning-of-statement)
2445 (message "No nested block before beginning of containing block.")))
2446 ;; Forward
2447 (let ((eos (save-excursion
2448 (idlwave-block-jump-out 1 'nomark)
2449 (point))))
52a244eb 2450 (if (setq status (idlwave-find-key
f32b3b91
CD
2451 idlwave-begin-block-reg 1 'nomark eos))
2452 (idlwave-end-of-statement)
2453 (message "No nested block before end of containing block."))))
2454 status))
2455
2456(defun idlwave-mark-doclib ()
2457 "Put point at beginning of doc library header, mark at end.
2458The marks are pushed."
2459 (interactive)
2460 (let (beg
2461 (here (point)))
2462 (goto-char (point-max))
2463 (if (re-search-backward idlwave-doclib-start nil t)
52a244eb 2464 (progn
f32b3b91
CD
2465 (setq beg (progn (beginning-of-line) (point)))
2466 (if (re-search-forward idlwave-doclib-end nil t)
2467 (progn
2468 (forward-line 1)
2469 (idlwave-push-mark beg nil t)
2470 (message "Could not find end of doc library header.")))
2471 (message "Could not find doc library header start.")
2472 (goto-char here)))))
2473
15e42531
CD
2474
2475(defun idlwave-current-routine ()
2476 "Return (NAME TYPE CLASS) of current routine."
2477 (idlwave-routines)
2478 (save-excursion
2479 (idlwave-beginning-of-subprogram)
2480 (if (looking-at "[ \t]*\\<\\(pro\\|function\\)\\>\\s-+\\(\\([a-zA-Z0-9$_]+\\)::\\)?\\([a-zA-Z0-9$_]+\\)")
2481 (let* ((type (if (string= (downcase (match-string 1)) "pro")
2482 'pro 'function))
2483 (class (idlwave-sintern-class (match-string 3)))
2484 (name (idlwave-sintern-routine-or-method (match-string 4) class)))
2485 (list name type class)))))
2486
f32b3b91
CD
2487(defvar idlwave-shell-prompt-pattern)
2488(defun idlwave-beginning-of-statement ()
2489 "Move to beginning of the current statement.
2490Skips back past statement continuations.
2491Point is placed at the beginning of the line whether or not this is an
2492actual statement."
2493 (interactive)
2494 (cond
2495 ((eq major-mode 'idlwave-shell-mode)
2496 (if (re-search-backward idlwave-shell-prompt-pattern nil t)
2497 (goto-char (match-end 0))))
52a244eb 2498 (t
f32b3b91
CD
2499 (if (save-excursion (forward-line -1) (idlwave-is-continuation-line))
2500 (idlwave-previous-statement)
2501 (beginning-of-line)))))
2502
2503(defun idlwave-previous-statement ()
2504 "Moves point to beginning of the previous statement.
2505Returns t if the current line before moving is the beginning of
2506the first non-comment statement in the file, and nil otherwise."
2507 (interactive)
2508 (let (first-statement)
2509 (if (not (= (forward-line -1) 0))
2510 ;; first line in file
2511 t
2512 ;; skip blank lines, label lines, include lines and line comments
2513 (while (and
2514 ;; The current statement is the first statement until we
2515 ;; reach another statement.
2516 (setq first-statement
2517 (or
2518 (looking-at idlwave-comment-line-start-skip)
2519 (looking-at "[ \t]*$")
2520 (looking-at (concat "[ \t]*" idlwave-label "[ \t]*$"))
2521 (looking-at "^@")))
2522 (= (forward-line -1) 0)))
2523 ;; skip continuation lines
2524 (while (and
2525 (save-excursion
2526 (forward-line -1)
2527 (idlwave-is-continuation-line))
2528 (= (forward-line -1) 0)))
2529 first-statement)))
2530
f32b3b91
CD
2531(defun idlwave-end-of-statement ()
2532 "Moves point to the end of the current IDL statement.
05a1abfc
CD
2533If not in a statement just moves to end of line. Returns position."
2534 (interactive)
2535 (while (and (idlwave-is-continuation-line)
2536 (= (forward-line 1) 0))
2537 (while (and (idlwave-is-comment-or-empty-line)
2538 (= (forward-line 1) 0))))
2539 (end-of-line)
2540 (point))
2541
2542(defun idlwave-end-of-statement0 ()
2543 "Moves point to the end of the current IDL statement.
f32b3b91
CD
2544If not in a statement just moves to end of line. Returns position."
2545 (interactive)
2546 (while (and (idlwave-is-continuation-line)
2547 (= (forward-line 1) 0)))
2548 (end-of-line)
2549 (point))
2550
2551(defun idlwave-next-statement ()
2552 "Moves point to beginning of the next IDL statement.
2553 Returns t if that statement is the last
2554 non-comment IDL statement in the file, and nil otherwise."
2555 (interactive)
2556 (let (last-statement)
2557 (idlwave-end-of-statement)
2558 ;; skip blank lines, label lines, include lines and line comments
2559 (while (and (= (forward-line 1) 0)
2560 ;; The current statement is the last statement until
2561 ;; we reach a new statement.
2562 (setq last-statement
2563 (or
2564 (looking-at idlwave-comment-line-start-skip)
2565 (looking-at "[ \t]*$")
2566 (looking-at (concat "[ \t]*" idlwave-label "[ \t]*$"))
2567 (looking-at "^@")))))
2568 last-statement))
2569
76959b77
S
2570(defun idlwave-skip-multi-commands (&optional lim)
2571 "Skip past multiple commands on a line (with `&')."
2572 (let ((save-point (point)))
2573 (when (re-search-forward ".*&" lim t)
2574 (goto-char (match-end 0))
52a244eb 2575 (if (idlwave-quoted) (goto-char save-point)))
76959b77
S
2576 (point)))
2577
15e42531 2578(defun idlwave-skip-label-or-case ()
f32b3b91
CD
2579 "Skip label or case statement element.
2580Returns position after label.
2581If there is no label point is not moved and nil is returned."
15e42531
CD
2582 ;; Case expressions and labels are terminated by a colon.
2583 ;; So we find the first colon in the line and make sure
2584 ;; - no `?' is before it (might be a ? b : c)
2585 ;; - it is not in a comment
2586 ;; - not in a string constant
2587 ;; - not in parenthesis (like a[0:3])
5e72c6b2 2588 ;; - not followed by another ":" in explicit class, ala a->b::c
15e42531 2589 ;; As many in this mode, this function is heuristic and not an exact
52a244eb 2590 ;; parser.
5e72c6b2
S
2591 (let* ((start (point))
2592 (eos (save-excursion (idlwave-end-of-statement) (point)))
2593 (end (idlwave-find-key ":" 1 'nomark eos)))
f32b3b91 2594 (if (and end
15e42531 2595 (= (nth 0 (parse-partial-sexp start end)) 0)
5e72c6b2
S
2596 (not (string-match "\\?" (buffer-substring start end)))
2597 (not (string-match "^::" (buffer-substring end eos))))
f32b3b91
CD
2598 (progn
2599 (forward-char)
2600 (point))
2601 (goto-char start)
2602 nil)))
2603
2604(defun idlwave-start-of-substatement (&optional pre)
2605 "Move to start of next IDL substatement after point.
2606Uses the type of the current IDL statement to determine if the next
2607statement is on a new line or is a subpart of the current statement.
2608Returns point at start of substatement modulo whitespace.
2609If optional argument is non-nil move to beginning of current
15e42531 2610substatement."
f32b3b91
CD
2611 (let ((orig (point))
2612 (eos (idlwave-end-of-statement))
2613 (ifnest 0)
2614 st nst last)
2615 (idlwave-beginning-of-statement)
15e42531 2616 (idlwave-skip-label-or-case)
52a244eb
S
2617 (if (< (point) orig)
2618 (idlwave-skip-multi-commands orig))
f32b3b91
CD
2619 (setq last (point))
2620 ;; Continue looking for substatements until we are past orig
2621 (while (and (<= (point) orig) (not (eobp)))
2622 (setq last (point))
2623 (setq nst (nth 1 (cdr (setq st (car (idlwave-statement-type))))))
2624 (if (equal (car st) 'if) (setq ifnest (1+ ifnest)))
2625 (cond ((and nst
2626 (idlwave-find-key nst 1 'nomark eos))
2627 (goto-char (match-end 0)))
2628 ((and (> ifnest 0) (idlwave-find-key "\\<else\\>" 1 'nomark eos))
2629 (setq ifnest (1- ifnest))
2630 (goto-char (match-end 0)))
2631 (t (setq ifnest 0)
2632 (idlwave-next-statement))))
2633 (if pre (goto-char last))
15e42531
CD
2634 ;; If a continuation line starts here, move to next line
2635 (if (looking-at "[ \t]*\\$\\([ \t]*\\(;\\|$\\)\\)")
2636 (beginning-of-line 2))
f32b3b91
CD
2637 (point)))
2638
2639(defun idlwave-statement-type ()
2640 "Return the type of the current IDL statement.
2641Uses `idlwave-statement-match' to return a cons of (type . point) with
2642point the ending position where the type was determined. Type is the
2643association from `idlwave-statement-match', i.e. the cons cell from the
2644list not just the type symbol. Returns nil if not an identifiable
2645statement."
2646 (save-excursion
2647 ;; Skip whitespace within a statement which is spaces, tabs, continuations
76959b77
S
2648 ;; and possibly comments
2649 (while (looking-at "[ \t]*\\$")
f32b3b91
CD
2650 (forward-line 1))
2651 (skip-chars-forward " \t")
2652 (let ((st idlwave-statement-match)
2653 (case-fold-search t))
2654 (while (and (not (looking-at (nth 0 (cdr (car st)))))
2655 (setq st (cdr st))))
2656 (if st
2657 (append st (match-end 0))))))
2658
2659(defun idlwave-expand-equal (&optional before after)
52a244eb
S
2660 "Pad '=' with spaces. Two cases: Assignment statement, and keyword
2661assignment. Which case is determined using
2662`idlwave-start-of-substatement' and `idlwave-statement-type'. The
2663equal sign will be surrounded by BEFORE and AFTER blanks. If
2664`idlwave-pad-keyword' is t then keyword assignment is treated just
15e42531
CD
2665like assignment statements. When nil, spaces are removed for keyword
2666assignment. Any other value keeps the current space around the `='.
52a244eb
S
2667Limits in for loops are treated as keyword assignment.
2668
2669Starting with IDL 6.0, a number of op= assignments are available.
2670Since ambiguities of the form:
2671
2672r and= b
2673rand= b
2674
2675can occur, alphanumeric operator assignment will never be pre-padded,
2676only post-padded. You must use a space before these to disambiguate
2677\(not just for padding, but for proper parsing by IDL too!). Other
2678operators, such as ##=, ^=, etc., will be pre-padded.
2679
2680See `idlwave-surround'."
f32b3b91 2681 (if idlwave-surround-by-blank
52a244eb
S
2682 (let
2683 ((non-an-ops "\\(##\\|\\*\\|\\+\\|-\\|/\\|<\\|>\\|\\^\\)\\=")
2684 (an-ops
2685 "\\s-\\(AND\\|EQ\\|GE\\|GT\\|LE\\|LT\\|MOD\\|NE\\|OR\\|XOR\\)\\=")
2686 (len 1))
2687
2688 (save-excursion
2689 (let ((case-fold-search t))
2690 (backward-char)
2691 (if (or
2692 (re-search-backward non-an-ops nil t)
2693 ;; Why doesn't ##? work for both?
2694 (re-search-backward "\\(#\\)\\=" nil t))
2695 (setq len (1+ (length (match-string 1))))
2696 (when (re-search-backward an-ops nil t)
2697 (setq begin nil) ; won't modify begin
2698 (setq len (1+ (length (match-string 1))))))))
2699
2700 (if (eq t idlwave-pad-keyword)
2701 ;; Everything gets padded equally
2702 (idlwave-surround before after nil len)
2703 ;; Treating keywords/for variables specially...
2704 (let ((st (save-excursion ; To catch "for" variables
2705 (idlwave-start-of-substatement t)
2706 (idlwave-statement-type)))
2707 (what (save-excursion ; To catch keywords
2708 (skip-chars-backward "= \t")
2709 (nth 2 (idlwave-where)))))
2710 (cond ((or (memq what '(function-keyword procedure-keyword))
2711 (memq (caar st) '(for pdef)))
2712 (cond
2713 ((null idlwave-pad-keyword)
2714 (idlwave-surround 0 0)
2715 ) ; remove space
2716 (t))) ; leave any spaces alone
2717 (t (idlwave-surround before after nil len))))))))
2718
f32b3b91 2719
5e72c6b2
S
2720(defun idlwave-indent-and-action (&optional arg)
2721 "Call `idlwave-indent-line' and do expand actions.
2722With prefix ARG non-nil, indent the entire sub-statement."
2723 (interactive "p")
05a1abfc 2724 (save-excursion
52a244eb
S
2725 (if (and idlwave-expand-generic-end
2726 (re-search-backward "\\<\\(end\\)\\s-*\\="
05a1abfc
CD
2727 (max 0 (- (point) 10)) t)
2728 (looking-at "\\(end\\)\\([ \n\t]\\|\\'\\)"))
2729 (progn (goto-char (match-end 1))
5e72c6b2
S
2730 ;;Expand the END abbreviation, just as RET or Space would have.
2731 (if abbrev-mode (expand-abbrev)
2732 (idlwave-show-begin)))))
52a244eb
S
2733 (when (and (not arg) current-prefix-arg)
2734 (setq arg current-prefix-arg)
2735 (setq current-prefix-arg nil))
2736 (if arg
5e72c6b2
S
2737 (idlwave-indent-statement)
2738 (idlwave-indent-line t)))
f32b3b91
CD
2739
2740(defun idlwave-indent-line (&optional expand)
2741 "Indents current IDL line as code or as a comment.
2742The actions in `idlwave-indent-action-table' are performed.
2743If the optional argument EXPAND is non-nil then the actions in
2744`idlwave-indent-expand-table' are performed."
2745 (interactive)
2746 ;; Move point out of left margin.
2747 (if (save-excursion
2748 (skip-chars-backward " \t")
2749 (bolp))
2750 (skip-chars-forward " \t"))
2751 (let ((mloc (point-marker)))
2752 (save-excursion
2753 (beginning-of-line)
2754 (if (looking-at idlwave-comment-line-start-skip)
2755 ;; Indentation for a line comment
2756 (progn
2757 (skip-chars-forward " \t")
2758 (idlwave-indent-left-margin (idlwave-comment-hook)))
2759 ;;
2760 ;; Code Line
2761 ;;
2762 ;; Before indenting, run action routines.
2763 ;;
2764 (if (and expand idlwave-do-actions)
2765 (mapcar 'idlwave-do-action idlwave-indent-expand-table))
2766 ;;
2767 (if idlwave-do-actions
2768 (mapcar 'idlwave-do-action idlwave-indent-action-table))
2769 ;;
2770 ;; No longer expand abbrevs on the line. The user can do this
2771 ;; manually using expand-region-abbrevs.
2772 ;;
2773 ;; Indent for code line
2774 ;;
2775 (beginning-of-line)
2776 (if (or
2777 ;; a label line
2778 (looking-at (concat "^" idlwave-label "[ \t]*$"))
2779 ;; a batch command
2780 (looking-at "^[ \t]*@"))
2781 ;; leave flush left
2782 nil
2783 ;; indent the line
2784 (idlwave-indent-left-margin (idlwave-calculate-indent)))
2785 ;; Adjust parallel comment
76959b77
S
2786 (end-of-line)
2787 (if (idlwave-in-comment)
2788 ;; Emacs 21 is too smart with fill-column on comment indent
2789 (let ((fill-column (if (fboundp 'comment-indent-new-line)
2790 (1- (frame-width))
2791 fill-column)))
2792 (indent-for-comment)))))
f32b3b91
CD
2793 (goto-char mloc)
2794 ;; Get rid of marker
76959b77 2795 (set-marker mloc nil)))
f32b3b91
CD
2796
2797(defun idlwave-do-action (action)
2798 "Perform an action repeatedly on a line.
2799ACTION is a list (REG . FUNC). REG is a regular expression. FUNC is
2800either a function name to be called with `funcall' or a list to be
2801evaluated with `eval'. The action performed by FUNC should leave point
2802after the match for REG - otherwise an infinite loop may be entered."
2803 (let ((action-key (car action))
2804 (action-routine (cdr action)))
2805 (beginning-of-line)
2806 (while (idlwave-look-at action-key)
2807 (if (listp action-routine)
2808 (eval action-routine)
2809 (funcall action-routine)))))
2810
2811(defun idlwave-indent-to (col &optional min)
2812 "Indent from point with spaces until column COL.
2813Inserts space before markers at point."
2814 (if (not min) (setq min 0))
2815 (insert-before-markers
15e42531 2816 (make-string (max min (- col (current-column))) ?\ )))
f32b3b91
CD
2817
2818(defun idlwave-indent-left-margin (col)
2819 "Indent the current line to column COL.
2820Indents such that first non-whitespace character is at column COL
2821Inserts spaces before markers at point."
2822 (save-excursion
2823 (beginning-of-line)
2824 (delete-horizontal-space)
2825 (idlwave-indent-to col)))
2826
2827(defun idlwave-indent-subprogram ()
2828 "Indents program unit which contains point."
2829 (interactive)
2830 (save-excursion
2831 (idlwave-end-of-statement)
2832 (idlwave-beginning-of-subprogram)
2833 (let ((beg (point)))
2834 (idlwave-forward-block)
2835 (message "Indenting subprogram...")
2836 (indent-region beg (point) nil))
2837 (message "Indenting subprogram...done.")))
2838
5e72c6b2
S
2839(defun idlwave-indent-statement ()
2840 "Indent current statement, including all continuation lines."
2841 (interactive)
2842 (save-excursion
2843 (idlwave-beginning-of-statement)
2844 (let ((beg (point)))
2845 (idlwave-end-of-statement)
2846 (indent-region beg (point) nil))))
2847
f32b3b91
CD
2848(defun idlwave-calculate-indent ()
2849 "Return appropriate indentation for current line as IDL code."
2850 (save-excursion
2851 (beginning-of-line)
2852 (cond
2853 ;; Check for beginning of unit - main (beginning of buffer), pro, or
2854 ;; function
2855 ((idlwave-look-at idlwave-begin-unit-reg)
2856 0)
2857 ;; Check for continuation line
2858 ((save-excursion
2859 (and (= (forward-line -1) 0)
2860 (idlwave-is-continuation-line)))
2861 (idlwave-calculate-cont-indent))
2862 ;; calculate indent based on previous and current statements
52a244eb
S
2863 (t (let* (beg-prev-pos
2864 (the-indent
2865 ;; calculate indent based on previous statement
2866 (save-excursion
2867 (cond
2868 ;; Beginning of file
2869 ((prog1
2870 (idlwave-previous-statement)
2871 (setq beg-prev-pos (point)))
2872 0)
2873 ;; Main block
2874 ((idlwave-look-at idlwave-begin-unit-reg t)
2875 (+ (idlwave-current-statement-indent)
2876 idlwave-main-block-indent))
2877 ;; Begin block
2878 ((idlwave-look-at idlwave-begin-block-reg t)
2879 (+ (idlwave-min-current-statement-indent)
2880 idlwave-block-indent))
2881 ;; End Block
2882 ((idlwave-look-at idlwave-end-block-reg t)
2883 (progn
2884 ;; Match to the *beginning* of the block opener
2885 (goto-char beg-prev-pos)
2886 (idlwave-block-jump-out -1 'nomark) ; go to begin block
2887 (idlwave-min-current-statement-indent)))
2888 ;; idlwave-end-offset
2889 ;; idlwave-block-indent))
2890
2891 ;; Default to current indent
2892 ((idlwave-current-statement-indent))))))
f32b3b91
CD
2893 ;; adjust the indentation based on the current statement
2894 (cond
2895 ;; End block
5e72c6b2
S
2896 ((idlwave-look-at idlwave-end-block-reg)
2897 (+ the-indent idlwave-end-offset))
f32b3b91
CD
2898 (the-indent)))))))
2899
2900;;
52a244eb 2901;; Parentheses indent
f32b3b91
CD
2902;;
2903
5e72c6b2
S
2904(defun idlwave-calculate-paren-indent (beg-reg end-reg close-exp)
2905 "Calculate the continuation indent inside a paren group.
52a244eb 2906Returns a cons-cell with (open . indent), where open is the
5e72c6b2
S
2907location of the open paren"
2908 (let ((open (nth 1 (parse-partial-sexp beg-reg end-reg))))
2909 ;; Found an innermost open paren.
2910 (when open
2911 (goto-char open)
2912 ;; Line up with next word unless this is a closing paren.
2913 (cons open
2914 (cond
52a244eb
S
2915 ;; Plain Kernighan-style nested indent
2916 (idlwave-indent-parens-nested
2917 (+ idlwave-continuation-indent (idlwave-current-indent)))
2918
5e72c6b2
S
2919 ;; This is a closed paren - line up under open paren.
2920 (close-exp
2921 (current-column))
52a244eb
S
2922
2923 ;; Empty (or just comment) follows -- revert to basic indent
5e72c6b2
S
2924 ((progn
2925 ;; Skip paren
2926 (forward-char 1)
2927 (looking-at "[ \t$]*\\(;.*\\)?$"))
52a244eb
S
2928 nil)
2929
2930 ;; Line up with first word after any blank space
5e72c6b2
S
2931 ((progn
2932 (skip-chars-forward " \t")
2933 (current-column))))))))
2934
f32b3b91 2935(defun idlwave-calculate-cont-indent ()
52a244eb
S
2936 "Calculates the IDL continuation indent column from the previous
2937statement. Note that here previous statement usually means the
2938beginning of the current statement if this statement is a continuation
2939of the previous line. Various special types of continuations,
2940including assignments, routine definitions, and parenthetical
2941groupings, are treated separately."
f32b3b91 2942 (save-excursion
52a244eb 2943 (let* ((case-fold-search t)
f32b3b91 2944 (end-reg (progn (beginning-of-line) (point)))
52a244eb
S
2945 (beg-last-statement (save-excursion (idlwave-previous-statement)
2946 (point)))
2947 (beg-reg (progn (idlwave-start-of-substatement 'pre)
2948 (if (eq (line-beginning-position) end-reg)
2949 (goto-char beg-last-statement)
2950 (point))))
2951 (basic-indent (+ (idlwave-min-current-statement-indent end-reg)
2952 idlwave-continuation-indent))
2953 fancy-nonparen-indent fancy-paren-indent)
2954 (cond
2955 ;; Align then with its matching if, etc.
2956 ((let ((matchers '(("\\<if\\>" . "[ \t]*then")
2957 ("\\<\\(if\\|end\\(if\\)?\\)\\>" . "[ \t]*else")
2958 ("\\<\\(for\\|while\\)\\>" . "[ \t]*do")
2959 ("\\<\\(repeat\\|end\\(rep\\)?\\)\\>" .
2960 "[ \t]*until")
2961 ("\\<case\\>" . "[ \t]*of")))
2962 match cont-re)
2963 (goto-char end-reg)
2964 (and
2965 (setq cont-re
2966 (catch 'exit
2967 (while (setq match (car matchers))
2968 (if (looking-at (cdr match))
2969 (throw 'exit (car match)))
2970 (setq matchers (cdr matchers)))))
2971 (idlwave-find-key cont-re -1 'nomark beg-last-statement)))
2972 (if (looking-at "end") ;; that one's special
2973 (- (idlwave-current-indent)
2974 (+ idlwave-block-indent idlwave-end-offset))
2975 (idlwave-current-indent)))
2976
2977 ;; Indent in from the previous line for continuing statements
2978 ((let ((matchers '("\\<then\\>"
2979 "\\<do\\>"
2980 "\\<repeat\\>"
2981 "\\<else\\>"))
2982 match)
2983 (catch 'exit
2984 (goto-char end-reg)
2985 (if (/= (forward-line -1) 0)
2986 (throw 'exit nil))
2987 (while (setq match (car matchers))
2988 (if (looking-at (concat ".*" match "[ \t]*\\$[ \t]*"
2989 "\\(;.*\\)?$"))
2990 (throw 'exit t))
2991 (setq matchers (cdr matchers)))))
2992 (+ idlwave-continuation-indent (idlwave-current-indent)))
2993
2994 ;; Parenthetical indent, either traditional or Kernighan style
2995 ((setq fancy-paren-indent
2996 (let* ((end-reg end-reg)
2997 (close-exp (progn
2998 (goto-char end-reg)
2999 (skip-chars-forward " \t")
3000 (looking-at "\\s)")))
3001 indent-cons)
3002 (catch 'loop
3003 (while (setq indent-cons (idlwave-calculate-paren-indent
3004 beg-reg end-reg close-exp))
3005 ;; First permitted containing paren
3006 (if (or
3007 idlwave-indent-to-open-paren
3008 idlwave-indent-parens-nested
3009 (null (cdr indent-cons))
3010 (< (- (cdr indent-cons) basic-indent)
3011 idlwave-max-extra-continuation-indent))
3012 (throw 'loop (cdr indent-cons)))
3013 (setq end-reg (car indent-cons))))))
5e72c6b2
S
3014 fancy-paren-indent)
3015
52a244eb
S
3016 ;; A continued assignment, or procedure call/definition
3017 ((and
3018 (> idlwave-max-extra-continuation-indent 0)
3019 (setq fancy-nonparen-indent
3020 (progn
3021 (goto-char beg-reg)
3022 (while (idlwave-look-at "&")) ; skip continued statements
3023 (cond
3024 ;; A continued Procedure call or definition
3025 ((progn
3026 (idlwave-look-at "^[ \t]*\\(pro\\|function\\)") ;skip over
3027 (looking-at "[ \t]*\\([a-zA-Z0-9.$_]+[ \t]*->[ \t]*\\)?[a-zA-Z][:a-zA-Z0-9$_]*[ \t]*\\(,\\)[ \t]*"))
3028 (goto-char (match-end 0))
3029 ;; Comment only, or blank line with "$"? Basic indent.
3030 (if (save-match-data (looking-at "[ \t$]*\\(;.*\\)?$"))
3031 nil
3032 (current-column)))
3033
3034 ;; Continued assignment (with =):
3035 ((catch 'assign ;
3036 (while (looking-at "[^=\n\r]*\\(=\\)[ \t]*")
3037 (goto-char (match-end 0))
3038 (if (null (idlwave-what-function beg-reg))
3039 (throw 'assign t))))
3040 (unless (or
3041 (idlwave-in-quote)
3042 (looking-at "[ \t$]*\\(;.*\\)?$") ; use basic
3043 (save-excursion
3044 (goto-char beg-last-statement)
3045 (eq (caar (idlwave-statement-type)) 'for)))
3046 (current-column))))))
3047 (< (- fancy-nonparen-indent basic-indent)
3048 idlwave-max-extra-continuation-indent))
3049 (if fancy-paren-indent ;calculated but disallowed paren indent
3050 (+ fancy-nonparen-indent idlwave-continuation-indent)
3051 fancy-nonparen-indent))
3052
3053 ;; Basic indent, by default
3054 (t basic-indent)))))
3055
3056
f32b3b91 3057
15e42531
CD
3058(defun idlwave-find-key (key-re &optional dir nomark limit)
3059 "Move to next match of the regular expression KEY-RE.
3060Matches inside comments or string constants will be ignored.
3061If DIR is negative, the search will be backwards.
3062At a successful match, the mark is pushed unless NOMARK is non-nil.
3063Searches are limited to LIMIT.
3064Searches are case-insensitive and use a special syntax table which
3065treats `$' and `_' as word characters.
3066Return value is the beginning of the match or (in case of failure) nil."
3067 (setq dir (or dir 0))
3068 (let ((case-fold-search t)
3069 (search-func (if (> dir 0) 're-search-forward 're-search-backward))
3070 found)
3071 (idlwave-with-special-syntax
3072 (save-excursion
3073 (catch 'exit
3074 (while (funcall search-func key-re limit t)
3075 (if (not (idlwave-quoted))
52a244eb
S
3076 (throw 'exit (setq found (match-beginning 0)))
3077 (if (or (and (> dir 0) (eobp))
3078 (and (< dir 0) (bobp)))
3079 (throw 'exit nil)))))))
15e42531
CD
3080 (if found
3081 (progn
3082 (if (not nomark) (push-mark))
3083 (goto-char found)
3084 found)
3085 nil)))
3086
f32b3b91
CD
3087(defun idlwave-block-jump-out (&optional dir nomark)
3088 "When optional argument DIR is non-negative, move forward to end of
3089current block using the `idlwave-begin-block-reg' and `idlwave-end-block-reg'
3090regular expressions. When DIR is negative, move backwards to block beginning.
3091Recursively calls itself to skip over nested blocks. DIR defaults to
3092forward. Calls `push-mark' unless the optional argument NOMARK is
3093non-nil. Movement is limited by the start of program units because of
3094possibility of unbalanced blocks."
3095 (interactive "P")
3096 (or dir (setq dir 0))
3097 (let* ((here (point))
3098 (case-fold-search t)
3099 (limit (if (>= dir 0) (point-max) (point-min)))
52a244eb 3100 (block-limit (if (>= dir 0)
f32b3b91
CD
3101 idlwave-begin-block-reg
3102 idlwave-end-block-reg))
3103 found
3104 (block-reg (concat idlwave-begin-block-reg "\\|"
3105 idlwave-end-block-reg))
3106 (unit-limit (or (save-excursion
3107 (if (< dir 0)
3108 (idlwave-find-key
3109 idlwave-begin-unit-reg dir t limit)
3110 (end-of-line)
52a244eb 3111 (idlwave-find-key
f32b3b91
CD
3112 idlwave-end-unit-reg dir t limit)))
3113 limit)))
3114 (if (>= dir 0) (end-of-line)) ;Make sure we are in current block
3115 (if (setq found (idlwave-find-key block-reg dir t unit-limit))
3116 (while (and found (looking-at block-limit))
3117 (if (>= dir 0) (forward-word 1))
3118 (idlwave-block-jump-out dir t)
3119 (setq found (idlwave-find-key block-reg dir t unit-limit))))
3120 (if (not nomark) (push-mark here))
3121 (if (not found) (goto-char unit-limit)
3122 (if (>= dir 0) (forward-word 1)))))
3123
52a244eb
S
3124(defun idlwave-min-current-statement-indent (&optional end-reg)
3125 "The minimum indent in the current statement."
3126 (idlwave-beginning-of-statement)
3127 (if (not (idlwave-is-continuation-line))
3128 (idlwave-current-indent)
3129 (let ((min (idlwave-current-indent)) comm-or-empty)
3130 (while (and (= (forward-line 1) 0)
3131 (or (setq comm-or-empty (idlwave-is-comment-or-empty-line))
3132 (idlwave-is-continuation-line))
3133 (or (null end-reg) (< (point) end-reg)))
3134 (unless comm-or-empty (setq min (min min (idlwave-current-indent)))))
3135 (if (or comm-or-empty (and end-reg (>= (point) end-reg)))
3136 min
3137 (min min (idlwave-current-indent))))))
3138
3139(defun idlwave-current-statement-indent (&optional last-line)
f32b3b91
CD
3140 "Return indentation of the current statement.
3141If in a statement, moves to beginning of statement before finding indent."
52a244eb
S
3142 (if last-line
3143 (idlwave-end-of-statement)
3144 (idlwave-beginning-of-statement))
f32b3b91
CD
3145 (idlwave-current-indent))
3146
3147(defun idlwave-current-indent ()
3148 "Return the column of the indentation of the current line.
3149Skips any whitespace. Returns 0 if the end-of-line follows the whitespace."
3150 (save-excursion
3151 (beginning-of-line)
3152 (skip-chars-forward " \t")
3153 ;; if we are at the end of blank line return 0
3154 (cond ((eolp) 0)
3155 ((current-column)))))
3156
3157(defun idlwave-is-continuation-line ()
5e72c6b2
S
3158 "Tests if current line is continuation line.
3159Blank or comment-only lines following regular continuation lines (with
3160`$') count as continuations too."
f32b3b91 3161 (save-excursion
52a244eb 3162 (or
5e72c6b2
S
3163 (idlwave-look-at "\\<\\$")
3164 (catch 'loop
52a244eb 3165 (while (and (looking-at "^[ \t]*\\(;.*\\)?$")
5e72c6b2
S
3166 (eq (forward-line -1) 0))
3167 (if (idlwave-look-at "\\<\\$") (throw 'loop t)))))))
f32b3b91
CD
3168
3169(defun idlwave-is-comment-line ()
05a1abfc 3170 "Tests if the current line is a comment line."
f32b3b91
CD
3171 (save-excursion
3172 (beginning-of-line 1)
3173 (looking-at "[ \t]*;")))
3174
05a1abfc
CD
3175(defun idlwave-is-comment-or-empty-line ()
3176 "Tests if the current line is a comment line."
3177 (save-excursion
3178 (beginning-of-line 1)
3179 (looking-at "[ \t]*[;\n]")))
3180
f32b3b91 3181(defun idlwave-look-at (regexp &optional cont beg)
15e42531
CD
3182 "Searches current line from current point for REGEXP.
3183If optional argument CONT is non-nil, searches to the end of
3184the current statement.
3185If optional arg BEG is non-nil, search starts from the beginning of the
3186current statement.
3187Ignores matches that end in a comment or inside a string expression.
3188Returns point if successful, nil otherwise.
3189This function produces unexpected results if REGEXP contains quotes or
3190a comment delimiter. The search is case insensitive.
3191If successful leaves point after the match, otherwise, does not move point."
f32b3b91 3192 (let ((here (point))
f32b3b91 3193 (case-fold-search t)
15e42531
CD
3194 (eos (save-excursion
3195 (if cont (idlwave-end-of-statement) (end-of-line))
3196 (point)))
f32b3b91 3197 found)
15e42531
CD
3198 (idlwave-with-special-syntax
3199 (if beg (idlwave-beginning-of-statement))
3200 (while (and (setq found (re-search-forward regexp eos t))
3201 (idlwave-quoted))))
f32b3b91
CD
3202 (if (not found) (goto-char here))
3203 found))
3204
3205(defun idlwave-fill-paragraph (&optional nohang)
3206 "Fills paragraphs in comments.
3207A paragraph is made up of all contiguous lines having the same comment
3208leader (the leading whitespace before the comment delimiter and the
3209comment delimiter). In addition, paragraphs are separated by blank
3210line comments. The indentation is given by the hanging indent of the
3211first line, otherwise by the minimum indentation of the lines after
3212the first line. The indentation of the first line does not change.
3213Does not effect code lines. Does not fill comments on the same line
3214with code. The hanging indent is given by the end of the first match
3215matching `idlwave-hang-indent-regexp' on the paragraph's first line . If the
3216optional argument NOHANG is non-nil then the hanging indent is
3217ignored."
3218 (interactive "P")
3219 ;; check if this is a line comment
3220 (if (save-excursion
3221 (beginning-of-line)
3222 (skip-chars-forward " \t")
3223 (looking-at comment-start))
3224 (let
3225 ((indent 999)
3226 pre here diff fill-prefix-reg bcl first-indent
3227 hang start end)
3228 ;; Change tabs to spaces in the surrounding paragraph.
3229 ;; The surrounding paragraph will be the largest containing block of
3230 ;; contiguous line comments. Thus, we may be changing tabs in
3231 ;; a much larger area than is needed, but this is the easiest
3232 ;; brute force way to do it.
3233 ;;
3234 ;; This has the undesirable side effect of replacing the tabs
3235 ;; permanently without the user's request or knowledge.
3236 (save-excursion
3237 (backward-paragraph)
3238 (setq start (point)))
3239 (save-excursion
3240 (forward-paragraph)
3241 (setq end (point)))
3242 (untabify start end)
3243 ;;
3244 (setq here (point))
3245 (beginning-of-line)
3246 (setq bcl (point))
3247 (re-search-forward
3248 (concat "^[ \t]*" comment-start "+")
3249 (save-excursion (end-of-line) (point))
3250 t)
3251 ;; Get the comment leader on the line and its length
3252 (setq pre (current-column))
3253 ;; the comment leader is the indentation plus exactly the
3254 ;; number of consecutive ";".
3255 (setq fill-prefix-reg
3256 (concat
3257 (setq fill-prefix
3258 (regexp-quote
3259 (buffer-substring (save-excursion
3260 (beginning-of-line) (point))
3261 (point))))
3262 "[^;]"))
52a244eb 3263
f32b3b91
CD
3264 ;; Mark the beginning and end of the paragraph
3265 (goto-char bcl)
3266 (while (and (looking-at fill-prefix-reg)
3267 (not (looking-at paragraph-separate))
3268 (not (bobp)))
3269 (forward-line -1))
3270 ;; Move to first line of paragraph
3271 (if (/= (point) bcl)
3272 (forward-line 1))
3273 (setq start (point))
3274 (goto-char bcl)
3275 (while (and (looking-at fill-prefix-reg)
3276 (not (looking-at paragraph-separate))
3277 (not (eobp)))
3278 (forward-line 1))
3279 (beginning-of-line)
3280 (if (or (not (looking-at fill-prefix-reg))
3281 (looking-at paragraph-separate))
3282 (forward-line -1))
3283 (end-of-line)
3284 ;; if at end of buffer add a newline (need this because
3285 ;; fill-region needs END to be at the beginning of line after
3286 ;; the paragraph or it will add a line).
3287 (if (eobp)
3288 (progn (insert ?\n) (backward-char 1)))
3289 ;; Set END to the beginning of line after the paragraph
3290 ;; END is calculated as distance from end of buffer
3291 (setq end (- (point-max) (point) 1))
3292 ;;
3293 ;; Calculate the indentation for the paragraph.
3294 ;;
3295 ;; In the following while statements, after one iteration
3296 ;; point will be at the beginning of a line in which case
3297 ;; the while will not be executed for the
3298 ;; the first paragraph line and thus will not affect the
3299 ;; indentation.
3300 ;;
3301 ;; First check to see if indentation is based on hanging indent.
3302 (if (and (not nohang) idlwave-hanging-indent
3303 (setq hang
3304 (save-excursion
3305 (goto-char start)
3306 (idlwave-calc-hanging-indent))))
3307 ;; Adjust lines of paragraph by inserting spaces so that
3308 ;; each line's indent is at least as great as the hanging
3309 ;; indent. This is needed for fill-paragraph to work with
3310 ;; a fill-prefix.
3311 (progn
3312 (setq indent hang)
3313 (beginning-of-line)
3314 (while (> (point) start)
3315 (re-search-forward comment-start-skip
3316 (save-excursion (end-of-line) (point))
3317 t)
3318 (if (> (setq diff (- indent (current-column))) 0)
3319 (progn
3320 (if (>= here (point))
3321 ;; adjust the original location for the
3322 ;; inserted text.
3323 (setq here (+ here diff)))
15e42531 3324 (insert (make-string diff ?\ ))))
f32b3b91
CD
3325 (forward-line -1))
3326 )
52a244eb 3327
f32b3b91
CD
3328 ;; No hang. Instead find minimum indentation of paragraph
3329 ;; after first line.
3330 ;; For the following while statement, since START is at the
aa87aafc 3331 ;; beginning of line and END is at the end of line
f32b3b91
CD
3332 ;; point is greater than START at least once (which would
3333 ;; be the case for a single line paragraph).
3334 (while (> (point) start)
3335 (beginning-of-line)
3336 (setq indent
3337 (min indent
3338 (progn
3339 (re-search-forward
3340 comment-start-skip
3341 (save-excursion (end-of-line) (point))
3342 t)
3343 (current-column))))
3344 (forward-line -1))
3345 )
3346 (setq fill-prefix (concat fill-prefix
3347 (make-string (- indent pre)
15e42531 3348 ?\ )))
f32b3b91
CD
3349 ;; first-line indent
3350 (setq first-indent
3351 (max
3352 (progn
3353 (re-search-forward
3354 comment-start-skip
3355 (save-excursion (end-of-line) (point))
3356 t)
3357 (current-column))
3358 indent))
52a244eb 3359
f32b3b91
CD
3360 ;; try to keep point at its original place
3361 (goto-char here)
3362
3363 ;; In place of the more modern fill-region-as-paragraph, a hack
3364 ;; to keep whitespace untouched on the first line within the
3365 ;; indent length and to preserve any indent on the first line
3366 ;; (first indent).
3367 (save-excursion
3368 (setq diff
3369 (buffer-substring start (+ start first-indent -1)))
15e42531 3370 (subst-char-in-region start (+ start first-indent -1) ?\ ?~ nil)
f32b3b91
CD
3371 (fill-region-as-paragraph
3372 start
3373 (- (point-max) end)
3374 (current-justification)
3375 nil)
3376 (delete-region start (+ start first-indent -1))
3377 (goto-char start)
3378 (insert diff))
3379 ;; When we want the point at the beginning of the comment
3380 ;; body fill-region will put it at the beginning of the line.
3381 (if (bolp) (skip-chars-forward (concat " \t" comment-start)))
3382 (setq fill-prefix nil))))
3383
3384(defun idlwave-calc-hanging-indent ()
3385 "Calculate the position of the hanging indent for the comment
3386paragraph. The hanging indent position is given by the first match
3387with the `idlwave-hang-indent-regexp'. If `idlwave-use-last-hang-indent' is
3388non-nil then use last occurrence matching `idlwave-hang-indent-regexp' on
3389the line.
3390If not found returns nil."
3391 (if idlwave-use-last-hang-indent
3392 (save-excursion
3393 (end-of-line)
3394 (if (re-search-backward
3395 idlwave-hang-indent-regexp
3396 (save-excursion (beginning-of-line) (point))
3397 t)
3398 (+ (current-column) (length idlwave-hang-indent-regexp))))
3399 (save-excursion
3400 (beginning-of-line)
3401 (if (re-search-forward
3402 idlwave-hang-indent-regexp
3403 (save-excursion (end-of-line) (point))
3404 t)
3405 (current-column)))))
3406
3407(defun idlwave-auto-fill ()
52a244eb
S
3408 "Called to break lines in auto fill mode.
3409Only fills non-comment lines if `idlwave-fill-comment-line-only' is
3410non-nil. Places a continuation character at the end of the line if
3411not in a comment. Splits strings with IDL concatenation operator `+'
3412if `idlwave-auto-fill-split-string' is non-nil."
f32b3b91
CD
3413 (if (<= (current-column) fill-column)
3414 nil ; do not to fill
3415 (if (or (not idlwave-fill-comment-line-only)
3416 (save-excursion
3417 ;; Check for comment line
3418 (beginning-of-line)
3419 (looking-at idlwave-comment-line-start-skip)))
3420 (let (beg)
3421 (idlwave-indent-line)
3422 ;; Prevent actions do-auto-fill which calls indent-line-function.
3423 (let (idlwave-do-actions
3424 (paragraph-start ".")
52a244eb
S
3425 (paragraph-separate ".")
3426 (fill-nobreak-predicate
3427 (if (and (idlwave-in-quote)
3428 idlwave-auto-fill-split-string)
3429 (lambda () ;; We'll need 5 spaces for " ' + $"
3430 (<= (- fill-column (current-column)) 5)
3431 ))))
f32b3b91
CD
3432 (do-auto-fill))
3433 (save-excursion
3434 (end-of-line 0)
3435 ;; Indent the split line
3436 (idlwave-indent-line)
3437 )
3438 (if (save-excursion
3439 (beginning-of-line)
3440 (looking-at idlwave-comment-line-start-skip))
3441 ;; A continued line comment
3442 ;; We treat continued line comments as part of a comment
3443 ;; paragraph. So we check for a hanging indent.
3444 (if idlwave-hanging-indent
3445 (let ((here (- (point-max) (point)))
3446 (indent
3447 (save-excursion
3448 (forward-line -1)
3449 (idlwave-calc-hanging-indent))))
3450 (if indent
3451 (progn
3452 ;; Remove whitespace between comment delimiter and
3453 ;; text, insert spaces for appropriate indentation.
3454 (beginning-of-line)
3455 (re-search-forward
3456 comment-start-skip
3457 (save-excursion (end-of-line) (point)) t)
3458 (delete-horizontal-space)
3459 (idlwave-indent-to indent)
3460 (goto-char (- (point-max) here)))
3461 )))
3462 ;; Split code or comment?
3463 (if (save-excursion
3464 (end-of-line 0)
3465 (idlwave-in-comment))
52a244eb 3466 ;; Splitting a non-full-line comment.
f32b3b91
CD
3467 ;; Insert the comment delimiter from split line
3468 (progn
3469 (save-excursion
3470 (beginning-of-line)
3471 (skip-chars-forward " \t")
3472 ;; Insert blank to keep off beginning of line
3473 (insert " "
3474 (save-excursion
3475 (forward-line -1)
3476 (buffer-substring (idlwave-goto-comment)
3477 (progn
3478 (skip-chars-forward "; ")
3479 (point))))))
3480 (idlwave-indent-line))
3481 ;; Split code line - add continuation character
3482 (save-excursion
3483 (end-of-line 0)
3484 ;; Check to see if we split a string
3485 (if (and (setq beg (idlwave-in-quote))
3486 idlwave-auto-fill-split-string)
3487 ;; Split the string and concatenate.
3488 ;; The first extra space is for the space
3489 ;; the line was split. That space was removed.
3490 (insert " " (char-after beg) " +"))
3491 (insert " $"))
3492 (if beg
3493 (if idlwave-auto-fill-split-string
3494 ;; Make the second part of continued string
3495 (save-excursion
3496 (beginning-of-line)
3497 (skip-chars-forward " \t")
3498 (insert (char-after beg)))
3499 ;; Warning
3500 (beep)
3501 (message "Warning: continuation inside a string.")))
3502 ;; Although do-auto-fill (via indent-new-comment-line) calls
3503 ;; idlwave-indent-line for the new line, re-indent again
3504 ;; because of the addition of the continuation character.
3505 (idlwave-indent-line))
3506 )))))
3507
3508(defun idlwave-auto-fill-mode (arg)
3509 "Toggle auto-fill mode for IDL mode.
3510With arg, turn auto-fill mode on if arg is positive.
3511In auto-fill mode, inserting a space at a column beyond `fill-column'
3512automatically breaks the line at a previous space."
3513 (interactive "P")
3514 (prog1 (set idlwave-fill-function
3515 (if (if (null arg)
3516 (not (symbol-value idlwave-fill-function))
3517 (> (prefix-numeric-value arg) 0))
3518 'idlwave-auto-fill
3519 nil))
3520 ;; update mode-line
3521 (set-buffer-modified-p (buffer-modified-p))))
3522
52a244eb
S
3523;(defun idlwave-fill-routine-call ()
3524; "Fill a routine definition or statement, indenting appropriately."
3525; (let ((where (idlwave-where)))))
3526
3527
f32b3b91
CD
3528(defun idlwave-doc-header (&optional nomark )
3529 "Insert a documentation header at the beginning of the unit.
3530Inserts the value of the variable idlwave-file-header. Sets mark before
3531moving to do insertion unless the optional prefix argument NOMARK
3532is non-nil."
3533 (interactive "P")
3534 (or nomark (push-mark))
3535 ;; make sure we catch the current line if it begins the unit
5e72c6b2
S
3536 (if idlwave-header-to-beginning-of-file
3537 (goto-char (point-min))
3538 (end-of-line)
3539 (idlwave-beginning-of-subprogram)
3540 (beginning-of-line)
3541 ;; skip function or procedure line
3542 (if (idlwave-look-at "\\<\\(pro\\|function\\)\\>")
3543 (progn
3544 (idlwave-end-of-statement)
3545 (if (> (forward-line 1) 0) (insert "\n")))))
3546 (let ((pos (point)))
3547 (if idlwave-file-header
3548 (cond ((car idlwave-file-header)
3549 (insert-file (car idlwave-file-header)))
3550 ((stringp (car (cdr idlwave-file-header)))
3551 (insert (car (cdr idlwave-file-header))))))
3552 (goto-char pos)))
f32b3b91
CD
3553
3554(defun idlwave-default-insert-timestamp ()
3555 "Default timestamp insertion function"
3556 (insert (current-time-string))
3557 (insert ", " (user-full-name))
5e72c6b2 3558 (if (boundp 'user-mail-address)
52a244eb 3559 (insert " <" user-mail-address ">")
5e72c6b2 3560 (insert " <" (user-login-name) "@" (system-name) ">"))
f32b3b91
CD
3561 ;; Remove extra spaces from line
3562 (idlwave-fill-paragraph)
3563 ;; Insert a blank line comment to separate from the date entry -
3564 ;; will keep the entry from flowing onto date line if re-filled.
5e72c6b2 3565 (insert "\n;\n;\t\t"))
f32b3b91
CD
3566
3567(defun idlwave-doc-modification ()
3568 "Insert a brief modification log at the beginning of the current program.
3569Looks for an occurrence of the value of user variable
3570`idlwave-doc-modifications-keyword' if non-nil. Inserts time and user name
3571and places the point for the user to add a log. Before moving, saves
3572location on mark ring so that the user can return to previous point."
3573 (interactive)
3574 (push-mark)
05a1abfc
CD
3575 (let* (beg end)
3576 (if (and (or (re-search-backward idlwave-doclib-start nil t)
3577 (progn
3578 (goto-char (point-min))
3579 (re-search-forward idlwave-doclib-start nil t)))
3580 (setq beg (match-beginning 0))
3581 (re-search-forward idlwave-doclib-end nil t)
3582 (setq end (match-end 0)))
3583 (progn
3584 (goto-char beg)
52a244eb 3585 (if (re-search-forward
05a1abfc
CD
3586 (concat idlwave-doc-modifications-keyword ":")
3587 end t)
3588 (end-of-line)
3589 (goto-char end)
3590 (end-of-line -1)
3591 (insert "\n" comment-start "\n")
3592 (insert comment-start " " idlwave-doc-modifications-keyword ":"))
3593 (insert "\n;\n;\t")
3594 (run-hooks 'idlwave-timestamp-hook))
3595 (error "No valid DOCLIB header"))))
f32b3b91
CD
3596
3597;;; CJC 3/16/93
3598;;; Interface to expand-region-abbrevs which did not work when the
3599;;; abbrev hook associated with an abbrev moves point backwards
3600;;; after abbrev expansion, e.g., as with the abbrev '.n'.
3601;;; The original would enter an infinite loop in attempting to expand
3602;;; .n (it would continually expand and unexpand the abbrev without expanding
3603;;; because the point would keep going back to the beginning of the
3604;;; abbrev instead of to the end of the abbrev). We now keep the
3605;;; abbrev hook from moving backwards.
3606;;;
3607(defun idlwave-expand-region-abbrevs (start end)
3608 "Expand each abbrev occurrence in the region.
3609Calling from a program, arguments are START END."
3610 (interactive "r")
3611 (save-excursion
3612 (goto-char (min start end))
3613 (let ((idlwave-show-block nil) ;Do not blink
3614 (idlwave-abbrev-move nil)) ;Do not move
3615 (expand-region-abbrevs start end 'noquery))))
3616
3617(defun idlwave-quoted ()
3618 "Returns t if point is in a comment or quoted string.
3619nil otherwise."
3620 (or (idlwave-in-comment) (idlwave-in-quote)))
3621
3622(defun idlwave-in-quote ()
3623 "Returns location of the opening quote
3624if point is in a IDL string constant, nil otherwise.
3625Ignores comment delimiters on the current line.
3626Properly handles nested quotation marks and octal
3627constants - a double quote followed by an octal digit."
3628;;; Treat an octal inside an apostrophe to be a normal string. Treat a
3629;;; double quote followed by an octal digit to be an octal constant
3630;;; rather than a string. Therefore, there is no terminating double
3631;;; quote.
3632 (save-excursion
3633 ;; Because single and double quotes can quote each other we must
3634 ;; search for the string start from the beginning of line.
3635 (let* ((start (point))
3636 (eol (progn (end-of-line) (point)))
3637 (bq (progn (beginning-of-line) (point)))
3638 (endq (point))
3639 (data (match-data))
3640 delim
3641 found)
3642 (while (< endq start)
3643 ;; Find string start
3644 ;; Don't find an octal constant beginning with a double quote
52a244eb 3645 (if (re-search-forward "[\"']" eol 'lim)
f32b3b91
CD
3646 ;; Find the string end.
3647 ;; In IDL, two consecutive delimiters after the start of a
3648 ;; string act as an
3649 ;; escape for the delimiter in the string.
3650 ;; Two consecutive delimiters alone (i.e., not after the
aa87aafc 3651 ;; start of a string) is the null string.
f32b3b91
CD
3652 (progn
3653 ;; Move to position after quote
3654 (goto-char (1+ (match-beginning 0)))
3655 (setq bq (1- (point)))
3656 ;; Get the string delimiter
3657 (setq delim (char-to-string (preceding-char)))
3658 ;; Check for null string
3659 (if (looking-at delim)
3660 (progn (setq endq (point)) (forward-char 1))
3661 ;; Look for next unpaired delimiter
3662 (setq found (search-forward delim eol 'lim))
3663 (while (looking-at delim)
3664 (forward-char 1)
3665 (setq found (search-forward delim eol 'lim)))
3666 (if found
3667 (setq endq (- (point) 1))
3668 (setq endq (point)))
3669 ))
3670 (progn (setq bq (point)) (setq endq (point)))))
3671 (store-match-data data)
3672 ;; return string beginning position or nil
3673 (if (> start bq) bq))))
3674
76959b77
S
3675(defun idlwave-is-pointer-dereference (&optional limit)
3676 "Determines if the character after point is a pointer dereference *."
3677 (let ((pos (point)))
3678 (and
3679 (eq (char-after) ?\*)
3680 (not (idlwave-in-quote))
3681 (save-excursion
3682 (forward-char)
52a244eb 3683 (re-search-backward (concat "\\(" idlwave-idl-keywords
76959b77
S
3684 "\\|[[(*+-/=,^><]\\)\\s-*\\*") limit t)))))
3685
3686
f32b3b91
CD
3687;; Statement templates
3688
3689;; Replace these with a general template function, something like
3690;; expand.el (I think there was also something with a name similar to
3691;; dmacro.el)
3692
3693(defun idlwave-template (s1 s2 &optional prompt noindent)
3694 "Build a template with optional prompt expression.
3695
3696Opens a line if point is not followed by a newline modulo intervening
3697whitespace. S1 and S2 are strings. S1 is inserted at point followed
595ab50b
CD
3698by S2. Point is inserted between S1 and S2. The case of S1 and S2 is
3699adjusted according to `idlwave-abbrev-change-case'. If optional argument
f32b3b91
CD
3700PROMPT is a string then it is displayed as a message in the
3701minibuffer. The PROMPT serves as a reminder to the user of an
3702expression to enter.
3703
3704The lines containing S1 and S2 are reindented using `indent-region'
3705unless the optional second argument NOINDENT is non-nil."
15e42531 3706 (if (eq major-mode 'idlwave-shell-mode)
05a1abfc 3707 ;; This is a gross hack to avoit template abbrev expansion
15e42531
CD
3708 ;; in the shell. FIXME: This is a dirty hack.
3709 (if (and (eq this-command 'self-insert-command)
3710 (equal last-abbrev-location (point)))
3711 (insert last-abbrev-text)
3712 (error "No templates in idlwave-shell"))
3713 (cond ((eq idlwave-abbrev-change-case 'down)
3714 (setq s1 (downcase s1) s2 (downcase s2)))
3715 (idlwave-abbrev-change-case
3716 (setq s1 (upcase s1) s2 (upcase s2))))
3717 (let ((beg (save-excursion (beginning-of-line) (point)))
3718 end)
3719 (if (not (looking-at "\\s-*\n"))
3720 (open-line 1))
3721 (insert s1)
3722 (save-excursion
3723 (insert s2)
3724 (setq end (point)))
3725 (if (not noindent)
3726 (indent-region beg end nil))
3727 (if (stringp prompt)
3728 (message prompt)))))
52a244eb 3729
595ab50b
CD
3730(defun idlwave-rw-case (string)
3731 "Make STRING have the case required by `idlwave-reserved-word-upcase'."
3732 (if idlwave-reserved-word-upcase
3733 (upcase string)
3734 string))
3735
f32b3b91
CD
3736(defun idlwave-elif ()
3737 "Build skeleton IDL if-else block."
3738 (interactive)
595ab50b
CD
3739 (idlwave-template
3740 (idlwave-rw-case "if")
3741 (idlwave-rw-case " then begin\n\nendif else begin\n\nendelse")
3742 "Condition expression"))
f32b3b91
CD
3743
3744(defun idlwave-case ()
3745 "Build skeleton IDL case statement."
3746 (interactive)
52a244eb 3747 (idlwave-template
595ab50b
CD
3748 (idlwave-rw-case "case")
3749 (idlwave-rw-case " of\n\nendcase")
3750 "Selector expression"))
f32b3b91 3751
05a1abfc
CD
3752(defun idlwave-switch ()
3753 "Build skeleton IDL switch statement."
3754 (interactive)
52a244eb 3755 (idlwave-template
05a1abfc
CD
3756 (idlwave-rw-case "switch")
3757 (idlwave-rw-case " of\n\nendswitch")
3758 "Selector expression"))
3759
f32b3b91
CD
3760(defun idlwave-for ()
3761 "Build skeleton for loop statment."
3762 (interactive)
52a244eb 3763 (idlwave-template
595ab50b
CD
3764 (idlwave-rw-case "for")
3765 (idlwave-rw-case " do begin\n\nendfor")
3766 "Loop expression"))
f32b3b91
CD
3767
3768(defun idlwave-if ()
3769 "Build skeleton for loop statment."
3770 (interactive)
595ab50b
CD
3771 (idlwave-template
3772 (idlwave-rw-case "if")
3773 (idlwave-rw-case " then begin\n\nendif")
3774 "Scalar logical expression"))
f32b3b91
CD
3775
3776(defun idlwave-procedure ()
3777 (interactive)
52a244eb 3778 (idlwave-template
595ab50b
CD
3779 (idlwave-rw-case "pro")
3780 (idlwave-rw-case "\n\nreturn\nend")
3781 "Procedure name"))
f32b3b91
CD
3782
3783(defun idlwave-function ()
3784 (interactive)
52a244eb 3785 (idlwave-template
595ab50b
CD
3786 (idlwave-rw-case "function")
3787 (idlwave-rw-case "\n\nreturn\nend")
3788 "Function name"))
f32b3b91
CD
3789
3790(defun idlwave-repeat ()
3791 (interactive)
595ab50b
CD
3792 (idlwave-template
3793 (idlwave-rw-case "repeat begin\n\nendrep until")
3794 (idlwave-rw-case "")
3795 "Exit condition"))
f32b3b91
CD
3796
3797(defun idlwave-while ()
3798 (interactive)
52a244eb 3799 (idlwave-template
595ab50b
CD
3800 (idlwave-rw-case "while")
3801 (idlwave-rw-case " do begin\n\nendwhile")
3802 "Entry condition"))
f32b3b91
CD
3803
3804(defun idlwave-split-string (string &optional pattern)
3805 "Return a list of substrings of STRING which are separated by PATTERN.
3806If PATTERN is omitted, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
3807 (or pattern
3808 (setq pattern "[ \f\t\n\r\v]+"))
3809 (let (parts (start 0))
3810 (while (string-match pattern string start)
3811 (setq parts (cons (substring string start (match-beginning 0)) parts)
3812 start (match-end 0)))
3813 (nreverse (cons (substring string start) parts))))
3814
3815(defun idlwave-replace-string (string replace_string replace_with)
3816 (let* ((start 0)
3817 (last (length string))
3818 (ret_string "")
3819 end)
3820 (while (setq end (string-match replace_string string start))
3821 (setq ret_string
3822 (concat ret_string (substring string start end) replace_with))
3823 (setq start (match-end 0)))
3824 (setq ret_string (concat ret_string (substring string start last)))))
3825
3826(defun idlwave-get-buffer-visiting (file)
3827 ;; Return the buffer currently visiting FILE
3828 (cond
3829 ((boundp 'find-file-compare-truenames) ; XEmacs
3830 (let ((find-file-compare-truenames t))
3831 (get-file-buffer file)))
3832 ((fboundp 'find-buffer-visiting) ; Emacs
3833 (find-buffer-visiting file))
3834 (t (error "This should not happen (idlwave-get-buffer-visiting)"))))
3835
15e42531
CD
3836(defvar idlwave-outlawed-buffers nil
3837 "List of buffer pulled up by idlwave for special reasons.
3838Buffers in this list may be killed by `idlwave-kill-autoloaded-buffers'.")
3839
3840(defun idlwave-find-file-noselect (file &optional why)
f32b3b91
CD
3841 ;; Return a buffer visiting file.
3842 (or (idlwave-get-buffer-visiting file)
15e42531
CD
3843 (let ((buf (find-file-noselect file)))
3844 (if why (add-to-list 'idlwave-outlawed-buffers (cons buf why)))
3845 buf)))
3846
3847(defun idlwave-kill-autoloaded-buffers ()
52a244eb 3848 "Kill buffers created automatically by IDLWAVE.
15e42531
CD
3849Function prompts for a letter to identify the buffers to kill.
3850Possible letters are:
3851
3852f Buffers created by the command \\[idlwave-find-module] or mouse
3853 clicks in the routine info window.
3854s Buffers created by the IDLWAVE Shell to display where execution
3855 stopped or an error was found.
3856a Both of the above.
3857
3858Buffer containing unsaved changes require confirmation before they are killed."
3859 (interactive)
3860 (if (null idlwave-outlawed-buffers)
3861 (error "No IDLWAVE-created buffers available")
3862 (princ (format "Kill IDLWAVE-created buffers: [f]ind source(%d), [s]hell display(%d), [a]ll ? "
3863 (idlwave-count-outlawed-buffers 'find)
3864 (idlwave-count-outlawed-buffers 'shell)))
3865 (let ((c (read-char)))
3866 (cond
3867 ((member c '(?f ?\C-f))
3868 (idlwave-do-kill-autoloaded-buffers 'find))
3869 ((member c '(?s ?\C-s))
3870 (idlwave-do-kill-autoloaded-buffers 'shell))
3871 ((member c '(?a ?\C-a))
3872 (idlwave-do-kill-autoloaded-buffers t))
3873 (t (error "Abort"))))))
3874
3875(defun idlwave-count-outlawed-buffers (tag)
3876 "How many outlawed buffers have tag TAG?"
3877 (length (delq nil
52a244eb
S
3878 (mapcar
3879 (lambda (x) (eq (cdr x) tag))
15e42531
CD
3880 idlwave-outlawed-buffers))))
3881
3882(defun idlwave-do-kill-autoloaded-buffers (&rest reasons)
3883 "Kill all buffers pulled up by IDLWAVE matching REASONS."
3884 (let* ((list (copy-sequence idlwave-outlawed-buffers))
3885 (cnt 0)
3886 entry)
3887 (while (setq entry (pop list))
3888 (if (buffer-live-p (car entry))
3889 (and (or (memq t reasons)
3890 (memq (cdr entry) reasons))
3891 (kill-buffer (car entry))
3892 (incf cnt)
52a244eb 3893 (setq idlwave-outlawed-buffers
15e42531 3894 (delq entry idlwave-outlawed-buffers)))
52a244eb 3895 (setq idlwave-outlawed-buffers
15e42531
CD
3896 (delq entry idlwave-outlawed-buffers))))
3897 (message "%d buffer%s killed" cnt (if (= cnt 1) "" "s"))))
3898
3899(defun idlwave-revoke-license-to-kill ()
3900 "Remove BUFFER from the buffers which may be killed.
3901Killing would be done by `idlwave-do-kill-autoloaded-buffers'.
3902Intended for `after-save-hook'."
3903 (let* ((buf (current-buffer))
3904 (entry (assq buf idlwave-outlawed-buffers)))
3905 ;; Revoke license
3906 (if entry
52a244eb 3907 (setq idlwave-outlawed-buffers
15e42531
CD
3908 (delq entry idlwave-outlawed-buffers)))
3909 ;; Remove this function from the hook.
3910 (remove-hook 'after-save-hook 'idlwave-revoke-license-to-kill 'local)))
3911
3912(defvar idlwave-path-alist)
3913(defun idlwave-locate-lib-file (file)
f32b3b91 3914 ;; Find FILE on the scanned lib path and return a buffer visiting it
15e42531 3915 (let* ((dirs idlwave-path-alist)
f32b3b91
CD
3916 dir efile)
3917 (catch 'exit
15e42531 3918 (while (setq dir (car (pop dirs)))
f32b3b91
CD
3919 (if (file-regular-p
3920 (setq efile (expand-file-name file dir)))
15e42531 3921 (throw 'exit efile))))))
52a244eb 3922
15e42531
CD
3923(defun idlwave-expand-lib-file-name (file)
3924 ;; Find FILE on the scanned lib path and return a buffer visiting it
52a244eb
S
3925 ;; This is for, e.g., finding source with no user catalog
3926 (cond
15e42531 3927 ((null file) nil)
15e42531
CD
3928 ((file-name-absolute-p file) file)
3929 (t (idlwave-locate-lib-file file))))
f32b3b91
CD
3930
3931(defun idlwave-make-tags ()
3932 "Creates the IDL tags file IDLTAGS in the current directory from
3933the list of directories specified in the minibuffer. Directories may be
3934for example: . /usr/local/rsi/idl/lib. All the subdirectories of the
3935specified top directories are searched if the directory name is prefixed
3936by @. Specify @ directories with care, it may take a long, long time if
3937you specify /."
3938 (interactive)
3939 (let (directory directories cmd append status numdirs dir getsubdirs
3940 buffer save_buffer files numfiles item errbuf)
52a244eb 3941
f32b3b91
CD
3942 ;;
3943 ;; Read list of directories
3944 (setq directory (read-string "Tag Directories: " "."))
3945 (setq directories (idlwave-split-string directory "[ \t]+"))
3946 ;;
3947 ;; Set etags command, vars
3948 (setq cmd "etags --output=IDLTAGS --language=none --regex='/[
3949\\t]*[pP][Rr][Oo][ \\t]+\\([^ \\t,]+\\)/' --regex='/[
3950\\t]*[Ff][Uu][Nn][Cc][Tt][Ii][Oo][Nn][ \\t]+\\([^ \\t,]+\\)/' ")
3951 (setq append " ")
3952 (setq status 0)
3953 ;;
3954 ;; For each directory
3955 (setq numdirs 0)
3956 (setq dir (nth numdirs directories))
3957 (while (and dir)
3958 ;;
3959 ;; Find the subdirectories
3960 (if (string-match "^[@]\\(.+\\)$" dir)
3961 (setq getsubdirs t) (setq getsubdirs nil))
3962 (if (and getsubdirs) (setq dir (substring dir 1 (length dir))))
3963 (setq dir (expand-file-name dir))
3964 (if (file-directory-p dir)
3965 (progn
3966 (if (and getsubdirs)
3967 (progn
3968 (setq buffer (get-buffer-create "*idltags*"))
3969 (call-process "sh" nil buffer nil "-c"
3970 (concat "find " dir " -type d -print"))
3971 (setq save_buffer (current-buffer))
3972 (set-buffer buffer)
3973 (setq files (idlwave-split-string
3974 (idlwave-replace-string
3975 (buffer-substring 1 (point-max))
3976 "\n" "/*.pro ")
3977 "[ \t]+"))
3978 (set-buffer save_buffer)
3979 (kill-buffer buffer))
3980 (setq files (list (concat dir "/*.pro"))))
3981 ;;
3982 ;; For each subdirectory
3983 (setq numfiles 0)
3984 (setq item (nth numfiles files))
3985 (while (and item)
3986 ;;
3987 ;; Call etags
3988 (if (not (string-match "^[ \\t]*$" item))
3989 (progn
3990 (message (concat "Tagging " item "..."))
3991 (setq errbuf (get-buffer-create "*idltags-error*"))
52a244eb
S
3992 (setq status (+ status
3993 (if (eq 0 (call-process
3994 "sh" nil errbuf nil "-c"
3995 (concat cmd append item)))
3996 0
3997 1)))
f32b3b91
CD
3998 ;;
3999 ;; Append additional tags
4000 (setq append " --append ")
4001 (setq numfiles (1+ numfiles))
4002 (setq item (nth numfiles files)))
4003 (progn
4004 (setq numfiles (1+ numfiles))
4005 (setq item (nth numfiles files))
4006 )))
52a244eb 4007
f32b3b91
CD
4008 (setq numdirs (1+ numdirs))
4009 (setq dir (nth numdirs directories)))
4010 (progn
4011 (setq numdirs (1+ numdirs))
4012 (setq dir (nth numdirs directories)))))
52a244eb 4013
f32b3b91
CD
4014 (setq errbuf (get-buffer-create "*idltags-error*"))
4015 (if (= status 0)
4016 (kill-buffer errbuf))
4017 (message "")
4018 ))
4019
4020(defun idlwave-toggle-comment-region (beg end &optional n)
4021 "Comment the lines in the region if the first non-blank line is
4022commented, and conversely, uncomment region. If optional prefix arg
4023N is non-nil, then for N positive, add N comment delimiters or for N
4024negative, remove N comment delimiters.
4025Uses `comment-region' which does not place comment delimiters on
4026blank lines."
4027 (interactive "r\nP")
4028 (if n
4029 (comment-region beg end (prefix-numeric-value n))
4030 (save-excursion
4031 (goto-char beg)
4032 (beginning-of-line)
4033 ;; skip blank lines
4034 (skip-chars-forward " \t\n")
4035 (if (looking-at (concat "[ \t]*\\(" comment-start "+\\)"))
52a244eb
S
4036 (if (fboundp 'uncomment-region)
4037 (uncomment-region beg end)
4038 (comment-region beg end
4039 (- (length (buffer-substring
4040 (match-beginning 1)
4041 (match-end 1))))))
4042 (comment-region beg end)))))
f32b3b91
CD
4043
4044
4045;; ----------------------------------------------------------------------------
4046;; ----------------------------------------------------------------------------
4047;; ----------------------------------------------------------------------------
4048;; ----------------------------------------------------------------------------
4049;;
4050;; Completion and Routine Info
4051;;
4052
4053;; String "intern" functions
4054
4055;; For the completion and routine info function, we want to normalize
4056;; the case of procedure names etc. We do this by "interning" these
4057;; string is a hand-crafted way. Hashes are used to map the downcase
52a244eb
S
4058;; version of the strings to the cased versions. Most *-sint-*
4059;; variables consist of *two* hashes, a buffer+shell, followed by a
4060;; system hash. The former is re-scanned, and the latter takes case
4061;; precedence.
4062;;
4063;; Since these cased versions are really lisp objects, we can use `eq'
4064;; to search, which is a large performance boost. All new strings
4065;; need to be "sinterned". We do this as early as possible after
4066;; getting these strings from completion or buffer substrings. So
4067;; most of the code can simply assume to deal with "sinterned"
4068;; strings. The only exception is that the functions which scan whole
4069;; buffers for routine information do not intern the grabbed strings.
4070;; This is only done afterwards. Therefore in these functions it is
4071;; *not* safe to assume the strings can be compared with `eq' and be
4072;; fed into the routine assq functions.
f32b3b91
CD
4073
4074;; Here we define the hashing functions.
4075
4076;; The variables which hold the hashes.
4077(defvar idlwave-sint-routines '(nil))
4078(defvar idlwave-sint-keywords '(nil))
4079(defvar idlwave-sint-methods '(nil))
4080(defvar idlwave-sint-classes '(nil))
52a244eb
S
4081(defvar idlwave-sint-dirs '(nil))
4082(defvar idlwave-sint-libnames '(nil))
f32b3b91
CD
4083
4084(defun idlwave-reset-sintern (&optional what)
4085 "Reset all sintern hashes."
4086 ;; Make sure the hash functions are accessible.
4087 (if (or (not (fboundp 'gethash))
4088 (not (fboundp 'puthash)))
52a244eb 4089 (progn
f32b3b91
CD
4090 (require 'cl)
4091 (or (fboundp 'puthash)
4092 (defalias 'puthash 'cl-puthash))))
4093 (let ((entries '((idlwave-sint-routines 1000 10)
4094 (idlwave-sint-keywords 1000 10)
4095 (idlwave-sint-methods 100 10)
4096 (idlwave-sint-classes 10 10))))
4097
4098 ;; Make sure these are lists
4099 (loop for entry in entries
4100 for var = (car entry)
4101 do (if (not (consp (symbol-value var))) (set var (list nil))))
4102
4103 (when (or (eq what t) (eq what 'syslib)
4104 (null (cdr idlwave-sint-routines)))
4105 ;; Reset the system & library hash
4106 (loop for entry in entries
4107 for var = (car entry) for size = (nth 1 entry)
52a244eb 4108 do (setcdr (symbol-value var)
f32b3b91 4109 (make-hash-table ':size size ':test 'equal)))
52a244eb
S
4110 (setq idlwave-sint-dirs nil
4111 idlwave-sint-libnames nil))
f32b3b91
CD
4112
4113 (when (or (eq what t) (eq what 'bufsh)
4114 (null (car idlwave-sint-routines)))
4115 ;; Reset the buffer & shell hash
4116 (loop for entry in entries
4117 for var = (car entry) for size = (nth 1 entry)
52a244eb 4118 do (setcar (symbol-value var)
f32b3b91
CD
4119 (make-hash-table ':size size ':test 'equal))))))
4120
4121(defun idlwave-sintern-routine-or-method (name &optional class set)
4122 (if class
4123 (idlwave-sintern-method name set)
4124 (idlwave-sintern-routine name set)))
4125
4126(defun idlwave-sintern (stype &rest args)
4127 (apply (intern (concat "idlwave-sintern-" (symbol-name stype))) args))
4128
4129;;(defmacro idlwave-sintern (type var)
4130;; `(cond ((not (stringp name)) name)
4131;; ((gethash (downcase name) (cdr ,var)))
4132;; ((gethash (downcase name) (car ,var)))
4133;; (set (idlwave-sintern-set name ,type ,var set))
4134;; (name)))
4135
4136(defun idlwave-sintern-routine (name &optional set)
4137 (cond ((not (stringp name)) name)
4138 ((gethash (downcase name) (cdr idlwave-sint-routines)))
4139 ((gethash (downcase name) (car idlwave-sint-routines)))
4140 (set (idlwave-sintern-set name 'routine idlwave-sint-routines set))
4141 (name)))
4142(defun idlwave-sintern-keyword (name &optional set)
4143 (cond ((not (stringp name)) name)
4144 ((gethash (downcase name) (cdr idlwave-sint-keywords)))
4145 ((gethash (downcase name) (car idlwave-sint-keywords)))
4146 (set (idlwave-sintern-set name 'keyword idlwave-sint-keywords set))
4147 (name)))
4148(defun idlwave-sintern-method (name &optional set)
4149 (cond ((not (stringp name)) name)
4150 ((gethash (downcase name) (cdr idlwave-sint-methods)))
4151 ((gethash (downcase name) (car idlwave-sint-methods)))
4152 (set (idlwave-sintern-set name 'method idlwave-sint-methods set))
4153 (name)))
4154(defun idlwave-sintern-class (name &optional set)
4155 (cond ((not (stringp name)) name)
4156 ((gethash (downcase name) (cdr idlwave-sint-classes)))
4157 ((gethash (downcase name) (car idlwave-sint-classes)))
4158 (set (idlwave-sintern-set name 'class idlwave-sint-classes set))
4159 (name)))
4160
52a244eb
S
4161(defun idlwave-sintern-dir (dir &optional set)
4162 (car (or (member dir idlwave-sint-dirs)
4163 (setq idlwave-sint-dirs (cons dir idlwave-sint-dirs)))))
4164(defun idlwave-sintern-libname (name &optional set)
4165 (car (or (member name idlwave-sint-libnames)
4166 (setq idlwave-sint-libnames (cons name idlwave-sint-libnames)))))
f32b3b91
CD
4167
4168(defun idlwave-sintern-set (name type tables set)
4169 (let* ((func (or (cdr (assq type idlwave-completion-case))
4170 'identity))
4171 (iname (funcall (if (eq func 'preserve) 'identity func) name))
4172 (table (if (eq set 'sys) (cdr tables) (car tables))))
4173 (puthash (downcase name) iname table)
4174 iname))
4175
52a244eb
S
4176(defun idlwave-sintern-keyword-list (kwd-list &optional set)
4177 "Sintern a set of keywords (file (key . link) (key2 . link2) ...)"
4178 (mapcar (lambda(x)
4179 (setcar x (idlwave-sintern-keyword (car x) set)))
4180 (cdr kwd-list))
4181 kwd-list)
4182
4183(defun idlwave-sintern-rinfo-list (list &optional set default-dir)
4184 "Sintern all strings in the rinfo LIST. With optional parameter
4185SET: also set new patterns. Probably this will always have to be t.
4186If DEFAULT-DIR is passed, it is used as the base of the directory"
4187 (let (entry name type class kwds res source call new)
f32b3b91
CD
4188 (while list
4189 (setq entry (car list)
4190 list (cdr list)
4191 name (car entry)
4192 type (nth 1 entry)
4193 class (nth 2 entry)
4194 source (nth 3 entry)
4195 call (nth 4 entry)
52a244eb
S
4196 kwds (nthcdr 5 entry))
4197
4198 ;; The class and name
f32b3b91
CD
4199 (if class
4200 (progn
4201 (if (symbolp class) (setq class (symbol-name class)))
4202 (setq class (idlwave-sintern-class class set))
4203 (setq name (idlwave-sintern-method name set)))
4204 (setq name (idlwave-sintern-routine name set)))
52a244eb
S
4205
4206 ;; The source
4207 (let ((source-type (car source))
4208 (source-file (nth 1 source))
4209 (source-dir (if default-dir
4210 (file-name-as-directory default-dir)
4211 (nth 2 source)))
4212 (source-lib (nth 3 source)))
4213 (if (stringp source-dir)
4214 (setq source-dir (idlwave-sintern-dir source-dir set)))
4215 (if (stringp source-lib)
4216 (setq source-lib (idlwave-sintern-libname source-lib set)))
4217 (setq source (list source-type source-file source-dir source-lib)))
4218
4219 ;; The keywords
4220 (setq kwds (mapcar (lambda (x)
4221 (idlwave-sintern-keyword-list x set))
4222 kwds))
4223
4224 ;; Build a canonicalized list
4225 (setq new (nconc (list name type class source call) kwds)
4226 res (cons new res)))
f32b3b91
CD
4227 (nreverse res)))
4228
05a1abfc
CD
4229;; Creating new sintern tables
4230
4231(defun idlwave-new-sintern-type (tag)
4232 "Define a variable and a function to sintern the new type TAG.
4233This defines the function `idlwave-sintern-TAG' and the variable
4234`idlwave-sint-TAGs'."
4235 (let* ((name (symbol-name tag))
4236 (names (concat name "s"))
4237 (var (intern (concat "idlwave-sint-" names)))
4238 (func (intern (concat "idlwave-sintern-" name))))
4239 (set var nil) ; initial value of the association list
4240 (fset func ; set the function
4241 `(lambda (name &optional set)
4242 (cond ((not (stringp name)) name)
4243 ((cdr (assoc (downcase name) ,var)))
4244 (set
4245 (setq ,var (cons (cons (downcase name) name) ,var))
4246 name)
4247 (name))))))
4248
4249(defun idlwave-reset-sintern-type (tag)
4250 "Reset the sintern variable associated with TAG."
4251 (set (intern (concat "idlwave-sint-" (symbol-name tag) "s")) nil))
4252
f32b3b91
CD
4253;;---------------------------------------------------------------------------
4254
4255
4256;; The variables which hold the information
15e42531 4257(defvar idlwave-system-routines nil
f32b3b91
CD
4258 "Holds the routine-info obtained by scanning buffers.")
4259(defvar idlwave-buffer-routines nil
4260 "Holds the routine-info obtained by scanning buffers.")
4261(defvar idlwave-compiled-routines nil
15e42531
CD
4262 "Holds the routine-info obtained by asking the shell.")
4263(defvar idlwave-unresolved-routines nil
4264 "Holds the unresolved routine-info obtained by asking the shell.")
52a244eb
S
4265(defvar idlwave-user-catalog-routines nil
4266 "Holds the procedure routine-info from the user scan.")
4267(defvar idlwave-library-catalog-routines nil
4268 "Holds the procedure routine-info from the library catalog files.")
15e42531 4269(defvar idlwave-path-alist nil
52a244eb
S
4270 "Alist with !PATH directories and zero or more flags if the dir has
4271been scanned in a user catalog ('user) or discovered in a library
4272catalog \('lib).")
15e42531
CD
4273(defvar idlwave-true-path-alist nil
4274 "Like `idlwave-path-alist', but with true filenames.")
f32b3b91 4275(defvar idlwave-routines nil
52a244eb 4276 "Holds the combinded procedure/function/method routine-info.")
f32b3b91
CD
4277(defvar idlwave-class-alist nil
4278 "Holds the class names known to IDLWAVE.")
4279(defvar idlwave-class-history nil
4280 "The history of classes selected with the minibuffer.")
4281(defvar idlwave-force-class-query nil)
4282(defvar idlwave-before-completion-wconf nil
4283 "The window configuration just before the completion buffer was displayed.")
15e42531
CD
4284(defvar idlwave-last-system-routine-info-cons-cell nil
4285 "The last cons cell in the system routine info.")
f32b3b91
CD
4286
4287;;
4288;; The code to get routine info from different sources.
4289
15e42531 4290(defvar idlwave-system-routines)
5e72c6b2
S
4291(defvar idlwave-catalog-process nil
4292 "The background process currently updating the catalog.")
4293
f32b3b91
CD
4294(defun idlwave-routines ()
4295 "Provide a list of IDL routines.
4296This routine loads the builtin routines on the first call. Later it
4297only returns the value of the variable."
5e72c6b2
S
4298 (if (and idlwave-catalog-process
4299 (processp idlwave-catalog-process))
4300 (progn
4301 (cond
4302 ((equal (process-status idlwave-catalog-process) 'exit)
4303 (message "updating........")
4304 (setq idlwave-catalog-process nil)
4305 (idlwave-update-routine-info '(4)))
4306 ((equal (process-status idlwave-catalog-process) 'run)
4307 ;; Keep it running...
4308 )
4309 (t
4310 ;; Something is wrong, get rid of the process
4311 (message "Problem with catalog process") (beep)
4312 (condition-case nil
4313 (kill-process idlwave-catalog-process)
4314 (error nil))
4315 (setq idlwave-catalog-process nil)))))
f32b3b91
CD
4316 (or idlwave-routines
4317 (progn
4318 (idlwave-update-routine-info)
4319 ;; return the current value
4320 idlwave-routines)))
4321
05a1abfc
CD
4322(defvar idlwave-update-rinfo-hook nil
4323 "List of functions which should run after a global rinfo update.
4324Does not run after automatic updates of buffer or the shell.")
4325
5e72c6b2
S
4326(defun idlwave-rescan-catalog-directories ()
4327 "Rescan the previously selected directories. For batch processing."
4328 (idlwave-update-routine-info '(16)))
4329
4330(defun idlwave-rescan-asynchronously ()
4331 "Dispatch another emacs instance to update the idlwave catalog.
4332After the process finishes normally, the first access to routine info
4333will re-read the catalog."
4334 (interactive)
4335 (if (processp idlwave-catalog-process)
4336 (if (eq (process-status idlwave-catalog-process) 'run)
4337 (if (yes-or-no-p "A catalog-updating process is running. Kill it? ")
4338 (progn
4339 (condition-case nil
4340 (kill-process idlwave-catalog-process)
4341 (error nil))
4342 (error "Process killed, no new process started"))
4343 (error "Quit"))
4344 (condition-case nil
4345 (kill-process idlwave-catalog-process)
4346 (error nil))))
52a244eb
S
4347 (if (or (not idlwave-user-catalog-file)
4348 (not (stringp idlwave-user-catalog-file))
4349 (not (file-regular-p idlwave-user-catalog-file)))
5e72c6b2
S
4350 (error "No catalog has been produced yet"))
4351 (let* ((emacs (expand-file-name (invocation-name) (invocation-directory)))
4352 (args (list "-batch"
4353 "-l" (expand-file-name "~/.emacs")
4354 "-l" "idlwave"
4355 "-f" "idlwave-rescan-catalog-directories"))
52a244eb 4356 (process (apply 'start-process "idlcat"
5e72c6b2
S
4357 nil emacs args)))
4358 (setq idlwave-catalog-process process)
52a244eb 4359 (set-process-sentinel
5e72c6b2
S
4360 process
4361 (lambda (pro why)
4362 (when (string-match "finished" why)
4363 (setq idlwave-routines nil
4364 idlwave-system-routines nil
4365 idlwave-catalog-process nil)
4366 (or (idlwave-start-load-rinfo-timer)
4367 (idlwave-update-routine-info '(4))))))
4368 (message "Background job started to update catalog file")))
4369
4370
52a244eb
S
4371;; Format for all routine info user catalog, library catalogs, etc.:
4372;;
4373;; ("ROUTINE" type class
4374;; (system) | (lib pro_file dir "LIBNAME") | (user pro_file dir "USERLIB") |
4375;; (buffer pro_file dir) | (compiled pro_file dir)
4376;; "calling_string" ("HELPFILE" (("KWD1" . link1) ...)))
4377;;
4378;; DIR will be supplied dynamically while loading library catalogs,
4379;; and is sinterned to save space, as is LIBNAME. PRO_FILE can be a
4380;; complete filepath, in which case DIR is unnecessary. HELPFILE can
4381;; be nil, as can LINK1, etc., if no HTML help is available.
4382
4383
5e72c6b2 4384(defvar idlwave-load-rinfo-idle-timer)
52a244eb 4385(defun idlwave-update-routine-info (&optional arg no-concatenate)
f32b3b91
CD
4386 "Update the internal routine-info lists.
4387These lists are used by `idlwave-routine-info' (\\[idlwave-routine-info])
4388and by `idlwave-complete' (\\[idlwave-complete]) to provide information
4389about individual routines.
4390
4391The information can come from 4 sources:
43921. IDL programs in the current editing session
43932. Compiled modules in an IDL shell running as Emacs subprocess
43943. A list which covers the IDL system routines.
43954. A list which covers the prescanned library files.
4396
4397Scans all IDLWAVE-mode buffers of the current editing session (see
4398`idlwave-scan-all-buffers-for-routine-info').
4399When an IDL shell is running, this command also queries the IDL program
4400for currently compiled routines.
4401
4402With prefix ARG, also reload the system and library lists.
52a244eb
S
4403With two prefix ARG's, also rescans the chosen user catalog tree.
4404With three prefix args, dispatch asynchronous process to do the update.
4405
4406If NO-CONCATENATE is non-nil, don't pre-concatenate the routine info
4407lists, but instead wait for the shell query to complete and
4408asynchronously finish updating routine info. This is set
4409automatically when called interactively. When you need routine
4410information updated immediately, leave NO-CONCATENATE nil."
751adbde 4411 (interactive "P\np")
5e72c6b2
S
4412 ;; Stop any idle processing
4413 (if (or (and (fboundp 'itimerp)
4414 (itimerp idlwave-load-rinfo-idle-timer))
4415 (and (fboundp 'timerp)
4416 (timerp idlwave-load-rinfo-idle-timer)))
4417 (cancel-timer idlwave-load-rinfo-idle-timer))
4418 (cond
4419 ((equal arg '(64))
4420 ;; Start a background process which updates the catalog.
4421 (idlwave-rescan-asynchronously))
4422 ((equal arg '(16))
52a244eb
S
4423 ;; Update the user catalog now, and wait for them.
4424 (idlwave-create-user-catalog-file t))
5e72c6b2
S
4425 (t
4426 (let* ((load (or arg
4427 idlwave-buffer-case-takes-precedence
4428 (null idlwave-routines)))
4429 ;; The override-idle means, even if the idle timer has done some
4430 ;; preparing work, load and renormalize everything anyway.
4431 (override-idle (or arg idlwave-buffer-case-takes-precedence)))
52a244eb 4432
f32b3b91 4433 (setq idlwave-buffer-routines nil
15e42531
CD
4434 idlwave-compiled-routines nil
4435 idlwave-unresolved-routines nil)
f32b3b91 4436 ;; Reset the appropriate hashes
5e72c6b2
S
4437 (if (get 'idlwave-reset-sintern 'done-by-idle)
4438 ;; reset was already done in idle time, so skip this step now once
4439 (put 'idlwave-reset-sintern 'done-by-idle nil)
4440 (idlwave-reset-sintern (cond (load t)
4441 ((null idlwave-system-routines) t)
4442 (t 'bufsh))))
52a244eb 4443
f32b3b91
CD
4444 (if idlwave-buffer-case-takes-precedence
4445 ;; We can safely scan the buffer stuff first
4446 (progn
4447 (idlwave-update-buffer-routine-info)
5e72c6b2 4448 (and load (idlwave-load-system-rinfo override-idle)))
f32b3b91 4449 ;; We first do the system info, and then the buffers
5e72c6b2 4450 (and load (idlwave-load-system-rinfo override-idle))
f32b3b91
CD
4451 (idlwave-update-buffer-routine-info))
4452
4453 ;; Let's see if there is a shell
4454 (let* ((shell-is-running (and (fboundp 'idlwave-shell-is-running)
4455 (idlwave-shell-is-running)))
4456 (ask-shell (and shell-is-running
4457 idlwave-query-shell-for-routine-info)))
52a244eb
S
4458
4459 ;; Load the library catalogs again, first re-scanning the path
4460 (when arg
4461 (if shell-is-running
4462 (idlwave-shell-send-command idlwave-shell-path-query
4463 '(progn
4464 (idlwave-shell-get-path-info)
4465 (idlwave-scan-library-catalogs))
4466 'hide)
4467 (idlwave-scan-library-catalogs)))
775591f7 4468
f32b3b91 4469 (if (or (not ask-shell)
52a244eb 4470 (not no-concatenate))
f32b3b91
CD
4471 ;; 1. If we are not going to ask the shell, we need to do the
4472 ;; concatenation now.
52a244eb
S
4473 ;; 2. When this function is called non-interactively, it
4474 ;; means that someone needs routine info *now*. The
4475 ;; shell update causes the concatenation to be
4476 ;; *delayed*, so not in time for the current command.
4477 ;; Therefore, we do a concatenation now, even though
4478 ;; the shell might do it again.
4479 (idlwave-concatenate-rinfo-lists nil 'run-hooks))
4480
f32b3b91 4481 (when ask-shell
52a244eb 4482 ;; Ask the shell about the routines it knows of.
f32b3b91 4483 (message "Querying the shell")
5e72c6b2
S
4484 (idlwave-shell-update-routine-info nil t)))))))
4485
52a244eb
S
4486
4487(defvar idlwave-load-rinfo-steps-done (make-vector 6 nil))
5e72c6b2
S
4488(defvar idlwave-load-rinfo-idle-timer nil)
4489(defun idlwave-start-load-rinfo-timer ()
4490 (if (or (and (fboundp 'itimerp)
4491 (itimerp idlwave-load-rinfo-idle-timer))
4492 (and (fboundp 'timerp)
4493 (timerp idlwave-load-rinfo-idle-timer)))
4494 (cancel-timer idlwave-load-rinfo-idle-timer))
52a244eb 4495 (setq idlwave-load-rinfo-steps-done (make-vector 6 nil))
5e72c6b2
S
4496 (setq idlwave-load-rinfo-idle-timer nil)
4497 (if (and idlwave-init-rinfo-when-idle-after
4498 (numberp idlwave-init-rinfo-when-idle-after)
4499 (not (equal 0 idlwave-init-rinfo-when-idle-after))
4500 (not idlwave-routines))
4501 (condition-case nil
4502 (progn
4503 (setq idlwave-load-rinfo-idle-timer
4504 (run-with-idle-timer
4505 idlwave-init-rinfo-when-idle-after
4506 nil 'idlwave-load-rinfo-next-step)))
4507 (error nil))))
4508
5e72c6b2
S
4509(defun idlwave-load-rinfo-next-step ()
4510 (let ((inhibit-quit t)
4511 (arr idlwave-load-rinfo-steps-done))
52a244eb 4512 (when (catch 'exit
5e72c6b2
S
4513 (when (not (aref arr 0))
4514 (message "Loading idlw-rinfo.el in idle time...")
4515 (load "idlw-rinfo" 'noerror 'nomessage)
4516 (message "Loading idlw-rinfo.el in idle time...done")
4517 (aset arr 0 t)
4518 (throw 'exit t))
4519 (when (not (aref arr 1))
4520 (message "Normalizing idlwave-system-routines in idle time...")
4521 (idlwave-reset-sintern t)
4522 (put 'idlwave-reset-sintern 'done-by-idle t)
4523 (setq idlwave-system-routines
4524 (idlwave-sintern-rinfo-list idlwave-system-routines 'sys))
4525 (message "Normalizing idlwave-system-routines in idle time...done")
4526 (aset arr 1 t)
4527 (throw 'exit t))
4528 (when (not (aref arr 2))
52a244eb
S
4529 (when (and (stringp idlwave-user-catalog-file)
4530 (file-regular-p idlwave-user-catalog-file))
4531 (message "Loading user catalog in idle time...")
5e72c6b2 4532 (condition-case nil
52a244eb
S
4533 (load-file idlwave-user-catalog-file)
4534 (error (throw 'exit nil)))
4535 ;; Check for the old style catalog and warn
4536 (if (and
4537 (boundp 'idlwave-library-routines)
4538 idlwave-library-routines)
775591f7 4539 (progn
52a244eb
S
4540 (setq idlwave-library-routines nil)
4541 (ding)
4542 (message "Outdated user catalog: %s... recreate"
4543 idlwave-user-catalog-file))
4544 (message "Loading user catalog in idle time...done"))
4545 (aset arr 2 t)
4546 (throw 'exit t)))
5e72c6b2 4547 (when (not (aref arr 3))
52a244eb
S
4548 (when idlwave-user-catalog-routines
4549 (message "Normalizing user catalog routines in idle time...")
4550 (setq idlwave-user-catalog-routines
4551 (idlwave-sintern-rinfo-list
4552 idlwave-user-catalog-routines 'sys))
4553 (message
4554 "Normalizing user catalog routines in idle time...done"))
5e72c6b2
S
4555 (aset arr 3 t)
4556 (throw 'exit t))
4557 (when (not (aref arr 4))
52a244eb
S
4558 (idlwave-scan-library-catalogs
4559 "Loading and normalizing library catalogs in idle time...")
4560 (aset arr 4 t)
4561 (throw 'exit t))
4562 (when (not (aref arr 5))
5e72c6b2
S
4563 (message "Finishing initialization in idle time...")
4564 (idlwave-routines)
4565 (message "Finishing initialization in idle time...done")
4566 (throw 'exit nil)))
52a244eb
S
4567 ;; restart the timer
4568 (if (sit-for 1)
4569 (idlwave-load-rinfo-next-step)
4570 (setq idlwave-load-rinfo-idle-timer
4571 (run-with-idle-timer
4572 idlwave-init-rinfo-when-idle-after
4573 nil 'idlwave-load-rinfo-next-step))))))
5e72c6b2
S
4574
4575(defun idlwave-load-system-rinfo (&optional force)
52a244eb 4576 ;; Load and case-treat the system and catalog files.
5e72c6b2
S
4577 (when (or force (not (aref idlwave-load-rinfo-steps-done 0)))
4578 (load "idlw-rinfo" 'noerror 'nomessage))
4579 (when (or force (not (aref idlwave-load-rinfo-steps-done 1)))
4580 (message "Normalizing idlwave-system-routines...")
4581 (setq idlwave-system-routines
4582 (idlwave-sintern-rinfo-list idlwave-system-routines 'sys))
4583 (message "Normalizing idlwave-system-routines...done"))
15e42531
CD
4584 (setq idlwave-routines (copy-sequence idlwave-system-routines))
4585 (setq idlwave-last-system-routine-info-cons-cell
4586 (nthcdr (1- (length idlwave-routines)) idlwave-routines))
52a244eb
S
4587 (when (and (stringp idlwave-user-catalog-file)
4588 (file-regular-p idlwave-user-catalog-file))
f32b3b91 4589 (condition-case nil
52a244eb
S
4590 (when (or force (not (aref idlwave-load-rinfo-steps-done 2)))
4591 (load-file idlwave-user-catalog-file))
4592 (error nil))
4593 (when (boundp 'idlwave-library-routines)
4594 (setq idlwave-library-routines nil)
4595 (error "Outdated user catalog: %s... recreate" idlwave-user-catalog-file))
4596 (setq idlwave-true-path-alist nil)
4597 (when (or force (not (aref idlwave-load-rinfo-steps-done 3)))
4598 (message "Normalizing user catalog routines...")
4599 (setq idlwave-user-catalog-routines
4600 (idlwave-sintern-rinfo-list
4601 idlwave-user-catalog-routines 'sys))
4602 (message "Normalizing user catalog routines...done")))
4603 (when (or force (not (aref idlwave-load-rinfo-steps-done 4)))
4604 (idlwave-scan-library-catalogs
4605 "Loading and normalizing library catalogs..."))
05a1abfc
CD
4606 (run-hooks 'idlwave-after-load-rinfo-hook))
4607
f32b3b91
CD
4608
4609(defun idlwave-update-buffer-routine-info ()
4610 (let (res)
52a244eb 4611 (cond
15e42531
CD
4612 ((eq idlwave-scan-all-buffers-for-routine-info t)
4613 ;; Scan all buffers, current buffer last
4614 (message "Scanning all buffers...")
52a244eb 4615 (setq res (idlwave-get-routine-info-from-buffers
15e42531
CD
4616 (reverse (buffer-list)))))
4617 ((null idlwave-scan-all-buffers-for-routine-info)
4618 ;; Don't scan any buffers
4619 (setq res nil))
4620 (t
f32b3b91
CD
4621 ;; Just scan this buffer
4622 (if (eq major-mode 'idlwave-mode)
4623 (progn
4624 (message "Scanning current buffer...")
4625 (setq res (idlwave-get-routine-info-from-buffers
15e42531 4626 (list (current-buffer))))))))
f32b3b91 4627 ;; Put the result into the correct variable
52a244eb
S
4628 (setq idlwave-buffer-routines
4629 (idlwave-sintern-rinfo-list res 'set))))
f32b3b91 4630
05a1abfc 4631(defun idlwave-concatenate-rinfo-lists (&optional quiet run-hook)
f32b3b91 4632 "Put the different sources for routine information together."
52a244eb 4633 ;; The sequence here is important because earlier definitions shadow
f32b3b91 4634 ;; later ones. We assume that if things in the buffers are newer
52a244eb 4635 ;; then in the shell of the system, they are meant to be different.
15e42531
CD
4636 (setcdr idlwave-last-system-routine-info-cons-cell
4637 (append idlwave-buffer-routines
4638 idlwave-compiled-routines
52a244eb
S
4639 idlwave-library-catalog-routines
4640 idlwave-user-catalog-routines))
f32b3b91 4641 (setq idlwave-class-alist nil)
15e42531 4642
f32b3b91 4643 ;; Give a message with information about the number of routines we have.
15e42531 4644 (unless quiet
52a244eb
S
4645 (message
4646 "Routines Found: buffer(%d) compiled(%d) library(%d) user(%d) system(%d)"
15e42531
CD
4647 (length idlwave-buffer-routines)
4648 (length idlwave-compiled-routines)
52a244eb
S
4649 (length idlwave-library-catalog-routines)
4650 (length idlwave-user-catalog-routines)
05a1abfc
CD
4651 (length idlwave-system-routines)))
4652 (if run-hook
4653 (run-hooks 'idlwave-update-rinfo-hook)))
15e42531
CD
4654
4655(defun idlwave-class-alist ()
4656 "Return the class alist - make it if necessary."
4657 (or idlwave-class-alist
4658 (let (class)
4659 (loop for x in idlwave-routines do
4660 (when (and (setq class (nth 2 x))
4661 (not (assq class idlwave-class-alist)))
4662 (push (list class) idlwave-class-alist)))
52a244eb 4663 idlwave-class-alist)))
15e42531
CD
4664
4665;; Three functions for the hooks
4666(defun idlwave-save-buffer-update ()
4667 (idlwave-update-current-buffer-info 'save-buffer))
4668(defun idlwave-kill-buffer-update ()
4669 (idlwave-update-current-buffer-info 'kill-buffer))
4670(defun idlwave-new-buffer-update ()
4671 (idlwave-update-current-buffer-info 'find-file))
4672
4673(defun idlwave-update-current-buffer-info (why)
52a244eb 4674 "Update idlwave-routines for current buffer. Can run from after-save-hook."
15e42531
CD
4675 (when (and (eq major-mode 'idlwave-mode)
4676 (or (eq t idlwave-auto-routine-info-updates)
4677 (memq why idlwave-auto-routine-info-updates))
4678 idlwave-scan-all-buffers-for-routine-info
4679 idlwave-routines)
4680 (condition-case nil
4681 (let (routines)
4682 (idlwave-replace-buffer-routine-info
4683 (buffer-file-name)
4684 (if (eq why 'kill-buffer)
4685 nil
4686 (setq routines
4687 (idlwave-sintern-rinfo-list
4688 (idlwave-get-routine-info-from-buffers
4689 (list (current-buffer))) 'set))))
4690 (idlwave-concatenate-rinfo-lists 'quiet)
4691 routines)
4692 (error nil))))
4693
4694(defun idlwave-replace-buffer-routine-info (file new)
4695 "Cut the part from FILE out of `idlwave-buffer-routines' and add NEW."
52a244eb 4696 (let ((list idlwave-buffer-routines)
15e42531
CD
4697 found)
4698 (while list
4699 ;; The following test uses eq to make sure it works correctly
4700 ;; when two buffers visit the same file. Then the file names
4701 ;; will be equal, but not eq.
52a244eb 4702 (if (eq (idlwave-routine-source-file (nth 3 (car list))) file)
15e42531
CD
4703 (progn
4704 (setcar list nil)
4705 (setq found t))
4706 (if found
52a244eb 4707 ;; End of that section reached. Jump.
15e42531
CD
4708 (setq list nil)))
4709 (setq list (cdr list)))
4710 (setq idlwave-buffer-routines
4711 (append new (delq nil idlwave-buffer-routines)))))
f32b3b91
CD
4712
4713;;----- Scanning buffers -------------------
4714
4715(defun idlwave-get-routine-info-from-buffers (buffers)
4716 "Call `idlwave-get-buffer-routine-info' on idlwave-mode buffers in BUFFERS."
4717 (let (buf routine-lists res)
4718 (save-excursion
4719 (while (setq buf (pop buffers))
4720 (set-buffer buf)
05a1abfc
CD
4721 (if (and (eq major-mode 'idlwave-mode)
4722 buffer-file-name)
f32b3b91
CD
4723 ;; yes, this buffer has the right mode.
4724 (progn (setq res (condition-case nil
4725 (idlwave-get-buffer-routine-info)
4726 (error nil)))
4727 (push res routine-lists)))))
4728 ;; Concatenate the individual lists and return the result
4729 (apply 'nconc routine-lists)))
4730
4731(defun idlwave-get-buffer-routine-info ()
4732 "Scan the current buffer for routine info. Return (PRO-LIST FUNC-LIST)."
4733 (let* ((case-fold-search t)
4734 routine-list string entry)
4735 (save-excursion
4736 (save-restriction
4737 (widen)
4738 (goto-char (point-min))
52a244eb 4739 (while (re-search-forward
15e42531 4740 "^[ \t]*\\(pro\\|function\\)[ \t]" nil t)
76959b77 4741 (setq string (buffer-substring-no-properties
f32b3b91 4742 (match-beginning 0)
52a244eb 4743 (progn
f32b3b91
CD
4744 (idlwave-end-of-statement)
4745 (point))))
4746 (setq entry (idlwave-parse-definition string))
4747 (push entry routine-list))))
4748 routine-list))
4749
15e42531 4750(defvar idlwave-scanning-lib-dir)
f32b3b91
CD
4751(defun idlwave-parse-definition (string)
4752 "Parse a module definition."
4753 (let ((case-fold-search t)
4754 start name args type keywords class)
4755 ;; Remove comments
4756 (while (string-match ";.*" string)
4757 (setq string (replace-match "" t t string)))
4758 ;; Remove the continuation line stuff
4759 (while (string-match "\\([^a-zA-Z0-9$_]\\)\\$[ \t]*\n" string)
4760 (setq string (replace-match "\\1 " t nil string)))
05a1abfc
CD
4761 (while (string-match "\n" string)
4762 (setq string (replace-match " " t nil string)))
f32b3b91
CD
4763 ;; Match the name and type.
4764 (when (string-match
4765 "\\<\\(pro\\|function\\)\\>\\s-+\\(\\([a-zA-Z0-9$_]+\\)::\\)?\\([a-zA-Z0-9$_]+\\)" string)
4766 (setq start (match-end 0))
4767 (setq type (downcase (match-string 1 string)))
4768 (if (match-beginning 3)
4769 (setq class (match-string 3 string)))
4770 (setq name (match-string 4 string)))
4771 ;; Match normal args and keyword args
4772 (while (string-match
15e42531 4773 ",\\s-*\\([a-zA-Z][a-zA-Z0-9$_]*\\|\\(_ref\\)?_extra\\)\\s-*\\(=\\)?"
f32b3b91
CD
4774 string start)
4775 (setq start (match-end 0))
15e42531 4776 (if (match-beginning 3)
f32b3b91
CD
4777 (push (match-string 1 string) keywords)
4778 (push (match-string 1 string) args)))
4779 ;; Normalize and sort.
4780 (setq args (nreverse args))
52a244eb 4781 (setq keywords (sort keywords (lambda (a b)
f32b3b91
CD
4782 (string< (downcase a) (downcase b)))))
4783 ;; Make and return the entry
4784 ;; We don't know which argument are optional, so this information
4785 ;; will not be contained in the calling sequence.
4786 (list name
4787 (if (equal type "pro") 'pro 'fun)
4788 class
4789 (cond ((not (boundp 'idlwave-scanning-lib))
52a244eb
S
4790 (list 'buffer (buffer-file-name)))
4791; ((string= (downcase
15e42531
CD
4792; (file-name-sans-extension
4793; (file-name-nondirectory (buffer-file-name))))
4794; (downcase name))
4795; (list 'lib))
4796; (t (cons 'lib (file-name-nondirectory (buffer-file-name))))
52a244eb
S
4797 (t (list 'user (file-name-nondirectory (buffer-file-name))
4798 idlwave-scanning-lib-dir "UserLib")))
4799 (concat
f32b3b91
CD
4800 (if (string= type "function") "Result = " "")
4801 (if class "Obj ->[%s::]" "")
4802 "%s"
4803 (if args
4804 (concat
4805 (if (string= type "function") "(" ", ")
4806 (mapconcat 'identity args ", ")
4807 (if (string= type "function") ")" ""))))
4808 (if keywords
52a244eb 4809 (cons nil (mapcar 'list keywords)) ;No help file
f32b3b91
CD
4810 nil))))
4811
f32b3b91 4812
52a244eb 4813;;----- Scanning the user catalog -------------------
15e42531
CD
4814
4815(defun idlwave-sys-dir ()
4816 "Return the syslib directory, or a dummy that never matches."
52a244eb
S
4817 (if (string= idlwave-system-directory "")
4818 "@@@@@@@@"
4819 idlwave-system-directory))
4820
15e42531
CD
4821
4822(defvar idlwave-shell-path-query)
52a244eb 4823(defun idlwave-create-user-catalog-file (&optional arg)
f32b3b91 4824 "Scan all files on selected dirs of IDL search path for routine information.
52a244eb
S
4825
4826A widget checklist will allow you to choose the directories. Write
4827the result as a file `idlwave-user-catalog-file'. When this file
4828exists, will be automatically loaded to give routine information about
4829library routines. With ARG, just rescan the same directories as last
4830time - so no widget will pop up."
f32b3b91
CD
4831 (interactive "P")
4832 ;; Make sure the file is loaded if it exists.
52a244eb
S
4833 (if (and (stringp idlwave-user-catalog-file)
4834 (file-regular-p idlwave-user-catalog-file))
f32b3b91 4835 (condition-case nil
52a244eb 4836 (load-file idlwave-user-catalog-file)
f32b3b91
CD
4837 (error nil)))
4838 ;; Make sure the file name makes sense
52a244eb
S
4839 (unless (and (stringp idlwave-user-catalog-file)
4840 (> (length idlwave-user-catalog-file) 0)
f32b3b91 4841 (file-accessible-directory-p
52a244eb
S
4842 (file-name-directory idlwave-user-catalog-file))
4843 (not (string= "" (file-name-nondirectory
4844 idlwave-user-catalog-file))))
4845 (error "`idlwave-user-catalog-file' does not point to a file in an accessible directory"))
4846
f32b3b91 4847 (cond
f32b3b91 4848 ;; Rescan the known directories
52a244eb
S
4849 ((and arg idlwave-path-alist
4850 (consp (car idlwave-path-alist)))
4851 (idlwave-scan-user-lib-files idlwave-path-alist))
4852
4853 ;; Expand the directories from library-path and run the widget
f32b3b91 4854 (idlwave-library-path
52a244eb
S
4855 (idlwave-display-user-catalog-widget
4856 (if idlwave-true-path-alist
4857 ;; Propagate any flags on the existing path-alist
4858 (mapcar (lambda (x)
4859 (let ((path-entry (assoc (file-truename x)
4860 idlwave-true-path-alist)))
4861 (if path-entry
4862 (cons x (cdr path-entry))
4863 (list x))))
4864 (idlwave-expand-path idlwave-library-path))
4865 (mapcar 'list (idlwave-expand-path idlwave-library-path)))))
4866
4867 ;; Ask the shell for the path and then run the widget
f32b3b91 4868 (t
f32b3b91 4869 (message "Asking the shell for IDL path...")
15e42531
CD
4870 (require 'idlw-shell)
4871 (idlwave-shell-send-command idlwave-shell-path-query
52a244eb 4872 '(idlwave-user-catalog-command-hook nil)
15e42531 4873 'hide))))
f32b3b91 4874
52a244eb
S
4875
4876;; Parse shell path information and select among it.
4877(defun idlwave-user-catalog-command-hook (&optional arg)
4878 ;; Command hook used by `idlwave-create-user-catalog-file'.
f32b3b91
CD
4879 (if arg
4880 ;; Scan immediately
52a244eb
S
4881 (idlwave-scan-user-lib-files idlwave-path-alist)
4882 ;; Set the path and display the widget
4883 (idlwave-shell-get-path-info 'no-write) ; set to something path-alist
4884 (idlwave-scan-library-catalogs "Locating library catalogs..." 'no-load)
4885 (idlwave-display-user-catalog-widget idlwave-path-alist)))
4886
4887(defconst idlwave-user-catalog-widget-help-string
4888 "This is the front-end to the creation of the IDLWAVE user catalog.
4889Please select the directories on IDL's search path from which you
4890would like to extract routine information, to be stored in the file:
f32b3b91
CD
4891
4892 %s
4893
52a244eb
S
4894If this is not the correct file, first set variable
4895`idlwave-user-catalog-file', and call this command again.
15e42531 4896
52a244eb
S
4897N.B. Many libraries include pre-scanned catalog files
4898\(\".idlwave_catalog\"). These are marked with \"[LIB]\", and need
4899not be scanned. You can scan your own libraries off-line using the
4900perl script `idlwave_catalog'.
15e42531 4901
f32b3b91
CD
4902After selecting the directories, choose [Scan & Save] to scan the library
4903directories and save the routine info.
4904\n")
4905
4906(defvar idlwave-widget)
4907(defvar widget-keymap)
52a244eb 4908(defun idlwave-display-user-catalog-widget (dirs-list)
f32b3b91
CD
4909 "Create the widget to select IDL search path directories for scanning."
4910 (interactive)
4911 (require 'widget)
4912 (require 'wid-edit)
52a244eb 4913 (unless dirs-list
f32b3b91
CD
4914 (error "Don't know IDL's search path"))
4915
f32b3b91
CD
4916 (kill-buffer (get-buffer-create "*IDLWAVE Widget*"))
4917 (switch-to-buffer (get-buffer-create "*IDLWAVE Widget*"))
4918 (kill-all-local-variables)
4919 (make-local-variable 'idlwave-widget)
52a244eb
S
4920 (widget-insert (format idlwave-user-catalog-widget-help-string
4921 idlwave-user-catalog-file))
4922
f32b3b91 4923 (widget-create 'push-button
52a244eb 4924 :notify 'idlwave-widget-scan-user-lib-files
f32b3b91
CD
4925 "Scan & Save")
4926 (widget-insert " ")
4927 (widget-create 'push-button
52a244eb 4928 :notify 'idlwave-delete-user-catalog-file
f32b3b91
CD
4929 "Delete File")
4930 (widget-insert " ")
4931 (widget-create 'push-button
52a244eb
S
4932 :notify
4933 '(lambda (&rest ignore)
4934 (let ((path-list (widget-get idlwave-widget :path-dirs)))
4935 (mapcar (lambda (x)
4936 (unless (memq 'lib (cdr x))
4937 (idlwave-path-alist-add-flag x 'user)))
4938 path-list)
4939 (idlwave-display-user-catalog-widget path-list)))
4940 "Select All Non-Lib")
f32b3b91
CD
4941 (widget-insert " ")
4942 (widget-create 'push-button
52a244eb
S
4943 :notify
4944 '(lambda (&rest ignore)
4945 (let ((path-list (widget-get idlwave-widget :path-dirs)))
4946 (mapcar (lambda (x)
4947 (idlwave-path-alist-remove-flag x 'user))
4948 path-list)
4949 (idlwave-display-user-catalog-widget path-list)))
f32b3b91 4950 "Deselect All")
52a244eb
S
4951 (widget-insert " ")
4952 (widget-create 'push-button
4953 :notify (lambda (&rest ignore)
4954 (kill-buffer (current-buffer)))
4955 "Quit")
f32b3b91
CD
4956 (widget-insert "\n\n")
4957
52a244eb
S
4958 (widget-insert "Select Directories: \n")
4959
f32b3b91
CD
4960 (setq idlwave-widget
4961 (apply 'widget-create
4962 'checklist
52a244eb
S
4963 :value (delq nil (mapcar (lambda (x)
4964 (if (memq 'user (cdr x))
4965 (car x)))
4966 dirs-list))
f32b3b91
CD
4967 :greedy t
4968 :tag "List of directories"
52a244eb
S
4969 (mapcar (lambda (x)
4970 (list 'item
4971 (if (memq 'lib (cdr x))
4972 (concat "[LIB] " (car x) )
4973 (car x)))) dirs-list)))
4974 (widget-put idlwave-widget :path-dirs dirs-list)
f32b3b91
CD
4975 (widget-insert "\n")
4976 (use-local-map widget-keymap)
4977 (widget-setup)
4978 (goto-char (point-min))
4979 (delete-other-windows))
52a244eb
S
4980
4981(defun idlwave-delete-user-catalog-file (&rest ignore)
f32b3b91 4982 (if (yes-or-no-p
52a244eb 4983 (format "Delete file %s " idlwave-user-catalog-file))
f32b3b91 4984 (progn
52a244eb
S
4985 (delete-file idlwave-user-catalog-file)
4986 (message "%s has been deleted" idlwave-user-catalog-file))))
f32b3b91 4987
52a244eb
S
4988(defun idlwave-widget-scan-user-lib-files (&rest ignore)
4989 ;; Call `idlwave-scan-user-lib-files' with data taken from the widget.
f32b3b91 4990 (let* ((widget idlwave-widget)
15e42531 4991 (selected-dirs (widget-value widget))
52a244eb
S
4992 (path-alist (widget-get widget :path-dirs))
4993 (this-path-alist path-alist)
4994 dir-entry)
4995 (while (setq dir-entry (pop this-path-alist))
4996 (if (member
4997 (if (memq 'lib (cdr dir-entry))
4998 (concat "[LIB] " (car dir-entry))
4999 (car dir-entry))
5000 selected-dirs)
5001 (idlwave-path-alist-add-flag dir-entry 'user)
5002 (idlwave-path-alist-remove-flag dir-entry 'user)))
5003 (idlwave-scan-user-lib-files path-alist)))
f32b3b91
CD
5004
5005(defvar font-lock-mode)
52a244eb
S
5006(defun idlwave-scan-user-lib-files (path-alist)
5007 ;; Scan the PRO files in PATH-ALIST and store the info in the user catalog
f32b3b91 5008 (let* ((idlwave-scanning-lib t)
15e42531 5009 (idlwave-scanning-lib-dir "")
f32b3b91 5010 (idlwave-completion-case nil)
15e42531 5011 dirs-alist dir files file)
52a244eb
S
5012 (setq idlwave-user-catalog-routines nil
5013 idlwave-path-alist path-alist ; for library-path instead
5014 idlwave-true-path-alist nil)
5015 (if idlwave-auto-write-paths (idlwave-write-paths))
f32b3b91
CD
5016 (save-excursion
5017 (set-buffer (get-buffer-create "*idlwave-scan.pro*"))
5018 (idlwave-mode)
15e42531
CD
5019 (setq dirs-alist (reverse path-alist))
5020 (while (setq dir (pop dirs-alist))
52a244eb 5021 (when (memq 'user (cdr dir)) ; Has it marked for scan?
15e42531 5022 (setq dir (car dir))
52a244eb 5023 (setq idlwave-scanning-lib-dir dir)
15e42531
CD
5024 (when (file-directory-p dir)
5025 (setq files (directory-files dir 'full "\\.[pP][rR][oO]\\'"))
5026 (while (setq file (pop files))
5027 (when (file-regular-p file)
5028 (if (not (file-readable-p file))
5029 (message "Skipping %s (no read permission)" file)
5030 (message "Scanning %s..." file)
5031 (erase-buffer)
5032 (insert-file-contents file 'visit)
52a244eb 5033 (setq idlwave-user-catalog-routines
15e42531
CD
5034 (append (idlwave-get-routine-info-from-buffers
5035 (list (current-buffer)))
52a244eb
S
5036 idlwave-user-catalog-routines)))))))))
5037 (message "Creating user catalog file...")
f32b3b91
CD
5038 (kill-buffer "*idlwave-scan.pro*")
5039 (kill-buffer (get-buffer-create "*IDLWAVE Widget*"))
15e42531
CD
5040 (let ((font-lock-maximum-size 0)
5041 (auto-mode-alist nil))
52a244eb 5042 (find-file idlwave-user-catalog-file))
f32b3b91
CD
5043 (if (and (boundp 'font-lock-mode)
5044 font-lock-mode)
5045 (font-lock-mode 0))
5046 (erase-buffer)
52a244eb 5047 (insert ";; IDLWAVE user catalog file\n")
f32b3b91
CD
5048 (insert (format ";; Created %s\n\n" (current-time-string)))
5049
f32b3b91 5050 ;; Define the routine info list
52a244eb 5051 (insert "\n(setq idlwave-user-catalog-routines\n '(")
5e72c6b2
S
5052 (let ((standard-output (current-buffer)))
5053 (mapcar (lambda (x)
5054 (insert "\n ")
5055 (prin1 x)
5056 (goto-char (point-max)))
52a244eb 5057 idlwave-user-catalog-routines))
f32b3b91 5058 (insert (format "))\n\n;;; %s ends here\n"
52a244eb 5059 (file-name-nondirectory idlwave-user-catalog-file)))
f32b3b91
CD
5060 (goto-char (point-min))
5061 ;; Save the buffer
5062 (save-buffer 0)
5063 (kill-buffer (current-buffer)))
52a244eb 5064 (message "Creating user catalog file...done")
f32b3b91 5065 (message "Info for %d routines saved in %s"
52a244eb
S
5066 (length idlwave-user-catalog-routines)
5067 idlwave-user-catalog-file)
f32b3b91
CD
5068 (sit-for 2)
5069 (idlwave-update-routine-info t))
5070
52a244eb
S
5071(defun idlwave-read-paths ()
5072 (if (and (stringp idlwave-path-file)
5073 (file-regular-p idlwave-path-file))
5074 (condition-case nil
5075 (load idlwave-path-file t t t)
5076 (error nil))))
5077
5078(defun idlwave-write-paths ()
5079 (interactive)
5080 (when (and idlwave-path-alist idlwave-system-directory)
5081 (let ((font-lock-maximum-size 0)
5082 (auto-mode-alist nil))
5083 (find-file idlwave-path-file))
5084 (if (and (boundp 'font-lock-mode)
5085 font-lock-mode)
5086 (font-lock-mode 0))
5087 (erase-buffer)
5088 (insert ";; IDLWAVE paths\n")
5089 (insert (format ";; Created %s\n\n" (current-time-string)))
5090 ;; Define the variable which knows the value of "!DIR"
5091 (insert (format "\n(setq idlwave-system-directory \"%s\")\n"
5092 idlwave-system-directory))
5093
5094 ;; Define the variable which contains a list of all scanned directories
5095 (insert "\n(setq idlwave-path-alist\n '(")
5096 (let ((standard-output (current-buffer)))
5097 (mapcar (lambda (x)
5098 (insert "\n ")
5099 (prin1 x)
5100 (goto-char (point-max)))
5101 idlwave-path-alist))
5102 (insert "))\n")
5103 (save-buffer 0)
5104 (kill-buffer (current-buffer))))
5105
5106
f32b3b91
CD
5107(defun idlwave-expand-path (path &optional default-dir)
5108 ;; Expand parts of path starting with '+' recursively into directory list.
5109 ;; Relative recursive path elements are expanded relative to DEFAULT-DIR.
5110 (message "Expanding path...")
5111 (let (path1 dir recursive)
5112 (while (setq dir (pop path))
5113 (if (setq recursive (string= (substring dir 0 1) "+"))
5114 (setq dir (substring dir 1)))
5115 (if (and recursive
5116 (not (file-name-absolute-p dir)))
5117 (setq dir (expand-file-name dir default-dir)))
5118 (if recursive
5119 ;; Expand recursively
5120 (setq path1 (append (idlwave-recursive-directory-list dir) path1))
5121 ;; Keep unchanged
5122 (push dir path1)))
5123 (message "Expanding path...done")
5124 (nreverse path1)))
5125
5126(defun idlwave-recursive-directory-list (dir)
5127 ;; Return a list of all directories below DIR, including DIR itself
5128 (let ((path (list dir)) path1 file files)
5129 (while (setq dir (pop path))
5130 (when (file-directory-p dir)
5131 (setq files (nreverse (directory-files dir t "[^.]")))
5132 (while (setq file (pop files))
52a244eb 5133 (if (file-directory-p file)
f32b3b91
CD
5134 (push (file-name-as-directory file) path)))
5135 (push dir path1)))
5136 path1))
5137
52a244eb
S
5138
5139;;----- Scanning the library catalogs ------------------
5140
5141(defun idlwave-scan-library-catalogs (&optional message-base no-load)
5142 "Scan for library catalog files (.idlwave_catalog) and ingest.
5143
5144All directories on `idlwave-path-alist' (or `idlwave-library-path'
5145instead, if present) are searched. Print MESSAGE-BASE along with the
5146libraries being loaded, if passed, and skip loading/normalizing if
5147NO-LOAD is non-nil. The variable `idlwave-use-library-catalogs' can
5148be set to nil to disable library catalog scanning."
5149 (when idlwave-use-library-catalogs
5150 (let ((dirs
5151 (if idlwave-library-path
5152 (idlwave-expand-path idlwave-library-path)
5153 (mapcar 'car idlwave-path-alist)))
5154 (old-libname "")
5155 dir-entry dir flags catalog all-routines)
5156 (if message-base (message message-base))
5157 (while (setq dir (pop dirs))
5158 (catch 'continue
5159 (when (file-readable-p
5160 (setq catalog (expand-file-name ".idlwave_catalog" dir)))
5161 (unless no-load
5162 (setq idlwave-library-catalog-routines nil)
5163 ;; Load the catalog file
5164 (condition-case nil
5165 (load catalog t t t)
5166 (error (throw 'continue t)))
5167 (when (and
5168 message-base
5169 (not (string= idlwave-library-catalog-libname
5170 old-libname)))
5171 (message (concat message-base
5172 idlwave-library-catalog-libname))
5173 (setq old-libname idlwave-library-catalog-libname))
5174 (when idlwave-library-catalog-routines
5175 (setq all-routines
5176 (append
5177 (idlwave-sintern-rinfo-list
5178 idlwave-library-catalog-routines 'sys dir)
5179 all-routines))))
5180
5181 ;; Add a 'lib flag if on path-alist
5182 (when (and idlwave-path-alist
5183 (setq dir-entry (assoc dir idlwave-path-alist)))
5184 (idlwave-path-alist-add-flag dir-entry 'lib)))))
5185 (unless no-load (setq idlwave-library-catalog-routines all-routines))
5186 (if message-base (message (concat message-base "done"))))))
5187
5188;;----- Communicating with the Shell -------------------
f32b3b91
CD
5189
5190;; First, here is the idl program which can be used to query IDL for
52a244eb 5191;; defined routines.
f32b3b91
CD
5192(defconst idlwave-routine-info.pro
5193 "
05a1abfc 5194;; START OF IDLWAVE SUPPORT ROUTINES
15e42531 5195pro idlwave_print_info_entry,name,func=func,separator=sep
f32b3b91 5196 ;; See if it's an object method
15e42531 5197 if name eq '' then return
52a244eb 5198 func = keyword_set(func)
f32b3b91
CD
5199 methsep = strpos(name,'::')
5200 meth = methsep ne -1
52a244eb 5201
f32b3b91
CD
5202 ;; Get routine info
5203 pars = routine_info(name,/parameters,functions=func)
5204 source = routine_info(name,/source,functions=func)
5205 nargs = pars.num_args
5206 nkw = pars.num_kw_args
5207 if nargs gt 0 then args = pars.args
5208 if nkw gt 0 then kwargs = pars.kw_args
52a244eb 5209
f32b3b91 5210 ;; Trim the class, and make the name
52a244eb 5211 if meth then begin
f32b3b91
CD
5212 class = strmid(name,0,methsep)
5213 name = strmid(name,methsep+2,strlen(name)-1)
52a244eb 5214 if nargs gt 0 then begin
f32b3b91
CD
5215 ;; remove the self argument
5216 wh = where(args ne 'SELF',nargs)
52a244eb 5217 if nargs gt 0 then args = args[wh]
f32b3b91
CD
5218 endif
5219 endif else begin
5220 ;; No class, just a normal routine.
5221 class = \"\"
5222 endelse
52a244eb 5223
f32b3b91
CD
5224 ;; Calling sequence
5225 cs = \"\"
5226 if func then cs = 'Result = '
5227 if meth then cs = cs + 'Obj -> [' + '%s' + '::]'
5228 cs = cs + '%s'
5229 if func then cs = cs + '(' else if nargs gt 0 then cs = cs + ', '
5230 if nargs gt 0 then begin
5231 for j=0,nargs-1 do begin
52a244eb 5232 cs = cs + args[j]
f32b3b91
CD
5233 if j lt nargs-1 then cs = cs + ', '
5234 endfor
5235 end
5236 if func then cs = cs + ')'
5237 ;; Keyword arguments
5238 kwstring = ''
5239 if nkw gt 0 then begin
5240 for j=0,nkw-1 do begin
52a244eb 5241 kwstring = kwstring + ' ' + kwargs[j]
f32b3b91
CD
5242 endfor
5243 endif
52a244eb
S
5244
5245 ret=(['IDLWAVE-PRO','IDLWAVE-FUN'])[func]
5246
5247 print,ret + ': ' + name + sep + class + sep + source[0].path $
f32b3b91
CD
5248 + sep + cs + sep + kwstring
5249end
5250
5251pro idlwave_routine_info
52a244eb 5252 on_error,1
f32b3b91
CD
5253 sep = '<@>'
5254 print,'>>>BEGIN OF IDLWAVE ROUTINE INFO (\"' + sep + '\" IS THE SEPARATOR)'
5255 all = routine_info()
5256 for i=0,n_elements(all)-1 do $
52a244eb 5257 idlwave_print_info_entry,all[i],separator=sep
f32b3b91
CD
5258 all = routine_info(/functions)
5259 for i=0,n_elements(all)-1 do $
52a244eb 5260 idlwave_print_info_entry,all[i],/func,separator=sep
f32b3b91
CD
5261 print,'>>>END OF IDLWAVE ROUTINE INFO'
5262end
05a1abfc
CD
5263
5264pro idlwave_get_sysvars
52a244eb 5265 on_error,1
05a1abfc
CD
5266 catch,error_status
5267 if error_status ne 0 then begin
5268 print, 'Cannot get info about system variables'
5269 endif else begin
5270 help,/brief,output=s,/system_variables ; ? unsafe use of OUTPUT=
5271 s = strtrim(strjoin(s,' ',/single),2) ; make one line
5272 v = strsplit(s,' +',/regex,/extract) ; get variables
5273 for i=0,n_elements(v)-1 do begin
5274 t = [''] ; get tag list
5275 a=execute('if n_tags('+v[i]+') gt 0 then t=tag_names('+v[i]+')')
5276 print, 'IDLWAVE-SYSVAR: '+v[i]+' '+strjoin(t,' ',/single)
5277 endfor
5278 endelse
5279end
5280
5281pro idlwave_get_class_tags, class
5282 res = execute('tags=tag_names({'+class+'})')
5e72c6b2 5283 if res then print,'IDLWAVE-CLASS-TAGS: '+class+' '+strjoin(tags,' ',/single)
05a1abfc
CD
5284end
5285;; END OF IDLWAVE SUPPORT ROUTINES
52a244eb 5286"
05a1abfc 5287 "The idl programs to get info from the shell.")
f32b3b91 5288
15e42531
CD
5289(defvar idlwave-idlwave_routine_info-compiled nil
5290 "Remembers if the routine info procedure is already compiled.")
f32b3b91
CD
5291
5292(defvar idlwave-shell-temp-pro-file)
15e42531 5293(defvar idlwave-shell-temp-rinfo-save-file)
52a244eb 5294(defun idlwave-shell-update-routine-info (&optional quiet run-hooks wait)
f32b3b91 5295 "Query the shell for routine_info of compiled modules and update the lists."
15e42531
CD
5296 ;; Save and compile the procedure. The compiled procedure is then
5297 ;; saved into an IDL SAVE file, to allow for fast RESTORE.
5298 ;; We need to RESTORE the procedure each time we use it, since
5e72c6b2 5299 ;; the user may have killed or redefined it. In particular,
15e42531
CD
5300 ;; .RESET_SESSION will kill all user procedures.
5301 (unless (and idlwave-idlwave_routine_info-compiled
5e72c6b2 5302 (file-readable-p (idlwave-shell-temp-file 'rinfo)))
15e42531
CD
5303 (save-excursion
5304 (set-buffer (idlwave-find-file-noselect
5e72c6b2 5305 (idlwave-shell-temp-file 'pro)))
15e42531
CD
5306 (erase-buffer)
5307 (insert idlwave-routine-info.pro)
5308 (save-buffer 0))
52a244eb 5309 (idlwave-shell-send-command
15e42531 5310 (concat ".run " idlwave-shell-temp-pro-file)
52a244eb 5311 nil 'hide wait)
5e72c6b2 5312; (message "SENDING SAVE") ; ????????????????????????
15e42531 5313 (idlwave-shell-send-command
52a244eb 5314 (format "save,'idlwave_routine_info','idlwave_print_info_entry','idlwave_get_class_tags','idlwave_get_sysvars',FILE='%s',/ROUTINES"
5e72c6b2 5315 (idlwave-shell-temp-file 'rinfo))
52a244eb 5316 nil 'hide wait))
15e42531
CD
5317
5318 ;; Restore and execute the procedure, analyze the output
5e72c6b2 5319; (message "SENDING RESTORE & EXECUTE") ; ????????????????????????
15e42531
CD
5320 (idlwave-shell-send-command
5321 (format "RESTORE, '%s' & idlwave_routine_info"
5322 idlwave-shell-temp-rinfo-save-file)
5323 `(progn
5324 (idlwave-shell-routine-info-filter)
05a1abfc 5325 (idlwave-concatenate-rinfo-lists ,quiet ,run-hooks))
52a244eb 5326 'hide wait))
f32b3b91
CD
5327
5328;; ---------------------------------------------------------------------------
5329;;
5330;; Completion and displaying routine calling sequences
5331
15e42531 5332(defvar idlwave-completion-help-info nil)
52a244eb 5333(defvar idlwave-completion-help-links nil)
15e42531 5334(defvar idlwave-current-obj_new-class nil)
05a1abfc 5335(defvar idlwave-complete-special nil)
15e42531 5336
f32b3b91
CD
5337(defun idlwave-complete (&optional arg module class)
5338 "Complete a function, procedure or keyword name at point.
5339This function is smart and figures out what can be legally completed
5340at this point.
5341- At the beginning of a statement it completes procedure names.
5342- In the middle of a statement it completes function names.
5343- after a `(' or `,' in the argument list of a function or procedure,
5344 it completes a keyword of the relevant function or procedure.
5345- In the first arg of `OBJ_NEW', it completes a class name.
5346
5e72c6b2 5347When several completions are possible, a list will be displayed in the
f32b3b91 5348*Completions* buffer. If this list is too long to fit into the
5e72c6b2
S
5349window, scrolling can be achieved by repeatedly pressing
5350\\[idlwave-complete].
f32b3b91
CD
5351
5352The function also knows about object methods. When it needs a class
5353name, the action depends upon `idlwave-query-class', which see. You
5e72c6b2
S
5354can force IDLWAVE to ask you for a class name with a
5355\\[universal-argument] prefix argument to this command.
f32b3b91
CD
5356
5357See also the variables `idlwave-keyword-completion-adds-equal' and
5358`idlwave-function-completion-adds-paren'.
5359
5360The optional ARG can be used to specify the completion type in order
5361to override IDLWAVE's idea of what should be completed at point.
5362Possible values are:
5363
53640 <=> query for the completion type
53651 <=> 'procedure
53662 <=> 'procedure-keyword
53673 <=> 'function
53684 <=> 'function-keyword
53695 <=> 'procedure-method
53706 <=> 'procedure-method-keyword
53717 <=> 'function-method
53728 <=> 'function-method-keyword
53739 <=> 'class
5374
5e72c6b2
S
5375As a special case, the universal argument C-u forces completion of
5376function names in places where the default would be a keyword.
5377
52a244eb
S
5378Two prefix argument, C-u C-u, prompts for a regexp by which to limit
5379completion.
5380
f32b3b91
CD
5381For Lisp programmers only:
5382When we force a keyword, optional argument MODULE can contain the module name.
5383When we force a method or a method keyword, CLASS can specify the class."
5384 (interactive "P")
5385 (idlwave-routines)
5386 (let* ((where-list
5387 (if (and arg
52a244eb 5388 (or (and (integerp arg) (not (equal arg '(16))))
f32b3b91
CD
5389 (symbolp arg)))
5390 (idlwave-make-force-complete-where-list arg module class)
5391 (idlwave-where)))
5392 (what (nth 2 where-list))
52a244eb
S
5393 (idlwave-force-class-query (equal arg '(4)))
5394 (completion-regexp-list
5395 (if (equal arg '(16))
5396 (list (read-string (concat "Completion Regexp: "))))))
5397
f32b3b91
CD
5398 (if (and module (string-match "::" module))
5399 (setq class (substring module 0 (match-beginning 0))
5400 module (substring module (match-end 0))))
5401
5402 (cond
5403
5404 ((and (null arg)
5405 (eq (car-safe last-command) 'idlwave-display-completion-list)
595ab50b 5406 (get-buffer-window "*Completions*"))
f32b3b91
CD
5407 (setq this-command last-command)
5408 (idlwave-scroll-completions))
5409
52a244eb 5410 ;; Complete a filename in quotes
05a1abfc
CD
5411 ((and (idlwave-in-quote)
5412 (not (eq what 'class)))
5413 (idlwave-complete-filename))
5414
52a244eb
S
5415 ;; Check for any special completion functions
5416 ((and idlwave-complete-special
5417 (idlwave-call-special idlwave-complete-special)))
5418
f32b3b91
CD
5419 ((null what)
5420 (error "Nothing to complete here"))
5421
52a244eb 5422 ;; Complete a class
f32b3b91 5423 ((eq what 'class)
15e42531 5424 (setq idlwave-completion-help-info '(class))
f32b3b91
CD
5425 (idlwave-complete-class))
5426
5427 ((eq what 'procedure)
5428 ;; Complete a procedure name
5e72c6b2
S
5429 (let* ((cw-list (nth 3 where-list))
5430 (class-selector (idlwave-determine-class cw-list 'pro))
5431 (super-classes (unless (idlwave-explicit-class-listed cw-list)
5432 (idlwave-all-class-inherits class-selector)))
f32b3b91
CD
5433 (isa (concat "procedure" (if class-selector "-method" "")))
5434 (type-selector 'pro))
52a244eb 5435 (setq idlwave-completion-help-info
05a1abfc 5436 (list 'routine nil type-selector class-selector nil super-classes))
f32b3b91
CD
5437 (idlwave-complete-in-buffer
5438 'procedure (if class-selector 'method 'routine)
5439 (idlwave-routines) 'idlwave-selector
5440 (format "Select a %s name%s"
5441 isa
5442 (if class-selector
52a244eb
S
5443 (format " (class is %s)"
5444 (if (eq class-selector t)
76959b77 5445 "unknown" class-selector))
f32b3b91
CD
5446 ""))
5447 isa
52a244eb 5448 'idlwave-attach-method-classes 'idlwave-add-file-link-selector)))
f32b3b91
CD
5449
5450 ((eq what 'function)
5451 ;; Complete a function name
5e72c6b2
S
5452 (let* ((cw-list (nth 3 where-list))
5453 (class-selector (idlwave-determine-class cw-list 'fun))
5454 (super-classes (unless (idlwave-explicit-class-listed cw-list)
5455 (idlwave-all-class-inherits class-selector)))
f32b3b91
CD
5456 (isa (concat "function" (if class-selector "-method" "")))
5457 (type-selector 'fun))
52a244eb 5458 (setq idlwave-completion-help-info
05a1abfc 5459 (list 'routine nil type-selector class-selector nil super-classes))
f32b3b91
CD
5460 (idlwave-complete-in-buffer
5461 'function (if class-selector 'method 'routine)
5462 (idlwave-routines) 'idlwave-selector
5463 (format "Select a %s name%s"
5464 isa
5465 (if class-selector
52a244eb 5466 (format " (class is %s)"
76959b77
S
5467 (if (eq class-selector t)
5468 "unknown" class-selector))
f32b3b91
CD
5469 ""))
5470 isa
52a244eb 5471 'idlwave-attach-method-classes 'idlwave-add-file-link-selector)))
f32b3b91 5472
52a244eb 5473 ((and (memq what '(procedure-keyword function-keyword)) ; Special Case
5e72c6b2
S
5474 (equal arg '(4)))
5475 (idlwave-complete 3))
5476
f32b3b91
CD
5477 ((eq what 'procedure-keyword)
5478 ;; Complete a procedure keyword
5479 (let* ((where (nth 3 where-list))
5480 (name (car where))
5481 (method-selector name)
5482 (type-selector 'pro)
5483 (class (idlwave-determine-class where 'pro))
5484 (class-selector class)
05a1abfc 5485 (super-classes (idlwave-all-class-inherits class-selector))
f32b3b91 5486 (isa (format "procedure%s-keyword" (if class "-method" "")))
15e42531 5487 (entry (idlwave-best-rinfo-assq
f32b3b91 5488 name 'pro class (idlwave-routines)))
52a244eb 5489 (list (idlwave-entry-keywords entry 'do-link)))
f32b3b91
CD
5490 (unless (or entry (eq class t))
5491 (error "Nothing known about procedure %s"
5492 (idlwave-make-full-name class name)))
52a244eb 5493 (setq list (idlwave-fix-keywords name 'pro class list super-classes))
f32b3b91
CD
5494 (unless list (error (format "No keywords available for procedure %s"
5495 (idlwave-make-full-name class name))))
52a244eb
S
5496 (setq idlwave-completion-help-info
5497 (list 'keyword name type-selector class-selector entry super-classes))
f32b3b91
CD
5498 (idlwave-complete-in-buffer
5499 'keyword 'keyword list nil
5500 (format "Select keyword for procedure %s%s"
5501 (idlwave-make-full-name class name)
15e42531 5502 (if (or (member '("_EXTRA") list)
52a244eb 5503 (member '("_REF_EXTRA") list))
15e42531 5504 " (note _EXTRA)" ""))
f32b3b91
CD
5505 isa
5506 'idlwave-attach-keyword-classes)))
5507
5508 ((eq what 'function-keyword)
5509 ;; Complete a function keyword
5510 (let* ((where (nth 3 where-list))
5511 (name (car where))
5512 (method-selector name)
5513 (type-selector 'fun)
5514 (class (idlwave-determine-class where 'fun))
5515 (class-selector class)
05a1abfc 5516 (super-classes (idlwave-all-class-inherits class-selector))
f32b3b91 5517 (isa (format "function%s-keyword" (if class "-method" "")))
15e42531 5518 (entry (idlwave-best-rinfo-assq
f32b3b91 5519 name 'fun class (idlwave-routines)))
52a244eb 5520 (list (idlwave-entry-keywords entry 'do-link))
15e42531 5521 msg-name)
f32b3b91
CD
5522 (unless (or entry (eq class t))
5523 (error "Nothing known about function %s"
5524 (idlwave-make-full-name class name)))
52a244eb 5525 (setq list (idlwave-fix-keywords name 'fun class list super-classes))
15e42531
CD
5526 ;; OBJ_NEW: Messages mention the proper Init method
5527 (setq msg-name (if (and (null class)
5528 (string= (upcase name) "OBJ_NEW"))
5529 (concat idlwave-current-obj_new-class
5530 "::Init (via OBJ_NEW)")
5531 (idlwave-make-full-name class name)))
f32b3b91 5532 (unless list (error (format "No keywords available for function %s"
15e42531 5533 msg-name)))
52a244eb 5534 (setq idlwave-completion-help-info
05a1abfc 5535 (list 'keyword name type-selector class-selector nil super-classes))
f32b3b91
CD
5536 (idlwave-complete-in-buffer
5537 'keyword 'keyword list nil
15e42531
CD
5538 (format "Select keyword for function %s%s" msg-name
5539 (if (or (member '("_EXTRA") list)
52a244eb 5540 (member '("_REF_EXTRA") list))
15e42531 5541 " (note _EXTRA)" ""))
f32b3b91
CD
5542 isa
5543 'idlwave-attach-keyword-classes)))
15e42531 5544
f32b3b91
CD
5545 (t (error "This should not happen (idlwave-complete)")))))
5546
05a1abfc
CD
5547(defvar idlwave-complete-special nil
5548 "List of special completion functions.
52a244eb
S
5549These functions are called for each completion. Each function must
5550check if its own special completion context is present. If yes, it
5551should use `idlwave-complete-in-buffer' to do some completion and
5552return t. If such a function returns t, *no further* attempts to
5553complete other contexts will be done. If the function returns nil,
5554other completions will be tried.")
76959b77
S
5555
5556(defun idlwave-call-special (functions &rest args)
5557 (let ((funcs functions)
5558 fun ret)
05a1abfc 5559 (catch 'exit
76959b77
S
5560 (while (setq fun (pop funcs))
5561 (if (setq ret (apply fun args))
5562 (throw 'exit ret)))
05a1abfc
CD
5563 nil)))
5564
f32b3b91
CD
5565(defun idlwave-make-force-complete-where-list (what &optional module class)
5566 ;; Return an artificial WHERE specification to force the completion
5567 ;; routine to complete a specific item independent of context.
5568 ;; WHAT is the prefix arg of `idlwave-complete', see there for details.
5569 ;; MODULE and CLASS can be used to specify the routine name and class.
5570 ;; The class name will also be found in MODULE if that is like "class::mod".
5571 (let* ((what-list '(("procedure") ("procedure-keyword")
5572 ("function") ("function-keyword")
5573 ("procedure-method") ("procedure-method-keyword")
5574 ("function-method") ("function-method-keyword")
5575 ("class")))
5576 (module (idlwave-sintern-routine-or-method module class))
5577 (class (idlwave-sintern-class class))
52a244eb 5578 (what (cond
f32b3b91
CD
5579 ((equal what 0)
5580 (setq what
52a244eb 5581 (intern (completing-read
f32b3b91
CD
5582 "Complete what? " what-list nil t))))
5583 ((integerp what)
5584 (setq what (intern (car (nth (1- what) what-list)))))
5585 ((and what
5586 (symbolp what)
5587 (assoc (symbol-name what) what-list))
5588 what)
5589 (t (error "Illegal WHAT"))))
5590 (nil-list '(nil nil nil nil))
5591 (class-list (list nil nil (or class t) nil)))
5592
5593 (cond
5594
5595 ((eq what 'procedure)
5596 (list nil-list nil-list 'procedure nil-list nil))
5597
5598 ((eq what 'procedure-keyword)
5599 (let* ((class-selector nil)
05a1abfc 5600 (super-classes nil)
f32b3b91
CD
5601 (type-selector 'pro)
5602 (pro (or module
52a244eb 5603 (idlwave-completing-read
f32b3b91
CD
5604 "Procedure: " (idlwave-routines) 'idlwave-selector))))
5605 (setq pro (idlwave-sintern-routine pro))
5606 (list nil-list nil-list 'procedure-keyword
5607 (list pro nil nil nil) nil)))
5608
5609 ((eq what 'function)
5610 (list nil-list nil-list 'function nil-list nil))
5611
5612 ((eq what 'function-keyword)
5613 (let* ((class-selector nil)
05a1abfc 5614 (super-classes nil)
f32b3b91
CD
5615 (type-selector 'fun)
5616 (func (or module
52a244eb 5617 (idlwave-completing-read
f32b3b91
CD
5618 "Function: " (idlwave-routines) 'idlwave-selector))))
5619 (setq func (idlwave-sintern-routine func))
5620 (list nil-list nil-list 'function-keyword
5621 (list func nil nil nil) nil)))
5622
5623 ((eq what 'procedure-method)
5624 (list nil-list nil-list 'procedure class-list nil))
5625
5626 ((eq what 'procedure-method-keyword)
5627 (let* ((class (idlwave-determine-class class-list 'pro))
5628 (class-selector class)
05a1abfc 5629 (super-classes (idlwave-all-class-inherits class-selector))
f32b3b91
CD
5630 (type-selector 'pro)
5631 (pro (or module
5632 (idlwave-completing-read
5633 (format "Procedure in %s class: " class-selector)
5634 (idlwave-routines) 'idlwave-selector))))
5635 (setq pro (idlwave-sintern-method pro))
5636 (list nil-list nil-list 'procedure-keyword
5637 (list pro nil class nil) nil)))
5638
5639 ((eq what 'function-method)
5640 (list nil-list nil-list 'function class-list nil))
5641
5642 ((eq what 'function-method-keyword)
5643 (let* ((class (idlwave-determine-class class-list 'fun))
5644 (class-selector class)
05a1abfc 5645 (super-classes (idlwave-all-class-inherits class-selector))
f32b3b91
CD
5646 (type-selector 'fun)
5647 (func (or module
5648 (idlwave-completing-read
5649 (format "Function in %s class: " class-selector)
5650 (idlwave-routines) 'idlwave-selector))))
5651 (setq func (idlwave-sintern-method func))
5652 (list nil-list nil-list 'function-keyword
5653 (list func nil class nil) nil)))
5654
5655 ((eq what 'class)
5656 (list nil-list nil-list 'class nil-list nil))
52a244eb 5657
f32b3b91
CD
5658 (t (error "Illegal value for WHAT")))))
5659
5660(defun idlwave-completing-read (&rest args)
5661 ;; Completing read, case insensitive
5662 (let ((old-value (default-value 'completion-ignore-case)))
5663 (unwind-protect
5664 (progn
5665 (setq-default completion-ignore-case t)
5666 (apply 'completing-read args))
5667 (setq-default completion-ignore-case old-value))))
5668
05a1abfc
CD
5669(defvar idlwave-shell-default-directory)
5670(defun idlwave-complete-filename ()
5671 "Use the comint stuff to complete a file name."
5672 (require 'comint)
5673 (let* ((comint-file-name-chars "~/A-Za-z0-9+@:_.$#%={}\\-")
5674 (comint-completion-addsuffix nil)
5675 (default-directory
5676 (if (and (boundp 'idlwave-shell-default-directory)
5677 (stringp idlwave-shell-default-directory)
5678 (file-directory-p idlwave-shell-default-directory))
5679 idlwave-shell-default-directory
52a244eb 5680 default-directory)))
05a1abfc
CD
5681 (comint-dynamic-complete-filename)))
5682
f32b3b91
CD
5683(defun idlwave-make-full-name (class name)
5684 ;; Make a fully qualified module name including the class name
5685 (concat (if class (format "%s::" class) "") name))
5686
15e42531
CD
5687(defun idlwave-rinfo-assoc (name type class list)
5688 "Like `idlwave-rinfo-assq', but sintern strings first."
52a244eb 5689 (idlwave-rinfo-assq
15e42531
CD
5690 (idlwave-sintern-routine-or-method name class)
5691 type (idlwave-sintern-class class) list))
5692
f32b3b91
CD
5693(defun idlwave-rinfo-assq (name type class list)
5694 ;; Works like assq, but also checks type and class
5695 (catch 'exit
5696 (let (match)
5697 (while (setq match (assq name list))
5698 (and (or (eq type t)
5699 (eq (nth 1 match) type))
5700 (eq (nth 2 match) class)
5701 (throw 'exit match))
5702 (setq list (cdr (memq match list)))))))
5703
05a1abfc 5704(defun idlwave-rinfo-assq-any-class (name type class list)
52a244eb 5705 ;; Return the first matching method on the inheritance list
05a1abfc
CD
5706 (let* ((classes (cons class (idlwave-all-class-inherits class)))
5707 class rtn)
5708 (while classes
5709 (if (setq rtn (idlwave-rinfo-assq name type (pop classes) list))
5710 (setq classes nil)))
5711 rtn))
5712
52a244eb
S
5713(defun idlwave-best-rinfo-assq (name type class list &optional with-file
5714 keep-system)
5715 "Like `idlwave-rinfo-assq', but get all twins and sort, then return first.
5716If WITH-FILE is passed, find the best rinfo entry with a file
5717included. If KEEP-SYSTEM is set, don't prune system for compiled
5718syslib files."
15e42531 5719 (let ((twins (idlwave-routine-twins
05a1abfc 5720 (idlwave-rinfo-assq-any-class name type class list)
15e42531
CD
5721 list))
5722 syslibp)
5723 (when (> (length twins) 1)
5724 (setq twins (sort twins 'idlwave-routine-entry-compare-twins))
52a244eb
S
5725 (if (and (null keep-system)
5726 (eq 'system (car (nth 3 (car twins))))
15e42531
CD
5727 (setq syslibp (idlwave-any-syslib (cdr twins)))
5728 (not (equal 1 syslibp)))
52a244eb
S
5729 ;; Its a compiled syslib, so we need to remove the system entry
5730 (setq twins (cdr twins)))
5731 (if with-file
5732 (setq twins (delq nil
5733 (mapcar (lambda (x)
5734 (if (nth 1 (nth 3 x)) x))
5735 twins)))))
15e42531
CD
5736 (car twins)))
5737
52a244eb
S
5738(defun idlwave-best-rinfo-assoc (name type class list &optional with-file
5739 keep-system)
15e42531
CD
5740 "Like `idlwave-best-rinfo-assq', but sintern strings first."
5741 (idlwave-best-rinfo-assq
5742 (idlwave-sintern-routine-or-method name class)
52a244eb 5743 type (idlwave-sintern-class class) list with-file keep-system))
15e42531
CD
5744
5745(defun idlwave-any-syslib (entries)
5746 "Does the entry list ENTRIES contain a syslib entry?
5747If yes, return the index (>=1)."
5748 (let (file (cnt 0))
5749 (catch 'exit
5750 (while entries
5751 (incf cnt)
52a244eb
S
5752 (setq file (idlwave-routine-source-file (nth 3 (car entries))))
5753 (if (and file (idlwave-syslib-p file))
15e42531
CD
5754 (throw 'exit cnt)
5755 (setq entries (cdr entries))))
5756 nil)))
5757
f32b3b91
CD
5758(defun idlwave-all-assq (key list)
5759 "Return a list of all associations of Key in LIST."
5760 (let (rtn elt)
5761 (while (setq elt (assq key list))
5762 (push elt rtn)
5763 (setq list (cdr (memq elt list))))
5764 (nreverse rtn)))
5765
5766(defun idlwave-all-method-classes (method &optional type)
5767 "Return all classes which have a method METHOD. TYPE is 'fun or 'pro.
5768When TYPE is not specified, both procedures and functions will be considered."
5769 (if (null method)
15e42531 5770 (mapcar 'car (idlwave-class-alist))
f32b3b91
CD
5771 (let (rtn)
5772 (mapcar (lambda (x)
5773 (and (nth 2 x)
5774 (or (not type)
5775 (eq type (nth 1 x)))
5776 (push (nth 2 x) rtn)))
5777 (idlwave-all-assq method (idlwave-routines)))
5778 (idlwave-uniquify rtn))))
5779
5780(defun idlwave-all-method-keyword-classes (method keyword &optional type)
5781 "Return all classes which have a method METHOD with keyword KEYWORD.
5782TYPE is 'fun or 'pro.
5783When TYPE is not specified, both procedures and functions will be considered."
5784 (if (or (null method)
5785 (null keyword))
5786 nil
5787 (let (rtn)
5788 (mapcar (lambda (x)
52a244eb
S
5789 (and (nth 2 x) ; non-nil class
5790 (or (not type) ; correct or unspecified type
f32b3b91 5791 (eq type (nth 1 x)))
52a244eb 5792 (assoc keyword (idlwave-entry-keywords x))
f32b3b91
CD
5793 (push (nth 2 x) rtn)))
5794 (idlwave-all-assq method (idlwave-routines)))
5795 (idlwave-uniquify rtn))))
5796
05a1abfc
CD
5797(defun idlwave-members-only (list club)
5798 "Return list of all elements in LIST which are also in CLUB."
5799 (let (rtn)
5800 (while list
5801 (if (member (car list) club)
5802 (setq rtn (cons (car list) rtn)))
5803 (setq list (cdr list)))
5804 (nreverse rtn)))
5805
5806(defun idlwave-nonmembers-only (list club)
5807 "Return list of all elements in LIST which are not in CLUB."
5808 (let (rtn)
5809 (while list
5810 (if (member (car list) club)
5811 nil
5812 (setq rtn (cons (car list) rtn)))
5813 (setq list (cdr list)))
5814 (nreverse rtn)))
5815
5e72c6b2
S
5816(defun idlwave-explicit-class-listed (info)
5817 "Return whether or not the class is listed explicitly, ala a->b::c.
5818INFO is as returned by idlwave-what-function or -procedure."
5819 (let ((apos (nth 3 info)))
5820 (if apos
5821 (save-excursion (goto-char apos)
5822 (looking-at "->[a-zA-Z][a-zA-Z0-9$_]*::")))))
5823
76959b77
S
5824(defvar idlwave-determine-class-special nil
5825 "List of special functions for determining class.
5826Must accept two arguments: `apos' and `info'")
5827
f32b3b91 5828(defun idlwave-determine-class (info type)
52a244eb 5829 ;; Determine the class of a routine call.
76959b77
S
5830 ;; INFO is the `cw-list' structure as returned by idlwave-where.
5831 ;; The second element in this structure is the class. When nil, we
5832 ;; return nil. When t, try to get the class from text properties at
5833 ;; the arrow. When the object is "self", we use the class of the
5834 ;; current routine. otherwise prompt the user for a class name.
5835 ;; Also stores the selected class as a text property at the arrow.
f32b3b91
CD
5836 ;; TYPE is 'fun or 'pro.
5837 (let* ((class (nth 2 info))
5838 (apos (nth 3 info))
5839 (nassoc (assoc (if (stringp (car info))
5840 (upcase (car info))
5841 (car info))
5842 idlwave-query-class))
5843 (dassoc (assq (if (car info) 'keyword-default 'method-default)
5844 idlwave-query-class))
5845 (query (cond (nassoc (cdr nassoc))
5846 (dassoc (cdr dassoc))
5847 (t t)))
5848 (arrow (and apos (string= (buffer-substring apos (+ 2 apos)) "->")))
52a244eb 5849 (is-self
15e42531
CD
5850 (and arrow
5851 (save-excursion (goto-char apos)
5852 (forward-word -1)
5853 (let ((case-fold-search t))
5854 (looking-at "self\\>")))))
f32b3b91 5855 (force-query idlwave-force-class-query)
76959b77 5856 store special-class class-alist)
f32b3b91
CD
5857 (cond
5858 ((null class) nil)
5859 ((eq t class)
5860 ;; There is an object which would like to know its class
5861 (if (and arrow (get-text-property apos 'idlwave-class)
5862 idlwave-store-inquired-class
5863 (not force-query))
5864 (setq class (get-text-property apos 'idlwave-class)
5865 class (idlwave-sintern-class class)))
76959b77
S
5866 (if (and (eq t class) is-self)
5867 (setq class (or (nth 2 (idlwave-current-routine)) class)))
5868
5869 ;; Before prompting, try any special class determination routines
52a244eb 5870 (when (and (eq t class)
76959b77
S
5871 idlwave-determine-class-special
5872 (not force-query))
52a244eb 5873 (setq special-class
76959b77 5874 (idlwave-call-special idlwave-determine-class-special apos))
52a244eb 5875 (if special-class
76959b77
S
5876 (setq class (idlwave-sintern-class special-class)
5877 store idlwave-store-inquired-class)))
52a244eb 5878
76959b77 5879 ;; Prompt for a class, if we need to
f32b3b91
CD
5880 (when (and (eq class t)
5881 (or force-query query))
52a244eb 5882 (setq class-alist
f32b3b91
CD
5883 (mapcar 'list (idlwave-all-method-classes (car info) type)))
5884 (setq class
5885 (idlwave-sintern-class
5886 (cond
5887 ((and (= (length class-alist) 0) (not force-query))
5888 (error "No classes available with method %s" (car info)))
5889 ((and (= (length class-alist) 1) (not force-query))
5890 (car (car class-alist)))
52a244eb 5891 (t
f32b3b91 5892 (setq store idlwave-store-inquired-class)
52a244eb 5893 (idlwave-completing-read
f32b3b91
CD
5894 (format "Class%s: " (if (stringp (car info))
5895 (format " for %s method %s"
5896 type (car info))
5897 ""))
5898 class-alist nil nil nil 'idlwave-class-history))))))
76959b77
S
5899
5900 ;; Store it, if requested
f32b3b91
CD
5901 (when (and class (not (eq t class)))
5902 ;; We have a real class here
5903 (when (and store arrow)
76959b77 5904 (condition-case ()
52a244eb
S
5905 (add-text-properties
5906 apos (+ apos 2)
5907 `(idlwave-class ,class face ,idlwave-class-arrow-face
76959b77
S
5908 rear-nonsticky t))
5909 (error nil)))
f32b3b91
CD
5910 (setf (nth 2 info) class))
5911 ;; Return the class
5912 class)
5913 ;; Default as fallback
5914 (t class))))
5915
5916(defvar type-selector)
5917(defvar class-selector)
5918(defvar method-selector)
05a1abfc 5919(defvar super-classes)
f32b3b91
CD
5920(defun idlwave-selector (a)
5921 (and (eq (nth 1 a) type-selector)
5922 (or (and (nth 2 a) (eq class-selector t))
05a1abfc 5923 (eq (nth 2 a) class-selector)
52a244eb
S
5924 (memq (nth 2 a) super-classes))))
5925
5926(defun idlwave-add-file-link-selector (a)
5927 ;; Record a file link, if any, for the tested names during selection.
5928 (let ((sel (idlwave-selector a)) file)
5929 (if (and sel (setq file (idlwave-entry-has-help a)))
5930 (push (cons (car a) file) idlwave-completion-help-links))
5931 sel))
5932
f32b3b91
CD
5933
5934(defun idlwave-where ()
52a244eb 5935 "Find out where we are.
f32b3b91 5936The return value is a list with the following stuff:
5e72c6b2 5937\(PRO-LIST FUNC-LIST COMPLETE-WHAT CW-LIST LAST-CHAR)
f32b3b91
CD
5938
5939PRO-LIST (PRO POINT CLASS ARROW)
5940FUNC-LIST (FUNC POINT CLASS ARROW)
5941COMPLETE-WHAT a symbol indicating what kind of completion makes sense here
52a244eb 5942CW-LIST (PRO-OR-FUNC POINT CLASS ARROW) Like PRO-LIST, for what can
5e72c6b2 5943 be completed here.
f32b3b91
CD
5944LAST-CHAR last relevant character before point (non-white non-comment,
5945 not part of current identifier or leading slash).
5946
5947In the lists, we have these meanings:
5948PRO: Procedure name
5949FUNC: Function name
5950POINT: Where is this
5951CLASS: What class has the routine (nil=no, t=is method, but class unknown)
5e72c6b2 5952ARROW: Location of the arrow"
f32b3b91 5953 (idlwave-routines)
52a244eb 5954 (let* (;(bos (save-excursion (idlwave-beginning-of-statement) (point)))
15e42531 5955 (bos (save-excursion (idlwave-start-of-substatement 'pre) (point)))
f32b3b91
CD
5956 (func-entry (idlwave-what-function bos))
5957 (func (car func-entry))
5958 (func-class (nth 1 func-entry))
5959 (func-arrow (nth 2 func-entry))
5960 (func-point (or (nth 3 func-entry) 0))
5961 (func-level (or (nth 4 func-entry) 0))
5962 (pro-entry (idlwave-what-procedure bos))
5963 (pro (car pro-entry))
5964 (pro-class (nth 1 pro-entry))
5965 (pro-arrow (nth 2 pro-entry))
5966 (pro-point (or (nth 3 pro-entry) 0))
5967 (last-char (idlwave-last-valid-char))
5968 (case-fold-search t)
52a244eb 5969 (match-string (buffer-substring bos (point)))
f32b3b91
CD
5970 cw cw-mod cw-arrow cw-class cw-point)
5971 (if (< func-point pro-point) (setq func nil))
5972 (cond
15e42531 5973 ((string-match "\\`[ \t]*\\(pro\\|function\\)[ \t]+[a-zA-Z0-9_]*\\'"
52a244eb 5974 match-string)
15e42531 5975 (setq cw 'class))
52a244eb
S
5976 ((string-match
5977 "\\`[ \t]*\\([a-zA-Z][a-zA-Z0-9$_]*\\)?\\'"
5978 (if (> pro-point 0)
5979 (buffer-substring pro-point (point))
5980 match-string))
f32b3b91
CD
5981 (setq cw 'procedure cw-class pro-class cw-point pro-point
5982 cw-arrow pro-arrow))
5983 ((string-match "\\`[ \t]*\\(pro\\|function\\)\\>"
52a244eb 5984 match-string)
f32b3b91 5985 nil)
05a1abfc 5986 ((string-match "OBJ_NEW([ \t]*['\"]\\([a-zA-Z0-9$_]*\\)?\\'"
52a244eb
S
5987 match-string)
5988 (setq cw 'class))
05a1abfc 5989 ((string-match "\\<inherits\\s-+\\([a-zA-Z0-9$_]*\\)?\\'"
52a244eb
S
5990 match-string)
5991 (setq cw 'class))
5992 ((and func
f32b3b91
CD
5993 (> func-point pro-point)
5994 (= func-level 1)
5995 (memq last-char '(?\( ?,)))
5996 (setq cw 'function-keyword cw-mod func cw-point func-point
5997 cw-class func-class cw-arrow func-arrow))
5998 ((and pro (eq last-char ?,))
5999 (setq cw 'procedure-keyword cw-mod pro cw-point pro-point
6000 cw-class pro-class cw-arrow pro-arrow))
6001; ((member last-char '(?\' ?\) ?\] ?!))
6002; ;; after these chars, a function makes no sense
6003; ;; FIXME: I am sure there can be more in this list
6004; ;; FIXME: Do we want to do this at all?
6005; nil)
6006 ;; Everywhere else we try a function.
6007 (t
6008 (setq cw 'function)
6009 (save-excursion
52a244eb 6010 (if (re-search-backward "->[ \t]*\\(\\$[ \t]*\\(;.*\\)?\n\\s-*\\)?\\(\\([$a-zA-Z0-9_]+\\)::\\)?[$a-zA-Z0-9_]*\\=" bos t)
76959b77 6011 (setq cw-arrow (copy-marker (match-beginning 0))
52a244eb
S
6012 cw-class (if (match-end 4)
6013 (idlwave-sintern-class (match-string 4))
5e72c6b2 6014 t))))))
f32b3b91
CD
6015 (list (list pro pro-point pro-class pro-arrow)
6016 (list func func-point func-class func-arrow)
6017 cw
6018 (list cw-mod cw-point cw-class cw-arrow)
6019 last-char)))
6020
6021(defun idlwave-this-word (&optional class)
6022 ;; Grab the word around point. CLASS is for the `skip-chars=...' functions
52a244eb 6023 (setq class (or class "a-zA-Z0-9$_."))
f32b3b91 6024 (save-excursion
52a244eb 6025 (buffer-substring
f32b3b91
CD
6026 (progn (skip-chars-backward class) (point))
6027 (progn (skip-chars-forward class) (point)))))
6028
f32b3b91
CD
6029(defun idlwave-what-function (&optional bound)
6030 ;; Find out if point is within the argument list of a function.
76959b77
S
6031 ;; The return value is ("function-name" class arrow-start (point) level).
6032 ;; Level is 1 on the top level parentheses, higher further down.
f32b3b91
CD
6033
6034 ;; If the optional BOUND is an integer, bound backwards directed
6035 ;; searches to this point.
6036
6037 (catch 'exit
52a244eb 6038 (let (pos
f32b3b91 6039 func-point
f32b3b91
CD
6040 (cnt 0)
6041 func arrow-start class)
15e42531
CD
6042 (idlwave-with-special-syntax
6043 (save-restriction
6044 (save-excursion
6045 (narrow-to-region (max 1 (or bound 0)) (point-max))
6046 ;; move back out of the current parenthesis
6047 (while (condition-case nil
6048 (progn (up-list -1) t)
6049 (error nil))
6050 (setq pos (point))
6051 (incf cnt)
6052 (when (and (= (following-char) ?\()
52a244eb 6053 (re-search-backward
15e42531
CD
6054 "\\(::\\|\\<\\)\\([a-zA-Z][a-zA-Z0-9$_]*\\)[ \t]*\\="
6055 bound t))
6056 (setq func (match-string 2)
6057 func-point (goto-char (match-beginning 2))
6058 pos func-point)
52a244eb 6059 (if (re-search-backward
15e42531 6060 "->[ \t]*\\(\\([a-zA-Z][a-zA-Z0-9$_]*\\)::\\)?\\=" bound t)
76959b77 6061 (setq arrow-start (copy-marker (match-beginning 0))
15e42531 6062 class (or (match-string 2) t)))
52a244eb
S
6063 (throw
6064 'exit
15e42531
CD
6065 (list
6066 (idlwave-sintern-routine-or-method func class)
6067 (idlwave-sintern-class class)
6068 arrow-start func-point cnt)))
6069 (goto-char pos))
6070 (throw 'exit nil)))))))
f32b3b91
CD
6071
6072(defun idlwave-what-procedure (&optional bound)
6073 ;; Find out if point is within the argument list of a procedure.
6074 ;; The return value is ("procedure-name" class arrow-pos (point)).
6075
6076 ;; If the optional BOUND is an integer, bound backwards directed
6077 ;; searches to this point.
6078 (let ((pos (point)) pro-point
6079 pro class arrow-start string)
52a244eb 6080 (save-excursion
05a1abfc 6081 ;;(idlwave-beginning-of-statement)
15e42531 6082 (idlwave-start-of-substatement 'pre)
f32b3b91 6083 (setq string (buffer-substring (point) pos))
52a244eb 6084 (if (string-match
76959b77
S
6085 "\\`[ \t]*\\([a-zA-Z][a-zA-Z0-9$_]*\\)[ \t]*\\(,\\|\\'\\)" string)
6086 (setq pro (match-string 1 string)
6087 pro-point (+ (point) (match-beginning 1)))
f32b3b91
CD
6088 (if (and (idlwave-skip-object)
6089 (setq string (buffer-substring (point) pos))
52a244eb
S
6090 (string-match
6091 "\\`[ \t]*\\(->\\)[ \t]*\\(\\([a-zA-Z][a-zA-Z0-9$_]*\\)::\\)?\\([a-zA-Z][a-zA-Z0-9$_]*\\)?[ \t]*\\(,\\|\\(\\$\\s *\\(;.*\\)?\\)?$\\)"
6092 string))
f32b3b91
CD
6093 (setq pro (if (match-beginning 4)
6094 (match-string 4 string))
6095 pro-point (if (match-beginning 4)
6096 (+ (point) (match-beginning 4))
6097 pos)
76959b77 6098 arrow-start (copy-marker (+ (point) (match-beginning 1)))
f32b3b91
CD
6099 class (or (match-string 3 string) t)))))
6100 (list (idlwave-sintern-routine-or-method pro class)
6101 (idlwave-sintern-class class)
6102 arrow-start
6103 pro-point)))
6104
6105(defun idlwave-skip-object ()
6106 ;; If there is an object at point, move over it and return t.
6107 (let ((pos (point)))
6108 (if (catch 'exit
6109 (save-excursion
6110 (skip-chars-forward " ") ; white space
6111 (skip-chars-forward "*") ; de-reference
6112 (cond
6113 ((looking-at idlwave-identifier)
6114 (goto-char (match-end 0)))
6115 ((eq (following-char) ?\()
6116 nil)
6117 (t (throw 'exit nil)))
6118 (catch 'endwhile
6119 (while t
6120 (cond ((eq (following-char) ?.)
6121 (forward-char 1)
6122 (if (not (looking-at idlwave-identifier))
6123 (throw 'exit nil))
6124 (goto-char (match-end 0)))
6125 ((memq (following-char) '(?\( ?\[))
6126 (condition-case nil
6127 (forward-list 1)
6128 (error (throw 'exit nil))))
6129 (t (throw 'endwhile t)))))
6130 (if (looking-at "[ \t]*->")
6131 (throw 'exit (setq pos (match-beginning 0)))
6132 (throw 'exit nil))))
6133 (goto-char pos)
6134 nil)))
52a244eb 6135
f32b3b91
CD
6136(defun idlwave-last-valid-char ()
6137 "Return the last character before point which is not white or a comment
6138and also not part of the current identifier. Since we do this in
6139order to identify places where keywords are, we consider the initial
6140`/' of a keyword as part of the identifier.
6141This function is not general, can only be used for completion stuff."
6142 (catch 'exit
6143 (save-excursion
6144 ;; skip the current identifier
6145 (skip-chars-backward "a-zA-Z0-9_$")
6146 ;; also skip a leading slash which might be belong to the keyword
6147 (if (eq (preceding-char) ?/)
6148 (backward-char 1))
6149 ;; FIXME: does not check if this is a valid identifier
6150 (while t
6151 (skip-chars-backward " \t")
6152 (cond
6153 ((memq (preceding-char) '(?\; ?\$)) (throw 'exit nil))
6154 ((eq (preceding-char) ?\n)
6155 (beginning-of-line 0)
5e72c6b2 6156 (if (looking-at "\\([^;\n]*\\)\\$[ \t]*\\(;[^\n]*\\)?\n")
f32b3b91
CD
6157 ;; continuation line
6158 (goto-char (match-end 1))
6159 (throw 'exit nil)))
6160 (t (throw 'exit (preceding-char))))))))
6161
6162(defvar idlwave-complete-after-success-form nil
6163 "A form to evaluate after successful completion.")
6164(defvar idlwave-complete-after-success-form-force nil
6165 "A form to evaluate after completion selection in *Completions* buffer.")
6166(defconst idlwave-completion-mark (make-marker)
6167 "A mark pointing to the beginning of the completion string.")
6168
6169(defun idlwave-complete-in-buffer (type stype list selector prompt isa
52a244eb
S
6170 &optional prepare-display-function
6171 special-selector)
f32b3b91 6172 "Perform TYPE completion of word before point against LIST.
76959b77 6173SELECTOR is the PREDICATE argument for the completion function. Show
52a244eb
S
6174PROMPT in echo area. TYPE is one of the intern types, e.g. 'function,
6175'procedure, 'class-tag, 'keyword, 'sysvar, etc.. SPECIAL-SELECTOR is
6176used only once, for `all-completions', and can be used to, e.g.,
6177accumulate information on matching completions."
f32b3b91
CD
6178 (let* ((completion-ignore-case t)
6179 beg (end (point)) slash part spart completion all-completions
6180 dpart dcompletion)
6181
6182 (unless list
6183 (error (concat prompt ": No completions available")))
6184
6185 ;; What is already in the buffer?
6186 (save-excursion
6187 (skip-chars-backward "a-zA-Z0-9_$")
6188 (setq slash (eq (preceding-char) ?/)
6189 beg (point)
6190 idlwave-complete-after-success-form
6191 (list 'idlwave-after-successful-completion
6192 (list 'quote type) slash beg)
6193 idlwave-complete-after-success-form-force
6194 (list 'idlwave-after-successful-completion
6195 (list 'quote type) slash (list 'quote 'force))))
6196
6197 ;; Try a completion
6198 (setq part (buffer-substring beg end)
6199 dpart (downcase part)
6200 spart (idlwave-sintern stype part)
6201 completion (try-completion part list selector)
52a244eb
S
6202 dcompletion (if (stringp completion) (downcase completion))
6203 idlwave-completion-help-links nil)
f32b3b91
CD
6204 (cond
6205 ((null completion)
6206 ;; nothing available.
76959b77 6207 (error (concat prompt ": no completion for \"%s\"") part))
f32b3b91
CD
6208 ((and (not (equal dpart dcompletion))
6209 (not (eq t completion)))
6210 ;; We can add something
6211 (delete-region beg end)
6212 (if (and (string= part dpart)
6213 (or (not (string= part ""))
6214 idlwave-complete-empty-string-as-lower-case)
6215 (not idlwave-completion-force-default-case))
6216 (insert dcompletion)
6217 (insert completion))
6218 (if (eq t (try-completion completion list selector))
6219 ;; Now this is a unique match
6220 (idlwave-after-successful-completion type slash beg))
6221 t)
6222 ((or (eq completion t)
52a244eb 6223 (and (= 1 (length (setq all-completions
f32b3b91 6224 (idlwave-uniquify
52a244eb
S
6225 (all-completions part list
6226 (or special-selector
6227 selector))))))
6228 (equal dpart dcompletion)))
f32b3b91
CD
6229 ;; This is already complete
6230 (idlwave-after-successful-completion type slash beg)
6231 (message "%s is already the complete %s" part isa)
6232 nil)
52a244eb 6233 (t
f32b3b91
CD
6234 ;; We cannot add something - offer a list.
6235 (message "Making completion list...")
52a244eb
S
6236
6237 (unless idlwave-completion-help-links ; already set somewhere?
6238 (mapcar (lambda (x) ; Pass link prop through to highlight-linked
6239 (let ((link (get-text-property 0 'link (car x))))
6240 (if link
6241 (push (cons (car x) link)
6242 idlwave-completion-help-links))))
6243 list))
f32b3b91 6244 (let* ((list all-completions)
05a1abfc 6245 ;; "complete" means, this is already a valid completion
f32b3b91 6246 (complete (memq spart all-completions))
52a244eb
S
6247 (completion-highlight-first-word-only t)) ; XEmacs
6248; (completion-fixup-function ; Emacs
6249; (lambda () (and (eq (preceding-char) ?>)
6250; (re-search-backward " <" beg t)))))
6251
f32b3b91
CD
6252 (setq list (sort list (lambda (a b)
6253 (string< (downcase a) (downcase b)))))
6254 (if prepare-display-function
6255 (setq list (funcall prepare-display-function list)))
6256 (if (and (string= part dpart)
6257 (or (not (string= part ""))
6258 idlwave-complete-empty-string-as-lower-case)
6259 (not idlwave-completion-force-default-case))
6260 (setq list (mapcar (lambda (x)
52a244eb 6261 (if (listp x)
f32b3b91
CD
6262 (setcar x (downcase (car x)))
6263 (setq x (downcase x)))
6264 x)
6265 list)))
6266 (idlwave-display-completion-list list prompt beg complete))
6267 t))))
6268
6269(defun idlwave-complete-class ()
6270 "Complete a class at point."
6271 (interactive)
6272 ;; Call `idlwave-routines' to make sure the class list will be available
6273 (idlwave-routines)
15e42531
CD
6274 ;; Check for the special case of completing empty string after pro/function
6275 (if (let ((case-fold-search t))
6276 (save-excursion
6277 (and
6278 (re-search-backward "\\<\\(pro\\|function\\)[ \t]+\\="
6279 (- (point) 15) t)
6280 (goto-char (point-min))
52a244eb 6281 (re-search-forward
15e42531
CD
6282 "^[ \t]*\\(pro\\|function\\)[ \t]+\\([a-zA-Z0-9_]+::\\)" nil t))))
6283 ;; Yank the full class specification
6284 (insert (match-string 2))
52a244eb
S
6285 ;; Do the completion, using list gathered from `idlwave-routines'
6286 (idlwave-complete-in-buffer
6287 'class 'class (idlwave-class-alist) nil
6288 "Select a class" "class"
6289 '(lambda (list) ;; Push it to help-links if system help available
6290 (mapcar (lambda (x)
6291 (let* ((entry (idlwave-class-info x))
6292 (link (nth 1 (assq 'link entry))))
6293 (if link (push (cons x link)
6294 idlwave-completion-help-links))
6295 x))
6296 list)))))
f32b3b91 6297
76959b77 6298(defun idlwave-attach-classes (list type show-classes)
05a1abfc 6299 ;; Attach the proper class list to a LIST of completion items.
76959b77
S
6300 ;; TYPE, when 'kwd, shows classes for method keywords, when
6301 ;; 'class-tag, for class tags, and otherwise for methods.
f32b3b91 6302 ;; SHOW-CLASSES is the value of `idlwave-completion-show-classes'.
76959b77
S
6303 (if (or (null show-classes) ; don't want to see classes
6304 (null class-selector) ; not a method call
52a244eb 6305 (and
76959b77
S
6306 (stringp class-selector) ; the class is already known
6307 (not super-classes))) ; no possibilities for inheritance
6308 ;; In these cases, we do not have to do anything
6309 list
05a1abfc
CD
6310 (let* ((do-prop (and (>= show-classes 0)
6311 (>= emacs-major-version 21)))
f32b3b91 6312 (do-buf (not (= show-classes 0)))
76959b77 6313 ;; (do-dots (featurep 'xemacs))
05a1abfc 6314 (do-dots t)
76959b77 6315 (inherit (if (and (not (eq type 'class-tag)) super-classes)
05a1abfc 6316 (cons class-selector super-classes)))
f32b3b91
CD
6317 (max (abs show-classes))
6318 (lmax (if do-dots (apply 'max (mapcar 'length list))))
6319 classes nclasses class-info space)
52a244eb 6320 (mapcar
f32b3b91
CD
6321 (lambda (x)
6322 ;; get the classes
76959b77
S
6323 (if (eq type 'class-tag)
6324 ;; Just one class for tags
6325 (setq classes
52a244eb 6326 (list
76959b77 6327 (idlwave-class-or-superclass-with-tag class-selector x)))
52a244eb 6328 ;; Multiple classes for method or method-keyword
76959b77
S
6329 (setq classes
6330 (if (eq type 'kwd)
6331 (idlwave-all-method-keyword-classes
6332 method-selector x type-selector)
6333 (idlwave-all-method-classes x type-selector)))
6334 (if inherit
52a244eb 6335 (setq classes
76959b77
S
6336 (delq nil
6337 (mapcar (lambda (x) (if (memq x inherit) x nil))
6338 classes)))))
f32b3b91
CD
6339 (setq nclasses (length classes))
6340 ;; Make the separator between item and class-info
6341 (if do-dots
6342 (setq space (concat " " (make-string (- lmax (length x)) ?.)))
6343 (setq space " "))
6344 (if do-buf
6345 ;; We do want info in the buffer
6346 (if (<= nclasses max)
6347 (setq class-info (concat
6348 space
6349 "<" (mapconcat 'identity classes ",") ">"))
6350 (setq class-info (format "%s<%d classes>" space nclasses)))
6351 (setq class-info nil))
6352 (when do-prop
6353 ;; We do want properties
6354 (setq x (copy-sequence x))
6355 (put-text-property 0 (length x)
52a244eb
S
6356 'help-echo (mapconcat 'identity classes " ")
6357 x))
f32b3b91
CD
6358 (if class-info
6359 (list x class-info)
6360 x))
6361 list))))
6362
6363(defun idlwave-attach-method-classes (list)
6364 ;; Call idlwave-attach-classes with method parameters
76959b77 6365 (idlwave-attach-classes list 'method idlwave-completion-show-classes))
f32b3b91
CD
6366(defun idlwave-attach-keyword-classes (list)
6367 ;; Call idlwave-attach-classes with keyword parameters
76959b77
S
6368 (idlwave-attach-classes list 'kwd idlwave-completion-show-classes))
6369(defun idlwave-attach-class-tag-classes (list)
6370 ;; Call idlwave-attach-classes with class structure tags
6371 (idlwave-attach-classes list 'class-tag idlwave-completion-show-classes))
52a244eb 6372
f32b3b91
CD
6373
6374;;----------------------------------------------------------------------
6375;;----------------------------------------------------------------------
6376;;----------------------------------------------------------------------
6377;;----------------------------------------------------------------------
6378;;----------------------------------------------------------------------
5e72c6b2
S
6379(defvar rtn)
6380(defun idlwave-pset (item)
6381 (set 'rtn item))
6382
6383(defun idlwave-popup-select (ev list title &optional sort)
6384 "Select an item in LIST with a popup menu.
6385TITLE is the title to put atop the popup. If SORT is non-nil,
6386sort the list before displaying"
6387 (let ((maxpopup idlwave-max-popup-menu-items)
6388 rtn menu resp)
6389 (cond ((null list))
6390 ((= 1 (length list))
6391 (setq rtn (car list)))
6392 ((featurep 'xemacs)
52a244eb 6393 (if sort (setq list (sort list (lambda (a b)
5e72c6b2
S
6394 (string< (upcase a) (upcase b))))))
6395 (setq menu
6396 (append (list title)
6397 (mapcar (lambda (x) (vector x (list 'idlwave-pset
6398 x)))
6399 list)))
6400 (setq menu (idlwave-split-menu-xemacs menu maxpopup))
6401 (setq resp (get-popup-menu-response menu))
6402 (funcall (event-function resp) (event-object resp)))
6403 (t
52a244eb 6404 (if sort (setq list (sort list (lambda (a b)
5e72c6b2
S
6405 (string< (upcase a) (upcase b))))))
6406 (setq menu (cons title
6407 (list
6408 (append (list "")
6409 (mapcar (lambda(x) (cons x x)) list)))))
6410 (setq menu (idlwave-split-menu-emacs menu maxpopup))
6411 (setq rtn (x-popup-menu ev menu))))
6412 rtn))
6413
6414(defun idlwave-split-menu-xemacs (menu N)
6415 "Split the MENU into submenus of maximum length N."
6416 (if (<= (length menu) (1+ N))
6417 ;; No splitting needed
6418 menu
6419 (let* ((title (car menu))
6420 (entries (cdr menu))
6421 (menu (list title))
6422 (cnt 0)
6423 (nextmenu nil))
6424 (while entries
6425 (while (and entries (< cnt N))
6426 (setq cnt (1+ cnt)
6427 nextmenu (cons (car entries) nextmenu)
6428 entries (cdr entries)))
6429 (setq nextmenu (nreverse nextmenu))
6430 (setq nextmenu (cons (format "%s...%s"
6431 (aref (car nextmenu) 0)
6432 (aref (nth (1- cnt) nextmenu) 0))
6433 nextmenu))
6434 (setq menu (cons nextmenu menu)
6435 nextmenu nil
6436 cnt 0))
6437 (nreverse menu))))
6438
6439(defun idlwave-split-menu-emacs (menu N)
6440 "Split the MENU into submenus of maximum length N."
6441 (if (<= (length (nth 1 menu)) (1+ N))
6442 ;; No splitting needed
6443 menu
6444 (let* ((title (car menu))
6445 (entries (cdr (nth 1 menu)))
6446 (menu nil)
6447 (cnt 0)
6448 (nextmenu nil))
6449 (while entries
6450 (while (and entries (< cnt N))
6451 (setq cnt (1+ cnt)
6452 nextmenu (cons (car entries) nextmenu)
6453 entries (cdr entries)))
6454 (setq nextmenu (nreverse nextmenu))
6455 (prin1 nextmenu)
6456 (setq nextmenu (cons (format "%s...%s"
6457 (car (car nextmenu))
6458 (car (nth (1- cnt) nextmenu)))
6459 nextmenu))
6460 (setq menu (cons nextmenu menu)
6461 nextmenu nil
6462 cnt 0))
6463 (setq menu (nreverse menu))
6464 (setq menu (cons title menu))
6465 menu)))
f32b3b91 6466
15e42531
CD
6467(defvar idlwave-completion-setup-hook nil)
6468
f32b3b91
CD
6469(defun idlwave-scroll-completions (&optional message)
6470 "Scroll the completion window on this frame."
6471 (let ((cwin (get-buffer-window "*Completions*" 'visible))
6472 (win (selected-window)))
6473 (unwind-protect
6474 (progn
6475 (select-window cwin)
6476 (condition-case nil
6477 (scroll-up)
6478 (error (if (and (listp last-command)
6479 (nth 2 last-command))
6480 (progn
6481 (select-window win)
6482 (eval idlwave-complete-after-success-form))
6483 (set-window-start cwin (point-min)))))
6484 (and message (message message)))
6485 (select-window win))))
6486
6487(defun idlwave-display-completion-list (list &optional message beg complete)
6488 "Display the completions in LIST in the completions buffer and echo MESSAGE."
6489 (unless (and (get-buffer-window "*Completions*")
6490 (idlwave-local-value 'idlwave-completion-p "*Completions*"))
6491 (move-marker idlwave-completion-mark beg)
6492 (setq idlwave-before-completion-wconf (current-window-configuration)))
6493
6494 (if (featurep 'xemacs)
52a244eb 6495 (idlwave-display-completion-list-xemacs
15e42531 6496 list)
f32b3b91
CD
6497 (idlwave-display-completion-list-emacs list))
6498
6499 ;; Store a special value in `this-command'. When `idlwave-complete'
6500 ;; finds this in `last-command', it will scroll the *Completions* buffer.
6501 (setq this-command (list 'idlwave-display-completion-list message complete))
6502
6503 ;; Mark the completions buffer as created by cib
6504 (idlwave-set-local 'idlwave-completion-p t "*Completions*")
6505
6506 ;; Fontify the classes
6507 (if (and idlwave-completion-fontify-classes
6508 (consp (car list)))
6509 (idlwave-completion-fontify-classes))
6510
15e42531
CD
6511 ;; Run the hook
6512 (run-hooks 'idlwave-completion-setup-hook)
6513
f32b3b91
CD
6514 ;; Display the message
6515 (message (or message "Making completion list...done")))
6516
6517(defun idlwave-choose (function &rest args)
6518 "Call FUNCTION as a completion chooser and pass ARGS to it."
6519 (let ((completion-ignore-case t)) ; install correct value
6520 (apply function args))
15e42531
CD
6521 (if (and (eq major-mode 'idlwave-shell-mode)
6522 (boundp 'font-lock-mode)
6523 (not font-lock-mode))
52a244eb 6524 ;; For the shell, remove the fontification of the word before point
15e42531
CD
6525 (let ((beg (save-excursion
6526 (skip-chars-backward "a-zA-Z0-9_")
6527 (point))))
6528 (remove-text-properties beg (point) '(face nil))))
f32b3b91
CD
6529 (eval idlwave-complete-after-success-form-force))
6530
76959b77
S
6531(defun idlwave-keyboard-quit ()
6532 (interactive)
6533 (unwind-protect
6534 (if (eq (car-safe last-command) 'idlwave-display-completion-list)
6535 (idlwave-restore-wconf-after-completion))
6536 (keyboard-quit)))
6537
f32b3b91
CD
6538(defun idlwave-restore-wconf-after-completion ()
6539 "Restore the old (before completion) window configuration."
6540 (and idlwave-completion-restore-window-configuration
6541 idlwave-before-completion-wconf
6542 (set-window-configuration idlwave-before-completion-wconf)))
6543
52a244eb
S
6544(defun idlwave-one-key-select (sym prompt delay)
6545 "Make the user select an element from the alist in the variable SYM.
6546The keys of the alist are expected to be strings. The function returns the
6547car of the selected association.
6548To do this, PROMPT is displayed and and the user must hit a letter key to
6549select an entry. If the user does not reply within DELAY seconds, a help
6550window with the options is displayed automatically.
6551The key which is associated with each option is generated automatically.
6552First, the strings are checked for preselected keys, like in \"[P]rint\".
6553If these don't exist, a letter in the string is automatically selected."
6554 (let* ((alist (symbol-value sym))
6555 (temp-buffer-show-hook (if (fboundp 'fit-window-to-buffer)
6556 '(fit-window-to-buffer)))
6557 keys-alist char)
6558 ;; First check the cache
6559 (if (and (eq (symbol-value sym) (get sym :one-key-alist-last)))
6560 (setq keys-alist (get sym :one-key-alist-cache))
6561 ;; Need to make new list
6562 (setq keys-alist (idlwave-make-one-key-alist alist))
6563 (put sym :one-key-alist-cache keys-alist)
6564 (put sym :one-key-alist-last alist))
6565 ;; Display prompt and wait for quick reply
6566 (message "%s[%s]" prompt
6567 (mapconcat (lambda(x) (char-to-string (car x)))
6568 keys-alist ""))
6569 (if (sit-for delay)
6570 ;; No quick reply: Show help
6571 (save-window-excursion
6572 (with-output-to-temp-buffer "*Completions*"
6573 (mapcar (lambda(x)
6574 (princ (nth 1 x))
6575 (princ "\n"))
6576 keys-alist))
6577 (setq char (read-char)))
6578 (setq char (read-char)))
6579 (message nil)
6580 ;; Return the selected result
6581 (nth 2 (assoc char keys-alist))))
6582
6583;; Used for, e.g., electric debug super-examine.
6584(defun idlwave-make-one-key-alist (alist)
6585 "Make an alist for single key selection."
6586 (let ((l alist) keys-alist name start char help
6587 (cnt 0)
6588 (case-fold-search nil))
6589 (while l
6590 (setq name (car (car l))
6591 l (cdr l))
6592 (catch 'exit
6593 ;; First check if the configuration predetermined a key
6594 (if (string-match "\\[\\(.\\)\\]" name)
6595 (progn
6596 (setq char (string-to-char (downcase (match-string 1 name)))
6597 help (format "%c: %s" char name)
6598 keys-alist (cons (list char help name) keys-alist))
6599 (throw 'exit t)))
6600 ;; Then check for capital letters
6601 (setq start 0)
6602 (while (string-match "[A-Z]" name start)
6603 (setq start (match-end 0)
6604 char (string-to-char (downcase (match-string 0 name))))
6605 (if (not (assoc char keys-alist))
6606 (progn
6607 (setq help (format "%c: %s" char
6608 (replace-match
6609 (concat "[" (match-string 0 name) "]")
6610 t t name))
6611 keys-alist (cons (list char help name) keys-alist))
6612 (throw 'exit t))))
6613 ;; Now check for lowercase letters
6614 (setq start 0)
6615 (while (string-match "[a-z]" name start)
6616 (setq start (match-end 0)
6617 char (string-to-char (match-string 0 name)))
6618 (if (not (assoc char keys-alist))
6619 (progn
6620 (setq help (format "%c: %s" char
6621 (replace-match
6622 (concat "[" (match-string 0 name) "]")
6623 t t name))
6624 keys-alist (cons (list char help name) keys-alist))
6625 (throw 'exit t))))
6626 ;; Bummer, nothing found! Use a stupid number
6627 (setq char (string-to-char (int-to-string (setq cnt (1+ cnt))))
6628 help (format "%c: %s" char name)
6629 keys-alist (cons (list char help name) keys-alist))))
6630 (nreverse keys-alist)))
6631
f32b3b91
CD
6632(defun idlwave-set-local (var value &optional buffer)
6633 "Set the buffer-local value of VAR in BUFFER to VALUE."
6634 (save-excursion
6635 (set-buffer (or buffer (current-buffer)))
6636 (set (make-local-variable var) value)))
6637
6638(defun idlwave-local-value (var &optional buffer)
6639 "Return the value of VAR in BUFFER, but only if VAR is local to BUFFER."
6640 (save-excursion
6641 (set-buffer (or buffer (current-buffer)))
6642 (and (local-variable-p var (current-buffer))
6643 (symbol-value var))))
6644
15e42531
CD
6645;; In XEmacs, we can use :activate-callback directly to advice the
6646;; choose functions. We use the private keymap only for the online
6647;; help feature.
f32b3b91 6648
15e42531
CD
6649(defvar idlwave-completion-map nil
6650 "Keymap for completion-list-mode with idlwave-complete.")
6651
6652(defun idlwave-display-completion-list-xemacs (list &rest cl-args)
f32b3b91 6653 (with-output-to-temp-buffer "*Completions*"
15e42531
CD
6654 (apply 'display-completion-list list
6655 ':activate-callback 'idlwave-default-choose-completion
6656 cl-args))
6657 (save-excursion
6658 (set-buffer "*Completions*")
6659 (use-local-map
6660 (or idlwave-completion-map
6661 (setq idlwave-completion-map
6662 (idlwave-make-modified-completion-map-xemacs
6663 (current-local-map)))))))
f32b3b91
CD
6664
6665(defun idlwave-default-choose-completion (&rest args)
6666 "Execute `default-choose-completion' and then restore the win-conf."
6667 (apply 'idlwave-choose 'default-choose-completion args))
6668
15e42531
CD
6669(defun idlwave-make-modified-completion-map-xemacs (old-map)
6670 "Replace `choose-completion' and `mouse-choose-completion' in OLD-MAP."
6671 (let ((new-map (copy-keymap old-map)))
6672 (define-key new-map [button3up] 'idlwave-mouse-completion-help)
6673 (define-key new-map [button3] (lambda ()
6674 (interactive)
6675 (setq this-command last-command)))
6676 new-map))
f32b3b91 6677
76959b77 6678;; In Emacs we also replace keybindings in the completion
15e42531 6679;; map in order to install our wrappers.
f32b3b91
CD
6680
6681(defun idlwave-display-completion-list-emacs (list)
6682 "Display completion list and install the choose wrappers."
6683 (with-output-to-temp-buffer "*Completions*"
6684 (display-completion-list list))
6685 (save-excursion
6686 (set-buffer "*Completions*")
6687 (use-local-map
6688 (or idlwave-completion-map
6689 (setq idlwave-completion-map
15e42531
CD
6690 (idlwave-make-modified-completion-map-emacs
6691 (current-local-map)))))))
6692
6693(defun idlwave-make-modified-completion-map-emacs (old-map)
f32b3b91
CD
6694 "Replace `choose-completion' and `mouse-choose-completion' in OLD-MAP."
6695 (let ((new-map (copy-keymap old-map)))
52a244eb 6696 (substitute-key-definition
f32b3b91
CD
6697 'choose-completion 'idlwave-choose-completion new-map)
6698 (substitute-key-definition
6699 'mouse-choose-completion 'idlwave-mouse-choose-completion new-map)
15e42531 6700 (define-key new-map [mouse-3] 'idlwave-mouse-completion-help)
f32b3b91
CD
6701 new-map))
6702
6703(defun idlwave-choose-completion (&rest args)
6704 "Choose the completion that point is in or next to."
6705 (interactive)
6706 (apply 'idlwave-choose 'choose-completion args))
6707
6708(defun idlwave-mouse-choose-completion (&rest args)
6709 "Click on an alternative in the `*Completions*' buffer to choose it."
6710 (interactive "e")
6711 (apply 'idlwave-choose 'mouse-choose-completion args))
6712
6713;;----------------------------------------------------------------------
6714;;----------------------------------------------------------------------
6715
05a1abfc 6716;;; ------------------------------------------------------------------------
52a244eb 6717;;; Stucture parsing code, and code to manage class info
05a1abfc
CD
6718
6719;;
6720;; - Go again over the documentation how to write a completion
6721;; plugin. It is in self.el, but currently still very bad.
52a244eb
S
6722;; This could be in a separate file in the distribution, or
6723;; in an appendix for the manual.
6724
6725(defvar idlwave-struct-skip
6726 "[ \t]*\\(\\$.*\n\\(^[ \t]*\\(\\$[ \t]*\\)?\\(;.*\\)?\n\\)*\\)?[ \t]*"
6727 "Regexp for skipping continued blank or comment-only lines in
6728structures")
6729
6730(defvar idlwave-struct-tag-regexp
6731 (concat "[{,]" ;leading comma/brace
6732 idlwave-struct-skip ; 4 groups
6733 "\\([a-zA-Z][a-zA-Z0-9_]*\\)" ;the tag itself, group 5
6734 "[ \t]*:") ; the final colon
6735 "Regexp for structure tags.")
05a1abfc
CD
6736
6737(defun idlwave-struct-tags ()
6738 "Return a list of all tags in the structure defined at point.
6739Point is expected just before the opening `{' of the struct definition."
6740 (save-excursion
6741 (let* ((borders (idlwave-struct-borders))
6742 (beg (car borders))
6743 (end (cdr borders))
6744 tags)
6745 (goto-char beg)
52a244eb
S
6746 (save-restriction
6747 (narrow-to-region beg end)
6748 (while (re-search-forward idlwave-struct-tag-regexp end t)
6749 ;; Check if we are still on the top level of the structure.
6750 (if (and (condition-case nil (progn (up-list -1) t) (error nil))
6751 (= (point) beg))
6752 (push (match-string-no-properties 5) tags))
6753 (goto-char (match-end 0))))
6754 (nreverse tags))))
05a1abfc 6755
76959b77
S
6756(defun idlwave-find-struct-tag (tag)
6757 "Find a given TAG in the structure defined at point."
6758 (let* ((borders (idlwave-struct-borders))
6759 (beg (car borders))
6760 (end (cdr borders))
6761 (case-fold-search t))
52a244eb 6762 (re-search-forward (concat "\\(^[ \t]*\\|[,{][ \t]*\\)" tag "[ \t]*:")
76959b77
S
6763 end t)))
6764
05a1abfc
CD
6765(defun idlwave-struct-inherits ()
6766 "Return a list of all `inherits' names in the struct at point.
6767Point is expected just before the opening `{' of the struct definition."
6768 (save-excursion
6769 (let* ((borders (idlwave-struct-borders))
6770 (beg (car borders))
6771 (end (cdr borders))
6772 (case-fold-search t)
6773 names)
6774 (goto-char beg)
52a244eb
S
6775 (save-restriction
6776 (narrow-to-region beg end)
6777 (while (re-search-forward
6778 (concat "[{,]" ;leading comma/brace
6779 idlwave-struct-skip ; 4 groups
6780 "inherits" ; The INHERITS tag
6781 idlwave-struct-skip ; 4 more
6782 "\\([a-zA-Z][a-zA-Z0-9_]*\\)") ; The super-group, #9
6783 end t)
6784 ;; Check if we are still on the top level of the structure.
6785 (if (and (condition-case nil (progn (up-list -1) t) (error nil))
6786 (= (point) beg))
6787 (push (match-string-no-properties 9) names))
6788 (goto-char (match-end 0))))
05a1abfc
CD
6789 (nreverse names))))
6790
5e72c6b2 6791(defun idlwave-in-structure ()
52a244eb 6792 "Return t if point is inside an IDL structure definition."
5e72c6b2
S
6793 (let ((beg (point)))
6794 (save-excursion
6795 (if (not (or (idlwave-in-comment) (idlwave-in-quote)))
6796 (if (idlwave-find-structure-definition nil nil 'back)
6797 (let ((borders (idlwave-struct-borders)))
6798 (or (= (car borders) (cdr borders)) ;; struct not yet closed...
6799 (and (> beg (car borders)) (< beg (cdr borders))))))))))
05a1abfc
CD
6800
6801(defun idlwave-struct-borders ()
6802 "Return the borders of the {...} after point as a cons cell."
6803 (let (beg)
6804 (save-excursion
6805 (skip-chars-forward "^{")
6806 (setq beg (point))
6807 (condition-case nil (forward-list 1)
6808 (error (goto-char beg)))
6809 (cons beg (point)))))
6810
6811(defun idlwave-find-structure-definition (&optional var name bound)
52a244eb
S
6812 "Search forward for a structure definition. If VAR is non-nil,
6813search for a structure assigned to variable VAR. If NAME is non-nil,
6814search for a named structure NAME, if a string, or a generic named
6815structure otherwise. If BOUND is an integer, limit the search. If
6816BOUND is the symbol `all', we search first back and then forward
6817through the entire file. If BOUND is the symbol `back' we search only
6818backward."
76959b77 6819 (let* ((ws "[ \t]*\\(\\$.*\n[ \t]*\\)*")
05a1abfc
CD
6820 (case-fold-search t)
6821 (lim (if (integerp bound) bound nil))
6822 (re (concat
6823 (if var
6824 (concat "\\<" (regexp-quote (downcase var)) "\\>" ws)
6825 "\\(\\)")
6826 "=" ws "\\({\\)"
52a244eb
S
6827 (if name
6828 (if (stringp name)
6829 (concat ws "\\(\\<" (downcase name) "\\)[^a-zA-Z0-9_$]")
6830 ;; Just a generic name
6831 (concat ws "\\<\\([a-zA-Z_0-9$]+\\)" ws ","))
6832 ""))))
5e72c6b2 6833 (if (or (and (or (eq bound 'all) (eq bound 'back))
05a1abfc 6834 (re-search-backward re nil t))
5e72c6b2 6835 (and (not (eq bound 'back)) (re-search-forward re lim t)))
52a244eb
S
6836 (progn
6837 (goto-char (match-beginning 3))
6838 (match-string-no-properties 5)))))
6839
6840(defvar idlwave-class-info nil)
6841(defvar idlwave-system-class-info nil) ; Gathered from idlw-rinfo
6842(defvar idlwave-class-reset nil) ; to reset buffer-local classes
05a1abfc 6843
05a1abfc 6844(add-hook 'idlwave-update-rinfo-hook
52a244eb 6845 (lambda () (setq idlwave-class-reset t)))
05a1abfc
CD
6846(add-hook 'idlwave-after-load-rinfo-hook
6847 (lambda () (setq idlwave-class-info nil)))
6848
6849(defun idlwave-class-info (class)
6850 (let (list entry)
52a244eb
S
6851 (if idlwave-class-info
6852 (if idlwave-class-reset
6853 (setq
6854 idlwave-class-reset nil
6855 idlwave-class-info ; Remove any visited in a buffer
6856 (delq nil (mapcar
6857 (lambda (x)
6858 (let ((filebuf
6859 (idlwave-class-file-or-buffer
6860 (or (cdr (assq 'found-in x)) (car x)))))
6861 (if (cdr filebuf)
6862 nil
6863 x)))
6864 idlwave-class-info))))
6865 ;; Info is nil, put in the system stuff to start.
05a1abfc
CD
6866 (setq idlwave-class-info idlwave-system-class-info)
6867 (setq list idlwave-class-info)
6868 (while (setq entry (pop list))
6869 (idlwave-sintern-class-info entry)))
6870 (setq class (idlwave-sintern-class class))
52a244eb
S
6871 (or (assq class idlwave-class-info)
6872 (progn (idlwave-scan-class-info class)
6873 (assq class idlwave-class-info)))))
05a1abfc
CD
6874
6875(defun idlwave-sintern-class-info (entry)
6876 "Sintern the class names in a class-info entry."
6877 (let ((taglist (assq 'tags entry))
6878 (inherits (assq 'inherits entry)))
6879 (setcar entry (idlwave-sintern-class (car entry) 'set))
6880 (if inherits
6881 (setcdr inherits (mapcar (lambda (x) (idlwave-sintern-class x 'set))
6882 (cdr inherits))))))
6883
52a244eb
S
6884(defun idlwave-find-class-definition (class &optional all-hook alt-class)
6885 "Find class structure definition(s)
6886If ALL-HOOK is set, find all named structure definitions in a given
6887class__define routine, on which ALL-HOOK will be run. If ALT-CLASS is
6888set, look for the name__define pro, and inside of it, for the ALT-CLASS
6889class/struct definition"
6890 (let ((case-fold-search t) end-lim list name)
6891 (when (re-search-forward
6892 (concat "^[ \t]*pro[ \t]+" (downcase class) "__define" "\\>") nil t)
6893 (if all-hook
6894 (progn
6895 ;; For everything there
6896 (setq end-lim (save-excursion (idlwave-end-of-subprogram) (point)))
6897 (while (setq name
6898 (idlwave-find-structure-definition nil t end-lim))
6899 (funcall all-hook name)))
6900 (idlwave-find-structure-definition nil (or alt-class class))))))
76959b77 6901
52a244eb
S
6902
6903(defun idlwave-class-file-or-buffer (class)
6904 "Find buffer visiting CLASS definition"
05a1abfc 6905 (let* ((pro (concat (downcase class) "__define"))
52a244eb
S
6906 (file (idlwave-routine-source-file
6907 (nth 3 (idlwave-rinfo-assoc pro 'pro nil
6908 (idlwave-routines))))))
6909 (cons file (if file (idlwave-get-buffer-visiting file)))))
6910
6911
6912(defun idlwave-scan-class-info (class)
6913 "Scan all class and named structure info in the class__define pro"
6914 (let* ((idlwave-auto-routine-info-updates nil)
6915 (filebuf (idlwave-class-file-or-buffer class))
6916 (file (car filebuf))
6917 (buf (cdr filebuf))
6918 (class (idlwave-sintern-class class)))
6919 (if (or
6920 (not file)
6921 (and ;; neither a regular file nor a visited buffer
6922 (not buf)
6923 (not (file-regular-p file))))
6924 nil ; Cannot find the file/buffer to get any info
05a1abfc 6925 (save-excursion
52a244eb
S
6926 (if buf (set-buffer buf)
6927 ;; Read the file in temporarily
05a1abfc
CD
6928 (set-buffer (get-buffer-create " *IDLWAVE-tmp*"))
6929 (erase-buffer)
6930 (unless (eq major-mode 'idlwave-mode)
6931 (idlwave-mode))
6932 (insert-file-contents file))
6933 (save-excursion
6934 (goto-char 1)
52a244eb
S
6935 (idlwave-find-class-definition class
6936 ;; Scan all of the structures found there
6937 (lambda (name)
6938 (let* ((this-class (idlwave-sintern-class name))
6939 (entry
6940 (list this-class
6941 (cons 'tags (idlwave-struct-tags))
6942 (cons 'inherits (idlwave-struct-inherits)))))
6943 (if (not (eq this-class class))
6944 (setq entry (nconc entry (list (cons 'found-in class)))))
6945 (idlwave-sintern-class-info entry)
6946 (push entry idlwave-class-info)))))))))
6947
6948(defun idlwave-class-found-in (class)
6949 "Return the FOUND-IN property of the class."
6950 (cdr (assq 'found-in (idlwave-class-info class))))
05a1abfc
CD
6951(defun idlwave-class-tags (class)
6952 "Return the native tags in CLASS."
6953 (cdr (assq 'tags (idlwave-class-info class))))
6954(defun idlwave-class-inherits (class)
6955 "Return the direct superclasses of CLASS."
6956 (cdr (assq 'inherits (idlwave-class-info class))))
6957
52a244eb 6958
05a1abfc
CD
6959(defun idlwave-all-class-tags (class)
6960 "Return a list of native and inherited tags in CLASS."
76959b77
S
6961 (condition-case err
6962 (apply 'append (mapcar 'idlwave-class-tags
6963 (cons class (idlwave-all-class-inherits class))))
52a244eb 6964 (error
76959b77
S
6965 (idlwave-class-tag-reset)
6966 (error "%s" (error-message-string err)))))
6967
05a1abfc
CD
6968
6969(defun idlwave-all-class-inherits (class)
6970 "Return a list of all superclasses of CLASS (recursively expanded).
5e72c6b2 6971The list is cached in `idlwave-class-info' for faster access."
05a1abfc
CD
6972 (cond
6973 ((not idlwave-support-inheritance) nil)
6974 ((eq class nil) nil)
6975 ((eq class t) nil)
6976 (t
6977 (let ((info (idlwave-class-info class))
6978 entry)
6979 (if (setq entry (assq 'all-inherits info))
6980 (cdr entry)
76959b77
S
6981 ;; Save the depth of inheritance scan to check for circular references
6982 (let ((inherits (mapcar (lambda (x) (cons x 0))
6983 (idlwave-class-inherits class)))
05a1abfc
CD
6984 rtn all-inherits cl)
6985 (while inherits
6986 (setq cl (pop inherits)
76959b77
S
6987 rtn (cons (car cl) rtn)
6988 inherits (append (mapcar (lambda (x)
6989 (cons x (1+ (cdr cl))))
6990 (idlwave-class-inherits (car cl)))
6991 inherits))
6992 (if (> (cdr cl) 999)
6993 (error
6994 "Class scan: inheritance depth exceeded. Circular inheritance?")
6995 ))
05a1abfc
CD
6996 (setq all-inherits (nreverse rtn))
6997 (nconc info (list (cons 'all-inherits all-inherits)))
6998 all-inherits))))))
6999
52a244eb
S
7000(defun idlwave-entry-keywords (entry &optional record-link)
7001 "Return the flat entry keywords alist from routine-info entry.
7002If RECORD-LINK is non-nil, the keyword text is copied and a text
7003property indicating the link is added."
7004 (let (kwds)
7005 (mapcar
7006 (lambda (key-list)
7007 (let ((file (car key-list)))
7008 (mapcar (lambda (key-cons)
7009 (let ((key (car key-cons))
7010 (link (cdr key-cons)))
7011 (when (and record-link file)
7012 (setq key (copy-sequence key))
7013 (put-text-property
7014 0 (length key)
7015 'link
7016 (concat
7017 file
7018 (if link
7019 (concat idlwave-html-link-sep
7020 (number-to-string link))))
7021 key))
7022 (push (list key) kwds)))
7023 (cdr key-list))))
7024 (nthcdr 5 entry))
7025 (nreverse kwds)))
7026
7027(defun idlwave-entry-find-keyword (entry keyword)
7028 "Find keyword KEYWORD in entry ENTRY, and return (with link) if set"
7029 (catch 'exit
7030 (mapc
7031 (lambda (key-list)
7032 (let ((file (car key-list))
7033 (kwd (assoc keyword (cdr key-list))))
7034 (when kwd
7035 (setq kwd (cons (car kwd)
7036 (if (and file (cdr kwd))
7037 (concat file
7038 idlwave-html-link-sep
7039 (number-to-string (cdr kwd)))
7040 (cdr kwd))))
7041 (throw 'exit kwd))))
7042 (nthcdr 5 entry))))
05a1abfc
CD
7043
7044;;==========================================================================
7045;;
7046;; Completing class structure tags. This is a completion plugin.
7047;; The necessary taglist is constructed dynamically
7048
7049(defvar idlwave-current-tags-class nil)
7050(defvar idlwave-current-class-tags nil)
7051(defvar idlwave-current-native-class-tags nil)
76959b77
S
7052(defvar idlwave-sint-class-tags nil)
7053(idlwave-new-sintern-type 'class-tag)
05a1abfc 7054(add-to-list 'idlwave-complete-special 'idlwave-complete-class-structure-tag)
76959b77 7055(add-hook 'idlwave-update-rinfo-hook 'idlwave-class-tag-reset)
05a1abfc
CD
7056
7057(defun idlwave-complete-class-structure-tag ()
7058 "Complete a structure tag on a `self' argument in an object method."
7059 (interactive)
7060 (let ((pos (point))
7061 (case-fold-search t))
7062 (if (save-excursion
7063 ;; Check if the context is right
52a244eb 7064 (skip-chars-backward "a-zA-Z0-9._$")
05a1abfc
CD
7065 (and (< (point) (- pos 4))
7066 (looking-at "self\\.")))
76959b77
S
7067 (let* ((class-selector (nth 2 (idlwave-current-routine)))
7068 (super-classes (idlwave-all-class-inherits class-selector)))
05a1abfc 7069 ;; Check if we are in a class routine
76959b77 7070 (unless class-selector
e8af40ee 7071 (error "Not in a method procedure or function"))
05a1abfc 7072 ;; Check if we need to update the "current" class
76959b77
S
7073 (if (not (equal class-selector idlwave-current-tags-class))
7074 (idlwave-prepare-class-tag-completion class-selector))
52a244eb 7075 (setq idlwave-completion-help-info
76959b77 7076 (list 'idlwave-complete-class-structure-tag-help
52a244eb 7077 (idlwave-sintern-routine
76959b77
S
7078 (concat class-selector "__define"))
7079 nil))
05a1abfc
CD
7080 (let ((idlwave-cpl-bold idlwave-current-native-class-tags))
7081 (idlwave-complete-in-buffer
52a244eb 7082 'class-tag 'class-tag
05a1abfc 7083 idlwave-current-class-tags nil
76959b77
S
7084 (format "Select a tag of class %s" class-selector)
7085 "class tag"
7086 'idlwave-attach-class-tag-classes))
05a1abfc
CD
7087 t) ; return t to skip other completions
7088 nil)))
7089
76959b77 7090(defun idlwave-class-tag-reset ()
05a1abfc
CD
7091 (setq idlwave-current-tags-class nil))
7092
7093(defun idlwave-prepare-class-tag-completion (class)
7094 "Find and parse the necessary class definitions for class structure tags."
76959b77 7095 (setq idlwave-sint-class-tags nil)
05a1abfc
CD
7096 (setq idlwave-current-tags-class class)
7097 (setq idlwave-current-class-tags
7098 (mapcar (lambda (x)
76959b77 7099 (list (idlwave-sintern-class-tag x 'set)))
05a1abfc
CD
7100 (idlwave-all-class-tags class)))
7101 (setq idlwave-current-native-class-tags
7102 (mapcar 'downcase (idlwave-class-tags class))))
7103
7104;===========================================================================
7105;;
7106;; Completing system variables and their structure fields
52a244eb 7107;; This is also a plugin.
05a1abfc
CD
7108
7109(defvar idlwave-sint-sysvars nil)
7110(defvar idlwave-sint-sysvartags nil)
7111(idlwave-new-sintern-type 'sysvar)
7112(idlwave-new-sintern-type 'sysvartag)
7113(add-to-list 'idlwave-complete-special 'idlwave-complete-sysvar-or-tag)
7114(add-hook 'idlwave-update-rinfo-hook 'idlwave-sysvars-reset)
05a1abfc
CD
7115(add-hook 'idlwave-after-load-rinfo-hook 'idlwave-sintern-sysvar-alist)
7116
52a244eb
S
7117(defvar idlwave-executive-commands-alist nil
7118 "Alist of system variables and their help files.")
7119
05a1abfc
CD
7120(defvar idlwave-system-variables-alist nil
7121 "Alist of system variables and the associated structure tags.
7122Gets set in `idlw-rinfo.el'.")
05a1abfc
CD
7123
7124(defun idlwave-complete-sysvar-or-tag ()
7125 "Complete a system variable."
7126 (interactive)
7127 (let ((pos (point))
7128 (case-fold-search t))
7129 (cond ((save-excursion
7130 ;; Check if the context is right for system variable
7131 (skip-chars-backward "[a-zA-Z0-9_$]")
7132 (equal (char-before) ?!))
7133 (setq idlwave-completion-help-info '(idlwave-complete-sysvar-help))
52a244eb 7134 (idlwave-complete-in-buffer 'sysvar 'sysvar
05a1abfc
CD
7135 idlwave-system-variables-alist nil
7136 "Select a system variable"
7137 "system variable")
7138 t) ; return t to skip other completions
7139 ((save-excursion
7140 ;; Check if the context is right for sysvar tag
52a244eb 7141 (skip-chars-backward "a-zA-Z0-9_$.")
05a1abfc
CD
7142 (and (equal (char-before) ?!)
7143 (looking-at "\\([a-zA-Z][a-zA-Z0-9_$]*\\)\\.")
7144 (<= (match-end 0) pos)))
7145 ;; Complete a system variable tag
7146 (let* ((var (idlwave-sintern-sysvar (match-string 1)))
7147 (entry (assq var idlwave-system-variables-alist))
52a244eb
S
7148 (tags (cdr (assq 'tags entry))))
7149 (or entry (error "!%s is not a known system variable" var))
05a1abfc
CD
7150 (or tags (error "System variable !%s is not a structure" var))
7151 (setq idlwave-completion-help-info
52a244eb
S
7152 (list 'idlwave-complete-sysvar-tag-help var))
7153 (idlwave-complete-in-buffer 'sysvartag 'sysvartag
05a1abfc
CD
7154 tags nil
7155 "Select a system variable tag"
7156 "system variable tag")
7157 t)) ; return t to skip other completions
7158 (t nil))))
7159
05a1abfc 7160(defun idlwave-complete-sysvar-help (mode word)
52a244eb
S
7161 (let ((word (or (nth 1 idlwave-completion-help-info) word))
7162 (entry (assoc word idlwave-system-variables-alist)))
7163 (cond
7164 ((eq mode 'test)
7165 (and (stringp word) entry (nth 1 (assq 'link entry))))
7166 ((eq mode 'set)
7167 (if entry (setq link (nth 1 (assq 'link entry))))) ;; setting dynamic!!!
7168 (t (error "This should not happen")))))
7169
7170(defun idlwave-complete-sysvar-tag-help (mode word)
7171 (let* ((var (nth 1 idlwave-completion-help-info))
7172 (entry (assoc var idlwave-system-variables-alist))
7173 (tags (cdr (assq 'tags entry)))
7174 (main (nth 1 (assq 'link entry)))
7175 target main-base)
7176 (cond
7177 ((eq mode 'test) ; we can at least link the main
7178 (and (stringp word) entry main))
7179 ((eq mode 'set)
7180 (if entry
7181 (setq link
7182 (if (setq target (cdr (assoc word tags)))
7183 (idlwave-substitute-link-target main target)
7184 main)))) ;; setting dynamic!!!
7185 (t (error "This should not happen")))))
7186
7187(defun idlwave-substitute-link-target (link target)
7188 "Substitute the target anchor for the given link."
7189 (let (main-base)
7190 (setq main-base (if (string-match "#" link)
7191 (substring link 0 (match-beginning 0))
7192 link))
7193 (if target
7194 (concat main-base idlwave-html-link-sep (number-to-string target))
7195 link)))
76959b77
S
7196
7197;; Fake help in the source buffer for class structure tags.
52a244eb
S
7198;; KWD AND NAME ARE GLOBAL-VARIABLES HERE.
7199(defvar name)
7200(defvar kwd)
76959b77
S
7201(defvar idlwave-help-do-class-struct-tag nil)
7202(defun idlwave-complete-class-structure-tag-help (mode word)
7203 (cond
7204 ((eq mode 'test) ; nothing gets fontified for class tags
7205 nil)
7206 ((eq mode 'set)
52a244eb
S
7207 (let (class-with found-in)
7208 (when (setq class-with
7209 (idlwave-class-or-superclass-with-tag
76959b77
S
7210 idlwave-current-tags-class
7211 word))
52a244eb 7212 (if (assq (idlwave-sintern-class class-with)
76959b77
S
7213 idlwave-system-class-info)
7214 (error "No help available for system class tags."))
52a244eb
S
7215 (if (setq found-in (idlwave-class-found-in class-with))
7216 (setq name (cons (concat found-in "__define") class-with))
7217 (setq name (concat class-with "__define")))))
76959b77
S
7218 (setq kwd word
7219 idlwave-help-do-class-struct-tag t))
7220 (t (error "This should not happen"))))
7221
7222(defun idlwave-class-or-superclass-with-tag (class tag)
7223 "Find and return the CLASS or one of its superclass with the
7224associated TAG, if any."
52a244eb 7225 (let ((sclasses (cons class (cdr (assq 'all-inherits
76959b77
S
7226 (idlwave-class-info class)))))
7227 cl)
7228 (catch 'exit
7229 (while sclasses
7230 (setq cl (pop sclasses))
7231 (let ((tags (idlwave-class-tags cl)))
7232 (while tags
7233 (if (eq t (compare-strings tag 0 nil (car tags) 0 nil t))
52a244eb 7234 (throw 'exit cl))
76959b77
S
7235 (setq tags (cdr tags))))))))
7236
05a1abfc
CD
7237
7238(defun idlwave-sysvars-reset ()
7239 (if (and (fboundp 'idlwave-shell-is-running)
52a244eb
S
7240 (idlwave-shell-is-running)
7241 idlwave-idlwave_routine_info-compiled)
05a1abfc
CD
7242 (idlwave-shell-send-command "idlwave_get_sysvars"
7243 'idlwave-process-sysvars 'hide)))
7244
7245(defun idlwave-process-sysvars ()
7246 (idlwave-shell-filter-sysvars)
7247 (setq idlwave-sint-sysvars nil
7248 idlwave-sint-sysvartags nil)
7249 (idlwave-sintern-sysvar-alist))
7250
05a1abfc 7251(defun idlwave-sintern-sysvar-alist ()
52a244eb 7252 (let ((list idlwave-system-variables-alist) entry tags)
05a1abfc
CD
7253 (while (setq entry (pop list))
7254 (setcar entry (idlwave-sintern-sysvar (car entry) 'set))
52a244eb
S
7255 (setq tags (assq 'tags entry))
7256 (if tags
7257 (setcdr tags
7258 (mapcar (lambda (x)
7259 (cons (idlwave-sintern-sysvartag (car x) 'set)
7260 (cdr x)))
7261 (cdr tags)))))))
05a1abfc
CD
7262
7263(defvar idlwave-shell-command-output)
7264(defun idlwave-shell-filter-sysvars ()
52a244eb 7265 "Get any new system variables and tags."
05a1abfc
CD
7266 (let ((text idlwave-shell-command-output)
7267 (start 0)
7268 (old idlwave-system-variables-alist)
52a244eb 7269 var tags type name class link old-entry)
05a1abfc
CD
7270 (setq idlwave-system-variables-alist nil)
7271 (while (string-match "^IDLWAVE-SYSVAR: !\\([a-zA-Z0-9_$]+\\)\\( \\(.*\\)\\)?"
7272 text start)
7273 (setq start (match-end 0)
7274 var (match-string 1 text)
52a244eb
S
7275 tags (if (match-end 3)
7276 (idlwave-split-string (match-string 3 text))))
7277 ;; Maintain old links, if present
7278 (setq old-entry (assq (idlwave-sintern-sysvar var) old))
7279 (setq link (assq 'link old-entry))
05a1abfc 7280 (setq idlwave-system-variables-alist
52a244eb
S
7281 (cons (list var
7282 (cons
7283 'tags
7284 (mapcar (lambda (x)
7285 (cons x
7286 (cdr (assq
7287 (idlwave-sintern-sysvartag x)
7288 (cdr (assq 'tags old-entry))))))
7289 tags)) link)
05a1abfc
CD
7290 idlwave-system-variables-alist)))
7291 ;; Keep the old value if query was not successful
7292 (setq idlwave-system-variables-alist
7293 (or idlwave-system-variables-alist old))))
7294
f32b3b91
CD
7295(defun idlwave-completion-fontify-classes ()
7296 "Goto the *Completions* buffer and fontify the class info."
7297 (when (featurep 'font-lock)
7298 (save-excursion
7299 (set-buffer "*Completions*")
7300 (save-excursion
7301 (goto-char (point-min))
76959b77
S
7302 (let ((buffer-read-only nil))
7303 (while (re-search-forward "\\.*<[^>]+>" nil t)
7304 (put-text-property (match-beginning 0) (match-end 0)
7305 'face 'font-lock-string-face)))))))
f32b3b91
CD
7306
7307(defun idlwave-uniquify (list)
52a244eb
S
7308 (let ((ht (make-hash-table :size (length list) :test 'equal)))
7309 (delq nil
7310 (mapcar (lambda (x)
7311 (unless (gethash x ht)
7312 (puthash x t ht)
7313 x))
7314 list))))
f32b3b91
CD
7315
7316(defun idlwave-after-successful-completion (type slash &optional verify)
7317 "Add `=' or `(' after successful completion of keyword and function.
7318Restore the pre-completion window configuration if possible."
7319 (cond
7320 ((eq type 'procedure)
7321 nil)
7322 ((eq type 'function)
7323 (cond
7324 ((equal idlwave-function-completion-adds-paren nil) nil)
7325 ((or (equal idlwave-function-completion-adds-paren t)
7326 (equal idlwave-function-completion-adds-paren 1))
7327 (insert "("))
7328 ((equal idlwave-function-completion-adds-paren 2)
7329 (insert "()")
7330 (backward-char 1))
7331 (t nil)))
7332 ((eq type 'keyword)
7333 (if (and idlwave-keyword-completion-adds-equal
7334 (not slash))
7335 (progn (insert "=") t)
7336 nil)))
7337
7338 ;; Restore the pre-completion window configuration if this is safe.
52a244eb
S
7339
7340 (if (or (eq verify 'force) ; force
7341 (and
f32b3b91 7342 (get-buffer-window "*Completions*") ; visible
52a244eb 7343 (idlwave-local-value 'idlwave-completion-p
f32b3b91
CD
7344 "*Completions*") ; cib-buffer
7345 (eq (marker-buffer idlwave-completion-mark)
7346 (current-buffer)) ; buffer OK
7347 (equal (marker-position idlwave-completion-mark)
7348 verify))) ; pos OK
7349 (idlwave-restore-wconf-after-completion))
7350 (move-marker idlwave-completion-mark nil)
7351 (setq idlwave-before-completion-wconf nil))
7352
15e42531
CD
7353(defun idlwave-mouse-context-help (ev &optional arg)
7354 "Call `idlwave-context-help' on the clicked location."
7355 (interactive "eP")
7356 (mouse-set-point ev)
7357 (idlwave-context-help arg))
7358
7359(defvar idlwave-last-context-help-pos nil)
7360(defun idlwave-context-help (&optional arg)
7361 "Display IDL Online Help on context.
76959b77
S
7362If point is on a keyword, help for that keyword will be shown. If
7363point is on a routine name or in the argument list of a routine, help
7364for that routine will be displayed. Works for system routines and
7365keywords, it pulls up text help. For other routies and keywords,
7366visits the source file, finding help in the header (if
7367`idlwave-help-source-try-header' is non-nil) or the routine definition
7368itself."
f32b3b91 7369 (interactive "P")
15e42531
CD
7370 (idlwave-do-context-help arg))
7371
7372(defun idlwave-mouse-completion-help (ev)
7373 "Display online help about the completion at point."
7374 (interactive "eP")
52a244eb
S
7375 ;; Restore last-command for next command, to make
7376 ;; scrolling/cancelling of completions work.
15e42531
CD
7377 (setq this-command last-command)
7378 (idlwave-do-mouse-completion-help ev))
15e42531 7379
f32b3b91 7380(defun idlwave-routine-info (&optional arg external)
52a244eb
S
7381 "Display a routines calling sequence and list of keywords. When
7382point is on the name a function or procedure, or in the argument list
7383of a function or procedure, this command displays a help buffer with
7384the information. When called with prefix arg, enforce class query.
f32b3b91
CD
7385
7386When point is on an object operator `->', display the class stored in
7387this arrow, if any (see `idlwave-store-inquired-class'). With a
7388prefix arg, the class property is cleared out."
7389
7390 (interactive "P")
7391 (idlwave-routines)
7392 (if (string-match "->" (buffer-substring
7393 (max (point-min) (1- (point)))
7394 (min (+ 2 (point)) (point-max))))
7395 ;; Cursor is on an arrow
7396 (if (get-text-property (point) 'idlwave-class)
7397 ;; arrow has class property
7398 (if arg
7399 ;; Remove property
7400 (save-excursion
7401 (backward-char 1)
7402 (when (looking-at ".?\\(->\\)")
7403 (remove-text-properties (match-beginning 1) (match-end 1)
7404 '(idlwave-class nil face nil))
7405 (message "Class property removed from arrow")))
7406 ;; Echo class property
7407 (message "Arrow has text property identifying object to be class %s"
7408 (get-text-property (point) 'idlwave-class)))
7409 ;; No property found
7410 (message "Arrow has no class text property"))
7411
7412 ;; Not on an arrow...
7413 (let* ((idlwave-query-class nil)
7414 (idlwave-force-class-query (equal arg '(4)))
7415 (module (idlwave-what-module)))
15e42531 7416 (if (car module)
05a1abfc
CD
7417 (apply 'idlwave-display-calling-sequence
7418 (idlwave-fix-module-if-obj_new module))
e8af40ee 7419 (error "Don't know which calling sequence to show")))))
f32b3b91
CD
7420
7421(defun idlwave-resolve (&optional arg)
52a244eb 7422 "Call RESOLVE_ROUTINE on the module name at point.
f32b3b91
CD
7423Like `idlwave-routine-info', this looks for a routine call at point.
7424After confirmation in the minibuffer, it will use the shell to issue
7425a RESOLVE call for this routine, to attempt to make it defined and its
7426routine info available for IDLWAVE. If the routine is a method call,
7427both `class__method' and `class__define' will be tried.
7428With ARG, enforce query for the class of object methods."
7429 (interactive "P")
7430 (let* ((idlwave-query-class nil)
7431 (idlwave-force-class-query (equal arg '(4)))
7432 (module (idlwave-what-module))
7433 (name (idlwave-make-full-name (nth 2 module) (car module)))
7434 (type (if (eq (nth 1 module) 'pro) "pro" "function"))
7435 (resolve (read-string "Resolve: " (format "%s %s" type name)))
7436 (kwd "")
7437 class)
7438 (if (string-match "\\(pro\\|function\\)[ \t]+\\(\\(.*\\)::\\)?\\(.*\\)"
7439 resolve)
7440 (setq type (match-string 1 resolve)
52a244eb 7441 class (if (match-beginning 2)
f32b3b91
CD
7442 (match-string 3 resolve)
7443 nil)
7444 name (match-string 4 resolve)))
7445 (if (string= (downcase type) "function")
7446 (setq kwd ",/is_function"))
7447
7448 (cond
7449 ((null class)
52a244eb 7450 (idlwave-shell-send-command
f32b3b91
CD
7451 (format "resolve_routine,'%s'%s" (downcase name) kwd)
7452 'idlwave-update-routine-info
7453 nil t))
7454 (t
52a244eb 7455 (idlwave-shell-send-command
f32b3b91 7456 (format "resolve_routine,'%s__define'%s" (downcase class) kwd)
52a244eb
S
7457 (list 'idlwave-shell-send-command
7458 (format "resolve_routine,'%s__%s'%s"
f32b3b91
CD
7459 (downcase class) (downcase name) kwd)
7460 '(idlwave-update-routine-info)
7461 nil t))))))
7462
7463(defun idlwave-find-module (&optional arg)
7464 "Find the source code of an IDL module.
52a244eb
S
7465Works for modules for which IDLWAVE has routine info available. The
7466function offers as default the module name `idlwave-routine-info'
7467would use. With ARG limit to this buffer. With two prefix ARG's
7468force class query for object methods."
f32b3b91
CD
7469 (interactive "P")
7470 (let* ((idlwave-query-class nil)
52a244eb
S
7471 (idlwave-force-class-query (equal arg '(16)))
7472 (this-buffer (equal arg '(4)))
05a1abfc 7473 (module (idlwave-fix-module-if-obj_new (idlwave-what-module)))
52a244eb
S
7474 (default (if module
7475 (concat (idlwave-make-full-name
7476 (nth 2 module) (car module))
7477 (if (eq (nth 1 module) 'pro) "<p>" "<f>"))
7478 "none"))
7479 (list
7480 (idlwave-uniquify
7481 (delq nil
7482 (mapcar (lambda (x)
7483 (if (eq 'system (car-safe (nth 3 x)))
7484 ;; Take out system routines with no source.
7485 nil
7486 (list
7487 (concat (idlwave-make-full-name
7488 (nth 2 x) (car x))
7489 (if (eq (nth 1 x) 'pro) "<p>" "<f>")))))
7490 (if this-buffer
7491 (idlwave-save-buffer-update)
7492 (idlwave-routines))))))
f32b3b91 7493 (name (idlwave-completing-read
52a244eb
S
7494 (if (or (not this-buffer)
7495 (assoc default list))
7496 (format "Module (Default %s): " default)
7497 (format "Module in this file: "))
f32b3b91
CD
7498 list))
7499 type class)
7500 (if (string-match "\\`\\s-*\\'" name)
7501 ;; Nothing, use the default.
7502 (setq name default))
7503 (if (string-match "<[fp]>" name)
7504 (setq type (substring name -2 -1)
7505 name (substring name 0 -3)))
7506 (if (string-match "\\(.*\\)::\\(.*\\)" name)
7507 (setq class (match-string 1 name)
7508 name (match-string 2 name)))
7509 (setq name (idlwave-sintern-routine-or-method name class)
7510 class (idlwave-sintern-class class)
7511 type (cond ((equal type "f") 'fun)
7512 ((equal type "p") 'pro)
7513 (t t)))
52a244eb 7514 (idlwave-do-find-module name type class nil this-buffer)))
f32b3b91 7515
52a244eb
S
7516(defun idlwave-do-find-module (name type class
7517 &optional force-source this-buffer)
f32b3b91 7518 (let ((name1 (idlwave-make-full-name class name))
52a244eb 7519 source buf1 entry
f32b3b91 7520 (buf (current-buffer))
05a1abfc 7521 (pos (point))
52a244eb
S
7522 file name2)
7523 (setq entry (idlwave-best-rinfo-assq name type class (idlwave-routines)
7524 'WITH-FILE)
05a1abfc
CD
7525 source (or force-source (nth 3 entry))
7526 name2 (if (nth 2 entry)
7527 (idlwave-make-full-name (nth 2 entry) name)
775591f7 7528 name1))
52a244eb
S
7529 (if source
7530 (setq file (idlwave-routine-source-file source)))
7531 (unless file ; Try to find it on the path.
7532 (setq file
7533 (idlwave-expand-lib-file-name
7534 (if class
7535 (format "%s__define.pro" (downcase class))
7536 (format "%s.pro" (downcase name))))))
f32b3b91
CD
7537 (cond
7538 ((or (null name) (equal name ""))
7539 (error "Abort"))
f32b3b91 7540 ((eq (car source) 'system)
52a244eb 7541 (error "Source code for system routine %s is not available"
05a1abfc 7542 name2))
52a244eb 7543 ((or (not file) (not (file-regular-p file)))
e8af40ee 7544 (error "Source code for routine %s is not available"
05a1abfc 7545 name2))
52a244eb
S
7546 (t
7547 (when (not this-buffer)
7548 (setq buf1
7549 (idlwave-find-file-noselect file 'find))
7550 (pop-to-buffer buf1 t))
15e42531 7551 (goto-char (point-max))
f32b3b91 7552 (let ((case-fold-search t))
15e42531 7553 (if (re-search-backward
f32b3b91 7554 (concat "^[ \t]*\\<"
52a244eb
S
7555 (cond ((eq type 'fun) "function")
7556 ((eq type 'pro) "pro")
f32b3b91 7557 (t "\\(pro\\|function\\)"))
52a244eb 7558 "\\>[ \t]+"
05a1abfc 7559 (regexp-quote (downcase name2))
f32b3b91
CD
7560 "[^a-zA-Z0-9_$]")
7561 nil t)
7562 (goto-char (match-beginning 0))
7563 (pop-to-buffer buf)
7564 (goto-char pos)
05a1abfc 7565 (error "Could not find routine %s" name2)))))))
f32b3b91
CD
7566
7567(defun idlwave-what-module ()
7568 "Return a default module for stuff near point.
7569Used by `idlwave-routine-info' and `idlwave-find-module'."
7570 (idlwave-routines)
15e42531
CD
7571 (if (let ((case-fold-search t))
7572 (save-excursion
7573 (idlwave-beginning-of-statement)
7574 (looking-at "[ \t]*\\(pro\\|function\\)[ \t]+\\(\\([a-zA-Z0-9_$]+\\)::\\)?\\([a-zA-Z0-9$_]+\\)\\([, \t\n]\\|$\\)")))
7575 ;; This is a function or procedure definition statement
7576 ;; We return the defined routine as module.
7577 (list
52a244eb
S
7578 (idlwave-sintern-routine-or-method (match-string-no-properties 4)
7579 (match-string-no-properties 2))
15e42531
CD
7580 (if (equal (downcase (match-string 1)) "pro") 'pro 'fun)
7581 (idlwave-sintern-class (match-string 3)))
7582
52a244eb 7583 ;; Not a definition statement - analyze precise position.
15e42531
CD
7584 (let* ((where (idlwave-where))
7585 (cw (nth 2 where))
7586 (pro (car (nth 0 where)))
7587 (func (car (nth 1 where)))
7588 (this-word (idlwave-this-word "a-zA-Z0-9$_"))
7589 (next-char (save-excursion (skip-chars-forward "a-zA-Z0-9$_")
7590 (following-char)))
7591 )
7592 (cond
7593 ((and (eq cw 'procedure)
7594 (not (equal this-word "")))
52a244eb 7595 (setq this-word (idlwave-sintern-routine-or-method
15e42531
CD
7596 this-word (nth 2 (nth 3 where))))
7597 (list this-word 'pro
52a244eb 7598 (idlwave-determine-class
15e42531
CD
7599 (cons this-word (cdr (nth 3 where)))
7600 'pro)))
52a244eb 7601 ((and (eq cw 'function)
15e42531
CD
7602 (not (equal this-word ""))
7603 (or (eq next-char ?\() ; exclude arrays, vars.
7604 (looking-at "[a-zA-Z0-9_]*[ \t]*(")))
52a244eb 7605 (setq this-word (idlwave-sintern-routine-or-method
15e42531
CD
7606 this-word (nth 2 (nth 3 where))))
7607 (list this-word 'fun
7608 (idlwave-determine-class
7609 (cons this-word (cdr (nth 3 where)))
7610 'fun)))
7611 ((and (memq cw '(function-keyword procedure-keyword))
7612 (not (equal this-word ""))
7613 (eq next-char ?\()) ; A function!
7614 (setq this-word (idlwave-sintern-routine this-word))
7615 (list this-word 'fun nil))
7616 (func
7617 (list func 'fun (idlwave-determine-class (nth 1 where) 'fun)))
7618 (pro
7619 (list pro 'pro (idlwave-determine-class (nth 0 where) 'pro)))
7620 (t nil)))))
f32b3b91 7621
05a1abfc
CD
7622(defun idlwave-what-module-find-class ()
7623 "Call idlwave-what-module and find the inherited class if necessary."
7624 (let* ((module (idlwave-what-module))
7625 (class (nth 2 module))
7626 classes)
7627 (if (and (= (length module) 3)
7628 (stringp class))
7629 (list (car module)
7630 (nth 1 module)
7631 (apply 'idlwave-find-inherited-class module))
7632 module)))
7633
7634(defun idlwave-find-inherited-class (name type class)
7635 "Find the class which defines TYPE NAME and is CLASS or inherited by CLASS."
7636 (let ((entry (idlwave-best-rinfo-assoc name type class (idlwave-routines))))
7637 (if entry
7638 (nth 2 entry)
7639 class)))
7640
7641(defun idlwave-fix-module-if-obj_new (module)
52a244eb
S
7642 "Check if MODULE points to obj_new.
7643If yes, and if the cursor is in the keyword region, change to the
7644appropriate Init method."
05a1abfc
CD
7645 (let* ((name (car module))
7646 (pos (point))
7647 (case-fold-search t)
7648 string)
7649 (if (and (stringp name)
7650 (equal (downcase name) "obj_new")
7651 (save-excursion
7652 (idlwave-beginning-of-statement)
7653 (setq string (buffer-substring (point) pos))
7654 (string-match "obj_new([^'\"]*['\"]\\([a-zA-Z0-9_]+\\)"
7655 string)))
7656 (let ((name "Init")
7657 (class (match-string 1 string)))
7658 (setq module (list (idlwave-sintern-method "Init")
7659 'fun
7660 (idlwave-sintern-class class)))))
7661 module))
7662
52a244eb
S
7663(defun idlwave-fix-keywords (name type class keywords &optional super-classes)
7664 "Update a list of keywords.
7665Translate OBJ_NEW, adding all super-class keywords, or all keywords
7666from all classes if class equals t."
5e72c6b2 7667 (let ((case-fold-search t))
f32b3b91
CD
7668
7669 ;; If this is the OBJ_NEW function, try to figure out the class and use
7670 ;; the keywords from the corresponding INIT method.
5e72c6b2 7671 (if (and (equal (upcase name) "OBJ_NEW")
05a1abfc
CD
7672 (or (eq major-mode 'idlwave-mode)
7673 (eq major-mode 'idlwave-shell-mode)))
f32b3b91
CD
7674 (let* ((bos (save-excursion (idlwave-beginning-of-statement) (point)))
7675 (string (buffer-substring bos (point)))
7676 (case-fold-search t)
7677 class)
7678 (and (string-match "obj_new([^'\"]*['\"]\\([a-zA-Z0-9_]+\\)"
7679 string)
7680 (setq class (idlwave-sintern-class (match-string 1 string)))
15e42531 7681 (setq idlwave-current-obj_new-class class)
52a244eb
S
7682 (setq keywords
7683 (append keywords
7684 (idlwave-entry-keywords
7685 (idlwave-rinfo-assq
7686 (idlwave-sintern-method "INIT")
7687 'fun
7688 class
7689 (idlwave-routines)) 'do-link))))))
7690
f32b3b91
CD
7691 ;; If the class is `t', combine all keywords of all methods NAME
7692 (when (eq class t)
52a244eb
S
7693 (mapc (lambda (entry)
7694 (and
7695 (nth 2 entry) ; non-nil class
7696 (eq (nth 1 entry) type) ; correct type
7697 (setq keywords
7698 (append keywords
7699 (idlwave-entry-keywords entry 'do-link)))))
7700 (idlwave-all-assq name (idlwave-routines)))
5e72c6b2 7701 (setq keywords (idlwave-uniquify keywords)))
52a244eb 7702
5e72c6b2 7703 ;; If we have inheritance, add all keywords from superclasses, if
52a244eb
S
7704 ;; the user indicated that method in `idlwave-keyword-class-inheritance'
7705 (when (and
7706 super-classes
5e72c6b2
S
7707 idlwave-keyword-class-inheritance
7708 (stringp class)
7709 (or (assq (idlwave-sintern-keyword "_extra") keywords)
7710 (assq (idlwave-sintern-keyword "_ref_extra") keywords))
5e72c6b2
S
7711 ;; Check if one of the keyword-class regexps matches the name
7712 (let ((regexps idlwave-keyword-class-inheritance) re)
7713 (catch 'exit
7714 (while (setq re (pop regexps))
7715 (if (string-match re name) (throw 'exit t))))))
52a244eb
S
7716
7717 (loop for entry in (idlwave-routines) do
7718 (and (nth 2 entry) ; non-nil class
7719 (memq (nth 2 entry) super-classes) ; an inherited class
7720 (eq (nth 1 entry) type) ; correct type
7721 (eq (car entry) name) ; correct name
5e72c6b2 7722 (mapcar (lambda (k) (add-to-list 'keywords k))
52a244eb 7723 (idlwave-entry-keywords entry 'do-link))))
f32b3b91 7724 (setq keywords (idlwave-uniquify keywords)))
52a244eb 7725
f32b3b91
CD
7726 ;; Return the final list
7727 keywords))
7728
15e42531
CD
7729(defun idlwave-expand-keyword (keyword module)
7730 "Expand KEYWORD to one of the legal keyword parameters of MODULE.
7731KEYWORD may be an exact match or an abbreviation of a keyword.
7732If the match is exact, KEYWORD itself is returned, even if there may be other
7733keywords of which KEYWORD is an abbreviation. This is necessary because some
7734system routines have keywords which are prefixes of other keywords.
7735If KEYWORD is an abbreviation of several keywords, a list of all possible
7736completions is returned.
7737If the abbreviation was unique, the correct keyword is returned.
7738If it cannot be a keyword, the function return nil.
7739If we do not know about MODULE, just return KEYWORD literally."
7740 (let* ((name (car module))
7741 (type (nth 1 module))
7742 (class (nth 2 module))
7743 (kwd (idlwave-sintern-keyword keyword))
7744 (entry (idlwave-best-rinfo-assoc name type class (idlwave-routines)))
52a244eb 7745 (kwd-alist (idlwave-entry-keywords entry))
15e42531
CD
7746 (extra (or (assq (idlwave-sintern-keyword "_EXTRA") kwd-alist)
7747 (assq (idlwave-sintern-keyword "_REF_EXTRA") kwd-alist)))
7748 (completion-ignore-case t)
7749 candidates)
52a244eb 7750 (cond ((assq kwd kwd-alist)
15e42531
CD
7751 kwd)
7752 ((setq candidates (all-completions kwd kwd-alist))
7753 (if (= (length candidates) 1)
7754 (car candidates)
7755 candidates))
7756 ((and entry extra)
52a244eb 7757 ;; Inheritance may cause this keyword to be correct
15e42531
CD
7758 keyword)
7759 (entry
7760 ;; We do know the function, which does not have the keyword.
7761 nil)
7762 (t
7763 ;; We do not know the function, so this just might be a correct
7764 ;; keyword - return it as it is.
7765 keyword))))
7766
7767(defvar idlwave-rinfo-mouse-map (make-sparse-keymap))
f32b3b91 7768(defvar idlwave-rinfo-map (make-sparse-keymap))
52a244eb 7769(define-key idlwave-rinfo-mouse-map
f32b3b91
CD
7770 (if (featurep 'xemacs) [button2] [mouse-2])
7771 'idlwave-mouse-active-rinfo)
52a244eb 7772(define-key idlwave-rinfo-mouse-map
15e42531
CD
7773 (if (featurep 'xemacs) [(shift button2)] [(shift mouse-2)])
7774 'idlwave-mouse-active-rinfo-shift)
52a244eb 7775(define-key idlwave-rinfo-mouse-map
f32b3b91
CD
7776 (if (featurep 'xemacs) [button3] [mouse-3])
7777 'idlwave-mouse-active-rinfo-right)
15e42531
CD
7778(define-key idlwave-rinfo-mouse-map " " 'idlwave-active-rinfo-space)
7779(define-key idlwave-rinfo-map "q" 'idlwave-quit-help)
7780(define-key idlwave-rinfo-mouse-map "q" 'idlwave-quit-help)
7781(defvar idlwave-popup-source nil)
7782(defvar idlwave-rinfo-marker (make-marker))
7783
7784(defun idlwave-quit-help ()
7785 (interactive)
7786 (let ((ri-window (get-buffer-window "*Help*"))
7787 (olh-window (get-buffer-window "*IDLWAVE Help*")))
7788 (when (and olh-window
7789 (fboundp 'idlwave-help-quit))
7790 (select-window olh-window)
7791 (idlwave-help-quit))
7792 (when (window-live-p ri-window)
7793 (delete-window ri-window))))
f32b3b91 7794
05a1abfc
CD
7795(defun idlwave-display-calling-sequence (name type class
7796 &optional initial-class)
f32b3b91 7797 ;; Display the calling sequence of module NAME, type TYPE in class CLASS.
05a1abfc
CD
7798 (let* ((initial-class (or initial-class class))
7799 (entry (or (idlwave-best-rinfo-assq name type class
15e42531 7800 (idlwave-routines))
52a244eb 7801 (idlwave-rinfo-assq name type class
15e42531 7802 idlwave-unresolved-routines)))
f32b3b91
CD
7803 (name (or (car entry) name))
7804 (class (or (nth 2 entry) class))
05a1abfc 7805 (superclasses (idlwave-all-class-inherits initial-class))
15e42531
CD
7806 (twins (idlwave-routine-twins entry))
7807 (dtwins (idlwave-study-twins twins))
7808 (all dtwins)
52a244eb 7809 (system (eq (car (nth 3 entry)) 'system))
f32b3b91 7810 (calling-seq (nth 4 entry))
52a244eb
S
7811 (keywords (idlwave-entry-keywords entry 'do-link))
7812 (html-file (car (nth 5 entry)))
15e42531 7813 (help-echo-kwd
52a244eb 7814 "Button2: Insert KEYWORD (SHIFT=`/KEYWORD') | Button3: Online Help ")
15e42531 7815 (help-echo-use
52a244eb 7816 "Button2/3: Online Help")
15e42531 7817 (help-echo-src
52a244eb 7818 "Button2: Jump to source and back | Button3: Source in Help window.")
05a1abfc
CD
7819 (help-echo-class
7820 "Button2: Display info about same method in superclass")
f32b3b91 7821 (col 0)
52a244eb 7822 (data (list name type class (current-buffer) nil initial-class))
f32b3b91 7823 (km-prop (if (featurep 'xemacs) 'keymap 'local-map))
15e42531
CD
7824 (face 'idlwave-help-link-face)
7825 beg props win cnt total)
52a244eb
S
7826 ;; Fix keywords, but don't add chained super-classes, since these
7827 ;; are shown separately for that super-class
f32b3b91
CD
7828 (setq keywords (idlwave-fix-keywords name type class keywords))
7829 (cond
7830 ((null entry)
05a1abfc
CD
7831 (error "No %s %s known %s" type name
7832 (if initial-class (concat "in class " initial-class) "")))
f32b3b91 7833 ((or (null name) (equal name ""))
e8af40ee 7834 (error "No function or procedure call at point"))
f32b3b91 7835 ((null calling-seq)
52a244eb 7836 (error "Calling sequence of %s %s not available" type name))
f32b3b91
CD
7837 (t
7838 (save-excursion
15e42531 7839 (move-marker idlwave-rinfo-marker (point))
f32b3b91 7840 (set-buffer (get-buffer-create "*Help*"))
15e42531 7841 (use-local-map idlwave-rinfo-map)
f32b3b91
CD
7842 (setq buffer-read-only nil)
7843 (erase-buffer)
7844 (set (make-local-variable 'idlwave-popup-source) nil)
15e42531
CD
7845 (set (make-local-variable 'idlwave-current-obj_new-class)
7846 idlwave-current-obj_new-class)
05a1abfc
CD
7847 (when superclasses
7848 (setq props (list 'mouse-face 'highlight
7849 km-prop idlwave-rinfo-mouse-map
7850 'help-echo help-echo-class
7851 'data (cons 'class data)))
7852 (let ((classes (cons initial-class superclasses)) c)
7853 (insert "Classes: ")
7854 (while (setq c (pop classes))
7855 (insert " ")
7856 (setq beg (point))
7857 (insert c)
7858 (if (equal (downcase c) (downcase class))
7859 (add-text-properties beg (point) (list 'face 'bold))
52a244eb 7860 ;; If Method exists in a different class link it
05a1abfc
CD
7861 (if (idlwave-rinfo-assq name type c (idlwave-routines))
7862 (add-text-properties beg (point) props))))
7863 (insert "\n")))
52a244eb
S
7864 (setq props (list 'mouse-face 'highlight
7865 km-prop idlwave-rinfo-mouse-map
7866 'help-echo help-echo-use
7867 'data (cons 'usage data)))
7868 (if html-file (setq props (append (list 'face face 'link html-file)
7869 props)))
f32b3b91
CD
7870 (insert "Usage: ")
7871 (setq beg (point))
7872 (insert (if class
52a244eb
S
7873 (format calling-seq class name class name class name)
7874 (format calling-seq name name name name))
f32b3b91
CD
7875 "\n")
7876 (add-text-properties beg (point) props)
52a244eb 7877
f32b3b91
CD
7878 (insert "Keywords:")
7879 (if (null keywords)
7880 (insert " No keywords accepted.")
7881 (setq col 9)
7882 (mapcar
7883 (lambda (x)
52a244eb 7884 (if (>= (+ col 1 (length (car x)))
f32b3b91
CD
7885 (window-width))
7886 (progn
7887 (insert "\n ")
7888 (setq col 9)))
7889 (insert " ")
7890 (setq beg (point)
52a244eb 7891 ;; Relevant keywords already have link property attached
f32b3b91 7892 props (list 'mouse-face 'highlight
15e42531 7893 km-prop idlwave-rinfo-mouse-map
f32b3b91 7894 'data (cons 'keyword data)
15e42531 7895 'help-echo help-echo-kwd
f32b3b91 7896 'keyword (car x)))
52a244eb 7897 (if system (setq props (append (list 'face face) props)))
f32b3b91
CD
7898 (insert (car x))
7899 (add-text-properties beg (point) props)
7900 (setq col (+ col 1 (length (car x)))))
7901 keywords))
52a244eb 7902
15e42531 7903 (setq cnt 1 total (length all))
52a244eb 7904 ;; Here entry is (key file (list of type-conses))
15e42531
CD
7905 (while (setq entry (pop all))
7906 (setq props (list 'mouse-face 'highlight
7907 km-prop idlwave-rinfo-mouse-map
7908 'help-echo help-echo-src
52a244eb
S
7909 'source (list (car (car (nth 2 entry))) ;type
7910 (nth 1 entry)
7911 nil
7912 (cdr (car (nth 2 entry))))
15e42531
CD
7913 'data (cons 'source data)))
7914 (idlwave-insert-source-location
52a244eb 7915 (format "\n%-8s %s"
15e42531
CD
7916 (if (equal cnt 1)
7917 (if (> total 1) "Sources:" "Source:")
7918 "")
7919 (if (> total 1) "- " ""))
7920 entry props)
7921 (incf cnt)
7922 (when (and all (> cnt idlwave-rinfo-max-source-lines))
7923 ;; No more source lines, please
52a244eb 7924 (insert (format
15e42531
CD
7925 "\n Source information truncated to %d entries."
7926 idlwave-rinfo-max-source-lines))
7927 (setq all nil)))
52a244eb 7928 (beginning-of-buffer)
f32b3b91
CD
7929 (setq buffer-read-only t))
7930 (display-buffer "*Help*")
7931 (if (and (setq win (get-buffer-window "*Help*"))
7932 idlwave-resize-routine-help-window)
7933 (progn
7934 (let ((ww (selected-window)))
7935 (unwind-protect
7936 (progn
7937 (select-window win)
52a244eb 7938 (enlarge-window (- (/ (frame-height) 2)
f32b3b91
CD
7939 (window-height)))
7940 (shrink-window-if-larger-than-buffer))
7941 (select-window ww)))))))))
7942
15e42531
CD
7943(defun idlwave-insert-source-location (prefix entry &optional file-props)
7944 "Insert a source location into the routine info buffer.
52a244eb
S
7945Start line with PREFIX. If a file name is inserted, add FILE-PROPS to
7946it."
15e42531
CD
7947 (let* ((key (car entry))
7948 (file (nth 1 entry))
7949 (types (nth 2 entry))
52a244eb
S
7950 (shell-flag (assq 'compiled types))
7951 (buffer-flag (assq 'buffer types))
7952 (user-flag (assq 'user types))
7953 (lib-flag (assq 'lib types))
7954 (ndupl (or (and buffer-flag (idlwave-count-memq 'buffer types))
7955 (and user-flag (idlwave-count-memq 'user types))
7956 (and lib-flag (idlwave-count-memq 'lib types))
15e42531
CD
7957 1))
7958 (doflags t)
7959 beg special)
7960
7961 (insert prefix)
7962
7963 (cond
7964 ((eq key 'system)
7965 (setq doflags nil)
52a244eb
S
7966 (insert "System "))
7967
15e42531
CD
7968 ((eq key 'builtin)
7969 (setq doflags nil)
52a244eb
S
7970 (insert "Builtin "))
7971
15e42531 7972 ((and (not file) shell-flag)
52a244eb
S
7973 (insert "Unresolved"))
7974
7975 ((null file)
7976 (insert "ERROR"))
7977
15e42531
CD
7978 ((idlwave-syslib-p file)
7979 (if (string-match "obsolete" (file-name-directory file))
52a244eb
S
7980 (insert "Obsolete ")
7981 (insert "SystemLib ")))
7982
7983 ;; New special syntax: taken directly from routine-info for
7984 ;; library catalog routines
7985 ((setq special (or (cdr lib-flag) (cdr user-flag)))
7986 (insert (format "%-10s" special)))
7987
7988 ;; Old special syntax: a matching regexp
7989 ((setq special (idlwave-special-lib-test file))
7990 (insert (format "%-10s" special)))
7991
7992 ;; Catch-all with file
15e42531 7993 ((idlwave-lib-p file) (insert "Library "))
52a244eb
S
7994
7995 ;; Sanity catch all
15e42531
CD
7996 (t (insert "Other ")))
7997
7998 (when doflags
7999 (insert (concat
8000 " ["
52a244eb
S
8001 (if lib-flag "L" "-")
8002 (if user-flag "C" "-")
15e42531
CD
8003 (if shell-flag "S" "-")
8004 (if buffer-flag "B" "-")
8005 "] ")))
52a244eb 8006 (when (> ndupl 1)
15e42531
CD
8007 (setq beg (point))
8008 (insert (format "(%dx) " ndupl))
8009 (add-text-properties beg (point) (list 'face 'bold)))
8010 (when (and file (not (equal file "")))
8011 (setq beg (point))
8012 (insert (apply 'abbreviate-file-name
8013 (if (featurep 'xemacs) (list file t) (list file))))
8014 (if file-props
8015 (add-text-properties beg (point) file-props)))))
8016
8017(defun idlwave-special-lib-test (file)
8018 "Check the path of FILE against the regexps which define special libs.
8019Return the name of the special lib if there is a match."
8020 (let ((alist idlwave-special-lib-alist)
8021 entry rtn)
8022 (cond
8023 ((stringp file)
8024 (while (setq entry (pop alist))
8025 (if (string-match (car entry) file)
8026 (setq rtn (cdr entry)
8027 alist nil)))
8028 rtn)
8029 (t nil))))
52a244eb 8030
f32b3b91
CD
8031(defun idlwave-mouse-active-rinfo-right (ev)
8032 (interactive "e")
8033 (idlwave-mouse-active-rinfo ev 'right))
8034
15e42531 8035(defun idlwave-mouse-active-rinfo-shift (ev)
f32b3b91 8036 (interactive "e")
15e42531
CD
8037 (idlwave-mouse-active-rinfo ev nil 'shift))
8038
8039(defun idlwave-active-rinfo-space ()
8040 (interactive)
8041 (idlwave-mouse-active-rinfo nil 'right))
8042
8043(defun idlwave-mouse-active-rinfo (ev &optional right shift)
8044 "Does the mouse actions in the routine info buffer.
8045Optional args RIGHT and SHIFT indicate, if mouse-3 was used, and if SHIFT
8046was pressed."
8047 (interactive "e")
8048 (if ev (mouse-set-point ev))
52a244eb 8049 (let (data id name type class buf bufwin source word initial-class)
f32b3b91 8050 (setq data (get-text-property (point) 'data)
15e42531 8051 source (get-text-property (point) 'source)
f32b3b91 8052 keyword (get-text-property (point) 'keyword)
52a244eb 8053 link (get-text-property (point) 'link)
f32b3b91 8054 id (car data)
15e42531 8055 name (nth 1 data) type (nth 2 data) class (nth 3 data)
f32b3b91 8056 buf (nth 4 data)
05a1abfc
CD
8057 initial-class (nth 6 data)
8058 word (idlwave-this-word)
f32b3b91 8059 bufwin (get-buffer-window buf t))
52a244eb
S
8060
8061 (cond ((eq id 'class) ; Switch class being displayed
05a1abfc 8062 (if (window-live-p bufwin) (select-window bufwin))
52a244eb 8063 (idlwave-display-calling-sequence
05a1abfc 8064 (idlwave-sintern-method name)
52a244eb 8065 type (idlwave-sintern-class word)
05a1abfc 8066 initial-class))
52a244eb
S
8067 ((eq id 'usage) ; Online help on this routine
8068 (idlwave-online-help link name type class))
8069 ((eq id 'source) ; Source in help or buffer
8070 (if right ; In help
15e42531
CD
8071 (let ((idlwave-extra-help-function 'idlwave-help-with-source)
8072 (idlwave-help-source-try-header nil)
52a244eb 8073 ;; Fake idlwave-routines so help will find the right entry
15e42531 8074 (idlwave-routines
52a244eb 8075 (list (list name type class source ""))))
15e42531 8076 (idlwave-help-get-special-help name type class nil))
52a244eb 8077 ;; Otherwise just pop to the source
f32b3b91
CD
8078 (setq idlwave-popup-source (not idlwave-popup-source))
8079 (if idlwave-popup-source
8080 (condition-case err
15e42531 8081 (idlwave-do-find-module name type class source)
f32b3b91
CD
8082 (error
8083 (setq idlwave-popup-source nil)
8084 (if (window-live-p bufwin) (select-window bufwin))
8085 (error (nth 1 err))))
8086 (if bufwin
8087 (select-window bufwin)
15e42531
CD
8088 (pop-to-buffer buf))
8089 (goto-char (marker-position idlwave-rinfo-marker)))))
f32b3b91
CD
8090 ((eq id 'keyword)
8091 (if right
52a244eb 8092 (idlwave-online-help link name type class keyword)
15e42531
CD
8093 (idlwave-rinfo-insert-keyword keyword buf shift))))))
8094
8095(defun idlwave-rinfo-insert-keyword (keyword buffer &optional shift)
8096 "Insert KEYWORD in BUFFER. Make sure buffer is displayed in a window."
8097 (let ((bwin (get-buffer-window buffer)))
8098 (if idlwave-complete-empty-string-as-lower-case
8099 (setq keyword (downcase keyword)))
8100 (if bwin
8101 (select-window bwin)
8102 (pop-to-buffer buffer)
8103 (setq bwin (get-buffer-window buffer)))
8104 (if (eq (preceding-char) ?/)
8105 (insert keyword)
52a244eb 8106 (unless (save-excursion
15e42531 8107 (re-search-backward
52a244eb 8108 "[(,][ \t]*\\(\\$[ \t]*\\(;.*\\)?\n\\)?[ \t]*\\="
15e42531
CD
8109 (min (- (point) 100) (point-min)) t))
8110 (insert ", "))
8111 (if shift (insert "/"))
8112 (insert keyword)
8113 (if (and (not shift)
8114 idlwave-keyword-completion-adds-equal)
8115 (insert "=")))))
8116
8117(defun idlwave-list-buffer-load-path-shadows (&optional arg)
8118 "List the load path shadows of all routines defined in current buffer."
8119 (interactive "P")
8120 (idlwave-routines)
8121 (if (eq major-mode 'idlwave-mode)
8122 (idlwave-list-load-path-shadows
8123 nil (idlwave-update-current-buffer-info 'save-buffer)
8124 "in current buffer")
8125 (error "Current buffer is not in idlwave-mode")))
8126
8127(defun idlwave-list-shell-load-path-shadows (&optional arg)
8128 "List the load path shadows of all routines compiled under the shell.
8129This is very useful for checking an IDL application. Just compile the
8130application, do RESOLVE_ALL, and `C-c C-i' to compile all referenced
8131routines and update IDLWAVE internal info. Then check for shadowing
8132with this command."
8133 (interactive "P")
8134 (cond
8135 ((or (not (fboundp 'idlwave-shell-is-running))
8136 (not (idlwave-shell-is-running)))
8137 (error "Shell is not running"))
8138 ((null idlwave-compiled-routines)
e8af40ee 8139 (error "No compiled routines. Maybe you need to update with `C-c C-i'"))
15e42531
CD
8140 (t
8141 (idlwave-list-load-path-shadows nil idlwave-compiled-routines
8142 "in the shell"))))
8143
8144(defun idlwave-list-all-load-path-shadows (&optional arg)
8145 "List the load path shadows of all routines known to IDLWAVE."
8146 (interactive "P")
8147 (idlwave-list-load-path-shadows nil nil "globally"))
8148
8149(defun idlwave-list-load-path-shadows (arg &optional special-routines loc)
8150 "List the routines which are defined multiple times.
8151Search the information IDLWAVE has about IDL routines for multiple
8152definitions.
8153When SPECIAL-ROUTINES in non-nil, only look for shadows of these routines.
8154
8155When IDL hits a routine call which is not defined, it will search on
8156the load path in order to find a definition. The output of this
8157command can be used to detect possible name clashes during this process."
8158 (idlwave-routines) ; Make sure everything is loaded.
52a244eb
S
8159 (unless (or idlwave-user-catalog-routines idlwave-library-catalog-routines)
8160 (or (y-or-n-p
8161 "You don't have any user or library catalogs. Continue anyway? ")
15e42531
CD
8162 (error "Abort")))
8163 (let* ((routines (append idlwave-system-routines
8164 idlwave-compiled-routines
52a244eb
S
8165 idlwave-library-catalog-routines
8166 idlwave-user-catalog-routines
15e42531
CD
8167 idlwave-buffer-routines
8168 nil))
8169 (km-prop (if (featurep 'xemacs) 'keymap 'local-map))
8170 (keymap (make-sparse-keymap))
8171 (props (list 'mouse-face 'highlight
8172 km-prop keymap
52a244eb 8173 'help-echo "Mouse2: Find source"))
15e42531
CD
8174 (nroutines (length (or special-routines routines)))
8175 (step (/ nroutines 99))
8176 (n 0)
8177 (next-perc 1)
8178 (cnt 0)
8179 (idlwave-sort-prefer-buffer-info nil)
8180 routine twins dtwins twin done props1 lroutines)
8181
8182 (if special-routines
8183 ;; Just looking for shadows of a few special routines
8184 (setq lroutines routines
8185 routines special-routines))
8186
8187 (message "Sorting routines...")
8188 (setq routines (sort routines
8189 (lambda (a b)
8190 (string< (downcase (idlwave-make-full-name
8191 (nth 2 a) (car a)))
8192 (downcase (idlwave-make-full-name
8193 (nth 2 b) (car b)))))))
8194 (message "Sorting routines...done")
8195
8196 (define-key keymap (if (featurep 'xemacs) [(button2)] [(mouse-2)])
52a244eb 8197 (lambda (ev)
15e42531
CD
8198 (interactive "e")
8199 (mouse-set-point ev)
8200 (apply 'idlwave-do-find-module
8201 (get-text-property (point) 'find-args))))
8202 (define-key keymap [(return)]
52a244eb 8203 (lambda ()
15e42531
CD
8204 (interactive)
8205 (apply 'idlwave-do-find-module
8206 (get-text-property (point) 'find-args))))
8207 (message "Compiling list...( 0%%)")
8208 (save-excursion
8209 (set-buffer (get-buffer-create "*Shadows*"))
8210 (setq buffer-read-only nil)
8211 (erase-buffer)
8212 (while (setq routine (pop routines))
8213 (setq n (1+ n))
8214 (if (= (* next-perc step) n)
8215 (progn
8216 (message "Compiling list...(%2d%%)" next-perc)
8217 (setq next-perc (1+ next-perc))))
8218 ;; Get a list of all twins
8219 (setq twins (idlwave-routine-twins routine (or lroutines routines)))
8220 (if (memq routine done)
8221 (setq dtwins nil)
8222 (setq dtwins (idlwave-study-twins twins)))
5e72c6b2 8223 ;; Mark all twins as dealt with
15e42531
CD
8224 (setq done (append twins done))
8225 (when (or (> (length dtwins) 1)
52a244eb
S
8226 (> (idlwave-count-memq 'lib (nth 2 (car dtwins))) 1)
8227 (> (idlwave-count-memq 'user (nth 2 (car dtwins))) 1)
8228 (> (idlwave-count-memq 'buffer (nth 2 (car dtwins))) 1))
15e42531
CD
8229 (incf cnt)
8230 (insert (format "\n%s%s"
52a244eb
S
8231 (idlwave-make-full-name (nth 2 routine)
8232 (car routine))
15e42531
CD
8233 (if (eq (nth 1 routine) 'fun) "()" "")))
8234 (while (setq twin (pop dtwins))
8235 (setq props1 (append (list 'find-args
52a244eb
S
8236 (list (nth 0 routine)
8237 (nth 1 routine)
8238 (nth 2 routine)))
15e42531
CD
8239 props))
8240 (idlwave-insert-source-location "\n - " twin props1))))
8241 (goto-char (point-min))
8242 (setq buffer-read-only t))
8243 (setq loc (or loc ""))
8244 (if (> cnt 0)
8245 (progn
8246 (display-buffer (get-buffer "*Shadows*"))
8247 (message "%d case%s of shadowing found %s"
8248 cnt (if (= cnt 1) "" "s") loc))
8249 (message "No shadowing conflicts found %s" loc))))
8250
8251(defun idlwave-print-source (routine)
8252 (let* ((source (nth 3 routine))
8253 (stype (car source))
52a244eb
S
8254 (sfile (idlwave-routine-source-file source)))
8255 (if (idlwave-syslib-p sfile) (setq stype 'syslib))
15e42531
CD
8256 (if (and (eq stype 'compiled)
8257 (or (not (stringp sfile))
8258 (not (string-match "\\S-" sfile))))
8259 (setq stype 'unresolved))
52a244eb 8260 (princ (format " %-10s %s\n"
15e42531
CD
8261 stype
8262 (if sfile sfile "No source code available")))))
8263
8264(defun idlwave-routine-twins (entry &optional list)
8265 "Return all twin entries of ENTRY in LIST.
8266LIST defaults to `idlwave-routines'.
8267Twin entries are those which have the same name, type, and class.
8268ENTRY will also be returned, as the first item of this list."
8269 (let* ((name (car entry))
8270 (type (nth 1 entry))
8271 (class (nth 2 entry))
8272 (candidates (idlwave-all-assq name (or list (idlwave-routines))))
8273 twins candidate)
8274 (while (setq candidate (pop candidates))
8275 (if (and (not (eq candidate entry))
8276 (eq type (nth 1 candidate))
8277 (eq class (nth 2 candidate)))
8278 (push candidate twins)))
52a244eb 8279 (if (setq candidate (idlwave-rinfo-assq name type class
15e42531
CD
8280 idlwave-unresolved-routines))
8281 (push candidate twins))
8282 (cons entry (nreverse twins))))
8283
8284(defun idlwave-study-twins (entries)
52a244eb
S
8285 "Return dangerous twins of first entry in ENTRIES.
8286Dangerous twins are routines with same name, but in different files on
8287the load path. If a file is in the system library and has an entry in
8288the `idlwave-system-routines' list, we omit the latter as
8289non-dangerous because many IDL routines are implemented as library
8290routines, and may have been scanned."
15e42531 8291 (let* ((entry (car entries))
52a244eb 8292 (name (car entry)) ;
15e42531
CD
8293 (type (nth 1 entry)) ; Must be bound for
8294 (class (nth 2 entry)) ; idlwave-routine-twin-compare
8295 (cnt 0)
52a244eb 8296 source type type-cons file alist syslibp key)
15e42531
CD
8297 (while (setq entry (pop entries))
8298 (incf cnt)
8299 (setq source (nth 3 entry)
8300 type (car source)
52a244eb
S
8301 type-cons (cons type (nth 3 source))
8302 file (idlwave-routine-source-file source))
8303
15e42531
CD
8304 ;; Make KEY to index entry properly
8305 (setq key (cond ((eq type 'system) type)
8306 (file (file-truename file))
8307 (t 'unresolved)))
52a244eb
S
8308
8309 ;; Check for an entry in the system library
8310 (if (and file
15e42531
CD
8311 (not syslibp)
8312 (idlwave-syslib-p file))
15e42531 8313 (setq syslibp t))
52a244eb
S
8314
8315 ;; If there's more than one matching entry for the same file, just
8316 ;; append the type-cons to the type list.
15e42531 8317 (if (setq entry (assoc key alist))
52a244eb
S
8318 (push type-cons (nth 2 entry))
8319 (push (list key file (list type-cons)) alist)))
8320
15e42531 8321 (setq alist (nreverse alist))
52a244eb 8322
15e42531 8323 (when syslibp
52a244eb
S
8324 ;; File is in system *library* - remove any 'system entry
8325 (setq alist (delq (assq 'system alist) alist)))
8326
8327 ;; If 'system remains and we've scanned the syslib, it's a builtin
8328 ;; (rather than a !DIR/lib/.pro file bundled as source).
15e42531
CD
8329 (when (and (idlwave-syslib-scanned-p)
8330 (setq entry (assoc 'system alist)))
8331 (setcar entry 'builtin))
8332 (sort alist 'idlwave-routine-twin-compare)))
8333
8334(defvar name)
8335(defvar type)
8336(defvar class)
8337(defvar idlwave-sort-prefer-buffer-info t
8338 "Internal variable used to influence `idlwave-routine-twin-compare'.")
8339
8340(defmacro idlwave-xor (a b)
8341 `(and (or ,a ,b)
8342 (not (and ,a ,b))))
8343
8344(defun idlwave-routine-entry-compare (a b)
8345 "Compare two routine info entries for sortiung. This is the general case.
8346It first compates class, names, and type. If it turns out that A and B
cef6cafe 8347are twins (same name, class, and type), calls another routine which
15e42531
CD
8348compares twins on the basis of their file names and path locations."
8349 (let ((name (car a)) (type (nth 1 a)) (class (nth 2 a)))
8350 (cond
8351 ((not (equal (idlwave-downcase-safe class)
8352 (idlwave-downcase-safe (nth 2 b))))
8353 ;; Class decides
8354 (cond ((null (nth 2 b)) nil)
8355 ((null class) t)
8356 (t (string< (downcase class) (downcase (nth 2 b))))))
8357 ((not (equal (downcase name) (downcase (car b))))
8358 ;; Name decides
8359 (string< (downcase name) (downcase (car b))))
8360 ((not (eq type (nth 1 b)))
8361 ;; Type decides
8362 (< (if (eq type 'fun) 1 0) (if (eq (nth 1 b) 'fun) 1 0)))
52a244eb 8363 (t
15e42531
CD
8364 ;; A and B are twins - so the decision is more complicated.
8365 ;; Call twin-compare with the proper arguments.
8366 (idlwave-routine-entry-compare-twins a b)))))
8367
8368(defun idlwave-routine-entry-compare-twins (a b)
52a244eb
S
8369 "Compare two routine entries, under the assumption that they are
8370twins. This basically calls `idlwave-routine-twin-compare' with the
8371correct args."
8372 (let* ((name (car a)) (type (nth 1 a)) (class (nth 2 a)) ; needed outside
8373 (asrc (nth 3 a))
8374 (atype (car asrc))
8375 (bsrc (nth 3 b))
8376 (btype (car bsrc))
8377 (afile (idlwave-routine-source-file asrc))
8378 (bfile (idlwave-routine-source-file bsrc)))
15e42531
CD
8379 (idlwave-routine-twin-compare
8380 (if (stringp afile)
8381 (list (file-truename afile) afile (list atype))
8382 (list atype afile (list atype)))
8383 (if (stringp bfile)
8384 (list (file-truename bfile) bfile (list btype))
8385 (list btype bfile (list btype))))
8386 ))
8387
8388(defun idlwave-routine-twin-compare (a b)
8389 "Compare two routine twin entries for sorting.
8390In here, A and B are not normal routine info entries, but special
8391lists (KEY FILENAME (TYPES...)).
8392This expects NAME TYPE CLASS to be bound to the right values."
8393 (let* (;; Dis-assemble entries
8394 (akey (car a)) (bkey (car b))
8395 (afile (nth 1 a)) (bfile (nth 1 b))
8396 (atypes (nth 2 a)) (btypes (nth 2 b))
8397 ;; System routines?
8398 (asysp (memq akey '(builtin system)))
8399 (bsysp (memq bkey '(builtin system)))
8400 ;; Compiled routines?
8401 (acompp (memq 'compiled atypes))
8402 (bcompp (memq 'compiled btypes))
8403 ;; Unresolved?
8404 (aunresp (or (eq akey 'unresolved)
8405 (and acompp (not afile))))
8406 (bunresp (or (eq bkey 'unresolved)
8407 (and bcompp (not bfile))))
8408 ;; Buffer info available?
8409 (abufp (memq 'buffer atypes))
8410 (bbufp (memq 'buffer btypes))
8411 ;; On search path?
8412 (tpath-alist (idlwave-true-path-alist))
52a244eb
S
8413 (apathp (and (stringp akey)
8414 (assoc (file-name-directory akey) tpath-alist)))
8415 (bpathp (and (stringp bkey)
8416 (assoc (file-name-directory bkey) tpath-alist)))
15e42531
CD
8417 ;; How early on search path? High number means early since we
8418 ;; measure the tail of the path list
8419 (anpath (length (memq apathp tpath-alist)))
8420 (bnpath (length (memq bpathp tpath-alist)))
8421 ;; Look at file names
8422 (aname (if (stringp afile) (downcase (file-name-nondirectory afile)) ""))
8423 (bname (if (stringp bfile) (downcase (file-name-nondirectory bfile)) ""))
8424 (fname-re (if class (format "\\`%s__\\(%s\\|define\\)\\.pro\\'"
8425 (regexp-quote (downcase class))
8426 (regexp-quote (downcase name)))
8427 (format "\\`%s\\.pro" (regexp-quote (downcase name)))))
8428 ;; Is file name derived from the routine name?
8429 ;; Method file or class definition file?
8430 (anamep (string-match fname-re aname))
8431 (adefp (and class anamep (string= "define" (match-string 1 aname))))
8432 (bnamep (string-match fname-re bname))
8433 (bdefp (and class bnamep (string= "define" (match-string 1 bname)))))
8434
8435 ;; Now: follow JD's ideas about sorting. Looks really simple now,
8436 ;; doesn't it? The difficult stuff is hidden above...
8437 (cond
8438 ((idlwave-xor asysp bsysp) asysp) ; System entries first
8439 ((idlwave-xor aunresp bunresp) bunresp) ; Unresolved last
8440 ((and idlwave-sort-prefer-buffer-info
8441 (idlwave-xor abufp bbufp)) abufp) ; Buffers before non-buffers
8442 ((idlwave-xor acompp bcompp) acompp) ; Compiled entries
8443 ((idlwave-xor apathp bpathp) apathp) ; Library before non-library
8444 ((idlwave-xor anamep bnamep) anamep) ; Correct file names first
8445 ((and class anamep bnamep ; both file names match ->
8446 (idlwave-xor adefp bdefp)) bdefp) ; __define after __method
8447 ((> anpath bnpath) t) ; Who is first on path?
8448 (t nil)))) ; Default
8449
52a244eb
S
8450(defun idlwave-routine-source-file (source)
8451 (if (nth 2 source)
8452 (expand-file-name (nth 1 source) (nth 2 source))
8453 (nth 1 source)))
8454
15e42531
CD
8455(defun idlwave-downcase-safe (string)
8456 "Donwcase if string, else return unchanged."
8457 (if (stringp string)
8458 (downcase string)
8459 string))
8460
8461(defun idlwave-count-eq (elt list)
8462 "How often is ELT in LIST?"
8463 (length (delq nil (mapcar (lambda (x) (eq x elt)) list))))
8464
52a244eb
S
8465(defun idlwave-count-memq (elt alist)
8466 "How often is ELT a key in ALIST?"
8467 (length (delq nil (mapcar (lambda (x) (eq (car x) elt)) alist))))
8468
15e42531 8469(defun idlwave-syslib-p (file)
52a244eb 8470 "Non-nil if FILE is in the system library."
15e42531
CD
8471 (let* ((true-syslib (file-name-as-directory
8472 (file-truename
8473 (expand-file-name "lib" (idlwave-sys-dir)))))
8474 (true-file (file-truename file)))
8475 (string-match (concat "^" (regexp-quote true-syslib)) true-file)))
8476
8477(defun idlwave-lib-p (file)
8478 "Non-nil if file is in the library"
8479 (let ((true-dir (file-name-directory (file-truename file))))
8480 (assoc true-dir (idlwave-true-path-alist))))
8481
52a244eb
S
8482(defun idlwave-path-alist-add-flag (list-entry flag)
8483 "Add a flag to the path list entry, if not set."
8484 (let ((flags (cdr list-entry)))
8485 (add-to-list 'flags flag)
8486 (setcdr list-entry flags)))
8487
8488(defun idlwave-path-alist-remove-flag (list-entry flag)
8489 "Remove a flag to the path list entry, if set."
8490 (let ((flags (delq flag (cdr list-entry))))
8491 (setcdr list-entry flags)))
8492
15e42531
CD
8493(defun idlwave-true-path-alist ()
8494 "Return `idlwave-path-alist' alist with true-names.
52a244eb 8495Info is cached, but relies on the functions setting `idlwave-path-alist'
15e42531
CD
8496to reset the variable `idlwave-true-path-alist' to nil."
8497 (or idlwave-true-path-alist
8498 (setq idlwave-true-path-alist
8499 (mapcar (lambda(x) (cons
8500 (file-name-as-directory
8501 (file-truename
8502 (directory-file-name
8503 (car x))))
8504 (cdr x)))
8505 idlwave-path-alist))))
8506
8507(defun idlwave-syslib-scanned-p ()
8508 "Non-nil if the system lib file !DIR/lib has been scanned."
8509 (let* ((true-syslib (file-name-as-directory
8510 (file-truename
8511 (expand-file-name "lib" (idlwave-sys-dir))))))
8512 (cdr (assoc true-syslib (idlwave-true-path-alist)))))
8513
8514;; ----------------------------------------------------------------------------
8515;;
8516;; Online Help display
8517
f32b3b91
CD
8518
8519;; ----------------------------------------------------------------------------
8520;;
8521;; Additions for use with imenu.el and func-menu.el
8522;; (pop-up a list of IDL units in the current file).
8523;;
8524
8525(defun idlwave-prev-index-position ()
8526 "Search for the previous procedure or function.
8527Return nil if not found. For use with imenu.el."
8528 (save-match-data
8529 (cond
8530 ((idlwave-find-key "\\<\\(pro\\|function\\)\\>" -1 'nomark))
8531 ;; ((idlwave-find-key idlwave-begin-unit-reg 1 'nomark)
8532 (t nil))))
8533
8534(defun idlwave-unit-name ()
8535 "Return the unit name.
8536Assumes that point is at the beginning of the unit as found by
8537`idlwave-prev-index-position'."
8538 (forward-sexp 2)
8539 (forward-sexp -1)
8540 (let ((begin (point)))
52a244eb
S
8541 (re-search-forward
8542 "[a-zA-Z_][a-zA-Z0-9$_]+\\(::[a-zA-Z_][a-zA-Z0-9$_]+\\)?")
f32b3b91
CD
8543 (if (fboundp 'buffer-substring-no-properties)
8544 (buffer-substring-no-properties begin (point))
8545 (buffer-substring begin (point)))))
8546
facebc7b
S
8547(defalias 'idlwave-function-menu
8548 (condition-case nil
f32b3b91
CD
8549 (progn
8550 (require 'func-menu)
facebc7b
S
8551 'function-menu)
8552 (error (condition-case nil
8553 (progn
8554 (require 'imenu)
8555 'imenu)
8556 (error nil)))))
f32b3b91 8557
52a244eb 8558;; Here we hack func-menu.el in order to support this new mode.
f32b3b91
CD
8559;; The latest versions of func-menu.el already have this stuff in, so
8560;; we hack only if it is not already there.
8561(when (fboundp 'eval-after-load)
8562 (eval-after-load "func-menu"
8563 '(progn
8564 (or (assq 'idlwave-mode fume-function-name-regexp-alist)
8565 (not (boundp 'fume-function-name-regexp-idl)) ; avoid problems
8566 (setq fume-function-name-regexp-alist
8567 (cons '(idlwave-mode . fume-function-name-regexp-idl)
8568 fume-function-name-regexp-alist)))
8569 (or (assq 'idlwave-mode fume-find-function-name-method-alist)
8570 (not (fboundp 'fume-find-next-idl-function-name)) ; avoid problems
8571 (setq fume-find-function-name-method-alist
8572 (cons '(idlwave-mode . fume-find-next-idl-function-name)
8573 fume-find-function-name-method-alist))))))
8574
8575(defun idlwave-edit-in-idlde ()
8576 "Edit the current file in IDL Development environment."
8577 (interactive)
8578 (start-process "idldeclient" nil
8579 idlwave-shell-explicit-file-name "-c" "-e"
8580 (buffer-file-name) "&"))
52a244eb 8581
f32b3b91
CD
8582(defun idlwave-launch-idlhelp ()
8583 "Start the IDLhelp application."
8584 (interactive)
8585 (start-process "idlhelp" nil idlwave-help-application))
52a244eb 8586
f32b3b91
CD
8587;; Menus - using easymenu.el
8588(defvar idlwave-mode-menu-def
8589 `("IDLWAVE"
8590 ["PRO/FUNC menu" idlwave-function-menu t]
8591 ("Motion"
8592 ["Subprogram Start" idlwave-beginning-of-subprogram t]
8593 ["Subprogram End" idlwave-end-of-subprogram t]
8594 ["Block Start" idlwave-beginning-of-block t]
8595 ["Block End" idlwave-end-of-block t]
8596 ["Up Block" idlwave-backward-up-block t]
8597 ["Down Block" idlwave-down-block t]
8598 ["Skip Block Backward" idlwave-backward-block t]
8599 ["Skip Block Forward" idlwave-forward-block t])
8600 ("Mark"
8601 ["Subprogram" idlwave-mark-subprogram t]
8602 ["Block" idlwave-mark-block t]
8603 ["Header" idlwave-mark-doclib t])
8604 ("Format"
8605 ["Indent Subprogram" idlwave-indent-subprogram t]
8606 ["(Un)Comment Region" idlwave-toggle-comment-region "C-c ;"]
8607 ["Continue/Split line" idlwave-split-line t]
8608 "--"
8609 ["Toggle Auto Fill" idlwave-auto-fill-mode :style toggle
8610 :selected (symbol-value idlwave-fill-function)])
8611 ("Templates"
8612 ["Procedure" idlwave-procedure t]
8613 ["Function" idlwave-function t]
8614 ["Doc Header" idlwave-doc-header t]
8615 ["Log" idlwave-doc-modification t]
8616 "--"
8617 ["Case" idlwave-case t]
8618 ["For" idlwave-for t]
8619 ["Repeat" idlwave-repeat t]
8620 ["While" idlwave-while t]
8621 "--"
8622 ["Close Block" idlwave-close-block t])
15e42531 8623 ("Completion"
f32b3b91
CD
8624 ["Complete" idlwave-complete t]
8625 ("Complete Special"
8626 ["1 Procedure Name" (idlwave-complete 'procedure) t]
8627 ["2 Procedure Keyword" (idlwave-complete 'procedure-keyword) t]
8628 "--"
8629 ["3 Function Name" (idlwave-complete 'function) t]
8630 ["4 Function Keyword" (idlwave-complete 'function-keyword) t]
8631 "--"
8632 ["5 Procedure Method Name" (idlwave-complete 'procedure-method) t]
8633 ["6 Procedure Method Keyword" (idlwave-complete 'procedure-method-keyword) t]
8634 "--"
8635 ["7 Function Method Name" (idlwave-complete 'function-method) t]
8636 ["8 Function Method Keyword" (idlwave-complete 'function-method-keyword) t]
8637 "--"
15e42531
CD
8638 ["9 Class Name" idlwave-complete-class t]))
8639 ("Routine Info"
f32b3b91 8640 ["Show Routine Info" idlwave-routine-info t]
52a244eb 8641 ["Online Context Help" idlwave-context-help t]
f32b3b91
CD
8642 "--"
8643 ["Find Routine Source" idlwave-find-module t]
15e42531 8644 ["Resolve Routine" idlwave-resolve (featurep 'idlw-shell)]
f32b3b91
CD
8645 "--"
8646 ["Update Routine Info" idlwave-update-routine-info t]
8647 "--"
52a244eb
S
8648 "IDL User Catalog"
8649 ["Select Catalog Directories" (idlwave-create-user-catalog-file nil) t]
15e42531 8650 ["Scan Directories" (idlwave-update-routine-info '(16))
5e72c6b2
S
8651 (and idlwave-path-alist (not idlwave-catalog-process))]
8652 ["Scan Directories &" (idlwave-update-routine-info '(64))
8653 (and idlwave-path-alist (not idlwave-catalog-process))]
15e42531
CD
8654 "--"
8655 "Routine Shadows"
8656 ["Check Current Buffer" idlwave-list-buffer-load-path-shadows t]
8657 ["Check Compiled Routines" idlwave-list-shell-load-path-shadows t]
8658 ["Check Everything" idlwave-list-all-load-path-shadows t])
8659 ("Misc"
8660 ["Kill auto-created buffers" idlwave-kill-autoloaded-buffers t]
8661 "--"
8662 ["Insert TAB character" idlwave-hard-tab t])
f32b3b91
CD
8663 "--"
8664 ("External"
8665 ["Generate IDL tags" idlwave-make-tags t]
8666 ["Start IDL shell" idlwave-shell t]
8667 ["Edit file in IDLDE" idlwave-edit-in-idlde t]
8668 ["Launch IDL Help" idlwave-launch-idlhelp t])
8669 "--"
8670 ("Customize"
8671 ["Browse IDLWAVE Group" idlwave-customize t]
8672 "--"
52a244eb 8673 ["Build Full Customize Menu" idlwave-create-customize-menu
f32b3b91
CD
8674 (fboundp 'customize-menu-create)])
8675 ("Documentation"
8676 ["Describe Mode" describe-mode t]
8677 ["Abbreviation List" idlwave-list-abbrevs t]
8678 "--"
8679 ["Commentary in idlwave.el" idlwave-show-commentary t]
595ab50b 8680 ["Commentary in idlw-shell.el" idlwave-shell-show-commentary t]
f32b3b91
CD
8681 "--"
8682 ["Info" idlwave-info t]
8683 "--"
8684 ["Launch IDL Help" idlwave-launch-idlhelp t])))
8685
8686(defvar idlwave-mode-debug-menu-def
8687 '("Debug"
8688 ["Start IDL shell" idlwave-shell t]
8689 ["Save and .RUN buffer" idlwave-shell-save-and-run
52a244eb 8690 (and (boundp 'idlwave-shell-automatic-start)
f32b3b91
CD
8691 idlwave-shell-automatic-start)]))
8692
8693(if (or (featurep 'easymenu) (load "easymenu" t))
8694 (progn
52a244eb
S
8695 (easy-menu-define idlwave-mode-menu idlwave-mode-map
8696 "IDL and WAVE CL editing menu"
f32b3b91 8697 idlwave-mode-menu-def)
52a244eb
S
8698 (easy-menu-define idlwave-mode-debug-menu idlwave-mode-map
8699 "IDL and WAVE CL editing menu"
f32b3b91
CD
8700 idlwave-mode-debug-menu-def)))
8701
8702(defun idlwave-customize ()
8703 "Call the customize function with idlwave as argument."
8704 (interactive)
52a244eb 8705 ;; Try to load the code for the shell, so that we can customize it
f32b3b91 8706 ;; as well.
22d5821d
CD
8707 (or (featurep 'idlw-shell)
8708 (load "idlw-shell" t))
f32b3b91
CD
8709 (customize-browse 'idlwave))
8710
8711(defun idlwave-create-customize-menu ()
8712 "Create a full customization menu for IDLWAVE, insert it into the menu."
8713 (interactive)
8714 (if (fboundp 'customize-menu-create)
8715 (progn
52a244eb 8716 ;; Try to load the code for the shell, so that we can customize it
f32b3b91 8717 ;; as well.
22d5821d
CD
8718 (or (featurep 'idlw-shell)
8719 (load "idlw-shell" t))
52a244eb 8720 (easy-menu-change
f32b3b91
CD
8721 '("IDLWAVE") "Customize"
8722 `(["Browse IDLWAVE group" idlwave-customize t]
8723 "--"
8724 ,(customize-menu-create 'idlwave)
8725 ["Set" Custom-set t]
8726 ["Save" Custom-save t]
8727 ["Reset to Current" Custom-reset-current t]
8728 ["Reset to Saved" Custom-reset-saved t]
8729 ["Reset to Standard Settings" Custom-reset-standard t]))
8730 (message "\"IDLWAVE\"-menu now contains full customization menu"))
8731 (error "Cannot expand menu (outdated version of cus-edit.el)")))
8732
8733(defun idlwave-show-commentary ()
8734 "Use the finder to view the file documentation from `idlwave.el'."
8735 (interactive)
8736 (require 'finder)
8737 (finder-commentary "idlwave.el"))
8738
8739(defun idlwave-shell-show-commentary ()
595ab50b 8740 "Use the finder to view the file documentation from `idlw-shell.el'."
f32b3b91
CD
8741 (interactive)
8742 (require 'finder)
595ab50b 8743 (finder-commentary "idlw-shell.el"))
f32b3b91
CD
8744
8745(defun idlwave-info ()
8746 "Read documentation for IDLWAVE in the info system."
8747 (interactive)
8748 (require 'info)
8749 (Info-goto-node "(idlwave)"))
8750
8751(defun idlwave-list-abbrevs (arg)
8752 "Show the code abbreviations define in IDLWAVE mode.
8753This lists all abbrevs where the replacement text differs from the input text.
8754These are the ones the users want to learn to speed up their writing.
8755
8756The function does *not* list abbrevs which replace a word with itself
8757to call a hook. These hooks are used to change the case of words or
8758to blink the matching `begin', and the user does not need to know them.
8759
8760With arg, list all abbrevs with the corresponding hook.
8761
8762This function was written since `list-abbrevs' looks terrible for IDLWAVE mode."
8763
8764 (interactive "P")
8765 (let ((table (symbol-value 'idlwave-mode-abbrev-table))
8766 abbrevs
8767 str rpl func fmt (len-str 0) (len-rpl 0))
52a244eb 8768 (mapatoms
f32b3b91
CD
8769 (lambda (sym)
8770 (if (symbol-value sym)
8771 (progn
8772 (setq str (symbol-name sym)
8773 rpl (symbol-value sym)
8774 func (symbol-function sym))
8775 (if arg
8776 (setq func (prin1-to-string func))
8777 (if (and (listp func) (stringp (nth 2 func)))
8778 (setq rpl (concat "EVAL: " (nth 2 func))
8779 func "")
8780 (setq func "")))
8781 (if (or arg (not (string= rpl str)))
8782 (progn
8783 (setq len-str (max len-str (length str)))
8784 (setq len-rpl (max len-rpl (length rpl)))
8785 (setq abbrevs (cons (list str rpl func) abbrevs)))))))
8786 table)
8787 ;; sort the list
8788 (setq abbrevs (sort abbrevs (lambda (a b) (string< (car a) (car b)))))
8789 ;; Make the format
8790 (setq fmt (format "%%-%ds %%-%ds %%s\n" len-str len-rpl))
8791 (with-output-to-temp-buffer "*Help*"
8792 (if arg
8793 (progn
52a244eb 8794 (princ "Abbreviations and Actions in IDLWAVE-Mode\n")
f32b3b91
CD
8795 (princ "=========================================\n\n")
8796 (princ (format fmt "KEY" "REPLACE" "HOOK"))
8797 (princ (format fmt "---" "-------" "----")))
8798 (princ "Code Abbreviations and Templates in IDLWAVE-Mode\n")
8799 (princ "================================================\n\n")
8800 (princ (format fmt "KEY" "ACTION" ""))
8801 (princ (format fmt "---" "------" "")))
8802 (mapcar
8803 (lambda (list)
8804 (setq str (car list)
8805 rpl (nth 1 list)
8806 func (nth 2 list))
8807 (princ (format fmt str rpl func)))
8808 abbrevs)))
8809 ;; Make sure each abbreviation uses only one display line
8810 (save-excursion
8811 (set-buffer "*Help*")
8812 (setq truncate-lines t)))
8813
5e72c6b2
S
8814;; Add .pro files to speedbar for support, if it's loaded
8815(eval-after-load "speedbar" '(speedbar-add-supported-extension ".pro"))
8816
5e72c6b2
S
8817;; Set an idle timer to load the routine info.
8818;; Will only work on systems which support this.
8819(or idlwave-routines (idlwave-start-load-rinfo-timer))
8820
52a244eb
S
8821;;;###autoload(add-to-list 'auto-mode-alist '("\\.[Pp][Rr][Oo]\\'" . idlwave-mode))
8822
15e42531 8823;; Run the hook
f32b3b91
CD
8824(run-hooks 'idlwave-load-hook)
8825
8826(provide 'idlwave)
8827
8828;;; idlwave.el ends here