2 #+author: Richard Westhaver
3 #+description: Core Library of Babel
4 #+setupfile: ../clean.theme
5 #+property: header-args :exports both
6 Welcome to the Core
[[https://www.gnu.org/software/emacs/manual/html_node/org/Library-of-Babel.html][Library of Babel]]. This file contains a
7 collection of code blocks used throughout our Org documents.
9 To load the library use
~C-c C-v i~.
11 * systemd-list-units :os: 13 :ID: 3b23c98d-a286-4988-846d-2dab3d25803d 15 #+name: systemd-list-units
16 #+begin_src sh :results replace 17 systemctl list-units --state=running | grep -v systemd | awk '{print $1}' | grep service
22 :ID: 70008815-9634-48ca-b672-b8dcf2a44074 25 #+begin_src shell :var dir="." 26 cd $dir && cat * | wc -l
31 :ID: be755790-f367-4654-87e5-cd2927bfef45 34 #+begin_src shell :var dir="." 35 cd $dir && cat * | wc -w
38 * tokei-dir-lines :fs: 40 :ID: 670e9855-f8d2-43eb-86af-3ef7292f90b9 42 #+name: tokei-dir-lines
43 #+begin_src shell :var src=(org-sbe org-current-h1-title) :results output replace 45 input=`tokei -C -o json`
46 echo $input | jq -r '.["Total"] | .code, .comments, .blanks'
49 * tokei-dir-langs :fs: 51 :ID: ff9682f0-bb64-427f-a87d-e0c655f9fdc9 53 #+name: tokei-dir-langs
54 #+begin_src shell :var src=(org-sbe org-current-h1-title) :results output replace 56 input=`tokei -C -o json`
57 echo $input | jq -r '.["Total"].children | keys[]'
62 :ID: d3c4ac69-337f-430b-a4db-760504f099ea 65 #+begin_src emacs-lisp :var s=tokei-dir-lines 69 for i in (split-string s) do
70 (setf tot (+ tot (string-to-number i)))
76 :ID: 0ee5b499-6f0b-4e15-8c6e-4876e90c20a9 79 #+begin_src emacs-lisp 80 (let* ((ast (org-element-parse-buffer)) ;; built up the abstract syntax tree of the org buffer
81 item-types ; all occuring item types. It could be that some task has more item types than another.
82 tasks ; accumulation list for the tasks
83 current-task ; name of the current task (header of level 1)
84 task-items) ; items of the current task
85 (org-element-map ast 'headline
87 (cl-case (org-element-property :level hl)
88 (1 ; We assume here that headers of level 1 are tasks.
89 (when current-task ; registering the old task
90 (setq tasks (cons (cons current-task (nreverse task-items)) tasks)))
91 (setq current-task (org-element-property :raw-value hl) ; preparing the new task
94 (let ((item-type (org-element-property :raw-value hl)))
95 (setq item-types (cons item-type item-types))
96 (setq task-items (cons (cons item-type (org-element-property :todo-keyword hl))
98 (setq tasks (nreverse (cons (cons current-task (nreverse task-items)) tasks)) ;add the last task
99 item-types (sort (cl-remove-duplicates (nreverse item-types) :test 'string-equal) ; list of unique item types
100 #'string<)) ;;Sorting the items lexicographical. Other criteria could be applied.
102 ;; generating the output table:
105 (cons "Item" (mapcar #'car tasks)) ; header
109 ;; mapping the items to the todo states associated to the tasks:
114 (let ((todo-status (cdr (assoc-string item-type task))))
120 * org-headlines-map :org: 122 :ID: 05955228-ca76-48bc-b769-f648b4310a9c 124 #+name: org-headlines-map
126 (org-element-map (org-element-parse-buffer 'headline )
129 (let ((parent (org-element-property :parent hl )))
130 (and (eq (org-element-type parent) 'headline)
131 (list (org-element-property :title parent) (org-element-property :title hl))))))
135 * make-info-tbl :org:fs:vc: 137 :ID: d5ba2f3d-fc2d-4db6-8bbd-7ca440ff0e8c 139 #+name: make-info-tbl
140 #+header: :var version="0.1.0"
141 #+header: :var name=(org-sbe org-current-h1-title)
142 #+header: :var dir="/home/ellis/comp/"
143 #+begin_src emacs-lisp :results table replace 144 (let* ((src (concat dir name))
145 (age (org-sbe "hg-log-age" ''(dir src)))
146 (rev (org-sbe "hg-rev" ''(dir src)))
147 (num (org-sbe "hg-id-num" ''(dir src)))
148 (cc1 (org-sbe "tokei-dir-lines" ''((dir src))))
149 (cc2 (org-sbe "tokei-dir-langs" ''((dir src))))
150 (nf (format "[[comp:docs/%s][%s]]" name name))
151 (rf (format "[[vc:comp/%s][%s:%s]]" name num rev))
152 ;; (gf (format "[[https://github.com/richardwesthaver/%s][github]]" name))
153 (vf (format "%s" rf))
154 (lsum (org-sbe sum-str-nums ('s 'cc1)))
155 (l (split-string cc1))
156 (lang (split-string cc2))
157 (cf (format "%s = λ:%s #:%s _:%s" lsum (pop l) (pop l) (pop l))))
168 #
+RESULTS: make-info-tbl 169 |---------+----------------------------|
170 | name | [[https://compiler.company/docs/org][org]] | 172 | vc | [[https://vc.compiler.company/comp/org][41+:0f4d1a0415d5]] | 173 | updated | 3 hours ago | 174 | lines | 13143 = λ:12268 #:46 _:829 | 175 | langs | (Html Org Svg) | 176 |---------+----------------------------| 178 * make-files-tbl :org:fs: 180 :ID: e2ff9dcf-8340-48b8-a1a6-e0036cbcc495 183 #+begin_src sh :results silent :var dir=(expand-file-name "~/comp") name=(org-sbe org-current-h1-title) 184 ls -lh $dir/$name --time-style=long-iso \
185 |awk '{if (NR!=1) print $8, $5, $6"-"$7}' \
186 |awk 'BEGIN{print "file size updated"}{print $0}'
189 #+name: make-files-tbl
190 #+begin_src python :var tab=ls-files() :results table :colnames yes :hlines yes :exports results :eval no-export 194 * org-current-h1-title :org: 196 :ID: ae61e7ed-c9ed-414c-8a5f-12b1702f018e 198 #+name: org-current-h1-title
199 #+begin_src emacs-lisp :results value 200 (org-element-property :title (save-excursion (org-up-heading-safe) (org-element-at-point)))
203 #+RESULTS: org-current-h1-title
206 * get-emacs-version :emacs: 208 :ID: af3d83a1-31bd-41b4-be35-c1f33507fd8d 210 #+name: get-emacs-version
211 #+begin_src elisp :results output 212 (princ (concat (format "%s\n" (emacs-version))
213 (format "Org v%s" (org-version))))
218 :ID: 8119cf43-f2e7-4829-939c-fc4e8531ae6c 221 #+begin_src sh :var src=(org-sbe org-current-h1-title) 222 cd ~/comp/$src && hg log -l 1 --template '{node|short}'
230 :ID: 9602faee-5522-445b-a568-be603e20a978 233 #+begin_src shell :var src=(org-sbe org-current-h1-title) 234 cd ~/comp/$src && hg id -n
242 :ID: 8492f4fb-51a6-4221-8705-a15eb5a50ed4 245 #+begin_src shell :var src=(org-sbe org-current-h1-title) 246 cd ~/comp/$src && hg log -l1 --template "{date|age}"
249 #+RESULTS: hg-log-age
254 :ID: 7b311df4-83a3-489d-89a0-929928bce051 258 emacs -Q --batch --eval "
261 (dolist (file command-line-args-left)
262 (with-current-buffer (find-file-noselect file)
263 (org-babel-tangle))))
267 * make-dot-tree :dot: 269 :ID: 5588f446-2d7a-4261-b829-68effd3778ac 271 #+name: make-dot-tree
272 #+begin_src emacs-lisp :var table=org-headlines-map :results output 273 (mapcar #'(lambda (x)
274 (princ (format "\"%s\" -> \"%s\";\n" (cl-first x) (cl-second x))))
280 :ID: a51c943d-0f01-4c8f-96ec-db28ae7fef26 283 #+begin_src dot :file /tmp/tree.png :cmdline -Kdot -Tpng :var input=make-dot-tree :eval no-export 294 :ID: 9ffd1d10-ffad-486e-9d7d-82422342b9ff 297 #+begin_src emacs-lisp :results silent :eval no-export 298 (unless (slime-connected-p) (slime))
299 (slime-eval '(ql:quickload :user))
300 (slime-eval '(cl:in-package :user))
304 :ID: 334dae41-5c35-48bd-8368-71d79f5e48d8 307 #+begin_src emacs-lisp :results silent :eval no-export 309 (slime-eval '(ql:quickload :std))
310 (slime-eval '(in-package :std-user))
313 * cargo-update-dir :rust: 315 :ID: 67dc87bb-a27b-46e4-a02f-58daac514630 317 #+name: cargo-update-dir
318 #+begin_src sh :var dir=() 319 # update all crates in dir
323 (*/*) dir=./${0%/*};;
327 find "$dir/.." -name Cargo.toml -execdir cargo update \;
329 ** rust-target-triple 331 :ID: 02f96ff2-c607-4889-979c-943203b8ad65 333 #+name: rust-target-triple
335 rustc -vV | sed -n -e 's/^host: //p'