From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp12.migadu.com ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms0.migadu.com with LMTPS id KKGtM3oQAWIq8AAAgWs5BA (envelope-from ) for ; Mon, 07 Feb 2022 13:28:42 +0100 Received: from aspmx1.migadu.com ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp12.migadu.com with LMTPS id iFIOMHoQAWKQcAAAauVa8A (envelope-from ) for ; Mon, 07 Feb 2022 13:28:42 +0100 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 36A3546AA0 for ; Mon, 7 Feb 2022 13:28:42 +0100 (CET) Received: from localhost ([::1]:57164 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nH38H-0000oC-Ip for larch@yhetil.org; Mon, 07 Feb 2022 07:28:39 -0500 Received: from eggs.gnu.org ([209.51.188.92]:59598) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nH36J-0000l8-BS for emacs-orgmode@gnu.org; Mon, 07 Feb 2022 07:26:35 -0500 Received: from [2a00:1450:4864:20::634] (port=34717 helo=mail-ej1-x634.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nH36H-0002JW-3y for emacs-orgmode@gnu.org; Mon, 07 Feb 2022 07:26:35 -0500 Received: by mail-ej1-x634.google.com with SMTP id p24so3884209ejo.1 for ; Mon, 07 Feb 2022 04:26:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=myhSzN7SKwBxgm10yZzNNdWdSFs/5iMQLVhpYebtO+s=; b=P8zzDH5xYcCulTCUUs9QF9zRiL606dm78V/1dXHQ51vCZ1wPMZi659YVskCya1ZrTE ynkNxRVgDKNuhraJ/KvpdRlPFSL5xwS+eCDmWGjE2Z0XHVkDXG0pSzXVoWztQiDxYwIs jV5Caf3K+SiC57QGJ0Ji7dQotpwKtJ7LEGmQ9wi2Tg279XMd5cxwbiTJHSeuLWsAUdK7 PrCAFl8ONhvmUgQKDaaj6kVIqg7Sgp4maIeBo+9fQUBVjvNbpn0sok/pYUY2G4vq56Eb IzQ6/P8PXu/C2LGu8oTeOVJWfPTqeTpbkR5FtnIDlIlNwJXTJjcomIg8RLXzdXds9A8g pVkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=myhSzN7SKwBxgm10yZzNNdWdSFs/5iMQLVhpYebtO+s=; b=N0zgTwUhFUpQM6Ch1YtyGodesXqt/dkq/bKFiCXtJ76YygK/F9DUJ7tdwRoms8vLLC NjWqyen5owhebdx0QDXGQdkEmLQdVdE9MCytyc7QuzD3B4Pwmb3DbbTsuUstuj8STADP LtrOb9g7lJz1c1KJ8nmiCkloig/3187U9obifdaBBbN7NjvwAT3szc8tSwBKGyTEL8Kv PUJFpAr+2EtNl8VpilFk1VvxbFGNaxNdJRcPChhx6ymbokroi8l1cG5/70SuIjLrLNj1 ZjfS8b53Yc+aKFWK2QfRGIpVmSnJ/l+aWHay5IIdrwGwEJCrseNNnUFgZm0wTnbz55vt vXJw== X-Gm-Message-State: AOAM532197NTYty+oM2KDgHy9VVnnckuzw98qcZBnPBLCCJWzogRUouP bjXkE1QTqTCP2k5pQSRDO30VVHgeEyyKww== X-Google-Smtp-Source: ABdhPJwxcQDkLhlEIAqI3WYVaqOglFKB41umnbenbuq9ThMIO7VhjMNa6rlCsYMZgMANk0WGY7vNTQ== X-Received: by 2002:a17:907:29c6:: with SMTP id ev6mr7869742ejc.371.1644236791389; Mon, 07 Feb 2022 04:26:31 -0800 (PST) Received: from localhost ([91.210.107.150]) by smtp.gmail.com with ESMTPSA id k7sm3633233eje.162.2022.02.07.04.26.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Feb 2022 04:26:31 -0800 (PST) From: Ihor Radchenko To: emacs-orgmode@gnu.org Subject: [RFC PATCH 2/2] org-babel-expand-noweb-references: Cache block info Date: Mon, 7 Feb 2022 20:31:15 +0800 Message-Id: <17a5be9ec7f969a7a79088e079e94efab8b838b8.1644236545.git.yantar92@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Host-Lookup-Failed: Reverse DNS lookup failed for 2a00:1450:4864:20::634 (failed) Received-SPF: pass client-ip=2a00:1450:4864:20::634; envelope-from=yantar92@gmail.com; helo=mail-ej1-x634.google.com X-Spam_score_int: -10 X-Spam_score: -1.1 X-Spam_bar: - X-Spam_report: (-1.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, PDS_HP_HELO_NORDNS=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-orgmode@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Ihor Radchenko Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: "Emacs-orgmode" X-Migadu-Flow: FLOW_IN X-Migadu-Country: US ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1644236922; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post:dkim-signature; bh=myhSzN7SKwBxgm10yZzNNdWdSFs/5iMQLVhpYebtO+s=; b=dJSaNHWnAXN1Ta2mwKvEEGa2aclROD8PM38UC8yALGD/L9yE279jou2wGOfSERMtifKCU8 /jzWFn5e2shqaAe12rhEX6t2M07GiKWCdh+koYSS8EVBZEp1k6JyavnSlPfqesAT0WZwyJ zHcsJ4q/ZLiKthAGCsjhexEqEzuMxevnZQ1afHhCEVIIX7ZXDb6fPo24lTuJ681b1TCnZb 30w02rWwHYRDKs3kZCg5TbrmpOqOpRzjT/K5SJ6ORSKraRua19pOtmsjvkAKNTQlV/rrwS L/oXCcPfdf8r2s3Xcgm4fRfwtjG/cTHU5eeZCOvHpXAzLGy2Gp0k28u+JzIzLw== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1644236922; a=rsa-sha256; cv=none; b=Fg2RD6UPgtwj+FR9OVPTigzWUaI/uAkx9cWurx3Dra3kTzwmLipsrtqjOajetJunAEq5Wd ryXlVVYM3ue2VJPpxqGYsoB+kkTAa5SR6jRt62mmJevGFc0BzQ8/IUPtc73iLPUbICl15/ lJpLpPMFs3rBQNclG0yj/UpvPg6dE/04gqdO7O1iu4W7vJ/Bn1vpaytLnM34t4TbqvoFTA /wir4bI81WvF6MYflVXw4Nh9tDaY07h3HujE4SBcE0uVi9qdul+bUmqa60CQZDm5jonCRX aqE6bBNGGAbwYEXDv4t3ZUPg+ZtygKiEIIpYpNfrAQuJnwTLafDux0DCuZFPtQ== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=P8zzDH5x; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (aspmx1.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org" X-Migadu-Spam-Score: -2.83 Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=P8zzDH5x; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (aspmx1.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org" X-Migadu-Queue-Id: 36A3546AA0 X-Spam-Score: -2.83 X-Migadu-Scanner: scn1.migadu.com X-TUID: 8mE/TZppyECW * lisp/ob-core.el (org-babel-expand-noweb-references--cache): (org-babel-expand-noweb-references--cache-buffer): New variables storing info cache. (org-babel-expand-noweb-references): Make use of global info cache to avoid extra parsing. Use `cl-macrolet' instead of defining transient lambda functions on every call. --- lisp/ob-core.el | 225 +++++++++++++++++++++++++++--------------------- 1 file changed, 127 insertions(+), 98 deletions(-) diff --git a/lisp/ob-core.el b/lisp/ob-core.el index 239a57f96..e767fd107 100644 --- a/lisp/ob-core.el +++ b/lisp/ob-core.el @@ -2790,6 +2790,10 @@ (defun org-babel-noweb-p (params context) (cl-some (lambda (v) (member v allowed-values)) (split-string (or (cdr (assq :noweb params)) ""))))) +(defvar org-babel-expand-noweb-references--cache nil + "Noweb reference cache used during expansion.") +(defvar org-babel-expand-noweb-references--cache-buffer nil + "Cons of (buffer . modified-tick) cached by `org-babel-expand-noweb-references--cache'.") (defun org-babel-expand-noweb-references (&optional info parent-buffer) "Expand Noweb references in the body of the current source code block. @@ -2827,104 +2831,129 @@ (defun org-babel-expand-noweb-references (&optional info parent-buffer) (comment (string= "noweb" (cdr (assq :comments (nth 2 info))))) (noweb-re (format "\\(.*?\\)\\(%s\\)" (with-current-buffer parent-buffer - (org-babel-noweb-wrap)))) - (cache nil) - (c-wrap - (lambda (s) - ;; Comment string S, according to LANG mode. Return new - ;; string. - (unless org-babel-tangle-uncomment-comments - (with-temp-buffer - (funcall (org-src-get-lang-mode lang)) - (comment-region (point) - (progn (insert s) (point))) - (org-trim (buffer-string)))))) - (expand-body - (lambda (i) - ;; Expand body of code represented by block info I. - (let ((b (if (org-babel-noweb-p (nth 2 i) :eval) - (org-babel-expand-noweb-references i) - (nth 1 i)))) - (if (not comment) b - (let ((cs (org-babel-tangle-comment-links i))) - (concat (funcall c-wrap (car cs)) "\n" - b "\n" - (funcall c-wrap (cadr cs)))))))) - (expand-references - (lambda (ref cache) - (pcase (gethash ref cache) - (`(,last . ,previous) - ;; Ignore separator for last block. - (let ((strings (list (funcall expand-body last)))) - (dolist (i previous) - (let ((parameters (nth 2 i))) - ;; Since we're operating in reverse order, first - ;; push separator, then body. - (push (or (cdr (assq :noweb-sep parameters)) "\n") - strings) - (push (funcall expand-body i) strings))) - (mapconcat #'identity strings ""))) - ;; Raise an error about missing reference, or return the - ;; empty string. - ((guard (or org-babel-noweb-error-all-langs - (member lang org-babel-noweb-error-langs))) - (error "Cannot resolve %s (see `org-babel-noweb-error-langs')" - (org-babel-noweb-wrap ref))) - (_ ""))))) - (replace-regexp-in-string - noweb-re - (lambda (m) - (with-current-buffer parent-buffer - (save-match-data - (let* ((prefix (match-string 1 m)) - (id (match-string 3 m)) - (evaluate (string-match-p "(.*)" id)) - (expansion - (cond - (evaluate - ;; Evaluation can potentially modify the buffer - ;; and invalidate the cache: reset it. - (setq cache nil) - (let ((raw (org-babel-ref-resolve id))) - (if (stringp raw) raw (format "%S" raw)))) - ;; Return the contents of headlines literally. - ((org-babel-ref-goto-headline-id id) - (org-babel-ref-headline-body)) - ;; Look for a source block named SOURCE-NAME. If - ;; found, assume it is unique; do not look after - ;; `:noweb-ref' header argument. - ((org-with-point-at 1 - (let ((r (org-babel-named-src-block-regexp-for-name id))) - (and (re-search-forward r nil t) - (not (org-in-commented-heading-p)) - (funcall expand-body - (org-babel-get-src-block-info t)))))) - ;; Retrieve from the Library of Babel. - ((nth 2 (assoc-string id org-babel-library-of-babel))) - ;; All Noweb references were cached in a previous - ;; run. Extract the information from the cache. - ((hash-table-p cache) - (funcall expand-references id cache)) - ;; Though luck. We go into the long process of - ;; checking each source block and expand those - ;; with a matching Noweb reference. Since we're - ;; going to visit all source blocks in the - ;; document, cache information about them as well. - (t - (setq cache (make-hash-table :test #'equal)) - (org-with-wide-buffer - (org-babel-map-src-blocks nil - (if (org-in-commented-heading-p) - (org-forward-heading-same-level nil t) - (let* ((info (org-babel-get-src-block-info t)) - (ref (cdr (assq :noweb-ref (nth 2 info))))) - (push info (gethash ref cache)))))) - (funcall expand-references id cache))))) - ;; Interpose PREFIX between every line. - (mapconcat #'identity - (split-string expansion "[\n\r]") - (concat "\n" prefix)))))) - body t t 2))) + (org-babel-noweb-wrap))))) + (unless (equal (cons parent-buffer + (with-current-buffer parent-buffer + (buffer-chars-modified-tick))) + org-babel-expand-noweb-references--cache-buffer) + (setq org-babel-expand-noweb-references--cache nil + org-babel-expand-noweb-references--cache-buffer + (cons parent-buffer + (with-current-buffer parent-buffer + (buffer-chars-modified-tick))))) + (cl-macrolet ((c-wrap + (s) + ;; Comment string S, according to LANG mode. Return new + ;; string. + `(unless org-babel-tangle-uncomment-comments + (with-temp-buffer + (funcall (org-src-get-lang-mode lang)) + (comment-region (point) + (progn (insert ,s) (point))) + (org-trim (buffer-string))))) + (expand-body + (i) + ;; Expand body of code represented by block info I. + `(let ((b (if (org-babel-noweb-p (nth 2 ,i) :eval) + (org-babel-expand-noweb-references ,i) + (nth 1 ,i)))) + (if (not comment) b + (let ((cs (org-babel-tangle-comment-links ,i))) + (concat (c-wrap (car cs)) "\n" + b "\n" + (c-wrap (cadr cs))))))) + (expand-references + (ref) + `(pcase (gethash ,ref org-babel-expand-noweb-references--cache) + (`(,last . ,previous) + ;; Ignore separator for last block. + (let ((strings (list (expand-body last)))) + (dolist (i previous) + (let ((parameters (nth 2 i))) + ;; Since we're operating in reverse order, first + ;; push separator, then body. + (push (or (cdr (assq :noweb-sep parameters)) "\n") + strings) + (push (expand-body i) strings))) + (mapconcat #'identity strings ""))) + ;; Raise an error about missing reference, or return the + ;; empty string. + ((guard (or org-babel-noweb-error-all-langs + (member lang org-babel-noweb-error-langs))) + (error "Cannot resolve %s (see `org-babel-noweb-error-langs')" + (org-babel-noweb-wrap ,ref))) + (_ "")))) + (replace-regexp-in-string + noweb-re + (lambda (m) + (with-current-buffer parent-buffer + (save-match-data + (let* ((prefix (match-string 1 m)) + (id (match-string 3 m)) + (evaluate (string-match-p "(.*)" id)) + (expansion + (cond + (evaluate + (prog1 + (let ((raw (org-babel-ref-resolve id))) + (if (stringp raw) raw (format "%S" raw))) + ;; Evaluation can potentially modify the buffer + ;; and invalidate the cache: reset it. + (unless (equal org-babel-expand-noweb-references--cache-buffer + (cons parent-buffer + (buffer-chars-modified-tick))) + (setq org-babel-expand-noweb-references--cache nil + org-babel-expand-noweb-references--cache-buffer + (cons parent-buffer + (with-current-buffer parent-buffer + (buffer-chars-modified-tick))))))) + ;; Already cached. + ((and (hash-table-p org-babel-expand-noweb-references--cache) + (gethash id org-babel-expand-noweb-references--cache)) + (expand-references id)) + ;; Return the contents of headlines literally. + ((org-babel-ref-goto-headline-id id) + (org-babel-ref-headline-body)) + ;; Look for a source block named SOURCE-NAME. If + ;; found, assume it is unique; do not look after + ;; `:noweb-ref' header argument. + ((org-with-point-at 1 + (let ((r (org-babel-named-src-block-regexp-for-name id))) + (and (re-search-forward r nil t) + (not (org-in-commented-heading-p)) + (let ((info (org-babel-get-src-block-info t))) + (unless (hash-table-p org-babel-expand-noweb-references--cache) + (setq org-babel-expand-noweb-references--cache (make-hash-table :test #'equal))) + (push info (gethash id org-babel-expand-noweb-references--cache)) + (expand-body info)))))) + ;; Retrieve from the Library of Babel. + ((nth 2 (assoc-string id org-babel-library-of-babel))) + ;; All Noweb references were cached in a previous + ;; run. Yet, ID is not in cache (see the above + ;; condition). Process missing reference in + ;; `expand-references'. + ((hash-table-p org-babel-expand-noweb-references--cache) + (expand-references id)) + ;; Though luck. We go into the long process of + ;; checking each source block and expand those + ;; with a matching Noweb reference. Since we're + ;; going to visit all source blocks in the + ;; document, cache information about them as well. + (t + (setq org-babel-expand-noweb-references--cache (make-hash-table :test #'equal)) + (org-with-wide-buffer + (org-babel-map-src-blocks nil + (if (org-in-commented-heading-p) + (org-forward-heading-same-level nil t) + (let* ((info (org-babel-get-src-block-info t)) + (ref (cdr (assq :noweb-ref (nth 2 info))))) + (push info (gethash ref org-babel-expand-noweb-references--cache)))))) + (expand-references id))))) + ;; Interpose PREFIX between every line. + (mapconcat #'identity + (split-string expansion "[\n\r]") + (concat "\n" prefix)))))) + body t t 2)))) (defun org-babel--script-escape-inner (str) (let (in-single in-double backslash out) -- 2.34.1