all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Concatenating Org property values from parent subtrees
@ 2018-09-26 22:57 Kaushal Modi
  2018-09-29 18:38 ` Michael Welle
  0 siblings, 1 reply; 7+ messages in thread
From: Kaushal Modi @ 2018-09-26 22:57 UTC (permalink / raw)
  To: emacs-org list

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

Hello,

Is there a way to achieve something like below? See the content in each
nested subtree in the example below.



#+title: Concatenating property values from parent subtrees

* Section
:PROPERTIES
:EXPORT_XYZ: a
:END:
At this point, the value of XYZ property should be "a".
** Sub-section
:PROPERTIES
:EXPORT_XYZ: b
:END:
At this point, the value of XYZ property should be "ab".
*** Sub-sub-section
:PROPERTIES
:EXPORT_XYZ: c
:END:
At this point, the value of XYZ property should be "abc".

--
Kaushal Modi

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

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

* Re: Concatenating Org property values from parent subtrees
  2018-09-26 22:57 Concatenating Org property values from parent subtrees Kaushal Modi
@ 2018-09-29 18:38 ` Michael Welle
  2018-09-29 19:01   ` Kaushal Modi
  0 siblings, 1 reply; 7+ messages in thread
From: Michael Welle @ 2018-09-29 18:38 UTC (permalink / raw)
  To: emacs-orgmode

Hello,

Kaushal Modi <kaushal.modi@gmail.com> writes:

> Hello,
>
> Is there a way to achieve something like below? See the content in each
> nested subtree in the example below.
I asked something similar earlier this year (concatenating compiler
flags given as header-args property, used for linking against different
libs in different sections of the Org file). I ended with a function
that grabs the current property value and returns the value concatenated
with new value. That function can be used as a 'property value'. That's
not a nice and bullet proof solution, but works good enough to me to
generate the solutions to the psets for the lecture.

Regards
hmw

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

* Re: Concatenating Org property values from parent subtrees
  2018-09-29 18:38 ` Michael Welle
@ 2018-09-29 19:01   ` Kaushal Modi
  2018-09-29 19:23     ` Michael Welle
  0 siblings, 1 reply; 7+ messages in thread
From: Kaushal Modi @ 2018-09-29 19:01 UTC (permalink / raw)
  To: mwe012008; +Cc: emacs-org list

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

On Sat, Sep 29, 2018 at 2:39 PM Michael Welle <mwe012008@gmx.net> wrote:

>
> I asked something similar earlier this year (concatenating compiler
> flags given as header-args property, used for linking against different
> libs in different sections of the Org file). I ended with a function
> that grabs the current property value and returns the value concatenated
> with new value. That function can be used as a 'property value'. That's
> not a nice and bullet proof solution, but works good enough to me to
> generate the solutions to the psets for the lecture.
>

Please share it if you don't mind. I plan to use it or its derivative in
ox-hugo. The property is planned to be a path property, and with nested
property values of "a","b" and "c", which I want to parse as "a/b/c".

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

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

* Re: Concatenating Org property values from parent subtrees
  2018-09-29 19:01   ` Kaushal Modi
@ 2018-09-29 19:23     ` Michael Welle
  2018-09-29 19:26       ` Michael Welle
  0 siblings, 1 reply; 7+ messages in thread
From: Michael Welle @ 2018-09-29 19:23 UTC (permalink / raw)
  To: emacs-orgmode

Hello,


Kaushal Modi <kaushal.modi@gmail.com> writes:

> On Sat, Sep 29, 2018 at 2:39 PM Michael Welle <mwe012008@gmx.net> wrote:
>
>>
>> I asked something similar earlier this year (concatenating compiler
>> flags given as header-args property, used for linking against different
>> libs in different sections of the Org file). I ended with a function
>> that grabs the current property value and returns the value concatenated
>> with new value. That function can be used as a 'property value'. That's
>> not a nice and bullet proof solution, but works good enough to me to
>> generate the solutions to the psets for the lecture.
>>
>
> Please share it if you don't mind. I plan to use it or its derivative in
> ox-hugo. The property is planned to be a path property, and with nested
> property values of "a","b" and "c", which I want to parse as "a/b/c".


(defun hmw/org-prop-append(prop value)
  (save-excursion
  (org-up-heading-safe)
  (format "%s %s" value (cdr (assq prop
                                   (car (org-babel-params-from-properties)))))))

(defalias 'A 'hmw/org-prop-append)


I use it like this:


* foo
 :PROPERTIES:
 :header-args: :flags -Wall
 :END:

** bar
#+begin_src C :flags (A :flags "-lm")
#+end_src

** baz
 :PROPERTIES:
 :header-args: :flags (A :flags "-lcunit")
 :END:

#+begin_src C
#+end_src


Regards
hmw

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

* Re: Concatenating Org property values from parent subtrees
  2018-09-29 19:23     ` Michael Welle
@ 2018-09-29 19:26       ` Michael Welle
  2018-10-01 15:47         ` Ihor Radchenko
  0 siblings, 1 reply; 7+ messages in thread
From: Michael Welle @ 2018-09-29 19:26 UTC (permalink / raw)
  To: emacs-orgmode

Hello,

Michael Welle <mwe012008@gmx.net> writes:
[...]
> (defun hmw/org-prop-append(prop value)
>   (save-excursion
>   (org-up-heading-safe)
>   (format "%s %s" value (cdr (assq prop
>                                    (car (org-babel-params-from-properties)))))))
>
> (defalias 'A 'hmw/org-prop-append)

and I just realise that it works with code blocks only. I guess the way
to get general property values has to be adapted.

Regards
hmw

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

* Re: Concatenating Org property values from parent subtrees
  2018-09-29 19:26       ` Michael Welle
@ 2018-10-01 15:47         ` Ihor Radchenko
  2018-10-01 21:00           ` Kaushal Modi
  0 siblings, 1 reply; 7+ messages in thread
From: Ihor Radchenko @ 2018-10-01 15:47 UTC (permalink / raw)
  To: Michael Welle, emacs-orgmode

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

Hi,

Check out the following code:

````
(defvar org-concatenated-properties '("AA")
  "A list of property names (strings), which should be computed via concatenation with the parent properties.")

(define-advice org-entry-get (:around (oldfun pom property &optional inherit literal-nil) concatenate-parents-maybe)
  "Concatenate the PROPERTY value with its parent entries' values if the PROPERTY is in `org-concatenated-properties' list."
  (if (not (member property org-concatenated-properties))
      (apply oldfun pom property inherit literal-nil)
    (let ((value-here (funcall oldfun pom property nil 't))
	  (value (funcall oldfun pom property inherit 't)))
      (if value-here
	  (format "%s%s"  (org-with-wide-buffer
			   (if (org-up-heading-safe)
                               (or (org-entry-get nil property inherit literal-nil) "")
                             ""))
		  (funcall oldfun pom property inherit literal-nil))
	(when value
	  (org-with-wide-buffer
           (org-up-heading-safe)
	   (org-entry-get nil property inherit literal-nil)))))))
````

Best,
Ihor


Michael Welle <mwe012008@gmx.net> writes:

> Hello,
>
> Michael Welle <mwe012008@gmx.net> writes:
> [...]
>> (defun hmw/org-prop-append(prop value)
>>   (save-excursion
>>   (org-up-heading-safe)
>>   (format "%s %s" value (cdr (assq prop
>>                                    (car (org-babel-params-from-properties)))))))
>>
>> (defalias 'A 'hmw/org-prop-append)
>
> and I just realise that it works with code blocks only. I guess the way
> to get general property values has to be adapted.
>
> Regards
> hmw
>


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 487 bytes --]

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

* Re: Concatenating Org property values from parent subtrees
  2018-10-01 15:47         ` Ihor Radchenko
@ 2018-10-01 21:00           ` Kaushal Modi
  0 siblings, 0 replies; 7+ messages in thread
From: Kaushal Modi @ 2018-10-01 21:00 UTC (permalink / raw)
  To: yantar92; +Cc: emacs-org list, mwe012008

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

On Mon, Oct 1, 2018 at 11:50 AM Ihor Radchenko <yantar92@gmail.com> wrote:

> Hi,
>
> Check out the following code:
>
> ````
> (defvar org-concatenated-properties '("AA")
>   "A list of property names (strings), which should be computed via
> concatenation with the parent properties.")
>
> (define-advice org-entry-get (:around (oldfun pom property &optional
> inherit literal-nil) concatenate-parents-maybe)
>

Hello Ihor,

That code is perfect!

I was able to get what I want with minor refactoring. Thanks!

Refactored code:

=====
(defvar org-concatenated-properties '("AA")
  "List of property names whose values are allowed to be concatenated.
The list is of type '(PROP1 PROP2 ..) where each element is a string.")

(defvar org-property-concat-string "/"
  "String use to concat the `org-concatenated-properties' properties.")

(defun org-get-parent-property (property inherit literal-nil)
  "Get the value of PROPERTY from the parent relative to current point."
  (org-with-wide-buffer
   (if (org-up-heading-safe)
       (or (org-entry-get nil property inherit literal-nil) "")
     "")))

(defun org/advice-concatenate-properties-maybe (orig-fun &rest args)
  "Concatenate an Org Property value with its inherited value.
The concatenation happens only if the Org Property is in
`org-concatenated-properties' list."
  (let* ((value-orig (apply orig-fun args))
         (property (nth 1 args))
         (dont-concat (not (member property org-concatenated-properties))))
    ;; (message "dbg: args:%S value-orig:%S property:%S" args value-orig
property)
    (if dont-concat
        value-orig
      (let* ((pom (nth 0 args))
             (inherit (nth 2 args))
             (literal-nil (nth 3 args))
             (value-here-no-inherit (apply orig-fun `(,pom ,property nil
,literal-nil)))
             (value-parent (apply #'org-get-parent-property `(,property
,inherit ,literal-nil))))
        ;; (message "dbg advice: value-here-no-inherit: %S"
value-here-no-inherit)
        (if value-here-no-inherit
            (format "%s%s%s"
                    value-parent
                    (if (org-string-nw-p value-parent)
                        org-property-concat-string
                      "")
                    value-orig)
          value-parent)))))
(advice-add 'org-entry-get :around
#'org/advice-concatenate-properties-maybe)
;; (advice-remove 'org-entry-get #'org/advice-concatenate-properties-maybe)
=====

Example Org file:

=====

* heading 1
:PROPERTIES:
:FOO:      abc
:END:

asdf
** heading 1
:PROPERTIES:
:FOO: def
:AA: pqr
:END:
*** heading 2
:PROPERTIES:
:FOO: 123
:AA: 456
:END:
**** heading 3
=====

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

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

end of thread, other threads:[~2018-10-01 21:01 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-09-26 22:57 Concatenating Org property values from parent subtrees Kaushal Modi
2018-09-29 18:38 ` Michael Welle
2018-09-29 19:01   ` Kaushal Modi
2018-09-29 19:23     ` Michael Welle
2018-09-29 19:26       ` Michael Welle
2018-10-01 15:47         ` Ihor Radchenko
2018-10-01 21:00           ` Kaushal Modi

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.