Exempt code chunks in an Sweave document from emacs spell check - r

In a Sweave document, code chunks in R are set off from the main text like so:
Catz are well known for their fur & pur.
<<echo = false>>=
catz <- 1 + 2
#
I'd like to run spell check for the LaTeX part (and flag "Catz") but have it skip the code chunks (not flag "catz"). In a long document, hitting "SPC" for each "misspelling" in the code section gets tedious.

Try adding this to your emacs init file:
(add-to-list 'ispell-skip-region-alist '("^<<.*>>=" . "^#"))
Edit (Re Michael Hoffman's comments):
If Flyspell is enabled, these two additional expressions will also be needed:
(defun flyspell-eligible ()
(let ((p (point)))
(save-excursion
(cond ((re-search-backward (ispell-begin-skip-region-regexp) nil t)
(ispell-skip-region (match-string-no-properties 0))
(< (point) p))
(t)))))
(put 'latex-mode 'flyspell-mode-predicate 'flyspell-eligible)
For other modes, replace the latex-mode in the last expression with the appropriate major mode names.

Related

How to change smart assign key ("_" to "<-") binding in ESS

In emacs ESS, how do I correctly change the keybinding for ess-smart-S-assign?
What I tried is adding
(custom-set-variables
'(ess-smart-S-assign-key ":"))
to my .emacs, but that made weird things happen: When I press :, just a normal : appears. On the other hand, pressing _ once yields <- as usual, whereas pressing _ a second time then converts this to :.
The desired behavior would be to be able to use _ as a normal key, with : being converted to <-.
I am using the official emacs 24.3 for windows and the latest development version of ESS (14.06).
Here's the docstring for ess-smart-S-assign-key:
Documentation:
Key used by `ess-smart-S-assign'. By default bound to
underscore, but can be set to any key. If this key is customized,
you must add
(ess-toggle-S-assign nil)
(ess-toggle-S-assign nil)
after the line that sets the customization and evaluate these
lines or reboot emacs. The first call clears the default
`ess-smart-S-assign' assignment and the second line re-assigns
it to the customized setting.
So: put this in your .emacs file to get the desired behavior:
(setq ess-smart-S-assign-key ":")
(ess-toggle-S-assign nil)
(ess-toggle-S-assign nil)
Kind of ugly, but it works.
The accepted answer didn't work for me, but the following did:
(global-set-key (kbd "C-;") (lambda () (interactive) (insert " <- ")))
(ess-toggle-underscore nil)
Insert your shortkey choice instead of C-;.
Another solution is
(eval-after-load "ess-mode" '(define-key ess-mode-map (kbd "C-;") "<-"))
(eval-after-load "ess-mode" '(define-key inferior-ess-mode-map (kbd "C-;") "<-"))
This allows to restrict the binding change to ess-mode. Note that the second line defines the binding for the inferior R process.

Have Emacs to automatically latex your Rnw file after knitting

I would like to start a LaTeX after knitting an .Rnw file. The meta code shoudl be something like:
(defun knit ()
(interactive)
(setq rproc (ess-get-process ess-current-process-name))
(setq c "knit('foo.rnw')\n")
(process-send-string rproc c)
;; Wait for ESS to finish
(shell-command-to-string "cd foo/path & pdflatex foo"))
The main problem is to have Emacs to wait for the inferior buffer to finish knitting and only after to start latexing. I found a couple of interesting functions: (ess-wait-for-process ...) and (inferior-ess-mark-as-busy ...)`, which could possibly work, but I am unable to understand how.
Note that (shell-command-to-string ... is only illustrative. The final choice might be:
(TeX-command "LaTeX" 'rnw-to-tex-ext -1))
knit2pdf() might be a different path to go, but I will lose the benefit of AUCTeX.
PS: My question is considered "subjective and likely to be closed" by the SE robot!?
I use texi2pdf and have R do it after the knit, but that's apparently the equivalent of knit2pdf. I wonder though if you could use system to call whatever tex command you want from within R, after the knit command, similar to how I use texi2pdf.
I'm not an emacs expert whatsoever but for what it's worth here's the function I have in my .emacs file.
; use knitr (was Sweave) script as compile function for Rnw files
(defun ess-swv-SweaveSh ()
"Use knitr script to knit an Rnw file and create a pdf."
(interactive)
(let
((compilation-buffer-name-function (function (lambda(ign)(concat "*" (buffer-file-name) "*")))))
(compile (concat "Rscript -e \".n <- '" (buffer-file-name) "'; library(knitr); knit(.n); library(tools); texi2pdf(sub('Rnw$','tex',.n))\"" ))
)
)
A possible solution is to check the 'busy property of the inferior ESS process.
The while loop waiting for it to be nil blocks command echo. This is why you see (redisplay). To avoid stressing your dear one, I set also a refresh delay (sleep-for .5).
Just in case ESS gets stuck the loop exists after 60 seconds. Adjust it, if you have heavy weight code.
Finally I attached latexing to AUCTeX. Therefore you can customise LaTeX-command-style with the help of TeX-expand-list documentation.
The function automatically sets the proper name for files to knit and latex, and sets R working dir to the .Rnw file to be knitted, so you can source other scripts or load data.
So you can run it everywhere with M-x knit or perhaps associate it with a shortcut.
The function saves the buffer with last changes before knitting and opens an inferior R buffer if there is none available; otherwise it uses the existing one.
(defun knit ()
"Save the buffer, knit and latex"
(interactive) ; You will associate this to you favourite key
(let* (
;; Get names & path
(cur-dir (file-name-directory (buffer-file-name)))
(rnw-name (file-name-nondirectory (buffer-file-name)))
(tex-name (concat (file-name-base (buffer-file-name)) ".tex"))
;; Create knit command
(cmd (format "require(knitr); setwd('%s'); knit('%s')" cur-dir rnw-name))
;; Time the knitting
(start-time (float-time))
(wait 60) ; Lifeboat to exit loop if smt wrong
)
;; Save rnw buffer... you are lazy, I know)
(save-buffer)
;; Send string to R at low-level
;;(setq rproc (ess-get-process ess-current-process-name))
;;(process-send-string rproc c)
;; or Send line with the ESS wrapper
(ess-eval-linewise cmd)
;; While loop to check when
(setq start-time (float-time)
wait 60) ; Lifeboat to exit loop after x secs
;; Wait for 'busy property nil, nut not more than wait seconds
(setq rproc (ess-get-process ess-current-process-name))
(while (< (- (float-time) start-time) wait)
(sleep-for .5)
;; (accept-process-output rproc .5) ;alt. way for process-send-string
(redisplay)
(if (not (process-get rproc 'busy))
(setq wait 0)
(message "Knitting... ")))
(message "Knitting finished, starting latexing")
;; Set LaTeX your fav options. See TeX-expand-list for % pars
(setq LaTeX-command-style '(("" "%(PDF)%(latex) -file-line-error %S%(PDFout)")))
;; TeX-command requires a 'file function (anonymous here) returning the filename.
;; TeX-command/TeX-expand-list say 'file has one opt arg: extension.
;; Actually they are 2, despite the second seems always passed true.
(TeX-command "LaTeX" (lambda (&optional ext dummy) tex-name))))

Add some shortcuts to ESS jags mode

I'm relatively new to emacs and bayesian data analysis and the JAGS software.
I would like to modify the ess jags-d.el file in order to have the F8 shortcut for <- ,as in the R-mode using ESS. IS it possible ?
I hope that my question it's clear, and sorry for my poor english.
Unfortunately ess-smart-underscore will not work in bugs mode.
Simple answer would be:
(define-key ess-bugs-mode-map [f8] (lambda() (interactive) (insert " <- ")))
this just binds f8 to insert <-.
To achieve exactly the same behavior in bugs as in ess put the following into your .emacs:
(define-key ess-bugs-mode-map (kbd "_") 'bugs-smart-underscore)
(defun bugs-smart-underscore ()
(interactive)
(let ((assign-len (length ess-S-assign)))
(if (and
(>= (point) (+ assign-len (point-min))) ;check that we can move back
(save-excursion
(backward-char assign-len)
(looking-at ess-S-assign)))
;; If we are currently looking at ess-S-assign, replace it with _
(progn
(delete-backward-char assign-len)
(insert "_"))
(delete-horizontal-space)
(insert ess-S-assign))))
Now, pressing _ once will get you <-, twice - _.
I don't use JAGS, but as far as I can see in ESS manual:
Changes/New Features in 5.4:
ESS[BUGS] and ESS[JAGS]: typing = now
results in <-.

Emacs, Auto Complete Mode, CSS, pain. (illustrated!)

I've got Auto Complete Mode installed for Emacs.
First: When I'm typing declarations I get the normal auto-complete behavior:
So I hit Tab to complete — no problem. But then I hit ;:
It instantly tries to complete something! And I can't hit Enter because that'll accept the erroneous completion!
So I have to hit C-j. What a pain.
Second: Once I'm done with a declaration, I type }:
...but it doesn't get indented properly unless I type Tab.
What gives?
Update, settings:
I'm using Emacs 23. My css-electric-keys are } and ;. My Auto Complete configuration is as follows:
(ac-config-default)
(setq ac-auto-start t)
(setq ac-delay 0.1)
(setq ac-auto-show-menu nil)
(setq ac-show-menu-immediately-on-auto-complete t)
(setq ac-trigger-key nil)
Here's a few suggestions:
(setq ac-auto-start t) starts autocomplete automatically. If you change that to (setq ac-auto-start 1) (or 2 or 3) then it will only start after that many characters have been typed. This might not solve your problem though if after you type the ;, it considers the entire preceding word as part of the current auto-complete search.
Maybe the problem is that it isn't recognizing the semicolon as a delimiting character (like whitespace), so it thinks you're still adding to the last word. Perhaps adding the semicolon string to ac-ignores would do the trick? (Not sure what the syntax for that would be)
Maybe you can prevent auto-completion via the enter key by adding: (define-key ac-complete-mode-map "\t" 'ac-complete) and (define-key ac-complete-mode-map "\r" nil). I'm not sure how this will interact with DWIM though (enabled by default).
Try adding semicolon as an auto-complete key?
My .emacs knowledge on a scale of 0 to 10 is like a 1.5, but maybe this will jog some better ideas.
Old stuff I know, but try the following:
(add-hook 'css-mode-hook
(lambda ()
(make-local-variable 'ac-ignores)
(add-to-list 'ac-ignores ";")))
From the manual

How to stop emacs from replacing underbar with <- in ess-mode

ess-mode is "Emacs speaks statistics." This mode is useful for editing programs for R or Splus (two separate statistics packages).
In my buffer, when ever I type _ the character is replaced with <-, which is very frustrating. Is there an emacs lisp statement to turn off this behavior?
emacs: 22.1.1
ess-mode release (unknown)
From ESS's manual (look under "Changes/New Features in 5.2.0"):
ESS[S]: Pressing underscore ("_") once inserts " <- " (as before); pressing underscore twice inserts a literal underscore. To stop this smart behaviour, add "(ess-toggle-underscore nil)" to your .emacs after ess-site has been loaded
Since the feature is useful. You can assign it to other key which is less used by you in R it will automatically unassign it from underscore. I personally assign it to ";" by adding following line in .emacs file.
(setq ess-smart-S-assign-key ";")
My version of emacs is 24.3 All-in-one installation file by Vincent Goulet.(Installed on windows 7)
hope this helps
Edit
In emacs 25.2 above do not work instead add following in the .emacs file
(setq ess-smart-S-assign-key ";")
(ess-toggle-S-assign nil)
(ess-toggle-S-assign nil)
A more recent version which seemed to work for me, and is a lot less verbose (you essentially keep normal underscores, but can set your own key for this smart behaviour!):
(global-set-key (kbd "C-;") (lambda () (interactive) (insert " <- ")))
(ess-toggle-underscore nil)
Insert your shortkey choice instead of C-;.
From http://www.r-bloggers.com/a-small-customization-of-ess/ and
How to change smart assign key ("_" to "<-") binding in ESS
To assign ":" to "<-" and to stop the assignment of underscore (underbar) "_" to "<-" put the following in .emacs (yes, the repeated line is correct)
(setq ess-smart-S-assign-key ":")
(ess-toggle-S-assign nil)
(ess-toggle-S-assign nil)
(ess-toggle-underscore nil) ; leave underscore key alone!
Like Michał Marczyk and this R mailing list thread suggested, add this line to ~/.emacs:
(ess-toggle-underscore nil)
Then reload it with M-x load-file and type ~/.emacs.
But if you load the file again, e.g. if you add another customization, then it toggles it back to the original state. So toggle it twice, the first one forcing it to the default:
(ess-toggle-underscore t)
(ess-toggle-underscore nil)
That being said, I like Drummermean's solution better, but it also reverts back to default if you add it to ~/.emacs and load it twice. So force a toggle to the default before:
(ess-toggle-underscore t)
(global-set-key (kbd "M--") (lambda () (interactive) (insert " <- ")))
(ess-toggle-underscore nil)
I bound the smart assignment to Opt-[minus] like RStudio (on a Mac).
As a follow-up on #mmorin answer. To set keybinding for the assignment operator the same way as in Rstudio add the following in your .emacs file
(ess-toggle-underscore t)
(ess-toggle-underscore nil)
(define-key ess-mode-map (kbd "M--") (lambda () (interactive) (just-one-space 1) (insert "<-") (just-one-space 1)))
(define-key inferior-ess-mode-map (kbd "M--") (lambda () (interactive) (just-one-space 1) (insert "<-") (just-one-space 1)))

Resources