Org-Mode Part 2 – execute code

29/12/2020
Chưa phân loại
When you want to learn and teach code, it can be useful to write the code in a document. This is also useful for the planning stage of a project. You may think that you just put comments in the code but that could be troublesome since comments need to show how this code works. In the document you can put project ideas, planning and abstract ideas that do not belong in the code commentary. The document can also contain results of the execution, which is powerful for drawing conclusions but has no place in final code.

Code in Emacs

Many developers use Emacs to develop software, it is a powerful code editor. In fact, many consider it an IDE, when correctly configured. But documents are usually separate from the code. If you want to demonstrate code or plan a new project you can use code snippets. To show how you believe it needs to work, you can write such a document. One powerful way to do this is to create an Org document. To add code to your document, you need to add a code block. A code block is a standard set of code which you can see below.

#+begin_src javascript
const {app, BrowserWindow} = require(‘electron’)
const path = require(‘path’)
#+end_src

The result can go to the document.

This example does not create an output, the simplest way to get output is to use shell. When you want to run a command and show the results, you need to add a header parameter.

#+begin_src shell :exports both
uname -a
#+end_src
uname -a

Linux mats-Ubuntu 5.0.0-20-generic #21-Ubuntu SMP Mon Jun 24 09:32:09
UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

The result is added by Emacs in block below the executing code. If you want more advanced results or you want to combine blocks , you will need to name your source code block. Add the optional #+NAME: tag. This tag works for tables as well as code.

Table 1.3.1:

x y z
0 1 4
1 3 6
2 4 8

With the calc module, you can put data in and have the results displayed in the document.

#+begin_src calc :var x=5 :var y=2 :exports both
2+a*x**y
#+end_src

First, the code is displayed, in the form you specify. Directly below, the results are shown.

2+a*x**y
25 a + 2

This is a very simple example, with support for LaTeX you can get any formula you want with the result in a very well formatted document. A

begin{equation}
x=sqrt{b}
end{equation}

If you produce plots with your code, you can display that inside the document. Below, you can see a very simple code that uses gnuplot to plot the result of x2.

f(x) = x**2
plot f(x)

For this to work, you need to have gnuplot installed on your system. These features are all called from the system and presented in Emacs. The same goes for all programming languages that Emacs supports.

Not all code is supported, out of the box

For the sake of efficiency, not all supported languages are active in a vanilla install of Emacs. To make sure you have it active, you can add code to your emacs configuration, init.el is the standard file.

‘(org-babel-load-languages
(quote
((python . t)
(emacs-lisp . t)
(shell . t)
(js . t)
(sqlite . t)
(calc . t))))

The above code enables six languages by setting them to ‘t’. the ‘t’ is the common way in Lisp to say true. You can also choose to set this value with the ‘M-x customize-variable’ method. When you do this, you need to type in the variable name, or group. In this case the variable is ‘org-babel-load-languages’. When you come to the page, you can see what is supported and activate the ones you want.

Adding a language

If your language is not in the list, you can look for language on https://orgmode.org/worg/org-contrib/babel/languages.Currently.html The list of supported languages is already long and is growing by the day. Note here, support for a language is only required for the evaluation. Viewing, exporting and tangling is supported without it. On the mentioned web page, you also have contributed languages that require a bit more work to install. If you still can’t see your language, there is a template for adding it yourself. This requires some lisp programming skills but if you look at the other languages you should be able to add it even with minor experience.

Conclusion

This article has only scratched the surface of what you can do with org-mode. More features are available and calling Emacs a simple editor is doing yourself a disservice.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Golang Logrus Package

In this lesson on Logrus package in Golang, we will study various examples on how effective Logging can be done in Go and...
28/12/2020

Setup Passwordless Login to Servers via SSH

As a Linux system administrator, you will be configuring and tweaking a lot of Linux servers frequently. So, you have to...
29/12/2020

Hướng dẫn cài đặt CPanel/WHM mới nhất centos 7/8

1. To check which are the interfaces managed by NetworkManager nmcli device status This displays a table that lists all...
30/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

mua Proxy riêng ở đâu, và nó đem lại lợi ích gì cho người sử dụng
22/11/2022

Hướng dẫn sử dụng Proxy Helper Fakeip khi thuê proxy
21/11/2022

PROXY NUÔI TÀI KHOẢN FACEBOOK – KINH NGHIỆM FAKE IP – THUÊ PROXY GIÁ RẺ
14/11/2022

Mua Proxy Nuôi Zalo Giá Rẻ Tại Onet.com.vn
14/11/2022

BẢNG GIÁ MUA PROXY VIỆT NAM và PROXY US Onet.com.vn
14/11/2022