emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Getting heading properties from org-element
@ 2015-07-05 17:33 John Kitchin
  2015-07-05 19:15 ` Thorsten Jolitz
  0 siblings, 1 reply; 5+ messages in thread
From: John Kitchin @ 2015-07-05 17:33 UTC (permalink / raw)
  To: emacs-orgmode@gnu.org

Hi all,

Is there a convenient way to get the properties of a headline from
org-element? I see they are in the parsed output, e.g. as :CATEGORIES
emacs,org :DATE today, but I didn't see a way to get them if I don't
already know what they are.

I know how to get these from an org file, e.g. org-entry-properties, and
I am looking for something like this from an element.

thanks.

--
Professor John Kitchin
Doherty Hall A207F
Department of Chemical Engineering
Carnegie Mellon University
Pittsburgh, PA 15213
412-268-7803
@johnkitchin
http://kitchingroup.cheme.cmu.edu

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Getting heading properties from org-element
  2015-07-05 17:33 Getting heading properties from org-element John Kitchin
@ 2015-07-05 19:15 ` Thorsten Jolitz
  2015-07-06 15:23   ` John Kitchin
  0 siblings, 1 reply; 5+ messages in thread
From: Thorsten Jolitz @ 2015-07-05 19:15 UTC (permalink / raw)
  To: emacs-orgmode

John Kitchin <jkitchin@andrew.cmu.edu> writes:

Hi John,

> Is there a convenient way to get the properties of a headline from
> org-element? I see they are in the parsed output, e.g. as :CATEGORIES
> emacs,org :DATE today, but I didn't see a way to get them if I don't
> already know what they are.
>
> I know how to get these from an org file, e.g. org-entry-properties, and
> I am looking for something like this from an element.


in my org-dp repo I have these two functions:

#+BEGIN_SRC emacs-lisp
(defun org-dp-contents (&optional element interpret-p no-properties-p)
  "Get contents of element-at-point or ELEMENT.
If INTERPRET-P is non-nil, call `org-element-interpret-data' on
return value. Call `org-no-properties' on result if
NO-PROPERTIES-P is non-nil too."
  (let* ((elem (cond
		((and (not (booleanp element))
		      (symbolp element))
		 (eval element))
		((stringp element)
		 (let ((el (car (read-from-string element))))
		   (when (consp el) el)))
		((consp element) element)
		(t (org-element-at-point))))
	 (beg (org-element-property :begin elem))
	 (end (org-element-property :end elem))
	 (type (org-element-type elem)))
    (if (and beg end)
	(save-restriction
	  (narrow-to-region beg end)
	  (let ((cont (org-element-map
			  (org-element-parse-buffer 'object)
			  type 'org-element-contents nil t)))
	    (cond
	     ((and interpret-p no-properties-p)
	      (org-no-properties (org-element-interpret-data cont)))
	     (interpret-p
	      (org-element-interpret-data cont))
	     (t cont))))
      (org-element-contents elem))))


(defun org-dp-filter-node-props (filter &optional negate-p verbose-p)
  "Return filtered node-properties.
FILTER should be either a symbol from `org-dp-prop-classes', the
symbol `org' (matching the union of all `org-dp-prop-classes' and
customizable variable `org-dp-misc-props'), a list of keys as
strings, or a (single) regexp-string. If NEGATE-P is non-nil, the
properties not matched by the filter are returned. If VERBOSE-P
is non-nil, a message is printed if no property-drawer is found,
otherwise nil is returned."
  (let ((props (save-excursion
		 (and
		  (or (org-at-heading-p)
		      (outline-previous-heading))
		  (re-search-forward
		   org-property-drawer-re
		   (save-excursion
		     (org-end-of-meta-data-and-drawers)
		     (point))
		   'NOERROR 1)
		  (progn
		    (goto-char (match-beginning 0))
		    (org-dp-contents)))))
	filtered-props)
    (if (not props)
	(when verbose-p
	  (message
	   "Could not find properties at point %d in buffer %s."
	   (point) (current-buffer)))
      (org-element-map props 'node-property
	(lambda (--prop)
	  (let* ((key (org-element-property :key --prop))
		 (val (org-element-property :value --prop))
		 (memberp (case filter
			    ((special custom default file global)
			     (member-ignore-case
			      key
			      (eval
			       (cdr-safe
				(assoc filter
				       org-dp-prop-classes)))))
			    (org (member-ignore-case
				  key (org-dp-org-props)))
			    (t (cond
				((stringp filter)
				 (string-match filter key))
				((consp filter)
				 (member-ignore-case key filter))
				(t (error "Not a valid filter: %s"
					  filter)))))))
	    (when (or (and negate-p (not memberp))
		      (and (not negate-p) memberp))
	      (setq filtered-props
		    (cons (cons key val) filtered-props))))))
      filtered-props)))

#+END_SRC

maybe thats somehow related...

-- 
cheers,
Thorsten

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Getting heading properties from org-element
  2015-07-05 19:15 ` Thorsten Jolitz
@ 2015-07-06 15:23   ` John Kitchin
  2015-07-06 15:36     ` Nicolas Goaziou
  0 siblings, 1 reply; 5+ messages in thread
From: John Kitchin @ 2015-07-06 15:23 UTC (permalink / raw)
  To: Thorsten Jolitz; +Cc: emacs-orgmode

hm. We must have different use cases in mind. Currently I am using

(save-excursion
                      (goto-char
                       (org-element-property :begin headline))
                      (org-entry-properties))

to get the properties. I thought there would be a way to get those from
the parse tree to avoid the excursion (I use this to index a few
thousand org-files so I want it to be fast). It turns out that is not
too slow, so until a better way comes along, I will use it!

Thorsten Jolitz writes:

> John Kitchin <jkitchin@andrew.cmu.edu> writes:
>
> Hi John,
>
>> Is there a convenient way to get the properties of a headline from
>> org-element? I see they are in the parsed output, e.g. as :CATEGORIES
>> emacs,org :DATE today, but I didn't see a way to get them if I don't
>> already know what they are.
>>
>> I know how to get these from an org file, e.g. org-entry-properties, and
>> I am looking for something like this from an element.
>
>
> in my org-dp repo I have these two functions:
>
> #+BEGIN_SRC emacs-lisp
> (defun org-dp-contents (&optional element interpret-p no-properties-p)
>   "Get contents of element-at-point or ELEMENT.
> If INTERPRET-P is non-nil, call `org-element-interpret-data' on
> return value. Call `org-no-properties' on result if
> NO-PROPERTIES-P is non-nil too."
>   (let* ((elem (cond
>               ((and (not (booleanp element))
>                     (symbolp element))
>                (eval element))
>               ((stringp element)
>                (let ((el (car (read-from-string element))))
>                  (when (consp el) el)))
>               ((consp element) element)
>               (t (org-element-at-point))))
>        (beg (org-element-property :begin elem))
>        (end (org-element-property :end elem))
>        (type (org-element-type elem)))
>     (if (and beg end)
>       (save-restriction
>         (narrow-to-region beg end)
>         (let ((cont (org-element-map
>                         (org-element-parse-buffer 'object)
>                         type 'org-element-contents nil t)))
>           (cond
>            ((and interpret-p no-properties-p)
>             (org-no-properties (org-element-interpret-data cont)))
>            (interpret-p
>             (org-element-interpret-data cont))
>            (t cont))))
>       (org-element-contents elem))))
>
>
> (defun org-dp-filter-node-props (filter &optional negate-p verbose-p)
>   "Return filtered node-properties.
> FILTER should be either a symbol from `org-dp-prop-classes', the
> symbol `org' (matching the union of all `org-dp-prop-classes' and
> customizable variable `org-dp-misc-props'), a list of keys as
> strings, or a (single) regexp-string. If NEGATE-P is non-nil, the
> properties not matched by the filter are returned. If VERBOSE-P
> is non-nil, a message is printed if no property-drawer is found,
> otherwise nil is returned."
>   (let ((props (save-excursion
>                (and
>                 (or (org-at-heading-p)
>                     (outline-previous-heading))
>                 (re-search-forward
>                  org-property-drawer-re
>                  (save-excursion
>                    (org-end-of-meta-data-and-drawers)
>                    (point))
>                  'NOERROR 1)
>                 (progn
>                   (goto-char (match-beginning 0))
>                   (org-dp-contents)))))
>       filtered-props)
>     (if (not props)
>       (when verbose-p
>         (message
>          "Could not find properties at point %d in buffer %s."
>          (point) (current-buffer)))
>       (org-element-map props 'node-property
>       (lambda (--prop)
>         (let* ((key (org-element-property :key --prop))
>                (val (org-element-property :value --prop))
>                (memberp (case filter
>                           ((special custom default file global)
>                            (member-ignore-case
>                             key
>                             (eval
>                              (cdr-safe
>                               (assoc filter
>                                      org-dp-prop-classes)))))
>                           (org (member-ignore-case
>                                 key (org-dp-org-props)))
>                           (t (cond
>                               ((stringp filter)
>                                (string-match filter key))
>                               ((consp filter)
>                                (member-ignore-case key filter))
>                               (t (error "Not a valid filter: %s"
>                                         filter)))))))
>           (when (or (and negate-p (not memberp))
>                     (and (not negate-p) memberp))
>             (setq filtered-props
>                   (cons (cons key val) filtered-props))))))
>       filtered-props)))
>
> #+END_SRC
>
> maybe thats somehow related...

--
Professor John Kitchin
Doherty Hall A207F
Department of Chemical Engineering
Carnegie Mellon University
Pittsburgh, PA 15213
412-268-7803
@johnkitchin
http://kitchingroup.cheme.cmu.edu

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Getting heading properties from org-element
  2015-07-06 15:23   ` John Kitchin
@ 2015-07-06 15:36     ` Nicolas Goaziou
  2015-07-06 16:03       ` John Kitchin
  0 siblings, 1 reply; 5+ messages in thread
From: Nicolas Goaziou @ 2015-07-06 15:36 UTC (permalink / raw)
  To: John Kitchin; +Cc: emacs-orgmode, Thorsten Jolitz

Hello,

John Kitchin <jkitchin@andrew.cmu.edu> writes:

> hm. We must have different use cases in mind. Currently I am using
>
> (save-excursion
>                       (goto-char
>                        (org-element-property :begin headline))
>                       (org-entry-properties))
>
> to get the properties. I thought there would be a way to get those from
> the parse tree to avoid the excursion (I use this to index a few
> thousand org-files so I want it to be fast).

If you have the full parse tree and you're only interested in local
properties (i.e., neither CATEGORY or special properties):

  (org-element-map headline 'node-property
    (lambda (p)
      (cons (org-element-property :key p)
            (org-element-property :value p))))


Regards,

-- 
Nicolas Goaziou

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Getting heading properties from org-element
  2015-07-06 15:36     ` Nicolas Goaziou
@ 2015-07-06 16:03       ` John Kitchin
  0 siblings, 0 replies; 5+ messages in thread
From: John Kitchin @ 2015-07-06 16:03 UTC (permalink / raw)
  To: Nicolas Goaziou; +Cc: Thorsten Jolitz, emacs-orgmode

beautiful! Thanks!

Nicolas Goaziou writes:

> Hello,
>
> John Kitchin <jkitchin@andrew.cmu.edu> writes:
>
>> hm. We must have different use cases in mind. Currently I am using
>>
>> (save-excursion
>>                       (goto-char
>>                        (org-element-property :begin headline))
>>                       (org-entry-properties))
>>
>> to get the properties. I thought there would be a way to get those from
>> the parse tree to avoid the excursion (I use this to index a few
>> thousand org-files so I want it to be fast).
>
> If you have the full parse tree and you're only interested in local
> properties (i.e., neither CATEGORY or special properties):
>
>   (org-element-map headline 'node-property
>     (lambda (p)
>       (cons (org-element-property :key p)
>             (org-element-property :value p))))
>
>
> Regards,

--
Professor John Kitchin
Doherty Hall A207F
Department of Chemical Engineering
Carnegie Mellon University
Pittsburgh, PA 15213
412-268-7803
@johnkitchin
http://kitchingroup.cheme.cmu.edu

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2015-07-06 16:03 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-07-05 17:33 Getting heading properties from org-element John Kitchin
2015-07-05 19:15 ` Thorsten Jolitz
2015-07-06 15:23   ` John Kitchin
2015-07-06 15:36     ` Nicolas Goaziou
2015-07-06 16:03       ` John Kitchin

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).