changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > org > meta / babel.org

changeset 3: 04bd01442fcd
parent: a4e233714062
child: 6538a100c792
author: Richard Westhaver <ellis@rwest.io>
date: Wed, 14 Aug 2024 22:19:38 -0400
permissions: -rw-r--r--
description: midweek updates
1 #+title: babel
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.
8 
9 To load the library use ~C-c C-v i~.
10 
11 * systemd-list-units :os:
12 :PROPERTIES:
13 :ID: 3b23c98d-a286-4988-846d-2dab3d25803d
14 :END:
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
18 #+end_src
19 
20 * wc-dir-lines :fs:
21 :PROPERTIES:
22 :ID: 70008815-9634-48ca-b672-b8dcf2a44074
23 :END:
24 #+name: wc-dir-lines
25 #+begin_src shell :var dir="."
26 cd $dir && cat * | wc -l
27 #+end_src
28 
29 * wc-dir-words :fs:
30 :PROPERTIES:
31 :ID: be755790-f367-4654-87e5-cd2927bfef45
32 :END:
33 #+name: wc-dir-words
34 #+begin_src shell :var dir="."
35 cd $dir && cat * | wc -w
36 #+end_src
37 
38 * tokei-dir-lines :fs:
39 :PROPERTIES:
40 :ID: 670e9855-f8d2-43eb-86af-3ef7292f90b9
41 :END:
42 #+name: tokei-dir-lines
43 #+begin_src shell :var src=(org-sbe org-current-h1-title) :results output replace
44  cd ~/comp/$src
45  input=`tokei -C -o json`
46  echo $input | jq -r '.["Total"] | .code, .comments, .blanks'
47 #+end_src
48 
49 * tokei-dir-langs :fs:
50 :PROPERTIES:
51 :ID: ff9682f0-bb64-427f-a87d-e0c655f9fdc9
52 :END:
53 #+name: tokei-dir-langs
54 #+begin_src shell :var src=(org-sbe org-current-h1-title) :results output replace
55  cd ~/comp/$src
56  input=`tokei -C -o json`
57  echo $input | jq -r '.["Total"].children | keys[]'
58 #+end_src
59 
60 * sum-str-nums :util:
61 :PROPERTIES:
62 :ID: d3c4ac69-337f-430b-a4db-760504f099ea
63 :END:
64 #+name: sum-str-nums
65 #+begin_src emacs-lisp :var s=tokei-dir-lines
66  (let ((tot 0))
67  (cl-loop
68  with tot = 0
69  for i in (split-string s) do
70  (setf tot (+ tot (string-to-number i)))
71  finally return tot))
72 #+end_src
73 
74 * org-task-tbl :org:
75 :PROPERTIES:
76 :ID: 0ee5b499-6f0b-4e15-8c6e-4876e90c20a9
77 :END:
78 #+name: org-task-tbl
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
86  (lambda (hl)
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
92  task-items nil))
93  (2 ; item
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))
97  task-items)))))))
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.
101  ;;;;;;;;;;
102  ;; generating the output table:
103  (apply
104  #'list
105  (cons "Item" (mapcar #'car tasks)) ; header
106  'hline
107  ;; rows:
108  (mapcar
109  ;; mapping the items to the todo states associated to the tasks:
110  (lambda (item-type)
111  (cons item-type
112  (mapcar
113  (lambda (task)
114  (let ((todo-status (cdr (assoc-string item-type task))))
115  todo-status))
116  tasks)))
117  item-types)))
118 #+end_src
119 
120 * org-headlines-map :org:
121 :PROPERTIES:
122 :ID: 05955228-ca76-48bc-b769-f648b4310a9c
123 :END:
124 #+name: org-headlines-map
125 #+begin_src elisp
126  (org-element-map (org-element-parse-buffer 'headline )
127  'headline
128  (lambda(hl)
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))))))
132 
133 #+end_src
134 
135 * make-info-tbl :org:fs:vc:
136 :PROPERTIES:
137 :ID: d5ba2f3d-fc2d-4db6-8bbd-7ca440ff0e8c
138 :END:
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))))
158  `(hline
159  (name ,nf)
160  (version ,version)
161  (vc ,vf)
162  (updated ,age)
163  (lines ,cf)
164  (langs ,lang)
165  hline))
166  #+end_src
167 
168  #+RESULTS: make-info-tbl
169  |---------+----------------------------|
170  | name | [[https://compiler.company/docs/org][org]] |
171  | version | 0.1.0 |
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  |---------+----------------------------|
177 
178 * make-files-tbl :org:fs:
179 :PROPERTIES:
180 :ID: e2ff9dcf-8340-48b8-a1a6-e0036cbcc495
181 :END:
182 #+name: ls-files
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}'
187 #+end_src
188 
189 #+name: make-files-tbl
190 #+begin_src python :var tab=ls-files() :results table :colnames yes :hlines yes :exports results :eval no-export
191 return tab
192 #+end_src
193 
194 * org-current-h1-title :org:
195 :PROPERTIES:
196 :ID: ae61e7ed-c9ed-414c-8a5f-12b1702f018e
197 :END:
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)))
201 #+end_src
202 
203 #+RESULTS: org-current-h1-title
204 : org
205 
206 * get-emacs-version :emacs:
207 :PROPERTIES:
208 :ID: af3d83a1-31bd-41b4-be35-c1f33507fd8d
209 :END:
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))))
214 #+end_src
215 
216 * hg-rev :vc:
217 :PROPERTIES:
218 :ID: 8119cf43-f2e7-4829-939c-fc4e8531ae6c
219 :END:
220 #+name: hg-rev
221 #+begin_src sh :var src=(org-sbe org-current-h1-title)
222 cd ~/comp/$src && hg log -l 1 --template '{node|short}'
223 #+end_src
224 
225 #+RESULTS: hg-rev
226 : 4de12ceca1c7
227 
228 * hg-id-num :vc:
229 :PROPERTIES:
230 :ID: 9602faee-5522-445b-a568-be603e20a978
231 :END:
232 #+name: hg-id-num
233 #+begin_src shell :var src=(org-sbe org-current-h1-title)
234 cd ~/comp/$src && hg id -n
235 #+end_src
236 
237 #+RESULTS: hg-id-num
238 : 36+
239 
240 * hg-log-age :vc:
241 :PROPERTIES:
242 :ID: 8492f4fb-51a6-4221-8705-a15eb5a50ed4
243 :END:
244 #+name: hg-log-age
245 #+begin_src shell :var src=(org-sbe org-current-h1-title)
246  cd ~/comp/$src && hg log -l1 --template "{date|age}"
247 #+end_src
248 
249 #+RESULTS: hg-log-age
250 : 4 days ago
251 
252 * sh-ob-tangle :org:
253 :PROPERTIES:
254 :ID: 7b311df4-83a3-489d-89a0-929928bce051
255 :END:
256 #+name: sh-ob-tangle
257 #+begin_src sh
258  emacs -Q --batch --eval "
259  (progn
260  (require 'ob-tangle)
261  (dolist (file command-line-args-left)
262  (with-current-buffer (find-file-noselect file)
263  (org-babel-tangle))))
264  " "$@"
265 #+end_src
266 
267 * make-dot-tree :dot:
268 :PROPERTIES:
269 :ID: 5588f446-2d7a-4261-b829-68effd3778ac
270 :END:
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))))
275  table)
276 #+end_src
277 
278 * gen-dot-tree :dot:
279 :PROPERTIES:
280 :ID: a51c943d-0f01-4c8f-96ec-db28ae7fef26
281 :END:
282 #+name: gen-dot-tree
283 #+begin_src dot :file /tmp/tree.png :cmdline -Kdot -Tpng :var input=make-dot-tree :eval no-export
284 digraph {
285  rankdir=TB;
286  splines=true;
287  node [shape=box];
288  $input
289  }
290 #+end_src
291 
292 * user-slime :lisp:
293 :PROPERTIES:
294 :ID: 9ffd1d10-ffad-486e-9d7d-82422342b9ff
295 :END:
296 #+name: user-slime
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))
301 #+end_src
302 * std-slime :lisp:
303 :PROPERTIES:
304 :ID: 334dae41-5c35-48bd-8368-71d79f5e48d8
305 :END:
306 #+name: std-slime
307 #+begin_src emacs-lisp :results silent :eval no-export
308  (slime)
309  (slime-eval '(ql:quickload :std))
310  (slime-eval '(in-package :std-user))
311 #+end_src
312 
313 * cargo-update-dir :rust:
314 :PROPERTIES:
315 :ID: 67dc87bb-a27b-46e4-a02f-58daac514630
316 :END:
317 #+name: cargo-update-dir
318 #+begin_src sh :var dir=()
319 # update all crates in dir
320 set -eu
321 case $0 in
322  (/*) dir=${0%/*}/;;
323  (*/*) dir=./${0%/*};;
324  (*) dir=.;;
325 esac
326 
327 find "$dir/.." -name Cargo.toml -execdir cargo update \;
328 #+end_src
329 ** rust-target-triple
330 :PROPERTIES:
331 :ID: 02f96ff2-c607-4889-979c-943203b8ad65
332 :END:
333  #+name: rust-target-triple
334  #+begin_src shell
335  rustc -vV | sed -n -e 's/^host: //p'
336  #+end_src