changelog shortlog graph tags branches files raw help

Mercurial > core / changeset: LOB

changeset 430: 8c2f595ad5b4
parent 429: a698dd5de562
child 431: c40d2a41d7ce
author: Richard Westhaver <ellis@rwest.io>
date: Sat, 08 Jun 2024 01:38:26 -0400
files: emacs/babel.org
description: LOB
     1.1--- a/emacs/babel.org	Sat Jun 08 01:12:18 2024 -0400
     1.2+++ b/emacs/babel.org	Sat Jun 08 01:38:26 2024 -0400
     1.3@@ -2,10 +2,247 @@
     1.4 #+author: Richard Westhaver
     1.5 #+description: Core Library of Babel
     1.6 #+setupfile: https://cdn.compiler.company/org/clean.theme
     1.7-#+PROPERTY: header-args :eval never-export
     1.8+#+property: header-args :eval never-export :exports both
     1.9 
    1.10 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
    1.11 collection of code blocks used throughout our Org documents.
    1.12 
    1.13 To load the library use ~C-c C-v i~
    1.14+* os
    1.15+** systemd
    1.16+#+name: systemd-list-units
    1.17+#+begin_src sh :results replace
    1.18+systemctl list-units --state=running | grep -v systemd | awk '{print $1}' | grep service
    1.19+#+end_src
    1.20 
    1.21+* fs
    1.22+** wc-dir-lines
    1.23+#+name: wc-dir-lines
    1.24+#+begin_src shell :var dir="."
    1.25+cd $dir && cat * | wc -l
    1.26+#+end_src
    1.27+
    1.28+** wc-dir-words
    1.29+#+name: wc-dir-words
    1.30+#+begin_src shell :var dir="."
    1.31+cd $dir && cat * | wc -w
    1.32+#+end_src
    1.33+
    1.34+** tokei-dir-lines
    1.35+#+name: tokei-dir-lines
    1.36+#+begin_src shell :var src=(org-sbe org-current-heading-title) :results output replace
    1.37+  cd ~/comp/$src
    1.38+  input=`tokei -C -o json`
    1.39+  echo $input | jq -r '.["Total"] | .code, .comments, .blanks'
    1.40+#+end_src
    1.41+
    1.42+** tokei-dir-langs
    1.43+#+name: tokei-dir-langs
    1.44+#+begin_src shell :var src=(org-sbe org-current-heading-title) :results output replace
    1.45+  cd ~/comp/$src
    1.46+  input=`tokei -C -o json`
    1.47+  echo $input | jq -r '.["Total"].children | keys[]'
    1.48+#+end_src
    1.49+
    1.50+** sum-str-nums
    1.51+#+name: sum-str-nums
    1.52+#+begin_src emacs-lisp :var s=tokei-dir-lines
    1.53+  (let ((tot 0))
    1.54+    (cl-loop
    1.55+     with tot = 0
    1.56+     for i in (split-string s) do
    1.57+     (setf tot (+ tot (string-to-number i)))
    1.58+     finally return tot))
    1.59+#+end_src
    1.60+
    1.61+* org
    1.62+** org-task-tbl
    1.63+#+name: org-task-tbl
    1.64+#+begin_src emacs-lisp
    1.65+  (let* ((ast (org-element-parse-buffer)) ;; built up the abstract syntax tree of the org buffer
    1.66+         item-types ; all occuring item types. It could be that some task has more item types than another.
    1.67+         tasks ; accumulation list for the tasks
    1.68+         current-task ; name of the current task (header of level 1)
    1.69+         task-items) ; items of the current task
    1.70+    (org-element-map ast 'headline
    1.71+      (lambda (hl)
    1.72+        (cl-case (org-element-property :level hl)
    1.73+          (1 ; We assume here that headers of level 1 are tasks.
    1.74+           (when current-task ; registering the old task
    1.75+             (setq tasks (cons (cons current-task (nreverse task-items)) tasks)))
    1.76+           (setq current-task (org-element-property :raw-value hl) ; preparing the new task
    1.77+                 task-items nil))
    1.78+          (2 ; item
    1.79+           (let ((item-type (org-element-property :raw-value hl)))
    1.80+             (setq item-types (cons item-type item-types))
    1.81+             (setq task-items (cons (cons item-type (org-element-property :todo-keyword hl))
    1.82+                                    task-items)))))))
    1.83+    (setq tasks (nreverse (cons (cons current-task (nreverse task-items)) tasks)) ;add the last task
    1.84+          item-types (sort (cl-remove-duplicates (nreverse item-types) :test 'string-equal) ; list of unique item types
    1.85+                           #'string<)) ;;Sorting the items lexicographical. Other criteria could be applied.
    1.86+      ;;;;;;;;;;
    1.87+    ;; generating the output table:
    1.88+    (apply
    1.89+     #'list
    1.90+     (cons "Item" (mapcar #'car tasks)) ; header
    1.91+     'hline
    1.92+     ;; rows:
    1.93+     (mapcar
    1.94+      ;; mapping the items to the todo states associated to the tasks:
    1.95+      (lambda (item-type)
    1.96+        (cons item-type
    1.97+              (mapcar
    1.98+               (lambda (task)
    1.99+                 (let ((todo-status (cdr (assoc-string item-type task))))
   1.100+                   todo-status))
   1.101+               tasks)))
   1.102+      item-types)))
   1.103+#+end_src
   1.104+
   1.105+** org-headlines-map
   1.106+#+name: org-headlines-map
   1.107+#+begin_src elisp
   1.108+  (org-element-map (org-element-parse-buffer 'headline )
   1.109+      'headline
   1.110+    (lambda(hl)
   1.111+      (let ((parent (org-element-property :parent hl )))
   1.112+        (and (eq (org-element-type parent) 'headline)
   1.113+             (list (org-element-property :title parent) (org-element-property :title hl))))))
   1.114+
   1.115+#+end_src
   1.116+
   1.117+** make-info-tbl
   1.118+#+name: meta-make-info-tbl
   1.119+#+header: :var version="alpha-0.1"
   1.120+#+header: :var name=(org-sbe org-current-heading-title)
   1.121+#+header: :var dir="/home/ellis/comp/"
   1.122+#+begin_src emacs-lisp :results table replace :eval no-export 
   1.123+  (let* ((src (concat dir name))
   1.124+         (age (org-sbe hg-log-age ''(dir src)))
   1.125+         (rev (org-sbe hg-rev ''(dir src)))
   1.126+         (num (org-sbe hg-id-num ''(dir src)))
   1.127+         (cc1 (org-sbe "tokei-dir-lines" ''((dir src))))
   1.128+         (cc2 (org-sbe "tokei-dir-langs" ''((dir src))))
   1.129+         (nf (format "[[https://rwest.io/m#%s][%s]]" name name))
   1.130+         (rf (format "[[https://hg.rwest.io/%s/rev/%s][%s:%s]]" name rev num rev))
   1.131+         (gf (format "[[https://github.com/richardwesthaver/%s][github]]" name))
   1.132+         (vf (format "%s, %s" rf gf))
   1.133+         (lsum (org-sbe sum-str-nums ('s 'cc1)))
   1.134+         (l (split-string cc1))
   1.135+         (lang (split-string cc2))
   1.136+         (cf (format "%s = λ:%s #:%s _:%s" lsum (pop l) (pop l) (pop l))))
   1.137+    `(hline
   1.138+      (name ,nf)
   1.139+      (version ,version)
   1.140+      (vc ,vf)
   1.141+      (updated ,age)
   1.142+      (lines ,cf)
   1.143+      (langs ,lang)
   1.144+      hline))
   1.145+  #+end_src
   1.146+
   1.147+** make-includes
   1.148+#+name: meta-make-includes
   1.149+#+begin_src emacs-lisp :var i=()
   1.150+`((includes ,i))
   1.151+#+end_src
   1.152+
   1.153+** make-files-tbl
   1.154+#+name: meta-ls-files
   1.155+#+begin_src sh :results silent :var a=(expand-file-name "~/comp") b=(org-sbe org-current-heading-title)
   1.156+  ls -lh $a/$b --time-style=long-iso \
   1.157+    |awk '{if (NR!=1) print $8, $5, $6"-"$7}' \
   1.158+    |awk 'BEGIN{print "file size updated"}{print $0}'
   1.159+#+end_src
   1.160+
   1.161+#+name: meta-make-files-tbl
   1.162+#+begin_src python :var tab=meta-ls-files() :results table :colnames yes :hlines yes :exports results :eval no-export
   1.163+return tab
   1.164+#+end_src
   1.165+
   1.166+** org-current-h1-title
   1.167+#+name: org-current-heading-title
   1.168+#+begin_src emacs-lisp :results value
   1.169+  (org-element-property :title (save-excursion (org-up-heading-safe) (org-element-at-point)))
   1.170+#+end_src
   1.171+
   1.172+* emacs
   1.173+** get-emacs-version
   1.174+#+name: get-emacs-version
   1.175+#+begin_src elisp :results output
   1.176+  (princ (concat (format "%s\n" (emacs-version))
   1.177+		 (format "Org v%s" (org-version))))
   1.178+#+end_src
   1.179+
   1.180+* vc
   1.181+** hg-rev
   1.182+#+name: hg-rev
   1.183+#+begin_src sh :var src=(org-sbe org-current-heading-title)
   1.184+cd ~/comp/$src && hg log -l 1 --template '{node|short}'
   1.185+#+end_src
   1.186+
   1.187+** hg-id-num
   1.188+#+name: hg-id-num
   1.189+#+begin_src shell :var src=(org-sbe org-current-heading-title)
   1.190+cd ~/comp/$src && hg id -n
   1.191+#+end_src
   1.192+
   1.193+** hg-log-age
   1.194+#+name: hg-log-age
   1.195+#+begin_src shell :results output :var src=(org-sbe org-current-heading-title)
   1.196+  cd ~/comp/$src && hg log -l1 --template "{date|age}"
   1.197+#+end_src
   1.198+
   1.199+* sh
   1.200+** sh-ob-tangle
   1.201+#+name: sh-ob-tangle
   1.202+#+begin_src sh
   1.203+  emacs -Q --batch --eval "
   1.204+      (progn
   1.205+	(require 'ob-tangle)
   1.206+	(dolist (file command-line-args-left)
   1.207+	  (with-current-buffer (find-file-noselect file)
   1.208+	    (org-babel-tangle))))
   1.209+    " "$@"
   1.210+#+end_src
   1.211+
   1.212+* dot
   1.213+** make-dot-tree
   1.214+#+name: make-dot-tree
   1.215+#+begin_src emacs-lisp :var table=org-headlines-map :results output
   1.216+  (mapcar #'(lambda (x)
   1.217+		(princ (format "\"%s\" -> \"%s\";\n" (cl-first x) (cl-second x))))
   1.218+	  table)
   1.219+#+end_src
   1.220+
   1.221+** gen-dot-tree
   1.222+#+name: gen-dot-tree
   1.223+#+begin_src dot :file /tmp/tree.png :cmdline -Kdot -Tpng :var input=make-dot-tree
   1.224+digraph {
   1.225+   rankdir=TB;
   1.226+   splines=true;
   1.227+   node [shape=box];
   1.228+   $input
   1.229+  }
   1.230+#+end_src
   1.231+
   1.232+
   1.233+* rust
   1.234+** cargo-update-dir
   1.235+#+name: cargo-update-dir
   1.236+#+begin_src sh :var dir=()
   1.237+# update all crates in dir
   1.238+set -eu
   1.239+case $0 in
   1.240+   (/*) dir=${0%/*}/;;
   1.241+   (*/*) dir=./${0%/*};;
   1.242+   (*) dir=.;;
   1.243+esac
   1.244+
   1.245+find "$dir/.." -name Cargo.toml -execdir cargo update \;
   1.246+#+end_src
   1.247+** rust-target-triple
   1.248+ #+name: rust-target-triple
   1.249+ #+begin_src shell
   1.250+ rustc -vV | sed -n -e 's/^host: //p'
   1.251+ #+end_src