├── .gitignore ├── README.org └── ob-swift.el /.gitignore: -------------------------------------------------------------------------------- 1 | *.elc 2 | -------------------------------------------------------------------------------- /README.org: -------------------------------------------------------------------------------- 1 | * ob-swift 2 | 3 | ** supported header arguments 4 | 5 | - session 6 | 7 | -------------------------------------------------------------------------------- /ob-swift.el: -------------------------------------------------------------------------------- 1 | ;;; ob-swift.el --- org-babel functions for swift evaluation 2 | 3 | ;; Copyright (C) 2015 Feng Zhou 4 | 5 | ;; Author: Feng Zhou 6 | ;; URL: http://github.com/zweifisch/ob-swift 7 | ;; Keywords: org babel swift 8 | ;; Version: 0.0.1 9 | ;; Created: 4th Dec 2015 10 | ;; Package-Requires: ((org "8")) 11 | 12 | ;; This program is free software; you can redistribute it and/or modify 13 | ;; it under the terms of the GNU General Public License as published by 14 | ;; the Free Software Foundation, either version 3 of the License, or 15 | ;; (at your option) any later version. 16 | 17 | ;; This program is distributed in the hope that it will be useful, 18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 | ;; GNU General Public License for more details. 21 | 22 | ;; You should have received a copy of the GNU General Public License 23 | ;; along with this program. If not, see . 24 | 25 | ;;; Commentary: 26 | ;; 27 | ;; org-babel functions for swift evaluation 28 | ;; 29 | 30 | ;;; Code: 31 | (require 'ob) 32 | 33 | (defvar ob-swift-process-output "") 34 | 35 | (defvar ob-swift-eoe "ob-swift-eoe") 36 | 37 | (defun org-babel-execute:swift (body params) 38 | (let ((session (cdr (assoc :session params)))) 39 | (if (string= "none" session) 40 | (ob-swift--eval body) 41 | (ob-swift--ensure-session session) 42 | (ob-swift--eval-in-repl session body)))) 43 | 44 | (defun ob-swift--eval (body) 45 | (with-temp-buffer 46 | (insert body) 47 | (shell-command-on-region (point-min) (point-max) "swift -" nil 't) 48 | (buffer-string))) 49 | 50 | (defun ob-swift--ensure-session (session) 51 | (let ((name (format "*ob-swift-%s*" session))) 52 | (unless (and (get-process name) 53 | (process-live-p (get-process name))) 54 | (let ((process (with-current-buffer (get-buffer-create name) 55 | (start-process name name "swift")))) 56 | (set-process-filter process 'ob-swift--process-filter) 57 | (ob-swift--wait "Welcome to Swift"))))) 58 | 59 | (defun ob-swift--process-filter (process output) 60 | (setq ob-swift-process-output (concat ob-swift-process-output output))) 61 | 62 | (defun ob-swift--wait (pattern) 63 | (while (not (string-match-p pattern ob-swift-process-output)) 64 | (sit-for 0.5))) 65 | 66 | (defun ob-swift--eval-in-repl (session body) 67 | (let ((name (format "*ob-swift-%s*" session))) 68 | (setq ob-swift-process-output "") 69 | (process-send-string name (format "%s\n\"%s\"\n" body ob-swift-eoe)) 70 | (accept-process-output (get-process name) nil nil 1) 71 | (ob-swift--wait ob-swift-eoe) 72 | (replace-regexp-in-string 73 | (format "^\\$R[0-9]+: String = \"%s\"\n" ob-swift-eoe) "" 74 | (replace-regexp-in-string 75 | "^\\([0-9]+\\. \\)+\\([0-9]+> \\)*" "" 76 | (replace-regexp-in-string 77 | "^\\([0-9]+> \\)+" "" 78 | ob-swift-process-output))))) 79 | 80 | (provide 'ob-swift) 81 | ;;; ob-swift.el ends here 82 | --------------------------------------------------------------------------------