From mboxrd@z Thu Jan 1 00:00:00 1970 From: John Kitchin Subject: Re: getting file properties Date: Sun, 5 May 2013 12:34:36 -0400 Message-ID: Mime-Version: 1.0 Content-Type: multipart/alternative; boundary=e89a8ff24377f36de704dbfb28b7 Return-path: Received: from eggs.gnu.org ([208.118.235.92]:47187) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UZ1tZ-0008QG-Bp for emacs-orgmode@gnu.org; Sun, 05 May 2013 12:34:44 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UZ1tW-0001iU-Lr for emacs-orgmode@gnu.org; Sun, 05 May 2013 12:34:41 -0400 Received: from mail-da0-x236.google.com ([2607:f8b0:400e:c00::236]:44076) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UZ1tW-0001iG-EY for emacs-orgmode@gnu.org; Sun, 05 May 2013 12:34:38 -0400 Received: by mail-da0-f54.google.com with SMTP id u36so1479246dak.41 for ; Sun, 05 May 2013 09:34:37 -0700 (PDT) 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 --e89a8ff24377f36de704dbfb28b7 Content-Type: text/plain; charset=ISO-8859-1 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 (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 --e89a8ff24377f36de704dbfb28b7 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Thanks for the great suggestions (Nick, Nick, and Eric)!

Here are t= he 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 ()
=A0 (org-element-map= (org-element-parse-buffer 'element) 'keyword
=A0=A0=A0 =A0=A0= =A0 =A0=A0 (lambda (keyword) (cons (org-element-property :key keyword)
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0 (org-element-prope= rty :value keyword)))))

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

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

#+RESULTS:
: tfj= kdsla jfkdsa

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

#+ANDREWID: jkitchin

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

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

#+RESULT= S:
: jkitchin


This is pretty awesome!

John

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

--e89a8ff24377f36de704dbfb28b7--