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