emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [O] Automatically adding local variables to tangled file


From: Thorsten Jolitz
Subject: Re: [O] Automatically adding local variables to tangled file
Date: Wed, 05 Jun 2013 19:10:17 +0200
User-agent: Gnus/5.130002 (Ma Gnus v0.2) Emacs/24.3 (gnu/linux)

Rainer M Krug <address@hidden> writes:

Hi,

> I am trying to improve my workflow of literate programming of R in
> org. My org file is tangled into many R files and I am using ESS to
> debug.

[...]

> Now this is error prone, as one (or is it only me?) is easily tempted to
> edit the R file which is reverted after the next tangle.

are you aware that literate programming can be done without any tangling
at all now? I had similar issues like you when using literate
programming for organizing my .emacs, and finally figured out its
actually a conceptual problem:

,------------------------------------------------------------------------
| <--- Text --- Text with Code --- | --- Code with (Comment) Text --- Code --->
`------------------------------------------------------------------------

If you write a complex text/book with source code examples, literate
programming is just perfect (see e.g. Bernt Hansens Org-mode tutorial).
But if you write code with some comment text, it becomes burdensome.
Although working with interpreted dynamic languages, your workflow with
all that tangling starts to feel similar like working with compiled
languages (with the frequent tangling step replacing the frequent
compilation step).

So, looking at the line above, right from the '|' you really might want
to work in a source code buffer to enjoy all the advantages of Lisps (or
R's) dynamic. Its about programming with some text then, not vice versa.

You can do this with outshine.el and outorg.el (and optionally
navi-mode.el and poporg.el), see the tutorial on Worg
(http://orgmode.org/worg/org-tutorials/org-outside-org.html).

Short example:

Structure your R file like an Org-mode file:

,-----------------------------------------------------------------------------
| ## * my-sources.R --- my R Source file
| ##   :PROPERTIES:
| ##   :copyright: my_name
| ##   :copyright-years: 2013
| ##   :version:  0.9
| ##   :created:  21-01-2013
| ##   :licence:  GPL 2 or later (free software)
| ##   :licence-url: http://www.gnu.org/licenses/
| ##   :author:   my_name
| ##   :author_email: my_email AT gamil DOT com
| ##   :inspiration: foo bar
| ##   :keywords: foo bar
| ##   :END:
|
| ## ** Commentary
|
| ## Geometry Object Model from "OGC OpenGIS Simple Features Specification for
| ## SQL" Revision. 1.1 [...]
|
| ## ** Changes
|
| ## | author  | version | date            |
| ## |---------+---------+-----------------|
| ## | my_name |     0.9 | <2013-06-05 Mi> |
|
| ## * code
| ## ** My first R Function
|
| ## simple example function from the manual
| twosam <- function(y1, y2) {
|          n1  <- length(y1); n2  <- length(y2)
|          yb1 <- mean(y1);   yb2 <- mean(y2)
|          s1  <- var(y1);    s2  <- var(y2)
|          s <- ((n1-1)*s1 + (n2-1)*s2)/(n1+n2-2)
|          tst <- (yb1 - yb2)/sqrt(s*(1/n1 + 1/n2))
|          tst
|        }
|
| ## ** My second R Function
|
| ## another simple example function from the manual
| bslash <- function(X, y) {
|        X <- qr(X)
|        qr.coef(X, y)
|      }
|
| ## my-sources.R ends here
`-----------------------------------------------------------------------------

With outline-minor-mode and outshine.el activated, you will have all the
structure-editing and outline-navigation commands you are used to from
Org-mode. But you can program in this buffer directly without any
intermediate steps, and send your functions or whatever via ESS to the R
process-buffer.

The literate programming part comes from outorg.el then. With 

,------
| C-c ’
`------

on or inside any header in this file, the subtree at point is offered
for editing in a temporary Org buffer, with all source code enclosed in
source blocks and all comment text uncommented, e.g. 

,--------------------------------------------------
| ** My first R Function
| 
| simple example function from the manual
| #+begin_src ess
| twosam <- function(y1, y2) {
|          n1  <- length(y1); n2  <- length(y2)
|          yb1 <- mean(y1);   yb2 <- mean(y2)
|          s1  <- var(y1);    s2  <- var(y2)
|          s <- ((n1-1)*s1 + (n2-1)*s2)/(n1+n2-2)
|          tst <- (yb1 - yb2)/sqrt(s*(1/n1 + 1/n2))
|          tst
|        }
| #+end_src
`--------------------------------------------------

With 

,----------
| C-u C-c '
`----------

the whole buffer is converted to Org. 

You can then do your (comment) text editing will full Org-mode
functionality, and since the headlines are converted too, you can export
to any backend or use other Org functions that act on headlines. 

When you are done, just type 

,----
| M-#
`----

and the temporary Org-mode buffer is killed and all the changes are
applied to the original source code buffer. 

So you really have the look&feel of Org-mode in your source code
buffers, but without the annoying and error prone tangling. It works
for all kinds of major modes (emacs-lisp, picolisp, ess, and, at least in
theory, all others).

This way the line between Org-mode and source code buffers is blurring
and literate programming becomes more efficient and enjoyable. 

--
cheers,
Thorsten




reply via email to

[Prev in Thread] Current Thread [Next in Thread]