Customizing the ESS environment for R - r

I am trying to optimize my ESS - R environment. So far I make use of the r-autoyas, I set intendation and stuff following style guides, in the mini-buffer there are eldoc hints for function arguments, and I have the option to press a key in order to find information about variable at point (more here).
Are there any other things you use in order to have a nice R environment? Maybe non-ESS people have some nice things to add (I got that info of variable at point from looking at an Eclipser). One example could be an easy way to insert "just-before-defined" variables without typing the variable name (should be something for that?).
(Please help me to change the question instead of "closing" the thread if it is not well formulated)

I am not using autoyas as I find auto-complete integration a better approach.
Insertion of previously defined symbols is a general emacs functionality called 'dabbrev-expand' and is bound to M-/. I have this in my .emacs to make it complete on full symbols:
(setq dabbrev-abbrev-char-regexp "\\sw\\|\\s_\\|s.")
(setq dabbrev-case-fold-search t)
Another thing which I use extensively is imenu-based-jump-to-symbol-definition. It offers similar functionality to emacs tags, but just for open buffers in the same mode as the current buffer. It also uses IDO for queries:
Put imenu-anywhere.el into your emacs load path and add this:
(require 'imenu-anywhere)
(global-set-key [?\M-o] 'imenu-anywhere)
Now, if I do M-o foo RET emacs jumps to the function/class/method/generic definition of 'foo' as long as 'foo' is defined in one of the open buffers. This of course works whenever a mode defines imenu-tags. ESS defines those, so you should not need to add more.
There is also somewhere a collection of R-yas templates. I didn't get around to starting using them but my guess is that it's a pretty efficient template insertion mechanism.
[edit] Activate tracebug:
(setq ess-use-tracebug t)

Related

How to remove a defmethod for a struct

I have 2 destructs: monster & orc. The orc includes monster. The generic monster has generic defmethods on it named monster-show & monster-hit. The orc has a specialized monster-hit but still keeps the generic monster-show. My problem is that I accidentally named the specialized method for the orc the wrong name (monster-show), so now when I try to use the generic monster-show, it runs code that it shouldn't (the wrongly named defmethod I compiled) instead of running the generic method.
Is there a way to get rid of a specialized defmethod in Slime + SBCL?
If you don't have an IDE or such, you can use remove-method:
(remove-method #'monster-show
(find-method #'monster-show
()
(list (find-class 'orc))))
Here’s how I would do it using the slime inspector:
Enter the generic function you want to modify:
CL-USER> #'monster-show
#<GENERIC FUNCTION: MONSTER-SHOW>
Move your cursor on to it and inspect the object by typing C-c C-v TAB
The inspector should show a list of methods for the function identified by their specialisers. Navigate to one and press the button to remove/unbind the method. You can click for this too.
Also by the description of your hierarchy it would probably be wiser to use real classes and not structs. Structs tens not to give a particularly big speedup compared to classes.
On spacemacs w/ evil mode :slime-inspect RET #'monster-show. Select remove-method on the orc.

Strange autocompletion result in emacs + common lisp

I am using Emacs with SLIME for my development environment. When I type (write-to and then C-M-i I get the following autocompletions:
Click on a completion to select it.
In this buffer, type RET to select the completion near point.
Possible completions are:
write-to-sting
write-to-string
I know Common Lisp is powerful, but I guess write-to-sting is not in the ANSI standard. Google didn't offer a single hit for this function. Then I tried to find it in the SBCL code, but alas
(documentation 'write-to-sting 'function) returns nil so it doesn't have a documentation string.
When I try to execute the function (write-to-sting) I get The function COMMON-LISP-USER::WRITE-TO-STING is undefined.
Apropos also finds an unbound function:
(apropos 'write-to)
WRITE-TO
WRITE-TO-STING
WRITE-TO-STRING (fbound)
My question is: What is going on? Does anyone knows the story behind this function?
At some point during your interaction with the Lisp environment, you wrote write-to-sting and it was read by the Lisp reader. The symbol was interned in the COMMON-LISP-USER package. After all, maybe you intended to implement a function that sends an email to Sting, who knows?
Auto-completion works by filtering the currently known symbols in the environment.
You can safely (unintern 'write-to-sting) (or implement it).

Defining aliases to standard Common Lisp functions?

Lisp is said to enable redefinitions of its core functions.
I want to define an alias to the function cl:documentation function, such that
(doc 'write 'function) === (documentation 'write 'function)
How can this be done and made permanent in SBCL?
Creating an Alias
You are not trying to redefine (i.e., change the definition of) the system function documentation, you want to define your own function with a shorter name which would do the same thing as the system function.
This can be done using fdefinition:
(setf (fdefinition 'doc) #'documentation)
How to make your change "permanent" in common lisp
There is no standard way, different implementation may do it differently, but, generally speaking, there are two common ways.
Add code to an init file - for beginners and casual users
SBCL
CLISP
Clozure
ECL
The code in question will be evaluated anew every time lisp starts.
Pro:
Easy to modify (just edit file)
Takes little disk space
Normal lisp invocation captures the change
Con:
Evaluated every time you start lisp (so, slows start up time if the code is slow)
Save image - for heavy-weight professionals
SBCL
CLISP
Clozure
ECL - not supported
The modified lisp world is saved to disk.
Pro:
Start uptime is unaffected
Con:
Requires re-dumping the world on each change
Lisp image is usually a large file (>10MB)
Must specify the image at invocation time
Even though #sds has already answered pretty thoroughly I just wanted to add that the utility library serapeum has defalias
I use a simple macro for this:
(defmacro alias (to fn)
`(setf (fdefinition ',to) #',fn))
e.g.
(alias neg -) => #<Compiled-function ... >
(neg 10) => -10
Other answers include detail about how to make this permanent.

Emacs ESS indentation and auto-completion

There are a number of useful variables to control TAB key indentation and completion in Emacs (R) code chunks, when using ESS mode.
ess-tab-complete-in-script first indents lines and, if there is nothing to indent, autocompletes the word.
"With great power, comes great responsibility", so, when fast indenting your code, you might end up completing code without noticing with catastrophic results. Therefore you can refine things with the variable
ess-first-tab-never-complete. For example: if 'unless-eol, TAB completes only when cursor is at the end of the line; if 'symbol, it completes also in the middle of a line, but not if you are in the middle of a word; etc (read doc for more
with F1vess-first-tab-never-complete).
The problem is that, at least for me, TAB is bound to ess-noweb-indent-line, but the command governing indent-or-complete behaviour is: ess-indent-or-complete. So I use to fix the tab binding with:
(add-hook 'ess-mode-hook
'(lambda()
(local-set-key (kbd "<tab>") 'ess-indent-or-complete)
))
This works, but I wonder if this is the proper way. In the manual I don't see any prompt to hook to ESS mode and reset tab binding.
Do you know which is the canonical way to perform this?
There are a couple of reasonable ways to set the tab key (or any key, for that matter) for a specific mode. The first you alluded to in your answer, by setting the key locally via a mode hook. Note, though, that it's generally preferable to use a named function rather than a lambda so that you can remove the hook later if you want to do so:
(defun ess-keys-hook ()
"Put a bunch of keybindings in here."
(local-set-key [tab] 'ess-indent-or-complete))
(add-hook 'ess-mode-hook 'ess-keys-hook)
The other option is to define the key in the relevant mode map, which you can do like so:
(define-key ess-mode-map [tab] 'ess-indent-or-complete)
Both ways are pretty commonly used, although my own preference is for the latter, as it strikes me as cleaner and more efficient.

VIM: Bijection between VIM Taglist elements and code snippets?

My Taglist in a C code:
macro
|| MIN_LEN
|| MAX_ITERATIONS
||- typedef
|| cell
|| source_cell
||- variable
|| len_given
Taglist elements (domain):
A = {MIN_LEN, MAX_ITERATIONS, cell, source_cell, len_given}
Code snippets (codomain):
B = {"code_MIN_LEN", "code_MAX_ITERATIONS", ..., "code_len_given"}
Goal: to have bijection between the sets A and B.
Example: I want to remove any element in A, such as the MIN_LEN, from A and B by removing either its element in A or B.
Question: Is there a way to quarantee a bijection between A and B so that a change either in A or in B results in a change in the other set?
I strongly doubt you can do that. The taglist plugin uses ctags to collect the symbols in your code and display them in a lateral split. The lateral split contains readonly information (if you try to work on that window, vim tells you that modifiable is off for that buffer).
What you want to achieve would imply quite complex parsing of the source code you are modifying. Even a simple task like automatic renaming (assuming you modify a function name entry in the taglist buffer and all the instances in your source are updated) requires pretty complex parsing, which is beyond the ctags features or taglist itself. Deleting and keeping everything in sync with a bijective relationship is even more complex. Suppose you have a printf line where you use a macro you want to remove. What should happen to that line? should the whole line disappear, or just the macro (in that case, the line will probably be syntactically incorrect.
taglist is a nice plugin for browsing your code, but it's unsuited for automatic refactoring (which is what you want to achieve).
Edit: as for the computational complexity, well, the worst case scenario is that you have to scout the whole document at every keystroke, looking for new occurrence of labels that could be integrated, so in this sense you could say it's O(n) at each keystroke. This is of course overkill and the worst method to implement it. I am not aware of the computational complexity of the syntax highlight in vim, (which would be useful to extract tags as well, via proper tokenization), but I would estimate it very low, and very limited in the amount of parsed data (you are unlikely to have large constructs to parse to extract the token and understand its context). In any case, this is not how taglist works. Taglist runs ctags at every vim invocation, it does not parse the document live while you type. This is however done by Eclipse, XCode and KDevelop for example, which also provide tools for automatic or semiautomatic refactoring, and can eventually integrate vim as an editor. If you need these features, you are definitely using the wrong tool.

Resources