|
|
@@ -43,6 +43,21 @@ automatically generated wrapper for `litorgy-script-execute'.")
|
|
|
(litorgy-script-execute ,cmd body params))))
|
|
|
cmds))
|
|
|
|
|
|
+(defvar litorgy-script-ruby-wrapper-method
|
|
|
+ "
|
|
|
+def main
|
|
|
+%s
|
|
|
+end
|
|
|
+puts main
|
|
|
+")
|
|
|
+
|
|
|
+(defvar litorgy-script-python-wrapper-method
|
|
|
+ "
|
|
|
+def main():
|
|
|
+%s
|
|
|
+
|
|
|
+print main()")
|
|
|
+
|
|
|
(defcustom litorgy-script-interpreters '("ruby" "python")
|
|
|
"List of interpreters of scripting languages which can be
|
|
|
executed through litorgy."
|
|
|
@@ -55,50 +70,48 @@ executed through litorgy."
|
|
|
(let ((vars (litorgy-ref-variables params)))
|
|
|
(save-window-excursion
|
|
|
(with-temp-buffer
|
|
|
- (when (string= "ruby" cmd) (insert "def main\n"))
|
|
|
- ;; define any variables
|
|
|
- (mapcar
|
|
|
- (lambda (pair)
|
|
|
- (insert (format "%s=%s\n"
|
|
|
- (car pair)
|
|
|
- (litorgy-script-table-to-ruby/python (cdr pair)))))
|
|
|
- vars)
|
|
|
- (case (intern cmd)
|
|
|
- ('ruby
|
|
|
- (insert body)
|
|
|
- (insert "\nend\n\nputs main.inspect\n"))
|
|
|
- ('python
|
|
|
- (insert "def main():\n")
|
|
|
+ (insert
|
|
|
+ (format
|
|
|
+ (case (intern cmd)
|
|
|
+ ('ruby litorgy-script-ruby-wrapper-method)
|
|
|
+ ('python litorgy-script-python-wrapper-method))
|
|
|
+ (concat
|
|
|
+ (mapconcat ;; define any variables
|
|
|
+ (lambda (pair)
|
|
|
+ (format "\t%s=%s"
|
|
|
+ (car pair)
|
|
|
+ (litorgy-script-var-to-ruby/python (cdr pair))))
|
|
|
+ vars "\n")
|
|
|
+ "\n"
|
|
|
(let ((body-lines (split-string body "[\n\r]+" t)))
|
|
|
- (mapc
|
|
|
+ (mapconcat
|
|
|
(lambda (line)
|
|
|
- (insert (format "\t%s\n" line)))
|
|
|
- (butlast body-lines))
|
|
|
- (insert (format "\treturn %s\n" (car (last body-lines)))))
|
|
|
- (insert "\nprint main()\n")))
|
|
|
+ (format "\t%s\n" line))
|
|
|
+ (butlast body-lines) "\n")
|
|
|
+ (format "\treturn %s\n" (car (last body-lines)))))))
|
|
|
(shell-command-on-region (point-min) (point-max) cmd nil 'replace)
|
|
|
(litorgy-script-table-or-results (buffer-string))))))
|
|
|
|
|
|
-(defun litorgy-script-table-to-ruby/python (table)
|
|
|
- "Convert an elisp table (nested lists) into a string of ruby
|
|
|
-source code specifying a table (nested arrays)."
|
|
|
- (if (listp table)
|
|
|
- (concat "[" (mapconcat #'litorgy-script-table-to-ruby/python table ", ") "]")
|
|
|
- (format "%S" table)))
|
|
|
+(defun litorgy-script-var-to-ruby/python (var)
|
|
|
+ "Convert an elisp var into a string of ruby or python source
|
|
|
+code specifying a var of the same value."
|
|
|
+ (if (listp var)
|
|
|
+ (concat "[" (mapconcat #'litorgy-script-var-to-ruby/python var ", ") "]")
|
|
|
+ (format "%S" var)))
|
|
|
|
|
|
(defun litorgy-script-table-or-results (results)
|
|
|
"If the results look like a table, then convert them into an
|
|
|
Emacs-lisp table, otherwise return the results as a string."
|
|
|
- (when (string-match "^\\[.+\\]$" results)
|
|
|
- (setq results
|
|
|
- ;; somewhat hacky, but thanks to similarities between
|
|
|
- ;; languages it seems to work
|
|
|
- (read (replace-regexp-in-string
|
|
|
- "\\[" "(" (replace-regexp-in-string
|
|
|
- "\\]" ")" (replace-regexp-in-string
|
|
|
- ", " " " (replace-regexp-in-string
|
|
|
- "'" "\"" results)))))))
|
|
|
- results)
|
|
|
+ (litorgy-read
|
|
|
+ (if (string-match "^\\[.+\\]$" results)
|
|
|
+ ;; somewhat hacky, but thanks to similarities between languages
|
|
|
+ ;; it seems to work
|
|
|
+ (replace-regexp-in-string
|
|
|
+ "\\[" "(" (replace-regexp-in-string
|
|
|
+ "\\]" ")" (replace-regexp-in-string
|
|
|
+ ", " " " (replace-regexp-in-string
|
|
|
+ "'" "\"" results))))
|
|
|
+ results)))
|
|
|
|
|
|
(provide 'litorgy-script)
|
|
|
;;; litorgy-script.el ends here
|