From mboxrd@z Thu Jan 1 00:00:00 1970 From: Eric Schulte Subject: Re: org babel support for tcl and awk Date: Tue, 24 May 2011 06:51:58 -0600 Message-ID: <8739k46z2p.fsf@gmail.com> References: <20110524113109.fo2dcd0mwno0c4w4@webmail.dds.nl> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Return-path: Received: from eggs.gnu.org ([140.186.70.92]:39485) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QOr5j-0006dp-Pn for emacs-orgmode@gnu.org; Tue, 24 May 2011 08:52:08 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1QOr5i-00089x-67 for emacs-orgmode@gnu.org; Tue, 24 May 2011 08:52:07 -0400 Received: from mail-pz0-f41.google.com ([209.85.210.41]:62640) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QOr5h-00089i-RG for emacs-orgmode@gnu.org; Tue, 24 May 2011 08:52:06 -0400 Received: by pzk4 with SMTP id 4so4019249pzk.0 for ; Tue, 24 May 2011 05:52:04 -0700 (PDT) In-Reply-To: <20110524113109.fo2dcd0mwno0c4w4@webmail.dds.nl> (orgmode@h-rd.org's message of "Tue, 24 May 2011 11:31:09 +0200") List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: "orgmode@h-rd.org" Cc: emacs-orgmode@gnu.org --=-=-= Content-Type: text/plain Hi, Are you aware of the ob-template.el file [1], which can be used as a jumping off point to simplify the addition of new languages? After globally replacing the term "template" with you language name, the only function that necessarily needs to be re-written is the main `org-babel-execute:template' function. I would recommend starting with only non-session based evaluation, and then slowly adding functionality. If you run into any specific problems I am happy to help trouble shoot. As an example, I've worked up an very simple ob-awk.el file from ob-template.el, it is attached along with an example org-mode file which demonstrates its usage. Best -- Eric --=-=-= Content-Type: application/emacs-lisp Content-Disposition: inline; filename=ob-awk.el Content-Transfer-Encoding: quoted-printable ;;; ob-awk.el --- org-babel functions for awk evaluation ;; Copyright (C) Eric Schulte ;; Author: Eric Schulte ;; Keywords: literate programming, reproducible research, awk ;; Homepage: http://orgmode.org ;; Version: 0.01 ;;; License: ;; 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, 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. ;; ;; You should have received a copy of the GNU General Public License ;; along with GNU Emacs; see the file COPYING. If not, write to the ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, ;; Boston, MA 02110-1301, USA. ;;; Commentary: ;; This file is not intended to ever be loaded by org-babel, rather it ;; is a awk for use in adding new language support to Org-babel. ;; Good first steps are to copy this file to a file named by the ;; language you are adding, and then use `query-replace' to replace ;; all strings of "awk" in this file with the name of your new ;; language. ;; ;; If you have questions as to any of the portions of the file defined ;; below please look to existing language support for guidance. ;; ;; If you are planning on adding a language to org-babel we would ask ;; that if possible you fill out the FSF copyright assignment form ;; available at http://orgmode.org/request-assign-future.txt as this ;; will make it possible to include your language support in the core ;; of Org-mode, otherwise unassigned language support files can still ;; be included in the contrib/ directory of the Org-mode repository. ;;; Requirements: ;; Use this section to list the requirements of this language. Most ;; languages will require that at least the language be installed on ;; the user's system, and the Emacs major mode relevant to the ;; language be installed as well. ;;; Code: (require 'ob) (require 'ob-ref) (require 'ob-comint) (require 'ob-eval) ;; possibly require modes required for your language ;; optionally define a file extension for this language (add-to-list 'org-babel-tangle-lang-exts '("awk" . "awk")) ;; optionally declare default header arguments for this language (defvar org-babel-default-header-args:awk '()) (defvar org-babel-awk-command "awk" "Name of the awk executable command.") ;; This function expands the body of a source code block by doing ;; things like prepending argument definitions to the body, it should ;; be called by the `org-babel-execute:awk' function below. (defun org-babel-expand-body:awk (body params &optional processed-params) "Expand BODY according to PARAMS, return the expanded body." (dolist (pair (mapcar #'cdr (org-babel-get-header params :var))) (setf body (replace-regexp-in-string (regexp-quote (concat "$" (car pair))) (cdr pair) body))) body) ;; This is the main function which is called to evaluate a code ;; block. ;; ;; This function will evaluate the body of the source code and ;; return the results as emacs-lisp depending on the value of the ;; :results header argument ;; - output means that the output to STDOUT will be captured and ;; returned ;; - value means that the value of the last statement in the ;; source code block will be returned ;; ;; The most common first step in this function is the expansion of the ;; PARAMS argument using `org-babel-process-params'. ;; ;; Please feel free to not implement options which aren't appropriate ;; for your language (e.g. not all languages support interactive ;; "session" evaluation). Also you are free to define any new header ;; arguments which you feel may be useful -- all header arguments ;; specified by the user will be available in the PARAMS variable. (defun org-babel-execute:awk (body params) "Execute a block of Awk code with org-babel. This function is called by `org-babel-execute-src-block'" (message "executing Awk source code block") (let* (;; (processed-params (org-babel-process-params params)) ;; set the session if the session variable is non-nil ;; (session (org-babel-awk-initiate-session (first processed-param= s))) ;; variables assigned for use in the block ;; (vars (second processed-params)) ;; (result-params (third processed-params)) ;; either OUTPUT or VALUE which should behave as described above ;; (result-type (fourth processed-params)) ;; expand the body with `org-babel-expand-body:awk' (full-body (org-babel-expand-body:awk body params)) (code-file (org-babel-temp-file "awk-")) (cmd-line (or (cdr (assoc :cmd-line params)) "")) (in-file (cdr (assoc :in-file params))) (cmd (format "%s -f %s %s %s" org-babel-awk-command code-file cmd-line in-file))) ;; actually execute the source-code block either in a session or ;; possibly by dropping it to a temporary file and evaluating the ;; file. ;;=20 ;; for session based evaluation the functions defined in ;; `org-babel-comint' will probably be helpful. ;; ;; for external evaluation the functions defined in ;; `org-babel-eval' will probably be helpful. ;; ;; when forming a shell command, or a fragment of code in some ;; other language, please preprocess any file names involved with ;; the function `org-babel-process-file-name'. (See the way that ;; function is used in the language files) (with-temp-file code-file (insert full-body)) (message "$ " cmd) (org-babel-eval cmd ""))) ;; This function should be used to assign any variables in params in ;; the context of the session environment. (defun org-babel-prep-session:awk (session params) "Prepare SESSION according to the header arguments specified in PARAMS." (error "`org-babel-prep-session:awk' is not implemented")) (defun org-babel-awk-table-or-string (results) "If the results look like a table, then convert them into an Emacs-lisp table, otherwise return the results as a string." (error "`org-babel-awk-table-or-string' is not implemented")) (defun org-babel-awk-initiate-session (&optional session) "If there is not a current inferior-process-buffer in SESSION then create. Return the initialized session." (unless (string=3D session "none") (error "`org-babel-awk-initiate-session' is not implemented"))) (provide 'ob-awk) ;;; ob-awk.el ends here --=-=-= Content-Type: text/x-org Content-Disposition: inline; filename=example.org * example use of ob-awk Header Arguments - cmd-line :: command line flags to pass to =awk= - in-file :: the text file on which to run the resulting =awk= script Currently only string results are returned directly from STDOUT. Currently only external (i.e., non-session) evaluation is supported. #+begin_src awk :in-file columns-of-numbers.txt {print $1} #+end_src #+results: : 0 : 1 : 2 : 3 : 4 : 5 : 6 : 7 : 8 --=-=-= Content-Type: text/plain "orgmode@h-rd.org" writes: > Hi, > > I am looking for support for Tcl (and AWK) for org-babel. Both have a > supplied emacs mode and Tcl also has an inferior interpreter mode. I > was trying to do it myself, however I am quite lost in the > instructions. Is there someone with the knowledge and willingness to > provide a support file for org babel for Tcl (and maybe awk)? > > thanks. > > > Footnotes: [1] http://repo.or.cz/w/Worg.git/blob/HEAD:/org-contrib/babel/ob-template.el -- Eric Schulte http://cs.unm.edu/~eschulte/ --=-=-=--