3

I define a function ess-rmarkdown to compile RMarkdown documents with polymode.

(use-package polymode
  :ensure t
  :config
  (use-package poly-R)
  (use-package poly-markdown)
  ;;; MARKDOWN
  (add-to-list 'auto-mode-alist '("\\.md\\'" . poly-markdown-mode))
  ;;; R modes
  (add-to-list 'auto-mode-alist '("\\.Snw\\'" . poly-noweb+r-mode))
  (add-to-list 'auto-mode-alist '("\\.Rnw\\'" . poly-noweb+r-mode))
  (add-to-list 'auto-mode-alist '("\\.Rmd\\'" . poly-markdown+r-mode))
  (markdown-toggle-math t)
  ;; from https://gist.github.com/benmarwick/ee0f400b14af87a57e4a
  ;; compile rmarkdown to HTML or PDF with M-n s
  ;; use YAML in Rmd doc to specify the usual options
  ;; which can be seen at http://rmarkdown.rstudio.com/
  ;; thanks http://roughtheory.com/posts/ess-rmarkdown.html
  (defun ess-rmarkdown ()
    "Compile R markdown (.Rmd). Should work for any output type."
    (interactive)
                                        ; Check if attached R-session
    (condition-case nil
        (ess-get-process)
      (error
       (ess-switch-process)))
    (let* ((rmd-buf (current-buffer)))
      (save-excursion
        (let* ((sprocess (ess-get-process ess-current-process-name))
               (sbuffer (process-buffer sprocess))
               (buf-coding (symbol-name buffer-file-coding-system))
               (R-cmd
                (format "library(rmarkdown); rmarkdown::render(\"%s\")"
                        buffer-file-name)))
          (message "Running rmarkdown on %s" buffer-file-name)
          (ess-execute R-cmd 'buffer nil nil)
          (switch-to-buffer rmd-buf)
          (ess-show-buffer (buffer-name sbuffer) nil)))))
  )

To bind ess-rmarkdown function to F5 key I use:

(with-eval-after-load 'polymode
  (define-key polymode-mode-map (kbd "<f5>") 'ess-rmarkdown))

Unfortunately, after reloading my init.el file, the following warning appears:

Symbol's value as variable is void: polymode-mode-map

I kindly as for any advice.

EDIT: Entering emacs with --debug-init returns me:

Debugger entered--Lisp error: (void-variable polymode-mode-map)
  (define-key polymode-mode-map (kbd "<f5>") (quote ess-rmarkdown))
  (lambda nil (define-key polymode-mode-map (kbd "<f5>") (quote ess-rmarkdown)))()
  eval-after-load(polymode (lambda nil (define-key polymode-mode-map (kbd "<f5>") (quote ess-rmarkdown))))
  eval-buffer(#<buffer  *load*> nil "/home/andrej/.emacs.d/init.el" nil t)  ; Reading at buffer position 4255
  load-with-code-conversion("/home/andrej/.emacs.d/init.el" "/home/andrej/.emacs.d/init.el" t t)
  load("/home/andrej/.emacs.d/init" t t)
  #[0 "\205\266
Andrej
  • 145
  • 5
  • 2
    Possible duplicate of [Defining key-bindings within \`helm-map\`: getting a \`void-variable helm-map\` error](https://emacs.stackexchange.com/questions/2539/defining-key-bindings-within-helm-map-getting-a-void-variable-helm-map-erro) – Drew Sep 23 '18 at 18:04
  • Please enable debugging with `M-x toggle-debug-on-error RET` and reproduce the error (or start Emacs with `--debug-init`). The backtrace should help pinpoint the source of the problem. – Stefan Sep 23 '18 at 21:55
  • @Stefan I added `debug-init`, unfortunately I'm not able to decipher it. – Andrej Sep 24 '18 at 02:53
  • this isn't a duplicate - the `with-eval-after-load` form ensures the `define-key` acts after the variable is defined (which is the problem in the duplicate). The problem is that the variable is incorrectly defined in the package, which is a bug. – Tyler Sep 26 '18 at 15:51

1 Answers1

4

The error is simple: the polymode package does not define any polymode-mode-map (nor polymode-mode for that matter). Instead it defines polymode-minor-mode and corresponding polymode-minor-mode-map.

Stefan
  • 26,154
  • 3
  • 46
  • 84
  • 1
    polymode does define `polymode-mode-map`. However, it defines it with `(defalias 'polymode-mode-map 'plolymode-minor-mode-map)`, which is incorrect, since `defalias` sets the function definition, not the variable definition. – Tyler Sep 26 '18 at 15:43
  • 1
    I submitted a bug report for this https://github.com/polymode/polymode/issues/175 – Tyler Sep 26 '18 at 15:50