Various things I want to give others access to

babel.org 20KB

literate programming: what is it good for

  • literate programming
  • The idea of mixing code and prose in a seamless manner
  • weaving
  • Presenting the prose/code in a format for humans, documentation

code blocks in Org Mode

  • tangling
  • puting the code together in a way that can be understood by a computer, source code Source code can be easily inserted into an org file to make it obvious that what you are looking at is code. It allows syntax highlighting and even code execution with the package Babel.
All Source code should be inserted inside a source block.
Create Template of a source block
After this you must type the language being use after BEGIN_SRC
  • Commands for buffer
  • C-c C-e
    org export
    * C-s :: toggle export scope
    C-c C-v C-t
    tangle all code blocks in file that have tangling enabled
    C-c C-x p
    set property for section
  • commands inside source block
  • C-c '
    open a new buffer for you to type your code into. When this buffer
    is saved your code will appear inside the source block
    C-c C-c
    execute code block, must give permission by language in your config file
    C-c C-v C-v
    expand noweb references and show in new buffer, which
    can't be modified

header arguements

  • :file :: output results to file
  • :tangle :: whether to allow tangling and where to store it
  • * no :: default, code block wont be tangled * yes :: allow tangling, will be written to file with same name but with but with language specific file extension * filename :: block will be written to specified filename
  • :results :: how code will be evaluated and results presented
  • collection options(can only use one of these)
  • * value :: functional mode, Default, returns what the code returns * output :: scripting mode, returns what the code prints
  • type of output to return(choose one)
  • * table :: format as org table * list :: format as org list * scalar, verbatim :: return results verbatim * file :: interpret as path to file. inserts a link to the file
  • :dir :: execute source block from specified directory
  • :var :: pass in variable to
  • :exports :: how block will act when exporting
  • * code :: default, only export code * results :: export the results of executing a code block * both :: return code and results

capturing output

Executing code will result in a the ouput being printed under a RESULTS property tag. Babel will try to format the ouput for you but you can configure it yourself with the :results header

#+name: ls #+BEGIN_SRC shell ls /home/z #+END_SRC

#+RESULTS: ls | bin | | config.org | | Desktop | | dl | | docs | | Downloads | | ex2.png | | FiraxisLive | | games | | knxw | | life | | lit | | nc | | Pictures | | prj | | Steam |

#+BEGIN_SRC shell :dir /home/z :var args=ls file $args #+END_SRC

#+RESULTS: | bin: | directory | | | | | | | | | | config.org: | Lisp/Scheme | program, | ASCII | text | | | | | | | Desktop: | directory | | | | | | | | | | dl: | directory | | | | | | | | | | docs: | directory | | | | | | | | | | Downloads: | directory | | | | | | | | | | ex2.png: | PNG | image | data, | 1920 | x | 1080, | 8-bit/color | RGB, | non-interlaced | | FiraxisLive: | directory | | | | | | | | | | games: | directory | | | | | | | | | | knxw: | directory | | | | | | | | | | life: | directory | | | | | | | | | | lit: | directory | | | | | | | | | | nc: | ASCII | text | | | | | | | | | Pictures: | directory | | | | | | | | | | prj: | directory | | | | | | | | | | Steam: | directory | | | | | | | | |

Arguments to code blocks

#+name: squared #+header: :var x=4 #+BEGIN_SRC python return x**2 #+END_SRC

#+RESULTS: squared

16

#+call: squared(9)

#+RESULTS:

81

#+name: reverseList #+header: :var listl = ls #+BEGIN_SRC python print(listl) #+END_SRC

#+RESULTS: reverseList

None

#+BEGIN_SRC shell :dir /zfadd.is: ls #+END_SRC

#+RESULTS: | custom ex4.txt ex7.txt gitea pass this-is-fine.png | | ex3.txt ex6.txt ex8.txt newuser this-is-fine.jpg |

weaving and tangling code

the header arguement noweb allows you to weave multiple code blocks together

#+NAME: life-cycle #+BEGIN_SRC python :noweb yes :results output verbatim :tangle ~/life.py <> while age <= lifespan: print"Happy Birthday, you are ", age, "years old!" age += 1 <> #+END_SRC

#+RESULTS: life-cycle #+begin_example Hello World! Happy Birthday, you are 1 years old! Happy Birthday, you are 2 years old! Happy Birthday, you are 3 years old! Happy Birthday, you are 4 years old! Happy Birthday, you are 5 years old! Happy Birthday, you are 6 years old! Happy Birthday, you are 7 years old! Happy Birthday, you are 8 years old! Happy Birthday, you are 9 years old! Happy Birthday, you are 10 years old! Goodbye cruel world! #+end_example

#+NAME: life #+BEGIN_SRC python print('Hello World!')

age = 1 lifespan = 10 #+END_SRC

#+NAME: death #+BEGIN_SRC python print('Goodbye cruel world!') #+END_SRC

weaving together init files with org mode and babel

Binary Tree

A hierarchical data structure in which each node contains pointers for up to two children nodes, refered to as left and right node.

  • properties
  • Maximum nodes at level n
    2^(n-1)
    Maximum nodes in tree of height n
    2^
    Minimum possible height of tree with n nodes
    ceil(Log2(n+1))
  • O(n) of common operations, assuming tree is height balanced
  • Search
    O(Log n)
    Insert
    O(Log n)
    Delete
    O(Log n)
    Traversal
    O(Log n)

#+Name: BiTree #+BEGIN_SRC python :noweb yes :tangle ~/bin/biTree.py class BiTree():

def __init__(self, data): self.data = data self.lBranch = None self.rBranch = None

def set_right(self, node): self.rBranch = node

def set_left(self, node): self.lBranch = node

<>

<>

<>

<> #+END_SRC

#+Name: usage examples #+BEGIN_SRC python :results output import sys sys.path.append('/home/z/bin') from biTree import BiTree

tree = BiTree(10) tree.insert(5) tree.insert(100) tree.insert(25) tree.insert(1) tree.insert(500) tree.insert(8)

print('In Order traversal: ') tree.inOrderTraversal()

print('Pre Order traversal: ') tree.preOrderTraversal()

print('Post Order traversal: ') tree.postOrderTraversal()

#+END_SRC

#+RESULTS: usage examples #+begin_example In Order traversal: 1 5 8 10 25 100 500 Pre Order traversal: 10 5 1 8 100 25 500 Post Order traversal: 1 8 5 25 500 100 10 #+end_example

#+RESULTS: #+begin_example In Order traversal: 1 5 8 10 25 100 500 Pre Order traversal: 10 5 1 8 100 25 500 #+end_example

insertion

#+Name: BiTree-insertion #+BEGIN_SRC python def insert(self, data):

if data == self.data: raise ValueError('data already in tree')

if data < self.data: if self.lBranch == None: self.lBranch = BiTree(data) else: self.lBranch.insert(data)

if data > self.data: if self.rBranch == None: self.rBranch = BiTree(data) else: self.rBranch.insert(data) #+END_SRC

Traversals

There are a number of ways to iterate through, or traverse, a tree

In Order

visit the left branch, then the current node, and finally the right branch

#+NAME: BiTree-inOrderTraversal #+BEGIN_SRC python def inOrderTraversal(self): if self.lBranch: self.lBranch.inOrderTraversal()

print(self.data)

if self.rBranch: self.rBranch.inOrderTraversal() #+END_SRC

Pre Order

vist the current node, then left tree, and finally the right tree

#+NAME: BiTree-preOrderTraversal #+BEGIN_SRC python def preOrderTraversal(self): print(self.data)

if self.lBranch: self.lBranch.preOrderTraversal()

if self.rBranch: self.rBranch.preOrderTraversal() #+END_SRC

Post Order

vist the left tree, then the right tree, and finally the parent node #+NAME: BiTree-postOrderTraversal #+BEGIN_SRC python def postOrderTraversal(self): if self.lBranch: self.lBranch.postOrderTraversal()

if self.rBranch: self.rBranch.postOrderTraversal()

print(self.data) #+END_SRC

emacs config

:PROPERTIES: :header-args: :tangle /home/z/config.org :END:

#+BEGIN_SRC emacs-lisp (org-babel-load-file "~/.emacs.d/config.org") #+END_SRC

Emacs

Package Management

load the package manager, package, add several archives for package to pull from

#+BEGIN_SRC emacs-lisp ;(require 'package)

; (add-to-list 'package-archives '("org" . "http://orgmode.org/elpa/")) ; (add-to-list 'package-archives '("melpa" . "http://melpa.org/packages/")) ; (add-to-list 'package-archives '("melpa-stable" . "http://stable.melpa.org/packages/"))

; (setq package-enable-at-startup nil) ; (package-initialize) #+END_SRC

Personal Info

Display

#+BEGIN_SRC emacs-lisp (setq user-full-name "Zachary Faddis" user-mail-address "zach@zfadd.is") #+END_SRC

Theme

#+BEGIN_SRC emacs-lisp (load-theme 'nimbus) #+END_SRC

Line Numbers

#+BEGIN_SRC emacs-lisp (global-linum-mode 1) #+END_SRC

Line Limit

Limit Line Length to 80. Whenever I exceed 80 chars insert a newline at 80 and move the offending word to the next line

#+BEGIN_SRC emacs-lisp (setq-default auto-fill-function 'do-auto-fill) (setq-default fill-column 80) #+END_SRC

Minimal UI
Highlight Current Line
Highlight Matching Parathese

#+BEGIN_SRC emacs-lisp (scroll-bar-mode -1) (tool-bar-mode -1) (tooltip-mode -1) (menu-bar-mode -1) #+END_SRC #+BEGIN_SRC emacs-lisp (global-hl-line-mode) #+END_SRC #+BEGIN_SRC emacs-lisp

Use icons when available

Files

;; Show matching parens (setq show-paren-delay 0) (show-paren-mode 1) #+END_SRC #+BEGIN_SRC emacs-lisp (require 'all-the-icons) #+END_SRC

save backups in ~/.emacs/backups #+BEGIN_SRC emacs-lisp

(setq backup-directory-alist `((".*" . , "~/.emacs.d/backups"))) (setq auto-save-file-name-transforms `((".*" ,"~/.emacs.d/backups" t))) #+END_SRC

Prompts

change yes/no to y/n #+BEGIN_SRC emacs-lisp (fset 'yes-or-no-p 'y-or-n-p) #+END_SRC

Default Browser

#+BEGIN_SRC emacs-lisp

(setq browse-url-browser-function 'browse-url-firefox) #+END_SRC

#+RESULTS:

browse-url-firefox

Org Mode

#+BEGIN_SRC emacs-lisp (require 'org) #+END_SRC

File Hooks

use org mode for any file ending in .org, .txt, or .org_archive #+BEGIN_SRC emacs-lisp (add-to-list 'auto-mode-alist '("\\.\\(org\\|org_archive\\|txt\\)$" . org-mode)) #+END_SRC

Todo

Change the todo sequence and set shortcuts #+BEGIN_SRC emacs-lisp (setq org-todo-keywords '((sequence "TODO(t)" "NEXT(n)" "DATE(d)" "|" "FIN(f)" "CANCEL(c)"))) #+END_SRC

Log the time a todo item is set to a finished state #+BEGIN_SRC emacs-lisp (setq org-log-into-drawer t) #+END_SRC

Time

Stuff the times of a headline into a drawer #+BEGIN_SRC emacs-lisp (setq org-log-done 'time) #+END_SRC

Agenda

#+BEGIN_SRC emacs-lisp (setq org-agenda-start-on-weekday nil)

(setq org-agenda-start-with-clockreport-mode t)

(setq org-agenda-files '("~/life/business.txt" "~/life/maintenance.txt" "~/life/social.txt" "~/life/experience.txt"))

(setq org-agenda-custom-commands '(("z" "Agenda and NEXT items" ((agenda "" ( (org-agenda-span 1))) (todo "NEXT" ) (agenda "" ( (org-agenda-start-day "+1d") (org-agenda-span 10)))))))

(setq org-agenda-prefix-format '((agenda . " %i %-12:c%?-12t% s %b") (timeline . " % s") (todo . " %i %-12:c %b") (tags . " %i %-12:c") (search . " %i %-12:c"))) #+END_SRC

search/agenda context detail

Key Bindings

#+BEGIN_SRC emacs-lisp (setq org-show-context-detail '((agenda . local) (default . lineage))) #+END_SRC

#+BEGIN_SRC emacs-lisp (global-set-key "\C-cl" 'org-store-link) (global-set-key "\C-ca" 'org-agenda) (global-set-key "\C-cb" 'org-iswitchb) #+END_SRC

Scheduling

Display

#+BEGIN_SRC emacs-lisp #+END_SRC

Make red anything surround by *'s #+BEGIN_SRC emacs-lisp (add-to-list 'org-emphasis-alist '("*" (:foreground "red") )) #+END_SRC

Use Bullets for headings #+BEGIN_SRC emacs-lisp (require 'org-bullets) (add-hook 'org-mode-hook (lambda () (org-bullets-mode 1))) #+END_SRC

Export

export settings #+BEGIN_SRC emacs-lisp (setq org-export-with-smart-quotes t) (setq org-export-wth-emphasize t) (setq org-export-with-author t) (setq org-export-with-broken-links t) (setq org-export-with-date t) (setq org-export-with-email t) (setq org-export-time-stamp-file nil) (setq org-export-with-title t) (setq org-export-wth-toc nil) (setq org-export-with-section-numbers nil) #+END_SRC

HTML

html settings #+BEGIN_SRC emacs-lisp (setq org-html-doctype "html5") (setq org-html-link-home "zfadd.is") #+END_SRC

Templates

#+BEGIN_SRC emacs-lisp (add-to-list 'org-structure-template-alist (list "c" (concat":PROPERTIES:\n" ":EMAIL:?\n" ":PHONE:\n" ":ADDRESS:\n" ":BIRTHDAY:\n" ":WEB:\n" ":END:")))

Source Blocks

    #+END_SRC
  • Use syntax highlighting for source blocks
  • #+BEGIN_SRC emacs-lisp (setq org-src-fontify-natively t) #+END_SRC
  • preserve indention when tangling code
  • #+BEGIN_SRC emacs-lisp (setq org-src-preserve-indentation t) #+END_SRC

Babel

  • hide blocks by default
  • #+BEGIN_SRC emacs-lisp (setq org-hide-block-startup t) #+END_SRC
  • Allow languages
  • #+BEGIN_SRC emacs-lisp (org-babel-do-load-languages 'org-babel-load-languages '((ledger . t) (shell . t) (python . t) (emacs-lisp . t))) #+END_SRC

Evil

#+BEGIN_SRC emacs-lisp (require 'evil) (evil-mode t) #+END_SRC

Disable in cretain modes

Novel

#+BEGIN_SRC emacs-lisp (add-to-list 'evil-emacs-state-modes 'elfeed-search-mode) (add-to-list 'evil-emacs-state-modes 'elfeed-show-mode) (add-to-list 'evil-emacs-state-modes 'magit-status) #+END_SRC

File hooks

Use nov-mode for any file with extension .epub

#+BEGIN_SRC emacs-lisp (add-to-list 'auto-mode-alist '("\\.epub\\'" . nov-mode)) #+END_SRC

Web Mode

Package to give syntax highlighting for html, css, and various templating languages used by different frameworks

#+BEGIN_SRC emacs-lisp (require 'web-mode) #+END_SRC

File Hooks

Use web-mode for any file that ends with extension .html #+BEGIN_SRC emacs-lisp (add-to-list 'auto-mode-alist '("\\.html\\'" . web-mode)) #+END_SRC

Use the Django syntax highlighting engine for any file ending in html #+BEGIN_SRC emacs-lisp

(setq web-mode-engines-alist '(("django" . "\\.html\\'"))) #+END_SRC

Ledger

File Hooks

Start any file that ends with .ledger in ledger mode #+BEGIN_SRC emacs-lisp (add-to-list 'auto-mode-alist '("\\.ledger$" . ledger-mode)) #+END_SRC

Display

Automatically align amount to 52nd collumn #+BEGIN_SRC emacs-lisp (setq ledger-post-auto-adjust-amounts t) #+END_SRC

Beeminder

#+BEGIN_SRC emacs-lisp (add-to-list 'load-path "/home/z/.emacs.d/beeminder/") ;(require 'request) ;(require 'anaphora) (require 'cl) (require 'beeminder) (setq beeminder-username "zacharius") (setq beeminder-auth-token "e5j8yjZJQyxVZ6qX228x") #+END_SRC

Declutter

#+BEGIN_SRC emacs-lisp (add-to-list 'load-path "/home/z/.emacs.d/declutter") (require 'declutter) #+END_SRC

NeoTree

#+BEGIN_SRC emacs-lisp

Magit

;; NeoTree (require 'neotree) (setq neo-theme (if (display-graphic-p) 'icons 'arrow)) #+END_SRC #+BEGIN_SRC emacs-lisp (add-to-list 'load-path "/home/z/.emacs.d/magit/lisp") (require 'magit)

(with-eval-after-load 'info (info-initialize) (add-to-list 'Info-directory-list "~/.emacs.d/magit/Documentation/")) #+END_SRC

keybindings

#+BEGIN_SRC emacs-lisp (global-set-key (kbd "C-x g") 'magit-status) #+END_SRC

Ibuffer

#+BEGIN_SRC emacs-lisp

(global-set-key (kbd "C-x C-b") 'ibuffer) ;; Use Ibuffer for Buffer List (setq ibuffer-saved-filter-groups '(("buffer groups" ("emacs-config" (filename . ".emacs.d")) ("documentation" (filename . "knxw")) ("organization" (or (filename . "life") (name . "\*Org Agenda\*"))) ("writings" (filename . "lit")) ("projects" (filename . "prj")) ("games" (filename . "games")))))

(add-hook 'ibuffer-mode-hook '(lambda () (ibuffer-switch-to-saved-filter-groups "buffer groups")))

(setq ibuffer-expert t)

#+END_SRC

Ido

#+BEGIN_SRC emacs-lisp

(setq ido-enable-flex-matching t) (setq ido-everywhere t) (ido-mode 1) #+END_SRC

Python

#+BEGIN_SRC emacs-lisp (elpy-enable) #+END_SRC

sources/contact info/self promotion

  • sources
  • http://howardism.org/Technical/Emacs/literate-programming-tutorial.html
  • https://orgmode.org/worg/org-contrib/babel/intro.html#literate-programming
  • personal website : https://zfadd.is/
  • this org file : https://git.zfadd.is/zacharius/resources/src/branch/master/babel.org
  • config file : https://git.zfadd.is/zacharius/init/src/branch/master/emacs.org