2

How does one run the arbitrary command to execute the tangled code? I use Emacs both on Windows and Linux, but currently I need to run the generated batch file in particular Windows environment.
Here is how the block looks like:

#+begin_src bat :exports both :tangle "run_tests.bat"
...
#+end_src

I have a batch file which sets the environment (d:\Programs\Microsoft Visual Studio 12.0\VC\vcvarsall.bat), but cannot run my tangled file inside this new environment. How to do this?

The code can be tangled using org-babel-tangle of course, but I cannot run the generated batch file from Emacs using org-ctrl-c-ctrl-c as it says:

org-babel-execute-src-block: No org-babel-execute function for bat!

Any help would be appreciated.

1 Answer 1

3

Tangling creates the batch file, execution of source block tries to execute the source block in the specified language (and there is no ob-bat I know of).

You can execute the tangled batch file in a shell source block. Therefore add the following to your org file:

#+BEGIN_SRC sh :exports results :results output
cmd /c ""d:\Programs\Microsoft Visual Studio 12.0\VC\vcvarsall.bat" & run_tests.bat"
#+END_SRC

This first runs the batch and then, separated by &, your batch script.

Note that I have a MinGW environment with a bash shell. If you want to run windows cmd.exe directly you may find a hint here, which introduces a shcmd header option.

Also, you need to require ob-sh or ob-shell, depending on your org version. Therefore I do:

(unless (require 'ob-sh nil 'noerror)
  (require 'ob-shell))

Btw, if all you want is to execute some command sequence you can simply do:

#+BEGIN_SRC sh :exports results :results output
call "d:\Programs\Microsoft Visual Studio 12.0\VC\vcvarsall.bat"
echo "insert commands here"
#+END_SRC

EDIT:

Since I may need a windows native execution facility in org-mode I will write an ob-bat.el as soon as I am back from holiday (next week). Meanwhile you may try this code snippet (since I have no windows box it is untested):

(require 'ob)
(require 'ob-shell)

(defvar org-babel-tangle-lang-exts)
(add-to-list 'org-babel-tangle-lang-exts '("bat" . "bat"))

(defcustom org-babel-bat-command "cmd.exe"
  "Name of command to use for executing bat code.")

(defun org-babel-execute:bat (body params)
  "Execute a block of bat code with cmd.exe.
This function is called by `org-babel-execute-src-block'."
  (let ((shell-file-name org-babel-bat-command))
    (org-babel-execute:shell body params)))

(provide 'ob-bat)

With this you should be able to execute bat source blocks:

#+BEGIN_SRC bat :exports results :results output
echo hello
#+END_SRC
2
  • Thank you very much! Unfortunately the last block of code does not work. It is strange. In *messages* I see the following: executing Sh code block... Wrote c:/Users/alk/AppData/Local/Temp/babel-956097_/ob-input-9560CdR Babel evaluation exited with code 1 Code block produced no output. And then in *Org-Babel Error Output* there is this following: 'sh' is not recognized as an internal or external command, operable program or batch file. I can only imagine for you it works because you use MinGW.
    – avp
    Aug 26, 2016 at 13:41
  • Unfortunately currently I do not have a windows box around, so I can not verify that sh os used from MinGW. I suppose the command may as well come from GnuWin32 CoreUtils... Btw this also give the opurtinity to install grep and DiffUtils, which I highly recommend for increased usefulness of emacs on windows.
    – theldoria
    Aug 26, 2016 at 16:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.