#+title: Notes on Emacs #+author: Daniel * Introduction to Babel Tutorial from http://orgmode.org/worg/org-contrib/babel/intro.html ** Source Code Execution #+begin_src hy (print "Hello, There!") #+end_src #+RESULTS: : Hello, There! #+begin_src sh echo "This file takes up `du -h emacs-org-babel-tutorial.org | sed 's/\([0-9k]*\)[ ]*emacs-org-babel-tutorial.org/\1/'`" #+end_src #+RESULTS: : This file takes up 4.0K #+begin_src R :colnames yes words <- tolower(scan("emacs-org-babel-tutorial.org", what="", na.strings=c("|",":"))) t(sort(table(words[nchar(words) > 3]), decreasing=TRUE)[1:10]) #+end_src #+RESULTS: | #+begin_src | #+end_src | #+results: | date | plus | today's | :results | hello, | import | is") | |-------------+-----------+------------+------+------+---------+----------+--------+--------+------| | 5 | 5 | 4 | 3 | 3 | 3 | 2 | 2 | 2 | 2 | *** Capturing the Results of Code Evaluation #+begin_src python :results value import time print("Hello, today's date is %s" % time.ctime()) print("Two plus two is") return 2 + 2 #+end_src #+RESULTS: : 4 #+begin_src python :results output import time print("Hello, today's date is %s" % time.ctime()) print("Two plus two is") 2 + 2 #+end_src #+RESULTS: : Hello, today's date is Sun Jun 26 16:04:36 2016 : Two plus two is *** Session-based Evaluation Have a look into /Emacs Speaks Statistics/ *** Arguments to Code Blocks #+name: square #+header: :var x = 0 #+begin_src python return x*x #+end_src #+call: square(x=6) #+RESULTS: : 36 #+tblname: fibonacci-inputs | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | | 2 | 4 | 6 | 8 | 10 | 12 | 14 | 16 | 18 | 20 | #+name: fibonacci-seq #+begin_src emacs-lisp :var fib-inputs=fibonacci-inputs (defun fibonacci (n) (if (or (= n 0) (= n 1)) n (+ (fibonacci (- n 1)) (fibonacci (- n 2))))) (mapcar (lambda (row) (mapcar #'fibonacci row)) fib-inputs) #+end_src #+RESULTS: fibonacci-seq | 1 | 1 | 2 | 3 | 5 | 8 | 13 | 21 | 34 | 55 | | 1 | 3 | 8 | 21 | 55 | 144 | 377 | 987 | 2584 | 6765 | *** In-line Code Blocks In-line code can be call without header arguments (like so: src_sh{date}) or with header arguments (like so: src_python[:results value]{return 10 + 10}). *** Code Block Body Expansion Preview: =C-c C-v v=, bound to =org-babel-expand-src-block= #+tblname: data | username | john-doe | | password | abc123 | #+begin_src emacs-lisp :var data=data (setq my-special-username (first (first data))) (setq my-special-password (first (second data))) #+end_src *** A Meta-programming Language for Org-mode #+name: directories #+begin_src sh :results replace cd ~ && du -sc * | grep -v total #+end_src #+RESULTS: directories | 538604 | Desktop | | 77332656 | Documents | | 1206668 | Mail | | 8 | News | #+name: directory-pie-chart #+begin_src R :session R-pie-example :var dirs=directories pie(dirs[,1], labels = dirs[,2]) #+end_src #+RESULTS: directory-pie-chart Note: the syntax =#+name: directory-pie-chart(dirs=directories)= did not work. *** Using Code Blocks in Org Tables **** Example 1: Data Summaries Using R #+name: tbl-example-data #+begin_src R runif(n=5, min=0, max=1) #+end_src #+name: R-mean #+begin_src R :var x="" colMeans(x) #+end_src #+tblname: summaries | mean | |-------------------| | 0.574235895462334 | #+TBLFM: @2$1='(org-sbe "R-mean" (x "tbl-example-data()")) **** Example 2: Babel Test Suite /No notes/ ** The Library of Babel #+lob: square(x=6) Does not do what I expected … ** Literate Programming #+name: hello-world-prefix #+begin_src sh :exports none echo "/-----------------------------------------------------------\\" #+end_src #+name: hello-world-postfix #+begin_src sh :exports none echo "\-----------------------------------------------------------/" #+end_src #+name: hello-world #+begin_src sh :tangle hello.sh :exports none :noweb yes <> echo "| hello world |" <> #+end_src Tangling with =C-c C-v t=. ** Reproducible Research * Random Notes ** How to paste then copy :NOTE: :PROPERTIES: :CREATED: [2018-08-11 Sat 21:47] :END: :LOGBOOK: CLOCK: [2018-08-11 Sat 21:47]--[2018-08-11 Sat 21:48] => 0:01 :END: Question: how to set a mark such that all subsequent copy operations move their text to that exact mark. Answer: use ~cua-selection-mode~! See https://www.reddit.com/r/emacs/comments/8ekz0u/how_to_pastethencopy/. /Update/: turns out it doesn’t work so well, disabled it again. ** Tramp and Telnet over non-standard ports :NOTE: :PROPERTIES: :CREATED: [2018-12-29 Sat 15:58] :END: Syntax: ~/telnet:HOST#PORT:~, works also with other protocols. ** Magit Walkthrough :NOTE: :PROPERTIES: :CREATED: [2018-08-11 Sat 21:05] :END: https://emacsair.me/2017/09/01/magit-walk-through/ ** Fractals in Emacs :NOTE: :PROPERTIES: :CREATED: [2018-08-04 Sat 13:01] :END: :LOGBOOK: CLOCK: [2018-08-04 Sat 13:01]--[2018-08-04 Sat 13:03] => 0:02 :END: From https://nullprogram.com/blog/2012/09/14/ #+begin_src emacs-lisp (defun sierpinski (s) (pop-to-buffer (get-buffer-create "*sierpinski*")) (fundamental-mode) (erase-buffer) (labels ((fill-p (x y) (cond ((or (zerop x) (zerop y)) "0") ((and (= 1 (mod x 3)) (= 1 (mod y 3))) "1") (t (fill-p (/ x 3) (/ y 3)))))) (insert (format "P1\n%d %d\n" s s)) (dotimes (y s) (dotimes (x s) (insert (fill-p x y) " ")))) (image-mode)) (defun mandelbrot () (pop-to-buffer (get-buffer-create "*mandelbrot*")) (let ((w 400) (h 300) (d 32)) (fundamental-mode) (erase-buffer) (set-buffer-multibyte nil) (insert (format "P6\n%d %d\n255\n" w h)) (dotimes (y h) (dotimes (x w) (let* ((cx (* 1.5 (/ (- x (/ w 1.45)) w 0.45))) (cy (* 1.5 (/ (- y (/ h 2.0)) h 0.5))) (zr 0) (zi 0) (v (dotimes (i d d) (if (> (+ (* zr zr) (* zi zi)) 4) (return i) (psetq zr (+ (* zr zr) (- (* zi zi)) cx) zi (+ (* (* zr zi) 2) cy)))))) (insert-char (floor (* 256 (/ v 1.0 d))) 3)))) (image-mode))) #+end_src