Re: [Orgmode] [babel] feature request - tangle-and-evaluate-tangled-file

2010-07-13 Thread Dr Rainer M Krug
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

On 12/07/2010 19:51, Eric Schulte wrote:
> Rainer M Krug  writes:
> 
>> Hi Eric,
>>
>> On Fri, Jul 9, 2010 at 5:44 PM, Eric Schulte  wrote:
>>
>>> Hi Rainer,
>>>
>>> Rainer M Krug  writes:
>>>
 On Thu, Jul 8, 2010 at 9:03 PM, Eric Schulte 
>>> wrote:
>>> [...]
>
> (add-hook 'org-babel-post-tangle-hook
>  (lambda () (ess-load-file (buffer-file-name
>

 Unfortunately, this does not work. I put the code above into my
 emacs.organd nothing happens - not even an error message. I tried to
 use the C-h v to customise the variable, but I don't manage it.
>>>
>>> If the variable isn't registered with C-h v, then it sounds like maybe
>>> you need to update Org-mode, and/or re-run make clean && make.
>>>
>>
>> OK - that is working now.
>>
>>
>>>
 In addition, The filename for es-load-file shoud not be the buffer
 name, but the buffer name with the extension replaced with R.

>>>
>>> `buffer-file-name', returns the name of the file that the buffer is
>>> visiting, so wouldn't this already be the case if the filename ends with
>>> .R?
>>>
>>
>> Isn't the buffer from which I am tangling the org buffer, and not the result
>> from the tangling? (see below for further remarks.
>>
> 
> No this hook is run from /within/ a buffer visiting the file of tangled
> code, so in the example hook function above (buffer-file-name) will
> equal the name of the file of tangled R code.  From your later message
> it looks like the issues below have been resolved.

Thanks a lot for a very useful addition, and I will see abut the
buffer-name and come back if it is still the org-file buffer for me,

Rainer

> 
> Best -- Eric
> 
>>
>>
>>>
>>> This minimal example with the hook defined as above works for me.  After
>>> tangling I can jump to the *R* buffer where both x and y are defined.
>>>
>>> --8<---cut here---start->8---
>>> ** tangle R and load
>>>   :PROPERTIES:
>>>   :tangle:   to-load.r
>>>   :END:
>>>
>>> #+begin_src R :comments yes
>>>  x <- 8
>>> #+end_src
>>>
>>> #+begin_src R
>>>  y <- 9
>>> #+end_src
>>> --8<---cut here---end--->8---
>>>
>>
>> Not here -
>>
>> Emacs and org-mode version:
>>
>> GNU Emacs 23.2.1 (i686-pc-linux-gnu, GTK+ Version 2.20.1)
>> Org-mode version 6.36trans (release_6.36.603.g227b)
>>
>> 1) if no R process is running, I get the following message:
>> ess-start-process-specific: No ESS processes running; not yet implemented to
>> start (Initial,nil)
>> No problem 0 I can start an R process manually.
>>
>> 2) when an R session is running, I get the following error message from ESS:
>> Errors: Use C-c ` to find error.
>> Symbol's function definition is void: with-temp-filebuffer
>>
>> and from ESS:
>>
>> Error in source("/home/rkrug/tmp/R.org") :
>>   /home/rkrug/tmp/R.org:1:1: unexpected '^'
>> 1: **
>>
>> it seems to submit the org file (the buffer is saved as R.org) to R.
>>
>> So buffer-file-name should actually be replaced with the name from the file
>> name resulting from the tangling, in the easiest case (which would be fine
>> with me) the org buffer name with .org replaced with .R.
>> So I did the following (my firs elisp code!):
>>
>>   (add-hook 'org-babel-post-tangle-hook
>> (lambda () (ess-load-file (replace-regexp-in-string ".org" ".R"
>> buffer-file-name
>>
>> and it is working - except that I still get the Symbol's function definition
>> is void: with-temp-filebuffer , but it does not seem to do anything.
>>
>> Thanks a lot,
>>
>> Rainer
>>
>>
>>
>>
>>

 I am sorry about all these questions, but emacs-lisp I have never used
 emacs-lisp (I should - and I will!)

>>>
>>> No problem at all, but be careful about learning too much lisp as you
>>> may begin to grow dissatisfied with R :)
>>>
>>> Best -- Eric
>>>

 Cheers and thanks for your patience,

 Rainer


> Best -- Eric
>
> Rainer M Krug  writes:
>
>> Hi
>>
>> I am using org-babel for literate programming in R and I am using the
>> following approach to test the code:
>>
>> 1) tangle
>> 2) refresh buffer containing the tangled code (I use auto-revert-mode
>>> or
>> global-auto-revert-mode for that)
>> 3) load the tangled file into an existing R session via ESS R for
> evaluation
>>
>> This involves switching between buffers in always the same sequence.
>> Therefore my suggestion:
>>
>> would it be possible to have a tangle-and-evaluate-tangled-file
> funcction,
>> which is doing this automatically?
>>
>> C-c - 0 and org-babel-execute-buffer do not work in this case, as
> functions
>> are split over several code blocks in R.
>>
>> Cheers,
>>
>> Rainer
>
>>>

-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYF

[Orgmode] Re: [BABEL] tangling with leaving non-source code lines as empty lines?

2010-07-13 Thread Dr Rainer M Krug
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

On 13/07/2010 02:46, Eric Schulte wrote:
> Hi Rainer,
> 
> With the tangling comments generated by the latest Org-mode, the
> following function should be able to jump from any code in a tangled
> code file back to the relevant block in the original Org-mode file.

Wow - that was quick.

Thanks.

> 
> --8<---cut here---start->8---
> (defun org-babel-tangle-jump-to-org ()
>   "Jump from a tangled code file to the related Org-mode file."
>   (interactive)
>   (let ((mid (point))
>   target-buffer target-char
>   start end link path block-name)
> (save-window-excursion
>   (save-excursion
>   (unless (and (re-search-backward org-bracket-link-analytic-regexp nil t)
>(setq start (point))
>(setq link (match-string 0))
>(setq path (match-string 3))
>(setq block-name (match-string 5))
>(re-search-forward (concat " " (regexp-quote block-name)
>   " ends here[\n\r]") nil t)
>(setq end (point))
>(< start mid) (< mid end))
> (error "not in tangled code")))
>   (when (string-match "::" path)
>   (setq path (substring path 0 (match-beginning 0
>   (find-file path) (setq target-buffer (current-buffer))
>   (goto-char start) (org-open-link-from-string link)
>   (if (string-match "[^ \t\n\r]:\\([[:digit:]]+\\)" block-name)
> (org-babel-next-src-block
>  (string-to-int (match-string 1 block-name)))
>   (org-babel-goto-named-src-block block-name))
>   (setq target-char (point)))
> (pop-to-buffer target-buffer)
> (goto-char target-char)))
> --8<---cut here---end--->8---
> 

I tried it out, and realised that one needs to name the blocks
(#+srcname:) to be able to use it. Then it jumps to the code block in
the org file.

Two more things (As pointed out in the other email) would be nice:

1) it would be nice to jump to the line of code in the Org-mode file
which corresponds to the line in the code file

2) It would be brilliant, if one could call the function from the
Org-file buffer and enter the line number and then jump to the block /
line in the block.

In addition:
3) it would be very useful, if this function could be used with
non-named source blocks.

But that function is already quite usefull.

Thanks,

Rainer


> This is too large of a block of untested code to push into Org-mode now,
> however please give it a try, and let me know if you think this could be
> generally useful.  Hopefully after the feature-freeze we can fold
> something like this into Babel to ease navigation between Org files and
> their tangled offspring.
> 
> Cheers -- Eric
> 
> "Eric Schulte"  writes:
> 
>> Hi Rainer,
>>
>> If I'm understand you correctly you want the absolute position (by LOC)
>> of the lines of R code to be the same in both the original org file and
>> the tangled file or R code.  I don't think this is possible.  It is
>> possible to tangle code blocks in any order (not just the order in which
>> they appear in the original org file), so there are many valid tangling
>> scenarios in which this feature could not work.
>>
>> Rather, I think that it may make sense to have a function for jumping
>> back and forth between tangled code blocks and the related Org file.  In
>> that case the R errors could be followed to the tangled R code file,
>> from which you could easily jump to the relevant line in the relevant
>> code block in the Org file.  If you are tangling with comments, then it
>> should be possible to have a function called from a line in a tangled
>> code file which when called
>>
>> 1) finds it's enclosing comments
>> 2) remembers it's offset from the comments (which would then be it's
>>offset in the code block in the Org file)
>> 3) read the comment to learn which code block in which Org file it's
>>tangled from
>> 4) jump to the relevant line, in the relevant block, in the relevant
>>file
>>
>> Similarly when called form within a code block in an Org file the
>> function could
>>
>> 1) read it's header argument to find the relevant tangled code file
>> 2) jump to that file
>> 3) use the comments in that file to move to the appropriate section of
>>code and related line
>>
>> I'll think about such a function, and if it makes sense to implement it
>> apart from a more general "activate org-mode links in comments" minor
>> mode.  Any ideas or suggestions would be welcome!
>>
>> Thanks -- Eric
>>
>> Rainer M Krug  writes:
>>
>>> Hi Eric,
>>>
>>> would it be possible, when tangling, to leave lines without source code in
>>> the resulting code file as empty lines? The reasoning would be that error
>>> messages (at least in R) give the line in which the error occurred. If the
>>> line numbers in which the code sits would be preserve

[Orgmode] Re: [BABEL] tangling with leaving non-source code lines as empty lines?

2010-07-13 Thread Dr Rainer M Krug
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Hi Eric,

On Mon, Jul 12, 2010 at 8:04 PM, Eric Schulte wrote:

> Hi Rainer,
>
> If I'm understand you correctly you want the absolute position (by LOC)
> of the lines of R code to be the same in both the original org file and
> the tangled file or R code.


That's right - that's what I was thinking about.


> I don't think this is possible.  It is
> possible to tangle code blocks in any order (not just the order in which
> they appear in the original org file), so there are many valid tangling
> scenarios in which this feature could not work.
>

Accepted - under these circumstances it would only concern a special case
where the whole file is tangled in order to another R file. I agree, that
other solutions would be more useful - please see comments below.


> Rather, I think that it may make sense to have a function for jumping
> back and forth between tangled code blocks and the related Org file.  In
> that case the R errors could be followed to the tangled R code file,
> from which you could easily jump to the relevant line in the relevant
> code block in the Org file.


That sounds perfect to me.


> If you are tangling with comments, then it
> should be possible to have a function called from a line in a tangled
> code file which when called
>
> 1) finds it's enclosing comments
> 2) remembers it's offset from the comments (which would then be it's
>   offset in the code block in the Org file)
> 3) read the comment to learn which code block in which Org file it's
>   tangled from
> 4) jump to the relevant line, in the relevant block, in the relevant
>   file
>
>
That would be really be useful for literate programming and the following
debugging.


> Similarly when called form within a code block in an Org file the
> function could
>
> 1) read it's header argument to find the relevant tangled code file
> 2) jump to that file
> 3) use the comments in that file to move to the appropriate section of
>   code and related line
>

Also quite useful, although I don't see an immediate use for this, as the
code file is dynamic, and changing it would not make much sense (unless, and
now I am probably dreaming, one could sync the changes back into the .org
file into the blocks. In that way, one could debug the code file as one
would a normal code file and sync it, after it is working, back into the
.org file. Comments could be used to define the blocks. Although, I guess
that could cause problems with more advanced features on babel, which I am
not using (yet?).)

Another important aspect of a function lie that could be to be able to type
in the line number in the code file (from e.g. an error message) and to jump
to the corresponding line in the .org file. That would directly address my
suggestion above in a MUCH more elegant and efficient way.


> I'll think about such a function, and if it makes sense to implement it
> apart from a more general "activate org-mode links in comments" minor
> mode.  Any ideas or suggestions would be welcome!
>

I think that both functions would be extremely useful. The one outlined
above as a tool for debugging and the "links in org-mode comments" as kind
of backward linking documentation.

Let me know if you have anything you want me to test.

Cheers,

Rainer


>
> Thanks -- Eric
>
> Rainer M Krug  writes:
>
> > Hi Eric,
> >
> > would it be possible, when tangling, to leave lines without source code
> in
> > the resulting code file as empty lines? The reasoning would be that error
> > messages (at least in R) give the line in which the error occurred. If
> the
> > line numbers in which the code sits would be preserved (by leaving empty
> > lines where no code is in the .org file), debugging would be much easier.
> >
> > By the way: I am using the after tangle hook and it works absolutely
> > perfectly.
> >
> > Thanks and cheers,
> >
> > Rainer
>



- -- 
NEW GERMAN FAX NUMBER!!!

Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation Biology,
UCT), Dipl. Phys. (Germany)

Centre of Excellence for Invasion Biology
Natural Sciences Building
Office Suite 2039
Stellenbosch University
Main Campus, Merriman Avenue
Stellenbosch
South Africa

Cell:   +27 - (0)83 9479 042
Fax:+27 - (0)86 516 2782
Fax:+49 - (0)321 2125 2244
email:  rai...@krugs.de

Skype:  RMkrug
Google: r.m.k...@gmail.com

-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkw8GhMACgkQoYgNqgF2egqtdQCfY9TTM9MRWftZHEQhbA4zLqMe
qZ8AnjT2p3hRf/vr+lMTCbe9djOk7HAC
=i/Z5
-END PGP SIGNATURE-

___
Emacs-orgmode mailing list
Please use `Reply All' to send replies to the list.
Emacs-orgmode@gnu.org
http://lists.gnu.org/mailman/listinfo/emacs-orgmode