2 #+author: Richard Westhaver
3 #+description: Core Library of Babel
4 #+setupfile: ../clean.theme
5 #+property: header-args :exports both :eval never
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.
17 #+begin_src emacs-lisp :var input="" 22 #+begin_src emacs-lisp :var file="" :var format="" 23 (if (string= format "csv")
25 (org-table-import (expand-file-name file) nil)
28 (insert-file-contents (expand-file-name file))
33 #+begin_src emacs-lisp :var data="" :var file="" :var ext='() 34 (cl-flet ((echo (r) (if (stringp r) r (format "%S" r))))
36 (case (and (listp data)
37 (or ext (intern (file-name-extension file))))
38 ('tsv (insert (orgtbl-to-tsv data '(:fmt echo))))
39 ('csv (insert (orgtbl-to-csv data '(:fmt echo))))
40 (t (org-babel-insert-result data)))))
45 #+begin_src emacs-lisp :var file='() :var url='() 49 (org-babel-with-temp-filebuffer file
50 (goto-char (point-min))
56 (goto-char (point-min))
62 #+begin_src emacs-lisp :var headline="" :var file='() 64 (when file (get-file-buffer file))
65 (org-open-link-from-string (org-make-link-string headline))
67 (org-narrow-to-subtree)
72 #+begin_src emacs-lisp :var table="" 73 (apply #'mapcar #'list (list table))
75 * all-to-string :table: 77 #+begin_src emacs-lisp :var tbl='() 78 (defun all-to-string (tbl)
80 (mapcar #'all-to-string tbl)
86 * systemd-list-units :os: 88 :ID: 3b23c98d-a286-4988-846d-2dab3d25803d 90 #+name: systemd-list-units
91 #+begin_src sh :results replace 92 systemctl list-units --state=running | grep -v systemd | awk '{print $1}' | grep service
97 :ID: 70008815-9634-48ca-b672-b8dcf2a44074 100 #+begin_src shell :var dir="." 101 cd $dir && cat * | wc -l
106 :ID: be755790-f367-4654-87e5-cd2927bfef45 109 #+begin_src shell :var dir="." 110 cd $dir && cat * | wc -w
113 * tokei-dir-lines :fs: 115 :ID: 670e9855-f8d2-43eb-86af-3ef7292f90b9 117 #+name: tokei-dir-lines
118 #+begin_src shell :var src="" :results output replace 120 input=`tokei -C -o json`
121 echo $input | jq -r '.["Total"] | .code, .comments, .blanks'
124 * tokei-dir-langs :fs: 126 :ID: ff9682f0-bb64-427f-a87d-e0c655f9fdc9 128 #+name: tokei-dir-langs
129 #+begin_src shell :var src="org" :results output replace 131 input=`tokei -C -o json`
132 echo $input | jq -r '.["Total"].children | keys[]'
135 * sum-str-nums :util: 137 :ID: d3c4ac69-337f-430b-a4db-760504f099ea 140 #+begin_src emacs-lisp :var s=tokei-dir-lines() 144 for i in (split-string s) do
145 (setf tot (+ tot (string-to-number i)))
151 :ID: 0ee5b499-6f0b-4e15-8c6e-4876e90c20a9 154 #+begin_src emacs-lisp 155 (let* ((ast (org-element-parse-buffer)) ;; built up the abstract syntax tree of the org buffer
156 item-types ; all occuring item types. It could be that some task has more item types than another.
157 tasks ; accumulation list for the tasks
158 current-task ; name of the current task (header of level 1)
159 task-items) ; items of the current task
160 (org-element-map ast 'headline
162 (cl-case (org-element-property :level hl)
163 (1 ; We assume here that headers of level 1 are tasks.
164 (when current-task ; registering the old task
165 (setq tasks (cons (cons current-task (nreverse task-items)) tasks)))
166 (setq current-task (org-element-property :raw-value hl) ; preparing the new task
169 (let ((item-type (org-element-property :raw-value hl)))
170 (setq item-types (cons item-type item-types))
171 (setq task-items (cons (cons item-type (org-element-property :todo-keyword hl))
173 (setq tasks (nreverse (cons (cons current-task (nreverse task-items)) tasks)) ;add the last task
174 item-types (sort (cl-remove-duplicates (nreverse item-types) :test 'string-equal) ; list of unique item types
175 #'string<)) ;;Sorting the items lexicographical. Other criteria could be applied.
177 ;; generating the output table:
180 (cons "Item" (mapcar #'car tasks)) ; header
184 ;; mapping the items to the todo states associated to the tasks:
189 (let ((todo-status (cdr (assoc-string item-type task))))
195 * org-headlines-map :org: 197 :ID: 05955228-ca76-48bc-b769-f648b4310a9c 199 #+name: org-headlines-map
201 (org-element-map (org-element-parse-buffer 'headline)
204 (let ((parent (org-element-property :parent hl)))
205 (and (eq (org-element-type parent) 'headline)
206 (list (org-element-property :title parent) (org-element-property :title hl))))))
210 * make-info-tbl :org:fs:vc: 212 :ID: d5ba2f3d-fc2d-4db6-8bbd-7ca440ff0e8c 214 #+name: make-info-tbl
215 #+header: :var version="0.1.0"
216 #+header: :var name="org"
217 #+header: :var dir="/home/ellis/comp/"
218 #+begin_src emacs-lisp :results table replace 219 (let* ((src (concat dir name))
220 (age (org-sbe "hg-log-age" ''(src name)))
221 (rev (org-sbe "hg-rev" ''(src name)))
222 (num (org-sbe "hg-id-num" ''(src name)))
223 (cc1 (org-sbe "tokei-dir-lines" ''(src name)))
224 (cc2 (org-sbe "tokei-dir-langs" ''(src name)))
225 (nf (format "[[comp:docs/%s][%s]]" name name))
226 (rf (format "[[vc:comp/%s][%s:%s]]" name num rev))
227 ;; (gf (format "[[https://github.com/richardwesthaver/%s][github]]" name))
228 (vf (format "%s" rf))
229 (lsum (org-sbe sum-str-nums ('s 'cc1)))
230 (l (split-string cc1))
231 (lang (split-string cc2))
232 (cf (format "%s = λ:%s #:%s _:%s" lsum (pop l) (pop l) (pop l))))
243 #
+RESULTS: make-info-tbl 244 |---------+----------------------------|
245 | name | [[https://compiler.company/docs/org][org]] | 247 | vc | [[https://vc.compiler.company/comp/org][44+:2b6f731f3684]] | 249 | lines | 14812 = λ:13917 #:45 _:850 | 250 | langs | (Html Org Svg) | 251 |---------+----------------------------| 253 * make-files-tbl :org:fs: 255 :ID: e2ff9dcf-8340-48b8-a1a6-e0036cbcc495 258 #+begin_src sh :results silent :var dir=(expand-file-name "~/comp") name="org" 259 ls -lh $dir/$name --time-style=long-iso \
260 |awk '{if (NR!=1) print $8, $5, $6"-"$7}' \
261 |awk 'BEGIN{print "file size updated"}{print $0}'
264 #+name: make-files-tbl
265 #+begin_src python :var tab=ls-files() :results table :colnames yes :hlines yes :exports results :eval no-export 271 :ID: 4c824478-0eaa-49b9-905f-2c9a6d4220eb 273 #+begin_src sh :results table replace 276 echo "$i|" | sed '0,/=/s//|/'
282 :ID: 8c1ecc68-ca25-4e72-81d8-415a43e59ae4 285 #+begin_src elisp :results output :var key="HOME" 292 * org-current-h1-title :org: 294 :ID: ae61e7ed-c9ed-414c-8a5f-12b1702f018e 296 #+name: org-current-h1-title
297 #+begin_src emacs-lisp :results value 298 (org-element-property :title (save-excursion (org-up-heading-safe) (org-element-at-point)))
301 #+RESULTS: org-current-h1-title
302 : org-current-h1-title
304 * get-emacs-version :emacs: 306 :ID: af3d83a1-31bd-41b4-be35-c1f33507fd8d 308 #+name: get-emacs-version
309 #+begin_src elisp :results output 310 (princ (concat (format "%s\n" (emacs-version))
311 (format "Org v%s" (org-version))))
316 #+header: :var limit=-1
317 #+header: :var buf=(buffer-name (current-buffer))
318 #+begin_src emacs-lisp 319 ;; Most of this code is copied from vc.el vc-print-log
321 (when (vc-find-backend-function
322 (vc-backend (buffer-file-name (get-buffer buf))) 'print-log)
327 (with-current-buffer (get-buffer buf)
328 (setq vc-fileset (vc-deduce-fileset t)) ; FIXME: Why t? --Stef
329 (setq backend (car vc-fileset))
330 (setq files (cadr vc-fileset)))
332 (let ((status (vc-call-backend
333 backend 'print-log files (current-buffer))))
334 (when (and (processp status) ; Make sure status is a process
335 (= 0 (process-exit-status status))) ; which has not terminated
336 (while (not (eq 'exit (process-status status)))
342 :ID: 8119cf43-f2e7-4829-939c-fc4e8531ae6c 345 #+begin_src sh :var src="org" 346 cd ~/comp/$src && hg log -l 1 --template '{node|short}'
354 :ID: 9602faee-5522-445b-a568-be603e20a978 357 #+begin_src shell :var src="org" 358 cd ~/comp/$src && hg id -n
366 :ID: 8492f4fb-51a6-4221-8705-a15eb5a50ed4 369 #+begin_src shell :var src="." 370 cd ~/comp/$src && hg log -l1 --template "{date|age}"
373 #+RESULTS: hg-log-age
377 :ID: 7b311df4-83a3-489d-89a0-929928bce051 381 emacs -Q --batch --eval "
384 (dolist (file command-line-args-left)
385 (with-current-buffer (find-file-noselect file)
386 (org-babel-tangle))))
390 * make-dot-tree :dot: 392 :ID: 5588f446-2d7a-4261-b829-68effd3778ac 394 #+name: make-dot-tree
395 #+begin_src emacs-lisp :var table=org-headlines-map() :results output 396 (mapcar #'(lambda (x)
397 (princ (format "\"%s\" -> \"%s\";\n" (cl-first x) (cl-second x))))
403 :ID: a51c943d-0f01-4c8f-96ec-db28ae7fef26 406 #+begin_src dot :file /tmp/tree.png :cmdline -Kdot -Tpng :var input=make-dot-tree() :eval no-export 415 #+RESULTS: gen-dot-tree
416 [[file:/tmp/tree.png]] 420 :ID: 9ffd1d10-ffad-486e-9d7d-82422342b9ff 423 #+begin_src emacs-lisp :results silent :eval no-export 424 (unless (slime-connected-p) (slime))
425 (slime-eval '(ql:quickload :user))
426 (slime-eval '(cl:in-package :user))
430 :ID: 334dae41-5c35-48bd-8368-71d79f5e48d8 433 #+begin_src emacs-lisp :results silent :eval no-export 435 (slime-eval '(ql:quickload :std))
436 (slime-eval '(in-package :std-user))
439 * cargo-update-dir :rust: 441 :ID: 67dc87bb-a27b-46e4-a02f-58daac514630 443 #+name: cargo-update-dir
444 #+begin_src sh :var dir=() 445 # update all crates in dir
449 (*/*) dir=./${0%/*};;
453 find "$dir/.." -name Cargo.toml -execdir cargo update \;
455 * rust-target-triple :rust: 457 :ID: 02f96ff2-c607-4889-979c-943203b8ad65 459 #
+name: rust-target-triple 461 rustc -vV | sed -n -e 's/^host: //p' 463 * post-align-table :table: 464 #+NAME: post-align-tables 465 #+header: :var text="|5|22222|\n|0||\n|12|45|\n|---\n|||\n#+TBLFM:@>$1=vsum(@1..@-1)\n\n|1|22222|\n|0||\n|12|45|\n" 466 #+BEGIN_SRC emacs-lisp :results value :exports both 469 (cl-assert text nil "PostAlignTables received nil instead of text ") 471 (beginning-of-buffer) 474 (search-forward-regexp org-table-any-line-regexp nil t) 476 (org-table-recalculate 'iterate) 477 (goto-char (org-table-end))) 480 * insert-table-from-file :table:fs: 481 #+NAME: insert-table-from-file 482 #+HEADER: :var tname="table" fname="/tmp/tbl.org" newcaption="" newattr="" newname="" 483 #+BEGIN_SRC elisp :results output drawer 484 (let* ((klist (cl-remove-if (lambda (x) (equal (cadr x) "")) 485 `(("ATTR_LATEX" ,newattr) ("CAPTION" ,newcaption) ("NAME" ,newname)))) 489 (insert-file-contents fname) 490 (goto-char (point-min)) 491 (unless (re-search-forward 492 (concat "^[ \t]*#\\+\\(tbl\\)?name:[ \t]
*" 493 (regexp-quote tname) "[ \t]*$")
495 (user-error "Can't find table named %s in file" tname fname))
497 (let ((tstart (match-beginning 0))
499 (while (looking-at "^[ \t]
*#\\+\\([^:]+\\): *\\(.
*\\)") 500 (add-to-list 'klist `(,(upcase (match-string 1)) ,(match-string 2))) 501 (delete-region (point) (line-end-position)) 503 (unless (looking-at org-table-line-regexp) 505 (user-error "no table at location of %s, Looking-at: '%s'" tname (match-string 0)))
506 (goto-char (org-table-end))
507 (while (looking-at-p "^[ \t]
*#\\+TBLFM:") 509 (buffer-substring tstart (point)))))) 510 (setq klist (nreverse klist)) ;; reverse for giving priority to new user settings 511 (dolist (elem '("NAME" "CAPTION" "ATTR_LATEX")) 512 (when (assoc elem klist) 513 (princ (format "#+%s: %s\n" elem (cadr (assoc elem klist)))))) 516 * filter-table :table:
518 #+HEADER: :var tbl="" col=0 vals=""
519 #+BEGIN_SRC elisp :results value :colnames y 520 (let ((lst (split-string vals)))
521 (concatenate 'list (loop for row in tbl
522 if (member (let ((field (nth col row)))
524 (number-to-string field)
526 collect row into newtbl
527 ;; else do (princ (format "%s: %s\n" (nth col row) lst))
528 finally return newtbl)))
530 * filter-table-re :table:rx: 531 #+NAME: filter-table-re
532 #+HEADER: :var tbl="" col=0 vals=".*"
533 #+BEGIN_SRC elisp :results value :colnames y 534 (let ((lst (split-string vals)))
535 (concatenate 'list (loop for row in tbl
536 if (let* ((rawfield (nth col row))
537 (field (if (numberp rawfield)
538 (number-to-string rawfield)
540 (loop for regx in lst
541 when(string-match-p regx field) return 't
543 collect row into newtbl
544 ;; else do (princ (format "%s: %s\n" (nth col row) lst))
545 finally return newtbl)))
547 * group-table :table: 549 #+HEADER: :var tbl="" grp="Name" op="sum" rescols="B"
550 #+BEGIN_SRC python :results output verbatim drawer :colnames no 553 import orgbabelhelper as obh
557 df = obh.orgtable_to_dataframe(tbl)
558 grparr = re.split(r",\s*", grp)
559 #print re.split(r",\s*", rescols) + [grp]
560 df = df[re.split(r",\s*", rescols) + grparr]
562 assert elem in df.columns, "Error: group column %s not in table columns %s" % (elem, ",".join(df.columns))
565 res = df.groupby(grparr).sum()
567 error("operation %s not implemented" % op)
570 print(obh.dataframe_to_orgtable(res))
574 #+NAME: lobInsertFile
575 #+HEADER: :var filename="/tmp/foo"
576 #+begin_src elisp :results value raw drawer 577 (cl-labels ((wrap-src
579 (list (format "#+BEGIN_SRC %s :eval never :exports source\n" lang)
582 (pcase (file-name-extension filename)
583 ("py" (wrap-src "python"))
584 (".el" (wrap-src "emacs-lisp"))
585 (t '("#+BEGIN_EXAMPLE\n" "#+END_EXAMPLE\n")))))
587 (goto-char (point-min))
588 (insert (format-time-string "# inserted at %Y-%m-%d %H:%M:%S\n"))
589 (insert (car wrappers))
590 (insert-file-contents filename)
591 (goto-char (point-max))
592 (insert (car (cdr wrappers)))