changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > org > meta / babel.org

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