Skip to content

Instantly share code, notes, and snippets.

@jefftrull
Created July 28, 2019 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jefftrull/996422d082e8dd3b58530a24fc21163d to your computer and use it in GitHub Desktop.
Save jefftrull/996422d082e8dd3b58530a24fc21163d to your computer and use it in GitHub Desktop.
org-babel demo from Emacs SF meetup 2019-07-24

Brief intro to Source Code in Org

Required Setup

;; add support for C++ and Python
(org-babel-do-load-languages
  'org-babel-load-languages
  '((C . t) (python . t)))

;; don't ask if I want to run code
(setq org-confirm-babel-evaluate nil)

Intro

Python Example

testdata = np.array([1, 2, 0, 3, 3])
print(np.cumsum(testdata))

C++ Example

using namespace std;

// tangling demo
vector<int> testdata{1, 2, 0, 3, 3};
vector<int> result;
partial_sum(begin(testdata), end(testdata),
            back_inserter(result));
copy(begin(result), end(result),
     ostream_iterator<int>(cout, " "));
cout << "\n";

Features

Input from Lists and Tables

NameState
ArgonneIL
Oak RidgeTN
SandiaNM
Savannah RiverSC

Exporting

Export Control

  • you can export output, results, both, or neither
  • preambles and other boilerplate from Org won’t appear

incorporating other code blocks with noweb references

// exported but not (directly) executed

// equivalent of key=lambda v: v[1] in Python
[](auto const& a, auto const& b)
{
    return get<1>(a) < get<1>(b);
}
// boilerplate: executed but not exported
using namespace std;
vector<pair<string, string>> nl;
for (auto const& row : cdata)
{
    nl.emplace_back(row[0], row[1]);
}
sort(begin(nl), end(nl),
     <<lambda>>
);
for (auto const & row : nl)
{
    // carefully quote to make org happy
    cout << "\"" << get<0>(row) << "\", " << get<1>(row) << "\n";
}

Things to talk about

  • C-c C-, prefix for structure templates followed by “s”
  • C-c ’ to edit code in its defined mode
  • C-c C-v v to show the code as it will be built (including boilerplate)
  • C-c C-c to run the code (and possibly insert results)
  • feeding source code from tables and lists
  • controlling output for slides: results and removing boilerplate

Export Configuration

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment