4
votes

Is it possible to get org-mode to use indentation rules of the specified language? For example, if I have the code below, I'd like var x; to indent properly when I hit the tab key (just as it would in javascript mode).

#+BEGIN_SRC javascript
function foo() {
var x;
}
#+END_SRC

I have set (setq org-src-fontify-natively t) which has enabled syntax highlighting, but not code formatting.

3
Formatting should be okay if you hit C-'. Is this no option for you? - Tobias
@Tobias That actives another pane which is not as convenient, especially if I already have another buffer open there. I'd like to be able to do the same thing inline if that's possible. - armandino
I get correct indentation when hitting TAB inside the source block. Only comment is that the whole block is indented 2 spaces. - Juancho
If I set the option org-src-fontify-natively to t indentation works, but after indentation point jumps back to the first line of the source block. org-version gives 8.2.5h. - Tobias
Sorry, in my last comment I meant org-src-tab-acts-natively. See the answer below. The jumping of the point was caused by my customization. The indentation works if I start emacs with the option -Q. - Tobias

3 Answers

4
votes

Customize the option org-src-tab-acts-natively to t. With this setting tab works as you expect it in the source code block. The additional indentation by two spaces is removed if you tangle the source code block or edit it with C-c '. My org-version is 8.2.5h.

1
votes

In spacemacs org-edit-special will open a new buffer with the right mode for regular formating key combos.

C-c ' brings up the new buffer

SPC m f l formats my new buffer (clojure major mode)

, c exits with save.

0
votes

Yes!

Source language block-mode specific indentation will happen if you adopt poly-org-mode, defined in Polymode.

But wait. There's more!

So will other language mode-specific feature (syntax-highlighting, REPL evaluation keyboard shortcuts, paren-balancing, code-navigation, etc).

I've used it on-and-off for many years, over which time it has rubbed out most idiosyncrasies and I now whole-heartedly endorse it and use it every day. I never use org-edit-special any more (control-').