emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* org-element-property syntax (turning strings into keyword symbols)
@ 2013-11-09  6:29 Matt Price
  2013-11-09  7:48 ` Nicolas Goaziou
  0 siblings, 1 reply; 3+ messages in thread
From: Matt Price @ 2013-11-09  6:29 UTC (permalink / raw)
  To: Org Mode

Hi,

I'm trying to write a query that will check to see if the current
element has any of several properties set; the properties are defined
in a defcustom so I don't know in advance which properties I'm
interested in.  So I have the following code which INSERTS properties:

(defcustom org-writers-room-properties '(("Synopsis" . "Put a short
summary here") ("Role in Book" . "Describe what you want this section
to accomplish") ("Characters" . "who is in this section"))
  "alist of properties to be inserted automatically on heading creation"
  :group 'org-writers-room
  :type 'alist)

(defun org-wr-main-heading-hook ()
  "Adds a properties drawer & populates it with several properties.
Intended to be used with org-insert-heading-hook, but is also
interactive."
  (interactive)
  (save-excursion
    (dolist (this-property org-writers-room-properties)
      (org-set-property (car this-property) (cdr this-property))
    )
    (org-flag-drawer 'nil)
    (select-window (window-with-name "guide"))
    (org-cycle-hide-drawers 'all)
    )
  )

This works fine.  Now when I come back to this buffer I want to check
whether any of the properties are actually there.  So I am trying
something like this:

(let ((hasprops nil))
  (dolist prop org-writers-room-properties
      (if (org-element-property (car prop) (org-element-at-point))
           (setq hasprops t))
      (if (hasprops)
          (etc.))

However this doesn't work because (1) the car of "prop" (which is in
fact the property name) is not necessarily capitalized and (2) the
"property" parameter of org-element-property is not a string, but a
"keyword symbol".  Somehow I have to turn my string into the
appropriate keyword symbol.  Does anyone know how to do this?

Thank you!

Matt

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

* Re: org-element-property syntax (turning strings into keyword symbols)
  2013-11-09  6:29 org-element-property syntax (turning strings into keyword symbols) Matt Price
@ 2013-11-09  7:48 ` Nicolas Goaziou
  2013-11-09 16:15   ` Matt Price
  0 siblings, 1 reply; 3+ messages in thread
From: Nicolas Goaziou @ 2013-11-09  7:48 UTC (permalink / raw)
  To: Matt Price; +Cc: Org Mode

Hello,

Matt Price <moptop99@gmail.com> writes:

> This works fine.  Now when I come back to this buffer I want to check
> whether any of the properties are actually there.  So I am trying
> something like this:
>
> (let ((hasprops nil))
>   (dolist prop org-writers-room-properties
>       (if (org-element-property (car prop) (org-element-at-point))
>            (setq hasprops t))
>       (if (hasprops)
>           (etc.))
>
> However this doesn't work because (1) the car of "prop" (which is in
> fact the property name) is not necessarily capitalized and

Then `upcase' the property name first. I assume you will only refer to
user-defined properties so their equivalent keyword will always be in
upper cases.

> (2) the "property" parameter of org-element-property is not a string,
> but a "keyword symbol". Somehow I have to turn my string into the
> appropriate keyword symbol. Does anyone know how to do this?

Use `intern'.

For efficiency reasons, I also suggest to store `org-element-at-point'
in a variable instead of computing it again each time you are looking
for a property:

  (let ((hashprops nil)
        (element (org-element-at-point)))
    (dolist (prop org-writers-room-properties)
      (if (org-element-property (intern (concat ":" (upcase prop))) element)
          ...)))


Regards,

-- 
Nicolas Goaziou

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

* Re: org-element-property syntax (turning strings into keyword symbols)
  2013-11-09  7:48 ` Nicolas Goaziou
@ 2013-11-09 16:15   ` Matt Price
  0 siblings, 0 replies; 3+ messages in thread
From: Matt Price @ 2013-11-09 16:15 UTC (permalink / raw)
  To: Nicolas Goaziou; +Cc: Org Mode

On Sat, Nov 9, 2013 at 2:48 AM, Nicolas Goaziou <n.goaziou@gmail.com> wrote:
> Hello,
>
> Matt Price <moptop99@gmail.com> writes:
>
>> This works fine.  Now when I come back to this buffer I want to check
>> whether any of the properties are actually there.  So I am trying
>> something like this:
>>
>> (let ((hasprops nil))
>>   (dolist prop org-writers-room-properties
>>       (if (org-element-property (car prop) (org-element-at-point))
>>            (setq hasprops t))
>>       (if (hasprops)
>>           (etc.))
>>
>> However this doesn't work because (1) the car of "prop" (which is in
>> fact the property name) is not necessarily capitalized and
>
> Then `upcase' the property name first. I assume you will only refer to
> user-defined properties so their equivalent keyword will always be in
> upper cases.
>
>> (2) the "property" parameter of org-element-property is not a string,
>> but a "keyword symbol". Somehow I have to turn my string into the
>> appropriate keyword symbol. Does anyone know how to do this?
>
> Use `intern'.
>
> For efficiency reasons, I also suggest to store `org-element-at-point'
> in a variable instead of computing it again each time you are looking
> for a property:
>
>   (let ((hashprops nil)
>         (element (org-element-at-point)))
>     (dolist (prop org-writers-room-properties)
>       (if (org-element-property (intern (concat ":" (upcase prop))) element)
>           ...)))
>
ah, thank you so much for this. I don't know how I would ever have
found intern on my own.  makes my life much easier!

>
> Regards,
>
> --
> Nicolas Goaziou

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

end of thread, other threads:[~2013-11-09 16:15 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-11-09  6:29 org-element-property syntax (turning strings into keyword symbols) Matt Price
2013-11-09  7:48 ` Nicolas Goaziou
2013-11-09 16:15   ` Matt Price

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