This is a small org-babel plugin that utilises dotnet-script to execute c# block
The idea for this came about after reading Scott Hanselman’s blog post about dotnet-script. I have been using a tool called LinqPad as part of my workflow to test code out but in my effort to never leave Emacs I wanted a LinqPad ish experiance in Org. This way I can use the scripting extension of Dotnet to create little blocks of code to execute.
- @samwdp (Author)
- @bernhardrichter & @filip_woj (dotnet-script)
;;; ob-cshrp.el --- org-babel functions for csharp evaluation
;;; Commentary:
;;
;; org-babel functions for rust evaluation
;;
;;; Code:
(require 'ob)
(require 'csharp-mode)
(add-to-list 'org-babel-tangle-lang-exts '("csharp" . "cs"))
(defvar org-babel-default-header-args:csharp '())
(defun ob-csharp--build-script-run-command (path)
"Create run command according to the PATH."
(format "dotnet script %s" path))
(defun org-babel-execute:csharp (body params)
(let* ((processed-params (org-babel-process-params params))
(src-temp (org-babel-temp-file "csharp-src-")))
(with-temp-file src-temp (insert body))
(let ((results (org-babel-eval (ob-csharp--build-script-run-command src-temp) "")))
(org-babel-reassemble-table
(org-babel-result-cond (cdr (assoc :result-params params))
(org-babel-read results)
(let ((tmp-file (org-babel-temp-file "c-")))
(with-temp-file tmp-file (insert results))
(org-babel-import-elisp-from-file tmp-file)))
(org-babel-pick-name
(cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
(org-babel-pick-name
(cdr (assoc :rowname-names params)) (cdr (assoc :rownames params)))))))
(provide 'ob-csharp)
;;; ob-csharp.el ends here
- dotnet core/5 installed (or any future versions that utilise the dotnet command)
- Install dotnet-script
dotnet tool install -g dotnet-script
- Make sure your .dotnet/tools directory is in your PATH variable (restart if needed)
- packages.el
(package! ob-csharp :recipe (:host github :repo "samwdp/ob-csharp"))
- config.el
(org-babel-do-load-languages 'org-babel-load-languages '((csharp . t)))
Console.WriteLine("Hello World");
Hello World
public class Calculator
{
public static int Add(int a, int b) =>
a + b;
}
Console.WriteLine(Calculator.Add(1, 2));
3
public class ExampleController
{
public void Render(ExampleModel obj)
{
Console.WriteLine(obj.ToString());
}
}
public class ExampleModel
{
public string Name {get; set;}
public override string ToString()
{
return Name;
}
}
var model = new ExampleModel()
{
Name = "Hello World"
};
var controller = new ExampleController();
controller.Render(model);
Hello World
for (char c = 'a'; c < 'd'; c++)
System.Console.Write("{0} ",c);
System.Console.WriteLine();
for (int i = 0; i < 3; i++)
System.Console.Write("{0} ",i);
a | b | c |
0 | 1 | 2 |