changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > org > meta / babel.org

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