remacsess

emacs ess : how to auto complete library function


I am trying to migrate from RStudio to emacs ess and i am trying now to find auto-completion functionality in emacs ess similar to Rstudio

For my question i'll use a simple RStudio example and i'll appreciate if you could tell me how to do the same in emacs ess (The purpose is of course to generalize):

in RStudio

when i type the text libr and then press TAB I get library()

inside the parentesis

when i type "tidyv" like that: library(tidyv) and then TAB i get library(tidyverse)

Could you please describe in a simple step by step manner how i can do the same in emacs ess?

Thanks

Rafael


Solution

  • This should work for you with the default company-backend after a recent backend addition. You will just need to update your ESS from melpa. If company-R-library is not part of your company-backends in an ESS buffer, just add it in your ESS mode hook. Note that you should have set ess-use-company.

    In response to comment, ess-use-company is a variable you should set to t in your config, not a function to call with M-x. I'll assume you have an init file, if not you can easily find information about that. Wherever you keep your config for ess in your init file, you could add (although this is the default, so unless you set ess-use-auto-complete, this is probably already set)

    (setq ess-use-company t)
    

    Every mode has a hook, which is a function it runs after it is setup in a buffer, allowing users to customize the mode. You can add such a hook using (add-hook 'ess-mode-hook 'my-ess-mode-hook), where my-ess-mode-hook is a function you write with your customizations, eg. (company-mode) unless you have that turned on globally (most likely).

    company-backends is also a variable, from an ESS buffer you can type M-:company-backends to evaluate lisp code to see the value of that variable. Make sure you reinstall the latest version of ESS from melpa.

    Edit: full init to test -- company-R-library may not have been added to default backends

    (setq-default package-archives 
                  '(("melpa"        . "http://melpa.milkbox.net/packages/")
                    ("gnu"          . "http://elpa.gnu.org/packages/")))
    (setq package-enable-at-startup nil)
    (package-initialize)
    
    ;;; company
    (require 'company)
    (setq tab-always-indent 'complete)
    
    (setq company-idle-delay 0.5
          company-show-numbers t
          company-minimum-prefix-length 2
          company-tooltip-flip-when-above t)
    
    (global-set-key (kbd "C-M-/") #'company-complete)
    (global-company-mode)
    
    ;;; ESS
    (defun my-ess-hook ()
      ;; ensure company-R-library is in ESS backends
      (make-local-variable 'company-backends)
      (cl-delete-if (lambda (x) (and (eq (car-safe x) 'company-R-args))) company-backends)
      (push (list 'company-R-args 'company-R-objects 'company-R-library :separate)
            company-backends))
    
    (add-hook 'ess-mode-hook 'my-ess-hook)
    
    (with-eval-after-load 'ess
      (setq ess-use-company t))
    

    Start emacs with

    emacs -Q -l /path/to/this/init.el
    

    Open an R file, start the inferior R process, then type library(ti M-C-/ and you should get package completions.