Hello, I do not always use code blocks in org-mode, but when I do, I have forgotten the syntax :-). In order to prevent that situation I wrote a little function which is similar to org-insert-link. I called that function org-insert-code-block. This function reads the language per minibuffer in and supports completion. It only allows languages which are loaded via org-babel-load-languages.
Is this function also useful to others? I'm not a long-time Emacs lisp hacker, so any comment is welcome. Has anyone an idea for a reasonable keybinding for org-insert-code-block which is not already taken by org-mode? Thanks in advance. Best regards, Florian
diff --git a/lisp/org.el b/lisp/org.el index 66f9c3e..19e91c0 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -9145,6 +9145,21 @@ a link description or nil." "[[" (car link) "]]"))) ;;;###autoload +(defun org-insert-code-block () + "Insert a code block. At the prompt, enter the language which is available. + +Completion can be used to insert any language which is loaded in +org-babel-load-lanuages." + (interactive) + (setq language (completing-read + "Code block : " + (mapcar 'symbol-name + (mapcar 'car org-babel-load-languages)) + nil nil)) + (insert (concat "#+BEGIN_SRC " language "\n\n")) + (insert "#+END_SRC") + (previous-line)) + (defun org-insert-link-global () "Insert a link like Org-mode does. This command can be called in any mode to insert a link in Org-mode syntax."