changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > org > meta / babel.org

changeset 11: 6e53b8dc7795
parent: 65bf0057ca35
child: f6df7eaa3684
author: Richard Westhaver <ellis@rwest.io>
date: Fri, 20 Sep 2024 20:00:07 -0400
permissions: -rw-r--r--
description: bump
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 * files :fs:
266 :PROPERTIES:
267 :ID: e2ff9dcf-8340-48b8-a1a6-e0036cbcc495
268 :END:
269 #+name: format-files
270 #+begin_src python :var tab="" :results table :colnames yes :hlines yes :exports results :eval no-export
271 return tab
272 #+end_src
273 
274 #+name: files
275 #+begin_src sh :results replace table :dir "." :colnames yes :hlines yes :post format-files(*this*)
276  ls -lh $PWD --time-style=long-iso \
277  |awk '{if (NR!=1) print $8, $5, $6"-"$7}' \
278  |awk 'BEGIN{print "file size updated"}{print $0}'
279 #+end_src
280 
281 #+RESULTS: files
282 | file | size | updated |
283 |---------------+------+------------------|
284 | babel.org | 24K | 2024-09-07-19:45 |
285 | business.org | 803 | 2024-08-19-21:19 |
286 | glossary.org | 1.2K | 2024-09-06-15:35 |
287 | mindset.org | 119 | 2024-08-15-21:20 |
288 | pitch.org | 1.6K | 2024-08-11-17:16 |
289 | readme.org | 1.1K | 2024-08-27-21:13 |
290 | style.org | 3.2K | 2024-08-21-16:18 |
291 | tech.org | 3.6K | 2024-08-21-18:16 |
292 | ulang.org | 12K | 2024-09-06-16:12 |
293 | workflows.org | 7.5K | 2024-09-03-16:59 |
294 
295 * skel-show :skel:
296 #+name: skel-show
297 #+begin_src shell :var key="id" :results replace
298  skel show $key
299 #+end_src
300 
301 #+RESULTS: skel-show
302 
303 #+RESULTS:
304 : (core lisp rust emacs c)
305 * project-root :project:
306 #+name: project-root
307 #+begin_src emacs-lisp :var project=(project-current)
308  (project-root project)
309 #+end_src
310 
311 #+RESULTS: project-root
312 : ~/comp/org/meta/
313 
314 * project-name :project:
315 #+name: project
316 #+begin_src emacs-lisp :var project="."
317  (project-name (project-current nil project))
318 #+end_src
319 
320 #+RESULTS: project
321 : meta
322 
323 * project-vc :project:
324 #+name: project-vc
325 #+begin_src emacs-lisp :var project=(project-current)
326  (cadr project)
327 #+end_src
328 
329 #+RESULTS: project-vc
330 : Hg
331 
332 * project-list :project:
333 #+name: project-list
334 #+begin_src emacs-lisp
335  project--list
336 #+end_src
337 
338 #+RESULTS: project-list
339 | ~/comp/core/ |
340 | ~/comp/org/meta/ |
341 | ~/.emacs.d/elpa/corfu-terminal/ |
342 | ~/comp/core |
343 | ~/comp/scratch/zig-pg/ |
344 | ~/comp/scratch/wam/ |
345 | ~/comp/scratch/vdeplug4/ |
346 | ~/comp/scratch/tmux/ |
347 | ~/comp/scratch/scryer-prolog/ |
348 | ~/comp/scratch/screamer/ |
349 | ~/comp/scratch/rerun/ |
350 | ~/comp/scratch/r8k/ |
351 | ~/comp/scratch/qemu/ |
352 | ~/comp/scratch/octatrack/ |
353 | ~/comp/scratch/nu_plugin_quic/ |
354 | ~/comp/scratch/mpk/ |
355 | ~/comp/scratch/lemmy/ |
356 | ~/comp/scratch/io-uring/ |
357 | ~/comp/scratch/infodb/ |
358 | ~/comp/scratch/hy/ |
359 | ~/comp/scratch/hg/ |
360 | ~/comp/scratch/hackrf/ |
361 | ~/comp/scratch/gengat/ |
362 | ~/comp/scratch/fin/ |
363 | ~/comp/scratch/egui/ |
364 | ~/comp/scratch/egui-video/ |
365 | ~/comp/scratch/dev/ |
366 | ~/comp/scratch/davfs2/ |
367 | ~/comp/scratch/crust-0/ |
368 | ~/comp/scratch/cepl.examples/ |
369 | ~/comp/scratch/cc-install/ |
370 | ~/comp/scratch/blok/ |
371 | ~/comp/scratch/archiso/ |
372 | ~/comp/scratch/arch-boxes/ |
373 | ~/comp/scratch/ |
374 | ~/comp/pod/ |
375 | ~/comp/org/notes/ |
376 | ~/comp/org/docs/ |
377 | ~/comp/org/blog/ |
378 | ~/comp/org/archive/ |
379 | ~/comp/org/ |
380 | ~/comp/infra/.stash/src/emacs/ |
381 | ~/comp/infra/ |
382 | ~/comp/home/ |
383 | ~/comp/etc/ |
384 | ~/comp/demo/ |
385 | ~/comp/box/ |
386 | ~/.emacs.d/elpa/org-glossary/ |
387 | ~/comp/org/plan/ |
388 | ~/.emacs.d/elpa/eglot-x/ |
389 
390 * project-details :project:
391 #+name: project-details
392 #+begin_src emacs-lisp :var project="."
393  (let* ((project (project-current nil project))
394  (name (project-name project))
395  (root (project-root project))
396  (default-directory root)
397  (vc-type (downcase (symbol-name (cadr project))))
398  (age (org-sbe hg-log-age ''(dir root)))
399  (rev (org-sbe hg-rev ''(dir root)))
400  (num (org-sbe hg-id-num ''(dir root)))
401  (tags (org-sbe skel-show (key "'tags'")))
402  (id (org-sbe skel-show (key "':id'") ''(dir root)))
403  (version (org-sbe skel-show (key "'version'") ''(dir root)))
404  (description (org-sbe skel-show (key "'description'") ''(dir root)))
405  (vc (format ":%s [[https://vc.compiler.company/%s][vc.compiler.company/%s]] :rev %s"
406  vc-type name name
407  (format "[[https://vc.compiler.company/%s/rev/%s][%s:%s]]" name rev rev num)))
408  (langs (mapcar (lambda (x) (downcase (format "%s" x))) (flatten (read (org-sbe langs ''(dir root))))))
409  (line-counts (flatten (read (org-sbe lines ''(dir root)))))
410  (line-sum (cl-reduce '+ line-counts))
411  (lines (format "%s :λ %s :# %s :_ %s" line-sum (pop line-counts) (pop line-counts) (pop line-counts))))
412  `(hline
413  (name ,name)
414  (version ,version)
415  (description ,description)
416  (skel-id ,id)
417  (root ,root)
418  (vc ,vc)
419  (tags ,tags)
420  (updated ,age)
421  (langs ,langs)
422  (lines ,lines)
423  hline))
424 #+end_src
425 
426 #+RESULTS: project-details
427 |-------------+---------------------------------------------------|
428 | name | meta |
429 | version | |
430 | description | |
431 | skel-id | |
432 | root | ~/comp/org/meta/ |
433 | vc | :hg [[https://vc.compiler.company/meta][vc.compiler.company/meta]] :rev [[https://vc.compiler.company/meta/rev/995df3d48af0][995df3d48af0:9+]] |
434 | tags | |
435 | updated | Sat Sep 07 22:38:02 2024 -0400 |
436 | langs | (org) |
437 | lines | 1828 :λ 1685 :# 4 :_ 139 |
438 |-------------+---------------------------------------------------|
439 
440 * project-files :project:fs:
441 List all files found in current project. Ignores files which don't
442 exist locally.
443 #+name: project-files
444 #+begin_src emacs-lisp :var project="." :results replace
445  (let* ((project (project-current nil project))
446  (name (project-name project))
447  (root (expand-file-name (project-root project)))
448  (project-files-relative-names t)
449  (project-vc-include-untracked nil))
450  `((file size modified)
451  hline
452  ,@(mapcar (lambda (x)
453  ;; don't rely on project/vc - only include files which exist locally
454  (when-let ((attr (file-attributes x)))
455  (list (format "[[https://vc.compiler.company/%s/file/tip/%s][%s]]" name x x)
456  (when-let ((size (file-attribute-size attr)))
457  (file-size-human-readable size))
458  (format-time-string "%Y-%m-%d %H:%M:%S" (file-attribute-modification-time attr)))))
459  (project-files project))))
460 #+end_src
461 
462 #+RESULTS: project-files
463 | file | size | modified |
464 |---------------+------+---------------------|
465 | [[https://vc.compiler.company/meta/file/tip/babel.org][babel.org]] | 30k | 2024-09-08 20:35:41 |
466 | [[https://vc.compiler.company/meta/file/tip/.hgignore][.hgignore]] | 7 | 2024-08-11 10:08:57 |
467 | [[https://vc.compiler.company/meta/file/tip/business.org][business.org]] | 803 | 2024-08-19 21:19:33 |
468 | [[https://vc.compiler.company/meta/file/tip/glossary.org][glossary.org]] | 1.1k | 2024-09-06 15:35:52 |
469 | [[https://vc.compiler.company/meta/file/tip/mindset.org][mindset.org]] | 119 | 2024-08-15 21:20:13 |
470 | [[https://vc.compiler.company/meta/file/tip/pitch.org][pitch.org]] | 1.6k | 2024-08-11 17:16:04 |
471 | [[https://vc.compiler.company/meta/file/tip/readme.org][readme.org]] | 1k | 2024-08-27 21:13:01 |
472 | [[https://vc.compiler.company/meta/file/tip/style.org][style.org]] | 3.2k | 2024-08-21 16:18:55 |
473 | [[https://vc.compiler.company/meta/file/tip/tech.org][tech.org]] | 3.5k | 2024-08-21 18:16:26 |
474 | [[https://vc.compiler.company/meta/file/tip/ulang.org][ulang.org]] | 11k | 2024-09-06 16:12:00 |
475 | [[https://vc.compiler.company/meta/file/tip/workflows.org][workflows.org]] | 7.4k | 2024-09-03 16:59:05 |
476 
477 * project-tasks :project:org:
478 List all project tasks.
479 #+name: project-tasks
480 #+begin_src emacs-lisp :results replace :var project="."
481  (let ((name (project-name (project-current nil project)))
482  (hdr (list '(todo item tags scheduled deadline)
483  'hline))
484  (tbl))
485  (save-excursion
486  (with-current-buffer (find-file-noselect
487  (join-paths company-org-directory
488  "plan/tasks/"
489  (format "%s.org" name)))
490  (org-with-wide-buffer
491  (org-map-entries (lambda ()
492  (let ((row))
493  (push (or (org-get-todo-state) "") row)
494  (push (org-get-heading t t t t) row)
495  (push (or (remove name (org-get-tags)) "") row)
496  (push (if-let ((ts (org-get-scheduled-time (point) t)))
497  (format-time-string (cdr org-timestamp-formats) ts)
498  "")
499  row)
500  (push (if-let ((ts (org-get-deadline-time (point) t)))
501  (format-time-string (cdr org-timestamp-formats) ts)
502  "")
503  row)
504  (push (nreverse row) tbl)))
505  "TODO<>\"DONE\"" nil)
506  )))
507  (append hdr (nreverse tbl))
508  )
509 #+end_src
510 
511 #+RESULTS: project-tasks
512 | todo | item | tags | scheduled | deadline |
513 |------+------+------+-----------+----------|
514 
515 * env-table :os:
516 :PROPERTIES:
517 :ID: 4c824478-0eaa-49b9-905f-2c9a6d4220eb
518 :END:
519 #+begin_src sh :results table replace
520  for i in $(env);
521  do
522  echo "$i|" | sed '0,/=/s//|/'
523  done
524 #+end_src
525 
526 * get-env :os:
527 :PROPERTIES:
528 :ID: 8c1ecc68-ca25-4e72-81d8-415a43e59ae4
529 :END:
530 #+name: get-env
531 #+begin_src elisp :results output :var key="HOME"
532 (princ (getenv key))
533 #+end_src
534 
535 #+RESULTS: get-env
536 : /home/ellis
537 
538 * org-current-h1-title :org:
539 :PROPERTIES:
540 :ID: ae61e7ed-c9ed-414c-8a5f-12b1702f018e
541 :END:
542 #+name: org-current-h1-title
543 #+begin_src emacs-lisp :results value
544  (org-element-property :title (save-excursion (org-up-heading-safe) (org-element-at-point)))
545 #+end_src
546 
547 #+RESULTS: org-current-h1-title
548 : org-current-h1-title
549 
550 * get-emacs-version :emacs:
551 :PROPERTIES:
552 :ID: af3d83a1-31bd-41b4-be35-c1f33507fd8d
553 :END:
554 #+name: get-emacs-version
555 #+begin_src elisp :results output
556  (princ (concat (format "%s\n" (emacs-version))
557  (format "Org v%s" (org-version))))
558 #+end_src
559 
560 * vc-buffer-log :vc:
561 :PROPERTIES:
562 :ID: b2d9f6f0-2ac7-4e45-abbe-b32cc2d0f7aa
563 :END:
564 #+name: vc-buffer-log
565 #+header: :var limit=-1
566 #+header: :var buf=(buffer-name (current-buffer))
567 #+begin_src emacs-lisp
568 ;; Most of this code is copied from vc.el vc-print-log
569 (require 'vc)
570 (when (vc-find-backend-function
571  (vc-backend (buffer-file-name (get-buffer buf))) 'print-log)
572  (let ((limit -1)
573  (vc-fileset nil)
574  (backend nil)
575  (files nil))
576  (with-current-buffer (get-buffer buf)
577  (setq vc-fileset (vc-deduce-fileset t)) ; FIXME: Why t? --Stef
578  (setq backend (car vc-fileset))
579  (setq files (cadr vc-fileset)))
580  (with-temp-buffer
581  (let ((status (vc-call-backend
582  backend 'print-log files (current-buffer))))
583  (when (and (processp status) ; Make sure status is a process
584  (= 0 (process-exit-status status))) ; which has not terminated
585  (while (not (eq 'exit (process-status status)))
586  (sit-for 1 t)))
587  (buffer-string)))))
588 #+end_src
589 * hg-rev :vc:
590 :PROPERTIES:
591 :ID: 8119cf43-f2e7-4829-939c-fc4e8531ae6c
592 :END:
593 #+name: hg-rev
594 #+begin_src sh :dir "."
595 hg log -l 1 --template '{node|short}'
596 #+end_src
597 
598 #+RESULTS: hg-rev
599 : 06698c6708de
600 * hg-id-num :vc:
601 :PROPERTIES:
602 :ID: 9602faee-5522-445b-a568-be603e20a978
603 :END:
604 #+name: hg-id-num
605 #+begin_src shell :dir "."
606 hg id -n
607 #+end_src
608 
609 #+RESULTS: hg-id-num
610 : 8+
611 
612 * hg-log-since :vc:
613 :PROPERTIES:
614 :ID: 8492f4fb-51a6-4221-8705-a15eb5a50ed4
615 :END:
616 #+name: hg-log-since
617 #+begin_src shell :dir "."
618  hg log -l1 --template "{date|age}"
619 #+end_src
620 
621 #+RESULTS: hg-log-since
622 : 3 days ago
623 
624 * hg-log-age :vc:
625 #+name: hg-log-age
626 #+begin_src shell :dir "."
627  # hg log -l1 --template "{date(date, '%Y-%m-%d %H:%M:%S')}\n"
628  hg log -l1 --template "{date(date)}\n"
629 #+end_src
630 
631 #+RESULTS: hg-log-age
632 : Sat Sep 07 22:38:02 2024 -0400
633 
634 * hg-churn :vc:
635 #+name: hg-churn
636 #+begin_src shell :dir "~/comp/core" :results output
637  hg churn -f "%Y-%m" -s
638 #+end_src
639 
640 #+RESULTS: hg-churn
641 #+begin_example
642 2023-10 36394 ************************************
643 2023-11 12777 ************
644 2023-12 61624 *************************************************************
645 2024-01 4923 ****
646 2024-02 5701 *****
647 2024-03 27292 ***************************
648 2024-04 23322 ***********************
649 2024-05 24141 ***********************
650 2024-06 17608 *****************
651 2024-07 9757 *********
652 2024-08 16177 ****************
653 2024-09 2430 **
654 #+end_example
655 
656 * hg-status :vc:
657 #+name: hg-status
658 #+begin_src shell :dir "."
659 hg status
660 #+end_src
661 
662 #+RESULTS: hg-status
663 | M | babel.org |
664 | M | glossary.org |
665 | M | ulang.org |
666 
667 * homer :homer:
668 #+begin_src shell
669 homer
670 #+end_src
671 
672 #+RESULTS:
673 | :PUSH | /home/ellis/.stash/scripts/wg-gen-keys.sh |
674 | :PUSH | /home/ellis/.stash/scripts/upgrade.sh |
675 | :PUSH | /home/ellis/.stash/scripts/sc.sh |
676 | :PUSH | /home/ellis/.stash/scripts/rec.sh |
677 | :PUSH | /home/ellis/.stash/scripts/port-scan.sh |
678 | :PUSH | /home/ellis/.stash/scripts/podman-machine-default-update.sh |
679 | :PUSH | /home/ellis/.stash/scripts/pacman-pkgsearch.sh |
680 | :PUSH | /home/ellis/.stash/scripts/nfs-export.sh |
681 | :PUSH | /home/ellis/.stash/scripts/new-mail.sh |
682 | :PUSH | /home/ellis/.stash/scripts/genfstab.sh |
683 | :PUSH | /home/ellis/.stash/scripts/gen-libera-cert.sh |
684 
685 * sh-ob-tangle :org:
686 :PROPERTIES:
687 :ID: 7b311df4-83a3-489d-89a0-929928bce051
688 :END:
689 #+name: sh-ob-tangle
690 #+begin_src sh
691  emacs -Q --batch --eval "
692  (progn
693  (require 'ob-tangle)
694  (dolist (file command-line-args-left)
695  (with-current-buffer (find-file-noselect file)
696  (org-babel-tangle))))
697  " "$@"
698 #+end_src
699 
700 * make-dot-tree :dot:
701 :PROPERTIES:
702 :ID: 5588f446-2d7a-4261-b829-68effd3778ac
703 :END:
704 #+name: make-dot-tree
705 #+begin_src emacs-lisp :var table=org-headlines-map() :results output
706  (mapcar #'(lambda (x)
707  (princ (format "\"%s\" -> \"%s\";\n" (cl-first x) (cl-second x))))
708  table)
709 #+end_src
710 
711 * gen-dot-tree :dot:
712 :PROPERTIES:
713 :ID: a51c943d-0f01-4c8f-96ec-db28ae7fef26
714 :END:
715 #+name: gen-dot-tree
716 #+begin_src dot :file /tmp/tree.png :cmdline -Kdot -Tpng :var input=make-dot-tree() :eval no-export
717 digraph {
718  rankdir=TB;
719  splines=true;
720  node [shape=box];
721  $input
722  }
723 #+end_src
724 
725 * user-slime :lisp:
726 :PROPERTIES:
727 :ID: 9ffd1d10-ffad-486e-9d7d-82422342b9ff
728 :END:
729 #+name: user-slime
730 #+begin_src emacs-lisp :results silent :eval no-export
731  (unless (slime-connected-p) (slime))
732  (slime-eval '(ql:quickload :user))
733  (slime-repl-set-package "USER")
734 #+end_src
735 * std-slime :lisp:
736 :PROPERTIES:
737 :ID: 334dae41-5c35-48bd-8368-71d79f5e48d8
738 :END:
739 #+name: std-slime
740 #+begin_src emacs-lisp :results silent :eval no-export
741  (unless (slime-connected-p) (slime))
742  (slime-eval '(ql:quickload :std))
743  (slime-repl-set-package "STD-USER")
744 #+end_src
745 * test-slime :lisp:
746 :PROPERTIES:
747 :ID: ff559b09-1c18-422c-a0ae-681180f685cf
748 :END:
749 #+name: std-slime
750 #+begin_src emacs-lisp :results silent :eval no-export
751  (unless (slime-connected-p) (slime))
752  (slime-eval '(ql:quickload :core/tests))
753  (slime-repl-set-package "CORE/TESTS")
754 #+end_src
755 * cargo-update-dir :rust:
756 :PROPERTIES:
757 :ID: 67dc87bb-a27b-46e4-a02f-58daac514630
758 :END:
759 #+name: cargo-update-dir
760 #+begin_src sh :var dir=()
761 # update all crates in dir
762 set -eu
763 case $0 in
764  (/*) dir=${0%/*}/;;
765  (*/*) dir=./${0%/*};;
766  (*) dir=.;;
767 esac
768 
769 find "$dir/.." -name Cargo.toml -execdir cargo update \;
770 #+end_src
771 * rust-target-triple :rust:
772 :PROPERTIES:
773 :ID: 02f96ff2-c607-4889-979c-943203b8ad65
774 :END:
775  #+name: rust-target-triple
776  #+begin_src shell
777  rustc -vV | sed -n -e 's/^host: //p'
778  #+end_src
779 * post-align-table :table:
780 :PROPERTIES:
781 :ID: d8a3cc05-a4ec-4820-935d-63f676ee32a3
782 :END:
783 #+NAME: post-align-tables
784 #+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"
785 #+BEGIN_SRC emacs-lisp :results value :exports both
786  (with-temp-buffer
787  (erase-buffer)
788  (cl-assert text nil "PostAlignTables received nil instead of text ")
789  (insert text)
790  (beginning-of-buffer)
791  (org-mode)
792  (while
793  (search-forward-regexp org-table-any-line-regexp nil t)
794  (org-table-align)
795  (org-table-recalculate 'iterate)
796  (goto-char (org-table-end)))
797  (buffer-string))
798 #+END_SRC
799 * insert-table-from-file :table:fs:
800 :PROPERTIES:
801 :ID: bde8705d-0969-493d-91b9-51a93ae09efa
802 :END:
803 #+NAME: insert-table-from-file
804 #+HEADER: :var tname="table" fname="/tmp/tbl.org" newcaption="" newattr="" newname=""
805 #+BEGIN_SRC elisp :results output drawer
806  (let* ((klist (cl-remove-if (lambda (x) (equal (cadr x) ""))
807  `(("ATTR_LATEX" ,newattr) ("CAPTION" ,newcaption) ("NAME" ,newname))))
808  (tbl
809  (with-temp-buffer
810  (org-mode)
811  (insert-file-contents fname)
812  (goto-char (point-min))
813  (unless (re-search-forward
814  (concat "^[ \t]*#\\+\\(tbl\\)?name:[ \t]*"
815  (regexp-quote tname) "[ \t]*$")
816  nil t)
817  (user-error "Can't find table named %s in file" tname fname))
818  (forward-line 0)
819  (let ((tstart (match-beginning 0))
820  tend)
821  (while (looking-at "^[ \t]*#\\+\\([^:]+\\): *\\(.*\\)")
822  (add-to-list 'klist `(,(upcase (match-string 1)) ,(match-string 2)))
823  (delete-region (point) (line-end-position))
824  (kill-line))
825  (unless (looking-at org-table-line-regexp)
826  (looking-at "^.*$")
827  (user-error "no table at location of %s, Looking-at: '%s'" tname (match-string 0)))
828  (goto-char (org-table-end))
829  (while (looking-at-p "^[ \t]*#\\+TBLFM:")
830  (forward-line 1))
831  (buffer-substring tstart (point))))))
832  (setq klist (nreverse klist)) ;; reverse for giving priority to new user settings
833  (dolist (elem '("NAME" "CAPTION" "ATTR_LATEX"))
834  (when (assoc elem klist)
835  (princ (format "#+%s: %s\n" elem (cadr (assoc elem klist))))))
836  (princ tbl))
837 #+END_SRC
838 * filter-table :table:
839 :PROPERTIES:
840 :ID: cec80330-99c3-4dab-9b13-bb0a520ace73
841 :END:
842 #+NAME: filter-table
843 #+HEADER: :var tbl="" col=0 vals=""
844 #+BEGIN_SRC elisp :results value :colnames y
845  (let ((lst (split-string vals)))
846  (concatenate 'list (loop for row in tbl
847  if (member (let ((field (nth col row)))
848  (if (numberp field)
849  (number-to-string field)
850  field)) lst)
851  collect row into newtbl
852  ;; else do (princ (format "%s: %s\n" (nth col row) lst))
853  finally return newtbl)))
854 #+END_SRC
855 * filter-table-re :table:rx:
856 :PROPERTIES:
857 :ID: 61e82902-3108-4cee-8e02-3e9d6770f5cb
858 :END:
859 #+NAME: filter-table-re
860 #+HEADER: :var tbl="" col=0 vals=".*"
861 #+BEGIN_SRC elisp :results value :colnames y
862  (let ((lst (split-string vals)))
863  (concatenate 'list (loop for row in tbl
864  if (let* ((rawfield (nth col row))
865  (field (if (numberp rawfield)
866  (number-to-string rawfield)
867  rawfield)))
868  (loop for regx in lst
869  when(string-match-p regx field) return 't
870  finally return nil))
871  collect row into newtbl
872  ;; else do (princ (format "%s: %s\n" (nth col row) lst))
873  finally return newtbl)))
874 #+END_SRC
875 * group-table :table:
876 :PROPERTIES:
877 :ID: d1cb8d5d-14bc-4fbe-baf0-ff819d51ec4c
878 :END:
879 #+NAME: group-table
880 #+HEADER: :var tbl="" grp="Name" op="sum" rescols="B"
881 #+BEGIN_SRC python :results output verbatim drawer :colnames no
882  import pandas as pd
883  import numpy as np
884  import orgbabelhelper as obh
885  import sys
886  import re
887 
888  df = obh.orgtable_to_dataframe(tbl)
889  grparr = re.split(r",\s*", grp)
890  #print re.split(r",\s*", rescols) + [grp]
891  df = df[re.split(r",\s*", rescols) + grparr]
892  for elem in grparr:
893  assert elem in df.columns, "Error: group column %s not in table columns %s" % (elem, ",".join(df.columns))
894 
895  if op == "sum":
896  res = df.groupby(grparr).sum()
897  else:
898  error("operation %s not implemented" % op)
899  sys.exit(1)
900 
901  print(obh.dataframe_to_orgtable(res))
902 
903 #+END_SRC
904 * insert-file :fs:
905 :PROPERTIES:
906 :ID: 57196c4d-a275-4023-8462-a5847676347c
907 :END:
908 #+NAME: lobInsertFile
909 #+HEADER: :var filename="/tmp/foo"
910 #+begin_src elisp :results value raw drawer
911  (cl-labels ((wrap-src
912  (lang)
913  (list (format "#+BEGIN_SRC %s :eval never :exports source\n" lang)
914  "#+END_SRC\n")))
915  (let ((wrappers
916  (pcase (file-name-extension filename)
917  ("py" (wrap-src "python"))
918  (".el" (wrap-src "emacs-lisp"))
919  (t '("#+BEGIN_EXAMPLE\n" "#+END_EXAMPLE\n")))))
920  (with-temp-buffer
921  (goto-char (point-min))
922  (insert (format-time-string "# inserted at %Y-%m-%d %H:%M:%S\n"))
923  (insert (car wrappers))
924  (insert-file-contents filename)
925  (goto-char (point-max))
926  (insert (car (cdr wrappers)))
927  (buffer-string))))
928 #+end_src