changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > org > meta / babel.org

changeset 8: 06698c6708de
parent: 4728f14839e4
child: 995df3d48af0
author: Richard Westhaver <ellis@rwest.io>
date: Wed, 04 Sep 2024 16:59:32 -0400
permissions: -rw-r--r--
description: fixes
1 #+title: babel
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
8 documentation.
9 
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.
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 :PROPERTIES:
17 :ID: 70334979-e1fd-442d-815f-9eeb7d21ef4f
18 :END:
19 #+name: echo
20 #+begin_src emacs-lisp :var input=""
21 input
22 #+end_src
23 * read :fs:
24 :PROPERTIES:
25 :ID: dade8198-f8ff-4ae9-a52d-becdc1a9c3ce
26 :END:
27 #+name: read
28 #+begin_src emacs-lisp :var file="" :var format=""
29 (if (string= format "csv")
30  (with-temp-buffer
31  (org-table-import (expand-file-name file) nil)
32  (org-table-to-lisp))
33  (with-temp-buffer
34  (insert-file-contents (expand-file-name file))
35  (buffer-string)))
36 #+end_src
37 * write :fs:
38 :PROPERTIES:
39 :ID: a866820f-e67c-49aa-ba7d-697c4f708654
40 :END:
41 #+name: write
42 #+begin_src emacs-lisp :var data="" :var file="" :var ext='()
43 (cl-flet ((echo (r) (if (stringp r) r (format "%S" r))))
44  (with-temp-file file
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)))))
50 nil
51 #+end_src
52 * json :json:
53 :PROPERTIES:
54 :ID: 063109d3-3bb1-45b3-b963-6b306776798c
55 :END:
56 #+name: json
57 #+begin_src emacs-lisp :var file='() :var url='()
58 (require 'json)
59 (cond
60  (file
61  (org-babel-with-temp-filebuffer file
62  (goto-char (point-min))
63  (json-read)))
64  (url
65  (require 'w3m)
66  (with-temp-buffer
67  (w3m-retrieve url)
68  (goto-char (point-min))
69  (json-read))))
70 #+end_src
71 
72 * headline :org:
73 :PROPERTIES:
74 :ID: e21d6130-804d-4ac7-bccf-8303fd33f509
75 :END:
76 #+name: headline
77 #+begin_src emacs-lisp :var headline="" :var file='()
78 (save-excursion
79  (when file (get-file-buffer file))
80  (org-open-link-from-string (org-make-link-string headline))
81  (save-restriction
82  (org-narrow-to-subtree)
83  (buffer-string)))
84 #+end_src
85 * transpose :table:
86 :PROPERTIES:
87 :ID: e35c9901-73b6-4794-8900-42860378c7dd
88 :END:
89 #+name: transpose
90 #+begin_src emacs-lisp :var table=""
91  (apply #'mapcar #'list (list table))
92 #+end_src
93 * all-to-string :table:
94 :PROPERTIES:
95 :ID: a122b2bd-abd6-4486-97c8-242e6534e5a8
96 :END:
97 #+name: all-to-string
98 #+begin_src emacs-lisp :var tbl='()
99 (defun all-to-string (tbl)
100  (if (listp tbl)
101  (mapcar #'all-to-string tbl)
102  (if (stringp tbl)
103  tbl
104  (format "%s" tbl))))
105 (all-to-string tbl)
106 #+end_src
107 * systemd-list-units :os:
108 :PROPERTIES:
109 :ID: 3b23c98d-a286-4988-846d-2dab3d25803d
110 :END:
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
114 #+end_src
115 
116 * wc-dir-lines :fs:
117 :PROPERTIES:
118 :ID: 70008815-9634-48ca-b672-b8dcf2a44074
119 :END:
120 #+name: wc-dir-lines
121 #+begin_src shell :var dir="."
122 cd $dir && cat * | wc -l
123 #+end_src
124 
125 * wc-dir-words :fs:
126 :PROPERTIES:
127 :ID: be755790-f367-4654-87e5-cd2927bfef45
128 :END:
129 #+name: wc-dir-words
130 #+begin_src shell :var dir="."
131 cd $dir && cat * | wc -w
132 #+end_src
133 
134 * buffer-name :util:
135 :PROPERTIES:
136 :ID: 7e9f492e-224a-4cf5-b36d-828ccedc630a
137 :END:
138 #+name: buffer-name
139 #+begin_src emacs-lisp
140 (buffer-name)
141 #+end_src
142 * tokei-dir-lines :fs:
143 :PROPERTIES:
144 :ID: 670e9855-f8d2-43eb-86af-3ef7292f90b9
145 :END:
146 #+name: tokei-dir-lines
147 #+begin_src shell :var src="" :results output replace
148  cd ~/comp/$src
149  input=`tokei -C -o json`
150  echo $input | jq -r '.["Total"] | .code, .comments, .blanks'
151 #+end_src
152 
153 * tokei-dir-langs :fs:
154 :PROPERTIES:
155 :ID: ff9682f0-bb64-427f-a87d-e0c655f9fdc9
156 :END:
157 #+name: tokei-dir-langs
158 #+begin_src shell :var src="org" :results output replace
159  cd ~/comp/$src
160  input=`tokei -C -o json`
161  echo $input | jq -r '.["Total"].children | keys[]'
162 #+end_src
163 
164 * sum-str-nums :util:
165 :PROPERTIES:
166 :ID: d3c4ac69-337f-430b-a4db-760504f099ea
167 :END:
168 #+name: sum-str-nums
169 #+begin_src emacs-lisp :var s=tokei-dir-lines()
170  (let ((tot 0))
171  (cl-loop
172  with tot = 0
173  for i in (split-string s) do
174  (setf tot (+ tot (string-to-number i)))
175  finally return tot))
176 #+end_src
177 
178 * org-task-tbl :org:
179 :PROPERTIES:
180 :ID: 0ee5b499-6f0b-4e15-8c6e-4876e90c20a9
181 :END:
182 #+name: org-task-tbl
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
190  (lambda (hl)
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
196  task-items nil))
197  (2 ; item
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))
201  task-items)))))))
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.
205  ;;;;;;;;;;
206  ;; generating the output table:
207  (apply
208  #'list
209  (cons "Item" (mapcar #'car tasks)) ; header
210  'hline
211  ;; rows:
212  (mapcar
213  ;; mapping the items to the todo states associated to the tasks:
214  (lambda (item-type)
215  (cons item-type
216  (mapcar
217  (lambda (task)
218  (let ((todo-status (cdr (assoc-string item-type task))))
219  todo-status))
220  tasks)))
221  item-types)))
222 #+end_src
223 
224 * org-headlines-map :org:
225 :PROPERTIES:
226 :ID: 05955228-ca76-48bc-b769-f648b4310a9c
227 :END:
228 #+name: org-headlines-map
229 #+begin_src elisp
230  (org-element-map (org-element-parse-buffer 'headline)
231  'headline
232  (lambda(hl)
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))))))
236 
237 #+end_src
238 
239 * make-files-tbl :org:fs:
240 :PROPERTIES:
241 :ID: e2ff9dcf-8340-48b8-a1a6-e0036cbcc495
242 :END:
243 #+name: ls-files
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}'
248 #+end_src
249 
250 #+name: make-files-tbl
251 #+begin_src python :var tab=ls-files() :results table :colnames yes :hlines yes :exports results :eval no-export
252 return tab
253 #+end_src
254 
255 * env-table :os:
256 :PROPERTIES:
257 :ID: 4c824478-0eaa-49b9-905f-2c9a6d4220eb
258 :END:
259 #+begin_src sh :results table replace
260  for i in $(env);
261  do
262  echo "$i|" | sed '0,/=/s//|/'
263  done
264 #+end_src
265 
266 * get-env :os:
267 :PROPERTIES:
268 :ID: 8c1ecc68-ca25-4e72-81d8-415a43e59ae4
269 :END:
270 #+name: get-env
271 #+begin_src elisp :results output :var key="HOME"
272 (princ (getenv key))
273 #+end_src
274 
275 #+RESULTS: get-env
276 : /home/ellis
277 
278 * org-current-h1-title :org:
279 :PROPERTIES:
280 :ID: ae61e7ed-c9ed-414c-8a5f-12b1702f018e
281 :END:
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)))
285 #+end_src
286 
287 #+RESULTS: org-current-h1-title
288 : org-current-h1-title
289 
290 * get-emacs-version :emacs:
291 :PROPERTIES:
292 :ID: af3d83a1-31bd-41b4-be35-c1f33507fd8d
293 :END:
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))))
298 #+end_src
299 
300 * vc-log :vc:
301 :PROPERTIES:
302 :ID: b2d9f6f0-2ac7-4e45-abbe-b32cc2d0f7aa
303 :END:
304 #+name: vc-log
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
309 (require 'vc)
310 (when (vc-find-backend-function
311  (vc-backend (buffer-file-name (get-buffer buf))) 'print-log)
312  (let ((limit -1)
313  (vc-fileset nil)
314  (backend nil)
315  (files nil))
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)))
320  (with-temp-buffer
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)))
326  (sit-for 1 t)))
327  (buffer-string)))))
328 #+end_src
329 * hg-rev :vc:
330 :PROPERTIES:
331 :ID: 8119cf43-f2e7-4829-939c-fc4e8531ae6c
332 :END:
333 #+name: hg-rev
334 #+begin_src sh :var src="org"
335 cd ~/comp/$src && hg log -l 1 --template '{node|short}'
336 #+end_src
337 
338 #+RESULTS: hg-rev
339 : 2b6f731f3684
340 
341 * hg-id-num :vc:
342 :PROPERTIES:
343 :ID: 9602faee-5522-445b-a568-be603e20a978
344 :END:
345 #+name: hg-id-num
346 #+begin_src shell :var src="org"
347 cd ~/comp/$src && hg id -n
348 #+end_src
349 
350 #+RESULTS: hg-id-num
351 : 36+
352 
353 * hg-log-age :vc:
354 :PROPERTIES:
355 :ID: 8492f4fb-51a6-4221-8705-a15eb5a50ed4
356 :END:
357 #+name: hg-log-age
358 #+begin_src shell :var src="."
359  cd ~/comp/$src && hg log -l1 --template "{date|age}"
360 #+end_src
361 
362 #+RESULTS: hg-log-age
363 
364 * sh-ob-tangle :org:
365 :PROPERTIES:
366 :ID: 7b311df4-83a3-489d-89a0-929928bce051
367 :END:
368 #+name: sh-ob-tangle
369 #+begin_src sh
370  emacs -Q --batch --eval "
371  (progn
372  (require 'ob-tangle)
373  (dolist (file command-line-args-left)
374  (with-current-buffer (find-file-noselect file)
375  (org-babel-tangle))))
376  " "$@"
377 #+end_src
378 
379 * make-dot-tree :dot:
380 :PROPERTIES:
381 :ID: 5588f446-2d7a-4261-b829-68effd3778ac
382 :END:
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))))
387  table)
388 #+end_src
389 
390 * gen-dot-tree :dot:
391 :PROPERTIES:
392 :ID: a51c943d-0f01-4c8f-96ec-db28ae7fef26
393 :END:
394 #+name: gen-dot-tree
395 #+begin_src dot :file /tmp/tree.png :cmdline -Kdot -Tpng :var input=make-dot-tree() :eval no-export
396 digraph {
397  rankdir=TB;
398  splines=true;
399  node [shape=box];
400  $input
401  }
402 #+end_src
403 
404 * user-slime :lisp:
405 :PROPERTIES:
406 :ID: 9ffd1d10-ffad-486e-9d7d-82422342b9ff
407 :END:
408 #+name: user-slime
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")
413 #+end_src
414 * std-slime :lisp:
415 :PROPERTIES:
416 :ID: 334dae41-5c35-48bd-8368-71d79f5e48d8
417 :END:
418 #+name: std-slime
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")
423 #+end_src
424 * test-slime :lisp:
425 :PROPERTIES:
426 :ID: ff559b09-1c18-422c-a0ae-681180f685cf
427 :END:
428 #+name: std-slime
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")
433 #+end_src
434 * cargo-update-dir :rust:
435 :PROPERTIES:
436 :ID: 67dc87bb-a27b-46e4-a02f-58daac514630
437 :END:
438 #+name: cargo-update-dir
439 #+begin_src sh :var dir=()
440 # update all crates in dir
441 set -eu
442 case $0 in
443  (/*) dir=${0%/*}/;;
444  (*/*) dir=./${0%/*};;
445  (*) dir=.;;
446 esac
447 
448 find "$dir/.." -name Cargo.toml -execdir cargo update \;
449 #+end_src
450 * rust-target-triple :rust:
451 :PROPERTIES:
452 :ID: 02f96ff2-c607-4889-979c-943203b8ad65
453 :END:
454  #+name: rust-target-triple
455  #+begin_src shell
456  rustc -vV | sed -n -e 's/^host: //p'
457  #+end_src
458 * post-align-table :table:
459 :PROPERTIES:
460 :ID: d8a3cc05-a4ec-4820-935d-63f676ee32a3
461 :END:
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
465  (with-temp-buffer
466  (erase-buffer)
467  (cl-assert text nil "PostAlignTables received nil instead of text ")
468  (insert text)
469  (beginning-of-buffer)
470  (org-mode)
471  (while
472  (search-forward-regexp org-table-any-line-regexp nil t)
473  (org-table-align)
474  (org-table-recalculate 'iterate)
475  (goto-char (org-table-end)))
476  (buffer-string))
477 #+END_SRC
478 * insert-table-from-file :table:fs:
479 :PROPERTIES:
480 :ID: bde8705d-0969-493d-91b9-51a93ae09efa
481 :END:
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))))
487  (tbl
488  (with-temp-buffer
489  (org-mode)
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]*$")
495  nil t)
496  (user-error "Can't find table named %s in file" tname fname))
497  (forward-line 0)
498  (let ((tstart (match-beginning 0))
499  tend)
500  (while (looking-at "^[ \t]*#\\+\\([^:]+\\): *\\(.*\\)")
501  (add-to-list 'klist `(,(upcase (match-string 1)) ,(match-string 2)))
502  (delete-region (point) (line-end-position))
503  (kill-line))
504  (unless (looking-at org-table-line-regexp)
505  (looking-at "^.*$")
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:")
509  (forward-line 1))
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))))))
515  (princ tbl))
516 #+END_SRC
517 * filter-table :table:
518 :PROPERTIES:
519 :ID: cec80330-99c3-4dab-9b13-bb0a520ace73
520 :END:
521 #+NAME: filter-table
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)))
527  (if (numberp field)
528  (number-to-string field)
529  field)) lst)
530  collect row into newtbl
531  ;; else do (princ (format "%s: %s\n" (nth col row) lst))
532  finally return newtbl)))
533 #+END_SRC
534 * filter-table-re :table:rx:
535 :PROPERTIES:
536 :ID: 61e82902-3108-4cee-8e02-3e9d6770f5cb
537 :END:
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)
546  rawfield)))
547  (loop for regx in lst
548  when(string-match-p regx field) return 't
549  finally return nil))
550  collect row into newtbl
551  ;; else do (princ (format "%s: %s\n" (nth col row) lst))
552  finally return newtbl)))
553 #+END_SRC
554 * group-table :table:
555 :PROPERTIES:
556 :ID: d1cb8d5d-14bc-4fbe-baf0-ff819d51ec4c
557 :END:
558 #+NAME: group-table
559 #+HEADER: :var tbl="" grp="Name" op="sum" rescols="B"
560 #+BEGIN_SRC python :results output verbatim drawer :colnames no
561  import pandas as pd
562  import numpy as np
563  import orgbabelhelper as obh
564  import sys
565  import re
566 
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]
571  for elem in grparr:
572  assert elem in df.columns, "Error: group column %s not in table columns %s" % (elem, ",".join(df.columns))
573 
574  if op == "sum":
575  res = df.groupby(grparr).sum()
576  else:
577  error("operation %s not implemented" % op)
578  sys.exit(1)
579 
580  print(obh.dataframe_to_orgtable(res))
581 
582 #+END_SRC
583 * insert-file :fs:
584 :PROPERTIES:
585 :ID: 57196c4d-a275-4023-8462-a5847676347c
586 :END:
587 #+NAME: lobInsertFile
588 #+HEADER: :var filename="/tmp/foo"
589 #+begin_src elisp :results value raw drawer
590  (cl-labels ((wrap-src
591  (lang)
592  (list (format "#+BEGIN_SRC %s :eval never :exports source\n" lang)
593  "#+END_SRC\n")))
594  (let ((wrappers
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")))))
599  (with-temp-buffer
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)))
606  (buffer-string))))
607 #+end_src