.emacs.d/doc/emacs-notes.org

6.4 KiB
Raw Blame History

Notes on Emacs

Introduction to Babel

Tutorial from http://orgmode.org/worg/org-contrib/babel/intro.html

Source Code Execution

  (print "Hello, There!")
Hello, There!
  echo "This file takes up `du -h emacs-org-babel-tutorial.org | sed 's/\([0-9k]*\)[ ]*emacs-org-babel-tutorial.org/\1/'`"
This file takes up 4.0K
  words <- tolower(scan("emacs-org-babel-tutorial.org", what="", na.strings=c("|",":")))
  t(sort(table(words[nchar(words) > 3]), decreasing=TRUE)[1:10])
#+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

  import time
  print("Hello, today's date is %s" % time.ctime())
  print("Two plus two is")
  return 2 + 2
4
  import time
  print("Hello, today's date is %s" % time.ctime())
  print("Two plus two is")
  2 + 2
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

  return x*x
36
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
  (defun fibonacci (n)
    (if (or (= n 0) (= n 1))
        n
      (+ (fibonacci (- n 1))
         (fibonacci (- n 2)))))

  (mapcar (lambda (row)
            (mapcar #'fibonacci row))
          fib-inputs)
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:

date
) or with header arguments (like so:
return 10 + 10
).

Code Block Body Expansion

Preview: C-c C-v v, bound to org-babel-expand-src-block

username john-doe
password abc123
(setq my-special-username (first (first data)))
(setq my-special-password (first (second data)))

A Meta-programming Language for Org-mode

  cd ~ && du -sc * | grep -v total
538604 Desktop
77332656 Documents
1206668 Mail
8 News
  pie(dirs[,1], labels = dirs[,2])

Note: the syntax #+name: directory-pie-chart(dirs=directories) did not work.

Using Code Blocks in Org Tables

Example 1: Data Summaries Using R
  runif(n=5, min=0, max=1)
  colMeans(x)
mean
0.574235895462334
Example 2: Babel Test Suite

No notes

The Library of Babel

Does not do what I expected …

Literate Programming

Tangling with C-c C-v t.

Reproducible Research

Random Notes

How to paste then copy   NOTE

CLOCK: [2018-08-11 Sat 21:47][2018-08-11 Sat 21:48] => 0:01

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 doesnt work so well, disabled it again.

Tramp and Telnet over non-standard ports   NOTE

Syntax: /telnet:HOST#PORT:, works also with other protocols.

Fractals in Emacs   NOTE

CLOCK: [2018-08-04 Sat 13:01][2018-08-04 Sat 13:03] => 0:02

From https://nullprogram.com/blog/2012/09/14/

(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)))