5

I use noweb references inside sh code blocks, for example:

#+begin_src sh
  echo "test"
  <<some-ref>>
  echo "test"
#+end_src

But this causes syntax highlighting to be broken because << gets interpreted as here document by the sh syntax highlighter and rest of the document (line 3 for the example above) becomes non-highlighted.

How can I fix this?

Drew
  • 75,699
  • 9
  • 109
  • 225
isamert
  • 103
  • 4
  • You could try the code from https://emacs.stackexchange.com/questions/63306/emphasize-text-snippets-in-source-blocks and set `(setq org-babel-default-header-args (cons '(:emph . "'((\"<<\" \">>\" 'hi-red-b))") (assq-delete-all :emph org-babel-default-header-args)))` file-locally in the org files where you need it or even in your init file if you really want that. Save the code in some file within your `load-path`, e.g., `org+-emph.el` and byte-compile it. Add `(require 'org+-emph)` to your init file. – Tobias Apr 01 '21 at 02:58
  • Did you try my suggestion? If not, what was the problem? – Tobias Apr 09 '21 at 04:21
  • @Tobias Thanks for the suggestion! Sorry for not being able to get back earlier. I tried your solution but it completely broke the syntax highlighting in the src blocks. It seems to be working (although with some little problems) if I write the src block manually or paste it from somewhere else, but right after editing it with `C-c '` the colors gets mixed up. OK, right now I realized the problem is the 2-space indentation that org automatically applies after doing `C-c '`, your package does not account for that I guess. – isamert Apr 10 '21 at 14:07
  • 1
    Please use [package `org-src-emph`](https://github.com/TobiasZawada/org-src-emph/). See the installation guide and the usage examples there. The code has some fix that hopefully makes editing with `C-c '` fully functional. It is clear that the Noweb references may interfere with the source code in the special edit buffer. I had to rename the package to comply with the guidelines of Melpa. If the package is stable enough we could propose it for Melpa. Please test. – Tobias Apr 11 '21 at 23:51
  • @Tobias although I am not the author of this question, I had the same problem and I tried your package. It solved the problem! Thanks for your work! – PsyFish Aug 23 '21 at 07:51

2 Answers2

3

You can define the delimiters babel use for noweb references, by setting org-babel-noweb-wrap-start and org-babel-noweb-wrap-start.

For instance, in org mode document where I dabble in bash, I insert at the bottom of the file:

* COMMENT Local Variables
# Local Variables:
# org-babel-noweb-wrap-start: "«"
# org-babel-noweb-wrap-end: "»"
# End:

The syntax to refer to noweb now is:

#+begin_src sh :noweb yes
«ze_bloc_to_include»
#+end_src

I like to use « and » characters, because they are very similar to << and >>, but you can choose other combination.

The drawback of this solution : it changes for the whole buffer, not only a single block. Otherwise, pretty great all around.

Dreammm
  • 46
  • 3
3

I liked @Dreammm's idea of using the « and » characters but didn't like having to declare file local variables.

I also did not want to always use « and ».

I just overwrote the org-babel-noweb-wrap function so now I can use either << and >> or « and ».

(defun org-babel-noweb-wrap (&optional regexp)
  "Return regexp matching a Noweb reference.

Match any reference, or only those matching REGEXP, if non-nil.

When matching, reference is stored in match group 1."
  (rx-to-string
   `(and (or "<<" "«")
           (group
            (not (or " " "\t" "\n"))
            (? (*? any) (not (or " " "\t" "\n"))))
           (or ">>" "»"))))
Chris
  • 143
  • 5