emacs-orgmode
[Top][All Lists]
Advanced

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

Re: [O] Programmatically handling org files


From: Thorsten Jolitz
Subject: Re: [O] Programmatically handling org files
Date: Mon, 12 Sep 2016 20:21:41 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux)

Joost Kremers <address@hidden> writes:

Hi,

> I was wondering if there is some sort of (semi)official API for handling
> org files programmatically. That's to say, is there a documented way for
> non-org Emacs packages to manipulate (the contents of) org files?
>
> Specifically, I'm wondering about creating and deleting entries (by
> entries I mean headers with their contents), changing TODO state, moving
> entries, that sort of thing.

by "non-org Emacs packages" you mean Emacs packages written in Elisp,
but not part of Org-mode? 

The org-mode parser converts an Org document into a nested list and
provides many convenience functions to work on this parse tree. So
org-element.el (and maybe ox.el too) is the core library for converting
an Org text document into an Elisp data structure and working with that,
have a look at these two functions:

,----
| 3965:(defun org-element-parse-buffer (&optional granularity visible-only)
| 4043:(defun org-element-map
`----

If you feel you don't need the whole parse tree, but rather want to act
locally on the Org element at point, you might want to look at 
org-dp.el with just two core functions (create and rewire an Org
element) and a mapping functions (plus quite a few utilities in
org-dp.el and org-dp-lib.el):

,----
| 523:(cl-defun org-dp-create
|        (elem-type &optional contents insert-p affiliated &rest args)
| 642:(cl-defun org-dp-rewire
|        (elem-type &optional contents replace affiliated element &rest args)
| 766:(defun org-dp-map
|        (fun-with-args rgxp &optional match-pos backward-search-p beg end
|         silent-p)
`----

Note that I recently added 4 "Tempo" Templates (a bit like Yasnippets)
to org-dp that make it easy to insert a 'create' or 'rewire' call with
just those parameters the interpreter uses for the element to be created
or rewired:

,----
| M-x tempo-template-org-dp-create
| M-x tempo-template-org-dp-create-with-comments
| M-x tempo-template-org-dp-rewire
| M-x tempo-template-org-dp-rewire-lambda
`----
 
e.g. calling the third one with elem type "headline" enters this template

#+BEGIN_SRC emacs-lisp
  (org-dp-rewire 'headline
  "\n" ;cont
  t ;ins 
  nil ;aff 
  nil ;elem 
  :level 1 ;1..8
  :priority nil ;65|66|67
  :todo-keyword TODO
  :title ""
  :tags '( )
  :commentedp nil
  :pre-blank 0
  :footnote-section-p nil
  )
#+END_SRC

while for elem type "example block" its only:

#+BEGIN_SRC emacs-lisp
  (org-dp-rewire 'example-block nil t ;cont ins
  nil ;aff 
  nil ;elem 
  :switches ""
  :preserve-indent ""
  :value ""
  )
#+END_SRC

since example-blocks have no content (:value is their content) and only
3 parameters that are actually interpreted.

Using this system, creating or rewiring an Org Element from Elisp
requires only to define the values of the interpreted parameters, all
the low level stuff (actually creating and inserting the new/modified
element in text form) is left to the interpreters (from org-element.el).

You just declare what you want and don't worry anymore how it is done
(=> dp stands for declarative programming, in this context at least ;-)

-- 
cheers,
Thorsten




reply via email to

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