Notes about open source software, computers, other stuff.

Tag: Emacs (Page 2 of 2)

Configuring R

I took some time today to configure my R experience. I’m mostly using R from Emacs using ESS (Emacs Speaks Statistics), which means I had to configure some settings there as well.

Previously, my settings only consisted of setting a customised directory in which to install my packages and an alias to start R without asking for saving the histroy when quitting. This I did by setting the following environment variable in my .bashrc and/or .zshrc, as well as an alias:

# Set the library path for R
export R_LIBS_USER=${R_LIBS_USER}:~/Programmeren/R/lib
 
if [ -n "$(/usr/bin/which R 2>/dev/null)" ]; then
alias R="$(/usr/bin/which R) --no-save"
fi

However, Emacs didn’t pick up either of these variables, so high time to fix that. This meant creating two files with the following content:

~/.Rprofile:

# Set the default CRAN repository used by install.packages()
options("repos" = c(CRAN = "http://cran-mirror.cs.uu.nl/"))

~/.Renviron:

R_LIBS="~/Programmeren/R/lib"

I added the following to my .emacs file to start R with the --no-save option:

(setq inferior-R-args "--no-save ")

Additionally I have the following in there to turn on a spelling checker and have line-wraps enabled:

(add-hook 'ess-mode-hook
          (lambda ()
            ;; Set pdflatex as the default command for Sweave (default: texi2pdf)
            (setq ess-swv-pdflatex-commands (quote ("pdflatex"
                                                    "texi2pdf"
                                                    "make")))
            (auto-fill-mode t)
            (flyspell-prog-mode)
            ))

Related Images:

Using BibTeX from org-mode

I use Emacsorg-mode a lot for writing notes, todo lists, presentations and writing short reports. Recently I started writing a larger report which I normally would have done in LaTeX. This time, since the notes related to the project were already in org format, I decided to write the whole report in org-mode. The one thing I needed for that was using BibTeX bibliographies (and RefTeX) from org-mode. A quick web search revealed that that can easily be done by adding the following to your .emacs file:

;; Configure RefTeX for use with org-mode. At the end of your
;; org-mode file you need to insert your style and bib file:
;; \bibliographystyle{plain}
;; \bibliography{ProbePosition}
;; See http://www.mfasold.net/blog/2009/02/using-emacs-org-mode-to-draft-papers/
(defun org-mode-reftex-setup ()
  (load-library "reftex")
  (and (buffer-file-name)
       (file-exists-p (buffer-file-name))
       (reftex-parse-all))
  (define-key org-mode-map (kbd "C-c )") 'reftex-citation)
  )
(add-hook 'org-mode-hook 'org-mode-reftex-setup)

After that, RefTeX works, but exporting the org document to PDF (via LaTeX) didn’t include the bibliography entries. A quick look at the error log showed that bibtex hadn’t been run, so the question was: how to tell org-mode to do that too when exporting. The answer is to tell org-mode to use the latexmk Perl script (on Debian/Ubuntu it is easily installed from the package repositories) when exporting to PDF. I added the following lines to my .emacs file:

;; Use latexmk for PDF export
(setq org-latex-to-pdf-process (list "latexmk -pdf -bibtex %f"))

Related Images:

Using CUA selection mode in Emacs to edit rectangles

Today Planet Emacsen brought me Irreal’s second blog post in a short time on CUA mode in Emacs. So far I’ve always ignored it because as far as I knew CUA mode is about getting the Windows keyboard shortcuts of Ctrl-c, Ctrl-x and Ctrl-v for copying and pasting to Emacs. The thing is, I date back to the DOS era when Shift-Del and Shift-Ins were used for that, so back in my Windows days I never got used to those ‘new’ keyboard shortcut. Now that I’ve been an Emacs user for more than a decade I’m so used to C-w and C-y and I see no reason for having the Windows shortcuts work in Emacs.

Back to Irreal. In his recent blog posts he writes about a subset of cua-mode: cua-selection-mode. The video by Mark Mansour that we writes about says it all (it’s short, so go and watch it!). What cua-selection-mode is all about is rectangle editting. So far I’ve been using the regular Emacs keys for rectangle selection and editing (basically C-space to select a rectangle and C-r-k to cut it, C-r-t to insert text and C-r-y to paste a rectangle). By setting

(cua-selection-mode 1)

in your ~/.emacs file you only enable the rectangle features of CUA mode.

So, for those that didn’t watch the video, what does the rectangle editing mean? It means that you can for example simply insert a list of increasing numbers in a text (this may come in handy in an org-mode table for example), or you can insert the same text in front of and/or behind a selected column of text.

Key combos to remember are:

  • C-return: Start selection
  • return: move the cursor to top-left, top-right, bottom-left and bottom-right corner of the selected rectangle
  • C-?: briefly list the available key combinations (with rectangle selection enabled)
  • M-i: if the selection is a column of numbers increase the numbers (by one)
  • M-n: Insert a number in the column (asks for start value and increment value)
  • C-1 C-w: Kill (cut) the contents of the rectangle to register 1 (you can use number 0–9 for different registers). Using C-1 C-y yanks (pastes) the rectangle at the cursor position.

Related Images:

Tabs and spaces in Emacs

Recently I added the following lines to my ~/.emacs file:

;; Don't insert tabs when indenting regions
(setq-default indent-tabs-mode nil)

The idea behind disabling the indent-tabs-mode was that (especially) while programming I want any tabs to be converted to spaces. Since different people have different settings for a tab width this seemed like a good choice.
However, once I opened a Makefile I ran into trouble. In a Makefile tabs are a requirement, not an option. Since all my tabs were converted to spaces the moment I saved a Makefile compiling became a nightmare. To solve this problem I added the following to my ~/.emacs file, after the aforementioned statement:

(add-hook 'makefile-mode-hook
          (lambda ()
            indent-tabs-mode t))

This enables tabs again for modes that involve Makefiles.

Related Images:

Subversion: merging updates in trunk to a branch

Yesterday I was working on ProbABEL, an open source package for running GWAS (genome-wide association studies). We use R-Forge and they provide us with a Subversion (SVN) server for revision control.

Some time ago we created a branch in which one of the co-developers is doing some major refactoring of the code. In the mean time I have been fixing bugs and adding new features to trunk. Now that the work in the refactoring branch comes to an end I thought it was high time to integrate the changes in trunk with the changes in the branch so that we can later promote the branch to trunk.

Since I had never done this before I decided to try the merge in the doc directory first, because I knew that in that directory nothing had changes since the branch was created, so all changes from trunk should be imported. At first I followed the SVN book instructions so I went into the doc dir in the branch and ran

$ svn merge ^/pkg/ProbABEL/doc

Unfortunately that didn’t work out. For some reasons conflicts appeared as wel as files that weren’t supposed to be there at all.

Thanks to Google and this blog post I found a solution. It boils down to explicitly telling SVN which revisions to use for the merge.

First I used

$ svn log | grep -C3 branch

to find out at which revision I created the branch:

------------------------------------------------------------------------
r864 | lckarssen | 2012-03-27 17:38:05 +0200 (Tue, 27 Mar 2012) | 1 line

Creating ProbABEL branch for code refactoring

Next I went to trunk and ran

$ svn update
At revision 987.

to find out at which revision trunk currently was. Back in the doc directory in the brach I ran

svn merge -r 864:987 ^/pkg/ProbABEL/doc

to merge al the changes since the branch was split off and it worked like a charm! All changes in trunk applied cleanly.

I then id the same for the other directories which also had changes in the branch. It turns out that when SVN find a conflict it is easier to postpone resolving the conflict because Emacs has a great SVN merge minor mode called SMerge! It highlights your changes vs. the incoming ones and allows you to select a resolution and move to the next conflict with a few easy keystrokes. After all conflicts have been resolved Emacs automtically removes the intermediate files SVN created and you are ready to commit.

Related Images:

RefTeX, how could I have missed this?

I’ve been using Emacs and LaTeX for more than ten years now and still I keep learning. For example, last Friday I came across Stephen Eglen’s ESS (Emacs Speaks Statistics) tutorial. In his slides he mentioned RefTeX. I had heard of it before, but for some reason I thought it was either a set of LaTeX styles or an AUCTeX replacement and consequently, I never looked into it.

This time, however, I made the small effort of looking up RefTeX’s website and lo and behold, it’s neither of the two things I thought it was. Instead, it is an extension to LaTeX that makes inserting citation, references, index entries and labels a breeze. Even if you don’t (want to) use all its capabilities, the following keystrokes are worth the effort:

C-c = Show table of contents of the document (also a great way to walk through your document)

C-c ( Insert a label at point
C-c ) Insert a reference to some label
C-c [ Search in your bibliography (either BibTeX or thebibliography environment) to insert citations

To start using RefTeX in a document you are already editing simple run

M-x reftex-mode

Insert the following in .emacs file if you want to load RefTeX on startup

;; Enable RefTeX
(add-hook 'LaTeX-mode-hook 'turn-on-reftex)   ; with AUCTeX LaTeX mode
(add-hook 'latex-mode-hook 'turn-on-reftex)   ; with Emacs latex mode

Related Images:

Overwriting selected text in Emacs

I learned something new in Emacs (my favourite text editor) today. Normally in Emacs, if you start typing while having some text selected will not overwrite this text. This is contrary to what most people are used to in other editors. It turns out that Emacs has a minor mode that fixes this: delete-selection-mode. Add the following to your .emacs file to have it loaded by default:

(delete-selection-mode t)

Since delete-selection-mode enables transient-mark-mode I commented the line

(setq transient-mark-mode t)

in my .emacs

References:

Related Images:

Enable incremental-search-forward in Bash

I just read Ruslan Spivak’s blog posting on how to get Ctrl-s (which is bound to incremental-search-forward in Emacs) working to search incrementally through the command history in Bash.

Normally this behaviour is shadowed by a terminal flow-control key binding. To turn that off and ‘reveal’ the search-forward function, simply type

stty -ixon

(of course, by adding this line to your ~/.bashrc file makes it permanent).

Great to get this working! Thanks Ruslan.

Related Images:

Newer posts »

© 2024 Lennart's weblog

Theme by Anders NorĂ©nUp ↑