Kishore Vancheeshwaran
Showing diff in orgmode

This is just an example to show diff in orgmode codeblocks. I suppose this would be useful when you use orgmode to demonstrate/teach programming concepts. The diff would help visualizing what lines have changed.

1
2
3
4
5
#+NAME: before
#+BEGIN_EXAMPLE
before
foobar
#+END_EXAMPLE

will render

1
2
before
foobar

Similarly,

1
2
3
4
5
#+NAME: after
#+BEGIN_EXAMPLE
after
foobar
#+END_EXAMPLE

will render

1
2
after
foobar

Now the following will evaluate the diff to the #+RESULTs:.

1
2
3
4
5
6
#+BEGIN_SRC sh :results verbatim :exports results :var in=before :var out=after :wrap SRC diff
echo "$in" >before
echo "$out" >after
diff -u before after
rm -f before after
#+END_SRC

Here is the result.

1
2
3
4
5
6
--- before	2021-06-21 10:58:47.749249096 +0530
+++ after	2021-06-21 10:58:47.749249096 +0530
@@ -1,3 +1,3 @@
-before
+after
 foobar

With python it looks like it’s slightly more involved.

beforepy

1
2
3
4
5
#+NAME: beforepy
#+BEGIN_SRC python
print('hello')
print('world')
#+END_SRC

renders as below.

1
2
print('hello')
print('world')

afterpy

1
2
3
4
5
#+NAME: afterpy
#+BEGIN_SRC python
print('hello')
print('myworld')
#+END_SRC

renders as below.

1
2
print('hello')
print('myworld')

To evaluate the diff, we do the following.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
#+BEGIN_SRC sh :results verbatim :exports results :noweb yes :wrap SRC diff
cat >beforepy <<'EOF'
<<beforepy>>
EOF
cat >afterpy <<'EOF'
<<afterpy>>
EOF
diff -u beforepy afterpy
rm -f beforepy afterpy
#+END_SRC

which gives the following results.

1
2
3
4
5
6
--- beforepy	2021-06-21 11:07:51.129784218 +0530
+++ afterpy	2021-06-21 11:07:51.139784174 +0530
@@ -1,2 +1,2 @@
 print('hello')
-print('world')
+print('myworld')

Hope that helps.

If you would like to leave a comment, contact me via email.
Post 8/99 - part of 100DaystoOffload