Org-Mode: Image get corrupted in result - emacs

I am using Emacs Org-Mode and I am trying to fetch a picture from the web using wget and display it as an inline image in a code block result. So, I write the following block of code:
#+BEGIN_SRC bash :results file :file ~/image.jpg
url='https://gist.githubusercontent.com/brettlangdon/85942af486eb79118467/raw/2a7409cd3c26a90b2e82bdc40dc7db18b92b3517/06b3FMA.jpg'
wget "$url" -O ~/image.jpg
#+END_SRC
#+RESULTS:
[[file:~/image.jpg]]
However, the image is not display and get corrupted. In console I get:
Premature end of JPEG file

#+BEGIN_SRC bash
url='https://gist.githubusercontent.com/brettlangdon/85942af486eb79118467/raw/2a7409cd3c26a90b2e82bdc40dc7db18b92b3517/06b3FMA.jpg'
wget "$url" -O ~/image.jpg
#+END_SRC
Executes script, downloading the specified URL to ~/image.jpg
#+BEGIN_SRC bash :results file :file ~/image.jpg
url='https://gist.githubusercontent.com/brettlangdon/85942af486eb79118467/raw/2a7409cd3c26a90b2e82bdc40dc7db18b92b3517/06b3FMA.jpg'
wget "$url" -O ~/image.jpg
#+END_SRC
Executes script, downloading the specified URL to ~/image.jpg, and then writes the standard output from the shell command to ~/image.jpg (which I expect leaves you with an empty file, as wget writes information to stderr).
So you can either use the first approach and include your image link independently of the code block; or if you want the downloaded image data to be captured in the :results you need to tell wget to write to stdout.
#+BEGIN_SRC bash :results file :file ~/image.jpg
url='https://gist.githubusercontent.com/brettlangdon/85942af486eb79118467/raw/2a7409cd3c26a90b2e82bdc40dc7db18b92b3517/06b3FMA.jpg'
wget "$url" -O -
#+END_SRC

Related

`org-export-to-html` does not export ANSI colors

The Issue
after running org-export-to-html on the following code:
#+BEGIN_SRC shell :exports both :results output
echo -e "\e[0;31mOUTPUT\e[0m"
#+END_SRC
OUTPUT is exported in black color
Desired output
OUTPUT being exported in red color
Proposed solution
The following code exports the output to html using (aha)
#+BEGIN_SRC shell :exports both :results output html
echo -e "\e[0;31mOUTPUT\e[0m" | aha
#+END_SRC
The issue with the proposed solution
The initial command is just echo -e "\e[0;31mOUTPUT\e[0m" not echo -e "\e[0;31mOUTPUT\e[0m" | aha

how to make org-babel-tangle to take account of :dir property?

I want org babel to take account of the dir header property when i use org-babel-tangle.
E.g.
#+begin_src bash :dir ~/blubb
echo $PWD
#+end_src
or even
#+begin_src bash :dir /ssh:someone#somewhere|sudo:anotherone#somewhere:somedir
echo $(hostname) $USER $PWD
#+end_src
When I do org-babel-tangle it creates a bash file with just the echo statement. I would like it to do a dir change, or wrap it in an ssh statement for the second example. (Same issue when I do org-export: the dir information is lost).
Has anybody found a solution for that?

org-babel shell always binds to "fish"

I have the excellent fish shell set as the default shell on my main laptop. For portability reasons (and editability reasons, as editing fish shell source blocks is buggy and slow) I'd still like to sometimes use sh or bash in my org-mode source blocks. It seems that org always resolves to fish, no matter what I try:
#+BEGIN_SRC sh
echo $SHELL
#+END_SRC
#+RESULTS:
: /usr/bin/fish
#+BEGIN_SRC bash
echo $SHELL
#+END_SRC
#+RESULTS:
: /usr/bin/fish
#+BEGIN_SRC fish
echo $SHELL
#+END_SRC
#+RESULTS:
: /usr/bin/fish
#+BEGIN_SRC shell
echo $SHELL
#+END_SRC
#+RESULTS:
: /usr/bin/fish
org-babel-shell-names is set to ("sh" "bash" "zsh" "fish" "csh" "ash" "dash" "ksh" "mksh" "posh"). How do I debug this? Or is there any way to set the shell to bash globally for org-babel? I'm using emacs 26.3 with the spacemacs configuration at commit 5fcd84d84 and latest org-mode.
I think you'll find that the SHELL environment variable isn't what you think it is.
Try echoing $0 instead -- or running any built-in shell command which will give shell-specific output (bash has a help command, for example).

How do I set pandoc latex template options with knitr's pandoc() function using an embedded pandoc config?

Is it possible to use knitr's pandoc() function with an embedded pandoc config in a markdown file to change the default pandoc latex template options?
For example, if I use the following embedded pandoc() config at the beginning of foo.md:
<!--pandoc
format: latex
s:
V: geometry:margin=1.5cm
o: foobar.pdf
-->
Then, from R prompt,
pandoc("foo.md","latex")
produces the following system call from R:
pandoc -s -V=geometry:margin=1.5cm -f markdown -t latex -o foobar.pdf 'foo.md'
However, For the page margins to actually be adjusted to 1.5cm, the system call needs to NOT have the "=" after the -V. In other words, the system call needs to be:
pandoc -s -V geometry:margin=1.5cm -f markdown -t latex -o foobar.pdf 'foo.md'
Sorry this is a bug of knitr that has been fixed in the development version (1.2.7) on Github.

In Emacs-lisp, what is the correct way to use call-process on an ls command?

I want to execute the following shell command in emacs-lisp:
ls -t ~/org *.txt | head -5
My attempt at the following:
(call-process "ls" nil t nil "-t" "~/org" "*.txt" "| head -5")
results in
ls: ~/org: No such file or directory
ls: *.txt: No such file or directory
ls: |head -5: No such file or directory
Any help would be greatly appreciated.
The problem is that tokens like ~, *, and | aren't processed/expanded by the ls program. Since the tokens aren't processed, ls is look for a file or directory literally called ~/org, a file or directory literally called *.txt, and a file or directory literally called | head -5. Thus the error message you received about `No such file or directory".
Those tokens are processed/expanded by the shell (like Bourne shell /bin/sh or Bash /bin/bash). Technically, interpretation of the tokens can be shell-specific, but most shell interpret at least some of the same standard tokens the same way, e.g. | means connecting programs together end-to-end to almost all shells. As a counterexample, Bourne shell (/bin/sh) does not do ~ tilde/home-directory expansion.
If you want to get the expansions, you have to get your calling program to do the expansion itself like a shell would (hard work) or run your ls command in a shell (much easier):
/bin/bash -c "ls -t ~/org *.txt | head -5"
so
(call-process "/bin/bash" nil t nil "-c" "ls -t ~/org *.txt | head -5")
Edit: Clarified some issues, like mentioning that /bin/sh doesn't do ~ expansion.
Depending on your use case, if you find yourself wanting to execute shell commands and have the output made available in a new buffer frequently, you can also make use of the shell-command feature. In your example, it would look something like this:
(shell-command "ls -t ~/org *.txt | head -5")
To have this inserted into the current buffer, however, would require that you set current-prefix-arg manually using something like (universal-argument), which is a bit of a hack. On the other hand, if you just want the output someplace you can get it and process it, shell-command will work as well as anything else.