]> code.delx.au - gnu-emacs/blob - lisp/progmodes/idlwave.el
Merge from emacs--devo--0
[gnu-emacs] / lisp / progmodes / idlwave.el
1 ;; idlwave.el --- IDL editing mode for GNU Emacs
2
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 ;; Free Software Foundation, Inc.
5
6 ;; Authors: J.D. Smith <jdsmith@as.arizona.edu>
7 ;; Carsten Dominik <dominik@science.uva.nl>
8 ;; Chris Chase <chase@att.com>
9 ;; Maintainer: J.D. Smith <jdsmith@as.arizona.edu>
10 ;; Version: 6.1_em22
11 ;; Keywords: languages
12
13 ;; This file is part of GNU Emacs.
14
15 ;; GNU Emacs is free software; you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; any later version.
19
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
24
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs; see the file COPYING. If not, write to the
27 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
28 ;; Boston, MA 02110-1301, USA.
29
30 ;;; Commentary:
31
32 ;; IDLWAVE enables feature-rich development and interaction with IDL,
33 ;; the Interactive Data Language. It provides a compelling,
34 ;; full-featured alternative to the IDLDE development environment
35 ;; bundled with IDL.
36
37 ;; In the remotely distant past, based on pascal.el, though bears
38 ;; little resemblance to it now.
39 ;;
40 ;; Incorporates many ideas, such as abbrevs, action routines, and
41 ;; continuation line indenting, from wave.el.
42 ;; wave.el original written by Lubos Pochman, Precision Visuals, Boulder.
43 ;;
44 ;; See the mode description ("C-h m" in idlwave-mode or "C-h f idlwave-mode")
45 ;; for features, key bindings, and info.
46 ;; Also, Info format documentation is available with `M-x idlwave-info'
47 ;;
48 ;; New versions of IDLWAVE, documentation, and more information
49 ;; available from:
50 ;; http://idlwave.org
51 ;;
52 ;; INSTALLATION
53 ;; ============
54 ;;
55 ;; Follow the instructions in the INSTALL file of the distribution.
56 ;; In short, put this file on your load path and add the following
57 ;; lines to your .emacs file:
58 ;;
59 ;; (autoload 'idlwave-mode "idlwave" "IDLWAVE Mode" t)
60 ;; (autoload 'idlwave-shell "idlw-shell" "IDLWAVE Shell" t)
61 ;; (setq auto-mode-alist (cons '("\\.pro\\'" . idlwave-mode) auto-mode-alist))
62 ;;
63 ;;
64 ;; SOURCE
65 ;; ======
66 ;;
67 ;; The newest version of this file is available from the maintainer's
68 ;; Webpage:
69 ;;
70 ;; http://idlwave.org
71 ;;
72 ;; DOCUMENTATION
73 ;; =============
74 ;;
75 ;; IDLWAVE is documented online in info format. A printable version
76 ;; of the documentation is available from the maintainers webpage (see
77 ;; SOURCE).
78 ;;
79 ;;
80 ;; ACKNOWLEDGMENTS
81 ;; ===============
82 ;;
83 ;; Thanks to the following people for their contributions and comments:
84 ;;
85 ;; Ulrik Dickow <dickow_at_nbi.dk>
86 ;; Eric E. Dors <edors_at_lanl.gov>
87 ;; Stein Vidar H. Haugan <s.v.h.haugan_at_astro.uio.no>
88 ;; David Huenemoerder <dph_at_space.mit.edu>
89 ;; Kevin Ivory <Kevin.Ivory_at_linmpi.mpg.de>
90 ;; Dick Jackson <dick_at_d-jackson.com>
91 ;; Xuyong Liu <liu_at_stsci.edu>
92 ;; Simon Marshall <Simon.Marshall_at_esrin.esa.it>
93 ;; Laurent Mugnier <mugnier_at_onera.fr>
94 ;; Lubos Pochman <lubos_at_rsinc.com>
95 ;; Bob Portmann <portmann_at_al.noaa.gov>
96 ;; Patrick M. Ryan <pat_at_jaameri.gsfc.nasa.gov>
97 ;; Marty Ryba <ryba_at_ll.mit.edu>
98 ;; Paul Sorenson <aardvark62_at_msn.com>
99 ;; Phil Sterne <sterne_at_dublin.llnl.gov>
100 ;; Phil Williams <williams_at_irc.chmcc.org>
101 ;;
102 ;; CUSTOMIZATION:
103 ;; =============
104 ;;
105 ;; IDLWAVE has extensive customize support; to learn about the
106 ;; variables which control the mode's behavior, use `M-x
107 ;; idlwave-customize'.
108 ;;
109 ;; You can set your own preferred values with Customize, or with Lisp
110 ;; code in .emacs. For an example of what to put into .emacs, check
111 ;; the TexInfo documentation or see a complete .emacs available at the
112 ;; website.
113 ;;
114 ;; KNOWN PROBLEMS:
115 ;; ==============
116 ;;
117 ;; IDLWAVE support for the IDL-derived PV-WAVE CL language of Visual
118 ;; Numerics, Inc. is growing less and less complete as the two
119 ;; languages grow increasingly apart. The mode probably shouldn't
120 ;; even have "WAVE" in its title, but it's catchy, and was required
121 ;; to avoid conflict with the CORBA idl.el mode. Caveat WAVEor.
122 ;;
123 ;; Moving the point backwards in conjunction with abbrev expansion
124 ;; does not work as I would like it, but this is a problem with
125 ;; emacs abbrev expansion done by the self-insert-command. It ends
126 ;; up inserting the character that expanded the abbrev after moving
127 ;; point backward, e.g., "\cl" expanded with a space becomes
128 ;; "LONG( )" with point before the close paren. This is solved by
129 ;; using a temporary function in `post-command-hook' - not pretty,
130 ;; but it works.
131 ;;
132 ;; Tabs and spaces are treated equally as whitespace when filling a
133 ;; comment paragraph. To accomplish this, tabs are permanently
134 ;; replaced by spaces in the text surrounding the paragraph, which
135 ;; may be an undesirable side-effect. Replacing tabs with spaces is
136 ;; limited to comments only and occurs only when a comment
137 ;; paragraph is filled via `idlwave-fill-paragraph'.
138 ;;
139 ;; Muti-statement lines (using "&") on block begin and end lines can
140 ;; ruin the formatting. For example, multiple end statements on a
141 ;; line: endif & endif. Using "&" outside of block begin/end lines
142 ;; should be okay.
143 ;;
144 ;; Determining the expression at point for printing and other
145 ;; examination commands is somewhat rough: currently only fairly
146 ;; simple entities are found. You can always drag-select or examine
147 ;; a pre-selected region.
148 ;;
149 ;; When forcing completion of method keywords, the initial
150 ;; query for a method has multiple entries for some methods. Would
151 ;; be too difficult to fix this hardly used case.
152 ;;
153 \f
154 ;;; Code:
155
156
157 (eval-when-compile (require 'cl))
158 (require 'idlw-help)
159
160 ;; For XEmacs
161 (unless (fboundp 'line-beginning-position)
162 (defalias 'line-beginning-position 'point-at-bol))
163 (unless (fboundp 'line-end-position)
164 (defalias 'line-end-position 'point-at-eol))
165 (unless (fboundp 'char-valid-p)
166 (defalias 'char-valid-p 'characterp))
167 (unless (fboundp 'match-string-no-properties)
168 (defalias 'match-string-no-properties 'match-string))
169
170 (if (not (fboundp 'cancel-timer))
171 (condition-case nil
172 (require 'timer)
173 (error nil)))
174
175 (eval-and-compile
176 ;; Kludge to allow `defcustom' for Emacs 19.
177 (condition-case () (require 'custom) (error nil))
178 (if (and (featurep 'custom) (fboundp 'custom-declare-variable))
179 nil ;; We've got what we needed
180 ;; We have the old or no custom-library, hack around it!
181 (defmacro defgroup (&rest args) nil)
182 (defmacro defcustom (var value doc &rest args)
183 `(defvar ,var ,value ,doc))))
184
185 (defgroup idlwave nil
186 "Major mode for editing IDL .pro files."
187 :tag "IDLWAVE"
188 :link '(url-link :tag "Home Page"
189 "http://idlwave.org")
190 :link '(emacs-commentary-link :tag "Commentary in idlw-shell.el"
191 "idlw-shell.el")
192 :link '(emacs-commentary-link :tag "Commentary in idlwave.el" "idlwave.el")
193 :link '(custom-manual "(idlwave)Top")
194 :prefix "idlwave"
195 :group 'languages)
196
197
198 ;;; Variables for indentation behavior ---------------------------------------
199
200 (defgroup idlwave-code-formatting nil
201 "Indentation and formatting options for IDLWAVE mode."
202 :group 'idlwave)
203
204 (defcustom idlwave-main-block-indent 2
205 "*Extra indentation for the main block of code.
206 That is the block between the FUNCTION/PRO statement and the END
207 statement for that program unit."
208 :group 'idlwave-code-formatting
209 :type 'integer)
210
211 (defcustom idlwave-block-indent 3
212 "*Extra indentation applied to block lines.
213 If you change this, you probably also want to change `idlwave-end-offset'."
214 :group 'idlwave-code-formatting
215 :type 'integer)
216
217 (defcustom idlwave-end-offset -3
218 "*Extra indentation applied to block END lines.
219 A value equal to negative `idlwave-block-indent' will make END lines
220 line up with the block BEGIN lines."
221 :group 'idlwave-code-formatting
222 :type 'integer)
223
224 (defcustom idlwave-continuation-indent 3
225 "*Extra indentation applied to continuation lines.
226 This extra offset applies to the first of a set of continuation lines.
227 The following lines receive the same indentation as the first."
228 :group 'idlwave-code-formatting
229 :type 'integer)
230
231 (defcustom idlwave-max-extra-continuation-indent 40
232 "*Maximum additional indentation for special continuation indent.
233 Several special indentations are tried to help line up continuation
234 lines in routine calls or definitions, other statements with
235 parentheses, or assignment statements. This variable specifies a
236 maximum amount by which this special indentation can exceed the
237 standard continuation indentation, otherwise defaulting to a fixed
238 offset. Set to 0 to effectively disable all special continuation
239 indentation, or to a large number (like 100) to enable it in all
240 cases. See also `idlwave-indent-to-open-paren', which can override
241 this variable."
242 :group 'idlwave-code-formatting
243 :type 'integer)
244
245 (defcustom idlwave-indent-to-open-paren t
246 "*Non-nil means, indent continuation lines to innermost open
247 parenthesis. This indentation occurs even if otherwise disallowed by
248 `idlwave-max-extra-continuation-indent'. Matching parens and the
249 interleaving args are lined up. Example:
250
251 x = function_a(function_b(function_c( a, b, [1,2,3, $
252 4,5,6 $
253 ], $
254 c, d $
255 )))
256
257 When this variable is nil, paren alignment may still occur, based on
258 the value of `max-extra-continuation-indent', which, if zero, would
259 yield:
260
261 x = function_a(function_b(function_c( a, b, [1,2,3, $
262 4,5,6 $
263 ], $
264 c, d $
265 )))"
266 :group 'idlwave-code-formatting
267 :type 'boolean)
268
269 (defcustom idlwave-indent-parens-nested nil
270 "*Non-nil means, indent continuation lines with parens by nesting
271 lines at consecutively deeper levels."
272 :group 'idlwave-code-formatting
273 :type 'boolean)
274
275
276 (defcustom idlwave-hanging-indent t
277 "*If set non-nil then comment paragraphs are indented under the
278 hanging indent given by `idlwave-hang-indent-regexp' match in the first line
279 of the paragraph."
280 :group 'idlwave-code-formatting
281 :type 'boolean)
282
283 (defcustom idlwave-hang-indent-regexp "- "
284 "*Regular expression matching the position of the hanging indent
285 in the first line of a comment paragraph. The size of the indent
286 extends to the end of the match for the regular expression."
287 :group 'idlwave-code-formatting
288 :type 'regexp)
289
290 (defcustom idlwave-use-last-hang-indent nil
291 "*If non-nil then use last match on line for `idlwave-indent-regexp'."
292 :group 'idlwave-code-formatting
293 :type 'boolean)
294
295 (defcustom idlwave-fill-comment-line-only t
296 "*If non-nil then auto fill will only operate on comment lines."
297 :group 'idlwave-code-formatting
298 :type 'boolean)
299
300 (defcustom idlwave-auto-fill-split-string t
301 "*If non-nil then auto fill will split strings with the IDL `+' operator.
302 When the line end falls within a string, string concatenation with the
303 '+' operator will be used to distribute a long string over lines.
304 If nil and a string is split then a terminal beep and warning are issued.
305
306 This variable is ignored when `idlwave-fill-comment-line-only' is
307 non-nil, since in this case code is not auto-filled."
308 :group 'idlwave-code-formatting
309 :type 'boolean)
310
311 (defcustom idlwave-split-line-string t
312 "*If non-nil then `idlwave-split-line' will split strings with `+'.
313 When the splitting point of a line falls inside a string, split the string
314 using the `+' string concatenation operator. If nil and a string is
315 split then a terminal beep and warning are issued."
316 :group 'idlwave-code-formatting
317 :type 'boolean)
318
319 (defcustom idlwave-no-change-comment ";;;"
320 "*The indentation of a comment that starts with this regular
321 expression will not be changed. Note that the indentation of a comment
322 at the beginning of a line is never changed."
323 :group 'idlwave-code-formatting
324 :type 'string)
325
326 (defcustom idlwave-begin-line-comment nil
327 "*A comment anchored at the beginning of line.
328 A comment matching this regular expression will not have its
329 indentation changed. If nil the default is \"^;\", i.e., any line
330 beginning with a \";\". Expressions for comments at the beginning of
331 the line should begin with \"^\"."
332 :group 'idlwave-code-formatting
333 :type '(choice (const :tag "Any line beginning with `;'" nil)
334 'regexp))
335
336 (defcustom idlwave-code-comment ";;[^;]"
337 "*A comment that starts with this regular expression on a line by
338 itself is indented as if it is a part of IDL code. As a result if
339 the comment is not preceded by whitespace it is unchanged."
340 :group 'idlwave-code-formatting
341 :type 'regexp)
342
343 ;; Comments not matching any of the above will be indented as a
344 ;; right-margin comment, i.e., to a minimum of `comment-column'.
345
346 ;;; Routine Info and Completion ---------------------------------------
347
348 (defgroup idlwave-routine-info nil
349 "Routine Info options for IDLWAVE mode."
350 :group 'idlwave)
351
352 (defcustom idlwave-use-library-catalogs t
353 "*Non-nil means search the IDL path for library catalog files.
354
355 These files, named .idlwave_catalog, document routine information for
356 individual directories and libraries of IDL .pro files. Many popular
357 libraries come with catalog files by default, so leaving this on is a
358 usually a good idea.."
359 :group 'idlwave-routine-info
360 :type 'boolean)
361
362 (defcustom idlwave-init-rinfo-when-idle-after 10
363 "*Seconds of idle time before routine info is automatically
364 initialized. Initializing the routine info can take a long time, in
365 particular if a large number of library catalogs are involved. When
366 Emacs is idle for more than the number of seconds specified by this
367 variable, it starts the initialization. The process is split into
368 five steps, in order to keep work interruption as short as possible.
369 If one of the steps finishes, and no user input has arrived in the
370 mean time, initialization proceeds immediately to the next step. A
371 good value for this variable is about 1/3 of the time initialization
372 take in your setup. So if you have a fast machine and no problems
373 with a slow network connection, don't hesitate to set this to 2
374 seconds. A Value of 0 means, don't initialize automatically, but
375 instead wait until routine information is needed, and initialize
376 then."
377 :group 'idlwave-routine-info
378 :type 'number)
379
380 (defcustom idlwave-scan-all-buffers-for-routine-info t
381 "*Non-nil means, scan buffers for IDL programs when updating info.
382 The scanning is done by the command `idlwave-update-routine-info'.
383 The following values are allowed:
384
385 nil Don't scan any buffers.
386 t Scan all idlwave-mode buffers in the current editing session.
387 current Scan only the current buffer, but no other buffers."
388 :group 'idlwave-routine-info
389 :type '(choice
390 (const :tag "No buffer" nil)
391 (const :tag "All buffers" t)
392 (const :tag "Current buffer only" 'current)))
393
394 (defcustom idlwave-query-shell-for-routine-info t
395 "*Non-nil means query the shell for info about compiled routines.
396 Querying the shell is useful to get information about compiled modules,
397 and it is turned on by default. However, when you have a complete library
398 scan, this is not necessary."
399 :group 'idlwave-routine-info
400 :type 'boolean)
401
402 (defcustom idlwave-auto-routine-info-updates
403 '(find-file save-buffer kill-buffer compile-buffer)
404 "*Controls under what circumstances routine info is updated automatically.
405 Possible values:
406 nil Never
407 t All available
408 \(...) A list of circumstances. Allowed members are:
409 find-file Add info for new IDLWAVE buffers.
410 save-buffer Update buffer info when buffer is saved
411 kill-buffer Remove buffer info when buffer gets killed
412 compile-buffer Update shell info after `idlwave-shell-save-and...'"
413 :group 'idlwave-routine-info
414 :type '(choice
415 (const :tag "Never" nil)
416 (const :tag "As often as possible" t)
417 (set :tag "Checklist" :greedy t
418 (const :tag "When visiting a file" find-file)
419 (const :tag "When saving a buffer" save-buffer)
420 (const :tag "After a buffer was killed" kill-buffer)
421 (const :tag "After a buffer was compiled successfully, update shell info" compile-buffer))))
422
423 (defcustom idlwave-rinfo-max-source-lines 5
424 "*Maximum number of source files displayed in the Routine Info window.
425 When an integer, it is the maximum number of source files displayed.
426 t means to show all source files."
427 :group 'idlwave-routine-info
428 :type 'integer)
429
430 (defcustom idlwave-library-path nil
431 "Library path for Windows and MacOS (OS9). Not needed under UNIX.
432 When selecting the directories to scan for IDL user catalog routine
433 info, IDLWAVE can, under UNIX, query the shell for the exact search
434 path \(the value of !PATH). However, under Windows and MacOS
435 \(pre-OSX), the IDLWAVE shell does not work. In this case, this
436 variable can be set to specify the paths where IDLWAVE can find PRO
437 files. The shell will only be asked for a list of paths when this
438 variable is nil. The value is a list of directories. A directory
439 preceeded by a `+' will be searched recursively. If you set this
440 variable on a UNIX system, the shell will not be queried. See also
441 `idlwave-system-directory'."
442 :group 'idlwave-routine-info
443 :type '(repeat (directory)))
444
445 (defcustom idlwave-system-directory ""
446 "The IDL system directory for Windows and MacOS. Not needed under
447 UNIX. Set this to the value of the `!DIR' system variable in IDL.
448 IDLWAVE uses this to find out which of the library routines belong to
449 the official system library. All files inside the `lib' subdirectory
450 are considered system library files - so don't install private stuff
451 in this directory. On UNIX systems, IDLWAVE queries the shell for the
452 value of `!DIR'. See also `idlwave-library-path'."
453 :group 'idlwave-routine-info
454 :type 'directory)
455
456 ;; Configuration files
457 (defcustom idlwave-config-directory
458 (convert-standard-filename "~/.idlwave")
459 "*Directory for configuration files and user-library catalog."
460 :group 'idlwave-routine-info
461 :type 'file)
462
463 (defvar idlwave-user-catalog-file "idlusercat.el")
464 (defvar idlwave-xml-system-rinfo-converted-file "idl_xml_rinfo.el")
465 (defvar idlwave-path-file "idlpath.el")
466
467 (defvar idlwave-libinfo-file nil
468 "*Obsolete variable, no longer used.")
469
470 (defcustom idlwave-special-lib-alist nil
471 "Alist of regular expressions matching special library directories.
472 When listing routine source locations, IDLWAVE gives a short hint where
473 the file defining the routine is located. By default it lists `SystemLib'
474 for routines in the system library `!DIR/lib' and `Library' for anything
475 else. This variable can define additional types. The car of each entry
476 is a regular expression matching the file name (they normally will match
477 on the path). The cdr is the string to be used as identifier. Max 10
478 chars are allowed."
479 :group 'idlwave-routine-info
480 :type '(repeat
481 (cons regexp string)))
482
483 (defcustom idlwave-auto-write-paths t
484 "Write out path (!PATH) and system directory (!DIR) info automatically.
485 Path info is needed to locate library catalog files. If non-nil,
486 whenever the path-list changes as a result of shell-query, etc., it is
487 written to file. Otherwise, the menu option \"Write Paths\" can be
488 used to force a write."
489 :group 'idlwave-routine-info
490 :type 'boolean)
491
492 (defgroup idlwave-completion nil
493 "Completion options for IDLWAVE mode."
494 :prefix "idlwave"
495 :group 'idlwave)
496
497 (eval-and-compile
498 (defconst idlwave-tmp
499 '(choice :tag "by applying the function"
500 (const upcase)
501 (const downcase)
502 (const capitalize)
503 (const preserve)
504 (symbol :tag "Other"))))
505
506 (defcustom idlwave-completion-case '((routine . upcase)
507 (keyword . upcase)
508 (class . preserve)
509 (method . preserve))
510 "Association list setting the case of completed words.
511
512 This variable determines the case (UPPER/lower/Capitalized...) of
513 words inserted into the buffer by completion. The preferred case can
514 be specified separately for routine names, keywords, classes and
515 methods.
516 This alist should therefore have entries for `routine' (normal
517 functions and procedures, i.e. non-methods), `keyword', `class', and
518 `method'. Plausible values are
519
520 upcase upcase whole word, like `BOX_CURSOR'
521 downcase downcase whole word, like `read_ppm'
522 capitalize capitalize each part, like `Widget_Control'
523 preserve preserve case as is, like `IDLgrView'
524
525 The value can also be any Emacs Lisp function which transforms the
526 case of characters in a string.
527
528 A value of `preserve' means that the case of the completed word is
529 identical to the way it was written in the definition statement of the
530 routine. This was implemented to allow for mixed-case completion, in
531 particular of object classes and methods.
532 If a completable word is defined in multiple locations, the meaning of
533 `preserve' is not unique since the different definitions might be
534 cased differently. Therefore IDLWAVE always takes the case of the
535 *first* definition it encounters during routine info collection and
536 uses the case derived from it consistently.
537
538 Note that a lowercase-only string in the buffer will always be completed in
539 lower case (but see the variable `idlwave-completion-force-default-case').
540
541 After changing this variable, you need to either restart Emacs or press
542 `C-u C-c C-i' to update the internal lists."
543 :group 'idlwave-completion
544 :type `(repeat
545 (cons (symbol :tag "Derive completion case for")
546 ,idlwave-tmp)))
547
548 (defcustom idlwave-completion-force-default-case nil
549 "*Non-nil means, completion will always honor `idlwave-completion-case'.
550 When nil, only the completion of a mixed case or upper case string
551 will honor the default settings in `idlwave-completion-case', while
552 the completion of lower case strings will be completed entirely in
553 lower case."
554 :group 'idlwave-completion
555 :type 'boolean)
556
557 (defcustom idlwave-complete-empty-string-as-lower-case nil
558 "*Non-nil means, the empty string is considered downcase for completion.
559 The case of what is already in the buffer determines the case of completions.
560 When this variable is non-nil, the empty string is considered to be downcase.
561 Completing on the empty string then offers downcase versions of the possible
562 completions."
563 :group 'idlwave-completion
564 :type 'boolean)
565
566 (defvar idlwave-default-completion-case-is-down nil
567 "Obsolete variable. See `idlwave-complete-empty-string-as-lower-case' and
568 `idlwave-completion-case'.")
569
570 (defcustom idlwave-buffer-case-takes-precedence nil
571 "*Non-nil means, the case of tokens in buffers dominates over system stuff.
572 To make this possible, we need to re-case everything each time we update
573 the routine info from the buffers. This is slow.
574 The default is to consider the case given in the system and library files
575 first which makes updating much faster."
576 :group 'idlwave-completion
577 :type 'boolean)
578
579 (defcustom idlwave-highlight-help-links-in-completion t
580 "*Non-nil means, highlight completions for which system help is available.
581 Help can then be accessed with mouse-3.
582 This option is only effective when the online help system is installed."
583 :group 'idlwave-completion
584 :type 'boolean)
585
586 (defcustom idlwave-support-inheritance t
587 "Non-nil means, treat inheritance with completion, online help etc.
588 When nil, IDLWAVE only knows about the native methods and tags of a class,
589 not about inherited ones."
590 :group 'idlwave-routine-info
591 :type 'boolean)
592
593 (defcustom idlwave-keyword-class-inheritance '("^[gs]etproperty$" "^init$")
594 "List of regular expressions for class-driven keyword inheritance.
595 Keyword inheritance is often tied to class inheritance by \"chaining\"
596 up the class tree. While it cannot be assumed that the presence of an
597 _EXTRA or _REF_EXTRA symbol guarantees such chaining will occur, for
598 certain methods this assumption is almost always true. The methods
599 for which to assume this can be set here."
600 :group 'idlwave-routine-info
601 :type '(repeat (regexp :tag "Match method:")))
602
603
604 (defcustom idlwave-completion-show-classes 1
605 "*Number of classes to show when completing object methods and keywords.
606 When completing methods or keywords for an object with unknown class,
607 the *Completions* buffer will show the valid classes for each completion
608 like this:
609
610 MyMethod <Class1,Class2,Class3>
611
612 The value of this variable may be nil to inhibit display, or an integer to
613 indicate the maximum number of classes to display.
614
615 On XEmacs, a full list of classes will also be placed into a `help-echo'
616 property on the competion items, so that the list of classes for the current
617 item is displayed in the echo area. If the value of this variable is a
618 negative integer, the `help-echo' property will be suppressed."
619 :group 'idlwave-completion
620 :type '(choice (const :tag "Don't show" nil)
621 (integer :tag "Number of classes shown" 1)))
622
623 (defcustom idlwave-completion-fontify-classes t
624 "*Non-nil means, fontify the classes in completions buffer.
625 This makes it easier to distinguish the completion items from the extra
626 class info listed. See `idlwave-completion-show-classes'."
627 :group 'idlwave-completion
628 :type 'boolean)
629
630 (defcustom idlwave-query-class '((method-default . nil)
631 (keyword-default . nil))
632 "Association list governing specification of object classes for completion.
633
634 When IDLWAVE tries to complete object-oriented methods, it usually
635 cannot determine the class of a given object from context. In order
636 to provide the user with a correct list of methods or keywords, it
637 needs to determine the appropriate class. IDLWAVE has two ways of
638 doing this (well, three ways if you count the shell... see
639 `idlwave-shell-query-for-class'):
640
641 1. Combine the items of all available classes which contain this
642 method for the purpose of completion. So when completing a method,
643 all methods of all known classes are available, and when completing
644 a keyword, all keywords allowed for this method in any class are
645 shown. This behavior is very much like normal completion and is
646 therefore the default. It works much better than one might think -
647 only for the INIT, GETPROPERTY and SETPROPERTY the keyword lists
648 become uncomfortably long. See also
649 `idlwave-completion-show-classes'.
650
651 2. The second possibility is to ask the user on each occasion. To
652 make this less interruptive, IDLWAVE can store the class as a text
653 property on the object operator `->'. For a given object in the
654 source code, class selection will then be needed only once
655 - for example to complete the method. Keywords to the method can
656 then be completed directly, because the class is already known.
657 You will have to turn on the storage of the selected class
658 explicitly with the variable `idlwave-store-inquired-class'.
659
660 This variable allows you to configure IDLWAVE's method and
661 method-keyword completion behavior. Its value is an alist, which
662 should contain at least two elements: (method-default . VALUE) and
663 \(keyword-default . VALUE), where VALUE is either t or nil. These
664 specify if the class should be found during method and keyword
665 completion, respectively.
666
667 The alist may have additional entries specifying exceptions from the
668 keyword completion rule for specific methods, like INIT or
669 GETPROPERTY. In order to turn on class specification for the INIT
670 method, add an entry (\"INIT\" . t). The method name must be ALL-CAPS."
671 :group 'idlwave-completion
672 :type '(list
673 (cons (const method-default)
674 (boolean :tag "Determine class when completing METHODS "))
675 (cons (const keyword-default)
676 (boolean :tag "Determine class when completing KEYWORDS "))
677 (repeat
678 :tag "Exceptions to defaults"
679 :inline t
680 (cons (string :tag "MODULE" :value "")
681 (boolean :tag "Determine class for this method")))))
682
683 (defcustom idlwave-store-inquired-class t
684 "*Non-nil means, store class of a method call as text property on `->'.
685 IDLWAVE sometimes has to ask the user for the class associated with a
686 particular object method call. This happens during the commands
687 `idlwave-routine-info' and `idlwave-complete', depending upon the
688 value of the variable `idlwave-query-class'.
689
690 When you specify a class, this information can be stored as a text
691 property on the `->' arrow in the source code, so that during the same
692 editing session, IDLWAVE will not have to ask again. When this
693 variable is non-nil, IDLWAVE will store and reuse the class information.
694 The class stored can be checked and removed with `\\[idlwave-routine-info]'
695 on the arrow.
696
697 The default of this variable is nil, since the result of commands then
698 is more predictable. However, if you know what you are doing, it can
699 be nice to turn this on.
700
701 An arrow which knows the class will be highlighted with
702 `idlwave-class-arrow-face'. The command \\[idlwave-routine-info]
703 displays (with prefix arg: deletes) the class stored on the arrow
704 at point."
705 :group 'idlwave-completion
706 :type 'boolean)
707
708 (defcustom idlwave-class-arrow-face 'bold
709 "*Face to highlight object operator arrows `->' which carry a class property.
710 When IDLWAVE stores a class name as text property on an object arrow
711 \(see variable `idlwave-store-inquired-class', it highlights the arrow
712 with this font in order to remind the user that this arrow is special."
713 :group 'idlwave-completion
714 :type 'symbol)
715
716 (defcustom idlwave-resize-routine-help-window t
717 "*Non-nil means, resize the Routine-info *Help* window to fit the content."
718 :group 'idlwave-completion
719 :type 'boolean)
720
721 (defcustom idlwave-keyword-completion-adds-equal t
722 "*Non-nil means, completion automatically adds `=' after completed keywords."
723 :group 'idlwave-completion
724 :type 'boolean)
725
726 (defcustom idlwave-function-completion-adds-paren t
727 "*Non-nil means, completion automatically adds `(' after completed function.
728 nil means, don't add anything.
729 A value of `2' means, also add the closing parenthesis and position cursor
730 between the two."
731 :group 'idlwave-completion
732 :type '(choice (const :tag "Nothing" nil)
733 (const :tag "(" t)
734 (const :tag "()" 2)))
735
736 (defcustom idlwave-completion-restore-window-configuration t
737 "*Non-nil means, try to restore the window configuration after completion.
738 When completion is not unique, Emacs displays a list of completions.
739 This messes up your window configuration. With this variable set, IDLWAVE
740 restores the old configuration after successful completion."
741 :group 'idlwave-completion
742 :type 'boolean)
743
744 ;;; Variables for abbrev and action behavior -----------------------------
745
746 (defgroup idlwave-abbrev-and-indent-action nil
747 "IDLWAVE performs actions when expanding abbreviations or indenting lines.
748 The variables in this group govern this."
749 :group 'idlwave)
750
751 (defcustom idlwave-do-actions nil
752 "*Non-nil means performs actions when indenting.
753 The actions that can be performed are listed in `idlwave-indent-action-table'."
754 :group 'idlwave-abbrev-and-indent-action
755 :type 'boolean)
756
757 (defcustom idlwave-abbrev-start-char "\\"
758 "*A single character string used to start abbreviations in abbrev mode.
759 Possible characters to chose from: ~`\%
760 or even '?'. '.' is not a good choice because it can make structure
761 field names act like abbrevs in certain circumstances.
762
763 Changes to this in `idlwave-mode-hook' will have no effect. Instead a user
764 must set it directly using `setq' in the .emacs file before idlwave.el
765 is loaded."
766 :group 'idlwave-abbrev-and-indent-action
767 :type 'string)
768
769 (defcustom idlwave-surround-by-blank nil
770 "*Non-nil means, enable `idlwave-surround'.
771 If non-nil, `=',`<',`>',`&',`,', `->' are surrounded with spaces by
772 `idlwave-surround'.
773 See help for `idlwave-indent-action-table' for symbols using `idlwave-surround'.
774
775 Also see the default key bindings for keys using `idlwave-surround'.
776 Keys are bound and made into actions calling `idlwave-surround' with
777 `idlwave-action-and-binding'.
778 See help for `idlwave-action-and-binding' for examples.
779
780 Also see help for `idlwave-surround'."
781 :group 'idlwave-abbrev-and-indent-action
782 :type 'boolean)
783
784 (defcustom idlwave-pad-keyword t
785 "*Non-nil means pad '=' in keywords (routine calls or defs) like assignment.
786 Whenever `idlwave-surround' is non-nil then this affects how '=' is
787 padded for keywords and for variables. If t, pad the same as for
788 assignments. If nil then spaces are removed. With any other value,
789 spaces are left unchanged."
790 :group 'idlwave-abbrev-and-indent-action
791 :type '(choice
792 (const :tag "Pad like assignments" t)
793 (const :tag "Remove space near `='" nil)
794 (const :tag "Keep space near `='" 'keep)))
795
796 (defcustom idlwave-show-block t
797 "*Non-nil means point blinks to block beginning for `idlwave-show-begin'."
798 :group 'idlwave-abbrev-and-indent-action
799 :type 'boolean)
800
801 (defcustom idlwave-expand-generic-end nil
802 "*Non-nil means expand generic END to ENDIF/ENDELSE/ENDWHILE etc."
803 :group 'idlwave-abbrev-and-indent-action
804 :type 'boolean)
805
806 (defcustom idlwave-reindent-end t
807 "*Non-nil means re-indent line after END was typed."
808 :group 'idlwave-abbrev-and-indent-action
809 :type 'boolean)
810
811 (defcustom idlwave-abbrev-move t
812 "*Non-nil means the abbrev hook can move point.
813 Set to nil by `idlwave-expand-region-abbrevs'. To see the abbrev
814 definitions, use the command `list-abbrevs', for abbrevs that move
815 point. Moving point is useful, for example, to place point between
816 parentheses of expanded functions.
817
818 See `idlwave-check-abbrev'."
819 :group 'idlwave-abbrev-and-indent-action
820 :type 'boolean)
821
822 (defcustom idlwave-abbrev-change-case nil
823 "*Non-nil means all abbrevs will be forced to either upper or lower case.
824 If the value t, all expanded abbrevs will be upper case.
825 If the value is 'down then abbrevs will be forced to lower case.
826 If nil, the case will not change.
827 If `idlwave-reserved-word-upcase' is non-nil, reserved words will always be
828 upper case, regardless of this variable."
829 :group 'idlwave-abbrev-and-indent-action
830 :type 'boolean)
831
832 (defcustom idlwave-reserved-word-upcase nil
833 "*Non-nil means, reserved words will be made upper case via abbrev expansion.
834 If nil case of reserved words is controlled by `idlwave-abbrev-change-case'.
835 Has effect only if in abbrev-mode."
836 :group 'idlwave-abbrev-and-indent-action
837 :type 'boolean)
838
839 ;;; Action/Expand Tables.
840 ;;
841 ;; The average user may have difficulty modifying this directly. It
842 ;; can be modified/set in idlwave-mode-hook, but it is easier to use
843 ;; idlwave-action-and-binding. See help for idlwave-action-and-binding for
844 ;; examples of how to add an action.
845 ;;
846 ;; The action table is used by `idlwave-indent-line' whereas both the
847 ;; action and expand tables are used by `idlwave-indent-and-action'. In
848 ;; general, the expand table is only used when a line is explicitly
849 ;; indented. Whereas, in addition to being used when the expand table
850 ;; is used, the action table is used when a line is indirectly
851 ;; indented via line splitting, auto-filling or a new line creation.
852 ;;
853 ;; Example actions:
854 ;;
855 ;; Capitalize system vars
856 ;; (idlwave-action-and-binding idlwave-sysvar '(capitalize-word 1) t)
857 ;;
858 ;; Capitalize procedure name
859 ;; (idlwave-action-and-binding "\\<\\(pro\\|function\\)\\>[ \t]*\\<"
860 ;; '(capitalize-word 1) t)
861 ;;
862 ;; Capitalize common block name
863 ;; (idlwave-action-and-binding "\\<common\\>[ \t]+\\<"
864 ;; '(capitalize-word 1) t)
865 ;; Capitalize label
866 ;; (idlwave-action-and-binding (concat "^[ \t]*" idlwave-label)
867 ;; '(capitalize-word -1) t)
868
869 (defvar idlwave-indent-action-table nil
870 "*Associated array containing action lists of search string (car),
871 and function as a cdr. This table is used by `idlwave-indent-line'.
872 See documentation for `idlwave-do-action' for a complete description of
873 the action lists.
874
875 Additions to the table are made with `idlwave-action-and-binding' when a
876 binding is not requested.
877 See help on `idlwave-action-and-binding' for examples.")
878
879 (defvar idlwave-indent-expand-table nil
880 "*Associated array containing action lists of search string (car),
881 and function as a cdr. The table is used by the
882 `idlwave-indent-and-action' function. See documentation for
883 `idlwave-do-action' for a complete description of the action lists.
884
885 Additions to the table are made with `idlwave-action-and-binding' when a
886 binding is requested.
887 See help on `idlwave-action-and-binding' for examples.")
888
889 ;;; Documentation header and history keyword ---------------------------------
890
891 (defgroup idlwave-documentation nil
892 "Options for documenting IDLWAVE files."
893 :group 'idlwave)
894
895 ;; FIXME: make defcustom?
896 (defvar idlwave-file-header
897 (list nil
898 ";+
899 ; NAME:
900 ;
901 ;
902 ;
903 ; PURPOSE:
904 ;
905 ;
906 ;
907 ; CATEGORY:
908 ;
909 ;
910 ;
911 ; CALLING SEQUENCE:
912 ;
913 ;
914 ;
915 ; INPUTS:
916 ;
917 ;
918 ;
919 ; OPTIONAL INPUTS:
920 ;
921 ;
922 ;
923 ; KEYWORD PARAMETERS:
924 ;
925 ;
926 ;
927 ; OUTPUTS:
928 ;
929 ;
930 ;
931 ; OPTIONAL OUTPUTS:
932 ;
933 ;
934 ;
935 ; COMMON BLOCKS:
936 ;
937 ;
938 ;
939 ; SIDE EFFECTS:
940 ;
941 ;
942 ;
943 ; RESTRICTIONS:
944 ;
945 ;
946 ;
947 ; PROCEDURE:
948 ;
949 ;
950 ;
951 ; EXAMPLE:
952 ;
953 ;
954 ;
955 ; MODIFICATION HISTORY:
956 ;
957 ;-
958 ")
959 "*A list (PATHNAME STRING) specifying the doc-header template to use for
960 summarizing a file. If PATHNAME is non-nil then this file will be included.
961 Otherwise STRING is used. If nil, the file summary will be omitted.
962 For example you might set PATHNAME to the path for the
963 lib_template.pro file included in the IDL distribution.")
964
965 (defcustom idlwave-header-to-beginning-of-file t
966 "*Non-nil means, the documentation header will always be at start of file.
967 When nil, the header is positioned between the PRO/FUNCTION line of
968 the current routine and the code, allowing several routine headers in
969 a file."
970 :group 'idlwave-documentation
971 :type 'boolean)
972
973 (defcustom idlwave-timestamp-hook 'idlwave-default-insert-timestamp
974 "*The hook function used to update the timestamp of a function."
975 :group 'idlwave-documentation
976 :type 'function)
977
978 (defcustom idlwave-doc-modifications-keyword "HISTORY"
979 "*The modifications keyword to use with the log documentation commands.
980 A ':' is added to the keyword end.
981 Inserted by doc-header and used to position logs by doc-modification.
982 If nil it will not be inserted."
983 :group 'idlwave-documentation
984 :type 'string)
985
986 (defcustom idlwave-doclib-start "^;+\\+"
987 "*Regexp matching the start of a document library header."
988 :group 'idlwave-documentation
989 :type 'regexp)
990
991 (defcustom idlwave-doclib-end "^;+-"
992 "*Regexp matching the end of a document library header."
993 :group 'idlwave-documentation
994 :type 'regexp)
995
996 ;;; External Programs -------------------------------------------------------
997
998 (defgroup idlwave-external-programs nil
999 "Path locations of external commands used by IDLWAVE."
1000 :group 'idlwave)
1001
1002 (defcustom idlwave-shell-explicit-file-name "idl"
1003 "*If non-nil, this is the command to run IDL.
1004 Should be an absolute file path or path relative to the current environment
1005 execution search path. If you want to specify command line switches
1006 for the idl program, use `idlwave-shell-command-line-options'.
1007
1008 I know the name of this variable is badly chosen, but I cannot change
1009 it without compromizing backwards-compatibility."
1010 :group 'idlwave-external-programs
1011 :type 'string)
1012
1013 (defcustom idlwave-shell-command-line-options nil
1014 "*A list of command line options for calling the IDL program.
1015 Since IDL is executed directly without going through a shell like /bin/sh,
1016 this should be a list of strings like '(\"-rt=file\" \"-nw\") with a separate
1017 string for each argument. But you may also give a single string which
1018 contains the options whitespace-separated. Emacs will be kind enough to
1019 split it for you."
1020 :type '(choice
1021 string
1022 (repeat (string :value "")))
1023 :group 'idlwave-external-programs)
1024
1025 (defcustom idlwave-help-application "idlhelp"
1026 "*The external application providing reference help for programming.
1027 Obsolete, if the IDL Assistant is being used for help."
1028 :group 'idlwave-external-programs
1029 :type 'string)
1030
1031 ;;; Some Shell variables which must be defined here.-----------------------
1032
1033 (defcustom idlwave-shell-debug-modifiers '()
1034 "List of modifiers to be used for the debugging commands.
1035 Will be used to bind debugging commands in the shell buffer and in all
1036 source buffers. These are additional convenience bindings, the debugging
1037 commands are always available with the `C-c C-d' prefix.
1038 If you set this to '(control shift), this means setting a breakpoint will
1039 be on `C-S-b', compiling a source file on `C-S-c' etc. Possible modifiers
1040 are `control', `meta', `super', `hyper', `alt', and `shift'."
1041 :group 'idlwave-shell-general-setup
1042 :type '(set :tag "Specify modifiers"
1043 (const control)
1044 (const meta)
1045 (const super)
1046 (const hyper)
1047 (const alt)
1048 (const shift)))
1049
1050 (defcustom idlwave-shell-automatic-start nil
1051 "*If non-nil attempt invoke idlwave-shell if not already running.
1052 This is checked when an attempt to send a command to an
1053 IDL process is made."
1054 :group 'idlwave-shell-general-setup
1055 :type 'boolean)
1056
1057 ;;; Miscellaneous variables -------------------------------------------------
1058
1059 (defgroup idlwave-misc nil
1060 "Miscellaneous options for IDLWAVE mode."
1061 :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
1062 :group 'idlwave)
1063
1064 (defcustom idlwave-startup-message t
1065 "*Non-nil displays a startup message when `idlwave-mode' is first called."
1066 :group 'idlwave-misc
1067 :type 'boolean)
1068
1069 (defcustom idlwave-default-font-lock-items
1070 '(pros-and-functions batch-files idlwave-idl-keywords label goto
1071 common-blocks class-arrows)
1072 "Items which should be fontified on the default fontification level 2.
1073 IDLWAVE defines 3 levels of fontification. Level 1 is very little, level 3
1074 is everything and level 2 is specified by this list.
1075 This variable must be set before IDLWAVE gets loaded. It is
1076 a list of symbols, the following symbols are allowed.
1077
1078 pros-and-functions Procedure and Function definitions
1079 batch-files Batch Files
1080 idlwave-idl-keywords IDL Keywords
1081 label Statement Labels
1082 goto Goto Statements
1083 common-blocks Common Blocks
1084 keyword-parameters Keyword Parameters in routine definitions and calls
1085 system-variables System Variables
1086 fixme FIXME: Warning in comments (on XEmacs only v. 21.0 and up)
1087 class-arrows Object Arrows with class property"
1088 :group 'idlwave-misc
1089 :type '(set
1090 :inline t :greedy t
1091 (const :tag "Procedure and Function definitions" pros-and-functions)
1092 (const :tag "Batch Files" batch-files)
1093 (const :tag "IDL Keywords (reserved words)" idlwave-idl-keywords)
1094 (const :tag "Statement Labels" label)
1095 (const :tag "Goto Statements" goto)
1096 (const :tag "Tags in Structure Definition" structtag)
1097 (const :tag "Structure Name" structname)
1098 (const :tag "Common Blocks" common-blocks)
1099 (const :tag "Keyword Parameters" keyword-parameters)
1100 (const :tag "System Variables" system-variables)
1101 (const :tag "FIXME: Warning" fixme)
1102 (const :tag "Object Arrows with class property " class-arrows)))
1103
1104 (defcustom idlwave-mode-hook nil
1105 "Normal hook. Executed when a buffer is put into `idlwave-mode'."
1106 :group 'idlwave-misc
1107 :type 'hook)
1108
1109 (defcustom idlwave-load-hook nil
1110 "Normal hook. Executed when idlwave.el is loaded."
1111 :group 'idlwave-misc
1112 :type 'hook)
1113
1114 (defvar idlwave-experimental nil
1115 "Non-nil means turn on a few experimental features.
1116 This variable is only for the maintainer, to test difficult stuff,
1117 while still distributing stable releases.
1118 As a user, you should not set this to t.")
1119
1120 ;;;
1121 ;;; End customization variables section
1122 ;;;
1123
1124 ;;; Non customization variables
1125
1126 ;;; font-lock mode - Additions by Phil Williams, Ulrik Dickow and
1127 ;;; Simon Marshall <simon_at_gnu.ai.mit.edu>
1128 ;;; and Carsten Dominik...
1129
1130 ;; The following are the reserved words in IDL. Maybe we should
1131 ;; highlight some more stuff as well?
1132 ;; Procedure declarations. Fontify keyword plus procedure name.
1133 (defvar idlwave-idl-keywords
1134 ;; To update this regexp, update the list of keywords and
1135 ;; evaluate the form.
1136 ;; (insert
1137 ;; (prin1-to-string
1138 ;; (concat
1139 ;; "\\<\\("
1140 ;; (regexp-opt
1141 ;; '("||" "&&" "and" "or" "xor" "not"
1142 ;; "eq" "ge" "gt" "le" "lt" "ne"
1143 ;; "for" "do" "endfor"
1144 ;; "if" "then" "endif" "else" "endelse"
1145 ;; "case" "of" "endcase"
1146 ;; "switch" "break" "continue" "endswitch"
1147 ;; "begin" "end"
1148 ;; "repeat" "until" "endrep"
1149 ;; "while" "endwhile"
1150 ;; "goto" "return"
1151 ;; "inherits" "mod"
1152 ;; "compile_opt" "forward_function"
1153 ;; "on_error" "on_ioerror")) ; on_error is not officially reserved
1154 ;; "\\)\\>")))
1155 "\\<\\(&&\\|and\\|b\\(egin\\|reak\\)\\|c\\(ase\\|o\\(mpile_opt\\|ntinue\\)\\)\\|do\\|e\\(lse\\|nd\\(case\\|else\\|for\\|if\\|rep\\|switch\\|while\\)?\\|q\\)\\|for\\(ward_function\\)?\\|g\\(oto\\|[et]\\)\\|i\\(f\\|nherits\\)\\|l[et]\\|mod\\|n\\(e\\|ot\\)\\|o\\(n_\\(error\\|ioerror\\)\\|[fr]\\)\\|re\\(peat\\|turn\\)\\|switch\\|then\\|until\\|while\\|xor\\|||\\)\\>")
1156
1157
1158 (let* (;; Procedure declarations. Fontify keyword plus procedure name.
1159 ;; Function declarations. Fontify keyword plus function name.
1160 (pros-and-functions
1161 '("\\<\\(function\\|pro\\)\\>[ \t]+\\(\\sw+\\(::\\sw+\\)?\\)"
1162 (1 font-lock-keyword-face)
1163 (2 font-lock-function-name-face nil t)))
1164
1165 ;; Common blocks
1166 (common-blocks
1167 '("\\<\\(common\\)\\>[ \t]*\\(\\sw+\\)?[ \t]*,?"
1168 (1 font-lock-keyword-face) ; "common"
1169 (2 font-lock-reference-face nil t) ; block name
1170 ("[ \t]*\\(\\sw+\\)[ ,]*"
1171 ;; Start with point after block name and comma
1172 (goto-char (match-end 0)) ; needed for XEmacs, could be nil
1173 nil
1174 (1 font-lock-variable-name-face) ; variable names
1175 )))
1176
1177 ;; Batch files
1178 (batch-files
1179 '("^[ \t]*\\(@[^ \t\n]+\\)" (1 font-lock-string-face)))
1180
1181 ;; FIXME warning.
1182 (fixme
1183 '("\\<FIXME:" (0 font-lock-warning-face t)))
1184
1185 ;; Labels
1186 (label
1187 '("^[ \t]*\\([a-zA-Z]\\sw*:\\)" (1 font-lock-reference-face)))
1188
1189 ;; The goto statement and its label
1190 (goto
1191 '("\\(goto\\)[ \t]*,[ \t]*\\([a-zA-Z]\\sw*\\)"
1192 (1 font-lock-keyword-face)
1193 (2 font-lock-reference-face)))
1194
1195 ;; Tags in structure definitions. Note that this definition
1196 ;; actually collides with labels, so we have to use the same
1197 ;; face. It also matches named subscript ranges,
1198 ;; e.g. vec{bottom:top]. No good way around this.
1199 (structtag
1200 '("\\<\\([a-zA-Z][a-zA-Z0-9_]*:\\)[^:]" (1 font-lock-reference-face)))
1201
1202 ;; Structure names
1203 (structname
1204 '("\\({\\|\\<inherits\\s-\\)\\s-*\\([a-zA-Z][a-zA-Z0-9_]*\\)[},\t \n]"
1205 (2 font-lock-function-name-face)))
1206
1207 ;; Keyword parameters, like /xlog or ,xrange=[]
1208 ;; This is anchored to the comma preceeding the keyword.
1209 ;; Treats continuation lines, works only during whole buffer
1210 ;; fontification. Slow, use it only in fancy fontification.
1211 (keyword-parameters
1212 '("\\(,\\|[a-zA-Z0-9_](\\)[ \t]*\\(\\$[ \t]*\\(;.*\\)?\n\\([ \t]*\\(;.*\\)?\n\\)*[ \t]*\\)?\\(/[a-zA-Z_]\\sw*\\|[a-zA-Z_]\\sw*[ \t]*=\\)"
1213 (6 font-lock-reference-face)))
1214
1215 ;; System variables start with a bang.
1216 (system-variables
1217 '("\\(![a-zA-Z_0-9]+\\(\\.\\sw+\\)?\\)"
1218 (1 font-lock-variable-name-face)))
1219
1220 ;; Special and unusual operators (not used because too noisy)
1221 (special-operators
1222 '("[<>#]" (0 font-lock-keyword-face)))
1223
1224 ;; All operators (not used because too noisy)
1225 (all-operators
1226 '("[-*^#+<>/]" (0 font-lock-keyword-face)))
1227
1228 ;; Arrows with text property `idlwave-class'
1229 (class-arrows
1230 '(idlwave-match-class-arrows (0 idlwave-class-arrow-face))))
1231
1232 (defconst idlwave-font-lock-keywords-1
1233 (list pros-and-functions batch-files)
1234 "Subdued level highlighting for IDLWAVE mode.")
1235
1236 (defconst idlwave-font-lock-keywords-2
1237 (mapcar 'symbol-value idlwave-default-font-lock-items)
1238 "Medium level highlighting for IDLWAVE mode.")
1239
1240 (defconst idlwave-font-lock-keywords-3
1241 (list pros-and-functions
1242 batch-files
1243 idlwave-idl-keywords
1244 label goto
1245 structtag
1246 structname
1247 common-blocks
1248 keyword-parameters
1249 system-variables
1250 class-arrows)
1251 "Gaudy level highlighting for IDLWAVE mode."))
1252
1253 (defun idlwave-match-class-arrows (limit)
1254 ;; Match an object arrow with class property
1255 (and idlwave-store-inquired-class
1256 (re-search-forward "->" limit 'limit)
1257 (get-text-property (match-beginning 0) 'idlwave-class)))
1258
1259 (defvar idlwave-font-lock-keywords idlwave-font-lock-keywords-2
1260 "Default expressions to highlight in IDLWAVE mode.")
1261
1262 (defvar idlwave-font-lock-defaults
1263 '((idlwave-font-lock-keywords
1264 idlwave-font-lock-keywords-1
1265 idlwave-font-lock-keywords-2
1266 idlwave-font-lock-keywords-3)
1267 nil t
1268 ((?$ . "w") (?_ . "w") (?. . "w") (?| . "w") (?& . "w"))
1269 beginning-of-line))
1270
1271 (put 'idlwave-mode 'font-lock-defaults
1272 idlwave-font-lock-defaults) ; XEmacs
1273
1274 (defconst idlwave-comment-line-start-skip "^[ \t]*;"
1275 "Regexp to match the start of a full-line comment.
1276 That is the _beginning_ of a line containing a comment delimiter `;' preceded
1277 only by whitespace.")
1278
1279 (defconst idlwave-begin-block-reg
1280 "\\<\\(pro\\|function\\|begin\\|case\\|switch\\)\\>"
1281 "Regular expression to find the beginning of a block. The case does
1282 not matter. The search skips matches in comments.")
1283
1284 (defconst idlwave-begin-unit-reg "^\\s-*\\(pro\\|function\\)\\>\\|\\`"
1285 "Regular expression to find the beginning of a unit. The case does
1286 not matter.")
1287
1288 (defconst idlwave-end-unit-reg "^\\s-*\\(pro\\|function\\)\\>\\|\\'"
1289 "Regular expression to find the line that indicates the end of unit.
1290 This line is the end of buffer or the start of another unit. The case does
1291 not matter. The search skips matches in comments.")
1292
1293 (defconst idlwave-continue-line-reg "\\<\\$"
1294 "Regular expression to match a continued line.")
1295
1296 (defconst idlwave-end-block-reg
1297 "\\<end\\(\\|case\\|switch\\|else\\|for\\|if\\|rep\\|while\\)\\>"
1298 "Regular expression to find the end of a block. The case does
1299 not matter. The search skips matches found in comments.")
1300
1301 (defconst idlwave-block-matches
1302 '(("pro" . "end")
1303 ("function" . "end")
1304 ("case" . "endcase")
1305 ("else" . "endelse")
1306 ("for" . "endfor")
1307 ("then" . "endif")
1308 ("repeat" . "endrep")
1309 ("switch" . "endswitch")
1310 ("while" . "endwhile"))
1311 "Matches between statements and the corresponding END variant.
1312 The cars are the reserved words starting a block. If the block really
1313 begins with BEGIN, the cars are the reserved words before the begin
1314 which can be used to identify the block type.
1315 This is used to check for the correct END type, to close blocks and
1316 to expand generic end statements to their detailed form.")
1317
1318 (defconst idlwave-block-match-regexp
1319 "\\<\\(else\\|for\\|then\\|repeat\\|while\\)\\>"
1320 "Regular expression matching reserved words which can stand before
1321 blocks starting with a BEGIN statement. The matches must have associations
1322 `idlwave-block-matches'")
1323
1324 (defconst idlwave-identifier "[a-zA-Z_][a-zA-Z0-9$_]*"
1325 "Regular expression matching an IDL identifier.")
1326
1327 (defconst idlwave-sysvar (concat "!" idlwave-identifier)
1328 "Regular expression matching IDL system variables.")
1329
1330 (defconst idlwave-variable (concat idlwave-identifier "\\|" idlwave-sysvar)
1331 "Regular expression matching IDL variable names.")
1332
1333 (defconst idlwave-label (concat idlwave-identifier ":")
1334 "Regular expression matching IDL labels.")
1335
1336 (defconst idlwave-method-call (concat idlwave-identifier "\\s *->"
1337 "\\(\\s *" idlwave-identifier "::\\)?"
1338 ))
1339
1340 (defconst idlwave-statement-match
1341 (list
1342 ;; "endif else" is the only possible "end" that can be
1343 ;; followed by a statement on the same line.
1344 '(endelse . ("end\\(\\|if\\)\\s +else" "end\\(\\|if\\)\\s +else"))
1345 ;; all other "end"s can not be followed by a statement.
1346 (cons 'end (list idlwave-end-block-reg nil))
1347 '(if . ("if\\>" "then"))
1348 '(for . ("for\\>" "do"))
1349 '(begin . ("begin\\>" nil))
1350 '(pdef . ("pro\\>\\|function\\>" nil))
1351 '(while . ("while\\>" "do"))
1352 '(repeat . ("repeat\\>" "repeat"))
1353 '(goto . ("goto\\>" nil))
1354 '(case . ("case\\>" nil))
1355 '(switch . ("switch\\>" nil))
1356 (cons 'call (list (concat "\\(" idlwave-variable "\\) *= *"
1357 "\\(" idlwave-method-call "\\s *\\)?"
1358 idlwave-identifier
1359 "\\s *(") nil))
1360 (cons 'call (list (concat
1361 "\\(" idlwave-method-call "\\s *\\)?"
1362 idlwave-identifier
1363 "\\( *\\($\\|\\$\\)\\|\\s *,\\)") nil))
1364 (cons 'assign (list (concat
1365 "\\(" idlwave-variable "\\) *=") nil)))
1366
1367 "Associated list of statement matching regular expressions.
1368 Each regular expression matches the start of an IDL statement. The
1369 first element of each association is a symbol giving the statement
1370 type. The associated value is a list. The first element of this list
1371 is a regular expression matching the start of an IDL statement for
1372 identifying the statement type. The second element of this list is a
1373 regular expression for finding a substatement for the type. The
1374 substatement starts after the end of the found match modulo
1375 whitespace. If it is nil then the statement has no substatement. The
1376 list order matters since matching an assignment statement exactly is
1377 not possible without parsing. Thus assignment statement become just
1378 the leftover unidentified statements containing an equal sign." )
1379
1380 (defvar idlwave-fill-function 'auto-fill-function
1381 "IDL mode auto fill function.")
1382
1383 (defvar idlwave-comment-indent-function 'comment-indent-function
1384 "IDL mode comment indent function.")
1385
1386 ;; Note that this is documented in the v18 manuals as being a string
1387 ;; of length one rather than a single character.
1388 ;; The code in this file accepts either format for compatibility.
1389 (defvar idlwave-comment-indent-char ?\
1390 "Character to be inserted for IDL comment indentation.
1391 Normally a space.")
1392
1393 (defconst idlwave-continuation-char ?$
1394 "Character which is inserted as a last character on previous line by
1395 \\[idlwave-split-line] to begin a continuation line. Normally $.")
1396
1397 (defconst idlwave-mode-version "6.1_em22")
1398
1399 (defmacro idlwave-keyword-abbrev (&rest args)
1400 "Creates a function for abbrev hooks to call `idlwave-check-abbrev' with args."
1401 `(quote (lambda ()
1402 ,(append '(idlwave-check-abbrev) args))))
1403
1404 ;; If I take the time I can replace idlwave-keyword-abbrev with
1405 ;; idlwave-code-abbrev and remove the quoted abbrev check from
1406 ;; idlwave-check-abbrev. Then, e.g, (idlwave-keyword-abbrev 0 t) becomes
1407 ;; (idlwave-code-abbrev idlwave-check-abbrev 0 t). In fact I should change
1408 ;; the name of idlwave-check-abbrev to something like idlwave-modify-abbrev.
1409
1410 (defmacro idlwave-code-abbrev (&rest args)
1411 "Creates a function for abbrev hooks that ensures abbrevs are not quoted.
1412 Specifically, if the abbrev is in a comment or string it is unexpanded.
1413 Otherwise ARGS forms a list that is evaluated."
1414 `(quote (lambda ()
1415 ,(prin1-to-string args) ;; Puts the code in the doc string
1416 (if (idlwave-quoted)
1417 (progn (unexpand-abbrev) nil)
1418 ,(append args)))))
1419
1420 (defvar idlwave-mode-map (make-sparse-keymap)
1421 "Keymap used in IDL mode.")
1422
1423 (defvar idlwave-mode-syntax-table (make-syntax-table)
1424 "Syntax table in use in `idlwave-mode' buffers.")
1425
1426 (modify-syntax-entry ?+ "." idlwave-mode-syntax-table)
1427 (modify-syntax-entry ?- "." idlwave-mode-syntax-table)
1428 (modify-syntax-entry ?* "." idlwave-mode-syntax-table)
1429 (modify-syntax-entry ?/ "." idlwave-mode-syntax-table)
1430 (modify-syntax-entry ?^ "." idlwave-mode-syntax-table)
1431 (modify-syntax-entry ?# "." idlwave-mode-syntax-table)
1432 (modify-syntax-entry ?= "." idlwave-mode-syntax-table)
1433 (modify-syntax-entry ?% "." idlwave-mode-syntax-table)
1434 (modify-syntax-entry ?< "." idlwave-mode-syntax-table)
1435 (modify-syntax-entry ?> "." idlwave-mode-syntax-table)
1436 (modify-syntax-entry ?\' "\"" idlwave-mode-syntax-table)
1437 (modify-syntax-entry ?\" "\"" idlwave-mode-syntax-table)
1438 (modify-syntax-entry ?\\ "." idlwave-mode-syntax-table)
1439 (modify-syntax-entry ?_ "_" idlwave-mode-syntax-table)
1440 (modify-syntax-entry ?{ "(}" idlwave-mode-syntax-table)
1441 (modify-syntax-entry ?} "){" idlwave-mode-syntax-table)
1442 (modify-syntax-entry ?$ "_" idlwave-mode-syntax-table)
1443 (modify-syntax-entry ?. "." idlwave-mode-syntax-table)
1444 (modify-syntax-entry ?\; "<" idlwave-mode-syntax-table)
1445 (modify-syntax-entry ?\n ">" idlwave-mode-syntax-table)
1446 (modify-syntax-entry ?\f ">" idlwave-mode-syntax-table)
1447
1448 (defvar idlwave-find-symbol-syntax-table
1449 (copy-syntax-table idlwave-mode-syntax-table)
1450 "Syntax table that treats symbol characters as word characters.")
1451
1452 (modify-syntax-entry ?$ "w" idlwave-find-symbol-syntax-table)
1453 (modify-syntax-entry ?_ "w" idlwave-find-symbol-syntax-table)
1454 (modify-syntax-entry ?! "w" idlwave-find-symbol-syntax-table)
1455 (modify-syntax-entry ?. "w" idlwave-find-symbol-syntax-table)
1456
1457 (defmacro idlwave-with-special-syntax (&rest body)
1458 "Execute BODY with a different syntax table."
1459 `(let ((saved-syntax (syntax-table)))
1460 (unwind-protect
1461 (progn
1462 (set-syntax-table idlwave-find-symbol-syntax-table)
1463 ,@body)
1464 (set-syntax-table saved-syntax))))
1465
1466 ;(defmacro idlwave-with-special-syntax1 (&rest body)
1467 ; "Execute BODY with a different syntax table."
1468 ; `(let ((saved-syntax (syntax-table)))
1469 ; (unwind-protect
1470 ; (progn
1471 ; (set-syntax-table idlwave-find-symbol-syntax-table)
1472 ; ,@body)
1473 ; (set-syntax-table saved-syntax))))
1474
1475 (defun idlwave-action-and-binding (key cmd &optional select)
1476 "KEY and CMD are made into a key binding and an indent action.
1477 KEY is a string - same as for the `define-key' function. CMD is a
1478 function of no arguments or a list to be evaluated. CMD is bound to
1479 KEY in `idlwave-mode-map' by defining an anonymous function calling
1480 `self-insert-command' followed by CMD. If KEY contains more than one
1481 character a binding will only be set if SELECT is 'both.
1482
1483 \(KEY . CMD\) is also placed in the `idlwave-indent-expand-table',
1484 replacing any previous value for KEY. If a binding is not set then it
1485 will instead be placed in `idlwave-indent-action-table'.
1486
1487 If the optional argument SELECT is nil then an action and binding are
1488 created. If SELECT is 'noaction, then a binding is always set and no
1489 action is created. If SELECT is 'both then an action and binding
1490 will both be created even if KEY contains more than one character.
1491 Otherwise, if SELECT is non-nil then only an action is created.
1492
1493 Some examples:
1494 No spaces before and 1 after a comma
1495 (idlwave-action-and-binding \",\" '(idlwave-surround 0 1))
1496 A minimum of 1 space before and after `=' (see `idlwave-expand-equal').
1497 (idlwave-action-and-binding \"=\" '(idlwave-expand-equal -1 -1))
1498 Capitalize system variables - action only
1499 (idlwave-action-and-binding idlwave-sysvar '(capitalize-word 1) t)"
1500 (if (not (equal select 'noaction))
1501 ;; Add action
1502 (let* ((table (if select 'idlwave-indent-action-table
1503 'idlwave-indent-expand-table))
1504 (table-key (regexp-quote key))
1505 (cell (assoc table-key (eval table))))
1506 (if cell
1507 ;; Replace action command
1508 (setcdr cell cmd)
1509 ;; New action
1510 (set table (append (eval table) (list (cons table-key cmd)))))))
1511 ;; Make key binding for action
1512 (if (or (and (null select) (= (length key) 1))
1513 (equal select 'noaction)
1514 (equal select 'both))
1515 (define-key idlwave-mode-map key
1516 (append '(lambda ()
1517 (interactive)
1518 (self-insert-command 1))
1519 (list (if (listp cmd)
1520 cmd
1521 (list cmd)))))))
1522
1523 (fset 'idlwave-debug-map (make-sparse-keymap))
1524
1525 (define-key idlwave-mode-map "\C-c " 'idlwave-hard-tab)
1526 (define-key idlwave-mode-map [(control tab)] 'idlwave-hard-tab)
1527 ;(define-key idlwave-mode-map "\C-c\C- " 'idlwave-hard-tab)
1528 (define-key idlwave-mode-map "'" 'idlwave-show-matching-quote)
1529 (define-key idlwave-mode-map "\"" 'idlwave-show-matching-quote)
1530 (define-key idlwave-mode-map "\C-g" 'idlwave-keyboard-quit)
1531 (define-key idlwave-mode-map "\C-c;" 'idlwave-toggle-comment-region)
1532 (define-key idlwave-mode-map "\C-\M-a" 'idlwave-beginning-of-subprogram)
1533 (define-key idlwave-mode-map "\C-\M-e" 'idlwave-end-of-subprogram)
1534 (define-key idlwave-mode-map "\C-c{" 'idlwave-beginning-of-block)
1535 (define-key idlwave-mode-map "\C-c}" 'idlwave-end-of-block)
1536 (define-key idlwave-mode-map "\C-c]" 'idlwave-close-block)
1537 (define-key idlwave-mode-map [(meta control h)] 'idlwave-mark-subprogram)
1538 (define-key idlwave-mode-map "\M-\C-n" 'idlwave-forward-block)
1539 (define-key idlwave-mode-map "\M-\C-p" 'idlwave-backward-block)
1540 (define-key idlwave-mode-map "\M-\C-d" 'idlwave-down-block)
1541 (define-key idlwave-mode-map "\M-\C-u" 'idlwave-backward-up-block)
1542 (define-key idlwave-mode-map "\M-\r" 'idlwave-split-line)
1543 (define-key idlwave-mode-map "\M-\C-q" 'idlwave-indent-subprogram)
1544 (define-key idlwave-mode-map "\C-c\C-p" 'idlwave-previous-statement)
1545 (define-key idlwave-mode-map "\C-c\C-n" 'idlwave-next-statement)
1546 ;; (define-key idlwave-mode-map "\r" 'idlwave-newline)
1547 ;; (define-key idlwave-mode-map "\t" 'idlwave-indent-line)
1548 (define-key idlwave-mode-map [(shift iso-lefttab)] 'idlwave-indent-statement)
1549 (define-key idlwave-mode-map "\C-c\C-a" 'idlwave-auto-fill-mode)
1550 (define-key idlwave-mode-map "\M-q" 'idlwave-fill-paragraph)
1551 (define-key idlwave-mode-map "\M-s" 'idlwave-edit-in-idlde)
1552 (define-key idlwave-mode-map "\C-c\C-h" 'idlwave-doc-header)
1553 (define-key idlwave-mode-map "\C-c\C-m" 'idlwave-doc-modification)
1554 (define-key idlwave-mode-map "\C-c\C-c" 'idlwave-case)
1555 (define-key idlwave-mode-map "\C-c\C-d" 'idlwave-debug-map)
1556 (when (and (boundp 'idlwave-shell-debug-modifiers)
1557 (listp idlwave-shell-debug-modifiers)
1558 (not (equal idlwave-shell-debug-modifiers '())))
1559 ;; Bind the debug commands also with the special modifiers.
1560 (let ((shift (memq 'shift idlwave-shell-debug-modifiers))
1561 (mods-noshift (delq 'shift
1562 (copy-sequence idlwave-shell-debug-modifiers))))
1563 (define-key idlwave-mode-map
1564 (vector (append mods-noshift (list (if shift ?C ?c))))
1565 'idlwave-shell-save-and-run)
1566 (define-key idlwave-mode-map
1567 (vector (append mods-noshift (list (if shift ?B ?b))))
1568 'idlwave-shell-break-here)
1569 (define-key idlwave-mode-map
1570 (vector (append mods-noshift (list (if shift ?E ?e))))
1571 'idlwave-shell-run-region)))
1572 (define-key idlwave-mode-map "\C-c\C-d\C-c" 'idlwave-shell-save-and-run)
1573 (define-key idlwave-mode-map "\C-c\C-d\C-b" 'idlwave-shell-break-here)
1574 (define-key idlwave-mode-map "\C-c\C-d\C-e" 'idlwave-shell-run-region)
1575 (define-key idlwave-mode-map "\C-c\C-f" 'idlwave-for)
1576 ;; (define-key idlwave-mode-map "\C-c\C-f" 'idlwave-function)
1577 ;; (define-key idlwave-mode-map "\C-c\C-p" 'idlwave-procedure)
1578 (define-key idlwave-mode-map "\C-c\C-r" 'idlwave-repeat)
1579 (define-key idlwave-mode-map "\C-c\C-w" 'idlwave-while)
1580 (define-key idlwave-mode-map "\C-c\C-k" 'idlwave-kill-autoloaded-buffers)
1581 (define-key idlwave-mode-map "\C-c\C-s" 'idlwave-shell)
1582 (define-key idlwave-mode-map "\C-c\C-l" 'idlwave-shell-recenter-shell-window)
1583 (define-key idlwave-mode-map "\C-c\C-b" 'idlwave-list-buffer-load-path-shadows)
1584 (autoload 'idlwave-shell "idlw-shell"
1585 "Run an inferior IDL, with I/O through buffer `(idlwave-shell-buffer)'." t)
1586 (autoload 'idlwave-shell-send-command "idlw-shell")
1587 (autoload 'idlwave-shell-recenter-shell-window "idlw-shell"
1588 "Run `idlwave-shell' and switch back to current window" t)
1589 (autoload 'idlwave-shell-save-and-run "idlw-shell"
1590 "Save and run buffer under the shell." t)
1591 (autoload 'idlwave-shell-break-here "idlw-shell"
1592 "Set breakpoint in current line." t)
1593 (autoload 'idlwave-shell-run-region "idlw-shell"
1594 "Compile and run the region." t)
1595 (define-key idlwave-mode-map "\C-c\C-v" 'idlwave-find-module)
1596 (define-key idlwave-mode-map "\C-c\C-t" 'idlwave-find-module-this-file)
1597 (define-key idlwave-mode-map "\C-c?" 'idlwave-routine-info)
1598 (define-key idlwave-mode-map "\M-?" 'idlwave-context-help)
1599 (define-key idlwave-mode-map [(control meta ?\?)]
1600 'idlwave-help-assistant-help-with-topic)
1601 ;; Pickup both forms of Esc/Meta binding
1602 (define-key idlwave-mode-map [(meta tab)] 'idlwave-complete)
1603 (define-key idlwave-mode-map [?\e?\t] 'idlwave-complete)
1604 (define-key idlwave-mode-map "\M-\C-i" 'idlwave-complete)
1605 (define-key idlwave-mode-map "\C-c\C-i" 'idlwave-update-routine-info)
1606 (define-key idlwave-mode-map "\C-c=" 'idlwave-resolve)
1607 (define-key idlwave-mode-map
1608 (if (featurep 'xemacs) [(shift button3)] [(shift mouse-3)])
1609 'idlwave-mouse-context-help)
1610
1611 ;; Set action and key bindings.
1612 ;; See description of the function `idlwave-action-and-binding'.
1613 ;; Automatically add spaces for the following characters
1614
1615 ;; Actions for & are complicated by &&
1616 (idlwave-action-and-binding "&" 'idlwave-custom-ampersand-surround)
1617
1618 ;; Automatically add spaces to equal sign if not keyword. This needs
1619 ;; to go ahead of > and <, so >= and <= will be treated correctly
1620 (idlwave-action-and-binding "=" '(idlwave-expand-equal -1 -1))
1621
1622 ;; Actions for > and < are complicated by >=, <=, and ->...
1623 (idlwave-action-and-binding "<" '(idlwave-custom-ltgtr-surround nil))
1624 (idlwave-action-and-binding ">" '(idlwave-custom-ltgtr-surround 'gtr))
1625
1626 (idlwave-action-and-binding "," '(idlwave-surround 0 -1 1))
1627
1628
1629 ;;;
1630 ;;; Abbrev Section
1631 ;;;
1632 ;;; When expanding abbrevs and the abbrev hook moves backward, an extra
1633 ;;; space is inserted (this is the space typed by the user to expanded
1634 ;;; the abbrev).
1635 ;;;
1636 (defvar idlwave-mode-abbrev-table nil
1637 "Abbreviation table used for IDLWAVE mode")
1638 (define-abbrev-table 'idlwave-mode-abbrev-table ())
1639
1640 (defun idlwave-define-abbrev (name expansion hook &optional noprefix table)
1641 "Define-abbrev with backward compatibility.
1642
1643 If NOPREFIX is non-nil, don't prepend prefix character. Installs into
1644 idlwave-mode-abbrev-table unless TABLE is non-nil."
1645 (let ((abbrevs-changed nil) ;; mask the current value to avoid save
1646 (args (list (or table idlwave-mode-abbrev-table)
1647 (if noprefix name (concat idlwave-abbrev-start-char name))
1648 expansion
1649 hook)))
1650 (condition-case nil
1651 (apply 'define-abbrev (append args '(0 t)))
1652 (error (apply 'define-abbrev args)))))
1653
1654 (condition-case nil
1655 (modify-syntax-entry (string-to-char idlwave-abbrev-start-char)
1656 "w" idlwave-mode-syntax-table)
1657 (error nil))
1658
1659 ;;
1660 ;; Templates
1661 ;;
1662 (idlwave-define-abbrev "c" "" (idlwave-code-abbrev idlwave-case))
1663 (idlwave-define-abbrev "sw" "" (idlwave-code-abbrev idlwave-switch))
1664 (idlwave-define-abbrev "f" "" (idlwave-code-abbrev idlwave-for))
1665 (idlwave-define-abbrev "fu" "" (idlwave-code-abbrev idlwave-function))
1666 (idlwave-define-abbrev "pr" "" (idlwave-code-abbrev idlwave-procedure))
1667 (idlwave-define-abbrev "r" "" (idlwave-code-abbrev idlwave-repeat))
1668 (idlwave-define-abbrev "w" "" (idlwave-code-abbrev idlwave-while))
1669 (idlwave-define-abbrev "i" "" (idlwave-code-abbrev idlwave-if))
1670 (idlwave-define-abbrev "elif" "" (idlwave-code-abbrev idlwave-elif))
1671 ;;
1672 ;; Keywords, system functions, conversion routines
1673 ;;
1674 (idlwave-define-abbrev "ap" "arg_present()" (idlwave-keyword-abbrev 1))
1675 (idlwave-define-abbrev "b" "begin" (idlwave-keyword-abbrev 0 t))
1676 (idlwave-define-abbrev "co" "common" (idlwave-keyword-abbrev 0 t))
1677 (idlwave-define-abbrev "cb" "byte()" (idlwave-keyword-abbrev 1))
1678 (idlwave-define-abbrev "cx" "fix()" (idlwave-keyword-abbrev 1))
1679 (idlwave-define-abbrev "cl" "long()" (idlwave-keyword-abbrev 1))
1680 (idlwave-define-abbrev "cf" "float()" (idlwave-keyword-abbrev 1))
1681 (idlwave-define-abbrev "cs" "string()" (idlwave-keyword-abbrev 1))
1682 (idlwave-define-abbrev "cc" "complex()" (idlwave-keyword-abbrev 1))
1683 (idlwave-define-abbrev "cd" "double()" (idlwave-keyword-abbrev 1))
1684 (idlwave-define-abbrev "e" "else" (idlwave-keyword-abbrev 0 t))
1685 (idlwave-define-abbrev "ec" "endcase" 'idlwave-show-begin)
1686 (idlwave-define-abbrev "es" "endswitch" 'idlwave-show-begin)
1687 (idlwave-define-abbrev "ee" "endelse" 'idlwave-show-begin)
1688 (idlwave-define-abbrev "ef" "endfor" 'idlwave-show-begin)
1689 (idlwave-define-abbrev "ei" "endif else if" 'idlwave-show-begin)
1690 (idlwave-define-abbrev "el" "endif else" 'idlwave-show-begin)
1691 (idlwave-define-abbrev "en" "endif" 'idlwave-show-begin)
1692 (idlwave-define-abbrev "er" "endrep" 'idlwave-show-begin)
1693 (idlwave-define-abbrev "ew" "endwhile" 'idlwave-show-begin)
1694 (idlwave-define-abbrev "g" "goto," (idlwave-keyword-abbrev 0 t))
1695 (idlwave-define-abbrev "h" "help," (idlwave-keyword-abbrev 0))
1696 (idlwave-define-abbrev "k" "keyword_set()" (idlwave-keyword-abbrev 1))
1697 (idlwave-define-abbrev "n" "n_elements()" (idlwave-keyword-abbrev 1))
1698 (idlwave-define-abbrev "on" "on_error," (idlwave-keyword-abbrev 0))
1699 (idlwave-define-abbrev "oi" "on_ioerror," (idlwave-keyword-abbrev 0 1))
1700 (idlwave-define-abbrev "ow" "openw," (idlwave-keyword-abbrev 0))
1701 (idlwave-define-abbrev "or" "openr," (idlwave-keyword-abbrev 0))
1702 (idlwave-define-abbrev "ou" "openu," (idlwave-keyword-abbrev 0))
1703 (idlwave-define-abbrev "p" "print," (idlwave-keyword-abbrev 0))
1704 (idlwave-define-abbrev "pt" "plot," (idlwave-keyword-abbrev 0))
1705 (idlwave-define-abbrev "re" "read," (idlwave-keyword-abbrev 0))
1706 (idlwave-define-abbrev "rf" "readf," (idlwave-keyword-abbrev 0))
1707 (idlwave-define-abbrev "ru" "readu," (idlwave-keyword-abbrev 0))
1708 (idlwave-define-abbrev "rt" "return" (idlwave-keyword-abbrev 0))
1709 (idlwave-define-abbrev "sc" "strcompress()" (idlwave-keyword-abbrev 1))
1710 (idlwave-define-abbrev "sn" "strlen()" (idlwave-keyword-abbrev 1))
1711 (idlwave-define-abbrev "sl" "strlowcase()" (idlwave-keyword-abbrev 1))
1712 (idlwave-define-abbrev "su" "strupcase()" (idlwave-keyword-abbrev 1))
1713 (idlwave-define-abbrev "sm" "strmid()" (idlwave-keyword-abbrev 1))
1714 (idlwave-define-abbrev "sp" "strpos()" (idlwave-keyword-abbrev 1))
1715 (idlwave-define-abbrev "st" "strput()" (idlwave-keyword-abbrev 1))
1716 (idlwave-define-abbrev "sr" "strtrim()" (idlwave-keyword-abbrev 1))
1717 (idlwave-define-abbrev "t" "then" (idlwave-keyword-abbrev 0 t))
1718 (idlwave-define-abbrev "u" "until" (idlwave-keyword-abbrev 0 t))
1719 (idlwave-define-abbrev "wu" "writeu," (idlwave-keyword-abbrev 0))
1720 (idlwave-define-abbrev "iap" "if arg_present() then" (idlwave-keyword-abbrev 6))
1721 (idlwave-define-abbrev "ik" "if keyword_set() then" (idlwave-keyword-abbrev 6))
1722 (idlwave-define-abbrev "ine" "if n_elements() eq 0 then" (idlwave-keyword-abbrev 11))
1723 (idlwave-define-abbrev "inn" "if n_elements() ne 0 then" (idlwave-keyword-abbrev 11))
1724 (idlwave-define-abbrev "np" "n_params()" (idlwave-keyword-abbrev 0))
1725 (idlwave-define-abbrev "s" "size()" (idlwave-keyword-abbrev 1))
1726 (idlwave-define-abbrev "wi" "widget_info()" (idlwave-keyword-abbrev 1))
1727 (idlwave-define-abbrev "wc" "widget_control," (idlwave-keyword-abbrev 0))
1728 (idlwave-define-abbrev "pv" "ptr_valid()" (idlwave-keyword-abbrev 1))
1729 (idlwave-define-abbrev "ipv" "if ptr_valid() then" (idlwave-keyword-abbrev 6))
1730
1731 ;; This section is reserved words only. (From IDL user manual)
1732 ;;
1733 (idlwave-define-abbrev "and" "and" (idlwave-keyword-abbrev 0 t) t)
1734 (idlwave-define-abbrev "begin" "begin" (idlwave-keyword-abbrev 0 t) t)
1735 (idlwave-define-abbrev "break" "break" (idlwave-keyword-abbrev 0 t) t)
1736 (idlwave-define-abbrev "case" "case" (idlwave-keyword-abbrev 0 t) t)
1737 (idlwave-define-abbrev "common" "common" (idlwave-keyword-abbrev 0 t) t)
1738 (idlwave-define-abbrev "continue" "continue" (idlwave-keyword-abbrev 0 t) t)
1739 (idlwave-define-abbrev "do" "do" (idlwave-keyword-abbrev 0 t) t)
1740 (idlwave-define-abbrev "else" "else" (idlwave-keyword-abbrev 0 t) t)
1741 (idlwave-define-abbrev "end" "end" 'idlwave-show-begin-check t)
1742 (idlwave-define-abbrev "endcase" "endcase" 'idlwave-show-begin-check t)
1743 (idlwave-define-abbrev "endelse" "endelse" 'idlwave-show-begin-check t)
1744 (idlwave-define-abbrev "endfor" "endfor" 'idlwave-show-begin-check t)
1745 (idlwave-define-abbrev "endif" "endif" 'idlwave-show-begin-check t)
1746 (idlwave-define-abbrev "endrep" "endrep" 'idlwave-show-begin-check t)
1747 (idlwave-define-abbrev "endswitch" "endswitch" 'idlwave-show-begin-check t)
1748 (idlwave-define-abbrev "endwhi" "endwhi" 'idlwave-show-begin-check t)
1749 (idlwave-define-abbrev "endwhile" "endwhile" 'idlwave-show-begin-check t)
1750 (idlwave-define-abbrev "eq" "eq" (idlwave-keyword-abbrev 0 t) t)
1751 (idlwave-define-abbrev "for" "for" (idlwave-keyword-abbrev 0 t) t)
1752 (idlwave-define-abbrev "function" "function" (idlwave-keyword-abbrev 0 t) t)
1753 (idlwave-define-abbrev "ge" "ge" (idlwave-keyword-abbrev 0 t) t)
1754 (idlwave-define-abbrev "goto" "goto" (idlwave-keyword-abbrev 0 t) t)
1755 (idlwave-define-abbrev "gt" "gt" (idlwave-keyword-abbrev 0 t) t)
1756 (idlwave-define-abbrev "if" "if" (idlwave-keyword-abbrev 0 t) t)
1757 (idlwave-define-abbrev "le" "le" (idlwave-keyword-abbrev 0 t) t)
1758 (idlwave-define-abbrev "lt" "lt" (idlwave-keyword-abbrev 0 t) t)
1759 (idlwave-define-abbrev "mod" "mod" (idlwave-keyword-abbrev 0 t) t)
1760 (idlwave-define-abbrev "ne" "ne" (idlwave-keyword-abbrev 0 t) t)
1761 (idlwave-define-abbrev "not" "not" (idlwave-keyword-abbrev 0 t) t)
1762 (idlwave-define-abbrev "of" "of" (idlwave-keyword-abbrev 0 t) t)
1763 (idlwave-define-abbrev "on_ioerror" "on_ioerror" (idlwave-keyword-abbrev 0 t) t)
1764 (idlwave-define-abbrev "or" "or" (idlwave-keyword-abbrev 0 t) t)
1765 (idlwave-define-abbrev "pro" "pro" (idlwave-keyword-abbrev 0 t) t)
1766 (idlwave-define-abbrev "repeat" "repeat" (idlwave-keyword-abbrev 0 t) t)
1767 (idlwave-define-abbrev "switch" "switch" (idlwave-keyword-abbrev 0 t) t)
1768 (idlwave-define-abbrev "then" "then" (idlwave-keyword-abbrev 0 t) t)
1769 (idlwave-define-abbrev "until" "until" (idlwave-keyword-abbrev 0 t) t)
1770 (idlwave-define-abbrev "while" "while" (idlwave-keyword-abbrev 0 t) t)
1771 (idlwave-define-abbrev "xor" "xor" (idlwave-keyword-abbrev 0 t) t)
1772
1773 (defvar imenu-create-index-function)
1774 (defvar extract-index-name-function)
1775 (defvar prev-index-position-function)
1776 (defvar imenu-extract-index-name-function)
1777 (defvar imenu-prev-index-position-function)
1778 ;; defined later - so just make the compiler hush
1779 (defvar idlwave-mode-menu)
1780 (defvar idlwave-mode-debug-menu)
1781
1782 ;;;###autoload
1783 (defun idlwave-mode ()
1784 "Major mode for editing IDL source files (version 6.1_em22).
1785
1786 The main features of this mode are
1787
1788 1. Indentation and Formatting
1789 --------------------------
1790 Like other Emacs programming modes, C-j inserts a newline and indents.
1791 TAB is used for explicit indentation of the current line.
1792
1793 To start a continuation line, use \\[idlwave-split-line]. This
1794 function can also be used in the middle of a line to split the line
1795 at that point. When used inside a long constant string, the string
1796 is split at that point with the `+' concatenation operator.
1797
1798 Comments are indented as follows:
1799
1800 `;;;' Indentation remains unchanged.
1801 `;;' Indent like the surrounding code
1802 `;' Indent to a minimum column.
1803
1804 The indentation of comments starting in column 0 is never changed.
1805
1806 Use \\[idlwave-fill-paragraph] to refill a paragraph inside a
1807 comment. The indentation of the second line of the paragraph
1808 relative to the first will be retained. Use
1809 \\[idlwave-auto-fill-mode] to toggle auto-fill mode for these
1810 comments. When the variable `idlwave-fill-comment-line-only' is
1811 nil, code can also be auto-filled and auto-indented.
1812
1813 To convert pre-existing IDL code to your formatting style, mark the
1814 entire buffer with \\[mark-whole-buffer] and execute
1815 \\[idlwave-expand-region-abbrevs]. Then mark the entire buffer
1816 again followed by \\[indent-region] (`indent-region').
1817
1818 2. Routine Info
1819 ------------
1820 IDLWAVE displays information about the calling sequence and the
1821 accepted keyword parameters of a procedure or function with
1822 \\[idlwave-routine-info]. \\[idlwave-find-module] jumps to the
1823 source file of a module. These commands know about system
1824 routines, all routines in idlwave-mode buffers and (when the
1825 idlwave-shell is active) about all modules currently compiled under
1826 this shell. It also makes use of pre-compiled or custom-scanned
1827 user and library catalogs many popular libraries ship with by
1828 default. Use \\[idlwave-update-routine-info] to update this
1829 information, which is also used for completion (see item 4).
1830
1831 3. Online IDL Help
1832 ---------------
1833
1834 \\[idlwave-context-help] displays the IDL documentation relevant
1835 for the system variable, keyword, or routines at point. A single
1836 key stroke gets you directly to the right place in the docs. See
1837 the manual to configure where and how the HTML help is displayed.
1838
1839 4. Completion
1840 ----------
1841 \\[idlwave-complete] completes the names of procedures, functions
1842 class names, keyword parameters, system variables and tags, class
1843 tags, structure tags, filenames and much more. It is context
1844 sensitive and figures out what is expected at point. Lower case
1845 strings are completed in lower case, other strings in mixed or
1846 upper case.
1847
1848 5. Code Templates and Abbreviations
1849 --------------------------------
1850 Many Abbreviations are predefined to expand to code fragments and templates.
1851 The abbreviations start generally with a `\\`. Some examples
1852
1853 \\pr PROCEDURE template
1854 \\fu FUNCTION template
1855 \\c CASE statement template
1856 \\sw SWITCH statement template
1857 \\f FOR loop template
1858 \\r REPEAT Loop template
1859 \\w WHILE loop template
1860 \\i IF statement template
1861 \\elif IF-ELSE statement template
1862 \\b BEGIN
1863
1864 For a full list, use \\[idlwave-list-abbrevs]. Some templates also
1865 have direct keybindings - see the list of keybindings below.
1866
1867 \\[idlwave-doc-header] inserts a documentation header at the
1868 beginning of the current program unit (pro, function or main).
1869 Change log entries can be added to the current program unit with
1870 \\[idlwave-doc-modification].
1871
1872 6. Automatic Case Conversion
1873 -------------------------
1874 The case of reserved words and some abbrevs is controlled by
1875 `idlwave-reserved-word-upcase' and `idlwave-abbrev-change-case'.
1876
1877 7. Automatic END completion
1878 ------------------------
1879 If the variable `idlwave-expand-generic-end' is non-nil, each END typed
1880 will be converted to the specific version, like ENDIF, ENDFOR, etc.
1881
1882 8. Hooks
1883 -----
1884 Loading idlwave.el runs `idlwave-load-hook'.
1885 Turning on `idlwave-mode' runs `idlwave-mode-hook'.
1886
1887 9. Documentation and Customization
1888 -------------------------------
1889 Info documentation for this package is available. Use
1890 \\[idlwave-info] to display (complain to your sysadmin if that does
1891 not work). For Postscript, PDF, and HTML versions of the
1892 documentation, check IDLWAVE's homepage at `http://idlwave.org'.
1893 IDLWAVE has customize support - see the group `idlwave'.
1894
1895 10.Keybindings
1896 -----------
1897 Here is a list of all keybindings of this mode.
1898 If some of the key bindings below show with ??, use \\[describe-key]
1899 followed by the key sequence to see what the key sequence does.
1900
1901 \\{idlwave-mode-map}"
1902
1903 (interactive)
1904 (kill-all-local-variables)
1905
1906 (if idlwave-startup-message
1907 (message "Emacs IDLWAVE mode version %s." idlwave-mode-version))
1908 (setq idlwave-startup-message nil)
1909
1910 (setq local-abbrev-table idlwave-mode-abbrev-table)
1911 (set-syntax-table idlwave-mode-syntax-table)
1912
1913 (set (make-local-variable 'indent-line-function) 'idlwave-indent-and-action)
1914
1915 (make-local-variable idlwave-comment-indent-function)
1916 (set idlwave-comment-indent-function 'idlwave-comment-hook)
1917
1918 (set (make-local-variable 'comment-start-skip) ";+[ \t]*")
1919 (set (make-local-variable 'comment-start) ";")
1920 (set (make-local-variable 'comment-add) 1) ; ";;" for new and regions
1921 (set (make-local-variable 'require-final-newline) t)
1922 (set (make-local-variable 'abbrev-all-caps) t)
1923 (set (make-local-variable 'indent-tabs-mode) nil)
1924 (set (make-local-variable 'completion-ignore-case) t)
1925
1926 (use-local-map idlwave-mode-map)
1927
1928 (when (featurep 'easymenu)
1929 (easy-menu-add idlwave-mode-menu idlwave-mode-map)
1930 (easy-menu-add idlwave-mode-debug-menu idlwave-mode-map))
1931
1932 (setq mode-name "IDLWAVE")
1933 (setq major-mode 'idlwave-mode)
1934 (setq abbrev-mode t)
1935
1936 (set (make-local-variable idlwave-fill-function) 'idlwave-auto-fill)
1937 (setq comment-end "")
1938 (set (make-local-variable 'comment-multi-line) nil)
1939 (set (make-local-variable 'paragraph-separate)
1940 "[ \t\f]*$\\|[ \t]*;+[ \t]*$\\|;+[+=-_*]+$")
1941 (set (make-local-variable 'paragraph-start) "[ \t\f]\\|[ \t]*;+[ \t]")
1942 (set (make-local-variable 'paragraph-ignore-fill-prefix) nil)
1943 (set (make-local-variable 'parse-sexp-ignore-comments) t)
1944
1945 ;; ChangeLog
1946 (set (make-local-variable 'add-log-current-defun-function)
1947 'idlwave-current-routine-fullname)
1948
1949 ;; Set tag table list to use IDLTAGS as file name.
1950 (if (boundp 'tag-table-alist)
1951 (add-to-list 'tag-table-alist '("\\.pro$" . "IDLTAGS")))
1952
1953 ;; Font-lock additions
1954 ;; Following line is for Emacs - XEmacs uses the corresponding property
1955 ;; on the `idlwave-mode' symbol.
1956 (set (make-local-variable 'font-lock-defaults) idlwave-font-lock-defaults)
1957 (set (make-local-variable 'font-lock-mark-block-function)
1958 'idlwave-mark-subprogram)
1959 (set (make-local-variable 'font-lock-fontify-region-function)
1960 'idlwave-font-lock-fontify-region)
1961
1962 ;; Imenu setup
1963 (set (make-local-variable 'imenu-create-index-function)
1964 'imenu-default-create-index-function)
1965 (set (make-local-variable 'imenu-extract-index-name-function)
1966 'idlwave-unit-name)
1967 (set (make-local-variable 'imenu-prev-index-position-function)
1968 'idlwave-prev-index-position)
1969
1970 ;; HideShow setup
1971 (add-to-list 'hs-special-modes-alist
1972 (list 'idlwave-mode
1973 idlwave-begin-block-reg
1974 idlwave-end-block-reg
1975 ";"
1976 'idlwave-forward-block nil))
1977
1978 ;; Make a local post-command-hook and add our hook to it
1979 ;; NB: `make-local-hook' needed for older/alternative Emacs compatibility
1980 ;; (make-local-hook 'post-command-hook)
1981 (add-hook 'post-command-hook 'idlwave-command-hook nil 'local)
1982
1983 ;; Make local hooks for buffer updates
1984 ;; NB: `make-local-hook' needed for older/alternative Emacs compatibility
1985 ;; (make-local-hook 'kill-buffer-hook)
1986 (add-hook 'kill-buffer-hook 'idlwave-kill-buffer-update nil 'local)
1987 ;; (make-local-hook 'after-save-hook)
1988 (add-hook 'after-save-hook 'idlwave-save-buffer-update nil 'local)
1989 (add-hook 'after-save-hook 'idlwave-revoke-license-to-kill nil 'local)
1990
1991 ;; Setup directories and file, if necessary
1992 (idlwave-setup)
1993
1994 ;; Update the routine info with info about current buffer?
1995 (idlwave-new-buffer-update)
1996
1997 ;; Check help location
1998 (idlwave-help-check-locations)
1999
2000 ;; Run the mode hook
2001 (run-mode-hooks 'idlwave-mode-hook))
2002
2003 (defvar idlwave-setup-done nil)
2004 (defun idlwave-setup ()
2005 (unless idlwave-setup-done
2006 (if (not (file-directory-p idlwave-config-directory))
2007 (make-directory idlwave-config-directory))
2008 (setq
2009 idlwave-user-catalog-file (expand-file-name
2010 idlwave-user-catalog-file
2011 idlwave-config-directory)
2012 idlwave-xml-system-rinfo-converted-file
2013 (expand-file-name
2014 idlwave-xml-system-rinfo-converted-file
2015 idlwave-config-directory)
2016 idlwave-path-file (expand-file-name
2017 idlwave-path-file
2018 idlwave-config-directory))
2019 (idlwave-read-paths) ; we may need these early
2020 (setq idlwave-setup-done t)))
2021
2022 (defun idlwave-font-lock-fontify-region (beg end &optional verbose)
2023 "Fontify continuation lines correctly."
2024 (let (pos)
2025 (save-excursion
2026 (goto-char beg)
2027 (forward-line -1)
2028 (when (setq pos (idlwave-is-continuation-line))
2029 (goto-char pos)
2030 (idlwave-beginning-of-statement)
2031 (setq beg (point)))))
2032 (font-lock-default-fontify-region beg end verbose))
2033
2034 ;;
2035 ;; Code Formatting ----------------------------------------------------
2036 ;;
2037
2038 (defun idlwave-hard-tab ()
2039 "Inserts TAB in buffer in current position."
2040 (interactive)
2041 (insert "\t"))
2042
2043 ;;; This stuff is experimental
2044
2045 (defvar idlwave-command-hook nil
2046 "If non-nil, a list that can be evaluated using `eval'.
2047 It is evaluated in the lisp function `idlwave-command-hook' which is
2048 placed in `post-command-hook'.")
2049
2050 (defun idlwave-command-hook ()
2051 "Command run after every command.
2052 Evaluates a non-nil value of the *variable* `idlwave-command-hook' and
2053 sets the variable to zero afterwards."
2054 (and idlwave-command-hook
2055 (listp idlwave-command-hook)
2056 (condition-case nil
2057 (eval idlwave-command-hook)
2058 (error nil)))
2059 (setq idlwave-command-hook nil))
2060
2061 ;;; End experiment
2062
2063 ;; It would be better to use expand.el for better abbrev handling and
2064 ;; versatility.
2065
2066 (defun idlwave-check-abbrev (arg &optional reserved)
2067 "Reverses abbrev expansion if in comment or string.
2068 Argument ARG is the number of characters to move point
2069 backward if `idlwave-abbrev-move' is non-nil.
2070 If optional argument RESERVED is non-nil then the expansion
2071 consists of reserved words, which will be capitalized if
2072 `idlwave-reserved-word-upcase' is non-nil.
2073 Otherwise, the abbrev will be capitalized if `idlwave-abbrev-change-case'
2074 is non-nil, unless its value is \`down in which case the abbrev will be
2075 made into all lowercase.
2076 Returns non-nil if abbrev is left expanded."
2077 (if (idlwave-quoted)
2078 (progn (unexpand-abbrev)
2079 nil)
2080 (if (and reserved idlwave-reserved-word-upcase)
2081 (upcase-region last-abbrev-location (point))
2082 (cond
2083 ((equal idlwave-abbrev-change-case 'down)
2084 (downcase-region last-abbrev-location (point)))
2085 (idlwave-abbrev-change-case
2086 (upcase-region last-abbrev-location (point)))))
2087 (if (and idlwave-abbrev-move (> arg 0))
2088 (if (boundp 'post-command-hook)
2089 (setq idlwave-command-hook (list 'backward-char (1+ arg)))
2090 (backward-char arg)))
2091 t))
2092
2093 (defun idlwave-in-comment ()
2094 "Returns t if point is inside a comment, nil otherwise."
2095 (save-excursion
2096 (let ((here (point)))
2097 (and (idlwave-goto-comment) (> here (point))))))
2098
2099 (defun idlwave-goto-comment ()
2100 "Move to start of comment delimiter on current line.
2101 Moves to end of line if there is no comment delimiter.
2102 Ignores comment delimiters in strings.
2103 Returns point if comment found and nil otherwise."
2104 (let ((eos (progn (end-of-line) (point)))
2105 (data (match-data))
2106 found)
2107 ;; Look for first comment delimiter not in a string
2108 (beginning-of-line)
2109 (setq found (search-forward comment-start eos 'lim))
2110 (while (and found (idlwave-in-quote))
2111 (setq found (search-forward comment-start eos 'lim)))
2112 (store-match-data data)
2113 (and found (not (idlwave-in-quote))
2114 (progn
2115 (backward-char 1)
2116 (point)))))
2117
2118 (defvar transient-mark-mode)
2119 (defvar zmacs-regions)
2120 (defvar mark-active)
2121 (defun idlwave-region-active-p ()
2122 "Is transient-mark-mode on and the region active?
2123 Works on both Emacs and XEmacs."
2124 (if (featurep 'xemacs)
2125 (and zmacs-regions (region-active-p))
2126 (and transient-mark-mode mark-active)))
2127
2128 (defun idlwave-show-matching-quote ()
2129 "Insert quote and show matching quote if this is end of a string."
2130 (interactive)
2131 (let ((bq (idlwave-in-quote))
2132 (inq last-command-char))
2133 (if (and bq (not (idlwave-in-comment)))
2134 (let ((delim (char-after bq)))
2135 (insert inq)
2136 (if (eq inq delim)
2137 (save-excursion
2138 (goto-char bq)
2139 (sit-for 1))))
2140 ;; Not the end of a string
2141 (insert inq))))
2142
2143 (defun idlwave-show-begin-check ()
2144 "Ensure that the previous word was a token before `idlwave-show-begin'.
2145 An END token must be preceded by whitespace."
2146 (if (not (idlwave-quoted))
2147 (if
2148 (save-excursion
2149 (backward-word 1)
2150 (backward-char 1)
2151 (looking-at "[ \t\n\f]"))
2152 (idlwave-show-begin))))
2153
2154 (defun idlwave-show-begin ()
2155 "Finds the start of current block and blinks to it for a second.
2156 Also checks if the correct end statement has been used."
2157 ;; All end statements are reserved words
2158 ;; Re-indent end line
2159 ;;(insert-char ?\ 1) ;; So indent, etc. work well
2160 ;;(backward-char 1)
2161 (let* ((pos (point-marker))
2162 (last-abbrev-marker (copy-marker last-abbrev-location))
2163 (eol-pos (save-excursion (end-of-line) (point)))
2164 begin-pos end-pos end end1 )
2165 (if idlwave-reindent-end (idlwave-indent-line))
2166 (setq last-abbrev-location (marker-position last-abbrev-marker))
2167 (when (and (idlwave-check-abbrev 0 t)
2168 idlwave-show-block)
2169 (save-excursion
2170 ;; Move inside current block
2171 (goto-char last-abbrev-marker)
2172 (idlwave-block-jump-out -1 'nomark)
2173 (setq begin-pos (point))
2174 (idlwave-block-jump-out 1 'nomark)
2175 (setq end-pos (point))
2176 (if (> end-pos eol-pos)
2177 (setq end-pos pos))
2178 (goto-char end-pos)
2179 (setq end (buffer-substring
2180 (progn
2181 (skip-chars-backward "a-zA-Z")
2182 (point))
2183 end-pos))
2184 (goto-char begin-pos)
2185 (when (setq end1 (cdr (idlwave-block-master)))
2186 (cond
2187 ((null end1)) ; no-operation
2188 ((string= (downcase end) (downcase end1))
2189 (sit-for 1))
2190 ((string= (downcase end) "end")
2191 ;; A generic end
2192 (if idlwave-expand-generic-end
2193 (save-excursion
2194 (goto-char pos)
2195 (backward-char 3)
2196 (insert (if (string= end "END") (upcase end1) end1))
2197 (delete-char 3)))
2198 (sit-for 1))
2199 (t
2200 (beep)
2201 (message "Warning: Shouldn't this be \"%s\" instead of \"%s\"?"
2202 end1 end)
2203 (sit-for 1))))))))
2204 ;;(delete-char 1))
2205
2206 (defun idlwave-block-master ()
2207 (let ((case-fold-search t))
2208 (save-excursion
2209 (cond
2210 ((looking-at "pro\\|case\\|switch\\|function\\>")
2211 (assoc (downcase (match-string 0)) idlwave-block-matches))
2212 ((looking-at "begin\\>")
2213 (let ((limit (save-excursion
2214 (idlwave-beginning-of-statement)
2215 (point))))
2216 (cond
2217 ((re-search-backward ":[ \t]*\\=" limit t)
2218 ;; seems to be a case thing
2219 '("begin" . "end"))
2220 ((re-search-backward idlwave-block-match-regexp limit t)
2221 (assoc (downcase (match-string 1))
2222 idlwave-block-matches))
2223 (t
2224 ;; Just a normal block
2225 '("begin" . "end")))))
2226 (t nil)))))
2227
2228 (defun idlwave-close-block ()
2229 "Terminate the current block with the correct END statement."
2230 (interactive)
2231 ;; Start new line if we are not in a new line
2232 (unless (save-excursion
2233 (skip-chars-backward " \t")
2234 (bolp))
2235 (let ((idlwave-show-block nil))
2236 (newline-and-indent)))
2237 (let ((last-abbrev-location (point))) ; for upcasing
2238 (insert "end")
2239 (idlwave-show-begin)))
2240
2241 (defun idlwave-custom-ampersand-surround (&optional is-action)
2242 "Surround &, leaving room for && (which surrround as well)."
2243 (let* ((prev-char (char-after (- (point) 2)))
2244 (next-char (char-after (point)))
2245 (amp-left (eq prev-char ?&))
2246 (amp-right (eq next-char ?&))
2247 (len (if amp-left 2 1)))
2248 (unless amp-right ;no need to do it twice, amp-left will catch it.
2249 (idlwave-surround -1 (if (or is-action amp-left) -1) len))))
2250
2251 (defun idlwave-custom-ltgtr-surround (gtr &optional is-action)
2252 "Surround > and < by blanks, leaving room for >= and <=, and considering ->."
2253 (let* ((prev-char (char-after (- (point) 2)))
2254 (next-char (char-after (point)))
2255 (method-invoke (and gtr (eq prev-char ?-)))
2256 (len (if method-invoke 2 1)))
2257 (unless (eq next-char ?=)
2258 ;; Key binding: pad only on left, to save for possible >=/<=
2259 (idlwave-surround -1 (if (or is-action method-invoke) -1) len))))
2260
2261 (defun idlwave-surround (&optional before after length is-action)
2262 "Surround the LENGTH characters before point with blanks.
2263 LENGTH defaults to 1.
2264 Optional arguments BEFORE and AFTER affect the behavior before and
2265 after the characters (see also description of `idlwave-make-space'):
2266
2267 nil do nothing
2268 0 force no spaces
2269 integer > 0 force exactly n spaces
2270 integer < 0 at least |n| spaces
2271
2272 The function does nothing if any of the following conditions is true:
2273 - `idlwave-surround-by-blank' is nil
2274 - the character before point is inside a string or comment"
2275 (when (and idlwave-surround-by-blank (not (idlwave-quoted)))
2276 (let ((length (or length 1))) ; establish a default for LENGTH
2277 (backward-char length)
2278 (save-restriction
2279 (let ((here (point)))
2280 (skip-chars-backward " \t")
2281 (if (bolp)
2282 ;; avoid clobbering indent
2283 (progn
2284 (move-to-column (idlwave-calculate-indent))
2285 (if (<= (point) here)
2286 (narrow-to-region (point) here))
2287 (goto-char here)))
2288 (idlwave-make-space before))
2289 (skip-chars-forward " \t"))
2290 (forward-char length)
2291 (idlwave-make-space after)
2292 ;; Check to see if the line should auto wrap
2293 (if (and (equal (char-after (1- (point))) ?\ )
2294 (> (current-column) fill-column))
2295 (funcall auto-fill-function)))))
2296
2297 (defun idlwave-make-space (n)
2298 "Make space at point.
2299 The space affected is all the spaces and tabs around point.
2300 If n is non-nil then point is left abs(n) spaces from the beginning of
2301 the contiguous space.
2302 The amount of space at point is determined by N.
2303 If the value of N is:
2304 nil - do nothing.
2305 > 0 - exactly N spaces.
2306 < 0 - a minimum of -N spaces, i.e., do not change if there are
2307 already -N spaces.
2308 0 - no spaces (i.e. remove any existing space)."
2309 (if (integerp n)
2310 (let
2311 ((start-col (progn (skip-chars-backward " \t") (current-column)))
2312 (left (point))
2313 (end-col (progn (skip-chars-forward " \t") (current-column))))
2314 (delete-horizontal-space)
2315 (cond
2316 ((> n 0)
2317 (idlwave-indent-to (+ start-col n))
2318 (goto-char (+ left n)))
2319 ((< n 0)
2320 (idlwave-indent-to end-col (- n))
2321 (goto-char (- left n)))
2322 ;; n = 0, done
2323 ))))
2324
2325 (defun idlwave-newline ()
2326 "Inserts a newline and indents the current and previous line."
2327 (interactive)
2328 ;;
2329 ;; Handle unterminated single and double quotes
2330 ;; If not in a comment and in a string then insertion of a newline
2331 ;; will mean unbalanced quotes.
2332 ;;
2333 (if (and (not (idlwave-in-comment)) (idlwave-in-quote))
2334 (progn (beep)
2335 (message "Warning: unbalanced quotes?")))
2336 (newline)
2337 ;;
2338 ;; The current line is being split, the cursor should be at the
2339 ;; beginning of the new line skipping the leading indentation.
2340 ;;
2341 ;; The reason we insert the new line before indenting is that the
2342 ;; indenting could be confused by keywords (e.g. END) on the line
2343 ;; after the split point. This prevents us from just using
2344 ;; `indent-for-tab-command' followed by `newline-and-indent'.
2345 ;;
2346 (beginning-of-line 0)
2347 (idlwave-indent-line)
2348 (forward-line)
2349 (idlwave-indent-line))
2350
2351 ;;
2352 ;; Use global variable 'comment-column' to set parallel comment
2353 ;;
2354 ;; Modeled on lisp.el
2355 ;; Emacs Lisp and IDL (Wave CL) have identical comment syntax
2356 (defun idlwave-comment-hook ()
2357 "Compute indent for the beginning of the IDL comment delimiter."
2358 (if (or (looking-at idlwave-no-change-comment)
2359 (if idlwave-begin-line-comment
2360 (looking-at idlwave-begin-line-comment)
2361 (looking-at "^;")))
2362 (current-column)
2363 (if (looking-at idlwave-code-comment)
2364 (if (save-excursion (skip-chars-backward " \t") (bolp))
2365 ;; On line by itself, indent as code
2366 (let ((tem (idlwave-calculate-indent)))
2367 (if (listp tem) (car tem) tem))
2368 ;; after code - do not change
2369 (current-column))
2370 (skip-chars-backward " \t")
2371 (max (if (bolp) 0 (1+ (current-column)))
2372 comment-column))))
2373
2374 (defun idlwave-split-line ()
2375 "Continue line by breaking line at point and indent the lines.
2376 For a code line insert continuation marker. If the line is a line comment
2377 then the new line will contain a comment with the same indentation.
2378 Splits strings with the IDL operator `+' if `idlwave-split-line-string' is
2379 non-nil."
2380 (interactive)
2381 ;; Expand abbreviation, just like normal RET would.
2382 (and abbrev-mode (expand-abbrev))
2383 (let (beg)
2384 (if (not (idlwave-in-comment))
2385 ;; For code line add continuation.
2386 ;; Check if splitting a string.
2387 (progn
2388 (if (setq beg (idlwave-in-quote))
2389 (if idlwave-split-line-string
2390 ;; Split the string.
2391 (progn (insert (setq beg (char-after beg)) " + "
2392 idlwave-continuation-char beg)
2393 (backward-char 1)
2394 (newline-and-indent)
2395 (forward-char 1))
2396 ;; Do not split the string.
2397 (beep)
2398 (message "Warning: continuation inside string!!")
2399 (insert " " idlwave-continuation-char))
2400 ;; Not splitting a string.
2401 (if (not (member (char-before) '(?\ ?\t)))
2402 (insert " "))
2403 (insert idlwave-continuation-char)
2404 (newline-and-indent)))
2405 (indent-new-comment-line))
2406 ;; Indent previous line
2407 (setq beg (- (point-max) (point)))
2408 (forward-line -1)
2409 (idlwave-indent-line)
2410 (goto-char (- (point-max) beg))
2411 ;; Reindent new line
2412 (idlwave-indent-line)))
2413
2414 (defun idlwave-beginning-of-subprogram ()
2415 "Moves point to the beginning of the current program unit."
2416 (interactive)
2417 (idlwave-find-key idlwave-begin-unit-reg -1))
2418
2419 (defun idlwave-end-of-subprogram ()
2420 "Moves point to the start of the next program unit."
2421 (interactive)
2422 (idlwave-end-of-statement)
2423 (idlwave-find-key idlwave-end-unit-reg 1))
2424
2425 (defun idlwave-mark-statement ()
2426 "Mark current IDL statement."
2427 (interactive)
2428 (idlwave-end-of-statement)
2429 (let ((end (point)))
2430 (idlwave-beginning-of-statement)
2431 (push-mark end nil t)))
2432
2433 (defun idlwave-mark-block ()
2434 "Mark containing block."
2435 (interactive)
2436 (idlwave-end-of-statement)
2437 (idlwave-backward-up-block -1)
2438 (idlwave-end-of-statement)
2439 (let ((end (point)))
2440 (idlwave-backward-block)
2441 (idlwave-beginning-of-statement)
2442 (push-mark end nil t)))
2443
2444
2445 (defun idlwave-mark-subprogram ()
2446 "Put mark at beginning of program, point at end.
2447 The marks are pushed."
2448 (interactive)
2449 (idlwave-end-of-statement)
2450 (idlwave-beginning-of-subprogram)
2451 (let ((beg (point)))
2452 (idlwave-forward-block)
2453 (push-mark beg nil t))
2454 (exchange-point-and-mark))
2455
2456 (defun idlwave-backward-up-block (&optional arg)
2457 "Move to beginning of enclosing block if prefix ARG >= 0.
2458 If prefix ARG < 0 then move forward to enclosing block end."
2459 (interactive "p")
2460 (idlwave-block-jump-out (- arg) 'nomark))
2461
2462 (defun idlwave-beginning-of-block ()
2463 "Go to the beginning of the current block."
2464 (interactive)
2465 (idlwave-block-jump-out -1 'nomark)
2466 (forward-word 1))
2467
2468 (defun idlwave-end-of-block ()
2469 "Go to the beginning of the current block."
2470 (interactive)
2471 (idlwave-block-jump-out 1 'nomark)
2472 (backward-word 1))
2473
2474 (defun idlwave-forward-block (&optional arg)
2475 "Move across next nested block."
2476 (interactive)
2477 (let ((arg (or arg 1)))
2478 (if (idlwave-down-block arg)
2479 (idlwave-block-jump-out arg 'nomark))))
2480
2481 (defun idlwave-backward-block ()
2482 "Move backward across previous nested block."
2483 (interactive)
2484 (if (idlwave-down-block -1)
2485 (idlwave-block-jump-out -1 'nomark)))
2486
2487 (defun idlwave-down-block (&optional arg)
2488 "Go down a block.
2489 With ARG: ARG >= 0 go forwards, ARG < 0 go backwards.
2490 Returns non-nil if successfull."
2491 (interactive "p")
2492 (let (status)
2493 (if (< arg 0)
2494 ;; Backward
2495 (let ((eos (save-excursion
2496 (idlwave-block-jump-out -1 'nomark)
2497 (point))))
2498 (if (setq status (idlwave-find-key
2499 idlwave-end-block-reg -1 'nomark eos))
2500 (idlwave-beginning-of-statement)
2501 (message "No nested block before beginning of containing block.")))
2502 ;; Forward
2503 (let ((eos (save-excursion
2504 (idlwave-block-jump-out 1 'nomark)
2505 (point))))
2506 (if (setq status (idlwave-find-key
2507 idlwave-begin-block-reg 1 'nomark eos))
2508 (idlwave-end-of-statement)
2509 (message "No nested block before end of containing block."))))
2510 status))
2511
2512 (defun idlwave-mark-doclib ()
2513 "Put point at beginning of doc library header, mark at end.
2514 The marks are pushed."
2515 (interactive)
2516 (let (beg
2517 (here (point)))
2518 (goto-char (point-max))
2519 (if (re-search-backward idlwave-doclib-start nil t)
2520 (progn
2521 (setq beg (progn (beginning-of-line) (point)))
2522 (if (re-search-forward idlwave-doclib-end nil t)
2523 (progn
2524 (forward-line 1)
2525 (push-mark beg nil t)
2526 (message "Could not find end of doc library header.")))
2527 (message "Could not find doc library header start.")
2528 (goto-char here)))))
2529
2530 (defun idlwave-current-routine-fullname ()
2531 (let ((name (idlwave-current-routine)))
2532 (idlwave-make-full-name (nth 2 name) (car name))))
2533
2534 (defun idlwave-current-routine ()
2535 "Return (NAME TYPE CLASS) of current routine."
2536 (idlwave-routines)
2537 (save-excursion
2538 (idlwave-beginning-of-subprogram)
2539 (if (looking-at "[ \t]*\\<\\(pro\\|function\\)\\>\\s-+\\(\\([a-zA-Z0-9$_]+\\)::\\)?\\([a-zA-Z0-9$_]+\\)")
2540 (let* ((type (if (string= (downcase (match-string 1)) "pro")
2541 'pro 'function))
2542 (class (idlwave-sintern-class (match-string 3)))
2543 (name (idlwave-sintern-routine-or-method (match-string 4) class)))
2544 (list name type class)))))
2545
2546 (defvar idlwave-shell-prompt-pattern)
2547 (defun idlwave-beginning-of-statement ()
2548 "Move to beginning of the current statement.
2549 Skips back past statement continuations.
2550 Point is placed at the beginning of the line whether or not this is an
2551 actual statement."
2552 (interactive)
2553 (cond
2554 ((eq major-mode 'idlwave-shell-mode)
2555 (if (re-search-backward idlwave-shell-prompt-pattern nil t)
2556 (goto-char (match-end 0))))
2557 (t
2558 (if (save-excursion (forward-line -1) (idlwave-is-continuation-line))
2559 (idlwave-previous-statement)
2560 (beginning-of-line)))))
2561
2562 (defun idlwave-previous-statement ()
2563 "Moves point to beginning of the previous statement.
2564 Returns t if the current line before moving is the beginning of
2565 the first non-comment statement in the file, and nil otherwise."
2566 (interactive)
2567 (let (first-statement)
2568 (if (not (= (forward-line -1) 0))
2569 ;; first line in file
2570 t
2571 ;; skip blank lines, label lines, include lines and line comments
2572 (while (and
2573 ;; The current statement is the first statement until we
2574 ;; reach another statement.
2575 (setq first-statement
2576 (or
2577 (looking-at idlwave-comment-line-start-skip)
2578 (looking-at "[ \t]*$")
2579 (looking-at (concat "[ \t]*" idlwave-label "[ \t]*$"))
2580 (looking-at "^@")))
2581 (= (forward-line -1) 0)))
2582 ;; skip continuation lines
2583 (while (and
2584 (save-excursion
2585 (forward-line -1)
2586 (idlwave-is-continuation-line))
2587 (= (forward-line -1) 0)))
2588 first-statement)))
2589
2590 (defun idlwave-end-of-statement ()
2591 "Moves point to the end of the current IDL statement.
2592 If not in a statement just moves to end of line. Returns position."
2593 (interactive)
2594 (while (and (idlwave-is-continuation-line)
2595 (= (forward-line 1) 0))
2596 (while (and (idlwave-is-comment-or-empty-line)
2597 (= (forward-line 1) 0))))
2598 (end-of-line)
2599 (point))
2600
2601 (defun idlwave-end-of-statement0 ()
2602 "Moves point to the end of the current IDL statement.
2603 If not in a statement just moves to end of line. Returns position."
2604 (interactive)
2605 (while (and (idlwave-is-continuation-line)
2606 (= (forward-line 1) 0)))
2607 (end-of-line)
2608 (point))
2609
2610 (defun idlwave-next-statement ()
2611 "Moves point to beginning of the next IDL statement.
2612 Returns t if that statement is the last
2613 non-comment IDL statement in the file, and nil otherwise."
2614 (interactive)
2615 (let (last-statement)
2616 (idlwave-end-of-statement)
2617 ;; skip blank lines, label lines, include lines and line comments
2618 (while (and (= (forward-line 1) 0)
2619 ;; The current statement is the last statement until
2620 ;; we reach a new statement.
2621 (setq last-statement
2622 (or
2623 (looking-at idlwave-comment-line-start-skip)
2624 (looking-at "[ \t]*$")
2625 (looking-at (concat "[ \t]*" idlwave-label "[ \t]*$"))
2626 (looking-at "^@")))))
2627 last-statement))
2628
2629 (defun idlwave-skip-multi-commands (&optional lim)
2630 "Skip past multiple commands on a line (with `&')."
2631 (let ((save-point (point)))
2632 (when (re-search-forward ".*&" lim t)
2633 (goto-char (match-end 0))
2634 (if (idlwave-quoted)
2635 (goto-char save-point)
2636 (if (eq (char-after (- (point) 2)) ?&) (goto-char save-point))))
2637 (point)))
2638
2639 (defun idlwave-skip-label-or-case ()
2640 "Skip label or case statement element.
2641 Returns position after label.
2642 If there is no label point is not moved and nil is returned."
2643 ;; Case expressions and labels are terminated by a colon.
2644 ;; So we find the first colon in the line and make sure
2645 ;; - no `?' is before it (might be a ? b : c)
2646 ;; - it is not in a comment
2647 ;; - not in a string constant
2648 ;; - not in parenthesis (like a[0:3])
2649 ;; - not followed by another ":" in explicit class, ala a->b::c
2650 ;; As many in this mode, this function is heuristic and not an exact
2651 ;; parser.
2652 (let* ((start (point))
2653 (eos (save-excursion (idlwave-end-of-statement) (point)))
2654 (end (idlwave-find-key ":" 1 'nomark eos)))
2655 (if (and end
2656 (= (nth 0 (parse-partial-sexp start end)) 0)
2657 (not (string-match "\\?" (buffer-substring start end)))
2658 (not (string-match "^::" (buffer-substring end eos))))
2659 (progn
2660 (forward-char)
2661 (point))
2662 (goto-char start)
2663 nil)))
2664
2665 (defun idlwave-start-of-substatement (&optional pre)
2666 "Move to start of next IDL substatement after point.
2667 Uses the type of the current IDL statement to determine if the next
2668 statement is on a new line or is a subpart of the current statement.
2669 Returns point at start of substatement modulo whitespace.
2670 If optional argument is non-nil move to beginning of current
2671 substatement."
2672 (let ((orig (point))
2673 (eos (idlwave-end-of-statement))
2674 (ifnest 0)
2675 st nst last)
2676 (idlwave-beginning-of-statement)
2677 (idlwave-skip-label-or-case)
2678 (if (< (point) orig)
2679 (idlwave-skip-multi-commands orig))
2680 (setq last (point))
2681 ;; Continue looking for substatements until we are past orig
2682 (while (and (<= (point) orig) (not (eobp)))
2683 (setq last (point))
2684 (setq nst (nth 1 (cdr (setq st (car (idlwave-statement-type))))))
2685 (if (equal (car st) 'if) (setq ifnest (1+ ifnest)))
2686 (cond ((and nst
2687 (idlwave-find-key nst 1 'nomark eos))
2688 (goto-char (match-end 0)))
2689 ((and (> ifnest 0) (idlwave-find-key "\\<else\\>" 1 'nomark eos))
2690 (setq ifnest (1- ifnest))
2691 (goto-char (match-end 0)))
2692 (t (setq ifnest 0)
2693 (idlwave-next-statement))))
2694 (if pre (goto-char last))
2695 ;; If a continuation line starts here, move to next line
2696 (if (looking-at "[ \t]*\\$\\([ \t]*\\(;\\|$\\)\\)")
2697 (beginning-of-line 2))
2698 (point)))
2699
2700 (defun idlwave-statement-type ()
2701 "Return the type of the current IDL statement.
2702 Uses `idlwave-statement-match' to return a cons of (type . point) with
2703 point the ending position where the type was determined. Type is the
2704 association from `idlwave-statement-match', i.e. the cons cell from the
2705 list not just the type symbol. Returns nil if not an identifiable
2706 statement."
2707 (save-excursion
2708 ;; Skip whitespace within a statement which is spaces, tabs, continuations
2709 ;; and possibly comments
2710 (while (looking-at "[ \t]*\\$")
2711 (forward-line 1))
2712 (skip-chars-forward " \t")
2713 (let ((st idlwave-statement-match)
2714 (case-fold-search t))
2715 (while (and (not (looking-at (nth 0 (cdr (car st)))))
2716 (setq st (cdr st))))
2717 (if st
2718 (append st (match-end 0))))))
2719
2720 (defun idlwave-expand-equal (&optional before after is-action)
2721 "Pad '=' with spaces. Two cases: Assignment statement, and keyword
2722 assignment. Which case is determined using
2723 `idlwave-start-of-substatement' and `idlwave-statement-type'. The
2724 equal sign will be surrounded by BEFORE and AFTER blanks. If
2725 `idlwave-pad-keyword' is t then keyword assignment is treated just
2726 like assignment statements. When nil, spaces are removed for keyword
2727 assignment. Any other value keeps the current space around the `='.
2728 Limits in for loops are treated as keyword assignment.
2729
2730 Starting with IDL 6.0, a number of op= assignments are available.
2731 Since ambiguities of the form:
2732
2733 r and= b
2734 rand= b
2735
2736 can occur, alphanumeric operator assignment will never be pre-padded,
2737 only post-padded. You must use a space before these to disambiguate
2738 \(not just for padding, but for proper parsing by IDL too!). Other
2739 operators, such as ##=, ^=, etc., will be pre-padded.
2740
2741 IS-ACTION is ignored.
2742
2743 See `idlwave-surround'."
2744 (if idlwave-surround-by-blank
2745 (let
2746 ((non-an-ops "\\(##\\|\\*\\|\\+\\|-\\|/\\|<\\|>\\|\\^\\)\\=")
2747 (an-ops
2748 "\\s-\\(AND\\|EQ\\|GE\\|GT\\|LE\\|LT\\|MOD\\|NE\\|OR\\|XOR\\)\\=")
2749 (len 1))
2750
2751 (save-excursion
2752 (let ((case-fold-search t))
2753 (backward-char)
2754 (if (or
2755 (re-search-backward non-an-ops nil t)
2756 ;; Why doesn't ##? work for both?
2757 (re-search-backward "\\(#\\)\\=" nil t))
2758 (setq len (1+ (length (match-string 1))))
2759 (when (re-search-backward an-ops nil t)
2760 ;(setq begin nil) ; won't modify begin
2761 (setq len (1+ (length (match-string 1))))))))
2762
2763 (if (eq t idlwave-pad-keyword)
2764 ;; Everything gets padded equally
2765 (idlwave-surround before after len)
2766 ;; Treating keywords/for variables specially...
2767 (let ((st (save-excursion ; To catch "for" variables
2768 (idlwave-start-of-substatement t)
2769 (idlwave-statement-type)))
2770 (what (save-excursion ; To catch keywords
2771 (skip-chars-backward "= \t")
2772 (nth 2 (idlwave-where)))))
2773 (cond ((or (memq what '(function-keyword procedure-keyword))
2774 (memq (caar st) '(for pdef)))
2775 (cond
2776 ((null idlwave-pad-keyword)
2777 (idlwave-surround 0 0)
2778 ) ; remove space
2779 (t))) ; leave any spaces alone
2780 (t (idlwave-surround before after len))))))))
2781
2782
2783 (defun idlwave-indent-and-action (&optional arg)
2784 "Call `idlwave-indent-line' and do expand actions.
2785 With prefix ARG non-nil, indent the entire sub-statement."
2786 (interactive "p")
2787 (save-excursion
2788 (if (and idlwave-expand-generic-end
2789 (re-search-backward "\\<\\(end\\)\\s-*\\="
2790 (max 0 (- (point) 10)) t)
2791 (looking-at "\\(end\\)\\([ \n\t]\\|\\'\\)"))
2792 (progn (goto-char (match-end 1))
2793 ;;Expand the END abbreviation, just as RET or Space would have.
2794 (if abbrev-mode (expand-abbrev)
2795 (idlwave-show-begin)))))
2796 (when (and (not arg) current-prefix-arg)
2797 (setq arg current-prefix-arg)
2798 (setq current-prefix-arg nil))
2799 (if arg
2800 (idlwave-indent-statement)
2801 (idlwave-indent-line t)))
2802
2803 (defun idlwave-indent-line (&optional expand)
2804 "Indents current IDL line as code or as a comment.
2805 The actions in `idlwave-indent-action-table' are performed.
2806 If the optional argument EXPAND is non-nil then the actions in
2807 `idlwave-indent-expand-table' are performed."
2808 (interactive)
2809 ;; Move point out of left margin.
2810 (if (save-excursion
2811 (skip-chars-backward " \t")
2812 (bolp))
2813 (skip-chars-forward " \t"))
2814 (let ((mloc (point-marker)))
2815 (save-excursion
2816 (beginning-of-line)
2817 (if (looking-at idlwave-comment-line-start-skip)
2818 ;; Indentation for a line comment
2819 (progn
2820 (skip-chars-forward " \t")
2821 (idlwave-indent-left-margin (idlwave-comment-hook)))
2822 ;;
2823 ;; Code Line
2824 ;;
2825 ;; Before indenting, run action routines.
2826 ;;
2827 (if (and expand idlwave-do-actions)
2828 (mapcar 'idlwave-do-action idlwave-indent-expand-table))
2829 ;;
2830 (if idlwave-do-actions
2831 (mapcar 'idlwave-do-action idlwave-indent-action-table))
2832 ;;
2833 ;; No longer expand abbrevs on the line. The user can do this
2834 ;; manually using expand-region-abbrevs.
2835 ;;
2836 ;; Indent for code line
2837 ;;
2838 (beginning-of-line)
2839 (if (or
2840 ;; a label line
2841 (looking-at (concat "^" idlwave-label "[ \t]*$"))
2842 ;; a batch command
2843 (looking-at "^[ \t]*@"))
2844 ;; leave flush left
2845 nil
2846 ;; indent the line
2847 (idlwave-indent-left-margin (idlwave-calculate-indent)))
2848 ;; Adjust parallel comment
2849 (end-of-line)
2850 (if (idlwave-in-comment)
2851 ;; Emacs 21 is too smart with fill-column on comment indent
2852 (let ((fill-column (if (fboundp 'comment-indent-new-line)
2853 (1- (frame-width))
2854 fill-column)))
2855 (indent-for-comment)))))
2856 (goto-char mloc)
2857 ;; Get rid of marker
2858 (set-marker mloc nil)))
2859
2860 (defun idlwave-do-action (action)
2861 "Perform an action repeatedly on a line. ACTION is a list (REG
2862 . FUNC). REG is a regular expression. FUNC is either a function name
2863 to be called with `funcall' or a list to be evaluated with `eval'.
2864 The action performed by FUNC should leave point after the match for
2865 REG - otherwise an infinite loop may be entered. FUNC is always
2866 passed a final argument of 'is-action, so it can discriminate between
2867 being run as an action, or a key binding"
2868 (let ((action-key (car action))
2869 (action-routine (cdr action)))
2870 (beginning-of-line)
2871 (while (idlwave-look-at action-key)
2872 (if (listp action-routine)
2873 (eval (append action-routine '('is-action)))
2874 (funcall action-routine 'is-action)))))
2875
2876 (defun idlwave-indent-to (col &optional min)
2877 "Indent from point with spaces until column COL.
2878 Inserts space before markers at point."
2879 (if (not min) (setq min 0))
2880 (insert-before-markers
2881 (make-string (max min (- col (current-column))) ?\ )))
2882
2883 (defun idlwave-indent-left-margin (col)
2884 "Indent the current line to column COL.
2885 Indents such that first non-whitespace character is at column COL
2886 Inserts spaces before markers at point."
2887 (save-excursion
2888 (beginning-of-line)
2889 (delete-horizontal-space)
2890 (idlwave-indent-to col)))
2891
2892 (defun idlwave-indent-subprogram ()
2893 "Indents program unit which contains point."
2894 (interactive)
2895 (save-excursion
2896 (idlwave-end-of-statement)
2897 (idlwave-beginning-of-subprogram)
2898 (let ((beg (point)))
2899 (idlwave-forward-block)
2900 (message "Indenting subprogram...")
2901 (indent-region beg (point) nil))
2902 (message "Indenting subprogram...done.")))
2903
2904 (defun idlwave-indent-statement ()
2905 "Indent current statement, including all continuation lines."
2906 (interactive)
2907 (save-excursion
2908 (idlwave-beginning-of-statement)
2909 (let ((beg (point)))
2910 (idlwave-end-of-statement)
2911 (indent-region beg (point) nil))))
2912
2913 (defun idlwave-calculate-indent ()
2914 "Return appropriate indentation for current line as IDL code."
2915 (save-excursion
2916 (beginning-of-line)
2917 (cond
2918 ;; Check for beginning of unit - main (beginning of buffer), pro, or
2919 ;; function
2920 ((idlwave-look-at idlwave-begin-unit-reg)
2921 0)
2922 ;; Check for continuation line
2923 ((save-excursion
2924 (and (= (forward-line -1) 0)
2925 (idlwave-is-continuation-line)))
2926 (idlwave-calculate-cont-indent))
2927 ;; calculate indent based on previous and current statements
2928 (t (let* (beg-prev-pos
2929 (the-indent
2930 ;; calculate indent based on previous statement
2931 (save-excursion
2932 (cond
2933 ;; Beginning of file
2934 ((prog1
2935 (idlwave-previous-statement)
2936 (setq beg-prev-pos (point)))
2937 0)
2938 ;; Main block
2939 ((idlwave-look-at idlwave-begin-unit-reg t)
2940 (+ (idlwave-current-statement-indent)
2941 idlwave-main-block-indent))
2942 ;; Begin block
2943 ((idlwave-look-at idlwave-begin-block-reg t)
2944 (+ (idlwave-min-current-statement-indent)
2945 idlwave-block-indent))
2946 ;; End Block
2947 ((idlwave-look-at idlwave-end-block-reg t)
2948 (progn
2949 ;; Match to the *beginning* of the block opener
2950 (goto-char beg-prev-pos)
2951 (idlwave-block-jump-out -1 'nomark) ; go to begin block
2952 (idlwave-min-current-statement-indent)))
2953 ;; idlwave-end-offset
2954 ;; idlwave-block-indent))
2955
2956 ;; Default to current indent
2957 ((idlwave-current-statement-indent))))))
2958 ;; adjust the indentation based on the current statement
2959 (cond
2960 ;; End block
2961 ((idlwave-look-at idlwave-end-block-reg)
2962 (+ the-indent idlwave-end-offset))
2963 (the-indent)))))))
2964
2965 ;;
2966 ;; Parentheses indent
2967 ;;
2968
2969 (defun idlwave-calculate-paren-indent (beg-reg end-reg close-exp)
2970 "Calculate the continuation indent inside a paren group.
2971 Returns a cons-cell with (open . indent), where open is the
2972 location of the open paren"
2973 (let ((open (nth 1 (parse-partial-sexp beg-reg end-reg))))
2974 ;; Found an innermost open paren.
2975 (when open
2976 (goto-char open)
2977 ;; Line up with next word unless this is a closing paren.
2978 (cons open
2979 (cond
2980 ;; Plain Kernighan-style nested indent
2981 (idlwave-indent-parens-nested
2982 (+ idlwave-continuation-indent (idlwave-current-indent)))
2983
2984 ;; This is a closed paren - line up under open paren.
2985 (close-exp
2986 (current-column))
2987
2988 ;; Empty (or just comment) follows -- revert to basic indent
2989 ((progn
2990 ;; Skip paren
2991 (forward-char 1)
2992 (looking-at "[ \t$]*\\(;.*\\)?$"))
2993 nil)
2994
2995 ;; Line up with first word after any blank space
2996 ((progn
2997 (skip-chars-forward " \t")
2998 (current-column))))))))
2999
3000 (defun idlwave-calculate-cont-indent ()
3001 "Calculates the IDL continuation indent column from the previous
3002 statement. Note that here previous statement usually means the
3003 beginning of the current statement if this statement is a continuation
3004 of the previous line. Various special types of continuations,
3005 including assignments, routine definitions, and parenthetical
3006 groupings, are treated separately."
3007 (save-excursion
3008 (let* ((case-fold-search t)
3009 (end-reg (progn (beginning-of-line) (point)))
3010 (beg-last-statement (save-excursion (idlwave-previous-statement)
3011 (point)))
3012 (beg-reg (progn (idlwave-start-of-substatement 'pre)
3013 (if (eq (line-beginning-position) end-reg)
3014 (goto-char beg-last-statement)
3015 (point))))
3016 (basic-indent (+ (idlwave-min-current-statement-indent end-reg)
3017 idlwave-continuation-indent))
3018 fancy-nonparen-indent fancy-paren-indent)
3019 (cond
3020 ;; Align then with its matching if, etc.
3021 ((let ((matchers '(("\\<if\\>" . "[ \t]*then")
3022 ("\\<\\(if\\|end\\(if\\)?\\)\\>" . "[ \t]*else")
3023 ("\\<\\(for\\|while\\)\\>" . "[ \t]*do")
3024 ("\\<\\(repeat\\|end\\(rep\\)?\\)\\>" .
3025 "[ \t]*until")
3026 ("\\<case\\>" . "[ \t]*of")))
3027 match cont-re)
3028 (goto-char end-reg)
3029 (and
3030 (setq cont-re
3031 (catch 'exit
3032 (while (setq match (car matchers))
3033 (if (looking-at (cdr match))
3034 (throw 'exit (car match)))
3035 (setq matchers (cdr matchers)))))
3036 (idlwave-find-key cont-re -1 'nomark beg-last-statement)))
3037 (if (looking-at "end") ;; that one's special
3038 (- (idlwave-current-indent)
3039 (+ idlwave-block-indent idlwave-end-offset))
3040 (idlwave-current-indent)))
3041
3042 ;; Indent in from the previous line for continuing statements
3043 ((let ((matchers '("\\<then\\>"
3044 "\\<do\\>"
3045 "\\<repeat\\>"
3046 "\\<else\\>"))
3047 match)
3048 (catch 'exit
3049 (goto-char end-reg)
3050 (if (/= (forward-line -1) 0)
3051 (throw 'exit nil))
3052 (while (setq match (car matchers))
3053 (if (looking-at (concat ".*" match "[ \t]*\\$[ \t]*"
3054 "\\(;.*\\)?$"))
3055 (throw 'exit t))
3056 (setq matchers (cdr matchers)))))
3057 (+ idlwave-continuation-indent (idlwave-current-indent)))
3058
3059 ;; Parenthetical indent, either traditional or Kernighan style
3060 ((setq fancy-paren-indent
3061 (let* ((end-reg end-reg)
3062 (close-exp (progn
3063 (goto-char end-reg)
3064 (skip-chars-forward " \t")
3065 (looking-at "\\s)")))
3066 indent-cons)
3067 (catch 'loop
3068 (while (setq indent-cons (idlwave-calculate-paren-indent
3069 beg-reg end-reg close-exp))
3070 ;; First permitted containing paren
3071 (if (or
3072 idlwave-indent-to-open-paren
3073 idlwave-indent-parens-nested
3074 (null (cdr indent-cons))
3075 (< (- (cdr indent-cons) basic-indent)
3076 idlwave-max-extra-continuation-indent))
3077 (throw 'loop (cdr indent-cons)))
3078 (setq end-reg (car indent-cons))))))
3079 fancy-paren-indent)
3080
3081 ;; A continued assignment, or procedure call/definition
3082 ((and
3083 (> idlwave-max-extra-continuation-indent 0)
3084 (setq fancy-nonparen-indent
3085 (progn
3086 (goto-char beg-reg)
3087 (while (idlwave-look-at "&")) ; skip continued statements
3088 (cond
3089 ;; A continued Procedure call or definition
3090 ((progn
3091 (idlwave-look-at "^[ \t]*\\(pro\\|function\\)") ;skip over
3092 (looking-at "[ \t]*\\([a-zA-Z0-9.$_]+[ \t]*->[ \t]*\\)?[a-zA-Z][:a-zA-Z0-9$_]*[ \t]*\\(,\\)[ \t]*"))
3093 (goto-char (match-end 0))
3094 ;; Comment only, or blank line with "$"? Basic indent.
3095 (if (save-match-data (looking-at "[ \t$]*\\(;.*\\)?$"))
3096 nil
3097 (current-column)))
3098
3099 ;; Continued assignment (with =):
3100 ((catch 'assign ;
3101 (while (looking-at "[^=\n\r]*\\(=\\)[ \t]*")
3102 (goto-char (match-end 0))
3103 (if (null (idlwave-what-function beg-reg))
3104 (throw 'assign t))))
3105 (unless (or
3106 (idlwave-in-quote)
3107 (looking-at "[ \t$]*\\(;.*\\)?$") ; use basic
3108 (save-excursion
3109 (goto-char beg-last-statement)
3110 (eq (caar (idlwave-statement-type)) 'for)))
3111 (current-column))))))
3112 (< (- fancy-nonparen-indent basic-indent)
3113 idlwave-max-extra-continuation-indent))
3114 (if fancy-paren-indent ;calculated but disallowed paren indent
3115 (+ fancy-nonparen-indent idlwave-continuation-indent)
3116 fancy-nonparen-indent))
3117
3118 ;; Basic indent, by default
3119 (t basic-indent)))))
3120
3121
3122
3123 (defun idlwave-find-key (key-re &optional dir nomark limit)
3124 "Move to next match of the regular expression KEY-RE.
3125 Matches inside comments or string constants will be ignored.
3126 If DIR is negative, the search will be backwards.
3127 At a successful match, the mark is pushed unless NOMARK is non-nil.
3128 Searches are limited to LIMIT.
3129 Searches are case-insensitive and use a special syntax table which
3130 treats `$' and `_' as word characters.
3131 Return value is the beginning of the match or (in case of failure) nil."
3132 (setq dir (or dir 0))
3133 (let ((case-fold-search t)
3134 (search-func (if (> dir 0) 're-search-forward 're-search-backward))
3135 found)
3136 (idlwave-with-special-syntax
3137 (save-excursion
3138 (catch 'exit
3139 (while (funcall search-func key-re limit t)
3140 (if (not (idlwave-quoted))
3141 (throw 'exit (setq found (match-beginning 0)))
3142 (if (or (and (> dir 0) (eobp))
3143 (and (< dir 0) (bobp)))
3144 (throw 'exit nil)))))))
3145 (if found
3146 (progn
3147 (if (not nomark) (push-mark))
3148 (goto-char found)
3149 found)
3150 nil)))
3151
3152 (defun idlwave-block-jump-out (&optional dir nomark)
3153 "When optional argument DIR is non-negative, move forward to end of
3154 current block using the `idlwave-begin-block-reg' and `idlwave-end-block-reg'
3155 regular expressions. When DIR is negative, move backwards to block beginning.
3156 Recursively calls itself to skip over nested blocks. DIR defaults to
3157 forward. Calls `push-mark' unless the optional argument NOMARK is
3158 non-nil. Movement is limited by the start of program units because of
3159 possibility of unbalanced blocks."
3160 (interactive "P")
3161 (or dir (setq dir 0))
3162 (let* ((here (point))
3163 (case-fold-search t)
3164 (limit (if (>= dir 0) (point-max) (point-min)))
3165 (block-limit (if (>= dir 0)
3166 idlwave-begin-block-reg
3167 idlwave-end-block-reg))
3168 found
3169 (block-reg (concat idlwave-begin-block-reg "\\|"
3170 idlwave-end-block-reg))
3171 (unit-limit (or (save-excursion
3172 (if (< dir 0)
3173 (idlwave-find-key
3174 idlwave-begin-unit-reg dir t limit)
3175 (end-of-line)
3176 (idlwave-find-key
3177 idlwave-end-unit-reg dir t limit)))
3178 limit)))
3179 (if (>= dir 0) (end-of-line)) ;Make sure we are in current block
3180 (if (setq found (idlwave-find-key block-reg dir t unit-limit))
3181 (while (and found (looking-at block-limit))
3182 (if (>= dir 0) (forward-word 1))
3183 (idlwave-block-jump-out dir t)
3184 (setq found (idlwave-find-key block-reg dir t unit-limit))))
3185 (if (not nomark) (push-mark here))
3186 (if (not found) (goto-char unit-limit)
3187 (if (>= dir 0) (forward-word 1)))))
3188
3189 (defun idlwave-min-current-statement-indent (&optional end-reg)
3190 "The minimum indent in the current statement."
3191 (idlwave-beginning-of-statement)
3192 (if (not (idlwave-is-continuation-line))
3193 (idlwave-current-indent)
3194 (let ((min (idlwave-current-indent)) comm-or-empty)
3195 (while (and (= (forward-line 1) 0)
3196 (or (setq comm-or-empty (idlwave-is-comment-or-empty-line))
3197 (idlwave-is-continuation-line))
3198 (or (null end-reg) (< (point) end-reg)))
3199 (unless comm-or-empty (setq min (min min (idlwave-current-indent)))))
3200 (if (or comm-or-empty (and end-reg (>= (point) end-reg)))
3201 min
3202 (min min (idlwave-current-indent))))))
3203
3204 (defun idlwave-current-statement-indent (&optional last-line)
3205 "Return indentation of the current statement.
3206 If in a statement, moves to beginning of statement before finding indent."
3207 (if last-line
3208 (idlwave-end-of-statement)
3209 (idlwave-beginning-of-statement))
3210 (idlwave-current-indent))
3211
3212 (defun idlwave-current-indent ()
3213 "Return the column of the indentation of the current line.
3214 Skips any whitespace. Returns 0 if the end-of-line follows the whitespace."
3215 (save-excursion
3216 (beginning-of-line)
3217 (skip-chars-forward " \t")
3218 ;; if we are at the end of blank line return 0
3219 (cond ((eolp) 0)
3220 ((current-column)))))
3221
3222 (defun idlwave-is-continuation-line ()
3223 "Tests if current line is continuation line.
3224 Blank or comment-only lines following regular continuation lines (with
3225 `$') count as continuations too."
3226 (let (p)
3227 (save-excursion
3228 (or
3229 (idlwave-look-at "\\<\\$")
3230 (catch 'loop
3231 (while (and (looking-at "^[ \t]*\\(;.*\\)?$")
3232 (eq (forward-line -1) 0))
3233 (if (setq p (idlwave-look-at "\\<\\$")) (throw 'loop p))))))))
3234
3235 (defun idlwave-is-comment-line ()
3236 "Tests if the current line is a comment line."
3237 (save-excursion
3238 (beginning-of-line 1)
3239 (looking-at "[ \t]*;")))
3240
3241 (defun idlwave-is-comment-or-empty-line ()
3242 "Tests if the current line is a comment line."
3243 (save-excursion
3244 (beginning-of-line 1)
3245 (looking-at "[ \t]*[;\n]")))
3246
3247 (defun idlwave-look-at (regexp &optional cont beg)
3248 "Searches current line from current point for REGEXP.
3249 If optional argument CONT is non-nil, searches to the end of
3250 the current statement.
3251 If optional arg BEG is non-nil, search starts from the beginning of the
3252 current statement.
3253 Ignores matches that end in a comment or inside a string expression.
3254 Returns point if successful, nil otherwise.
3255 This function produces unexpected results if REGEXP contains quotes or
3256 a comment delimiter. The search is case insensitive.
3257 If successful leaves point after the match, otherwise, does not move point."
3258 (let ((here (point))
3259 (case-fold-search t)
3260 (eos (save-excursion
3261 (if cont (idlwave-end-of-statement) (end-of-line))
3262 (point)))
3263 found)
3264 (idlwave-with-special-syntax
3265 (if beg (idlwave-beginning-of-statement))
3266 (while (and (setq found (re-search-forward regexp eos t))
3267 (idlwave-quoted))))
3268 (if (not found) (goto-char here))
3269 found))
3270
3271 (defun idlwave-fill-paragraph (&optional nohang)
3272 "Fills paragraphs in comments.
3273 A paragraph is made up of all contiguous lines having the same comment
3274 leader (the leading whitespace before the comment delimiter and the
3275 comment delimiter). In addition, paragraphs are separated by blank
3276 line comments. The indentation is given by the hanging indent of the
3277 first line, otherwise by the minimum indentation of the lines after
3278 the first line. The indentation of the first line does not change.
3279 Does not effect code lines. Does not fill comments on the same line
3280 with code. The hanging indent is given by the end of the first match
3281 matching `idlwave-hang-indent-regexp' on the paragraph's first line . If the
3282 optional argument NOHANG is non-nil then the hanging indent is
3283 ignored."
3284 (interactive "P")
3285 ;; check if this is a line comment
3286 (if (save-excursion
3287 (beginning-of-line)
3288 (skip-chars-forward " \t")
3289 (looking-at comment-start))
3290 (let
3291 ((indent 999)
3292 pre here diff fill-prefix-reg bcl first-indent
3293 hang start end)
3294 ;; Change tabs to spaces in the surrounding paragraph.
3295 ;; The surrounding paragraph will be the largest containing block of
3296 ;; contiguous line comments. Thus, we may be changing tabs in
3297 ;; a much larger area than is needed, but this is the easiest
3298 ;; brute force way to do it.
3299 ;;
3300 ;; This has the undesirable side effect of replacing the tabs
3301 ;; permanently without the user's request or knowledge.
3302 (save-excursion
3303 (backward-paragraph)
3304 (setq start (point)))
3305 (save-excursion
3306 (forward-paragraph)
3307 (setq end (point)))
3308 (untabify start end)
3309 ;;
3310 (setq here (point))
3311 (beginning-of-line)
3312 (setq bcl (point))
3313 (re-search-forward
3314 (concat "^[ \t]*" comment-start "+")
3315 (save-excursion (end-of-line) (point))
3316 t)
3317 ;; Get the comment leader on the line and its length
3318 (setq pre (current-column))
3319 ;; the comment leader is the indentation plus exactly the
3320 ;; number of consecutive ";".
3321 (setq fill-prefix-reg
3322 (concat
3323 (setq fill-prefix
3324 (regexp-quote
3325 (buffer-substring (save-excursion
3326 (beginning-of-line) (point))
3327 (point))))
3328 "[^;]"))
3329
3330 ;; Mark the beginning and end of the paragraph
3331 (goto-char bcl)
3332 (while (and (looking-at fill-prefix-reg)
3333 (not (looking-at paragraph-separate))
3334 (not (bobp)))
3335 (forward-line -1))
3336 ;; Move to first line of paragraph
3337 (if (/= (point) bcl)
3338 (forward-line 1))
3339 (setq start (point))
3340 (goto-char bcl)
3341 (while (and (looking-at fill-prefix-reg)
3342 (not (looking-at paragraph-separate))
3343 (not (eobp)))
3344 (forward-line 1))
3345 (beginning-of-line)
3346 (if (or (not (looking-at fill-prefix-reg))
3347 (looking-at paragraph-separate))
3348 (forward-line -1))
3349 (end-of-line)
3350 ;; if at end of buffer add a newline (need this because
3351 ;; fill-region needs END to be at the beginning of line after
3352 ;; the paragraph or it will add a line).
3353 (if (eobp)
3354 (progn (insert ?\n) (backward-char 1)))
3355 ;; Set END to the beginning of line after the paragraph
3356 ;; END is calculated as distance from end of buffer
3357 (setq end (- (point-max) (point) 1))
3358 ;;
3359 ;; Calculate the indentation for the paragraph.
3360 ;;
3361 ;; In the following while statements, after one iteration
3362 ;; point will be at the beginning of a line in which case
3363 ;; the while will not be executed for the
3364 ;; the first paragraph line and thus will not affect the
3365 ;; indentation.
3366 ;;
3367 ;; First check to see if indentation is based on hanging indent.
3368 (if (and (not nohang) idlwave-hanging-indent
3369 (setq hang
3370 (save-excursion
3371 (goto-char start)
3372 (idlwave-calc-hanging-indent))))
3373 ;; Adjust lines of paragraph by inserting spaces so that
3374 ;; each line's indent is at least as great as the hanging
3375 ;; indent. This is needed for fill-paragraph to work with
3376 ;; a fill-prefix.
3377 (progn
3378 (setq indent hang)
3379 (beginning-of-line)
3380 (while (> (point) start)
3381 (re-search-forward comment-start-skip
3382 (save-excursion (end-of-line) (point))
3383 t)
3384 (if (> (setq diff (- indent (current-column))) 0)
3385 (progn
3386 (if (>= here (point))
3387 ;; adjust the original location for the
3388 ;; inserted text.
3389 (setq here (+ here diff)))
3390 (insert (make-string diff ?\ ))))
3391 (forward-line -1))
3392 )
3393
3394 ;; No hang. Instead find minimum indentation of paragraph
3395 ;; after first line.
3396 ;; For the following while statement, since START is at the
3397 ;; beginning of line and END is at the end of line
3398 ;; point is greater than START at least once (which would
3399 ;; be the case for a single line paragraph).
3400 (while (> (point) start)
3401 (beginning-of-line)
3402 (setq indent
3403 (min indent
3404 (progn
3405 (re-search-forward
3406 comment-start-skip
3407 (save-excursion (end-of-line) (point))
3408 t)
3409 (current-column))))
3410 (forward-line -1))
3411 )
3412 (setq fill-prefix (concat fill-prefix
3413 (make-string (- indent pre)
3414 ?\ )))
3415 ;; first-line indent
3416 (setq first-indent
3417 (max
3418 (progn
3419 (re-search-forward
3420 comment-start-skip
3421 (save-excursion (end-of-line) (point))
3422 t)
3423 (current-column))
3424 indent))
3425
3426 ;; try to keep point at its original place
3427 (goto-char here)
3428
3429 ;; In place of the more modern fill-region-as-paragraph, a hack
3430 ;; to keep whitespace untouched on the first line within the
3431 ;; indent length and to preserve any indent on the first line
3432 ;; (first indent).
3433 (save-excursion
3434 (setq diff
3435 (buffer-substring start (+ start first-indent -1)))
3436 (subst-char-in-region start (+ start first-indent -1) ?\ ?~ nil)
3437 (fill-region-as-paragraph
3438 start
3439 (- (point-max) end)
3440 (current-justification)
3441 nil)
3442 (delete-region start (+ start first-indent -1))
3443 (goto-char start)
3444 (insert diff))
3445 ;; When we want the point at the beginning of the comment
3446 ;; body fill-region will put it at the beginning of the line.
3447 (if (bolp) (skip-chars-forward (concat " \t" comment-start)))
3448 (setq fill-prefix nil))))
3449
3450 (defun idlwave-calc-hanging-indent ()
3451 "Calculate the position of the hanging indent for the comment
3452 paragraph. The hanging indent position is given by the first match
3453 with the `idlwave-hang-indent-regexp'. If `idlwave-use-last-hang-indent' is
3454 non-nil then use last occurrence matching `idlwave-hang-indent-regexp' on
3455 the line.
3456 If not found returns nil."
3457 (if idlwave-use-last-hang-indent
3458 (save-excursion
3459 (end-of-line)
3460 (if (re-search-backward
3461 idlwave-hang-indent-regexp
3462 (save-excursion (beginning-of-line) (point))
3463 t)
3464 (+ (current-column) (length idlwave-hang-indent-regexp))))
3465 (save-excursion
3466 (beginning-of-line)
3467 (if (re-search-forward
3468 idlwave-hang-indent-regexp
3469 (save-excursion (end-of-line) (point))
3470 t)
3471 (current-column)))))
3472
3473 (defun idlwave-auto-fill ()
3474 "Called to break lines in auto fill mode.
3475 Only fills non-comment lines if `idlwave-fill-comment-line-only' is
3476 non-nil. Places a continuation character at the end of the line if
3477 not in a comment. Splits strings with IDL concatenation operator `+'
3478 if `idlwave-auto-fill-split-string' is non-nil."
3479 (if (<= (current-column) fill-column)
3480 nil ; do not to fill
3481 (if (or (not idlwave-fill-comment-line-only)
3482 (save-excursion
3483 ;; Check for comment line
3484 (beginning-of-line)
3485 (looking-at idlwave-comment-line-start-skip)))
3486 (let (beg)
3487 (idlwave-indent-line)
3488 ;; Prevent actions do-auto-fill which calls indent-line-function.
3489 (let (idlwave-do-actions
3490 (paragraph-separate ".")
3491 (fill-nobreak-predicate
3492 (if (and (idlwave-in-quote)
3493 idlwave-auto-fill-split-string)
3494 (lambda () ;; We'll need 5 spaces for " ' + $"
3495 (<= (- fill-column (current-column)) 5)
3496 ))))
3497 (do-auto-fill))
3498 (save-excursion
3499 (end-of-line 0)
3500 ;; Indent the split line
3501 (idlwave-indent-line))
3502 (if (save-excursion
3503 (beginning-of-line)
3504 (looking-at idlwave-comment-line-start-skip))
3505 ;; A continued line comment
3506 ;; We treat continued line comments as part of a comment
3507 ;; paragraph. So we check for a hanging indent.
3508 (if idlwave-hanging-indent
3509 (let ((here (- (point-max) (point)))
3510 (indent
3511 (save-excursion
3512 (forward-line -1)
3513 (idlwave-calc-hanging-indent))))
3514 (if indent
3515 (progn
3516 ;; Remove whitespace between comment delimiter and
3517 ;; text, insert spaces for appropriate indentation.
3518 (beginning-of-line)
3519 (re-search-forward
3520 comment-start-skip
3521 (save-excursion (end-of-line) (point)) t)
3522 (delete-horizontal-space)
3523 (idlwave-indent-to indent)
3524 (goto-char (- (point-max) here)))
3525 )))
3526 ;; Split code or comment?
3527 (if (save-excursion
3528 (end-of-line 0)
3529 (idlwave-in-comment))
3530 ;; Splitting a non-full-line comment.
3531 ;; Insert the comment delimiter from split line
3532 (progn
3533 (save-excursion
3534 (beginning-of-line)
3535 (skip-chars-forward " \t")
3536 ;; Insert blank to keep off beginning of line
3537 (insert " "
3538 (save-excursion
3539 (forward-line -1)
3540 (buffer-substring (idlwave-goto-comment)
3541 (progn
3542 (skip-chars-forward "; ")
3543 (point))))))
3544 (idlwave-indent-line))
3545 ;; Split code line - add continuation character
3546 (save-excursion
3547 (end-of-line 0)
3548 ;; Check to see if we split a string
3549 (if (and (setq beg (idlwave-in-quote))
3550 idlwave-auto-fill-split-string)
3551 ;; Split the string and concatenate.
3552 ;; The first extra space is for the space
3553 ;; the line was split. That space was removed.
3554 (insert " " (char-after beg) " +"))
3555 (insert " $"))
3556 (if beg
3557 (if idlwave-auto-fill-split-string
3558 ;; Make the second part of continued string
3559 (save-excursion
3560 (beginning-of-line)
3561 (skip-chars-forward " \t")
3562 (insert (char-after beg)))
3563 ;; Warning
3564 (beep)
3565 (message "Warning: continuation inside a string.")))
3566 ;; Although do-auto-fill (via indent-new-comment-line) calls
3567 ;; idlwave-indent-line for the new line, re-indent again
3568 ;; because of the addition of the continuation character.
3569 (idlwave-indent-line))
3570 )))))
3571
3572 (defun idlwave-auto-fill-mode (arg)
3573 "Toggle auto-fill mode for IDL mode.
3574 With arg, turn auto-fill mode on if arg is positive.
3575 In auto-fill mode, inserting a space at a column beyond `fill-column'
3576 automatically breaks the line at a previous space."
3577 (interactive "P")
3578 (prog1 (set idlwave-fill-function
3579 (if (if (null arg)
3580 (not (symbol-value idlwave-fill-function))
3581 (> (prefix-numeric-value arg) 0))
3582 'idlwave-auto-fill
3583 nil))
3584 ;; update mode-line
3585 (set-buffer-modified-p (buffer-modified-p))))
3586
3587 ;(defun idlwave-fill-routine-call ()
3588 ; "Fill a routine definition or statement, indenting appropriately."
3589 ; (let ((where (idlwave-where)))))
3590
3591
3592 (defun idlwave-doc-header (&optional nomark )
3593 "Insert a documentation header at the beginning of the unit.
3594 Inserts the value of the variable idlwave-file-header. Sets mark before
3595 moving to do insertion unless the optional prefix argument NOMARK
3596 is non-nil."
3597 (interactive "P")
3598 (or nomark (push-mark))
3599 ;; make sure we catch the current line if it begins the unit
3600 (if idlwave-header-to-beginning-of-file
3601 (goto-char (point-min))
3602 (end-of-line)
3603 (idlwave-beginning-of-subprogram)
3604 (beginning-of-line)
3605 ;; skip function or procedure line
3606 (if (idlwave-look-at "\\<\\(pro\\|function\\)\\>")
3607 (progn
3608 (idlwave-end-of-statement)
3609 (if (> (forward-line 1) 0) (insert "\n")))))
3610 (let ((pos (point)))
3611 (if idlwave-file-header
3612 (cond ((car idlwave-file-header)
3613 (insert-file-contents (car idlwave-file-header)))
3614 ((stringp (car (cdr idlwave-file-header)))
3615 (insert (car (cdr idlwave-file-header))))))
3616 (goto-char pos)))
3617
3618 (defun idlwave-default-insert-timestamp ()
3619 "Default timestamp insertion function"
3620 (insert (current-time-string))
3621 (insert ", " (user-full-name))
3622 (if (boundp 'user-mail-address)
3623 (insert " <" user-mail-address ">")
3624 (insert " <" (user-login-name) "@" (system-name) ">"))
3625 ;; Remove extra spaces from line
3626 (idlwave-fill-paragraph)
3627 ;; Insert a blank line comment to separate from the date entry -
3628 ;; will keep the entry from flowing onto date line if re-filled.
3629 (insert "\n;\n;\t\t"))
3630
3631 (defun idlwave-doc-modification ()
3632 "Insert a brief modification log at the beginning of the current program.
3633 Looks for an occurrence of the value of user variable
3634 `idlwave-doc-modifications-keyword' if non-nil. Inserts time and user name
3635 and places the point for the user to add a log. Before moving, saves
3636 location on mark ring so that the user can return to previous point."
3637 (interactive)
3638 (push-mark)
3639 (let* (beg end)
3640 (if (and (or (re-search-backward idlwave-doclib-start nil t)
3641 (progn
3642 (goto-char (point-min))
3643 (re-search-forward idlwave-doclib-start nil t)))
3644 (setq beg (match-beginning 0))
3645 (re-search-forward idlwave-doclib-end nil t)
3646 (setq end (match-end 0)))
3647 (progn
3648 (goto-char beg)
3649 (if (re-search-forward
3650 (concat idlwave-doc-modifications-keyword ":")
3651 end t)
3652 (end-of-line)
3653 (goto-char end)
3654 (end-of-line -1)
3655 (insert "\n" comment-start "\n")
3656 (insert comment-start " " idlwave-doc-modifications-keyword ":"))
3657 (insert "\n;\n;\t")
3658 (run-hooks 'idlwave-timestamp-hook))
3659 (error "No valid DOCLIB header"))))
3660
3661
3662 ;;; CJC 3/16/93
3663 ;;; Interface to expand-region-abbrevs which did not work when the
3664 ;;; abbrev hook associated with an abbrev moves point backwards
3665 ;;; after abbrev expansion, e.g., as with the abbrev '.n'.
3666 ;;; The original would enter an infinite loop in attempting to expand
3667 ;;; .n (it would continually expand and unexpand the abbrev without expanding
3668 ;;; because the point would keep going back to the beginning of the
3669 ;;; abbrev instead of to the end of the abbrev). We now keep the
3670 ;;; abbrev hook from moving backwards.
3671 ;;;
3672 (defun idlwave-expand-region-abbrevs (start end)
3673 "Expand each abbrev occurrence in the region.
3674 Calling from a program, arguments are START END."
3675 (interactive "r")
3676 (save-excursion
3677 (goto-char (min start end))
3678 (let ((idlwave-show-block nil) ;Do not blink
3679 (idlwave-abbrev-move nil)) ;Do not move
3680 (expand-region-abbrevs start end 'noquery))))
3681
3682 (defun idlwave-quoted ()
3683 "Returns t if point is in a comment or quoted string.
3684 nil otherwise."
3685 (or (idlwave-in-comment) (idlwave-in-quote)))
3686
3687 (defun idlwave-in-quote ()
3688 "Returns location of the opening quote
3689 if point is in a IDL string constant, nil otherwise.
3690 Ignores comment delimiters on the current line.
3691 Properly handles nested quotation marks and octal
3692 constants - a double quote followed by an octal digit."
3693 ;;; Treat an octal inside an apostrophe to be a normal string. Treat a
3694 ;;; double quote followed by an octal digit to be an octal constant
3695 ;;; rather than a string. Therefore, there is no terminating double
3696 ;;; quote.
3697 (save-excursion
3698 ;; Because single and double quotes can quote each other we must
3699 ;; search for the string start from the beginning of line.
3700 (let* ((start (point))
3701 (eol (progn (end-of-line) (point)))
3702 (bq (progn (beginning-of-line) (point)))
3703 (endq (point))
3704 (data (match-data))
3705 delim
3706 found)
3707 (while (< endq start)
3708 ;; Find string start
3709 ;; Don't find an octal constant beginning with a double quote
3710 (if (re-search-forward "[\"']" eol 'lim)
3711 ;; Find the string end.
3712 ;; In IDL, two consecutive delimiters after the start of a
3713 ;; string act as an
3714 ;; escape for the delimiter in the string.
3715 ;; Two consecutive delimiters alone (i.e., not after the
3716 ;; start of a string) is the null string.
3717 (progn
3718 ;; Move to position after quote
3719 (goto-char (1+ (match-beginning 0)))
3720 (setq bq (1- (point)))
3721 ;; Get the string delimiter
3722 (setq delim (char-to-string (preceding-char)))
3723 ;; Check for null string
3724 (if (looking-at delim)
3725 (progn (setq endq (point)) (forward-char 1))
3726 ;; Look for next unpaired delimiter
3727 (setq found (search-forward delim eol 'lim))
3728 (while (looking-at delim)
3729 (forward-char 1)
3730 (setq found (search-forward delim eol 'lim)))
3731 (if found
3732 (setq endq (- (point) 1))
3733 (setq endq (point)))
3734 ))
3735 (progn (setq bq (point)) (setq endq (point)))))
3736 (store-match-data data)
3737 ;; return string beginning position or nil
3738 (if (> start bq) bq))))
3739
3740 (defun idlwave-is-pointer-dereference (&optional limit)
3741 "Determines if the character after point is a pointer dereference *."
3742 (let ((pos (point)))
3743 (and
3744 (eq (char-after) ?\*)
3745 (not (idlwave-in-quote))
3746 (save-excursion
3747 (forward-char)
3748 (re-search-backward (concat "\\(" idlwave-idl-keywords
3749 "\\|[[(*+-/=,^><]\\)\\s-*\\*") limit t)))))
3750
3751
3752 ;; Statement templates
3753
3754 ;; Replace these with a general template function, something like
3755 ;; expand.el (I think there was also something with a name similar to
3756 ;; dmacro.el)
3757
3758 (defun idlwave-template (s1 s2 &optional prompt noindent)
3759 "Build a template with optional prompt expression.
3760
3761 Opens a line if point is not followed by a newline modulo intervening
3762 whitespace. S1 and S2 are strings. S1 is inserted at point followed
3763 by S2. Point is inserted between S1 and S2. The case of S1 and S2 is
3764 adjusted according to `idlwave-abbrev-change-case'. If optional argument
3765 PROMPT is a string then it is displayed as a message in the
3766 minibuffer. The PROMPT serves as a reminder to the user of an
3767 expression to enter.
3768
3769 The lines containing S1 and S2 are reindented using `indent-region'
3770 unless the optional second argument NOINDENT is non-nil."
3771 (if (eq major-mode 'idlwave-shell-mode)
3772 ;; This is a gross hack to avoit template abbrev expansion
3773 ;; in the shell. FIXME: This is a dirty hack.
3774 (if (and (eq this-command 'self-insert-command)
3775 (equal last-abbrev-location (point)))
3776 (insert last-abbrev-text)
3777 (error "No templates in idlwave-shell"))
3778 (cond ((eq idlwave-abbrev-change-case 'down)
3779 (setq s1 (downcase s1) s2 (downcase s2)))
3780 (idlwave-abbrev-change-case
3781 (setq s1 (upcase s1) s2 (upcase s2))))
3782 (let ((beg (save-excursion (beginning-of-line) (point)))
3783 end)
3784 (if (not (looking-at "\\s-*\n"))
3785 (open-line 1))
3786 (insert s1)
3787 (save-excursion
3788 (insert s2)
3789 (setq end (point)))
3790 (if (not noindent)
3791 (indent-region beg end nil))
3792 (if (stringp prompt)
3793 (message prompt)))))
3794
3795 (defun idlwave-rw-case (string)
3796 "Make STRING have the case required by `idlwave-reserved-word-upcase'."
3797 (if idlwave-reserved-word-upcase
3798 (upcase string)
3799 string))
3800
3801 (defun idlwave-elif ()
3802 "Build skeleton IDL if-else block."
3803 (interactive)
3804 (idlwave-template
3805 (idlwave-rw-case "if")
3806 (idlwave-rw-case " then begin\n\nendif else begin\n\nendelse")
3807 "Condition expression"))
3808
3809 (defun idlwave-case ()
3810 "Build skeleton IDL case statement."
3811 (interactive)
3812 (idlwave-template
3813 (idlwave-rw-case "case")
3814 (idlwave-rw-case " of\n\nendcase")
3815 "Selector expression"))
3816
3817 (defun idlwave-switch ()
3818 "Build skeleton IDL switch statement."
3819 (interactive)
3820 (idlwave-template
3821 (idlwave-rw-case "switch")
3822 (idlwave-rw-case " of\n\nendswitch")
3823 "Selector expression"))
3824
3825 (defun idlwave-for ()
3826 "Build skeleton for loop statment."
3827 (interactive)
3828 (idlwave-template
3829 (idlwave-rw-case "for")
3830 (idlwave-rw-case " do begin\n\nendfor")
3831 "Loop expression"))
3832
3833 (defun idlwave-if ()
3834 "Build skeleton for loop statment."
3835 (interactive)
3836 (idlwave-template
3837 (idlwave-rw-case "if")
3838 (idlwave-rw-case " then begin\n\nendif")
3839 "Scalar logical expression"))
3840
3841 (defun idlwave-procedure ()
3842 (interactive)
3843 (idlwave-template
3844 (idlwave-rw-case "pro")
3845 (idlwave-rw-case "\n\nreturn\nend")
3846 "Procedure name"))
3847
3848 (defun idlwave-function ()
3849 (interactive)
3850 (idlwave-template
3851 (idlwave-rw-case "function")
3852 (idlwave-rw-case "\n\nreturn\nend")
3853 "Function name"))
3854
3855 (defun idlwave-repeat ()
3856 (interactive)
3857 (idlwave-template
3858 (idlwave-rw-case "repeat begin\n\nendrep until")
3859 (idlwave-rw-case "")
3860 "Exit condition"))
3861
3862 (defun idlwave-while ()
3863 (interactive)
3864 (idlwave-template
3865 (idlwave-rw-case "while")
3866 (idlwave-rw-case " do begin\n\nendwhile")
3867 "Entry condition"))
3868
3869 (defun idlwave-split-string (string &optional pattern)
3870 "Return a list of substrings of STRING which are separated by PATTERN.
3871 If PATTERN is omitted, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
3872 (or pattern
3873 (setq pattern "[ \f\t\n\r\v]+"))
3874 (let (parts (start 0))
3875 (while (string-match pattern string start)
3876 (setq parts (cons (substring string start (match-beginning 0)) parts)
3877 start (match-end 0)))
3878 (nreverse (cons (substring string start) parts))))
3879
3880 (defun idlwave-replace-string (string replace_string replace_with)
3881 (let* ((start 0)
3882 (last (length string))
3883 (ret_string "")
3884 end)
3885 (while (setq end (string-match replace_string string start))
3886 (setq ret_string
3887 (concat ret_string (substring string start end) replace_with))
3888 (setq start (match-end 0)))
3889 (setq ret_string (concat ret_string (substring string start last)))))
3890
3891 (defun idlwave-get-buffer-visiting (file)
3892 ;; Return the buffer currently visiting FILE
3893 (cond
3894 ((boundp 'find-file-compare-truenames) ; XEmacs
3895 (let ((find-file-compare-truenames t))
3896 (get-file-buffer file)))
3897 ((fboundp 'find-buffer-visiting) ; Emacs
3898 (find-buffer-visiting file))
3899 (t (error "This should not happen (idlwave-get-buffer-visiting)"))))
3900
3901 (defvar idlwave-outlawed-buffers nil
3902 "List of buffer pulled up by idlwave for special reasons.
3903 Buffers in this list may be killed by `idlwave-kill-autoloaded-buffers'.")
3904
3905 (defun idlwave-find-file-noselect (file &optional why)
3906 ;; Return a buffer visiting file.
3907 (or (idlwave-get-buffer-visiting file)
3908 (let ((buf (find-file-noselect file)))
3909 (if why (add-to-list 'idlwave-outlawed-buffers (cons buf why)))
3910 buf)))
3911
3912 (defun idlwave-kill-autoloaded-buffers ()
3913 "Kill buffers created automatically by IDLWAVE.
3914 Function prompts for a letter to identify the buffers to kill.
3915 Possible letters are:
3916
3917 f Buffers created by the command \\[idlwave-find-module] or mouse
3918 clicks in the routine info window.
3919 s Buffers created by the IDLWAVE Shell to display where execution
3920 stopped or an error was found.
3921 a Both of the above.
3922
3923 Buffer containing unsaved changes require confirmation before they are killed."
3924 (interactive)
3925 (if (null idlwave-outlawed-buffers)
3926 (error "No IDLWAVE-created buffers available")
3927 (princ (format "Kill IDLWAVE-created buffers: [f]ind source(%d), [s]hell display(%d), [a]ll ? "
3928 (idlwave-count-outlawed-buffers 'find)
3929 (idlwave-count-outlawed-buffers 'shell)))
3930 (let ((c (read-char)))
3931 (cond
3932 ((member c '(?f ?\C-f))
3933 (idlwave-do-kill-autoloaded-buffers 'find))
3934 ((member c '(?s ?\C-s))
3935 (idlwave-do-kill-autoloaded-buffers 'shell))
3936 ((member c '(?a ?\C-a))
3937 (idlwave-do-kill-autoloaded-buffers t))
3938 (t (error "Abort"))))))
3939
3940 (defun idlwave-count-outlawed-buffers (tag)
3941 "How many outlawed buffers have tag TAG?"
3942 (length (delq nil
3943 (mapcar
3944 (lambda (x) (eq (cdr x) tag))
3945 idlwave-outlawed-buffers))))
3946
3947 (defun idlwave-do-kill-autoloaded-buffers (&rest reasons)
3948 "Kill all buffers pulled up by IDLWAVE matching REASONS."
3949 (let* ((list (copy-sequence idlwave-outlawed-buffers))
3950 (cnt 0)
3951 entry)
3952 (while (setq entry (pop list))
3953 (if (buffer-live-p (car entry))
3954 (and (or (memq t reasons)
3955 (memq (cdr entry) reasons))
3956 (kill-buffer (car entry))
3957 (incf cnt)
3958 (setq idlwave-outlawed-buffers
3959 (delq entry idlwave-outlawed-buffers)))
3960 (setq idlwave-outlawed-buffers
3961 (delq entry idlwave-outlawed-buffers))))
3962 (message "%d buffer%s killed" cnt (if (= cnt 1) "" "s"))))
3963
3964 (defun idlwave-revoke-license-to-kill ()
3965 "Remove BUFFER from the buffers which may be killed.
3966 Killing would be done by `idlwave-do-kill-autoloaded-buffers'.
3967 Intended for `after-save-hook'."
3968 (let* ((buf (current-buffer))
3969 (entry (assq buf idlwave-outlawed-buffers)))
3970 ;; Revoke license
3971 (if entry
3972 (setq idlwave-outlawed-buffers
3973 (delq entry idlwave-outlawed-buffers)))
3974 ;; Remove this function from the hook.
3975 (remove-hook 'after-save-hook 'idlwave-revoke-license-to-kill 'local)))
3976
3977 (defvar idlwave-path-alist)
3978 (defun idlwave-locate-lib-file (file)
3979 ;; Find FILE on the scanned lib path and return a buffer visiting it
3980 (let* ((dirs idlwave-path-alist)
3981 dir efile)
3982 (catch 'exit
3983 (while (setq dir (car (pop dirs)))
3984 (if (file-regular-p
3985 (setq efile (expand-file-name file dir)))
3986 (throw 'exit efile))))))
3987
3988 (defun idlwave-expand-lib-file-name (file)
3989 ;; Find FILE on the scanned lib path and return a buffer visiting it
3990 ;; This is for, e.g., finding source with no user catalog
3991 (cond
3992 ((null file) nil)
3993 ((file-name-absolute-p file) file)
3994 (t (idlwave-locate-lib-file file))))
3995
3996 (defun idlwave-make-tags ()
3997 "Creates the IDL tags file IDLTAGS in the current directory from
3998 the list of directories specified in the minibuffer. Directories may be
3999 for example: . /usr/local/rsi/idl/lib. All the subdirectories of the
4000 specified top directories are searched if the directory name is prefixed
4001 by @. Specify @ directories with care, it may take a long, long time if
4002 you specify /."
4003 (interactive)
4004 (let (directory directories cmd append status numdirs dir getsubdirs
4005 buffer save_buffer files numfiles item errbuf)
4006
4007 ;;
4008 ;; Read list of directories
4009 (setq directory (read-string "Tag Directories: " "."))
4010 (setq directories (idlwave-split-string directory "[ \t]+"))
4011 ;;
4012 ;; Set etags command, vars
4013 (setq cmd "etags --output=IDLTAGS --language=none --regex='/[
4014 \\t]*[pP][Rr][Oo][ \\t]+\\([^ \\t,]+\\)/' --regex='/[
4015 \\t]*[Ff][Uu][Nn][Cc][Tt][Ii][Oo][Nn][ \\t]+\\([^ \\t,]+\\)/' ")
4016 (setq append " ")
4017 (setq status 0)
4018 ;;
4019 ;; For each directory
4020 (setq numdirs 0)
4021 (setq dir (nth numdirs directories))
4022 (while (and dir)
4023 ;;
4024 ;; Find the subdirectories
4025 (if (string-match "^[@]\\(.+\\)$" dir)
4026 (setq getsubdirs t) (setq getsubdirs nil))
4027 (if (and getsubdirs) (setq dir (substring dir 1 (length dir))))
4028 (setq dir (expand-file-name dir))
4029 (if (file-directory-p dir)
4030 (progn
4031 (if (and getsubdirs)
4032 (progn
4033 (setq buffer (get-buffer-create "*idltags*"))
4034 (call-process "sh" nil buffer nil "-c"
4035 (concat "find " dir " -type d -print"))
4036 (setq save_buffer (current-buffer))
4037 (set-buffer buffer)
4038 (setq files (idlwave-split-string
4039 (idlwave-replace-string
4040 (buffer-substring 1 (point-max))
4041 "\n" "/*.pro ")
4042 "[ \t]+"))
4043 (set-buffer save_buffer)
4044 (kill-buffer buffer))
4045 (setq files (list (concat dir "/*.pro"))))
4046 ;;
4047 ;; For each subdirectory
4048 (setq numfiles 0)
4049 (setq item (nth numfiles files))
4050 (while (and item)
4051 ;;
4052 ;; Call etags
4053 (if (not (string-match "^[ \\t]*$" item))
4054 (progn
4055 (message "%s" (concat "Tagging " item "..."))
4056 (setq errbuf (get-buffer-create "*idltags-error*"))
4057 (setq status (+ status
4058 (if (eq 0 (call-process
4059 "sh" nil errbuf nil "-c"
4060 (concat cmd append item)))
4061 0
4062 1)))
4063 ;;
4064 ;; Append additional tags
4065 (setq append " --append ")
4066 (setq numfiles (1+ numfiles))
4067 (setq item (nth numfiles files)))
4068 (progn
4069 (setq numfiles (1+ numfiles))
4070 (setq item (nth numfiles files))
4071 )))
4072
4073 (setq numdirs (1+ numdirs))
4074 (setq dir (nth numdirs directories)))
4075 (progn
4076 (setq numdirs (1+ numdirs))
4077 (setq dir (nth numdirs directories)))))
4078
4079 (setq errbuf (get-buffer-create "*idltags-error*"))
4080 (if (= status 0)
4081 (kill-buffer errbuf))
4082 (message "")
4083 ))
4084
4085 (defun idlwave-toggle-comment-region (beg end &optional n)
4086 "Comment the lines in the region if the first non-blank line is
4087 commented, and conversely, uncomment region. If optional prefix arg
4088 N is non-nil, then for N positive, add N comment delimiters or for N
4089 negative, remove N comment delimiters.
4090 Uses `comment-region' which does not place comment delimiters on
4091 blank lines."
4092 (interactive "r\nP")
4093 (if n
4094 (comment-region beg end (prefix-numeric-value n))
4095 (save-excursion
4096 (goto-char beg)
4097 (beginning-of-line)
4098 ;; skip blank lines
4099 (skip-chars-forward " \t\n")
4100 (if (looking-at (concat "[ \t]*\\(" comment-start "+\\)"))
4101 (if (fboundp 'uncomment-region)
4102 (uncomment-region beg end)
4103 (comment-region beg end
4104 (- (length (buffer-substring
4105 (match-beginning 1)
4106 (match-end 1))))))
4107 (comment-region beg end)))))
4108
4109
4110 ;; ----------------------------------------------------------------------------
4111 ;; ----------------------------------------------------------------------------
4112 ;; ----------------------------------------------------------------------------
4113 ;; ----------------------------------------------------------------------------
4114 ;;
4115 ;; Completion and Routine Info
4116 ;;
4117
4118 ;; String "intern" functions
4119
4120 ;; For the completion and routine info function, we want to normalize
4121 ;; the case of procedure names etc. We do this by "interning" these
4122 ;; string is a hand-crafted way. Hashes are used to map the downcase
4123 ;; version of the strings to the cased versions. Most *-sint-*
4124 ;; variables consist of *two* hashes, a buffer+shell, followed by a
4125 ;; system hash. The former is re-scanned, and the latter takes case
4126 ;; precedence.
4127 ;;
4128 ;; Since these cased versions are really lisp objects, we can use `eq'
4129 ;; to search, which is a large performance boost. All new strings
4130 ;; need to be "sinterned". We do this as early as possible after
4131 ;; getting these strings from completion or buffer substrings. So
4132 ;; most of the code can simply assume to deal with "sinterned"
4133 ;; strings. The only exception is that the functions which scan whole
4134 ;; buffers for routine information do not intern the grabbed strings.
4135 ;; This is only done afterwards. Therefore in these functions it is
4136 ;; *not* safe to assume the strings can be compared with `eq' and be
4137 ;; fed into the routine assq functions.
4138
4139 ;; Here we define the hashing functions.
4140
4141 ;; The variables which hold the hashes.
4142 (defvar idlwave-sint-routines '(nil))
4143 (defvar idlwave-sint-keywords '(nil))
4144 (defvar idlwave-sint-methods '(nil))
4145 (defvar idlwave-sint-classes '(nil))
4146 (defvar idlwave-sint-dirs '(nil))
4147 (defvar idlwave-sint-libnames '(nil))
4148
4149 (defun idlwave-reset-sintern (&optional what)
4150 "Reset all sintern hashes."
4151 ;; Make sure the hash functions are accessible.
4152 (if (or (not (fboundp 'gethash))
4153 (not (fboundp 'puthash)))
4154 (progn
4155 (require 'cl)
4156 (or (fboundp 'puthash)
4157 (defalias 'puthash 'cl-puthash))))
4158 (let ((entries '((idlwave-sint-routines 1000 10)
4159 (idlwave-sint-keywords 1000 10)
4160 (idlwave-sint-methods 100 10)
4161 (idlwave-sint-classes 10 10))))
4162
4163 ;; Make sure these are lists
4164 (loop for entry in entries
4165 for var = (car entry)
4166 do (if (not (consp (symbol-value var))) (set var (list nil))))
4167
4168 ;; Reset the system & library hash
4169 (when (or (eq what t) (eq what 'syslib)
4170 (null (cdr idlwave-sint-routines)))
4171 (loop for entry in entries
4172 for var = (car entry) for size = (nth 1 entry)
4173 do (setcdr (symbol-value var)
4174 (make-hash-table ':size size ':test 'equal)))
4175 (setq idlwave-sint-dirs nil
4176 idlwave-sint-libnames nil))
4177
4178 ;; Reset the buffer & shell hash
4179 (when (or (eq what t) (eq what 'bufsh)
4180 (null (car idlwave-sint-routines)))
4181 (loop for entry in entries
4182 for var = (car entry) for size = (nth 1 entry)
4183 do (setcar (symbol-value var)
4184 (make-hash-table ':size size ':test 'equal))))))
4185
4186 (defun idlwave-sintern-routine-or-method (name &optional class set)
4187 (if class
4188 (idlwave-sintern-method name set)
4189 (idlwave-sintern-routine name set)))
4190
4191 (defun idlwave-sintern (stype &rest args)
4192 (apply (intern (concat "idlwave-sintern-" (symbol-name stype))) args))
4193
4194 ;;(defmacro idlwave-sintern (type var)
4195 ;; `(cond ((not (stringp name)) name)
4196 ;; ((gethash (downcase name) (cdr ,var)))
4197 ;; ((gethash (downcase name) (car ,var)))
4198 ;; (set (idlwave-sintern-set name ,type ,var set))
4199 ;; (name)))
4200
4201 (defun idlwave-sintern-routine (name &optional set)
4202 (cond ((not (stringp name)) name)
4203 ((gethash (downcase name) (cdr idlwave-sint-routines)))
4204 ((gethash (downcase name) (car idlwave-sint-routines)))
4205 (set (idlwave-sintern-set name 'routine idlwave-sint-routines set))
4206 (name)))
4207 (defun idlwave-sintern-keyword (name &optional set)
4208 (cond ((not (stringp name)) name)
4209 ((gethash (downcase name) (cdr idlwave-sint-keywords)))
4210 ((gethash (downcase name) (car idlwave-sint-keywords)))
4211 (set (idlwave-sintern-set name 'keyword idlwave-sint-keywords set))
4212 (name)))
4213 (defun idlwave-sintern-method (name &optional set)
4214 (cond ((not (stringp name)) name)
4215 ((gethash (downcase name) (cdr idlwave-sint-methods)))
4216 ((gethash (downcase name) (car idlwave-sint-methods)))
4217 (set (idlwave-sintern-set name 'method idlwave-sint-methods set))
4218 (name)))
4219 (defun idlwave-sintern-class (name &optional set)
4220 (cond ((not (stringp name)) name)
4221 ((gethash (downcase name) (cdr idlwave-sint-classes)))
4222 ((gethash (downcase name) (car idlwave-sint-classes)))
4223 (set (idlwave-sintern-set name 'class idlwave-sint-classes set))
4224 (name)))
4225
4226 (defun idlwave-sintern-dir (dir &optional set)
4227 (car (or (member dir idlwave-sint-dirs)
4228 (setq idlwave-sint-dirs (cons dir idlwave-sint-dirs)))))
4229 (defun idlwave-sintern-libname (name &optional set)
4230 (car (or (member name idlwave-sint-libnames)
4231 (setq idlwave-sint-libnames (cons name idlwave-sint-libnames)))))
4232
4233 (defun idlwave-sintern-set (name type tables set)
4234 (let* ((func (or (cdr (assq type idlwave-completion-case))
4235 'identity))
4236 (iname (funcall (if (eq func 'preserve) 'identity func) name))
4237 (table (if (eq set 'sys) (cdr tables) (car tables))))
4238 (puthash (downcase name) iname table)
4239 iname))
4240
4241 (defun idlwave-sintern-keyword-list (kwd-list &optional set)
4242 "Sintern a set of keywords (file (key . link) (key2 . link2) ...)"
4243 (mapcar (lambda(x)
4244 (setcar x (idlwave-sintern-keyword (car x) set)))
4245 (cdr kwd-list))
4246 kwd-list)
4247
4248 (defun idlwave-sintern-rinfo-list (list &optional set default-dir)
4249 "Sintern all strings in the rinfo LIST. With optional parameter
4250 SET: also set new patterns. Probably this will always have to be t.
4251 If DEFAULT-DIR is passed, it is used as the base of the directory"
4252 (let (entry name type class kwds res source call new)
4253 (while list
4254 (setq entry (car list)
4255 list (cdr list)
4256 name (car entry)
4257 type (nth 1 entry)
4258 class (nth 2 entry)
4259 source (nth 3 entry)
4260 call (nth 4 entry)
4261 kwds (nthcdr 5 entry))
4262
4263 ;; The class and name
4264 (if class
4265 (progn
4266 (if (symbolp class) (setq class (symbol-name class)))
4267 (setq class (idlwave-sintern-class class set))
4268 (setq name (idlwave-sintern-method name set)))
4269 (setq name (idlwave-sintern-routine name set)))
4270
4271 ;; The source
4272 (let ((source-type (car source))
4273 (source-file (nth 1 source))
4274 (source-dir (if default-dir
4275 (file-name-as-directory default-dir)
4276 (nth 2 source)))
4277 (source-lib (nth 3 source)))
4278 (if (stringp source-dir)
4279 (setq source-dir (idlwave-sintern-dir source-dir set)))
4280 (if (stringp source-lib)
4281 (setq source-lib (idlwave-sintern-libname source-lib set)))
4282 (setq source (list source-type source-file source-dir source-lib)))
4283
4284 ;; The keywords
4285 (setq kwds (mapcar (lambda (x)
4286 (idlwave-sintern-keyword-list x set))
4287 kwds))
4288
4289 ;; Build a canonicalized list
4290 (setq new (nconc (list name type class source call) kwds)
4291 res (cons new res)))
4292 (nreverse res)))
4293
4294 ;; Creating new sintern tables
4295
4296 (defun idlwave-new-sintern-type (tag)
4297 "Define a variable and a function to sintern the new type TAG.
4298 This defines the function `idlwave-sintern-TAG' and the variable
4299 `idlwave-sint-TAGs'."
4300 (let* ((name (symbol-name tag))
4301 (names (concat name "s"))
4302 (var (intern (concat "idlwave-sint-" names)))
4303 (func (intern (concat "idlwave-sintern-" name))))
4304 (set var nil) ; initial value of the association list
4305 (fset func ; set the function
4306 `(lambda (name &optional set)
4307 (cond ((not (stringp name)) name)
4308 ((cdr (assoc (downcase name) ,var)))
4309 (set
4310 (setq ,var (cons (cons (downcase name) name) ,var))
4311 name)
4312 (name))))))
4313
4314 (defun idlwave-reset-sintern-type (tag)
4315 "Reset the sintern variable associated with TAG."
4316 (set (intern (concat "idlwave-sint-" (symbol-name tag) "s")) nil))
4317
4318 ;;---------------------------------------------------------------------------
4319
4320
4321 ;; The variables which hold the information
4322 (defvar idlwave-system-routines nil
4323 "Holds the routine-info obtained by scanning buffers.")
4324 (defvar idlwave-buffer-routines nil
4325 "Holds the routine-info obtained by scanning buffers.")
4326 (defvar idlwave-compiled-routines nil
4327 "Holds the routine-info obtained by asking the shell.")
4328 (defvar idlwave-unresolved-routines nil
4329 "Holds the unresolved routine-info obtained by asking the shell.")
4330 (defvar idlwave-user-catalog-routines nil
4331 "Holds the procedure routine-info from the user scan.")
4332 (defvar idlwave-library-catalog-routines nil
4333 "Holds the procedure routine-info from the .idlwave_catalog library files.")
4334 (defvar idlwave-library-catalog-libname nil
4335 "Name of library catalog loaded from .idlwave_catalog files.")
4336 (defvar idlwave-path-alist nil
4337 "Alist with !PATH directories and zero or more flags if the dir has
4338 been scanned in a user catalog ('user) or discovered in a library
4339 catalog \('lib).")
4340 (defvar idlwave-true-path-alist nil
4341 "Like `idlwave-path-alist', but with true filenames.")
4342 (defvar idlwave-routines nil
4343 "Holds the combinded procedure/function/method routine-info.")
4344 (defvar idlwave-class-alist nil
4345 "Holds the class names known to IDLWAVE.")
4346 (defvar idlwave-class-history nil
4347 "The history of classes selected with the minibuffer.")
4348 (defvar idlwave-force-class-query nil)
4349 (defvar idlwave-before-completion-wconf nil
4350 "The window configuration just before the completion buffer was displayed.")
4351 (defvar idlwave-last-system-routine-info-cons-cell nil
4352 "The last cons cell in the system routine info.")
4353
4354 ;;
4355 ;; The code to get routine info from different sources.
4356
4357 (defvar idlwave-system-routines)
4358 (defvar idlwave-catalog-process nil
4359 "The background process currently updating the catalog.")
4360
4361 (defun idlwave-routines ()
4362 "Provide a list of IDL routines.
4363 This routine loads the builtin routines on the first call. Later it
4364 only returns the value of the variable."
4365 (if (and idlwave-catalog-process
4366 (processp idlwave-catalog-process))
4367 (progn
4368 (cond
4369 ((equal (process-status idlwave-catalog-process) 'exit)
4370 (message "updating........")
4371 (setq idlwave-catalog-process nil)
4372 (idlwave-update-routine-info '(4)))
4373 ((equal (process-status idlwave-catalog-process) 'run)
4374 ;; Keep it running...
4375 )
4376 (t
4377 ;; Something is wrong, get rid of the process
4378 (message "Problem with catalog process") (beep)
4379 (condition-case nil
4380 (kill-process idlwave-catalog-process)
4381 (error nil))
4382 (setq idlwave-catalog-process nil)))))
4383 (or idlwave-routines
4384 (progn
4385 (idlwave-update-routine-info)
4386 ;; return the current value
4387 idlwave-routines)))
4388
4389 (defvar idlwave-update-rinfo-hook nil
4390 "List of functions which should run after a global rinfo update.
4391 Does not run after automatic updates of buffer or the shell.")
4392
4393 (defun idlwave-rescan-catalog-directories ()
4394 "Rescan the previously selected directories. For batch processing."
4395 (idlwave-update-routine-info '(16)))
4396
4397 (defun idlwave-rescan-asynchronously ()
4398 "Dispatch another Emacs instance to update the idlwave catalog.
4399 After the process finishes normally, the first access to routine info
4400 will re-read the catalog."
4401 (interactive)
4402 (if (processp idlwave-catalog-process)
4403 (if (eq (process-status idlwave-catalog-process) 'run)
4404 (if (yes-or-no-p "A catalog-updating process is running. Kill it? ")
4405 (progn
4406 (condition-case nil
4407 (kill-process idlwave-catalog-process)
4408 (error nil))
4409 (error "Process killed, no new process started"))
4410 (error "Quit"))
4411 (condition-case nil
4412 (kill-process idlwave-catalog-process)
4413 (error nil))))
4414 (if (or (not idlwave-user-catalog-file)
4415 (not (stringp idlwave-user-catalog-file))
4416 (not (file-regular-p idlwave-user-catalog-file)))
4417 (error "No catalog has been produced yet"))
4418 (let* ((emacs (concat invocation-directory invocation-name))
4419 (args (list "-batch"
4420 "-l" (expand-file-name "~/.emacs")
4421 "-l" "idlwave"
4422 "-f" "idlwave-rescan-catalog-directories"))
4423 (process (apply 'start-process "idlcat"
4424 nil emacs args)))
4425 (setq idlwave-catalog-process process)
4426 (set-process-sentinel
4427 process
4428 (lambda (pro why)
4429 (when (string-match "finished" why)
4430 (setq idlwave-routines nil
4431 idlwave-system-routines nil
4432 idlwave-catalog-process nil)
4433 (or (idlwave-start-load-rinfo-timer)
4434 (idlwave-update-routine-info '(4))))))
4435 (message "Background job started to update catalog file")))
4436
4437
4438 ;; Format for all routine info user catalog, library catalogs, etc.:
4439 ;;
4440 ;; ("ROUTINE" type class
4441 ;; (system) | (lib pro_file dir "LIBNAME") | (user pro_file dir "USERLIB") |
4442 ;; (buffer pro_file dir) | (compiled pro_file dir)
4443 ;; "calling_string" ("HELPFILE" (("KWD1" . link1) ...))
4444 ;; ("HELPFILE2" (("KWD2" . link) ...)) ...)
4445 ;;
4446 ;; DIR will be supplied dynamically while loading library catalogs,
4447 ;; and is sinterned to save space, as is LIBNAME. PRO_FILE can be a
4448 ;; complete filepath, in which case DIR is unnecessary. HELPFILE can
4449 ;; be nil, as can LINK1, etc., if no HTML help is available.
4450
4451
4452 (defvar idlwave-load-rinfo-idle-timer)
4453 (defvar idlwave-shell-path-query)
4454
4455 (defun idlwave-update-routine-info (&optional arg no-concatenate)
4456 "Update the internal routine-info lists.
4457 These lists are used by `idlwave-routine-info' (\\[idlwave-routine-info])
4458 and by `idlwave-complete' (\\[idlwave-complete]) to provide information
4459 about individual routines.
4460
4461 The information can come from 4 sources:
4462 1. IDL programs in the current editing session
4463 2. Compiled modules in an IDL shell running as Emacs subprocess
4464 3. A list which covers the IDL system routines.
4465 4. A list which covers the prescanned library files.
4466
4467 Scans all IDLWAVE-mode buffers of the current editing session (see
4468 `idlwave-scan-all-buffers-for-routine-info').
4469 When an IDL shell is running, this command also queries the IDL program
4470 for currently compiled routines.
4471
4472 With prefix ARG, also reload the system and library lists.
4473 With two prefix ARG's, also rescans the chosen user catalog tree.
4474 With three prefix args, dispatch asynchronous process to do the update.
4475
4476 If NO-CONCATENATE is non-nil, don't pre-concatenate the routine info
4477 lists, but instead wait for the shell query to complete and
4478 asynchronously finish updating routine info. This is set
4479 automatically when called interactively. When you need routine
4480 information updated immediately, leave NO-CONCATENATE nil."
4481 (interactive "P\np")
4482 ;; Stop any idle processing
4483 (if (or (and (fboundp 'itimerp)
4484 (itimerp idlwave-load-rinfo-idle-timer))
4485 (and (fboundp 'timerp)
4486 (timerp idlwave-load-rinfo-idle-timer)))
4487 (cancel-timer idlwave-load-rinfo-idle-timer))
4488 (cond
4489 ((equal arg '(64))
4490 ;; Start a background process which updates the catalog.
4491 (idlwave-rescan-asynchronously))
4492 ((equal arg '(16))
4493 ;; Update the user catalog now, and wait for them.
4494 (idlwave-create-user-catalog-file t))
4495 (t
4496 (let* ((load (or arg
4497 idlwave-buffer-case-takes-precedence
4498 (null idlwave-routines)))
4499 ;; The override-idle means, even if the idle timer has done some
4500 ;; preparing work, load and renormalize everything anyway.
4501 (override-idle (or arg idlwave-buffer-case-takes-precedence)))
4502
4503 (setq idlwave-buffer-routines nil
4504 idlwave-compiled-routines nil
4505 idlwave-unresolved-routines nil)
4506 ;; Reset the appropriate hashes
4507 (if (get 'idlwave-reset-sintern 'done-by-idle)
4508 ;; reset was already done in idle time, so skip this step now once
4509 (put 'idlwave-reset-sintern 'done-by-idle nil)
4510 (idlwave-reset-sintern (cond (load t)
4511 ((null idlwave-system-routines) t)
4512 (t 'bufsh))))
4513
4514 (if idlwave-buffer-case-takes-precedence
4515 ;; We can safely scan the buffer stuff first
4516 (progn
4517 (idlwave-update-buffer-routine-info)
4518 (and load (idlwave-load-all-rinfo override-idle)))
4519 ;; We first do the system info, and then the buffers
4520 (and load (idlwave-load-all-rinfo override-idle))
4521 (idlwave-update-buffer-routine-info))
4522
4523 ;; Let's see if there is a shell
4524 (let* ((shell-is-running (and (fboundp 'idlwave-shell-is-running)
4525 (idlwave-shell-is-running)))
4526 (ask-shell (and shell-is-running
4527 idlwave-query-shell-for-routine-info)))
4528
4529 ;; Load the library catalogs again, first re-scanning the path
4530 (when arg
4531 (if shell-is-running
4532 (idlwave-shell-send-command idlwave-shell-path-query
4533 '(progn
4534 (idlwave-shell-get-path-info)
4535 (idlwave-scan-library-catalogs))
4536 'hide)
4537 (idlwave-scan-library-catalogs)))
4538
4539 (if (or (not ask-shell)
4540 (not no-concatenate))
4541 ;; 1. If we are not going to ask the shell, we need to do the
4542 ;; concatenation now.
4543 ;; 2. When this function is called non-interactively, it
4544 ;; means that someone needs routine info *now*. The
4545 ;; shell update causes the concatenation to be
4546 ;; *delayed*, so not in time for the current command.
4547 ;; Therefore, we do a concatenation now, even though
4548 ;; the shell might do it again.
4549 (idlwave-concatenate-rinfo-lists nil 'run-hooks))
4550
4551 (when ask-shell
4552 ;; Ask the shell about the routines it knows of.
4553 (message "Querying the shell")
4554 (idlwave-shell-update-routine-info nil t)))))))
4555
4556
4557 (defvar idlwave-load-rinfo-steps-done (make-vector 6 nil))
4558 (defvar idlwave-load-rinfo-idle-timer nil)
4559 (defun idlwave-start-load-rinfo-timer ()
4560 (if (or (and (fboundp 'itimerp)
4561 (itimerp idlwave-load-rinfo-idle-timer))
4562 (and (fboundp 'timerp)
4563 (timerp idlwave-load-rinfo-idle-timer)))
4564 (cancel-timer idlwave-load-rinfo-idle-timer))
4565 (setq idlwave-load-rinfo-steps-done (make-vector 6 nil))
4566 (setq idlwave-load-rinfo-idle-timer nil)
4567 (if (and idlwave-init-rinfo-when-idle-after
4568 (numberp idlwave-init-rinfo-when-idle-after)
4569 (not (equal 0 idlwave-init-rinfo-when-idle-after))
4570 (not idlwave-routines))
4571 (condition-case nil
4572 (progn
4573 (setq idlwave-load-rinfo-idle-timer
4574 (run-with-idle-timer
4575 idlwave-init-rinfo-when-idle-after
4576 nil 'idlwave-load-rinfo-next-step)))
4577 (error nil))))
4578
4579 (defvar idlwave-library-routines nil "Obsolete variable.")
4580
4581 ;;------ XML Help routine info system
4582 (defun idlwave-load-system-routine-info ()
4583 ;; Load the system routine info from the cached routine info file,
4584 ;; which, if necessary, will be re-created from the XML file on
4585 ;; disk. As a last fallback, load the (likely outdated) idlw-rinfo
4586 ;; file distributed with older IDLWAVE versions (<6.0)
4587 (unless (and (load idlwave-xml-system-rinfo-converted-file
4588 'noerror 'nomessage)
4589 (idlwave-xml-system-routine-info-up-to-date))
4590 ;; See if we can create it from XML source
4591 (condition-case nil
4592 (idlwave-convert-xml-system-routine-info)
4593 (error
4594 (unless (load idlwave-xml-system-rinfo-converted-file
4595 'noerror 'nomessage)
4596 (if idlwave-system-routines
4597 (message
4598 "Failed to load converted routine info, using old conversion.")
4599 (message
4600 "Failed to convert XML routine info, falling back on idlw-rinfo.")
4601 (if (not (load "idlw-rinfo" 'noerror 'nomessage))
4602 (message
4603 "Could not locate any system routine information."))))))))
4604
4605 (defun idlwave-xml-system-routine-info-up-to-date()
4606 (let* ((dir (file-name-as-directory
4607 (expand-file-name "help/online_help" (idlwave-sys-dir))))
4608 (catalog-file (expand-file-name "idl_catalog.xml" dir)))
4609 (file-newer-than-file-p ;converted file is newer than catalog
4610 idlwave-xml-system-rinfo-converted-file
4611 catalog-file)))
4612
4613 (defvar idlwave-system-class-info nil) ; Gathered from idlw-rinfo
4614 (defvar idlwave-system-variables-alist nil
4615 "Alist of system variables and the associated structure tags.
4616 Gets set in cached XML rinfo, or `idlw-rinfo.el'.")
4617 (defvar idlwave-executive-commands-alist nil
4618 "Alist of system variables and their help files.")
4619 (defvar idlwave-help-special-topic-words nil)
4620
4621
4622 (defun idlwave-shorten-syntax (syntax name &optional class)
4623 ;; From a list of syntax statments, shorten with %s and group with "or"
4624 (let ((case-fold-search t))
4625 (mapconcat
4626 (lambda (x)
4627 (while (string-match name x)
4628 (setq x (replace-match "%s" t t x)))
4629 (if class
4630 (while (string-match class x)
4631 (setq x (replace-match "%s" t t x))))
4632 x)
4633 (nreverse syntax)
4634 " or ")))
4635
4636 (defun idlwave-xml-create-class-method-lists (xml-entry)
4637 ;; Create a class list entry from the xml parsed list., returning a
4638 ;; cons of form (class-entry method-entries).
4639 (let* ((nameblock (nth 1 xml-entry))
4640 (class (cdr (assq 'name nameblock)))
4641 (link (cdr (assq 'link nameblock)))
4642 (params (cddr xml-entry))
4643 (case-fold-search t)
4644 class-entry
4645 method methods-entry extra-kwds
4646 props get-props set-props init-props inherits
4647 pelem ptype)
4648 (while params
4649 (setq pelem (car params))
4650 (when (listp pelem)
4651 (setq ptype (car pelem)
4652 props (car (cdr pelem)))
4653 (cond
4654 ((eq ptype 'SUPERCLASS)
4655 (let ((pname (cdr (assq 'name props)))
4656 (plink (cdr (assq 'link props))))
4657 (unless (and (string= pname "None")
4658 (string= plink "None"))
4659 (push pname inherits))))
4660
4661 ((eq ptype 'PROPERTY)
4662 (let ((pname (cdr (assq 'name props)))
4663 (plink (cdr (assq 'link props)))
4664 (get (string= (cdr (assq 'get props)) "Yes"))
4665 (set (string= (cdr (assq 'set props)) "Yes"))
4666 (init (string= (cdr (assq 'init props)) "Yes")))
4667 (if get (push (list pname plink) get-props))
4668 (if set (push (list pname plink) set-props))
4669 (if init (push (list pname plink) init-props))))
4670
4671 ((eq ptype 'METHOD)
4672 (setq method (cdr (assq 'name props)))
4673 (setq extra-kwds ;;Assume all property keywords are gathered already
4674 (cond
4675 ((string-match (concat class "::Init") method)
4676 (put 'init-props 'matched t)
4677 init-props)
4678 ((string-match (concat class "::GetProperty") method)
4679 (put 'get-props 'matched t)
4680 get-props)
4681 ((string-match (concat class "::SetProperty") method)
4682 (put 'set-props 'matched t)
4683 set-props)
4684 (t nil)))
4685 (setq methods-entry
4686 (nconc (idlwave-xml-create-rinfo-list pelem class extra-kwds)
4687 methods-entry)))
4688 (t)))
4689 (setq params (cdr params)))
4690 ;(unless (get 'init-props 'matched)
4691 ; (message "Failed to match Init in class %s" class))
4692 ;(unless (get 'get-props 'matched)
4693 ; (message "Failed to match GetProperty in class %s" class))
4694 ;(unless (get 'set-props 'matched)
4695 ; (message "Failed to match SetProperty in class %s" class))
4696 (setq class-entry
4697 (if inherits
4698 (list class (append '(inherits) inherits) (list 'link link))
4699 (list class (list 'link link))))
4700 (cons class-entry methods-entry)))
4701
4702 (defun idlwave-xml-create-rinfo-list (xml-entry &optional class extra-kws)
4703 ;; Create correctly structured list elements from ROUTINE or METHOD
4704 ;; XML list structures. Return a list of list elements, with more
4705 ;; than one sub-list possible if a routine can serve as both
4706 ;; procedure and function (e.g. call_method).
4707 (let* ((nameblock (nth 1 xml-entry))
4708 (name (cdr (assq 'name nameblock)))
4709 (link (cdr (assq 'link nameblock)))
4710 (params (cddr xml-entry))
4711 (syntax-vec (make-vector 3 nil)) ; procedure, function, exec command
4712 (case-fold-search t)
4713 syntax kwd klink pref-list kwds pelem ptype entry props result type)
4714 (if class ;; strip out class name from class method name string
4715 (if (string-match (concat class "::") name)
4716 (setq name (substring name (match-end 0)))))
4717 (while params
4718 (setq pelem (car params))
4719 (when (listp pelem)
4720 (setq ptype (car pelem)
4721 props (car (cdr pelem)))
4722 (cond
4723 ((eq ptype 'SYNTAX)
4724 (setq syntax (cdr (assq 'name props)))
4725 (if (string-match "-&gt;" syntax)
4726 (setq syntax (replace-match "->" t nil syntax)))
4727 (setq type (cdr (assq 'type props)))
4728 (push syntax
4729 (aref syntax-vec (cond
4730 ((string-match "^pro" type) 0)
4731 ((string-match "^fun" type) 1)
4732 ((string-match "^exec" type) 2)))))
4733 ((eq ptype 'KEYWORD)
4734 (setq kwd (cdr (assq 'name props))
4735 klink (cdr (assq 'link props)))
4736 (if (string-match "^\\[XY\\(Z?\\)\\]" kwd)
4737 (progn
4738 (setq pref-list
4739 (if (match-string 1 kwd) '("X" "Y" "Z") '("X" "Y"))
4740 kwd (substring kwd (match-end 0)))
4741 (loop for x in pref-list do
4742 (push (list (concat x kwd) klink) kwds)))
4743 (push (list kwd klink) kwds)))
4744
4745 (t))); Do nothing for the others
4746 (setq params (cdr params)))
4747
4748 ;; Debug
4749 ; (if (and (null (aref syntax-vec 0))
4750 ; (null (aref syntax-vec 1))
4751 ; (null (aref syntax-vec 2)))
4752 ; (with-current-buffer (get-buffer-create "IDL_XML_catalog_complaints")
4753 ; (if class
4754 ; (insert (format "Missing SYNTAX entry for %s::%s\n" class name))
4755 ; (insert (message "Missing SYNTAX entry for %s\n" name)))))
4756
4757 ;; Executive commands are treated specially
4758 (if (aref syntax-vec 2)
4759 (cons (substring name 1) link)
4760 (if extra-kws (setq kwds (nconc kwds extra-kws)))
4761 (setq kwds (idlwave-rinfo-group-keywords kwds link))
4762 (loop for idx from 0 to 1 do
4763 (if (aref syntax-vec idx)
4764 (push (append (list name (if (eq idx 0) 'pro 'fun)
4765 class '(system)
4766 (idlwave-shorten-syntax
4767 (aref syntax-vec idx) name class))
4768 kwds) result)))
4769 result)))
4770
4771
4772 (defun idlwave-rinfo-group-keywords (kwds master-link)
4773 ;; Group keywords by link file, as a list with elements
4774 ;; (linkfile ( ("KWD1" . link1) ("KWD2" . link2))
4775 (let (kwd link anchor linkfiles block master-elt)
4776 (while kwds
4777 (setq kwd (car kwds)
4778 link (idlwave-split-link-target (nth 1 kwd))
4779 anchor (cdr link)
4780 link (car link)
4781 kwd (car kwd))
4782 (if (setq block (assoc link linkfiles))
4783 (push (cons kwd anchor) (cdr block))
4784 (push (list link (cons kwd anchor)) linkfiles))
4785 (setq kwds (cdr kwds)))
4786 ;; Ensure the master link is there
4787 (if (setq master-elt (assoc master-link linkfiles))
4788 (if (eq (car linkfiles) master-elt)
4789 linkfiles
4790 (cons master-elt (delq master-elt linkfiles)))
4791 (push (list master-link) linkfiles))))
4792
4793 (defun idlwave-convert-xml-clean-statement-aliases (aliases)
4794 ;; Clean up the syntax of routines which are actually aliases by
4795 ;; removing the "OR" from the statements
4796 (let (syntax entry)
4797 (loop for x in aliases do
4798 (setq entry (assoc x idlwave-system-routines))
4799 (when entry
4800 (while (string-match " +or +" (setq syntax (nth 4 entry)))
4801 (setf (nth 4 entry) (replace-match ", " t t syntax)))))))
4802
4803 (defun idlwave-convert-xml-clean-routine-aliases (aliases)
4804 ;; Duplicate and trim original routine aliases from rinfo list
4805 ;; This if for, e.g. OPENR/OPENW/OPENU
4806 (let (alias remove-list new parts all-parts)
4807 (loop for x in aliases do
4808 (when (setq parts (split-string (cdr x) "/"))
4809 (setq new (assoc (cdr x) all-parts))
4810 (unless new
4811 (setq new (cons (cdr x) parts))
4812 (push new all-parts))
4813 (setcdr new (delete (car x) (cdr new)))))
4814
4815 ;; Add any missing aliases (separate by slashes)
4816 (loop for x in all-parts do
4817 (if (cdr x)
4818 (push (cons (nth 1 x) (car x)) aliases)))
4819
4820 (loop for x in aliases do
4821 (when (setq alias (assoc (cdr x) idlwave-system-routines))
4822 (unless (memq alias remove-list) (push alias remove-list))
4823 (setq alias (copy-sequence alias))
4824 (setcar alias (car x))
4825 (push alias idlwave-system-routines)))
4826 (loop for x in remove-list do
4827 (delq x idlwave-system-routines))))
4828
4829 (defun idlwave-convert-xml-clean-sysvar-aliases (aliases)
4830 ;; Duplicate and trim original routine aliases from rinfo list
4831 ;; This if for, e.g. !X, !Y, !Z.
4832 (let (alias remove-list new parts all-parts)
4833 (loop for x in aliases do
4834 (when (setq alias (assoc (cdr x) idlwave-system-variables-alist))
4835 (unless (memq alias remove-list) (push alias remove-list))
4836 (setq alias (copy-sequence alias))
4837 (setcar alias (car x))
4838 (push alias idlwave-system-variables-alist)))
4839 (loop for x in remove-list do
4840 (delq x idlwave-system-variables-alist))))
4841
4842
4843 (defun idlwave-xml-create-sysvar-alist (xml-entry)
4844 ;; Create a sysvar list entry from the xml parsed list.
4845 (let* ((nameblock (nth 1 xml-entry))
4846 (name (cdr (assq 'name nameblock)))
4847 (sysvar (substring name (progn (string-match "^ *!" name)
4848 (match-end 0))))
4849 (link (cdr (assq 'link nameblock)))
4850 (params (cddr xml-entry))
4851 (case-fold-search t)
4852 pelem ptype props fields tags)
4853 (while params
4854 (setq pelem (car params))
4855 (when (listp pelem)
4856 (setq ptype (car pelem)
4857 props (car (cdr pelem)))
4858 (cond
4859 ((eq ptype 'FIELD)
4860 (push (cons (cdr (assq 'name props))
4861 (cdr
4862 (idlwave-split-link-target (cdr (assq 'link props)))))
4863 tags))))
4864 (setq params (cdr params)))
4865 (delq nil
4866 (list sysvar (if tags (cons 'tags tags)) (list 'link link)))))
4867
4868
4869 (defvar idlwave-xml-routine-info-file nil)
4870
4871 (defun idlwave-save-routine-info ()
4872 (if idlwave-xml-routine-info-file
4873 (with-temp-file idlwave-xml-system-rinfo-converted-file
4874 (insert
4875 (concat ";; *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
4876 ;; IDLWAVE Routine Information File (IDLWAVE version " idlwave-mode-version ")
4877 ;; Automatically generated from source file:
4878 ;; " idlwave-xml-routine-info-file "
4879 ;; on " (current-time-string) "
4880 ;; Do not edit."))
4881 (insert (format "\n(setq idlwave-xml-routine-info-file \n \"%s\")"
4882 idlwave-xml-routine-info-file))
4883 (insert "\n(setq idlwave-system-routines\n '")
4884 (prin1 idlwave-system-routines (current-buffer))
4885 (insert ")")
4886 (insert "\n(setq idlwave-system-variables-alist\n '")
4887 (prin1 idlwave-system-variables-alist (current-buffer))
4888 (insert ")")
4889 (insert "\n(setq idlwave-system-class-info\n '")
4890 (prin1 idlwave-system-class-info (current-buffer))
4891 (insert ")")
4892 (insert "\n(setq idlwave-executive-commands-alist\n '")
4893 (prin1 idlwave-executive-commands-alist (current-buffer))
4894 (insert ")")
4895 (insert "\n(setq idlwave-help-special-topic-words\n '")
4896 (prin1 idlwave-help-special-topic-words (current-buffer))
4897 (insert ")"))))
4898
4899 (defun idlwave-convert-xml-system-routine-info ()
4900 "Convert XML supplied IDL routine info into internal form.
4901 Cache to disk for quick recovery."
4902 (interactive)
4903 (let* ((dir (file-name-as-directory
4904 (expand-file-name "help/online_help" (idlwave-sys-dir))))
4905 (catalog-file (expand-file-name "idl_catalog.xml" dir))
4906 (elem-cnt 0)
4907 props rinfo msg-cnt elem type nelem class-result alias
4908 routines routine-aliases statement-aliases sysvar-aliases
4909 version-string)
4910 (if (not (file-exists-p catalog-file))
4911 (error "No such XML routine info file: %s" catalog-file)
4912 (if (not (file-readable-p catalog-file))
4913 (error "Cannot read XML routine info file: %s" catalog-file)))
4914 (require 'xml)
4915 (message "Reading XML routine info...")
4916 (setq rinfo (xml-parse-file catalog-file))
4917 (message "Reading XML routine info...done")
4918 (setq rinfo (assq 'CATALOG rinfo))
4919 (unless rinfo (error "Failed to parse XML routine info"))
4920 ;;(setq rinfo (car rinfo)) ; Skip the catalog stuff.
4921
4922 (setq version-string (cdr (assq 'version (nth 1 rinfo)))
4923 rinfo (cddr rinfo))
4924
4925 (setq nelem (length rinfo)
4926 msg-cnt (/ nelem 20))
4927
4928 (setq idlwave-xml-routine-info-file nil)
4929 (message "Converting XML routine info...")
4930 (setq idlwave-system-routines nil
4931 idlwave-system-variables-alist nil
4932 idlwave-system-class-info nil
4933 idlwave-executive-commands-alist nil
4934 idlwave-help-special-topic-words nil)
4935
4936 (while rinfo
4937 (setq elem (car rinfo)
4938 rinfo (cdr rinfo))
4939 (incf elem-cnt)
4940 (when (listp elem)
4941 (setq type (car elem)
4942 props (car (cdr elem)))
4943 (if (= (mod elem-cnt msg-cnt) 0)
4944 (message "Converting XML routine info...%2d%%"
4945 (/ (* elem-cnt 100) nelem)))
4946 (cond
4947 ((eq type 'ROUTINE)
4948 (if (setq alias (assq 'alias_to props))
4949 (push (cons (cdr (assq 'name props)) (cdr alias))
4950 routine-aliases)
4951 (setq routines (idlwave-xml-create-rinfo-list elem))
4952 (if (listp (cdr routines))
4953 (setq idlwave-system-routines
4954 (nconc idlwave-system-routines routines))
4955 ;; a cons cell is an executive commands
4956 (push routines idlwave-executive-commands-alist))))
4957
4958 ((eq type 'CLASS)
4959 (setq class-result (idlwave-xml-create-class-method-lists elem))
4960 (push (car class-result) idlwave-system-class-info)
4961 (setq idlwave-system-routines
4962 (nconc idlwave-system-routines (cdr class-result))))
4963
4964 ((eq type 'STATEMENT)
4965 (push (cons (cdr (assq 'name props))
4966 (cdr (assq 'link props)))
4967 idlwave-help-special-topic-words)
4968 ;; Save the links to those which are statement aliases (not routines)
4969 (if (setq alias (assq 'alias_to props))
4970 (unless (member (cdr alias) statement-aliases)
4971 (push (cdr alias) statement-aliases))))
4972
4973 ((eq type 'SYSVAR)
4974 (if (setq alias (cdr (assq 'alias_to props)))
4975 (push (cons (substring (cdr (assq 'name props)) 1)
4976 (substring alias 1))
4977 sysvar-aliases)
4978 (push (idlwave-xml-create-sysvar-alist elem)
4979 idlwave-system-variables-alist)))
4980 (t))))
4981 (idlwave-convert-xml-clean-routine-aliases routine-aliases)
4982 (idlwave-convert-xml-clean-statement-aliases statement-aliases)
4983 (idlwave-convert-xml-clean-sysvar-aliases sysvar-aliases)
4984
4985 (setq idlwave-xml-routine-info-file catalog-file)
4986 (idlwave-save-routine-info)
4987 (message "Converting XML routine info...done")))
4988
4989
4990 ;; ("ROUTINE" type class
4991 ;; (system) | (lib pro_file dir "LIBNAME") | (user pro_file dir "USERLIB") |
4992 ;; (buffer pro_file dir) | (compiled pro_file dir)
4993 ;; "calling_string" ("HELPFILE" (("KWD1" . link1) ...))
4994 ;; ("HELPFILE2" (("KWD2" . link) ...)) ...)
4995
4996
4997 (defun idlwave-load-rinfo-next-step ()
4998 (let ((inhibit-quit t)
4999 (arr idlwave-load-rinfo-steps-done))
5000 (if (catch 'exit
5001 (when (not (aref arr 0))
5002 (message "Loading system routine info in idle time...")
5003 (idlwave-load-system-routine-info)
5004 ;;(load "idlw-rinfo" 'noerror 'nomessage)
5005 (message "Loading system routine info in idle time...done")
5006 (aset arr 0 t)
5007 (throw 'exit t))
5008
5009 (when (not (aref arr 1))
5010 (message "Normalizing idlwave-system-routines in idle time...")
5011 (idlwave-reset-sintern t)
5012 (put 'idlwave-reset-sintern 'done-by-idle t)
5013 (setq idlwave-system-routines
5014 (idlwave-sintern-rinfo-list idlwave-system-routines 'sys))
5015 (message "Normalizing idlwave-system-routines in idle time...done")
5016 (aset arr 1 t)
5017 (throw 'exit t))
5018
5019 (when (not (aref arr 2))
5020 (when (and (stringp idlwave-user-catalog-file)
5021 (file-regular-p idlwave-user-catalog-file))
5022 (message "Loading user catalog in idle time...")
5023 (condition-case nil
5024 (load-file idlwave-user-catalog-file)
5025 (error (throw 'exit nil)))
5026 ;; Check for the old style catalog and warn
5027 (if (and
5028 (boundp 'idlwave-library-routines)
5029 idlwave-library-routines)
5030 (progn
5031 (setq idlwave-library-routines nil)
5032 (ding)
5033 (message "Outdated user catalog: %s... recreate"
5034 idlwave-user-catalog-file))
5035 (message "Loading user catalog in idle time...done")))
5036 (aset arr 2 t)
5037 (throw 'exit t))
5038
5039 (when (not (aref arr 3))
5040 (when idlwave-user-catalog-routines
5041 (message "Normalizing user catalog routines in idle time...")
5042 (setq idlwave-user-catalog-routines
5043 (idlwave-sintern-rinfo-list
5044 idlwave-user-catalog-routines 'sys))
5045 (message
5046 "Normalizing user catalog routines in idle time...done"))
5047 (aset arr 3 t)
5048 (throw 'exit t))
5049
5050 (when (not (aref arr 4))
5051 (idlwave-scan-library-catalogs
5052 "Loading and normalizing library catalogs in idle time...")
5053 (aset arr 4 t)
5054 (throw 'exit t))
5055 (when (not (aref arr 5))
5056 (message "Finishing initialization in idle time...")
5057 (idlwave-routines)
5058 (message "Finishing initialization in idle time...done")
5059 (aset arr 5 t)
5060 (throw 'exit nil)))
5061 ;; restart the timer
5062 (if (sit-for 1)
5063 (idlwave-load-rinfo-next-step)
5064 (setq idlwave-load-rinfo-idle-timer
5065 (run-with-idle-timer
5066 idlwave-init-rinfo-when-idle-after
5067 nil 'idlwave-load-rinfo-next-step))))))
5068
5069 (defun idlwave-load-all-rinfo (&optional force)
5070 ;; Load and case-treat the system, user catalog, and library routine
5071 ;; info files.
5072
5073 ;; System
5074 (when (or force (not (aref idlwave-load-rinfo-steps-done 0)))
5075 ;;(load "idlw-rinfo" 'noerror 'nomessage))
5076 (idlwave-load-system-routine-info))
5077 (when (or force (not (aref idlwave-load-rinfo-steps-done 1)))
5078 (message "Normalizing idlwave-system-routines...")
5079 (setq idlwave-system-routines
5080 (idlwave-sintern-rinfo-list idlwave-system-routines 'sys))
5081 (message "Normalizing idlwave-system-routines...done"))
5082 (when idlwave-system-routines
5083 (setq idlwave-routines (copy-sequence idlwave-system-routines))
5084 (setq idlwave-last-system-routine-info-cons-cell
5085 (nthcdr (1- (length idlwave-routines)) idlwave-routines)))
5086
5087 ;; User catalog
5088 (when (and (stringp idlwave-user-catalog-file)
5089 (file-regular-p idlwave-user-catalog-file))
5090 (condition-case nil
5091 (when (or force (not (aref idlwave-load-rinfo-steps-done 2)))
5092 (load-file idlwave-user-catalog-file))
5093 (error nil))
5094 (when (and
5095 (boundp 'idlwave-library-routines)
5096 idlwave-library-routines)
5097 (setq idlwave-library-routines nil)
5098 (error "Outdated user catalog: %s... recreate"
5099 idlwave-user-catalog-file))
5100 (setq idlwave-true-path-alist nil)
5101 (when (or force (not (aref idlwave-load-rinfo-steps-done 3)))
5102 (message "Normalizing user catalog routines...")
5103 (setq idlwave-user-catalog-routines
5104 (idlwave-sintern-rinfo-list
5105 idlwave-user-catalog-routines 'sys))
5106 (message "Normalizing user catalog routines...done")))
5107
5108 ;; Library catalog
5109 (when (or force (not (aref idlwave-load-rinfo-steps-done 4)))
5110 (idlwave-scan-library-catalogs
5111 "Loading and normalizing library catalogs..."))
5112 (run-hooks 'idlwave-after-load-rinfo-hook))
5113
5114
5115 (defun idlwave-update-buffer-routine-info ()
5116 (let (res)
5117 (cond
5118 ((eq idlwave-scan-all-buffers-for-routine-info t)
5119 ;; Scan all buffers, current buffer last
5120 (message "Scanning all buffers...")
5121 (setq res (idlwave-get-routine-info-from-buffers
5122 (reverse (buffer-list)))))
5123 ((null idlwave-scan-all-buffers-for-routine-info)
5124 ;; Don't scan any buffers
5125 (setq res nil))
5126 (t
5127 ;; Just scan this buffer
5128 (if (eq major-mode 'idlwave-mode)
5129 (progn
5130 (message "Scanning current buffer...")
5131 (setq res (idlwave-get-routine-info-from-buffers
5132 (list (current-buffer))))))))
5133 ;; Put the result into the correct variable
5134 (setq idlwave-buffer-routines
5135 (idlwave-sintern-rinfo-list res 'set))))
5136
5137 (defun idlwave-concatenate-rinfo-lists (&optional quiet run-hook)
5138 "Put the different sources for routine information together."
5139 ;; The sequence here is important because earlier definitions shadow
5140 ;; later ones. We assume that if things in the buffers are newer
5141 ;; then in the shell of the system, they are meant to be different.
5142 (setcdr idlwave-last-system-routine-info-cons-cell
5143 (append idlwave-buffer-routines
5144 idlwave-compiled-routines
5145 idlwave-library-catalog-routines
5146 idlwave-user-catalog-routines))
5147 (setq idlwave-class-alist nil)
5148
5149 ;; Give a message with information about the number of routines we have.
5150 (unless quiet
5151 (message
5152 "Routines Found: buffer(%d) compiled(%d) library(%d) user(%d) system(%d)"
5153 (length idlwave-buffer-routines)
5154 (length idlwave-compiled-routines)
5155 (length idlwave-library-catalog-routines)
5156 (length idlwave-user-catalog-routines)
5157 (length idlwave-system-routines)))
5158 (if run-hook
5159 (run-hooks 'idlwave-update-rinfo-hook)))
5160
5161 (defun idlwave-class-alist ()
5162 "Return the class alist - make it if necessary."
5163 (or idlwave-class-alist
5164 (let (class)
5165 (loop for x in idlwave-routines do
5166 (when (and (setq class (nth 2 x))
5167 (not (assq class idlwave-class-alist)))
5168 (push (list class) idlwave-class-alist)))
5169 idlwave-class-alist)))
5170
5171 ;; Three functions for the hooks
5172 (defun idlwave-save-buffer-update ()
5173 (idlwave-update-current-buffer-info 'save-buffer))
5174 (defun idlwave-kill-buffer-update ()
5175 (idlwave-update-current-buffer-info 'kill-buffer))
5176 (defun idlwave-new-buffer-update ()
5177 (idlwave-update-current-buffer-info 'find-file))
5178
5179 (defun idlwave-update-current-buffer-info (why)
5180 "Update idlwave-routines for current buffer. Can run from after-save-hook."
5181 (when (and (eq major-mode 'idlwave-mode)
5182 (or (eq t idlwave-auto-routine-info-updates)
5183 (memq why idlwave-auto-routine-info-updates))
5184 idlwave-scan-all-buffers-for-routine-info
5185 idlwave-routines)
5186 (condition-case nil
5187 (let (routines)
5188 (idlwave-replace-buffer-routine-info
5189 (buffer-file-name)
5190 (if (eq why 'kill-buffer)
5191 nil
5192 (setq routines
5193 (idlwave-sintern-rinfo-list
5194 (idlwave-get-routine-info-from-buffers
5195 (list (current-buffer))) 'set))))
5196 (idlwave-concatenate-rinfo-lists 'quiet)
5197 routines)
5198 (error nil))))
5199
5200 (defun idlwave-replace-buffer-routine-info (file new)
5201 "Cut the part from FILE out of `idlwave-buffer-routines' and add NEW."
5202 (let ((list idlwave-buffer-routines)
5203 found)
5204 (while list
5205 ;; The following test uses eq to make sure it works correctly
5206 ;; when two buffers visit the same file. Then the file names
5207 ;; will be equal, but not eq.
5208 (if (eq (idlwave-routine-source-file (nth 3 (car list))) file)
5209 (progn
5210 (setcar list nil)
5211 (setq found t))
5212 (if found
5213 ;; End of that section reached. Jump.
5214 (setq list nil)))
5215 (setq list (cdr list)))
5216 (setq idlwave-buffer-routines
5217 (append new (delq nil idlwave-buffer-routines)))))
5218
5219 ;;----- Scanning buffers -------------------
5220
5221 (defun idlwave-get-routine-info-from-buffers (buffers)
5222 "Call `idlwave-get-buffer-routine-info' on idlwave-mode buffers in BUFFERS."
5223 (let (buf routine-lists res)
5224 (save-excursion
5225 (while (setq buf (pop buffers))
5226 (set-buffer buf)
5227 (if (and (eq major-mode 'idlwave-mode)
5228 buffer-file-name)
5229 ;; yes, this buffer has the right mode.
5230 (progn (setq res (condition-case nil
5231 (idlwave-get-buffer-routine-info)
5232 (error nil)))
5233 (push res routine-lists)))))
5234 ;; Concatenate the individual lists and return the result
5235 (apply 'nconc routine-lists)))
5236
5237 (defun idlwave-get-buffer-routine-info ()
5238 "Scan the current buffer for routine info. Return (PRO-LIST FUNC-LIST)."
5239 (let* ((case-fold-search t)
5240 routine-list string entry)
5241 (save-excursion
5242 (save-restriction
5243 (widen)
5244 (goto-char (point-min))
5245 (while (re-search-forward
5246 "^[ \t]*\\(pro\\|function\\)[ \t]" nil t)
5247 (setq string (buffer-substring-no-properties
5248 (match-beginning 0)
5249 (progn
5250 (idlwave-end-of-statement)
5251 (point))))
5252 (setq entry (idlwave-parse-definition string))
5253 (push entry routine-list))))
5254 routine-list))
5255
5256 (defvar idlwave-scanning-lib-dir)
5257 (defun idlwave-parse-definition (string)
5258 "Parse a module definition."
5259 (let ((case-fold-search t)
5260 start name args type keywords class)
5261 ;; Remove comments
5262 (while (string-match ";.*" string)
5263 (setq string (replace-match "" t t string)))
5264 ;; Remove the continuation line stuff
5265 (while (string-match "\\([^a-zA-Z0-9$_]\\)\\$[ \t]*\n" string)
5266 (setq string (replace-match "\\1 " t nil string)))
5267 (while (string-match "\n" string)
5268 (setq string (replace-match " " t nil string)))
5269 ;; Match the name and type.
5270 (when (string-match
5271 "\\<\\(pro\\|function\\)\\>\\s-+\\(\\([a-zA-Z0-9$_]+\\)::\\)?\\([a-zA-Z0-9$_]+\\)" string)
5272 (setq start (match-end 0))
5273 (setq type (downcase (match-string 1 string)))
5274 (if (match-beginning 3)
5275 (setq class (match-string 3 string)))
5276 (setq name (match-string 4 string)))
5277 ;; Match normal args and keyword args
5278 (while (string-match
5279 ",\\s-*\\([a-zA-Z][a-zA-Z0-9$_]*\\|\\(_ref\\)?_extra\\)\\s-*\\(=\\)?"
5280 string start)
5281 (setq start (match-end 0))
5282 (if (match-beginning 3)
5283 (push (match-string 1 string) keywords)
5284 (push (match-string 1 string) args)))
5285 ;; Normalize and sort.
5286 (setq args (nreverse args))
5287 (setq keywords (sort keywords (lambda (a b)
5288 (string< (downcase a) (downcase b)))))
5289 ;; Make and return the entry
5290 ;; We don't know which argument are optional, so this information
5291 ;; will not be contained in the calling sequence.
5292 (list name
5293 (if (equal type "pro") 'pro 'fun)
5294 class
5295 (cond ((not (boundp 'idlwave-scanning-lib))
5296 (list 'buffer (buffer-file-name)))
5297 ; ((string= (downcase
5298 ; (file-name-sans-extension
5299 ; (file-name-nondirectory (buffer-file-name))))
5300 ; (downcase name))
5301 ; (list 'lib))
5302 ; (t (cons 'lib (file-name-nondirectory (buffer-file-name))))
5303 (t (list 'user (file-name-nondirectory (buffer-file-name))
5304 idlwave-scanning-lib-dir "UserLib")))
5305 (concat
5306 (if (string= type "function") "Result = " "")
5307 (if class "Obj ->[%s::]" "")
5308 "%s"
5309 (if args
5310 (concat
5311 (if (string= type "function") "(" ", ")
5312 (mapconcat 'identity args ", ")
5313 (if (string= type "function") ")" ""))))
5314 (if keywords
5315 (cons nil (mapcar 'list keywords)) ;No help file
5316 nil))))
5317
5318
5319 ;;----- Scanning the user catalog -------------------
5320
5321 (defun idlwave-sys-dir ()
5322 "Return the syslib directory, or a dummy that never matches."
5323 (cond
5324 ((and idlwave-system-directory
5325 (not (string= idlwave-system-directory "")))
5326 idlwave-system-directory)
5327 ((getenv "IDL_DIR"))
5328 (t "@@@@@@@@")))
5329
5330
5331 (defun idlwave-create-user-catalog-file (&optional arg)
5332 "Scan all files on selected dirs of IDL search path for routine information.
5333
5334 A widget checklist will allow you to choose the directories. Write
5335 the result as a file `idlwave-user-catalog-file'. When this file
5336 exists, will be automatically loaded to give routine information about
5337 library routines. With ARG, just rescan the same directories as last
5338 time - so no widget will pop up."
5339 (interactive "P")
5340 ;; Make sure the file is loaded if it exists.
5341 (if (and (stringp idlwave-user-catalog-file)
5342 (file-regular-p idlwave-user-catalog-file))
5343 (condition-case nil
5344 (load-file idlwave-user-catalog-file)
5345 (error nil)))
5346 ;; Make sure the file name makes sense
5347 (unless (and (stringp idlwave-user-catalog-file)
5348 (> (length idlwave-user-catalog-file) 0)
5349 (file-accessible-directory-p
5350 (file-name-directory idlwave-user-catalog-file))
5351 (not (string= "" (file-name-nondirectory
5352 idlwave-user-catalog-file))))
5353 (error "`idlwave-user-catalog-file' does not point to a file in an accessible directory"))
5354
5355 (cond
5356 ;; Rescan the known directories
5357 ((and arg idlwave-path-alist
5358 (consp (car idlwave-path-alist)))
5359 (idlwave-scan-user-lib-files idlwave-path-alist))
5360
5361 ;; Expand the directories from library-path and run the widget
5362 (idlwave-library-path
5363 (idlwave-display-user-catalog-widget
5364 (if idlwave-true-path-alist
5365 ;; Propagate any flags on the existing path-alist
5366 (mapcar (lambda (x)
5367 (let ((path-entry (assoc (file-truename x)
5368 idlwave-true-path-alist)))
5369 (if path-entry
5370 (cons x (cdr path-entry))
5371 (list x))))
5372 (idlwave-expand-path idlwave-library-path))
5373 (mapcar 'list (idlwave-expand-path idlwave-library-path)))))
5374
5375 ;; Ask the shell for the path and then run the widget
5376 (t
5377 (message "Asking the shell for IDL path...")
5378 (require 'idlw-shell)
5379 (idlwave-shell-send-command idlwave-shell-path-query
5380 '(idlwave-user-catalog-command-hook nil)
5381 'hide))))
5382
5383
5384 ;; Parse shell path information and select among it.
5385 (defun idlwave-user-catalog-command-hook (&optional arg)
5386 ;; Command hook used by `idlwave-create-user-catalog-file'.
5387 (if arg
5388 ;; Scan immediately
5389 (idlwave-scan-user-lib-files idlwave-path-alist)
5390 ;; Set the path and display the widget
5391 (idlwave-shell-get-path-info 'no-write) ; set to something path-alist
5392 (idlwave-scan-library-catalogs "Locating library catalogs..." 'no-load)
5393 (idlwave-display-user-catalog-widget idlwave-path-alist)))
5394
5395 (defconst idlwave-user-catalog-widget-help-string
5396 "This is the front-end to the creation of the IDLWAVE user catalog.
5397 Please select the directories on IDL's search path from which you
5398 would like to extract routine information, to be stored in the file:
5399
5400 %s
5401
5402 If this is not the correct file, first set variable
5403 `idlwave-user-catalog-file', and call this command again.
5404
5405 N.B. Many libraries include pre-scanned catalog files
5406 \(\".idlwave_catalog\"). These are marked with \"[LIB]\", and need
5407 not be scanned. You can scan your own libraries off-line using the
5408 perl script `idlwave_catalog'.
5409
5410 After selecting the directories, choose [Scan & Save] to scan the library
5411 directories and save the routine info.
5412 \n")
5413
5414 (defvar idlwave-widget)
5415 (defvar widget-keymap)
5416 (defun idlwave-display-user-catalog-widget (dirs-list)
5417 "Create the widget to select IDL search path directories for scanning."
5418 (interactive)
5419 (require 'widget)
5420 (require 'wid-edit)
5421 (unless dirs-list
5422 (error "Don't know IDL's search path"))
5423
5424 (kill-buffer (get-buffer-create "*IDLWAVE Widget*"))
5425 (switch-to-buffer (get-buffer-create "*IDLWAVE Widget*"))
5426 (kill-all-local-variables)
5427 (make-local-variable 'idlwave-widget)
5428 (widget-insert (format idlwave-user-catalog-widget-help-string
5429 idlwave-user-catalog-file))
5430
5431 (widget-create 'push-button
5432 :notify 'idlwave-widget-scan-user-lib-files
5433 "Scan & Save")
5434 (widget-insert " ")
5435 (widget-create 'push-button
5436 :notify 'idlwave-delete-user-catalog-file
5437 "Delete File")
5438 (widget-insert " ")
5439 (widget-create 'push-button
5440 :notify
5441 '(lambda (&rest ignore)
5442 (let ((path-list (widget-get idlwave-widget :path-dirs)))
5443 (mapcar (lambda (x)
5444 (unless (memq 'lib (cdr x))
5445 (idlwave-path-alist-add-flag x 'user)))
5446 path-list)
5447 (idlwave-display-user-catalog-widget path-list)))
5448 "Select All Non-Lib")
5449 (widget-insert " ")
5450 (widget-create 'push-button
5451 :notify
5452 '(lambda (&rest ignore)
5453 (let ((path-list (widget-get idlwave-widget :path-dirs)))
5454 (mapcar (lambda (x)
5455 (idlwave-path-alist-remove-flag x 'user))
5456 path-list)
5457 (idlwave-display-user-catalog-widget path-list)))
5458 "Deselect All")
5459 (widget-insert " ")
5460 (widget-create 'push-button
5461 :notify (lambda (&rest ignore)
5462 (kill-buffer (current-buffer)))
5463 "Quit")
5464 (widget-insert "\n\n")
5465
5466 (widget-insert "Select Directories: \n")
5467
5468 (setq idlwave-widget
5469 (apply 'widget-create
5470 'checklist
5471 :value (delq nil (mapcar (lambda (x)
5472 (if (memq 'user (cdr x))
5473 (car x)))
5474 dirs-list))
5475 :greedy t
5476 :tag "List of directories"
5477 (mapcar (lambda (x)
5478 (list 'item
5479 (if (memq 'lib (cdr x))
5480 (concat "[LIB] " (car x) )
5481 (car x)))) dirs-list)))
5482 (widget-put idlwave-widget :path-dirs dirs-list)
5483 (widget-insert "\n")
5484 (use-local-map widget-keymap)
5485 (widget-setup)
5486 (goto-char (point-min))
5487 (delete-other-windows))
5488
5489 (defun idlwave-delete-user-catalog-file (&rest ignore)
5490 (if (yes-or-no-p
5491 (format "Delete file %s " idlwave-user-catalog-file))
5492 (progn
5493 (delete-file idlwave-user-catalog-file)
5494 (message "%s has been deleted" idlwave-user-catalog-file))))
5495
5496 (defun idlwave-widget-scan-user-lib-files (&rest ignore)
5497 ;; Call `idlwave-scan-user-lib-files' with data taken from the widget.
5498 (let* ((widget idlwave-widget)
5499 (selected-dirs (widget-value widget))
5500 (path-alist (widget-get widget :path-dirs))
5501 (this-path-alist path-alist)
5502 dir-entry)
5503 (while (setq dir-entry (pop this-path-alist))
5504 (if (member
5505 (if (memq 'lib (cdr dir-entry))
5506 (concat "[LIB] " (car dir-entry))
5507 (car dir-entry))
5508 selected-dirs)
5509 (idlwave-path-alist-add-flag dir-entry 'user)
5510 (idlwave-path-alist-remove-flag dir-entry 'user)))
5511 (idlwave-scan-user-lib-files path-alist)))
5512
5513 (defvar font-lock-mode)
5514 (defun idlwave-scan-user-lib-files (path-alist)
5515 ;; Scan the PRO files in PATH-ALIST and store the info in the user catalog
5516 (let* ((idlwave-scanning-lib t)
5517 (idlwave-scanning-lib-dir "")
5518 (idlwave-completion-case nil)
5519 dirs-alist dir files file)
5520 (setq idlwave-user-catalog-routines nil
5521 idlwave-path-alist path-alist ; for library-path instead
5522 idlwave-true-path-alist nil)
5523 (if idlwave-auto-write-paths (idlwave-write-paths))
5524 (save-excursion
5525 (set-buffer (get-buffer-create "*idlwave-scan.pro*"))
5526 (idlwave-mode)
5527 (setq dirs-alist (reverse path-alist))
5528 (while (setq dir (pop dirs-alist))
5529 (when (memq 'user (cdr dir)) ; Has it marked for scan?
5530 (setq dir (car dir))
5531 (setq idlwave-scanning-lib-dir dir)
5532 (when (file-directory-p dir)
5533 (setq files (directory-files dir 'full "\\.[pP][rR][oO]\\'"))
5534 (while (setq file (pop files))
5535 (when (file-regular-p file)
5536 (if (not (file-readable-p file))
5537 (message "Skipping %s (no read permission)" file)
5538 (message "Scanning %s..." file)
5539 (erase-buffer)
5540 (insert-file-contents file 'visit)
5541 (setq idlwave-user-catalog-routines
5542 (append (idlwave-get-routine-info-from-buffers
5543 (list (current-buffer)))
5544 idlwave-user-catalog-routines)))))))))
5545 (message "Creating user catalog file...")
5546 (kill-buffer "*idlwave-scan.pro*")
5547 (kill-buffer (get-buffer-create "*IDLWAVE Widget*"))
5548 (let ((font-lock-maximum-size 0)
5549 (auto-mode-alist nil))
5550 (find-file idlwave-user-catalog-file))
5551 (if (and (boundp 'font-lock-mode)
5552 font-lock-mode)
5553 (font-lock-mode 0))
5554 (erase-buffer)
5555 (insert ";; IDLWAVE user catalog file\n")
5556 (insert (format ";; Created %s\n\n" (current-time-string)))
5557
5558 ;; Define the routine info list
5559 (insert "\n(setq idlwave-user-catalog-routines\n '(")
5560 (let ((standard-output (current-buffer)))
5561 (mapcar (lambda (x)
5562 (insert "\n ")
5563 (prin1 x)
5564 (goto-char (point-max)))
5565 idlwave-user-catalog-routines))
5566 (insert (format "))\n\n;;; %s ends here\n"
5567 (file-name-nondirectory idlwave-user-catalog-file)))
5568 (goto-char (point-min))
5569 ;; Save the buffer
5570 (save-buffer 0)
5571 (kill-buffer (current-buffer)))
5572 (message "Creating user catalog file...done")
5573 (message "Info for %d routines saved in %s"
5574 (length idlwave-user-catalog-routines)
5575 idlwave-user-catalog-file)
5576 (sit-for 2)
5577 (idlwave-update-routine-info t))
5578
5579 (defun idlwave-read-paths ()
5580 (if (and (stringp idlwave-path-file)
5581 (file-regular-p idlwave-path-file))
5582 (condition-case nil
5583 (load idlwave-path-file t t t)
5584 (error nil))))
5585
5586 (defun idlwave-write-paths ()
5587 (interactive)
5588 (when (and idlwave-path-alist idlwave-system-directory)
5589 (let ((font-lock-maximum-size 0)
5590 (auto-mode-alist nil))
5591 (find-file idlwave-path-file))
5592 (if (and (boundp 'font-lock-mode)
5593 font-lock-mode)
5594 (font-lock-mode 0))
5595 (erase-buffer)
5596 (insert ";; IDLWAVE paths\n")
5597 (insert (format ";; Created %s\n\n" (current-time-string)))
5598 ;; Define the variable which knows the value of "!DIR"
5599 (insert (format "\n(setq idlwave-system-directory \"%s\")\n"
5600 idlwave-system-directory))
5601
5602 ;; Define the variable which contains a list of all scanned directories
5603 (insert "\n(setq idlwave-path-alist\n '(")
5604 (let ((standard-output (current-buffer)))
5605 (mapcar (lambda (x)
5606 (insert "\n ")
5607 (prin1 x)
5608 (goto-char (point-max)))
5609 idlwave-path-alist))
5610 (insert "))\n")
5611 (save-buffer 0)
5612 (kill-buffer (current-buffer))))
5613
5614
5615 (defun idlwave-expand-path (path &optional default-dir)
5616 ;; Expand parts of path starting with '+' recursively into directory list.
5617 ;; Relative recursive path elements are expanded relative to DEFAULT-DIR.
5618 (message "Expanding path...")
5619 (let (path1 dir recursive)
5620 (while (setq dir (pop path))
5621 (if (setq recursive (string= (substring dir 0 1) "+"))
5622 (setq dir (substring dir 1)))
5623 (if (and recursive
5624 (not (file-name-absolute-p dir)))
5625 (setq dir (expand-file-name dir default-dir)))
5626 (if recursive
5627 ;; Expand recursively
5628 (setq path1 (append (idlwave-recursive-directory-list dir) path1))
5629 ;; Keep unchanged
5630 (push dir path1)))
5631 (message "Expanding path...done")
5632 (nreverse path1)))
5633
5634 (defun idlwave-recursive-directory-list (dir)
5635 ;; Return a list of all directories below DIR, including DIR itself
5636 (let ((path (list dir)) path1 file files)
5637 (while (setq dir (pop path))
5638 (when (file-directory-p dir)
5639 (setq files (nreverse (directory-files dir t "[^.]")))
5640 (while (setq file (pop files))
5641 (if (file-directory-p file)
5642 (push (file-name-as-directory file) path)))
5643 (push dir path1)))
5644 path1))
5645
5646
5647 ;;----- Scanning the library catalogs ------------------
5648
5649
5650
5651
5652 (defun idlwave-scan-library-catalogs (&optional message-base no-load)
5653 "Scan for library catalog files (.idlwave_catalog) and ingest.
5654
5655 All directories on `idlwave-path-alist' (or `idlwave-library-path'
5656 instead, if present) are searched. Print MESSAGE-BASE along with the
5657 libraries being loaded, if passed, and skip loading/normalizing if
5658 NO-LOAD is non-nil. The variable `idlwave-use-library-catalogs' can
5659 be set to nil to disable library catalog scanning."
5660 (when idlwave-use-library-catalogs
5661 (let ((dirs
5662 (if idlwave-library-path
5663 (idlwave-expand-path idlwave-library-path)
5664 (mapcar 'car idlwave-path-alist)))
5665 (old-libname "")
5666 dir-entry dir flags catalog all-routines)
5667 (if message-base (message message-base))
5668 (while (setq dir (pop dirs))
5669 (catch 'continue
5670 (when (file-readable-p
5671 (setq catalog (expand-file-name ".idlwave_catalog" dir)))
5672 (unless no-load
5673 (setq idlwave-library-catalog-routines nil)
5674 ;; Load the catalog file
5675 (condition-case nil
5676 (load catalog t t t)
5677 (error (throw 'continue t)))
5678 (when (and
5679 message-base
5680 (not (string= idlwave-library-catalog-libname
5681 old-libname)))
5682 (message "%s" (concat message-base
5683 idlwave-library-catalog-libname))
5684 (setq old-libname idlwave-library-catalog-libname))
5685 (when idlwave-library-catalog-routines
5686 (setq all-routines
5687 (append
5688 (idlwave-sintern-rinfo-list
5689 idlwave-library-catalog-routines 'sys dir)
5690 all-routines))))
5691
5692 ;; Add a 'lib flag if on path-alist
5693 (when (and idlwave-path-alist
5694 (setq dir-entry (assoc dir idlwave-path-alist)))
5695 (idlwave-path-alist-add-flag dir-entry 'lib)))))
5696 (unless no-load (setq idlwave-library-catalog-routines all-routines))
5697 (if message-base (message (concat message-base "done"))))))
5698
5699 ;;----- Communicating with the Shell -------------------
5700
5701 ;; First, here is the idl program which can be used to query IDL for
5702 ;; defined routines.
5703 (defconst idlwave-routine-info.pro
5704 "
5705 ;; START OF IDLWAVE SUPPORT ROUTINES
5706 pro idlwave_print_safe,item,limit
5707 catch,err
5708 if err ne 0 then begin
5709 print,'Could not print item.'
5710 return
5711 endif
5712 if n_elements(item) gt limit then $
5713 print,item[0:limit-1],'<... truncated at ',strtrim(limit,2),' elements>' $
5714 else print,item
5715 end
5716
5717 pro idlwave_print_info_entry,name,func=func,separator=sep
5718 ;; See if it's an object method
5719 if name eq '' then return
5720 func = keyword_set(func)
5721 methsep = strpos(name,'::')
5722 meth = methsep ne -1
5723
5724 ;; Get routine info
5725 pars = routine_info(name,/parameters,functions=func)
5726 source = routine_info(name,/source,functions=func)
5727 nargs = pars.num_args
5728 nkw = pars.num_kw_args
5729 if nargs gt 0 then args = pars.args
5730 if nkw gt 0 then kwargs = pars.kw_args
5731
5732 ;; Trim the class, and make the name
5733 if meth then begin
5734 class = strmid(name,0,methsep)
5735 name = strmid(name,methsep+2,strlen(name)-1)
5736 if nargs gt 0 then begin
5737 ;; remove the self argument
5738 wh = where(args ne 'SELF',nargs)
5739 if nargs gt 0 then args = args[wh]
5740 endif
5741 endif else begin
5742 ;; No class, just a normal routine.
5743 class = \"\"
5744 endelse
5745
5746 ;; Calling sequence
5747 cs = \"\"
5748 if func then cs = 'Result = '
5749 if meth then cs = cs + 'Obj -> [' + '%s' + '::]'
5750 cs = cs + '%s'
5751 if func then cs = cs + '(' else if nargs gt 0 then cs = cs + ', '
5752 if nargs gt 0 then begin
5753 for j=0,nargs-1 do begin
5754 cs = cs + args[j]
5755 if j lt nargs-1 then cs = cs + ', '
5756 endfor
5757 end
5758 if func then cs = cs + ')'
5759 ;; Keyword arguments
5760 kwstring = ''
5761 if nkw gt 0 then begin
5762 for j=0,nkw-1 do begin
5763 kwstring = kwstring + ' ' + kwargs[j]
5764 endfor
5765 endif
5766
5767 ret=(['IDLWAVE-PRO','IDLWAVE-FUN'])[func]
5768
5769 print,ret + ': ' + name + sep + class + sep + source[0].path $
5770 + sep + cs + sep + kwstring
5771 end
5772
5773 pro idlwave_routine_info,file
5774 on_error,1
5775 sep = '<@>'
5776 print,'>>>BEGIN OF IDLWAVE ROUTINE INFO (\"' + sep + '\" IS THE SEPARATOR)'
5777 all = routine_info()
5778 fileQ=n_elements(file) ne 0
5779 if fileQ then file=strtrim(file,2)
5780 for i=0L,n_elements(all)-1L do begin
5781 if fileQ then begin
5782 if (routine_info(all[i],/SOURCE)).path eq file then $
5783 idlwave_print_info_entry,all[i],separator=sep
5784 endif else idlwave_print_info_entry,all[i],separator=sep
5785 endfor
5786 all = routine_info(/functions)
5787 for i=0L,n_elements(all)-1L do begin
5788 if fileQ then begin
5789 if (routine_info(all[i],/FUNCTIONS,/SOURCE)).path eq file then $
5790 idlwave_print_info_entry,all[i],separator=sep,/FUNC
5791 endif else idlwave_print_info_entry,all[i],separator=sep,/FUNC
5792 endfor
5793 print,'>>>END OF IDLWAVE ROUTINE INFO'
5794 end
5795
5796 pro idlwave_get_sysvars
5797 on_error,1
5798 catch,error_status
5799 if error_status ne 0 then begin
5800 print, 'Cannot get info about system variables'
5801 endif else begin
5802 help,/brief,output=s,/system_variables ; ? unsafe use of OUTPUT=
5803 s = strtrim(strjoin(s,' ',/single),2) ; make one line
5804 v = strsplit(s,' +',/regex,/extract) ; get variables
5805 for i=0L,n_elements(v)-1 do begin
5806 t = [''] ; get tag list
5807 a=execute('if n_tags('+v[i]+') gt 0 then t=tag_names('+v[i]+')')
5808 print, 'IDLWAVE-SYSVAR: '+v[i]+' '+strjoin(t,' ',/single)
5809 endfor
5810 endelse
5811 end
5812
5813 pro idlwave_get_class_tags, class
5814 res = execute('tags=tag_names({'+class+'})')
5815 if res then print,'IDLWAVE-CLASS-TAGS: '+class+' '+strjoin(tags,' ',/single)
5816 end
5817 ;; END OF IDLWAVE SUPPORT ROUTINES
5818 "
5819 "The idl programs to get info from the shell.")
5820
5821 (defvar idlwave-idlwave_routine_info-compiled nil
5822 "Remembers if the routine info procedure is already compiled.")
5823
5824 (defvar idlwave-shell-temp-pro-file)
5825 (defvar idlwave-shell-temp-rinfo-save-file)
5826
5827 (defun idlwave-shell-compile-helper-routines (&optional wait)
5828 (unless (and idlwave-idlwave_routine_info-compiled
5829 (file-readable-p (idlwave-shell-temp-file 'rinfo)))
5830 (save-excursion
5831 (set-buffer (idlwave-find-file-noselect
5832 (idlwave-shell-temp-file 'pro)))
5833 (erase-buffer)
5834 (insert idlwave-routine-info.pro)
5835 (save-buffer 0))
5836 (idlwave-shell-send-command
5837 (concat ".run \"" idlwave-shell-temp-pro-file "\"")
5838 nil 'hide wait)
5839 (idlwave-shell-send-command
5840 (format "save,'idlwave_print_safe','idlwave_routine_info','idlwave_print_info_entry','idlwave_get_class_tags','idlwave_get_sysvars',FILE='%s',/ROUTINES"
5841 (idlwave-shell-temp-file 'rinfo))
5842 nil 'hide)
5843 (setq idlwave-idlwave_routine_info-compiled t))
5844
5845 ;; Restore if necessary. Must use execute to hide lame routine_info
5846 ;; errors on undefinded routine
5847 (idlwave-shell-send-command
5848 (format "if execute(\"_v=routine_info('idlwave_routine_info',/SOURCE)\") eq 0 then restore,'%s' else if _v.path eq '' then restore,'%s'"
5849 idlwave-shell-temp-rinfo-save-file
5850 idlwave-shell-temp-rinfo-save-file)
5851 nil 'hide))
5852
5853
5854 (defun idlwave-shell-update-routine-info (&optional quiet run-hooks wait file)
5855 "Query the shell for routine_info of compiled modules and update the lists."
5856 ;; Save and compile the procedure. The compiled procedure is then
5857 ;; saved into an IDL SAVE file, to allow for fast RESTORE. We may
5858 ;; need to test for and possibly RESTORE the procedure each time we
5859 ;; use it, since the user may have killed or redefined it. In
5860 ;; particular, .RESET_SESSION will kill all user procedures. If
5861 ;; FILE is set, only update routine info for routines in that file.
5862
5863 (idlwave-shell-compile-helper-routines wait)
5864 ; execute the routine_info procedure, and analyze the output
5865 (idlwave-shell-send-command
5866 (format "idlwave_routine_info%s" (if file (concat ",'" file "'") ""))
5867 `(progn
5868 (idlwave-shell-routine-info-filter)
5869 (idlwave-concatenate-rinfo-lists ,quiet ,run-hooks))
5870 'hide wait))
5871
5872 ;; ---------------------------------------------------------------------------
5873 ;;
5874 ;; Completion and displaying routine calling sequences
5875
5876 (defvar idlwave-completion-help-info nil)
5877 (defvar idlwave-completion-help-links nil)
5878 (defvar idlwave-current-obj_new-class nil)
5879 (defvar idlwave-complete-special nil)
5880
5881 (defun idlwave-complete (&optional arg module class)
5882 "Complete a function, procedure or keyword name at point.
5883 This function is smart and figures out what can be completed
5884 at this point.
5885 - At the beginning of a statement it completes procedure names.
5886 - In the middle of a statement it completes function names.
5887 - after a `(' or `,' in the argument list of a function or procedure,
5888 it completes a keyword of the relevant function or procedure.
5889 - In the first arg of `OBJ_NEW', it completes a class name.
5890
5891 When several completions are possible, a list will be displayed in the
5892 *Completions* buffer. If this list is too long to fit into the
5893 window, scrolling can be achieved by repeatedly pressing
5894 \\[idlwave-complete].
5895
5896 The function also knows about object methods. When it needs a class
5897 name, the action depends upon `idlwave-query-class', which see. You
5898 can force IDLWAVE to ask you for a class name with a
5899 \\[universal-argument] prefix argument to this command.
5900
5901 See also the variables `idlwave-keyword-completion-adds-equal' and
5902 `idlwave-function-completion-adds-paren'.
5903
5904 The optional ARG can be used to specify the completion type in order
5905 to override IDLWAVE's idea of what should be completed at point.
5906 Possible values are:
5907
5908 0 <=> query for the completion type
5909 1 <=> 'procedure
5910 2 <=> 'procedure-keyword
5911 3 <=> 'function
5912 4 <=> 'function-keyword
5913 5 <=> 'procedure-method
5914 6 <=> 'procedure-method-keyword
5915 7 <=> 'function-method
5916 8 <=> 'function-method-keyword
5917 9 <=> 'class
5918
5919 As a special case, the universal argument C-u forces completion of
5920 function names in places where the default would be a keyword.
5921
5922 Two prefix argument, C-u C-u, prompts for a regexp by which to limit
5923 completion.
5924
5925 For Lisp programmers only:
5926 When we force a keyword, optional argument MODULE can contain the module name.
5927 When we force a method or a method keyword, CLASS can specify the class."
5928 (interactive "P")
5929 (idlwave-routines)
5930 (let* ((where-list
5931 (if (and arg
5932 (or (and (integerp arg) (not (equal arg '(16))))
5933 (symbolp arg)))
5934 (idlwave-make-force-complete-where-list arg module class)
5935 (idlwave-where)))
5936 (what (nth 2 where-list))
5937 (idlwave-force-class-query (equal arg '(4)))
5938 (completion-regexp-list
5939 (if (equal arg '(16))
5940 (list (read-string (concat "Completion Regexp: "))))))
5941
5942 (if (and module (string-match "::" module))
5943 (setq class (substring module 0 (match-beginning 0))
5944 module (substring module (match-end 0))))
5945
5946 (cond
5947
5948 ((and (null arg)
5949 (eq (car-safe last-command) 'idlwave-display-completion-list)
5950 (get-buffer-window "*Completions*"))
5951 (setq this-command last-command)
5952 (idlwave-scroll-completions))
5953
5954 ;; Complete a filename in quotes
5955 ((and (idlwave-in-quote)
5956 (not (eq what 'class)))
5957 (idlwave-complete-filename))
5958
5959 ;; Check for any special completion functions
5960 ((and idlwave-complete-special
5961 (idlwave-call-special idlwave-complete-special)))
5962
5963 ((null what)
5964 (error "Nothing to complete here"))
5965
5966 ;; Complete a class
5967 ((eq what 'class)
5968 (setq idlwave-completion-help-info '(class))
5969 (idlwave-complete-class))
5970
5971 ((eq what 'procedure)
5972 ;; Complete a procedure name
5973 (let* ((cw-list (nth 3 where-list))
5974 (class-selector (idlwave-determine-class cw-list 'pro))
5975 (super-classes (unless (idlwave-explicit-class-listed cw-list)
5976 (idlwave-all-class-inherits class-selector)))
5977 (isa (concat "procedure" (if class-selector "-method" "")))
5978 (type-selector 'pro))
5979 (setq idlwave-completion-help-info
5980 (list 'routine nil type-selector class-selector nil super-classes))
5981 (idlwave-complete-in-buffer
5982 'procedure (if class-selector 'method 'routine)
5983 (idlwave-routines) 'idlwave-selector
5984 (format "Select a %s name%s"
5985 isa
5986 (if class-selector
5987 (format " (class is %s)"
5988 (if (eq class-selector t)
5989 "unknown" class-selector))
5990 ""))
5991 isa
5992 'idlwave-attach-method-classes 'idlwave-add-file-link-selector)))
5993
5994 ((eq what 'function)
5995 ;; Complete a function name
5996 (let* ((cw-list (nth 3 where-list))
5997 (class-selector (idlwave-determine-class cw-list 'fun))
5998 (super-classes (unless (idlwave-explicit-class-listed cw-list)
5999 (idlwave-all-class-inherits class-selector)))
6000 (isa (concat "function" (if class-selector "-method" "")))
6001 (type-selector 'fun))
6002 (setq idlwave-completion-help-info
6003 (list 'routine nil type-selector class-selector nil super-classes))
6004 (idlwave-complete-in-buffer
6005 'function (if class-selector 'method 'routine)
6006 (idlwave-routines) 'idlwave-selector
6007 (format "Select a %s name%s"
6008 isa
6009 (if class-selector
6010 (format " (class is %s)"
6011 (if (eq class-selector t)
6012 "unknown" class-selector))
6013 ""))
6014 isa
6015 'idlwave-attach-method-classes 'idlwave-add-file-link-selector)))
6016
6017 ((and (memq what '(procedure-keyword function-keyword)) ; Special Case
6018 (equal arg '(4)))
6019 (idlwave-complete 3))
6020
6021 ((eq what 'procedure-keyword)
6022 ;; Complete a procedure keyword
6023 (let* ((where (nth 3 where-list))
6024 (name (car where))
6025 (method-selector name)
6026 (type-selector 'pro)
6027 (class (idlwave-determine-class where 'pro))
6028 (class-selector class)
6029 (super-classes (idlwave-all-class-inherits class-selector))
6030 (isa (format "procedure%s-keyword" (if class "-method" "")))
6031 (entry (idlwave-best-rinfo-assq
6032 name 'pro class (idlwave-routines)))
6033 (system (if entry (eq (car (nth 3 entry)) 'system)))
6034 (list (idlwave-entry-keywords entry 'do-link)))
6035 (unless (or entry (eq class t))
6036 (error "Nothing known about procedure %s"
6037 (idlwave-make-full-name class name)))
6038 (setq list (idlwave-fix-keywords name 'pro class list
6039 super-classes system))
6040 (unless list (error "No keywords available for procedure %s"
6041 (idlwave-make-full-name class name)))
6042 (setq idlwave-completion-help-info
6043 (list 'keyword name type-selector class-selector entry super-classes))
6044 (idlwave-complete-in-buffer
6045 'keyword 'keyword list nil
6046 (format "Select keyword for procedure %s%s"
6047 (idlwave-make-full-name class name)
6048 (if (or (member '("_EXTRA") list)
6049 (member '("_REF_EXTRA") list))
6050 " (note _EXTRA)" ""))
6051 isa
6052 'idlwave-attach-keyword-classes)))
6053
6054 ((eq what 'function-keyword)
6055 ;; Complete a function keyword
6056 (let* ((where (nth 3 where-list))
6057 (name (car where))
6058 (method-selector name)
6059 (type-selector 'fun)
6060 (class (idlwave-determine-class where 'fun))
6061 (class-selector class)
6062 (super-classes (idlwave-all-class-inherits class-selector))
6063 (isa (format "function%s-keyword" (if class "-method" "")))
6064 (entry (idlwave-best-rinfo-assq
6065 name 'fun class (idlwave-routines)))
6066 (system (if entry (eq (car (nth 3 entry)) 'system)))
6067 (list (idlwave-entry-keywords entry 'do-link))
6068 msg-name)
6069 (unless (or entry (eq class t))
6070 (error "Nothing known about function %s"
6071 (idlwave-make-full-name class name)))
6072 (setq list (idlwave-fix-keywords name 'fun class list
6073 super-classes system))
6074 ;; OBJ_NEW: Messages mention the proper Init method
6075 (setq msg-name (if (and (null class)
6076 (string= (upcase name) "OBJ_NEW"))
6077 (concat idlwave-current-obj_new-class
6078 "::Init (via OBJ_NEW)")
6079 (idlwave-make-full-name class name)))
6080 (unless list (error "No keywords available for function %s"
6081 msg-name))
6082 (setq idlwave-completion-help-info
6083 (list 'keyword name type-selector class-selector nil super-classes))
6084 (idlwave-complete-in-buffer
6085 'keyword 'keyword list nil
6086 (format "Select keyword for function %s%s" msg-name
6087 (if (or (member '("_EXTRA") list)
6088 (member '("_REF_EXTRA") list))
6089 " (note _EXTRA)" ""))
6090 isa
6091 'idlwave-attach-keyword-classes)))
6092
6093 (t (error "This should not happen (idlwave-complete)")))))
6094
6095 (defvar idlwave-complete-special nil
6096 "List of special completion functions.
6097 These functions are called for each completion. Each function must
6098 check if its own special completion context is present. If yes, it
6099 should use `idlwave-complete-in-buffer' to do some completion and
6100 return t. If such a function returns t, *no further* attempts to
6101 complete other contexts will be done. If the function returns nil,
6102 other completions will be tried.")
6103
6104 (defun idlwave-call-special (functions &rest args)
6105 (let ((funcs functions)
6106 fun ret)
6107 (catch 'exit
6108 (while (setq fun (pop funcs))
6109 (if (setq ret (apply fun args))
6110 (throw 'exit ret)))
6111 nil)))
6112
6113 (defun idlwave-make-force-complete-where-list (what &optional module class)
6114 ;; Return an artificial WHERE specification to force the completion
6115 ;; routine to complete a specific item independent of context.
6116 ;; WHAT is the prefix arg of `idlwave-complete', see there for details.
6117 ;; MODULE and CLASS can be used to specify the routine name and class.
6118 ;; The class name will also be found in MODULE if that is like "class::mod".
6119 (let* ((what-list '(("procedure") ("procedure-keyword")
6120 ("function") ("function-keyword")
6121 ("procedure-method") ("procedure-method-keyword")
6122 ("function-method") ("function-method-keyword")
6123 ("class")))
6124 (module (idlwave-sintern-routine-or-method module class))
6125 (class (idlwave-sintern-class class))
6126 (what (cond
6127 ((equal what 0)
6128 (setq what
6129 (intern (completing-read
6130 "Complete what? " what-list nil t))))
6131 ((integerp what)
6132 (setq what (intern (car (nth (1- what) what-list)))))
6133 ((and what
6134 (symbolp what)
6135 (assoc (symbol-name what) what-list))
6136 what)
6137 (t (error "Invalid WHAT"))))
6138 (nil-list '(nil nil nil nil))
6139 (class-list (list nil nil (or class t) nil)))
6140
6141 (cond
6142
6143 ((eq what 'procedure)
6144 (list nil-list nil-list 'procedure nil-list nil))
6145
6146 ((eq what 'procedure-keyword)
6147 (let* ((class-selector nil)
6148 (super-classes nil)
6149 (type-selector 'pro)
6150 (pro (or module
6151 (idlwave-completing-read
6152 "Procedure: " (idlwave-routines) 'idlwave-selector))))
6153 (setq pro (idlwave-sintern-routine pro))
6154 (list nil-list nil-list 'procedure-keyword
6155 (list pro nil nil nil) nil)))
6156
6157 ((eq what 'function)
6158 (list nil-list nil-list 'function nil-list nil))
6159
6160 ((eq what 'function-keyword)
6161 (let* ((class-selector nil)
6162 (super-classes nil)
6163 (type-selector 'fun)
6164 (func (or module
6165 (idlwave-completing-read
6166 "Function: " (idlwave-routines) 'idlwave-selector))))
6167 (setq func (idlwave-sintern-routine func))
6168 (list nil-list nil-list 'function-keyword
6169 (list func nil nil nil) nil)))
6170
6171 ((eq what 'procedure-method)
6172 (list nil-list nil-list 'procedure class-list nil))
6173
6174 ((eq what 'procedure-method-keyword)
6175 (let* ((class (idlwave-determine-class class-list 'pro))
6176 (class-selector class)
6177 (super-classes (idlwave-all-class-inherits class-selector))
6178 (type-selector 'pro)
6179 (pro (or module
6180 (idlwave-completing-read
6181 (format "Procedure in %s class: " class-selector)
6182 (idlwave-routines) 'idlwave-selector))))
6183 (setq pro (idlwave-sintern-method pro))
6184 (list nil-list nil-list 'procedure-keyword
6185 (list pro nil class nil) nil)))
6186
6187 ((eq what 'function-method)
6188 (list nil-list nil-list 'function class-list nil))
6189
6190 ((eq what 'function-method-keyword)
6191 (let* ((class (idlwave-determine-class class-list 'fun))
6192 (class-selector class)
6193 (super-classes (idlwave-all-class-inherits class-selector))
6194 (type-selector 'fun)
6195 (func (or module
6196 (idlwave-completing-read
6197 (format "Function in %s class: " class-selector)
6198 (idlwave-routines) 'idlwave-selector))))
6199 (setq func (idlwave-sintern-method func))
6200 (list nil-list nil-list 'function-keyword
6201 (list func nil class nil) nil)))
6202
6203 ((eq what 'class)
6204 (list nil-list nil-list 'class nil-list nil))
6205
6206 (t (error "Invalid value for WHAT")))))
6207
6208 (defun idlwave-completing-read (&rest args)
6209 ;; Completing read, case insensitive
6210 (let ((old-value (default-value 'completion-ignore-case)))
6211 (unwind-protect
6212 (progn
6213 (setq-default completion-ignore-case t)
6214 (apply 'completing-read args))
6215 (setq-default completion-ignore-case old-value))))
6216
6217 (defvar idlwave-shell-default-directory)
6218 (defun idlwave-complete-filename ()
6219 "Use the comint stuff to complete a file name."
6220 (require 'comint)
6221 (let* ((comint-file-name-chars "~/A-Za-z0-9+@:_.$#%={}\\-")
6222 (comint-completion-addsuffix nil)
6223 (default-directory
6224 (if (and (boundp 'idlwave-shell-default-directory)
6225 (stringp idlwave-shell-default-directory)
6226 (file-directory-p idlwave-shell-default-directory))
6227 idlwave-shell-default-directory
6228 default-directory)))
6229 (comint-dynamic-complete-filename)))
6230
6231 (defun idlwave-make-full-name (class name)
6232 ;; Make a fully qualified module name including the class name
6233 (concat (if class (format "%s::" class) "") name))
6234
6235 (defun idlwave-rinfo-assoc (name type class list)
6236 "Like `idlwave-rinfo-assq', but sintern strings first."
6237 (idlwave-rinfo-assq
6238 (idlwave-sintern-routine-or-method name class)
6239 type (idlwave-sintern-class class) list))
6240
6241 (defun idlwave-rinfo-assq (name type class list)
6242 ;; Works like assq, but also checks type and class
6243 (catch 'exit
6244 (let (match)
6245 (while (setq match (assq name list))
6246 (and (or (eq type t)
6247 (eq (nth 1 match) type))
6248 (eq (nth 2 match) class)
6249 (throw 'exit match))
6250 (setq list (cdr (memq match list)))))))
6251
6252 (defun idlwave-rinfo-assq-any-class (name type class list)
6253 ;; Return the first matching method on the inheritance list
6254 (let* ((classes (cons class (idlwave-all-class-inherits class)))
6255 class rtn)
6256 (while classes
6257 (if (setq rtn (idlwave-rinfo-assq name type (pop classes) list))
6258 (setq classes nil)))
6259 rtn))
6260
6261 (defun idlwave-best-rinfo-assq (name type class list &optional with-file
6262 keep-system)
6263 "Like `idlwave-rinfo-assq', but get all twins and sort, then return first.
6264 If WITH-FILE is passed, find the best rinfo entry with a file
6265 included. If KEEP-SYSTEM is set, don't prune system for compiled
6266 syslib files."
6267 (let ((twins (idlwave-routine-twins
6268 (idlwave-rinfo-assq-any-class name type class list)
6269 list))
6270 syslibp)
6271 (when (> (length twins) 1)
6272 (setq twins (sort twins 'idlwave-routine-entry-compare-twins))
6273 (if (and (null keep-system)
6274 (eq 'system (car (nth 3 (car twins))))
6275 (setq syslibp (idlwave-any-syslib (cdr twins)))
6276 (not (equal 1 syslibp)))
6277 ;; Its a compiled syslib, so we need to remove the system entry
6278 (setq twins (cdr twins)))
6279 (if with-file
6280 (setq twins (delq nil
6281 (mapcar (lambda (x)
6282 (if (nth 1 (nth 3 x)) x))
6283 twins)))))
6284 (car twins)))
6285
6286 (defun idlwave-best-rinfo-assoc (name type class list &optional with-file
6287 keep-system)
6288 "Like `idlwave-best-rinfo-assq', but sintern strings first."
6289 (idlwave-best-rinfo-assq
6290 (idlwave-sintern-routine-or-method name class)
6291 type (idlwave-sintern-class class) list with-file keep-system))
6292
6293 (defun idlwave-any-syslib (entries)
6294 "Does the entry list ENTRIES contain a syslib entry?
6295 If yes, return the index (>=1)."
6296 (let (file (cnt 0))
6297 (catch 'exit
6298 (while entries
6299 (incf cnt)
6300 (setq file (idlwave-routine-source-file (nth 3 (car entries))))
6301 (if (and file (idlwave-syslib-p file))
6302 (throw 'exit cnt)
6303 (setq entries (cdr entries))))
6304 nil)))
6305
6306 (defun idlwave-all-assq (key list)
6307 "Return a list of all associations of Key in LIST."
6308 (let (rtn elt)
6309 (while (setq elt (assq key list))
6310 (push elt rtn)
6311 (setq list (cdr (memq elt list))))
6312 (nreverse rtn)))
6313
6314 (defun idlwave-all-method-classes (method &optional type)
6315 "Return all classes which have a method METHOD. TYPE is 'fun or 'pro.
6316 When TYPE is not specified, both procedures and functions will be considered."
6317 (if (null method)
6318 (mapcar 'car (idlwave-class-alist))
6319 (let (rtn)
6320 (mapcar (lambda (x)
6321 (and (nth 2 x)
6322 (or (not type)
6323 (eq type (nth 1 x)))
6324 (push (nth 2 x) rtn)))
6325 (idlwave-all-assq method (idlwave-routines)))
6326 (idlwave-uniquify rtn))))
6327
6328 (defun idlwave-all-method-keyword-classes (method keyword &optional type)
6329 "Return all classes which have a method METHOD with keyword KEYWORD.
6330 TYPE is 'fun or 'pro.
6331 When TYPE is not specified, both procedures and functions will be considered."
6332 (if (or (null method)
6333 (null keyword))
6334 nil
6335 (let (rtn)
6336 (mapcar (lambda (x)
6337 (and (nth 2 x) ; non-nil class
6338 (or (not type) ; correct or unspecified type
6339 (eq type (nth 1 x)))
6340 (assoc keyword (idlwave-entry-keywords x))
6341 (push (nth 2 x) rtn)))
6342 (idlwave-all-assq method (idlwave-routines)))
6343 (idlwave-uniquify rtn))))
6344
6345 (defun idlwave-members-only (list club)
6346 "Return list of all elements in LIST which are also in CLUB."
6347 (let (rtn)
6348 (while list
6349 (if (member (car list) club)
6350 (setq rtn (cons (car list) rtn)))
6351 (setq list (cdr list)))
6352 (nreverse rtn)))
6353
6354 (defun idlwave-nonmembers-only (list club)
6355 "Return list of all elements in LIST which are not in CLUB."
6356 (let (rtn)
6357 (while list
6358 (if (member (car list) club)
6359 nil
6360 (setq rtn (cons (car list) rtn)))
6361 (setq list (cdr list)))
6362 (nreverse rtn)))
6363
6364 (defun idlwave-explicit-class-listed (info)
6365 "Return whether or not the class is listed explicitly, ala a->b::c.
6366 INFO is as returned by idlwave-what-function or -procedure."
6367 (let ((apos (nth 3 info)))
6368 (if apos
6369 (save-excursion (goto-char apos)
6370 (looking-at "->[a-zA-Z][a-zA-Z0-9$_]*::")))))
6371
6372 (defvar idlwave-determine-class-special nil
6373 "List of special functions for determining class.
6374 Must accept two arguments: `apos' and `info'")
6375
6376 (defun idlwave-determine-class (info type)
6377 ;; Determine the class of a routine call.
6378 ;; INFO is the `cw-list' structure as returned by idlwave-where.
6379 ;; The second element in this structure is the class. When nil, we
6380 ;; return nil. When t, try to get the class from text properties at
6381 ;; the arrow. When the object is "self", we use the class of the
6382 ;; current routine. otherwise prompt the user for a class name.
6383 ;; Also stores the selected class as a text property at the arrow.
6384 ;; TYPE is 'fun or 'pro.
6385 (let* ((class (nth 2 info))
6386 (apos (nth 3 info))
6387 (nassoc (assoc (if (stringp (car info))
6388 (upcase (car info))
6389 (car info))
6390 idlwave-query-class))
6391 (dassoc (assq (if (car info) 'keyword-default 'method-default)
6392 idlwave-query-class))
6393 (query (cond (nassoc (cdr nassoc))
6394 (dassoc (cdr dassoc))
6395 (t t)))
6396 (arrow (and apos (string= (buffer-substring apos (+ 2 apos)) "->")))
6397 (is-self
6398 (and arrow
6399 (save-excursion (goto-char apos)
6400 (forward-word -1)
6401 (let ((case-fold-search t))
6402 (looking-at "self\\>")))))
6403 (force-query idlwave-force-class-query)
6404 store special-class class-alist)
6405 (cond
6406 ((null class) nil)
6407 ((eq t class)
6408 ;; There is an object which would like to know its class
6409 (if (and arrow (get-text-property apos 'idlwave-class)
6410 idlwave-store-inquired-class
6411 (not force-query))
6412 (setq class (get-text-property apos 'idlwave-class)
6413 class (idlwave-sintern-class class)))
6414 (if (and (eq t class) is-self)
6415 (setq class (or (nth 2 (idlwave-current-routine)) class)))
6416
6417 ;; Before prompting, try any special class determination routines
6418 (when (and (eq t class)
6419 idlwave-determine-class-special
6420 (not force-query))
6421 (setq special-class
6422 (idlwave-call-special idlwave-determine-class-special apos))
6423 (if special-class
6424 (setq class (idlwave-sintern-class special-class)
6425 store idlwave-store-inquired-class)))
6426
6427 ;; Prompt for a class, if we need to
6428 (when (and (eq class t)
6429 (or force-query query))
6430 (setq class-alist
6431 (mapcar 'list (idlwave-all-method-classes (car info) type)))
6432 (setq class
6433 (idlwave-sintern-class
6434 (cond
6435 ((and (= (length class-alist) 0) (not force-query))
6436 (error "No classes available with method %s" (car info)))
6437 ((and (= (length class-alist) 1) (not force-query))
6438 (car (car class-alist)))
6439 (t
6440 (setq store idlwave-store-inquired-class)
6441 (idlwave-completing-read
6442 (format "Class%s: " (if (stringp (car info))
6443 (format " for %s method %s"
6444 type (car info))
6445 ""))
6446 class-alist nil nil nil 'idlwave-class-history))))))
6447
6448 ;; Store it, if requested
6449 (when (and class (not (eq t class)))
6450 ;; We have a real class here
6451 (when (and store arrow)
6452 (condition-case ()
6453 (add-text-properties
6454 apos (+ apos 2)
6455 `(idlwave-class ,class face ,idlwave-class-arrow-face
6456 rear-nonsticky t))
6457 (error nil)))
6458 (setf (nth 2 info) class))
6459 ;; Return the class
6460 class)
6461 ;; Default as fallback
6462 (t class))))
6463
6464 (defvar type-selector)
6465 (defvar class-selector)
6466 (defvar method-selector)
6467 (defvar super-classes)
6468 (defun idlwave-selector (a)
6469 (and (eq (nth 1 a) type-selector)
6470 (or (and (nth 2 a) (eq class-selector t))
6471 (eq (nth 2 a) class-selector)
6472 (memq (nth 2 a) super-classes))))
6473
6474 (defun idlwave-add-file-link-selector (a)
6475 ;; Record a file link, if any, for the tested names during selection.
6476 (let ((sel (idlwave-selector a)) file)
6477 (if (and sel (setq file (idlwave-entry-has-help a)))
6478 (push (cons (car a) file) idlwave-completion-help-links))
6479 sel))
6480
6481
6482 (defun idlwave-where ()
6483 "Find out where we are.
6484 The return value is a list with the following stuff:
6485 \(PRO-LIST FUNC-LIST COMPLETE-WHAT CW-LIST LAST-CHAR)
6486
6487 PRO-LIST (PRO POINT CLASS ARROW)
6488 FUNC-LIST (FUNC POINT CLASS ARROW)
6489 COMPLETE-WHAT a symbol indicating what kind of completion makes sense here
6490 CW-LIST (PRO-OR-FUNC POINT CLASS ARROW) Like PRO-LIST, for what can
6491 be completed here.
6492 LAST-CHAR last relevant character before point (non-white non-comment,
6493 not part of current identifier or leading slash).
6494
6495 In the lists, we have these meanings:
6496 PRO: Procedure name
6497 FUNC: Function name
6498 POINT: Where is this
6499 CLASS: What class has the routine (nil=no, t=is method, but class unknown)
6500 ARROW: Location of the arrow"
6501 (idlwave-routines)
6502 (let* (;(bos (save-excursion (idlwave-beginning-of-statement) (point)))
6503 (bos (save-excursion (idlwave-start-of-substatement 'pre) (point)))
6504 (func-entry (idlwave-what-function bos))
6505 (func (car func-entry))
6506 (func-class (nth 1 func-entry))
6507 (func-arrow (nth 2 func-entry))
6508 (func-point (or (nth 3 func-entry) 0))
6509 (func-level (or (nth 4 func-entry) 0))
6510 (pro-entry (idlwave-what-procedure bos))
6511 (pro (car pro-entry))
6512 (pro-class (nth 1 pro-entry))
6513 (pro-arrow (nth 2 pro-entry))
6514 (pro-point (or (nth 3 pro-entry) 0))
6515 (last-char (idlwave-last-valid-char))
6516 (case-fold-search t)
6517 (match-string (buffer-substring bos (point)))
6518 cw cw-mod cw-arrow cw-class cw-point)
6519 (if (< func-point pro-point) (setq func nil))
6520 (cond
6521 ((string-match "\\`[ \t]*\\(pro\\|function\\)[ \t]+[a-zA-Z0-9_]*\\'"
6522 match-string)
6523 (setq cw 'class))
6524 ((string-match
6525 "\\`[ \t]*\\([a-zA-Z][a-zA-Z0-9$_]*\\)?\\'"
6526 (if (> pro-point 0)
6527 (buffer-substring pro-point (point))
6528 match-string))
6529 (setq cw 'procedure cw-class pro-class cw-point pro-point
6530 cw-arrow pro-arrow))
6531 ((string-match "\\`[ \t]*\\(pro\\|function\\)\\>"
6532 match-string)
6533 nil)
6534 ((string-match "OBJ_NEW([ \t]*['\"]\\([a-zA-Z0-9$_]*\\)?\\'"
6535 match-string)
6536 (setq cw 'class))
6537 ((string-match "\\<inherits\\s-+\\([a-zA-Z0-9$_]*\\)?\\'"
6538 match-string)
6539 (setq cw 'class))
6540 ((and func
6541 (> func-point pro-point)
6542 (= func-level 1)
6543 (memq last-char '(?\( ?,)))
6544 (setq cw 'function-keyword cw-mod func cw-point func-point
6545 cw-class func-class cw-arrow func-arrow))
6546 ((and pro (eq last-char ?,))
6547 (setq cw 'procedure-keyword cw-mod pro cw-point pro-point
6548 cw-class pro-class cw-arrow pro-arrow))
6549 ; ((member last-char '(?\' ?\) ?\] ?!))
6550 ; ;; after these chars, a function makes no sense
6551 ; ;; FIXME: I am sure there can be more in this list
6552 ; ;; FIXME: Do we want to do this at all?
6553 ; nil)
6554 ;; Everywhere else we try a function.
6555 (t
6556 (setq cw 'function)
6557 (save-excursion
6558 (if (re-search-backward "->[ \t]*\\(\\$[ \t]*\\(;.*\\)?\n\\s-*\\)?\\(\\([$a-zA-Z0-9_]+\\)::\\)?[$a-zA-Z0-9_]*\\=" bos t)
6559 (setq cw-arrow (copy-marker (match-beginning 0))
6560 cw-class (if (match-end 4)
6561 (idlwave-sintern-class (match-string 4))
6562 t))))))
6563 (list (list pro pro-point pro-class pro-arrow)
6564 (list func func-point func-class func-arrow)
6565 cw
6566 (list cw-mod cw-point cw-class cw-arrow)
6567 last-char)))
6568
6569 (defun idlwave-this-word (&optional class)
6570 ;; Grab the word around point. CLASS is for the `skip-chars=...' functions
6571 (setq class (or class "a-zA-Z0-9$_."))
6572 (save-excursion
6573 (buffer-substring
6574 (progn (skip-chars-backward class) (point))
6575 (progn (skip-chars-forward class) (point)))))
6576
6577 (defun idlwave-what-function (&optional bound)
6578 ;; Find out if point is within the argument list of a function.
6579 ;; The return value is ("function-name" class arrow-start (point) level).
6580 ;; Level is 1 on the top level parentheses, higher further down.
6581
6582 ;; If the optional BOUND is an integer, bound backwards directed
6583 ;; searches to this point.
6584
6585 (catch 'exit
6586 (let (pos
6587 func-point
6588 (cnt 0)
6589 func arrow-start class)
6590 (idlwave-with-special-syntax
6591 (save-restriction
6592 (save-excursion
6593 (narrow-to-region (max 1 (or bound 0)) (point-max))
6594 ;; move back out of the current parenthesis
6595 (while (condition-case nil
6596 (progn (up-list -1) t)
6597 (error nil))
6598 (setq pos (point))
6599 (incf cnt)
6600 (when (and (= (following-char) ?\()
6601 (re-search-backward
6602 "\\(::\\|\\<\\)\\([a-zA-Z][a-zA-Z0-9$_]*\\)[ \t]*\\="
6603 bound t))
6604 (setq func (match-string 2)
6605 func-point (goto-char (match-beginning 2))
6606 pos func-point)
6607 (if (re-search-backward
6608 "->[ \t]*\\(\\([a-zA-Z][a-zA-Z0-9$_]*\\)::\\)?\\=" bound t)
6609 (setq arrow-start (copy-marker (match-beginning 0))
6610 class (or (match-string 2) t)))
6611 (throw
6612 'exit
6613 (list
6614 (idlwave-sintern-routine-or-method func class)
6615 (idlwave-sintern-class class)
6616 arrow-start func-point cnt)))
6617 (goto-char pos))
6618 (throw 'exit nil)))))))
6619
6620 (defun idlwave-what-procedure (&optional bound)
6621 ;; Find out if point is within the argument list of a procedure.
6622 ;; The return value is ("procedure-name" class arrow-pos (point)).
6623
6624 ;; If the optional BOUND is an integer, bound backwards directed
6625 ;; searches to this point.
6626 (let ((pos (point)) pro-point
6627 pro class arrow-start string)
6628 (save-excursion
6629 ;;(idlwave-beginning-of-statement)
6630 (idlwave-start-of-substatement 'pre)
6631 (setq string (buffer-substring (point) pos))
6632 (if (string-match
6633 "\\`[ \t]*\\([a-zA-Z][a-zA-Z0-9$_]*\\)[ \t]*\\(,\\|\\'\\)" string)
6634 (setq pro (match-string 1 string)
6635 pro-point (+ (point) (match-beginning 1)))
6636 (if (and (idlwave-skip-object)
6637 (setq string (buffer-substring (point) pos))
6638 (string-match
6639 "\\`[ \t]*\\(->\\)[ \t]*\\(\\([a-zA-Z][a-zA-Z0-9$_]*\\)::\\)?\\([a-zA-Z][a-zA-Z0-9$_]*\\)?[ \t]*\\(,\\|\\(\\$\\s *\\(;.*\\)?\\)?$\\)"
6640 string))
6641 (setq pro (if (match-beginning 4)
6642 (match-string 4 string))
6643 pro-point (if (match-beginning 4)
6644 (+ (point) (match-beginning 4))
6645 pos)
6646 arrow-start (copy-marker (+ (point) (match-beginning 1)))
6647 class (or (match-string 3 string) t)))))
6648 (list (idlwave-sintern-routine-or-method pro class)
6649 (idlwave-sintern-class class)
6650 arrow-start
6651 pro-point)))
6652
6653 (defun idlwave-skip-object ()
6654 ;; If there is an object at point, move over it and return t.
6655 (let ((pos (point)))
6656 (if (catch 'exit
6657 (save-excursion
6658 (skip-chars-forward " ") ; white space
6659 (skip-chars-forward "*") ; de-reference
6660 (cond
6661 ((looking-at idlwave-identifier)
6662 (goto-char (match-end 0)))
6663 ((eq (following-char) ?\()
6664 nil)
6665 (t (throw 'exit nil)))
6666 (catch 'endwhile
6667 (while t
6668 (cond ((eq (following-char) ?.)
6669 (forward-char 1)
6670 (if (not (looking-at idlwave-identifier))
6671 (throw 'exit nil))
6672 (goto-char (match-end 0)))
6673 ((memq (following-char) '(?\( ?\[))
6674 (condition-case nil
6675 (forward-list 1)
6676 (error (throw 'exit nil))))
6677 (t (throw 'endwhile t)))))
6678 (if (looking-at "[ \t]*->")
6679 (throw 'exit (setq pos (match-beginning 0)))
6680 (throw 'exit nil))))
6681 (goto-char pos)
6682 nil)))
6683
6684 (defun idlwave-last-valid-char ()
6685 "Return the last character before point which is not white or a comment
6686 and also not part of the current identifier. Since we do this in
6687 order to identify places where keywords are, we consider the initial
6688 `/' of a keyword as part of the identifier.
6689 This function is not general, can only be used for completion stuff."
6690 (catch 'exit
6691 (save-excursion
6692 ;; skip the current identifier
6693 (skip-chars-backward "a-zA-Z0-9_$")
6694 ;; also skip a leading slash which might be belong to the keyword
6695 (if (eq (preceding-char) ?/)
6696 (backward-char 1))
6697 ;; FIXME: does not check if this is a valid identifier
6698 (while t
6699 (skip-chars-backward " \t")
6700 (cond
6701 ((memq (preceding-char) '(?\; ?\$)) (throw 'exit nil))
6702 ((eq (preceding-char) ?\n)
6703 (beginning-of-line 0)
6704 (if (looking-at "\\([^\n]*\\)\\$[ \t]*\\(;[^\n]*\\)?\n")
6705 ;; continuation line
6706 (goto-char (match-end 1))
6707 (throw 'exit nil)))
6708 (t (throw 'exit (preceding-char))))))))
6709
6710 (defvar idlwave-complete-after-success-form nil
6711 "A form to evaluate after successful completion.")
6712 (defvar idlwave-complete-after-success-form-force nil
6713 "A form to evaluate after completion selection in *Completions* buffer.")
6714 (defconst idlwave-completion-mark (make-marker)
6715 "A mark pointing to the beginning of the completion string.")
6716
6717 (defun idlwave-complete-in-buffer (type stype list selector prompt isa
6718 &optional prepare-display-function
6719 special-selector)
6720 "Perform TYPE completion of word before point against LIST.
6721 SELECTOR is the PREDICATE argument for the completion function. Show
6722 PROMPT in echo area. TYPE is one of the intern types, e.g. 'function,
6723 'procedure, 'class-tag, 'keyword, 'sysvar, etc.. SPECIAL-SELECTOR is
6724 used only once, for `all-completions', and can be used to, e.g.,
6725 accumulate information on matching completions."
6726 (let* ((completion-ignore-case t)
6727 beg (end (point)) slash part spart completion all-completions
6728 dpart dcompletion)
6729
6730 (unless list
6731 (error (concat prompt ": No completions available")))
6732
6733 ;; What is already in the buffer?
6734 (save-excursion
6735 (skip-chars-backward "a-zA-Z0-9_$")
6736 (setq slash (eq (preceding-char) ?/)
6737 beg (point)
6738 idlwave-complete-after-success-form
6739 (list 'idlwave-after-successful-completion
6740 (list 'quote type) slash beg)
6741 idlwave-complete-after-success-form-force
6742 (list 'idlwave-after-successful-completion
6743 (list 'quote type) slash (list 'quote 'force))))
6744
6745 ;; Try a completion
6746 (setq part (buffer-substring beg end)
6747 dpart (downcase part)
6748 spart (idlwave-sintern stype part)
6749 completion (try-completion part list selector)
6750 dcompletion (if (stringp completion) (downcase completion))
6751 idlwave-completion-help-links nil)
6752 (cond
6753 ((null completion)
6754 ;; nothing available.
6755 (error (concat prompt ": no completion for \"%s\"") part))
6756 ((and (not (equal dpart dcompletion))
6757 (not (eq t completion)))
6758 ;; We can add something
6759 (delete-region beg end)
6760 (if (and (string= part dpart)
6761 (or (not (string= part ""))
6762 idlwave-complete-empty-string-as-lower-case)
6763 (not idlwave-completion-force-default-case))
6764 (insert dcompletion)
6765 (insert completion))
6766 (if (eq t (try-completion completion list selector))
6767 ;; Now this is a unique match
6768 (idlwave-after-successful-completion type slash beg))
6769 t)
6770 ((or (eq completion t)
6771 (and (= 1 (length (setq all-completions
6772 (idlwave-uniquify
6773 (all-completions part list
6774 (or special-selector
6775 selector))))))
6776 (equal dpart dcompletion)))
6777 ;; This is already complete
6778 (idlwave-after-successful-completion type slash beg)
6779 (message "%s is already the complete %s" part isa)
6780 nil)
6781 (t
6782 ;; We cannot add something - offer a list.
6783 (message "Making completion list...")
6784
6785 (unless idlwave-completion-help-links ; already set somewhere?
6786 (mapcar (lambda (x) ; Pass link prop through to highlight-linked
6787 (let ((link (get-text-property 0 'link (car x))))
6788 (if link
6789 (push (cons (car x) link)
6790 idlwave-completion-help-links))))
6791 list))
6792 (let* ((list all-completions)
6793 ;; "complete" means, this is already a valid completion
6794 (complete (memq spart all-completions))
6795 (completion-highlight-first-word-only t)) ; XEmacs
6796 ; (completion-fixup-function ; Emacs
6797 ; (lambda () (and (eq (preceding-char) ?>)
6798 ; (re-search-backward " <" beg t)))))
6799
6800 (setq list (sort list (lambda (a b)
6801 (string< (downcase a) (downcase b)))))
6802 (if prepare-display-function
6803 (setq list (funcall prepare-display-function list)))
6804 (if (and (string= part dpart)
6805 (or (not (string= part ""))
6806 idlwave-complete-empty-string-as-lower-case)
6807 (not idlwave-completion-force-default-case))
6808 (setq list (mapcar (lambda (x)
6809 (if (listp x)
6810 (setcar x (downcase (car x)))
6811 (setq x (downcase x)))
6812 x)
6813 list)))
6814 (idlwave-display-completion-list list prompt beg complete))
6815 t))))
6816
6817 (defun idlwave-complete-class ()
6818 "Complete a class at point."
6819 (interactive)
6820 ;; Call `idlwave-routines' to make sure the class list will be available
6821 (idlwave-routines)
6822 ;; Check for the special case of completing empty string after pro/function
6823 (if (let ((case-fold-search t))
6824 (save-excursion
6825 (and
6826 (re-search-backward "\\<\\(pro\\|function\\)[ \t]+\\="
6827 (- (point) 15) t)
6828 (goto-char (point-min))
6829 (re-search-forward
6830 "^[ \t]*\\(pro\\|function\\)[ \t]+\\([a-zA-Z0-9_]+::\\)" nil t))))
6831 ;; Yank the full class specification
6832 (insert (match-string 2))
6833 ;; Do the completion, using list gathered from `idlwave-routines'
6834 (idlwave-complete-in-buffer
6835 'class 'class (idlwave-class-alist) nil
6836 "Select a class" "class"
6837 '(lambda (list) ;; Push it to help-links if system help available
6838 (mapcar (lambda (x)
6839 (let* ((entry (idlwave-class-info x))
6840 (link (nth 1 (assq 'link entry))))
6841 (if link (push (cons x link)
6842 idlwave-completion-help-links))
6843 x))
6844 list)))))
6845
6846 (defun idlwave-attach-classes (list type show-classes)
6847 ;; Attach the proper class list to a LIST of completion items.
6848 ;; TYPE, when 'kwd, shows classes for method keywords, when
6849 ;; 'class-tag, for class tags, and otherwise for methods.
6850 ;; SHOW-CLASSES is the value of `idlwave-completion-show-classes'.
6851 (if (or (null show-classes) ; don't want to see classes
6852 (null class-selector) ; not a method call
6853 (and
6854 (stringp class-selector) ; the class is already known
6855 (not super-classes))) ; no possibilities for inheritance
6856 ;; In these cases, we do not have to do anything
6857 list
6858 (let* ((do-prop (and (>= show-classes 0)
6859 (>= emacs-major-version 21)))
6860 (do-buf (not (= show-classes 0)))
6861 ;; (do-dots (featurep 'xemacs))
6862 (do-dots t)
6863 (inherit (if (and (not (eq type 'class-tag)) super-classes)
6864 (cons class-selector super-classes)))
6865 (max (abs show-classes))
6866 (lmax (if do-dots (apply 'max (mapcar 'length list))))
6867 classes nclasses class-info space)
6868 (mapcar
6869 (lambda (x)
6870 ;; get the classes
6871 (if (eq type 'class-tag)
6872 ;; Just one class for tags
6873 (setq classes
6874 (list
6875 (idlwave-class-or-superclass-with-tag class-selector x)))
6876 ;; Multiple classes for method or method-keyword
6877 (setq classes
6878 (if (eq type 'kwd)
6879 (idlwave-all-method-keyword-classes
6880 method-selector x type-selector)
6881 (idlwave-all-method-classes x type-selector)))
6882 (if inherit
6883 (setq classes
6884 (delq nil
6885 (mapcar (lambda (x) (if (memq x inherit) x nil))
6886 classes)))))
6887 (setq nclasses (length classes))
6888 ;; Make the separator between item and class-info
6889 (if do-dots
6890 (setq space (concat " " (make-string (- lmax (length x)) ?.)))
6891 (setq space " "))
6892 (if do-buf
6893 ;; We do want info in the buffer
6894 (if (<= nclasses max)
6895 (setq class-info (concat
6896 space
6897 "<" (mapconcat 'identity classes ",") ">"))
6898 (setq class-info (format "%s<%d classes>" space nclasses)))
6899 (setq class-info nil))
6900 (when do-prop
6901 ;; We do want properties
6902 (setq x (copy-sequence x))
6903 (put-text-property 0 (length x)
6904 'help-echo (mapconcat 'identity classes " ")
6905 x))
6906 (if class-info
6907 (list x class-info)
6908 x))
6909 list))))
6910
6911 (defun idlwave-attach-method-classes (list)
6912 ;; Call idlwave-attach-classes with method parameters
6913 (idlwave-attach-classes list 'method idlwave-completion-show-classes))
6914 (defun idlwave-attach-keyword-classes (list)
6915 ;; Call idlwave-attach-classes with keyword parameters
6916 (idlwave-attach-classes list 'kwd idlwave-completion-show-classes))
6917 (defun idlwave-attach-class-tag-classes (list)
6918 ;; Call idlwave-attach-classes with class structure tags
6919 (idlwave-attach-classes list 'class-tag idlwave-completion-show-classes))
6920
6921
6922 ;;----------------------------------------------------------------------
6923 ;;----------------------------------------------------------------------
6924 ;;----------------------------------------------------------------------
6925 ;;----------------------------------------------------------------------
6926 ;;----------------------------------------------------------------------
6927 (defvar rtn)
6928 (defun idlwave-pset (item)
6929 (set 'rtn item))
6930
6931 (defun idlwave-popup-select (ev list title &optional sort)
6932 "Select an item in LIST with a popup menu.
6933 TITLE is the title to put atop the popup. If SORT is non-nil,
6934 sort the list before displaying"
6935 (let ((maxpopup idlwave-max-popup-menu-items)
6936 rtn menu resp)
6937 (cond ((null list))
6938 ((= 1 (length list))
6939 (setq rtn (car list)))
6940 ((featurep 'xemacs)
6941 (if sort (setq list (sort list (lambda (a b)
6942 (string< (upcase a) (upcase b))))))
6943 (setq menu
6944 (append (list title)
6945 (mapcar (lambda (x) (vector x (list 'idlwave-pset
6946 x)))
6947 list)))
6948 (setq menu (idlwave-split-menu-xemacs menu maxpopup))
6949 (setq resp (get-popup-menu-response menu))
6950 (funcall (event-function resp) (event-object resp)))
6951 (t
6952 (if sort (setq list (sort list (lambda (a b)
6953 (string< (upcase a) (upcase b))))))
6954 (setq menu (cons title
6955 (list
6956 (append (list "")
6957 (mapcar (lambda(x) (cons x x)) list)))))
6958 (setq menu (idlwave-split-menu-emacs menu maxpopup))
6959 (setq rtn (x-popup-menu ev menu))))
6960 rtn))
6961
6962 (defun idlwave-split-menu-xemacs (menu N)
6963 "Split the MENU into submenus of maximum length N."
6964 (if (<= (length menu) (1+ N))
6965 ;; No splitting needed
6966 menu
6967 (let* ((title (car menu))
6968 (entries (cdr menu))
6969 (menu (list title))
6970 (cnt 0)
6971 (nextmenu nil))
6972 (while entries
6973 (while (and entries (< cnt N))
6974 (setq cnt (1+ cnt)
6975 nextmenu (cons (car entries) nextmenu)
6976 entries (cdr entries)))
6977 (setq nextmenu (nreverse nextmenu))
6978 (setq nextmenu (cons (format "%s...%s"
6979 (aref (car nextmenu) 0)
6980 (aref (nth (1- cnt) nextmenu) 0))
6981 nextmenu))
6982 (setq menu (cons nextmenu menu)
6983 nextmenu nil
6984 cnt 0))
6985 (nreverse menu))))
6986
6987 (defun idlwave-split-menu-emacs (menu N)
6988 "Split the MENU into submenus of maximum length N."
6989 (if (<= (length (nth 1 menu)) (1+ N))
6990 ;; No splitting needed
6991 menu
6992 (let* ((title (car menu))
6993 (entries (cdr (nth 1 menu)))
6994 (menu nil)
6995 (cnt 0)
6996 (nextmenu nil))
6997 (while entries
6998 (while (and entries (< cnt N))
6999 (setq cnt (1+ cnt)
7000 nextmenu (cons (car entries) nextmenu)
7001 entries (cdr entries)))
7002 (setq nextmenu (nreverse nextmenu))
7003 (prin1 nextmenu)
7004 (setq nextmenu (cons (format "%s...%s"
7005 (car (car nextmenu))
7006 (car (nth (1- cnt) nextmenu)))
7007 nextmenu))
7008 (setq menu (cons nextmenu menu)
7009 nextmenu nil
7010 cnt 0))
7011 (setq menu (nreverse menu))
7012 (setq menu (cons title menu))
7013 menu)))
7014
7015 (defvar idlwave-completion-setup-hook nil)
7016
7017 (defun idlwave-scroll-completions (&optional message)
7018 "Scroll the completion window on this frame."
7019 (let ((cwin (get-buffer-window "*Completions*" 'visible))
7020 (win (selected-window)))
7021 (unwind-protect
7022 (progn
7023 (select-window cwin)
7024 (condition-case nil
7025 (scroll-up)
7026 (error (if (and (listp last-command)
7027 (nth 2 last-command))
7028 (progn
7029 (select-window win)
7030 (eval idlwave-complete-after-success-form))
7031 (set-window-start cwin (point-min)))))
7032 (and message (message message)))
7033 (select-window win))))
7034
7035 (defun idlwave-display-completion-list (list &optional message beg complete)
7036 "Display the completions in LIST in the completions buffer and echo MESSAGE."
7037 (unless (and (get-buffer-window "*Completions*")
7038 (idlwave-local-value 'idlwave-completion-p "*Completions*"))
7039 (move-marker idlwave-completion-mark beg)
7040 (setq idlwave-before-completion-wconf (current-window-configuration)))
7041
7042 (if (featurep 'xemacs)
7043 (idlwave-display-completion-list-xemacs
7044 list)
7045 (idlwave-display-completion-list-emacs list))
7046
7047 ;; Store a special value in `this-command'. When `idlwave-complete'
7048 ;; finds this in `last-command', it will scroll the *Completions* buffer.
7049 (setq this-command (list 'idlwave-display-completion-list message complete))
7050
7051 ;; Mark the completions buffer as created by cib
7052 (idlwave-set-local 'idlwave-completion-p t "*Completions*")
7053
7054 ;; Fontify the classes
7055 (if (and idlwave-completion-fontify-classes
7056 (consp (car list)))
7057 (idlwave-completion-fontify-classes))
7058
7059 ;; Run the hook
7060 (run-hooks 'idlwave-completion-setup-hook)
7061
7062 ;; Display the message
7063 (message (or message "Making completion list...done")))
7064
7065 (defun idlwave-choose (function &rest args)
7066 "Call FUNCTION as a completion chooser and pass ARGS to it."
7067 (let ((completion-ignore-case t)) ; install correct value
7068 (apply function args))
7069 (if (and (eq major-mode 'idlwave-shell-mode)
7070 (boundp 'font-lock-mode)
7071 (not font-lock-mode))
7072 ;; For the shell, remove the fontification of the word before point
7073 (let ((beg (save-excursion
7074 (skip-chars-backward "a-zA-Z0-9_")
7075 (point))))
7076 (remove-text-properties beg (point) '(face nil))))
7077 (eval idlwave-complete-after-success-form-force))
7078
7079 (defun idlwave-keyboard-quit ()
7080 (interactive)
7081 (unwind-protect
7082 (if (eq (car-safe last-command) 'idlwave-display-completion-list)
7083 (idlwave-restore-wconf-after-completion))
7084 (keyboard-quit)))
7085
7086 (defun idlwave-restore-wconf-after-completion ()
7087 "Restore the old (before completion) window configuration."
7088 (and idlwave-completion-restore-window-configuration
7089 idlwave-before-completion-wconf
7090 (set-window-configuration idlwave-before-completion-wconf)))
7091
7092 (defun idlwave-one-key-select (sym prompt delay)
7093 "Make the user select an element from the alist in the variable SYM.
7094 The keys of the alist are expected to be strings. The function returns the
7095 car of the selected association.
7096 To do this, PROMPT is displayed and and the user must hit a letter key to
7097 select an entry. If the user does not reply within DELAY seconds, a help
7098 window with the options is displayed automatically.
7099 The key which is associated with each option is generated automatically.
7100 First, the strings are checked for preselected keys, like in \"[P]rint\".
7101 If these don't exist, a letter in the string is automatically selected."
7102 (let* ((alist (symbol-value sym))
7103 (temp-buffer-show-hook (if (fboundp 'fit-window-to-buffer)
7104 '(fit-window-to-buffer)))
7105 keys-alist char)
7106 ;; First check the cache
7107 (if (and (eq (symbol-value sym) (get sym :one-key-alist-last)))
7108 (setq keys-alist (get sym :one-key-alist-cache))
7109 ;; Need to make new list
7110 (setq keys-alist (idlwave-make-one-key-alist alist))
7111 (put sym :one-key-alist-cache keys-alist)
7112 (put sym :one-key-alist-last alist))
7113 ;; Display prompt and wait for quick reply
7114 (message "%s[%s]" prompt
7115 (mapconcat (lambda(x) (char-to-string (car x)))
7116 keys-alist ""))
7117 (if (sit-for delay)
7118 ;; No quick reply: Show help
7119 (save-window-excursion
7120 (with-output-to-temp-buffer "*Completions*"
7121 (mapcar (lambda(x)
7122 (princ (nth 1 x))
7123 (princ "\n"))
7124 keys-alist))
7125 (setq char (read-char)))
7126 (setq char (read-char)))
7127 (message nil)
7128 ;; Return the selected result
7129 (nth 2 (assoc char keys-alist))))
7130
7131 ;; Used for, e.g., electric debug super-examine.
7132 (defun idlwave-make-one-key-alist (alist)
7133 "Make an alist for single key selection."
7134 (let ((l alist) keys-alist name start char help
7135 (cnt 0)
7136 (case-fold-search nil))
7137 (while l
7138 (setq name (car (car l))
7139 l (cdr l))
7140 (catch 'exit
7141 ;; First check if the configuration predetermined a key
7142 (if (string-match "\\[\\(.\\)\\]" name)
7143 (progn
7144 (setq char (string-to-char (downcase (match-string 1 name)))
7145 help (format "%c: %s" char name)
7146 keys-alist (cons (list char help name) keys-alist))
7147 (throw 'exit t)))
7148 ;; Then check for capital letters
7149 (setq start 0)
7150 (while (string-match "[A-Z]" name start)
7151 (setq start (match-end 0)
7152 char (string-to-char (downcase (match-string 0 name))))
7153 (if (not (assoc char keys-alist))
7154 (progn
7155 (setq help (format "%c: %s" char
7156 (replace-match
7157 (concat "[" (match-string 0 name) "]")
7158 t t name))
7159 keys-alist (cons (list char help name) keys-alist))
7160 (throw 'exit t))))
7161 ;; Now check for lowercase letters
7162 (setq start 0)
7163 (while (string-match "[a-z]" name start)
7164 (setq start (match-end 0)
7165 char (string-to-char (match-string 0 name)))
7166 (if (not (assoc char keys-alist))
7167 (progn
7168 (setq help (format "%c: %s" char
7169 (replace-match
7170 (concat "[" (match-string 0 name) "]")
7171 t t name))
7172 keys-alist (cons (list char help name) keys-alist))
7173 (throw 'exit t))))
7174 ;; Bummer, nothing found! Use a stupid number
7175 (setq char (string-to-char (int-to-string (setq cnt (1+ cnt))))
7176 help (format "%c: %s" char name)
7177 keys-alist (cons (list char help name) keys-alist))))
7178 (nreverse keys-alist)))
7179
7180 (defun idlwave-set-local (var value &optional buffer)
7181 "Set the buffer-local value of VAR in BUFFER to VALUE."
7182 (save-excursion
7183 (set-buffer (or buffer (current-buffer)))
7184 (set (make-local-variable var) value)))
7185
7186 (defun idlwave-local-value (var &optional buffer)
7187 "Return the value of VAR in BUFFER, but only if VAR is local to BUFFER."
7188 (save-excursion
7189 (set-buffer (or buffer (current-buffer)))
7190 (and (local-variable-p var (current-buffer))
7191 (symbol-value var))))
7192
7193 ;; In XEmacs, we can use :activate-callback directly to advice the
7194 ;; choose functions. We use the private keymap only for the online
7195 ;; help feature.
7196
7197 (defvar idlwave-completion-map nil
7198 "Keymap for completion-list-mode with idlwave-complete.")
7199
7200 (defun idlwave-display-completion-list-xemacs (list &rest cl-args)
7201 (with-output-to-temp-buffer "*Completions*"
7202 (apply 'display-completion-list list
7203 ':activate-callback 'idlwave-default-choose-completion
7204 cl-args))
7205 (save-excursion
7206 (set-buffer "*Completions*")
7207 (use-local-map
7208 (or idlwave-completion-map
7209 (setq idlwave-completion-map
7210 (idlwave-make-modified-completion-map-xemacs
7211 (current-local-map)))))))
7212
7213 (defun idlwave-default-choose-completion (&rest args)
7214 "Execute `default-choose-completion' and then restore the win-conf."
7215 (apply 'idlwave-choose 'default-choose-completion args))
7216
7217 (defun idlwave-make-modified-completion-map-xemacs (old-map)
7218 "Replace `choose-completion' and `mouse-choose-completion' in OLD-MAP."
7219 (let ((new-map (copy-keymap old-map)))
7220 (define-key new-map [button3up] 'idlwave-mouse-completion-help)
7221 (define-key new-map [button3] (lambda ()
7222 (interactive)
7223 (setq this-command last-command)))
7224 new-map))
7225
7226 ;; In Emacs we also replace keybindings in the completion
7227 ;; map in order to install our wrappers.
7228
7229 (defun idlwave-display-completion-list-emacs (list)
7230 "Display completion list and install the choose wrappers."
7231 (with-output-to-temp-buffer "*Completions*"
7232 (display-completion-list list))
7233 (save-excursion
7234 (set-buffer "*Completions*")
7235 (use-local-map
7236 (or idlwave-completion-map
7237 (setq idlwave-completion-map
7238 (idlwave-make-modified-completion-map-emacs
7239 (current-local-map)))))))
7240
7241 (defun idlwave-make-modified-completion-map-emacs (old-map)
7242 "Replace `choose-completion' and `mouse-choose-completion' in OLD-MAP."
7243 (let ((new-map (copy-keymap old-map)))
7244 (substitute-key-definition
7245 'choose-completion 'idlwave-choose-completion new-map)
7246 (substitute-key-definition
7247 'mouse-choose-completion 'idlwave-mouse-choose-completion new-map)
7248 (define-key new-map [mouse-3] 'idlwave-mouse-completion-help)
7249 new-map))
7250
7251 (defun idlwave-choose-completion (&rest args)
7252 "Choose the completion that point is in or next to."
7253 (interactive)
7254 (apply 'idlwave-choose 'choose-completion args))
7255
7256 (defun idlwave-mouse-choose-completion (&rest args)
7257 "Click on an alternative in the `*Completions*' buffer to choose it."
7258 (interactive "e")
7259 (apply 'idlwave-choose 'mouse-choose-completion args))
7260
7261 ;;----------------------------------------------------------------------
7262 ;;----------------------------------------------------------------------
7263
7264 ;;; ------------------------------------------------------------------------
7265 ;;; Stucture parsing code, and code to manage class info
7266
7267 ;;
7268 ;; - Go again over the documentation how to write a completion
7269 ;; plugin. It is in self.el, but currently still very bad.
7270 ;; This could be in a separate file in the distribution, or
7271 ;; in an appendix for the manual.
7272
7273 (defvar idlwave-struct-skip
7274 "[ \t]*\\(\\$.*\n\\(^[ \t]*\\(\\$[ \t]*\\)?\\(;.*\\)?\n\\)*\\)?[ \t]*"
7275 "Regexp for skipping continued blank or comment-only lines in
7276 structures")
7277
7278 (defvar idlwave-struct-tag-regexp
7279 (concat "[{,]" ;leading comma/brace
7280 idlwave-struct-skip ; 4 groups
7281 "\\([a-zA-Z][a-zA-Z0-9_]*\\)" ;the tag itself, group 5
7282 "[ \t]*:") ; the final colon
7283 "Regexp for structure tags.")
7284
7285 (defun idlwave-struct-tags ()
7286 "Return a list of all tags in the structure defined at point.
7287 Point is expected just before the opening `{' of the struct definition."
7288 (save-excursion
7289 (let* ((borders (idlwave-struct-borders))
7290 (beg (car borders))
7291 (end (cdr borders))
7292 tags)
7293 (goto-char beg)
7294 (save-restriction
7295 (narrow-to-region beg end)
7296 (while (re-search-forward idlwave-struct-tag-regexp end t)
7297 ;; Check if we are still on the top level of the structure.
7298 (if (and (condition-case nil (progn (up-list -1) t) (error nil))
7299 (= (point) beg))
7300 (push (match-string-no-properties 5) tags))
7301 (goto-char (match-end 0))))
7302 (nreverse tags))))
7303
7304 (defun idlwave-find-struct-tag (tag)
7305 "Find a given TAG in the structure defined at point."
7306 (let* ((borders (idlwave-struct-borders))
7307 (beg (car borders))
7308 (end (cdr borders))
7309 (case-fold-search t))
7310 (re-search-forward (concat "\\(^[ \t]*\\|[,{][ \t]*\\)" tag "[ \t]*:")
7311 end t)))
7312
7313 (defun idlwave-struct-inherits ()
7314 "Return a list of all `inherits' names in the struct at point.
7315 Point is expected just before the opening `{' of the struct definition."
7316 (save-excursion
7317 (let* ((borders (idlwave-struct-borders))
7318 (beg (car borders))
7319 (end (cdr borders))
7320 (case-fold-search t)
7321 names)
7322 (goto-char beg)
7323 (save-restriction
7324 (narrow-to-region beg end)
7325 (while (re-search-forward
7326 (concat "[{,]" ;leading comma/brace
7327 idlwave-struct-skip ; 4 groups
7328 "inherits" ; The INHERITS tag
7329 idlwave-struct-skip ; 4 more
7330 "\\([a-zA-Z][a-zA-Z0-9_]*\\)") ; The super-group, #9
7331 end t)
7332 ;; Check if we are still on the top level of the structure.
7333 (if (and (condition-case nil (progn (up-list -1) t) (error nil))
7334 (= (point) beg))
7335 (push (match-string-no-properties 9) names))
7336 (goto-char (match-end 0))))
7337 (nreverse names))))
7338
7339 (defun idlwave-in-structure ()
7340 "Return t if point is inside an IDL structure definition."
7341 (let ((beg (point)))
7342 (save-excursion
7343 (if (not (or (idlwave-in-comment) (idlwave-in-quote)))
7344 (if (idlwave-find-structure-definition nil nil 'back)
7345 (let ((borders (idlwave-struct-borders)))
7346 (or (= (car borders) (cdr borders)) ;; struct not yet closed...
7347 (and (> beg (car borders)) (< beg (cdr borders))))))))))
7348
7349 (defun idlwave-struct-borders ()
7350 "Return the borders of the {...} after point as a cons cell."
7351 (let (beg)
7352 (save-excursion
7353 (skip-chars-forward "^{")
7354 (setq beg (point))
7355 (condition-case nil (forward-list 1)
7356 (error (goto-char beg)))
7357 (cons beg (point)))))
7358
7359 (defun idlwave-find-structure-definition (&optional var name bound)
7360 "Search forward for a structure definition. If VAR is non-nil,
7361 search for a structure assigned to variable VAR. If NAME is non-nil,
7362 search for a named structure NAME, if a string, or a generic named
7363 structure otherwise. If BOUND is an integer, limit the search. If
7364 BOUND is the symbol `all', we search first back and then forward
7365 through the entire file. If BOUND is the symbol `back' we search only
7366 backward."
7367 (let* ((ws "[ \t]*\\(\\$.*\n[ \t]*\\)*")
7368 (case-fold-search t)
7369 (lim (if (integerp bound) bound nil))
7370 (re (concat
7371 (if var
7372 (concat "\\<" (regexp-quote (downcase var)) "\\>" ws)
7373 "\\(\\)")
7374 "=" ws "\\({\\)"
7375 (if name
7376 (if (stringp name)
7377 (concat ws "\\(\\<" (downcase name) "\\)[^a-zA-Z0-9_$]")
7378 ;; Just a generic name
7379 (concat ws "\\<\\([a-zA-Z_0-9$]+\\)" ws ","))
7380 ""))))
7381 (if (or (and (or (eq bound 'all) (eq bound 'back))
7382 (re-search-backward re nil t))
7383 (and (not (eq bound 'back)) (re-search-forward re lim t)))
7384 (progn
7385 (goto-char (match-beginning 3))
7386 (match-string-no-properties 5)))))
7387
7388 (defvar idlwave-class-info nil)
7389 (defvar idlwave-class-reset nil) ; to reset buffer-local classes
7390
7391 (add-hook 'idlwave-update-rinfo-hook
7392 (lambda () (setq idlwave-class-reset t)))
7393 (add-hook 'idlwave-after-load-rinfo-hook
7394 (lambda () (setq idlwave-class-info nil)))
7395
7396 (defun idlwave-class-info (class)
7397 (let (list entry)
7398 (if idlwave-class-info
7399 (if idlwave-class-reset
7400 (setq
7401 idlwave-class-reset nil
7402 idlwave-class-info ; Remove any visited in a buffer
7403 (delq nil (mapcar
7404 (lambda (x)
7405 (let ((filebuf
7406 (idlwave-class-file-or-buffer
7407 (or (cdr (assq 'found-in x)) (car x)))))
7408 (if (cdr filebuf)
7409 nil
7410 x)))
7411 idlwave-class-info))))
7412 ;; Info is nil, put in the system stuff to start.
7413 (setq idlwave-class-info idlwave-system-class-info)
7414 (setq list idlwave-class-info)
7415 (while (setq entry (pop list))
7416 (idlwave-sintern-class-info entry)))
7417 (setq class (idlwave-sintern-class class))
7418 (or (assq class idlwave-class-info)
7419 (progn (idlwave-scan-class-info class)
7420 (assq class idlwave-class-info)))))
7421
7422 (defun idlwave-sintern-class-info (entry)
7423 "Sintern the class names in a class-info entry."
7424 (let ((taglist (assq 'tags entry))
7425 (inherits (assq 'inherits entry)))
7426 (setcar entry (idlwave-sintern-class (car entry) 'set))
7427 (if inherits
7428 (setcdr inherits (mapcar (lambda (x) (idlwave-sintern-class x 'set))
7429 (cdr inherits))))))
7430
7431 (defun idlwave-find-class-definition (class &optional all-hook alt-class)
7432 "Find class structure definition(s)
7433 If ALL-HOOK is set, find all named structure definitions in a given
7434 class__define routine, on which ALL-HOOK will be run. If ALT-CLASS is
7435 set, look for the name__define pro, and inside of it, for the ALT-CLASS
7436 class/struct definition"
7437 (let ((case-fold-search t) end-lim list name)
7438 (when (re-search-forward
7439 (concat "^[ \t]*pro[ \t]+" (downcase class) "__define" "\\>") nil t)
7440 (if all-hook
7441 (progn
7442 ;; For everything there
7443 (setq end-lim (save-excursion (idlwave-end-of-subprogram) (point)))
7444 (while (setq name
7445 (idlwave-find-structure-definition nil t end-lim))
7446 (funcall all-hook name)))
7447 (idlwave-find-structure-definition nil (or alt-class class))))))
7448
7449
7450 (defun idlwave-class-file-or-buffer (class)
7451 "Find buffer visiting CLASS definition"
7452 (let* ((pro (concat (downcase class) "__define"))
7453 (file (idlwave-routine-source-file
7454 (nth 3 (idlwave-rinfo-assoc pro 'pro nil
7455 (idlwave-routines))))))
7456 (cons file (if file (idlwave-get-buffer-visiting file)))))
7457
7458
7459 (defun idlwave-scan-class-info (class)
7460 "Scan all class and named structure info in the class__define pro"
7461 (let* ((idlwave-auto-routine-info-updates nil)
7462 (filebuf (idlwave-class-file-or-buffer class))
7463 (file (car filebuf))
7464 (buf (cdr filebuf))
7465 (class (idlwave-sintern-class class)))
7466 (if (or
7467 (not file)
7468 (and ;; neither a regular file nor a visited buffer
7469 (not buf)
7470 (not (file-regular-p file))))
7471 nil ; Cannot find the file/buffer to get any info
7472 (save-excursion
7473 (if buf (set-buffer buf)
7474 ;; Read the file in temporarily
7475 (set-buffer (get-buffer-create " *IDLWAVE-tmp*"))
7476 (erase-buffer)
7477 (unless (eq major-mode 'idlwave-mode)
7478 (idlwave-mode))
7479 (insert-file-contents file))
7480 (save-excursion
7481 (goto-char 1)
7482 (idlwave-find-class-definition class
7483 ;; Scan all of the structures found there
7484 (lambda (name)
7485 (let* ((this-class (idlwave-sintern-class name))
7486 (entry
7487 (list this-class
7488 (cons 'tags (idlwave-struct-tags))
7489 (cons 'inherits (idlwave-struct-inherits)))))
7490 (if (not (eq this-class class))
7491 (setq entry (nconc entry (list (cons 'found-in class)))))
7492 (idlwave-sintern-class-info entry)
7493 (push entry idlwave-class-info)))))))))
7494
7495 (defun idlwave-class-found-in (class)
7496 "Return the FOUND-IN property of the class."
7497 (cdr (assq 'found-in (idlwave-class-info class))))
7498 (defun idlwave-class-tags (class)
7499 "Return the native tags in CLASS."
7500 (cdr (assq 'tags (idlwave-class-info class))))
7501 (defun idlwave-class-inherits (class)
7502 "Return the direct superclasses of CLASS."
7503 (cdr (assq 'inherits (idlwave-class-info class))))
7504
7505
7506 (defun idlwave-all-class-tags (class)
7507 "Return a list of native and inherited tags in CLASS."
7508 (condition-case err
7509 (apply 'append (mapcar 'idlwave-class-tags
7510 (cons class (idlwave-all-class-inherits class))))
7511 (error
7512 (idlwave-class-tag-reset)
7513 (error "%s" (error-message-string err)))))
7514
7515
7516 (defun idlwave-all-class-inherits (class)
7517 "Return a list of all superclasses of CLASS (recursively expanded).
7518 The list is cached in `idlwave-class-info' for faster access."
7519 (cond
7520 ((not idlwave-support-inheritance) nil)
7521 ((eq class nil) nil)
7522 ((eq class t) nil)
7523 (t
7524 (let ((info (idlwave-class-info class))
7525 entry)
7526 (if (setq entry (assq 'all-inherits info))
7527 (cdr entry)
7528 ;; Save the depth of inheritance scan to check for circular references
7529 (let ((inherits (mapcar (lambda (x) (cons x 0))
7530 (idlwave-class-inherits class)))
7531 rtn all-inherits cl)
7532 (while inherits
7533 (setq cl (pop inherits)
7534 rtn (cons (car cl) rtn)
7535 inherits (append (mapcar (lambda (x)
7536 (cons x (1+ (cdr cl))))
7537 (idlwave-class-inherits (car cl)))
7538 inherits))
7539 (if (> (cdr cl) 999)
7540 (error
7541 "Class scan: inheritance depth exceeded. Circular inheritance?")
7542 ))
7543 (setq all-inherits (nreverse rtn))
7544 (nconc info (list (cons 'all-inherits all-inherits)))
7545 all-inherits))))))
7546
7547 (defun idlwave-entry-keywords (entry &optional record-link)
7548 "Return the flat entry keywords alist from routine-info entry.
7549 If RECORD-LINK is non-nil, the keyword text is copied and a text
7550 property indicating the link is added."
7551 (let (kwds)
7552 (mapcar
7553 (lambda (key-list)
7554 (let ((file (car key-list)))
7555 (mapcar (lambda (key-cons)
7556 (let ((key (car key-cons))
7557 (link (cdr key-cons)))
7558 (when (and record-link file)
7559 (setq key (copy-sequence key))
7560 (put-text-property
7561 0 (length key)
7562 'link
7563 (concat
7564 file
7565 (if link
7566 (concat idlwave-html-link-sep
7567 (number-to-string link))))
7568 key))
7569 (push (list key) kwds)))
7570 (cdr key-list))))
7571 (nthcdr 5 entry))
7572 (nreverse kwds)))
7573
7574 (defun idlwave-entry-find-keyword (entry keyword)
7575 "Find keyword KEYWORD in entry ENTRY, and return (with link) if set"
7576 (catch 'exit
7577 (mapc
7578 (lambda (key-list)
7579 (let ((file (car key-list))
7580 (kwd (assoc keyword (cdr key-list))))
7581 (when kwd
7582 (setq kwd (cons (car kwd)
7583 (if (and file (cdr kwd))
7584 (concat file
7585 idlwave-html-link-sep
7586 (number-to-string (cdr kwd)))
7587 (cdr kwd))))
7588 (throw 'exit kwd))))
7589 (nthcdr 5 entry))))
7590
7591 ;;==========================================================================
7592 ;;
7593 ;; Completing class structure tags. This is a completion plugin.
7594 ;; The necessary taglist is constructed dynamically
7595
7596 (defvar idlwave-current-tags-class nil)
7597 (defvar idlwave-current-class-tags nil)
7598 (defvar idlwave-current-native-class-tags nil)
7599 (defvar idlwave-sint-class-tags nil)
7600 (idlwave-new-sintern-type 'class-tag)
7601 (add-to-list 'idlwave-complete-special 'idlwave-complete-class-structure-tag)
7602 (add-hook 'idlwave-update-rinfo-hook 'idlwave-class-tag-reset)
7603
7604 (defun idlwave-complete-class-structure-tag ()
7605 "Complete a structure tag on a `self' argument in an object method."
7606 (interactive)
7607 (let ((pos (point))
7608 (case-fold-search t))
7609 (if (save-excursion
7610 ;; Check if the context is right
7611 (skip-chars-backward "a-zA-Z0-9._$")
7612 (and (< (point) (- pos 4))
7613 (looking-at "self\\.")))
7614 (let* ((class-selector (nth 2 (idlwave-current-routine)))
7615 (super-classes (idlwave-all-class-inherits class-selector)))
7616 ;; Check if we are in a class routine
7617 (unless class-selector
7618 (error "Not in a method procedure or function"))
7619 ;; Check if we need to update the "current" class
7620 (if (not (equal class-selector idlwave-current-tags-class))
7621 (idlwave-prepare-class-tag-completion class-selector))
7622 (setq idlwave-completion-help-info
7623 (list 'idlwave-complete-class-structure-tag-help
7624 (idlwave-sintern-routine
7625 (concat class-selector "__define"))
7626 nil))
7627 (let ((idlwave-cpl-bold idlwave-current-native-class-tags))
7628 (idlwave-complete-in-buffer
7629 'class-tag 'class-tag
7630 idlwave-current-class-tags nil
7631 (format "Select a tag of class %s" class-selector)
7632 "class tag"
7633 'idlwave-attach-class-tag-classes))
7634 t) ; return t to skip other completions
7635 nil)))
7636
7637 (defun idlwave-class-tag-reset ()
7638 (setq idlwave-current-tags-class nil))
7639
7640 (defun idlwave-prepare-class-tag-completion (class)
7641 "Find and parse the necessary class definitions for class structure tags."
7642 (setq idlwave-sint-class-tags nil)
7643 (setq idlwave-current-tags-class class)
7644 (setq idlwave-current-class-tags
7645 (mapcar (lambda (x)
7646 (list (idlwave-sintern-class-tag x 'set)))
7647 (idlwave-all-class-tags class)))
7648 (setq idlwave-current-native-class-tags
7649 (mapcar 'downcase (idlwave-class-tags class))))
7650
7651 ;===========================================================================
7652 ;;
7653 ;; Completing system variables and their structure fields
7654 ;; This is also a plugin.
7655
7656 (defvar idlwave-sint-sysvars nil)
7657 (defvar idlwave-sint-sysvartags nil)
7658 (idlwave-new-sintern-type 'sysvar)
7659 (idlwave-new-sintern-type 'sysvartag)
7660 (add-to-list 'idlwave-complete-special 'idlwave-complete-sysvar-or-tag)
7661 (add-hook 'idlwave-update-rinfo-hook 'idlwave-sysvars-reset)
7662 (add-hook 'idlwave-after-load-rinfo-hook 'idlwave-sintern-sysvar-alist)
7663
7664
7665 (defun idlwave-complete-sysvar-or-tag ()
7666 "Complete a system variable."
7667 (interactive)
7668 (let ((pos (point))
7669 (case-fold-search t))
7670 (cond ((save-excursion
7671 ;; Check if the context is right for system variable
7672 (skip-chars-backward "[a-zA-Z0-9_$]")
7673 (equal (char-before) ?!))
7674 (setq idlwave-completion-help-info '(idlwave-complete-sysvar-help))
7675 (idlwave-complete-in-buffer 'sysvar 'sysvar
7676 idlwave-system-variables-alist nil
7677 "Select a system variable"
7678 "system variable")
7679 t) ; return t to skip other completions
7680 ((save-excursion
7681 ;; Check if the context is right for sysvar tag
7682 (skip-chars-backward "a-zA-Z0-9_$.")
7683 (and (equal (char-before) ?!)
7684 (looking-at "\\([a-zA-Z][a-zA-Z0-9_$]*\\)\\.")
7685 (<= (match-end 0) pos)))
7686 ;; Complete a system variable tag
7687 (let* ((var (idlwave-sintern-sysvar (match-string 1)))
7688 (entry (assq var idlwave-system-variables-alist))
7689 (tags (cdr (assq 'tags entry))))
7690 (or entry (error "!%s is not a known system variable" var))
7691 (or tags (error "System variable !%s is not a structure" var))
7692 (setq idlwave-completion-help-info
7693 (list 'idlwave-complete-sysvar-tag-help var))
7694 (idlwave-complete-in-buffer 'sysvartag 'sysvartag
7695 tags nil
7696 "Select a system variable tag"
7697 "system variable tag")
7698 t)) ; return t to skip other completions
7699 (t nil))))
7700
7701 (defvar link) ;dynamic variables set by help callback
7702 (defvar props)
7703 (defun idlwave-complete-sysvar-help (mode word)
7704 (let ((word (or (nth 1 idlwave-completion-help-info) word))
7705 (entry (assoc word idlwave-system-variables-alist)))
7706 (cond
7707 ((eq mode 'test)
7708 (and (stringp word) entry (nth 1 (assq 'link entry))))
7709 ((eq mode 'set)
7710 (if entry (setq link (nth 1 (assq 'link entry))))) ;; setting dynamic!!!
7711 (t (error "This should not happen")))))
7712
7713 (defun idlwave-complete-sysvar-tag-help (mode word)
7714 (let* ((var (nth 1 idlwave-completion-help-info))
7715 (entry (assoc var idlwave-system-variables-alist))
7716 (tags (cdr (assq 'tags entry)))
7717 (main (nth 1 (assq 'link entry)))
7718 target main-base)
7719 (cond
7720 ((eq mode 'test) ; we can at least link the main
7721 (and (stringp word) entry main))
7722 ((eq mode 'set)
7723 (if entry
7724 (setq link
7725 (if (setq target (cdr (assoc-string word tags t)))
7726 (idlwave-substitute-link-target main target)
7727 main)))) ;; setting dynamic!!!
7728 (t (error "This should not happen")))))
7729
7730 (defun idlwave-split-link-target (link)
7731 "Split a given link into link file and anchor."
7732 (if (string-match idlwave-html-link-sep link)
7733 (cons (substring link 0 (match-beginning 0))
7734 (string-to-number (substring link (match-end 0))))))
7735
7736 (defun idlwave-substitute-link-target (link target)
7737 "Substitute the target anchor for the given link."
7738 (let (main-base)
7739 (setq main-base (if (string-match "#" link)
7740 (substring link 0 (match-beginning 0))
7741 link))
7742 (if target
7743 (concat main-base idlwave-html-link-sep (number-to-string target))
7744 link)))
7745
7746 ;; Fake help in the source buffer for class structure tags.
7747 ;; KWD AND NAME ARE GLOBAL-VARIABLES HERE.
7748 (defvar name)
7749 (defvar kwd)
7750 (defvar idlwave-help-do-class-struct-tag nil)
7751 (defun idlwave-complete-class-structure-tag-help (mode word)
7752 (cond
7753 ((eq mode 'test) ; nothing gets fontified for class tags
7754 nil)
7755 ((eq mode 'set)
7756 (let (class-with found-in)
7757 (when (setq class-with
7758 (idlwave-class-or-superclass-with-tag
7759 idlwave-current-tags-class
7760 word))
7761 (if (assq (idlwave-sintern-class class-with)
7762 idlwave-system-class-info)
7763 (error "No help available for system class tags"))
7764 (if (setq found-in (idlwave-class-found-in class-with))
7765 (setq name (cons (concat found-in "__define") class-with))
7766 (setq name (concat class-with "__define")))))
7767 (setq kwd word
7768 idlwave-help-do-class-struct-tag t))
7769 (t (error "This should not happen"))))
7770
7771 (defun idlwave-class-or-superclass-with-tag (class tag)
7772 "Find and return the CLASS or one of its superclass with the
7773 associated TAG, if any."
7774 (let ((sclasses (cons class (idlwave-all-class-inherits class)))
7775 cl)
7776 (catch 'exit
7777 (while sclasses
7778 (setq cl (pop sclasses))
7779 (let ((tags (idlwave-class-tags cl)))
7780 (while tags
7781 (if (eq t (compare-strings tag 0 nil (car tags) 0 nil t))
7782 (throw 'exit cl))
7783 (setq tags (cdr tags))))))))
7784
7785
7786 (defun idlwave-sysvars-reset ()
7787 (if (and (fboundp 'idlwave-shell-is-running)
7788 (idlwave-shell-is-running)
7789 idlwave-idlwave_routine_info-compiled)
7790 (idlwave-shell-send-command "idlwave_get_sysvars"
7791 'idlwave-process-sysvars 'hide)))
7792
7793 (defun idlwave-process-sysvars ()
7794 (idlwave-shell-filter-sysvars)
7795 (setq idlwave-sint-sysvars nil
7796 idlwave-sint-sysvartags nil)
7797 (idlwave-sintern-sysvar-alist))
7798
7799 (defun idlwave-sintern-sysvar-alist ()
7800 (let ((list idlwave-system-variables-alist) entry tags)
7801 (while (setq entry (pop list))
7802 (setcar entry (idlwave-sintern-sysvar (car entry) 'set))
7803 (setq tags (assq 'tags entry))
7804 (if tags
7805 (setcdr tags
7806 (mapcar (lambda (x)
7807 (cons (idlwave-sintern-sysvartag (car x) 'set)
7808 (cdr x)))
7809 (cdr tags)))))))
7810
7811 (defvar idlwave-shell-command-output)
7812 (defun idlwave-shell-filter-sysvars ()
7813 "Get any new system variables and tags."
7814 (let ((text idlwave-shell-command-output)
7815 (start 0)
7816 (old idlwave-system-variables-alist)
7817 var tags type name class link old-entry)
7818 (setq idlwave-system-variables-alist nil)
7819 (while (string-match "^IDLWAVE-SYSVAR: !\\([a-zA-Z0-9_$]+\\)\\( \\(.*\\)\\)?"
7820 text start)
7821 (setq start (match-end 0)
7822 var (match-string 1 text)
7823 tags (if (match-end 3)
7824 (idlwave-split-string (match-string 3 text))))
7825 ;; Maintain old links, if present
7826 (setq old-entry (assq (idlwave-sintern-sysvar var) old))
7827 (setq link (assq 'link old-entry))
7828 (setq idlwave-system-variables-alist
7829 (cons (list var
7830 (cons
7831 'tags
7832 (mapcar (lambda (x)
7833 (cons x
7834 (cdr (assq
7835 (idlwave-sintern-sysvartag x)
7836 (cdr (assq 'tags old-entry))))))
7837 tags)) link)
7838 idlwave-system-variables-alist)))
7839 ;; Keep the old value if query was not successful
7840 (setq idlwave-system-variables-alist
7841 (or idlwave-system-variables-alist old))))
7842
7843 (defun idlwave-completion-fontify-classes ()
7844 "Goto the *Completions* buffer and fontify the class info."
7845 (when (featurep 'font-lock)
7846 (save-excursion
7847 (set-buffer "*Completions*")
7848 (save-excursion
7849 (goto-char (point-min))
7850 (let ((buffer-read-only nil))
7851 (while (re-search-forward "\\.*<[^>]+>" nil t)
7852 (put-text-property (match-beginning 0) (match-end 0)
7853 'face 'font-lock-string-face)))))))
7854
7855 (defun idlwave-uniquify (list)
7856 (let ((ht (make-hash-table :size (length list) :test 'equal)))
7857 (delq nil
7858 (mapcar (lambda (x)
7859 (unless (gethash x ht)
7860 (puthash x t ht)
7861 x))
7862 list))))
7863
7864 (defun idlwave-after-successful-completion (type slash &optional verify)
7865 "Add `=' or `(' after successful completion of keyword and function.
7866 Restore the pre-completion window configuration if possible."
7867 (cond
7868 ((eq type 'procedure)
7869 nil)
7870 ((eq type 'function)
7871 (cond
7872 ((equal idlwave-function-completion-adds-paren nil) nil)
7873 ((or (equal idlwave-function-completion-adds-paren t)
7874 (equal idlwave-function-completion-adds-paren 1))
7875 (insert "("))
7876 ((equal idlwave-function-completion-adds-paren 2)
7877 (insert "()")
7878 (backward-char 1))
7879 (t nil)))
7880 ((eq type 'keyword)
7881 (if (and idlwave-keyword-completion-adds-equal
7882 (not slash))
7883 (progn (insert "=") t)
7884 nil)))
7885
7886 ;; Restore the pre-completion window configuration if this is safe.
7887
7888 (if (or (eq verify 'force) ; force
7889 (and
7890 (get-buffer-window "*Completions*") ; visible
7891 (idlwave-local-value 'idlwave-completion-p
7892 "*Completions*") ; cib-buffer
7893 (eq (marker-buffer idlwave-completion-mark)
7894 (current-buffer)) ; buffer OK
7895 (equal (marker-position idlwave-completion-mark)
7896 verify))) ; pos OK
7897 (idlwave-restore-wconf-after-completion))
7898 (move-marker idlwave-completion-mark nil)
7899 (setq idlwave-before-completion-wconf nil))
7900
7901 (defun idlwave-mouse-context-help (ev &optional arg)
7902 "Call `idlwave-context-help' on the clicked location."
7903 (interactive "eP")
7904 (mouse-set-point ev)
7905 (idlwave-context-help arg))
7906
7907 (defvar idlwave-last-context-help-pos nil)
7908 (defun idlwave-context-help (&optional arg)
7909 "Display IDL Online Help on context.
7910 If point is on a keyword, help for that keyword will be shown. If
7911 point is on a routine name or in the argument list of a routine, help
7912 for that routine will be displayed. Works for system routines and
7913 keywords, it pulls up text help. For other routies and keywords,
7914 visits the source file, finding help in the header (if
7915 `idlwave-help-source-try-header' is non-nil) or the routine definition
7916 itself."
7917 (interactive "P")
7918 (idlwave-do-context-help arg))
7919
7920 (defun idlwave-mouse-completion-help (ev)
7921 "Display online help about the completion at point."
7922 (interactive "eP")
7923 ;; Restore last-command for next command, to make
7924 ;; scrolling/cancelling of completions work.
7925 (setq this-command last-command)
7926 (idlwave-do-mouse-completion-help ev))
7927
7928 (defun idlwave-routine-info (&optional arg external)
7929 "Display a routines calling sequence and list of keywords. When
7930 point is on the name a function or procedure, or in the argument list
7931 of a function or procedure, this command displays a help buffer with
7932 the information. When called with prefix arg, enforce class query.
7933
7934 When point is on an object operator `->', display the class stored in
7935 this arrow, if any (see `idlwave-store-inquired-class'). With a
7936 prefix arg, the class property is cleared out."
7937
7938 (interactive "P")
7939 (idlwave-routines)
7940 (if (string-match "->" (buffer-substring
7941 (max (point-min) (1- (point)))
7942 (min (+ 2 (point)) (point-max))))
7943 ;; Cursor is on an arrow
7944 (if (get-text-property (point) 'idlwave-class)
7945 ;; arrow has class property
7946 (if arg
7947 ;; Remove property
7948 (save-excursion
7949 (backward-char 1)
7950 (when (looking-at ".?\\(->\\)")
7951 (remove-text-properties (match-beginning 1) (match-end 1)
7952 '(idlwave-class nil face nil))
7953 (message "Class property removed from arrow")))
7954 ;; Echo class property
7955 (message "Arrow has text property identifying object to be class %s"
7956 (get-text-property (point) 'idlwave-class)))
7957 ;; No property found
7958 (message "Arrow has no class text property"))
7959
7960 ;; Not on an arrow...
7961 (let* ((idlwave-query-class nil)
7962 (idlwave-force-class-query (equal arg '(4)))
7963 (module (idlwave-what-module)))
7964 (if (car module)
7965 (apply 'idlwave-display-calling-sequence
7966 (idlwave-fix-module-if-obj_new module))
7967 (error "Don't know which calling sequence to show")))))
7968
7969 (defun idlwave-resolve (&optional arg)
7970 "Call RESOLVE_ROUTINE on the module name at point.
7971 Like `idlwave-routine-info', this looks for a routine call at point.
7972 After confirmation in the minibuffer, it will use the shell to issue
7973 a RESOLVE call for this routine, to attempt to make it defined and its
7974 routine info available for IDLWAVE. If the routine is a method call,
7975 both `class__method' and `class__define' will be tried.
7976 With ARG, enforce query for the class of object methods."
7977 (interactive "P")
7978 (let* ((idlwave-query-class nil)
7979 (idlwave-force-class-query (equal arg '(4)))
7980 (module (idlwave-what-module))
7981 (name (idlwave-make-full-name (nth 2 module) (car module)))
7982 (type (if (eq (nth 1 module) 'pro) "pro" "function"))
7983 (resolve (read-string "Resolve: " (format "%s %s" type name)))
7984 (kwd "")
7985 class)
7986 (if (string-match "\\(pro\\|function\\)[ \t]+\\(\\(.*\\)::\\)?\\(.*\\)"
7987 resolve)
7988 (setq type (match-string 1 resolve)
7989 class (if (match-beginning 2)
7990 (match-string 3 resolve)
7991 nil)
7992 name (match-string 4 resolve)))
7993 (if (string= (downcase type) "function")
7994 (setq kwd ",/is_function"))
7995
7996 (cond
7997 ((null class)
7998 (idlwave-shell-send-command
7999 (format "resolve_routine,'%s'%s" (downcase name) kwd)
8000 'idlwave-update-routine-info
8001 nil t))
8002 (t
8003 (idlwave-shell-send-command
8004 (format "resolve_routine,'%s__define'%s" (downcase class) kwd)
8005 (list 'idlwave-shell-send-command
8006 (format "resolve_routine,'%s__%s'%s"
8007 (downcase class) (downcase name) kwd)
8008 '(idlwave-update-routine-info)
8009 nil t))))))
8010
8011 (defun idlwave-find-module-this-file ()
8012 (interactive)
8013 (idlwave-find-module '(4)))
8014
8015 (defun idlwave-find-module (&optional arg)
8016 "Find the source code of an IDL module.
8017 Works for modules for which IDLWAVE has routine info available. The
8018 function offers as default the module name `idlwave-routine-info'
8019 would use. With ARG limit to this buffer. With two prefix ARG's
8020 force class query for object methods."
8021 (interactive "P")
8022 (let* ((idlwave-query-class nil)
8023 (idlwave-force-class-query (equal arg '(16)))
8024 (this-buffer (equal arg '(4)))
8025 (module (idlwave-fix-module-if-obj_new (idlwave-what-module)))
8026 (default (if module
8027 (concat (idlwave-make-full-name
8028 (nth 2 module) (car module))
8029 (if (eq (nth 1 module) 'pro) "<p>" "<f>"))
8030 "none"))
8031 (list
8032 (idlwave-uniquify
8033 (delq nil
8034 (mapcar (lambda (x)
8035 (if (eq 'system (car-safe (nth 3 x)))
8036 ;; Take out system routines with no source.
8037 nil
8038 (list
8039 (concat (idlwave-make-full-name
8040 (nth 2 x) (car x))
8041 (if (eq (nth 1 x) 'pro) "<p>" "<f>")))))
8042 (if this-buffer
8043 (idlwave-save-buffer-update)
8044 (idlwave-routines))))))
8045 (name (idlwave-completing-read
8046 (if (or (not this-buffer)
8047 (assoc default list))
8048 (format "Module (Default %s): " default)
8049 (format "Module in this file: "))
8050 list))
8051 type class)
8052 (if (string-match "\\`\\s-*\\'" name)
8053 ;; Nothing, use the default.
8054 (setq name default))
8055 (if (string-match "<[fp]>" name)
8056 (setq type (substring name -2 -1)
8057 name (substring name 0 -3)))
8058 (if (string-match "\\(.*\\)::\\(.*\\)" name)
8059 (setq class (match-string 1 name)
8060 name (match-string 2 name)))
8061 (setq name (idlwave-sintern-routine-or-method name class)
8062 class (idlwave-sintern-class class)
8063 type (cond ((equal type "f") 'fun)
8064 ((equal type "p") 'pro)
8065 (t t)))
8066 (idlwave-do-find-module name type class nil this-buffer)))
8067
8068 (defun idlwave-do-find-module (name type class
8069 &optional force-source this-buffer)
8070 (let ((name1 (idlwave-make-full-name class name))
8071 source buf1 entry
8072 (buf (current-buffer))
8073 (pos (point))
8074 file name2)
8075 (setq entry (idlwave-best-rinfo-assq name type class (idlwave-routines)
8076 'WITH-FILE)
8077 source (or force-source (nth 3 entry))
8078 name2 (if (nth 2 entry)
8079 (idlwave-make-full-name (nth 2 entry) name)
8080 name1))
8081 (if source
8082 (setq file (idlwave-routine-source-file source)))
8083 (unless file ; Try to find it on the path.
8084 (setq file
8085 (idlwave-expand-lib-file-name
8086 (if class
8087 (format "%s__define.pro" (downcase class))
8088 (format "%s.pro" (downcase name))))))
8089 (cond
8090 ((or (null name) (equal name ""))
8091 (error "Abort"))
8092 ((eq (car source) 'system)
8093 (error "Source code for system routine %s is not available"
8094 name2))
8095 ((or (not file) (not (file-regular-p file)))
8096 (error "Source code for routine %s is not available"
8097 name2))
8098 (t
8099 (when (not this-buffer)
8100 (setq buf1
8101 (idlwave-find-file-noselect file 'find))
8102 (pop-to-buffer buf1 t))
8103 (goto-char (point-max))
8104 (let ((case-fold-search t))
8105 (if (re-search-backward
8106 (concat "^[ \t]*\\<"
8107 (cond ((eq type 'fun) "function")
8108 ((eq type 'pro) "pro")
8109 (t "\\(pro\\|function\\)"))
8110 "\\>[ \t]+"
8111 (regexp-quote (downcase name2))
8112 "[^a-zA-Z0-9_$]")
8113 nil t)
8114 (goto-char (match-beginning 0))
8115 (pop-to-buffer buf)
8116 (goto-char pos)
8117 (error "Could not find routine %s" name2)))))))
8118
8119 (defun idlwave-what-module ()
8120 "Return a default module for stuff near point.
8121 Used by `idlwave-routine-info' and `idlwave-find-module'."
8122 (idlwave-routines)
8123 (if (let ((case-fold-search t))
8124 (save-excursion
8125 (idlwave-beginning-of-statement)
8126 (looking-at "[ \t]*\\(pro\\|function\\)[ \t]+\\(\\([a-zA-Z0-9_$]+\\)::\\)?\\([a-zA-Z0-9$_]+\\)\\([, \t\n]\\|$\\)")))
8127 ;; This is a function or procedure definition statement
8128 ;; We return the defined routine as module.
8129 (list
8130 (idlwave-sintern-routine-or-method (match-string-no-properties 4)
8131 (match-string-no-properties 2))
8132 (if (equal (downcase (match-string 1)) "pro") 'pro 'fun)
8133 (idlwave-sintern-class (match-string 3)))
8134
8135 ;; Not a definition statement - analyze precise position.
8136 (let* ((where (idlwave-where))
8137 (cw (nth 2 where))
8138 (pro (car (nth 0 where)))
8139 (func (car (nth 1 where)))
8140 (this-word (idlwave-this-word "a-zA-Z0-9$_"))
8141 (next-char (save-excursion (skip-chars-forward "a-zA-Z0-9$_")
8142 (following-char)))
8143 )
8144 (cond
8145 ((and (eq cw 'procedure)
8146 (not (equal this-word "")))
8147 (setq this-word (idlwave-sintern-routine-or-method
8148 this-word (nth 2 (nth 3 where))))
8149 (list this-word 'pro
8150 (idlwave-determine-class
8151 (cons this-word (cdr (nth 3 where)))
8152 'pro)))
8153 ((and (eq cw 'function)
8154 (not (equal this-word ""))
8155 (or (eq next-char ?\() ; exclude arrays, vars.
8156 (looking-at "[a-zA-Z0-9_]*[ \t]*(")))
8157 (setq this-word (idlwave-sintern-routine-or-method
8158 this-word (nth 2 (nth 3 where))))
8159 (list this-word 'fun
8160 (idlwave-determine-class
8161 (cons this-word (cdr (nth 3 where)))
8162 'fun)))
8163 ((and (memq cw '(function-keyword procedure-keyword))
8164 (not (equal this-word ""))
8165 (eq next-char ?\()) ; A function!
8166 (setq this-word (idlwave-sintern-routine this-word))
8167 (list this-word 'fun nil))
8168 (func
8169 (list func 'fun (idlwave-determine-class (nth 1 where) 'fun)))
8170 (pro
8171 (list pro 'pro (idlwave-determine-class (nth 0 where) 'pro)))
8172 (t nil)))))
8173
8174 (defun idlwave-what-module-find-class ()
8175 "Call idlwave-what-module and find the inherited class if necessary."
8176 (let* ((module (idlwave-what-module))
8177 (class (nth 2 module))
8178 classes)
8179 (if (and (= (length module) 3)
8180 (stringp class))
8181 (list (car module)
8182 (nth 1 module)
8183 (apply 'idlwave-find-inherited-class module))
8184 module)))
8185
8186 (defun idlwave-find-inherited-class (name type class)
8187 "Find the class which defines TYPE NAME and is CLASS or inherited by CLASS."
8188 (let ((entry (idlwave-best-rinfo-assoc name type class (idlwave-routines))))
8189 (if entry
8190 (nth 2 entry)
8191 class)))
8192
8193 (defun idlwave-fix-module-if-obj_new (module)
8194 "Check if MODULE points to obj_new.
8195 If yes, and if the cursor is in the keyword region, change to the
8196 appropriate Init method."
8197 (let* ((name (car module))
8198 (pos (point))
8199 (case-fold-search t)
8200 string)
8201 (if (and (stringp name)
8202 (equal (downcase name) "obj_new")
8203 (save-excursion
8204 (idlwave-beginning-of-statement)
8205 (setq string (buffer-substring (point) pos))
8206 (string-match "obj_new([^'\"]*['\"]\\([a-zA-Z0-9_]+\\)"
8207 string)))
8208 (let ((name "Init")
8209 (class (match-string 1 string)))
8210 (setq module (list (idlwave-sintern-method "Init")
8211 'fun
8212 (idlwave-sintern-class class)))))
8213 module))
8214
8215 (defun idlwave-fix-keywords (name type class keywords
8216 &optional super-classes system)
8217 "Update a list of keywords.
8218 Translate OBJ_NEW, adding all super-class keywords, or all keywords
8219 from all classes if class equals t. If SYSTEM is non-nil, don't
8220 demand _EXTRA in the keyword list."
8221 (let ((case-fold-search t))
8222
8223 ;; If this is the OBJ_NEW function, try to figure out the class and use
8224 ;; the keywords from the corresponding INIT method.
8225 (if (and (equal (upcase name) "OBJ_NEW")
8226 (or (eq major-mode 'idlwave-mode)
8227 (eq major-mode 'idlwave-shell-mode)))
8228 (let* ((bos (save-excursion (idlwave-beginning-of-statement) (point)))
8229 (string (buffer-substring bos (point)))
8230 (case-fold-search t)
8231 class)
8232 (and (string-match "obj_new([^'\"]*['\"]\\([a-zA-Z0-9_]+\\)"
8233 string)
8234 (setq class (idlwave-sintern-class (match-string 1 string)))
8235 (setq idlwave-current-obj_new-class class)
8236 (setq keywords
8237 (append keywords
8238 (idlwave-entry-keywords
8239 (idlwave-rinfo-assq
8240 (idlwave-sintern-method "INIT")
8241 'fun
8242 class
8243 (idlwave-routines)) 'do-link))))))
8244
8245 ;; If the class is `t', combine all keywords of all methods NAME
8246 (when (eq class t)
8247 (mapc (lambda (entry)
8248 (and
8249 (nth 2 entry) ; non-nil class
8250 (eq (nth 1 entry) type) ; correct type
8251 (setq keywords
8252 (append keywords
8253 (idlwave-entry-keywords entry 'do-link)))))
8254 (idlwave-all-assq name (idlwave-routines)))
8255 (setq keywords (idlwave-uniquify keywords)))
8256
8257 ;; If we have inheritance, add all keywords from superclasses, if
8258 ;; the user indicated that method in `idlwave-keyword-class-inheritance'
8259 (when (and
8260 super-classes
8261 idlwave-keyword-class-inheritance
8262 (stringp class)
8263 (or
8264 system
8265 (assq (idlwave-sintern-keyword "_extra") keywords)
8266 (assq (idlwave-sintern-keyword "_ref_extra") keywords))
8267 ;; Check if one of the keyword-class regexps matches the name
8268 (let ((regexps idlwave-keyword-class-inheritance) re)
8269 (catch 'exit
8270 (while (setq re (pop regexps))
8271 (if (string-match re name) (throw 'exit t))))))
8272
8273 (loop for entry in (idlwave-routines) do
8274 (and (nth 2 entry) ; non-nil class
8275 (memq (nth 2 entry) super-classes) ; an inherited class
8276 (eq (nth 1 entry) type) ; correct type
8277 (eq (car entry) name) ; correct name
8278 (mapcar (lambda (k) (add-to-list 'keywords k))
8279 (idlwave-entry-keywords entry 'do-link))))
8280 (setq keywords (idlwave-uniquify keywords)))
8281
8282 ;; Return the final list
8283 keywords))
8284
8285 (defun idlwave-expand-keyword (keyword module)
8286 "Expand KEYWORD to one of the valid keyword parameters of MODULE.
8287 KEYWORD may be an exact match or an abbreviation of a keyword.
8288 If the match is exact, KEYWORD itself is returned, even if there may be other
8289 keywords of which KEYWORD is an abbreviation. This is necessary because some
8290 system routines have keywords which are prefixes of other keywords.
8291 If KEYWORD is an abbreviation of several keywords, a list of all possible
8292 completions is returned.
8293 If the abbreviation was unique, the correct keyword is returned.
8294 If it cannot be a keyword, the function return nil.
8295 If we do not know about MODULE, just return KEYWORD literally."
8296 (let* ((name (car module))
8297 (type (nth 1 module))
8298 (class (nth 2 module))
8299 (kwd (idlwave-sintern-keyword keyword))
8300 (entry (idlwave-best-rinfo-assoc name type class (idlwave-routines)))
8301 (kwd-alist (idlwave-entry-keywords entry))
8302 (extra (or (assq (idlwave-sintern-keyword "_EXTRA") kwd-alist)
8303 (assq (idlwave-sintern-keyword "_REF_EXTRA") kwd-alist)))
8304 (completion-ignore-case t)
8305 candidates)
8306 (cond ((assq kwd kwd-alist)
8307 kwd)
8308 ((setq candidates (all-completions kwd kwd-alist))
8309 (if (= (length candidates) 1)
8310 (car candidates)
8311 candidates))
8312 ((and entry extra)
8313 ;; Inheritance may cause this keyword to be correct
8314 keyword)
8315 (entry
8316 ;; We do know the function, which does not have the keyword.
8317 nil)
8318 (t
8319 ;; We do not know the function, so this just might be a correct
8320 ;; keyword - return it as it is.
8321 keyword))))
8322
8323 (defvar idlwave-rinfo-mouse-map (make-sparse-keymap))
8324 (defvar idlwave-rinfo-map (make-sparse-keymap))
8325 (define-key idlwave-rinfo-mouse-map
8326 (if (featurep 'xemacs) [button2] [mouse-2])
8327 'idlwave-mouse-active-rinfo)
8328 (define-key idlwave-rinfo-mouse-map
8329 (if (featurep 'xemacs) [(shift button2)] [(shift mouse-2)])
8330 'idlwave-mouse-active-rinfo-shift)
8331 (define-key idlwave-rinfo-mouse-map
8332 (if (featurep 'xemacs) [button3] [mouse-3])
8333 'idlwave-mouse-active-rinfo-right)
8334 (define-key idlwave-rinfo-mouse-map " " 'idlwave-active-rinfo-space)
8335 (define-key idlwave-rinfo-map "q" 'idlwave-quit-help)
8336 (define-key idlwave-rinfo-mouse-map "q" 'idlwave-quit-help)
8337 (defvar idlwave-popup-source nil)
8338 (defvar idlwave-rinfo-marker (make-marker))
8339
8340 (defun idlwave-quit-help ()
8341 (interactive)
8342 (let ((ri-window (get-buffer-window "*Help*"))
8343 (olh-window (get-buffer-window "*IDLWAVE Help*")))
8344 (when (and olh-window
8345 (fboundp 'idlwave-help-quit))
8346 (select-window olh-window)
8347 (idlwave-help-quit))
8348 (when (window-live-p ri-window)
8349 (delete-window ri-window))))
8350
8351 (defun idlwave-display-calling-sequence (name type class
8352 &optional initial-class)
8353 ;; Display the calling sequence of module NAME, type TYPE in class CLASS.
8354 (let* ((initial-class (or initial-class class))
8355 (entry (or (idlwave-best-rinfo-assq name type class
8356 (idlwave-routines))
8357 (idlwave-rinfo-assq name type class
8358 idlwave-unresolved-routines)))
8359 (name (or (car entry) name))
8360 (class (or (nth 2 entry) class))
8361 (superclasses (idlwave-all-class-inherits initial-class))
8362 (twins (idlwave-routine-twins entry))
8363 (dtwins (idlwave-study-twins twins))
8364 (all dtwins)
8365 (system (eq (car (nth 3 entry)) 'system))
8366 (calling-seq (nth 4 entry))
8367 (keywords (idlwave-entry-keywords entry 'do-link))
8368 (html-file (car (nth 5 entry)))
8369 (help-echo-kwd
8370 "Button2: Insert KEYWORD (SHIFT=`/KEYWORD') | Button3: Online Help ")
8371 (help-echo-use
8372 "Button2/3: Online Help")
8373 (help-echo-src
8374 "Button2: Jump to source and back | Button3: Source in Help window.")
8375 (help-echo-class
8376 "Button2: Display info about same method in superclass")
8377 (col 0)
8378 (data (list name type class (current-buffer) nil initial-class))
8379 (km-prop (if (featurep 'xemacs) 'keymap 'local-map))
8380 (face 'idlwave-help-link)
8381 beg props win cnt total)
8382 ;; Fix keywords, but don't add chained super-classes, since these
8383 ;; are shown separately for that super-class
8384 (setq keywords (idlwave-fix-keywords name type class keywords))
8385 (cond
8386 ((null entry)
8387 (error "No %s %s known %s" type name
8388 (if initial-class (concat "in class " initial-class) "")))
8389 ((or (null name) (equal name ""))
8390 (error "No function or procedure call at point"))
8391 ((null calling-seq)
8392 (error "Calling sequence of %s %s not available" type name))
8393 (t
8394 (save-excursion
8395 (move-marker idlwave-rinfo-marker (point))
8396 (set-buffer (get-buffer-create "*Help*"))
8397 (use-local-map idlwave-rinfo-map)
8398 (setq buffer-read-only nil)
8399 (erase-buffer)
8400 (set (make-local-variable 'idlwave-popup-source) nil)
8401 (set (make-local-variable 'idlwave-current-obj_new-class)
8402 idlwave-current-obj_new-class)
8403 (when superclasses
8404 (setq props (list 'mouse-face 'highlight
8405 km-prop idlwave-rinfo-mouse-map
8406 'help-echo help-echo-class
8407 'data (cons 'class data)))
8408 (let ((classes (cons initial-class superclasses)) c)
8409 (insert "Classes: ")
8410 (while (setq c (pop classes))
8411 (insert " ")
8412 (setq beg (point))
8413 (insert c)
8414 (if (equal (downcase c) (downcase class))
8415 (add-text-properties beg (point) (list 'face 'bold))
8416 ;; If Method exists in a different class link it
8417 (if (idlwave-rinfo-assq name type c (idlwave-routines))
8418 (add-text-properties beg (point) props))))
8419 (insert "\n")))
8420 (setq props (list 'mouse-face 'highlight
8421 km-prop idlwave-rinfo-mouse-map
8422 'help-echo help-echo-use
8423 'data (cons 'usage data)))
8424 (if html-file (setq props (append (list 'face face 'link html-file)
8425 props)))
8426 (insert "Usage: ")
8427 (setq beg (point))
8428 (insert (if class
8429 (format calling-seq class name class name class name)
8430 (format calling-seq name name name name))
8431 "\n")
8432 (add-text-properties beg (point) props)
8433
8434 (insert "Keywords:")
8435 (if (null keywords)
8436 (insert " No keywords accepted.")
8437 (setq col 9)
8438 (mapcar
8439 (lambda (x)
8440 (if (>= (+ col 1 (length (car x)))
8441 (window-width))
8442 (progn
8443 (insert "\n ")
8444 (setq col 9)))
8445 (insert " ")
8446 (setq beg (point)
8447 ;; Relevant keywords already have link property attached
8448 props (list 'mouse-face 'highlight
8449 km-prop idlwave-rinfo-mouse-map
8450 'data (cons 'keyword data)
8451 'help-echo help-echo-kwd
8452 'keyword (car x)))
8453 (if system (setq props (append (list 'face face) props)))
8454 (insert (car x))
8455 (add-text-properties beg (point) props)
8456 (setq col (+ col 1 (length (car x)))))
8457 keywords))
8458
8459 (setq cnt 1 total (length all))
8460 ;; Here entry is (key file (list of type-conses))
8461 (while (setq entry (pop all))
8462 (setq props (list 'mouse-face 'highlight
8463 km-prop idlwave-rinfo-mouse-map
8464 'help-echo help-echo-src
8465 'source (list (car (car (nth 2 entry))) ;type
8466 (nth 1 entry)
8467 nil
8468 (cdr (car (nth 2 entry))))
8469 'data (cons 'source data)))
8470 (idlwave-insert-source-location
8471 (format "\n%-8s %s"
8472 (if (equal cnt 1)
8473 (if (> total 1) "Sources:" "Source:")
8474 "")
8475 (if (> total 1) "- " ""))
8476 entry props)
8477 (incf cnt)
8478 (when (and all (> cnt idlwave-rinfo-max-source-lines))
8479 ;; No more source lines, please
8480 (insert (format
8481 "\n Source information truncated to %d entries."
8482 idlwave-rinfo-max-source-lines))
8483 (setq all nil)))
8484 (goto-char (point-min))
8485 (setq buffer-read-only t))
8486 (display-buffer "*Help*")
8487 (if (and (setq win (get-buffer-window "*Help*"))
8488 idlwave-resize-routine-help-window)
8489 (progn
8490 (let ((ww (selected-window)))
8491 (unwind-protect
8492 (progn
8493 (select-window win)
8494 (enlarge-window (- (/ (frame-height) 2)
8495 (window-height)))
8496 (shrink-window-if-larger-than-buffer))
8497 (select-window ww)))))))))
8498
8499 (defun idlwave-insert-source-location (prefix entry &optional file-props)
8500 "Insert a source location into the routine info buffer.
8501 Start line with PREFIX. If a file name is inserted, add FILE-PROPS to
8502 it."
8503 (let* ((key (car entry))
8504 (file (nth 1 entry))
8505 (types (nth 2 entry))
8506 (shell-flag (assq 'compiled types))
8507 (buffer-flag (assq 'buffer types))
8508 (user-flag (assq 'user types))
8509 (lib-flag (assq 'lib types))
8510 (ndupl (or (and buffer-flag (idlwave-count-memq 'buffer types))
8511 (and user-flag (idlwave-count-memq 'user types))
8512 (and lib-flag (idlwave-count-memq 'lib types))
8513 1))
8514 (doflags t)
8515 beg special)
8516
8517 (insert prefix)
8518
8519 (cond
8520 ((eq key 'system)
8521 (setq doflags nil)
8522 (insert "System "))
8523
8524 ((eq key 'builtin)
8525 (setq doflags nil)
8526 (insert "Builtin "))
8527
8528 ((and (not file) shell-flag)
8529 (insert "Unresolved"))
8530
8531 ((null file)
8532 (insert "ERROR"))
8533
8534 ((idlwave-syslib-p file)
8535 (if (string-match "obsolete" (file-name-directory file))
8536 (insert "Obsolete ")
8537 (insert "SystemLib ")))
8538
8539 ;; New special syntax: taken directly from routine-info for
8540 ;; library catalog routines
8541 ((setq special (or (cdr lib-flag) (cdr user-flag)))
8542 (insert (format "%-10s" special)))
8543
8544 ;; Old special syntax: a matching regexp
8545 ((setq special (idlwave-special-lib-test file))
8546 (insert (format "%-10s" special)))
8547
8548 ;; Catch-all with file
8549 ((idlwave-lib-p file) (insert "Library "))
8550
8551 ;; Sanity catch all
8552 (t (insert "Other ")))
8553
8554 (when doflags
8555 (insert (concat
8556 " ["
8557 (if lib-flag "L" "-")
8558 (if user-flag "C" "-")
8559 (if shell-flag "S" "-")
8560 (if buffer-flag "B" "-")
8561 "] ")))
8562 (when (> ndupl 1)
8563 (setq beg (point))
8564 (insert (format "(%dx) " ndupl))
8565 (add-text-properties beg (point) (list 'face 'bold)))
8566 (when (and file (not (equal file "")))
8567 (setq beg (point))
8568 (insert (apply 'abbreviate-file-name
8569 (if (featurep 'xemacs) (list file t) (list file))))
8570 (if file-props
8571 (add-text-properties beg (point) file-props)))))
8572
8573 (defun idlwave-special-lib-test (file)
8574 "Check the path of FILE against the regexps which define special libs.
8575 Return the name of the special lib if there is a match."
8576 (let ((alist idlwave-special-lib-alist)
8577 entry rtn)
8578 (cond
8579 ((stringp file)
8580 (while (setq entry (pop alist))
8581 (if (string-match (car entry) file)
8582 (setq rtn (cdr entry)
8583 alist nil)))
8584 rtn)
8585 (t nil))))
8586
8587 (defun idlwave-mouse-active-rinfo-right (ev)
8588 (interactive "e")
8589 (idlwave-mouse-active-rinfo ev 'right))
8590
8591 (defun idlwave-mouse-active-rinfo-shift (ev)
8592 (interactive "e")
8593 (idlwave-mouse-active-rinfo ev nil 'shift))
8594
8595 (defun idlwave-active-rinfo-space ()
8596 (interactive)
8597 (idlwave-mouse-active-rinfo nil 'right))
8598
8599 (defun idlwave-mouse-active-rinfo (ev &optional right shift)
8600 "Does the mouse actions in the routine info buffer.
8601 Optional args RIGHT and SHIFT indicate, if mouse-3 was used, and if SHIFT
8602 was pressed."
8603 (interactive "e")
8604 (if ev (mouse-set-point ev))
8605 (let (data id name type class buf bufwin source link keyword
8606 word initial-class)
8607 (setq data (get-text-property (point) 'data)
8608 source (get-text-property (point) 'source)
8609 keyword (get-text-property (point) 'keyword)
8610 link (get-text-property (point) 'link)
8611 id (car data)
8612 name (nth 1 data) type (nth 2 data) class (nth 3 data)
8613 buf (nth 4 data)
8614 initial-class (nth 6 data)
8615 word (idlwave-this-word)
8616 bufwin (get-buffer-window buf t))
8617
8618 (cond ((eq id 'class) ; Switch class being displayed
8619 (if (window-live-p bufwin) (select-window bufwin))
8620 (idlwave-display-calling-sequence
8621 (idlwave-sintern-method name)
8622 type (idlwave-sintern-class word)
8623 initial-class))
8624 ((eq id 'usage) ; Online help on this routine
8625 (idlwave-online-help link name type class))
8626 ((eq id 'source) ; Source in help or buffer
8627 (if right ; In help
8628 (let ((idlwave-extra-help-function 'idlwave-help-with-source)
8629 (idlwave-help-source-try-header nil)
8630 ;; Fake idlwave-routines so help will find the right entry
8631 (idlwave-routines
8632 (list (list name type class source ""))))
8633 (idlwave-help-get-special-help name type class nil))
8634 ;; Otherwise just pop to the source
8635 (setq idlwave-popup-source (not idlwave-popup-source))
8636 (if idlwave-popup-source
8637 (condition-case err
8638 (idlwave-do-find-module name type class source)
8639 (error
8640 (setq idlwave-popup-source nil)
8641 (if (window-live-p bufwin) (select-window bufwin))
8642 (error (nth 1 err))))
8643 (if bufwin
8644 (select-window bufwin)
8645 (pop-to-buffer buf))
8646 (goto-char (marker-position idlwave-rinfo-marker)))))
8647 ((eq id 'keyword)
8648 (if right
8649 (idlwave-online-help link name type class keyword)
8650 (idlwave-rinfo-insert-keyword keyword buf shift))))))
8651
8652 (defun idlwave-rinfo-insert-keyword (keyword buffer &optional shift)
8653 "Insert KEYWORD in BUFFER. Make sure buffer is displayed in a window."
8654 (let ((bwin (get-buffer-window buffer)))
8655 (if idlwave-complete-empty-string-as-lower-case
8656 (setq keyword (downcase keyword)))
8657 (if bwin
8658 (select-window bwin)
8659 (pop-to-buffer buffer)
8660 (setq bwin (get-buffer-window buffer)))
8661 (if (eq (preceding-char) ?/)
8662 (insert keyword)
8663 (unless (save-excursion
8664 (re-search-backward
8665 "[(,][ \t]*\\(\\$[ \t]*\\(;.*\\)?\n\\)?[ \t]*\\="
8666 (min (- (point) 100) (point-min)) t))
8667 (insert ", "))
8668 (if shift (insert "/"))
8669 (insert keyword)
8670 (if (and (not shift)
8671 idlwave-keyword-completion-adds-equal)
8672 (insert "=")))))
8673
8674 (defun idlwave-list-buffer-load-path-shadows (&optional arg)
8675 "List the load path shadows of all routines defined in current buffer."
8676 (interactive "P")
8677 (idlwave-routines)
8678 (if (eq major-mode 'idlwave-mode)
8679 (idlwave-list-load-path-shadows
8680 nil (idlwave-update-current-buffer-info 'save-buffer)
8681 "in current buffer")
8682 (error "Current buffer is not in idlwave-mode")))
8683
8684 (defun idlwave-list-shell-load-path-shadows (&optional arg)
8685 "List the load path shadows of all routines compiled under the shell.
8686 This is very useful for checking an IDL application. Just compile the
8687 application, do RESOLVE_ALL, and `C-c C-i' to compile all referenced
8688 routines and update IDLWAVE internal info. Then check for shadowing
8689 with this command."
8690 (interactive "P")
8691 (cond
8692 ((or (not (fboundp 'idlwave-shell-is-running))
8693 (not (idlwave-shell-is-running)))
8694 (error "Shell is not running"))
8695 ((null idlwave-compiled-routines)
8696 (error "No compiled routines. Maybe you need to update with `C-c C-i'"))
8697 (t
8698 (idlwave-list-load-path-shadows nil idlwave-compiled-routines
8699 "in the shell"))))
8700
8701 (defun idlwave-list-all-load-path-shadows (&optional arg)
8702 "List the load path shadows of all routines known to IDLWAVE."
8703 (interactive "P")
8704 (idlwave-list-load-path-shadows nil nil "globally"))
8705
8706 (defun idlwave-list-load-path-shadows (arg &optional special-routines loc)
8707 "List the routines which are defined multiple times.
8708 Search the information IDLWAVE has about IDL routines for multiple
8709 definitions.
8710 When SPECIAL-ROUTINES in non-nil, only look for shadows of these routines.
8711
8712 When IDL hits a routine call which is not defined, it will search on
8713 the load path in order to find a definition. The output of this
8714 command can be used to detect possible name clashes during this process."
8715 (idlwave-routines) ; Make sure everything is loaded.
8716 (unless (or idlwave-user-catalog-routines idlwave-library-catalog-routines)
8717 (or (y-or-n-p
8718 "You don't have any user or library catalogs. Continue anyway? ")
8719 (error "Abort")))
8720 (let* ((routines (append idlwave-system-routines
8721 idlwave-compiled-routines
8722 idlwave-library-catalog-routines
8723 idlwave-user-catalog-routines
8724 idlwave-buffer-routines
8725 nil))
8726 (km-prop (if (featurep 'xemacs) 'keymap 'local-map))
8727 (keymap (make-sparse-keymap))
8728 (props (list 'mouse-face 'highlight
8729 km-prop keymap
8730 'help-echo "Mouse2: Find source"))
8731 (nroutines (length (or special-routines routines)))
8732 (step (/ nroutines 100))
8733 (n 0)
8734 (cnt 0)
8735 (idlwave-sort-prefer-buffer-info nil)
8736 routine twins dtwins twin done props1 lroutines)
8737
8738 (if special-routines
8739 ;; Just looking for shadows of a few special routines
8740 (setq lroutines routines
8741 routines special-routines))
8742
8743 (message "Sorting routines...")
8744 (setq routines (sort routines
8745 (lambda (a b)
8746 (string< (downcase (idlwave-make-full-name
8747 (nth 2 a) (car a)))
8748 (downcase (idlwave-make-full-name
8749 (nth 2 b) (car b)))))))
8750 (message "Sorting routines...done")
8751
8752 (define-key keymap (if (featurep 'xemacs) [(button2)] [(mouse-2)])
8753 (lambda (ev)
8754 (interactive "e")
8755 (mouse-set-point ev)
8756 (apply 'idlwave-do-find-module
8757 (get-text-property (point) 'find-args))))
8758 (define-key keymap [(return)]
8759 (lambda ()
8760 (interactive)
8761 (apply 'idlwave-do-find-module
8762 (get-text-property (point) 'find-args))))
8763 (message "Compiling list...( 0%%)")
8764 (save-excursion
8765 (set-buffer (get-buffer-create "*Shadows*"))
8766 (setq buffer-read-only nil)
8767 (erase-buffer)
8768 (while (setq routine (pop routines))
8769 (if (= (mod (setq n (1+ n)) step) 0)
8770 (message "Compiling list...(%2d%%)" (/ (* n 100) nroutines)))
8771
8772 ;; Get a list of all twins
8773 (setq twins (idlwave-routine-twins routine (or lroutines routines)))
8774 (if (memq routine done)
8775 (setq dtwins nil)
8776 (setq dtwins (idlwave-study-twins twins)))
8777 ;; Mark all twins as dealt with
8778 (setq done (append twins done))
8779 (when (or (> (length dtwins) 1)
8780 (> (idlwave-count-memq 'lib (nth 2 (car dtwins))) 1)
8781 (> (idlwave-count-memq 'user (nth 2 (car dtwins))) 1)
8782 (> (idlwave-count-memq 'buffer (nth 2 (car dtwins))) 1))
8783 (incf cnt)
8784 (insert (format "\n%s%s"
8785 (idlwave-make-full-name (nth 2 routine)
8786 (car routine))
8787 (if (eq (nth 1 routine) 'fun) "()" "")))
8788 (while (setq twin (pop dtwins))
8789 (setq props1 (append (list 'find-args
8790 (list (nth 0 routine)
8791 (nth 1 routine)
8792 (nth 2 routine)))
8793 props))
8794 (idlwave-insert-source-location "\n - " twin props1))))
8795 (goto-char (point-min))
8796 (setq buffer-read-only t))
8797 (setq loc (or loc ""))
8798 (if (> cnt 0)
8799 (progn
8800 (display-buffer (get-buffer "*Shadows*"))
8801 (message "%d case%s of shadowing found %s"
8802 cnt (if (= cnt 1) "" "s") loc))
8803 (message "No shadowing conflicts found %s" loc))))
8804
8805 (defun idlwave-print-source (routine)
8806 (let* ((source (nth 3 routine))
8807 (stype (car source))
8808 (sfile (idlwave-routine-source-file source)))
8809 (if (idlwave-syslib-p sfile) (setq stype 'syslib))
8810 (if (and (eq stype 'compiled)
8811 (or (not (stringp sfile))
8812 (not (string-match "\\S-" sfile))))
8813 (setq stype 'unresolved))
8814 (princ (format " %-10s %s\n"
8815 stype
8816 (if sfile sfile "No source code available")))))
8817
8818 (defun idlwave-routine-twins (entry &optional list)
8819 "Return all twin entries of ENTRY in LIST.
8820 LIST defaults to `idlwave-routines'.
8821 Twin entries are those which have the same name, type, and class.
8822 ENTRY will also be returned, as the first item of this list."
8823 (let* ((name (car entry))
8824 (type (nth 1 entry))
8825 (class (nth 2 entry))
8826 (candidates (idlwave-all-assq name (or list (idlwave-routines))))
8827 twins candidate)
8828 (while (setq candidate (pop candidates))
8829 (if (and (not (eq candidate entry))
8830 (eq type (nth 1 candidate))
8831 (eq class (nth 2 candidate)))
8832 (push candidate twins)))
8833 (if (setq candidate (idlwave-rinfo-assq name type class
8834 idlwave-unresolved-routines))
8835 (push candidate twins))
8836 (cons entry (nreverse twins))))
8837
8838 (defun idlwave-study-twins (entries)
8839 "Return dangerous twins of first entry in ENTRIES.
8840 Dangerous twins are routines with same name, but in different files on
8841 the load path. If a file is in the system library and has an entry in
8842 the `idlwave-system-routines' list, we omit the latter as
8843 non-dangerous because many IDL routines are implemented as library
8844 routines, and may have been scanned."
8845 (let* ((entry (car entries))
8846 (name (car entry)) ;
8847 (type (nth 1 entry)) ; Must be bound for
8848 (class (nth 2 entry)) ; idlwave-routine-twin-compare
8849 (cnt 0)
8850 source type type-cons file alist syslibp key)
8851 (while (setq entry (pop entries))
8852 (incf cnt)
8853 (setq source (nth 3 entry)
8854 type (car source)
8855 type-cons (cons type (nth 3 source))
8856 file (idlwave-routine-source-file source))
8857
8858 ;; Make KEY to index entry properly
8859 (setq key (cond ((eq type 'system) type)
8860 (file (file-truename file))
8861 (t 'unresolved)))
8862
8863 ;; Check for an entry in the system library
8864 (if (and file
8865 (not syslibp)
8866 (idlwave-syslib-p file))
8867 (setq syslibp t))
8868
8869 ;; If there's more than one matching entry for the same file, just
8870 ;; append the type-cons to the type list.
8871 (if (setq entry (assoc key alist))
8872 (push type-cons (nth 2 entry))
8873 (push (list key file (list type-cons)) alist)))
8874
8875 (setq alist (nreverse alist))
8876
8877 (when syslibp
8878 ;; File is in system *library* - remove any 'system entry
8879 (setq alist (delq (assq 'system alist) alist)))
8880
8881 ;; If 'system remains and we've scanned the syslib, it's a builtin
8882 ;; (rather than a !DIR/lib/.pro file bundled as source).
8883 (when (and (idlwave-syslib-scanned-p)
8884 (setq entry (assoc 'system alist)))
8885 (setcar entry 'builtin))
8886 (sort alist 'idlwave-routine-twin-compare)))
8887
8888 (defvar type)
8889 (defvar class)
8890 (defvar idlwave-sort-prefer-buffer-info t
8891 "Internal variable used to influence `idlwave-routine-twin-compare'.")
8892
8893 (defmacro idlwave-xor (a b)
8894 `(and (or ,a ,b)
8895 (not (and ,a ,b))))
8896
8897 (defun idlwave-routine-entry-compare (a b)
8898 "Compare two routine info entries for sortiung. This is the general case.
8899 It first compates class, names, and type. If it turns out that A and B
8900 are twins (same name, class, and type), calls another routine which
8901 compares twins on the basis of their file names and path locations."
8902 (let ((name (car a)) (type (nth 1 a)) (class (nth 2 a)))
8903 (cond
8904 ((not (equal (idlwave-downcase-safe class)
8905 (idlwave-downcase-safe (nth 2 b))))
8906 ;; Class decides
8907 (cond ((null (nth 2 b)) nil)
8908 ((null class) t)
8909 (t (string< (downcase class) (downcase (nth 2 b))))))
8910 ((not (equal (downcase name) (downcase (car b))))
8911 ;; Name decides
8912 (string< (downcase name) (downcase (car b))))
8913 ((not (eq type (nth 1 b)))
8914 ;; Type decides
8915 (< (if (eq type 'fun) 1 0) (if (eq (nth 1 b) 'fun) 1 0)))
8916 (t
8917 ;; A and B are twins - so the decision is more complicated.
8918 ;; Call twin-compare with the proper arguments.
8919 (idlwave-routine-entry-compare-twins a b)))))
8920
8921 (defun idlwave-routine-entry-compare-twins (a b)
8922 "Compare two routine entries, under the assumption that they are
8923 twins. This basically calls `idlwave-routine-twin-compare' with the
8924 correct args."
8925 (let* ((name (car a)) (type (nth 1 a)) (class (nth 2 a)) ; needed outside
8926 (asrc (nth 3 a))
8927 (atype (car asrc))
8928 (bsrc (nth 3 b))
8929 (btype (car bsrc))
8930 (afile (idlwave-routine-source-file asrc))
8931 (bfile (idlwave-routine-source-file bsrc)))
8932 (idlwave-routine-twin-compare
8933 (if (stringp afile)
8934 (list (file-truename afile) afile (list atype))
8935 (list atype afile (list atype)))
8936 (if (stringp bfile)
8937 (list (file-truename bfile) bfile (list btype))
8938 (list btype bfile (list btype))))
8939 ))
8940
8941 (defun idlwave-routine-twin-compare (a b)
8942 "Compare two routine twin entries for sorting.
8943 In here, A and B are not normal routine info entries, but special
8944 lists (KEY FILENAME (TYPES...)).
8945 This expects NAME TYPE CLASS to be bound to the right values."
8946 (let* (;; Dis-assemble entries
8947 (akey (car a)) (bkey (car b))
8948 (afile (nth 1 a)) (bfile (nth 1 b))
8949 (atypes (nth 2 a)) (btypes (nth 2 b))
8950 ;; System routines?
8951 (asysp (memq akey '(builtin system)))
8952 (bsysp (memq bkey '(builtin system)))
8953 ;; Compiled routines?
8954 (acompp (memq 'compiled atypes))
8955 (bcompp (memq 'compiled btypes))
8956 ;; Unresolved?
8957 (aunresp (or (eq akey 'unresolved)
8958 (and acompp (not afile))))
8959 (bunresp (or (eq bkey 'unresolved)
8960 (and bcompp (not bfile))))
8961 ;; Buffer info available?
8962 (abufp (memq 'buffer atypes))
8963 (bbufp (memq 'buffer btypes))
8964 ;; On search path?
8965 (tpath-alist (idlwave-true-path-alist))
8966 (apathp (and (stringp akey)
8967 (assoc (file-name-directory akey) tpath-alist)))
8968 (bpathp (and (stringp bkey)
8969 (assoc (file-name-directory bkey) tpath-alist)))
8970 ;; How early on search path? High number means early since we
8971 ;; measure the tail of the path list
8972 (anpath (length (memq apathp tpath-alist)))
8973 (bnpath (length (memq bpathp tpath-alist)))
8974 ;; Look at file names
8975 (aname (if (stringp afile) (downcase (file-name-nondirectory afile)) ""))
8976 (bname (if (stringp bfile) (downcase (file-name-nondirectory bfile)) ""))
8977 (fname-re (if class (format "\\`%s__\\(%s\\|define\\)\\.pro\\'"
8978 (regexp-quote (downcase class))
8979 (regexp-quote (downcase name)))
8980 (format "\\`%s\\.pro" (regexp-quote (downcase name)))))
8981 ;; Is file name derived from the routine name?
8982 ;; Method file or class definition file?
8983 (anamep (string-match fname-re aname))
8984 (adefp (and class anamep (string= "define" (match-string 1 aname))))
8985 (bnamep (string-match fname-re bname))
8986 (bdefp (and class bnamep (string= "define" (match-string 1 bname)))))
8987
8988 ;; Now: follow JD's ideas about sorting. Looks really simple now,
8989 ;; doesn't it? The difficult stuff is hidden above...
8990 (cond
8991 ((idlwave-xor asysp bsysp) asysp) ; System entries first
8992 ((idlwave-xor aunresp bunresp) bunresp) ; Unresolved last
8993 ((and idlwave-sort-prefer-buffer-info
8994 (idlwave-xor abufp bbufp)) abufp) ; Buffers before non-buffers
8995 ((idlwave-xor acompp bcompp) acompp) ; Compiled entries
8996 ((idlwave-xor apathp bpathp) apathp) ; Library before non-library
8997 ((idlwave-xor anamep bnamep) anamep) ; Correct file names first
8998 ((and class anamep bnamep ; both file names match ->
8999 (idlwave-xor adefp bdefp)) bdefp) ; __define after __method
9000 ((> anpath bnpath) t) ; Who is first on path?
9001 (t nil)))) ; Default
9002
9003 (defun idlwave-routine-source-file (source)
9004 (if (nth 2 source)
9005 (expand-file-name (nth 1 source) (nth 2 source))
9006 (nth 1 source)))
9007
9008 (defun idlwave-downcase-safe (string)
9009 "Donwcase if string, else return unchanged."
9010 (if (stringp string)
9011 (downcase string)
9012 string))
9013
9014 (defun idlwave-count-eq (elt list)
9015 "How often is ELT in LIST?"
9016 (length (delq nil (mapcar (lambda (x) (eq x elt)) list))))
9017
9018 (defun idlwave-count-memq (elt alist)
9019 "How often is ELT a key in ALIST?"
9020 (length (delq nil (mapcar (lambda (x) (eq (car x) elt)) alist))))
9021
9022 (defun idlwave-syslib-p (file)
9023 "Non-nil if FILE is in the system library."
9024 (let* ((true-syslib (file-name-as-directory
9025 (file-truename
9026 (expand-file-name "lib" (idlwave-sys-dir)))))
9027 (true-file (file-truename file)))
9028 (string-match (concat "^" (regexp-quote true-syslib)) true-file)))
9029
9030 (defun idlwave-lib-p (file)
9031 "Non-nil if file is in the library"
9032 (let ((true-dir (file-name-directory (file-truename file))))
9033 (assoc true-dir (idlwave-true-path-alist))))
9034
9035 (defun idlwave-path-alist-add-flag (list-entry flag)
9036 "Add a flag to the path list entry, if not set."
9037 (let ((flags (cdr list-entry)))
9038 (add-to-list 'flags flag)
9039 (setcdr list-entry flags)))
9040
9041 (defun idlwave-path-alist-remove-flag (list-entry flag)
9042 "Remove a flag to the path list entry, if set."
9043 (let ((flags (delq flag (cdr list-entry))))
9044 (setcdr list-entry flags)))
9045
9046 (defun idlwave-true-path-alist ()
9047 "Return `idlwave-path-alist' alist with true-names.
9048 Info is cached, but relies on the functions setting `idlwave-path-alist'
9049 to reset the variable `idlwave-true-path-alist' to nil."
9050 (or idlwave-true-path-alist
9051 (setq idlwave-true-path-alist
9052 (mapcar (lambda(x) (cons
9053 (file-name-as-directory
9054 (file-truename
9055 (directory-file-name
9056 (car x))))
9057 (cdr x)))
9058 idlwave-path-alist))))
9059
9060 (defun idlwave-syslib-scanned-p ()
9061 "Non-nil if the system lib file !DIR/lib has been scanned."
9062 (let* ((true-syslib (file-name-as-directory
9063 (file-truename
9064 (expand-file-name "lib" (idlwave-sys-dir))))))
9065 (cdr (assoc true-syslib (idlwave-true-path-alist)))))
9066
9067 ;; ----------------------------------------------------------------------------
9068 ;;
9069 ;; Online Help display
9070
9071
9072 ;; ----------------------------------------------------------------------------
9073 ;;
9074 ;; Additions for use with imenu.el and func-menu.el
9075 ;; (pop-up a list of IDL units in the current file).
9076 ;;
9077
9078 (defun idlwave-prev-index-position ()
9079 "Search for the previous procedure or function.
9080 Return nil if not found. For use with imenu.el."
9081 (save-match-data
9082 (cond
9083 ((idlwave-find-key "\\<\\(pro\\|function\\)\\>" -1 'nomark))
9084 ;; ((idlwave-find-key idlwave-begin-unit-reg 1 'nomark)
9085 (t nil))))
9086
9087 (defun idlwave-unit-name ()
9088 "Return the unit name.
9089 Assumes that point is at the beginning of the unit as found by
9090 `idlwave-prev-index-position'."
9091 (forward-sexp 2)
9092 (forward-sexp -1)
9093 (let ((begin (point)))
9094 (re-search-forward
9095 "[a-zA-Z_][a-zA-Z0-9$_]+\\(::[a-zA-Z_][a-zA-Z0-9$_]+\\)?")
9096 (if (fboundp 'buffer-substring-no-properties)
9097 (buffer-substring-no-properties begin (point))
9098 (buffer-substring begin (point)))))
9099
9100 (defalias 'idlwave-function-menu
9101 (condition-case nil
9102 (progn
9103 (require 'func-menu)
9104 'function-menu)
9105 (error (condition-case nil
9106 (progn
9107 (require 'imenu)
9108 'imenu)
9109 (error nil)))))
9110
9111 ;; Here we hack func-menu.el in order to support this new mode.
9112 ;; The latest versions of func-menu.el already have this stuff in, so
9113 ;; we hack only if it is not already there.
9114 (when (fboundp 'eval-after-load)
9115 (eval-after-load "func-menu"
9116 '(progn
9117 (or (assq 'idlwave-mode fume-function-name-regexp-alist)
9118 (not (boundp 'fume-function-name-regexp-idl)) ; avoid problems
9119 (setq fume-function-name-regexp-alist
9120 (cons '(idlwave-mode . fume-function-name-regexp-idl)
9121 fume-function-name-regexp-alist)))
9122 (or (assq 'idlwave-mode fume-find-function-name-method-alist)
9123 (not (fboundp 'fume-find-next-idl-function-name)) ; avoid problems
9124 (setq fume-find-function-name-method-alist
9125 (cons '(idlwave-mode . fume-find-next-idl-function-name)
9126 fume-find-function-name-method-alist))))))
9127
9128 (defun idlwave-edit-in-idlde ()
9129 "Edit the current file in IDL Development environment."
9130 (interactive)
9131 (start-process "idldeclient" nil
9132 idlwave-shell-explicit-file-name "-c" "-e"
9133 (buffer-file-name)))
9134
9135 (defvar idlwave-help-use-assistant)
9136 (defun idlwave-launch-idlhelp ()
9137 "Start the IDLhelp application."
9138 (interactive)
9139 (if idlwave-help-use-assistant
9140 (idlwave-help-assistant-raise)
9141 (start-process "idlhelp" nil idlwave-help-application)))
9142
9143 ;; Menus - using easymenu.el
9144 (defvar idlwave-mode-menu-def
9145 `("IDLWAVE"
9146 ["PRO/FUNC menu" idlwave-function-menu t]
9147 ("Motion"
9148 ["Subprogram Start" idlwave-beginning-of-subprogram t]
9149 ["Subprogram End" idlwave-end-of-subprogram t]
9150 ["Block Start" idlwave-beginning-of-block t]
9151 ["Block End" idlwave-end-of-block t]
9152 ["Up Block" idlwave-backward-up-block t]
9153 ["Down Block" idlwave-down-block t]
9154 ["Skip Block Backward" idlwave-backward-block t]
9155 ["Skip Block Forward" idlwave-forward-block t])
9156 ("Mark"
9157 ["Subprogram" idlwave-mark-subprogram t]
9158 ["Block" idlwave-mark-block t]
9159 ["Header" idlwave-mark-doclib t])
9160 ("Format"
9161 ["Indent Entire Statement" idlwave-indent-statement
9162 :active t :keys "C-u \\[indent-for-tab-command]" ]
9163 ["Indent Subprogram" idlwave-indent-subprogram t]
9164 ["(Un)Comment Region" idlwave-toggle-comment-region t]
9165 ["Continue/Split line" idlwave-split-line t]
9166 "--"
9167 ["Toggle Auto Fill" idlwave-auto-fill-mode :style toggle
9168 :selected (symbol-value idlwave-fill-function)])
9169 ("Templates"
9170 ["Procedure" idlwave-procedure t]
9171 ["Function" idlwave-function t]
9172 ["Doc Header" idlwave-doc-header t]
9173 ["Log" idlwave-doc-modification t]
9174 "--"
9175 ["Case" idlwave-case t]
9176 ["For" idlwave-for t]
9177 ["Repeat" idlwave-repeat t]
9178 ["While" idlwave-while t]
9179 "--"
9180 ["Close Block" idlwave-close-block t])
9181 ("Completion"
9182 ["Complete" idlwave-complete t]
9183 ("Complete Specific"
9184 ["1 Procedure Name" (idlwave-complete 'procedure) t]
9185 ["2 Procedure Keyword" (idlwave-complete 'procedure-keyword) t]
9186 "--"
9187 ["3 Function Name" (idlwave-complete 'function) t]
9188 ["4 Function Keyword" (idlwave-complete 'function-keyword) t]
9189 "--"
9190 ["5 Procedure Method Name" (idlwave-complete 'procedure-method) t]
9191 ["6 Procedure Method Keyword" (idlwave-complete 'procedure-method-keyword) t]
9192 "--"
9193 ["7 Function Method Name" (idlwave-complete 'function-method) t]
9194 ["8 Function Method Keyword" (idlwave-complete 'function-method-keyword) t]
9195 "--"
9196 ["9 Class Name" idlwave-complete-class t]))
9197 ("Routine Info"
9198 ["Show Routine Info" idlwave-routine-info t]
9199 ["Online Context Help" idlwave-context-help t]
9200 "--"
9201 ["Find Routine Source" idlwave-find-module t]
9202 ["Resolve Routine" idlwave-resolve (featurep 'idlw-shell)]
9203 "--"
9204 ["Update Routine Info" idlwave-update-routine-info t]
9205 ["Rescan XML Help Catalog" idlwave-convert-xml-system-routine-info t]
9206 "--"
9207 "IDL User Catalog"
9208 ["Select Catalog Directories" (idlwave-create-user-catalog-file nil) t]
9209 ["Scan Directories" (idlwave-update-routine-info '(16))
9210 (and idlwave-path-alist (not idlwave-catalog-process))]
9211 ["Scan Directories &" (idlwave-update-routine-info '(64))
9212 (and idlwave-path-alist (not idlwave-catalog-process))]
9213 "--"
9214 "Routine Shadows"
9215 ["Check Current Buffer" idlwave-list-buffer-load-path-shadows t]
9216 ["Check Compiled Routines" idlwave-list-shell-load-path-shadows t]
9217 ["Check Everything" idlwave-list-all-load-path-shadows t])
9218 ("Misc"
9219 ["Kill auto-created buffers" idlwave-kill-autoloaded-buffers t]
9220 "--"
9221 ["Insert TAB character" idlwave-hard-tab t])
9222 "--"
9223 ("External"
9224 ["Start IDL shell" idlwave-shell t]
9225 ["Edit file in IDLDE" idlwave-edit-in-idlde t]
9226 ["Launch IDL Help" idlwave-launch-idlhelp t])
9227 "--"
9228 ("Customize"
9229 ["Browse IDLWAVE Group" idlwave-customize t]
9230 "--"
9231 ["Build Full Customize Menu" idlwave-create-customize-menu
9232 (fboundp 'customize-menu-create)])
9233 ("Documentation"
9234 ["Describe Mode" describe-mode t]
9235 ["Abbreviation List" idlwave-list-abbrevs t]
9236 "--"
9237 ["Commentary in idlwave.el" idlwave-show-commentary t]
9238 ["Commentary in idlw-shell.el" idlwave-shell-show-commentary t]
9239 "--"
9240 ["Info" idlwave-info t]
9241 "--"
9242 ["Help with Topic" idlwave-help-assistant-help-with-topic
9243 idlwave-help-use-assistant]
9244 ["Launch IDL Help" idlwave-launch-idlhelp t])))
9245
9246 (defvar idlwave-mode-debug-menu-def
9247 '("Debug"
9248 ["Start IDL shell" idlwave-shell t]
9249 ["Save and .RUN buffer" idlwave-shell-save-and-run
9250 (and (boundp 'idlwave-shell-automatic-start)
9251 idlwave-shell-automatic-start)]))
9252
9253 (if (or (featurep 'easymenu) (load "easymenu" t))
9254 (progn
9255 (easy-menu-define idlwave-mode-menu idlwave-mode-map
9256 "IDL and WAVE CL editing menu"
9257 idlwave-mode-menu-def)
9258 (easy-menu-define idlwave-mode-debug-menu idlwave-mode-map
9259 "IDL and WAVE CL editing menu"
9260 idlwave-mode-debug-menu-def)))
9261
9262 (defun idlwave-customize ()
9263 "Call the customize function with idlwave as argument."
9264 (interactive)
9265 ;; Try to load the code for the shell, so that we can customize it
9266 ;; as well.
9267 (or (featurep 'idlw-shell)
9268 (load "idlw-shell" t))
9269 (customize-browse 'idlwave))
9270
9271 (defun idlwave-create-customize-menu ()
9272 "Create a full customization menu for IDLWAVE, insert it into the menu."
9273 (interactive)
9274 (if (fboundp 'customize-menu-create)
9275 (progn
9276 ;; Try to load the code for the shell, so that we can customize it
9277 ;; as well.
9278 (or (featurep 'idlw-shell)
9279 (load "idlw-shell" t))
9280 (easy-menu-change
9281 '("IDLWAVE") "Customize"
9282 `(["Browse IDLWAVE group" idlwave-customize t]
9283 "--"
9284 ,(customize-menu-create 'idlwave)
9285 ["Set" Custom-set t]
9286 ["Save" Custom-save t]
9287 ["Reset to Current" Custom-reset-current t]
9288 ["Reset to Saved" Custom-reset-saved t]
9289 ["Reset to Standard Settings" Custom-reset-standard t]))
9290 (message "\"IDLWAVE\"-menu now contains full customization menu"))
9291 (error "Cannot expand menu (outdated version of cus-edit.el)")))
9292
9293 (defun idlwave-show-commentary ()
9294 "Use the finder to view the file documentation from `idlwave.el'."
9295 (interactive)
9296 (require 'finder)
9297 (finder-commentary "idlwave.el"))
9298
9299 (defun idlwave-shell-show-commentary ()
9300 "Use the finder to view the file documentation from `idlw-shell.el'."
9301 (interactive)
9302 (require 'finder)
9303 (finder-commentary "idlw-shell.el"))
9304
9305 (defun idlwave-info ()
9306 "Read documentation for IDLWAVE in the info system."
9307 (interactive)
9308 (require 'info)
9309 (Info-goto-node "(idlwave)"))
9310
9311 (defun idlwave-list-abbrevs (arg)
9312 "Show the code abbreviations define in IDLWAVE mode.
9313 This lists all abbrevs where the replacement text differs from the input text.
9314 These are the ones the users want to learn to speed up their writing.
9315
9316 The function does *not* list abbrevs which replace a word with itself
9317 to call a hook. These hooks are used to change the case of words or
9318 to blink the matching `begin', and the user does not need to know them.
9319
9320 With arg, list all abbrevs with the corresponding hook.
9321
9322 This function was written since `list-abbrevs' looks terrible for IDLWAVE mode."
9323
9324 (interactive "P")
9325 (let ((table (symbol-value 'idlwave-mode-abbrev-table))
9326 abbrevs
9327 str rpl func fmt (len-str 0) (len-rpl 0))
9328 (mapatoms
9329 (lambda (sym)
9330 (if (symbol-value sym)
9331 (progn
9332 (setq str (symbol-name sym)
9333 rpl (symbol-value sym)
9334 func (symbol-function sym))
9335 (if arg
9336 (setq func (prin1-to-string func))
9337 (if (and (listp func) (stringp (nth 2 func)))
9338 (setq rpl (concat "EVAL: " (nth 2 func))
9339 func "")
9340 (setq func "")))
9341 (if (or arg (not (string= rpl str)))
9342 (progn
9343 (setq len-str (max len-str (length str)))
9344 (setq len-rpl (max len-rpl (length rpl)))
9345 (setq abbrevs (cons (list str rpl func) abbrevs)))))))
9346 table)
9347 ;; sort the list
9348 (setq abbrevs (sort abbrevs (lambda (a b) (string< (car a) (car b)))))
9349 ;; Make the format
9350 (setq fmt (format "%%-%ds %%-%ds %%s\n" len-str len-rpl))
9351 (with-output-to-temp-buffer "*Help*"
9352 (if arg
9353 (progn
9354 (princ "Abbreviations and Actions in IDLWAVE-Mode\n")
9355 (princ "=========================================\n\n")
9356 (princ (format fmt "KEY" "REPLACE" "HOOK"))
9357 (princ (format fmt "---" "-------" "----")))
9358 (princ "Code Abbreviations and Templates in IDLWAVE-Mode\n")
9359 (princ "================================================\n\n")
9360 (princ (format fmt "KEY" "ACTION" ""))
9361 (princ (format fmt "---" "------" "")))
9362 (mapcar
9363 (lambda (list)
9364 (setq str (car list)
9365 rpl (nth 1 list)
9366 func (nth 2 list))
9367 (princ (format fmt str rpl func)))
9368 abbrevs)))
9369 ;; Make sure each abbreviation uses only one display line
9370 (save-excursion
9371 (set-buffer "*Help*")
9372 (setq truncate-lines t)))
9373
9374 ;; Add .pro files to speedbar for support, if it's loaded
9375 (eval-after-load "speedbar" '(speedbar-add-supported-extension ".pro"))
9376
9377 ;; Set an idle timer to load the routine info.
9378 ;; Will only work on systems which support this.
9379 (or idlwave-routines (idlwave-start-load-rinfo-timer))
9380
9381 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.[Pp][Rr][Oo]\\'" . idlwave-mode))
9382
9383 ;; Run the hook
9384 (run-hooks 'idlwave-load-hook)
9385
9386 (provide 'idlwave)
9387
9388 ;; arch-tag: f77f3b0c-c37c-424f-a328-0886fd42b6fb
9389 ;;; idlwave.el ends here