org-element: COMMENT and QUOTE keywords, ARCHIVE tags are case sensitive
[org-mode/org-mode-NeilSmithlineMods.git] / lisp / ob-fortran.el
blobfadec9d7af471458a1e1ebf5f26847aef0f35885
1 ;;; ob-fortran.el --- org-babel functions for fortran
3 ;; Copyright (C) 2011-2012 Sergey Litvinov, Eric Schulte
5 ;; Authors: Sergey Litvinov (based on ob-C.el by Eric Schulte), Eric Schulte
6 ;; Keywords: literate programming, reproducible research, fortran
7 ;; Homepage: http://orgmode.org
8 ;; Version: 7.8.02
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 3, or (at your option)
13 ;; any later version.
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
25 ;;; Commentary:
27 ;; Org-Babel support for evaluating fortran code.
29 ;;; Code:
30 (require 'ob)
31 (require 'ob-eval)
32 (require 'cc-mode)
34 (declare-function org-entry-get "org"
35 (pom property &optional inherit literal-nil))
37 (defvar org-babel-tangle-lang-exts)
38 (add-to-list 'org-babel-tangle-lang-exts '("fortran" . "F90"))
40 (defvar org-babel-default-header-args:fortran '())
42 (defvar org-babel-fortran-compiler "gfortran"
43 "fortran command used to compile a fortran source code file into an
44 executable.")
46 (defun org-babel-execute:fortran (body params)
47 "This function should only be called by `org-babel-execute:fortran'"
48 (let* ((tmp-src-file (org-babel-temp-file "fortran-src-" ".F90"))
49 (tmp-bin-file (org-babel-temp-file "fortran-bin-"))
50 (cmdline (cdr (assoc :cmdline params)))
51 (flags (cdr (assoc :flags params)))
52 (full-body (org-babel-expand-body:fortran body params))
53 (compile
54 (progn
55 (with-temp-file tmp-src-file (insert full-body))
56 (org-babel-eval
57 (format "%s -o %s %s %s"
58 org-babel-fortran-compiler
59 (org-babel-process-file-name tmp-bin-file)
60 (mapconcat 'identity
61 (if (listp flags) flags (list flags)) " ")
62 (org-babel-process-file-name tmp-src-file)) ""))))
63 ((lambda (results)
64 (org-babel-reassemble-table
65 (if (member "vector" (cdr (assoc :result-params params)))
66 (let ((tmp-file (org-babel-temp-file "f-")))
67 (with-temp-file tmp-file (insert results))
68 (org-babel-import-elisp-from-file tmp-file))
69 (org-babel-read results))
70 (org-babel-pick-name
71 (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
72 (org-babel-pick-name
73 (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params)))))
74 (org-babel-trim
75 (org-babel-eval
76 (concat tmp-bin-file (if cmdline (concat " " cmdline) "")) "")))))
78 (defun org-babel-expand-body:fortran (body params)
79 "Expand a block of fortran or fortran code with org-babel according to
80 it's header arguments."
81 (let ((vars (mapcar #'cdr (org-babel-get-header params :var)))
82 (main-p (not (string= (cdr (assoc :main params)) "no")))
83 (includes (or (cdr (assoc :includes params))
84 (org-babel-read (org-entry-get nil "includes" t))))
85 (defines (org-babel-read
86 (or (cdr (assoc :defines params))
87 (org-babel-read (org-entry-get nil "defines" t))))))
88 (mapconcat 'identity
89 (list
90 ;; includes
91 (mapconcat
92 (lambda (inc) (format "#include %s" inc))
93 (if (listp includes) includes (list includes)) "\n")
94 ;; defines
95 (mapconcat
96 (lambda (inc) (format "#define %s" inc))
97 (if (listp defines) defines (list defines)) "\n")
98 ;; body
99 (if main-p
100 (org-babel-fortran-ensure-main-wrap
101 (concat
102 ;; variables
103 (mapconcat 'org-babel-fortran-var-to-fortran vars "\n")
104 body) params)
105 body) "\n") "\n")))
107 (defun org-babel-fortran-ensure-main-wrap (body params)
108 "Wrap body in a \"program ... end program\" block if none exists."
109 (if (string-match "^[ \t]*program[ \t]*.*" (capitalize body))
110 (let ((vars (mapcar #'cdr (org-babel-get-header params :var))))
111 (if vars (error "cannot use :vars if 'program' statement is present"))
112 body)
113 (format "program main\n%s\nend program main\n" body)))
115 (defun org-babel-prep-session:fortran (session params)
116 "This function does nothing as fortran is a compiled language with no
117 support for sessions"
118 (error "fortran is a compiled languages -- no support for sessions"))
120 (defun org-babel-load-session:fortran (session body params)
121 "This function does nothing as fortran is a compiled language with no
122 support for sessions"
123 (error "fortran is a compiled languages -- no support for sessions"))
125 ;; helper functions
127 (defun org-babel-fortran-var-to-fortran (pair)
128 "Convert an elisp val into a string of fortran code specifying a var
129 of the same value."
130 ;; TODO list support
131 (let ((var (car pair))
132 (val (cdr pair)))
133 (when (symbolp val)
134 (setq val (symbol-name val))
135 (when (= (length val) 1)
136 (setq val (string-to-char val))))
137 (cond
138 ((integerp val)
139 (format "integer, parameter :: %S = %S\n" var val))
140 ((floatp val)
141 (format "real, parameter :: %S = %S\n" var val))
142 ((or (integerp val))
143 (format "character, parameter :: %S = '%S'\n" var val))
144 ((stringp val)
145 (format "character(len=%d), parameter :: %S = '%s'\n"
146 (length val) var val))
147 ((listp val)
148 (format "real, parameter :: %S(%d) = %s\n"
149 var (length val) (ob-fortran-transform-list val)))
151 (error (format "the type of parameter %s is not supported by ob-fortran"
152 var))))))
154 (defun ob-fortran-transform-list (val)
155 "Return a fortran representation of enclose syntactic lists."
156 (if (listp val)
157 (concat "(/" (mapconcat #'ob-fortran-transform-list val ", ") "/)")
158 (format "%S" val)))
160 (provide 'ob-fortran)
162 ;;; ob-fortran.el ends here