From mboxrd@z Thu Jan 1 00:00:00 1970 From: Thorsten Jolitz Subject: Two potentially useful functions for org-element Date: Thu, 07 Aug 2014 01:01:58 +0200 Message-ID: <87wqalql21.fsf@gmail.com> Mime-Version: 1.0 Content-Type: text/plain Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:52354) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XFADt-0004dz-Go for emacs-orgmode@gnu.org; Wed, 06 Aug 2014 19:02:29 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XFADl-0007T7-Vz for emacs-orgmode@gnu.org; Wed, 06 Aug 2014 19:02:21 -0400 Received: from plane.gmane.org ([80.91.229.3]:57923) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XFADl-0007St-PD for emacs-orgmode@gnu.org; Wed, 06 Aug 2014 19:02:13 -0400 Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1XFADi-0006pa-DF for emacs-orgmode@gnu.org; Thu, 07 Aug 2014 01:02:10 +0200 Received: from e178063155.adsl.alicedsl.de ([85.178.63.155]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 07 Aug 2014 01:02:10 +0200 Received: from tjolitz by e178063155.adsl.alicedsl.de with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 07 Aug 2014 01:02:10 +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 Hi List, now that I understand the 'org-element API' a bit better, I think that the following two functions can be very useful for creating and modifying Org elements without the usual point movements, regexp searches and string operations in a buffer: #+begin_src emacs-lisp ;; might become `org-element-create' (defun* tj/create-element (&optional insert-p &rest args &key (type 'headline) &allow-other-keys) "Create Org element, maybe insert at point." (let ((strg (org-element-interpret-data (list type args)))) (if insert-p (insert strg) strg))) #+end_src #+results: : tj/create-element #+begin_src emacs-lisp ;; might become `org-element-rewire' (defun* tj/rewire-element (&optional replace &rest args &key type &allow-other-keys) "Rewire element at point, maybe replace it." (let* ((elem (org-element-at-point)) (plist (cadr elem)) (beg (org-element-property :begin elem)) (end (org-element-property :end elem)) strg) (while args (setq plist (plist-put plist (pop args) (pop args)))) (setq strg (org-element-interpret-data (list (or type (org-element-type elem)) plist))) (case replace (append (save-excursion (goto-char end) (insert strg))) (prepend (goto-char beg) (insert strg)) (t (if replace (let ((marker (save-excursion (goto-char end) (point-marker)))) (delete-region beg end) (goto-char marker) (save-excursion (insert strg))) strg))))) #+end_src #+results: : tj/rewire-element Here are a few usage examples: #+begin_src emacs-lisp (tj/create-element nil :type 'src-block :language "emacs-lisp" :value "(+ 2 2)\n" :parameters ":results raw" :name "myblock" :header '(":var x=5")) #+end_src #+results: : #+NAME: myblock : #+HEADER: :var x=5 : #+BEGIN_SRC emacs-lisp :results raw : (+ 2 2) : #+END_SRC #+begin_src emacs-lisp :wrap org (tj/create-element nil :type 'headline :level 2 :todo-keyword "DONE" :priority 66 :title "Hello World" :tags '("tag1" "tag2")) #+end_src #+results: #+BEGIN_org ,** DONE [#B] Hello World :tag1:tag2: #+END_org Now eval next src-block #+begin_src emacs-lisp (defun test-fun () (tj/rewire-element 'append :parameters ":results table :exports both" :name (format "random-block-%d" (1+ (random 11))) :header '(":var x=5") :value (format "%s\n%s\n" '(+ 2 2) '(* 3 x)))) #+end_src #+results: : test-fun and then, with point at beginning of following src-block, eval 'M-: (test-fun)' #+BEGIN_SRC emacs-lisp :results raw (+ 2 2) #+END_SRC #+NAME: random-block-6 #+HEADER: :var x=5 #+BEGIN_SRC emacs-lisp :results table :exports both (+ 2 2) (* 3 x) #+END_SRC -- cheers, Thorsten