Created
March 26, 2009 03:25
-
-
Save typester/85855 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(require 'url) | |
(require 'url-util) | |
(require 'json) | |
(defun lleval-detect-lang () | |
"detect target lang parameter from current major mode" | |
(let ((mode-list '(("emacs-lisp-mode" . "el") | |
("lisp-interaction-mode" . "el") | |
("hugs-mode" . "hs") | |
("io-mode" . "io") | |
("javascript-mode" . "js") | |
("lisp-mode" . "lsp") | |
("lua-mode" . "lua") | |
("ocaml-mode" . "ml") | |
("php-mode" . "php") | |
("perl-mode" . "pl") | |
("cperl-mode" . "pl") | |
("python-mode" . "py") | |
("ruby-mode" . "rb") | |
("scheme-mode" . "scm") | |
("tcl-mode" . "tcl")))) | |
(cdr (assoc (format "%s" major-mode) mode-list)))) | |
(defun lleval-buffer () | |
"Run lleval on current buffer" | |
(interactive) | |
(lleval (buffer-string) (lleval-detect-lang))) | |
(defun lleval-region (begin end) | |
"Run lleval on current region" | |
(interactive "r") | |
(save-excursion | |
(lleval (buffer-substring begin end) (lleval-detect-lang)))) | |
(defun lleval (code &optional lang) | |
"Run lleval and display its result" | |
(interactive | |
(let ((lang (or (lleval-detect-lang) "auto"))) | |
(list (read-string (format "Run lleval (%s): " lang)) lang))) | |
(let ((lang (or lang "auto")) | |
(url (format "http://api.dan.co.jp/lleval.cgi?l=%s&c=cb&s=%s" | |
lang (url-hexify-string code))) | |
(cb (lambda (status) | |
(url-mark-buffer-as-dead (current-buffer)) | |
(if status | |
(let ((err (plist-get status :error))) | |
(if err (message (format "%s" err)) | |
(message (buffer-string)))) | |
(let ((raw (buffer-string))) | |
(with-temp-buffer | |
(insert (decode-coding-string raw 'utf-8)) | |
(beginning-of-buffer) | |
(if (re-search-forward "\r?\n\r?\ncb(\\(.*\\));$" nil t) | |
(lleval-show-result (json-read-from-string (match-string 1))) | |
(message (buffer-string))))))))) | |
(url-retrieve url cb))) | |
(defun lleval-show-result (json) | |
"parse result json" | |
(let* ((status (cdr (assoc 'status json))) | |
(stdout (cdr (assoc 'stdout json))) | |
(stderr (cdr (assoc 'stderr json)))) | |
(with-current-buffer (get-buffer-create "*lleval*") | |
(erase-buffer) | |
(insert (concat | |
"------- stdout -------\n\n" | |
stdout "\n\n" | |
"------- stderr -------\n\n" | |
stderr "\n\n")) | |
(switch-to-buffer-other-window "*lleval*")))) | |
(provide 'lleval) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment