how to override the save-file command in org-babel - emacs

I really like the org-babel that enables me to organize my scripts in org-mode, however, i found there are some issues with the org src buffer when i edit the source code in a separate buffer (using keybinding C-c ').
first thing is, even i explicitly run write-file, and then specifies the file path and name to save, the buffer is not saved to that file, but the source code block in the .org file gets updated and the .org -file is saved.
second thing is, whenever i run save-buffer in org src buffer, the buffer screen will automatically scroll down till current mouse position is the last line in the buffer. this is annoying because sometimes i lose tracking my scripts.
i am not so familiar with elisp, and can only do simple work like define-key or add-hook, i hope i can get help from here. thanks

Org-babel is not meant for organizing scripts, but for including source code as part of your document.
You may be interested in tangling, which allows to join and extract source code blocks from your orgmode document into separate files.
The following example will merge 2 source code fragments into the file test.m when tangling (org-babel-tangle, bound to C-c C-v t):
* Tangling example
Set up a vector:
#+begin_src octave :tangle test.m
a = 1:10;
#+end_src
Then find out squares
#+begin_src octave :tangle test.m
b = a.^2
#+end_src

Related

Change emacs org-mode key binding for code block

In org-mode I use <s then TAB to insert a code block. This action will insert a code block like
#+BEGIN_SRC
.
.
.
#+END_SRC
but I want to modify this action to insert something like
#+BEGIN_SRC python -n :results output pp replace :exports both
.
.
.
#+END_SRC
I know it's possible to change default behaviour of :result or :exports in emacs init file but I prefer to change this shortcut behaviour, because it makes me able to change the options in line.
Answering my own question based on legoscia's answer.
As mentioned in the Easy Templates section of the org-mode manual, you can modify these templates by customizing the variable org-structure-template-alist. Using M-x customize-option and applying changes will add all easy templates to your init file, if you don't like it you can add just one line to your init file to change a template or add one.
In my case I added this line to my emacs init file to add <p then TAB :
(add-to-list 'org-structure-template-alist '("p" "#+BEGIN_SRC python -n :results output pp replace :exports both\n?\n#+END_SRC"))
ALL CREDITS GOES TO legoscia
As of Org 9.2 the method employed by #shae128 is no longer valid. Instead you'll need to use tempo-define-template, like this:
(tempo-define-template "python-block"
'("#+begin_src python :results raw output"
n n p n n
"#+end_src")
"<p"
"Insert an outputting Python block"
'org-tempo-tags)
n stands for newline, p for where to leave the mark, <p for the command to expand from when hitting tab.
Thanks to Omar's answer here
As mentioned in the Easy Templates section of the org-mode manual, you can modify these templates by customizing the variable org-structure-template-alist. (Use M-x customize-option.)
For <s, the default expansion is "#+BEGIN_SRC ?\n\n#+END_SRC". You can just edit it to include the options you want after BEGIN_SRC. Alternatively, you could add a new template, e.g. <p, that expands to the text you want.

Evaluate all code chunks below current header in emacs org mode

I use org mode for reproducible research and literate programming, mainly using R for computations. I'm new to emacs.
If I'm starting a new R session or my session is interrupted, I will have to rerun a number of code chunks. This is very easy with the speed commands (typing "e" to evaluate the chunk, then "n" to move to the next). I love this feature, the only problem is that my headers and subtrees are then expanded and I have to close them again to keep the page tidy.
Is there an existing feature or lisp expression to evaluate all code chunks below a header? And is it possible to do this without expanding the content below?
many thanks
For example, if the cursor were at the start of header 1, could you evaluate the code chunks under header 2 and 3 with a single command?
* header 1
** header 2
#+BEGIN_SRC R
print("hello")
#+END_SRC
** header 3
#+BEGIN_SRC R
print("world")
#+END_SRC
It sounds like you want org-babel-execute-buffer or org-babel-execute-subtree (C-c C-v s).

How to copy/select a whole file or buffer in Emacs?

Is there any command to select the whole file contents in Emacs?
For example, Control+a selects whole contents of a file in Notepad, Notepad++, etc.
I can select whole contents using the mouse, but it's inconvenient for large files. I found the basic Emacs commands here and here, but could not find what I am looking for.
C-x h will select the entire buffer.
You can search for help within Emacs using the built-in help system.
C-h f will look for help for specific functions. In this case, you could have searched for whole-buffer to find mark-whole-buffer.
Other help commands of interest would be:
C-h m to show available commands for the current modes
C-h v for help related to variables
C-h k to find which functions keys are bound to
C-h w to see which key bindings are defined for a given function
C-h ? to see which other options are available.
Note that C-x h will only highlight all the text. To actually copy the selected text, you must use C-w for cut (kill-region) or M-w for copy (kill-ring-save).
I use CUA, so I didn't like mark-whole-buffer because it doesn't use a temporary region.
After much messing about, I finally achieved this using a keyboard macro saved to a function:
Define a macro which selects the whole buffer
Run kmacro-name-last-macro to name the macro
Use insert-kbd-macro to script it out
Now you can copy it into your config & map it to a key, like C-a
Copying and selecting are related, but distinct, actions. A file and a buffer are also related, but distinct.
To copy a buffer, consider using jac.el. It handles both the "copying" and dealing with modes.

How do I get org-mode to execute code blocks consistently

I'm running R code blocks in a session in an org-mode file, and I can usually get them to execute by C-c C-c, as expected.
I think the pertinent PROPERTY lines I've put at the top of my file are
#+PROPERTY: session *R*
#+PROPERTY: cache yes
#+PROPERTY: exports both
#+PROPERTY: tangle yes
Sometimes, nothing happens when I press C-c C-c. AFAICT, that's a seemingly random event; the same code block may work one time and fail another time.
When such a block fails, I do see the results echoed to the minibuffer, but I don't see any results (e.g., a new data frame, as I would have expected in many cases) in the R session.
If I press C-' to edit the code block and then press C-j on each line (or C-r on each region), the code does execute, and the results get echoed appropriately to the org file and show up in the R session.
Here are some sample code block begin lines:
#+begin_src R :results silent :exports code
#+begin_src R :results value :colnames yes :exports both
I tested both just now by doing rm(list=ls()), running the code blocks, and doing ls() in the session to see if the results were there.
The first block is simple:
#+begin_src R :results silent :exports code
require(stringr)
require(ggplot2)
require(scales)
require(arm)
require(YaleToolkit)
require(stinepack)
require(mixtools)
require(lubridate)
source("utilities.R")
pf <- function (x,y) {
z <- sqrt(x * x + y * y)
return(x/z)
}
#+end_src
In two tests, it worked once and failed once, as detected by looking for pf in the R session.
If important, I can try to produce an ECM that fails frequently enough to be useful, but I suspect this may be a common setup or version problem that someone has seen and solved. It seems related to Org-mode code block evaluation, but the solution there seems to have been to :export results. In most cases, I use :exports both, which would seem to cover that case, and, in at least some cases, I care about setting up the environment in the session and not exporting to the org file. I'm never passing data through org-mode; I'm only using the session for that.
BTW, I'm running org-mode 7.8.03 on GNU Emacs 23.3.1 on XP Pro 64.

What is the correct path to the C source files for Emacs?

So, way back in January, I went here:
http://emacsformacosx.com/
I downloaded Emacs and have been using it on my Mac and I like it. I've started trying to get into Elisp programming. To learn more, I'd like to look up some functions. So for instance I do:
C-h f
and then type "scroll-down"
This gives me the following text:
>scroll-down is an interactive built-in function in `window.c'.
>
>It is bound to <kp-prior>, <prior>, C-1, C-x C-1, M-v.
>
>(scroll-down &optional ARG)
>
>Scroll text of selected window down ARG lines.
>If ARG is omitted or nil, scroll down by a near full screen.
>A near full screen is `next-screen-context-lines' less than a full screen.
>Negative ARG means scroll upward.
>If ARG is the atom `-', scroll upward by nearly full screen.
>When calling from a program, supply as argument a number, nil, or `-'.
And the text "window.c" is a link. So I click on the link and I get:
find-function-C-source: The C source file window.c is not available
I'm getting this error a lot while doing a lot of different things. Where do I find the right path, and how do I tell Emacs what that path is?
I did just recently install some ELPA packages, so maybe one of them is causing some chaos?
The variable source-directory will point to the location where the C sources are. If you have a separately downloaded copy, you'll have to point this variable to that directory.
Most packagers don't include the sources, or split them off into a separate package. Install the sources (and maybe tweak an init script to tell Emacs where you put them, if it's not the default location. The pertinent variable is find-function-C-source-directory).
If you didn't manually build Emacs from the source code and patch the C source code, value of source-directory or find-function-C-source-directory would be wrong.
You can manually download Emacs source code, unpack it somewhere and set above two variables accordingly like following
(setq source-directory "/path/to/your-emacs-repo")
;; OR
(setq find-function-C-source-directory "/path/to/your-emacs-repo/src")
GNU Emacs source code and development is hosted on savannah.gnu.org. You can find all the tags here and download the one that matches your M-x emacs-version.