org.el: exclude current heading from the refile table.
[org-mode/org-mode-NeilSmithlineMods.git] / lisp / ob-fortran.el
blob4c366cc7964efc7505db330193a86b399e48aa9c
1 ;;; ob-fortran.el --- org-babel functions for fortran
3 ;; Copyright (C) 2011 Free Software Foundation, Inc.
5 ;; Author: Sergey Litvinov (based on ob-C.el by Eric Schulte)
6 ;; Keywords: literate programming, reproducible research, fortran
7 ;; Homepage: http://orgmode.org
8 ;; Version: 7.6
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 fortran code.
30 ;;; Code:
31 (require 'ob)
32 (require 'ob-eval)
33 (require 'cc-mode)
35 (declare-function org-entry-get "org"
36 (pom property &optional inherit literal-nil))
38 (defvar org-babel-tangle-lang-exts)
39 (add-to-list 'org-babel-tangle-lang-exts '("fortran" . "F90"))
41 (defvar org-babel-default-header-args:fortran '())
43 (defvar org-babel-fortran-compiler "gfortran"
44 "fortran command used to compile a fortran source code file into an
45 executable.")
47 (defun org-babel-execute:fortran (body params)
48 "This function should only be called by `org-babel-execute:fortran'"
49 (let* ((tmp-src-file (org-babel-temp-file "fortran-src-" ".F90"))
50 (tmp-bin-file (org-babel-temp-file "fortran-bin-"))
51 (cmdline (cdr (assoc :cmdline params)))
52 (flags (cdr (assoc :flags params)))
53 (full-body (org-babel-expand-body:fortran body params))
54 (compile
55 (progn
56 (with-temp-file tmp-src-file (insert full-body))
57 (org-babel-eval
58 (format "%s -o %s %s %s"
59 org-babel-fortran-compiler
60 (org-babel-process-file-name tmp-bin-file)
61 (mapconcat 'identity
62 (if (listp flags) flags (list flags)) " ")
63 (org-babel-process-file-name tmp-src-file)) ""))))
64 ((lambda (results)
65 (org-babel-reassemble-table
66 (if (member "vector" (cdr (assoc :result-params params)))
67 (let ((tmp-file (org-babel-temp-file "f-")))
68 (with-temp-file tmp-file (insert results))
69 (org-babel-import-elisp-from-file tmp-file))
70 (org-babel-read results))
71 (org-babel-pick-name
72 (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
73 (org-babel-pick-name
74 (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params)))))
75 (org-babel-trim
76 (org-babel-eval
77 (concat tmp-bin-file (if cmdline (concat " " cmdline) "")) "")))))
79 (defun org-babel-expand-body:fortran (body params)
80 "Expand a block of fortran or fortran code with org-babel according to
81 it's header arguments."
82 (let ((vars (mapcar #'cdr (org-babel-get-header params :var)))
83 (main-p (not (string= (cdr (assoc :main params)) "no")))
84 (includes (or (cdr (assoc :includes params))
85 (org-babel-read (org-entry-get nil "includes" t))))
86 (defines (org-babel-read
87 (or (cdr (assoc :defines params))
88 (org-babel-read (org-entry-get nil "defines" t))))))
89 (mapconcat 'identity
90 (list
91 ;; includes
92 (mapconcat
93 (lambda (inc) (format "#include %s" inc))
94 (if (listp includes) includes (list includes)) "\n")
95 ;; defines
96 (mapconcat
97 (lambda (inc) (format "#define %s" inc))
98 (if (listp defines) defines (list defines)) "\n")
99 ;; body
100 (if main-p
101 (org-babel-fortran-ensure-main-wrap
102 (concat
103 ;; variables
104 (mapconcat 'org-babel-fortran-var-to-fortran vars "\n")
105 body) params)
106 body) "\n") "\n")))
108 (defun org-babel-fortran-ensure-main-wrap (body params)
109 "Wrap body in a \"program ... end program\" block if none exists."
110 (if (string-match "^[ \t]*program[ \t]*.*" (capitalize body))
111 (let ((vars (mapcar #'cdr (org-babel-get-header params :var))))
112 (if vars (error "cannot use :vars if 'program' statment is present"))
113 body)
114 (format "program main\n%s\nend program main\n" body)))
116 (defun org-babel-prep-session:fortran (session params)
117 "This function does nothing as fortran is a compiled language with no
118 support for sessions"
119 (error "fortran is a compiled languages -- no support for sessions"))
121 (defun org-babel-load-session:fortran (session body params)
122 "This function does nothing as fortran is a compiled language with no
123 support for sessions"
124 (error "fortran is a compiled languages -- no support for sessions"))
126 ;; helper functions
128 (defun org-babel-fortran-var-to-fortran (pair)
129 "fortranonvert an elisp val into a string of fortran code specifying a var
130 of the same value."
131 ;; TODO list support
132 (let ((var (car pair))
133 (val (cdr pair)))
134 (when (symbolp val)
135 (setq val (symbol-name val))
136 (when (= (length val) 1)
137 (setq val (string-to-char val))))
138 (cond
139 ((integerp val)
140 (format "integer, parameter :: %S = %S\n" var val))
141 ((floatp val)
142 (format "real, parameter :: %S = %S\n" var val))
143 ((or (characterp val))
144 (format "character, parameter :: %S = '%S'\n" var val))
145 ((stringp val)
146 (format "character, parameter :: %S(%d) = '%s'\n"
147 var (length val) val))
148 ((listp val)
149 (format "real, parameter :: %S(%d) = %s\n"
150 var (length val) (ob-fortran-transform-list val)))
152 (error (format "the type of parameter %s is not supported by ob-fortran"
153 var))))))
155 (defun ob-fortran-transform-list (val)
156 "Return a fortran representation of enclose syntactic lists."
157 (if (listp val)
158 (concat "(/" (mapconcat #'ob-fortran-transform-list val ", ") "/)")
159 (format "%S" val)))
161 (provide 'ob-fortran)
163 ;; arch-tag: 466c8aa4-b919-462d-b1da-3e147073b56e
165 ;;; ob-fortran.el ends here