Emacs:insert source code block in org-mode

When taking notes, writing blogs and managing knowledge base with org-mode, I need to insert source code blocks quite frequently, so I wrote an Elisp function to help me to do it.

1 An example of org-mode source code block

This is an Elisp source code block (foldable) in org-mode:

#+BEGIN_SRC emacs-lisp
  (message "天地玄黄,鸟生鱼汤")
#+END_SRC

Note : In Emacs, you could put the cursor in between #+BEGIN_SRC and #+END_SRC, then press M-x org-edit-src-code (I have bound C-c s e to this function) to edit the source code with its corresponding editing mode(s).

2 org-insert-src-block

In order to reduce the keystrokes, I wrote this Elisp function:

(defun org-insert-src-block (src-code-type)
  "Insert a `SRC-CODE-TYPE' type source code block in org-mode."
  (interactive
   (let ((src-code-types
          '("emacs-lisp" "python" "C" "sh" "java" "js" "clojure" "C++" "css"
            "calc" "asymptote" "dot" "gnuplot" "ledger" "lilypond" "mscgen"
            "octave" "oz" "plantuml" "R" "sass" "screen" "sql" "awk" "ditaa"
            "haskell" "latex" "lisp" "matlab" "ocaml" "org" "perl" "ruby"
            "scheme" "sqlite")))
     (list (ido-completing-read "Source code type: " src-code-types))))
  (progn
    (newline-and-indent)
    (insert (format "#+BEGIN_SRC %s\n" src-code-type))
    (newline-and-indent)
    (insert "#+END_SRC\n")
    (previous-line 2)
    (org-edit-src-code)))

eval it and press M-x org-insert-src-block , then type in the wanted Emacs major mode for the block, for example, emacs-lisp (press TAB to do auto-completion).

Note Thanks to Xah Lee ‘s help on the ido-completing-read function.

3 Create a keyboard shortcut

I have bound C-c s i to the function above, here is my configuration:

(add-hook 'org-mode-hook '(lambda ()
                            ;; turn on flyspell-mode by default
                            (flyspell-mode 1)
                            ;; C-TAB for expanding
                            (local-set-key (kbd "C-<tab>")
                                           'yas/expand-from-trigger-key)
                            ;; keybinding for editing source code blocks
                            (local-set-key (kbd "C-c s e")
                                           'org-edit-src-code)
                            ;; keybinding for inserting code blocks
                            (local-set-key (kbd "C-c s i")
                                           'org-insert-src-block)
                            ))

4 Synatx Highlighting

Set variable org-src-fontify-natively to t to enable syntax highlighting in the source code blocks (you may need to revert-buffer to reload the org file).

(setq org-src-fontify-natively t)

org2blog makes use of the export engine of org-mode to convert org files into HTML. The source blocks are also syntax highlighted in the generated HTML files.

One more thing to mention. The colors of syntax highlighting in the generated HTML are the same as what you see in emacs. Therefore you’d better make your blog’s color theme similar to your emacs’s color theme. For example, I use zenburn in emacs, so I changed the CSS file of my blog to achieve a “not 100 percent identical but close enough” color theme. Otherwise, comfortable highlighting in your emacs might become difficult to read once the background color is changed. If your blog has a light background, while emacs has a dark one, you may switch to a lighter color theme such as tango (M-x load-theme tango) before calling M-x org2blog/wp-post-buffer.

5 Languages that are supported by org-mode

Currently (version 7.9.3d), org-mode supports the following languages:

Language Identifier
Asymptote asymptote
Emacs Calc calc
C++ C++
CSS css
Graphviz dot
gnuplot gnuplot
Java java
Javascript js
Ledger ledger
Lilypond lilypond
Mscgen mscgen
Octave octave
Oz oz
Plantuml plantuml
R R
Sass sass
GNU Screen screen
SQL sql
Awk awk
C C
Clojure clojure
ditaa ditaa
Emacs Lisp emacs-lisp
Haskell haskell
LaTeX latex
Lisp lisp
MATLAB matlab
Objective Caml ocaml
Org mode org
Perl perl
Python python
Ruby ruby
Scheme scheme
shell sh
SQLite sqlite

6 Other Solutions

Thanks to Celavd Evra ‘s recommendation, the Easy Templates of org-mode provide similar functionality: you could press <s TAB to get a source code block (but no language identifiers auto-completion), please read org-mode’s documents for details.

Note: English is not my first language, so please feel free to point out any
mistakes you might find.

2 thoughts on “Emacs:insert source code block in org-mode

    1. Wenshan Post author

      Thanks, I learned the trick after posting this blog, see section 6 Other Solutions.

      My solution provides auto-completion, it also automatically opens a temp buffer for editing the source code block.

      I use both (my solution has more “features”, the easy template one is quicker).