Last active
August 10, 2020 06:39
-
-
Save zot/0dd34b50acf81416dd88 to your computer and use it in GitHub Desktop.
Small hack to let orgmode babel JS blocks use skewer if it's currently connected
This file contains 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
;; modify ob-js to redirect to skewer if it is currently connected | |
;; this code can go in an emacs settings file | |
(require 'ob-js) | |
(require 'cl) | |
(advice-add 'org-babel-execute:js :around 'bill/org-babel-execute:skewer) | |
;;(advice-remove 'org-babel-execute:js #'bill/org-babel-execute:skewer) | |
(defun bill/org-babel-execute:skewer (oldFunc body params) | |
(if (skewer-ping) | |
(lexical-let* ((result-type (cdr (assoc :result-type params))) | |
(full-body (org-babel-expand-body:generic | |
body params (org-babel-variable-assignments:js params))) | |
(pos (point))) | |
(skewer-eval full-body (lambda (result) | |
(let ((value )) | |
(goto-char pos) | |
(org-babel-insert-result | |
(if (skewer-success-p result) | |
(cdr (assoc 'value result)) | |
(let ((error (cdr (assoc 'error result)))) | |
(or (cdr (assoc 'message error)) "")))))) :verbose nil) | |
nil) | |
(apply oldFunc body params nil))) | |
This file contains 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
// this code goes in skewer.js, to replace the current version of skewer.fn.eval. | |
/** | |
* Handles an code evaluation request from Emacs. | |
* @param request The request object sent by Emacs | |
* @returns The result object to be returned to Emacs | |
*/ | |
skewer.fn.eval = function(request) { | |
var result = { | |
strict: request.strict | |
}; | |
var start = skewer.now(); | |
var oc = console.log; | |
var strResult = ''; | |
try { | |
if (!request.verbose) { | |
console.log = function() { | |
for (var i = 0; i < arguments.length; i++) { | |
if (i > 0) { | |
strResult += ' '; | |
} | |
strResult += String(arguments[i]); | |
} | |
strResult += '\n'; | |
}; | |
} | |
var prefix = request.strict ? '"use strict";\n' : ""; | |
var value = skewer.globalEval(prefix + request.eval); | |
if (!strResult) { | |
strResult = skewer.safeStringify(value, request.verbose) + '\n'; | |
} | |
result.value = strResult; | |
} catch (error) { | |
result = skewer.errorResult(error, result, request); | |
} finally { | |
console.log = oc; | |
} | |
result.time = (skewer.now() - start) / 1000; | |
return result; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a rough cut -- it can be improved but, anyway, it works for what I need right now :).
It redirects orgmode js src blocks to use skewer when it is connected. Also, I modified skewer.fn.eval() in skewer.js so that when a request is not verbose, it will to send console.log() to the output string and if there is output, it does not add the return value.