2 #+author: Richard Westhaver
3 #+description: Core Library of Babel
4 #+setupfile: ../clean.theme
5 #+property: header-args :exports both :eval no-export
6 Welcome to the CC
[[https://www.gnu.org/software/emacs/manual/html_node/org/Library-of-Babel.html][Library of Babel]]. This file contains a collection of
7 code blocks which may be used by authors throughout our public
10 This library depends on the
[[vc:comp/core][core]] being properly installed and the
11 =core/emacs= init system loaded in emacs. See
[[vc:comp/home][home]] for details.
13 To load the library itself use
~C-c C-v i~ while visiting this file in
14 an org-mode buffer or
=org-babel-lob-ingest= from elisp.
16 * systemd-list-units :os: 18 :ID: 3b23c98d-a286-4988-846d-2dab3d25803d 20 #+name: systemd-list-units
21 #+begin_src sh :results replace 22 systemctl list-units --state=running | grep -v systemd | awk '{print $1}' | grep service
27 :ID: 70008815-9634-48ca-b672-b8dcf2a44074 30 #+begin_src shell :var dir="." 31 cd $dir && cat * | wc -l
36 :ID: be755790-f367-4654-87e5-cd2927bfef45 39 #+begin_src shell :var dir="." 40 cd $dir && cat * | wc -w
43 * tokei-dir-lines :fs: 45 :ID: 670e9855-f8d2-43eb-86af-3ef7292f90b9 47 #+name: tokei-dir-lines
48 #+begin_src shell :var src="" :results output replace 50 input=`tokei -C -o json`
51 echo $input | jq -r '.["Total"] | .code, .comments, .blanks'
54 * tokei-dir-langs :fs: 56 :ID: ff9682f0-bb64-427f-a87d-e0c655f9fdc9 58 #+name: tokei-dir-langs
59 #+begin_src shell :var src="org" :results output replace 61 input=`tokei -C -o json`
62 echo $input | jq -r '.["Total"].children | keys[]'
67 :ID: d3c4ac69-337f-430b-a4db-760504f099ea 70 #+begin_src emacs-lisp :var s=tokei-dir-lines() 74 for i in (split-string s) do
75 (setf tot (+ tot (string-to-number i)))
81 :ID: 0ee5b499-6f0b-4e15-8c6e-4876e90c20a9 84 #+begin_src emacs-lisp 85 (let* ((ast (org-element-parse-buffer)) ;; built up the abstract syntax tree of the org buffer
86 item-types ; all occuring item types. It could be that some task has more item types than another.
87 tasks ; accumulation list for the tasks
88 current-task ; name of the current task (header of level 1)
89 task-items) ; items of the current task
90 (org-element-map ast 'headline
92 (cl-case (org-element-property :level hl)
93 (1 ; We assume here that headers of level 1 are tasks.
94 (when current-task ; registering the old task
95 (setq tasks (cons (cons current-task (nreverse task-items)) tasks)))
96 (setq current-task (org-element-property :raw-value hl) ; preparing the new task
99 (let ((item-type (org-element-property :raw-value hl)))
100 (setq item-types (cons item-type item-types))
101 (setq task-items (cons (cons item-type (org-element-property :todo-keyword hl))
103 (setq tasks (nreverse (cons (cons current-task (nreverse task-items)) tasks)) ;add the last task
104 item-types (sort (cl-remove-duplicates (nreverse item-types) :test 'string-equal) ; list of unique item types
105 #'string<)) ;;Sorting the items lexicographical. Other criteria could be applied.
107 ;; generating the output table:
110 (cons "Item" (mapcar #'car tasks)) ; header
114 ;; mapping the items to the todo states associated to the tasks:
119 (let ((todo-status (cdr (assoc-string item-type task))))
125 * org-headlines-map :org: 127 :ID: 05955228-ca76-48bc-b769-f648b4310a9c 129 #+name: org-headlines-map
131 (org-element-map (org-element-parse-buffer 'headline )
134 (let ((parent (org-element-property :parent hl )))
135 (and (eq (org-element-type parent) 'headline)
136 (list (org-element-property :title parent) (org-element-property :title hl))))))
140 * make-info-tbl :org:fs:vc: 142 :ID: d5ba2f3d-fc2d-4db6-8bbd-7ca440ff0e8c 144 #+name: make-info-tbl
145 #+header: :var version="0.1.0"
146 #+header: :var name="org"
147 #+header: :var dir="/home/ellis/comp/"
148 #+begin_src emacs-lisp :results table replace 149 (let* ((src (concat dir name))
150 (age (org-sbe "hg-log-age" ''(src name)))
151 (rev (org-sbe "hg-rev" ''(src name)))
152 (num (org-sbe "hg-id-num" ''(src name)))
153 (cc1 (org-sbe "tokei-dir-lines" ''(src name)))
154 (cc2 (org-sbe "tokei-dir-langs" ''(src name)))
155 (nf (format "[[comp:docs/%s][%s]]" name name))
156 (rf (format "[[vc:comp/%s][%s:%s]]" name num rev))
157 ;; (gf (format "[[https://github.com/richardwesthaver/%s][github]]" name))
158 (vf (format "%s" rf))
159 (lsum (org-sbe sum-str-nums ('s 'cc1)))
160 (l (split-string cc1))
161 (lang (split-string cc2))
162 (cf (format "%s = λ:%s #:%s _:%s" lsum (pop l) (pop l) (pop l))))
173 #
+RESULTS: make-info-tbl 174 |---------+----------------------------|
175 | name | [[https://compiler.company/docs/org][org]] | 177 | vc | [[https://vc.compiler.company/comp/org][44+:2b6f731f3684]] | 179 | lines | 14812 = λ:13917 #:45 _:850 | 180 | langs | (Html Org Svg) | 181 |---------+----------------------------| 183 * make-files-tbl :org:fs: 185 :ID: e2ff9dcf-8340-48b8-a1a6-e0036cbcc495 188 #+begin_src sh :results silent :var dir=(expand-file-name "~/comp") name="org" 189 ls -lh $dir/$name --time-style=long-iso \
190 |awk '{if (NR!=1) print $8, $5, $6"-"$7}' \
191 |awk 'BEGIN{print "file size updated"}{print $0}'
194 #+name: make-files-tbl
195 #+begin_src python :var tab=ls-files() :results table :colnames yes :hlines yes :exports results :eval no-export 201 :ID: 4c824478-0eaa-49b9-905f-2c9a6d4220eb 203 #+begin_src sh :results table replace 206 echo "$i|" | sed '0,/=/s//|/'
212 :ID: 8c1ecc68-ca25-4e72-81d8-415a43e59ae4 215 #+begin_src elisp :results output :var key="HOME" 222 * org-current-h1-title :org: 224 :ID: ae61e7ed-c9ed-414c-8a5f-12b1702f018e 226 #+name: org-current-h1-title
227 #+begin_src emacs-lisp :results value 228 (org-element-property :title (save-excursion (org-up-heading-safe) (org-element-at-point)))
231 #+RESULTS: org-current-h1-title
232 : org-current-h1-title
234 * get-emacs-version :emacs: 236 :ID: af3d83a1-31bd-41b4-be35-c1f33507fd8d 238 #+name: get-emacs-version
239 #+begin_src elisp :results output 240 (princ (concat (format "%s\n" (emacs-version))
241 (format "Org v%s" (org-version))))
246 :ID: 8119cf43-f2e7-4829-939c-fc4e8531ae6c 249 #+begin_src sh :var src="org" 250 cd ~/comp/$src && hg log -l 1 --template '{node|short}'
258 :ID: 9602faee-5522-445b-a568-be603e20a978 261 #+begin_src shell :var src="org" 262 cd ~/comp/$src && hg id -n
270 :ID: 8492f4fb-51a6-4221-8705-a15eb5a50ed4 273 #+begin_src shell :var src="." 274 cd ~/comp/$src && hg log -l1 --template "{date|age}"
277 #+RESULTS: hg-log-age
281 :ID: 7b311df4-83a3-489d-89a0-929928bce051 285 emacs -Q --batch --eval "
288 (dolist (file command-line-args-left)
289 (with-current-buffer (find-file-noselect file)
290 (org-babel-tangle))))
294 * make-dot-tree :dot: 296 :ID: 5588f446-2d7a-4261-b829-68effd3778ac 298 #+name: make-dot-tree
299 #+begin_src emacs-lisp :var table=org-headlines-map :results output 300 (mapcar #'(lambda (x)
301 (princ (format "\"%s\" -> \"%s\";\n" (cl-first x) (cl-second x))))
307 :ID: a51c943d-0f01-4c8f-96ec-db28ae7fef26 310 #+begin_src dot :file /tmp/tree.png :cmdline -Kdot -Tpng :var input=make-dot-tree :eval no-export 319 #+RESULTS: gen-dot-tree
320 [[file:/tmp/tree.png]] 324 :ID: 9ffd1d10-ffad-486e-9d7d-82422342b9ff 327 #+begin_src emacs-lisp :results silent :eval no-export 328 (unless (slime-connected-p) (slime))
329 (slime-eval '(ql:quickload :user))
330 (slime-eval '(cl:in-package :user))
334 :ID: 334dae41-5c35-48bd-8368-71d79f5e48d8 337 #+begin_src emacs-lisp :results silent :eval no-export 339 (slime-eval '(ql:quickload :std))
340 (slime-eval '(in-package :std-user))
343 * cargo-update-dir :rust: 345 :ID: 67dc87bb-a27b-46e4-a02f-58daac514630 347 #+name: cargo-update-dir
348 #+begin_src sh :var dir=() 349 # update all crates in dir
353 (*/*) dir=./${0%/*};;
357 find "$dir/.." -name Cargo.toml -execdir cargo update \;
359 * rust-target-triple :rust: 361 :ID: 02f96ff2-c607-4889-979c-943203b8ad65 363 #+name: rust-target-triple
365 rustc -vV | sed -n -e 's/^host: //p'