Next: , Previous: Adding export back-ends, Up: Hacking

A.5 Context-sensitive commands

Org has several commands that act differently depending on context. The most important example is the C-c C-c (see The very busy C-c C-c key). Also the M-cursor and M-S-cursor keys have this property.

Add-ons can tap into this functionality by providing a function that detects special context for that add-on and executes functionality appropriate for the context. Here is an example from Dan Davison's org-R.el which allows you to evaluate commands based on the R programming language 1. For this package, special contexts are lines that start with #+R: or #+RR:.

     (defun org-R-apply-maybe ()
       "Detect if this is context for org-R and execute R commands."
       (if (save-excursion
             (beginning-of-line 1)
             (looking-at "#\\+RR?:"))
           (progn (call-interactively 'org-R-apply)
                  t) ;; to signal that we took action
         nil)) ;; to signal that we did not
     (add-hook 'org-ctrl-c-ctrl-c-hook 'org-R-apply-maybe)

The function first checks if the cursor is in such a line. If that is the case, org-R-apply is called and the function returns t to signal that action was taken, and C-c C-c will stop looking for other contexts. If the function finds it should do nothing locally, it returns nil so that other, similar functions can have a try.


[1] org-R.el has been replaced by the Org mode functionality described in Working With Source Code and is now obsolete.