From mboxrd@z Thu Jan 1 00:00:00 1970 From: Thorsten Jolitz Subject: Re: Controlling example block export? Date: Tue, 01 Apr 2014 17:16:00 +0200 Message-ID: <874n2dm66n.fsf@gmail.com> References: <87k3b9mkcb.fsf@gmail.com> <87a9c5m7a7.fsf@gmail.com> Mime-Version: 1.0 Content-Type: text/plain Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:42320) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WV0Oy-0000UR-IE for emacs-orgmode@gnu.org; Tue, 01 Apr 2014 11:15:08 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WV0Or-0001tU-1C for emacs-orgmode@gnu.org; Tue, 01 Apr 2014 11:15:00 -0400 Received: from plane.gmane.org ([80.91.229.3]:51726) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WV0Oq-0001tQ-QB for emacs-orgmode@gnu.org; Tue, 01 Apr 2014 11:14:52 -0400 Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1WV0On-0006tv-9v for emacs-orgmode@gnu.org; Tue, 01 Apr 2014 17:14:49 +0200 Received: from e178190017.adsl.alicedsl.de ([85.178.190.17]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 01 Apr 2014 17:14:49 +0200 Received: from tjolitz by e178190017.adsl.alicedsl.de with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 01 Apr 2014 17:14:49 +0200 List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: emacs-orgmode@gnu.org Thorsten Jolitz writes: > Heikki Lehvaslaiho 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