From mboxrd@z Thu Jan 1 00:00:00 1970 From: jemarch@gnu.org (Jose E. Marchesi) Subject: [BABEL] ob-rec.el and some questions Date: Mon, 14 Feb 2011 22:07:29 +0100 Message-ID: <877hd2uxym.fsf@gnu.org> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Return-path: Received: from [140.186.70.92] (port=56588 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Pp5j2-0005bd-T6 for emacs-orgmode@gnu.org; Mon, 14 Feb 2011 16:12:54 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Pp5j1-0003nW-0W for emacs-orgmode@gnu.org; Mon, 14 Feb 2011 16:12:51 -0500 Received: from fencepost.gnu.org ([140.186.70.10]:40846) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Pp5j0-0003nS-TS for emacs-orgmode@gnu.org; Mon, 14 Feb 2011 16:12:50 -0500 Received: from p578ef5f1.dip.t-dialin.net ([87.142.245.241]:36487 helo=termi.gnu.org) by fencepost.gnu.org with esmtpa (Exim 4.71) (envelope-from ) id 1Pp5j0-0000EI-JE for emacs-orgmode@gnu.org; Mon, 14 Feb 2011 16:12:50 -0500 List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: emacs-orgmode@gnu.org --=-=-= Hi. I have been playing a bit with org babel, the goal being to be able to query a recfile[1] and insert the result as a rec table. Thanks to the superb design of org-babel and the existing examples, I quickly wrote the attached little hack. The hack allows to query a rec file 'foo.rec' as follows: #+begin_src rec :data hackers.rec :fields Name,Email :type Hacker Papers = 'requested' && CreatedAt << '01 January 2011' #+end_src Where :data points to the recfile (or list of files), :fields is a comma-separated list of fields (allowing subscripts) and :type selects the record set where to make the query. :cmdline can also be used to specify any other command line option to recsel. All parameters but :data are optional. The selection expression can be any expression allowed by recsel in the -e command line option. Of course you have to install the recutils for it to work. It roughly works and I want to add more functionality, but due to my lack of experience with org-babel I would like to ask some questions before continuing working on it. - Is there a way to execute empty code blocks? It would be quite common to require all the records stored in a record set, e.g. #+begin_src rec :data inventory.rec #+end_src C-cC-c in that block echoes "Local setup has been refreshed" and nothing happens. A workaround would be to use a selection expression that always evaluates to "true", such as: #+begin_src rec :data inventory.rec 1 #+end_src but it would be nice to avoid it. - Is there a way to dinamically change the value of the :results parameter in the org-babel-execute:rec function? I would like to add a :template parameter containing a template for recfmt, and in that case the default value "raw" would not be appropriate. Thanks in advance :) [1] http://www.gnu.org/software/recutils --=-=-= Content-Type: application/emacs-lisp Content-Disposition: attachment; filename=ob-rec.el Content-Transfer-Encoding: quoted-printable Content-Description: ob-rec.el ;;; ob-rec.el --- org-babel functions for recutils evaluation ;; Copyright (C) 2011 Jose E. Marchesi ;; Author: Jose E. Marchesi ;; Keywords: literate programming ;; Homepage: http://www.gnu.org/software/recutils ;; This file is NOT part of GNU Emacs. ;; This program is free software: you can redistribute it and/or ;; modify it under the terms of the GNU General Public License as ;; published by the Free Software Foundation, either version 3 of the ;; License, or (at your option) any later version. ;; This program is distributed in the hope that it will be useful, but ;; WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ;; General Public License for more details. ;;; Commentary: ;; Org-Babel support for evaluating recsel queries and substituing the ;; contained template. ;;; Code: (require 'ob) (defvar org-babel-default-header-args:rec '((:results . "raw") (:exports . "results"))) (defun org-babel-execute:rec (body params) "Execute a block containing a recsel query. This function is called by `org-babel-execute-src-block'." (let* ((in-file ((lambda (el) (or el (error "rec code block requires :data header argument"))) (cdr (assoc :data params)))) (cmdline (cdr (assoc :cmdline params))) (rec-type (cdr (assoc :type params))) (fields (cdr (assoc :fields params))) (cmd (concat "recsel" (when rec-type (concat " -t " rec-type " ")) " " (expand-file-name in-file) (when body (concat " -e " "\"" (replace-regexp-in-string "\"" "\\\\\"" body) "\"")) (when fields (concat " -p " fields " ")) " | rec2csv"))) (with-temp-buffer (shell-command cmd (current-buffer)) (when (not (equal (point-min) (point-max))) (org-table-convert-region (point-min) (point-max) '(4))) (buffer-substring (point-min) (point-max))))) =20=20=20=20 (provide 'ob-rec) ;; ob-rec.el ends here --=-=-= -- Jose E. Marchesi jemarch@gnu.org GNU Project http://www.gnu.org --=-=-= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Emacs-orgmode mailing list Please use `Reply All' to send replies to the list. Emacs-orgmode@gnu.org http://lists.gnu.org/mailman/listinfo/emacs-orgmode --=-=-=--