2 #+author: Richard Westhaver
3 #+description: Core Library of Babel
4 #+setupfile: ../clean.theme
5 #+property: header-args :exports both :eval no
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:core][core]] being properly installed and the
11 =core/emacs= init system loaded in emacs. See
[[vc: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 :ID: 70334979-e1fd-442d-815f-9eeb7d21ef4f 20 #+begin_src emacs-lisp :var input="" 25 :ID: dade8198-f8ff-4ae9-a52d-becdc1a9c3ce 28 #+begin_src emacs-lisp :var file="" :var format="" 29 (if (string= format "csv")
31 (org-table-import (expand-file-name file) nil)
34 (insert-file-contents (expand-file-name file))
39 :ID: a866820f-e67c-49aa-ba7d-697c4f708654 42 #+begin_src emacs-lisp :var data="" :var file="" :var ext='() 43 (cl-flet ((echo (r) (if (stringp r) r (format "%S" r))))
45 (case (and (listp data)
46 (or ext (intern (file-name-extension file))))
47 ('tsv (insert (orgtbl-to-tsv data '(:fmt echo))))
48 ('csv (insert (orgtbl-to-csv data '(:fmt echo))))
49 (t (org-babel-insert-result data)))))
54 :ID: 063109d3-3bb1-45b3-b963-6b306776798c 57 #+begin_src emacs-lisp :var file='() :var url='() 61 (org-babel-with-temp-filebuffer file
62 (goto-char (point-min))
68 (goto-char (point-min))
74 :ID: e21d6130-804d-4ac7-bccf-8303fd33f509 77 #+begin_src emacs-lisp :var headline="" :var file='() 79 (when file (get-file-buffer file))
80 (org-open-link-from-string (org-make-link-string headline))
82 (org-narrow-to-subtree)
87 :ID: e35c9901-73b6-4794-8900-42860378c7dd 90 #+begin_src emacs-lisp :var table="" 91 (apply #'mapcar #'list (list table))
93 * all-to-string :table: 95 :ID: a122b2bd-abd6-4486-97c8-242e6534e5a8 98 #+begin_src emacs-lisp :var tbl='() 99 (defun all-to-string (tbl)
101 (mapcar #'all-to-string tbl)
107 * systemd-list-units :os: 109 :ID: 3b23c98d-a286-4988-846d-2dab3d25803d 111 #+name: systemd-list-units
112 #+begin_src sh :results replace 113 systemctl list-units --state=running | grep -v systemd | awk '{print $1}' | grep service
118 :ID: 70008815-9634-48ca-b672-b8dcf2a44074 121 #+begin_src shell :var dir="." 122 cd $dir && cat * | wc -l
127 :ID: be755790-f367-4654-87e5-cd2927bfef45 130 #+begin_src shell :var dir="." 131 cd $dir && cat * | wc -w
136 :ID: 7e9f492e-224a-4cf5-b36d-828ccedc630a 139 #+begin_src emacs-lisp 142 * tokei-dir-lines :fs: 144 :ID: 670e9855-f8d2-43eb-86af-3ef7292f90b9 146 #+name: tokei-dir-lines
147 #+begin_src shell :var src="" :results output replace 149 input=`tokei -C -o json`
150 echo $input | jq -r '.["Total"] | .code, .comments, .blanks'
153 * tokei-dir-langs :fs: 155 :ID: ff9682f0-bb64-427f-a87d-e0c655f9fdc9 157 #+name: tokei-dir-langs
158 #+begin_src shell :var src="org" :results output replace 160 input=`tokei -C -o json`
161 echo $input | jq -r '.["Total"].children | keys[]'
164 * sum-str-nums :util: 166 :ID: d3c4ac69-337f-430b-a4db-760504f099ea 169 #+begin_src emacs-lisp :var s=tokei-dir-lines() 173 for i in (split-string s) do
174 (setf tot (+ tot (string-to-number i)))
180 :ID: 0ee5b499-6f0b-4e15-8c6e-4876e90c20a9 183 #+begin_src emacs-lisp 184 (let* ((ast (org-element-parse-buffer)) ;; built up the abstract syntax tree of the org buffer
185 item-types ; all occuring item types. It could be that some task has more item types than another.
186 tasks ; accumulation list for the tasks
187 current-task ; name of the current task (header of level 1)
188 task-items) ; items of the current task
189 (org-element-map ast 'headline
191 (cl-case (org-element-property :level hl)
192 (1 ; We assume here that headers of level 1 are tasks.
193 (when current-task ; registering the old task
194 (setq tasks (cons (cons current-task (nreverse task-items)) tasks)))
195 (setq current-task (org-element-property :raw-value hl) ; preparing the new task
198 (let ((item-type (org-element-property :raw-value hl)))
199 (setq item-types (cons item-type item-types))
200 (setq task-items (cons (cons item-type (org-element-property :todo-keyword hl))
202 (setq tasks (nreverse (cons (cons current-task (nreverse task-items)) tasks)) ;add the last task
203 item-types (sort (cl-remove-duplicates (nreverse item-types) :test 'string-equal) ; list of unique item types
204 #'string<)) ;;Sorting the items lexicographical. Other criteria could be applied.
206 ;; generating the output table:
209 (cons "Item" (mapcar #'car tasks)) ; header
213 ;; mapping the items to the todo states associated to the tasks:
218 (let ((todo-status (cdr (assoc-string item-type task))))
224 * org-headlines-map :org: 226 :ID: 05955228-ca76-48bc-b769-f648b4310a9c 228 #+name: org-headlines-map
230 (org-element-map (org-element-parse-buffer 'headline)
233 (let ((parent (org-element-property :parent hl)))
234 (and (eq (org-element-type parent) 'headline)
235 (list (org-element-property :title parent) (org-element-property :title hl))))))
239 * make-files-tbl :org:fs: 241 :ID: e2ff9dcf-8340-48b8-a1a6-e0036cbcc495 244 #+begin_src sh :results silent :var dir=(expand-file-name "~/comp") name="org" 245 ls -lh $dir/$name --time-style=long-iso \
246 |awk '{if (NR!=1) print $8, $5, $6"-"$7}' \
247 |awk 'BEGIN{print "file size updated"}{print $0}'
250 #+name: make-files-tbl
251 #+begin_src python :var tab=ls-files() :results table :colnames yes :hlines yes :exports results :eval no-export 257 :ID: 4c824478-0eaa-49b9-905f-2c9a6d4220eb 259 #+begin_src sh :results table replace 262 echo "$i|" | sed '0,/=/s//|/'
268 :ID: 8c1ecc68-ca25-4e72-81d8-415a43e59ae4 271 #+begin_src elisp :results output :var key="HOME" 278 * org-current-h1-title :org: 280 :ID: ae61e7ed-c9ed-414c-8a5f-12b1702f018e 282 #+name: org-current-h1-title
283 #+begin_src emacs-lisp :results value 284 (org-element-property :title (save-excursion (org-up-heading-safe) (org-element-at-point)))
287 #+RESULTS: org-current-h1-title
288 : org-current-h1-title
290 * get-emacs-version :emacs: 292 :ID: af3d83a1-31bd-41b4-be35-c1f33507fd8d 294 #+name: get-emacs-version
295 #+begin_src elisp :results output 296 (princ (concat (format "%s\n" (emacs-version))
297 (format "Org v%s" (org-version))))
302 :ID: b2d9f6f0-2ac7-4e45-abbe-b32cc2d0f7aa 305 #+header: :var limit=-1
306 #+header: :var buf=(buffer-name (current-buffer))
307 #+begin_src emacs-lisp 308 ;; Most of this code is copied from vc.el vc-print-log
310 (when (vc-find-backend-function
311 (vc-backend (buffer-file-name (get-buffer buf))) 'print-log)
316 (with-current-buffer (get-buffer buf)
317 (setq vc-fileset (vc-deduce-fileset t)) ; FIXME: Why t? --Stef
318 (setq backend (car vc-fileset))
319 (setq files (cadr vc-fileset)))
321 (let ((status (vc-call-backend
322 backend 'print-log files (current-buffer))))
323 (when (and (processp status) ; Make sure status is a process
324 (= 0 (process-exit-status status))) ; which has not terminated
325 (while (not (eq 'exit (process-status status)))
331 :ID: 8119cf43-f2e7-4829-939c-fc4e8531ae6c 334 #+begin_src sh :var src="org" 335 cd ~/comp/$src && hg log -l 1 --template '{node|short}'
343 :ID: 9602faee-5522-445b-a568-be603e20a978 346 #+begin_src shell :var src="org" 347 cd ~/comp/$src && hg id -n
355 :ID: 8492f4fb-51a6-4221-8705-a15eb5a50ed4 358 #+begin_src shell :var src="." 359 cd ~/comp/$src && hg log -l1 --template "{date|age}"
362 #+RESULTS: hg-log-age
366 :ID: 7b311df4-83a3-489d-89a0-929928bce051 370 emacs -Q --batch --eval "
373 (dolist (file command-line-args-left)
374 (with-current-buffer (find-file-noselect file)
375 (org-babel-tangle))))
379 * make-dot-tree :dot: 381 :ID: 5588f446-2d7a-4261-b829-68effd3778ac 383 #+name: make-dot-tree
384 #+begin_src emacs-lisp :var table=org-headlines-map() :results output 385 (mapcar #'(lambda (x)
386 (princ (format "\"%s\" -> \"%s\";\n" (cl-first x) (cl-second x))))
392 :ID: a51c943d-0f01-4c8f-96ec-db28ae7fef26 395 #+begin_src dot :file /tmp/tree.png :cmdline -Kdot -Tpng :var input=make-dot-tree() :eval no-export 406 :ID: 9ffd1d10-ffad-486e-9d7d-82422342b9ff 409 #+begin_src emacs-lisp :results silent :eval no-export 410 (unless (slime-connected-p) (slime))
411 (slime-eval '(ql:quickload :user))
412 (slime-repl-set-package "USER")
416 :ID: 334dae41-5c35-48bd-8368-71d79f5e48d8 419 #+begin_src emacs-lisp :results silent :eval no-export 420 (unless (slime-connected-p) (slime))
421 (slime-eval '(ql:quickload :std))
422 (slime-repl-set-package "STD-USER")
426 :ID: ff559b09-1c18-422c-a0ae-681180f685cf 429 #+begin_src emacs-lisp :results silent :eval no-export 430 (unless (slime-connected-p) (slime))
431 (slime-eval '(ql:quickload :core/tests))
432 (slime-repl-set-package "CORE/TESTS")
434 * cargo-update-dir :rust: 436 :ID: 67dc87bb-a27b-46e4-a02f-58daac514630 438 #+name: cargo-update-dir
439 #+begin_src sh :var dir=() 440 # update all crates in dir
444 (*/*) dir=./${0%/*};;
448 find "$dir/.." -name Cargo.toml -execdir cargo update \;
450 * rust-target-triple :rust: 452 :ID: 02f96ff2-c607-4889-979c-943203b8ad65 454 #
+name: rust-target-triple 456 rustc -vV | sed -n -e 's/^host: //p' 458 * post-align-table :table: 460 :ID: d8a3cc05-a4ec-4820-935d-63f676ee32a3 462 #+NAME: post-align-tables 463 #+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" 464 #+BEGIN_SRC emacs-lisp :results value :exports both 467 (cl-assert text nil "PostAlignTables received nil instead of text ") 469 (beginning-of-buffer) 472 (search-forward-regexp org-table-any-line-regexp nil t) 474 (org-table-recalculate 'iterate) 475 (goto-char (org-table-end))) 478 * insert-table-from-file :table:fs: 480 :ID: bde8705d-0969-493d-91b9-51a93ae09efa 482 #+NAME: insert-table-from-file 483 #+HEADER: :var tname="table" fname="/tmp/tbl.org" newcaption="" newattr="" newname="" 484 #+BEGIN_SRC elisp :results output drawer 485 (let* ((klist (cl-remove-if (lambda (x) (equal (cadr x) "")) 486 `(("ATTR_LATEX" ,newattr) ("CAPTION" ,newcaption) ("NAME" ,newname)))) 490 (insert-file-contents fname) 491 (goto-char (point-min)) 492 (unless (re-search-forward 493 (concat "^[ \t]*#\\+\\(tbl\\)?name:[ \t]
*" 494 (regexp-quote tname) "[ \t]*$")
496 (user-error "Can't find table named %s in file" tname fname))
498 (let ((tstart (match-beginning 0))
500 (while (looking-at "^[ \t]
*#\\+\\([^:]+\\): *\\(.
*\\)") 501 (add-to-list 'klist `(,(upcase (match-string 1)) ,(match-string 2))) 502 (delete-region (point) (line-end-position)) 504 (unless (looking-at org-table-line-regexp) 506 (user-error "no table at location of %s, Looking-at: '%s'" tname (match-string 0)))
507 (goto-char (org-table-end))
508 (while (looking-at-p "^[ \t]
*#\\+TBLFM:") 510 (buffer-substring tstart (point)))))) 511 (setq klist (nreverse klist)) ;; reverse for giving priority to new user settings 512 (dolist (elem '("NAME" "CAPTION" "ATTR_LATEX")) 513 (when (assoc elem klist) 514 (princ (format "#+%s: %s\n" elem (cadr (assoc elem klist)))))) 517 * filter-table :table:
519 :ID: cec80330-99c3-4dab-9b13-bb0a520ace73 522 #+HEADER: :var tbl="" col=0 vals=""
523 #+BEGIN_SRC elisp :results value :colnames y 524 (let ((lst (split-string vals)))
525 (concatenate 'list (loop for row in tbl
526 if (member (let ((field (nth col row)))
528 (number-to-string field)
530 collect row into newtbl
531 ;; else do (princ (format "%s: %s\n" (nth col row) lst))
532 finally return newtbl)))
534 * filter-table-re :table:rx: 536 :ID: 61e82902-3108-4cee-8e02-3e9d6770f5cb 538 #+NAME: filter-table-re
539 #+HEADER: :var tbl="" col=0 vals=".*"
540 #+BEGIN_SRC elisp :results value :colnames y 541 (let ((lst (split-string vals)))
542 (concatenate 'list (loop for row in tbl
543 if (let* ((rawfield (nth col row))
544 (field (if (numberp rawfield)
545 (number-to-string rawfield)
547 (loop for regx in lst
548 when(string-match-p regx field) return 't
550 collect row into newtbl
551 ;; else do (princ (format "%s: %s\n" (nth col row) lst))
552 finally return newtbl)))
554 * group-table :table: 556 :ID: d1cb8d5d-14bc-4fbe-baf0-ff819d51ec4c 559 #+HEADER: :var tbl="" grp="Name" op="sum" rescols="B"
560 #+BEGIN_SRC python :results output verbatim drawer :colnames no 563 import orgbabelhelper as obh
567 df = obh.orgtable_to_dataframe(tbl)
568 grparr = re.split(r",\s*", grp)
569 #print re.split(r",\s*", rescols) + [grp]
570 df = df[re.split(r",\s*", rescols) + grparr]
572 assert elem in df.columns, "Error: group column %s not in table columns %s" % (elem, ",".join(df.columns))
575 res = df.groupby(grparr).sum()
577 error("operation %s not implemented" % op)
580 print(obh.dataframe_to_orgtable(res))
585 :ID: 57196c4d-a275-4023-8462-a5847676347c 587 #+NAME: lobInsertFile
588 #+HEADER: :var filename="/tmp/foo"
589 #+begin_src elisp :results value raw drawer 590 (cl-labels ((wrap-src
592 (list (format "#+BEGIN_SRC %s :eval never :exports source\n" lang)
595 (pcase (file-name-extension filename)
596 ("py" (wrap-src "python"))
597 (".el" (wrap-src "emacs-lisp"))
598 (t '("#+BEGIN_EXAMPLE\n" "#+END_EXAMPLE\n")))))
600 (goto-char (point-min))
601 (insert (format-time-string "# inserted at %Y-%m-%d %H:%M:%S\n"))
602 (insert (car wrappers))
603 (insert-file-contents filename)
604 (goto-char (point-max))
605 (insert (car (cdr wrappers)))