7

At times, especially when taking notes, a literate org file can contain one-line source blocks, as follows:

* Taking notes
:PROPERTIES:
:tangle: whatever.pl
:comments: org
:END:

Perl set command
#+BEGIN_SRC perl
set $random_var=1;
#+END_SRC

Perl unset command (just making it up... :P)
#+BEGIN_SRC perl
unset $random_var;
#+END_SRC

For such applications, the #+BEGIN_SRC...#+END_SRC pragma looks to be an overkill. Easy templates reduce the typing, but the org doc still looks a bit overflowing.

Is there an easy way to do this? I know that a line starting with a : gets to verbatim text automatically. Is there a way to do something similar with org-tangle?

I am looking for a solution something like this, or at least close to these lines, while still retaining the source highlighting and tangling facilities for parent mode:

* Taking notes
:PROPERTIES:
:tangle: whatever.pl
:comments: org
:mode: perl
:END:

Perl set command
: set $random_var=1;

Perl unset command (just making it up... :P)
: unset $random_var;

It's cleaner, easy to look at and, IMHO, a bit less noisy.

Any suggestions would be appreciated. Thanks in advance!

  • How about a hide/unhide command for the entire file as to beginning and ending source code blocks, leaving whatever is sandwiched in between? The beginning and ending would have ... instead, or you can use whatever shorthand you like to indicate hidden text (e.g., a smiley face ), or you can have no indicator whatsoever for the hidden lines – lawlist Aug 9 '16 at 15:36
  • @lawlist Am not able to understand your example... can you show a demo? – deejay Aug 9 '16 at 15:42
  • 1
    In a buffer containing your first example, evaluate the following snippet to hide text : (save-excursion (goto-char (point-max)) (while (re-search-backward "#\\+BEGIN_SRC\\|#\\+END_SRC" nil t) (let ((ov (make-overlay (line-beginning-position) (1+ (line-end-position))))) (overlay-put ov 'invisible t)))) When you are done with your test, evaluate (remove-overlays). This is just a simple example of how to hide text. You can create place-holders for the hidden text that look ... or whatever you want -- e.g., hidden text lies here. Evaluate code with M-x eval-expression – lawlist Aug 9 '16 at 16:07
  • @lawlist thanks. that cleans up the buffer significantly. I would have been more eager for the org mode solution proposed above, though. One question: I tried to wrap your code in a function: (defun clean-org-buffer() (save-excursion (goto-char (point-max)) (while (re-search-backward "#\\+BEGIN_SRC\\|#\\+END_SRC" nil t) (let ((ov (make-overlay (line-beginning-position) (1+ (line-end-position))))) (overlay-put ov 'invisible t))))) However, evaluating this function results in error as follows: Wrong type argument: commandp, clean-org-buffer. Can you please suggest how to help this? – deejay Sep 27 '16 at 10:29
  • 1
    You can use either M-x eval-expression RET (clean-org-buffer) RET (without any modifications to the snippet) and/or you can add (interactive) just before the snippet in my previous comment (2 paragraphs above) so that it is usable interactively with M-x clean-org-buffer. – lawlist Sep 27 '16 at 15:11
4

From (org) Structure of code blocks:

Live code blocks can also be specified inline using

src_<language>{<body>}

or

src_<language>[<header arguments>]{<body>}

So you should be able to use src_perl{set $random_var=1;} instead of the full block.

  • 1
    This does not seem to work. 1) The syntax highlighting is gone, 2) The tangled code comes as a comment - not a statement in the output file (whatever.pl, in this case). Am I doing it wrong? – deejay Aug 10 '16 at 5:53

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.