EMACS folding/unfolding R code - emacs

Is there an Emacs minor-mode (or piece of elisp code) that lets you selectively hide/show environments while in Sweave (Sweave = R + LaTeX)?
For instance, I would like to move to the beginning of a code block (<<>>), hit a keystroke, and have the contents of the environment hidden from view.
Is this possible? I just tried hs-minor-mode, allout-mode, and outline-minor-mode, but most of them don't recognize R environments.
I also tryed org-mode that works great for folding/unfolding but, do not support the LaTeX highlighting code for expression like: \cite{}; \ref{}; \ce{} ...
Best
Riccardo
--EDIT--
I tried for some day to use emacs-folding-mode but, because I work on a very long code (more than 2000 rows), folding mode "goes crazy" and for example misunderstand the mining of some special character (i.e. $), that has very different use both in R than LaTeX. I think the problem is intrinsic to Sweave, because in the same buffer I have R code and LaTeX code together.
So, now I'm testing emacs outline minor mode. But when I move through R from LaTeX (and vice versa) all the outlined part were unfolded despite I write in my .emacs:
(defun turn-on-outline-minor-mode ()
(outline-minor-mode 1))
(add-hook 'ess-mode-hook 'turn-on-outline-minor-mode)
(add-hook 'LaTeX-mode-hook 'turn-on-outline-minor-mode)
(add-hook 'latex-mode-hook 'turn-on-outline-minor-mode)
(setq outline-minor-mode-prefix "\C-c\C-o")
Do you have any suggestions??
Regards
--EDIT 2--
It seems to work:
(load "folding" 'nomessage 'noerror)
(folding-mode-add-find-file-hook)
(add-hook 'LaTeX-mode-hook 'folding-mode)
(add-hook 'ess-mode-hook 'folding-mode)
(folding-add-to-marks-list 'ess-mode "#{{{ " "#}}}" " ")
I don't know if is right that, when you leave the chunk, it is automatically unfolded.

There's a generic folding mode here: http://www.emacswiki.org/emacs/FoldingMode

I have had very good results with the hideshow hs-minor-mode, these are the lines I basically use in my ~/.emacs.d/init.el:
(add-hook 'ess-mode-hook 'hs-minor-mode)
(eval-after-load 'hideshow
'(progn
(global-set-key (kbd "C-+") 'hs-toggle-hiding)))

Related

Indent code in org-babel src blocks

In an org-mode file, with code like the following:
#+begin_src emacs-lisp
(add-to-list 'org-tab-before-tab-emulation-hook
(lambda ()
(when (within-the-body-of-a-begin-src-block)
(indent-for-tab-command--as-if-in-lisp-mode))))
#+end_src
I would like the TAB key to indent the code as it would if it were in a buffer in lisp mode.
What I need is:
A way to figure out whether the cursor is within a src block. It needs to not trigger when on the header line itself, as in that case the default org folding should take place.
A way to indent the code according to the mode (emacs-lisp in this case) specified in the header.
Org can already syntax highlight src blocks according to mode, and the TAB hooks are there. This looks do-able.
Since Emacs 24.1 you can now set the following option:
(setq org-src-tab-acts-natively t)
...and that should handle all src blocks.
Just move point into the code block and press C-c '
This will pop up a buffer in elisp-mode, syntax higlighting ad all...
Here's a rough solution:
(defun indent-org-src-block-line ()
"Indent the current line of emacs lisp code."
(interactive)
(let ((info (org-babel-get-src-block-info 'light)))
(when info
(let ((lang (nth 0 info)))
(when (string= lang "emacs-lisp")
(let ((indent-line-function 'lisp-indent-line))
(indent-for-tab-command)))))))
(add-to-list 'org-tab-before-tab-emulation-hook
'indent-org-src-block-line)
It only handles emacs-lisp blocks. I've only tested with the src block un-indented (not the org default).
It is tough in general to make one mode work inside another - many keyboard commands will conflict. But some of the more basic strokes, like tab for indent, newline, commenting (org will comment the lisp code with #, which is wrong) seem like they could be made to work and would have the largest impact.
(defun my/org-cleanup ()
(interactive)
(org-edit-special)
(indent-buffer)
(org-edit-src-exit))
should do it, where `indent-buffer' is defined as:
(defun indent-buffer ()
(interactive)
(indent-region (point-min) (point-max)))

Funky indentation in SML mode

I installed SML Mode in Emacs and the indentation is messed up. I disabled all my .emacs customizations, but that didn't make any difference. At the end of each line in the code below, I used C-j, which is mapped to newline-and-indent.
If I highlight everything and reindent (C-M-\), the result makes more sense:
I'm using Emacs 24.1.1 and SML Mode 6.2 on Ubuntu 12.10. What should I do?
Don't use newline-and-indent.
You can use reindent-then-new-and-indent or electric-indent-mode instead.
try in the .emacs file:
(setq default-tab-width 4);
(setq-default indent-tabs-mode nil);
(global-set-key (kbd "TAB") 'self-insert-command);
I'm no wizard but that worked for me.
http://www.pement.org/emacs_tabs.htm
Following up on Stefan's (now 4 year old...) suggestion, you can auto-enable the minor mode electric-indent-mode for sml-mode by putting this:
(push (lambda () electric-indent-local-mode 1) sml-mode-hook)
somewhere in your emacs initialization code.
Or, you might prefer:
(use-package sml-mode
:config
(push (lambda () electric-indent-local-mode 1) sml-mode-hook))
This seems to me to produce reasonably sane behavior for indenting code in sml-mode.

Emacs: Turn off indentation when doing a paragraph fill in LaTeX mode

When I am using emacs to edit a latex document the paragraph fill (Esc-q) does not do what I want. For example, something like:
The component \vn{%vec} is not similar to
When I use fill I get:
The component \vn{%vec} is not
% similar to
That is, emacs is taking "%" to be a comment character and filling the paragraph accordingly. However, "\vn" is a macro of mine that sets text in texttt mode and here "%" is simply a regular printable character so the paragraph fill has done things incorrectly.
So what I want is for paragraph fill to be the same it is as in text-mode. That is, no indentation and no adding extra characters. But I don't want to have to toggle between text-mode and latex-mode every time I want to paragraph fill. Is there any way to do this?
Thanks for the help. -- David
PS: Yes, I do know that if there are real comments at the end of lines then the test-mode fill will not do things correctly. But I never put comments at the end of lines so this will never bother me.
I found the solution. I put this in my init.el file:
(add-hook 'latex-mode-hook '(lambda() (setq comment-start nil)))
(add-hook 'tex-mode-hook '(lambda() (setq comment-start nil)))
(add-hook 'latex-mode-hook '(lambda() (setq fill-indent-according-to-mode nil)))
(add-hook 'tex-mode-hook '(lambda() (setq fill-indent-according-to-mode nil)))
I love emacs but the documentation (or lack thereof) can sometimes drive me crazy... :).
You can try:
(setq comment-start nil)
to handle this specific case.
What you're trying to fix is a symptom of the real problem -- latex-mode is
naively marking code after "%" as comment.
Does installing auctex-mode fix your problem?

How to highlight code parts that are longer than 80 chars?

In Emacs, I'd like to highlight the parts of long lines that exceed 80 characters.
The package highlight-80+ is great for that. But how can I automatically enable it when a C++ source file is loaded?
I tried to add highlight-80+ to the C++ mode, but it didn't work:
(require 'highlight-80+)
(defun my-c++-mode-common-hook ()
(highlight-80+-mode 1))
(add-hook 'c++-mode-common-hook 'my-c++-mode-common-hook)
When I load a .cc file it goes in C++ mode but highlight-80+ is not enabled, so the long lines are not marked.
Note that the Highlight80Plus wiki says that it is built-in to emacs starting with 23. I believe it's referring to whitespace-mode; it does this and is built in to emacs.
There is a function in emacs-starter-kit that does something like this already but you could easily duplicate it,
(defun esk-turn-on-whitespace ()
(whitespace-mode t))
(add-hook 'prog-mode-hook 'esk-turn-on-whitespace)
See the whitespace-mode, it does this kind of highlighting and more:
http://www.emacswiki.org/emacs/WhiteSpace
Can you try this:
(autoload 'highlight-80+)
(add-to-list 'auto-mode-alist '("\\.cpp$" . highlight-80+-mode))

Auto-complete mode doesn't turn on automatically in ObjC buffers

I load auto-complete mode like this:
(let ((ac-path "path/to/auto-complete"))
(add-to-list 'load-path ac-path)
(require 'auto-complete-config)
(add-to-list 'ac-dictionary-directories (concat ac-path "ac-dict"))
(ac-config-default))
It works fine with C major mode, but doesn't turn on automatically when I open ObjC files. I can still turn it on manually and it will work fine along with the ObjC major mode.
Here's a snippet from the docs regarding ObjC major mode:
The hook `c-mode-common-hook' is run with no args
at mode initialization, then `objc-mode-hook'.
If I understand correctly, auto-complete adds a hook to the c-mode-common-hook, but objc-mode-hook somehow overrides it. Is there a way to fix this?
Thanks.
While looking through the source code of auto-complete.el, I've stumbled upon this definition
(defcustom ac-modes
'(emacs-lisp-mode
lisp-interaction-mode
c-mode cc-mode c++-mode
java-mode clojure-mode scala-mode
scheme-mode
ocaml-mode tuareg-mode
perl-mode cperl-mode python-mode ruby-mode
ecmascript-mode javascript-mode js-mode js2-mode php-mode css-mode
makefile-mode sh-mode fortran-mode f90-mode ada-mode
xml-mode sgml-mode)
"Major modes `auto-complete-mode' can run on."
:type '(repeat symbol)
:group 'auto-complete)
It turns out that auto-complete doesn't have a true global mode. It is enabled only with those major modes that are included in the ac-modes variable.
So, adding the following line to the .emacs file has solved the issue for me.
; add this line after the auto-complete mode has been loaded
(add-to-list 'ac-modes 'objc-mode)
Use the following:
(defun my-objc-mode-hook ()
(auto-complete-mode 1))
(add-hook 'objc-mode-hook 'my-objc-mode-hook)
Note 1: The function auto-complete-mode is a toggle function, when called with no arguments.
Note 2: It's possible to add an anonymous function using lambda, but this have several drawbacks. The most important ones are: modifying the function and reevaluating the expression will add the modified function in addition to the earlier version and C-h v xxx will print the full unformatted lambda function, which typically is hopeless to read and understand.
(add-hook 'objc-mode-hook 'auto-complete-mode)
That should do it if you're using auto-complete-mode. You can add more complex things to mode hooks by doing:
(add-hook 'objc-mode-hook '(lambda ()
(something-with arguments)))
Note that both arguments to add-hook are quoted, this is necessary and if you add unquoted functions they will probably not work.