]> code.delx.au - gnu-emacs/blob - lisp/progmodes/idlw-help.el
(grep-mode-font-lock-keywords): Use the font-lock-face property to
[gnu-emacs] / lisp / progmodes / idlw-help.el
1 ;;; idlw-help.el --- HTML Help code for IDLWAVE
2 ;; Copyright (c) 2000 Carsten Dominik
3 ;; Copyright (c) 2001, 2002 J.D. Smith
4 ;; Copyright (c) 2003,2004,2005 Free Software Foundation
5 ;;
6 ;; Authors: J.D. Smith <jdsmith@as.arizona.edu>
7 ;; Carsten Dominik <dominik@astro.uva.nl>
8 ;; Maintainer: J.D. Smith <jdsmith@as.arizona.edu>
9 ;; Version: 5.5
10
11 ;; This file is part of GNU Emacs.
12
13 ;; This file is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
17
18 ;; This file is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
27
28 ;;; Commentary:
29
30 ;; The help link information for IDLWAVE's online help feature for
31 ;; system routines is extracted automatically from the IDL
32 ;; documentation, and is available, along with general routine
33 ;; information, in the file idlw-rinfo.el. The HTML help file
34 ;; themselves are not distributable with Emacs, but are available,
35 ;; along with new versions of IDLWAVE, documentation, and more
36 ;; information, at:
37 ;;
38 ;; http://idlwave.org
39 ;;
40 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
41 \f
42
43 ;;; Code:
44 (require 'browse-url)
45
46 (defgroup idlwave-online-help nil
47 "Online Help options for IDLWAVE mode."
48 :group 'idlwave)
49
50 (defcustom idlwave-html-help-pre-v6 nil
51 "Whether pre or post-v6.0 IDL help documents are being used."
52 :group 'idlwave-online-help
53 :type 'boolean)
54
55 (defvar idlwave-html-link-sep
56 (if idlwave-html-help-pre-v6 "#" "#wp"))
57
58 (defcustom idlwave-html-help-location
59 (if (memq system-type '(ms-dos windows-nt))
60 nil
61 "/usr/local/etc/")
62 "The directory where the idl_html_help/ dir or idl.chm help file
63 (Windows only) lives."
64 :group 'idlwave-online-help
65 :type 'directory)
66
67 (defcustom idlwave-help-use-hh nil
68 "Whether to use the HTMLHelp viewer with idl.chm (Windows only)."
69 :group 'idlwave-online-help
70 :type '(choice :tag "use help viewer"
71 (const :tag "<none>" nil)
72 (const :tag "hh" 'hh)
73 (const :tag "keyhh" 'keyhh)))
74
75 (defcustom idlwave-help-browser-function browse-url-browser-function
76 "Function to use to display html help.
77 Defaults to `browse-url-browser-function', which see."
78 :group 'idlwave-online-help
79 :type 'function)
80
81 (defcustom idlwave-help-browser-generic-program browse-url-generic-program
82 "Program to run if using browse-url-generic-program."
83 :group 'idlwave-online-help
84 :type 'string)
85
86 (defcustom idlwave-help-browser-generic-args
87 (if (boundp 'browse-url-generic-args)
88 browse-url-generic-args "")
89 "Program args to use if using browse-url-generic-program."
90 :group 'idlwave-online-help
91 :type 'string)
92
93 (defcustom idlwave-help-browser-is-local nil
94 "Whether the browser will display locally in an Emacs window.
95 Several browsers run and/or display inside Emacs windows, but most are
96 external programs. If the browser name contains \"-w3\", it is
97 assumed to be local to Emacs. For other local browsers, this variable
98 must be explicitly set non-nil in order for the variable
99 `idlwave-help-use-dedicated-frame' to function."
100 :group 'idlwave-online-help
101 :type 'boolean)
102
103 (defvar idlwave-help-directory ""
104 "Obsolete variable. See idlwave-html-help-location.")
105
106 (defcustom idlwave-help-use-dedicated-frame t
107 "*Non-nil means, use a separate frame for Online Help if possible."
108 :group 'idlwave-online-help
109 :type 'boolean)
110
111 (defcustom idlwave-help-frame-parameters
112 '((height . 32) (unsplittable . t))
113 "The frame parameters for the special Online Help frame.
114 See also `idlwave-help-use-dedicated-frame'.
115 If you do not set the frame width here, the value specified in
116 `idlw-help.el' will be used."
117 :group 'idlwave-online-help
118 :type '(repeat
119 (cons symbol sexp)))
120
121 (defcustom idlwave-max-popup-menu-items 20
122 "Maximum number of items per pane in popup menus.
123 Currently only used for class selection during completion help."
124 :group 'idlwave-online-help
125 :type 'integer)
126
127 (defcustom idlwave-extra-help-function 'idlwave-help-with-source
128 "The function to call for online help if the normal help fails.
129 Online help works only for system routines which are described in the
130 IDL manuals. A function may be specified to access help from other sources.
131
132 The function must accept four arguments: NAME, TYPE, CLASS, KEYWORD.
133 The Help buffer is current when this function is called, and the help
134 text should be loaded into this buffer. If help is found, the
135 function should return the buffer position which should be used as
136 `window-start' in the help window. Also, the variable
137 `idlwave-help-mode-line-indicator' should be set to a useful string,
138 which will be displayed in the mode line of the help window. If
139 should also set the variable `idlwave-help-min-frame-width' to a
140 positive integer. IDLWAVE will ensure that the help frame is at least
141 that many columns wide. Failure to find help should be indicated by
142 throwing an error.
143
144 When this variable is non-nil, IDLWAVE will allow the mouse-3 help click
145 for every routine and keyword, even though the item may not be highlighted
146 in blue (indicating the availability of system documentation).
147
148 The default value for this function is `idlwave-help-with-source' which
149 loads the routine source file into the help buffer. If you try to write
150 a different function which accesses a special help file or so, it is
151 probably a good idea to still call this function as a fallback."
152 :group 'idlwave-online-help
153 :type 'symbol)
154
155 (defcustom idlwave-help-fontify-source-code nil
156 "*Non-nil means, fontify source code displayed as help like normal code."
157 :group 'idlwave-online-help
158 :type 'boolean)
159
160 (defcustom idlwave-help-source-try-header t
161 "*Non-nil means, try to find help in routine header when displaying source.
162 Routines which are not documented in the system manual use their source as
163 help text. When this variable is non-nil, we try to find a description of
164 the help item in the first routine doclib header above the routine definition.
165 If the variable is nil, or if we cannot find/parse the header, the routine
166 definition is displayed instead."
167 :group 'idlwave-online-help
168 :type 'boolean)
169
170
171 (defcustom idlwave-help-doclib-name "name"
172 "*A regexp for the heading word to search for in doclib headers
173 which specifies the `name' section. Can be used for localization
174 support."
175 :group 'idlwave-online-help
176 :type 'string)
177
178 (defcustom idlwave-help-doclib-keyword "KEYWORD"
179 "*A regexp for the heading word to search for in doclib headers
180 which specifies the `keywords' section. Can be used for localization
181 support."
182 :group 'idlwave-online-help
183 :type 'string)
184
185 (defface idlwave-help-link-face
186 '((((min-colors 88) (class color)) (:foreground "Blue1"))
187 (((class color)) (:foreground "Blue"))
188 (t (:weight bold)))
189 "Face for highlighting links into IDLWAVE online help."
190 :group 'idlwave-online-help)
191
192 (defvar idlwave-help-activate-links-aggressively nil
193 "Obsolete variable.")
194
195 (defvar idlwave-completion-help-info)
196
197 (defvar idlwave-help-frame nil
198 "The frame for display of IDL online help.")
199 (defvar idlwave-help-frame-width 102
200 "The default width of the help frame.")
201
202 (defvar idlwave-html-help-is-available nil
203 "Is the system online help text avaiable?")
204
205 (defvar idlwave-help-mode-line-indicator ""
206 "Used for the special mode line in the idlwave-help-mode.")
207
208 (defvar idlwave-help-window-configuration nil)
209 (defvar idlwave-help-special-topic-words nil) ; defined by get_rinfo
210
211 ;; Define the key bindings for the Help application
212
213 (defvar idlwave-help-mode-map (make-sparse-keymap)
214 "The keymap used in idlwave-help-mode.")
215
216 (define-key idlwave-help-mode-map "q" 'idlwave-help-quit)
217 (define-key idlwave-help-mode-map "w" 'widen)
218 (define-key idlwave-help-mode-map "\C-m" (lambda (arg)
219 (interactive "p")
220 (scroll-up arg)))
221 (define-key idlwave-help-mode-map " " 'scroll-up)
222 (define-key idlwave-help-mode-map [delete] 'scroll-down)
223 (define-key idlwave-help-mode-map "h" 'idlwave-help-find-header)
224 (define-key idlwave-help-mode-map "H" 'idlwave-help-find-first-header)
225 (define-key idlwave-help-mode-map "." 'idlwave-help-toggle-header-match-and-def)
226 (define-key idlwave-help-mode-map "F" 'idlwave-help-fontify)
227 (define-key idlwave-help-mode-map "\M-?" 'idlwave-help-return-to-calling-frame)
228 (define-key idlwave-help-mode-map "x" 'idlwave-help-return-to-calling-frame)
229
230 ;; Define the menu for the Help application
231
232 (easy-menu-define
233 idlwave-help-menu idlwave-help-mode-map
234 "Menu for Help IDLWAVE system"
235 '("IDLHelp"
236 ["Definition <-> Help Text" idlwave-help-toggle-header-match-and-def t]
237 ["Find DocLib Header" idlwave-help-find-header t]
238 ["Find First DocLib Header" idlwave-help-find-first-header t]
239 ["Fontify help buffer" idlwave-help-fontify t]
240 "--"
241 ["Quit" idlwave-help-quit t]))
242
243 (defun idlwave-help-mode ()
244 "Major mode for displaying IDL Help.
245
246 This is a VIEW mode for the ASCII version of IDL Help files,
247 with some extras. Its main purpose is speed - so don't
248 expect a fully hyper-linked help.
249
250 Scrolling: SPC DEL RET
251 Text Searches: Inside Topic: Use Emacs search functions
252 Exit: [q]uit or mouse button 3 will kill the frame
253
254 When the hep text is a source file, the following commands are available
255
256 Fontification: [F]ontify the buffer like source code
257 Jump: [h] to function doclib header
258 [H] to file doclib header
259 [.] back and forward between header and definition
260
261 Here are all keybindings.
262 \\{idlwave-help-mode-map}"
263 (kill-all-local-variables)
264 (buffer-disable-undo)
265 (setq major-mode 'idlwave-help-mode
266 mode-name "IDLWAVE Help")
267 (use-local-map idlwave-help-mode-map)
268 (easy-menu-add idlwave-help-menu idlwave-help-mode-map)
269 (setq truncate-lines t)
270 (setq case-fold-search t)
271 (setq mode-line-format
272 (list ""
273 'mode-line-modified
274 'mode-line-buffer-identification
275 ": " 'idlwave-help-mode-line-indicator
276 " -%-"))
277 (setq buffer-read-only t)
278 (set (make-local-variable 'idlwave-help-def-pos) nil)
279 (set (make-local-variable 'idlwave-help-args) nil)
280 (set (make-local-variable 'idlwave-help-in-header) nil)
281 (run-hooks 'idlwave-help-mode-hook))
282
283 (defun idlwave-html-help-location ()
284 "Return the help directory where HTML files are, or nil if that is unknown."
285 (or (and (stringp idlwave-html-help-location)
286 (> (length idlwave-html-help-location) 0)
287 (file-directory-p idlwave-html-help-location)
288 idlwave-html-help-location)
289 (getenv "IDLWAVE_HELP_LOCATION")
290 (and (memq system-type '(ms-dos windows-nt)) ; Base it on sysdir
291 idlwave-help-use-hh
292 (stringp idlwave-system-directory)
293 (> (length idlwave-system-directory) 0)
294 (file-directory-p idlwave-system-directory)
295 (expand-file-name "HELP" idlwave-system-directory))))
296
297 (defvar idlwave-current-obj_new-class)
298 (defvar idlwave-help-diagnostics)
299 (defvar idlwave-experimental)
300 (defvar idlwave-last-context-help-pos)
301 (defun idlwave-do-context-help (&optional arg)
302 "Wrapper around the call to idlwave-context-help1.
303 It collects and prints the diagnostics messages."
304 (let ((marker (list (current-buffer) (point)))
305 (idlwave-help-diagnostics nil))
306 ;; Check for frame switching. When the command is invoked twice
307 ;; at the same position, we try to switch to the help frame
308 ;; FIXME: Frame switching works only on XEmacs
309 (if (and idlwave-experimental
310 (equal last-command this-command)
311 (equal idlwave-last-context-help-pos marker))
312 (idlwave-help-select-help-frame)
313 ;; Do the real thing.
314 (setq idlwave-last-context-help-pos marker)
315 (idlwave-do-context-help1 arg)
316 (if idlwave-help-diagnostics
317 (message "%s" (mapconcat 'identity
318 (nreverse idlwave-help-diagnostics)
319 "; "))))))
320
321 (defvar idlwave-help-do-class-struct-tag nil)
322 (defvar idlwave-help-do-struct-tag nil)
323 (defun idlwave-do-context-help1 (&optional arg)
324 "The work-horse version of `idlwave-context-help', which see."
325 (save-excursion
326 (if (equal (char-after) ?/)
327 (forward-char 1)
328 (if (equal (char-before) ?=)
329 (backward-char 1)))
330 (let* ((idlwave-query-class nil)
331 (idlwave-force-class-query (equal arg '(4)))
332 (chars "a-zA-Z0-9_$.!")
333 (beg (save-excursion (skip-chars-backward chars) (point)))
334 (end (save-excursion (skip-chars-forward chars) (point)))
335 (this-word (buffer-substring-no-properties beg end))
336 (st-ass (assoc (downcase this-word)
337 idlwave-help-special-topic-words))
338 (classtag (and (string-match "self\\." this-word)
339 (< beg (- end 4))))
340 (structtag (and (fboundp 'idlwave-complete-structure-tag)
341 (string-match "\\`\\([^.]+\\)\\." this-word)
342 (< beg (- end 4))))
343 module keyword cw mod1 mod2 mod3)
344 (if (or arg
345 (and (not st-ass)
346 (not classtag)
347 (not structtag)
348 (not (member (string-to-char this-word) '(?! ?.)))))
349 ;; Need the module information
350 (progn
351 ;; MODULE is (name type class), for this or any inheriting class
352 (setq module (idlwave-what-module-find-class)
353 cw (nth 2 (idlwave-where))) ;what would we complete here?
354 ;; Correct for OBJ_NEW, we may need an INIT method instead.
355 (if (equal (idlwave-downcase-safe (car module)) "obj_new")
356 (let* ((bos (save-excursion (idlwave-beginning-of-statement)
357 (point)))
358 (str (buffer-substring bos (point))))
359 (if (string-match "OBJ_NEW([ \t]*['\"]\\([a-zA-Z][a-zA-Z0-9$_]+\\)['\"]" str)
360 (setq module (list "init" 'fun (match-string 1 str))
361 idlwave-current-obj_new-class (match-string 1 str))
362 )))))
363 (cond
364 (arg (setq mod1 module))
365
366 ;; A special topic -- only system help
367 (st-ass (setq mod1 (list (cdr st-ass))))
368
369 ;; A system variable -- only system help
370 ((string-match
371 "\\`!\\([a-zA-Z0-9_]+\\)\\(\.\\([A-Za-z0-9_]+\\)\\)?"
372 this-word)
373 (let* ((word (match-string-no-properties 1 this-word))
374 (entry (assq (idlwave-sintern-sysvar word)
375 idlwave-system-variables-alist))
376 (tag (match-string-no-properties 3 this-word))
377 (tag-target (if tag
378 (cdr
379 (assq (idlwave-sintern-sysvartag tag)
380 (cdr (assq 'tags entry))))))
381 (link (nth 1 (assq 'link entry))))
382 (if tag-target
383 (setq link (idlwave-substitute-link-target link
384 tag-target)))
385 (setq mod1 (list link))))
386
387 ;; An executive command -- only system help
388 ((string-match "^\\.\\([A-Z_]+\\)" this-word)
389 (let* ((word (match-string 1 this-word))
390 (link (cdr (assoc-ignore-case
391 word
392 idlwave-executive-commands-alist))))
393 (setq mod1 (list link))))
394
395 ;; A class -- system OR in-text help (via class__define).
396 ((and (eq cw 'class)
397 (or (idlwave-in-quote) ; e.g. obj_new
398 (re-search-backward "\\<inherits[ \t]+[A-Za-z0-9_]*\\="
399 (max (point-min) (- (point) 40)) t)))
400 ;; Class completion inside string delimiters must be
401 ;; the class inside OBJ_NEW.
402 (let* ((entry (assq
403 (idlwave-sintern-class this-word)
404 idlwave-system-class-info))
405 (name (concat (downcase this-word) "__define"))
406 (link (nth 1 (assq 'link entry))))
407 (setq mod1 (list link name 'pro))))
408
409 ;; A class structure tag (self.BLAH) -- only in-text help available
410 (classtag
411 (let ((tag (substring this-word (match-end 0)))
412 class-with found-in)
413 (when (setq class-with
414 (idlwave-class-or-superclass-with-tag
415 (nth 2 (idlwave-current-routine))
416 tag))
417 (setq found-in (idlwave-class-found-in class-with))
418 (if (assq (idlwave-sintern-class class-with)
419 idlwave-system-class-info)
420 (error "No help available for system class tags"))
421 (setq idlwave-help-do-class-struct-tag t)
422 (setq mod1 (list nil
423 (if found-in
424 (cons (concat found-in "__define") class-with)
425 (concat class-with "__define"))
426 'pro
427 nil ; no class.... it's a procedure!
428 tag)))))
429
430 ;; A regular structure tag -- only in text, and if
431 ;; optional `complete-structtag' loaded.
432 (structtag
433 (let ((var (match-string 1 this-word))
434 (tag (substring this-word (match-end 0))))
435 ;; Check if we need to update the "current" structure
436 (idlwave-prepare-structure-tag-completion var)
437 (setq idlwave-help-do-struct-tag
438 idlwave-structtag-struct-location
439 mod1 (list nil nil nil nil tag))))
440
441 ;; A routine keyword -- in text or system help
442 ((and (memq cw '(function-keyword procedure-keyword))
443 (stringp this-word)
444 (string-match "\\S-" this-word)
445 (not (string-match "!" this-word)))
446 (cond ((or (= (char-before beg) ?/)
447 (save-excursion (goto-char end)
448 (looking-at "[ \t]*=")))
449 ;; Certainly a keyword. Check for abbreviation etc.
450 (setq keyword (idlwave-expand-keyword this-word module))
451 (cond
452 ((null keyword)
453 (idlwave-help-diagnostics
454 (format "%s does not accept `%s' kwd"
455 (idlwave-make-full-name (nth 2 module)
456 (car module))
457 (upcase this-word))
458 'ding))
459 ((consp keyword)
460 (idlwave-help-diagnostics
461 (format "%d matches for kwd abbrev `%s'"
462 (length keyword) this-word)
463 'ding)
464 ;; We continue anyway with the first match...
465 (setq keyword (car keyword))))
466 ;; Keyword, or just module
467 (setq mod1 (append (list t) module (list keyword)))
468 (setq mod2 (append (list t) module)))
469 ((equal (char-after end) ?\()
470 ;; A function - what-module will have caught this
471 (setq mod1 (append (list t) module)))
472 (t
473 ;; undecided - try function, keyword, then enclosing mod.
474 ;; Check for keyword abbreviations, but do not report
475 ;; errors, because it might be something else.
476 ;; FIXME: is this a good way to handle this?
477 (setq keyword (idlwave-expand-keyword this-word module))
478 (if (consp keyword) (setq keyword (car keyword)))
479 (setq mod1 (append (list t) module (list keyword))
480 mod2 (list t this-word 'fun nil)
481 mod3 (append (list t) module)))))
482
483 ;; Everything else
484 (t
485 (setq mod1 (append (list t) module))))
486 (if mod3
487 (condition-case nil
488 (apply 'idlwave-online-help mod1)
489 (error (condition-case nil
490 (apply 'idlwave-online-help mod2)
491 (error (apply 'idlwave-online-help mod3)))))
492 (if mod2
493 (condition-case nil
494 (apply 'idlwave-online-help mod1)
495 (error (apply 'idlwave-online-help mod2)))
496 (if mod1
497 (apply 'idlwave-online-help mod1)
498 (error "Don't know which item to show help for")))))))
499
500 (defun idlwave-do-mouse-completion-help (ev)
501 "Display online help on an item in the *Completions* buffer.
502 Needs additional info stored in global `idlwave-completion-help-info'."
503 (let* ((cw (selected-window))
504 (info idlwave-completion-help-info) ; global passed in
505 (what (nth 0 info))
506 (name (nth 1 info))
507 (type (nth 2 info))
508 (class (nth 3 info))
509 (need-class class)
510 (kwd (nth 4 info))
511 (sclasses (nth 5 info))
512 word link)
513 (mouse-set-point ev)
514
515
516 ;; See if we can also find help somewhere, e.g. for multiple classes
517 (setq word (idlwave-this-word))
518 (if (string= word "")
519 (error "No help item selected"))
520 (setq link (get-text-property 0 'link word))
521 (select-window cw)
522 (cond
523 ;; Routine name
524 ((memq what '(procedure function routine))
525 (setq name word)
526 (if (or (eq class t)
527 (and (stringp class) sclasses))
528 (let* ((classes (idlwave-all-method-classes
529 (idlwave-sintern-method name)
530 type)))
531 (setq link t) ; No specific link valid yet
532 (if sclasses
533 (setq classes (idlwave-members-only
534 classes (cons class sclasses))))
535 (setq class (idlwave-popup-select ev classes
536 "Select Class" 'sort))))
537
538 ;; XXX is this necessary, given all-method-classes?
539 (if (stringp class)
540 (setq class (idlwave-find-inherited-class
541 (idlwave-sintern-routine-or-method name class)
542 type (idlwave-sintern-class class)))))
543
544 ;; Keyword
545 ((eq what 'keyword)
546 (setq kwd word)
547 (if (or (eq class t)
548 (and (stringp class) sclasses))
549 (let ((classes (idlwave-all-method-keyword-classes
550 (idlwave-sintern-method name)
551 (idlwave-sintern-keyword kwd)
552 type)))
553 (setq link t) ; Link can't be correct yet
554 (if sclasses
555 (setq classes (idlwave-members-only
556 classes (cons class sclasses))))
557 (setq class (idlwave-popup-select ev classes
558 "Select Class" 'sort))
559 ;; XXX is this necessary, given all-method-keyword-classes?
560 (if (stringp class)
561 (setq class (idlwave-find-inherited-class
562 (idlwave-sintern-routine-or-method name class)
563 type (idlwave-sintern-class class)))))
564 (if (string= (downcase name) "obj_new")
565 (setq class idlwave-current-obj_new-class
566 name "Init"))))
567
568 ;; Class name
569 ((eq what 'class)
570 (setq class word
571 word nil))
572
573 ;; A special named function to call which sets some of our variables
574 ((and (symbolp what)
575 (fboundp what))
576 (funcall what 'set word))
577
578 (t (error "Cannot help with this item")))
579 (if (and need-class (not class) (not (and link (not (eq link t)))))
580 (error "Cannot help with this item"))
581 (idlwave-online-help link (or name word) type class kwd)))
582
583 (defvar idlwave-highlight-help-links-in-completion)
584 (defvar idlwave-completion-help-links)
585 (defun idlwave-highlight-linked-completions ()
586 "Highlight all completions for which help is available and attach link.
587 Those words in `idlwave-completion-help-links' have links. The
588 `idlwave-help-link-face' face is used for this."
589 (if idlwave-highlight-help-links-in-completion
590 (with-current-buffer (get-buffer "*Completions*")
591 (save-excursion
592 (let* ((case-fold-search t)
593 (props (list 'face 'idlwave-help-link-face))
594 (info idlwave-completion-help-info) ; global passed in
595 (what (nth 0 info)) ; what was completed, or a func
596 (class (nth 3 info)) ; any class
597 word beg end doit)
598 (goto-char (point-min))
599 (re-search-forward "possible completions are:" nil t)
600 (while (re-search-forward "\\s-\\([A-Za-z0-9_.]+\\)\\(\\s-\\|\\'\\)"
601 nil t)
602 (setq beg (match-beginning 1) end (match-end 1)
603 word (match-string 1) doit nil)
604 ;; Call special completion function test
605 (if (and (symbolp what)
606 (fboundp what))
607 (setq doit (funcall what 'test word))
608 ;; Look for special link property passed in help-links
609 (if idlwave-completion-help-links
610 (setq doit (assoc-ignore-case
611 word idlwave-completion-help-links))))
612 (when doit
613 (if (consp doit)
614 (setq props (append props `(link ,(cdr doit)))))
615 (let ((buffer-read-only nil))
616 (add-text-properties beg end props)))
617 (goto-char end)))))))
618
619 ;; Arrange for this function to be called after completion
620 (add-hook 'idlwave-completion-setup-hook
621 'idlwave-highlight-linked-completions)
622
623 (defvar idlwave-help-return-frame nil
624 "The frame to return to from the help frame.")
625
626 (defun idlwave-help-quit ()
627 "Exit IDLWAVE Help buffer. Kill the dedicated frame if any."
628 (interactive)
629 (cond ((and idlwave-help-use-dedicated-frame
630 (eq (selected-frame) idlwave-help-frame))
631 (if (and idlwave-experimental
632 (frame-live-p idlwave-help-return-frame))
633 ;; Try to select the return frame.
634 ;; This can crash on slow network connections, obviously when
635 ;; we kill the help frame before the return-frame is selected.
636 ;; To protect the workings, we wait for up to one second
637 ;; and check if the return-frame *is* now selected.
638 ;; This is marked "eperimental" since we are not sure when its OK.
639 (let ((maxtime 1.0) (time 0.) (step 0.1))
640 (select-frame idlwave-help-return-frame)
641 (while (and (sit-for step)
642 (not (eq (selected-frame)
643 idlwave-help-return-frame))
644 (< (setq time (+ time step)) maxtime)))))
645 (delete-frame idlwave-help-frame))
646 ((window-configuration-p idlwave-help-window-configuration)
647 (set-window-configuration idlwave-help-window-configuration)
648 (select-window (previous-window)))
649 (t (kill-buffer (idlwave-help-get-help-buffer)))))
650
651
652 (defvar default-toolbar-visible-p)
653
654 (defun idlwave-help-display-help-window (&optional pos-or-func)
655 "Display the help window.
656 Move window start to POS-OR-FUNC, if passed as a position, or call it
657 if passed as a function. See `idlwave-help-use-dedicated-frame'."
658 (let ((cw (selected-window))
659 (buf (idlwave-help-get-help-buffer)))
660 (if (and window-system idlwave-help-use-dedicated-frame)
661 (progn
662 (idlwave-help-show-help-frame)
663 (switch-to-buffer buf))
664 ;; Do it in this frame and save the window configuration
665 (if (not (get-buffer-window buf nil))
666 (setq idlwave-help-window-configuration
667 (current-window-configuration)))
668 (display-buffer buf nil (selected-frame))
669 (select-window (get-buffer-window buf)))
670 (raise-frame)
671 (if pos-or-func
672 (if (functionp pos-or-func)
673 (funcall pos-or-func)
674 (goto-char pos-or-func)
675 (recenter 0)))
676 (select-window cw)))
677
678 (defun idlwave-help-select-help-frame ()
679 "Select the help frame."
680 (if (and (frame-live-p idlwave-help-frame)
681 (not (eq (selected-frame) idlwave-help-frame)))
682 (progn
683 (setq idlwave-help-return-frame (selected-frame))
684 (select-frame idlwave-help-frame))))
685
686 (defun idlwave-help-return-to-calling-frame ()
687 "Select the frame from which the help frame was selected."
688 (interactive)
689 (if (and (frame-live-p idlwave-help-return-frame)
690 (not (eq (selected-frame) idlwave-help-return-frame)))
691 (select-frame idlwave-help-return-frame)))
692
693 (defun idlwave-online-help (link &optional name type class keyword)
694 "Display HTML or other special help on a certain topic.
695 Either loads an HTML link, if LINK is non-nil, or gets special-help on
696 the optional arguments, if any special help is defined. If LINK is
697 `t', first look up the optional arguments in the routine info list to
698 see if a link is set for it. Try extra help functions if necessary."
699 ;; Lookup link
700 (if (eq link t)
701 (let ((entry (idlwave-best-rinfo-assoc name type class
702 (idlwave-routines) nil t)))
703 (cond
704 ;; Try keyword link
705 ((and keyword
706 (setq link (cdr (idlwave-entry-find-keyword entry keyword)))))
707 ;; Default, regular entry link
708 (t (setq link (idlwave-entry-has-help entry))))))
709
710 (cond
711 ;; An explicit link
712 ((stringp link)
713 (idlwave-help-html-link link))
714
715 ;; Any extra help
716 (idlwave-extra-help-function
717 (idlwave-help-get-special-help name type class keyword))
718
719 ;; Nothing worked
720 (t (idlwave-help-error name type class keyword))))
721
722
723 (defun idlwave-help-get-special-help (name type class keyword)
724 "Call the function given by `idlwave-extra-help-function'."
725 (let* ((cw (selected-window))
726 (help-pos (save-excursion
727 (set-buffer (idlwave-help-get-help-buffer))
728 (let ((buffer-read-only nil))
729 (funcall idlwave-extra-help-function
730 name type class keyword)))))
731 (if help-pos
732 (idlwave-help-display-help-window help-pos)
733 (idlwave-help-error name type class keyword))
734 (select-window cw)))
735
736 (defun idlwave-help-html-link (link)
737 "Get html help on a given LINK."
738 (let ((browse-url-browser-function idlwave-help-browser-function)
739 (help-loc (idlwave-html-help-location))
740 (browse-url-generic-program idlwave-help-browser-generic-program)
741 ;(browse-url-generic-args idlwave-help-browser-generic-args)
742 full-link)
743
744 (if (and (memq system-type '(ms-dos windows-nt))
745 idlwave-help-use-hh)
746 (progn
747 (setq browse-url-browser-function 'browse-url-generic
748 full-link (concat (expand-file-name "idl.chm" help-loc)
749 "::/"
750 link))
751 (if (memq 'keyhh idlwave-help-use-hh)
752 (setq browse-url-generic-program "KEYHH"
753 browse-url-generic-args '("-IDLWAVE"))
754 (setq browse-url-generic-program "HH")))
755 ;; Just a regular file name (+ anchor name)
756 (unless (and (stringp help-loc)
757 (file-directory-p help-loc))
758 (error
759 "Invalid help location; customize `idlwave-html-help-location'."))
760 (setq full-link (concat
761 "file://"
762 (expand-file-name
763 link
764 (expand-file-name "idl_html_help" help-loc)))))
765
766 ;; Check for a local browser
767 (if (or idlwave-help-browser-is-local
768 (string-match "w3" (symbol-name idlwave-help-browser-function)))
769 (idlwave-help-display-help-window '(lambda () (browse-url full-link)))
770 (browse-url full-link))))
771
772 ;; A special help routine for source-level syntax help in files.
773 (defvar idlwave-help-def-pos)
774 (defvar idlwave-help-args)
775 (defvar idlwave-help-in-header)
776 (defvar idlwave-help-fontify-source-code)
777 (defvar idlwave-help-source-try-header)
778 (defun idlwave-help-with-source (name type class keyword)
779 "Provide help for routines not documented in the IDL manuals. Works
780 by loading the routine source file into the help buffer. Depending on
781 the value of `idlwave-help-source-try-header', it attempts to show the
782 routine definition or the header description. If
783 `idlwave-help-do-class-struct-tag' is non-nil, keyword is a tag to
784 show help on from the class definition structure. If
785 `idlwave-help-do-struct-tag' is non-nil, show help from the matching
786 structure tag definition.
787
788 This function can be used as `idlwave-extra-help-function'."
789 (let* ((class-struct-tag idlwave-help-do-class-struct-tag)
790 (struct-tag idlwave-help-do-struct-tag)
791 (case-fold-search t)
792 (real-class (if (consp name) (cdr name)))
793 (name (if (consp name) (car name) name))
794 (class-only (and (stringp class) (not (stringp name))))
795 file header-pos def-pos in-buf)
796 (if class-only ;Help with class? Using "Init" as source.
797 (setq name "Init"
798 type 'fun))
799 (if (not struct-tag)
800 (setq file
801 (idlwave-routine-source-file
802 (nth 3 (idlwave-best-rinfo-assoc
803 name (or type t) class (idlwave-routines))))))
804 (setq idlwave-help-def-pos nil
805 idlwave-help-args (list name type class keyword)
806 idlwave-help-in-header nil
807 idlwave-help-do-struct-tag nil
808 idlwave-help-do-class-struct-tag nil)
809 (if (or struct-tag (stringp file))
810 (progn
811 (setq in-buf ; structure-tag completion is always in current buffer
812 (if struct-tag
813 idlwave-current-tags-buffer
814 (idlwave-get-buffer-visiting file)))
815 ;; see if file is in a visited buffer, insert those contents
816 (if in-buf
817 (progn
818 (setq file (buffer-file-name in-buf))
819 (erase-buffer)
820 (insert-buffer in-buf))
821 (if (file-exists-p file) ;; otherwise just load the file
822 (progn
823 (erase-buffer)
824 (insert-file-contents file nil nil nil 'replace))
825 (idlwave-help-error name type class keyword)))
826 (if (and idlwave-help-fontify-source-code (not in-buf))
827 (idlwave-help-fontify)))
828 (idlwave-help-error name type class keyword))
829 (setq idlwave-help-mode-line-indicator file)
830
831 ;; Try to find a good place to display
832 (setq def-pos
833 ;; Find the class structure tag if that's what we're after
834 (cond
835 ;; Class structure tags: find the class or named structure
836 ;; definition
837 (class-struct-tag
838 (save-excursion
839 (setq class
840 (if (string-match "[a-zA-Z0-9]\\(__\\)" name)
841 (substring name 0 (match-beginning 1))
842 idlwave-current-tags-class))
843 (and
844 (idlwave-find-class-definition class nil real-class)
845 (idlwave-find-struct-tag keyword))))
846
847 ;; Generic structure tags: the structure definition
848 ;; location within the file has been recorded in
849 ;; `struct-tag'
850 (struct-tag
851 (save-excursion
852 (and
853 (integerp struct-tag)
854 (goto-char struct-tag)
855 (idlwave-find-struct-tag keyword))))
856
857 ;; Just find the routine definition
858 (t
859 (if class-only (point-min)
860 (idlwave-help-find-routine-definition name type class keyword))))
861 idlwave-help-def-pos def-pos)
862
863 (if (and idlwave-help-source-try-header
864 (not (or struct-tag class-struct-tag)))
865 ;; Check if we can find the header
866 (save-excursion
867 (goto-char (or def-pos (point-max)))
868 (setq header-pos (idlwave-help-find-in-doc-header
869 name type class keyword 'exact)
870 idlwave-help-in-header header-pos)))
871
872 (if (or header-pos def-pos)
873 (progn
874 (if (boundp 'idlwave-help-min-frame-width)
875 (setq idlwave-help-min-frame-width 80))
876 (goto-char (or header-pos def-pos)))
877 (idlwave-help-error name type class keyword))
878
879 (point)))
880
881
882 (defun idlwave-help-find-routine-definition (name type class keyword)
883 "Find the definition of routine CLASS::NAME in current buffer.
884 KEYWORD is ignored. Returns the point of match if successful, nil otherwise."
885 (save-excursion
886 (goto-char (point-max))
887 (if (re-search-backward
888 (concat "^[ \t]*"
889 (if (eq type 'pro) "pro"
890 (if (eq type 'fun) "function"
891 "\\(pro\\|function\\)"))
892 "[ \t]+"
893 (regexp-quote (downcase (idlwave-make-full-name class name)))
894 "[, \t\r\n]")
895 nil t)
896 (match-beginning 0)
897 nil)))
898
899 (defvar idlwave-doclib-start)
900 (defvar idlwave-doclib-end)
901 (defun idlwave-help-find-in-doc-header (name type class keyword
902 &optional exact)
903 "Find the requested help in the doc-header above point.
904
905 First checks if there is a doc-lib header which describes the correct
906 routine. Then tries to find the KEYWORDS section and the KEYWORD, if
907 given. Returns the point which should be window start of the help
908 window. If EXACT is non-nil, the full help position must be found -
909 down to the keyword requested. This setting is for context help, if
910 the exact spot is needed.
911
912 If EXACT is nil, the position of the header is returned if it
913 describes the correct routine - even if the keyword description cannot
914 be found. TYPE is ignored.
915
916 This function expects a more or less standard routine header. In
917 particlar it looks for the `NAME:' tag, either with a colon, or alone
918 on a line. Then `NAME:' must be followed by the routine name on the
919 same or the next line. When KEYWORD is non-nil, looks first for a
920 `KEYWORDS' section. It is amazing how inconsisten this is through
921 some IDL libraries I have seen. We settle for a line containing an
922 upper case \"KEYWORD\" string. If this line is not fould we search
923 for the keyword anyway to increase the hit-rate
924
925 When one of these sections exists we check for a line starting with any of
926
927 /KEYWORD KEYWORD- KEYWORD= KEYWORD
928
929 with spaces allowed between the keyword and the following dash or equal sign.
930 If there is a match, we assume it is the keyword description."
931 (let* ((case-fold-search t)
932 (rname (if (stringp class)
933 (concat
934 "\\("
935 ;; Traditional name or class::name
936 "\\("
937 "\\(" (regexp-quote (downcase class)) "::\\)?"
938 (regexp-quote (downcase name))
939 "\\>\\)"
940 (concat
941 "\\|"
942 ;; class__define or just class
943 (regexp-quote (downcase class)) "\\(__define\\)?")
944 "\\)")
945 (regexp-quote (downcase name))))
946
947 ;; NAME tag plus the routine name. The new version is from JD.
948 (name-re (concat
949 "\\(^;+\\*?[ \t]*"
950 idlwave-help-doclib-name
951 "\\([ \t]*:\\|[ \t]*$\\)[ \t]*\\(\n;+[ \t]*\\)*"
952 rname
953 "\\|"
954 "^;+[ \t]*"
955 rname
956 ":[ \t]*$\\)"))
957
958 ;; Header start plus name
959 (header-re (concat "\\(" idlwave-doclib-start "\\).*\n"
960 "\\(^;+.*\n\\)*"
961 "\\(" name-re "\\)"))
962 ;; A keywords section
963 (kwds-re (concat ; forgiving
964 "^;+\\*?[ \t]*"
965 "\\([-A-Z_ ]*"
966 idlwave-help-doclib-keyword
967 "[-A-Z_ ]*\\)"
968 "\\(:\\|[ \t]*\n\\)"))
969
970 ;; The individual keyword description line.
971 (kwd-re (if keyword ; hard (well...)
972 (concat
973 "^;+[ \t]+"
974 "\\(/" (regexp-quote (upcase keyword))
975 "\\|" (regexp-quote (upcase keyword)) "[ \t]*[-=:\n]"
976 "\\)")))
977 (kwd-re2 (if keyword ; forgiving
978 (concat
979 "^;+[ \t]+"
980 (regexp-quote (upcase keyword))
981 "\\>")))
982 dstart dend name-pos kwds-pos kwd-pos)
983 (catch 'exit
984 (save-excursion
985 (goto-char (point-min))
986 (while (and (setq dstart (re-search-forward idlwave-doclib-start nil t))
987 (setq dend (re-search-forward idlwave-doclib-end nil t)))
988 ;; found a routine header
989 (goto-char dstart)
990 (if (setq name-pos (re-search-forward name-re dend t))
991 (progn
992 (if keyword
993 ;; We do need a keyword
994 (progn
995 ;; Try to find a keyword section, but don't force it.
996 (goto-char name-pos)
997 (if (let ((case-fold-search nil))
998 (re-search-forward kwds-re dend t))
999 (setq kwds-pos (match-beginning 0)))
1000 ;; Find the keyword description
1001 (if (or (let ((case-fold-search nil))
1002 (re-search-forward kwd-re dend t))
1003 (re-search-forward kwd-re dend t)
1004 (let ((case-fold-search nil))
1005 (re-search-forward kwd-re2 dend t))
1006 (re-search-forward kwd-re2 dend t))
1007 (setq kwd-pos (match-beginning 0))
1008 (if exact
1009 (progn
1010 (idlwave-help-diagnostics
1011 (format "Could not find description of kwd %s"
1012 (upcase keyword)))
1013 (throw 'exit nil))))))
1014 ;; Return the best position we got
1015 (throw 'exit (or kwd-pos kwds-pos name-pos dstart)))
1016 (goto-char dend))))
1017 (idlwave-help-diagnostics "Could not find doclib header")
1018 (throw 'exit nil))))
1019
1020 (defun idlwave-help-diagnostics (string &optional ding)
1021 "Add a diagnostics string to the list.
1022 When DING is non-nil, ring the bell as well."
1023 (if (boundp 'idlwave-help-diagnostics)
1024 (progn
1025 (setq idlwave-help-diagnostics
1026 (cons string idlwave-help-diagnostics))
1027 (if ding (ding)))))
1028
1029 (defun idlwave-help-toggle-header-top-and-def (arg)
1030 (interactive "P")
1031 (let (pos)
1032 (if idlwave-help-in-header
1033 ;; Header was the last thing displayed
1034 (progn
1035 (setq idlwave-help-in-header nil)
1036 (setq pos idlwave-help-def-pos))
1037 ;; Try to display header
1038 (setq pos (idlwave-help-find-in-doc-header
1039 (nth 0 idlwave-help-args)
1040 (nth 1 idlwave-help-args)
1041 (nth 2 idlwave-help-args)
1042 nil))
1043 (if pos
1044 (setq idlwave-help-in-header t)
1045 (error "Cannot find doclib header for routine %s"
1046 (idlwave-make-full-name (nth 2 idlwave-help-args)
1047 (nth 0 idlwave-help-args)))))
1048 (if pos
1049 (progn
1050 (goto-char pos)
1051 (recenter 0)))))
1052
1053 (defun idlwave-help-find-first-header (arg)
1054 (interactive "P")
1055 (let (pos)
1056 (save-excursion
1057 (goto-char (point-min))
1058 (if (re-search-forward idlwave-doclib-start nil t)
1059 (setq pos (match-beginning 0))))
1060 (if pos
1061 (progn
1062 (goto-char pos)
1063 (recenter 0))
1064 (error "No DocLib Header in current file"))))
1065
1066 (defun idlwave-help-find-header (arg)
1067 "Jump to the DocLib Header."
1068 (interactive "P")
1069 (if arg
1070 (idlwave-help-find-first-header nil)
1071 (setq idlwave-help-in-header nil)
1072 (idlwave-help-toggle-header-match-and-def arg 'top)))
1073
1074 (defun idlwave-help-toggle-header-match-and-def (arg &optional top)
1075 (interactive "P")
1076 (let ((args idlwave-help-args)
1077 pos)
1078 (if idlwave-help-in-header
1079 ;; Header was the last thing displayed
1080 (progn
1081 (setq idlwave-help-in-header nil)
1082 (setq pos idlwave-help-def-pos))
1083 ;; Try to display header
1084 (setq pos (apply 'idlwave-help-find-in-doc-header
1085 (if top
1086 (list (car args) (nth 1 args) (nth 2 args) nil)
1087 args)))
1088 (if pos
1089 (setq idlwave-help-in-header t)
1090 (error "Cannot find doclib header for routine %s"
1091 (idlwave-make-full-name (nth 2 idlwave-help-args)
1092 (nth 0 idlwave-help-args)))))
1093 (if pos
1094 (progn
1095 (goto-char pos)
1096 (recenter 0)))))
1097
1098 (defvar font-lock-verbose)
1099 (defvar idlwave-mode-syntax-table)
1100 (defvar idlwave-font-lock-defaults)
1101 (defun idlwave-help-fontify ()
1102 "Fontify the Help buffer as source code.
1103 Useful when source code is displayed as help. See the option
1104 `idlwave-help-fontify-source-code'."
1105 (interactive)
1106 (if (featurep 'font-lock)
1107 (let ((major-mode 'idlwave-mode)
1108 (font-lock-verbose
1109 (if (interactive-p) font-lock-verbose nil))
1110 (syntax-table (syntax-table)))
1111 (unwind-protect
1112 (progn
1113 (set-syntax-table idlwave-mode-syntax-table)
1114 (set (make-local-variable 'font-lock-defaults)
1115 idlwave-font-lock-defaults)
1116 (font-lock-fontify-buffer))
1117 (set-syntax-table syntax-table)))))
1118
1119
1120 (defun idlwave-help-error (name type class keyword)
1121 (error "Can't find help on %s%s %s"
1122 (or (and (or class name) (idlwave-make-full-name class name))
1123 "<unknown>")
1124 (if keyword (format ", keyword %s" (upcase keyword)) "")
1125 (if idlwave-html-help-location
1126 ""
1127 "(help location unknown)")))
1128
1129 (defun idlwave-help-show-help-frame ()
1130 "Show the help frame, creating it if necessary"
1131 ;; Use a special frame for this
1132 (unless (frame-live-p idlwave-help-frame)
1133 (setq idlwave-help-frame
1134 (make-frame idlwave-help-frame-parameters))
1135 ;; Strip menubar (?) and toolbar from the Help frame.
1136 (if (fboundp 'set-specifier)
1137 (progn
1138 ;; XEmacs
1139 (let ((sval (cons idlwave-help-frame nil)))
1140 ;; (set-specifier menubar-visible-p sval)
1141 (set-specifier default-toolbar-visible-p sval)))
1142 ;; Emacs
1143 (modify-frame-parameters idlwave-help-frame
1144 '(;;(menu-bar-lines . 0)
1145 (tool-bar-lines . 0)))))
1146 (select-frame idlwave-help-frame))
1147
1148 (defun idlwave-help-get-help-buffer ()
1149 "Return the IDLWAVE Help buffer. Make it first if necessary."
1150 (let ((buf (get-buffer "*IDLWAVE Help*")))
1151 (if buf
1152 nil
1153 (setq buf (get-buffer-create "*IDLWAVE Help*"))
1154 (save-excursion
1155 (set-buffer buf)
1156 (idlwave-help-mode)))
1157 buf))
1158
1159 (defun idlwave-grep (regexp list)
1160 (let (rtn)
1161 (while list
1162 (if (string-match regexp (car list))
1163 (setq rtn (cons (car list) rtn)))
1164 (setq list (cdr list)))
1165 (nreverse rtn)))
1166
1167 (defun idlwave-entry-has-help (entry)
1168 (and entry (car (nth 5 entry))))
1169
1170 (defun idlwave-has-help (name type class)
1171 "Does this have help associated with it?"
1172 (let ((entry (idlwave-best-rinfo-assoc name type class (idlwave-routines))))
1173 (idlwave-entry-has-help entry)))
1174
1175 (provide 'idlw-help)
1176 (provide 'idlwave-help)
1177
1178 ;; arch-tag: d27b5505-59de-497f-ba3f-f199fd4fb911
1179 ;;; idlw-help.el ends here