2

I would like to use Emac's org mode for taking notes of java snippets. I would like the java snippets to be syntax highlighted.

I tried running Org-mode in minor mode and Java-mode in major mode, but I found this lacks a lot of Org-Mode features (e.g links). I would prefer to run Org-mode in major mode and have some minor mode to do java syntax highlihgting for when it finds java syntax.

I would rather avoid the #+begin_src business as my file would be full of those.

Is this possible?

[Edit] in was thinking along the lines of soft syntax highlighting for none headings and non org-items . I.e general paragraph body?

6
  • 1
    "I would rather avoid the #+begin_src business as my file would be full of those." So how exactly do you hope to separate Java code from Org content? Maybe you should add an example of what you're going for to your question. Commented Jan 22, 2015 at 1:41
  • Thank for asking. I added it in edit. Commented Jan 22, 2015 at 4:07
  • 1
    Org mode doesn't work like that. You can get syntax highlighting with #begin_src and #end_src, but you explicitly say you don't want to use that feature. (Note that this actually does more than just syntax highlighting; C-' in such a block opens up a new buffer so you can edit source code in its major mode.) Commented Jan 22, 2015 at 13:44
  • but for me there is no syntax highlighting in #begin_src blocks while editing the flie thou? I did try to enable org-src-fontify-natively as mentioned here: stackoverflow.com/questions/10642888/… Can you get syntax highlithing inside org mode while editing text? Commented Jan 22, 2015 at 17:07
  • What version of Emacs (M-x version) and Org mode (M-x org-version) are you using? Does this persist with emacs -Q, when you have manually set org-src-fontify-natively to t? What if you manually call org-src-fontify-block with point inside a code block? It works fine for me with a stable release, e.g. Emacs 24.3.1 and Org 7.9.3f under Ubuntu, but under a current trunk build (Emacs "25.0.50.1" and Org "8.2.10") I get org-mode fontification error. Commented Jan 22, 2015 at 18:26

1 Answer 1

4

The only mechanism of which I'm aware that supports syntax-highlighted code blocks in Org-mode is the source code block feature you have already mentioned.

Setting org-src-fontify-natively to t should enable syntax highlighting for such blocks:

(setf org-src-fontify-natively t)

Code blocks should look something like this:

* Pretty sweet Org heading

  This is an org-mode file, which is cool for lots of reasons, e.g.

  - it's Emacs, and
  - it supports syntax-highlighted blocks
    - (note that this requires the variable ~org-src-fontify-natively~
      to be set to ~t~)

#+BEGIN_SRC java
  public class HelloWorld {
      public static void main(String[] args) {
          System.out.println("Hello, World");
      }
  }
#+END_SRC

A few tips:

  • The quickest way to start a new code block is to type <s and then hit Tab. This expands to

    #+BEGIN_SRC |
    
    #+END_SRC
    

    with the cursor represented by |, so you can just type java and start editing.

  • With point inside such a block, org-edit-special, bound to C-c ' by default, will open the code block up in a separate buffer with the appropriate major mode active. You can use the full power of that mode, then type C-c ' again to update the embedded snippet.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much for the tips! I went along with using begin_src for longer snippets of code and not using it for very short one-liners. The tips and org-edit-special are of great help!.
the hotkeys like <s seems not-working in Emacs 27.1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.