Active code

Continuing on my current note system arrangement, the topic today is active code. Active code is the ability to execute source code and show the result on the same document.

The main inspiration is babel, emacs org-mode package. Babel can deal with multiple programming languages (babel languages), attach headers on how the language is used.

Unfortunately babel is integrated with emacs and orgmode. I’d require to change all my documents to .org extension and use emacs.

cog in vim

As I don’t need multiple language support I can use something with a single language (python) and that integrates with vim without issue.

The library cogapp offers a lightweight import in python and a binary that performs substitutions in text files, running standard python interpreter.

# text before

calendar entries

from tools import *;print_calendar()

Sat Oct 08      event 1
Wed Oct 12      event 2


# text after

where contains:

import cog
def print_calendar():
    import subprocess;cog.outl("")
    result ="calendar -w -A14".split(), capture_output=True)

In order to execute the code, I added the following line in my .vimrc .

nmap <F10> :!cog -r %:p<CR>:e<CR>

which overwrites the open file and then rereads it in vim.