emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [O] export code with backslashes


From: Charles C. Berry
Subject: Re: [O] export code with backslashes
Date: Mon, 16 Mar 2015 15:28:03 -0700
User-agent: Alpine 2.00 (OSX 1167 2008-08-23)

On Mon, 16 Mar 2015, hymie! wrote:

Greetings.

I'm only asking this question because it seems that Orgmode can do
anything, although I admit that what I'm asking for is probably outside
the normal scope.

I have snips of code in my org files, denoted as ~code~.  I prefer ~code~ to
BEGIN_SRC blocks, because I don't like the big grey text boxes in my
exported documents.  Sometimes my code is very long and includes long lists
of options and arguments and such; for example:

~useradd -U -G wheel -p
'$6$wcMRrkcdGeNHLT5b$password0ISmGZSsILOyV/WJnpassword//'
accountname~

This is all one line.

Then I use C-c C-e t A to "export" this into an ascii buffer, the sole
reason being to remove the tilde characters and provide me an easy
cut-n-paste option.   I end up with this:

useradd -U -G wheel -p <newline>
'$6$wcMRrkcdGeNHLT5b$password0ISmGZSsILOyV/WJnpassword//'
<newline>
accountname <newline>

I would really really really like it if, in addition to the newlines that
are added, a backslash could be added as well.  "Hey, this is a line
enclosed in tildes.  I'm going to add line-breaks.  Each line-break except
for the one at the end needs a backslash"

useradd -U -G wheel -p \<newline>
'$6$wcMRrkcdGeNHLT5b$password0ISmGZSsILOyV/WJnpassword//' \<newline>
accountname <newline>

Is such a thing possible?

Yes.

You can add a filter, see (info "(org) Advanced configuration")

The line breaks come _after_ the code is processed. So you need to make the line breaks happen before you know can replace them. Try:

#+BEGIN_SRC emacs-lisp
  (defun org-export-ascii-filter-code (text back-end info)
  "Replace `\\n' with `\\' in ascii code."
    (if (eq back-end 'ascii)
        (replace-regexp-in-string
         "\n" "\\\n"
         (org-babel-chomp
          (org-export-string-as text 'ascii t))
         nil t)
      text))
  (add-to-list 'org-export-filter-code-functions
               'org-export-ascii-filter-code)
#+END_SRC

with ascii export.

When I run this on your example, I get only one line break, but it is preceeded by a backslash.

Naturally, you will need to adapt this up for other backends as a single backslash might not be what is wanted.

HTH,

Chuck



reply via email to

[Prev in Thread] Current Thread [Next in Thread]