From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp1 ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id eB82AKvsJmC5egAA0tVLHw (envelope-from ) for ; Fri, 12 Feb 2021 21:01:31 +0000 Received: from aspmx1.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp1 with LMTPS id iKp8N6rsJmCiXwAAbx9fmQ (envelope-from ) for ; Fri, 12 Feb 2021 21:01:30 +0000 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 BC3C112555 for ; Fri, 12 Feb 2021 22:01:30 +0100 (CET) Received: from localhost ([::1]:48672 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lAfZB-0003ab-6T for larch@yhetil.org; Fri, 12 Feb 2021 16:01:29 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:38348) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lAfXR-0003ZZ-Qg for emacs-orgmode@gnu.org; Fri, 12 Feb 2021 15:59:41 -0500 Received: from mail-qk1-x734.google.com ([2607:f8b0:4864:20::734]:45415) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lAfXP-0005cT-QW for emacs-orgmode@gnu.org; Fri, 12 Feb 2021 15:59:41 -0500 Received: by mail-qk1-x734.google.com with SMTP id r77so904871qka.12 for ; Fri, 12 Feb 2021 12:59:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=references:user-agent:from:to:cc:subject:in-reply-to:date :message-id:mime-version; bh=p7pXklR4mrPWqVo0Se8cIy896x9V/vPde96qaX4zKG4=; b=fcNg8SlPhtIg7rhIjV8w6KUAKpFhQv7bfZD7sVlW0sWxMb7nmr7EP00HluaTfI5xW0 4REAJSFzw/ZXPKmvK6V8IUucJDNuGKIwD/gcHNyejuap6r8jPbY38LfYmpYp7QxVMy99 QTgeG9G0k8hZCaQ6dXePqPptN2alvz0oZr4Kq9aWA+oMOGIBBcDpheMJJkAFC3BgQ1Ir gdk9hWW3U+YRYdpwr2UdPDPRbyD5+mXXj1IM8v2Wpgu5ogsCA3UWriLVTizJ02/rypSs sF8t0Z6utuhZOKj/vGFRB/U3x6294gtEju8rYzM/Z9muBlbXiKvjIAkBRmVbZFHnhPk7 27Kg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:references:user-agent:from:to:cc:subject :in-reply-to:date:message-id:mime-version; bh=p7pXklR4mrPWqVo0Se8cIy896x9V/vPde96qaX4zKG4=; b=RWfx07w/DFDNEnwGw9OBQQwUebzv+MvgVM1OwHYfKPEJxjn5vkp6lykOeHHl2DXKdF g9wnagipGrSCYcvkyAhmDip0aQ18v6PL0UucxPglHta+T0rgAqdBOliGEMIn2WQEilul bizSmg2Y+BkZNo7U73H85GF3y/LZGbh16Gyvq1d1fzGz4NGUDpr9TdP/KYYtAWM3I7Ty vS42UbXKkgdY5RW9MfU9oIvTh50BWZlinSPkW/9nyVIPS1lZp3BQ7tzzapiIyv8KHWhr TGU7i25oZHDPCcrCMh8ZLeXF96xv+DWLL3+/2BPrQL/gvQhLgPXqPLbdvcQF2LvR1Hlm bcsg== X-Gm-Message-State: AOAM533o9r7EI616S5CyDAOqjsRYDe8m0yMiyyK97SDqerQZLnvCrs8J GhsbHCi3urDYqB+wePH57Sj+tgacUlI= X-Google-Smtp-Source: ABdhPJxR0Y0uTJgE8UqGirDhFwAPhen2Y1jqOqntZ2RGYWyqx/PYn86i34Q6OJrQV/ijC5+h5CUlMA== X-Received: by 2002:a37:4a89:: with SMTP id x131mr4346325qka.233.1613163578725; Fri, 12 Feb 2021 12:59:38 -0800 (PST) Received: from localhost ([190.239.232.181]) by smtp.gmail.com with ESMTPSA id b65sm4783211qkd.120.2021.02.12.12.59.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Feb 2021 12:59:38 -0800 (PST) References: <87blcqhwzf.fsf@gmail.com> <87h7mh99eb.fsf@kyleam.com> User-agent: mu4e 1.4.14; emacs 27.1 From: Rodrigo Morales To: Kyle Meyer Subject: Re: How to get the content of a code block by its name? In-reply-to: <87h7mh99eb.fsf@kyleam.com> Date: Fri, 12 Feb 2021 15:56:06 -0500 Message-ID: <87o8gp2eft.fsf@gmail.com> MIME-Version: 1.0 Content-Type: text/plain Received-SPF: pass client-ip=2607:f8b0:4864:20::734; envelope-from=moralesrodrigo1100@gmail.com; helo=mail-qk1-x734.google.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 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, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-orgmode@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: emacs-orgmode@gnu.org Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: "Emacs-orgmode" X-Migadu-Flow: FLOW_IN X-Migadu-Spam-Score: -4.00 Authentication-Results: aspmx1.migadu.com; none X-Migadu-Queue-Id: BC3C112555 X-Spam-Score: -4.00 X-Migadu-Scanner: scn0.migadu.com X-TUID: 8iuUS4zDP1bn Kyle Meyer writes: > This came up somewhat recently, and there were a couple of suggestions: > https://orgmode.org/list/CA+pajW+jViTPE2cwTZ=tHVZyHjrT000-BB6JK2ntE4P9UFBmpQ@mail.gmail.com/T/#u Thanks for the information! That was really helpful. I retrieved some information from that post and wrote the following function.I would really appreciate if someone could give me some feedback on this function. #+begin_src elisp (defun org-babel-get-block-as-string (name) "Get the content of a code block with name NAME as an string. This function also considers the code blocks stored in `org-babel-library-of-babel'." (interactive) (catch 'found ;; Let's first search the code block in the current buffer. (let ((position (org-babel-find-named-block name)) content) (if position (progn (setq content (save-excursion (goto-char position) ;; This function uses `org-babel-expand-src-block' ;; instead of `org-edit-src-code' so that the ;; value of the header arguments :prologue and the ;; :epilogue are included as the content of the ;; code block. (org-babel-expand-src-block))) (throw 'found content)))) ;; If the code block doesn't exist in the current buffer, then ;; let's search in `org-babel-library-of-babel'. (let ((library-block-data (alist-get (intern name) org-babel-library-of-babel)) content) (if library-block-data (progn (setq content (nth 1 library-block-data)) (throw 'found content)))) ; If the code block is not found, then an error is shown. Throwing ; an error is important since this makes code blocks which uses ; this function as the :epilogue and :prologue don't be evaluated ; in the scenario that a code block is not found. (error (format "The code block with name \"%s\" was not found" name)))) #+end_src * Minimal example Below, there is an example that shows how to use this function. We can use =printf= as it follows #+NAME: test-printf #+begin_src bash printf "%s\n" "printf-foo" printf "%s\n" "printf-bar" #+end_src #+RESULTS: test-printf #+begin_example printf-foo printf-bar #+end_example We can use =echo= as it follows #+NAME: test-echo #+begin_src bash echo echo-foo echo echo-bar #+end_src #+RESULTS: test-echo #+begin_example echo-foo echo-bar #+end_example We can use both of them as it follows #+HEADER: :prologue (org-babel-get-block-as-string "test-printf") #+HEADER: :epilogue (org-babel-get-block-as-string "test-echo") #+begin_src bash echo a #+end_src #+RESULTS: #+begin_example printf-foo printf-bar a echo-foo echo-bar #+end_example -- Greetings, Rodrigo Morales. IRC: rdrg109 (freenode)