From: Thorsten Jolitz <tjolitz@gmail.com>
To: emacs-orgmode@gnu.org
Subject: Two potentially useful functions for org-element
Date: Thu, 07 Aug 2014 01:01:58 +0200 [thread overview]
Message-ID: <87wqalql21.fsf@gmail.com> (raw)
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
next reply other threads:[~2014-08-06 23:02 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-08-06 23:01 Thorsten Jolitz [this message]
2014-08-07 8:59 ` Two potentially useful functions for org-element Thorsten Jolitz
2014-08-08 8:03 ` Nicolas Goaziou
2014-08-08 8:27 ` Thorsten Jolitz
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87wqalql21.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 external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.