Add some shortcuts to ESS jags mode - r

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 <-.

Related

Capture output of cl-async:spawn

I was hoping to experiment with cl-async to run a series of external programs with a large combinations of command line arguments. However, I can't figure out how to read the stdout of the processes launched with as:spawn.
I would typically use uiop which makes it easy to capture the process output:
(let ((p (uiop:launch-program ... :output :stream)))
(do-something-else-until-p-is-done)
(format t "~a~%" (read-line (uiop:process-info-output p))))
I've tried both :output :pipe and :output :stream options to as:spawn and executing (as:process-output process-object) in my exit-callback shows the appropriate pipe or async-stream objects but I can't figure out how to read from them.
Can anyone with experience with this library tell how to accomplish this?
So you go to your repl and type:
CL-USER> (documentation 'as:spawn 'function)
And you read whatever comes out (or put your point on the symbol and hit C-c C-d f). If you read it you’ll see that the format for the :input, etc arguments is either :pipe, (:pipe args...), :stream, or (:stream args...) (or some other options). And that :stream behaves similarly to :pipe but gives output of a different type and that for details of args one should look at PIPE-CONNECT so you go and look up the documentation for that. Well it tells you what the options are but it isn’t very useful. What’s the documentation/description of PIPE or STREAM? Well it turns out that pipe is a class and a subclass of STREAMISH. What about PROCESS that’s a class too and it has slots (and accessors) for things like PROCESS-OUTPUT. So what is a good plan for how to figure out what to do next? Here’s a suggestion:
Spawn a long running process (like cat foo.txt -) with :output :stream :input :pipe say
Inspect the result (C-c C-v TAB)
Hopefully it’s an instance of PROCESS. What is it’s output? Inspect that
Hopefully the output is a Gray stream (ASYNC-STREAM). Get it into your repl and see what happens if you try to read from it?
And what about the input? See what type that has and what you can do with it
The above is all speculation. I’ve not tried running any of this but you should. Alternatively go look at the source code for the library. It’s already on your computer and if you can’t find it it’s on GitHub. There are only about half a dozen source files and they’re all small. Just read them and see what you can learn. Or go to the symbol you want to know about and hit M-. to jump straight to its definition. Then read the code. Then see if you can figure out what to do.
I found the answer in the test suite. The output stream can only be processed asynchronously via a read call-back. The following is simple example for posterity
(as:start-event-loop
(lambda ()
(let ((bytes (make-array 0 :element-type '(unsigned-byte 8))))
(as:spawn "./test.sh" '()
:exit-cb (lambda (proc exit-status term-signal)
(declare (ignore proc exit-status term-signal))
(format t "proc output:~%~a"
(babel:octets-to-string bytes)))
:output (list :stream
:read-cb (lambda (pipe stream)
(declare (ignore pipe))
(let ((buf (make-array 128 :element-type '(unsigned-byte 8))))
(loop for n = (read-sequence buf stream)
while (plusp n) do
(setf bytes
(concatenate '(vector (unsigned-byte 8))
bytes
(subseq buf 0 n)))))))))))
with
$ cat test.sh
#!/bin/bash
sleep_time=$((1+$RANDOM%10))
echo "Process $$ will sleep for $sleep_time"
sleep $sleep_time
echo "Process $$ exiting"
yields the expected output

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.

ESS (R) Auto-complete

I try to get a practical development environment for R in Emacs, hoping to get auto-completion working as shown in http://www.emacswiki.org/emacs/ESSAuto-complete.
However, even in a minimal configuration, I can't get it working.
See what I get on http://screencast.com/t/qcyVwkECX. In fact, while AC does work (see completion menu appearing), it's like if there was no info from the R language itself, while ac-source-R is WELL added to ac-sources.
Do you understand what's happening?
Best regards.
PS- Here is my minimal Emacs configuration file for the demo:
;; Auto Completion
(add-to-list 'load-path "~/.emacs.d/elpa/auto-complete-20140824.1658/")
(add-to-list 'load-path "~/.emacs.d/elpa/popup-20140815.629/")
(when (require 'auto-complete-config)
(ac-config-default)
;; use `C-n/C-p' to select candidates
(setq ac-use-menu-map t)
(define-key ac-menu-map (kbd "C-n") 'ac-next)
(define-key ac-menu-map (kbd "C-p") 'ac-previous)
;; unbind some keys (inconvenient in iESS buffers)
(define-key ac-completing-map (kbd "M-n") nil)
(define-key ac-completing-map (kbd "M-p") nil)
;; set default sources
(setq ac-sources
(append '(ac-source-features
ac-source-functions
ac-source-yasnippet
ac-source-variables
ac-source-symbols)
ac-sources))
(setq ac-delay 0) ; faster than default 0.1
(setq ac-auto-show-menu 0.2)
(setq ac-quick-help-delay 0.5)
(setq ac-quick-help-height 10)
(setq ac-candidate-limit 100)
;; completion by TAB
(define-key ac-completing-map
(kbd "<tab>") 'ac-complete)
;; avoid Flyspell processes when auto completion is being started
(ac-flyspell-workaround))
;; ESS: Emacs Speaks Statistics
(add-to-list 'load-path "~/.emacs.d/elpa/ess-20140824.1452/lisp/")
(setq shell-file-name "zsh.exe")
(add-to-list 'auto-mode-alist '("\\.[rR]\\'" . R-mode))
(autoload 'R "ess-site" "Call 'R', the 'GNU S' system from the R Foundation." t)
(autoload 'R-mode "ess-site" "Major mode for editing R source." t)
(setq ess-ask-for-ess-directory nil)
(setq inferior-ess-same-window nil)
(setq ess-default-style 'DEFAULT)
(with-eval-after-load "ess-site"
;; use eldoc to report R function names
(require 'ess-eldoc)
(add-hook 'inferior-ess-mode-hook 'ess-use-eldoc))
PPS- In fact, ElDoc does not seem to work either!
UPDATE
I just discovered it almost works when the iESS buffer gets created, not when just editing R code in its own buffer. See http://screencast.com/t/fKRjLmIC6K0.
What would explain that iESS must be run first before it finally works?
Still, something that does not work is the completion on function arguments (like with the cat function on the page http://www.emacswiki.org/emacs/ESSAuto-complete).
Why does that not work?
After opening a R file with emacs, if you have this mode described into your emacs status bar:
(ESS[S] [none] ElDoc AS)
You can run this shortcut C-c C-s to attach a R session to you ESS[S] editor mode.
If you already have one or some R session open, emacs will ask you to choose the R session you want to use. Otherwise if you have not already open a R session then emacs will open a new one for you.
Next, you should have this information inside your status bar:
(ESS[S] [R db -] ElDoc AS)
and the completion should work.

How to make the current prompt of R at the top of buffer in ESS just like Control + L in R console

I have tried to put the following in my .emacs file.
(defun clear-shell ()
(interactive)
(let ((old-max comint-buffer-maximum-size))
(setq comint-buffer-maximum-size 0)
(comint-truncate-buffer)
(setq comint-buffer-maximum-size old-max)))
(global-set-key (kbd "\C-x c") 'clear-shell)
It worked, but it also remove all the command I have typed before. So it is not what I want. I just want the current prompt > at the top of the buffer and not to delete any command I typed before.
Does anyone know?
For me Esc-0 Ctr-l seems to work.
`Ctrl-h k' output is:
C-l runs the command recenter-top-bottom,
which is an interactive compiled Lisp function in window.el'.
According to this page from the Emacs manual:
Scroll the selected window so the current line is the
center-most text line; on subsequent consecutive invocations,
make the current line the top line, the bottom line, and so on in
cyclic order. Possibly redisplay the screen too (recenter-top-bottom).
This seems to get the job done (though I am actually not really sure if this is what you are after):
(defun clean-shell ()
(interactive)
; if you call this from your .r script, it will switch to the next window
(when (eq major-mode 'ess-mode) (other-window 1))
(mark-whole-buffer)
(exchange-point-and-mark)
(move-beginning-of-line 1)
(delete-region (region-beginning) (region-end))
(end-of-line)
)
EDIT: Or maybe this?
(defun clean-shell ()
(interactive)
(when (eq major-mode 'ess-mode) (other-window 1))
(mark-whole-buffer)
(exchange-point-and-mark)
(move-beginning-of-line 0)
(delete-region (region-beginning) (region-end))
(end-of-line)
)
What is wrong with C-l C-l? It works in any buffer.

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