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?

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
Leo Ufimtsev
  • 6,240
  • 5
  • 40
  • 48
  • 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. – ChrisGPT was on strike Jan 22 '15 at 01:41
  • Thank for asking. I added it in edit. – Leo Ufimtsev Jan 22 '15 at 04: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.) – ChrisGPT was on strike Jan 22 '15 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: http://stackoverflow.com/questions/10642888/syntax-highlighting-within-begin-src-block-in-emacs-orgmode-not-working Can you get syntax highlithing inside org mode while *editing* text? – Leo Ufimtsev Jan 22 '15 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`. – ChrisGPT was on strike Jan 22 '15 at 18:26
  • ~early comment post. Thank you for replying. It looks like I didn't set src-fontify-nativley variable correctly. I pressed 'apply' in customizer but I forgot to first press on 'Toggle' (user error). Now with it enabled I'm actually getting syntax highlighting inside the begin_src block while editing. It seems like there is no way to get away from the Begin_src blocks. Maybe I can just have no syntax highlighting for short snippets and use begin_src blocks for longer chunks of code. If you post an answer suggesting to use begin_src, I'll accept that as there seems to be no other way? – Leo Ufimtsev Jan 22 '15 at 18:34

1 Answers1

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.

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257