Escaping org-mode example block inside of an example block - org-mode

If I am writing about emacs org-mode in an org-mode document, how do I properly escape examples. e.g.
#+BEGIN_EXAMPLE
#+BEGIN_EXAMPLE
* This is a heading
#+END_EXAMPLE
#+END_EXAMPLE
should show
#+BEGIN_EXAMPLE
* This is a heading
#+END_EXAMPLE
but I need to escape the #+END_EXAMPLE somehow. I'm not finding it in the manual.
Thanks!

Hi think you should use #begin_src org, since you want to write Org-Mode snippets.
#+begin_src org
,#+BEGIN_EXAMPLE
,* This is a heading
,#+END_EXAMPLE
#+end_src
You can easily edit those snippets with M-x `org-edit-special' RET or C-c '.

If the examples are short, you should be able to export them successfully using the alternate form for examples (Literal Examples)
: #+BEGIN_EXAMPLE
: * This is a headline
: #+END_EXAMPLE
Shows the expected
#+BEGIN_EXAMPLE
* This is a heading
#+END_EXAMPLE
Of course if your examples are significantly longer this will not be nearly as convenient. Hopefully you'll only need to escape the #+END_EXAMPLE in short use cases (or use rectangle insert I suppose)
EDIT:
After a bit more testing, you can actually suppress commands on individual lines which will allow for longer example cases.
#+BEGIN_EXAMPLE
#+BEGIN_EXAMPLE
* This is a headline
,#+END_EXAMPLE
#+END_EXAMPLE
Will export as the expected
#+BEGIN_EXAMPLE
* This is a heading
#+END_EXAMPLE
It will also allow for manual indentation of example blocks, if you require more indentation within the block.

Related

Emacs - How to wrap selection in org-mode source code block?

I have a bunch of elisp and other code with some notes i wanted to reformat to be more organized, and i found that having to type
#+BEGIN_SRC emacs-lisp ... #+END_SRC
all the time around what i want, is taking a bit longer than expected...
So what i wanted to do instead is to wrap/or put the selected content (with C-space) and put it in a template source code block for org-mode (in my case it's mostly elisp code, but i plan to use it for other things maybe)
How could i do this in emacs or in elisp?
There is a new templating mechanism in recent Org mode (>= 9.0 IIRC) that allows you tor wrap a region in a block: after selecting the region in the usual manner, you say C-c C-, s. You still have to type the emacs-lisp part though. That's the disadvantage. The advantage is that it is general enough to allow you to wrap a region in any kind of block. In your case, I think the disadvantage outweighs the advantage, so I would go with the wrap-region method in the other answer, but this one is good to know as well.
You can try wrap-region. It will allow you to define what type of string you want to wrap around a selection.
Put this in your init.el and evaluate it.
(wrap-region-global-mode t)
(wrap-region-add-wrapper "#+BEGIN_SRC emacs-lisp\n" "#+END_SRC" "#" 'org-mode)
Then, while you are editing your org files, you can select a block of text and type #, which will wrap it with your string. You can change the # to another character that will do the wrapping.
There is a feature in org-mode to do exactly that. It's like a snippet of some sort where you enter <eland hit TAB, the < char is here to say we're gonna use a template and the el part tells which template to use. But of course, you have to configure it first.
For that, you can just add this to an org-mode file or to your init.el file :
#+begin_src emacs-lisp
;; This is needed as of Org 9.2
(require 'org-tempo)
(add-to-list `org-structure-template-alist `("sh" . "src shell"))
(add-to-list `org-structure-template-alist `("el" . "src emacs-lisp"))
(add-to-list `org-structure-template-alist `("py" . "src python"))
#+end_src
There a bunch of way to use this, it's actually more useful than just use it as a template, you can go check the documentation here.

org mode - clickable text jump to specific line in emacs

I have a file list and wish to jump to specific line if click the text.
#+BEGIN_SRC python :results output
for i in range(0,10):
print "[[./test%d.txt:100]]"%i
#+END_SRC
#+RESULTS:
#+begin_example
[[./test0.txt:100]]
[[./test1.txt:100]]
[[./test2.txt:100]]
[[./test3.txt:100]]
[[./test4.txt:100]]
[[./test5.txt:100]]
[[./test6.txt:100]]
[[./test7.txt:100]]
[[./test8.txt:100]]
[[./test9.txt:100]]
#+end_example
If without the linenumber at the end, click the text line will open the file but not jump to the specific line number.
How should I change the syntax to jump to line number?
In your example, the only problem was the syntax for Org external links. It is
[[./test%d.txt::100]] (and not [[./test%d.txt:100]])
This will work in your Emacs Org buffer, however the link will not be exported if you publish your file (C-c C-e h o).
If you also want to export/publish your links you can use:
#+OPTIONS: d:t \n:t
#+BEGIN_SRC python :results output drawer :exports both
for i in range(0,10):
print "[[./test%d.txt::100][test%d.txt::100]]" %(i,i)
#+END_SRC
The d:t option tells to export drawers, the \n:t one to preserve linebreaks.
Putting your python code result into a drawer (the :results output drawer) allows org mode to interpret it as true org-mode code.

Have basic autoindentation in code blocks in org-mode

Emacs' fine org-mode has CODE and EXAMPLE blocks that are easily edited in a proper major mode using C-c '. But for quick alterations and minor edits one might prefer editing the block inline without opening new popup windows for a two-second operations.
Of course org lets you do that but the automatic indentations are off radar while editing inline like that. Where and what should I hack to make org's indentation logic to act like default dumb autoindentation (ie. copy the indent level of the previous line) while the cursor is in a code block?
#+BEGIN_SRC python
def foo():
return 42
#+END_SRC
#+BEGIN_EXAMPLE
Oh my
hh
#+END_EXAMPLE
If the cursor was at the end of the line containing hh, pressing C-j would get me a new line with the same indentation as the previous one.
The following allows me to edit (and indent) code "inline" without ever going to the indirect buffers (except for M-q on comments, which did work and does not anymore).
;; same effect for `tab' as in the language major mode buffer
(setq org-src-tab-acts-natively t)

Font-locking for SQL-MODE inside of ORG-MODE not font-locking

Related to question: org-mode: fontify code blocks natively
I've got the latest org-mode and emacs versions as of November 1, 2012 (org stored in org-20121105).
I've also got the sql-mode that comes with emacs-24.
I've got fontification turned one:
;; fontify code in code blocks
(setq org-src-fontify-natively t)
Yet this does not fontify in my org documents. Java, bash, etc. all work.
#+BEGIN_SRC SQL
SELECT foo FROM bar
#+END_SRC
When I open a file foobar.sql, the mode indicator says SQL[ANSI] (which I also tried as the source type), and font-locking works.
Thanks in advance for any tips.
Firstly, the name of the SRC block mode is case-sensitive. It should be sql instead of SQL.
#+BEGIN_SRC sql
SELECT foo FROM bar
#+END_SRC
Secondly, the initial font-lock of sql-mode seams not to highlight SQL keywords, (at least to me, it looks no difference no matter you turn it on or off). Therefore, you need to specify which kind of SQL you want to highlight. If you are using MySQL for example:
(add-hook 'sql-mode-hook
(lambda ()
(sql-highlight-mysql-keywords)))
Then Restart Emacs. It should work now.
Oh, wait, try putting #+BEGIN_SRC sql in lower case. See here for identifiers.
Try refreshing the display, by making the block be reparsed (break the syntax and undo, or something). It often happens to me with python or bibtex blocks, but this fixes it.
I can't see why it wouldn't fontify inline if it finds the right mode when you C-c '.
Also, I'm afraid fontification, while being one of org-mode's nicer features, is not exactly perfectly handled. From the mailing list :
The fontification engine is not very powerful and get easily fooled.

Literate Programming using org-babel

I am on a literate program using org-babel. My source is structured like so,
-imports
-utility fns
-game structure
- detailed explanations
This is the normal structure of the code, what I would like to do is move explanations of the utility fns to the end so it does not show up first in the generated pdf file. Now this can be done with noweb extension, but the problem is when you have lots of little functions for each one I have to add a src_block with a unique name scroll down the file and add a reference to that in the file which is really annoying. Is there a way to name all the src_blocks in a section? say all code in this section goes into block A.
You can give multiple chunks the same name. For example, I generate my .emacs file with org-tangle, and at the top of the org file, I have a master template that looks something like this:
#+begin_src emacs-lisp :tangle "/path/to/.emacs" :comments both :noweb tangle
<<generated-file-warning>
<<includes>>
<<definitions>>
<<settings>>
<<generated-file-warning>
#+end_src
Underneath that, I have my outline with source blocks like so:
* extensions
** yasnippet
#+name: early-includes
#+begin_src emacs-lisp
(require 'yasnippet)
(yas/initialize)
#+end_src
#+name: settings
#+begin_src emacs-lisp
(yas/load/directory "/path/to/my/snippets")
#+end_src
Note: for older versions of org-mode, you may need to use #+srcname: instead of #+name:
You can also create a property called noweb-ref, which applies the same name to all source blocks in a sub-tree.