babel

Welcome to the CC Library of Babel. This file contains a collection of code blocks which may be used by authors throughout our public documentation.

This library depends on the core being properly installed and the core/emacs init system loaded in emacs. See home for details.

To load the library itself use C-c C-v i while visiting this file in an org-mode buffer or org-babel-lob-ingest from elisp.

1. echo   util

input

2. read   fs

(if (string= format "csv")
    (with-temp-buffer
      (org-table-import (expand-file-name file) nil)
      (org-table-to-lisp))
  (with-temp-buffer
    (insert-file-contents (expand-file-name file))
    (buffer-string)))

3. write   fs

(cl-flet ((echo (r) (if (stringp r) r (format "%S" r))))
  (with-temp-file file
    (case (and (listp data)
               (or ext (intern (file-name-extension file))))
      ('tsv (insert (orgtbl-to-tsv data '(:fmt echo))))
      ('csv (insert (orgtbl-to-csv data '(:fmt echo))))
      (t    (org-babel-insert-result data)))))
nil

4. json   json

(require 'json)
(cond
 (file
  (org-babel-with-temp-filebuffer file
    (goto-char (point-min))
    (json-read)))
 (url
  (require 'w3m)
  (with-temp-buffer
    (w3m-retrieve url)
    (goto-char (point-min))
    (json-read))))

5. headline   org

(save-excursion
  (when file (get-file-buffer file))
  (org-open-link-from-string (org-make-link-string headline))
  (save-restriction
    (org-narrow-to-subtree)
    (buffer-string)))

6. transpose   table

(apply #'mapcar #'list (list table))

7. all-to-string   table

(defun all-to-string (tbl)
  (if (listp tbl)
      (mapcar #'all-to-string tbl)
    (if (stringp tbl)
        tbl
      (format "%s" tbl))))
(all-to-string tbl)

8. systemd-list-units   os

systemctl list-units --state=running | grep -v systemd | awk '{print $1}' | grep service

9. wc-lines   fs

cat * | wc -l
1574

10. wc-words   fs

cat * | wc -w
5944

11. buffer-name   util

(buffer-name)

12. lines   fs

input=`tokei -C -o json`
echo $input | jq -r '.["Total"] | .code, .comments, .blanks'
1584
1
127

13. langs   fs

input=`tokei -C -o json`
echo $input | jq -r '.["Total"].children | keys[]'
Org

14. sum-str-nums   util

(let ((tot 0))
  (cl-loop
   with tot = 0
   for i in (split-string str) do
   (setf tot (+ tot (string-to-number i)))
   finally return tot))

15. org-task-tbl   org

(let* ((ast (org-element-parse-buffer)) ;; built up the abstract syntax tree of the org buffer
       item-types ; all occuring item types. It could be that some task has more item types than another.
       tasks ; accumulation list for the tasks
       current-task ; name of the current task (header of level 1)
       task-items) ; items of the current task
  (org-element-map ast 'headline
    (lambda (hl)
      (cl-case (org-element-property :level hl)
        (1 ; We assume here that headers of level 1 are tasks.
         (when current-task ; registering the old task
           (setq tasks (cons (cons current-task (nreverse task-items)) tasks)))
         (setq current-task (org-element-property :raw-value hl) ; preparing the new task
               task-items nil))
        (2 ; item
         (let ((item-type (org-element-property :raw-value hl)))
           (setq item-types (cons item-type item-types))
           (setq task-items (cons (cons item-type (org-element-property :todo-keyword hl))
                                  task-items)))))))
  (setq tasks (nreverse (cons (cons current-task (nreverse task-items)) tasks)) ;add the last task
        item-types (sort (cl-remove-duplicates (nreverse item-types) :test 'string-equal) ; list of unique item types
                         #'string<)) ;;Sorting the items lexicographical. Other criteria could be applied.
    ;;;;;;;;;;
  ;; generating the output table:
  (apply
   #'list
   (cons "Item" (mapcar #'car tasks)) ; header
   'hline
   ;; rows:
   (mapcar
    ;; mapping the items to the todo states associated to the tasks:
    (lambda (item-type)
      (cons item-type
            (mapcar
             (lambda (task)
               (let ((todo-status (cdr (assoc-string item-type task))))
                 todo-status))
             tasks)))
    item-types)))

16. org-headlines-map   org

(org-element-map (org-element-parse-buffer 'headline)
    'headline
  (lambda(hl)
    (let ((parent (org-element-property :parent hl)))
      (and (eq (org-element-type parent) 'headline)
           (list (org-element-property :title parent) (org-element-property :title hl))))))

17. trim   util

# remove leading whitespace characters
str="${str#"${str%%[![:space:]]*}"}"
# remove trailing whitespace characters
str="${str%"${str##*[![:space:]]}"}"
printf '%s' "$str"
a b c

18. files   fs

ls -lh $PWD --time-style=long-iso \
  |awk '{if (NR!=1) print $8, $5, $6"-"$7}' \
  |awk 'BEGIN{print "file size updated"}{print $0}'
file size updated
babel.org 24K 2024-09-07-19:45
business.org 803 2024-08-19-21:19
glossary.org 1.2K 2024-09-06-15:35
mindset.org 119 2024-08-15-21:20
pitch.org 1.6K 2024-08-11-17:16
readme.org 1.1K 2024-08-27-21:13
style.org 3.2K 2024-08-21-16:18
tech.org 3.6K 2024-08-21-18:16
ulang.org 12K 2024-09-06-16:12
workflows.org 7.5K 2024-09-03-16:59

19. skel-show   skel

skel show $key
(core lisp rust emacs c)

20. project-root   project

(project-root project)
~/comp/org/meta/

21. project-name   project

(project-name (project-current nil project))
meta

22. project-vc   project

(cadr project)
Hg

23. project-list   project

project--list
~/comp/core/
~/comp/org/meta/
~/.emacs.d/elpa/corfu-terminal/
~/comp/core
~/comp/scratch/zig-pg/
~/comp/scratch/wam/
~/comp/scratch/vdeplug4/
~/comp/scratch/tmux/
~/comp/scratch/scryer-prolog/
~/comp/scratch/screamer/
~/comp/scratch/rerun/
~/comp/scratch/r8k/
~/comp/scratch/qemu/
~/comp/scratch/octatrack/
~/comp/scratch/nu_plugin_quic/
~/comp/scratch/mpk/
~/comp/scratch/lemmy/
~/comp/scratch/io-uring/
~/comp/scratch/infodb/
~/comp/scratch/hy/
~/comp/scratch/hg/
~/comp/scratch/hackrf/
~/comp/scratch/gengat/
~/comp/scratch/fin/
~/comp/scratch/egui/
~/comp/scratch/egui-video/
~/comp/scratch/dev/
~/comp/scratch/davfs2/
~/comp/scratch/crust-0/
~/comp/scratch/cepl.examples/
~/comp/scratch/cc-install/
~/comp/scratch/blok/
~/comp/scratch/archiso/
~/comp/scratch/arch-boxes/
~/comp/scratch/
~/comp/pod/
~/comp/org/notes/
~/comp/org/docs/
~/comp/org/blog/
~/comp/org/archive/
~/comp/org/
~/comp/infra/.stash/src/emacs/
~/comp/infra/
~/comp/home/
~/comp/etc/
~/comp/demo/
~/comp/box/
~/.emacs.d/elpa/org-glossary/
~/comp/org/plan/
~/.emacs.d/elpa/eglot-x/

24. project-details   project

(let* ((project (project-current nil project))
       (name (project-name project))
       (root (project-root project))
       (default-directory root)
       (vc-type (downcase (symbol-name (cadr project))))
       (age (org-sbe hg-log-age ''(dir root)))
       (rev (org-sbe hg-rev ''(dir root)))
       (num (org-sbe hg-id-num ''(dir root)))
       (tags (org-sbe skel-show (key "'tags'")))
       (id (org-sbe skel-show (key "':id'") ''(dir root)))
       (version (org-sbe skel-show (key "'version'") ''(dir root)))
       (description (org-sbe skel-show (key "'description'") ''(dir root)))
       (vc (format ":%s [[https://vc.compiler.company/%s][vc.compiler.company/%s]] :rev %s"
                   vc-type name name
                   (format "[[https://vc.compiler.company/%s/rev/%s][%s:%s]]" name rev rev num)))
       (langs (mapcar (lambda (x) (downcase (format "%s" x))) (flatten (read (org-sbe langs ''(dir root))))))
       (line-counts (flatten (read (org-sbe lines ''(dir root)))))
       (line-sum (cl-reduce '+ line-counts))
       (lines (format "%s :λ %s :# %s :_ %s" line-sum (pop line-counts) (pop line-counts) (pop line-counts))))
  `(hline
    (name ,name)
    (version ,version)
    (description ,description)
    (skel-id ,id)
    (root ,root)
    (vc ,vc)
    (tags ,tags)
    (updated ,age)
    (langs ,langs)
    (lines ,lines)
    hline))
name meta
version  
description  
skel-id  
root ~/comp/org/meta/
vc :hg vc.compiler.company/meta :rev 995df3d48af0:9+
tags  
updated Sat Sep 07 22:38:02 2024 -0400
langs (org)
lines 1828 :λ 1685 :# 4 :_ 139

25. project-files   project fs

List all files found in current project. Ignores files which don't exist locally.

(let* ((project (project-current nil project))
       (name (project-name project))
       (root (expand-file-name (project-root project)))
       (project-files-relative-names t)
       (project-vc-include-untracked nil))
  `((file size modified)
    hline
    ,@(mapcar (lambda (x)
                ;; don't rely on project/vc - only include files which exist locally
                (when-let ((attr (file-attributes x)))
                  (list (format "[[https://vc.compiler.company/%s/file/tip/%s][%s]]" name x x)
                        (when-let ((size (file-attribute-size attr)))
                          (file-size-human-readable size))
                        (format-time-string "%Y-%m-%d %H:%M:%S" (file-attribute-modification-time attr)))))
              (project-files project))))
file size modified
babel.org 30k 2024-09-08 20:35:41
.hgignore 7 2024-08-11 10:08:57
business.org 803 2024-08-19 21:19:33
glossary.org 1.1k 2024-09-06 15:35:52
mindset.org 119 2024-08-15 21:20:13
pitch.org 1.6k 2024-08-11 17:16:04
readme.org 1k 2024-08-27 21:13:01
style.org 3.2k 2024-08-21 16:18:55
tech.org 3.5k 2024-08-21 18:16:26
ulang.org 11k 2024-09-06 16:12:00
workflows.org 7.4k 2024-09-03 16:59:05

26. project-tasks   project org

List all project tasks.

(let ((name (project-name (project-current nil project)))
       (hdr (list '(todo item tags scheduled deadline)
                  'hline))
       (tbl))
  (save-excursion
    (with-current-buffer (find-file-noselect
                          (join-paths company-org-directory 
                                      "plan/tasks/" 
                                      (format "%s.org" name)))
      (org-with-wide-buffer
       (org-map-entries (lambda ()
                          (let ((row))
                            (push (or (org-get-todo-state) "") row)
                            (push (org-get-heading t t t t) row)
                            (push (or (remove name (org-get-tags)) "") row)
                            (push (if-let ((ts (org-get-scheduled-time (point) t)))
                                      (format-time-string (cdr org-timestamp-formats) ts)
                                    "")
                                  row)
                            (push (if-let ((ts (org-get-deadline-time (point) t)))
                                      (format-time-string (cdr org-timestamp-formats) ts)
                                    "")
                                  row)
                            (push (nreverse row) tbl)))
                        "TODO<>\"DONE\"" nil)
       )))
  (append hdr (nreverse tbl))
  )
todo item tags scheduled deadline

27. env-table   os

for i in $(env);
do
    echo "$i|" | sed '0,/=/s//|/'
done

28. get-env   os

(princ (getenv key))
/home/ellis

29. org-current-h1-title   org

(org-element-property :title (save-excursion (org-up-heading-safe) (org-element-at-point)))
org-current-h1-title

30. get-emacs-version   emacs

(princ (concat (format "%s\n" (emacs-version))
         (format "Org v%s" (org-version))))

31. vc-buffer-log   vc

;; Most of this code is copied from vc.el vc-print-log
(require 'vc)
(when (vc-find-backend-function
       (vc-backend (buffer-file-name (get-buffer buf))) 'print-log)
  (let ((limit -1)
        (vc-fileset nil)
        (backend nil)
        (files nil))
    (with-current-buffer (get-buffer buf)
      (setq vc-fileset (vc-deduce-fileset t)) ; FIXME: Why t? --Stef
      (setq backend (car vc-fileset))
      (setq files (cadr vc-fileset)))
    (with-temp-buffer
      (let ((status (vc-call-backend
                     backend 'print-log files (current-buffer))))
        (when (and (processp status)   ; Make sure status is a process
                   (= 0 (process-exit-status status))) ; which has not terminated
          (while (not (eq 'exit (process-status status)))
            (sit-for 1 t)))
        (buffer-string)))))

32. hg-rev   vc

hg log -l 1 --template '{node|short}'
06698c6708de

33. hg-id-num   vc

hg id -n
8+

34. hg-log-since   vc

hg log -l1 --template "{date|age}"
3 days ago

35. hg-log-age   vc

# hg log -l1 --template "{date(date, '%Y-%m-%d %H:%M:%S')}\n"
hg log -l1 --template "{date(date)}\n"
Sat Sep 07 22:38:02 2024 -0400

36. hg-churn   vc

hg churn -f "%Y-%m" -s
2023-10  36394 ************************************
2023-11  12777 ************
2023-12  61624 *************************************************************
2024-01   4923 ****
2024-02   5701 *****
2024-03  27292 ***************************
2024-04  23322 ***********************
2024-05  24141 ***********************
2024-06  17608 *****************
2024-07   9757 *********
2024-08  16177 ****************
2024-09   2430 **

37. hg-status   vc

hg status
M babel.org
M glossary.org
M ulang.org

38. homer   homer

homer
:PUSH home/ellis.stash/scripts/wg-gen-keys.sh
:PUSH home/ellis.stash/scripts/upgrade.sh
:PUSH home/ellis.stash/scripts/sc.sh
:PUSH home/ellis.stash/scripts/rec.sh
:PUSH home/ellis.stash/scripts/port-scan.sh
:PUSH home/ellis.stash/scripts/podman-machine-default-update.sh
:PUSH home/ellis.stash/scripts/pacman-pkgsearch.sh
:PUSH home/ellis.stash/scripts/nfs-export.sh
:PUSH home/ellis.stash/scripts/new-mail.sh
:PUSH home/ellis.stash/scripts/genfstab.sh
:PUSH home/ellis.stash/scripts/gen-libera-cert.sh

39. sh-ob-tangle   org

emacs -Q --batch --eval "
    (progn
      (require 'ob-tangle)
      (dolist (file command-line-args-left)
        (with-current-buffer (find-file-noselect file)
          (org-babel-tangle))))
  " "$@"

40. make-dot-tree   dot

(mapcar #'(lambda (x)
        (princ (format "\"%s\" -> \"%s\";\n" (cl-first x) (cl-second x))))
        table)

41. gen-dot-tree   dot

digraph {
   rankdir=TB;
   splines=true;
   node [shape=box];
   $input
  }

42. user-slime   lisp

(unless (slime-connected-p) (slime))
(slime-eval '(ql:quickload :user))
(slime-repl-set-package "USER")

43. std-slime   lisp

(unless (slime-connected-p) (slime))
(slime-eval '(ql:quickload :std))
(slime-repl-set-package "STD-USER")

44. test-slime   lisp

(unless (slime-connected-p) (slime))
(slime-eval '(ql:quickload :core/tests))
(slime-repl-set-package "CORE/TESTS")

45. cargo-update-dir   rust

# update all crates in dir
set -eu
case $0 in
   (/*) dir=${0%/*}/;;
   (*/*) dir=./${0%/*};;
   (*) dir=.;;
esac

find "$dir/.." -name Cargo.toml -execdir cargo update \;

46. rust-target-triple   rust

rustc -vV | sed -n -e 's/^host: //p'

47. post-align-table   table

(with-temp-buffer
  (erase-buffer)
  (cl-assert text nil "PostAlignTables received nil instead of text ")
  (insert text)
  (beginning-of-buffer)
  (org-mode)
  (while
      (search-forward-regexp org-table-any-line-regexp nil t)
    (org-table-align)
    (org-table-recalculate 'iterate)
    (goto-char (org-table-end)))
  (buffer-string))

48. insert-table-from-file   table fs

(let* ((klist (cl-remove-if (lambda (x) (equal (cadr x) ""))
                            `(("ATTR_LATEX" ,newattr) ("CAPTION" ,newcaption) ("NAME" ,newname))))
       (tbl
        (with-temp-buffer
          (org-mode)
          (insert-file-contents fname)
          (goto-char (point-min))
          (unless (re-search-forward
                   (concat "^[ \t]*#\\+\\(tbl\\)?name:[ \t]*"
                           (regexp-quote tname) "[ \t]*$")
                   nil t)
            (user-error "Can't find table named %s in file" tname fname))
          (forward-line 0)
          (let ((tstart (match-beginning 0))
                tend)
            (while (looking-at "^[ \t]*#\\+\\([^:]+\\): *\\(.*\\)")
              (add-to-list 'klist `(,(upcase (match-string 1)) ,(match-string 2)))
              (delete-region (point) (line-end-position))
              (kill-line))
            (unless (looking-at org-table-line-regexp)
              (looking-at "^.*$")
              (user-error "no table at location of %s, Looking-at: '%s'" tname (match-string 0)))
            (goto-char (org-table-end))
            (while (looking-at-p "^[ \t]*#\\+TBLFM:")
              (forward-line 1))
            (buffer-substring tstart (point))))))
  (setq klist (nreverse klist)) ;; reverse for giving priority to new user settings
  (dolist (elem '("NAME" "CAPTION" "ATTR_LATEX"))
    (when (assoc elem klist)
      (princ (format "#+%s: %s\n" elem (cadr (assoc elem klist))))))
  (princ tbl))

49. filter-table   table

(let ((lst (split-string vals)))
  (concatenate 'list  (loop for row in tbl
                            if (member (let ((field (nth col row)))
                                         (if (numberp field)
                                             (number-to-string field)
                                           field)) lst)
                            collect row into newtbl
                            ;; else do (princ (format "%s: %s\n" (nth col row) lst))
                            finally return newtbl)))

50. filter-table-re   table rx

(let ((lst (split-string vals)))
  (concatenate 'list  (loop for row in tbl
                            if (let* ((rawfield (nth col row))
                                      (field (if (numberp rawfield)
                                                 (number-to-string rawfield)
                                               rawfield)))
                                 (loop for regx in lst
                                       when(string-match-p regx field) return 't
                                       finally return nil))
                            collect row into newtbl
                            ;; else do (princ (format "%s: %s\n" (nth col row) lst))
                            finally return newtbl)))

51. group-table   table

import pandas as pd
import numpy as np
import orgbabelhelper as obh
import sys
import re

df = obh.orgtable_to_dataframe(tbl)
grparr = re.split(r",\s*", grp)
#print re.split(r",\s*", rescols) + [grp]
df = df[re.split(r",\s*", rescols) + grparr]
for elem in grparr:
    assert elem in df.columns, "Error: group column %s not in table columns %s" % (elem, ",".join(df.columns))

if op == "sum":
    res = df.groupby(grparr).sum()
else:
    error("operation %s not implemented" % op)
    sys.exit(1)

print(obh.dataframe_to_orgtable(res))

52. insert-file   fs

(cl-labels ((wrap-src
              (lang)
              (list (format "#+BEGIN_SRC %s :eval never :exports source\n" lang)
                    "#+END_SRC\n")))
  (let ((wrappers
         (pcase (file-name-extension filename)
           ("py" (wrap-src "python"))
           (".el" (wrap-src "emacs-lisp"))
           (t '("#+BEGIN_EXAMPLE\n" "#+END_EXAMPLE\n")))))
    (with-temp-buffer
      (goto-char (point-min))
      (insert (format-time-string "# inserted at %Y-%m-%d %H:%M:%S\n"))
      (insert (car wrappers))
      (insert-file-contents filename)
      (goto-char (point-max))
      (insert (car (cdr wrappers)))
      (buffer-string))))