Re: [O] Concatenating Org property values from parent subtrees

2018-10-01 Thread Kaushal Modi
On Mon, Oct 1, 2018 at 11:50 AM Ihor Radchenko  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 
> 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  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
=


Re: [O] Concatenating Org property values from parent subtrees

2018-10-01 Thread Ihor Radchenko
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  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  writes:

> Hello,
>
> Michael Welle  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
>



signature.asc
Description: PGP signature


Re: [O] Concatenating Org property values from parent subtrees

2018-09-29 Thread Michael Welle
Hello,

Michael Welle  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



Re: [O] Concatenating Org property values from parent subtrees

2018-09-29 Thread Michael Welle
Hello,


Kaushal Modi  writes:

> On Sat, Sep 29, 2018 at 2:39 PM Michael Welle  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



Re: [O] Concatenating Org property values from parent subtrees

2018-09-29 Thread Kaushal Modi
On Sat, Sep 29, 2018 at 2:39 PM Michael Welle  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".


Re: [O] Concatenating Org property values from parent subtrees

2018-09-29 Thread Michael Welle
Hello,

Kaushal Modi  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



[O] Concatenating Org property values from parent subtrees

2018-09-26 Thread Kaushal Modi
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