changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > org > meta / babel.org

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