0

How can I set a default TAB behavior (i.e. 4 spaces instead of tab) in an Org source-block when the language is not supported? Example:

#+begin_src xyz
lkjsdf
    iouqweou  
#+end_src

Using TAB within the source-block gives me the following error message:

org-edit-src-code: No such language mode: xyz-mode

I've already added

(setq-default indent-tabs-mode nil)
(setq-default tab-width 4)

to the top of my init-file, and I'm loading org-mode via use-package

(use-package org
:ensure org-plus-contrib
:mode (("\\.org\\'" . org-mode))
:hook
(org-mode . variable-pitch-mode)
(org-mode . visual-line-mode)
:config
(setq org-src-preserve-indentation t)
(setq org-src-tab-acts-natively t))
  • Note: tab-width is definitely not what you want to change here. C-h o tab-width RET says "NOTE: This controls the display width of a TAB character, and not the size of an indentation step." – Stefan Dec 22 '19 at 20:29
  • By default, TAB is bound to org-cycle, which does not give rise to the error you show, so you probably have some customization in place. Check what TAB is bound to in your case with C-h c TAB. Also, try emacs -q -l minimal.el whereminimal.el is an init file with the bare minimum of initialization needed and see if you can reproduce the problem there: I suspect you will not be able to. – NickD Dec 23 '19 at 0:51
  • @NickD When I start Emacs with emacs -q TAB is bound to org-cycle; when I use my own init-file, TAB is also bound to org-cycle -- so no difference there. And also when starting with emacs -q the issue remains the same: If I press TAB in a code-block of an unknown language, the cursor won't move. – Michu Dec 23 '19 at 9:33
  • That's different behavior than what you report in your question: there you report an error, not that the cursor does not move. The latter is to be expected, the former not. But how are we to figure out what's happening, if you don't report accurately? – NickD Dec 23 '19 at 16:03
  • @NickD sry I wasn't clear: When I press TAB in a code-block of an unknown language, the cursor won't move and I get the aforementioned error-message … and that's not the behavior I want; if I press TAB in an source-block of an unknown language, I want that the cursor moves 4 spaces. – Michu Dec 24 '19 at 10:37
0

The org-tab-first-hook hook runs as the first action when TAB is pressed, so you need a function that checks if (1) the point is inside a src block and (2) the language is not found. If both conditions are true, indent to column 4. To insert 4 spaces literally, change the indent-to expression to (insert (make-string 4 ?\s)).

(add-hook 'org-tab-first-hook
          (lambda ()
            (when (org-in-src-block-p t)
              (let* ((elt (org-element-at-point))
                     (lang (intern (org-element-property :language elt)))
                     (langs org-babel-load-languages))
                (unless (alist-get lang langs)
                  (indent-to 4))))))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.