├── .gitignore ├── Makefile ├── ob-racket-runtime-typed.rkt ├── ob-racket-runtime.rkt ├── README.org ├── ob-racket-runtime-private.rkt ├── EXAMPLE.org ├── ob-racket.el └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | \#* 3 | .\#* 4 | *.swp 5 | .DS_Store 6 | *.elc 7 | /compiled/ 8 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | byte-compile-racket : 2 | raco make ob-racket-runtime.rkt 3 | 4 | byte-compile-elisp : 5 | emacs --batch --eval '(byte-compile-file "ob-racket.el")' 6 | 7 | clean : 8 | -rm -r compiled 9 | -rm *.elc 10 | -------------------------------------------------------------------------------- /ob-racket-runtime-typed.rkt: -------------------------------------------------------------------------------- 1 | #lang typed/racket/base 2 | 3 | (provide ob-racket-begin-print-elisp 4 | ob-racket-begin-print-table) 5 | 6 | (require/typed "ob-racket-runtime-private.rkt" 7 | [datum->elisp (-> Any String)] 8 | [datum->table (-> Any String)]) 9 | 10 | (define-syntax-rule 11 | (ob-racket-begin-print-elisp body ...) 12 | (display 13 | (datum->elisp 14 | (let () 15 | body ...)))) 16 | 17 | (define-syntax-rule 18 | (ob-racket-begin-print-table body ...) 19 | (display 20 | (datum->table 21 | (let () 22 | body ...)))) 23 | 24 | #| 25 | 26 | Copyright (C) 2019-2024 the authors. 27 | 28 | Authors: Tero Hasu 29 | 30 | Permission is hereby granted, free of charge, to any person obtaining 31 | a copy of this software and associated documentation files (the 32 | "Software"), to deal in the Software without restriction, including 33 | without limitation the rights to use, copy, modify, merge, publish, 34 | distribute, sublicense, and/or sell copies of the Software, and to 35 | permit persons to whom the Software is furnished to do so, subject to 36 | the following conditions: 37 | 38 | The above copyright notice and this permission notice shall be 39 | included in all copies or substantial portions of the Software. 40 | 41 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 42 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 43 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 44 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 45 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 46 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 47 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 48 | 49 | |# 50 | -------------------------------------------------------------------------------- /ob-racket-runtime.rkt: -------------------------------------------------------------------------------- 1 | #lang racket/base 2 | 3 | #| 4 | |# 5 | 6 | (provide ob-racket-begin-print-elisp 7 | ob-racket-begin-print-table) 8 | 9 | (require "ob-racket-runtime-private.rkt") 10 | 11 | ;;; 12 | ;;; Racket data to Emacs Lisp. 13 | ;;; 14 | 15 | (define-syntax-rule 16 | (ob-racket-begin-print-elisp body ...) 17 | (display 18 | (datum->elisp 19 | (let () 20 | body ...)))) 21 | 22 | ;;; 23 | ;;; Racket data to Org Babel table. 24 | ;;; 25 | 26 | (define-syntax-rule 27 | (ob-racket-begin-print-table body ...) 28 | (display 29 | (datum->table 30 | (let () 31 | body ...)))) 32 | 33 | #| 34 | 35 | Copyright (C) 2019 the authors. 36 | 37 | Authors: Tero Hasu 38 | 39 | Permission is hereby granted, free of charge, to any person obtaining 40 | a copy of this software and associated documentation files (the 41 | "Software"), to deal in the Software without restriction, including 42 | without limitation the rights to use, copy, modify, merge, publish, 43 | distribute, sublicense, and/or sell copies of the Software, and to 44 | permit persons to whom the Software is furnished to do so, subject to 45 | the following conditions: 46 | 47 | The above copyright notice and this permission notice shall be 48 | included in all copies or substantial portions of the Software. 49 | 50 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 51 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 52 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 53 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 54 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 55 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 56 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 57 | 58 | |# 59 | -------------------------------------------------------------------------------- /README.org: -------------------------------------------------------------------------------- 1 | An [[https://www.gnu.org/software/emacs/][Emacs]] feature that defines support for [[https://orgmode.org/][Org-Mode]] Babel code blocks written in [[https://racket-lang.org/][Racket]]. 2 | 3 | Includes support for many of the usual Org =SRC= block header arguments (with the notable exception of =:session=), as well as some extras for controlling the way that code blocks are evaluated. The additional controls are perhaps more important for Racket than for most other languages, since Racket is not just one language. Rather, Racket is an open-ended, user-extensible collection of languages (including, e.g., Racket, Scribble, Slideshow, Redex, and Magnolisp), which may not always be evaluated in the Racket VM in the usual way of just invoking the =racket= executable. 4 | 5 | * Pre-Requisites 6 | 7 | To use =ob-racket=, first ensure that Org-Mode is installed in Emacs, as it probably is. For a richer editing experience you may also want to ensure that a major mode (such as [[https://github.com/greghendershott/racket-mode][racket-mode]]) has been configured for editing =racket= code blocks in Org. 8 | 9 | If your mode for editing Racket code is not called =racket-mode=, you'll want to specify the mode to use with something like: 10 | #+BEGIN_SRC emacs-lisp 11 | (add-to-list 'org-src-lang-modes '("racket" . geiser)) 12 | #+END_SRC 13 | 14 | * Installation 15 | 16 | Firstly, you may want to byte compile the "ob-racket.el" file, for example by invoking the =byte-compile-file= function under Emacs. 17 | 18 | Then make sure the =ob-racket= feature is on Emacs' =load-path= so that it can be loaded on demand: 19 | #+BEGIN_SRC emacs-lisp 20 | (add-to-list 'load-path "/my/path/to/emacs-ob-racket") 21 | #+END_SRC 22 | 23 | Furthermore, you should enable =racket= code for evaluation (e.g., upon invoking =org-babel-execute-src-block=, by default bound to =C-c C-c= under Org) by making available the =*:racket= definitions of the =ob-racket= feature. One way to do that is to simply =require= the feature, but Org can also be made to load =ob-racket= by having the customizable =org-babel-load-languages= variable include =racket=. Programmatically, something like this should do the trick: 24 | #+BEGIN_SRC emacs-lisp 25 | (org-babel-do-load-languages 26 | 'org-babel-load-languages 27 | '((emacs-lisp . t) 28 | (racket . t) 29 | ;;(scribble . t) ;; if Scribble support is available 30 | )) 31 | #+END_SRC 32 | 33 | Code evaluation may still require confirmation, depending on the value of the =org-confirm-babel-evaluate= variable, which is customizable, and may also be set file locally. 34 | 35 | Some =ob-racket= functionality relies on a Racket module for converting Racket values to Emacs Lisp ones, and it can be worthwhile to byte-compile the implementations of that module: 36 | #+begin_src shell-script 37 | raco make ob-racket-runtime*.rkt 38 | #+end_src 39 | The "ob-racket-runtime.rkt" module is the default implementation, but as there is no one “correct” way to map values between languages, nor is the particular implementation even compatible with all possible Racket-based languages, it is possible to use different implementations in different situations or with different =#lang='uages. An example "ob-racket-runtime-typed.rkt" module is included for Typed Racket. 40 | 41 | ** Installation with straight.el 42 | 43 | The cloning, byte-compilation, loading, and configuration can all be done declaratively with [[https://github.com/raxod502/straight.el][straight.el]], [[https://github.com/jwiegley/use-package][use-package]], and the included =ob-racket-raco-make-runtime-library= function: 44 | #+begin_src emacs-lisp 45 | (use-package ob-racket 46 | :after org 47 | :config 48 | (add-hook 'ob-racket-pre-runtime-library-load-hook 49 | #'ob-racket-raco-make-runtime-library) 50 | :straight (ob-racket 51 | :type git :host github :repo "hasu/emacs-ob-racket" 52 | :files ("*.el" "*.rkt"))) 53 | #+end_src 54 | 55 | * Configuration 56 | 57 | For evaluating code blocks written in Racket in the usual way the =ob-racket= feature by default assumes that there is an executable named "racket" in the search =PATH= of the Emacs process. If that is not the case then a different way of invoking Racket can be specified using =ob-racket='s “command templates.” 58 | 59 | For example, if you have set the variable =racket-program= for =racket-mode= so that it specifies a path to a =racket= program executable, then you can also configure the same value for =ob-racket= by overriding the default =racket= program template: 60 | #+begin_src emacs-lisp 61 | (setq ob-racket-custom-command-templates 62 | `((racket . ,racket-program))) 63 | #+end_src 64 | 65 | * Documentation 66 | 67 | There is no separate documentation for =ob-racket=, so look at the [[./ob-racket.el][source code]], and the Emacs Lisp docstrings of the functions and variables appearing there. You will probably also find the [[https://orgmode.org/org.html#Working-with-Source-Code][relevant Org documentation]] useful: 68 | #+BEGIN_SRC emacs-lisp 69 | (progn 70 | (info-display-manual "org") 71 | (Info-goto-node "Working With Source Code")) 72 | #+END_SRC 73 | -------------------------------------------------------------------------------- /ob-racket-runtime-private.rkt: -------------------------------------------------------------------------------- 1 | #lang racket/base 2 | 3 | #| 4 | |# 5 | 6 | (provide (all-defined-out)) 7 | 8 | (require racket/dict racket/extflonum racket/math 9 | racket/set racket/string 10 | compatibility/mlist) 11 | 12 | ;;; 13 | ;;; Racket data to Emacs Lisp. 14 | ;;; 15 | 16 | (define (char-code->string num) 17 | (string (integer->char num))) 18 | 19 | ;; Characters not needing escaping in Emacs Lisp character, symbol, or string syntax. 20 | (define (safe-ascii-code->string num) 21 | (cond 22 | ((= num 33) "!") 23 | ((= num 36) "$") 24 | ((= num 37) "%") 25 | ((= num 38) "&") 26 | ((= num 42) "*") 27 | ((= num 43) "+") 28 | ((= num 45) "-") 29 | ((= num 47) "/") 30 | ((<= 48 num 57) ;; 0-9 31 | (char-code->string num)) 32 | ((= num 58) ":") 33 | ((= num 60) "<") 34 | ((= num 61) "=") 35 | ((= num 62) ">") 36 | ((= num 63) "?") 37 | ((= num 64) "@") 38 | ((<= 65 num 90) ;; A-Z 39 | (char-code->string num)) 40 | ((= num 91) "[") 41 | ((= num 93) "]") 42 | ((= num 94) "^") 43 | ((= num 95) "_") 44 | ((<= 97 num 122) ;; a-z 45 | (char-code->string num)) 46 | ((= num 123) "{") 47 | ((= num 125) "}") 48 | ((= num 126) "~") 49 | (else #f))) 50 | 51 | (define (shorthand-escaped-ascii-code->string num) 52 | (cond 53 | ((= num 7) "\\a") 54 | ((= num 8) "\\b") 55 | ((= num 9) "\\t") 56 | ((= num 10) "\\n") 57 | ((= num 11) "\\v") 58 | ((= num 12) "\\f") 59 | ((= num 13) "\\r") 60 | ((= num 27) "\\e") 61 | ((= num 127) "\\d") 62 | (else #f))) 63 | 64 | (define (char->elisp ch) 65 | (define num (char->integer ch)) 66 | (cond 67 | ((safe-ascii-code->string num) 68 | => (lambda (str) (string-append "?" str))) 69 | ((shorthand-escaped-ascii-code->string num) 70 | => (lambda (str) (string-append "?" str))) 71 | ((= num 32) "\\s") 72 | ((= num 34) "?\\\"") 73 | ((= num 35) "?\\#") 74 | ((= num 39) "?\\'") 75 | ((= num 40) "?\\(") 76 | ((= num 41) "?\\)") 77 | ((= num 44) "?\\,") 78 | ((= num 46) "?\\.") 79 | ((= num 59) "?\\;") 80 | ((= num 92) "?\\\\") 81 | ((= num 96) "?\\`") 82 | ((= num 124) "?\\|") 83 | ((> num 4194303) 84 | ;; Not an Emacs Lisp character. 85 | (format "~s" ch)) 86 | (else 87 | ;; Print everything else as a number. 88 | ;; In Emacs Lisp, characters are just numbers. 89 | (number->string num)))) 90 | 91 | (define (string-pad s up-to-len pad-ch) 92 | (define len (string-length s)) 93 | (if (>= len up-to-len) 94 | s 95 | (let ((n (- up-to-len len))) 96 | (string-append (make-string n pad-ch) s)))) 97 | 98 | (define (u-escape v) 99 | (string-append "\\u" (string-pad (format "~x" v) 4 #\0))) 100 | 101 | (define (U-escape v) 102 | (string-append "\\U00" (string-pad (format "~x" v) 6 #\0))) 103 | 104 | (define (string-char->elisp ch) 105 | (define num (char->integer ch)) 106 | (cond 107 | ((shorthand-escaped-ascii-code->string num) 108 | => (lambda (str) str)) 109 | ((= num 34) "\\\"") 110 | ((= num 92) "\\\\") 111 | ((<= num 127) (string ch)) 112 | ((<= num #xffff) 113 | (u-escape num)) 114 | ((<= num #x10ffff) 115 | ;; Maximum Unicode code point, so we cannot use Unicode escapes for anything larger. 116 | (U-escape num)) 117 | ((<= num 4194303) 118 | ;; These codes are still allowed for characters in Emacs multibyte strings. 119 | (format "\\x~x\\ " num)) 120 | (else 121 | ;; Not an Emacs Lisp character. 122 | (format "~s" ch)))) 123 | 124 | (define (string->elisp str) 125 | (string-append "\"" 126 | (apply string-append 127 | (map string-char->elisp (string->list str))) 128 | "\"")) 129 | 130 | (define (symbol-escape str) 131 | (regexp-replace* 132 | #rx"([^a-zA-Z0-9+=*/_~!@$%^&:<>{}?-])" 133 | str 134 | "\\\\\\1")) 135 | 136 | (define (symbol->elisp sym) 137 | (define str (symbol->string sym)) 138 | (cond 139 | ((string=? str "") 140 | ;; Special syntax for the empty symbol. 141 | "\\#\\#") 142 | ((regexp-match? #rx"^[0-9]" str) 143 | (string-append "\\" (symbol-escape str))) 144 | (else 145 | (symbol-escape str)))) 146 | 147 | (define (number->elisp num) 148 | (cond 149 | ((exact-integer? num) 150 | ;; Emacs Lisp supports integers, with machine-dependent range. 151 | ;; Out of range integers may be treated as being of the floating point type. 152 | (format "~v" num)) 153 | ((not (real? num)) 154 | ;; Emacs Lisp does not support imaginary numbers. 155 | (format "~s" num)) 156 | ((nan? num) 157 | (if (negative? num) 158 | "-0.0e+NaN" 159 | "0.0e+NaN")) 160 | ((infinite? num) 161 | (if (negative? num) 162 | "-1.0e+INF" 163 | "1.0e+INF")) 164 | ((flonum? num) 165 | ;; Emacs Lisp supports floating point numbers. Flonums should 166 | ;; `print' in an Emacs Lisp compatible way, favoring shorter 167 | ;; representations. 168 | (format "~v" num)) 169 | ((single-flonum? num) 170 | ;; Single-precision numbers print in a way that is incompatible 171 | ;; with Emacs Lisp, but we can convert to something else. 172 | (format "~a" (exact->inexact (inexact->exact num)))) 173 | (else 174 | ;; Emacs Lisp does not support exact rational number syntax, so 175 | ;; convert to something else. 176 | (format "~a" (exact->inexact num))))) 177 | 178 | (define (datum->elisp datum) 179 | (cond 180 | ((boolean? datum) 181 | (if datum "t" "nil")) 182 | ((box? datum) 183 | (datum->elisp (list (unbox datum)))) 184 | ((bytes? datum) 185 | (datum->elisp (bytes->list datum))) 186 | ((char? datum) 187 | (char->elisp datum)) 188 | ((keyword? datum) 189 | (symbol->elisp (string->symbol (string-append ":" (keyword->string datum))))) 190 | ((null? datum) 191 | "nil") 192 | ((list? datum) 193 | (string-append "(" (string-join (map datum->elisp datum) " ") ")")) 194 | ((mpair? datum) 195 | ;; Emacs Lisp pairs actually are mutable, so this is appropriate. 196 | (datum->elisp (cons (mcar datum) (mcdr datum)))) 197 | ((and (extflonum-available?) (extflonum? datum)) 198 | ;; An extflonum is not a number, but we can turn it into one. 199 | (number->elisp (extfl->exact datum))) 200 | ((number? datum) 201 | (number->elisp datum)) 202 | ((pair? datum) 203 | (format "(~a . ~a)" (datum->elisp (car datum)) (datum->elisp (cdr datum)))) 204 | ((or (set? datum) (set-mutable? datum) (set-weak? datum)) 205 | (datum->elisp (set->list datum))) 206 | ((string? datum) 207 | (string->elisp datum)) 208 | ((symbol? datum) 209 | (symbol->elisp datum)) 210 | ((syntax? datum) 211 | (datum->elisp (syntax->datum datum))) 212 | ((vector? datum) 213 | (string-append "[" (string-join (map datum->elisp (vector->list datum)) " ") "]")) 214 | ((void? datum) 215 | "nil") 216 | ((dict? datum) 217 | ;; Emacs Lisp would also have hash tables with exotic #s(....) read syntax. 218 | (datum->elisp (dict->list datum))) 219 | (else 220 | ;; Unsupported data type: use Racket string representation, which is at least `read'able. 221 | (datum->elisp (format "~s" datum))))) 222 | 223 | ;;; 224 | ;;; Racket data to Org Babel table. 225 | ;;; 226 | 227 | (define (datum->table datum) 228 | (cond 229 | ((list? datum) 230 | (string-append "(" (string-join (map datum->table datum) " ") ")")) 231 | ((pair? datum) 232 | (datum->table (list (car datum) (cdr datum)))) 233 | ((mlist? datum) 234 | (datum->table (mlist->list datum))) 235 | ((mpair? datum) 236 | (datum->table (list (mcar datum) (mcdr datum)))) 237 | ((and (extflonum-available?) (extflonum? datum)) 238 | (datum->elisp datum)) 239 | ((number? datum) 240 | (datum->elisp datum)) 241 | ((or (set? datum) (set-mutable? datum) (set-weak? datum)) 242 | (datum->table (set->list datum))) 243 | ((symbol? datum) 244 | (datum->elisp datum)) 245 | ((syntax? datum) 246 | (datum->table (syntax->datum datum))) 247 | ((vector? datum) 248 | (datum->table (vector->list datum))) 249 | ((dict? datum) 250 | (datum->table (dict->list datum))) 251 | (else 252 | (datum->elisp (format "~s" datum))))) 253 | 254 | #| 255 | 256 | Copyright (C) 2019 the authors. 257 | 258 | Authors: Tero Hasu 259 | 260 | Permission is hereby granted, free of charge, to any person obtaining 261 | a copy of this software and associated documentation files (the 262 | "Software"), to deal in the Software without restriction, including 263 | without limitation the rights to use, copy, modify, merge, publish, 264 | distribute, sublicense, and/or sell copies of the Software, and to 265 | permit persons to whom the Software is furnished to do so, subject to 266 | the following conditions: 267 | 268 | The above copyright notice and this permission notice shall be 269 | included in all copies or substantial portions of the Software. 270 | 271 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 272 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 273 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 274 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 275 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 276 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 277 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 278 | 279 | |# 280 | -------------------------------------------------------------------------------- /EXAMPLE.org: -------------------------------------------------------------------------------- 1 | The body is evaluated, but nothing is output into the buffer. 2 | #+BEGIN_SRC racket :results output silent 3 | 'not-printed 4 | #+END_SRC 5 | 6 | The body is treated as a =racket= language expression, and the result is output. 7 | #+BEGIN_SRC racket :results value 8 | (define two 2) 9 | two ;; not output 10 | (* two two two) 11 | #+END_SRC 12 | 13 | #+RESULTS: 14 | : 8 15 | 16 | The body is in =racket=, and top-level values get output. 17 | #+BEGIN_SRC racket :results output 18 | #lang racket 19 | (list 1 2 3) 20 | (set 1 2 3) 21 | #+END_SRC 22 | 23 | #+RESULTS: 24 | : '(1 2 3) 25 | : (set 1 3 2) 26 | 27 | The body is in =racket/base=, and top-level values get output. 28 | #+BEGIN_SRC racket :results output 29 | #lang racket/base 30 | 1 31 | 2 32 | 3 33 | #+END_SRC 34 | 35 | #+RESULTS: 36 | : 1 37 | : 2 38 | : 3 39 | 40 | The body is in =racket/base=, and the value gets output. 41 | #+BEGIN_SRC racket :prologue "#lang racket/base" 42 | (list 1 2) 43 | #+END_SRC 44 | 45 | #+RESULTS: 46 | : (1 2) 47 | 48 | The above can also be written more concisely as 49 | #+BEGIN_SRC racket :lang racket/base 50 | (list 1 2) 51 | #+END_SRC 52 | 53 | #+RESULTS: 54 | : (1 2) 55 | 56 | The body is in =racket=, and we are passing in values for it. 57 | #+BEGIN_SRC racket :var x=5 :var y=6 58 | (/ x y) 59 | #+END_SRC 60 | 61 | #+RESULTS: 62 | : 5/6 63 | 64 | The body is in =racket/base=, and we are passing in values for it. 65 | #+BEGIN_SRC racket :var x="5" :var y="6" :prologue "#lang racket/base" 66 | (string-append x y) 67 | #+END_SRC 68 | 69 | #+RESULTS: 70 | : "56" 71 | 72 | The body is in =at-exp racket/base=, and we are passing in values for it. 73 | #+BEGIN_SRC racket :var x=''x :var y=''y :lang at-exp racket/base 74 | @list[x y] 75 | #+END_SRC 76 | 77 | #+RESULTS: 78 | : (x y) 79 | 80 | As above, but now requiring a library not otherwise available in =racket/base=. 81 | #+BEGIN_SRC racket :var x=5 :var y=6 :prologue "#lang racket/base\n(require racket/set)" 82 | (set x (add1 y)) 83 | #+END_SRC 84 | 85 | #+RESULTS: 86 | : # 87 | 88 | It is possible to show the =#lang= line and the =require= form in the listing. 89 | #+BEGIN_SRC racket :results output 90 | #lang racket/base 91 | (require racket/set) 92 | (for/set ([i (in-range 10)]) 93 | i) 94 | #+END_SRC 95 | 96 | #+RESULTS: 97 | : (set 1 5 9 3 7 0 2 6 4 8) 98 | 99 | As =require= is a top-level form, the above does not work for a =value= computing block. We can, however, write 100 | #+BEGIN_SRC racket :results value :lang racket/base :require racket/set 101 | (for/set ([i (in-range 100)]) 102 | i) 103 | (for/set ([i (in-range 10)]) 104 | i) 105 | #+END_SRC 106 | 107 | #+RESULTS: 108 | : # 109 | 110 | The =require= header argument accepts multiple require specifications. 111 | #+BEGIN_SRC racket :lang racket/base :require racket/function racket/set 112 | ((const (seteqv 1))) 113 | #+END_SRC 114 | 115 | #+RESULTS: 116 | : # 117 | 118 | Babel allows code block argument values to be specified as Emacs Lisp expressions. 119 | #+BEGIN_SRC racket :var fn=(file-name-nondirectory (buffer-file-name)) 120 | fn 121 | #+END_SRC 122 | 123 | #+RESULTS: 124 | : "EXAMPLE.org" 125 | 126 | Babel allows code blocks to be named. 127 | #+NAME: double 128 | #+BEGIN_SRC racket :var x=0 129 | (* x 2) 130 | #+END_SRC 131 | 132 | #+RESULTS: double 133 | : 0 134 | 135 | Babel allows code blocks to be called. Arguments tend to be given as Emacs Lisp values, and passed to Racket by =write='ing them within the program in Emacs Lisp literal syntax. 136 | #+CALL: double(x=3.14) 137 | 138 | #+RESULTS: 139 | : 6.28 140 | 141 | It is possible to refer to Racket names if they are in the language of the called block. In this case, the Emacs Lisp expression ='pi= self-evaluates to a symbol that prints as a Racket identifier defined in =racket/math=. 142 | #+CALL: double(x='pi) 143 | 144 | #+RESULTS: 145 | : 6.283185307179586 146 | 147 | Another way to get the same result is to state that the =:vars-are= given as Racket expression strings. 148 | #+CALL: double[:vars-are racket](x="pi") 149 | 150 | #+RESULTS: 151 | : 6.283185307179586 152 | 153 | We can avoid the requirement on =racket/math= by instead referencing an equivalent Emacs Lisp variable. 154 | #+CALL: double(x=(+ float-pi)) 155 | 156 | #+RESULTS: 157 | : 6.283185307179586 158 | 159 | #+NAME: some-x 160 | #+BEGIN_SRC racket 161 | 3 162 | #+END_SRC 163 | 164 | #+RESULTS: some-x 165 | : 3 166 | 167 | As usual in Babel, a =:var= value can be given by referencing a named block. The result of a Racket block is normally a string of =write='n Racket values, and with =:vars-are racket= we can avoid having to have the referencing block explicitly parse those expressions. 168 | #+BEGIN_SRC racket :var x=some-x :vars-are racket 169 | (/ (+ x 7) x) 170 | #+END_SRC 171 | 172 | #+RESULTS: 173 | : 10/3 174 | 175 | It is especially useful to insert the =:var= values as Racket expressions if we have an existing block that does not do any parsing. 176 | #+CALL: double[:vars-are racket](x=some-x) 177 | 178 | #+RESULTS: 179 | : 6 180 | 181 | We would have full control over how argument values are converted into Racket expressions by overriding the =var-value= code template. The =:var-value= header argument is less flexible. 182 | #+BEGIN_SRC racket :var x=1 :var y=3 :var z=7 :var-value "1/2" 183 | (+ x y z) 184 | #+END_SRC 185 | 186 | #+RESULTS: 187 | : 3/2 188 | 189 | The =:value= to turn into a Racket expression is the raw header argument value, and must be coerced into a Racket string. 190 | #+BEGIN_SRC racket :var x=1 :var y=3 :var z=7 :var-value '(format "(- %s 1)" :value) 191 | (+ x y z) 192 | #+END_SRC 193 | 194 | #+RESULTS: 195 | : 8 196 | 197 | Code in the listing can be run via a submodule that is not shown in the listing itself. 198 | #+HEADER: :epilogue "(module* main #f (run))" 199 | #+BEGIN_SRC racket :results output :var x='5/6 :var y='6/7 :lang racket/base 200 | (define (run) 201 | (write (+ x y))) 202 | #+END_SRC 203 | 204 | #+RESULTS: 205 | : 71/42 206 | 207 | For even more control over what gets evaluated, it is possible to override templates using header arguments of the same name. 208 | #+HEADER: :program '(lines "#lang racket/base" "(require racket/function racket/set)" :body) 209 | #+BEGIN_SRC racket 210 | (identity (seteq 'x 'y 'z)) 211 | #+END_SRC 212 | 213 | #+RESULTS: 214 | : (seteq 'x 'y 'z) 215 | 216 | More complex program templates can perhaps be more conveniently specified as an S-expression. 217 | #+BEGIN_SRC racket :program '(sexp module m racket/base (require racket/function racket/set) (define (run) :body) (module+ main (run))) 218 | (identity (set "a" "b" "c")) 219 | #+END_SRC 220 | 221 | #+RESULTS: 222 | : (set "b" "c" "a") 223 | 224 | There is special support for converting a Racket result to Emacs Lisp. 225 | #+NAME: some-data 226 | #+BEGIN_SRC racket :results-as elisp 227 | (list #("a" "b" "c") (seteq 'd 'e 'f)) 228 | #+END_SRC 229 | 230 | #+RESULTS: some-data 231 | : (["a" "b" "c"] (f d e)) 232 | 233 | Such conversion can be useful if we wish to =read= it into Emacs Lisp to do further processing. 234 | #+BEGIN_SRC emacs-lisp :var x=some-data :results scalar 235 | (reverse (apply #'append (read x))) 236 | #+END_SRC 237 | 238 | #+RESULTS: 239 | : (e d f "c" "b" "a") 240 | 241 | As usual with Babel, we can display results as a table. 242 | #+CALL: some-data() :results table 243 | 244 | #+RESULTS: 245 | | [a b c] | (f d e) | 246 | 247 | Or, we can display them as a list. 248 | #+CALL: some-data() :results list 249 | 250 | #+RESULTS: 251 | - ["a" "b" "c"] 252 | - (f d e) 253 | 254 | Unless otherwise specified, "racket" blocks default to the "scalar" result type. 255 | #+CALL: some-data() 256 | 257 | #+RESULTS: 258 | : (["a" "b" "c"] (f d e)) 259 | 260 | Racket data can be output two dimensionally. 261 | #+BEGIN_SRC racket :results table 262 | (set (seteq 'a 'b 'c) 263 | (vector-immutable 'h 'i 'j) 264 | (list 'x 'y 'z)) 265 | #+END_SRC 266 | 267 | #+RESULTS: 268 | | h | i | j | 269 | | x | y | z | 270 | | b | c | a | 271 | 272 | #+BEGIN_SRC racket :results table 273 | #hash((1 . "one") (2 . "two") (3 . "three")) 274 | #+END_SRC 275 | 276 | #+RESULTS: 277 | | 1 | "one" | 278 | | 3 | "three" | 279 | | 2 | "two" | 280 | 281 | It is possible to pass arguments to a Racket program. 282 | #+BEGIN_SRC racket :cmdline a b c 283 | (current-command-line-arguments) 284 | #+END_SRC 285 | 286 | #+RESULTS: 287 | : #("a" "b" "c") 288 | 289 | Should we need the evaluated Racket source with a specific name or in a specific directory, we can specify those names, but the directory should already exist, since it does not get created. 290 | #+BEGIN_SRC racket :in-file /tmp/foo/bar.rkt :eval no-export 291 | 'tmp/foo 292 | #+END_SRC 293 | 294 | #+RESULTS: 295 | : tmp/foo 296 | 297 | The =dir= header argument is standard for Babel, also affecting the =in-file= header argument, since it actually temporarily changes the working directory. 298 | #+BEGIN_SRC racket :dir /tmp/foo :in-file bar.rkt :eval no-export 299 | 'baz 300 | #+END_SRC 301 | 302 | #+RESULTS: 303 | : baz 304 | 305 | We might want to know the Racket source file name if we use a custom command to evaluate it, and that command assumes a certain name. However, if the command accepts the name as an argument, then we can just pass =in-file= to it. 306 | #+HEADER: :command '(spaced "strings" (file :in-file)) 307 | #+BEGIN_SRC racket 308 | 'baz 309 | #+END_SRC 310 | 311 | #+RESULTS: 312 | : #lang racket 313 | : (write (let () 314 | : 'baz)) 315 | 316 | This can be useful for inspecting the code that is being executed. 317 | #+HEADER: :command '(spaced "cat" (file :in-file)) 318 | #+BEGIN_SRC racket :lang racket/base :var x=1 y=2 z='1/100 319 | (* x y z) 320 | #+END_SRC 321 | 322 | #+RESULTS: 323 | : #lang racket/base 324 | : 325 | : (write (let () 326 | : (define-values (x y z) (values 1 2 1/100)) 327 | : (* x y z))) 328 | 329 | Some languages may not support that form of defining variables, or we may want to have the previous header argument variables to be in scope for the next variable initializer. The variable definition syntax can be specified with =:define-var=. 330 | #+HEADER: :command '(spaced "cat" (file :in-file)) 331 | #+HEADER: :define-var '(parens (spaced "define" var-name var-value)) 332 | #+BEGIN_SRC racket :lang racket/base :var x=1 y='x z='(+ x y) 333 | (+ x y z) 334 | #+END_SRC 335 | 336 | #+RESULTS: 337 | : #lang racket/base 338 | : 339 | : (write (let () 340 | : (define x 1) 341 | : (define y x) 342 | : (define z (+ x y)) 343 | : (+ x y z))) 344 | 345 | There is a predefined shorthand =with-define= for using a sequence of =define= forms instead of the default =define-values= form. 346 | #+HEADER: :define-var 'with-define 347 | #+BEGIN_SRC racket :lang racket/base :var x=1 y='x z='(+ x y) 348 | (+ x y z) 349 | #+END_SRC 350 | 351 | #+RESULTS: 352 | : 4 353 | 354 | #+HEADER: :command '(spaced "cat" (file :in-file)) 355 | #+HEADER: :define-var '(format "val %s = %s;" :name :value) 356 | #+HEADER: :program '(lines prologue define-vars (spaced "println" :body)) 357 | #+BEGIN_SRC racket :lang some :var x=1 y='x z="x + y" 358 | x + y + z 359 | #+END_SRC 360 | 361 | #+RESULTS: 362 | : #lang some 363 | : 364 | : val x = 1; 365 | : val y = x; 366 | : val z = x + y; 367 | : println x + y + z 368 | 369 | /Typed Racket/ is a well-known Racket dialect. 370 | #+begin_src racket :lang typed/racket :results output 371 | (let ([x : Number 1]) 372 | x) 373 | #+end_src 374 | 375 | #+RESULTS: 376 | : 1 377 | 378 | #+begin_src racket :lang typed/racket :results value 379 | (let ([x : Number 1]) 380 | (add1 x)) 381 | #+end_src 382 | 383 | #+RESULTS: 384 | : 2 385 | 386 | When using Typed Racket the =require= forms or the required modules may need to be different to provide static type information. For this code =ob-racket= automatically uses "ob-racket-runtime-typed.rkt" in constructing the Org table: 387 | #+begin_src racket :lang typed/racket :results table 388 | '((1 2) (3 4)) 389 | #+end_src 390 | 391 | #+RESULTS: 392 | | 1 | 2 | 393 | | 3 | 4 | 394 | 395 | We may sometimes need to be more explicit about such choices if the =#lang= is not known to =ob-racket=: 396 | #+begin_src racket :lang at-exp typed/racket :results table :ob-rkt ob-racket-runtime-typed 397 | @reverse['((1 2) (3 4))] 398 | #+end_src 399 | 400 | #+RESULTS: 401 | | 3 | 4 | 402 | | 1 | 2 | 403 | 404 | We can also give full require specifications as one would with =:require=, but for selecting the modules for =ob-racket= processing time. For example if we had installed =ob-racket= as a Racket package we should be able to write: 405 | #+begin_src racket :lang at-exp typed/racket :results table :ob-rkt-require ob-racket/ob-racket-runtime-typed 406 | @map[(lambda ([xs : (Listof Integer)]) (map add1 xs)) 407 | '((1 2 3) (4 5 6) (7 8 9))] 408 | #+end_src 409 | 410 | #+RESULTS: 411 | | 2 | 3 | 4 | 412 | | 5 | 6 | 7 | 413 | | 8 | 9 | 10 | 414 | 415 | There are also typed Scheme languages for Racket. 416 | #+begin_src racket :lang typed/scheme 417 | '(1/5 2/5 3/5 4/5) 418 | #+end_src 419 | 420 | #+RESULTS: 421 | : (1/5 2/5 3/5 4/5) 422 | 423 | Where conversion to Emacs Lisp is required it gets done using "ob-racket-runtime-typed.rkt" also for =typed/scheme=: 424 | #+begin_src racket :lang typed/scheme :results-as elisp 425 | '(1/5 2/5 3/5 4/5) 426 | #+end_src 427 | 428 | #+RESULTS: 429 | : (0.2 0.4 0.6 0.8) 430 | 431 | #+begin_src racket :lang typed/scheme/base :results list 432 | '(1/5 2/5 3/5 4/5) 433 | #+end_src 434 | 435 | #+RESULTS: 436 | - 0.2 437 | - 0.4 438 | - 0.6 439 | - 0.8 440 | 441 | #+begin_src racket :lang typed-scheme :results table 442 | '(1/5 2/5 3/5 4/5) 443 | #+end_src 444 | 445 | #+RESULTS: 446 | | 0.2 | 0.4 | 0.6 | 0.8 | 447 | 448 | It is possible to evaluate the code block content as an executable script, if that is what one wants to demonstrate with the listing. It is important here to ensure that the shebang line is the very first one, but that can be ensured by having nothing but the code block body in the program. 449 | #+HEADER: :command '(spaced "chmod" "u+x" (file :in-file) "&&" (file :in-file)) 450 | #+BEGIN_SRC racket :results output :program ':body 451 | #!/usr/bin/env racket 452 | #lang racket 453 | "This is a script written in Racket." 454 | #+END_SRC 455 | 456 | #+RESULTS: 457 | : "This is a script written in Racket." 458 | 459 | Finally, for something useful, let us byte-compile the ob-racket Racket runtime: 460 | #+BEGIN_SRC racket :require compiler/compiler :results output 461 | ((compile-zos #f #:module? #t #:verbose? #t) 462 | '("ob-racket-runtime.rkt") 'auto) 463 | #+END_SRC 464 | 465 | #+RESULTS: 466 | : [output to "./compiled/ob-racket-runtime_rkt.zo"] 467 | 468 | Do the byte-compilation using =raco=: 469 | #+BEGIN_SRC racket :require raco/all-tools :results silent 470 | (define raco-make-spec (hash-ref (all-tools) "make")) 471 | (parameterize ([current-command-line-arguments (vector "ob-racket-runtime.rkt")]) 472 | (dynamic-require (second raco-make-spec) #f)) 473 | #+END_SRC 474 | -------------------------------------------------------------------------------- /ob-racket.el: -------------------------------------------------------------------------------- 1 | ;;; ob-racket.el --- Racket SRC block support for Org -*- lexical-binding: t; -*- 2 | 3 | ;; Copyright (C) 2015, 2019, 2020, 2021, 2024 the authors 4 | ;; 5 | ;; Authors: Tero Hasu 6 | ;; Homepage: https://github.com/hasu/emacs-ob-racket 7 | 8 | ;;; Commentary: 9 | ;; 10 | ;; Support for "racket" language blocks in Org mode Babel. 11 | ;; 12 | ;; Since Racket is itself such a customizable language, the Racket 13 | ;; Babel support defined here also features customization options and 14 | ;; mechanisms. 15 | ;; 16 | ;; As usual for Babel, the default SRC block header arguments for 17 | ;; "racket" blocks may be specified by setting 18 | ;; `org-babel-default-header-args:racket'. 19 | ;; 20 | ;; There are "racket" specific options that may be set. For cases 21 | ;; where a header argument does not specify the Racket language, 22 | ;; `ob-racket-default-lang' specifies the default language to use. The 23 | ;; default command used for evaluating "racket code blocks is 24 | ;; specified by `ob-racket-default-command'. For determining whether a 25 | ;; block of code already contains a #lang specifier, the 26 | ;; `ob-racket-hash-lang-regexp' is used. The variable 27 | ;; `ob-racket-preprocessor-function' determines the specifics of how 28 | ;; the code and the header arguments are affected by the presence of a 29 | ;; #lang line. 30 | ;; 31 | ;; To further customize the behavior, you may provide custom templates 32 | ;; to use for constructing Racket programs and shell commands to run 33 | ;; for evaluating the code. The variables 34 | ;; `ob-racket-custom-code-templates' and 35 | ;; `ob-racket-custom-command-templates' exist for that purpose. The 36 | ;; templates also for the most part determine which header arguments 37 | ;; affect SRC block handling. The default templates assume that 38 | ;; `require`, `define-values`, `values`, `let`, and `write` have their 39 | ;; `racket` language semantics. 40 | ;; 41 | ;; As templates may be specified as functions, it is possible for them 42 | ;; to do unsafe things. Org's standard `org-babel-safe-header-args' 43 | ;; facility may be used to indicate what header arguments are safe to 44 | ;; set, and with what values, given the set of templates in use. 45 | 46 | ;; This file is not part of GNU Emacs. 47 | 48 | ;;; License: 49 | ;; 50 | ;; This file is free software: you can redistribute it and/or modify 51 | ;; it under the terms of the GNU General Public License as published 52 | ;; by the Free Software Foundation, either version 3 of the License, 53 | ;; or (at your option) any later version. 54 | ;; 55 | ;; This file is distributed in the hope that it will be useful, but 56 | ;; WITHOUT ANY WARRANTY; without even the implied warranty of 57 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 58 | ;; General Public License for more details. 59 | ;; 60 | ;; You should have received a copy of the GNU General Public License 61 | ;; along with this program. If not, see . 62 | 63 | (require 'cl-lib) 64 | (require 'ob) 65 | (require 'subr-x) 66 | 67 | ;;; Code: 68 | 69 | (eval-when-compile 70 | (defvar org-babel-tangle-lang-exts)) 71 | (add-to-list 'org-babel-tangle-lang-exts '("racket" . "rkt")) 72 | 73 | (defvar org-babel-default-header-args:racket '((:session . "none")) 74 | "Default header arguments for a Racket SRC block.") 75 | 76 | (defvar ob-racket-custom-code-templates nil 77 | "Code template overrides and additions. 78 | A list of the form ((SYMBOL . TEMPLATE) ...). See 79 | `ob-racket-expand-template' for details on how templates are 80 | expanded.") 81 | 82 | (defvar ob-racket-custom-command-templates nil 83 | "Command template overrides and additions. 84 | A list of the form ((SYMBOL . TEMPLATE) ...). See 85 | `ob-racket-expand-template' for details on how templates are 86 | expanded.") 87 | 88 | (defvar ob-racket-default-lang "racket" 89 | "Default #lang Racket language. 90 | By default (as specified in `ob-racket-default-code-templates'), 91 | this default #lang gets used when no :lang parameter is given for 92 | a code block, and when the body of the code block does not appear 93 | to already contain a #lang line (the `ob-racket-hash-lang-regexp' 94 | pattern is used to look for it).") 95 | 96 | (defvar ob-racket-default-command 97 | '(spaced racket "-u" (file :in-file) cmdline) 98 | "Template for command to execute. 99 | May be overridden for a code block with the :command header 100 | argument. As with some other Babel languages, a :cmdline header 101 | argument may be used to specify arguments to the executed 102 | script.") 103 | 104 | (defvar ob-racket-hash-lang-regexp 105 | "^[[:space:]]*#lang[[:space:]]+\\(.+\\)$" 106 | "A regexp for finding \"#lang\" from body. 107 | The regular expression should match the entire #lang line. The 108 | first parenthesized expression should give the language name. To 109 | prevent such ad-hoc search of a #lang specifier, either set this 110 | regexp to nil, or give a :lang header argument for each SRC 111 | block.") 112 | 113 | (defvar ob-racket-debug nil 114 | "Whether to dump or preserve information. 115 | The symbol `dump-params' means to dump parameters. The symbol 116 | `dump-code' means to dump code template expansion results without 117 | evaluating. The symbol `dump-command' means the same, but for 118 | command templates. The symbol `keep-file' means to evaluate, but 119 | leave the source file intact. The symbol `dump-result' means to 120 | dump the result of code block evaluation without any (table, 121 | list, etc.) formatting.") 122 | 123 | (defvar ob-racket-preprocessor-function 124 | (lambda (body params) 125 | (when (and ob-racket-hash-lang-regexp 126 | (not (assq :lang params)) 127 | (not (assq :lang-line params)) 128 | (not (assq :prologue params))) 129 | (save-match-data 130 | (when (let ((case-fold-search nil)) 131 | (string-match ob-racket-hash-lang-regexp body)) 132 | (let* ((lang-line (match-string 0 body)) 133 | (lang-spec (string-trim (match-string 1 body))) 134 | (result-type (cdr (assq :result-type params))) 135 | (is-value-type (eq 'value result-type))) 136 | ;; Set both :lang and :lang-line, with the former only for 137 | ;; checking, as the latter encompasses it. 138 | (setq params (cons (cons :lang lang-spec) params)) 139 | (if is-value-type 140 | (setq params (cons (cons :lang-line lang-line) params) 141 | body (replace-match "" t t body)) 142 | (setq params (cons (cons :lang-line nil) params))))))) 143 | (list body params)) 144 | "A function for preprocessing the block. 145 | If this variable is non-nil, it is used for preprocessing the SRC 146 | block content and the header arguments right before calling 147 | `ob-racket-expand-template' with the program template to arrive 148 | at the complete Racket source file content to evaluate. Any 149 | function set to this variable should accept a (BODY PARAMS) 150 | argument list, and also return a '(BODY PARAMS) list.") 151 | 152 | (defvar ob-racket-runtime-libraries 153 | '("ob-racket-runtime.rkt" 154 | "ob-racket-runtime-typed.rkt") 155 | "Racket-based runtime libraries to be byte-compiled. 156 | A list of file names to be processed if 157 | `ob-racket-raco-make-runtime-library' is called.") 158 | 159 | (defvar ob-racket-locate-runtime-library-function 160 | (lambda (name) 161 | (if (file-name-absolute-p name) 162 | (expand-file-name name) 163 | (let ((load-suffixes '(".rkt"))) 164 | (locate-library name nil)))) 165 | "A function to locate a Racket-based runtime library. 166 | Should be a function returning the full file path of a Racket 167 | module when given a file NAME. When this variable is set to nil 168 | no conversion takes place.") 169 | 170 | (defvar ob-racket-pre-runtime-library-load-hook nil 171 | "Hook run once before loading runtime library. 172 | That is, all the hook functions are run once before first using 173 | any ob-racket Racket runtime library, as known to 174 | `ob-racket-locate-runtime-library-function'.") 175 | 176 | (defvar ob-racket-have-run-pre-runtime-hook nil 177 | "Whether the pre-runtime hook has been run. 178 | Set to a non-nil value once an attempt has been made to run 179 | `ob-racket-pre-runtime-library-load-hook'.") 180 | 181 | (defvar ob-racket-default-code-templates 182 | `( 183 | (program 184 | . ,(lambda (_env params) 185 | (let* ((result-type (cdr (assq :result-type params))) 186 | (is-value-type (eq 'value result-type))) 187 | (if is-value-type 188 | (if (cdr (assq :elisp-printing-form params)) 189 | 'elisp-value-program 190 | 'value-program) 191 | 'output-program)))) 192 | (ob-rkt 193 | . ,(lambda (_env params) 194 | (let ((lang (or (cdr (assq :lang params)) 195 | ob-racket-default-lang))) 196 | (pcase lang 197 | ((or "typed/racket" "typed/racket/base" 198 | "typed/scheme" "typed/scheme/base" "typed-scheme") 199 | "ob-racket-runtime-typed") 200 | (_ "ob-racket-runtime"))))) 201 | (ob-rkt-require ;; cf., `require`, can take multiple specs 202 | . (parens (spaced "file" (locate ob-rkt)))) 203 | (ob-rkt-requires ;; cf., `requires` 204 | . (parens (spaced "require" ob-rkt-require))) 205 | (elisp-value-program 206 | . (prologue 207 | "\n" ob-rkt-requires 208 | "\n" "(" :elisp-printing-form 209 | "\n" define-vars 210 | "\n" :body ")" 211 | "\n" epilogue)) 212 | (value-program 213 | . (prologue 214 | "\n" "(write (let ()" 215 | "\n" define-vars 216 | "\n" :body "))" 217 | "\n" epilogue)) 218 | (output-program 219 | . (prologue 220 | "\n" define-vars 221 | "\n" :body 222 | "\n" epilogue)) 223 | (prologue 224 | . (lang-line "\n" 225 | requires)) 226 | (epilogue . nil) 227 | (lang-line 228 | . ,(lambda (_env params) 229 | (let ((p (assq :lang params))) 230 | (when (or (not p) (cdr p)) 231 | (let ((lang (or (cdr p) ob-racket-default-lang))) 232 | (when lang 233 | `(spaced "#lang" ,lang))))))) 234 | (requires 235 | . ,(lambda (_env params) 236 | (let ((req (assq :require params))) 237 | (when req 238 | `(parens (spaced "require" ,(cdr req))))))) 239 | (define-vars 240 | . ,(lambda (env params) 241 | (let ((vars (org-babel--get-vars params))) 242 | (when vars 243 | (if (ob-racket-get-template 'define-var env params) 244 | `(lines 245 | ,@(mapcar 246 | (lambda (var) 247 | `(parameterize 248 | (:name ,(car var)) 249 | (parameterize 250 | (:value ,(cdr var)) 251 | define-var))) 252 | vars)) 253 | `(parens 254 | (spaced 255 | "define-values" 256 | (parens 257 | (spaced ,@(mapcar 258 | (lambda (var) 259 | `(parameterize 260 | (:name ,(car var)) 261 | var-name)) 262 | vars))) 263 | (parens 264 | (spaced "values" 265 | ,@(mapcar 266 | (lambda (var) 267 | `(parameterize 268 | (:value ,(cdr var)) 269 | var-value)) 270 | vars)))))))))) 271 | (var-name 272 | . ,(lambda (_env params) 273 | (symbol-name (cdr (assq :name params))))) 274 | (var-value 275 | . ,(lambda (_env params) 276 | (let ((in-racket (equal "racket" (cdr (assq :vars-are params)))) 277 | ;; In this case we assume that `val' is a non-template, 278 | ;; and something we may immediately coerce into a string. 279 | (val (cdr (assq :value params)))) 280 | (if (and in-racket (stringp val)) 281 | ;; Already a Racket expression. 282 | val 283 | ;; May not format as Racket, but most 284 | ;; other Babel language definitions 285 | ;; also do not seem to do much else. 286 | (format "%S" val))))) 287 | (with-define . (parens (spaced "define" var-name var-value))) 288 | ) 289 | "Default code templates. 290 | A list of the form ((SYMBOL . TEMPLATE) ...). See 291 | `ob-racket-expand-template' for details on how templates are 292 | expanded.") 293 | 294 | (defvar ob-racket-default-command-templates 295 | `( 296 | (cmdline . nil) 297 | (command . ,(lambda (_env _params) 298 | ob-racket-default-command)) 299 | (racket . "racket") 300 | ) 301 | "Default command templates. 302 | A list of the form ((SYMBOL . TEMPLATE) ...). See 303 | `ob-racket-expand-template' for details on how templates are 304 | expanded.") 305 | 306 | (defun ob-racket-get-template (name env params) 307 | "Get the definition of template NAME. 308 | The name must be given as the symbol, not the keyword. If it has 309 | no template definition, return nil. Both the environment ENV and 310 | the header PARAMS are checked." 311 | (or (assq name env) 312 | (assq (intern (concat ":" (symbol-name name))) params))) 313 | 314 | (defun ob-racket-locate-runtime-library (name) 315 | "Locate a Racket-based runtime library. 316 | Do that in terms of `ob-racket-locate-runtime-library-function' 317 | and a file NAME. Return nil if not found, and otherwise a 318 | complete path." 319 | (when ob-racket-locate-runtime-library-function 320 | (condition-case nil 321 | (funcall ob-racket-locate-runtime-library-function name) 322 | ((wrong-number-of-arguments) ;; for backward compatibility 323 | (funcall ob-racket-locate-runtime-library-function))))) 324 | 325 | (defun ob-racket-expand-sexp (template env params) 326 | "Expand an S-expression, and return a string. 327 | Like `ob-racket-expand-template', but regards TEMPLATE as an 328 | S-expression representing a Racket program fragment. The symbol 329 | table ENV is not referenced directly. As with templates, it is 330 | possible to include Emacs Lisp keywords naming one of the PARAMS. 331 | For now, do nothing special to pretty print symbols and literals 332 | in a Racket reader compatible format. Datums not acceptable to 333 | the Emacs Lisp reader can be escaped as (as-is STRING), causing 334 | the STRING to be included as is." 335 | (pcase template 336 | (`(as-is ,(and (pred stringp) str)) 337 | str) 338 | ((pred listp) 339 | (concat "(" 340 | (mapconcat 341 | (lambda (template) 342 | (ob-racket-expand-sexp template env params)) 343 | template " ") 344 | ")")) 345 | ((pred stringp) 346 | (format "%S" template)) 347 | ((pred keywordp) 348 | (ob-racket-expand-template template env params)) 349 | (_ 350 | (format "%s" template)))) 351 | 352 | (defun ob-racket-expand-template (template env params) 353 | "Expand a TEMPLATE, and return a string. 354 | The TEMPLATE argument may be a string, a symbol naming a template 355 | in environment ENV, a keyword naming one of the PARAMS, a list of 356 | such elements, or a special form named by its first symbol. As a 357 | special case, if the keyword variant of a symbol names one of the 358 | PARAMS, then that parameter is used instead of anything in ENV." 359 | (pcase template 360 | (`nil "") ;; equivalent to (concat) 361 | ((pred stringp) template) 362 | ((pred keywordp) 363 | (let ((p (assq template params))) 364 | (unless p 365 | (error "No assignment for parameter %s in %S" template params)) 366 | (ob-racket-expand-template (cdr p) env params))) 367 | ((pred symbolp) 368 | (let ((param-name (intern (concat ":" (symbol-name template))))) 369 | (if (assq param-name params) 370 | (ob-racket-expand-template param-name env params) 371 | (let ((p (assq template env))) 372 | (unless p 373 | (error "No definition for template %s in %S" template env)) 374 | (let* ((template (cdr p)) 375 | (template (if (functionp template) 376 | (funcall template env params) 377 | template))) 378 | (ob-racket-expand-template template env params)))))) 379 | (`(concat . ,rest) ;; special form 380 | (mapconcat 381 | (lambda (template) 382 | (ob-racket-expand-template template env params)) 383 | rest "")) 384 | (`(lines . ,rest) ;; special form 385 | (mapconcat 386 | (lambda (template) 387 | (ob-racket-expand-template template env params)) 388 | rest "\n")) 389 | (`(spaced . ,rest) ;; special form 390 | (mapconcat 391 | (lambda (template) 392 | (ob-racket-expand-template template env params)) 393 | rest " ")) 394 | (`(join ,sep . ,rest) ;; special form 395 | (mapconcat 396 | (lambda (template) 397 | (ob-racket-expand-template template env params)) 398 | rest 399 | (ob-racket-expand-template sep env params))) 400 | (`(parens . ,rest) ;; special form 401 | (ob-racket-expand-template `(concat "(" ,@rest ")") env params)) 402 | (`(sexp . ,rest) ;; special form 403 | (ob-racket-expand-sexp rest env params)) 404 | (`(format ;; special form 405 | ,(and (pred stringp) fmt) 406 | . ,(and (pred (lambda (xs) 407 | (and (listp xs) (cl-every #'keywordp xs)))) 408 | args)) 409 | (apply #'format fmt (mapcar 410 | (lambda (x) 411 | (let ((p (assq x params))) 412 | (unless p 413 | (error "No assignment for parameter %s in %S" 414 | x params)) 415 | (cdr p))) 416 | args))) 417 | (`(file ,name) ;; special form 418 | (org-babel-process-file-name 419 | (ob-racket-expand-template name env params))) 420 | (`(locate ,name-exp) ;; special form 421 | (let* ((name (ob-racket-expand-template name-exp env params)) 422 | (file (and (stringp name) 423 | (ob-racket-locate-runtime-library name)))) 424 | (if file 425 | (format "%S" file) 426 | (error "Could not locate runtime library %S" name)))) 427 | (`(parameterize ;; special form 428 | (,(and (pred keywordp) x) ,v) 429 | . ,rest) 430 | (ob-racket-expand-template rest env (cons (cons x v) params))) 431 | ((pred listp) ;; implicit default special form 432 | (ob-racket-expand-template (cons 'concat template) env params)) 433 | (_ 434 | (error "Unsupported template form: %S" template)))) 435 | 436 | (defun ob-racket-dump-if (mode data &optional as-text) 437 | "If in MODE, dump DATA. 438 | If mode is the one specified by `ob-racket-debug', then dump 439 | data, optionally AS-TEXT, and do a non-local return. Otherwise do 440 | nothing." 441 | (when (eq mode ob-racket-debug) 442 | (error (concat "DEBUG: %s:\n" (if as-text "%s" "%S")) 443 | mode data))) 444 | 445 | (defun org-babel-expand-body:racket (body params) 446 | "Expand BODY according to PARAMS, and return the result." 447 | (when ob-racket-preprocessor-function 448 | (pcase (funcall ob-racket-preprocessor-function body params) 449 | (`(,b ,p) (setq body b params p)))) 450 | (let ((templates (append ob-racket-custom-code-templates 451 | ob-racket-default-code-templates)) 452 | (params (cons (cons :body body) params))) 453 | (ob-racket-expand-template 'program templates params))) 454 | 455 | (defun org-babel-execute:racket (body params) 456 | "Evaluate a `racket` code block. 457 | Evaluate the block BODY according to PARAMS. Some non-standard, 458 | subject-to-change parameters are supported for more control over 459 | the way that the code block is evaluated, and how its `:var's and 460 | results are processed." 461 | (ob-racket-dump-if 'dump-params params) 462 | (let* ((result-type (cdr (assq :result-type params))) 463 | (is-value-type (eq 'value result-type)) 464 | (result-params 465 | (let ((p (cdr (assq :result-params params)))) 466 | (if (or (member "file" p) 467 | (member "list" p) 468 | (member "scalar" p) 469 | (member "table" p) 470 | (member "vector" p) 471 | (member "verbatim" p)) 472 | p (cons "scalar" p)))) 473 | (elisp-printing-form 474 | (when (and is-value-type 475 | ob-racket-locate-runtime-library-function) 476 | (cond 477 | ((equal "elisp" (cdr (assq :results-as params))) 478 | "ob-racket-begin-print-elisp") 479 | ((or (member "list" result-params) 480 | (member "table" result-params) 481 | (member "vector" result-params)) 482 | "ob-racket-begin-print-table")))) 483 | (params 484 | `((:elisp-printing-form . ,elisp-printing-form) 485 | (:result-params . ,result-params) 486 | ,@params)) 487 | (full-body (org-babel-expand-body:racket body params))) 488 | (when elisp-printing-form 489 | (unless ob-racket-have-run-pre-runtime-hook 490 | (setq ob-racket-have-run-pre-runtime-hook t) 491 | (run-hooks 'ob-racket-pre-runtime-library-load-hook))) 492 | (ob-racket-dump-if 'dump-code full-body t) 493 | (let* ((in-file 494 | (or (cdr (assq :in-file params)) 495 | (org-babel-temp-file "org-babel-" ".rkt"))) 496 | (params `((:in-file . ,in-file) 497 | ,@params)) 498 | (templates 499 | (append ob-racket-custom-command-templates 500 | ob-racket-default-command-templates)) 501 | (command 502 | (ob-racket-expand-template 'command templates params))) 503 | (ob-racket-dump-if 'dump-command command t) 504 | (with-temp-file in-file 505 | (insert full-body)) 506 | (prog1 507 | (progn 508 | (message command) 509 | (let ((result (shell-command-to-string command))) 510 | (ob-racket-dump-if 'dump-result result t) 511 | (org-babel-result-cond result-params 512 | result 513 | (org-babel-reassemble-table 514 | (if elisp-printing-form 515 | (read result) 516 | (org-babel-read result)) 517 | (org-babel-pick-name (cdr (assq :colname-names params)) 518 | (cdr (assq :colnames params))) 519 | (org-babel-pick-name (cdr (assq :rowname-names params)) 520 | (cdr (assq :rownames params))))))) 521 | (unless (eq ob-racket-debug 'keep-file) 522 | (delete-file in-file)))))) 523 | 524 | (defun org-babel-prep-session:racket (_session _params) 525 | "Error out due to lack of support. 526 | SESSION and PARAMS are ignored." 527 | (error "Sessions are not supported for `racket`")) 528 | 529 | (defun ob-racket-locatable-runtime-libraries () 530 | "Return a list of locatable runtime libraries. 531 | The result includes the path names of those 532 | `ob-racket-runtime-libraries' that can be located by 533 | `ob-racket-locate-runtime-library-function' or that are already 534 | absolute paths. Duplicates are not included in the result." 535 | (when ob-racket-locate-runtime-library-function 536 | (delete-dups 537 | (delete 538 | nil 539 | (mapcar 540 | #'ob-racket-locate-runtime-library 541 | ob-racket-runtime-libraries))))) 542 | 543 | (defun ob-racket-raco-make-runtime-library () 544 | "Run raco make on any ob-racket Racket runtime libraries. 545 | To do that run Racket as defined by ob-racket command templates. 546 | Ignore any `ob-racket-runtime-libraries' that are not known to 547 | `ob-racket-locate-runtime-library-function'." 548 | (let ((runtime-libraries 549 | (ob-racket-locatable-runtime-libraries))) 550 | (when runtime-libraries 551 | (let* ((in-file 552 | (org-babel-temp-file "org-babel-" ".rkt")) 553 | (command 554 | (ob-racket-expand-template 555 | 'command 556 | (append ob-racket-custom-command-templates 557 | ob-racket-default-command-templates) 558 | `((:in-file . ,in-file))))) 559 | (with-temp-file in-file 560 | (insert 561 | (ob-racket-expand-template 562 | `(lines 563 | "#lang racket/base" 564 | "(require raco/all-tools)" 565 | "(define raco-make-spec (hash-ref (all-tools) \"make\"))" 566 | (parens 567 | (spaced 568 | "parameterize" 569 | (parens 570 | (parens 571 | (spaced 572 | "current-command-line-arguments" 573 | (parens (spaced "vector" runtime-libraries))))) 574 | "(dynamic-require (cadr raco-make-spec) #f)"))) 575 | `((runtime-libraries 576 | . ,(lambda (_env _params) 577 | (mapconcat 578 | (lambda (x) (format "%S" x)) 579 | runtime-libraries " ")))) 580 | nil))) 581 | (prog1 582 | (with-temp-buffer 583 | (shell-command command nil (current-buffer)) 584 | (let ((error-message (buffer-string))) 585 | (unless (= 0 (length error-message)) 586 | (message "Failed to raco make %S: %s" 587 | runtime-libraries error-message)))) 588 | (delete-file in-file)))))) 589 | 590 | (provide 'ob-racket) 591 | 592 | ;;; ob-racket.el ends here 593 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------