From 1cbe4ffa13091e429b1419a4cf1c913995a7b080 Mon Sep 17 00:00:00 2001 From: ag91 Date: Mon, 20 Nov 2023 17:31:43 +0000 Subject: [PATCH] optimize search of org tables by reducing scope to current parent heading also add test utility --- moldable-emacs.el | 41 +++++++++++++++++++++++++++-------------- test.sh | 3 +++ 2 files changed, 30 insertions(+), 14 deletions(-) create mode 100755 test.sh diff --git a/moldable-emacs.el b/moldable-emacs.el index a77b69d..d52f3eb 100644 --- a/moldable-emacs.el +++ b/moldable-emacs.el @@ -281,27 +281,40 @@ Make sure table is also indented." (orgtbl-to-orgtbl it nil) (me-org-table-to-flat-plist it))) +(defmacro me-with-org-parent-heading (&rest body) + "Execute BODY with narrowing on the upmost parent heading if it exists." + `(save-excursion + (while (org-up-heading-safe)) + (ignore-errors (org-narrow-to-subtree)) + (let ((r__ (progn ,@body))) + (widen) + r__))) + (defun me-first-org-table (&optional buffer) "Find first org table. Optionally in BUFFER." (ignore-errors (with-current-buffer (or buffer (current-buffer)) ;; TODO remove org links in table! - (save-excursion - (re-search-forward org-table-line-regexp nil t) - (me-org-tabletolisp-to-plist (org-table-to-lisp)))))) + (me-with-org-parent-heading + (re-search-forward org-table-line-regexp nil t) + (me-org-tabletolisp-to-plist (org-table-to-lisp)))))) -(defun me-all-flat-org-tables (&optional buffer) - "Find first org table. Optionally in BUFFER." +(defun me-all-flat-org-tables (&optional buffer whole-buffer) + "Find org tables within current headline or in whole buffer if no headline found. +Optionally in input BUFFER. Search in WHOLE-BUFFER, if t." (ignore-errors (with-current-buffer (or buffer (current-buffer)) ;; TODO remove org links in table! - (save-excursion - (let (result) - (while (and - (re-search-forward org-table-line-regexp nil t) - (goto-char (- (org-table-end) 1))) - (setq result - (cons (me-org-tabletolisp-to-plist (org-table-to-lisp)) - result))) - result))))) + (me-with-org-parent-heading + (when whole-buffer + (goto-char 0) + (widen)) + (let (result) + (while (and + (re-search-forward org-table-line-regexp nil t) + (goto-char (- (org-table-end) 1))) + (setq result + (cons (me-org-tabletolisp-to-plist (org-table-to-lisp)) + result))) + result))))) (defun me-types (tree) "List types in current syntax TREE. diff --git a/test.sh b/test.sh new file mode 100755 index 0000000..df7d93c --- /dev/null +++ b/test.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +./eldev -p -dtT test