emacs-orgmode
[Top][All Lists]
Advanced

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

Re: [O] getting file properties


From: John Kitchin
Subject: Re: [O] getting file properties
Date: Sun, 5 May 2013 12:34:36 -0400

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 <address@hidden>
(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


reply via email to

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