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
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>
11 ;; This file is part of GNU Emacs.
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)
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.
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.
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
40 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
46 (defgroup idlwave-online-help nil
47 "Online Help options for IDLWAVE mode."
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
55 (defvar idlwave-html-link-sep
56 (if idlwave-html-help-pre-v6 "#" "#wp"))
58 (defcustom idlwave-html-help-location
59 (if (memq system-type '(ms-dos windows-nt))
62 "The directory where the idl_html_help/ dir or idl.chm help file
63 (Windows only) lives."
64 :group 'idlwave-online-help
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)
73 (const :tag "keyhh" 'keyhh)))
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
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
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
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
103 (defvar idlwave-help-directory ""
104 "Obsolete variable. See idlwave-html-help-location.")
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
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
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
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.
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
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).
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
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
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
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
175 :group 'idlwave-online-help
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
182 :group 'idlwave-online-help
185 (defface idlwave-help-link
186 '((((min-colors 88) (class color)) (:foreground "Blue1"))
187 (((class color)) (:foreground "Blue"))
189 "Face for highlighting links into IDLWAVE online help."
190 :group 'idlwave-online-help)
191 ;; backward-compatibility alias
192 (put 'idlwave-help-link-face 'face-alias 'idlwave-help-link)
194 (defvar idlwave-help-activate-links-aggressively nil
195 "Obsolete variable.")
197 (defvar idlwave-completion-help-info)
199 (defvar idlwave-help-frame nil
200 "The frame for display of IDL online help.")
201 (defvar idlwave-help-frame-width 102
202 "The default width of the help frame.")
204 (defvar idlwave-html-help-is-available nil
205 "Is the system online help text avaiable?")
207 (defvar idlwave-help-mode-line-indicator ""
208 "Used for the special mode line in the idlwave-help-mode.")
210 (defvar idlwave-help-window-configuration nil)
211 (defvar idlwave-help-special-topic-words nil) ; defined by get_rinfo
213 ;; Define the key bindings for the Help application
215 (defvar idlwave-help-mode-map (make-sparse-keymap)
216 "The keymap used in idlwave-help-mode.")
218 (define-key idlwave-help-mode-map "q" 'idlwave-help-quit)
219 (define-key idlwave-help-mode-map "w" 'widen)
220 (define-key idlwave-help-mode-map "\C-m" (lambda (arg)
223 (define-key idlwave-help-mode-map " " 'scroll-up)
224 (define-key idlwave-help-mode-map [delete] 'scroll-down)
225 (define-key idlwave-help-mode-map "h" 'idlwave-help-find-header)
226 (define-key idlwave-help-mode-map "H" 'idlwave-help-find-first-header)
227 (define-key idlwave-help-mode-map "." 'idlwave-help-toggle-header-match-and-def)
228 (define-key idlwave-help-mode-map "F" 'idlwave-help-fontify)
229 (define-key idlwave-help-mode-map "\M-?" 'idlwave-help-return-to-calling-frame)
230 (define-key idlwave-help-mode-map "x" 'idlwave-help-return-to-calling-frame)
232 ;; Define the menu for the Help application
235 idlwave-help-menu idlwave-help-mode-map
236 "Menu for Help IDLWAVE system"
238 ["Definition <-> Help Text" idlwave-help-toggle-header-match-and-def t]
239 ["Find DocLib Header" idlwave-help-find-header t]
240 ["Find First DocLib Header" idlwave-help-find-first-header t]
241 ["Fontify help buffer" idlwave-help-fontify t]
243 ["Quit" idlwave-help-quit t]))
245 (defun idlwave-help-mode ()
246 "Major mode for displaying IDL Help.
248 This is a VIEW mode for the ASCII version of IDL Help files,
249 with some extras. Its main purpose is speed - so don't
250 expect a fully hyper-linked help.
252 Scrolling: SPC DEL RET
253 Text Searches: Inside Topic: Use Emacs search functions
254 Exit: [q]uit or mouse button 3 will kill the frame
256 When the hep text is a source file, the following commands are available
258 Fontification: [F]ontify the buffer like source code
259 Jump: [h] to function doclib header
260 [H] to file doclib header
261 [.] back and forward between header and definition
263 Here are all keybindings.
264 \\{idlwave-help-mode-map}"
265 (kill-all-local-variables)
266 (buffer-disable-undo)
267 (setq major-mode 'idlwave-help-mode
268 mode-name "IDLWAVE Help")
269 (use-local-map idlwave-help-mode-map)
270 (easy-menu-add idlwave-help-menu idlwave-help-mode-map)
271 (setq truncate-lines t)
272 (setq case-fold-search t)
273 (setq mode-line-format
276 'mode-line-buffer-identification
277 ": " 'idlwave-help-mode-line-indicator
279 (setq buffer-read-only t)
280 (set (make-local-variable 'idlwave-help-def-pos) nil)
281 (set (make-local-variable 'idlwave-help-args) nil)
282 (set (make-local-variable 'idlwave-help-in-header) nil)
283 (run-mode-hooks 'idlwave-help-mode-hook))
285 (defun idlwave-html-help-location ()
286 "Return the help directory where HTML files are, or nil if that is unknown."
287 (or (and (stringp idlwave-html-help-location)
288 (> (length idlwave-html-help-location) 0)
289 (file-directory-p idlwave-html-help-location)
290 idlwave-html-help-location)
291 (getenv "IDLWAVE_HELP_LOCATION")
292 (and (memq system-type '(ms-dos windows-nt)) ; Base it on sysdir
294 (stringp idlwave-system-directory)
295 (> (length idlwave-system-directory) 0)
296 (file-directory-p idlwave-system-directory)
297 (expand-file-name "HELP" idlwave-system-directory))))
299 (defvar idlwave-current-obj_new-class)
300 (defvar idlwave-help-diagnostics)
301 (defvar idlwave-experimental)
302 (defvar idlwave-last-context-help-pos)
303 (defun idlwave-do-context-help (&optional arg)
304 "Wrapper around the call to idlwave-context-help1.
305 It collects and prints the diagnostics messages."
306 (let ((marker (list (current-buffer) (point)))
307 (idlwave-help-diagnostics nil))
308 ;; Check for frame switching. When the command is invoked twice
309 ;; at the same position, we try to switch to the help frame
310 ;; FIXME: Frame switching works only on XEmacs
311 (if (and idlwave-experimental
312 (equal last-command this-command)
313 (equal idlwave-last-context-help-pos marker))
314 (idlwave-help-select-help-frame)
315 ;; Do the real thing.
316 (setq idlwave-last-context-help-pos marker)
317 (idlwave-do-context-help1 arg)
318 (if idlwave-help-diagnostics
319 (message "%s" (mapconcat 'identity
320 (nreverse idlwave-help-diagnostics)
323 (defvar idlwave-help-do-class-struct-tag nil)
324 (defvar idlwave-help-do-struct-tag nil)
325 (defun idlwave-do-context-help1 (&optional arg)
326 "The work-horse version of `idlwave-context-help', which see."
328 (if (equal (char-after) ?/)
330 (if (equal (char-before) ?=)
332 (let* ((idlwave-query-class nil)
333 (idlwave-force-class-query (equal arg '(4)))
334 (chars "a-zA-Z0-9_$.!")
335 (beg (save-excursion (skip-chars-backward chars) (point)))
336 (end (save-excursion (skip-chars-forward chars) (point)))
337 (this-word (buffer-substring-no-properties beg end))
338 (st-ass (assoc (downcase this-word)
339 idlwave-help-special-topic-words))
340 (classtag (and (string-match "self\\." this-word)
342 (structtag (and (fboundp 'idlwave-complete-structure-tag)
343 (string-match "\\`\\([^.]+\\)\\." this-word)
345 module keyword cw mod1 mod2 mod3)
350 (not (member (string-to-char this-word) '(?! ?.)))))
351 ;; Need the module information
353 ;; MODULE is (name type class), for this or any inheriting class
354 (setq module (idlwave-what-module-find-class)
355 cw (nth 2 (idlwave-where))) ;what would we complete here?
356 ;; Correct for OBJ_NEW, we may need an INIT method instead.
357 (if (equal (idlwave-downcase-safe (car module)) "obj_new")
358 (let* ((bos (save-excursion (idlwave-beginning-of-statement)
360 (str (buffer-substring bos (point))))
361 (if (string-match "OBJ_NEW([ \t]*['\"]\\([a-zA-Z][a-zA-Z0-9$_]+\\)['\"]" str)
362 (setq module (list "init" 'fun (match-string 1 str))
363 idlwave-current-obj_new-class (match-string 1 str))
366 (arg (setq mod1 module))
368 ;; A special topic -- only system help
369 (st-ass (setq mod1 (list (cdr st-ass))))
371 ;; A system variable -- only system help
373 "\\`!\\([a-zA-Z0-9_]+\\)\\(\.\\([A-Za-z0-9_]+\\)\\)?"
375 (let* ((word (match-string-no-properties 1 this-word))
376 (entry (assq (idlwave-sintern-sysvar word)
377 idlwave-system-variables-alist))
378 (tag (match-string-no-properties 3 this-word))
381 (assq (idlwave-sintern-sysvartag tag)
382 (cdr (assq 'tags entry))))))
383 (link (nth 1 (assq 'link entry))))
385 (setq link (idlwave-substitute-link-target link
387 (setq mod1 (list link))))
389 ;; An executive command -- only system help
390 ((string-match "^\\.\\([A-Z_]+\\)" this-word)
391 (let* ((word (match-string 1 this-word))
392 (link (cdr (assoc-string
394 idlwave-executive-commands-alist
396 (setq mod1 (list link))))
398 ;; A class -- system OR in-text help (via class__define).
400 (or (idlwave-in-quote) ; e.g. obj_new
401 (re-search-backward "\\<inherits[ \t]+[A-Za-z0-9_]*\\="
402 (max (point-min) (- (point) 40)) t)))
403 ;; Class completion inside string delimiters must be
404 ;; the class inside OBJ_NEW.
406 (idlwave-sintern-class this-word)
407 idlwave-system-class-info))
408 (name (concat (downcase this-word) "__define"))
409 (link (nth 1 (assq 'link entry))))
410 (setq mod1 (list link name 'pro))))
412 ;; A class structure tag (self.BLAH) -- only in-text help available
414 (let ((tag (substring this-word (match-end 0)))
416 (when (setq class-with
417 (idlwave-class-or-superclass-with-tag
418 (nth 2 (idlwave-current-routine))
420 (setq found-in (idlwave-class-found-in class-with))
421 (if (assq (idlwave-sintern-class class-with)
422 idlwave-system-class-info)
423 (error "No help available for system class tags"))
424 (setq idlwave-help-do-class-struct-tag t)
427 (cons (concat found-in "__define") class-with)
428 (concat class-with "__define"))
430 nil ; no class.... it's a procedure!
433 ;; A regular structure tag -- only in text, and if
434 ;; optional `complete-structtag' loaded.
436 (let ((var (match-string 1 this-word))
437 (tag (substring this-word (match-end 0))))
438 ;; Check if we need to update the "current" structure
439 (idlwave-prepare-structure-tag-completion var)
440 (setq idlwave-help-do-struct-tag
441 idlwave-structtag-struct-location
442 mod1 (list nil nil nil nil tag))))
444 ;; A routine keyword -- in text or system help
445 ((and (memq cw '(function-keyword procedure-keyword))
447 (string-match "\\S-" this-word)
448 (not (string-match "!" this-word)))
449 (cond ((or (= (char-before beg) ?/)
450 (save-excursion (goto-char end)
451 (looking-at "[ \t]*=")))
452 ;; Certainly a keyword. Check for abbreviation etc.
453 (setq keyword (idlwave-expand-keyword this-word module))
456 (idlwave-help-diagnostics
457 (format "%s does not accept `%s' kwd"
458 (idlwave-make-full-name (nth 2 module)
463 (idlwave-help-diagnostics
464 (format "%d matches for kwd abbrev `%s'"
465 (length keyword) this-word)
467 ;; We continue anyway with the first match...
468 (setq keyword (car keyword))))
469 ;; Keyword, or just module
470 (setq mod1 (append (list t) module (list keyword)))
471 (setq mod2 (append (list t) module)))
472 ((equal (char-after end) ?\()
473 ;; A function - what-module will have caught this
474 (setq mod1 (append (list t) module)))
476 ;; undecided - try function, keyword, then enclosing mod.
477 ;; Check for keyword abbreviations, but do not report
478 ;; errors, because it might be something else.
479 ;; FIXME: is this a good way to handle this?
480 (setq keyword (idlwave-expand-keyword this-word module))
481 (if (consp keyword) (setq keyword (car keyword)))
482 (setq mod1 (append (list t) module (list keyword))
483 mod2 (list t this-word 'fun nil)
484 mod3 (append (list t) module)))))
488 (setq mod1 (append (list t) module))))
491 (apply 'idlwave-online-help mod1)
492 (error (condition-case nil
493 (apply 'idlwave-online-help mod2)
494 (error (apply 'idlwave-online-help mod3)))))
497 (apply 'idlwave-online-help mod1)
498 (error (apply 'idlwave-online-help mod2)))
500 (apply 'idlwave-online-help mod1)
501 (error "Don't know which item to show help for")))))))
503 (defun idlwave-do-mouse-completion-help (ev)
504 "Display online help on an item in the *Completions* buffer.
505 Needs additional info stored in global `idlwave-completion-help-info'."
506 (let* ((cw (selected-window))
507 (info idlwave-completion-help-info) ; global passed in
514 (sclasses (nth 5 info))
519 ;; See if we can also find help somewhere, e.g. for multiple classes
520 (setq word (idlwave-this-word))
521 (if (string= word "")
522 (error "No help item selected"))
523 (setq link (get-text-property 0 'link word))
527 ((memq what '(procedure function routine))
530 (and (stringp class) sclasses))
531 (let* ((classes (idlwave-all-method-classes
532 (idlwave-sintern-method name)
534 (setq link t) ; No specific link valid yet
536 (setq classes (idlwave-members-only
537 classes (cons class sclasses))))
538 (setq class (idlwave-popup-select ev classes
539 "Select Class" 'sort))))
541 ;; XXX is this necessary, given all-method-classes?
543 (setq class (idlwave-find-inherited-class
544 (idlwave-sintern-routine-or-method name class)
545 type (idlwave-sintern-class class)))))
551 (and (stringp class) sclasses))
552 (let ((classes (idlwave-all-method-keyword-classes
553 (idlwave-sintern-method name)
554 (idlwave-sintern-keyword kwd)
556 (setq link t) ; Link can't be correct yet
558 (setq classes (idlwave-members-only
559 classes (cons class sclasses))))
560 (setq class (idlwave-popup-select ev classes
561 "Select Class" 'sort))
562 ;; XXX is this necessary, given all-method-keyword-classes?
564 (setq class (idlwave-find-inherited-class
565 (idlwave-sintern-routine-or-method name class)
566 type (idlwave-sintern-class class)))))
567 (if (string= (downcase name) "obj_new")
568 (setq class idlwave-current-obj_new-class
576 ;; A special named function to call which sets some of our variables
579 (funcall what 'set word))
581 (t (error "Cannot help with this item")))
582 (if (and need-class (not class) (not (and link (not (eq link t)))))
583 (error "Cannot help with this item"))
584 (idlwave-online-help link (or name word) type class kwd)))
586 (defvar idlwave-highlight-help-links-in-completion)
587 (defvar idlwave-completion-help-links)
588 (defun idlwave-highlight-linked-completions ()
589 "Highlight all completions for which help is available and attach link.
590 Those words in `idlwave-completion-help-links' have links. The
591 `idlwave-help-link' face is used for this."
592 (if idlwave-highlight-help-links-in-completion
593 (with-current-buffer (get-buffer "*Completions*")
595 (let* ((case-fold-search t)
596 (props (list 'face 'idlwave-help-link))
597 (info idlwave-completion-help-info) ; global passed in
598 (what (nth 0 info)) ; what was completed, or a func
599 (class (nth 3 info)) ; any class
601 (goto-char (point-min))
602 (re-search-forward "possible completions are:" nil t)
603 (while (re-search-forward "\\s-\\([A-Za-z0-9_.]+\\)\\(\\s-\\|\\'\\)"
605 (setq beg (match-beginning 1) end (match-end 1)
606 word (match-string 1) doit nil)
607 ;; Call special completion function test
608 (if (and (symbolp what)
610 (setq doit (funcall what 'test word))
611 ;; Look for special link property passed in help-links
612 (if idlwave-completion-help-links
613 (setq doit (assoc-string
614 word idlwave-completion-help-links t))))
617 (setq props (append props `(link ,(cdr doit)))))
618 (let ((buffer-read-only nil))
619 (add-text-properties beg end props)))
620 (goto-char end)))))))
622 ;; Arrange for this function to be called after completion
623 (add-hook 'idlwave-completion-setup-hook
624 'idlwave-highlight-linked-completions)
626 (defvar idlwave-help-return-frame nil
627 "The frame to return to from the help frame.")
629 (defun idlwave-help-quit ()
630 "Exit IDLWAVE Help buffer. Kill the dedicated frame if any."
632 (cond ((and idlwave-help-use-dedicated-frame
633 (eq (selected-frame) idlwave-help-frame))
634 (if (and idlwave-experimental
635 (frame-live-p idlwave-help-return-frame))
636 ;; Try to select the return frame.
637 ;; This can crash on slow network connections, obviously when
638 ;; we kill the help frame before the return-frame is selected.
639 ;; To protect the workings, we wait for up to one second
640 ;; and check if the return-frame *is* now selected.
641 ;; This is marked "eperimental" since we are not sure when its OK.
642 (let ((maxtime 1.0) (time 0.) (step 0.1))
643 (select-frame idlwave-help-return-frame)
644 (while (and (sit-for step)
645 (not (eq (selected-frame)
646 idlwave-help-return-frame))
647 (< (setq time (+ time step)) maxtime)))))
648 (delete-frame idlwave-help-frame))
649 ((window-configuration-p idlwave-help-window-configuration)
650 (set-window-configuration idlwave-help-window-configuration)
651 (select-window (previous-window)))
652 (t (kill-buffer (idlwave-help-get-help-buffer)))))
655 (defvar default-toolbar-visible-p)
657 (defun idlwave-help-display-help-window (&optional pos-or-func)
658 "Display the help window.
659 Move window start to POS-OR-FUNC, if passed as a position, or call it
660 if passed as a function. See `idlwave-help-use-dedicated-frame'."
661 (let ((cw (selected-window))
662 (buf (idlwave-help-get-help-buffer)))
663 (if (and window-system idlwave-help-use-dedicated-frame)
665 (idlwave-help-show-help-frame)
666 (switch-to-buffer buf))
667 ;; Do it in this frame and save the window configuration
668 (if (not (get-buffer-window buf nil))
669 (setq idlwave-help-window-configuration
670 (current-window-configuration)))
671 (display-buffer buf nil (selected-frame))
672 (select-window (get-buffer-window buf)))
675 (if (functionp pos-or-func)
676 (funcall pos-or-func)
677 (goto-char pos-or-func)
681 (defun idlwave-help-select-help-frame ()
682 "Select the help frame."
683 (if (and (frame-live-p idlwave-help-frame)
684 (not (eq (selected-frame) idlwave-help-frame)))
686 (setq idlwave-help-return-frame (selected-frame))
687 (select-frame idlwave-help-frame))))
689 (defun idlwave-help-return-to-calling-frame ()
690 "Select the frame from which the help frame was selected."
692 (if (and (frame-live-p idlwave-help-return-frame)
693 (not (eq (selected-frame) idlwave-help-return-frame)))
694 (select-frame idlwave-help-return-frame)))
696 (defun idlwave-online-help (link &optional name type class keyword)
697 "Display HTML or other special help on a certain topic.
698 Either loads an HTML link, if LINK is non-nil, or gets special-help on
699 the optional arguments, if any special help is defined. If LINK is
700 `t', first look up the optional arguments in the routine info list to
701 see if a link is set for it. Try extra help functions if necessary."
704 (let ((entry (idlwave-best-rinfo-assoc name type class
705 (idlwave-routines) nil t)))
709 (setq link (cdr (idlwave-entry-find-keyword entry keyword)))))
710 ;; Default, regular entry link
711 (t (setq link (idlwave-entry-has-help entry))))))
716 (idlwave-help-html-link link))
719 (idlwave-extra-help-function
720 (idlwave-help-get-special-help name type class keyword))
723 (t (idlwave-help-error name type class keyword))))
726 (defun idlwave-help-get-special-help (name type class keyword)
727 "Call the function given by `idlwave-extra-help-function'."
728 (let* ((cw (selected-window))
729 (help-pos (save-excursion
730 (set-buffer (idlwave-help-get-help-buffer))
731 (let ((buffer-read-only nil))
732 (funcall idlwave-extra-help-function
733 name type class keyword)))))
735 (idlwave-help-display-help-window help-pos)
736 (idlwave-help-error name type class keyword))
739 (defun idlwave-help-html-link (link)
740 "Get html help on a given LINK."
741 (let ((browse-url-browser-function idlwave-help-browser-function)
742 (help-loc (idlwave-html-help-location))
743 (browse-url-generic-program idlwave-help-browser-generic-program)
744 ;(browse-url-generic-args idlwave-help-browser-generic-args)
747 (if (and (memq system-type '(ms-dos windows-nt))
750 (setq browse-url-browser-function 'browse-url-generic
751 full-link (concat (expand-file-name "idl.chm" help-loc)
754 (if (memq 'keyhh idlwave-help-use-hh)
755 (setq browse-url-generic-program "KEYHH"
756 browse-url-generic-args '("-IDLWAVE"))
757 (setq browse-url-generic-program "HH")))
758 ;; Just a regular file name (+ anchor name)
759 (unless (and (stringp help-loc)
760 (file-directory-p help-loc))
762 "Invalid help location; customize `idlwave-html-help-location'."))
763 (setq full-link (concat
767 (expand-file-name "idl_html_help" help-loc)))))
769 ;; Check for a local browser
770 (if (or idlwave-help-browser-is-local
771 (string-match "w3" (symbol-name idlwave-help-browser-function)))
772 (idlwave-help-display-help-window '(lambda () (browse-url full-link)))
773 (browse-url full-link))))
775 ;; A special help routine for source-level syntax help in files.
776 (defvar idlwave-help-def-pos)
777 (defvar idlwave-help-args)
778 (defvar idlwave-help-in-header)
779 (defvar idlwave-help-fontify-source-code)
780 (defvar idlwave-help-source-try-header)
781 (defun idlwave-help-with-source (name type class keyword)
782 "Provide help for routines not documented in the IDL manuals. Works
783 by loading the routine source file into the help buffer. Depending on
784 the value of `idlwave-help-source-try-header', it attempts to show the
785 routine definition or the header description. If
786 `idlwave-help-do-class-struct-tag' is non-nil, keyword is a tag to
787 show help on from the class definition structure. If
788 `idlwave-help-do-struct-tag' is non-nil, show help from the matching
789 structure tag definition.
791 This function can be used as `idlwave-extra-help-function'."
792 (let* ((class-struct-tag idlwave-help-do-class-struct-tag)
793 (struct-tag idlwave-help-do-struct-tag)
795 (real-class (if (consp name) (cdr name)))
796 (name (if (consp name) (car name) name))
797 (class-only (and (stringp class) (not (stringp name))))
798 file header-pos def-pos in-buf)
799 (if class-only ;Help with class? Using "Init" as source.
804 (idlwave-routine-source-file
805 (nth 3 (idlwave-best-rinfo-assoc
806 name (or type t) class (idlwave-routines))))))
807 (setq idlwave-help-def-pos nil
808 idlwave-help-args (list name type class keyword)
809 idlwave-help-in-header nil
810 idlwave-help-do-struct-tag nil
811 idlwave-help-do-class-struct-tag nil)
812 (if (or struct-tag (stringp file))
814 (setq in-buf ; structure-tag completion is always in current buffer
816 idlwave-current-tags-buffer
817 (idlwave-get-buffer-visiting file)))
818 ;; see if file is in a visited buffer, insert those contents
821 (setq file (buffer-file-name in-buf))
823 (insert-buffer in-buf))
824 (if (file-exists-p file) ;; otherwise just load the file
827 (insert-file-contents file nil nil nil 'replace))
828 (idlwave-help-error name type class keyword)))
829 (if (and idlwave-help-fontify-source-code (not in-buf))
830 (idlwave-help-fontify)))
831 (idlwave-help-error name type class keyword))
832 (setq idlwave-help-mode-line-indicator file)
834 ;; Try to find a good place to display
836 ;; Find the class structure tag if that's what we're after
838 ;; Class structure tags: find the class or named structure
843 (if (string-match "[a-zA-Z0-9]\\(__\\)" name)
844 (substring name 0 (match-beginning 1))
845 idlwave-current-tags-class))
847 (idlwave-find-class-definition class nil real-class)
848 (idlwave-find-struct-tag keyword))))
850 ;; Generic structure tags: the structure definition
851 ;; location within the file has been recorded in
856 (integerp struct-tag)
857 (goto-char struct-tag)
858 (idlwave-find-struct-tag keyword))))
860 ;; Just find the routine definition
862 (if class-only (point-min)
863 (idlwave-help-find-routine-definition name type class keyword))))
864 idlwave-help-def-pos def-pos)
866 (if (and idlwave-help-source-try-header
867 (not (or struct-tag class-struct-tag)))
868 ;; Check if we can find the header
870 (goto-char (or def-pos (point-max)))
871 (setq header-pos (idlwave-help-find-in-doc-header
872 name type class keyword 'exact)
873 idlwave-help-in-header header-pos)))
875 (if (or header-pos def-pos)
877 (if (boundp 'idlwave-help-min-frame-width)
878 (setq idlwave-help-min-frame-width 80))
879 (goto-char (or header-pos def-pos)))
880 (idlwave-help-error name type class keyword))
885 (defun idlwave-help-find-routine-definition (name type class keyword)
886 "Find the definition of routine CLASS::NAME in current buffer.
887 KEYWORD is ignored. Returns the point of match if successful, nil otherwise."
889 (goto-char (point-max))
890 (if (re-search-backward
892 (if (eq type 'pro) "pro"
893 (if (eq type 'fun) "function"
894 "\\(pro\\|function\\)"))
896 (regexp-quote (downcase (idlwave-make-full-name class name)))
902 (defvar idlwave-doclib-start)
903 (defvar idlwave-doclib-end)
904 (defun idlwave-help-find-in-doc-header (name type class keyword
906 "Find the requested help in the doc-header above point.
908 First checks if there is a doc-lib header which describes the correct
909 routine. Then tries to find the KEYWORDS section and the KEYWORD, if
910 given. Returns the point which should be window start of the help
911 window. If EXACT is non-nil, the full help position must be found -
912 down to the keyword requested. This setting is for context help, if
913 the exact spot is needed.
915 If EXACT is nil, the position of the header is returned if it
916 describes the correct routine - even if the keyword description cannot
917 be found. TYPE is ignored.
919 This function expects a more or less standard routine header. In
920 particlar it looks for the `NAME:' tag, either with a colon, or alone
921 on a line. Then `NAME:' must be followed by the routine name on the
922 same or the next line. When KEYWORD is non-nil, looks first for a
923 `KEYWORDS' section. It is amazing how inconsisten this is through
924 some IDL libraries I have seen. We settle for a line containing an
925 upper case \"KEYWORD\" string. If this line is not fould we search
926 for the keyword anyway to increase the hit-rate
928 When one of these sections exists we check for a line starting with any of
930 /KEYWORD KEYWORD- KEYWORD= KEYWORD
932 with spaces allowed between the keyword and the following dash or equal sign.
933 If there is a match, we assume it is the keyword description."
934 (let* ((case-fold-search t)
935 (rname (if (stringp class)
938 ;; Traditional name or class::name
940 "\\(" (regexp-quote (downcase class)) "::\\)?"
941 (regexp-quote (downcase name))
945 ;; class__define or just class
946 (regexp-quote (downcase class)) "\\(__define\\)?")
948 (regexp-quote (downcase name))))
950 ;; NAME tag plus the routine name. The new version is from JD.
953 idlwave-help-doclib-name
954 "\\([ \t]*:\\|[ \t]*$\\)[ \t]*\\(\n;+[ \t]*\\)*"
961 ;; Header start plus name
962 (header-re (concat "\\(" idlwave-doclib-start "\\).*\n"
964 "\\(" name-re "\\)"))
965 ;; A keywords section
966 (kwds-re (concat ; forgiving
969 idlwave-help-doclib-keyword
971 "\\(:\\|[ \t]*\n\\)"))
973 ;; The individual keyword description line.
974 (kwd-re (if keyword ; hard (well...)
977 "\\(/" (regexp-quote (upcase keyword))
978 "\\|" (regexp-quote (upcase keyword)) "[ \t]*[-=:\n]"
980 (kwd-re2 (if keyword ; forgiving
983 (regexp-quote (upcase keyword))
985 dstart dend name-pos kwds-pos kwd-pos)
988 (goto-char (point-min))
989 (while (and (setq dstart (re-search-forward idlwave-doclib-start nil t))
990 (setq dend (re-search-forward idlwave-doclib-end nil t)))
991 ;; found a routine header
993 (if (setq name-pos (re-search-forward name-re dend t))
996 ;; We do need a keyword
998 ;; Try to find a keyword section, but don't force it.
1000 (if (let ((case-fold-search nil))
1001 (re-search-forward kwds-re dend t))
1002 (setq kwds-pos (match-beginning 0)))
1003 ;; Find the keyword description
1004 (if (or (let ((case-fold-search nil))
1005 (re-search-forward kwd-re dend t))
1006 (re-search-forward kwd-re dend t)
1007 (let ((case-fold-search nil))
1008 (re-search-forward kwd-re2 dend t))
1009 (re-search-forward kwd-re2 dend t))
1010 (setq kwd-pos (match-beginning 0))
1013 (idlwave-help-diagnostics
1014 (format "Could not find description of kwd %s"
1016 (throw 'exit nil))))))
1017 ;; Return the best position we got
1018 (throw 'exit (or kwd-pos kwds-pos name-pos dstart)))
1020 (idlwave-help-diagnostics "Could not find doclib header")
1021 (throw 'exit nil))))
1023 (defun idlwave-help-diagnostics (string &optional ding)
1024 "Add a diagnostics string to the list.
1025 When DING is non-nil, ring the bell as well."
1026 (if (boundp 'idlwave-help-diagnostics)
1028 (setq idlwave-help-diagnostics
1029 (cons string idlwave-help-diagnostics))
1032 (defun idlwave-help-toggle-header-top-and-def (arg)
1035 (if idlwave-help-in-header
1036 ;; Header was the last thing displayed
1038 (setq idlwave-help-in-header nil)
1039 (setq pos idlwave-help-def-pos))
1040 ;; Try to display header
1041 (setq pos (idlwave-help-find-in-doc-header
1042 (nth 0 idlwave-help-args)
1043 (nth 1 idlwave-help-args)
1044 (nth 2 idlwave-help-args)
1047 (setq idlwave-help-in-header t)
1048 (error "Cannot find doclib header for routine %s"
1049 (idlwave-make-full-name (nth 2 idlwave-help-args)
1050 (nth 0 idlwave-help-args)))))
1056 (defun idlwave-help-find-first-header (arg)
1060 (goto-char (point-min))
1061 (if (re-search-forward idlwave-doclib-start nil t)
1062 (setq pos (match-beginning 0))))
1067 (error "No DocLib Header in current file"))))
1069 (defun idlwave-help-find-header (arg)
1070 "Jump to the DocLib Header."
1073 (idlwave-help-find-first-header nil)
1074 (setq idlwave-help-in-header nil)
1075 (idlwave-help-toggle-header-match-and-def arg 'top)))
1077 (defun idlwave-help-toggle-header-match-and-def (arg &optional top)
1079 (let ((args idlwave-help-args)
1081 (if idlwave-help-in-header
1082 ;; Header was the last thing displayed
1084 (setq idlwave-help-in-header nil)
1085 (setq pos idlwave-help-def-pos))
1086 ;; Try to display header
1087 (setq pos (apply 'idlwave-help-find-in-doc-header
1089 (list (car args) (nth 1 args) (nth 2 args) nil)
1092 (setq idlwave-help-in-header t)
1093 (error "Cannot find doclib header for routine %s"
1094 (idlwave-make-full-name (nth 2 idlwave-help-args)
1095 (nth 0 idlwave-help-args)))))
1101 (defvar font-lock-verbose)
1102 (defvar idlwave-mode-syntax-table)
1103 (defvar idlwave-font-lock-defaults)
1104 (defun idlwave-help-fontify ()
1105 "Fontify the Help buffer as source code.
1106 Useful when source code is displayed as help. See the option
1107 `idlwave-help-fontify-source-code'."
1109 (if (featurep 'font-lock)
1110 (let ((major-mode 'idlwave-mode)
1112 (if (interactive-p) font-lock-verbose nil))
1113 (syntax-table (syntax-table)))
1116 (set-syntax-table idlwave-mode-syntax-table)
1117 (set (make-local-variable 'font-lock-defaults)
1118 idlwave-font-lock-defaults)
1119 (font-lock-fontify-buffer))
1120 (set-syntax-table syntax-table)))))
1123 (defun idlwave-help-error (name type class keyword)
1124 (error "Can't find help on %s%s %s"
1125 (or (and (or class name) (idlwave-make-full-name class name))
1127 (if keyword (format ", keyword %s" (upcase keyword)) "")
1128 (if idlwave-html-help-location
1130 "(help location unknown)")))
1132 (defun idlwave-help-show-help-frame ()
1133 "Show the help frame, creating it if necessary"
1134 ;; Use a special frame for this
1135 (unless (frame-live-p idlwave-help-frame)
1136 (setq idlwave-help-frame
1137 (make-frame idlwave-help-frame-parameters))
1138 ;; Strip menubar (?) and toolbar from the Help frame.
1139 (if (fboundp 'set-specifier)
1142 (let ((sval (cons idlwave-help-frame nil)))
1143 ;; (set-specifier menubar-visible-p sval)
1144 (set-specifier default-toolbar-visible-p sval)))
1146 (modify-frame-parameters idlwave-help-frame
1147 '(;;(menu-bar-lines . 0)
1148 (tool-bar-lines . 0)))))
1149 (select-frame idlwave-help-frame))
1151 (defun idlwave-help-get-help-buffer ()
1152 "Return the IDLWAVE Help buffer. Make it first if necessary."
1153 (let ((buf (get-buffer "*IDLWAVE Help*")))
1156 (setq buf (get-buffer-create "*IDLWAVE Help*"))
1159 (idlwave-help-mode)))
1162 (defun idlwave-grep (regexp list)
1165 (if (string-match regexp (car list))
1166 (setq rtn (cons (car list) rtn)))
1167 (setq list (cdr list)))
1170 (defun idlwave-entry-has-help (entry)
1171 (and entry (car (nth 5 entry))))
1173 (defun idlwave-has-help (name type class)
1174 "Does this have help associated with it?"
1175 (let ((entry (idlwave-best-rinfo-assoc name type class (idlwave-routines))))
1176 (idlwave-entry-has-help entry)))
1178 (provide 'idlw-help)
1179 (provide 'idlwave-help)
1181 ;; arch-tag: d27b5505-59de-497f-ba3f-f199fd4fb911
1182 ;;; idlw-help.el ends here