changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > org > meta / babel.org

changeset 9: 995df3d48af0
parent: 06698c6708de
child: 65bf0057ca35
author: Richard Westhaver <ellis@rwest.io>
date: Sat, 07 Sep 2024 22:38:02 -0400
permissions: -rw-r--r--
description: ulang
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-export
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-lines :fs:
117 :PROPERTIES:
118 :ID: 70008815-9634-48ca-b672-b8dcf2a44074
119 :END:
120 #+name: wc-lines
121 #+begin_src shell :dir "."
122 cat * | wc -l
123 #+end_src
124 
125 #+RESULTS: wc-lines
126 : 1574
127 
128 * wc-words :fs:
129 :PROPERTIES:
130 :ID: be755790-f367-4654-87e5-cd2927bfef45
131 :END:
132 #+name: wc-words
133 #+begin_src shell :dir "."
134 cat * | wc -w
135 #+end_src
136 
137 #+RESULTS: wc-words
138 : 5944
139 
140 * buffer-name :util:
141 :PROPERTIES:
142 :ID: 7e9f492e-224a-4cf5-b36d-828ccedc630a
143 :END:
144 #+name: buffer-name
145 #+begin_src emacs-lisp
146 (buffer-name)
147 #+end_src
148 
149 * lines :fs:
150 :PROPERTIES:
151 :ID: 670e9855-f8d2-43eb-86af-3ef7292f90b9
152 :END:
153 #+name: lines
154 #+begin_src shell :dir "." :results table replace
155  input=`tokei -C -o json`
156  echo $input | jq -r '.["Total"] | .code, .comments, .blanks'
157 #+end_src
158 
159 #+RESULTS: lines
160 | 1584 |
161 | 1 |
162 | 127 |
163 
164 * langs :fs:
165 :PROPERTIES:
166 :ID: ff9682f0-bb64-427f-a87d-e0c655f9fdc9
167 :END:
168 #+name: langs
169 #+begin_src shell :dir "." :results output table replace
170  input=`tokei -C -o json`
171  echo $input | jq -r '.["Total"].children | keys[]'
172 #+end_src
173 
174 #+RESULTS: langs
175 : Org
176 
177 * sum-str-nums :util:
178 :PROPERTIES:
179 :ID: d3c4ac69-337f-430b-a4db-760504f099ea
180 :END:
181 #+name: sum-str-nums
182 #+begin_src emacs-lisp :var str=lines()
183  (let ((tot 0))
184  (cl-loop
185  with tot = 0
186  for i in (split-string str) do
187  (setf tot (+ tot (string-to-number i)))
188  finally return tot))
189 #+end_src
190 
191 * org-task-tbl :org:
192 :PROPERTIES:
193 :ID: 0ee5b499-6f0b-4e15-8c6e-4876e90c20a9
194 :END:
195 #+name: org-task-tbl
196 #+begin_src emacs-lisp
197  (let* ((ast (org-element-parse-buffer)) ;; built up the abstract syntax tree of the org buffer
198  item-types ; all occuring item types. It could be that some task has more item types than another.
199  tasks ; accumulation list for the tasks
200  current-task ; name of the current task (header of level 1)
201  task-items) ; items of the current task
202  (org-element-map ast 'headline
203  (lambda (hl)
204  (cl-case (org-element-property :level hl)
205  (1 ; We assume here that headers of level 1 are tasks.
206  (when current-task ; registering the old task
207  (setq tasks (cons (cons current-task (nreverse task-items)) tasks)))
208  (setq current-task (org-element-property :raw-value hl) ; preparing the new task
209  task-items nil))
210  (2 ; item
211  (let ((item-type (org-element-property :raw-value hl)))
212  (setq item-types (cons item-type item-types))
213  (setq task-items (cons (cons item-type (org-element-property :todo-keyword hl))
214  task-items)))))))
215  (setq tasks (nreverse (cons (cons current-task (nreverse task-items)) tasks)) ;add the last task
216  item-types (sort (cl-remove-duplicates (nreverse item-types) :test 'string-equal) ; list of unique item types
217  #'string<)) ;;Sorting the items lexicographical. Other criteria could be applied.
218  ;;;;;;;;;;
219  ;; generating the output table:
220  (apply
221  #'list
222  (cons "Item" (mapcar #'car tasks)) ; header
223  'hline
224  ;; rows:
225  (mapcar
226  ;; mapping the items to the todo states associated to the tasks:
227  (lambda (item-type)
228  (cons item-type
229  (mapcar
230  (lambda (task)
231  (let ((todo-status (cdr (assoc-string item-type task))))
232  todo-status))
233  tasks)))
234  item-types)))
235 #+end_src
236 
237 * org-headlines-map :org:
238 :PROPERTIES:
239 :ID: 05955228-ca76-48bc-b769-f648b4310a9c
240 :END:
241 #+name: org-headlines-map
242 #+begin_src elisp
243  (org-element-map (org-element-parse-buffer 'headline)
244  'headline
245  (lambda(hl)
246  (let ((parent (org-element-property :parent hl)))
247  (and (eq (org-element-type parent) 'headline)
248  (list (org-element-property :title parent) (org-element-property :title hl))))))
249 
250 #+end_src
251 
252 * trim :util:
253 #+name: trim
254 #+begin_src bash :var str=""
255  # remove leading whitespace characters
256  str="${str#"${str%%[![:space:]]*}"}"
257  # remove trailing whitespace characters
258  str="${str%"${str##*[![:space:]]}"}"
259  printf '%s' "$str"
260 #+end_src
261 
262 #+RESULTS: trim
263 : a b c
264 
265 * skel-show
266 #+name: skel-show
267 #+begin_src shell :dir "~/comp/core" :var key=":id" :results replace
268  echo $(skel show $key)
269 #+end_src
270 
271 #+RESULTS: skel-show
272 : 15bc-b763-ec92-1cf5
273 
274 #+RESULTS:
275 : (core lisp rust emacs c)
276 * project-root :project:
277 #+name: project-root
278 #+begin_src emacs-lisp :var project=(project-current)
279  (project-root project)
280 #+end_src
281 
282 #+RESULTS: project-root
283 : ~/comp/org/meta/
284 
285 * project-name :project:
286 #+name: project
287 #+begin_src emacs-lisp :var project="."
288  (project-name (project-current nil project))
289 #+end_src
290 
291 #+RESULTS: project
292 : meta
293 
294 * project-vc :project:
295 #+name: project-vc
296 #+begin_src emacs-lisp :var project=(project-current)
297  (cadr project)
298 #+end_src
299 
300 #+RESULTS: project-vc
301 : Hg
302 
303 * project-list :project:
304 #+name: project-list
305 #+begin_src emacs-lisp
306  project--list
307 #+end_src
308 
309 #+RESULTS: project-list
310 | ~/comp/core/ |
311 | ~/comp/org/meta/ |
312 | ~/.emacs.d/elpa/corfu-terminal/ |
313 | ~/comp/core |
314 | ~/comp/scratch/zig-pg/ |
315 | ~/comp/scratch/wam/ |
316 | ~/comp/scratch/vdeplug4/ |
317 | ~/comp/scratch/tmux/ |
318 | ~/comp/scratch/scryer-prolog/ |
319 | ~/comp/scratch/screamer/ |
320 | ~/comp/scratch/rerun/ |
321 | ~/comp/scratch/r8k/ |
322 | ~/comp/scratch/qemu/ |
323 | ~/comp/scratch/octatrack/ |
324 | ~/comp/scratch/nu_plugin_quic/ |
325 | ~/comp/scratch/mpk/ |
326 | ~/comp/scratch/lemmy/ |
327 | ~/comp/scratch/io-uring/ |
328 | ~/comp/scratch/infodb/ |
329 | ~/comp/scratch/hy/ |
330 | ~/comp/scratch/hg/ |
331 | ~/comp/scratch/hackrf/ |
332 | ~/comp/scratch/gengat/ |
333 | ~/comp/scratch/fin/ |
334 | ~/comp/scratch/egui/ |
335 | ~/comp/scratch/egui-video/ |
336 | ~/comp/scratch/dev/ |
337 | ~/comp/scratch/davfs2/ |
338 | ~/comp/scratch/crust-0/ |
339 | ~/comp/scratch/cepl.examples/ |
340 | ~/comp/scratch/cc-install/ |
341 | ~/comp/scratch/blok/ |
342 | ~/comp/scratch/archiso/ |
343 | ~/comp/scratch/arch-boxes/ |
344 | ~/comp/scratch/ |
345 | ~/comp/pod/ |
346 | ~/comp/org/notes/ |
347 | ~/comp/org/docs/ |
348 | ~/comp/org/blog/ |
349 | ~/comp/org/archive/ |
350 | ~/comp/org/ |
351 | ~/comp/infra/.stash/src/emacs/ |
352 | ~/comp/infra/ |
353 | ~/comp/home/ |
354 | ~/comp/etc/ |
355 | ~/comp/demo/ |
356 | ~/comp/box/ |
357 | ~/.emacs.d/elpa/org-glossary/ |
358 | ~/comp/org/plan/ |
359 | ~/.emacs.d/elpa/eglot-x/ |
360 
361 * project-details :project:
362 #+name: project-details
363 #+begin_src emacs-lisp :var project="."
364  (let* ((project (project-current nil project))
365  (name (project-name project))
366  (root (project-root project))
367  (vc-type (downcase (symbol-name (cadr project))))
368  (age (org-sbe hg-log-age ''(dir root)))
369  (rev (org-sbe hg-rev ''(dir root)))
370  (num (org-sbe hg-id-num ''(dir root)))
371  (tags (org-sbe skel-show (key "':tags'") ''(dir root)))
372  (id (org-sbe skel-show (key "':id'") ''(dir root)))
373  (version (org-sbe skel-show (key "':version'") ''(dir root)))
374  (description (org-sbe skel-show (key "':description'") ''(dir root)))
375  (vc (format ":%s [[https://vc.compiler.company/%s][vc.compiler.company/%s]] :rev %s"
376  vc-type name name
377  (format "[[https://vc.compiler.company/%s/rev/%s][%s:%s]]" name rev rev num)))
378  (langs (mapcar (lambda (x) (downcase (format "%s" x))) (flatten (read (org-sbe langs ''(dir root))))))
379  (line-counts (flatten (read (org-sbe lines ''(dir root)))))
380  (line-sum (cl-reduce '+ line-counts))
381  (lines (format "%s :λ %s :# %s :_ %s" line-sum (pop line-counts) (pop line-counts) (pop line-counts))))
382  `(hline
383  (name ,name)
384  (version ,version)
385  (description ,description)
386  (skel-id ,id)
387  (root ,root)
388  (vc ,vc)
389  (tags ,tags)
390  (updated ,age)
391  (langs ,langs)
392  (lines ,lines)
393  hline))
394 #+end_src
395 
396 #+RESULTS: project-details
397 |-------------+---------------------------------------------------|
398 | name | meta |
399 | version | 0.1.0 |
400 | description | The Compiler Company Core |
401 | skel-id | 15bc-b763-ec92-1cf5 |
402 | root | ~/comp/org/meta/ |
403 | vc | :hg [[https://vc.compiler.company/meta][vc.compiler.company/meta]] :rev [[https://vc.compiler.company/meta/rev/06698c6708de][06698c6708de:8+]] |
404 | tags | (core lisp rust emacs c) |
405 | updated | 3 days ago |
406 | langs | (org) |
407 | lines | 1768 :λ 1632 :# 3 :_ 133 |
408 |-------------+---------------------------------------------------|
409 
410 * files :fs:
411 :PROPERTIES:
412 :ID: e2ff9dcf-8340-48b8-a1a6-e0036cbcc495
413 :END:
414 #+name: format-files
415 #+begin_src python :var tab="" :results table :colnames yes :hlines yes :exports results :eval no-export
416 return tab
417 #+end_src
418 
419 #+name: files
420 #+begin_src sh :results replace table :dir "." :colnames yes :hlines yes :post format-files-tbl(*this*)
421  ls -lh $PWD --time-style=long-iso \
422  |awk '{if (NR!=1) print $8, $5, $6"-"$7}' \
423  |awk 'BEGIN{print "file size updated"}{print $0}'
424 #+end_src
425 
426 #+RESULTS: files
427 | file | size | updated |
428 |---------------+------+------------------|
429 | babel.org | 24K | 2024-09-07-19:45 |
430 | business.org | 803 | 2024-08-19-21:19 |
431 | glossary.org | 1.2K | 2024-09-06-15:35 |
432 | mindset.org | 119 | 2024-08-15-21:20 |
433 | pitch.org | 1.6K | 2024-08-11-17:16 |
434 | readme.org | 1.1K | 2024-08-27-21:13 |
435 | style.org | 3.2K | 2024-08-21-16:18 |
436 | tech.org | 3.6K | 2024-08-21-18:16 |
437 | ulang.org | 12K | 2024-09-06-16:12 |
438 | workflows.org | 7.5K | 2024-09-03-16:59 |
439 
440 * env-table :os:
441 :PROPERTIES:
442 :ID: 4c824478-0eaa-49b9-905f-2c9a6d4220eb
443 :END:
444 #+begin_src sh :results table replace
445  for i in $(env);
446  do
447  echo "$i|" | sed '0,/=/s//|/'
448  done
449 #+end_src
450 
451 * get-env :os:
452 :PROPERTIES:
453 :ID: 8c1ecc68-ca25-4e72-81d8-415a43e59ae4
454 :END:
455 #+name: get-env
456 #+begin_src elisp :results output :var key="HOME"
457 (princ (getenv key))
458 #+end_src
459 
460 #+RESULTS: get-env
461 : /home/ellis
462 
463 * org-current-h1-title :org:
464 :PROPERTIES:
465 :ID: ae61e7ed-c9ed-414c-8a5f-12b1702f018e
466 :END:
467 #+name: org-current-h1-title
468 #+begin_src emacs-lisp :results value
469  (org-element-property :title (save-excursion (org-up-heading-safe) (org-element-at-point)))
470 #+end_src
471 
472 #+RESULTS: org-current-h1-title
473 : org-current-h1-title
474 
475 * get-emacs-version :emacs:
476 :PROPERTIES:
477 :ID: af3d83a1-31bd-41b4-be35-c1f33507fd8d
478 :END:
479 #+name: get-emacs-version
480 #+begin_src elisp :results output
481  (princ (concat (format "%s\n" (emacs-version))
482  (format "Org v%s" (org-version))))
483 #+end_src
484 
485 * vc-buffer-log :vc:
486 :PROPERTIES:
487 :ID: b2d9f6f0-2ac7-4e45-abbe-b32cc2d0f7aa
488 :END:
489 #+name: vc-buffer-log
490 #+header: :var limit=-1
491 #+header: :var buf=(buffer-name (current-buffer))
492 #+begin_src emacs-lisp
493 ;; Most of this code is copied from vc.el vc-print-log
494 (require 'vc)
495 (when (vc-find-backend-function
496  (vc-backend (buffer-file-name (get-buffer buf))) 'print-log)
497  (let ((limit -1)
498  (vc-fileset nil)
499  (backend nil)
500  (files nil))
501  (with-current-buffer (get-buffer buf)
502  (setq vc-fileset (vc-deduce-fileset t)) ; FIXME: Why t? --Stef
503  (setq backend (car vc-fileset))
504  (setq files (cadr vc-fileset)))
505  (with-temp-buffer
506  (let ((status (vc-call-backend
507  backend 'print-log files (current-buffer))))
508  (when (and (processp status) ; Make sure status is a process
509  (= 0 (process-exit-status status))) ; which has not terminated
510  (while (not (eq 'exit (process-status status)))
511  (sit-for 1 t)))
512  (buffer-string)))))
513 #+end_src
514 * hg-rev :vc:
515 :PROPERTIES:
516 :ID: 8119cf43-f2e7-4829-939c-fc4e8531ae6c
517 :END:
518 #+name: hg-rev
519 #+begin_src sh :dir "."
520 hg log -l 1 --template '{node|short}'
521 #+end_src
522 
523 #+RESULTS: hg-rev
524 : 06698c6708de
525 * hg-id-num :vc:
526 :PROPERTIES:
527 :ID: 9602faee-5522-445b-a568-be603e20a978
528 :END:
529 #+name: hg-id-num
530 #+begin_src shell :dir "."
531 hg id -n
532 #+end_src
533 
534 #+RESULTS: hg-id-num
535 : 8+
536 
537 * hg-log-age :vc:
538 :PROPERTIES:
539 :ID: 8492f4fb-51a6-4221-8705-a15eb5a50ed4
540 :END:
541 #+name: hg-log-age
542 #+begin_src shell :dir "."
543  hg log -l1 --template "{date|age}"
544 #+end_src
545 
546 #+RESULTS: hg-log-age
547 : 3 days ago
548 
549 * hg-churn :vc:
550 #+name: hg-churn
551 #+begin_src shell :dir "~/comp/core" :results output
552  hg churn -f "%Y-%m" -s
553 #+end_src
554 
555 #+RESULTS: hg-churn
556 #+begin_example
557 2023-10 36394 ************************************
558 2023-11 12777 ************
559 2023-12 61624 *************************************************************
560 2024-01 4923 ****
561 2024-02 5701 *****
562 2024-03 27292 ***************************
563 2024-04 23322 ***********************
564 2024-05 24141 ***********************
565 2024-06 17608 *****************
566 2024-07 9757 *********
567 2024-08 16177 ****************
568 2024-09 2430 **
569 #+end_example
570 
571 * hg-status
572 #+name: hg-status
573 #+begin_src shell :dir "."
574 hg status
575 #+end_src
576 
577 #+RESULTS: hg-status
578 | M | babel.org |
579 | M | glossary.org |
580 | M | ulang.org |
581 
582 * homer
583 #+begin_src shell
584 homer
585 #+end_src
586 
587 #+RESULTS:
588 | :PUSH | /home/ellis/.stash/scripts/wg-gen-keys.sh |
589 | :PUSH | /home/ellis/.stash/scripts/upgrade.sh |
590 | :PUSH | /home/ellis/.stash/scripts/sc.sh |
591 | :PUSH | /home/ellis/.stash/scripts/rec.sh |
592 | :PUSH | /home/ellis/.stash/scripts/port-scan.sh |
593 | :PUSH | /home/ellis/.stash/scripts/podman-machine-default-update.sh |
594 | :PUSH | /home/ellis/.stash/scripts/pacman-pkgsearch.sh |
595 | :PUSH | /home/ellis/.stash/scripts/nfs-export.sh |
596 | :PUSH | /home/ellis/.stash/scripts/new-mail.sh |
597 | :PUSH | /home/ellis/.stash/scripts/genfstab.sh |
598 | :PUSH | /home/ellis/.stash/scripts/gen-libera-cert.sh |
599 
600 * sh-ob-tangle :org:
601 :PROPERTIES:
602 :ID: 7b311df4-83a3-489d-89a0-929928bce051
603 :END:
604 #+name: sh-ob-tangle
605 #+begin_src sh
606  emacs -Q --batch --eval "
607  (progn
608  (require 'ob-tangle)
609  (dolist (file command-line-args-left)
610  (with-current-buffer (find-file-noselect file)
611  (org-babel-tangle))))
612  " "$@"
613 #+end_src
614 
615 * make-dot-tree :dot:
616 :PROPERTIES:
617 :ID: 5588f446-2d7a-4261-b829-68effd3778ac
618 :END:
619 #+name: make-dot-tree
620 #+begin_src emacs-lisp :var table=org-headlines-map() :results output
621  (mapcar #'(lambda (x)
622  (princ (format "\"%s\" -> \"%s\";\n" (cl-first x) (cl-second x))))
623  table)
624 #+end_src
625 
626 * gen-dot-tree :dot:
627 :PROPERTIES:
628 :ID: a51c943d-0f01-4c8f-96ec-db28ae7fef26
629 :END:
630 #+name: gen-dot-tree
631 #+begin_src dot :file /tmp/tree.png :cmdline -Kdot -Tpng :var input=make-dot-tree() :eval no-export
632 digraph {
633  rankdir=TB;
634  splines=true;
635  node [shape=box];
636  $input
637  }
638 #+end_src
639 
640 * user-slime :lisp:
641 :PROPERTIES:
642 :ID: 9ffd1d10-ffad-486e-9d7d-82422342b9ff
643 :END:
644 #+name: user-slime
645 #+begin_src emacs-lisp :results silent :eval no-export
646  (unless (slime-connected-p) (slime))
647  (slime-eval '(ql:quickload :user))
648  (slime-repl-set-package "USER")
649 #+end_src
650 * std-slime :lisp:
651 :PROPERTIES:
652 :ID: 334dae41-5c35-48bd-8368-71d79f5e48d8
653 :END:
654 #+name: std-slime
655 #+begin_src emacs-lisp :results silent :eval no-export
656  (unless (slime-connected-p) (slime))
657  (slime-eval '(ql:quickload :std))
658  (slime-repl-set-package "STD-USER")
659 #+end_src
660 * test-slime :lisp:
661 :PROPERTIES:
662 :ID: ff559b09-1c18-422c-a0ae-681180f685cf
663 :END:
664 #+name: std-slime
665 #+begin_src emacs-lisp :results silent :eval no-export
666  (unless (slime-connected-p) (slime))
667  (slime-eval '(ql:quickload :core/tests))
668  (slime-repl-set-package "CORE/TESTS")
669 #+end_src
670 * cargo-update-dir :rust:
671 :PROPERTIES:
672 :ID: 67dc87bb-a27b-46e4-a02f-58daac514630
673 :END:
674 #+name: cargo-update-dir
675 #+begin_src sh :var dir=()
676 # update all crates in dir
677 set -eu
678 case $0 in
679  (/*) dir=${0%/*}/;;
680  (*/*) dir=./${0%/*};;
681  (*) dir=.;;
682 esac
683 
684 find "$dir/.." -name Cargo.toml -execdir cargo update \;
685 #+end_src
686 * rust-target-triple :rust:
687 :PROPERTIES:
688 :ID: 02f96ff2-c607-4889-979c-943203b8ad65
689 :END:
690  #+name: rust-target-triple
691  #+begin_src shell
692  rustc -vV | sed -n -e 's/^host: //p'
693  #+end_src
694 * post-align-table :table:
695 :PROPERTIES:
696 :ID: d8a3cc05-a4ec-4820-935d-63f676ee32a3
697 :END:
698 #+NAME: post-align-tables
699 #+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"
700 #+BEGIN_SRC emacs-lisp :results value :exports both
701  (with-temp-buffer
702  (erase-buffer)
703  (cl-assert text nil "PostAlignTables received nil instead of text ")
704  (insert text)
705  (beginning-of-buffer)
706  (org-mode)
707  (while
708  (search-forward-regexp org-table-any-line-regexp nil t)
709  (org-table-align)
710  (org-table-recalculate 'iterate)
711  (goto-char (org-table-end)))
712  (buffer-string))
713 #+END_SRC
714 * insert-table-from-file :table:fs:
715 :PROPERTIES:
716 :ID: bde8705d-0969-493d-91b9-51a93ae09efa
717 :END:
718 #+NAME: insert-table-from-file
719 #+HEADER: :var tname="table" fname="/tmp/tbl.org" newcaption="" newattr="" newname=""
720 #+BEGIN_SRC elisp :results output drawer
721  (let* ((klist (cl-remove-if (lambda (x) (equal (cadr x) ""))
722  `(("ATTR_LATEX" ,newattr) ("CAPTION" ,newcaption) ("NAME" ,newname))))
723  (tbl
724  (with-temp-buffer
725  (org-mode)
726  (insert-file-contents fname)
727  (goto-char (point-min))
728  (unless (re-search-forward
729  (concat "^[ \t]*#\\+\\(tbl\\)?name:[ \t]*"
730  (regexp-quote tname) "[ \t]*$")
731  nil t)
732  (user-error "Can't find table named %s in file" tname fname))
733  (forward-line 0)
734  (let ((tstart (match-beginning 0))
735  tend)
736  (while (looking-at "^[ \t]*#\\+\\([^:]+\\): *\\(.*\\)")
737  (add-to-list 'klist `(,(upcase (match-string 1)) ,(match-string 2)))
738  (delete-region (point) (line-end-position))
739  (kill-line))
740  (unless (looking-at org-table-line-regexp)
741  (looking-at "^.*$")
742  (user-error "no table at location of %s, Looking-at: '%s'" tname (match-string 0)))
743  (goto-char (org-table-end))
744  (while (looking-at-p "^[ \t]*#\\+TBLFM:")
745  (forward-line 1))
746  (buffer-substring tstart (point))))))
747  (setq klist (nreverse klist)) ;; reverse for giving priority to new user settings
748  (dolist (elem '("NAME" "CAPTION" "ATTR_LATEX"))
749  (when (assoc elem klist)
750  (princ (format "#+%s: %s\n" elem (cadr (assoc elem klist))))))
751  (princ tbl))
752 #+END_SRC
753 * filter-table :table:
754 :PROPERTIES:
755 :ID: cec80330-99c3-4dab-9b13-bb0a520ace73
756 :END:
757 #+NAME: filter-table
758 #+HEADER: :var tbl="" col=0 vals=""
759 #+BEGIN_SRC elisp :results value :colnames y
760  (let ((lst (split-string vals)))
761  (concatenate 'list (loop for row in tbl
762  if (member (let ((field (nth col row)))
763  (if (numberp field)
764  (number-to-string field)
765  field)) lst)
766  collect row into newtbl
767  ;; else do (princ (format "%s: %s\n" (nth col row) lst))
768  finally return newtbl)))
769 #+END_SRC
770 * filter-table-re :table:rx:
771 :PROPERTIES:
772 :ID: 61e82902-3108-4cee-8e02-3e9d6770f5cb
773 :END:
774 #+NAME: filter-table-re
775 #+HEADER: :var tbl="" col=0 vals=".*"
776 #+BEGIN_SRC elisp :results value :colnames y
777  (let ((lst (split-string vals)))
778  (concatenate 'list (loop for row in tbl
779  if (let* ((rawfield (nth col row))
780  (field (if (numberp rawfield)
781  (number-to-string rawfield)
782  rawfield)))
783  (loop for regx in lst
784  when(string-match-p regx field) return 't
785  finally return nil))
786  collect row into newtbl
787  ;; else do (princ (format "%s: %s\n" (nth col row) lst))
788  finally return newtbl)))
789 #+END_SRC
790 * group-table :table:
791 :PROPERTIES:
792 :ID: d1cb8d5d-14bc-4fbe-baf0-ff819d51ec4c
793 :END:
794 #+NAME: group-table
795 #+HEADER: :var tbl="" grp="Name" op="sum" rescols="B"
796 #+BEGIN_SRC python :results output verbatim drawer :colnames no
797  import pandas as pd
798  import numpy as np
799  import orgbabelhelper as obh
800  import sys
801  import re
802 
803  df = obh.orgtable_to_dataframe(tbl)
804  grparr = re.split(r",\s*", grp)
805  #print re.split(r",\s*", rescols) + [grp]
806  df = df[re.split(r",\s*", rescols) + grparr]
807  for elem in grparr:
808  assert elem in df.columns, "Error: group column %s not in table columns %s" % (elem, ",".join(df.columns))
809 
810  if op == "sum":
811  res = df.groupby(grparr).sum()
812  else:
813  error("operation %s not implemented" % op)
814  sys.exit(1)
815 
816  print(obh.dataframe_to_orgtable(res))
817 
818 #+END_SRC
819 * insert-file :fs:
820 :PROPERTIES:
821 :ID: 57196c4d-a275-4023-8462-a5847676347c
822 :END:
823 #+NAME: lobInsertFile
824 #+HEADER: :var filename="/tmp/foo"
825 #+begin_src elisp :results value raw drawer
826  (cl-labels ((wrap-src
827  (lang)
828  (list (format "#+BEGIN_SRC %s :eval never :exports source\n" lang)
829  "#+END_SRC\n")))
830  (let ((wrappers
831  (pcase (file-name-extension filename)
832  ("py" (wrap-src "python"))
833  (".el" (wrap-src "emacs-lisp"))
834  (t '("#+BEGIN_EXAMPLE\n" "#+END_EXAMPLE\n")))))
835  (with-temp-buffer
836  (goto-char (point-min))
837  (insert (format-time-string "# inserted at %Y-%m-%d %H:%M:%S\n"))
838  (insert (car wrappers))
839  (insert-file-contents filename)
840  (goto-char (point-max))
841  (insert (car (cdr wrappers)))
842  (buffer-string))))
843 #+end_src