Skip to content

Instantly share code, notes, and snippets.

@dangom
Forked from zot/ob-skewer.el
Created May 18, 2016 14:25
Show Gist options
  • Save dangom/f368f0f24e8fbd109e15b6035bf0d164 to your computer and use it in GitHub Desktop.
Save dangom/f368f0f24e8fbd109e15b6035bf0d164 to your computer and use it in GitHub Desktop.
Small hack to let orgmode babel JS blocks use skewer if it's currently connected
;; 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 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