功能点

switches

  • 方法:

    • -n : 显示行号

代码预览

功能解释:

  • 最终 执行 或者 导出 的代码,并不一定是看到的代码的样子
  • 如果要预览后台真正使用的代码,就需要使用整个功能了

调用方法:

  • M-x org-babel-expand-src-block
  • 按键:

    • C-c C-v v
    • C-c C-v C-v

source block

执行代码块

https://emacs.stackexchange.com/questions/17673/no-org-babel-execute-function-for-c-and-no-org-babel-execute-function-for-c

  • 启用方法

    1
    2
    3
    
    ;; Here C --> for C, C++
    (org-babel-do-load-languages 'org-babel-load-languages
                                 '((python . t) (C . t) ))
  • 执行快捷键

    • Ctr-C Ctr-C

rich format

代码行数标记

  • -n : 设定起始行数

    1
    2
    
    ;; This exports with line number 20. 当前行是第 20 行
    (message "This is line 21")
  • +n : 设定为上面的代码 -n 20 加上设定的增加值

    1
    2
    
    ;; This is listed as line 31. (1) -n 20 (2) +n 10 (3) 所以 20 + 10 = 30, 当前行是30 + 1 = 31 行
    (message "This is line 32")

代码内 anchor 引用和链接

  • 设定 anchor

    1
    2
    
    (save-excursion                 ;; (ref:sc)
       (goto-char (point-min))      ;; (ref:jump)
    • 这里 (ref:sc) 和 (ref:jump) 就是 代码块 内部的 anchor
  • 链接中使用 代码快 anchor

    • 例句: (sc) 是可以跳转到代码块内部链接, link_label
    • 格式:

      • 不加 label: [[(anchor_name)]]
      • 加上 label: [[(anchor_name)][label_text]]

代码块 anchor 取消显示 -r

方法: 在 +begin_src lang 后面加上 -r 选项

python 支持

  • :var 语法对表格支持

    • 内部转换成二维 2d 列表
  • :var 语法对 Plain List 支持

    • 单层列表能够识别
    • 多层嵌套列表,报错

按键前缀

  • 快捷键 前缀 C-c C-v
  • 执行 C-c C-v C-e

支持的语言 support babel languages List

导出

参考:Exporting Code Blocks (The Org Manual)

通过 :exports header 控制 取值:

  • code
  • results
  • both
  • none

headers

noweb 代码块引用

参考:

:noweb 引用单个代码块

  • :noweb 取值

    • no

      • (默认值)
      • 不 expand
    • yes
    • tangle

      • 只有在 tangle 时才 expand, 即: evaluating 执行和 exporting 导出时,不 expand
    • eval

      • 只有执行时,才 expand
    • no-export

      • 只有 exporting 时,不 expand
    • strip-export

      • evaluating 执行和 tangle 时 expand
      • export 时,(不 expand),去掉 noweb 语法声明部分

使用方法:

  1. 被引用代码块 #+NAME: given-name 唯一化标识

    1
    
    print("hello")
  2. 引用格式

    1
    
    <<given-name>>
    • 注意: 要设置好 :noweb header 否则可能 expand 失败
  3. 检验生效方法

    • 在使用引用的代码块中 M-x org-babel-expand-src-block 或者 C-c C-v C-v

:noweb-ref 引用多个代码块

使用方法:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#+BEGIN_SRC sh :tangle yes :noweb yes :shebang #!/bin/sh
  <<fullest-disk>>
#+END_SRC
* the mount point of the fullest disk
  :PROPERTIES:
  :header-args: :noweb-ref fullest-disk
  :END:

** query all mounted disks
#+BEGIN_SRC sh
  df \
#+END_SRC

** strip the header row
#+BEGIN_SRC sh
  |sed '1d' \
#+END_SRC

** output mount point of fullest disk
#+BEGIN_SRC sh
  |awk '{if (u < +$5) {u = +$5; m = $6}} END {print m}'
#+END_SRC

语言

参考: Babel: Languages

shell

参考:

source block 代码块支持的 声明方式

  • sh: /bin/sh

    1
    2
    
    #+begin_src sh
    #+end_src
  • shell: 默认 shell

    1
    2
    3
    
    #+begin_src shell
    
    #+end_src
  • bash

    1
    2
    3
    
    #+begin_src bash
    
    #+end_src

特殊 headers

  • :stdin <element-name> : 通过 org #+NAME: 工具传入内容
  • :cmdline arg1 arg2 : 控制出入的命令行参数
  • :shebang [shebang] : 修改关联的 shell

    • eg: :shebang #!/usr/bin/env bash

cmd 支持

参考: microsoft windows - Org-babel invoking cmd.exe - Emacs Stack Exchange

补丁代码:

1
2
3
4
5
(defadvice org-babel-sh-evaluate (around set-shell activate)
  "Add header argument :shcmd that determines the shell to be called."
  (let* ((org-babel-sh-command (or (cdr (assoc :shcmd params)) org-babel-sh-command)))
    ad-do-it
    ))

用法:

  • :shcmd <cmd-comand> : 指定 cmd.exe 命令
  • 例子:

    1
    
    dir

注意

  • shshell 不一样

    • sh 是 /bin/sh
    • shell 是用户的默认 shell, chsh 设定的默认 shell, 例如:bash 或者 zsh 等

c-mode, cpp-mode, D-mode

参考:

举例:

1
  std::cout<<"Hello World!\n";
Hello World!
1
cout<<"hello"<<endl;
hello

headers

:includes 头文件
:cmdline 可执行文件的命令行参数
:flags 编译控制
:main 是否用 main 包裹代码块
  • 可选值

    • "no": 禁用 main 包裹
:defines 宏定义
:namespaces C++ 的 using namespace <name>
:libs 链接时的 library 指定

python

参考:

headers

:results

取值:

  • output: 输出打印到 stdout 中的内容
  • value :

    • 三种变体

      • raw : 即 org-babel 默认的 value 形式
      • pp : 调用 pprint.pformat(%s)
      • file: 适合输出文件的类型,例如图片类型输出
图片处理

例子:

  • 通过 noweb 减少手写代码

注意:这里的 return 不可省略,否则不会输出图片

1
2
3
4
5
6
7
  import matplotlib, numpy
  import matplotlib.pyplot as plt
  fig=plt.figure(figsize=(4,2))
  x=numpy.linspace(-15,15)
  plt.plot(numpy.sin(x)/x)
  fig.tight_layout()
  <<savefig(figname="plot.png", width=10, height=5)>>

jupyter

jupyter-python

FAQ
pandas 输出
1
2
3
import pandas as pd

pd.DataFrame([[1,2, 3], 'abc'])
012
0123
1abc

FAQ

执行确认提醒

参考:

方法:

  • 通过配置变量 org-confirm-babel-evaluate 为 nil 取消执行确认

    • 对于单个 org 文件: buffer-local 设置
    • 对于特定代码块:

      1
      2
      3
      
      (defun my-org-confirm-babel-evaluate (lang body)
        (not (string= lang "python")))  ;don't ask for ditaa
      (setq org-confirm-babel-evaluate #'my-org-confirm-babel-evaluate)

解决文件夹不存在问题

1
2
3
@startuml
A->B: hello
@enduml

I img/pdemo01.svg