changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > org > meta / babel.org

changeset 7: 4728f14839e4
parent: f747ffac7f40
child: 06698c6708de
author: Richard Westhaver <ellis@rwest.io>
date: Tue, 27 Aug 2024 21:35:44 -0400
permissions: -rw-r--r--
description: publishing updates
1 #+title: babel
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
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 * echo :util:
16 #+name: echo
17 #+begin_src emacs-lisp :var input=""
18 input
19 #+end_src
20 * read :fs:
21 #+name: read
22 #+begin_src emacs-lisp :var file="" :var format=""
23 (if (string= format "csv")
24  (with-temp-buffer
25  (org-table-import (expand-file-name file) nil)
26  (org-table-to-lisp))
27  (with-temp-buffer
28  (insert-file-contents (expand-file-name file))
29  (buffer-string)))
30 #+end_src
31 * write :fs:
32 #+name: write
33 #+begin_src emacs-lisp :var data="" :var file="" :var ext='()
34 (cl-flet ((echo (r) (if (stringp r) r (format "%S" r))))
35  (with-temp-file file
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)))))
41 nil
42 #+end_src
43 * json :json:
44 #+name: json
45 #+begin_src emacs-lisp :var file='() :var url='()
46 (require 'json)
47 (cond
48  (file
49  (org-babel-with-temp-filebuffer file
50  (goto-char (point-min))
51  (json-read)))
52  (url
53  (require 'w3m)
54  (with-temp-buffer
55  (w3m-retrieve url)
56  (goto-char (point-min))
57  (json-read))))
58 #+end_src
59 
60 * headline :org:
61 #+name: headline
62 #+begin_src emacs-lisp :var headline="" :var file='()
63 (save-excursion
64  (when file (get-file-buffer file))
65  (org-open-link-from-string (org-make-link-string headline))
66  (save-restriction
67  (org-narrow-to-subtree)
68  (buffer-string)))
69 #+end_src
70 * transpose :table:
71 #+name: transpose
72 #+begin_src emacs-lisp :var table=""
73  (apply #'mapcar #'list (list table))
74 #+end_src
75 * all-to-string :table:
76 #+name: all-to-string
77 #+begin_src emacs-lisp :var tbl='()
78 (defun all-to-string (tbl)
79  (if (listp tbl)
80  (mapcar #'all-to-string tbl)
81  (if (stringp tbl)
82  tbl
83  (format "%s" tbl))))
84 (all-to-string tbl)
85 #+end_src
86 * systemd-list-units :os:
87 :PROPERTIES:
88 :ID: 3b23c98d-a286-4988-846d-2dab3d25803d
89 :END:
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
93 #+end_src
94 
95 * wc-dir-lines :fs:
96 :PROPERTIES:
97 :ID: 70008815-9634-48ca-b672-b8dcf2a44074
98 :END:
99 #+name: wc-dir-lines
100 #+begin_src shell :var dir="."
101 cd $dir && cat * | wc -l
102 #+end_src
103 
104 * wc-dir-words :fs:
105 :PROPERTIES:
106 :ID: be755790-f367-4654-87e5-cd2927bfef45
107 :END:
108 #+name: wc-dir-words
109 #+begin_src shell :var dir="."
110 cd $dir && cat * | wc -w
111 #+end_src
112 
113 * tokei-dir-lines :fs:
114 :PROPERTIES:
115 :ID: 670e9855-f8d2-43eb-86af-3ef7292f90b9
116 :END:
117 #+name: tokei-dir-lines
118 #+begin_src shell :var src="" :results output replace
119  cd ~/comp/$src
120  input=`tokei -C -o json`
121  echo $input | jq -r '.["Total"] | .code, .comments, .blanks'
122 #+end_src
123 
124 * tokei-dir-langs :fs:
125 :PROPERTIES:
126 :ID: ff9682f0-bb64-427f-a87d-e0c655f9fdc9
127 :END:
128 #+name: tokei-dir-langs
129 #+begin_src shell :var src="org" :results output replace
130  cd ~/comp/$src
131  input=`tokei -C -o json`
132  echo $input | jq -r '.["Total"].children | keys[]'
133 #+end_src
134 
135 * sum-str-nums :util:
136 :PROPERTIES:
137 :ID: d3c4ac69-337f-430b-a4db-760504f099ea
138 :END:
139 #+name: sum-str-nums
140 #+begin_src emacs-lisp :var s=tokei-dir-lines()
141  (let ((tot 0))
142  (cl-loop
143  with tot = 0
144  for i in (split-string s) do
145  (setf tot (+ tot (string-to-number i)))
146  finally return tot))
147 #+end_src
148 
149 * org-task-tbl :org:
150 :PROPERTIES:
151 :ID: 0ee5b499-6f0b-4e15-8c6e-4876e90c20a9
152 :END:
153 #+name: org-task-tbl
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
161  (lambda (hl)
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
167  task-items nil))
168  (2 ; item
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))
172  task-items)))))))
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.
176  ;;;;;;;;;;
177  ;; generating the output table:
178  (apply
179  #'list
180  (cons "Item" (mapcar #'car tasks)) ; header
181  'hline
182  ;; rows:
183  (mapcar
184  ;; mapping the items to the todo states associated to the tasks:
185  (lambda (item-type)
186  (cons item-type
187  (mapcar
188  (lambda (task)
189  (let ((todo-status (cdr (assoc-string item-type task))))
190  todo-status))
191  tasks)))
192  item-types)))
193 #+end_src
194 
195 * org-headlines-map :org:
196 :PROPERTIES:
197 :ID: 05955228-ca76-48bc-b769-f648b4310a9c
198 :END:
199 #+name: org-headlines-map
200 #+begin_src elisp
201  (org-element-map (org-element-parse-buffer 'headline)
202  'headline
203  (lambda(hl)
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))))))
207 
208 #+end_src
209 
210 * make-info-tbl :org:fs:vc:
211 :PROPERTIES:
212 :ID: d5ba2f3d-fc2d-4db6-8bbd-7ca440ff0e8c
213 :END:
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))))
233  `(hline
234  (name ,nf)
235  (version ,version)
236  (vc ,vf)
237  (updated ,age)
238  (lines ,cf)
239  (langs ,lang)
240  hline))
241  #+end_src
242 
243  #+RESULTS: make-info-tbl
244  |---------+----------------------------|
245  | name | [[https://compiler.company/docs/org][org]] |
246  | version | 0.1.0 |
247  | vc | [[https://vc.compiler.company/comp/org][44+:2b6f731f3684]] |
248  | updated | nil |
249  | lines | 14812 = λ:13917 #:45 _:850 |
250  | langs | (Html Org Svg) |
251  |---------+----------------------------|
252 
253 * make-files-tbl :org:fs:
254 :PROPERTIES:
255 :ID: e2ff9dcf-8340-48b8-a1a6-e0036cbcc495
256 :END:
257 #+name: ls-files
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}'
262 #+end_src
263 
264 #+name: make-files-tbl
265 #+begin_src python :var tab=ls-files() :results table :colnames yes :hlines yes :exports results :eval no-export
266 return tab
267 #+end_src
268 
269 * env-table :os:
270 :PROPERTIES:
271 :ID: 4c824478-0eaa-49b9-905f-2c9a6d4220eb
272 :END:
273 #+begin_src sh :results table replace
274  for i in $(env);
275  do
276  echo "$i|" | sed '0,/=/s//|/'
277  done
278 #+end_src
279 
280 * get-env :os:
281 :PROPERTIES:
282 :ID: 8c1ecc68-ca25-4e72-81d8-415a43e59ae4
283 :END:
284 #+name: get-env
285 #+begin_src elisp :results output :var key="HOME"
286 (princ (getenv key))
287 #+end_src
288 
289 #+RESULTS: get-env
290 : /home/ellis
291 
292 * org-current-h1-title :org:
293 :PROPERTIES:
294 :ID: ae61e7ed-c9ed-414c-8a5f-12b1702f018e
295 :END:
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)))
299 #+end_src
300 
301 #+RESULTS: org-current-h1-title
302 : org-current-h1-title
303 
304 * get-emacs-version :emacs:
305 :PROPERTIES:
306 :ID: af3d83a1-31bd-41b4-be35-c1f33507fd8d
307 :END:
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))))
312 #+end_src
313 
314 * vc-log :vc:
315 #+name: vc-log
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
320 (require 'vc)
321 (when (vc-find-backend-function
322  (vc-backend (buffer-file-name (get-buffer buf))) 'print-log)
323  (let ((limit -1)
324  (vc-fileset nil)
325  (backend nil)
326  (files nil))
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)))
331  (with-temp-buffer
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)))
337  (sit-for 1 t)))
338  (buffer-string)))))
339 #+end_src
340 * hg-rev :vc:
341 :PROPERTIES:
342 :ID: 8119cf43-f2e7-4829-939c-fc4e8531ae6c
343 :END:
344 #+name: hg-rev
345 #+begin_src sh :var src="org"
346 cd ~/comp/$src && hg log -l 1 --template '{node|short}'
347 #+end_src
348 
349 #+RESULTS: hg-rev
350 : 2b6f731f3684
351 
352 * hg-id-num :vc:
353 :PROPERTIES:
354 :ID: 9602faee-5522-445b-a568-be603e20a978
355 :END:
356 #+name: hg-id-num
357 #+begin_src shell :var src="org"
358 cd ~/comp/$src && hg id -n
359 #+end_src
360 
361 #+RESULTS: hg-id-num
362 : 36+
363 
364 * hg-log-age :vc:
365 :PROPERTIES:
366 :ID: 8492f4fb-51a6-4221-8705-a15eb5a50ed4
367 :END:
368 #+name: hg-log-age
369 #+begin_src shell :var src="."
370  cd ~/comp/$src && hg log -l1 --template "{date|age}"
371 #+end_src
372 
373 #+RESULTS: hg-log-age
374 
375 * sh-ob-tangle :org:
376 :PROPERTIES:
377 :ID: 7b311df4-83a3-489d-89a0-929928bce051
378 :END:
379 #+name: sh-ob-tangle
380 #+begin_src sh
381  emacs -Q --batch --eval "
382  (progn
383  (require 'ob-tangle)
384  (dolist (file command-line-args-left)
385  (with-current-buffer (find-file-noselect file)
386  (org-babel-tangle))))
387  " "$@"
388 #+end_src
389 
390 * make-dot-tree :dot:
391 :PROPERTIES:
392 :ID: 5588f446-2d7a-4261-b829-68effd3778ac
393 :END:
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))))
398  table)
399 #+end_src
400 
401 * gen-dot-tree :dot:
402 :PROPERTIES:
403 :ID: a51c943d-0f01-4c8f-96ec-db28ae7fef26
404 :END:
405 #+name: gen-dot-tree
406 #+begin_src dot :file /tmp/tree.png :cmdline -Kdot -Tpng :var input=make-dot-tree() :eval no-export
407 digraph {
408  rankdir=TB;
409  splines=true;
410  node [shape=box];
411  $input
412  }
413 #+end_src
414 
415 #+RESULTS: gen-dot-tree
416 [[file:/tmp/tree.png]]
417 
418 * user-slime :lisp:
419 :PROPERTIES:
420 :ID: 9ffd1d10-ffad-486e-9d7d-82422342b9ff
421 :END:
422 #+name: user-slime
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))
427 #+end_src
428 * std-slime :lisp:
429 :PROPERTIES:
430 :ID: 334dae41-5c35-48bd-8368-71d79f5e48d8
431 :END:
432 #+name: std-slime
433 #+begin_src emacs-lisp :results silent :eval no-export
434  (slime)
435  (slime-eval '(ql:quickload :std))
436  (slime-eval '(in-package :std-user))
437 #+end_src
438 
439 * cargo-update-dir :rust:
440 :PROPERTIES:
441 :ID: 67dc87bb-a27b-46e4-a02f-58daac514630
442 :END:
443 #+name: cargo-update-dir
444 #+begin_src sh :var dir=()
445 # update all crates in dir
446 set -eu
447 case $0 in
448  (/*) dir=${0%/*}/;;
449  (*/*) dir=./${0%/*};;
450  (*) dir=.;;
451 esac
452 
453 find "$dir/.." -name Cargo.toml -execdir cargo update \;
454 #+end_src
455 * rust-target-triple :rust:
456 :PROPERTIES:
457 :ID: 02f96ff2-c607-4889-979c-943203b8ad65
458 :END:
459  #+name: rust-target-triple
460  #+begin_src shell
461  rustc -vV | sed -n -e 's/^host: //p'
462  #+end_src
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
467  (with-temp-buffer
468  (erase-buffer)
469  (cl-assert text nil "PostAlignTables received nil instead of text ")
470  (insert text)
471  (beginning-of-buffer)
472  (org-mode)
473  (while
474  (search-forward-regexp org-table-any-line-regexp nil t)
475  (org-table-align)
476  (org-table-recalculate 'iterate)
477  (goto-char (org-table-end)))
478  (buffer-string))
479 #+END_SRC
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))))
486  (tbl
487  (with-temp-buffer
488  (org-mode)
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]*$")
494  nil t)
495  (user-error "Can't find table named %s in file" tname fname))
496  (forward-line 0)
497  (let ((tstart (match-beginning 0))
498  tend)
499  (while (looking-at "^[ \t]*#\\+\\([^:]+\\): *\\(.*\\)")
500  (add-to-list 'klist `(,(upcase (match-string 1)) ,(match-string 2)))
501  (delete-region (point) (line-end-position))
502  (kill-line))
503  (unless (looking-at org-table-line-regexp)
504  (looking-at "^.*$")
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:")
508  (forward-line 1))
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))))))
514  (princ tbl))
515 #+END_SRC
516 * filter-table :table:
517 #+NAME: filter-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)))
523  (if (numberp field)
524  (number-to-string field)
525  field)) lst)
526  collect row into newtbl
527  ;; else do (princ (format "%s: %s\n" (nth col row) lst))
528  finally return newtbl)))
529 #+END_SRC
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)
539  rawfield)))
540  (loop for regx in lst
541  when(string-match-p regx field) return 't
542  finally return nil))
543  collect row into newtbl
544  ;; else do (princ (format "%s: %s\n" (nth col row) lst))
545  finally return newtbl)))
546 #+END_SRC
547 * group-table :table:
548 #+NAME: group-table
549 #+HEADER: :var tbl="" grp="Name" op="sum" rescols="B"
550 #+BEGIN_SRC python :results output verbatim drawer :colnames no
551  import pandas as pd
552  import numpy as np
553  import orgbabelhelper as obh
554  import sys
555  import re
556 
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]
561  for elem in grparr:
562  assert elem in df.columns, "Error: group column %s not in table columns %s" % (elem, ",".join(df.columns))
563 
564  if op == "sum":
565  res = df.groupby(grparr).sum()
566  else:
567  error("operation %s not implemented" % op)
568  sys.exit(1)
569 
570  print(obh.dataframe_to_orgtable(res))
571 
572 #+END_SRC
573 * insert-file :fs:
574 #+NAME: lobInsertFile
575 #+HEADER: :var filename="/tmp/foo"
576 #+begin_src elisp :results value raw drawer
577  (cl-labels ((wrap-src
578  (lang)
579  (list (format "#+BEGIN_SRC %s :eval never :exports source\n" lang)
580  "#+END_SRC\n")))
581  (let ((wrappers
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")))))
586  (with-temp-buffer
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)))
593  (buffer-string))))
594 #+end_src