1.1--- a/babel.org Sun Aug 25 00:15:40 2024 -0400
1.2+++ b/babel.org Tue Aug 27 21:35:44 2024 -0400
1.3@@ -2,7 +2,7 @@
1.4 #+author: Richard Westhaver
1.5 #+description: Core Library of Babel
1.6 #+setupfile: ../clean.theme
1.7-#+property: header-args :exports both :eval no-export
1.8+#+property: header-args :exports both :eval never
1.9 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
1.10 code blocks which may be used by authors throughout our public
1.11 documentation.
1.12@@ -12,7 +12,77 @@
1.13
1.14 To load the library itself use ~C-c C-v i~ while visiting this file in
1.15 an org-mode buffer or =org-babel-lob-ingest= from elisp.
1.16+* echo :util:
1.17+#+name: echo
1.18+#+begin_src emacs-lisp :var input=""
1.19+input
1.20+#+end_src
1.21+* read :fs:
1.22+#+name: read
1.23+#+begin_src emacs-lisp :var file="" :var format=""
1.24+(if (string= format "csv")
1.25+ (with-temp-buffer
1.26+ (org-table-import (expand-file-name file) nil)
1.27+ (org-table-to-lisp))
1.28+ (with-temp-buffer
1.29+ (insert-file-contents (expand-file-name file))
1.30+ (buffer-string)))
1.31+#+end_src
1.32+* write :fs:
1.33+#+name: write
1.34+#+begin_src emacs-lisp :var data="" :var file="" :var ext='()
1.35+(cl-flet ((echo (r) (if (stringp r) r (format "%S" r))))
1.36+ (with-temp-file file
1.37+ (case (and (listp data)
1.38+ (or ext (intern (file-name-extension file))))
1.39+ ('tsv (insert (orgtbl-to-tsv data '(:fmt echo))))
1.40+ ('csv (insert (orgtbl-to-csv data '(:fmt echo))))
1.41+ (t (org-babel-insert-result data)))))
1.42+nil
1.43+#+end_src
1.44+* json :json:
1.45+#+name: json
1.46+#+begin_src emacs-lisp :var file='() :var url='()
1.47+(require 'json)
1.48+(cond
1.49+ (file
1.50+ (org-babel-with-temp-filebuffer file
1.51+ (goto-char (point-min))
1.52+ (json-read)))
1.53+ (url
1.54+ (require 'w3m)
1.55+ (with-temp-buffer
1.56+ (w3m-retrieve url)
1.57+ (goto-char (point-min))
1.58+ (json-read))))
1.59+#+end_src
1.60
1.61+* headline :org:
1.62+#+name: headline
1.63+#+begin_src emacs-lisp :var headline="" :var file='()
1.64+(save-excursion
1.65+ (when file (get-file-buffer file))
1.66+ (org-open-link-from-string (org-make-link-string headline))
1.67+ (save-restriction
1.68+ (org-narrow-to-subtree)
1.69+ (buffer-string)))
1.70+#+end_src
1.71+* transpose :table:
1.72+#+name: transpose
1.73+#+begin_src emacs-lisp :var table=""
1.74+ (apply #'mapcar #'list (list table))
1.75+#+end_src
1.76+* all-to-string :table:
1.77+#+name: all-to-string
1.78+#+begin_src emacs-lisp :var tbl='()
1.79+(defun all-to-string (tbl)
1.80+ (if (listp tbl)
1.81+ (mapcar #'all-to-string tbl)
1.82+ (if (stringp tbl)
1.83+ tbl
1.84+ (format "%s" tbl))))
1.85+(all-to-string tbl)
1.86+#+end_src
1.87 * systemd-list-units :os:
1.88 :PROPERTIES:
1.89 :ID: 3b23c98d-a286-4988-846d-2dab3d25803d
1.90@@ -128,10 +198,10 @@
1.91 :END:
1.92 #+name: org-headlines-map
1.93 #+begin_src elisp
1.94- (org-element-map (org-element-parse-buffer 'headline )
1.95+ (org-element-map (org-element-parse-buffer 'headline)
1.96 'headline
1.97 (lambda(hl)
1.98- (let ((parent (org-element-property :parent hl )))
1.99+ (let ((parent (org-element-property :parent hl)))
1.100 (and (eq (org-element-type parent) 'headline)
1.101 (list (org-element-property :title parent) (org-element-property :title hl))))))
1.102
1.103@@ -241,6 +311,32 @@
1.104 (format "Org v%s" (org-version))))
1.105 #+end_src
1.106
1.107+* vc-log :vc:
1.108+#+name: vc-log
1.109+#+header: :var limit=-1
1.110+#+header: :var buf=(buffer-name (current-buffer))
1.111+#+begin_src emacs-lisp
1.112+;; Most of this code is copied from vc.el vc-print-log
1.113+(require 'vc)
1.114+(when (vc-find-backend-function
1.115+ (vc-backend (buffer-file-name (get-buffer buf))) 'print-log)
1.116+ (let ((limit -1)
1.117+ (vc-fileset nil)
1.118+ (backend nil)
1.119+ (files nil))
1.120+ (with-current-buffer (get-buffer buf)
1.121+ (setq vc-fileset (vc-deduce-fileset t)) ; FIXME: Why t? --Stef
1.122+ (setq backend (car vc-fileset))
1.123+ (setq files (cadr vc-fileset)))
1.124+ (with-temp-buffer
1.125+ (let ((status (vc-call-backend
1.126+ backend 'print-log files (current-buffer))))
1.127+ (when (and (processp status) ; Make sure status is a process
1.128+ (= 0 (process-exit-status status))) ; which has not terminated
1.129+ (while (not (eq 'exit (process-status status)))
1.130+ (sit-for 1 t)))
1.131+ (buffer-string)))))
1.132+#+end_src
1.133 * hg-rev :vc:
1.134 :PROPERTIES:
1.135 :ID: 8119cf43-f2e7-4829-939c-fc4e8531ae6c
1.136@@ -296,7 +392,7 @@
1.137 :ID: 5588f446-2d7a-4261-b829-68effd3778ac
1.138 :END:
1.139 #+name: make-dot-tree
1.140-#+begin_src emacs-lisp :var table=org-headlines-map :results output
1.141+#+begin_src emacs-lisp :var table=org-headlines-map() :results output
1.142 (mapcar #'(lambda (x)
1.143 (princ (format "\"%s\" -> \"%s\";\n" (cl-first x) (cl-second x))))
1.144 table)
1.145@@ -307,7 +403,7 @@
1.146 :ID: a51c943d-0f01-4c8f-96ec-db28ae7fef26
1.147 :END:
1.148 #+name: gen-dot-tree
1.149-#+begin_src dot :file /tmp/tree.png :cmdline -Kdot -Tpng :var input=make-dot-tree :eval no-export
1.150+#+begin_src dot :file /tmp/tree.png :cmdline -Kdot -Tpng :var input=make-dot-tree() :eval no-export
1.151 digraph {
1.152 rankdir=TB;
1.153 splines=true;
1.154@@ -364,3 +460,135 @@
1.155 #+begin_src shell
1.156 rustc -vV | sed -n -e 's/^host: //p'
1.157 #+end_src
1.158+* post-align-table :table:
1.159+#+NAME: post-align-tables
1.160+#+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"
1.161+#+BEGIN_SRC emacs-lisp :results value :exports both
1.162+ (with-temp-buffer
1.163+ (erase-buffer)
1.164+ (cl-assert text nil "PostAlignTables received nil instead of text ")
1.165+ (insert text)
1.166+ (beginning-of-buffer)
1.167+ (org-mode)
1.168+ (while
1.169+ (search-forward-regexp org-table-any-line-regexp nil t)
1.170+ (org-table-align)
1.171+ (org-table-recalculate 'iterate)
1.172+ (goto-char (org-table-end)))
1.173+ (buffer-string))
1.174+#+END_SRC
1.175+* insert-table-from-file :table:fs:
1.176+#+NAME: insert-table-from-file
1.177+#+HEADER: :var tname="table" fname="/tmp/tbl.org" newcaption="" newattr="" newname=""
1.178+#+BEGIN_SRC elisp :results output drawer
1.179+ (let* ((klist (cl-remove-if (lambda (x) (equal (cadr x) ""))
1.180+ `(("ATTR_LATEX" ,newattr) ("CAPTION" ,newcaption) ("NAME" ,newname))))
1.181+ (tbl
1.182+ (with-temp-buffer
1.183+ (org-mode)
1.184+ (insert-file-contents fname)
1.185+ (goto-char (point-min))
1.186+ (unless (re-search-forward
1.187+ (concat "^[ \t]*#\\+\\(tbl\\)?name:[ \t]*"
1.188+ (regexp-quote tname) "[ \t]*$")
1.189+ nil t)
1.190+ (user-error "Can't find table named %s in file" tname fname))
1.191+ (forward-line 0)
1.192+ (let ((tstart (match-beginning 0))
1.193+ tend)
1.194+ (while (looking-at "^[ \t]*#\\+\\([^:]+\\): *\\(.*\\)")
1.195+ (add-to-list 'klist `(,(upcase (match-string 1)) ,(match-string 2)))
1.196+ (delete-region (point) (line-end-position))
1.197+ (kill-line))
1.198+ (unless (looking-at org-table-line-regexp)
1.199+ (looking-at "^.*$")
1.200+ (user-error "no table at location of %s, Looking-at: '%s'" tname (match-string 0)))
1.201+ (goto-char (org-table-end))
1.202+ (while (looking-at-p "^[ \t]*#\\+TBLFM:")
1.203+ (forward-line 1))
1.204+ (buffer-substring tstart (point))))))
1.205+ (setq klist (nreverse klist)) ;; reverse for giving priority to new user settings
1.206+ (dolist (elem '("NAME" "CAPTION" "ATTR_LATEX"))
1.207+ (when (assoc elem klist)
1.208+ (princ (format "#+%s: %s\n" elem (cadr (assoc elem klist))))))
1.209+ (princ tbl))
1.210+#+END_SRC
1.211+* filter-table :table:
1.212+#+NAME: filter-table
1.213+#+HEADER: :var tbl="" col=0 vals=""
1.214+#+BEGIN_SRC elisp :results value :colnames y
1.215+ (let ((lst (split-string vals)))
1.216+ (concatenate 'list (loop for row in tbl
1.217+ if (member (let ((field (nth col row)))
1.218+ (if (numberp field)
1.219+ (number-to-string field)
1.220+ field)) lst)
1.221+ collect row into newtbl
1.222+ ;; else do (princ (format "%s: %s\n" (nth col row) lst))
1.223+ finally return newtbl)))
1.224+#+END_SRC
1.225+* filter-table-re :table:rx:
1.226+#+NAME: filter-table-re
1.227+#+HEADER: :var tbl="" col=0 vals=".*"
1.228+#+BEGIN_SRC elisp :results value :colnames y
1.229+ (let ((lst (split-string vals)))
1.230+ (concatenate 'list (loop for row in tbl
1.231+ if (let* ((rawfield (nth col row))
1.232+ (field (if (numberp rawfield)
1.233+ (number-to-string rawfield)
1.234+ rawfield)))
1.235+ (loop for regx in lst
1.236+ when(string-match-p regx field) return 't
1.237+ finally return nil))
1.238+ collect row into newtbl
1.239+ ;; else do (princ (format "%s: %s\n" (nth col row) lst))
1.240+ finally return newtbl)))
1.241+#+END_SRC
1.242+* group-table :table:
1.243+#+NAME: group-table
1.244+#+HEADER: :var tbl="" grp="Name" op="sum" rescols="B"
1.245+#+BEGIN_SRC python :results output verbatim drawer :colnames no
1.246+ import pandas as pd
1.247+ import numpy as np
1.248+ import orgbabelhelper as obh
1.249+ import sys
1.250+ import re
1.251+
1.252+ df = obh.orgtable_to_dataframe(tbl)
1.253+ grparr = re.split(r",\s*", grp)
1.254+ #print re.split(r",\s*", rescols) + [grp]
1.255+ df = df[re.split(r",\s*", rescols) + grparr]
1.256+ for elem in grparr:
1.257+ assert elem in df.columns, "Error: group column %s not in table columns %s" % (elem, ",".join(df.columns))
1.258+
1.259+ if op == "sum":
1.260+ res = df.groupby(grparr).sum()
1.261+ else:
1.262+ error("operation %s not implemented" % op)
1.263+ sys.exit(1)
1.264+
1.265+ print(obh.dataframe_to_orgtable(res))
1.266+
1.267+#+END_SRC
1.268+* insert-file :fs:
1.269+#+NAME: lobInsertFile
1.270+#+HEADER: :var filename="/tmp/foo"
1.271+#+begin_src elisp :results value raw drawer
1.272+ (cl-labels ((wrap-src
1.273+ (lang)
1.274+ (list (format "#+BEGIN_SRC %s :eval never :exports source\n" lang)
1.275+ "#+END_SRC\n")))
1.276+ (let ((wrappers
1.277+ (pcase (file-name-extension filename)
1.278+ ("py" (wrap-src "python"))
1.279+ (".el" (wrap-src "emacs-lisp"))
1.280+ (t '("#+BEGIN_EXAMPLE\n" "#+END_EXAMPLE\n")))))
1.281+ (with-temp-buffer
1.282+ (goto-char (point-min))
1.283+ (insert (format-time-string "# inserted at %Y-%m-%d %H:%M:%S\n"))
1.284+ (insert (car wrappers))
1.285+ (insert-file-contents filename)
1.286+ (goto-char (point-max))
1.287+ (insert (car (cdr wrappers)))
1.288+ (buffer-string))))
1.289+#+end_src