emacs-orgmode
[Top][All Lists]
Advanced

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

Re: [O] Controlling example block export?


From: Thorsten Jolitz
Subject: Re: [O] Controlling example block export?
Date: Tue, 01 Apr 2014 17:16:00 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)

Thorsten Jolitz <address@hidden> writes:

> Heikki Lehvaslaiho <address@hidden> 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




reply via email to

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