(latexenc-find-file-coding-system): Don't inherit the EOL part of the
[emacs.git] / lisp / progmodes / idlw-help.el
blob6c2cb00bbde5df66a33ea61535cca249e0485085
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
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)
16 ;; any later version.
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.
28 ;;; Commentary:
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:
38 ;; http://idlwave.org
40 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
43 ;;; Code:
44 (require 'browse-url)
46 (defgroup idlwave-online-help nil
47 "Online Help options for IDLWAVE mode."
48 :group 'idlwave)
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)
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))
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)
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)))
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)
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)
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)
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)
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
109 :type 'boolean)
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)))
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)
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
142 throwing an error.
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
153 :type 'symbol)
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)
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)
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)
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)
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)
192 (defvar idlwave-help-activate-links-aggressively nil
193 "Obsolete variable.")
195 (defvar idlwave-completion-help-info)
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.")
202 (defvar idlwave-html-help-is-available nil
203 "Is the system online help text avaiable?")
205 (defvar idlwave-help-mode-line-indicator ""
206 "Used for the special mode line in the idlwave-help-mode.")
208 (defvar idlwave-help-window-configuration nil)
209 (defvar idlwave-help-special-topic-words nil) ; defined by get_rinfo
211 ;; Define the key bindings for the Help application
213 (defvar idlwave-help-mode-map (make-sparse-keymap)
214 "The keymap used in idlwave-help-mode.")
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)
230 ;; Define the menu for the Help application
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]))
243 (defun idlwave-help-mode ()
244 "Major mode for displaying IDL Help.
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.
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
254 When the hep text is a source file, the following commands are available
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
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-mode-hooks 'idlwave-help-mode-hook))
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))))
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 "; "))))))
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))
366 ;; A special topic -- only system help
367 (st-ass (setq mod1 (list (cdr st-ass))))
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))))
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-string
391 word
392 idlwave-executive-commands-alist
393 t))))
394 (setq mod1 (list link))))
396 ;; A class -- system OR in-text help (via class__define).
397 ((and (eq cw 'class)
398 (or (idlwave-in-quote) ; e.g. obj_new
399 (re-search-backward "\\<inherits[ \t]+[A-Za-z0-9_]*\\="
400 (max (point-min) (- (point) 40)) t)))
401 ;; Class completion inside string delimiters must be
402 ;; the class inside OBJ_NEW.
403 (let* ((entry (assq
404 (idlwave-sintern-class this-word)
405 idlwave-system-class-info))
406 (name (concat (downcase this-word) "__define"))
407 (link (nth 1 (assq 'link entry))))
408 (setq mod1 (list link name 'pro))))
410 ;; A class structure tag (self.BLAH) -- only in-text help available
411 (classtag
412 (let ((tag (substring this-word (match-end 0)))
413 class-with found-in)
414 (when (setq class-with
415 (idlwave-class-or-superclass-with-tag
416 (nth 2 (idlwave-current-routine))
417 tag))
418 (setq found-in (idlwave-class-found-in class-with))
419 (if (assq (idlwave-sintern-class class-with)
420 idlwave-system-class-info)
421 (error "No help available for system class tags"))
422 (setq idlwave-help-do-class-struct-tag t)
423 (setq mod1 (list nil
424 (if found-in
425 (cons (concat found-in "__define") class-with)
426 (concat class-with "__define"))
427 'pro
428 nil ; no class.... it's a procedure!
429 tag)))))
431 ;; A regular structure tag -- only in text, and if
432 ;; optional `complete-structtag' loaded.
433 (structtag
434 (let ((var (match-string 1 this-word))
435 (tag (substring this-word (match-end 0))))
436 ;; Check if we need to update the "current" structure
437 (idlwave-prepare-structure-tag-completion var)
438 (setq idlwave-help-do-struct-tag
439 idlwave-structtag-struct-location
440 mod1 (list nil nil nil nil tag))))
442 ;; A routine keyword -- in text or system help
443 ((and (memq cw '(function-keyword procedure-keyword))
444 (stringp this-word)
445 (string-match "\\S-" this-word)
446 (not (string-match "!" this-word)))
447 (cond ((or (= (char-before beg) ?/)
448 (save-excursion (goto-char end)
449 (looking-at "[ \t]*=")))
450 ;; Certainly a keyword. Check for abbreviation etc.
451 (setq keyword (idlwave-expand-keyword this-word module))
452 (cond
453 ((null keyword)
454 (idlwave-help-diagnostics
455 (format "%s does not accept `%s' kwd"
456 (idlwave-make-full-name (nth 2 module)
457 (car module))
458 (upcase this-word))
459 'ding))
460 ((consp keyword)
461 (idlwave-help-diagnostics
462 (format "%d matches for kwd abbrev `%s'"
463 (length keyword) this-word)
464 'ding)
465 ;; We continue anyway with the first match...
466 (setq keyword (car keyword))))
467 ;; Keyword, or just module
468 (setq mod1 (append (list t) module (list keyword)))
469 (setq mod2 (append (list t) module)))
470 ((equal (char-after end) ?\()
471 ;; A function - what-module will have caught this
472 (setq mod1 (append (list t) module)))
474 ;; undecided - try function, keyword, then enclosing mod.
475 ;; Check for keyword abbreviations, but do not report
476 ;; errors, because it might be something else.
477 ;; FIXME: is this a good way to handle this?
478 (setq keyword (idlwave-expand-keyword this-word module))
479 (if (consp keyword) (setq keyword (car keyword)))
480 (setq mod1 (append (list t) module (list keyword))
481 mod2 (list t this-word 'fun nil)
482 mod3 (append (list t) module)))))
484 ;; Everything else
486 (setq mod1 (append (list t) module))))
487 (if mod3
488 (condition-case nil
489 (apply 'idlwave-online-help mod1)
490 (error (condition-case nil
491 (apply 'idlwave-online-help mod2)
492 (error (apply 'idlwave-online-help mod3)))))
493 (if mod2
494 (condition-case nil
495 (apply 'idlwave-online-help mod1)
496 (error (apply 'idlwave-online-help mod2)))
497 (if mod1
498 (apply 'idlwave-online-help mod1)
499 (error "Don't know which item to show help for")))))))
501 (defun idlwave-do-mouse-completion-help (ev)
502 "Display online help on an item in the *Completions* buffer.
503 Needs additional info stored in global `idlwave-completion-help-info'."
504 (let* ((cw (selected-window))
505 (info idlwave-completion-help-info) ; global passed in
506 (what (nth 0 info))
507 (name (nth 1 info))
508 (type (nth 2 info))
509 (class (nth 3 info))
510 (need-class class)
511 (kwd (nth 4 info))
512 (sclasses (nth 5 info))
513 word link)
514 (mouse-set-point ev)
517 ;; See if we can also find help somewhere, e.g. for multiple classes
518 (setq word (idlwave-this-word))
519 (if (string= word "")
520 (error "No help item selected"))
521 (setq link (get-text-property 0 'link word))
522 (select-window cw)
523 (cond
524 ;; Routine name
525 ((memq what '(procedure function routine))
526 (setq name word)
527 (if (or (eq class t)
528 (and (stringp class) sclasses))
529 (let* ((classes (idlwave-all-method-classes
530 (idlwave-sintern-method name)
531 type)))
532 (setq link t) ; No specific link valid yet
533 (if sclasses
534 (setq classes (idlwave-members-only
535 classes (cons class sclasses))))
536 (setq class (idlwave-popup-select ev classes
537 "Select Class" 'sort))))
539 ;; XXX is this necessary, given all-method-classes?
540 (if (stringp class)
541 (setq class (idlwave-find-inherited-class
542 (idlwave-sintern-routine-or-method name class)
543 type (idlwave-sintern-class class)))))
545 ;; Keyword
546 ((eq what 'keyword)
547 (setq kwd word)
548 (if (or (eq class t)
549 (and (stringp class) sclasses))
550 (let ((classes (idlwave-all-method-keyword-classes
551 (idlwave-sintern-method name)
552 (idlwave-sintern-keyword kwd)
553 type)))
554 (setq link t) ; Link can't be correct yet
555 (if sclasses
556 (setq classes (idlwave-members-only
557 classes (cons class sclasses))))
558 (setq class (idlwave-popup-select ev classes
559 "Select Class" 'sort))
560 ;; XXX is this necessary, given all-method-keyword-classes?
561 (if (stringp class)
562 (setq class (idlwave-find-inherited-class
563 (idlwave-sintern-routine-or-method name class)
564 type (idlwave-sintern-class class)))))
565 (if (string= (downcase name) "obj_new")
566 (setq class idlwave-current-obj_new-class
567 name "Init"))))
569 ;; Class name
570 ((eq what 'class)
571 (setq class word
572 word nil))
574 ;; A special named function to call which sets some of our variables
575 ((and (symbolp what)
576 (fboundp what))
577 (funcall what 'set word))
579 (t (error "Cannot help with this item")))
580 (if (and need-class (not class) (not (and link (not (eq link t)))))
581 (error "Cannot help with this item"))
582 (idlwave-online-help link (or name word) type class kwd)))
584 (defvar idlwave-highlight-help-links-in-completion)
585 (defvar idlwave-completion-help-links)
586 (defun idlwave-highlight-linked-completions ()
587 "Highlight all completions for which help is available and attach link.
588 Those words in `idlwave-completion-help-links' have links. The
589 `idlwave-help-link-face' face is used for this."
590 (if idlwave-highlight-help-links-in-completion
591 (with-current-buffer (get-buffer "*Completions*")
592 (save-excursion
593 (let* ((case-fold-search t)
594 (props (list 'face 'idlwave-help-link-face))
595 (info idlwave-completion-help-info) ; global passed in
596 (what (nth 0 info)) ; what was completed, or a func
597 (class (nth 3 info)) ; any class
598 word beg end doit)
599 (goto-char (point-min))
600 (re-search-forward "possible completions are:" nil t)
601 (while (re-search-forward "\\s-\\([A-Za-z0-9_.]+\\)\\(\\s-\\|\\'\\)"
602 nil t)
603 (setq beg (match-beginning 1) end (match-end 1)
604 word (match-string 1) doit nil)
605 ;; Call special completion function test
606 (if (and (symbolp what)
607 (fboundp what))
608 (setq doit (funcall what 'test word))
609 ;; Look for special link property passed in help-links
610 (if idlwave-completion-help-links
611 (setq doit (assoc-string
612 word idlwave-completion-help-links t))))
613 (when doit
614 (if (consp doit)
615 (setq props (append props `(link ,(cdr doit)))))
616 (let ((buffer-read-only nil))
617 (add-text-properties beg end props)))
618 (goto-char end)))))))
620 ;; Arrange for this function to be called after completion
621 (add-hook 'idlwave-completion-setup-hook
622 'idlwave-highlight-linked-completions)
624 (defvar idlwave-help-return-frame nil
625 "The frame to return to from the help frame.")
627 (defun idlwave-help-quit ()
628 "Exit IDLWAVE Help buffer. Kill the dedicated frame if any."
629 (interactive)
630 (cond ((and idlwave-help-use-dedicated-frame
631 (eq (selected-frame) idlwave-help-frame))
632 (if (and idlwave-experimental
633 (frame-live-p idlwave-help-return-frame))
634 ;; Try to select the return frame.
635 ;; This can crash on slow network connections, obviously when
636 ;; we kill the help frame before the return-frame is selected.
637 ;; To protect the workings, we wait for up to one second
638 ;; and check if the return-frame *is* now selected.
639 ;; This is marked "eperimental" since we are not sure when its OK.
640 (let ((maxtime 1.0) (time 0.) (step 0.1))
641 (select-frame idlwave-help-return-frame)
642 (while (and (sit-for step)
643 (not (eq (selected-frame)
644 idlwave-help-return-frame))
645 (< (setq time (+ time step)) maxtime)))))
646 (delete-frame idlwave-help-frame))
647 ((window-configuration-p idlwave-help-window-configuration)
648 (set-window-configuration idlwave-help-window-configuration)
649 (select-window (previous-window)))
650 (t (kill-buffer (idlwave-help-get-help-buffer)))))
653 (defvar default-toolbar-visible-p)
655 (defun idlwave-help-display-help-window (&optional pos-or-func)
656 "Display the help window.
657 Move window start to POS-OR-FUNC, if passed as a position, or call it
658 if passed as a function. See `idlwave-help-use-dedicated-frame'."
659 (let ((cw (selected-window))
660 (buf (idlwave-help-get-help-buffer)))
661 (if (and window-system idlwave-help-use-dedicated-frame)
662 (progn
663 (idlwave-help-show-help-frame)
664 (switch-to-buffer buf))
665 ;; Do it in this frame and save the window configuration
666 (if (not (get-buffer-window buf nil))
667 (setq idlwave-help-window-configuration
668 (current-window-configuration)))
669 (display-buffer buf nil (selected-frame))
670 (select-window (get-buffer-window buf)))
671 (raise-frame)
672 (if pos-or-func
673 (if (functionp pos-or-func)
674 (funcall pos-or-func)
675 (goto-char pos-or-func)
676 (recenter 0)))
677 (select-window cw)))
679 (defun idlwave-help-select-help-frame ()
680 "Select the help frame."
681 (if (and (frame-live-p idlwave-help-frame)
682 (not (eq (selected-frame) idlwave-help-frame)))
683 (progn
684 (setq idlwave-help-return-frame (selected-frame))
685 (select-frame idlwave-help-frame))))
687 (defun idlwave-help-return-to-calling-frame ()
688 "Select the frame from which the help frame was selected."
689 (interactive)
690 (if (and (frame-live-p idlwave-help-return-frame)
691 (not (eq (selected-frame) idlwave-help-return-frame)))
692 (select-frame idlwave-help-return-frame)))
694 (defun idlwave-online-help (link &optional name type class keyword)
695 "Display HTML or other special help on a certain topic.
696 Either loads an HTML link, if LINK is non-nil, or gets special-help on
697 the optional arguments, if any special help is defined. If LINK is
698 `t', first look up the optional arguments in the routine info list to
699 see if a link is set for it. Try extra help functions if necessary."
700 ;; Lookup link
701 (if (eq link t)
702 (let ((entry (idlwave-best-rinfo-assoc name type class
703 (idlwave-routines) nil t)))
704 (cond
705 ;; Try keyword link
706 ((and keyword
707 (setq link (cdr (idlwave-entry-find-keyword entry keyword)))))
708 ;; Default, regular entry link
709 (t (setq link (idlwave-entry-has-help entry))))))
711 (cond
712 ;; An explicit link
713 ((stringp link)
714 (idlwave-help-html-link link))
716 ;; Any extra help
717 (idlwave-extra-help-function
718 (idlwave-help-get-special-help name type class keyword))
720 ;; Nothing worked
721 (t (idlwave-help-error name type class keyword))))
724 (defun idlwave-help-get-special-help (name type class keyword)
725 "Call the function given by `idlwave-extra-help-function'."
726 (let* ((cw (selected-window))
727 (help-pos (save-excursion
728 (set-buffer (idlwave-help-get-help-buffer))
729 (let ((buffer-read-only nil))
730 (funcall idlwave-extra-help-function
731 name type class keyword)))))
732 (if help-pos
733 (idlwave-help-display-help-window help-pos)
734 (idlwave-help-error name type class keyword))
735 (select-window cw)))
737 (defun idlwave-help-html-link (link)
738 "Get html help on a given LINK."
739 (let ((browse-url-browser-function idlwave-help-browser-function)
740 (help-loc (idlwave-html-help-location))
741 (browse-url-generic-program idlwave-help-browser-generic-program)
742 ;(browse-url-generic-args idlwave-help-browser-generic-args)
743 full-link)
745 (if (and (memq system-type '(ms-dos windows-nt))
746 idlwave-help-use-hh)
747 (progn
748 (setq browse-url-browser-function 'browse-url-generic
749 full-link (concat (expand-file-name "idl.chm" help-loc)
750 "::/"
751 link))
752 (if (memq 'keyhh idlwave-help-use-hh)
753 (setq browse-url-generic-program "KEYHH"
754 browse-url-generic-args '("-IDLWAVE"))
755 (setq browse-url-generic-program "HH")))
756 ;; Just a regular file name (+ anchor name)
757 (unless (and (stringp help-loc)
758 (file-directory-p help-loc))
759 (error
760 "Invalid help location; customize `idlwave-html-help-location'."))
761 (setq full-link (concat
762 "file://"
763 (expand-file-name
764 link
765 (expand-file-name "idl_html_help" help-loc)))))
767 ;; Check for a local browser
768 (if (or idlwave-help-browser-is-local
769 (string-match "w3" (symbol-name idlwave-help-browser-function)))
770 (idlwave-help-display-help-window '(lambda () (browse-url full-link)))
771 (browse-url full-link))))
773 ;; A special help routine for source-level syntax help in files.
774 (defvar idlwave-help-def-pos)
775 (defvar idlwave-help-args)
776 (defvar idlwave-help-in-header)
777 (defvar idlwave-help-fontify-source-code)
778 (defvar idlwave-help-source-try-header)
779 (defun idlwave-help-with-source (name type class keyword)
780 "Provide help for routines not documented in the IDL manuals. Works
781 by loading the routine source file into the help buffer. Depending on
782 the value of `idlwave-help-source-try-header', it attempts to show the
783 routine definition or the header description. If
784 `idlwave-help-do-class-struct-tag' is non-nil, keyword is a tag to
785 show help on from the class definition structure. If
786 `idlwave-help-do-struct-tag' is non-nil, show help from the matching
787 structure tag definition.
789 This function can be used as `idlwave-extra-help-function'."
790 (let* ((class-struct-tag idlwave-help-do-class-struct-tag)
791 (struct-tag idlwave-help-do-struct-tag)
792 (case-fold-search t)
793 (real-class (if (consp name) (cdr name)))
794 (name (if (consp name) (car name) name))
795 (class-only (and (stringp class) (not (stringp name))))
796 file header-pos def-pos in-buf)
797 (if class-only ;Help with class? Using "Init" as source.
798 (setq name "Init"
799 type 'fun))
800 (if (not struct-tag)
801 (setq file
802 (idlwave-routine-source-file
803 (nth 3 (idlwave-best-rinfo-assoc
804 name (or type t) class (idlwave-routines))))))
805 (setq idlwave-help-def-pos nil
806 idlwave-help-args (list name type class keyword)
807 idlwave-help-in-header nil
808 idlwave-help-do-struct-tag nil
809 idlwave-help-do-class-struct-tag nil)
810 (if (or struct-tag (stringp file))
811 (progn
812 (setq in-buf ; structure-tag completion is always in current buffer
813 (if struct-tag
814 idlwave-current-tags-buffer
815 (idlwave-get-buffer-visiting file)))
816 ;; see if file is in a visited buffer, insert those contents
817 (if in-buf
818 (progn
819 (setq file (buffer-file-name in-buf))
820 (erase-buffer)
821 (insert-buffer in-buf))
822 (if (file-exists-p file) ;; otherwise just load the file
823 (progn
824 (erase-buffer)
825 (insert-file-contents file nil nil nil 'replace))
826 (idlwave-help-error name type class keyword)))
827 (if (and idlwave-help-fontify-source-code (not in-buf))
828 (idlwave-help-fontify)))
829 (idlwave-help-error name type class keyword))
830 (setq idlwave-help-mode-line-indicator file)
832 ;; Try to find a good place to display
833 (setq def-pos
834 ;; Find the class structure tag if that's what we're after
835 (cond
836 ;; Class structure tags: find the class or named structure
837 ;; definition
838 (class-struct-tag
839 (save-excursion
840 (setq class
841 (if (string-match "[a-zA-Z0-9]\\(__\\)" name)
842 (substring name 0 (match-beginning 1))
843 idlwave-current-tags-class))
844 (and
845 (idlwave-find-class-definition class nil real-class)
846 (idlwave-find-struct-tag keyword))))
848 ;; Generic structure tags: the structure definition
849 ;; location within the file has been recorded in
850 ;; `struct-tag'
851 (struct-tag
852 (save-excursion
853 (and
854 (integerp struct-tag)
855 (goto-char struct-tag)
856 (idlwave-find-struct-tag keyword))))
858 ;; Just find the routine definition
860 (if class-only (point-min)
861 (idlwave-help-find-routine-definition name type class keyword))))
862 idlwave-help-def-pos def-pos)
864 (if (and idlwave-help-source-try-header
865 (not (or struct-tag class-struct-tag)))
866 ;; Check if we can find the header
867 (save-excursion
868 (goto-char (or def-pos (point-max)))
869 (setq header-pos (idlwave-help-find-in-doc-header
870 name type class keyword 'exact)
871 idlwave-help-in-header header-pos)))
873 (if (or header-pos def-pos)
874 (progn
875 (if (boundp 'idlwave-help-min-frame-width)
876 (setq idlwave-help-min-frame-width 80))
877 (goto-char (or header-pos def-pos)))
878 (idlwave-help-error name type class keyword))
880 (point)))
883 (defun idlwave-help-find-routine-definition (name type class keyword)
884 "Find the definition of routine CLASS::NAME in current buffer.
885 KEYWORD is ignored. Returns the point of match if successful, nil otherwise."
886 (save-excursion
887 (goto-char (point-max))
888 (if (re-search-backward
889 (concat "^[ \t]*"
890 (if (eq type 'pro) "pro"
891 (if (eq type 'fun) "function"
892 "\\(pro\\|function\\)"))
893 "[ \t]+"
894 (regexp-quote (downcase (idlwave-make-full-name class name)))
895 "[, \t\r\n]")
896 nil t)
897 (match-beginning 0)
898 nil)))
900 (defvar idlwave-doclib-start)
901 (defvar idlwave-doclib-end)
902 (defun idlwave-help-find-in-doc-header (name type class keyword
903 &optional exact)
904 "Find the requested help in the doc-header above point.
906 First checks if there is a doc-lib header which describes the correct
907 routine. Then tries to find the KEYWORDS section and the KEYWORD, if
908 given. Returns the point which should be window start of the help
909 window. If EXACT is non-nil, the full help position must be found -
910 down to the keyword requested. This setting is for context help, if
911 the exact spot is needed.
913 If EXACT is nil, the position of the header is returned if it
914 describes the correct routine - even if the keyword description cannot
915 be found. TYPE is ignored.
917 This function expects a more or less standard routine header. In
918 particlar it looks for the `NAME:' tag, either with a colon, or alone
919 on a line. Then `NAME:' must be followed by the routine name on the
920 same or the next line. When KEYWORD is non-nil, looks first for a
921 `KEYWORDS' section. It is amazing how inconsisten this is through
922 some IDL libraries I have seen. We settle for a line containing an
923 upper case \"KEYWORD\" string. If this line is not fould we search
924 for the keyword anyway to increase the hit-rate
926 When one of these sections exists we check for a line starting with any of
928 /KEYWORD KEYWORD- KEYWORD= KEYWORD
930 with spaces allowed between the keyword and the following dash or equal sign.
931 If there is a match, we assume it is the keyword description."
932 (let* ((case-fold-search t)
933 (rname (if (stringp class)
934 (concat
935 "\\("
936 ;; Traditional name or class::name
937 "\\("
938 "\\(" (regexp-quote (downcase class)) "::\\)?"
939 (regexp-quote (downcase name))
940 "\\>\\)"
941 (concat
942 "\\|"
943 ;; class__define or just class
944 (regexp-quote (downcase class)) "\\(__define\\)?")
945 "\\)")
946 (regexp-quote (downcase name))))
948 ;; NAME tag plus the routine name. The new version is from JD.
949 (name-re (concat
950 "\\(^;+\\*?[ \t]*"
951 idlwave-help-doclib-name
952 "\\([ \t]*:\\|[ \t]*$\\)[ \t]*\\(\n;+[ \t]*\\)*"
953 rname
954 "\\|"
955 "^;+[ \t]*"
956 rname
957 ":[ \t]*$\\)"))
959 ;; Header start plus name
960 (header-re (concat "\\(" idlwave-doclib-start "\\).*\n"
961 "\\(^;+.*\n\\)*"
962 "\\(" name-re "\\)"))
963 ;; A keywords section
964 (kwds-re (concat ; forgiving
965 "^;+\\*?[ \t]*"
966 "\\([-A-Z_ ]*"
967 idlwave-help-doclib-keyword
968 "[-A-Z_ ]*\\)"
969 "\\(:\\|[ \t]*\n\\)"))
971 ;; The individual keyword description line.
972 (kwd-re (if keyword ; hard (well...)
973 (concat
974 "^;+[ \t]+"
975 "\\(/" (regexp-quote (upcase keyword))
976 "\\|" (regexp-quote (upcase keyword)) "[ \t]*[-=:\n]"
977 "\\)")))
978 (kwd-re2 (if keyword ; forgiving
979 (concat
980 "^;+[ \t]+"
981 (regexp-quote (upcase keyword))
982 "\\>")))
983 dstart dend name-pos kwds-pos kwd-pos)
984 (catch 'exit
985 (save-excursion
986 (goto-char (point-min))
987 (while (and (setq dstart (re-search-forward idlwave-doclib-start nil t))
988 (setq dend (re-search-forward idlwave-doclib-end nil t)))
989 ;; found a routine header
990 (goto-char dstart)
991 (if (setq name-pos (re-search-forward name-re dend t))
992 (progn
993 (if keyword
994 ;; We do need a keyword
995 (progn
996 ;; Try to find a keyword section, but don't force it.
997 (goto-char name-pos)
998 (if (let ((case-fold-search nil))
999 (re-search-forward kwds-re dend t))
1000 (setq kwds-pos (match-beginning 0)))
1001 ;; Find the keyword description
1002 (if (or (let ((case-fold-search nil))
1003 (re-search-forward kwd-re dend t))
1004 (re-search-forward kwd-re dend t)
1005 (let ((case-fold-search nil))
1006 (re-search-forward kwd-re2 dend t))
1007 (re-search-forward kwd-re2 dend t))
1008 (setq kwd-pos (match-beginning 0))
1009 (if exact
1010 (progn
1011 (idlwave-help-diagnostics
1012 (format "Could not find description of kwd %s"
1013 (upcase keyword)))
1014 (throw 'exit nil))))))
1015 ;; Return the best position we got
1016 (throw 'exit (or kwd-pos kwds-pos name-pos dstart)))
1017 (goto-char dend))))
1018 (idlwave-help-diagnostics "Could not find doclib header")
1019 (throw 'exit nil))))
1021 (defun idlwave-help-diagnostics (string &optional ding)
1022 "Add a diagnostics string to the list.
1023 When DING is non-nil, ring the bell as well."
1024 (if (boundp 'idlwave-help-diagnostics)
1025 (progn
1026 (setq idlwave-help-diagnostics
1027 (cons string idlwave-help-diagnostics))
1028 (if ding (ding)))))
1030 (defun idlwave-help-toggle-header-top-and-def (arg)
1031 (interactive "P")
1032 (let (pos)
1033 (if idlwave-help-in-header
1034 ;; Header was the last thing displayed
1035 (progn
1036 (setq idlwave-help-in-header nil)
1037 (setq pos idlwave-help-def-pos))
1038 ;; Try to display header
1039 (setq pos (idlwave-help-find-in-doc-header
1040 (nth 0 idlwave-help-args)
1041 (nth 1 idlwave-help-args)
1042 (nth 2 idlwave-help-args)
1043 nil))
1044 (if pos
1045 (setq idlwave-help-in-header t)
1046 (error "Cannot find doclib header for routine %s"
1047 (idlwave-make-full-name (nth 2 idlwave-help-args)
1048 (nth 0 idlwave-help-args)))))
1049 (if pos
1050 (progn
1051 (goto-char pos)
1052 (recenter 0)))))
1054 (defun idlwave-help-find-first-header (arg)
1055 (interactive "P")
1056 (let (pos)
1057 (save-excursion
1058 (goto-char (point-min))
1059 (if (re-search-forward idlwave-doclib-start nil t)
1060 (setq pos (match-beginning 0))))
1061 (if pos
1062 (progn
1063 (goto-char pos)
1064 (recenter 0))
1065 (error "No DocLib Header in current file"))))
1067 (defun idlwave-help-find-header (arg)
1068 "Jump to the DocLib Header."
1069 (interactive "P")
1070 (if arg
1071 (idlwave-help-find-first-header nil)
1072 (setq idlwave-help-in-header nil)
1073 (idlwave-help-toggle-header-match-and-def arg 'top)))
1075 (defun idlwave-help-toggle-header-match-and-def (arg &optional top)
1076 (interactive "P")
1077 (let ((args idlwave-help-args)
1078 pos)
1079 (if idlwave-help-in-header
1080 ;; Header was the last thing displayed
1081 (progn
1082 (setq idlwave-help-in-header nil)
1083 (setq pos idlwave-help-def-pos))
1084 ;; Try to display header
1085 (setq pos (apply 'idlwave-help-find-in-doc-header
1086 (if top
1087 (list (car args) (nth 1 args) (nth 2 args) nil)
1088 args)))
1089 (if pos
1090 (setq idlwave-help-in-header t)
1091 (error "Cannot find doclib header for routine %s"
1092 (idlwave-make-full-name (nth 2 idlwave-help-args)
1093 (nth 0 idlwave-help-args)))))
1094 (if pos
1095 (progn
1096 (goto-char pos)
1097 (recenter 0)))))
1099 (defvar font-lock-verbose)
1100 (defvar idlwave-mode-syntax-table)
1101 (defvar idlwave-font-lock-defaults)
1102 (defun idlwave-help-fontify ()
1103 "Fontify the Help buffer as source code.
1104 Useful when source code is displayed as help. See the option
1105 `idlwave-help-fontify-source-code'."
1106 (interactive)
1107 (if (featurep 'font-lock)
1108 (let ((major-mode 'idlwave-mode)
1109 (font-lock-verbose
1110 (if (interactive-p) font-lock-verbose nil))
1111 (syntax-table (syntax-table)))
1112 (unwind-protect
1113 (progn
1114 (set-syntax-table idlwave-mode-syntax-table)
1115 (set (make-local-variable 'font-lock-defaults)
1116 idlwave-font-lock-defaults)
1117 (font-lock-fontify-buffer))
1118 (set-syntax-table syntax-table)))))
1121 (defun idlwave-help-error (name type class keyword)
1122 (error "Can't find help on %s%s %s"
1123 (or (and (or class name) (idlwave-make-full-name class name))
1124 "<unknown>")
1125 (if keyword (format ", keyword %s" (upcase keyword)) "")
1126 (if idlwave-html-help-location
1128 "(help location unknown)")))
1130 (defun idlwave-help-show-help-frame ()
1131 "Show the help frame, creating it if necessary"
1132 ;; Use a special frame for this
1133 (unless (frame-live-p idlwave-help-frame)
1134 (setq idlwave-help-frame
1135 (make-frame idlwave-help-frame-parameters))
1136 ;; Strip menubar (?) and toolbar from the Help frame.
1137 (if (fboundp 'set-specifier)
1138 (progn
1139 ;; XEmacs
1140 (let ((sval (cons idlwave-help-frame nil)))
1141 ;; (set-specifier menubar-visible-p sval)
1142 (set-specifier default-toolbar-visible-p sval)))
1143 ;; Emacs
1144 (modify-frame-parameters idlwave-help-frame
1145 '(;;(menu-bar-lines . 0)
1146 (tool-bar-lines . 0)))))
1147 (select-frame idlwave-help-frame))
1149 (defun idlwave-help-get-help-buffer ()
1150 "Return the IDLWAVE Help buffer. Make it first if necessary."
1151 (let ((buf (get-buffer "*IDLWAVE Help*")))
1152 (if buf
1154 (setq buf (get-buffer-create "*IDLWAVE Help*"))
1155 (save-excursion
1156 (set-buffer buf)
1157 (idlwave-help-mode)))
1158 buf))
1160 (defun idlwave-grep (regexp list)
1161 (let (rtn)
1162 (while list
1163 (if (string-match regexp (car list))
1164 (setq rtn (cons (car list) rtn)))
1165 (setq list (cdr list)))
1166 (nreverse rtn)))
1168 (defun idlwave-entry-has-help (entry)
1169 (and entry (car (nth 5 entry))))
1171 (defun idlwave-has-help (name type class)
1172 "Does this have help associated with it?"
1173 (let ((entry (idlwave-best-rinfo-assoc name type class (idlwave-routines))))
1174 (idlwave-entry-has-help entry)))
1176 (provide 'idlw-help)
1177 (provide 'idlwave-help)
1179 ;; arch-tag: d27b5505-59de-497f-ba3f-f199fd4fb911
1180 ;;; idlw-help.el ends here