emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Re: getting file properties
@ 2013-05-05 16:34 John Kitchin
  0 siblings, 0 replies; 6+ messages in thread
From: John Kitchin @ 2013-05-05 16:34 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 1577 bytes --]

Thanks for the great suggestions (Nick, Nick, and Eric)!

Here are the two functions that finally do what I wanted. I added the
second function to get a specific result.

#+RANDOM: tfjkdsla jfkdsa

#+BEGIN_SRC emacs-lisp :results value
; suggested by Nicolas Goaziou <n.goaziou@gmail.com>
(defun jk-org-kwds ()
  (org-element-map (org-element-parse-buffer 'element) 'keyword
           (lambda (keyword) (cons (org-element-property :key keyword)
                       (org-element-property :value keyword)))))

(defun jk-org-kwd (KEYWORD)
  "get the value of a KEYWORD in the form of #+KEYWORD: value"
  (cdr (assoc KEYWORD (jk-org-kwds))))

(jk-org-kwd "RANDOM")
#+END_SRC

#+RESULTS:
: tfjkdsla jfkdsa

A less orgish way I worked out last night after browsing through org.el is:

#+ANDREWID: jkitchin

#+BEGIN_SRC emacs-lisp :results value
(defun jk-get-file-keyword (KEYWORD)
  "get the value from a line like this
#+OPTION: value
in a file."
  (interactive)
  (let ((case-fold-search t)
    (re (format "^#\\+%s:[ \t]+\\([^\t\n]+\\)" KEYWORD)))
    (if (not (save-excursion
           (or (re-search-forward re nil t)
           (re-search-backward re nil t))))
    (error (format "No line containing #+%s: value found" KEYWORD)))
    (match-string 1)))

(jk-get-file-keyword "ANDREWID")
#+END_SRC

#+RESULTS:
: jkitchin


This is pretty awesome!

John

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

[-- Attachment #2: Type: text/html, Size: 1986 bytes --]

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

* Re: getting file properties
  2013-05-05  4:36   ` Nick Dokos
@ 2013-05-05  7:36     ` Nicolas Goaziou
  0 siblings, 0 replies; 6+ messages in thread
From: Nicolas Goaziou @ 2013-05-05  7:36 UTC (permalink / raw)
  To: Nick Dokos; +Cc: emacs-orgmode

Hello,

Nick Dokos <ndokos@gmail.com> writes:

> Here's one way:
>
> (defun jk-org-kwds ()
>   (let* ((parse-tree (org-element-parse-buffer))
> 	 (keys (org-element-map parse-tree 'keyword (function identity))))
>     (mapcar (function  (lambda (x) (cons (org-element-property :key x)
> 					 (org-element-property :value x)))) keys)))
>
> This returns a list of (KEY. VALUE) pairs.
>
> I'm sure there are other (better, cheaper) ways, but I'm going through
> org-element.el, and having that hammer in hand, everything looks like
> a nail to me :-)

I suggest the slightly more efficient variation:

  (defun jk-org-kwds ()
    (org-element-map (org-element-parse-buffer 'element) 'keyword
      (lambda (keyword) (cons (org-element-property :key keyword)
                         (org-element-property :value keyword)))))

The optional argument `element' prevents `org-element-parse-buffer' from
diving too deep and `org-element-map' does the job of `mapcar'.


Regards,

-- 
Nicolas Goaziou

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

* Re: getting file properties
  2013-05-04 21:42 John Kitchin
  2013-05-05  1:51 ` J. David Boyd
@ 2013-05-05  5:11 ` Eric Abrahamsen
  1 sibling, 0 replies; 6+ messages in thread
From: Eric Abrahamsen @ 2013-05-05  5:11 UTC (permalink / raw)
  To: emacs-orgmode

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

> Hi everyone,
>
> if I have an org-file with this in it:
>
> #+EMAIL: jkitchin@cmu.edu
>
> is there an org lisp command to get the email address after I have
> opened the file? I am looping through many files to generate a report,
> and would like to do this in emacs-lisp. I had hoped org-entry-get
> would do it, but it does not (it will retrieve it from #+PROPERTY:
> EMAIL jkitchin@cmu.edu).
>
> Is it possible to define other keywords similar to that? E.g.
>
> #+ANDREWID: jkitchin
>
> and then to be able to read them from a lisp command (without writing
> the parsing code myself)? something like (org-entry-get-keyword
> "ANDREWID"). 
>
> I could not find anything like this in the property API
> (http://orgmode.org/manual/Using-the-property-API.html) which is where
> i thought it would be.
>
> Thanks!
>
> John

If you mean file-level properties (ie not properties on headlines),
`org-export-get-environment' is one place to look. It runs
`org-export--get-inbuffer-options', which parses options at the top of
the file. I don't think it will read arbitrary properties, though...

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

* Re: getting file properties
  2013-05-05  1:51 ` J. David Boyd
@ 2013-05-05  4:36   ` Nick Dokos
  2013-05-05  7:36     ` Nicolas Goaziou
  0 siblings, 1 reply; 6+ messages in thread
From: Nick Dokos @ 2013-05-05  4:36 UTC (permalink / raw)
  To: emacs-orgmode

david@adboyd.com (J. David Boyd) writes:

> John Kitchin <jkitchin@andrew.cmu.edu> writes:
>
>> Hi everyone,
>>
>> if I have an org-file with this in it:
>>
>> #+EMAIL: jkitchin@cmu.edu
>>
>> is there an org lisp command to get the email address after I have
>> opened the file? I am looping through many files to generate a report,
>> and would like to do this in emacs-lisp. I had hoped org-entry-get
>> would do it, but it does not (it will retrieve it from #+PROPERTY:
>> EMAIL jkitchin@cmu.edu).
>>
>> Is it possible to define other keywords similar to that? E.g.
>>
>> #+ANDREWID: jkitchin
>>
>> and then to be able to read them from a lisp command (without writing
>> the parsing code myself)? something like (org-entry-get-keyword
>> "ANDREWID"). 
>>
>> I could not find anything like this in the property API
>> (http://orgmode.org/manual/Using-the-property-API.html) which is where
>> i thought it would be.
>>
>> Thanks!
>>
>> John
>>
>> -----------------------------------
>> John Kitchin
>> Associate Professor
>> Doherty Hall A207F
>> Department of Chemical Engineering
>> Carnegie Mellon University
>> Pittsburgh, PA 15213
>> 412-268-7803
>> http://kitchingroup.cheme.cmu.edu
>
>
> I'm interested in an answer to this as well...
>
>
>

Here's one way:

--8<---------------cut here---------------start------------->8---
(defun jk-org-kwds ()
  (let* ((parse-tree (org-element-parse-buffer))
	 (keys (org-element-map parse-tree 'keyword (function identity))))
    (mapcar (function  (lambda (x) (cons (org-element-property :key x)
					 (org-element-property :value x)))) keys)))
--8<---------------cut here---------------end--------------->8---

This returns a list of (KEY. VALUE) pairs.

I'm sure there are other (better, cheaper) ways, but I'm going through
org-element.el, and having that hammer in hand, everything looks like
a nail to me :-)

-- 
Nick

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

* Re: getting file properties
  2013-05-04 21:42 John Kitchin
@ 2013-05-05  1:51 ` J. David Boyd
  2013-05-05  4:36   ` Nick Dokos
  2013-05-05  5:11 ` Eric Abrahamsen
  1 sibling, 1 reply; 6+ messages in thread
From: J. David Boyd @ 2013-05-05  1:51 UTC (permalink / raw)
  To: emacs-orgmode

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

> Hi everyone,
>
> if I have an org-file with this in it:
>
> #+EMAIL: jkitchin@cmu.edu
>
> is there an org lisp command to get the email address after I have
> opened the file? I am looping through many files to generate a report,
> and would like to do this in emacs-lisp. I had hoped org-entry-get
> would do it, but it does not (it will retrieve it from #+PROPERTY:
> EMAIL jkitchin@cmu.edu).
>
> Is it possible to define other keywords similar to that? E.g.
>
> #+ANDREWID: jkitchin
>
> and then to be able to read them from a lisp command (without writing
> the parsing code myself)? something like (org-entry-get-keyword
> "ANDREWID"). 
>
> I could not find anything like this in the property API
> (http://orgmode.org/manual/Using-the-property-API.html) which is where
> i thought it would be.
>
> Thanks!
>
> John
>
> -----------------------------------
> John Kitchin
> Associate Professor
> Doherty Hall A207F
> Department of Chemical Engineering
> Carnegie Mellon University
> Pittsburgh, PA 15213
> 412-268-7803
> http://kitchingroup.cheme.cmu.edu


I'm interested in an answer to this as well...

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

* getting file properties
@ 2013-05-04 21:42 John Kitchin
  2013-05-05  1:51 ` J. David Boyd
  2013-05-05  5:11 ` Eric Abrahamsen
  0 siblings, 2 replies; 6+ messages in thread
From: John Kitchin @ 2013-05-04 21:42 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 997 bytes --]

Hi everyone,

if I have an org-file with this in it:

#+EMAIL: jkitchin@cmu.edu

is there an org lisp command to get the email address after I have opened
the file? I am looping through many files to generate a report, and would
like to do this in emacs-lisp. I had hoped org-entry-get would do it, but
it does not (it will retrieve it from #+PROPERTY: EMAIL jkitchin@cmu.edu).

Is it possible to define other keywords similar to that? E.g.

#+ANDREWID: jkitchin

and then to be able to read them from a lisp command (without writing the
parsing code myself)? something like (org-entry-get-keyword "ANDREWID").

I could not find anything like this in the property API (
http://orgmode.org/manual/Using-the-property-API.html) which is where i
thought it would be.

Thanks!

John

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

[-- Attachment #2: Type: text/html, Size: 1339 bytes --]

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

end of thread, other threads:[~2013-05-05 16:34 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-05-05 16:34 getting file properties John Kitchin
  -- strict thread matches above, loose matches on Subject: below --
2013-05-04 21:42 John Kitchin
2013-05-05  1:51 ` J. David Boyd
2013-05-05  4:36   ` Nick Dokos
2013-05-05  7:36     ` Nicolas Goaziou
2013-05-05  5:11 ` Eric Abrahamsen

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).