]> 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 (fill-nobreak-predicate
3491 (if (and (idlwave-in-quote)
3492 idlwave-auto-fill-split-string)
3493 (lambda () ;; We'll need 5 spaces for " ' + $"
3494 (<= (- fill-column (current-column)) 5)
3495 ))))
3496 (do-auto-fill))
3497 (save-excursion
3498 (end-of-line 0)
3499 ;; Indent the split line
3500 (idlwave-indent-line)
3501 )
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 (sysvar (substring (cdr (assq 'name nameblock)) 1))
4847 (link (cdr (assq 'link nameblock)))
4848 (params (cddr xml-entry))
4849 (case-fold-search t)
4850 pelem ptype props fields tags)
4851 (while params
4852 (setq pelem (car params))
4853 (when (listp pelem)
4854 (setq ptype (car pelem)
4855 props (car (cdr pelem)))
4856 (cond
4857 ((eq ptype 'FIELD)
4858 (push (cons (cdr (assq 'name props))
4859 (cdr
4860 (idlwave-split-link-target (cdr (assq 'link props)))))
4861 tags))))
4862 (setq params (cdr params)))
4863 (delq nil
4864 (list sysvar (if tags (cons 'tags tags)) (list 'link link)))))
4865
4866
4867 (defvar idlwave-xml-routine-info-file nil)
4868
4869 (defun idlwave-save-routine-info ()
4870 (if idlwave-xml-routine-info-file
4871 (with-temp-file idlwave-xml-system-rinfo-converted-file
4872 (insert
4873 (concat ";; *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
4874 ;; IDLWAVE Routine Information File (IDLWAVE version " idlwave-mode-version ")
4875 ;; Automatically generated from source file:
4876 ;; " idlwave-xml-routine-info-file "
4877 ;; on " (current-time-string) "
4878 ;; Do not edit."))
4879 (insert (format "\n(setq idlwave-xml-routine-info-file \n \"%s\")"
4880 idlwave-xml-routine-info-file))
4881 (insert "\n(setq idlwave-system-routines\n '")
4882 (prin1 idlwave-system-routines (current-buffer))
4883 (insert ")")
4884 (insert "\n(setq idlwave-system-variables-alist\n '")
4885 (prin1 idlwave-system-variables-alist (current-buffer))
4886 (insert ")")
4887 (insert "\n(setq idlwave-system-class-info\n '")
4888 (prin1 idlwave-system-class-info (current-buffer))
4889 (insert ")")
4890 (insert "\n(setq idlwave-executive-commands-alist\n '")
4891 (prin1 idlwave-executive-commands-alist (current-buffer))
4892 (insert ")")
4893 (insert "\n(setq idlwave-help-special-topic-words\n '")
4894 (prin1 idlwave-help-special-topic-words (current-buffer))
4895 (insert ")"))))
4896
4897 (defun idlwave-convert-xml-system-routine-info ()
4898 "Convert XML supplied IDL routine info into internal form.
4899 Cache to disk for quick recovery."
4900 (interactive)
4901 (let* ((dir (file-name-as-directory
4902 (expand-file-name "help/online_help" (idlwave-sys-dir))))
4903 (catalog-file (expand-file-name "idl_catalog.xml" dir))
4904 (elem-cnt 0)
4905 props rinfo msg-cnt elem type nelem class-result alias
4906 routines routine-aliases statement-aliases sysvar-aliases
4907 version-string)
4908 (if (not (file-exists-p catalog-file))
4909 (error "No such XML routine info file: %s" catalog-file)
4910 (if (not (file-readable-p catalog-file))
4911 (error "Cannot read XML routine info file: %s" catalog-file)))
4912 (require 'xml)
4913 (message "Reading XML routine info...")
4914 (setq rinfo (xml-parse-file catalog-file))
4915 (message "Reading XML routine info...done")
4916 (setq rinfo (assq 'CATALOG rinfo))
4917 (unless rinfo (error "Failed to parse XML routine info"))
4918 ;;(setq rinfo (car rinfo)) ; Skip the catalog stuff.
4919
4920 (setq version-string (cdr (assq 'version (nth 1 rinfo)))
4921 rinfo (cddr rinfo))
4922
4923 (setq nelem (length rinfo)
4924 msg-cnt (/ nelem 20))
4925
4926 (setq idlwave-xml-routine-info-file nil)
4927 (message "Converting XML routine info...")
4928 (setq idlwave-system-routines nil
4929 idlwave-system-variables-alist nil
4930 idlwave-system-class-info nil
4931 idlwave-executive-commands-alist nil
4932 idlwave-help-special-topic-words nil)
4933
4934 (while rinfo
4935 (setq elem (car rinfo)
4936 rinfo (cdr rinfo))
4937 (incf elem-cnt)
4938 (when (listp elem)
4939 (setq type (car elem)
4940 props (car (cdr elem)))
4941 (if (= (mod elem-cnt msg-cnt) 0)
4942 (message "Converting XML routine info...%2d%%"
4943 (/ (* elem-cnt 100) nelem)))
4944 (cond
4945 ((eq type 'ROUTINE)
4946 (if (setq alias (assq 'alias_to props))
4947 (push (cons (cdr (assq 'name props)) (cdr alias))
4948 routine-aliases)
4949 (setq routines (idlwave-xml-create-rinfo-list elem))
4950 (if (listp (cdr routines))
4951 (setq idlwave-system-routines
4952 (nconc idlwave-system-routines routines))
4953 ;; a cons cell is an executive commands
4954 (push routines idlwave-executive-commands-alist))))
4955
4956 ((eq type 'CLASS)
4957 (setq class-result (idlwave-xml-create-class-method-lists elem))
4958 (push (car class-result) idlwave-system-class-info)
4959 (setq idlwave-system-routines
4960 (nconc idlwave-system-routines (cdr class-result))))
4961
4962 ((eq type 'STATEMENT)
4963 (push (cons (cdr (assq 'name props))
4964 (cdr (assq 'link props)))
4965 idlwave-help-special-topic-words)
4966 ;; Save the links to those which are statement aliases (not routines)
4967 (if (setq alias (assq 'alias_to props))
4968 (unless (member (cdr alias) statement-aliases)
4969 (push (cdr alias) statement-aliases))))
4970
4971 ((eq type 'SYSVAR)
4972 (if (setq alias (cdr (assq 'alias_to props)))
4973 (push (cons (substring (cdr (assq 'name props)) 1)
4974 (substring alias 1))
4975 sysvar-aliases)
4976 (push (idlwave-xml-create-sysvar-alist elem)
4977 idlwave-system-variables-alist)))
4978 (t))))
4979 (idlwave-convert-xml-clean-routine-aliases routine-aliases)
4980 (idlwave-convert-xml-clean-statement-aliases statement-aliases)
4981 (idlwave-convert-xml-clean-sysvar-aliases sysvar-aliases)
4982
4983 (setq idlwave-xml-routine-info-file catalog-file)
4984 (idlwave-save-routine-info)
4985 (message "Converting XML routine info...done")))
4986
4987
4988 ;; ("ROUTINE" type class
4989 ;; (system) | (lib pro_file dir "LIBNAME") | (user pro_file dir "USERLIB") |
4990 ;; (buffer pro_file dir) | (compiled pro_file dir)
4991 ;; "calling_string" ("HELPFILE" (("KWD1" . link1) ...))
4992 ;; ("HELPFILE2" (("KWD2" . link) ...)) ...)
4993
4994
4995 (defun idlwave-load-rinfo-next-step ()
4996 (let ((inhibit-quit t)
4997 (arr idlwave-load-rinfo-steps-done))
4998 (if (catch 'exit
4999 (when (not (aref arr 0))
5000 (message "Loading system routine info in idle time...")
5001 (idlwave-load-system-routine-info)
5002 ;;(load "idlw-rinfo" 'noerror 'nomessage)
5003 (message "Loading system routine info in idle time...done")
5004 (aset arr 0 t)
5005 (throw 'exit t))
5006
5007 (when (not (aref arr 1))
5008 (message "Normalizing idlwave-system-routines in idle time...")
5009 (idlwave-reset-sintern t)
5010 (put 'idlwave-reset-sintern 'done-by-idle t)
5011 (setq idlwave-system-routines
5012 (idlwave-sintern-rinfo-list idlwave-system-routines 'sys))
5013 (message "Normalizing idlwave-system-routines in idle time...done")
5014 (aset arr 1 t)
5015 (throw 'exit t))
5016
5017 (when (not (aref arr 2))
5018 (when (and (stringp idlwave-user-catalog-file)
5019 (file-regular-p idlwave-user-catalog-file))
5020 (message "Loading user catalog in idle time...")
5021 (condition-case nil
5022 (load-file idlwave-user-catalog-file)
5023 (error (throw 'exit nil)))
5024 ;; Check for the old style catalog and warn
5025 (if (and
5026 (boundp 'idlwave-library-routines)
5027 idlwave-library-routines)
5028 (progn
5029 (setq idlwave-library-routines nil)
5030 (ding)
5031 (message "Outdated user catalog: %s... recreate"
5032 idlwave-user-catalog-file))
5033 (message "Loading user catalog in idle time...done")))
5034 (aset arr 2 t)
5035 (throw 'exit t))
5036
5037 (when (not (aref arr 3))
5038 (when idlwave-user-catalog-routines
5039 (message "Normalizing user catalog routines in idle time...")
5040 (setq idlwave-user-catalog-routines
5041 (idlwave-sintern-rinfo-list
5042 idlwave-user-catalog-routines 'sys))
5043 (message
5044 "Normalizing user catalog routines in idle time...done"))
5045 (aset arr 3 t)
5046 (throw 'exit t))
5047
5048 (when (not (aref arr 4))
5049 (idlwave-scan-library-catalogs
5050 "Loading and normalizing library catalogs in idle time...")
5051 (aset arr 4 t)
5052 (throw 'exit t))
5053 (when (not (aref arr 5))
5054 (message "Finishing initialization in idle time...")
5055 (idlwave-routines)
5056 (message "Finishing initialization in idle time...done")
5057 (aset arr 5 t)
5058 (throw 'exit nil)))
5059 ;; restart the timer
5060 (if (sit-for 1)
5061 (idlwave-load-rinfo-next-step)
5062 (setq idlwave-load-rinfo-idle-timer
5063 (run-with-idle-timer
5064 idlwave-init-rinfo-when-idle-after
5065 nil 'idlwave-load-rinfo-next-step))))))
5066
5067 (defun idlwave-load-all-rinfo (&optional force)
5068 ;; Load and case-treat the system, user catalog, and library routine
5069 ;; info files.
5070
5071 ;; System
5072 (when (or force (not (aref idlwave-load-rinfo-steps-done 0)))
5073 ;;(load "idlw-rinfo" 'noerror 'nomessage))
5074 (idlwave-load-system-routine-info))
5075 (when (or force (not (aref idlwave-load-rinfo-steps-done 1)))
5076 (message "Normalizing idlwave-system-routines...")
5077 (setq idlwave-system-routines
5078 (idlwave-sintern-rinfo-list idlwave-system-routines 'sys))
5079 (message "Normalizing idlwave-system-routines...done"))
5080 (when idlwave-system-routines
5081 (setq idlwave-routines (copy-sequence idlwave-system-routines))
5082 (setq idlwave-last-system-routine-info-cons-cell
5083 (nthcdr (1- (length idlwave-routines)) idlwave-routines)))
5084
5085 ;; User catalog
5086 (when (and (stringp idlwave-user-catalog-file)
5087 (file-regular-p idlwave-user-catalog-file))
5088 (condition-case nil
5089 (when (or force (not (aref idlwave-load-rinfo-steps-done 2)))
5090 (load-file idlwave-user-catalog-file))
5091 (error nil))
5092 (when (and
5093 (boundp 'idlwave-library-routines)
5094 idlwave-library-routines)
5095 (setq idlwave-library-routines nil)
5096 (error "Outdated user catalog: %s... recreate"
5097 idlwave-user-catalog-file))
5098 (setq idlwave-true-path-alist nil)
5099 (when (or force (not (aref idlwave-load-rinfo-steps-done 3)))
5100 (message "Normalizing user catalog routines...")
5101 (setq idlwave-user-catalog-routines
5102 (idlwave-sintern-rinfo-list
5103 idlwave-user-catalog-routines 'sys))
5104 (message "Normalizing user catalog routines...done")))
5105
5106 ;; Library catalog
5107 (when (or force (not (aref idlwave-load-rinfo-steps-done 4)))
5108 (idlwave-scan-library-catalogs
5109 "Loading and normalizing library catalogs..."))
5110 (run-hooks 'idlwave-after-load-rinfo-hook))
5111
5112
5113 (defun idlwave-update-buffer-routine-info ()
5114 (let (res)
5115 (cond
5116 ((eq idlwave-scan-all-buffers-for-routine-info t)
5117 ;; Scan all buffers, current buffer last
5118 (message "Scanning all buffers...")
5119 (setq res (idlwave-get-routine-info-from-buffers
5120 (reverse (buffer-list)))))
5121 ((null idlwave-scan-all-buffers-for-routine-info)
5122 ;; Don't scan any buffers
5123 (setq res nil))
5124 (t
5125 ;; Just scan this buffer
5126 (if (eq major-mode 'idlwave-mode)
5127 (progn
5128 (message "Scanning current buffer...")
5129 (setq res (idlwave-get-routine-info-from-buffers
5130 (list (current-buffer))))))))
5131 ;; Put the result into the correct variable
5132 (setq idlwave-buffer-routines
5133 (idlwave-sintern-rinfo-list res 'set))))
5134
5135 (defun idlwave-concatenate-rinfo-lists (&optional quiet run-hook)
5136 "Put the different sources for routine information together."
5137 ;; The sequence here is important because earlier definitions shadow
5138 ;; later ones. We assume that if things in the buffers are newer
5139 ;; then in the shell of the system, they are meant to be different.
5140 (setcdr idlwave-last-system-routine-info-cons-cell
5141 (append idlwave-buffer-routines
5142 idlwave-compiled-routines
5143 idlwave-library-catalog-routines
5144 idlwave-user-catalog-routines))
5145 (setq idlwave-class-alist nil)
5146
5147 ;; Give a message with information about the number of routines we have.
5148 (unless quiet
5149 (message
5150 "Routines Found: buffer(%d) compiled(%d) library(%d) user(%d) system(%d)"
5151 (length idlwave-buffer-routines)
5152 (length idlwave-compiled-routines)
5153 (length idlwave-library-catalog-routines)
5154 (length idlwave-user-catalog-routines)
5155 (length idlwave-system-routines)))
5156 (if run-hook
5157 (run-hooks 'idlwave-update-rinfo-hook)))
5158
5159 (defun idlwave-class-alist ()
5160 "Return the class alist - make it if necessary."
5161 (or idlwave-class-alist
5162 (let (class)
5163 (loop for x in idlwave-routines do
5164 (when (and (setq class (nth 2 x))
5165 (not (assq class idlwave-class-alist)))
5166 (push (list class) idlwave-class-alist)))
5167 idlwave-class-alist)))
5168
5169 ;; Three functions for the hooks
5170 (defun idlwave-save-buffer-update ()
5171 (idlwave-update-current-buffer-info 'save-buffer))
5172 (defun idlwave-kill-buffer-update ()
5173 (idlwave-update-current-buffer-info 'kill-buffer))
5174 (defun idlwave-new-buffer-update ()
5175 (idlwave-update-current-buffer-info 'find-file))
5176
5177 (defun idlwave-update-current-buffer-info (why)
5178 "Update idlwave-routines for current buffer. Can run from after-save-hook."
5179 (when (and (eq major-mode 'idlwave-mode)
5180 (or (eq t idlwave-auto-routine-info-updates)
5181 (memq why idlwave-auto-routine-info-updates))
5182 idlwave-scan-all-buffers-for-routine-info
5183 idlwave-routines)
5184 (condition-case nil
5185 (let (routines)
5186 (idlwave-replace-buffer-routine-info
5187 (buffer-file-name)
5188 (if (eq why 'kill-buffer)
5189 nil
5190 (setq routines
5191 (idlwave-sintern-rinfo-list
5192 (idlwave-get-routine-info-from-buffers
5193 (list (current-buffer))) 'set))))
5194 (idlwave-concatenate-rinfo-lists 'quiet)
5195 routines)
5196 (error nil))))
5197
5198 (defun idlwave-replace-buffer-routine-info (file new)
5199 "Cut the part from FILE out of `idlwave-buffer-routines' and add NEW."
5200 (let ((list idlwave-buffer-routines)
5201 found)
5202 (while list
5203 ;; The following test uses eq to make sure it works correctly
5204 ;; when two buffers visit the same file. Then the file names
5205 ;; will be equal, but not eq.
5206 (if (eq (idlwave-routine-source-file (nth 3 (car list))) file)
5207 (progn
5208 (setcar list nil)
5209 (setq found t))
5210 (if found
5211 ;; End of that section reached. Jump.
5212 (setq list nil)))
5213 (setq list (cdr list)))
5214 (setq idlwave-buffer-routines
5215 (append new (delq nil idlwave-buffer-routines)))))
5216
5217 ;;----- Scanning buffers -------------------
5218
5219 (defun idlwave-get-routine-info-from-buffers (buffers)
5220 "Call `idlwave-get-buffer-routine-info' on idlwave-mode buffers in BUFFERS."
5221 (let (buf routine-lists res)
5222 (save-excursion
5223 (while (setq buf (pop buffers))
5224 (set-buffer buf)
5225 (if (and (eq major-mode 'idlwave-mode)
5226 buffer-file-name)
5227 ;; yes, this buffer has the right mode.
5228 (progn (setq res (condition-case nil
5229 (idlwave-get-buffer-routine-info)
5230 (error nil)))
5231 (push res routine-lists)))))
5232 ;; Concatenate the individual lists and return the result
5233 (apply 'nconc routine-lists)))
5234
5235 (defun idlwave-get-buffer-routine-info ()
5236 "Scan the current buffer for routine info. Return (PRO-LIST FUNC-LIST)."
5237 (let* ((case-fold-search t)
5238 routine-list string entry)
5239 (save-excursion
5240 (save-restriction
5241 (widen)
5242 (goto-char (point-min))
5243 (while (re-search-forward
5244 "^[ \t]*\\(pro\\|function\\)[ \t]" nil t)
5245 (setq string (buffer-substring-no-properties
5246 (match-beginning 0)
5247 (progn
5248 (idlwave-end-of-statement)
5249 (point))))
5250 (setq entry (idlwave-parse-definition string))
5251 (push entry routine-list))))
5252 routine-list))
5253
5254 (defvar idlwave-scanning-lib-dir)
5255 (defun idlwave-parse-definition (string)
5256 "Parse a module definition."
5257 (let ((case-fold-search t)
5258 start name args type keywords class)
5259 ;; Remove comments
5260 (while (string-match ";.*" string)
5261 (setq string (replace-match "" t t string)))
5262 ;; Remove the continuation line stuff
5263 (while (string-match "\\([^a-zA-Z0-9$_]\\)\\$[ \t]*\n" string)
5264 (setq string (replace-match "\\1 " t nil string)))
5265 (while (string-match "\n" string)
5266 (setq string (replace-match " " t nil string)))
5267 ;; Match the name and type.
5268 (when (string-match
5269 "\\<\\(pro\\|function\\)\\>\\s-+\\(\\([a-zA-Z0-9$_]+\\)::\\)?\\([a-zA-Z0-9$_]+\\)" string)
5270 (setq start (match-end 0))
5271 (setq type (downcase (match-string 1 string)))
5272 (if (match-beginning 3)
5273 (setq class (match-string 3 string)))
5274 (setq name (match-string 4 string)))
5275 ;; Match normal args and keyword args
5276 (while (string-match
5277 ",\\s-*\\([a-zA-Z][a-zA-Z0-9$_]*\\|\\(_ref\\)?_extra\\)\\s-*\\(=\\)?"
5278 string start)
5279 (setq start (match-end 0))
5280 (if (match-beginning 3)
5281 (push (match-string 1 string) keywords)
5282 (push (match-string 1 string) args)))
5283 ;; Normalize and sort.
5284 (setq args (nreverse args))
5285 (setq keywords (sort keywords (lambda (a b)
5286 (string< (downcase a) (downcase b)))))
5287 ;; Make and return the entry
5288 ;; We don't know which argument are optional, so this information
5289 ;; will not be contained in the calling sequence.
5290 (list name
5291 (if (equal type "pro") 'pro 'fun)
5292 class
5293 (cond ((not (boundp 'idlwave-scanning-lib))
5294 (list 'buffer (buffer-file-name)))
5295 ; ((string= (downcase
5296 ; (file-name-sans-extension
5297 ; (file-name-nondirectory (buffer-file-name))))
5298 ; (downcase name))
5299 ; (list 'lib))
5300 ; (t (cons 'lib (file-name-nondirectory (buffer-file-name))))
5301 (t (list 'user (file-name-nondirectory (buffer-file-name))
5302 idlwave-scanning-lib-dir "UserLib")))
5303 (concat
5304 (if (string= type "function") "Result = " "")
5305 (if class "Obj ->[%s::]" "")
5306 "%s"
5307 (if args
5308 (concat
5309 (if (string= type "function") "(" ", ")
5310 (mapconcat 'identity args ", ")
5311 (if (string= type "function") ")" ""))))
5312 (if keywords
5313 (cons nil (mapcar 'list keywords)) ;No help file
5314 nil))))
5315
5316
5317 ;;----- Scanning the user catalog -------------------
5318
5319 (defun idlwave-sys-dir ()
5320 "Return the syslib directory, or a dummy that never matches."
5321 (cond
5322 ((and idlwave-system-directory
5323 (not (string= idlwave-system-directory "")))
5324 idlwave-system-directory)
5325 ((getenv "IDL_DIR"))
5326 (t "@@@@@@@@")))
5327
5328
5329 (defun idlwave-create-user-catalog-file (&optional arg)
5330 "Scan all files on selected dirs of IDL search path for routine information.
5331
5332 A widget checklist will allow you to choose the directories. Write
5333 the result as a file `idlwave-user-catalog-file'. When this file
5334 exists, will be automatically loaded to give routine information about
5335 library routines. With ARG, just rescan the same directories as last
5336 time - so no widget will pop up."
5337 (interactive "P")
5338 ;; Make sure the file is loaded if it exists.
5339 (if (and (stringp idlwave-user-catalog-file)
5340 (file-regular-p idlwave-user-catalog-file))
5341 (condition-case nil
5342 (load-file idlwave-user-catalog-file)
5343 (error nil)))
5344 ;; Make sure the file name makes sense
5345 (unless (and (stringp idlwave-user-catalog-file)
5346 (> (length idlwave-user-catalog-file) 0)
5347 (file-accessible-directory-p
5348 (file-name-directory idlwave-user-catalog-file))
5349 (not (string= "" (file-name-nondirectory
5350 idlwave-user-catalog-file))))
5351 (error "`idlwave-user-catalog-file' does not point to a file in an accessible directory"))
5352
5353 (cond
5354 ;; Rescan the known directories
5355 ((and arg idlwave-path-alist
5356 (consp (car idlwave-path-alist)))
5357 (idlwave-scan-user-lib-files idlwave-path-alist))
5358
5359 ;; Expand the directories from library-path and run the widget
5360 (idlwave-library-path
5361 (idlwave-display-user-catalog-widget
5362 (if idlwave-true-path-alist
5363 ;; Propagate any flags on the existing path-alist
5364 (mapcar (lambda (x)
5365 (let ((path-entry (assoc (file-truename x)
5366 idlwave-true-path-alist)))
5367 (if path-entry
5368 (cons x (cdr path-entry))
5369 (list x))))
5370 (idlwave-expand-path idlwave-library-path))
5371 (mapcar 'list (idlwave-expand-path idlwave-library-path)))))
5372
5373 ;; Ask the shell for the path and then run the widget
5374 (t
5375 (message "Asking the shell for IDL path...")
5376 (require 'idlw-shell)
5377 (idlwave-shell-send-command idlwave-shell-path-query
5378 '(idlwave-user-catalog-command-hook nil)
5379 'hide))))
5380
5381
5382 ;; Parse shell path information and select among it.
5383 (defun idlwave-user-catalog-command-hook (&optional arg)
5384 ;; Command hook used by `idlwave-create-user-catalog-file'.
5385 (if arg
5386 ;; Scan immediately
5387 (idlwave-scan-user-lib-files idlwave-path-alist)
5388 ;; Set the path and display the widget
5389 (idlwave-shell-get-path-info 'no-write) ; set to something path-alist
5390 (idlwave-scan-library-catalogs "Locating library catalogs..." 'no-load)
5391 (idlwave-display-user-catalog-widget idlwave-path-alist)))
5392
5393 (defconst idlwave-user-catalog-widget-help-string
5394 "This is the front-end to the creation of the IDLWAVE user catalog.
5395 Please select the directories on IDL's search path from which you
5396 would like to extract routine information, to be stored in the file:
5397
5398 %s
5399
5400 If this is not the correct file, first set variable
5401 `idlwave-user-catalog-file', and call this command again.
5402
5403 N.B. Many libraries include pre-scanned catalog files
5404 \(\".idlwave_catalog\"). These are marked with \"[LIB]\", and need
5405 not be scanned. You can scan your own libraries off-line using the
5406 perl script `idlwave_catalog'.
5407
5408 After selecting the directories, choose [Scan & Save] to scan the library
5409 directories and save the routine info.
5410 \n")
5411
5412 (defvar idlwave-widget)
5413 (defvar widget-keymap)
5414 (defun idlwave-display-user-catalog-widget (dirs-list)
5415 "Create the widget to select IDL search path directories for scanning."
5416 (interactive)
5417 (require 'widget)
5418 (require 'wid-edit)
5419 (unless dirs-list
5420 (error "Don't know IDL's search path"))
5421
5422 (kill-buffer (get-buffer-create "*IDLWAVE Widget*"))
5423 (switch-to-buffer (get-buffer-create "*IDLWAVE Widget*"))
5424 (kill-all-local-variables)
5425 (make-local-variable 'idlwave-widget)
5426 (widget-insert (format idlwave-user-catalog-widget-help-string
5427 idlwave-user-catalog-file))
5428
5429 (widget-create 'push-button
5430 :notify 'idlwave-widget-scan-user-lib-files
5431 "Scan & Save")
5432 (widget-insert " ")
5433 (widget-create 'push-button
5434 :notify 'idlwave-delete-user-catalog-file
5435 "Delete File")
5436 (widget-insert " ")
5437 (widget-create 'push-button
5438 :notify
5439 '(lambda (&rest ignore)
5440 (let ((path-list (widget-get idlwave-widget :path-dirs)))
5441 (mapcar (lambda (x)
5442 (unless (memq 'lib (cdr x))
5443 (idlwave-path-alist-add-flag x 'user)))
5444 path-list)
5445 (idlwave-display-user-catalog-widget path-list)))
5446 "Select All Non-Lib")
5447 (widget-insert " ")
5448 (widget-create 'push-button
5449 :notify
5450 '(lambda (&rest ignore)
5451 (let ((path-list (widget-get idlwave-widget :path-dirs)))
5452 (mapcar (lambda (x)
5453 (idlwave-path-alist-remove-flag x 'user))
5454 path-list)
5455 (idlwave-display-user-catalog-widget path-list)))
5456 "Deselect All")
5457 (widget-insert " ")
5458 (widget-create 'push-button
5459 :notify (lambda (&rest ignore)
5460 (kill-buffer (current-buffer)))
5461 "Quit")
5462 (widget-insert "\n\n")
5463
5464 (widget-insert "Select Directories: \n")
5465
5466 (setq idlwave-widget
5467 (apply 'widget-create
5468 'checklist
5469 :value (delq nil (mapcar (lambda (x)
5470 (if (memq 'user (cdr x))
5471 (car x)))
5472 dirs-list))
5473 :greedy t
5474 :tag "List of directories"
5475 (mapcar (lambda (x)
5476 (list 'item
5477 (if (memq 'lib (cdr x))
5478 (concat "[LIB] " (car x) )
5479 (car x)))) dirs-list)))
5480 (widget-put idlwave-widget :path-dirs dirs-list)
5481 (widget-insert "\n")
5482 (use-local-map widget-keymap)
5483 (widget-setup)
5484 (goto-char (point-min))
5485 (delete-other-windows))
5486
5487 (defun idlwave-delete-user-catalog-file (&rest ignore)
5488 (if (yes-or-no-p
5489 (format "Delete file %s " idlwave-user-catalog-file))
5490 (progn
5491 (delete-file idlwave-user-catalog-file)
5492 (message "%s has been deleted" idlwave-user-catalog-file))))
5493
5494 (defun idlwave-widget-scan-user-lib-files (&rest ignore)
5495 ;; Call `idlwave-scan-user-lib-files' with data taken from the widget.
5496 (let* ((widget idlwave-widget)
5497 (selected-dirs (widget-value widget))
5498 (path-alist (widget-get widget :path-dirs))
5499 (this-path-alist path-alist)
5500 dir-entry)
5501 (while (setq dir-entry (pop this-path-alist))
5502 (if (member
5503 (if (memq 'lib (cdr dir-entry))
5504 (concat "[LIB] " (car dir-entry))
5505 (car dir-entry))
5506 selected-dirs)
5507 (idlwave-path-alist-add-flag dir-entry 'user)
5508 (idlwave-path-alist-remove-flag dir-entry 'user)))
5509 (idlwave-scan-user-lib-files path-alist)))
5510
5511 (defvar font-lock-mode)
5512 (defun idlwave-scan-user-lib-files (path-alist)
5513 ;; Scan the PRO files in PATH-ALIST and store the info in the user catalog
5514 (let* ((idlwave-scanning-lib t)
5515 (idlwave-scanning-lib-dir "")
5516 (idlwave-completion-case nil)
5517 dirs-alist dir files file)
5518 (setq idlwave-user-catalog-routines nil
5519 idlwave-path-alist path-alist ; for library-path instead
5520 idlwave-true-path-alist nil)
5521 (if idlwave-auto-write-paths (idlwave-write-paths))
5522 (save-excursion
5523 (set-buffer (get-buffer-create "*idlwave-scan.pro*"))
5524 (idlwave-mode)
5525 (setq dirs-alist (reverse path-alist))
5526 (while (setq dir (pop dirs-alist))
5527 (when (memq 'user (cdr dir)) ; Has it marked for scan?
5528 (setq dir (car dir))
5529 (setq idlwave-scanning-lib-dir dir)
5530 (when (file-directory-p dir)
5531 (setq files (directory-files dir 'full "\\.[pP][rR][oO]\\'"))
5532 (while (setq file (pop files))
5533 (when (file-regular-p file)
5534 (if (not (file-readable-p file))
5535 (message "Skipping %s (no read permission)" file)
5536 (message "Scanning %s..." file)
5537 (erase-buffer)
5538 (insert-file-contents file 'visit)
5539 (setq idlwave-user-catalog-routines
5540 (append (idlwave-get-routine-info-from-buffers
5541 (list (current-buffer)))
5542 idlwave-user-catalog-routines)))))))))
5543 (message "Creating user catalog file...")
5544 (kill-buffer "*idlwave-scan.pro*")
5545 (kill-buffer (get-buffer-create "*IDLWAVE Widget*"))
5546 (let ((font-lock-maximum-size 0)
5547 (auto-mode-alist nil))
5548 (find-file idlwave-user-catalog-file))
5549 (if (and (boundp 'font-lock-mode)
5550 font-lock-mode)
5551 (font-lock-mode 0))
5552 (erase-buffer)
5553 (insert ";; IDLWAVE user catalog file\n")
5554 (insert (format ";; Created %s\n\n" (current-time-string)))
5555
5556 ;; Define the routine info list
5557 (insert "\n(setq idlwave-user-catalog-routines\n '(")
5558 (let ((standard-output (current-buffer)))
5559 (mapcar (lambda (x)
5560 (insert "\n ")
5561 (prin1 x)
5562 (goto-char (point-max)))
5563 idlwave-user-catalog-routines))
5564 (insert (format "))\n\n;;; %s ends here\n"
5565 (file-name-nondirectory idlwave-user-catalog-file)))
5566 (goto-char (point-min))
5567 ;; Save the buffer
5568 (save-buffer 0)
5569 (kill-buffer (current-buffer)))
5570 (message "Creating user catalog file...done")
5571 (message "Info for %d routines saved in %s"
5572 (length idlwave-user-catalog-routines)
5573 idlwave-user-catalog-file)
5574 (sit-for 2)
5575 (idlwave-update-routine-info t))
5576
5577 (defun idlwave-read-paths ()
5578 (if (and (stringp idlwave-path-file)
5579 (file-regular-p idlwave-path-file))
5580 (condition-case nil
5581 (load idlwave-path-file t t t)
5582 (error nil))))
5583
5584 (defun idlwave-write-paths ()
5585 (interactive)
5586 (when (and idlwave-path-alist idlwave-system-directory)
5587 (let ((font-lock-maximum-size 0)
5588 (auto-mode-alist nil))
5589 (find-file idlwave-path-file))
5590 (if (and (boundp 'font-lock-mode)
5591 font-lock-mode)
5592 (font-lock-mode 0))
5593 (erase-buffer)
5594 (insert ";; IDLWAVE paths\n")
5595 (insert (format ";; Created %s\n\n" (current-time-string)))
5596 ;; Define the variable which knows the value of "!DIR"
5597 (insert (format "\n(setq idlwave-system-directory \"%s\")\n"
5598 idlwave-system-directory))
5599
5600 ;; Define the variable which contains a list of all scanned directories
5601 (insert "\n(setq idlwave-path-alist\n '(")
5602 (let ((standard-output (current-buffer)))
5603 (mapcar (lambda (x)
5604 (insert "\n ")
5605 (prin1 x)
5606 (goto-char (point-max)))
5607 idlwave-path-alist))
5608 (insert "))\n")
5609 (save-buffer 0)
5610 (kill-buffer (current-buffer))))
5611
5612
5613 (defun idlwave-expand-path (path &optional default-dir)
5614 ;; Expand parts of path starting with '+' recursively into directory list.
5615 ;; Relative recursive path elements are expanded relative to DEFAULT-DIR.
5616 (message "Expanding path...")
5617 (let (path1 dir recursive)
5618 (while (setq dir (pop path))
5619 (if (setq recursive (string= (substring dir 0 1) "+"))
5620 (setq dir (substring dir 1)))
5621 (if (and recursive
5622 (not (file-name-absolute-p dir)))
5623 (setq dir (expand-file-name dir default-dir)))
5624 (if recursive
5625 ;; Expand recursively
5626 (setq path1 (append (idlwave-recursive-directory-list dir) path1))
5627 ;; Keep unchanged
5628 (push dir path1)))
5629 (message "Expanding path...done")
5630 (nreverse path1)))
5631
5632 (defun idlwave-recursive-directory-list (dir)
5633 ;; Return a list of all directories below DIR, including DIR itself
5634 (let ((path (list dir)) path1 file files)
5635 (while (setq dir (pop path))
5636 (when (file-directory-p dir)
5637 (setq files (nreverse (directory-files dir t "[^.]")))
5638 (while (setq file (pop files))
5639 (if (file-directory-p file)
5640 (push (file-name-as-directory file) path)))
5641 (push dir path1)))
5642 path1))
5643
5644
5645 ;;----- Scanning the library catalogs ------------------
5646
5647
5648
5649
5650 (defun idlwave-scan-library-catalogs (&optional message-base no-load)
5651 "Scan for library catalog files (.idlwave_catalog) and ingest.
5652
5653 All directories on `idlwave-path-alist' (or `idlwave-library-path'
5654 instead, if present) are searched. Print MESSAGE-BASE along with the
5655 libraries being loaded, if passed, and skip loading/normalizing if
5656 NO-LOAD is non-nil. The variable `idlwave-use-library-catalogs' can
5657 be set to nil to disable library catalog scanning."
5658 (when idlwave-use-library-catalogs
5659 (let ((dirs
5660 (if idlwave-library-path
5661 (idlwave-expand-path idlwave-library-path)
5662 (mapcar 'car idlwave-path-alist)))
5663 (old-libname "")
5664 dir-entry dir flags catalog all-routines)
5665 (if message-base (message message-base))
5666 (while (setq dir (pop dirs))
5667 (catch 'continue
5668 (when (file-readable-p
5669 (setq catalog (expand-file-name ".idlwave_catalog" dir)))
5670 (unless no-load
5671 (setq idlwave-library-catalog-routines nil)
5672 ;; Load the catalog file
5673 (condition-case nil
5674 (load catalog t t t)
5675 (error (throw 'continue t)))
5676 (when (and
5677 message-base
5678 (not (string= idlwave-library-catalog-libname
5679 old-libname)))
5680 (message "%s" (concat message-base
5681 idlwave-library-catalog-libname))
5682 (setq old-libname idlwave-library-catalog-libname))
5683 (when idlwave-library-catalog-routines
5684 (setq all-routines
5685 (append
5686 (idlwave-sintern-rinfo-list
5687 idlwave-library-catalog-routines 'sys dir)
5688 all-routines))))
5689
5690 ;; Add a 'lib flag if on path-alist
5691 (when (and idlwave-path-alist
5692 (setq dir-entry (assoc dir idlwave-path-alist)))
5693 (idlwave-path-alist-add-flag dir-entry 'lib)))))
5694 (unless no-load (setq idlwave-library-catalog-routines all-routines))
5695 (if message-base (message (concat message-base "done"))))))
5696
5697 ;;----- Communicating with the Shell -------------------
5698
5699 ;; First, here is the idl program which can be used to query IDL for
5700 ;; defined routines.
5701 (defconst idlwave-routine-info.pro
5702 "
5703 ;; START OF IDLWAVE SUPPORT ROUTINES
5704 pro idlwave_print_safe,item,limit
5705 catch,err
5706 if err ne 0 then begin
5707 print,'Could not print item.'
5708 return
5709 endif
5710 if n_elements(item) gt limit then $
5711 print,item[0:limit-1],'<... truncated at ',strtrim(limit,2),' elements>' $
5712 else print,item
5713 end
5714
5715 pro idlwave_print_info_entry,name,func=func,separator=sep
5716 ;; See if it's an object method
5717 if name eq '' then return
5718 func = keyword_set(func)
5719 methsep = strpos(name,'::')
5720 meth = methsep ne -1
5721
5722 ;; Get routine info
5723 pars = routine_info(name,/parameters,functions=func)
5724 source = routine_info(name,/source,functions=func)
5725 nargs = pars.num_args
5726 nkw = pars.num_kw_args
5727 if nargs gt 0 then args = pars.args
5728 if nkw gt 0 then kwargs = pars.kw_args
5729
5730 ;; Trim the class, and make the name
5731 if meth then begin
5732 class = strmid(name,0,methsep)
5733 name = strmid(name,methsep+2,strlen(name)-1)
5734 if nargs gt 0 then begin
5735 ;; remove the self argument
5736 wh = where(args ne 'SELF',nargs)
5737 if nargs gt 0 then args = args[wh]
5738 endif
5739 endif else begin
5740 ;; No class, just a normal routine.
5741 class = \"\"
5742 endelse
5743
5744 ;; Calling sequence
5745 cs = \"\"
5746 if func then cs = 'Result = '
5747 if meth then cs = cs + 'Obj -> [' + '%s' + '::]'
5748 cs = cs + '%s'
5749 if func then cs = cs + '(' else if nargs gt 0 then cs = cs + ', '
5750 if nargs gt 0 then begin
5751 for j=0,nargs-1 do begin
5752 cs = cs + args[j]
5753 if j lt nargs-1 then cs = cs + ', '
5754 endfor
5755 end
5756 if func then cs = cs + ')'
5757 ;; Keyword arguments
5758 kwstring = ''
5759 if nkw gt 0 then begin
5760 for j=0,nkw-1 do begin
5761 kwstring = kwstring + ' ' + kwargs[j]
5762 endfor
5763 endif
5764
5765 ret=(['IDLWAVE-PRO','IDLWAVE-FUN'])[func]
5766
5767 print,ret + ': ' + name + sep + class + sep + source[0].path $
5768 + sep + cs + sep + kwstring
5769 end
5770
5771 pro idlwave_routine_info,file
5772 on_error,1
5773 sep = '<@>'
5774 print,'>>>BEGIN OF IDLWAVE ROUTINE INFO (\"' + sep + '\" IS THE SEPARATOR)'
5775 all = routine_info()
5776 fileQ=n_elements(file) ne 0
5777 if fileQ then file=strtrim(file,2)
5778 for i=0L,n_elements(all)-1L do begin
5779 if fileQ then begin
5780 if (routine_info(all[i],/SOURCE)).path eq file then $
5781 idlwave_print_info_entry,all[i],separator=sep
5782 endif else idlwave_print_info_entry,all[i],separator=sep
5783 endfor
5784 all = routine_info(/functions)
5785 for i=0L,n_elements(all)-1L do begin
5786 if fileQ then begin
5787 if (routine_info(all[i],/FUNCTIONS,/SOURCE)).path eq file then $
5788 idlwave_print_info_entry,all[i],separator=sep,/FUNC
5789 endif else idlwave_print_info_entry,all[i],separator=sep,/FUNC
5790 endfor
5791 print,'>>>END OF IDLWAVE ROUTINE INFO'
5792 end
5793
5794 pro idlwave_get_sysvars
5795 on_error,1
5796 catch,error_status
5797 if error_status ne 0 then begin
5798 print, 'Cannot get info about system variables'
5799 endif else begin
5800 help,/brief,output=s,/system_variables ; ? unsafe use of OUTPUT=
5801 s = strtrim(strjoin(s,' ',/single),2) ; make one line
5802 v = strsplit(s,' +',/regex,/extract) ; get variables
5803 for i=0L,n_elements(v)-1 do begin
5804 t = [''] ; get tag list
5805 a=execute('if n_tags('+v[i]+') gt 0 then t=tag_names('+v[i]+')')
5806 print, 'IDLWAVE-SYSVAR: '+v[i]+' '+strjoin(t,' ',/single)
5807 endfor
5808 endelse
5809 end
5810
5811 pro idlwave_get_class_tags, class
5812 res = execute('tags=tag_names({'+class+'})')
5813 if res then print,'IDLWAVE-CLASS-TAGS: '+class+' '+strjoin(tags,' ',/single)
5814 end
5815 ;; END OF IDLWAVE SUPPORT ROUTINES
5816 "
5817 "The idl programs to get info from the shell.")
5818
5819 (defvar idlwave-idlwave_routine_info-compiled nil
5820 "Remembers if the routine info procedure is already compiled.")
5821
5822 (defvar idlwave-shell-temp-pro-file)
5823 (defvar idlwave-shell-temp-rinfo-save-file)
5824
5825 (defun idlwave-shell-compile-helper-routines (&optional wait)
5826 (unless (and idlwave-idlwave_routine_info-compiled
5827 (file-readable-p (idlwave-shell-temp-file 'rinfo)))
5828 (save-excursion
5829 (set-buffer (idlwave-find-file-noselect
5830 (idlwave-shell-temp-file 'pro)))
5831 (erase-buffer)
5832 (insert idlwave-routine-info.pro)
5833 (save-buffer 0))
5834 (idlwave-shell-send-command
5835 (concat ".run \"" idlwave-shell-temp-pro-file "\"")
5836 nil 'hide wait)
5837 (idlwave-shell-send-command
5838 (format "save,'idlwave_print_safe','idlwave_routine_info','idlwave_print_info_entry','idlwave_get_class_tags','idlwave_get_sysvars',FILE='%s',/ROUTINES"
5839 (idlwave-shell-temp-file 'rinfo))
5840 nil 'hide)
5841 (setq idlwave-idlwave_routine_info-compiled t))
5842
5843 ;; Restore if necessary. Must use execute to hide lame routine_info
5844 ;; errors on undefinded routine
5845 (idlwave-shell-send-command
5846 (format "if execute(\"_v=routine_info('idlwave_routine_info',/SOURCE)\") eq 0 then restore,'%s' else if _v.path eq '' then restore,'%s'"
5847 idlwave-shell-temp-rinfo-save-file
5848 idlwave-shell-temp-rinfo-save-file)
5849 nil 'hide))
5850
5851
5852 (defun idlwave-shell-update-routine-info (&optional quiet run-hooks wait file)
5853 "Query the shell for routine_info of compiled modules and update the lists."
5854 ;; Save and compile the procedure. The compiled procedure is then
5855 ;; saved into an IDL SAVE file, to allow for fast RESTORE. We may
5856 ;; need to test for and possibly RESTORE the procedure each time we
5857 ;; use it, since the user may have killed or redefined it. In
5858 ;; particular, .RESET_SESSION will kill all user procedures. If
5859 ;; FILE is set, only update routine info for routines in that file.
5860
5861 (idlwave-shell-compile-helper-routines wait)
5862 ; execute the routine_info procedure, and analyze the output
5863 (idlwave-shell-send-command
5864 (format "idlwave_routine_info%s" (if file (concat ",'" file "'") ""))
5865 `(progn
5866 (idlwave-shell-routine-info-filter)
5867 (idlwave-concatenate-rinfo-lists ,quiet ,run-hooks))
5868 'hide wait))
5869
5870 ;; ---------------------------------------------------------------------------
5871 ;;
5872 ;; Completion and displaying routine calling sequences
5873
5874 (defvar idlwave-completion-help-info nil)
5875 (defvar idlwave-completion-help-links nil)
5876 (defvar idlwave-current-obj_new-class nil)
5877 (defvar idlwave-complete-special nil)
5878
5879 (defun idlwave-complete (&optional arg module class)
5880 "Complete a function, procedure or keyword name at point.
5881 This function is smart and figures out what can be completed
5882 at this point.
5883 - At the beginning of a statement it completes procedure names.
5884 - In the middle of a statement it completes function names.
5885 - after a `(' or `,' in the argument list of a function or procedure,
5886 it completes a keyword of the relevant function or procedure.
5887 - In the first arg of `OBJ_NEW', it completes a class name.
5888
5889 When several completions are possible, a list will be displayed in the
5890 *Completions* buffer. If this list is too long to fit into the
5891 window, scrolling can be achieved by repeatedly pressing
5892 \\[idlwave-complete].
5893
5894 The function also knows about object methods. When it needs a class
5895 name, the action depends upon `idlwave-query-class', which see. You
5896 can force IDLWAVE to ask you for a class name with a
5897 \\[universal-argument] prefix argument to this command.
5898
5899 See also the variables `idlwave-keyword-completion-adds-equal' and
5900 `idlwave-function-completion-adds-paren'.
5901
5902 The optional ARG can be used to specify the completion type in order
5903 to override IDLWAVE's idea of what should be completed at point.
5904 Possible values are:
5905
5906 0 <=> query for the completion type
5907 1 <=> 'procedure
5908 2 <=> 'procedure-keyword
5909 3 <=> 'function
5910 4 <=> 'function-keyword
5911 5 <=> 'procedure-method
5912 6 <=> 'procedure-method-keyword
5913 7 <=> 'function-method
5914 8 <=> 'function-method-keyword
5915 9 <=> 'class
5916
5917 As a special case, the universal argument C-u forces completion of
5918 function names in places where the default would be a keyword.
5919
5920 Two prefix argument, C-u C-u, prompts for a regexp by which to limit
5921 completion.
5922
5923 For Lisp programmers only:
5924 When we force a keyword, optional argument MODULE can contain the module name.
5925 When we force a method or a method keyword, CLASS can specify the class."
5926 (interactive "P")
5927 (idlwave-routines)
5928 (let* ((where-list
5929 (if (and arg
5930 (or (and (integerp arg) (not (equal arg '(16))))
5931 (symbolp arg)))
5932 (idlwave-make-force-complete-where-list arg module class)
5933 (idlwave-where)))
5934 (what (nth 2 where-list))
5935 (idlwave-force-class-query (equal arg '(4)))
5936 (completion-regexp-list
5937 (if (equal arg '(16))
5938 (list (read-string (concat "Completion Regexp: "))))))
5939
5940 (if (and module (string-match "::" module))
5941 (setq class (substring module 0 (match-beginning 0))
5942 module (substring module (match-end 0))))
5943
5944 (cond
5945
5946 ((and (null arg)
5947 (eq (car-safe last-command) 'idlwave-display-completion-list)
5948 (get-buffer-window "*Completions*"))
5949 (setq this-command last-command)
5950 (idlwave-scroll-completions))
5951
5952 ;; Complete a filename in quotes
5953 ((and (idlwave-in-quote)
5954 (not (eq what 'class)))
5955 (idlwave-complete-filename))
5956
5957 ;; Check for any special completion functions
5958 ((and idlwave-complete-special
5959 (idlwave-call-special idlwave-complete-special)))
5960
5961 ((null what)
5962 (error "Nothing to complete here"))
5963
5964 ;; Complete a class
5965 ((eq what 'class)
5966 (setq idlwave-completion-help-info '(class))
5967 (idlwave-complete-class))
5968
5969 ((eq what 'procedure)
5970 ;; Complete a procedure name
5971 (let* ((cw-list (nth 3 where-list))
5972 (class-selector (idlwave-determine-class cw-list 'pro))
5973 (super-classes (unless (idlwave-explicit-class-listed cw-list)
5974 (idlwave-all-class-inherits class-selector)))
5975 (isa (concat "procedure" (if class-selector "-method" "")))
5976 (type-selector 'pro))
5977 (setq idlwave-completion-help-info
5978 (list 'routine nil type-selector class-selector nil super-classes))
5979 (idlwave-complete-in-buffer
5980 'procedure (if class-selector 'method 'routine)
5981 (idlwave-routines) 'idlwave-selector
5982 (format "Select a %s name%s"
5983 isa
5984 (if class-selector
5985 (format " (class is %s)"
5986 (if (eq class-selector t)
5987 "unknown" class-selector))
5988 ""))
5989 isa
5990 'idlwave-attach-method-classes 'idlwave-add-file-link-selector)))
5991
5992 ((eq what 'function)
5993 ;; Complete a function name
5994 (let* ((cw-list (nth 3 where-list))
5995 (class-selector (idlwave-determine-class cw-list 'fun))
5996 (super-classes (unless (idlwave-explicit-class-listed cw-list)
5997 (idlwave-all-class-inherits class-selector)))
5998 (isa (concat "function" (if class-selector "-method" "")))
5999 (type-selector 'fun))
6000 (setq idlwave-completion-help-info
6001 (list 'routine nil type-selector class-selector nil super-classes))
6002 (idlwave-complete-in-buffer
6003 'function (if class-selector 'method 'routine)
6004 (idlwave-routines) 'idlwave-selector
6005 (format "Select a %s name%s"
6006 isa
6007 (if class-selector
6008 (format " (class is %s)"
6009 (if (eq class-selector t)
6010 "unknown" class-selector))
6011 ""))
6012 isa
6013 'idlwave-attach-method-classes 'idlwave-add-file-link-selector)))
6014
6015 ((and (memq what '(procedure-keyword function-keyword)) ; Special Case
6016 (equal arg '(4)))
6017 (idlwave-complete 3))
6018
6019 ((eq what 'procedure-keyword)
6020 ;; Complete a procedure keyword
6021 (let* ((where (nth 3 where-list))
6022 (name (car where))
6023 (method-selector name)
6024 (type-selector 'pro)
6025 (class (idlwave-determine-class where 'pro))
6026 (class-selector class)
6027 (super-classes (idlwave-all-class-inherits class-selector))
6028 (isa (format "procedure%s-keyword" (if class "-method" "")))
6029 (entry (idlwave-best-rinfo-assq
6030 name 'pro class (idlwave-routines)))
6031 (system (if entry (eq (car (nth 3 entry)) 'system)))
6032 (list (idlwave-entry-keywords entry 'do-link)))
6033 (unless (or entry (eq class t))
6034 (error "Nothing known about procedure %s"
6035 (idlwave-make-full-name class name)))
6036 (setq list (idlwave-fix-keywords name 'pro class list
6037 super-classes system))
6038 (unless list (error "No keywords available for procedure %s"
6039 (idlwave-make-full-name class name)))
6040 (setq idlwave-completion-help-info
6041 (list 'keyword name type-selector class-selector entry super-classes))
6042 (idlwave-complete-in-buffer
6043 'keyword 'keyword list nil
6044 (format "Select keyword for procedure %s%s"
6045 (idlwave-make-full-name class name)
6046 (if (or (member '("_EXTRA") list)
6047 (member '("_REF_EXTRA") list))
6048 " (note _EXTRA)" ""))
6049 isa
6050 'idlwave-attach-keyword-classes)))
6051
6052 ((eq what 'function-keyword)
6053 ;; Complete a function keyword
6054 (let* ((where (nth 3 where-list))
6055 (name (car where))
6056 (method-selector name)
6057 (type-selector 'fun)
6058 (class (idlwave-determine-class where 'fun))
6059 (class-selector class)
6060 (super-classes (idlwave-all-class-inherits class-selector))
6061 (isa (format "function%s-keyword" (if class "-method" "")))
6062 (entry (idlwave-best-rinfo-assq
6063 name 'fun class (idlwave-routines)))
6064 (system (if entry (eq (car (nth 3 entry)) 'system)))
6065 (list (idlwave-entry-keywords entry 'do-link))
6066 msg-name)
6067 (unless (or entry (eq class t))
6068 (error "Nothing known about function %s"
6069 (idlwave-make-full-name class name)))
6070 (setq list (idlwave-fix-keywords name 'fun class list
6071 super-classes system))
6072 ;; OBJ_NEW: Messages mention the proper Init method
6073 (setq msg-name (if (and (null class)
6074 (string= (upcase name) "OBJ_NEW"))
6075 (concat idlwave-current-obj_new-class
6076 "::Init (via OBJ_NEW)")
6077 (idlwave-make-full-name class name)))
6078 (unless list (error "No keywords available for function %s"
6079 msg-name))
6080 (setq idlwave-completion-help-info
6081 (list 'keyword name type-selector class-selector nil super-classes))
6082 (idlwave-complete-in-buffer
6083 'keyword 'keyword list nil
6084 (format "Select keyword for function %s%s" msg-name
6085 (if (or (member '("_EXTRA") list)
6086 (member '("_REF_EXTRA") list))
6087 " (note _EXTRA)" ""))
6088 isa
6089 'idlwave-attach-keyword-classes)))
6090
6091 (t (error "This should not happen (idlwave-complete)")))))
6092
6093 (defvar idlwave-complete-special nil
6094 "List of special completion functions.
6095 These functions are called for each completion. Each function must
6096 check if its own special completion context is present. If yes, it
6097 should use `idlwave-complete-in-buffer' to do some completion and
6098 return t. If such a function returns t, *no further* attempts to
6099 complete other contexts will be done. If the function returns nil,
6100 other completions will be tried.")
6101
6102 (defun idlwave-call-special (functions &rest args)
6103 (let ((funcs functions)
6104 fun ret)
6105 (catch 'exit
6106 (while (setq fun (pop funcs))
6107 (if (setq ret (apply fun args))
6108 (throw 'exit ret)))
6109 nil)))
6110
6111 (defun idlwave-make-force-complete-where-list (what &optional module class)
6112 ;; Return an artificial WHERE specification to force the completion
6113 ;; routine to complete a specific item independent of context.
6114 ;; WHAT is the prefix arg of `idlwave-complete', see there for details.
6115 ;; MODULE and CLASS can be used to specify the routine name and class.
6116 ;; The class name will also be found in MODULE if that is like "class::mod".
6117 (let* ((what-list '(("procedure") ("procedure-keyword")
6118 ("function") ("function-keyword")
6119 ("procedure-method") ("procedure-method-keyword")
6120 ("function-method") ("function-method-keyword")
6121 ("class")))
6122 (module (idlwave-sintern-routine-or-method module class))
6123 (class (idlwave-sintern-class class))
6124 (what (cond
6125 ((equal what 0)
6126 (setq what
6127 (intern (completing-read
6128 "Complete what? " what-list nil t))))
6129 ((integerp what)
6130 (setq what (intern (car (nth (1- what) what-list)))))
6131 ((and what
6132 (symbolp what)
6133 (assoc (symbol-name what) what-list))
6134 what)
6135 (t (error "Invalid WHAT"))))
6136 (nil-list '(nil nil nil nil))
6137 (class-list (list nil nil (or class t) nil)))
6138
6139 (cond
6140
6141 ((eq what 'procedure)
6142 (list nil-list nil-list 'procedure nil-list nil))
6143
6144 ((eq what 'procedure-keyword)
6145 (let* ((class-selector nil)
6146 (super-classes nil)
6147 (type-selector 'pro)
6148 (pro (or module
6149 (idlwave-completing-read
6150 "Procedure: " (idlwave-routines) 'idlwave-selector))))
6151 (setq pro (idlwave-sintern-routine pro))
6152 (list nil-list nil-list 'procedure-keyword
6153 (list pro nil nil nil) nil)))
6154
6155 ((eq what 'function)
6156 (list nil-list nil-list 'function nil-list nil))
6157
6158 ((eq what 'function-keyword)
6159 (let* ((class-selector nil)
6160 (super-classes nil)
6161 (type-selector 'fun)
6162 (func (or module
6163 (idlwave-completing-read
6164 "Function: " (idlwave-routines) 'idlwave-selector))))
6165 (setq func (idlwave-sintern-routine func))
6166 (list nil-list nil-list 'function-keyword
6167 (list func nil nil nil) nil)))
6168
6169 ((eq what 'procedure-method)
6170 (list nil-list nil-list 'procedure class-list nil))
6171
6172 ((eq what 'procedure-method-keyword)
6173 (let* ((class (idlwave-determine-class class-list 'pro))
6174 (class-selector class)
6175 (super-classes (idlwave-all-class-inherits class-selector))
6176 (type-selector 'pro)
6177 (pro (or module
6178 (idlwave-completing-read
6179 (format "Procedure in %s class: " class-selector)
6180 (idlwave-routines) 'idlwave-selector))))
6181 (setq pro (idlwave-sintern-method pro))
6182 (list nil-list nil-list 'procedure-keyword
6183 (list pro nil class nil) nil)))
6184
6185 ((eq what 'function-method)
6186 (list nil-list nil-list 'function class-list nil))
6187
6188 ((eq what 'function-method-keyword)
6189 (let* ((class (idlwave-determine-class class-list 'fun))
6190 (class-selector class)
6191 (super-classes (idlwave-all-class-inherits class-selector))
6192 (type-selector 'fun)
6193 (func (or module
6194 (idlwave-completing-read
6195 (format "Function in %s class: " class-selector)
6196 (idlwave-routines) 'idlwave-selector))))
6197 (setq func (idlwave-sintern-method func))
6198 (list nil-list nil-list 'function-keyword
6199 (list func nil class nil) nil)))
6200
6201 ((eq what 'class)
6202 (list nil-list nil-list 'class nil-list nil))
6203
6204 (t (error "Invalid value for WHAT")))))
6205
6206 (defun idlwave-completing-read (&rest args)
6207 ;; Completing read, case insensitive
6208 (let ((old-value (default-value 'completion-ignore-case)))
6209 (unwind-protect
6210 (progn
6211 (setq-default completion-ignore-case t)
6212 (apply 'completing-read args))
6213 (setq-default completion-ignore-case old-value))))
6214
6215 (defvar idlwave-shell-default-directory)
6216 (defun idlwave-complete-filename ()
6217 "Use the comint stuff to complete a file name."
6218 (require 'comint)
6219 (let* ((comint-file-name-chars "~/A-Za-z0-9+@:_.$#%={}\\-")
6220 (comint-completion-addsuffix nil)
6221 (default-directory
6222 (if (and (boundp 'idlwave-shell-default-directory)
6223 (stringp idlwave-shell-default-directory)
6224 (file-directory-p idlwave-shell-default-directory))
6225 idlwave-shell-default-directory
6226 default-directory)))
6227 (comint-dynamic-complete-filename)))
6228
6229 (defun idlwave-make-full-name (class name)
6230 ;; Make a fully qualified module name including the class name
6231 (concat (if class (format "%s::" class) "") name))
6232
6233 (defun idlwave-rinfo-assoc (name type class list)
6234 "Like `idlwave-rinfo-assq', but sintern strings first."
6235 (idlwave-rinfo-assq
6236 (idlwave-sintern-routine-or-method name class)
6237 type (idlwave-sintern-class class) list))
6238
6239 (defun idlwave-rinfo-assq (name type class list)
6240 ;; Works like assq, but also checks type and class
6241 (catch 'exit
6242 (let (match)
6243 (while (setq match (assq name list))
6244 (and (or (eq type t)
6245 (eq (nth 1 match) type))
6246 (eq (nth 2 match) class)
6247 (throw 'exit match))
6248 (setq list (cdr (memq match list)))))))
6249
6250 (defun idlwave-rinfo-assq-any-class (name type class list)
6251 ;; Return the first matching method on the inheritance list
6252 (let* ((classes (cons class (idlwave-all-class-inherits class)))
6253 class rtn)
6254 (while classes
6255 (if (setq rtn (idlwave-rinfo-assq name type (pop classes) list))
6256 (setq classes nil)))
6257 rtn))
6258
6259 (defun idlwave-best-rinfo-assq (name type class list &optional with-file
6260 keep-system)
6261 "Like `idlwave-rinfo-assq', but get all twins and sort, then return first.
6262 If WITH-FILE is passed, find the best rinfo entry with a file
6263 included. If KEEP-SYSTEM is set, don't prune system for compiled
6264 syslib files."
6265 (let ((twins (idlwave-routine-twins
6266 (idlwave-rinfo-assq-any-class name type class list)
6267 list))
6268 syslibp)
6269 (when (> (length twins) 1)
6270 (setq twins (sort twins 'idlwave-routine-entry-compare-twins))
6271 (if (and (null keep-system)
6272 (eq 'system (car (nth 3 (car twins))))
6273 (setq syslibp (idlwave-any-syslib (cdr twins)))
6274 (not (equal 1 syslibp)))
6275 ;; Its a compiled syslib, so we need to remove the system entry
6276 (setq twins (cdr twins)))
6277 (if with-file
6278 (setq twins (delq nil
6279 (mapcar (lambda (x)
6280 (if (nth 1 (nth 3 x)) x))
6281 twins)))))
6282 (car twins)))
6283
6284 (defun idlwave-best-rinfo-assoc (name type class list &optional with-file
6285 keep-system)
6286 "Like `idlwave-best-rinfo-assq', but sintern strings first."
6287 (idlwave-best-rinfo-assq
6288 (idlwave-sintern-routine-or-method name class)
6289 type (idlwave-sintern-class class) list with-file keep-system))
6290
6291 (defun idlwave-any-syslib (entries)
6292 "Does the entry list ENTRIES contain a syslib entry?
6293 If yes, return the index (>=1)."
6294 (let (file (cnt 0))
6295 (catch 'exit
6296 (while entries
6297 (incf cnt)
6298 (setq file (idlwave-routine-source-file (nth 3 (car entries))))
6299 (if (and file (idlwave-syslib-p file))
6300 (throw 'exit cnt)
6301 (setq entries (cdr entries))))
6302 nil)))
6303
6304 (defun idlwave-all-assq (key list)
6305 "Return a list of all associations of Key in LIST."
6306 (let (rtn elt)
6307 (while (setq elt (assq key list))
6308 (push elt rtn)
6309 (setq list (cdr (memq elt list))))
6310 (nreverse rtn)))
6311
6312 (defun idlwave-all-method-classes (method &optional type)
6313 "Return all classes which have a method METHOD. TYPE is 'fun or 'pro.
6314 When TYPE is not specified, both procedures and functions will be considered."
6315 (if (null method)
6316 (mapcar 'car (idlwave-class-alist))
6317 (let (rtn)
6318 (mapcar (lambda (x)
6319 (and (nth 2 x)
6320 (or (not type)
6321 (eq type (nth 1 x)))
6322 (push (nth 2 x) rtn)))
6323 (idlwave-all-assq method (idlwave-routines)))
6324 (idlwave-uniquify rtn))))
6325
6326 (defun idlwave-all-method-keyword-classes (method keyword &optional type)
6327 "Return all classes which have a method METHOD with keyword KEYWORD.
6328 TYPE is 'fun or 'pro.
6329 When TYPE is not specified, both procedures and functions will be considered."
6330 (if (or (null method)
6331 (null keyword))
6332 nil
6333 (let (rtn)
6334 (mapcar (lambda (x)
6335 (and (nth 2 x) ; non-nil class
6336 (or (not type) ; correct or unspecified type
6337 (eq type (nth 1 x)))
6338 (assoc keyword (idlwave-entry-keywords x))
6339 (push (nth 2 x) rtn)))
6340 (idlwave-all-assq method (idlwave-routines)))
6341 (idlwave-uniquify rtn))))
6342
6343 (defun idlwave-members-only (list club)
6344 "Return list of all elements in LIST which are also in CLUB."
6345 (let (rtn)
6346 (while list
6347 (if (member (car list) club)
6348 (setq rtn (cons (car list) rtn)))
6349 (setq list (cdr list)))
6350 (nreverse rtn)))
6351
6352 (defun idlwave-nonmembers-only (list club)
6353 "Return list of all elements in LIST which are not in CLUB."
6354 (let (rtn)
6355 (while list
6356 (if (member (car list) club)
6357 nil
6358 (setq rtn (cons (car list) rtn)))
6359 (setq list (cdr list)))
6360 (nreverse rtn)))
6361
6362 (defun idlwave-explicit-class-listed (info)
6363 "Return whether or not the class is listed explicitly, ala a->b::c.
6364 INFO is as returned by idlwave-what-function or -procedure."
6365 (let ((apos (nth 3 info)))
6366 (if apos
6367 (save-excursion (goto-char apos)
6368 (looking-at "->[a-zA-Z][a-zA-Z0-9$_]*::")))))
6369
6370 (defvar idlwave-determine-class-special nil
6371 "List of special functions for determining class.
6372 Must accept two arguments: `apos' and `info'")
6373
6374 (defun idlwave-determine-class (info type)
6375 ;; Determine the class of a routine call.
6376 ;; INFO is the `cw-list' structure as returned by idlwave-where.
6377 ;; The second element in this structure is the class. When nil, we
6378 ;; return nil. When t, try to get the class from text properties at
6379 ;; the arrow. When the object is "self", we use the class of the
6380 ;; current routine. otherwise prompt the user for a class name.
6381 ;; Also stores the selected class as a text property at the arrow.
6382 ;; TYPE is 'fun or 'pro.
6383 (let* ((class (nth 2 info))
6384 (apos (nth 3 info))
6385 (nassoc (assoc (if (stringp (car info))
6386 (upcase (car info))
6387 (car info))
6388 idlwave-query-class))
6389 (dassoc (assq (if (car info) 'keyword-default 'method-default)
6390 idlwave-query-class))
6391 (query (cond (nassoc (cdr nassoc))
6392 (dassoc (cdr dassoc))
6393 (t t)))
6394 (arrow (and apos (string= (buffer-substring apos (+ 2 apos)) "->")))
6395 (is-self
6396 (and arrow
6397 (save-excursion (goto-char apos)
6398 (forward-word -1)
6399 (let ((case-fold-search t))
6400 (looking-at "self\\>")))))
6401 (force-query idlwave-force-class-query)
6402 store special-class class-alist)
6403 (cond
6404 ((null class) nil)
6405 ((eq t class)
6406 ;; There is an object which would like to know its class
6407 (if (and arrow (get-text-property apos 'idlwave-class)
6408 idlwave-store-inquired-class
6409 (not force-query))
6410 (setq class (get-text-property apos 'idlwave-class)
6411 class (idlwave-sintern-class class)))
6412 (if (and (eq t class) is-self)
6413 (setq class (or (nth 2 (idlwave-current-routine)) class)))
6414
6415 ;; Before prompting, try any special class determination routines
6416 (when (and (eq t class)
6417 idlwave-determine-class-special
6418 (not force-query))
6419 (setq special-class
6420 (idlwave-call-special idlwave-determine-class-special apos))
6421 (if special-class
6422 (setq class (idlwave-sintern-class special-class)
6423 store idlwave-store-inquired-class)))
6424
6425 ;; Prompt for a class, if we need to
6426 (when (and (eq class t)
6427 (or force-query query))
6428 (setq class-alist
6429 (mapcar 'list (idlwave-all-method-classes (car info) type)))
6430 (setq class
6431 (idlwave-sintern-class
6432 (cond
6433 ((and (= (length class-alist) 0) (not force-query))
6434 (error "No classes available with method %s" (car info)))
6435 ((and (= (length class-alist) 1) (not force-query))
6436 (car (car class-alist)))
6437 (t
6438 (setq store idlwave-store-inquired-class)
6439 (idlwave-completing-read
6440 (format "Class%s: " (if (stringp (car info))
6441 (format " for %s method %s"
6442 type (car info))
6443 ""))
6444 class-alist nil nil nil 'idlwave-class-history))))))
6445
6446 ;; Store it, if requested
6447 (when (and class (not (eq t class)))
6448 ;; We have a real class here
6449 (when (and store arrow)
6450 (condition-case ()
6451 (add-text-properties
6452 apos (+ apos 2)
6453 `(idlwave-class ,class face ,idlwave-class-arrow-face
6454 rear-nonsticky t))
6455 (error nil)))
6456 (setf (nth 2 info) class))
6457 ;; Return the class
6458 class)
6459 ;; Default as fallback
6460 (t class))))
6461
6462 (defvar type-selector)
6463 (defvar class-selector)
6464 (defvar method-selector)
6465 (defvar super-classes)
6466 (defun idlwave-selector (a)
6467 (and (eq (nth 1 a) type-selector)
6468 (or (and (nth 2 a) (eq class-selector t))
6469 (eq (nth 2 a) class-selector)
6470 (memq (nth 2 a) super-classes))))
6471
6472 (defun idlwave-add-file-link-selector (a)
6473 ;; Record a file link, if any, for the tested names during selection.
6474 (let ((sel (idlwave-selector a)) file)
6475 (if (and sel (setq file (idlwave-entry-has-help a)))
6476 (push (cons (car a) file) idlwave-completion-help-links))
6477 sel))
6478
6479
6480 (defun idlwave-where ()
6481 "Find out where we are.
6482 The return value is a list with the following stuff:
6483 \(PRO-LIST FUNC-LIST COMPLETE-WHAT CW-LIST LAST-CHAR)
6484
6485 PRO-LIST (PRO POINT CLASS ARROW)
6486 FUNC-LIST (FUNC POINT CLASS ARROW)
6487 COMPLETE-WHAT a symbol indicating what kind of completion makes sense here
6488 CW-LIST (PRO-OR-FUNC POINT CLASS ARROW) Like PRO-LIST, for what can
6489 be completed here.
6490 LAST-CHAR last relevant character before point (non-white non-comment,
6491 not part of current identifier or leading slash).
6492
6493 In the lists, we have these meanings:
6494 PRO: Procedure name
6495 FUNC: Function name
6496 POINT: Where is this
6497 CLASS: What class has the routine (nil=no, t=is method, but class unknown)
6498 ARROW: Location of the arrow"
6499 (idlwave-routines)
6500 (let* (;(bos (save-excursion (idlwave-beginning-of-statement) (point)))
6501 (bos (save-excursion (idlwave-start-of-substatement 'pre) (point)))
6502 (func-entry (idlwave-what-function bos))
6503 (func (car func-entry))
6504 (func-class (nth 1 func-entry))
6505 (func-arrow (nth 2 func-entry))
6506 (func-point (or (nth 3 func-entry) 0))
6507 (func-level (or (nth 4 func-entry) 0))
6508 (pro-entry (idlwave-what-procedure bos))
6509 (pro (car pro-entry))
6510 (pro-class (nth 1 pro-entry))
6511 (pro-arrow (nth 2 pro-entry))
6512 (pro-point (or (nth 3 pro-entry) 0))
6513 (last-char (idlwave-last-valid-char))
6514 (case-fold-search t)
6515 (match-string (buffer-substring bos (point)))
6516 cw cw-mod cw-arrow cw-class cw-point)
6517 (if (< func-point pro-point) (setq func nil))
6518 (cond
6519 ((string-match "\\`[ \t]*\\(pro\\|function\\)[ \t]+[a-zA-Z0-9_]*\\'"
6520 match-string)
6521 (setq cw 'class))
6522 ((string-match
6523 "\\`[ \t]*\\([a-zA-Z][a-zA-Z0-9$_]*\\)?\\'"
6524 (if (> pro-point 0)
6525 (buffer-substring pro-point (point))
6526 match-string))
6527 (setq cw 'procedure cw-class pro-class cw-point pro-point
6528 cw-arrow pro-arrow))
6529 ((string-match "\\`[ \t]*\\(pro\\|function\\)\\>"
6530 match-string)
6531 nil)
6532 ((string-match "OBJ_NEW([ \t]*['\"]\\([a-zA-Z0-9$_]*\\)?\\'"
6533 match-string)
6534 (setq cw 'class))
6535 ((string-match "\\<inherits\\s-+\\([a-zA-Z0-9$_]*\\)?\\'"
6536 match-string)
6537 (setq cw 'class))
6538 ((and func
6539 (> func-point pro-point)
6540 (= func-level 1)
6541 (memq last-char '(?\( ?,)))
6542 (setq cw 'function-keyword cw-mod func cw-point func-point
6543 cw-class func-class cw-arrow func-arrow))
6544 ((and pro (eq last-char ?,))
6545 (setq cw 'procedure-keyword cw-mod pro cw-point pro-point
6546 cw-class pro-class cw-arrow pro-arrow))
6547 ; ((member last-char '(?\' ?\) ?\] ?!))
6548 ; ;; after these chars, a function makes no sense
6549 ; ;; FIXME: I am sure there can be more in this list
6550 ; ;; FIXME: Do we want to do this at all?
6551 ; nil)
6552 ;; Everywhere else we try a function.
6553 (t
6554 (setq cw 'function)
6555 (save-excursion
6556 (if (re-search-backward "->[ \t]*\\(\\$[ \t]*\\(;.*\\)?\n\\s-*\\)?\\(\\([$a-zA-Z0-9_]+\\)::\\)?[$a-zA-Z0-9_]*\\=" bos t)
6557 (setq cw-arrow (copy-marker (match-beginning 0))
6558 cw-class (if (match-end 4)
6559 (idlwave-sintern-class (match-string 4))
6560 t))))))
6561 (list (list pro pro-point pro-class pro-arrow)
6562 (list func func-point func-class func-arrow)
6563 cw
6564 (list cw-mod cw-point cw-class cw-arrow)
6565 last-char)))
6566
6567 (defun idlwave-this-word (&optional class)
6568 ;; Grab the word around point. CLASS is for the `skip-chars=...' functions
6569 (setq class (or class "a-zA-Z0-9$_."))
6570 (save-excursion
6571 (buffer-substring
6572 (progn (skip-chars-backward class) (point))
6573 (progn (skip-chars-forward class) (point)))))
6574
6575 (defun idlwave-what-function (&optional bound)
6576 ;; Find out if point is within the argument list of a function.
6577 ;; The return value is ("function-name" class arrow-start (point) level).
6578 ;; Level is 1 on the top level parentheses, higher further down.
6579
6580 ;; If the optional BOUND is an integer, bound backwards directed
6581 ;; searches to this point.
6582
6583 (catch 'exit
6584 (let (pos
6585 func-point
6586 (cnt 0)
6587 func arrow-start class)
6588 (idlwave-with-special-syntax
6589 (save-restriction
6590 (save-excursion
6591 (narrow-to-region (max 1 (or bound 0)) (point-max))
6592 ;; move back out of the current parenthesis
6593 (while (condition-case nil
6594 (progn (up-list -1) t)
6595 (error nil))
6596 (setq pos (point))
6597 (incf cnt)
6598 (when (and (= (following-char) ?\()
6599 (re-search-backward
6600 "\\(::\\|\\<\\)\\([a-zA-Z][a-zA-Z0-9$_]*\\)[ \t]*\\="
6601 bound t))
6602 (setq func (match-string 2)
6603 func-point (goto-char (match-beginning 2))
6604 pos func-point)
6605 (if (re-search-backward
6606 "->[ \t]*\\(\\([a-zA-Z][a-zA-Z0-9$_]*\\)::\\)?\\=" bound t)
6607 (setq arrow-start (copy-marker (match-beginning 0))
6608 class (or (match-string 2) t)))
6609 (throw
6610 'exit
6611 (list
6612 (idlwave-sintern-routine-or-method func class)
6613 (idlwave-sintern-class class)
6614 arrow-start func-point cnt)))
6615 (goto-char pos))
6616 (throw 'exit nil)))))))
6617
6618 (defun idlwave-what-procedure (&optional bound)
6619 ;; Find out if point is within the argument list of a procedure.
6620 ;; The return value is ("procedure-name" class arrow-pos (point)).
6621
6622 ;; If the optional BOUND is an integer, bound backwards directed
6623 ;; searches to this point.
6624 (let ((pos (point)) pro-point
6625 pro class arrow-start string)
6626 (save-excursion
6627 ;;(idlwave-beginning-of-statement)
6628 (idlwave-start-of-substatement 'pre)
6629 (setq string (buffer-substring (point) pos))
6630 (if (string-match
6631 "\\`[ \t]*\\([a-zA-Z][a-zA-Z0-9$_]*\\)[ \t]*\\(,\\|\\'\\)" string)
6632 (setq pro (match-string 1 string)
6633 pro-point (+ (point) (match-beginning 1)))
6634 (if (and (idlwave-skip-object)
6635 (setq string (buffer-substring (point) pos))
6636 (string-match
6637 "\\`[ \t]*\\(->\\)[ \t]*\\(\\([a-zA-Z][a-zA-Z0-9$_]*\\)::\\)?\\([a-zA-Z][a-zA-Z0-9$_]*\\)?[ \t]*\\(,\\|\\(\\$\\s *\\(;.*\\)?\\)?$\\)"
6638 string))
6639 (setq pro (if (match-beginning 4)
6640 (match-string 4 string))
6641 pro-point (if (match-beginning 4)
6642 (+ (point) (match-beginning 4))
6643 pos)
6644 arrow-start (copy-marker (+ (point) (match-beginning 1)))
6645 class (or (match-string 3 string) t)))))
6646 (list (idlwave-sintern-routine-or-method pro class)
6647 (idlwave-sintern-class class)
6648 arrow-start
6649 pro-point)))
6650
6651 (defun idlwave-skip-object ()
6652 ;; If there is an object at point, move over it and return t.
6653 (let ((pos (point)))
6654 (if (catch 'exit
6655 (save-excursion
6656 (skip-chars-forward " ") ; white space
6657 (skip-chars-forward "*") ; de-reference
6658 (cond
6659 ((looking-at idlwave-identifier)
6660 (goto-char (match-end 0)))
6661 ((eq (following-char) ?\()
6662 nil)
6663 (t (throw 'exit nil)))
6664 (catch 'endwhile
6665 (while t
6666 (cond ((eq (following-char) ?.)
6667 (forward-char 1)
6668 (if (not (looking-at idlwave-identifier))
6669 (throw 'exit nil))
6670 (goto-char (match-end 0)))
6671 ((memq (following-char) '(?\( ?\[))
6672 (condition-case nil
6673 (forward-list 1)
6674 (error (throw 'exit nil))))
6675 (t (throw 'endwhile t)))))
6676 (if (looking-at "[ \t]*->")
6677 (throw 'exit (setq pos (match-beginning 0)))
6678 (throw 'exit nil))))
6679 (goto-char pos)
6680 nil)))
6681
6682 (defun idlwave-last-valid-char ()
6683 "Return the last character before point which is not white or a comment
6684 and also not part of the current identifier. Since we do this in
6685 order to identify places where keywords are, we consider the initial
6686 `/' of a keyword as part of the identifier.
6687 This function is not general, can only be used for completion stuff."
6688 (catch 'exit
6689 (save-excursion
6690 ;; skip the current identifier
6691 (skip-chars-backward "a-zA-Z0-9_$")
6692 ;; also skip a leading slash which might be belong to the keyword
6693 (if (eq (preceding-char) ?/)
6694 (backward-char 1))
6695 ;; FIXME: does not check if this is a valid identifier
6696 (while t
6697 (skip-chars-backward " \t")
6698 (cond
6699 ((memq (preceding-char) '(?\; ?\$)) (throw 'exit nil))
6700 ((eq (preceding-char) ?\n)
6701 (beginning-of-line 0)
6702 (if (looking-at "\\([^\n]*\\)\\$[ \t]*\\(;[^\n]*\\)?\n")
6703 ;; continuation line
6704 (goto-char (match-end 1))
6705 (throw 'exit nil)))
6706 (t (throw 'exit (preceding-char))))))))
6707
6708 (defvar idlwave-complete-after-success-form nil
6709 "A form to evaluate after successful completion.")
6710 (defvar idlwave-complete-after-success-form-force nil
6711 "A form to evaluate after completion selection in *Completions* buffer.")
6712 (defconst idlwave-completion-mark (make-marker)
6713 "A mark pointing to the beginning of the completion string.")
6714
6715 (defun idlwave-complete-in-buffer (type stype list selector prompt isa
6716 &optional prepare-display-function
6717 special-selector)
6718 "Perform TYPE completion of word before point against LIST.
6719 SELECTOR is the PREDICATE argument for the completion function. Show
6720 PROMPT in echo area. TYPE is one of the intern types, e.g. 'function,
6721 'procedure, 'class-tag, 'keyword, 'sysvar, etc.. SPECIAL-SELECTOR is
6722 used only once, for `all-completions', and can be used to, e.g.,
6723 accumulate information on matching completions."
6724 (let* ((completion-ignore-case t)
6725 beg (end (point)) slash part spart completion all-completions
6726 dpart dcompletion)
6727
6728 (unless list
6729 (error (concat prompt ": No completions available")))
6730
6731 ;; What is already in the buffer?
6732 (save-excursion
6733 (skip-chars-backward "a-zA-Z0-9_$")
6734 (setq slash (eq (preceding-char) ?/)
6735 beg (point)
6736 idlwave-complete-after-success-form
6737 (list 'idlwave-after-successful-completion
6738 (list 'quote type) slash beg)
6739 idlwave-complete-after-success-form-force
6740 (list 'idlwave-after-successful-completion
6741 (list 'quote type) slash (list 'quote 'force))))
6742
6743 ;; Try a completion
6744 (setq part (buffer-substring beg end)
6745 dpart (downcase part)
6746 spart (idlwave-sintern stype part)
6747 completion (try-completion part list selector)
6748 dcompletion (if (stringp completion) (downcase completion))
6749 idlwave-completion-help-links nil)
6750 (cond
6751 ((null completion)
6752 ;; nothing available.
6753 (error (concat prompt ": no completion for \"%s\"") part))
6754 ((and (not (equal dpart dcompletion))
6755 (not (eq t completion)))
6756 ;; We can add something
6757 (delete-region beg end)
6758 (if (and (string= part dpart)
6759 (or (not (string= part ""))
6760 idlwave-complete-empty-string-as-lower-case)
6761 (not idlwave-completion-force-default-case))
6762 (insert dcompletion)
6763 (insert completion))
6764 (if (eq t (try-completion completion list selector))
6765 ;; Now this is a unique match
6766 (idlwave-after-successful-completion type slash beg))
6767 t)
6768 ((or (eq completion t)
6769 (and (= 1 (length (setq all-completions
6770 (idlwave-uniquify
6771 (all-completions part list
6772 (or special-selector
6773 selector))))))
6774 (equal dpart dcompletion)))
6775 ;; This is already complete
6776 (idlwave-after-successful-completion type slash beg)
6777 (message "%s is already the complete %s" part isa)
6778 nil)
6779 (t
6780 ;; We cannot add something - offer a list.
6781 (message "Making completion list...")
6782
6783 (unless idlwave-completion-help-links ; already set somewhere?
6784 (mapcar (lambda (x) ; Pass link prop through to highlight-linked
6785 (let ((link (get-text-property 0 'link (car x))))
6786 (if link
6787 (push (cons (car x) link)
6788 idlwave-completion-help-links))))
6789 list))
6790 (let* ((list all-completions)
6791 ;; "complete" means, this is already a valid completion
6792 (complete (memq spart all-completions))
6793 (completion-highlight-first-word-only t)) ; XEmacs
6794 ; (completion-fixup-function ; Emacs
6795 ; (lambda () (and (eq (preceding-char) ?>)
6796 ; (re-search-backward " <" beg t)))))
6797
6798 (setq list (sort list (lambda (a b)
6799 (string< (downcase a) (downcase b)))))
6800 (if prepare-display-function
6801 (setq list (funcall prepare-display-function list)))
6802 (if (and (string= part dpart)
6803 (or (not (string= part ""))
6804 idlwave-complete-empty-string-as-lower-case)
6805 (not idlwave-completion-force-default-case))
6806 (setq list (mapcar (lambda (x)
6807 (if (listp x)
6808 (setcar x (downcase (car x)))
6809 (setq x (downcase x)))
6810 x)
6811 list)))
6812 (idlwave-display-completion-list list prompt beg complete))
6813 t))))
6814
6815 (defun idlwave-complete-class ()
6816 "Complete a class at point."
6817 (interactive)
6818 ;; Call `idlwave-routines' to make sure the class list will be available
6819 (idlwave-routines)
6820 ;; Check for the special case of completing empty string after pro/function
6821 (if (let ((case-fold-search t))
6822 (save-excursion
6823 (and
6824 (re-search-backward "\\<\\(pro\\|function\\)[ \t]+\\="
6825 (- (point) 15) t)
6826 (goto-char (point-min))
6827 (re-search-forward
6828 "^[ \t]*\\(pro\\|function\\)[ \t]+\\([a-zA-Z0-9_]+::\\)" nil t))))
6829 ;; Yank the full class specification
6830 (insert (match-string 2))
6831 ;; Do the completion, using list gathered from `idlwave-routines'
6832 (idlwave-complete-in-buffer
6833 'class 'class (idlwave-class-alist) nil
6834 "Select a class" "class"
6835 '(lambda (list) ;; Push it to help-links if system help available
6836 (mapcar (lambda (x)
6837 (let* ((entry (idlwave-class-info x))
6838 (link (nth 1 (assq 'link entry))))
6839 (if link (push (cons x link)
6840 idlwave-completion-help-links))
6841 x))
6842 list)))))
6843
6844 (defun idlwave-attach-classes (list type show-classes)
6845 ;; Attach the proper class list to a LIST of completion items.
6846 ;; TYPE, when 'kwd, shows classes for method keywords, when
6847 ;; 'class-tag, for class tags, and otherwise for methods.
6848 ;; SHOW-CLASSES is the value of `idlwave-completion-show-classes'.
6849 (if (or (null show-classes) ; don't want to see classes
6850 (null class-selector) ; not a method call
6851 (and
6852 (stringp class-selector) ; the class is already known
6853 (not super-classes))) ; no possibilities for inheritance
6854 ;; In these cases, we do not have to do anything
6855 list
6856 (let* ((do-prop (and (>= show-classes 0)
6857 (>= emacs-major-version 21)))
6858 (do-buf (not (= show-classes 0)))
6859 ;; (do-dots (featurep 'xemacs))
6860 (do-dots t)
6861 (inherit (if (and (not (eq type 'class-tag)) super-classes)
6862 (cons class-selector super-classes)))
6863 (max (abs show-classes))
6864 (lmax (if do-dots (apply 'max (mapcar 'length list))))
6865 classes nclasses class-info space)
6866 (mapcar
6867 (lambda (x)
6868 ;; get the classes
6869 (if (eq type 'class-tag)
6870 ;; Just one class for tags
6871 (setq classes
6872 (list
6873 (idlwave-class-or-superclass-with-tag class-selector x)))
6874 ;; Multiple classes for method or method-keyword
6875 (setq classes
6876 (if (eq type 'kwd)
6877 (idlwave-all-method-keyword-classes
6878 method-selector x type-selector)
6879 (idlwave-all-method-classes x type-selector)))
6880 (if inherit
6881 (setq classes
6882 (delq nil
6883 (mapcar (lambda (x) (if (memq x inherit) x nil))
6884 classes)))))
6885 (setq nclasses (length classes))
6886 ;; Make the separator between item and class-info
6887 (if do-dots
6888 (setq space (concat " " (make-string (- lmax (length x)) ?.)))
6889 (setq space " "))
6890 (if do-buf
6891 ;; We do want info in the buffer
6892 (if (<= nclasses max)
6893 (setq class-info (concat
6894 space
6895 "<" (mapconcat 'identity classes ",") ">"))
6896 (setq class-info (format "%s<%d classes>" space nclasses)))
6897 (setq class-info nil))
6898 (when do-prop
6899 ;; We do want properties
6900 (setq x (copy-sequence x))
6901 (put-text-property 0 (length x)
6902 'help-echo (mapconcat 'identity classes " ")
6903 x))
6904 (if class-info
6905 (list x class-info)
6906 x))
6907 list))))
6908
6909 (defun idlwave-attach-method-classes (list)
6910 ;; Call idlwave-attach-classes with method parameters
6911 (idlwave-attach-classes list 'method idlwave-completion-show-classes))
6912 (defun idlwave-attach-keyword-classes (list)
6913 ;; Call idlwave-attach-classes with keyword parameters
6914 (idlwave-attach-classes list 'kwd idlwave-completion-show-classes))
6915 (defun idlwave-attach-class-tag-classes (list)
6916 ;; Call idlwave-attach-classes with class structure tags
6917 (idlwave-attach-classes list 'class-tag idlwave-completion-show-classes))
6918
6919
6920 ;;----------------------------------------------------------------------
6921 ;;----------------------------------------------------------------------
6922 ;;----------------------------------------------------------------------
6923 ;;----------------------------------------------------------------------
6924 ;;----------------------------------------------------------------------
6925 (defvar rtn)
6926 (defun idlwave-pset (item)
6927 (set 'rtn item))
6928
6929 (defun idlwave-popup-select (ev list title &optional sort)
6930 "Select an item in LIST with a popup menu.
6931 TITLE is the title to put atop the popup. If SORT is non-nil,
6932 sort the list before displaying"
6933 (let ((maxpopup idlwave-max-popup-menu-items)
6934 rtn menu resp)
6935 (cond ((null list))
6936 ((= 1 (length list))
6937 (setq rtn (car list)))
6938 ((featurep 'xemacs)
6939 (if sort (setq list (sort list (lambda (a b)
6940 (string< (upcase a) (upcase b))))))
6941 (setq menu
6942 (append (list title)
6943 (mapcar (lambda (x) (vector x (list 'idlwave-pset
6944 x)))
6945 list)))
6946 (setq menu (idlwave-split-menu-xemacs menu maxpopup))
6947 (setq resp (get-popup-menu-response menu))
6948 (funcall (event-function resp) (event-object resp)))
6949 (t
6950 (if sort (setq list (sort list (lambda (a b)
6951 (string< (upcase a) (upcase b))))))
6952 (setq menu (cons title
6953 (list
6954 (append (list "")
6955 (mapcar (lambda(x) (cons x x)) list)))))
6956 (setq menu (idlwave-split-menu-emacs menu maxpopup))
6957 (setq rtn (x-popup-menu ev menu))))
6958 rtn))
6959
6960 (defun idlwave-split-menu-xemacs (menu N)
6961 "Split the MENU into submenus of maximum length N."
6962 (if (<= (length menu) (1+ N))
6963 ;; No splitting needed
6964 menu
6965 (let* ((title (car menu))
6966 (entries (cdr menu))
6967 (menu (list title))
6968 (cnt 0)
6969 (nextmenu nil))
6970 (while entries
6971 (while (and entries (< cnt N))
6972 (setq cnt (1+ cnt)
6973 nextmenu (cons (car entries) nextmenu)
6974 entries (cdr entries)))
6975 (setq nextmenu (nreverse nextmenu))
6976 (setq nextmenu (cons (format "%s...%s"
6977 (aref (car nextmenu) 0)
6978 (aref (nth (1- cnt) nextmenu) 0))
6979 nextmenu))
6980 (setq menu (cons nextmenu menu)
6981 nextmenu nil
6982 cnt 0))
6983 (nreverse menu))))
6984
6985 (defun idlwave-split-menu-emacs (menu N)
6986 "Split the MENU into submenus of maximum length N."
6987 (if (<= (length (nth 1 menu)) (1+ N))
6988 ;; No splitting needed
6989 menu
6990 (let* ((title (car menu))
6991 (entries (cdr (nth 1 menu)))
6992 (menu nil)
6993 (cnt 0)
6994 (nextmenu nil))
6995 (while entries
6996 (while (and entries (< cnt N))
6997 (setq cnt (1+ cnt)
6998 nextmenu (cons (car entries) nextmenu)
6999 entries (cdr entries)))
7000 (setq nextmenu (nreverse nextmenu))
7001 (prin1 nextmenu)
7002 (setq nextmenu (cons (format "%s...%s"
7003 (car (car nextmenu))
7004 (car (nth (1- cnt) nextmenu)))
7005 nextmenu))
7006 (setq menu (cons nextmenu menu)
7007 nextmenu nil
7008 cnt 0))
7009 (setq menu (nreverse menu))
7010 (setq menu (cons title menu))
7011 menu)))
7012
7013 (defvar idlwave-completion-setup-hook nil)
7014
7015 (defun idlwave-scroll-completions (&optional message)
7016 "Scroll the completion window on this frame."
7017 (let ((cwin (get-buffer-window "*Completions*" 'visible))
7018 (win (selected-window)))
7019 (unwind-protect
7020 (progn
7021 (select-window cwin)
7022 (condition-case nil
7023 (scroll-up)
7024 (error (if (and (listp last-command)
7025 (nth 2 last-command))
7026 (progn
7027 (select-window win)
7028 (eval idlwave-complete-after-success-form))
7029 (set-window-start cwin (point-min)))))
7030 (and message (message message)))
7031 (select-window win))))
7032
7033 (defun idlwave-display-completion-list (list &optional message beg complete)
7034 "Display the completions in LIST in the completions buffer and echo MESSAGE."
7035 (unless (and (get-buffer-window "*Completions*")
7036 (idlwave-local-value 'idlwave-completion-p "*Completions*"))
7037 (move-marker idlwave-completion-mark beg)
7038 (setq idlwave-before-completion-wconf (current-window-configuration)))
7039
7040 (if (featurep 'xemacs)
7041 (idlwave-display-completion-list-xemacs
7042 list)
7043 (idlwave-display-completion-list-emacs list))
7044
7045 ;; Store a special value in `this-command'. When `idlwave-complete'
7046 ;; finds this in `last-command', it will scroll the *Completions* buffer.
7047 (setq this-command (list 'idlwave-display-completion-list message complete))
7048
7049 ;; Mark the completions buffer as created by cib
7050 (idlwave-set-local 'idlwave-completion-p t "*Completions*")
7051
7052 ;; Fontify the classes
7053 (if (and idlwave-completion-fontify-classes
7054 (consp (car list)))
7055 (idlwave-completion-fontify-classes))
7056
7057 ;; Run the hook
7058 (run-hooks 'idlwave-completion-setup-hook)
7059
7060 ;; Display the message
7061 (message (or message "Making completion list...done")))
7062
7063 (defun idlwave-choose (function &rest args)
7064 "Call FUNCTION as a completion chooser and pass ARGS to it."
7065 (let ((completion-ignore-case t)) ; install correct value
7066 (apply function args))
7067 (if (and (eq major-mode 'idlwave-shell-mode)
7068 (boundp 'font-lock-mode)
7069 (not font-lock-mode))
7070 ;; For the shell, remove the fontification of the word before point
7071 (let ((beg (save-excursion
7072 (skip-chars-backward "a-zA-Z0-9_")
7073 (point))))
7074 (remove-text-properties beg (point) '(face nil))))
7075 (eval idlwave-complete-after-success-form-force))
7076
7077 (defun idlwave-keyboard-quit ()
7078 (interactive)
7079 (unwind-protect
7080 (if (eq (car-safe last-command) 'idlwave-display-completion-list)
7081 (idlwave-restore-wconf-after-completion))
7082 (keyboard-quit)))
7083
7084 (defun idlwave-restore-wconf-after-completion ()
7085 "Restore the old (before completion) window configuration."
7086 (and idlwave-completion-restore-window-configuration
7087 idlwave-before-completion-wconf
7088 (set-window-configuration idlwave-before-completion-wconf)))
7089
7090 (defun idlwave-one-key-select (sym prompt delay)
7091 "Make the user select an element from the alist in the variable SYM.
7092 The keys of the alist are expected to be strings. The function returns the
7093 car of the selected association.
7094 To do this, PROMPT is displayed and and the user must hit a letter key to
7095 select an entry. If the user does not reply within DELAY seconds, a help
7096 window with the options is displayed automatically.
7097 The key which is associated with each option is generated automatically.
7098 First, the strings are checked for preselected keys, like in \"[P]rint\".
7099 If these don't exist, a letter in the string is automatically selected."
7100 (let* ((alist (symbol-value sym))
7101 (temp-buffer-show-hook (if (fboundp 'fit-window-to-buffer)
7102 '(fit-window-to-buffer)))
7103 keys-alist char)
7104 ;; First check the cache
7105 (if (and (eq (symbol-value sym) (get sym :one-key-alist-last)))
7106 (setq keys-alist (get sym :one-key-alist-cache))
7107 ;; Need to make new list
7108 (setq keys-alist (idlwave-make-one-key-alist alist))
7109 (put sym :one-key-alist-cache keys-alist)
7110 (put sym :one-key-alist-last alist))
7111 ;; Display prompt and wait for quick reply
7112 (message "%s[%s]" prompt
7113 (mapconcat (lambda(x) (char-to-string (car x)))
7114 keys-alist ""))
7115 (if (sit-for delay)
7116 ;; No quick reply: Show help
7117 (save-window-excursion
7118 (with-output-to-temp-buffer "*Completions*"
7119 (mapcar (lambda(x)
7120 (princ (nth 1 x))
7121 (princ "\n"))
7122 keys-alist))
7123 (setq char (read-char)))
7124 (setq char (read-char)))
7125 (message nil)
7126 ;; Return the selected result
7127 (nth 2 (assoc char keys-alist))))
7128
7129 ;; Used for, e.g., electric debug super-examine.
7130 (defun idlwave-make-one-key-alist (alist)
7131 "Make an alist for single key selection."
7132 (let ((l alist) keys-alist name start char help
7133 (cnt 0)
7134 (case-fold-search nil))
7135 (while l
7136 (setq name (car (car l))
7137 l (cdr l))
7138 (catch 'exit
7139 ;; First check if the configuration predetermined a key
7140 (if (string-match "\\[\\(.\\)\\]" name)
7141 (progn
7142 (setq char (string-to-char (downcase (match-string 1 name)))
7143 help (format "%c: %s" char name)
7144 keys-alist (cons (list char help name) keys-alist))
7145 (throw 'exit t)))
7146 ;; Then check for capital letters
7147 (setq start 0)
7148 (while (string-match "[A-Z]" name start)
7149 (setq start (match-end 0)
7150 char (string-to-char (downcase (match-string 0 name))))
7151 (if (not (assoc char keys-alist))
7152 (progn
7153 (setq help (format "%c: %s" char
7154 (replace-match
7155 (concat "[" (match-string 0 name) "]")
7156 t t name))
7157 keys-alist (cons (list char help name) keys-alist))
7158 (throw 'exit t))))
7159 ;; Now check for lowercase letters
7160 (setq start 0)
7161 (while (string-match "[a-z]" name start)
7162 (setq start (match-end 0)
7163 char (string-to-char (match-string 0 name)))
7164 (if (not (assoc char keys-alist))
7165 (progn
7166 (setq help (format "%c: %s" char
7167 (replace-match
7168 (concat "[" (match-string 0 name) "]")
7169 t t name))
7170 keys-alist (cons (list char help name) keys-alist))
7171 (throw 'exit t))))
7172 ;; Bummer, nothing found! Use a stupid number
7173 (setq char (string-to-char (int-to-string (setq cnt (1+ cnt))))
7174 help (format "%c: %s" char name)
7175 keys-alist (cons (list char help name) keys-alist))))
7176 (nreverse keys-alist)))
7177
7178 (defun idlwave-set-local (var value &optional buffer)
7179 "Set the buffer-local value of VAR in BUFFER to VALUE."
7180 (save-excursion
7181 (set-buffer (or buffer (current-buffer)))
7182 (set (make-local-variable var) value)))
7183
7184 (defun idlwave-local-value (var &optional buffer)
7185 "Return the value of VAR in BUFFER, but only if VAR is local to BUFFER."
7186 (save-excursion
7187 (set-buffer (or buffer (current-buffer)))
7188 (and (local-variable-p var (current-buffer))
7189 (symbol-value var))))
7190
7191 ;; In XEmacs, we can use :activate-callback directly to advice the
7192 ;; choose functions. We use the private keymap only for the online
7193 ;; help feature.
7194
7195 (defvar idlwave-completion-map nil
7196 "Keymap for completion-list-mode with idlwave-complete.")
7197
7198 (defun idlwave-display-completion-list-xemacs (list &rest cl-args)
7199 (with-output-to-temp-buffer "*Completions*"
7200 (apply 'display-completion-list list
7201 ':activate-callback 'idlwave-default-choose-completion
7202 cl-args))
7203 (save-excursion
7204 (set-buffer "*Completions*")
7205 (use-local-map
7206 (or idlwave-completion-map
7207 (setq idlwave-completion-map
7208 (idlwave-make-modified-completion-map-xemacs
7209 (current-local-map)))))))
7210
7211 (defun idlwave-default-choose-completion (&rest args)
7212 "Execute `default-choose-completion' and then restore the win-conf."
7213 (apply 'idlwave-choose 'default-choose-completion args))
7214
7215 (defun idlwave-make-modified-completion-map-xemacs (old-map)
7216 "Replace `choose-completion' and `mouse-choose-completion' in OLD-MAP."
7217 (let ((new-map (copy-keymap old-map)))
7218 (define-key new-map [button3up] 'idlwave-mouse-completion-help)
7219 (define-key new-map [button3] (lambda ()
7220 (interactive)
7221 (setq this-command last-command)))
7222 new-map))
7223
7224 ;; In Emacs we also replace keybindings in the completion
7225 ;; map in order to install our wrappers.
7226
7227 (defun idlwave-display-completion-list-emacs (list)
7228 "Display completion list and install the choose wrappers."
7229 (with-output-to-temp-buffer "*Completions*"
7230 (display-completion-list list))
7231 (save-excursion
7232 (set-buffer "*Completions*")
7233 (use-local-map
7234 (or idlwave-completion-map
7235 (setq idlwave-completion-map
7236 (idlwave-make-modified-completion-map-emacs
7237 (current-local-map)))))))
7238
7239 (defun idlwave-make-modified-completion-map-emacs (old-map)
7240 "Replace `choose-completion' and `mouse-choose-completion' in OLD-MAP."
7241 (let ((new-map (copy-keymap old-map)))
7242 (substitute-key-definition
7243 'choose-completion 'idlwave-choose-completion new-map)
7244 (substitute-key-definition
7245 'mouse-choose-completion 'idlwave-mouse-choose-completion new-map)
7246 (define-key new-map [mouse-3] 'idlwave-mouse-completion-help)
7247 new-map))
7248
7249 (defun idlwave-choose-completion (&rest args)
7250 "Choose the completion that point is in or next to."
7251 (interactive)
7252 (apply 'idlwave-choose 'choose-completion args))
7253
7254 (defun idlwave-mouse-choose-completion (&rest args)
7255 "Click on an alternative in the `*Completions*' buffer to choose it."
7256 (interactive "e")
7257 (apply 'idlwave-choose 'mouse-choose-completion args))
7258
7259 ;;----------------------------------------------------------------------
7260 ;;----------------------------------------------------------------------
7261
7262 ;;; ------------------------------------------------------------------------
7263 ;;; Stucture parsing code, and code to manage class info
7264
7265 ;;
7266 ;; - Go again over the documentation how to write a completion
7267 ;; plugin. It is in self.el, but currently still very bad.
7268 ;; This could be in a separate file in the distribution, or
7269 ;; in an appendix for the manual.
7270
7271 (defvar idlwave-struct-skip
7272 "[ \t]*\\(\\$.*\n\\(^[ \t]*\\(\\$[ \t]*\\)?\\(;.*\\)?\n\\)*\\)?[ \t]*"
7273 "Regexp for skipping continued blank or comment-only lines in
7274 structures")
7275
7276 (defvar idlwave-struct-tag-regexp
7277 (concat "[{,]" ;leading comma/brace
7278 idlwave-struct-skip ; 4 groups
7279 "\\([a-zA-Z][a-zA-Z0-9_]*\\)" ;the tag itself, group 5
7280 "[ \t]*:") ; the final colon
7281 "Regexp for structure tags.")
7282
7283 (defun idlwave-struct-tags ()
7284 "Return a list of all tags in the structure defined at point.
7285 Point is expected just before the opening `{' of the struct definition."
7286 (save-excursion
7287 (let* ((borders (idlwave-struct-borders))
7288 (beg (car borders))
7289 (end (cdr borders))
7290 tags)
7291 (goto-char beg)
7292 (save-restriction
7293 (narrow-to-region beg end)
7294 (while (re-search-forward idlwave-struct-tag-regexp end t)
7295 ;; Check if we are still on the top level of the structure.
7296 (if (and (condition-case nil (progn (up-list -1) t) (error nil))
7297 (= (point) beg))
7298 (push (match-string-no-properties 5) tags))
7299 (goto-char (match-end 0))))
7300 (nreverse tags))))
7301
7302 (defun idlwave-find-struct-tag (tag)
7303 "Find a given TAG in the structure defined at point."
7304 (let* ((borders (idlwave-struct-borders))
7305 (beg (car borders))
7306 (end (cdr borders))
7307 (case-fold-search t))
7308 (re-search-forward (concat "\\(^[ \t]*\\|[,{][ \t]*\\)" tag "[ \t]*:")
7309 end t)))
7310
7311 (defun idlwave-struct-inherits ()
7312 "Return a list of all `inherits' names in the struct at point.
7313 Point is expected just before the opening `{' of the struct definition."
7314 (save-excursion
7315 (let* ((borders (idlwave-struct-borders))
7316 (beg (car borders))
7317 (end (cdr borders))
7318 (case-fold-search t)
7319 names)
7320 (goto-char beg)
7321 (save-restriction
7322 (narrow-to-region beg end)
7323 (while (re-search-forward
7324 (concat "[{,]" ;leading comma/brace
7325 idlwave-struct-skip ; 4 groups
7326 "inherits" ; The INHERITS tag
7327 idlwave-struct-skip ; 4 more
7328 "\\([a-zA-Z][a-zA-Z0-9_]*\\)") ; The super-group, #9
7329 end t)
7330 ;; Check if we are still on the top level of the structure.
7331 (if (and (condition-case nil (progn (up-list -1) t) (error nil))
7332 (= (point) beg))
7333 (push (match-string-no-properties 9) names))
7334 (goto-char (match-end 0))))
7335 (nreverse names))))
7336
7337 (defun idlwave-in-structure ()
7338 "Return t if point is inside an IDL structure definition."
7339 (let ((beg (point)))
7340 (save-excursion
7341 (if (not (or (idlwave-in-comment) (idlwave-in-quote)))
7342 (if (idlwave-find-structure-definition nil nil 'back)
7343 (let ((borders (idlwave-struct-borders)))
7344 (or (= (car borders) (cdr borders)) ;; struct not yet closed...
7345 (and (> beg (car borders)) (< beg (cdr borders))))))))))
7346
7347 (defun idlwave-struct-borders ()
7348 "Return the borders of the {...} after point as a cons cell."
7349 (let (beg)
7350 (save-excursion
7351 (skip-chars-forward "^{")
7352 (setq beg (point))
7353 (condition-case nil (forward-list 1)
7354 (error (goto-char beg)))
7355 (cons beg (point)))))
7356
7357 (defun idlwave-find-structure-definition (&optional var name bound)
7358 "Search forward for a structure definition. If VAR is non-nil,
7359 search for a structure assigned to variable VAR. If NAME is non-nil,
7360 search for a named structure NAME, if a string, or a generic named
7361 structure otherwise. If BOUND is an integer, limit the search. If
7362 BOUND is the symbol `all', we search first back and then forward
7363 through the entire file. If BOUND is the symbol `back' we search only
7364 backward."
7365 (let* ((ws "[ \t]*\\(\\$.*\n[ \t]*\\)*")
7366 (case-fold-search t)
7367 (lim (if (integerp bound) bound nil))
7368 (re (concat
7369 (if var
7370 (concat "\\<" (regexp-quote (downcase var)) "\\>" ws)
7371 "\\(\\)")
7372 "=" ws "\\({\\)"
7373 (if name
7374 (if (stringp name)
7375 (concat ws "\\(\\<" (downcase name) "\\)[^a-zA-Z0-9_$]")
7376 ;; Just a generic name
7377 (concat ws "\\<\\([a-zA-Z_0-9$]+\\)" ws ","))
7378 ""))))
7379 (if (or (and (or (eq bound 'all) (eq bound 'back))
7380 (re-search-backward re nil t))
7381 (and (not (eq bound 'back)) (re-search-forward re lim t)))
7382 (progn
7383 (goto-char (match-beginning 3))
7384 (match-string-no-properties 5)))))
7385
7386 (defvar idlwave-class-info nil)
7387 (defvar idlwave-class-reset nil) ; to reset buffer-local classes
7388
7389 (add-hook 'idlwave-update-rinfo-hook
7390 (lambda () (setq idlwave-class-reset t)))
7391 (add-hook 'idlwave-after-load-rinfo-hook
7392 (lambda () (setq idlwave-class-info nil)))
7393
7394 (defun idlwave-class-info (class)
7395 (let (list entry)
7396 (if idlwave-class-info
7397 (if idlwave-class-reset
7398 (setq
7399 idlwave-class-reset nil
7400 idlwave-class-info ; Remove any visited in a buffer
7401 (delq nil (mapcar
7402 (lambda (x)
7403 (let ((filebuf
7404 (idlwave-class-file-or-buffer
7405 (or (cdr (assq 'found-in x)) (car x)))))
7406 (if (cdr filebuf)
7407 nil
7408 x)))
7409 idlwave-class-info))))
7410 ;; Info is nil, put in the system stuff to start.
7411 (setq idlwave-class-info idlwave-system-class-info)
7412 (setq list idlwave-class-info)
7413 (while (setq entry (pop list))
7414 (idlwave-sintern-class-info entry)))
7415 (setq class (idlwave-sintern-class class))
7416 (or (assq class idlwave-class-info)
7417 (progn (idlwave-scan-class-info class)
7418 (assq class idlwave-class-info)))))
7419
7420 (defun idlwave-sintern-class-info (entry)
7421 "Sintern the class names in a class-info entry."
7422 (let ((taglist (assq 'tags entry))
7423 (inherits (assq 'inherits entry)))
7424 (setcar entry (idlwave-sintern-class (car entry) 'set))
7425 (if inherits
7426 (setcdr inherits (mapcar (lambda (x) (idlwave-sintern-class x 'set))
7427 (cdr inherits))))))
7428
7429 (defun idlwave-find-class-definition (class &optional all-hook alt-class)
7430 "Find class structure definition(s)
7431 If ALL-HOOK is set, find all named structure definitions in a given
7432 class__define routine, on which ALL-HOOK will be run. If ALT-CLASS is
7433 set, look for the name__define pro, and inside of it, for the ALT-CLASS
7434 class/struct definition"
7435 (let ((case-fold-search t) end-lim list name)
7436 (when (re-search-forward
7437 (concat "^[ \t]*pro[ \t]+" (downcase class) "__define" "\\>") nil t)
7438 (if all-hook
7439 (progn
7440 ;; For everything there
7441 (setq end-lim (save-excursion (idlwave-end-of-subprogram) (point)))
7442 (while (setq name
7443 (idlwave-find-structure-definition nil t end-lim))
7444 (funcall all-hook name)))
7445 (idlwave-find-structure-definition nil (or alt-class class))))))
7446
7447
7448 (defun idlwave-class-file-or-buffer (class)
7449 "Find buffer visiting CLASS definition"
7450 (let* ((pro (concat (downcase class) "__define"))
7451 (file (idlwave-routine-source-file
7452 (nth 3 (idlwave-rinfo-assoc pro 'pro nil
7453 (idlwave-routines))))))
7454 (cons file (if file (idlwave-get-buffer-visiting file)))))
7455
7456
7457 (defun idlwave-scan-class-info (class)
7458 "Scan all class and named structure info in the class__define pro"
7459 (let* ((idlwave-auto-routine-info-updates nil)
7460 (filebuf (idlwave-class-file-or-buffer class))
7461 (file (car filebuf))
7462 (buf (cdr filebuf))
7463 (class (idlwave-sintern-class class)))
7464 (if (or
7465 (not file)
7466 (and ;; neither a regular file nor a visited buffer
7467 (not buf)
7468 (not (file-regular-p file))))
7469 nil ; Cannot find the file/buffer to get any info
7470 (save-excursion
7471 (if buf (set-buffer buf)
7472 ;; Read the file in temporarily
7473 (set-buffer (get-buffer-create " *IDLWAVE-tmp*"))
7474 (erase-buffer)
7475 (unless (eq major-mode 'idlwave-mode)
7476 (idlwave-mode))
7477 (insert-file-contents file))
7478 (save-excursion
7479 (goto-char 1)
7480 (idlwave-find-class-definition class
7481 ;; Scan all of the structures found there
7482 (lambda (name)
7483 (let* ((this-class (idlwave-sintern-class name))
7484 (entry
7485 (list this-class
7486 (cons 'tags (idlwave-struct-tags))
7487 (cons 'inherits (idlwave-struct-inherits)))))
7488 (if (not (eq this-class class))
7489 (setq entry (nconc entry (list (cons 'found-in class)))))
7490 (idlwave-sintern-class-info entry)
7491 (push entry idlwave-class-info)))))))))
7492
7493 (defun idlwave-class-found-in (class)
7494 "Return the FOUND-IN property of the class."
7495 (cdr (assq 'found-in (idlwave-class-info class))))
7496 (defun idlwave-class-tags (class)
7497 "Return the native tags in CLASS."
7498 (cdr (assq 'tags (idlwave-class-info class))))
7499 (defun idlwave-class-inherits (class)
7500 "Return the direct superclasses of CLASS."
7501 (cdr (assq 'inherits (idlwave-class-info class))))
7502
7503
7504 (defun idlwave-all-class-tags (class)
7505 "Return a list of native and inherited tags in CLASS."
7506 (condition-case err
7507 (apply 'append (mapcar 'idlwave-class-tags
7508 (cons class (idlwave-all-class-inherits class))))
7509 (error
7510 (idlwave-class-tag-reset)
7511 (error "%s" (error-message-string err)))))
7512
7513
7514 (defun idlwave-all-class-inherits (class)
7515 "Return a list of all superclasses of CLASS (recursively expanded).
7516 The list is cached in `idlwave-class-info' for faster access."
7517 (cond
7518 ((not idlwave-support-inheritance) nil)
7519 ((eq class nil) nil)
7520 ((eq class t) nil)
7521 (t
7522 (let ((info (idlwave-class-info class))
7523 entry)
7524 (if (setq entry (assq 'all-inherits info))
7525 (cdr entry)
7526 ;; Save the depth of inheritance scan to check for circular references
7527 (let ((inherits (mapcar (lambda (x) (cons x 0))
7528 (idlwave-class-inherits class)))
7529 rtn all-inherits cl)
7530 (while inherits
7531 (setq cl (pop inherits)
7532 rtn (cons (car cl) rtn)
7533 inherits (append (mapcar (lambda (x)
7534 (cons x (1+ (cdr cl))))
7535 (idlwave-class-inherits (car cl)))
7536 inherits))
7537 (if (> (cdr cl) 999)
7538 (error
7539 "Class scan: inheritance depth exceeded. Circular inheritance?")
7540 ))
7541 (setq all-inherits (nreverse rtn))
7542 (nconc info (list (cons 'all-inherits all-inherits)))
7543 all-inherits))))))
7544
7545 (defun idlwave-entry-keywords (entry &optional record-link)
7546 "Return the flat entry keywords alist from routine-info entry.
7547 If RECORD-LINK is non-nil, the keyword text is copied and a text
7548 property indicating the link is added."
7549 (let (kwds)
7550 (mapcar
7551 (lambda (key-list)
7552 (let ((file (car key-list)))
7553 (mapcar (lambda (key-cons)
7554 (let ((key (car key-cons))
7555 (link (cdr key-cons)))
7556 (when (and record-link file)
7557 (setq key (copy-sequence key))
7558 (put-text-property
7559 0 (length key)
7560 'link
7561 (concat
7562 file
7563 (if link
7564 (concat idlwave-html-link-sep
7565 (number-to-string link))))
7566 key))
7567 (push (list key) kwds)))
7568 (cdr key-list))))
7569 (nthcdr 5 entry))
7570 (nreverse kwds)))
7571
7572 (defun idlwave-entry-find-keyword (entry keyword)
7573 "Find keyword KEYWORD in entry ENTRY, and return (with link) if set"
7574 (catch 'exit
7575 (mapc
7576 (lambda (key-list)
7577 (let ((file (car key-list))
7578 (kwd (assoc keyword (cdr key-list))))
7579 (when kwd
7580 (setq kwd (cons (car kwd)
7581 (if (and file (cdr kwd))
7582 (concat file
7583 idlwave-html-link-sep
7584 (number-to-string (cdr kwd)))
7585 (cdr kwd))))
7586 (throw 'exit kwd))))
7587 (nthcdr 5 entry))))
7588
7589 ;;==========================================================================
7590 ;;
7591 ;; Completing class structure tags. This is a completion plugin.
7592 ;; The necessary taglist is constructed dynamically
7593
7594 (defvar idlwave-current-tags-class nil)
7595 (defvar idlwave-current-class-tags nil)
7596 (defvar idlwave-current-native-class-tags nil)
7597 (defvar idlwave-sint-class-tags nil)
7598 (idlwave-new-sintern-type 'class-tag)
7599 (add-to-list 'idlwave-complete-special 'idlwave-complete-class-structure-tag)
7600 (add-hook 'idlwave-update-rinfo-hook 'idlwave-class-tag-reset)
7601
7602 (defun idlwave-complete-class-structure-tag ()
7603 "Complete a structure tag on a `self' argument in an object method."
7604 (interactive)
7605 (let ((pos (point))
7606 (case-fold-search t))
7607 (if (save-excursion
7608 ;; Check if the context is right
7609 (skip-chars-backward "a-zA-Z0-9._$")
7610 (and (< (point) (- pos 4))
7611 (looking-at "self\\.")))
7612 (let* ((class-selector (nth 2 (idlwave-current-routine)))
7613 (super-classes (idlwave-all-class-inherits class-selector)))
7614 ;; Check if we are in a class routine
7615 (unless class-selector
7616 (error "Not in a method procedure or function"))
7617 ;; Check if we need to update the "current" class
7618 (if (not (equal class-selector idlwave-current-tags-class))
7619 (idlwave-prepare-class-tag-completion class-selector))
7620 (setq idlwave-completion-help-info
7621 (list 'idlwave-complete-class-structure-tag-help
7622 (idlwave-sintern-routine
7623 (concat class-selector "__define"))
7624 nil))
7625 (let ((idlwave-cpl-bold idlwave-current-native-class-tags))
7626 (idlwave-complete-in-buffer
7627 'class-tag 'class-tag
7628 idlwave-current-class-tags nil
7629 (format "Select a tag of class %s" class-selector)
7630 "class tag"
7631 'idlwave-attach-class-tag-classes))
7632 t) ; return t to skip other completions
7633 nil)))
7634
7635 (defun idlwave-class-tag-reset ()
7636 (setq idlwave-current-tags-class nil))
7637
7638 (defun idlwave-prepare-class-tag-completion (class)
7639 "Find and parse the necessary class definitions for class structure tags."
7640 (setq idlwave-sint-class-tags nil)
7641 (setq idlwave-current-tags-class class)
7642 (setq idlwave-current-class-tags
7643 (mapcar (lambda (x)
7644 (list (idlwave-sintern-class-tag x 'set)))
7645 (idlwave-all-class-tags class)))
7646 (setq idlwave-current-native-class-tags
7647 (mapcar 'downcase (idlwave-class-tags class))))
7648
7649 ;===========================================================================
7650 ;;
7651 ;; Completing system variables and their structure fields
7652 ;; This is also a plugin.
7653
7654 (defvar idlwave-sint-sysvars nil)
7655 (defvar idlwave-sint-sysvartags nil)
7656 (idlwave-new-sintern-type 'sysvar)
7657 (idlwave-new-sintern-type 'sysvartag)
7658 (add-to-list 'idlwave-complete-special 'idlwave-complete-sysvar-or-tag)
7659 (add-hook 'idlwave-update-rinfo-hook 'idlwave-sysvars-reset)
7660 (add-hook 'idlwave-after-load-rinfo-hook 'idlwave-sintern-sysvar-alist)
7661
7662
7663 (defun idlwave-complete-sysvar-or-tag ()
7664 "Complete a system variable."
7665 (interactive)
7666 (let ((pos (point))
7667 (case-fold-search t))
7668 (cond ((save-excursion
7669 ;; Check if the context is right for system variable
7670 (skip-chars-backward "[a-zA-Z0-9_$]")
7671 (equal (char-before) ?!))
7672 (setq idlwave-completion-help-info '(idlwave-complete-sysvar-help))
7673 (idlwave-complete-in-buffer 'sysvar 'sysvar
7674 idlwave-system-variables-alist nil
7675 "Select a system variable"
7676 "system variable")
7677 t) ; return t to skip other completions
7678 ((save-excursion
7679 ;; Check if the context is right for sysvar tag
7680 (skip-chars-backward "a-zA-Z0-9_$.")
7681 (and (equal (char-before) ?!)
7682 (looking-at "\\([a-zA-Z][a-zA-Z0-9_$]*\\)\\.")
7683 (<= (match-end 0) pos)))
7684 ;; Complete a system variable tag
7685 (let* ((var (idlwave-sintern-sysvar (match-string 1)))
7686 (entry (assq var idlwave-system-variables-alist))
7687 (tags (cdr (assq 'tags entry))))
7688 (or entry (error "!%s is not a known system variable" var))
7689 (or tags (error "System variable !%s is not a structure" var))
7690 (setq idlwave-completion-help-info
7691 (list 'idlwave-complete-sysvar-tag-help var))
7692 (idlwave-complete-in-buffer 'sysvartag 'sysvartag
7693 tags nil
7694 "Select a system variable tag"
7695 "system variable tag")
7696 t)) ; return t to skip other completions
7697 (t nil))))
7698
7699 (defvar link) ;dynamic variables set by help callback
7700 (defvar props)
7701 (defun idlwave-complete-sysvar-help (mode word)
7702 (let ((word (or (nth 1 idlwave-completion-help-info) word))
7703 (entry (assoc word idlwave-system-variables-alist)))
7704 (cond
7705 ((eq mode 'test)
7706 (and (stringp word) entry (nth 1 (assq 'link entry))))
7707 ((eq mode 'set)
7708 (if entry (setq link (nth 1 (assq 'link entry))))) ;; setting dynamic!!!
7709 (t (error "This should not happen")))))
7710
7711 (defun idlwave-complete-sysvar-tag-help (mode word)
7712 (let* ((var (nth 1 idlwave-completion-help-info))
7713 (entry (assoc var idlwave-system-variables-alist))
7714 (tags (cdr (assq 'tags entry)))
7715 (main (nth 1 (assq 'link entry)))
7716 target main-base)
7717 (cond
7718 ((eq mode 'test) ; we can at least link the main
7719 (and (stringp word) entry main))
7720 ((eq mode 'set)
7721 (if entry
7722 (setq link
7723 (if (setq target (cdr (assoc-string word tags t)))
7724 (idlwave-substitute-link-target main target)
7725 main)))) ;; setting dynamic!!!
7726 (t (error "This should not happen")))))
7727
7728 (defun idlwave-split-link-target (link)
7729 "Split a given link into link file and anchor."
7730 (if (string-match idlwave-html-link-sep link)
7731 (cons (substring link 0 (match-beginning 0))
7732 (string-to-number (substring link (match-end 0))))))
7733
7734 (defun idlwave-substitute-link-target (link target)
7735 "Substitute the target anchor for the given link."
7736 (let (main-base)
7737 (setq main-base (if (string-match "#" link)
7738 (substring link 0 (match-beginning 0))
7739 link))
7740 (if target
7741 (concat main-base idlwave-html-link-sep (number-to-string target))
7742 link)))
7743
7744 ;; Fake help in the source buffer for class structure tags.
7745 ;; KWD AND NAME ARE GLOBAL-VARIABLES HERE.
7746 (defvar name)
7747 (defvar kwd)
7748 (defvar idlwave-help-do-class-struct-tag nil)
7749 (defun idlwave-complete-class-structure-tag-help (mode word)
7750 (cond
7751 ((eq mode 'test) ; nothing gets fontified for class tags
7752 nil)
7753 ((eq mode 'set)
7754 (let (class-with found-in)
7755 (when (setq class-with
7756 (idlwave-class-or-superclass-with-tag
7757 idlwave-current-tags-class
7758 word))
7759 (if (assq (idlwave-sintern-class class-with)
7760 idlwave-system-class-info)
7761 (error "No help available for system class tags"))
7762 (if (setq found-in (idlwave-class-found-in class-with))
7763 (setq name (cons (concat found-in "__define") class-with))
7764 (setq name (concat class-with "__define")))))
7765 (setq kwd word
7766 idlwave-help-do-class-struct-tag t))
7767 (t (error "This should not happen"))))
7768
7769 (defun idlwave-class-or-superclass-with-tag (class tag)
7770 "Find and return the CLASS or one of its superclass with the
7771 associated TAG, if any."
7772 (let ((sclasses (cons class (idlwave-all-class-inherits class)))
7773 cl)
7774 (catch 'exit
7775 (while sclasses
7776 (setq cl (pop sclasses))
7777 (let ((tags (idlwave-class-tags cl)))
7778 (while tags
7779 (if (eq t (compare-strings tag 0 nil (car tags) 0 nil t))
7780 (throw 'exit cl))
7781 (setq tags (cdr tags))))))))
7782
7783
7784 (defun idlwave-sysvars-reset ()
7785 (if (and (fboundp 'idlwave-shell-is-running)
7786 (idlwave-shell-is-running)
7787 idlwave-idlwave_routine_info-compiled)
7788 (idlwave-shell-send-command "idlwave_get_sysvars"
7789 'idlwave-process-sysvars 'hide)))
7790
7791 (defun idlwave-process-sysvars ()
7792 (idlwave-shell-filter-sysvars)
7793 (setq idlwave-sint-sysvars nil
7794 idlwave-sint-sysvartags nil)
7795 (idlwave-sintern-sysvar-alist))
7796
7797 (defun idlwave-sintern-sysvar-alist ()
7798 (let ((list idlwave-system-variables-alist) entry tags)
7799 (while (setq entry (pop list))
7800 (setcar entry (idlwave-sintern-sysvar (car entry) 'set))
7801 (setq tags (assq 'tags entry))
7802 (if tags
7803 (setcdr tags
7804 (mapcar (lambda (x)
7805 (cons (idlwave-sintern-sysvartag (car x) 'set)
7806 (cdr x)))
7807 (cdr tags)))))))
7808
7809 (defvar idlwave-shell-command-output)
7810 (defun idlwave-shell-filter-sysvars ()
7811 "Get any new system variables and tags."
7812 (let ((text idlwave-shell-command-output)
7813 (start 0)
7814 (old idlwave-system-variables-alist)
7815 var tags type name class link old-entry)
7816 (setq idlwave-system-variables-alist nil)
7817 (while (string-match "^IDLWAVE-SYSVAR: !\\([a-zA-Z0-9_$]+\\)\\( \\(.*\\)\\)?"
7818 text start)
7819 (setq start (match-end 0)
7820 var (match-string 1 text)
7821 tags (if (match-end 3)
7822 (idlwave-split-string (match-string 3 text))))
7823 ;; Maintain old links, if present
7824 (setq old-entry (assq (idlwave-sintern-sysvar var) old))
7825 (setq link (assq 'link old-entry))
7826 (setq idlwave-system-variables-alist
7827 (cons (list var
7828 (cons
7829 'tags
7830 (mapcar (lambda (x)
7831 (cons x
7832 (cdr (assq
7833 (idlwave-sintern-sysvartag x)
7834 (cdr (assq 'tags old-entry))))))
7835 tags)) link)
7836 idlwave-system-variables-alist)))
7837 ;; Keep the old value if query was not successful
7838 (setq idlwave-system-variables-alist
7839 (or idlwave-system-variables-alist old))))
7840
7841 (defun idlwave-completion-fontify-classes ()
7842 "Goto the *Completions* buffer and fontify the class info."
7843 (when (featurep 'font-lock)
7844 (save-excursion
7845 (set-buffer "*Completions*")
7846 (save-excursion
7847 (goto-char (point-min))
7848 (let ((buffer-read-only nil))
7849 (while (re-search-forward "\\.*<[^>]+>" nil t)
7850 (put-text-property (match-beginning 0) (match-end 0)
7851 'face 'font-lock-string-face)))))))
7852
7853 (defun idlwave-uniquify (list)
7854 (let ((ht (make-hash-table :size (length list) :test 'equal)))
7855 (delq nil
7856 (mapcar (lambda (x)
7857 (unless (gethash x ht)
7858 (puthash x t ht)
7859 x))
7860 list))))
7861
7862 (defun idlwave-after-successful-completion (type slash &optional verify)
7863 "Add `=' or `(' after successful completion of keyword and function.
7864 Restore the pre-completion window configuration if possible."
7865 (cond
7866 ((eq type 'procedure)
7867 nil)
7868 ((eq type 'function)
7869 (cond
7870 ((equal idlwave-function-completion-adds-paren nil) nil)
7871 ((or (equal idlwave-function-completion-adds-paren t)
7872 (equal idlwave-function-completion-adds-paren 1))
7873 (insert "("))
7874 ((equal idlwave-function-completion-adds-paren 2)
7875 (insert "()")
7876 (backward-char 1))
7877 (t nil)))
7878 ((eq type 'keyword)
7879 (if (and idlwave-keyword-completion-adds-equal
7880 (not slash))
7881 (progn (insert "=") t)
7882 nil)))
7883
7884 ;; Restore the pre-completion window configuration if this is safe.
7885
7886 (if (or (eq verify 'force) ; force
7887 (and
7888 (get-buffer-window "*Completions*") ; visible
7889 (idlwave-local-value 'idlwave-completion-p
7890 "*Completions*") ; cib-buffer
7891 (eq (marker-buffer idlwave-completion-mark)
7892 (current-buffer)) ; buffer OK
7893 (equal (marker-position idlwave-completion-mark)
7894 verify))) ; pos OK
7895 (idlwave-restore-wconf-after-completion))
7896 (move-marker idlwave-completion-mark nil)
7897 (setq idlwave-before-completion-wconf nil))
7898
7899 (defun idlwave-mouse-context-help (ev &optional arg)
7900 "Call `idlwave-context-help' on the clicked location."
7901 (interactive "eP")
7902 (mouse-set-point ev)
7903 (idlwave-context-help arg))
7904
7905 (defvar idlwave-last-context-help-pos nil)
7906 (defun idlwave-context-help (&optional arg)
7907 "Display IDL Online Help on context.
7908 If point is on a keyword, help for that keyword will be shown. If
7909 point is on a routine name or in the argument list of a routine, help
7910 for that routine will be displayed. Works for system routines and
7911 keywords, it pulls up text help. For other routies and keywords,
7912 visits the source file, finding help in the header (if
7913 `idlwave-help-source-try-header' is non-nil) or the routine definition
7914 itself."
7915 (interactive "P")
7916 (idlwave-do-context-help arg))
7917
7918 (defun idlwave-mouse-completion-help (ev)
7919 "Display online help about the completion at point."
7920 (interactive "eP")
7921 ;; Restore last-command for next command, to make
7922 ;; scrolling/cancelling of completions work.
7923 (setq this-command last-command)
7924 (idlwave-do-mouse-completion-help ev))
7925
7926 (defun idlwave-routine-info (&optional arg external)
7927 "Display a routines calling sequence and list of keywords. When
7928 point is on the name a function or procedure, or in the argument list
7929 of a function or procedure, this command displays a help buffer with
7930 the information. When called with prefix arg, enforce class query.
7931
7932 When point is on an object operator `->', display the class stored in
7933 this arrow, if any (see `idlwave-store-inquired-class'). With a
7934 prefix arg, the class property is cleared out."
7935
7936 (interactive "P")
7937 (idlwave-routines)
7938 (if (string-match "->" (buffer-substring
7939 (max (point-min) (1- (point)))
7940 (min (+ 2 (point)) (point-max))))
7941 ;; Cursor is on an arrow
7942 (if (get-text-property (point) 'idlwave-class)
7943 ;; arrow has class property
7944 (if arg
7945 ;; Remove property
7946 (save-excursion
7947 (backward-char 1)
7948 (when (looking-at ".?\\(->\\)")
7949 (remove-text-properties (match-beginning 1) (match-end 1)
7950 '(idlwave-class nil face nil))
7951 (message "Class property removed from arrow")))
7952 ;; Echo class property
7953 (message "Arrow has text property identifying object to be class %s"
7954 (get-text-property (point) 'idlwave-class)))
7955 ;; No property found
7956 (message "Arrow has no class text property"))
7957
7958 ;; Not on an arrow...
7959 (let* ((idlwave-query-class nil)
7960 (idlwave-force-class-query (equal arg '(4)))
7961 (module (idlwave-what-module)))
7962 (if (car module)
7963 (apply 'idlwave-display-calling-sequence
7964 (idlwave-fix-module-if-obj_new module))
7965 (error "Don't know which calling sequence to show")))))
7966
7967 (defun idlwave-resolve (&optional arg)
7968 "Call RESOLVE_ROUTINE on the module name at point.
7969 Like `idlwave-routine-info', this looks for a routine call at point.
7970 After confirmation in the minibuffer, it will use the shell to issue
7971 a RESOLVE call for this routine, to attempt to make it defined and its
7972 routine info available for IDLWAVE. If the routine is a method call,
7973 both `class__method' and `class__define' will be tried.
7974 With ARG, enforce query for the class of object methods."
7975 (interactive "P")
7976 (let* ((idlwave-query-class nil)
7977 (idlwave-force-class-query (equal arg '(4)))
7978 (module (idlwave-what-module))
7979 (name (idlwave-make-full-name (nth 2 module) (car module)))
7980 (type (if (eq (nth 1 module) 'pro) "pro" "function"))
7981 (resolve (read-string "Resolve: " (format "%s %s" type name)))
7982 (kwd "")
7983 class)
7984 (if (string-match "\\(pro\\|function\\)[ \t]+\\(\\(.*\\)::\\)?\\(.*\\)"
7985 resolve)
7986 (setq type (match-string 1 resolve)
7987 class (if (match-beginning 2)
7988 (match-string 3 resolve)
7989 nil)
7990 name (match-string 4 resolve)))
7991 (if (string= (downcase type) "function")
7992 (setq kwd ",/is_function"))
7993
7994 (cond
7995 ((null class)
7996 (idlwave-shell-send-command
7997 (format "resolve_routine,'%s'%s" (downcase name) kwd)
7998 'idlwave-update-routine-info
7999 nil t))
8000 (t
8001 (idlwave-shell-send-command
8002 (format "resolve_routine,'%s__define'%s" (downcase class) kwd)
8003 (list 'idlwave-shell-send-command
8004 (format "resolve_routine,'%s__%s'%s"
8005 (downcase class) (downcase name) kwd)
8006 '(idlwave-update-routine-info)
8007 nil t))))))
8008
8009 (defun idlwave-find-module-this-file ()
8010 (interactive)
8011 (idlwave-find-module '(4)))
8012
8013 (defun idlwave-find-module (&optional arg)
8014 "Find the source code of an IDL module.
8015 Works for modules for which IDLWAVE has routine info available. The
8016 function offers as default the module name `idlwave-routine-info'
8017 would use. With ARG limit to this buffer. With two prefix ARG's
8018 force class query for object methods."
8019 (interactive "P")
8020 (let* ((idlwave-query-class nil)
8021 (idlwave-force-class-query (equal arg '(16)))
8022 (this-buffer (equal arg '(4)))
8023 (module (idlwave-fix-module-if-obj_new (idlwave-what-module)))
8024 (default (if module
8025 (concat (idlwave-make-full-name
8026 (nth 2 module) (car module))
8027 (if (eq (nth 1 module) 'pro) "<p>" "<f>"))
8028 "none"))
8029 (list
8030 (idlwave-uniquify
8031 (delq nil
8032 (mapcar (lambda (x)
8033 (if (eq 'system (car-safe (nth 3 x)))
8034 ;; Take out system routines with no source.
8035 nil
8036 (list
8037 (concat (idlwave-make-full-name
8038 (nth 2 x) (car x))
8039 (if (eq (nth 1 x) 'pro) "<p>" "<f>")))))
8040 (if this-buffer
8041 (idlwave-save-buffer-update)
8042 (idlwave-routines))))))
8043 (name (idlwave-completing-read
8044 (if (or (not this-buffer)
8045 (assoc default list))
8046 (format "Module (Default %s): " default)
8047 (format "Module in this file: "))
8048 list))
8049 type class)
8050 (if (string-match "\\`\\s-*\\'" name)
8051 ;; Nothing, use the default.
8052 (setq name default))
8053 (if (string-match "<[fp]>" name)
8054 (setq type (substring name -2 -1)
8055 name (substring name 0 -3)))
8056 (if (string-match "\\(.*\\)::\\(.*\\)" name)
8057 (setq class (match-string 1 name)
8058 name (match-string 2 name)))
8059 (setq name (idlwave-sintern-routine-or-method name class)
8060 class (idlwave-sintern-class class)
8061 type (cond ((equal type "f") 'fun)
8062 ((equal type "p") 'pro)
8063 (t t)))
8064 (idlwave-do-find-module name type class nil this-buffer)))
8065
8066 (defun idlwave-do-find-module (name type class
8067 &optional force-source this-buffer)
8068 (let ((name1 (idlwave-make-full-name class name))
8069 source buf1 entry
8070 (buf (current-buffer))
8071 (pos (point))
8072 file name2)
8073 (setq entry (idlwave-best-rinfo-assq name type class (idlwave-routines)
8074 'WITH-FILE)
8075 source (or force-source (nth 3 entry))
8076 name2 (if (nth 2 entry)
8077 (idlwave-make-full-name (nth 2 entry) name)
8078 name1))
8079 (if source
8080 (setq file (idlwave-routine-source-file source)))
8081 (unless file ; Try to find it on the path.
8082 (setq file
8083 (idlwave-expand-lib-file-name
8084 (if class
8085 (format "%s__define.pro" (downcase class))
8086 (format "%s.pro" (downcase name))))))
8087 (cond
8088 ((or (null name) (equal name ""))
8089 (error "Abort"))
8090 ((eq (car source) 'system)
8091 (error "Source code for system routine %s is not available"
8092 name2))
8093 ((or (not file) (not (file-regular-p file)))
8094 (error "Source code for routine %s is not available"
8095 name2))
8096 (t
8097 (when (not this-buffer)
8098 (setq buf1
8099 (idlwave-find-file-noselect file 'find))
8100 (pop-to-buffer buf1 t))
8101 (goto-char (point-max))
8102 (let ((case-fold-search t))
8103 (if (re-search-backward
8104 (concat "^[ \t]*\\<"
8105 (cond ((eq type 'fun) "function")
8106 ((eq type 'pro) "pro")
8107 (t "\\(pro\\|function\\)"))
8108 "\\>[ \t]+"
8109 (regexp-quote (downcase name2))
8110 "[^a-zA-Z0-9_$]")
8111 nil t)
8112 (goto-char (match-beginning 0))
8113 (pop-to-buffer buf)
8114 (goto-char pos)
8115 (error "Could not find routine %s" name2)))))))
8116
8117 (defun idlwave-what-module ()
8118 "Return a default module for stuff near point.
8119 Used by `idlwave-routine-info' and `idlwave-find-module'."
8120 (idlwave-routines)
8121 (if (let ((case-fold-search t))
8122 (save-excursion
8123 (idlwave-beginning-of-statement)
8124 (looking-at "[ \t]*\\(pro\\|function\\)[ \t]+\\(\\([a-zA-Z0-9_$]+\\)::\\)?\\([a-zA-Z0-9$_]+\\)\\([, \t\n]\\|$\\)")))
8125 ;; This is a function or procedure definition statement
8126 ;; We return the defined routine as module.
8127 (list
8128 (idlwave-sintern-routine-or-method (match-string-no-properties 4)
8129 (match-string-no-properties 2))
8130 (if (equal (downcase (match-string 1)) "pro") 'pro 'fun)
8131 (idlwave-sintern-class (match-string 3)))
8132
8133 ;; Not a definition statement - analyze precise position.
8134 (let* ((where (idlwave-where))
8135 (cw (nth 2 where))
8136 (pro (car (nth 0 where)))
8137 (func (car (nth 1 where)))
8138 (this-word (idlwave-this-word "a-zA-Z0-9$_"))
8139 (next-char (save-excursion (skip-chars-forward "a-zA-Z0-9$_")
8140 (following-char)))
8141 )
8142 (cond
8143 ((and (eq cw 'procedure)
8144 (not (equal this-word "")))
8145 (setq this-word (idlwave-sintern-routine-or-method
8146 this-word (nth 2 (nth 3 where))))
8147 (list this-word 'pro
8148 (idlwave-determine-class
8149 (cons this-word (cdr (nth 3 where)))
8150 'pro)))
8151 ((and (eq cw 'function)
8152 (not (equal this-word ""))
8153 (or (eq next-char ?\() ; exclude arrays, vars.
8154 (looking-at "[a-zA-Z0-9_]*[ \t]*(")))
8155 (setq this-word (idlwave-sintern-routine-or-method
8156 this-word (nth 2 (nth 3 where))))
8157 (list this-word 'fun
8158 (idlwave-determine-class
8159 (cons this-word (cdr (nth 3 where)))
8160 'fun)))
8161 ((and (memq cw '(function-keyword procedure-keyword))
8162 (not (equal this-word ""))
8163 (eq next-char ?\()) ; A function!
8164 (setq this-word (idlwave-sintern-routine this-word))
8165 (list this-word 'fun nil))
8166 (func
8167 (list func 'fun (idlwave-determine-class (nth 1 where) 'fun)))
8168 (pro
8169 (list pro 'pro (idlwave-determine-class (nth 0 where) 'pro)))
8170 (t nil)))))
8171
8172 (defun idlwave-what-module-find-class ()
8173 "Call idlwave-what-module and find the inherited class if necessary."
8174 (let* ((module (idlwave-what-module))
8175 (class (nth 2 module))
8176 classes)
8177 (if (and (= (length module) 3)
8178 (stringp class))
8179 (list (car module)
8180 (nth 1 module)
8181 (apply 'idlwave-find-inherited-class module))
8182 module)))
8183
8184 (defun idlwave-find-inherited-class (name type class)
8185 "Find the class which defines TYPE NAME and is CLASS or inherited by CLASS."
8186 (let ((entry (idlwave-best-rinfo-assoc name type class (idlwave-routines))))
8187 (if entry
8188 (nth 2 entry)
8189 class)))
8190
8191 (defun idlwave-fix-module-if-obj_new (module)
8192 "Check if MODULE points to obj_new.
8193 If yes, and if the cursor is in the keyword region, change to the
8194 appropriate Init method."
8195 (let* ((name (car module))
8196 (pos (point))
8197 (case-fold-search t)
8198 string)
8199 (if (and (stringp name)
8200 (equal (downcase name) "obj_new")
8201 (save-excursion
8202 (idlwave-beginning-of-statement)
8203 (setq string (buffer-substring (point) pos))
8204 (string-match "obj_new([^'\"]*['\"]\\([a-zA-Z0-9_]+\\)"
8205 string)))
8206 (let ((name "Init")
8207 (class (match-string 1 string)))
8208 (setq module (list (idlwave-sintern-method "Init")
8209 'fun
8210 (idlwave-sintern-class class)))))
8211 module))
8212
8213 (defun idlwave-fix-keywords (name type class keywords
8214 &optional super-classes system)
8215 "Update a list of keywords.
8216 Translate OBJ_NEW, adding all super-class keywords, or all keywords
8217 from all classes if class equals t. If SYSTEM is non-nil, don't
8218 demand _EXTRA in the keyword list."
8219 (let ((case-fold-search t))
8220
8221 ;; If this is the OBJ_NEW function, try to figure out the class and use
8222 ;; the keywords from the corresponding INIT method.
8223 (if (and (equal (upcase name) "OBJ_NEW")
8224 (or (eq major-mode 'idlwave-mode)
8225 (eq major-mode 'idlwave-shell-mode)))
8226 (let* ((bos (save-excursion (idlwave-beginning-of-statement) (point)))
8227 (string (buffer-substring bos (point)))
8228 (case-fold-search t)
8229 class)
8230 (and (string-match "obj_new([^'\"]*['\"]\\([a-zA-Z0-9_]+\\)"
8231 string)
8232 (setq class (idlwave-sintern-class (match-string 1 string)))
8233 (setq idlwave-current-obj_new-class class)
8234 (setq keywords
8235 (append keywords
8236 (idlwave-entry-keywords
8237 (idlwave-rinfo-assq
8238 (idlwave-sintern-method "INIT")
8239 'fun
8240 class
8241 (idlwave-routines)) 'do-link))))))
8242
8243 ;; If the class is `t', combine all keywords of all methods NAME
8244 (when (eq class t)
8245 (mapc (lambda (entry)
8246 (and
8247 (nth 2 entry) ; non-nil class
8248 (eq (nth 1 entry) type) ; correct type
8249 (setq keywords
8250 (append keywords
8251 (idlwave-entry-keywords entry 'do-link)))))
8252 (idlwave-all-assq name (idlwave-routines)))
8253 (setq keywords (idlwave-uniquify keywords)))
8254
8255 ;; If we have inheritance, add all keywords from superclasses, if
8256 ;; the user indicated that method in `idlwave-keyword-class-inheritance'
8257 (when (and
8258 super-classes
8259 idlwave-keyword-class-inheritance
8260 (stringp class)
8261 (or
8262 system
8263 (assq (idlwave-sintern-keyword "_extra") keywords)
8264 (assq (idlwave-sintern-keyword "_ref_extra") keywords))
8265 ;; Check if one of the keyword-class regexps matches the name
8266 (let ((regexps idlwave-keyword-class-inheritance) re)
8267 (catch 'exit
8268 (while (setq re (pop regexps))
8269 (if (string-match re name) (throw 'exit t))))))
8270
8271 (loop for entry in (idlwave-routines) do
8272 (and (nth 2 entry) ; non-nil class
8273 (memq (nth 2 entry) super-classes) ; an inherited class
8274 (eq (nth 1 entry) type) ; correct type
8275 (eq (car entry) name) ; correct name
8276 (mapcar (lambda (k) (add-to-list 'keywords k))
8277 (idlwave-entry-keywords entry 'do-link))))
8278 (setq keywords (idlwave-uniquify keywords)))
8279
8280 ;; Return the final list
8281 keywords))
8282
8283 (defun idlwave-expand-keyword (keyword module)
8284 "Expand KEYWORD to one of the valid keyword parameters of MODULE.
8285 KEYWORD may be an exact match or an abbreviation of a keyword.
8286 If the match is exact, KEYWORD itself is returned, even if there may be other
8287 keywords of which KEYWORD is an abbreviation. This is necessary because some
8288 system routines have keywords which are prefixes of other keywords.
8289 If KEYWORD is an abbreviation of several keywords, a list of all possible
8290 completions is returned.
8291 If the abbreviation was unique, the correct keyword is returned.
8292 If it cannot be a keyword, the function return nil.
8293 If we do not know about MODULE, just return KEYWORD literally."
8294 (let* ((name (car module))
8295 (type (nth 1 module))
8296 (class (nth 2 module))
8297 (kwd (idlwave-sintern-keyword keyword))
8298 (entry (idlwave-best-rinfo-assoc name type class (idlwave-routines)))
8299 (kwd-alist (idlwave-entry-keywords entry))
8300 (extra (or (assq (idlwave-sintern-keyword "_EXTRA") kwd-alist)
8301 (assq (idlwave-sintern-keyword "_REF_EXTRA") kwd-alist)))
8302 (completion-ignore-case t)
8303 candidates)
8304 (cond ((assq kwd kwd-alist)
8305 kwd)
8306 ((setq candidates (all-completions kwd kwd-alist))
8307 (if (= (length candidates) 1)
8308 (car candidates)
8309 candidates))
8310 ((and entry extra)
8311 ;; Inheritance may cause this keyword to be correct
8312 keyword)
8313 (entry
8314 ;; We do know the function, which does not have the keyword.
8315 nil)
8316 (t
8317 ;; We do not know the function, so this just might be a correct
8318 ;; keyword - return it as it is.
8319 keyword))))
8320
8321 (defvar idlwave-rinfo-mouse-map (make-sparse-keymap))
8322 (defvar idlwave-rinfo-map (make-sparse-keymap))
8323 (define-key idlwave-rinfo-mouse-map
8324 (if (featurep 'xemacs) [button2] [mouse-2])
8325 'idlwave-mouse-active-rinfo)
8326 (define-key idlwave-rinfo-mouse-map
8327 (if (featurep 'xemacs) [(shift button2)] [(shift mouse-2)])
8328 'idlwave-mouse-active-rinfo-shift)
8329 (define-key idlwave-rinfo-mouse-map
8330 (if (featurep 'xemacs) [button3] [mouse-3])
8331 'idlwave-mouse-active-rinfo-right)
8332 (define-key idlwave-rinfo-mouse-map " " 'idlwave-active-rinfo-space)
8333 (define-key idlwave-rinfo-map "q" 'idlwave-quit-help)
8334 (define-key idlwave-rinfo-mouse-map "q" 'idlwave-quit-help)
8335 (defvar idlwave-popup-source nil)
8336 (defvar idlwave-rinfo-marker (make-marker))
8337
8338 (defun idlwave-quit-help ()
8339 (interactive)
8340 (let ((ri-window (get-buffer-window "*Help*"))
8341 (olh-window (get-buffer-window "*IDLWAVE Help*")))
8342 (when (and olh-window
8343 (fboundp 'idlwave-help-quit))
8344 (select-window olh-window)
8345 (idlwave-help-quit))
8346 (when (window-live-p ri-window)
8347 (delete-window ri-window))))
8348
8349 (defun idlwave-display-calling-sequence (name type class
8350 &optional initial-class)
8351 ;; Display the calling sequence of module NAME, type TYPE in class CLASS.
8352 (let* ((initial-class (or initial-class class))
8353 (entry (or (idlwave-best-rinfo-assq name type class
8354 (idlwave-routines))
8355 (idlwave-rinfo-assq name type class
8356 idlwave-unresolved-routines)))
8357 (name (or (car entry) name))
8358 (class (or (nth 2 entry) class))
8359 (superclasses (idlwave-all-class-inherits initial-class))
8360 (twins (idlwave-routine-twins entry))
8361 (dtwins (idlwave-study-twins twins))
8362 (all dtwins)
8363 (system (eq (car (nth 3 entry)) 'system))
8364 (calling-seq (nth 4 entry))
8365 (keywords (idlwave-entry-keywords entry 'do-link))
8366 (html-file (car (nth 5 entry)))
8367 (help-echo-kwd
8368 "Button2: Insert KEYWORD (SHIFT=`/KEYWORD') | Button3: Online Help ")
8369 (help-echo-use
8370 "Button2/3: Online Help")
8371 (help-echo-src
8372 "Button2: Jump to source and back | Button3: Source in Help window.")
8373 (help-echo-class
8374 "Button2: Display info about same method in superclass")
8375 (col 0)
8376 (data (list name type class (current-buffer) nil initial-class))
8377 (km-prop (if (featurep 'xemacs) 'keymap 'local-map))
8378 (face 'idlwave-help-link)
8379 beg props win cnt total)
8380 ;; Fix keywords, but don't add chained super-classes, since these
8381 ;; are shown separately for that super-class
8382 (setq keywords (idlwave-fix-keywords name type class keywords))
8383 (cond
8384 ((null entry)
8385 (error "No %s %s known %s" type name
8386 (if initial-class (concat "in class " initial-class) "")))
8387 ((or (null name) (equal name ""))
8388 (error "No function or procedure call at point"))
8389 ((null calling-seq)
8390 (error "Calling sequence of %s %s not available" type name))
8391 (t
8392 (save-excursion
8393 (move-marker idlwave-rinfo-marker (point))
8394 (set-buffer (get-buffer-create "*Help*"))
8395 (use-local-map idlwave-rinfo-map)
8396 (setq buffer-read-only nil)
8397 (erase-buffer)
8398 (set (make-local-variable 'idlwave-popup-source) nil)
8399 (set (make-local-variable 'idlwave-current-obj_new-class)
8400 idlwave-current-obj_new-class)
8401 (when superclasses
8402 (setq props (list 'mouse-face 'highlight
8403 km-prop idlwave-rinfo-mouse-map
8404 'help-echo help-echo-class
8405 'data (cons 'class data)))
8406 (let ((classes (cons initial-class superclasses)) c)
8407 (insert "Classes: ")
8408 (while (setq c (pop classes))
8409 (insert " ")
8410 (setq beg (point))
8411 (insert c)
8412 (if (equal (downcase c) (downcase class))
8413 (add-text-properties beg (point) (list 'face 'bold))
8414 ;; If Method exists in a different class link it
8415 (if (idlwave-rinfo-assq name type c (idlwave-routines))
8416 (add-text-properties beg (point) props))))
8417 (insert "\n")))
8418 (setq props (list 'mouse-face 'highlight
8419 km-prop idlwave-rinfo-mouse-map
8420 'help-echo help-echo-use
8421 'data (cons 'usage data)))
8422 (if html-file (setq props (append (list 'face face 'link html-file)
8423 props)))
8424 (insert "Usage: ")
8425 (setq beg (point))
8426 (insert (if class
8427 (format calling-seq class name class name class name)
8428 (format calling-seq name name name name))
8429 "\n")
8430 (add-text-properties beg (point) props)
8431
8432 (insert "Keywords:")
8433 (if (null keywords)
8434 (insert " No keywords accepted.")
8435 (setq col 9)
8436 (mapcar
8437 (lambda (x)
8438 (if (>= (+ col 1 (length (car x)))
8439 (window-width))
8440 (progn
8441 (insert "\n ")
8442 (setq col 9)))
8443 (insert " ")
8444 (setq beg (point)
8445 ;; Relevant keywords already have link property attached
8446 props (list 'mouse-face 'highlight
8447 km-prop idlwave-rinfo-mouse-map
8448 'data (cons 'keyword data)
8449 'help-echo help-echo-kwd
8450 'keyword (car x)))
8451 (if system (setq props (append (list 'face face) props)))
8452 (insert (car x))
8453 (add-text-properties beg (point) props)
8454 (setq col (+ col 1 (length (car x)))))
8455 keywords))
8456
8457 (setq cnt 1 total (length all))
8458 ;; Here entry is (key file (list of type-conses))
8459 (while (setq entry (pop all))
8460 (setq props (list 'mouse-face 'highlight
8461 km-prop idlwave-rinfo-mouse-map
8462 'help-echo help-echo-src
8463 'source (list (car (car (nth 2 entry))) ;type
8464 (nth 1 entry)
8465 nil
8466 (cdr (car (nth 2 entry))))
8467 'data (cons 'source data)))
8468 (idlwave-insert-source-location
8469 (format "\n%-8s %s"
8470 (if (equal cnt 1)
8471 (if (> total 1) "Sources:" "Source:")
8472 "")
8473 (if (> total 1) "- " ""))
8474 entry props)
8475 (incf cnt)
8476 (when (and all (> cnt idlwave-rinfo-max-source-lines))
8477 ;; No more source lines, please
8478 (insert (format
8479 "\n Source information truncated to %d entries."
8480 idlwave-rinfo-max-source-lines))
8481 (setq all nil)))
8482 (goto-char (point-min))
8483 (setq buffer-read-only t))
8484 (display-buffer "*Help*")
8485 (if (and (setq win (get-buffer-window "*Help*"))
8486 idlwave-resize-routine-help-window)
8487 (progn
8488 (let ((ww (selected-window)))
8489 (unwind-protect
8490 (progn
8491 (select-window win)
8492 (enlarge-window (- (/ (frame-height) 2)
8493 (window-height)))
8494 (shrink-window-if-larger-than-buffer))
8495 (select-window ww)))))))))
8496
8497 (defun idlwave-insert-source-location (prefix entry &optional file-props)
8498 "Insert a source location into the routine info buffer.
8499 Start line with PREFIX. If a file name is inserted, add FILE-PROPS to
8500 it."
8501 (let* ((key (car entry))
8502 (file (nth 1 entry))
8503 (types (nth 2 entry))
8504 (shell-flag (assq 'compiled types))
8505 (buffer-flag (assq 'buffer types))
8506 (user-flag (assq 'user types))
8507 (lib-flag (assq 'lib types))
8508 (ndupl (or (and buffer-flag (idlwave-count-memq 'buffer types))
8509 (and user-flag (idlwave-count-memq 'user types))
8510 (and lib-flag (idlwave-count-memq 'lib types))
8511 1))
8512 (doflags t)
8513 beg special)
8514
8515 (insert prefix)
8516
8517 (cond
8518 ((eq key 'system)
8519 (setq doflags nil)
8520 (insert "System "))
8521
8522 ((eq key 'builtin)
8523 (setq doflags nil)
8524 (insert "Builtin "))
8525
8526 ((and (not file) shell-flag)
8527 (insert "Unresolved"))
8528
8529 ((null file)
8530 (insert "ERROR"))
8531
8532 ((idlwave-syslib-p file)
8533 (if (string-match "obsolete" (file-name-directory file))
8534 (insert "Obsolete ")
8535 (insert "SystemLib ")))
8536
8537 ;; New special syntax: taken directly from routine-info for
8538 ;; library catalog routines
8539 ((setq special (or (cdr lib-flag) (cdr user-flag)))
8540 (insert (format "%-10s" special)))
8541
8542 ;; Old special syntax: a matching regexp
8543 ((setq special (idlwave-special-lib-test file))
8544 (insert (format "%-10s" special)))
8545
8546 ;; Catch-all with file
8547 ((idlwave-lib-p file) (insert "Library "))
8548
8549 ;; Sanity catch all
8550 (t (insert "Other ")))
8551
8552 (when doflags
8553 (insert (concat
8554 " ["
8555 (if lib-flag "L" "-")
8556 (if user-flag "C" "-")
8557 (if shell-flag "S" "-")
8558 (if buffer-flag "B" "-")
8559 "] ")))
8560 (when (> ndupl 1)
8561 (setq beg (point))
8562 (insert (format "(%dx) " ndupl))
8563 (add-text-properties beg (point) (list 'face 'bold)))
8564 (when (and file (not (equal file "")))
8565 (setq beg (point))
8566 (insert (apply 'abbreviate-file-name
8567 (if (featurep 'xemacs) (list file t) (list file))))
8568 (if file-props
8569 (add-text-properties beg (point) file-props)))))
8570
8571 (defun idlwave-special-lib-test (file)
8572 "Check the path of FILE against the regexps which define special libs.
8573 Return the name of the special lib if there is a match."
8574 (let ((alist idlwave-special-lib-alist)
8575 entry rtn)
8576 (cond
8577 ((stringp file)
8578 (while (setq entry (pop alist))
8579 (if (string-match (car entry) file)
8580 (setq rtn (cdr entry)
8581 alist nil)))
8582 rtn)
8583 (t nil))))
8584
8585 (defun idlwave-mouse-active-rinfo-right (ev)
8586 (interactive "e")
8587 (idlwave-mouse-active-rinfo ev 'right))
8588
8589 (defun idlwave-mouse-active-rinfo-shift (ev)
8590 (interactive "e")
8591 (idlwave-mouse-active-rinfo ev nil 'shift))
8592
8593 (defun idlwave-active-rinfo-space ()
8594 (interactive)
8595 (idlwave-mouse-active-rinfo nil 'right))
8596
8597 (defun idlwave-mouse-active-rinfo (ev &optional right shift)
8598 "Does the mouse actions in the routine info buffer.
8599 Optional args RIGHT and SHIFT indicate, if mouse-3 was used, and if SHIFT
8600 was pressed."
8601 (interactive "e")
8602 (if ev (mouse-set-point ev))
8603 (let (data id name type class buf bufwin source link keyword
8604 word initial-class)
8605 (setq data (get-text-property (point) 'data)
8606 source (get-text-property (point) 'source)
8607 keyword (get-text-property (point) 'keyword)
8608 link (get-text-property (point) 'link)
8609 id (car data)
8610 name (nth 1 data) type (nth 2 data) class (nth 3 data)
8611 buf (nth 4 data)
8612 initial-class (nth 6 data)
8613 word (idlwave-this-word)
8614 bufwin (get-buffer-window buf t))
8615
8616 (cond ((eq id 'class) ; Switch class being displayed
8617 (if (window-live-p bufwin) (select-window bufwin))
8618 (idlwave-display-calling-sequence
8619 (idlwave-sintern-method name)
8620 type (idlwave-sintern-class word)
8621 initial-class))
8622 ((eq id 'usage) ; Online help on this routine
8623 (idlwave-online-help link name type class))
8624 ((eq id 'source) ; Source in help or buffer
8625 (if right ; In help
8626 (let ((idlwave-extra-help-function 'idlwave-help-with-source)
8627 (idlwave-help-source-try-header nil)
8628 ;; Fake idlwave-routines so help will find the right entry
8629 (idlwave-routines
8630 (list (list name type class source ""))))
8631 (idlwave-help-get-special-help name type class nil))
8632 ;; Otherwise just pop to the source
8633 (setq idlwave-popup-source (not idlwave-popup-source))
8634 (if idlwave-popup-source
8635 (condition-case err
8636 (idlwave-do-find-module name type class source)
8637 (error
8638 (setq idlwave-popup-source nil)
8639 (if (window-live-p bufwin) (select-window bufwin))
8640 (error (nth 1 err))))
8641 (if bufwin
8642 (select-window bufwin)
8643 (pop-to-buffer buf))
8644 (goto-char (marker-position idlwave-rinfo-marker)))))
8645 ((eq id 'keyword)
8646 (if right
8647 (idlwave-online-help link name type class keyword)
8648 (idlwave-rinfo-insert-keyword keyword buf shift))))))
8649
8650 (defun idlwave-rinfo-insert-keyword (keyword buffer &optional shift)
8651 "Insert KEYWORD in BUFFER. Make sure buffer is displayed in a window."
8652 (let ((bwin (get-buffer-window buffer)))
8653 (if idlwave-complete-empty-string-as-lower-case
8654 (setq keyword (downcase keyword)))
8655 (if bwin
8656 (select-window bwin)
8657 (pop-to-buffer buffer)
8658 (setq bwin (get-buffer-window buffer)))
8659 (if (eq (preceding-char) ?/)
8660 (insert keyword)
8661 (unless (save-excursion
8662 (re-search-backward
8663 "[(,][ \t]*\\(\\$[ \t]*\\(;.*\\)?\n\\)?[ \t]*\\="
8664 (min (- (point) 100) (point-min)) t))
8665 (insert ", "))
8666 (if shift (insert "/"))
8667 (insert keyword)
8668 (if (and (not shift)
8669 idlwave-keyword-completion-adds-equal)
8670 (insert "=")))))
8671
8672 (defun idlwave-list-buffer-load-path-shadows (&optional arg)
8673 "List the load path shadows of all routines defined in current buffer."
8674 (interactive "P")
8675 (idlwave-routines)
8676 (if (eq major-mode 'idlwave-mode)
8677 (idlwave-list-load-path-shadows
8678 nil (idlwave-update-current-buffer-info 'save-buffer)
8679 "in current buffer")
8680 (error "Current buffer is not in idlwave-mode")))
8681
8682 (defun idlwave-list-shell-load-path-shadows (&optional arg)
8683 "List the load path shadows of all routines compiled under the shell.
8684 This is very useful for checking an IDL application. Just compile the
8685 application, do RESOLVE_ALL, and `C-c C-i' to compile all referenced
8686 routines and update IDLWAVE internal info. Then check for shadowing
8687 with this command."
8688 (interactive "P")
8689 (cond
8690 ((or (not (fboundp 'idlwave-shell-is-running))
8691 (not (idlwave-shell-is-running)))
8692 (error "Shell is not running"))
8693 ((null idlwave-compiled-routines)
8694 (error "No compiled routines. Maybe you need to update with `C-c C-i'"))
8695 (t
8696 (idlwave-list-load-path-shadows nil idlwave-compiled-routines
8697 "in the shell"))))
8698
8699 (defun idlwave-list-all-load-path-shadows (&optional arg)
8700 "List the load path shadows of all routines known to IDLWAVE."
8701 (interactive "P")
8702 (idlwave-list-load-path-shadows nil nil "globally"))
8703
8704 (defun idlwave-list-load-path-shadows (arg &optional special-routines loc)
8705 "List the routines which are defined multiple times.
8706 Search the information IDLWAVE has about IDL routines for multiple
8707 definitions.
8708 When SPECIAL-ROUTINES in non-nil, only look for shadows of these routines.
8709
8710 When IDL hits a routine call which is not defined, it will search on
8711 the load path in order to find a definition. The output of this
8712 command can be used to detect possible name clashes during this process."
8713 (idlwave-routines) ; Make sure everything is loaded.
8714 (unless (or idlwave-user-catalog-routines idlwave-library-catalog-routines)
8715 (or (y-or-n-p
8716 "You don't have any user or library catalogs. Continue anyway? ")
8717 (error "Abort")))
8718 (let* ((routines (append idlwave-system-routines
8719 idlwave-compiled-routines
8720 idlwave-library-catalog-routines
8721 idlwave-user-catalog-routines
8722 idlwave-buffer-routines
8723 nil))
8724 (km-prop (if (featurep 'xemacs) 'keymap 'local-map))
8725 (keymap (make-sparse-keymap))
8726 (props (list 'mouse-face 'highlight
8727 km-prop keymap
8728 'help-echo "Mouse2: Find source"))
8729 (nroutines (length (or special-routines routines)))
8730 (step (/ nroutines 100))
8731 (n 0)
8732 (cnt 0)
8733 (idlwave-sort-prefer-buffer-info nil)
8734 routine twins dtwins twin done props1 lroutines)
8735
8736 (if special-routines
8737 ;; Just looking for shadows of a few special routines
8738 (setq lroutines routines
8739 routines special-routines))
8740
8741 (message "Sorting routines...")
8742 (setq routines (sort routines
8743 (lambda (a b)
8744 (string< (downcase (idlwave-make-full-name
8745 (nth 2 a) (car a)))
8746 (downcase (idlwave-make-full-name
8747 (nth 2 b) (car b)))))))
8748 (message "Sorting routines...done")
8749
8750 (define-key keymap (if (featurep 'xemacs) [(button2)] [(mouse-2)])
8751 (lambda (ev)
8752 (interactive "e")
8753 (mouse-set-point ev)
8754 (apply 'idlwave-do-find-module
8755 (get-text-property (point) 'find-args))))
8756 (define-key keymap [(return)]
8757 (lambda ()
8758 (interactive)
8759 (apply 'idlwave-do-find-module
8760 (get-text-property (point) 'find-args))))
8761 (message "Compiling list...( 0%%)")
8762 (save-excursion
8763 (set-buffer (get-buffer-create "*Shadows*"))
8764 (setq buffer-read-only nil)
8765 (erase-buffer)
8766 (while (setq routine (pop routines))
8767 (if (= (mod (setq n (1+ n)) step) 0)
8768 (message "Compiling list...(%2d%%)" (/ (* n 100) nroutines)))
8769
8770 ;; Get a list of all twins
8771 (setq twins (idlwave-routine-twins routine (or lroutines routines)))
8772 (if (memq routine done)
8773 (setq dtwins nil)
8774 (setq dtwins (idlwave-study-twins twins)))
8775 ;; Mark all twins as dealt with
8776 (setq done (append twins done))
8777 (when (or (> (length dtwins) 1)
8778 (> (idlwave-count-memq 'lib (nth 2 (car dtwins))) 1)
8779 (> (idlwave-count-memq 'user (nth 2 (car dtwins))) 1)
8780 (> (idlwave-count-memq 'buffer (nth 2 (car dtwins))) 1))
8781 (incf cnt)
8782 (insert (format "\n%s%s"
8783 (idlwave-make-full-name (nth 2 routine)
8784 (car routine))
8785 (if (eq (nth 1 routine) 'fun) "()" "")))
8786 (while (setq twin (pop dtwins))
8787 (setq props1 (append (list 'find-args
8788 (list (nth 0 routine)
8789 (nth 1 routine)
8790 (nth 2 routine)))
8791 props))
8792 (idlwave-insert-source-location "\n - " twin props1))))
8793 (goto-char (point-min))
8794 (setq buffer-read-only t))
8795 (setq loc (or loc ""))
8796 (if (> cnt 0)
8797 (progn
8798 (display-buffer (get-buffer "*Shadows*"))
8799 (message "%d case%s of shadowing found %s"
8800 cnt (if (= cnt 1) "" "s") loc))
8801 (message "No shadowing conflicts found %s" loc))))
8802
8803 (defun idlwave-print-source (routine)
8804 (let* ((source (nth 3 routine))
8805 (stype (car source))
8806 (sfile (idlwave-routine-source-file source)))
8807 (if (idlwave-syslib-p sfile) (setq stype 'syslib))
8808 (if (and (eq stype 'compiled)
8809 (or (not (stringp sfile))
8810 (not (string-match "\\S-" sfile))))
8811 (setq stype 'unresolved))
8812 (princ (format " %-10s %s\n"
8813 stype
8814 (if sfile sfile "No source code available")))))
8815
8816 (defun idlwave-routine-twins (entry &optional list)
8817 "Return all twin entries of ENTRY in LIST.
8818 LIST defaults to `idlwave-routines'.
8819 Twin entries are those which have the same name, type, and class.
8820 ENTRY will also be returned, as the first item of this list."
8821 (let* ((name (car entry))
8822 (type (nth 1 entry))
8823 (class (nth 2 entry))
8824 (candidates (idlwave-all-assq name (or list (idlwave-routines))))
8825 twins candidate)
8826 (while (setq candidate (pop candidates))
8827 (if (and (not (eq candidate entry))
8828 (eq type (nth 1 candidate))
8829 (eq class (nth 2 candidate)))
8830 (push candidate twins)))
8831 (if (setq candidate (idlwave-rinfo-assq name type class
8832 idlwave-unresolved-routines))
8833 (push candidate twins))
8834 (cons entry (nreverse twins))))
8835
8836 (defun idlwave-study-twins (entries)
8837 "Return dangerous twins of first entry in ENTRIES.
8838 Dangerous twins are routines with same name, but in different files on
8839 the load path. If a file is in the system library and has an entry in
8840 the `idlwave-system-routines' list, we omit the latter as
8841 non-dangerous because many IDL routines are implemented as library
8842 routines, and may have been scanned."
8843 (let* ((entry (car entries))
8844 (name (car entry)) ;
8845 (type (nth 1 entry)) ; Must be bound for
8846 (class (nth 2 entry)) ; idlwave-routine-twin-compare
8847 (cnt 0)
8848 source type type-cons file alist syslibp key)
8849 (while (setq entry (pop entries))
8850 (incf cnt)
8851 (setq source (nth 3 entry)
8852 type (car source)
8853 type-cons (cons type (nth 3 source))
8854 file (idlwave-routine-source-file source))
8855
8856 ;; Make KEY to index entry properly
8857 (setq key (cond ((eq type 'system) type)
8858 (file (file-truename file))
8859 (t 'unresolved)))
8860
8861 ;; Check for an entry in the system library
8862 (if (and file
8863 (not syslibp)
8864 (idlwave-syslib-p file))
8865 (setq syslibp t))
8866
8867 ;; If there's more than one matching entry for the same file, just
8868 ;; append the type-cons to the type list.
8869 (if (setq entry (assoc key alist))
8870 (push type-cons (nth 2 entry))
8871 (push (list key file (list type-cons)) alist)))
8872
8873 (setq alist (nreverse alist))
8874
8875 (when syslibp
8876 ;; File is in system *library* - remove any 'system entry
8877 (setq alist (delq (assq 'system alist) alist)))
8878
8879 ;; If 'system remains and we've scanned the syslib, it's a builtin
8880 ;; (rather than a !DIR/lib/.pro file bundled as source).
8881 (when (and (idlwave-syslib-scanned-p)
8882 (setq entry (assoc 'system alist)))
8883 (setcar entry 'builtin))
8884 (sort alist 'idlwave-routine-twin-compare)))
8885
8886 (defvar type)
8887 (defvar class)
8888 (defvar idlwave-sort-prefer-buffer-info t
8889 "Internal variable used to influence `idlwave-routine-twin-compare'.")
8890
8891 (defmacro idlwave-xor (a b)
8892 `(and (or ,a ,b)
8893 (not (and ,a ,b))))
8894
8895 (defun idlwave-routine-entry-compare (a b)
8896 "Compare two routine info entries for sortiung. This is the general case.
8897 It first compates class, names, and type. If it turns out that A and B
8898 are twins (same name, class, and type), calls another routine which
8899 compares twins on the basis of their file names and path locations."
8900 (let ((name (car a)) (type (nth 1 a)) (class (nth 2 a)))
8901 (cond
8902 ((not (equal (idlwave-downcase-safe class)
8903 (idlwave-downcase-safe (nth 2 b))))
8904 ;; Class decides
8905 (cond ((null (nth 2 b)) nil)
8906 ((null class) t)
8907 (t (string< (downcase class) (downcase (nth 2 b))))))
8908 ((not (equal (downcase name) (downcase (car b))))
8909 ;; Name decides
8910 (string< (downcase name) (downcase (car b))))
8911 ((not (eq type (nth 1 b)))
8912 ;; Type decides
8913 (< (if (eq type 'fun) 1 0) (if (eq (nth 1 b) 'fun) 1 0)))
8914 (t
8915 ;; A and B are twins - so the decision is more complicated.
8916 ;; Call twin-compare with the proper arguments.
8917 (idlwave-routine-entry-compare-twins a b)))))
8918
8919 (defun idlwave-routine-entry-compare-twins (a b)
8920 "Compare two routine entries, under the assumption that they are
8921 twins. This basically calls `idlwave-routine-twin-compare' with the
8922 correct args."
8923 (let* ((name (car a)) (type (nth 1 a)) (class (nth 2 a)) ; needed outside
8924 (asrc (nth 3 a))
8925 (atype (car asrc))
8926 (bsrc (nth 3 b))
8927 (btype (car bsrc))
8928 (afile (idlwave-routine-source-file asrc))
8929 (bfile (idlwave-routine-source-file bsrc)))
8930 (idlwave-routine-twin-compare
8931 (if (stringp afile)
8932 (list (file-truename afile) afile (list atype))
8933 (list atype afile (list atype)))
8934 (if (stringp bfile)
8935 (list (file-truename bfile) bfile (list btype))
8936 (list btype bfile (list btype))))
8937 ))
8938
8939 (defun idlwave-routine-twin-compare (a b)
8940 "Compare two routine twin entries for sorting.
8941 In here, A and B are not normal routine info entries, but special
8942 lists (KEY FILENAME (TYPES...)).
8943 This expects NAME TYPE CLASS to be bound to the right values."
8944 (let* (;; Dis-assemble entries
8945 (akey (car a)) (bkey (car b))
8946 (afile (nth 1 a)) (bfile (nth 1 b))
8947 (atypes (nth 2 a)) (btypes (nth 2 b))
8948 ;; System routines?
8949 (asysp (memq akey '(builtin system)))
8950 (bsysp (memq bkey '(builtin system)))
8951 ;; Compiled routines?
8952 (acompp (memq 'compiled atypes))
8953 (bcompp (memq 'compiled btypes))
8954 ;; Unresolved?
8955 (aunresp (or (eq akey 'unresolved)
8956 (and acompp (not afile))))
8957 (bunresp (or (eq bkey 'unresolved)
8958 (and bcompp (not bfile))))
8959 ;; Buffer info available?
8960 (abufp (memq 'buffer atypes))
8961 (bbufp (memq 'buffer btypes))
8962 ;; On search path?
8963 (tpath-alist (idlwave-true-path-alist))
8964 (apathp (and (stringp akey)
8965 (assoc (file-name-directory akey) tpath-alist)))
8966 (bpathp (and (stringp bkey)
8967 (assoc (file-name-directory bkey) tpath-alist)))
8968 ;; How early on search path? High number means early since we
8969 ;; measure the tail of the path list
8970 (anpath (length (memq apathp tpath-alist)))
8971 (bnpath (length (memq bpathp tpath-alist)))
8972 ;; Look at file names
8973 (aname (if (stringp afile) (downcase (file-name-nondirectory afile)) ""))
8974 (bname (if (stringp bfile) (downcase (file-name-nondirectory bfile)) ""))
8975 (fname-re (if class (format "\\`%s__\\(%s\\|define\\)\\.pro\\'"
8976 (regexp-quote (downcase class))
8977 (regexp-quote (downcase name)))
8978 (format "\\`%s\\.pro" (regexp-quote (downcase name)))))
8979 ;; Is file name derived from the routine name?
8980 ;; Method file or class definition file?
8981 (anamep (string-match fname-re aname))
8982 (adefp (and class anamep (string= "define" (match-string 1 aname))))
8983 (bnamep (string-match fname-re bname))
8984 (bdefp (and class bnamep (string= "define" (match-string 1 bname)))))
8985
8986 ;; Now: follow JD's ideas about sorting. Looks really simple now,
8987 ;; doesn't it? The difficult stuff is hidden above...
8988 (cond
8989 ((idlwave-xor asysp bsysp) asysp) ; System entries first
8990 ((idlwave-xor aunresp bunresp) bunresp) ; Unresolved last
8991 ((and idlwave-sort-prefer-buffer-info
8992 (idlwave-xor abufp bbufp)) abufp) ; Buffers before non-buffers
8993 ((idlwave-xor acompp bcompp) acompp) ; Compiled entries
8994 ((idlwave-xor apathp bpathp) apathp) ; Library before non-library
8995 ((idlwave-xor anamep bnamep) anamep) ; Correct file names first
8996 ((and class anamep bnamep ; both file names match ->
8997 (idlwave-xor adefp bdefp)) bdefp) ; __define after __method
8998 ((> anpath bnpath) t) ; Who is first on path?
8999 (t nil)))) ; Default
9000
9001 (defun idlwave-routine-source-file (source)
9002 (if (nth 2 source)
9003 (expand-file-name (nth 1 source) (nth 2 source))
9004 (nth 1 source)))
9005
9006 (defun idlwave-downcase-safe (string)
9007 "Donwcase if string, else return unchanged."
9008 (if (stringp string)
9009 (downcase string)
9010 string))
9011
9012 (defun idlwave-count-eq (elt list)
9013 "How often is ELT in LIST?"
9014 (length (delq nil (mapcar (lambda (x) (eq x elt)) list))))
9015
9016 (defun idlwave-count-memq (elt alist)
9017 "How often is ELT a key in ALIST?"
9018 (length (delq nil (mapcar (lambda (x) (eq (car x) elt)) alist))))
9019
9020 (defun idlwave-syslib-p (file)
9021 "Non-nil if FILE is in the system library."
9022 (let* ((true-syslib (file-name-as-directory
9023 (file-truename
9024 (expand-file-name "lib" (idlwave-sys-dir)))))
9025 (true-file (file-truename file)))
9026 (string-match (concat "^" (regexp-quote true-syslib)) true-file)))
9027
9028 (defun idlwave-lib-p (file)
9029 "Non-nil if file is in the library"
9030 (let ((true-dir (file-name-directory (file-truename file))))
9031 (assoc true-dir (idlwave-true-path-alist))))
9032
9033 (defun idlwave-path-alist-add-flag (list-entry flag)
9034 "Add a flag to the path list entry, if not set."
9035 (let ((flags (cdr list-entry)))
9036 (add-to-list 'flags flag)
9037 (setcdr list-entry flags)))
9038
9039 (defun idlwave-path-alist-remove-flag (list-entry flag)
9040 "Remove a flag to the path list entry, if set."
9041 (let ((flags (delq flag (cdr list-entry))))
9042 (setcdr list-entry flags)))
9043
9044 (defun idlwave-true-path-alist ()
9045 "Return `idlwave-path-alist' alist with true-names.
9046 Info is cached, but relies on the functions setting `idlwave-path-alist'
9047 to reset the variable `idlwave-true-path-alist' to nil."
9048 (or idlwave-true-path-alist
9049 (setq idlwave-true-path-alist
9050 (mapcar (lambda(x) (cons
9051 (file-name-as-directory
9052 (file-truename
9053 (directory-file-name
9054 (car x))))
9055 (cdr x)))
9056 idlwave-path-alist))))
9057
9058 (defun idlwave-syslib-scanned-p ()
9059 "Non-nil if the system lib file !DIR/lib has been scanned."
9060 (let* ((true-syslib (file-name-as-directory
9061 (file-truename
9062 (expand-file-name "lib" (idlwave-sys-dir))))))
9063 (cdr (assoc true-syslib (idlwave-true-path-alist)))))
9064
9065 ;; ----------------------------------------------------------------------------
9066 ;;
9067 ;; Online Help display
9068
9069
9070 ;; ----------------------------------------------------------------------------
9071 ;;
9072 ;; Additions for use with imenu.el and func-menu.el
9073 ;; (pop-up a list of IDL units in the current file).
9074 ;;
9075
9076 (defun idlwave-prev-index-position ()
9077 "Search for the previous procedure or function.
9078 Return nil if not found. For use with imenu.el."
9079 (save-match-data
9080 (cond
9081 ((idlwave-find-key "\\<\\(pro\\|function\\)\\>" -1 'nomark))
9082 ;; ((idlwave-find-key idlwave-begin-unit-reg 1 'nomark)
9083 (t nil))))
9084
9085 (defun idlwave-unit-name ()
9086 "Return the unit name.
9087 Assumes that point is at the beginning of the unit as found by
9088 `idlwave-prev-index-position'."
9089 (forward-sexp 2)
9090 (forward-sexp -1)
9091 (let ((begin (point)))
9092 (re-search-forward
9093 "[a-zA-Z_][a-zA-Z0-9$_]+\\(::[a-zA-Z_][a-zA-Z0-9$_]+\\)?")
9094 (if (fboundp 'buffer-substring-no-properties)
9095 (buffer-substring-no-properties begin (point))
9096 (buffer-substring begin (point)))))
9097
9098 (defalias 'idlwave-function-menu
9099 (condition-case nil
9100 (progn
9101 (require 'func-menu)
9102 'function-menu)
9103 (error (condition-case nil
9104 (progn
9105 (require 'imenu)
9106 'imenu)
9107 (error nil)))))
9108
9109 ;; Here we hack func-menu.el in order to support this new mode.
9110 ;; The latest versions of func-menu.el already have this stuff in, so
9111 ;; we hack only if it is not already there.
9112 (when (fboundp 'eval-after-load)
9113 (eval-after-load "func-menu"
9114 '(progn
9115 (or (assq 'idlwave-mode fume-function-name-regexp-alist)
9116 (not (boundp 'fume-function-name-regexp-idl)) ; avoid problems
9117 (setq fume-function-name-regexp-alist
9118 (cons '(idlwave-mode . fume-function-name-regexp-idl)
9119 fume-function-name-regexp-alist)))
9120 (or (assq 'idlwave-mode fume-find-function-name-method-alist)
9121 (not (fboundp 'fume-find-next-idl-function-name)) ; avoid problems
9122 (setq fume-find-function-name-method-alist
9123 (cons '(idlwave-mode . fume-find-next-idl-function-name)
9124 fume-find-function-name-method-alist))))))
9125
9126 (defun idlwave-edit-in-idlde ()
9127 "Edit the current file in IDL Development environment."
9128 (interactive)
9129 (start-process "idldeclient" nil
9130 idlwave-shell-explicit-file-name "-c" "-e"
9131 (buffer-file-name)))
9132
9133 (defvar idlwave-help-use-assistant)
9134 (defun idlwave-launch-idlhelp ()
9135 "Start the IDLhelp application."
9136 (interactive)
9137 (if idlwave-help-use-assistant
9138 (idlwave-help-assistant-raise)
9139 (start-process "idlhelp" nil idlwave-help-application)))
9140
9141 ;; Menus - using easymenu.el
9142 (defvar idlwave-mode-menu-def
9143 `("IDLWAVE"
9144 ["PRO/FUNC menu" idlwave-function-menu t]
9145 ("Motion"
9146 ["Subprogram Start" idlwave-beginning-of-subprogram t]
9147 ["Subprogram End" idlwave-end-of-subprogram t]
9148 ["Block Start" idlwave-beginning-of-block t]
9149 ["Block End" idlwave-end-of-block t]
9150 ["Up Block" idlwave-backward-up-block t]
9151 ["Down Block" idlwave-down-block t]
9152 ["Skip Block Backward" idlwave-backward-block t]
9153 ["Skip Block Forward" idlwave-forward-block t])
9154 ("Mark"
9155 ["Subprogram" idlwave-mark-subprogram t]
9156 ["Block" idlwave-mark-block t]
9157 ["Header" idlwave-mark-doclib t])
9158 ("Format"
9159 ["Indent Entire Statement" idlwave-indent-statement
9160 :active t :keys "C-u \\[indent-for-tab-command]" ]
9161 ["Indent Subprogram" idlwave-indent-subprogram t]
9162 ["(Un)Comment Region" idlwave-toggle-comment-region t]
9163 ["Continue/Split line" idlwave-split-line t]
9164 "--"
9165 ["Toggle Auto Fill" idlwave-auto-fill-mode :style toggle
9166 :selected (symbol-value idlwave-fill-function)])
9167 ("Templates"
9168 ["Procedure" idlwave-procedure t]
9169 ["Function" idlwave-function t]
9170 ["Doc Header" idlwave-doc-header t]
9171 ["Log" idlwave-doc-modification t]
9172 "--"
9173 ["Case" idlwave-case t]
9174 ["For" idlwave-for t]
9175 ["Repeat" idlwave-repeat t]
9176 ["While" idlwave-while t]
9177 "--"
9178 ["Close Block" idlwave-close-block t])
9179 ("Completion"
9180 ["Complete" idlwave-complete t]
9181 ("Complete Specific"
9182 ["1 Procedure Name" (idlwave-complete 'procedure) t]
9183 ["2 Procedure Keyword" (idlwave-complete 'procedure-keyword) t]
9184 "--"
9185 ["3 Function Name" (idlwave-complete 'function) t]
9186 ["4 Function Keyword" (idlwave-complete 'function-keyword) t]
9187 "--"
9188 ["5 Procedure Method Name" (idlwave-complete 'procedure-method) t]
9189 ["6 Procedure Method Keyword" (idlwave-complete 'procedure-method-keyword) t]
9190 "--"
9191 ["7 Function Method Name" (idlwave-complete 'function-method) t]
9192 ["8 Function Method Keyword" (idlwave-complete 'function-method-keyword) t]
9193 "--"
9194 ["9 Class Name" idlwave-complete-class t]))
9195 ("Routine Info"
9196 ["Show Routine Info" idlwave-routine-info t]
9197 ["Online Context Help" idlwave-context-help t]
9198 "--"
9199 ["Find Routine Source" idlwave-find-module t]
9200 ["Resolve Routine" idlwave-resolve (featurep 'idlw-shell)]
9201 "--"
9202 ["Update Routine Info" idlwave-update-routine-info t]
9203 ["Rescan XML Help Catalog" idlwave-convert-xml-system-routine-info t]
9204 "--"
9205 "IDL User Catalog"
9206 ["Select Catalog Directories" (idlwave-create-user-catalog-file nil) t]
9207 ["Scan Directories" (idlwave-update-routine-info '(16))
9208 (and idlwave-path-alist (not idlwave-catalog-process))]
9209 ["Scan Directories &" (idlwave-update-routine-info '(64))
9210 (and idlwave-path-alist (not idlwave-catalog-process))]
9211 "--"
9212 "Routine Shadows"
9213 ["Check Current Buffer" idlwave-list-buffer-load-path-shadows t]
9214 ["Check Compiled Routines" idlwave-list-shell-load-path-shadows t]
9215 ["Check Everything" idlwave-list-all-load-path-shadows t])
9216 ("Misc"
9217 ["Kill auto-created buffers" idlwave-kill-autoloaded-buffers t]
9218 "--"
9219 ["Insert TAB character" idlwave-hard-tab t])
9220 "--"
9221 ("External"
9222 ["Start IDL shell" idlwave-shell t]
9223 ["Edit file in IDLDE" idlwave-edit-in-idlde t]
9224 ["Launch IDL Help" idlwave-launch-idlhelp t])
9225 "--"
9226 ("Customize"
9227 ["Browse IDLWAVE Group" idlwave-customize t]
9228 "--"
9229 ["Build Full Customize Menu" idlwave-create-customize-menu
9230 (fboundp 'customize-menu-create)])
9231 ("Documentation"
9232 ["Describe Mode" describe-mode t]
9233 ["Abbreviation List" idlwave-list-abbrevs t]
9234 "--"
9235 ["Commentary in idlwave.el" idlwave-show-commentary t]
9236 ["Commentary in idlw-shell.el" idlwave-shell-show-commentary t]
9237 "--"
9238 ["Info" idlwave-info t]
9239 "--"
9240 ["Help with Topic" idlwave-help-assistant-help-with-topic
9241 idlwave-help-use-assistant]
9242 ["Launch IDL Help" idlwave-launch-idlhelp t])))
9243
9244 (defvar idlwave-mode-debug-menu-def
9245 '("Debug"
9246 ["Start IDL shell" idlwave-shell t]
9247 ["Save and .RUN buffer" idlwave-shell-save-and-run
9248 (and (boundp 'idlwave-shell-automatic-start)
9249 idlwave-shell-automatic-start)]))
9250
9251 (if (or (featurep 'easymenu) (load "easymenu" t))
9252 (progn
9253 (easy-menu-define idlwave-mode-menu idlwave-mode-map
9254 "IDL and WAVE CL editing menu"
9255 idlwave-mode-menu-def)
9256 (easy-menu-define idlwave-mode-debug-menu idlwave-mode-map
9257 "IDL and WAVE CL editing menu"
9258 idlwave-mode-debug-menu-def)))
9259
9260 (defun idlwave-customize ()
9261 "Call the customize function with idlwave as argument."
9262 (interactive)
9263 ;; Try to load the code for the shell, so that we can customize it
9264 ;; as well.
9265 (or (featurep 'idlw-shell)
9266 (load "idlw-shell" t))
9267 (customize-browse 'idlwave))
9268
9269 (defun idlwave-create-customize-menu ()
9270 "Create a full customization menu for IDLWAVE, insert it into the menu."
9271 (interactive)
9272 (if (fboundp 'customize-menu-create)
9273 (progn
9274 ;; Try to load the code for the shell, so that we can customize it
9275 ;; as well.
9276 (or (featurep 'idlw-shell)
9277 (load "idlw-shell" t))
9278 (easy-menu-change
9279 '("IDLWAVE") "Customize"
9280 `(["Browse IDLWAVE group" idlwave-customize t]
9281 "--"
9282 ,(customize-menu-create 'idlwave)
9283 ["Set" Custom-set t]
9284 ["Save" Custom-save t]
9285 ["Reset to Current" Custom-reset-current t]
9286 ["Reset to Saved" Custom-reset-saved t]
9287 ["Reset to Standard Settings" Custom-reset-standard t]))
9288 (message "\"IDLWAVE\"-menu now contains full customization menu"))
9289 (error "Cannot expand menu (outdated version of cus-edit.el)")))
9290
9291 (defun idlwave-show-commentary ()
9292 "Use the finder to view the file documentation from `idlwave.el'."
9293 (interactive)
9294 (require 'finder)
9295 (finder-commentary "idlwave.el"))
9296
9297 (defun idlwave-shell-show-commentary ()
9298 "Use the finder to view the file documentation from `idlw-shell.el'."
9299 (interactive)
9300 (require 'finder)
9301 (finder-commentary "idlw-shell.el"))
9302
9303 (defun idlwave-info ()
9304 "Read documentation for IDLWAVE in the info system."
9305 (interactive)
9306 (require 'info)
9307 (Info-goto-node "(idlwave)"))
9308
9309 (defun idlwave-list-abbrevs (arg)
9310 "Show the code abbreviations define in IDLWAVE mode.
9311 This lists all abbrevs where the replacement text differs from the input text.
9312 These are the ones the users want to learn to speed up their writing.
9313
9314 The function does *not* list abbrevs which replace a word with itself
9315 to call a hook. These hooks are used to change the case of words or
9316 to blink the matching `begin', and the user does not need to know them.
9317
9318 With arg, list all abbrevs with the corresponding hook.
9319
9320 This function was written since `list-abbrevs' looks terrible for IDLWAVE mode."
9321
9322 (interactive "P")
9323 (let ((table (symbol-value 'idlwave-mode-abbrev-table))
9324 abbrevs
9325 str rpl func fmt (len-str 0) (len-rpl 0))
9326 (mapatoms
9327 (lambda (sym)
9328 (if (symbol-value sym)
9329 (progn
9330 (setq str (symbol-name sym)
9331 rpl (symbol-value sym)
9332 func (symbol-function sym))
9333 (if arg
9334 (setq func (prin1-to-string func))
9335 (if (and (listp func) (stringp (nth 2 func)))
9336 (setq rpl (concat "EVAL: " (nth 2 func))
9337 func "")
9338 (setq func "")))
9339 (if (or arg (not (string= rpl str)))
9340 (progn
9341 (setq len-str (max len-str (length str)))
9342 (setq len-rpl (max len-rpl (length rpl)))
9343 (setq abbrevs (cons (list str rpl func) abbrevs)))))))
9344 table)
9345 ;; sort the list
9346 (setq abbrevs (sort abbrevs (lambda (a b) (string< (car a) (car b)))))
9347 ;; Make the format
9348 (setq fmt (format "%%-%ds %%-%ds %%s\n" len-str len-rpl))
9349 (with-output-to-temp-buffer "*Help*"
9350 (if arg
9351 (progn
9352 (princ "Abbreviations and Actions in IDLWAVE-Mode\n")
9353 (princ "=========================================\n\n")
9354 (princ (format fmt "KEY" "REPLACE" "HOOK"))
9355 (princ (format fmt "---" "-------" "----")))
9356 (princ "Code Abbreviations and Templates in IDLWAVE-Mode\n")
9357 (princ "================================================\n\n")
9358 (princ (format fmt "KEY" "ACTION" ""))
9359 (princ (format fmt "---" "------" "")))
9360 (mapcar
9361 (lambda (list)
9362 (setq str (car list)
9363 rpl (nth 1 list)
9364 func (nth 2 list))
9365 (princ (format fmt str rpl func)))
9366 abbrevs)))
9367 ;; Make sure each abbreviation uses only one display line
9368 (save-excursion
9369 (set-buffer "*Help*")
9370 (setq truncate-lines t)))
9371
9372 ;; Add .pro files to speedbar for support, if it's loaded
9373 (eval-after-load "speedbar" '(speedbar-add-supported-extension ".pro"))
9374
9375 ;; Set an idle timer to load the routine info.
9376 ;; Will only work on systems which support this.
9377 (or idlwave-routines (idlwave-start-load-rinfo-timer))
9378
9379 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.[Pp][Rr][Oo]\\'" . idlwave-mode))
9380
9381 ;; Run the hook
9382 (run-hooks 'idlwave-load-hook)
9383
9384 (provide 'idlwave)
9385
9386 ;; arch-tag: f77f3b0c-c37c-424f-a328-0886fd42b6fb
9387 ;;; idlwave.el ends here