Fix problem when org-capturing text from French email
[org-mode/org-jambu.git] / lisp / ob-perl.el
blob63ff675fae840e105a05cdb3275fa5476ac2fe90
1 ;;; ob-perl.el --- org-babel functions for perl evaluation
3 ;; Copyright (C) 2009, 2010 Free Software Foundation
5 ;; Author: Dan Davison, Eric Schulte
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
8 ;; Version: 0.01
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; Org-Babel support for evaluating perl source code.
29 ;;; Code:
30 (require 'ob)
31 (require 'ob-eval)
32 (eval-when-compile (require 'cl))
34 (add-to-list 'org-babel-tangle-lang-exts '("perl" . "pl"))
36 (defvar org-babel-default-header-args:perl '())
38 (defvar org-babel-perl-command "perl"
39 "Name of command to use for executing perl code.")
41 (defun org-babel-expand-body:perl (body params &optional processed-params)
42 "Expand BODY according to PARAMS, return the expanded body."
43 (let ((vars (nth 1 (or processed-params (org-babel-process-params params)))))
44 (concat
45 (mapconcat ;; define any variables
46 (lambda (pair)
47 (format "$%s=%s;"
48 (car pair)
49 (org-babel-perl-var-to-perl (cdr pair))))
50 vars "\n") "\n" (org-babel-trim body) "\n")))
52 (defun org-babel-execute:perl (body params)
53 "Execute a block of Perl code with Babel.
54 This function is called by `org-babel-execute-src-block'."
55 (let* ((processed-params (org-babel-process-params params))
56 (session (nth 0 processed-params))
57 (vars (nth 1 processed-params))
58 (result-params (nth 2 processed-params))
59 (result-type (nth 3 processed-params))
60 (full-body (org-babel-expand-body:perl
61 body params processed-params))
62 (session (org-babel-perl-initiate-session session)))
63 (org-babel-reassemble-table
64 (org-babel-perl-evaluate session full-body result-type)
65 (org-babel-pick-name
66 (nth 4 processed-params) (cdr (assoc :colnames params)))
67 (org-babel-pick-name
68 (nth 5 processed-params) (cdr (assoc :rownames params))))))
70 (defun org-babel-prep-session:perl (session params)
71 "Prepare SESSION according to the header arguments in PARAMS."
72 (error "Sessions are not supported for Perl."))
74 ;; helper functions
76 (defun org-babel-perl-var-to-perl (var)
77 "Convert an elisp value to a perl variable.
78 The elisp value, VAR, is converted to a string of perl source code
79 specifying a var of the same value."
80 (if (listp var)
81 (concat "[" (mapconcat #'org-babel-perl-var-to-perl var ", ") "]")
82 (format "%S" var)))
84 (defvar org-babel-perl-buffers '(:default . nil))
86 (defun org-babel-perl-initiate-session (&optional session params)
87 "Return nil because sessions are not supported by perl"
88 nil)
90 (defvar org-babel-perl-wrapper-method
92 sub main {
95 @r = main;
96 open(o, \">%s\");
97 print o join(\"\\n\", @r), \"\\n\"")
99 (defvar org-babel-perl-pp-wrapper-method
100 nil)
102 (defun org-babel-perl-evaluate (session body &optional result-type)
103 "Pass BODY to the Perl process in SESSION.
104 If RESULT-TYPE equals 'output then return a list of the outputs
105 of the statements in BODY, if RESULT-TYPE equals 'value then
106 return the value of the last statement in BODY, as elisp."
107 (when session (error "Sessions are not supported for Perl."))
108 (case result-type
109 (output (org-babel-eval org-babel-perl-command body))
110 (value (let ((tmp-file (make-temp-file "org-babel-perl-results-")))
111 (org-babel-eval
112 org-babel-perl-command
113 (format org-babel-perl-wrapper-method body tmp-file))
114 (org-babel-eval-read-file tmp-file)))))
116 (provide 'ob-perl)
118 ;; arch-tag: 88ef9396-d857-4dc3-8946-5a72bdfa2337
120 ;;; ob-perl.el ends here