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