[ESS] elisp code: show function arguments

Patrick Drechsler patrick at pdrechsler.de
Tue Apr 3 19:26:18 CEST 2007


Patrick Drechsler <patrick at pdrechsler.de> writes:

> Sven Hartenstein <lists at svenhartenstein.de> writes:
>
>> I wrote an emacs lisp function that shows a R-function's arguments and
>> their default values on a single keystroke or as you type the opening
>> paranthesis ("(") after a function name. People like me who can't
>> remember all the parameter's names can thus very easily see them.
>
> Very useful -- thanks for sharing this Sven!

Since some people are having some problems with this, Sven asked me to
post my setup (see below). 

Also, here is what I do to use Sven's function:

1. Open Emacs with an R file and an R session (ie `emacs junk.R -f R')
2. Switch to the R script (junk.R)
3. Type something like

    lm(

   and you'll see the tooltip.


,----[ version infos ]
| R      : 2.4.1
| Emacs  : GNU Emacs 23.0.0.1 (i686-pc-linux-gnu, GTK+ Version 2.8.20)
|  of 2007-03-18 on trurl
| Package: ess-mode 5.3.3
| 
| current state:
| ==============
| (setq
|  ess-language "S"
|  ess-dialect "R"
|  ess-ask-for-ess-directory nil
|  ess-ask-about-transfile nil
|  ess-directory nil
|  ess-keep-dump-files "always"
|  ess-source-directory "/tmp/"
|  )
`----

--8<---------------cut here---------------start------------->8---
;;  ============================================================
;; ESS =========================================================
;;  ============================================================
(cond
 ((file-directory-p "C:/Programme")
  ; do Windows stuff:
  (setq-default inferior-R-program-name "C:/Programme/R/R-2.4.1/bin/Rterm")
  )
 ((file-directory-p "/home/patrick/")
  ; do GNU/Linux stuff
  )
)

(load "~/.emacs.d/mysitelisp/ess/ess-5.3.3/lisp/ess-site")
;; jump to bottom line of output:
(setq comint-scroll-to-bottom-on-input t)
(setq comint-scroll-to-bottom-on-output t)
(setq comint-move-point-for-output t)

;; use rdired (invoke with `M-x ess-rdired'; usage ie p:print, v:view):
(autoload 'ess-rdired "ess-rdired"
  "View *R* objects in a dired-like buffer." t)

;; map `ess-eval-chunk-and-go' to keybinding `C-c M-c':
(define-key ess-mode-map "\C-c\M-c" 'ess-eval-chunk-and-go)

;; ;; SMALL MACROS: ===============================================

;; ...

;;* Nice extra functions from Sven Hartenstein
;;* Message-ID: <20070402213225.GA1682 at schlepptop.schlepptopnetwork>
;;* Archived-At: <http://permalink.gmane.org/gmane.emacs.ess.general/2241>
;;* Code posted at: http://www.svenhartenstein.de/emacs-ess.php
(defun my-r-show-args ()
  "Show arguments and their default values of function in minibuffer."
  (interactive "*")
  (let ((pointposition (point)))
    (up-list -1)
    (let ((posend (point)))
	(backward-sexp 1)
	(setq object (buffer-substring-no-properties posend (point)))
	(ess-command (concat "try(args(" object "), silent=TRUE)\n")
		     (get-buffer-create "*my-r-args-completion*"))
	)
    (goto-char pointposition)
    )
  (with-current-buffer "*my-r-args-completion*"
    (goto-char (point-min))
    (if (equal nil (search-forward "function" 10 t))
	  (message my-r-noargsmsg)
	(goto-char (point-min))
	(zap-to-char 1 (string-to-char "("))
	(goto-char (point-max))
	(zap-to-char -1 (string-to-char ")"))
	(delete-trailing-whitespace)
	(if (equal my-r-show-as "tooltip")
	    (tooltip-show (concat "ARGS: " (buffer-string)))
	  (message (concat "ARGS: " (buffer-string)))
	  )))
  (kill-buffer "*my-r-args-completion*")
  )
(defvar my-r-noargsmsg ""
  "The message that is returned if my-r-show-args does not find a list
of arguments.")
(defvar my-r-show-as nil
  "How my-r-show-args should show the argument list. Possible values
are: 'message' (the default) or 'tooltip'.")

;;
(setq my-r-noargsmsg "") ; no message when no arguments found
;; (setq my-r-show-as "tooltip") ; show arguments as tooltip

;; bind my-r-show-args to F11
(define-key ess-mode-map [f11] 'my-r-show-args)

;; call my-r-show-args automatically
(define-key ess-mode-map "(" '(lambda nil "" (interactive)
				  (skeleton-pair-insert-maybe nil)
				  (my-r-show-args)))

;; Put mouse away when using keyboard
(mouse-avoidance-mode 'banish)

;; Set the tooltip position in absolute pixels from the upper left
;; corner of the screen
(setq tooltip-frame-parameters
      '((name . "tooltip")
	(left . 20)
	(top . 20)))



(cond
 ((file-directory-p "C:/Programme")
  ;; do Windows stuff:
  )
 ((file-directory-p "/home/patrick/")
  ;;; 2007-03-14 PD: removed definitions for Sweave. They are now included directly in ESS (ess-swv.el)
  ;;; 2007-03-14 PD: changed acroread to xpdf
  (defun ess-makePDF ()
    "Create a PDF file and display it with acroread."
    (interactive)
    (let* ((namestem (substring (buffer-name) 0 (search ".Rnw" (buffer-name))))
	   (tex-filename (concat namestem ".tex")))
      (shell-command (concat "pdflatex " tex-filename))
      (shell-command (concat "xpdf " namestem ".pdf &"))))
  )
)
--8<---------------cut here---------------end--------------->8---

HTH

Patrick
-- 
Ein Intellektueller ist jemand, der sich auf einem Gebiet gut
auskennt, aber nur auf anderen das Wort ergreift.  
Aus T. Wolfe "Im Land der Rokkoko-Marxisten"




More information about the ESS-help mailing list