emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Matt Price <moptop99@gmail.com>
To: John Kitchin <jkitchin@andrew.cmu.edu>, Org Mode <emacs-orgmode@gnu.org>
Subject: Re: creating new #+KEYWORD: variables
Date: Thu, 15 Nov 2018 07:15:16 -0500	[thread overview]
Message-ID: <CAN_Dec-cJTGQwZvmt=WsHNxbsoX53_Utn+g5G=96Q78-eBZEvA@mail.gmail.com> (raw)
In-Reply-To: <87r2fsbvz2.fsf@nicolasgoaziou.fr>

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

On Sat, Nov 10, 2018 at 1:58 PM Nicolas Goaziou <mail@nicolasgoaziou.fr>
wrote:

> Hello,
>
> John Kitchin <jkitchin@andrew.cmu.edu> writes:
>
> > You can retrieve keywords in the org-file like this:
> >
> > (defun get-keyword (key)
> >   (org-element-map (org-element-parse-buffer) 'keyword
> >     (lambda (k)
> >       (when (string= key (org-element-property :key k))
> > (org-element-property :value k)))
> >     nil t))
>
> As a minor addendum,
>
>     (org-element-parse-buffer 'element)
>
> is more efficient in this case.
>
> An even more efficient way to retrieve keywords, assuming buffer is not
> already parsed, would be:
>
>     (org-with-point-at 1
>       (let ((case-fold-search t)
>             (regexp (format "^[ \t]*#\\+%s:" key))
>             (result nil))
>         (while (re-search-forward regexp nil t)
>           (let ((element (org-element-at-point)))
>             (when (eq 'keyword (org-element-type element))
>               (push (org-element-property :value element) result))))
>         result))
>
> This is very helpful, Nicolas and John.

What about *setting* a global keyword? I would like to write something like
this:

(defun org-lms-set-global-prop-value (key value)
  "Add or update keyword KEY in the org file header."
  (save-excursion
    (goto-char (point-min))
    (insert (format "#+%s: %" (upcase key) value))))

But
(a) insert the value at the *end* of the headers section, not the
beginning.
(b) preferably replace any existing values of the keyword rather than write
a whole new line.

(b) I guess could be achieved with something like

(replace-regexp (format "\(^[ \t]*#\\+%s: \)\(.*\)" key) (concat "\1"
value))
But what about (a)? I thought org already had a couple of functions that
performed this kind of serach but now I'm not so sure.




> Regards,
>
> --
> Nicolas Goaziou
>

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

  reply	other threads:[~2018-11-15 12:14 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-11-09  3:32 creating new #+KEYWORD: variables Matt Price
2018-11-09  9:29 ` Diego Zamboni
2018-11-10  2:24   ` Matt Price
2018-11-10  7:22 ` Nicolas Goaziou
2018-11-10 16:17   ` Matt Price
2018-11-10 17:11     ` John Kitchin
2018-11-10 18:58       ` Nicolas Goaziou
2018-11-15 12:15         ` Matt Price [this message]
2018-11-15 19:51           ` John Kitchin
2018-11-12  9:11   ` Diego Zamboni

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAN_Dec-cJTGQwZvmt=WsHNxbsoX53_Utn+g5G=96Q78-eBZEvA@mail.gmail.com' \
    --to=moptop99@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=jkitchin@andrew.cmu.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).