From: Thorsten Jolitz <tjolitz@gmail.com>
To: emacs-orgmode@gnu.org
Subject: Re: Controlling example block export?
Date: Tue, 01 Apr 2014 17:16:00 +0200 [thread overview]
Message-ID: <874n2dm66n.fsf@gmail.com> (raw)
In-Reply-To: 87a9c5m7a7.fsf@gmail.com
Thorsten Jolitz <tjolitz@gmail.com> writes:
> Heikki Lehvaslaiho <heikki.lehvaslaiho@gmail.com> writes:
>
>> Unless someone beats me to it, I'll write it one day. :)
>
> What about:
>
> #+begin_src emacs-lisp
> (defun tj/toggle-example-and-comment-blocks ()
> "Toggle example and comment blocks in current buffer."
> (interactive)
> (org-block-map
> (lambda ()
> (org-mark-element)
> (when (use-region-p)
> (let ((beg (region-beginning))
> (end (region-end)))
> (cond
> ((save-excursion
> (re-search-forward "#\\+begin_comment"
> (line-end-position) 'NOERROR))
> (replace-string "#+begin_comment" "#+begin_example"
> nil beg end)
> (replace-string "#+end_comment" "#+end_example"
> nil beg end))
> ((save-excursion
> (re-search-forward "#\\+begin_example"
> (line-end-position) 'NOERROR))
> (replace-string "#+begin_example" "#+begin_comment"
> nil beg end)
> (replace-string "#+end_example" "#+end_comment"
> nil beg end))
> (t nil))
> (deactivate-mark))))))
> #+end_src
I should have read the help string of `replace-string' till the end,
because
,-------------------------------------------------------------------
| This function is usually the wrong thing to use in a Lisp program.
| What you probably want is a loop like this:
| (while (search-forward FROM-STRING nil t)
| (replace-match TO-STRING nil t))
| which will run faster and will not set the mark or print anything.
`-------------------------------------------------------------------
Here is the corrected version:
#+begin_src emacs-lisp
(defun tj/toggle-example-and-comment-blocks ()
"Toggle example and comment blocks in current buffer."
(interactive)
(org-block-map
(lambda ()
(org-mark-element)
(when (use-region-p)
(let ((end (region-end)))
(cond
((save-excursion
(re-search-forward "#\\+begin_comment"
(line-end-position) 'NOERROR))
(while (search-forward "#+begin_comment"
(line-end-position) t)
(replace-match "#+begin_example" nil t))
(while (search-forward "#+end_comment" end t)
(replace-match "#+end_example" nil t)))
((save-excursion
(re-search-forward "#\\+begin_example"
(line-end-position) 'NOERROR))
(while (search-forward "#+begin_example"
(line-end-position) t)
(replace-match "#+begin_comment" nil t))
(while (search-forward "#+end_example" end t)
(replace-match "#+end_comment" nil t)))
(t nil))
(deactivate-mark))))))
#+end_src
--
cheers,
Thorsten
next prev parent reply other threads:[~2014-04-01 15:15 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-04-01 9:20 Controlling example block export? Heikki Lehvaslaiho
2014-04-01 10:10 ` Thorsten Jolitz
2014-04-01 12:40 ` Heikki Lehvaslaiho
2014-04-01 14:52 ` Thorsten Jolitz
2014-04-01 15:16 ` Thorsten Jolitz [this message]
2014-04-01 15:38 ` Nicolas Goaziou
2014-04-01 15:34 ` Nicolas Goaziou
2014-04-01 16:33 ` Charles Berry
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.orgmode.org/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=874n2dm66n.fsf@gmail.com \
--to=tjolitz@gmail.com \
--cc=emacs-orgmode@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).