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).
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
5 Languages that are supported by org-mode
Currently (version 7.9.3d), org-mode supports the following languages:
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.