1 ;;; ess-help.el --- Support for viewing ESS help files
3 ;; Copyright (C) 1989-1994 Bates, Kademan, Ritter and Smith
4 ;; Copyright (C) 1997, A.J. Rossini <rossini@stat.sc.edu>
5 ;; Copyright (C) 1998--2001 A.J. Rossini, Martin Maechler, Kurt Hornik and
6 ;; Richard M. Heiberger <rmh@temple.edu>.
7 ;; Copyright (C) 2001--2004 A.J. Rossini, Rich M. Heiberger, Martin
8 ;; Maechler, Kurt Hornik, Rodney Sparapani, and Stephen Eglen.
10 ;; Original Author: David Smith <dsmith@stats.adelaide.edu.au>
11 ;; Created: 7 Jan 1994
12 ;; Maintainers: ESS-core <ESS-core@stat.math.ethz.ch>
14 ;; This file is part of ESS
16 ;; This file is free software; you can redistribute it and/or modify
17 ;; it under the terms of the GNU General Public License as published by
18 ;; the Free Software Foundation; either version 2, or (at your option)
21 ;; This file is distributed in the hope that it will be useful,
22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ;; GNU General Public License for more details.
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with GNU Emacs; see the file COPYING. If not, write to
28 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
32 ;; Code for dealing with ESS help files. See README.<LANGUAGE> where
33 ;; <LANGUAGE> is one of `S', `SAS', `Stata'or `XLispStat'.
37 \f ; Requires and autoloads
46 (autoload 'ess-eval-region
"ess-inf" "[autoload]" t
)
47 (autoload 'ess-eval-region-and-go
"ess-inf" "[autoload]" t
)
48 (autoload 'ess-eval-function
"ess-inf" "[autoload]" t
)
49 (autoload 'ess-eval-function-and-go
"ess-inf" "[autoload]" t
)
50 (autoload 'ess-eval-line
"ess-inf" "[autoload]" t
)
51 (autoload 'ess-eval-line-and-go
"ess-inf" "[autoload]" t
)
52 (autoload 'ess-eval-line-and-step
"ess-inf" "[autoload]" t
)
54 (autoload 'ess-beginning-of-function
"ess-mode" "[autoload]" t
)
55 (autoload 'ess-end-of-function
"ess-mode" "[autoload]" t
)
57 (autoload 'ess-load-file
"ess-inf" "[autoload]" t
)
58 (autoload 'ess-command
"ess-inf" "(autoload)" nil
)
59 (autoload 'ess-display-temp-buffer
"ess-inf" "(autoload)" nil
)
60 (autoload 'ess-switch-to-ESS
"ess-inf" "(autoload)" nil
)
61 (autoload 'ess-read-object-name-default
"ess-inf" "(autoload)" nil
)
62 (autoload 'ess-make-buffer-current
"ess-inf" "(autoload)" nil
)
63 (autoload 'ess-search-list
"ess-inf" "(autoload)" nil
)
64 (autoload 'ess-get-object-list
"ess-inf" "(autoload)" nil
)
66 (autoload 'ess-ddeclient-p
"ess-inf" "(autoload)" nil
)
68 (autoload 'ess-display-help-on-object-ddeclient
"ess-dde" "(autoload)" nil
)
72 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
75 ;;;; * The function ess-display-help-on-object
76 ;;;; * The major mode ess-help-mode
77 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
79 (defun ess-help-bogus-buffer-p (buffer &optional nr-first return-match debug
)
80 "Return non-nil if BUFFER looks like a bogus ESS help buffer.
81 NR-FIRST is the number of characters at the start of the buffer
82 to examine when deciding if the buffer if bogus. If nil, the
83 first 120 characters of the buffer are searched. Return pair
84 of (match-beg. match-end) when optional RETURN-MATCH is non-nil.
85 Utility used in \\[ess-display-help-on-object]."
87 ;; search in first nr-first (default 120) chars only
88 (if (not nr-first
) (setq nr-first
120))
90 (let* ((searching nil
)
91 (buffer-ok (bufferp buffer
))
94 (save-excursion;; ask for new buffer if old one looks bogus ..
97 (ess-write-to-dribble-buffer
98 (format "(ess-help-bogus-buffer-p %s)" (buffer-name))))
102 (case-fold-search t
) )
103 (or ;; evaluate up to first non-nil (or end):
104 (< (- (point-max) PM
) 80); buffer less than 80 chars
105 (not (setq searching t
))
106 (progn (goto-char PM
) ;; R:
107 (re-search-forward "Error in help" nr-first t
))
108 (progn (goto-char PM
) ;; S-plus 5.1 :
109 (re-search-forward "^cat: .*--" nr-first t
))
110 (progn (goto-char PM
) ;; S version 3 ; R :
111 (re-search-forward "no documentation" nr-first t
))
115 (ess-write-to-dribble-buffer
116 (format " |--> %s [searching %s]\n" res searching
)))
118 (if (and res return-match searching
)
119 (list (match-beginning 0) (match-end 0))
123 (defun ess-display-help-on-object (object)
124 "Display documentation for OBJECT in another window.
125 If prefix arg is given, forces a query of the ESS process for the help
126 file. Otherwise just pops to an existing buffer if it exists.
127 Uses the variable `inferior-ess-help-command' for the actual help command."
128 (interactive (ess-find-help-file "Help on: "))
130 (if (ess-ddeclient-p)
132 (progn (ess-display-help-on-object-ddeclient object
)
134 ;; else: "normal", non-DDE behavior:
135 (let* ((hb-name (concat "*help["
136 ess-current-process-name
138 (old-hb-p (get-buffer hb-name
))
139 (curr-win-mode major-mode
)
140 (tbuffer (get-buffer-create hb-name
))
141 (curr-help-command inferior-ess-help-command
)
142 ;;-- pass the buffer-local 'ess-help-sec-..' to the ess-help buffer:
143 (curr-help-sec-regex ess-help-sec-regex
)
144 (curr-help-sec-keys-alist ess-help-sec-keys-alist
)
145 (curr-help-syntax-table (syntax-table))
146 (alist ess-local-customize-alist
))
149 (ess-setq-vars-local (eval alist
))
150 (setq ess-help-sec-regex curr-help-sec-regex
)
151 (setq ess-help-sec-keys-alist curr-help-sec-keys-alist
)
152 ;; see above, do same for inferior-ess-help-command... (i.e. remove
153 ;; hack, restore old code :-).
155 (if (or (not old-hb-p
)
157 (ess-help-bogus-buffer-p old-hb-p nil nil
'debug
)
160 ;; Ask the corresponding ESS process for the help file:
162 (if buffer-read-only
(setq buffer-read-only nil
))
163 (delete-region (point-min) (point-max))
165 (setq ess-local-process-name ess-current-process-name
)
166 (ess-command (format curr-help-command object
) tbuffer
)
167 ;; was inferior-ess-help-command
170 ;; Stata is clean, so we get a big BARF from this.
171 (if (not (string= ess-language
"STA"))
174 (goto-char (point-min))))
177 (let ((PM (point-min))
179 (ess-help-bogus-buffer-p (current-buffer) nil
'give-match
)))
182 ess-help-kill-bogus-buffers
)
184 (if (not (listp nodocs
))
185 (setq nodocs
(list PM
(point-max))))
186 (ess-write-to-dribble-buffer
187 (format "(ess-help: error-buffer '%s' nodocs (%d %d)\n"
188 (buffer-name) (car nodocs
) (cadr nodocs
)))
189 ;; Avoid using 'message here -- may be %'s in string
190 ;;(princ (buffer-substring (car nodocs) (cadr nodocs)) t)
191 ;; MM [3/2000]: why avoid? Yes, I *do* want message:
192 (message "%s" (buffer-substring (car nodocs
) (cadr nodocs
)))
193 ;; ^^^ fixme : remove new lines from the above {and abbrev.}
195 (kill-buffer tbuffer
))
197 ;; else : show the help buffer.
199 ;; Check if this buffer describes where help can be found in
200 ;; various packages. (R only). This is a kind of bogus help
201 ;; buffer, but it should not be killed immediately even if
202 ;; ess-help-kill-bogus-buffers is t.
204 ;; e.g. if within R, the user does:
206 ;; > options("help.try.all.packages" = TRUE)
210 ;; then a list of packages for where ?rlm is defined is
211 ;; shown. (In this case, rlm is in package MASS). This
212 ;; help buffer is then renamed *help[R](rlm in packages)* so
213 ;; that after MASS is loaded, ?rlm will then show
216 (if (equal inferior-ess-program inferior-R-program-name
)
217 ;; this code should be used only for R processes.
219 (goto-char (point-min))
220 (if (looking-at "Help for topic")
223 (concat "*help[" ess-current-process-name
224 "](" object
" in packages)*")))
225 ;; if NEWBUF already exists, remove it.
226 (if (get-buffer newbuf
)
227 (kill-buffer newbuf
))
228 (rename-buffer newbuf
)))))
230 ;;dbg (ess-write-to-dribble-buffer
231 ;;dbg (format "(ess-help '%s' before switch-to..\n" hb-name)
232 (let ((special-display-regexps
233 (if ess-help-own-frame
'(".") nil
))
234 (special-display-frame-alist ess-help-frame-alist
)
235 (special-display-function
236 (if (eq ess-help-own-frame
'one
)
238 special-display-function
)))
239 (if (eq curr-win-mode
'ess-help-mode
)
240 (if ess-help-own-frame
241 (pop-to-buffer tbuffer
)
242 (switch-to-buffer tbuffer
))
243 (ess-display-temp-buffer tbuffer
)))
244 (if curr-help-syntax-table
245 (set-syntax-table curr-help-syntax-table
))
246 (set-buffer-modified-p 'nil
)
247 (toggle-read-only t
)))))))
249 (defvar ess-help-frame nil
250 "Stores the frame used for displaying R help buffers.")
252 (defun ess-help-own-frame (buffer &rest ignore
)
253 "Put all ESS help buffers into `ess-help-frame'."
254 ;; SJE: Code adapted from Kevin Rodgers.
255 (if (frame-live-p ess-help-frame
)
257 (or (frame-visible-p ess-help-frame
)
258 (make-frame-visible ess-help-frame
))
259 (raise-frame ess-help-frame
)
260 (select-frame ess-help-frame
)
261 (switch-to-buffer buffer
)
264 (let ((window (special-display-popup-frame buffer
)))
265 (set-window-dedicated-p window nil
)
266 (setq ess-help-frame
(window-frame window
))
273 ;(defun ess-display-w3-help-on-object-other-window (object)
274 ; "Display R-documentation for OBJECT using W3"
275 ; (interactive "s Help on :")
276 ; (let* ((ess-help-url (concat ess-help-w3-url-prefix
277 ; ess-help-w3-url-funs
280 ;;(w3-fetch-other-window ess-help-url)
284 ;;*;; Major mode definition
287 (defvar ess-help-sec-map nil
"Sub-keymap for ESS help mode.")
289 (defvar ess-help-mode-map nil
"Keymap for ESS help mode.")
290 (if ess-help-mode-map
292 (setq ess-help-mode-map
(make-keymap)); Full keymap, in order to
293 (suppress-keymap ess-help-mode-map
) ; suppress all usual "printing" characters
294 (define-key ess-help-mode-map
" " 'scroll-up
)
295 (define-key ess-help-mode-map
"b" 'scroll-down
)
296 (define-key ess-help-mode-map
"\177" 'scroll-down
) ; DEL
297 (define-key ess-help-mode-map
"q" 'ess-switch-to-end-of-ESS
)
298 (define-key ess-help-mode-map
"\C-m" 'next-line
)
299 ;; (define-key ess-help-mode-map "s" ess-help-sec-map)
300 (define-key ess-help-mode-map
"h" 'ess-display-help-on-object
)
301 ;; TODO: `electric mouse-2'
302 ;; (define-key ess-help-mode-map [mouse-2] 'ess-display-help-on-object)
303 (define-key ess-help-mode-map
"l" 'ess-eval-line-and-step
)
304 (define-key ess-help-mode-map
"r" 'ess-eval-region-and-go
)
305 (define-key ess-help-mode-map
"f" 'ess-eval-function-or-paragraph-and-step
)
306 (define-key ess-help-mode-map
"n" 'ess-skip-to-next-section
)
307 (define-key ess-help-mode-map
"p" 'ess-skip-to-previous-section
)
308 (define-key ess-help-mode-map
"/" 'isearch-forward
)
309 (define-key ess-help-mode-map
">" 'end-of-buffer
)
310 (define-key ess-help-mode-map
"<" 'beginning-of-buffer
)
311 (define-key ess-help-mode-map
"x" 'ess-kill-buffer-and-go
)
312 (define-key ess-help-mode-map
"k" 'kill-buffer
)
313 (define-key ess-help-mode-map
"?" 'ess-describe-help-mode
)
314 ;;-- those should be "inherited" from ess-mode-map ( ./ess-mode.el )
315 (define-key ess-help-mode-map
"\C-c\C-s" 'ess-switch-process
)
316 (define-key ess-help-mode-map
"\C-c\C-r" 'ess-eval-region
)
317 (define-key ess-help-mode-map
"\C-c\M-r" 'ess-eval-region-and-go
)
318 (define-key ess-help-mode-map
"\C-c\C-f" 'ess-eval-function
)
319 (define-key ess-help-mode-map
"\M-\C-x" 'ess-eval-function
)
320 (define-key ess-help-mode-map
"\C-c\M-f" 'ess-eval-function-and-go
)
321 (define-key ess-help-mode-map
"\C-c\C-j" 'ess-eval-line
)
322 (define-key ess-help-mode-map
"\C-c\M-j" 'ess-eval-line-and-go
)
323 (define-key ess-help-mode-map
"\M-\C-a" 'ess-beginning-of-function
)
324 (define-key ess-help-mode-map
"\M-\C-e" 'ess-end-of-function
)
325 (define-key ess-help-mode-map
"\C-c\C-y" 'ess-switch-to-ESS
)
326 (define-key ess-help-mode-map
"\C-c\C-z" 'ess-switch-to-end-of-ESS
)
327 (define-key ess-help-mode-map
"\C-c\C-l" 'ess-load-file
)
328 (define-key ess-help-mode-map
"\C-c\C-v" 'ess-display-help-on-object
)
329 (define-key ess-help-mode-map
"\C-c\C-k" 'ess-request-a-process
))
331 ;; One reason for the following menu is to <TEACH> the user about key strokes
332 (defvar ess-help-mode-menu
334 ["Search Forward" isearch-forward t
]
335 ["Next Section" ess-skip-to-next-section t
]
336 ["Previous Section" ess-skip-to-previous-section t
]
337 ["Help on Section Skipping" ess-describe-sec-map t
]
338 ["Beginning of Buffer" beginning-of-buffer t
]
339 ["End of Buffer" end-of-buffer t
]
341 ["Help on ..." ess-display-help-on-object t
]
343 ["Eval Line" ess-eval-line-and-step t
]
344 ["Eval Paragraph & step" ess-eval-paragraph-and-step t
]
345 ["Eval Region & Go" ess-eval-region-and-go t
]
346 ["Switch to ESS Process" ess-switch-to-ESS t
]
347 ["Switch to End of ESS Proc." ess-switch-to-end-of-ESS t
]
348 ["Switch _the_ Process" ess-switch-process t
]
350 ["Describe ESS-help Mode" ess-describe-help-mode t
]
352 ["Kill Buffer" kill-buffer t
]
353 ["Kill Buffer & Go" ess-kill-buffer-and-go t
]
355 "Menu used in ess-help mode.")
358 (defun ess-help-mode ()
359 ;;; Largely ripped from more-mode.el,
360 ;;; originally by Wolfgang Rupprecht wolfgang@mgm.mit.edu
361 "Mode for viewing ESS help files.
362 Use SPC and DEL to page back and forth through the file.
363 Use `n' and `p' to move to next and previous section,
364 `s' to jump to a particular section; `s ?' for help.
365 Use `q' to return to your ESS session; `x' to kill this buffer first.
366 The usual commands for evaluating ESS source are available.
367 Other keybindings are as follows:
368 \\{ess-help-mode-map}"
370 (setq major-mode
'ess-help-mode
)
371 (setq mode-name
"ESS Help")
372 (use-local-map ess-help-mode-map
)
373 (make-local-variable 'ess-local-process-name
)
375 ;;; Keep <tabs> out of the code.
376 (make-local-variable 'indent-tabs-mode
)
377 (setq indent-tabs-mode nil
)
380 (easy-menu-define ess-help-mode-menu-map ess-help-mode-map
381 "Menu keymap for ess-help mode." ess-help-mode-menu
)
382 (easy-menu-add ess-help-mode-menu-map ess-help-mode-map
)
384 ;; Add the keys for navigating among sections; this is done
385 ;; dynamically since different languages (e.g. S vs R) have different
388 (setq ess-help-sec-map
(make-sparse-keymap))
389 (mapcar '(lambda (key)
390 (define-key ess-help-sec-map
(char-to-string key
)
391 'ess-skip-to-help-section
))
392 (mapcar 'car ess-help-sec-keys-alist
))
393 (define-key ess-help-sec-map
"?" 'ess-describe-sec-map
)
394 (define-key ess-help-sec-map
">" 'end-of-buffer
)
395 (define-key ess-help-sec-map
"<" 'beginning-of-buffer
)
396 (define-key ess-help-mode-map
"s" ess-help-sec-map
)
398 (run-hooks 'ess-help-mode-hook
))
400 ;;*;; User commands defined in ESS help mode
402 (defun ess-skip-to-help-section nil
403 "Jump to a section heading of a help buffer. The section selected
404 is determined by the command letter used to invoke the command, as
405 indicated by `ess-help-sec-keys-alist'. Use \\[ess-describe-sec-map]
406 to see which keystrokes find which sections."
408 (let ((old-point (point))
409 (case-fold-search nil
))
410 (goto-char (point-min))
411 (let ((the-sec (cdr (assoc last-command-char
412 ess-help-sec-keys-alist
))))
413 (if (not the-sec
) (error "Invalid section key: %c"
415 (if (re-search-forward (concat "^" the-sec
) nil t
) nil
416 (message "No %s section in this help. Sorry." the-sec
)
417 (goto-char old-point
))))))
419 (defun ess-skip-to-next-section nil
420 "Jump to next section in ESS help buffer."
422 (let ((case-fold-search nil
))
423 (if (re-search-forward ess-help-sec-regex nil
'no-error
) nil
424 (message "No more sections."))))
426 (defun ess-skip-to-previous-section nil
427 "Jump to previous section in ESS help buffer."
429 (let ((case-fold-search nil
))
430 (if (re-search-backward ess-help-sec-regex nil
'no-error
) nil
431 (message "No previous section."))))
433 (defun ess-describe-help-mode nil
434 "Display help for `ess-mode'."
436 (describe-function 'ess-help-mode
))
438 (defun ess-kill-buffer-and-go nil
439 "Kill the current buffer and switch back to the ESS process."
441 (kill-buffer (current-buffer))
442 (ess-switch-to-ESS nil
))
444 (defun ess-describe-sec-map nil
445 "Display help for the `s' key."
447 (let ((keys-alist ess-help-sec-keys-alist
))
448 (describe-function 'ess-skip-to-help-section
)
450 (set-buffer "*Help*")
451 (toggle-read-only nil
)
452 (goto-char (point-max))
453 (insert "\n\nCurrently defined keys are:
456 --------- -------\n")
457 (mapcar '(lambda (cs) (insert " "
462 (insert "\nFull list of key definitions:\n"
463 (substitute-command-keys
464 "\\{ess-help-sec-map}")))))
466 (defun ess-read-helpobj-name-default (olist)
467 ;;; Returns the object name at point, or else the name of the
468 ;;; function call point is in if that has a help file. A name has a
469 ;;; help file if it is a member of olist.
470 (or (car (assoc (ess-read-object-name-default) olist
))
474 (narrow-to-region (max (point-min) (- (point) 1000))
478 (car (assoc (ess-read-object-name-default) olist
))))
481 (defun ess-find-help-file (p-string)
482 "Find help, prompting for P-STRING. Note that we can't search SAS,
483 Stata or XLispStat for additional information."
484 (ess-make-buffer-current)
487 (string-match "XLS" ess-language
)
488 (string-match "STA" ess-language
)
489 (string-match "SAS" ess-language
)))
490 (let* ((help-files-list (or (ess-get-help-files-list)
493 ess-current-process-name
))))
494 (default (ess-read-helpobj-name-default help-files-list
))
495 (prompt-string (if default
496 (format "%s(default %s) " p-string default
)
498 (spec (completing-read prompt-string help-files-list
)))
500 ((string= spec
"") default
)
502 (let* ((spec (read-string p-string
)))
506 ;;*;; Utility functions
508 (defun ess-get-help-files-list ()
509 "Return a list of files which have help available."
512 (mapcar '(lambda (dirname)
513 (if (file-directory-p dirname
)
514 (directory-files dirname
)))
515 (mapcar '(lambda (str) (concat str
"/.Help"))
516 (ess-search-list))))))
518 (defun ess-nuke-help-bs ()
519 "Remove ASCII underlining and overstriking performed by ^H codes."
520 ;; This function is a modification of nuke-nroff-bs in man.el from the
521 ;; standard emacs 18 lisp library.
522 ;; Nuke underlining and overstriking (only by the same letter)
523 (goto-char (point-min))
524 (while (search-forward "\b" nil t
)
525 (let* ((preceding (char-after (- (point) 2)))
526 (following (following-char)))
527 (cond ((= preceding following
)
535 (delete-region (1- (point)) (1+ (point)))))))
536 ;; Crunch blank lines
537 (goto-char (point-min))
538 (while (re-search-forward "\n\n\n\n*" nil t
)
539 (replace-match "\n\n"))
540 ;; Nuke blanks lines at start.
541 (goto-char (point-min))
542 (skip-chars-forward "\n")
543 (delete-region (point-min) (point)))
545 (defun ess-help-underline ()
546 "Replace _^H codes with underline face."
548 (goto-char (point-min))
549 (while (search-forward "_\b" nil t
)
550 (backward-delete-char 2)
551 (put-text-property (point) (1+ (point)) 'face
'underline
))))
555 (defun ess-goto-info (node)
556 "Display node NODE from ess-mode info."
560 (Info-goto-node (concat "(ess)" node
)))
564 (defun ess-submit-bug-report ()
565 "Submit a bug report on the ess-mode package."
569 (let ((reporter-prompt-for-summary-p 't
))
570 (reporter-submit-bug-report
571 "ess-bugs@stat.math.ethz.ch"
572 (concat "ess-mode " ess-version
)
575 'ess-ask-for-ess-directory
576 'ess-ask-about-transfile
579 'ess-source-directory
)
581 (lambda () (goto-char (point-max)) (insert-buffer "*ESS*")))))
588 \f ; Local variables section
590 ;;; This file is automatically placed in Outline minor mode.
591 ;;; The file is structured as follows:
594 ;;; Subsections: ;;;*;;;
595 ;;; Components: defuns, defvars, defconsts
596 ;;; Random code beginning with a ;;;;* comment
600 ;;; mode: outline-minor
601 ;;; outline-regexp: "\^L\\|\\`;\\|;;\\*\\|;;;\\*\\|(def[cvu]\\|(setq\\|;;;;\\*"
604 ;;; ess-help.el ends here