all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Bug: [patch] Fix org-babel-result-to-file never expanding links when babel is evaluated in indirect buffer [9.3.7 (release_9.3.7-728-g1efc4e @ /home/yantar92/.emacs.d/straight/build/org/)]
@ 2020-07-31  8:49 Ihor Radchenko
  2020-07-31 17:24 ` John Herrlin
  2020-09-05 14:42 ` Bastien
  0 siblings, 2 replies; 9+ messages in thread
From: Ihor Radchenko @ 2020-07-31  8:49 UTC (permalink / raw)
  To: emacs-orgmode

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


Remember to cover the basics, that is, what you expected to happen and
what in fact did happen.  You don't know how to make a good report?  See

     https://orgmode.org/manual/Feedback.html#Feedback

Your bug report will be posted to the Org mailing list.
------------------------------------------------------------------------

I noticed that org-babel-result-to-file never expands file: links in
src block results when ran from indirect buffer and default-directory
is not same with base buffer file's directory. This is against the
docstring stating that

> If the `default-directory' is different from the containing
> file's directory then expand relative links.

The misbehavior happens because buffer-file-name variable is always
nil in indirect buffers. We need to call (file-name-directory
(buffer-file-name (buffer-base-buffer))) instead.

The patch is attached.

Best,
Ihor


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: org-babel-eval-indirect.patch --]
[-- Type: text/x-diff, Size: 1200 bytes --]

diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index f86282f0f..d37eef5f9 100644
--- a/lisp/ob-core.el
+++ b/lisp/ob-core.el
@@ -2561,16 +2561,16 @@ If the `default-directory' is different from the containing
 file's directory then expand relative links."
   (when (stringp result)
     (let ((same-directory?
-	   (and buffer-file-name
+	   (and (buffer-file-name (buffer-base-buffer))
 		(not (string= (expand-file-name default-directory)
-			      (expand-file-name
-			       (file-name-directory buffer-file-name)))))))
+			    (expand-file-name
+			     (file-name-directory (buffer-file-name (buffer-base-buffer)))))))))
       (format "[[file:%s]%s]"
-	      (if (and default-directory buffer-file-name same-directory?)
+	      (if (and default-directory (buffer-file-name (buffer-base-buffer)) same-directory?)
 		  (if (eq org-link-file-path-type 'adaptive)
 		      (file-relative-name
 		       (expand-file-name result default-directory)
-		       (file-name-directory (buffer-file-name)))
+		       (file-name-directory (buffer-file-name (buffer-base-buffer))))
 		    (expand-file-name result default-directory))
 		result)
 	      (if description (concat "[" description "]") "")))))

[-- Attachment #3: Type: text/plain, Size: 485 bytes --]


Emacs  : GNU Emacs 28.0.50 (build 4, x86_64-pc-linux-gnu, GTK+ Version 3.24.20, cairo version 1.16.0)
 of 2020-07-27
Package: Org mode version 9.3.7 (release_9.3.7-728-g1efc4e @ /home/yantar92/.emacs.d/straight/build/org/)

-- 
Ihor Radchenko,
PhD,
Center for Advancing Materials Performance from the Nanoscale (CAMP-nano)
State Key Laboratory for Mechanical Behavior of Materials, Xi'an Jiaotong University, Xi'an, China
Email: yantar92@gmail.com, ihor_radchenko@alumni.sutd.edu.sg


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

* Re: Bug: [patch] Fix org-babel-result-to-file never expanding links when babel is evaluated in indirect buffer [9.3.7 (release_9.3.7-728-g1efc4e @ /home/yantar92/.emacs.d/straight/build/org/)]
  2020-07-31  8:49 Bug: [patch] Fix org-babel-result-to-file never expanding links when babel is evaluated in indirect buffer [9.3.7 (release_9.3.7-728-g1efc4e @ /home/yantar92/.emacs.d/straight/build/org/)] Ihor Radchenko
@ 2020-07-31 17:24 ` John Herrlin
  2020-07-31 23:32   ` Ihor Radchenko
  2020-09-05 14:42 ` Bastien
  1 sibling, 1 reply; 9+ messages in thread
From: John Herrlin @ 2020-07-31 17:24 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: emacs-orgmode


Hey Ihor,

Could not get the patch to work.

I used this use case.

(with-current-buffer
  (find-file "/tmp/tmp.org")
  (insert "* A\n** b\n** c\n\n   some text")
  (dired "~/")
  (make-indirect-buffer "tmp.org" "tmp-indirect")
  (switch-to-buffer "tmp-indirect")
  (goto-char (point-max))
  (funcall 'org-mode)
  (insert (concat "\n\n| default-directory | " default-directory " |\n"
                  "| org-link-file-path-type | " (symbol-name org-link-file-path-type) " |\n"
                  "| (expand-file-name default-directory) | " (expand-file-name default-directory) " |\n"
                  "| (buffer-file-name (buffer-base-buffer)) | " (buffer-file-name (buffer-base-buffer)) " |\n"))
  (org-table-align)
  (insert "\n\n#+BEGIN_SRC shell :results file :file script.sh
       echo \"#!/bin/bash\"
       echo \"echo Hey\"\n#+END_SRC")
  (org-babel-execute-src-block))

In my case the result is a relative link, but if I follow the link I end
up in the wrong place.

src_emacs-lisp{emacs-version} {{{results(=26.3=)}}}
src_emacs-lisp{org-version} {{{results(=9.3.7=)}}}

Best regards
John


Ihor Radchenko <yantar92@gmail.com> writes:

> Remember to cover the basics, that is, what you expected to happen and
> what in fact did happen.  You don't know how to make a good report?  See
>
>      https://orgmode.org/manual/Feedback.html#Feedback
>
> Your bug report will be posted to the Org mailing list.
> ------------------------------------------------------------------------
>
> I noticed that org-babel-result-to-file never expands file: links in
> src block results when ran from indirect buffer and default-directory
> is not same with base buffer file's directory. This is against the
> docstring stating that
>
>> If the `default-directory' is different from the containing
>> file's directory then expand relative links.
>
> The misbehavior happens because buffer-file-name variable is always
> nil in indirect buffers. We need to call (file-name-directory
> (buffer-file-name (buffer-base-buffer))) instead.
>
> The patch is attached.
>
> Best,
> Ihor
>
> diff --git a/lisp/ob-core.el b/lisp/ob-core.el
> index f86282f0f..d37eef5f9 100644
> --- a/lisp/ob-core.el
> +++ b/lisp/ob-core.el
> @@ -2561,16 +2561,16 @@ If the `default-directory' is different from the containing
>  file's directory then expand relative links."
>    (when (stringp result)
>      (let ((same-directory?
> -	   (and buffer-file-name
> +	   (and (buffer-file-name (buffer-base-buffer))
>  		(not (string= (expand-file-name default-directory)
> -			      (expand-file-name
> -			       (file-name-directory buffer-file-name)))))))
> +			    (expand-file-name
> +			     (file-name-directory (buffer-file-name (buffer-base-buffer)))))))))
>        (format "[[file:%s]%s]"
> -	      (if (and default-directory buffer-file-name same-directory?)
> +	      (if (and default-directory (buffer-file-name (buffer-base-buffer)) same-directory?)
>  		  (if (eq org-link-file-path-type 'adaptive)
>  		      (file-relative-name
>  		       (expand-file-name result default-directory)
> -		       (file-name-directory (buffer-file-name)))
> +		       (file-name-directory (buffer-file-name (buffer-base-buffer))))
>  		    (expand-file-name result default-directory))
>  		result)
>  	      (if description (concat "[" description "]") "")))))
>
> Emacs  : GNU Emacs 28.0.50 (build 4, x86_64-pc-linux-gnu, GTK+ Version 3.24.20, cairo version 1.16.0)
>  of 2020-07-27
> Package: Org mode version 9.3.7 (release_9.3.7-728-g1efc4e @ /home/yantar92/.emacs.d/straight/build/org/)


--
Mvh John


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

* Re: Bug: [patch] Fix org-babel-result-to-file never expanding links when babel is evaluated in indirect buffer [9.3.7 (release_9.3.7-728-g1efc4e @ /home/yantar92/.emacs.d/straight/build/org/)]
  2020-07-31 17:24 ` John Herrlin
@ 2020-07-31 23:32   ` Ihor Radchenko
  2020-08-01 16:33     ` John Herrlin
  0 siblings, 1 reply; 9+ messages in thread
From: Ihor Radchenko @ 2020-07-31 23:32 UTC (permalink / raw)
  To: John Herrlin; +Cc: emacs-orgmode

> In my case the result is a relative link, but if I follow the link I end
> up in the wrong place.

Do you mean that the generated script.sh ends up in your home dir?
It is expected since default-directory in your buffer is "~/":

>   (dired "~/")
>   (make-indirect-buffer "tmp.org" "tmp-indirect")

Note that you called make-indirect-buffer without third argument.

According to manual:

> (make-indirect-buffer BASE-BUFFER NAME &optional CLONE)
> CLONE nil means the indirect buffer's state is reset to default values.

>     Org first tries to generate the filename from the value of the
>     ‘file’ header argument and the directory specified using the
>     ‘output-dir’ header arguments.  If ‘output-dir’ is not specified,
>     Org assumes it is the current directory.

No surprise you got your file in the default-directory (~/).

If you use (make-indirect-buffer "tmp.org" "tmp-indirect" t) instead,
the script.sh link will be relative, as expected.

Let me know if I miss something.

Best,
Ihor


> Hey Ihor,
>
> Could not get the patch to work.
>
> I used this use case.
>
> (with-current-buffer
>   (find-file "/tmp/tmp.org")
>   (insert "* A\n** b\n** c\n\n   some text")
>   (dired "~/")
>   (make-indirect-buffer "tmp.org" "tmp-indirect")
>   (switch-to-buffer "tmp-indirect")
>   (goto-char (point-max))
>   (funcall 'org-mode)
>   (insert (concat "\n\n| default-directory | " default-directory " |\n"
>                   "| org-link-file-path-type | " (symbol-name org-link-file-path-type) " |\n"
>                   "| (expand-file-name default-directory) | " (expand-file-name default-directory) " |\n"
>                   "| (buffer-file-name (buffer-base-buffer)) | " (buffer-file-name (buffer-base-buffer)) " |\n"))
>   (org-table-align)
>   (insert "\n\n#+BEGIN_SRC shell :results file :file script.sh
>        echo \"#!/bin/bash\"
>        echo \"echo Hey\"\n#+END_SRC")
>   (org-babel-execute-src-block))
>
> In my case the result is a relative link, but if I follow the link I end
> up in the wrong place.
>
> src_emacs-lisp{emacs-version} {{{results(=26.3=)}}}
> src_emacs-lisp{org-version} {{{results(=9.3.7=)}}}
>
> Best regards
> John
>
>
> Ihor Radchenko <yantar92@gmail.com> writes:
>
>> Remember to cover the basics, that is, what you expected to happen and
>> what in fact did happen.  You don't know how to make a good report?  See
>>
>>      https://orgmode.org/manual/Feedback.html#Feedback
>>
>> Your bug report will be posted to the Org mailing list.
>> ------------------------------------------------------------------------
>>
>> I noticed that org-babel-result-to-file never expands file: links in
>> src block results when ran from indirect buffer and default-directory
>> is not same with base buffer file's directory. This is against the
>> docstring stating that
>>
>>> If the `default-directory' is different from the containing
>>> file's directory then expand relative links.
>>
>> The misbehavior happens because buffer-file-name variable is always
>> nil in indirect buffers. We need to call (file-name-directory
>> (buffer-file-name (buffer-base-buffer))) instead.
>>
>> The patch is attached.
>>
>> Best,
>> Ihor
>>
>> diff --git a/lisp/ob-core.el b/lisp/ob-core.el
>> index f86282f0f..d37eef5f9 100644
>> --- a/lisp/ob-core.el
>> +++ b/lisp/ob-core.el
>> @@ -2561,16 +2561,16 @@ If the `default-directory' is different from the containing
>>  file's directory then expand relative links."
>>    (when (stringp result)
>>      (let ((same-directory?
>> -	   (and buffer-file-name
>> +	   (and (buffer-file-name (buffer-base-buffer))
>>  		(not (string= (expand-file-name default-directory)
>> -			      (expand-file-name
>> -			       (file-name-directory buffer-file-name)))))))
>> +			    (expand-file-name
>> +			     (file-name-directory (buffer-file-name (buffer-base-buffer)))))))))
>>        (format "[[file:%s]%s]"
>> -	      (if (and default-directory buffer-file-name same-directory?)
>> +	      (if (and default-directory (buffer-file-name (buffer-base-buffer)) same-directory?)
>>  		  (if (eq org-link-file-path-type 'adaptive)
>>  		      (file-relative-name
>>  		       (expand-file-name result default-directory)
>> -		       (file-name-directory (buffer-file-name)))
>> +		       (file-name-directory (buffer-file-name (buffer-base-buffer))))
>>  		    (expand-file-name result default-directory))
>>  		result)
>>  	      (if description (concat "[" description "]") "")))))
>>
>> Emacs  : GNU Emacs 28.0.50 (build 4, x86_64-pc-linux-gnu, GTK+ Version 3.24.20, cairo version 1.16.0)
>>  of 2020-07-27
>> Package: Org mode version 9.3.7 (release_9.3.7-728-g1efc4e @ /home/yantar92/.emacs.d/straight/build/org/)
>
>
> --
> Mvh John

-- 
Ihor Radchenko,
PhD,
Center for Advancing Materials Performance from the Nanoscale (CAMP-nano)
State Key Laboratory for Mechanical Behavior of Materials, Xi'an Jiaotong University, Xi'an, China
Email: yantar92@gmail.com, ihor_radchenko@alumni.sutd.edu.sg


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

* Re: Bug: [patch] Fix org-babel-result-to-file never expanding links when babel is evaluated in indirect buffer [9.3.7 (release_9.3.7-728-g1efc4e @ /home/yantar92/.emacs.d/straight/build/org/)]
  2020-07-31 23:32   ` Ihor Radchenko
@ 2020-08-01 16:33     ` John Herrlin
  2020-08-02  4:36       ` Ihor Radchenko
  0 siblings, 1 reply; 9+ messages in thread
From: John Herrlin @ 2020-08-01 16:33 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: emacs-orgmode


It's intended to change directory and dont clone base buffer values. I
tried to accomplish "If the `default-directory' is different from the
containing file's directory then expand relative links." As I
understands it that's when it inserts a relative link, otherwise it just
`result'. I was looking from the indirect buffers point of view and from
there the relative link is off in this case. But from the base buffers
point of view everything works fine. I guess that it was my use case
that is strange.

Looks good!

Best regards
John

>> In my case the result is a relative link, but if I follow the link I end
>> up in the wrong place.
>
> Do you mean that the generated script.sh ends up in your home dir?
> It is expected since default-directory in your buffer is "~/":
>
>>   (dired "~/")
>>   (make-indirect-buffer "tmp.org" "tmp-indirect")
>
> Note that you called make-indirect-buffer without third argument.
>
> According to manual:
>
>> (make-indirect-buffer BASE-BUFFER NAME &optional CLONE)
>> CLONE nil means the indirect buffer's state is reset to default values.
>
>>     Org first tries to generate the filename from the value of the
>>     ‘file’ header argument and the directory specified using the
>>     ‘output-dir’ header arguments.  If ‘output-dir’ is not specified,
>>     Org assumes it is the current directory.
>
> No surprise you got your file in the default-directory (~/).
>
> If you use (make-indirect-buffer "tmp.org" "tmp-indirect" t) instead,
> the script.sh link will be relative, as expected.
>
> Let me know if I miss something.
>
> Best,
> Ihor
>
>
>> Hey Ihor,
>>
>> Could not get the patch to work.
>>
>> I used this use case.
>>
>> (with-current-buffer
>>   (find-file "/tmp/tmp.org")
>>   (insert "* A\n** b\n** c\n\n   some text")
>>   (dired "~/")
>>   (make-indirect-buffer "tmp.org" "tmp-indirect")
>>   (switch-to-buffer "tmp-indirect")
>>   (goto-char (point-max))
>>   (funcall 'org-mode)
>>   (insert (concat "\n\n| default-directory | " default-directory " |\n"
>>                   "| org-link-file-path-type | " (symbol-name org-link-file-path-type) " |\n"
>>                   "| (expand-file-name default-directory) | " (expand-file-name default-directory) " |\n"
>>                   "| (buffer-file-name (buffer-base-buffer)) | " (buffer-file-name (buffer-base-buffer)) " |\n"))
>>   (org-table-align)
>>   (insert "\n\n#+BEGIN_SRC shell :results file :file script.sh
>>        echo \"#!/bin/bash\"
>>        echo \"echo Hey\"\n#+END_SRC")
>>   (org-babel-execute-src-block))
>>
>> In my case the result is a relative link, but if I follow the link I end
>> up in the wrong place.
>>
>> src_emacs-lisp{emacs-version} {{{results(=26.3=)}}}
>> src_emacs-lisp{org-version} {{{results(=9.3.7=)}}}
>>
>> Best regards
>> John
>>
>>
>> Ihor Radchenko <yantar92@gmail.com> writes:
>>
>>> Remember to cover the basics, that is, what you expected to happen and
>>> what in fact did happen.  You don't know how to make a good report?  See
>>>
>>>      https://orgmode.org/manual/Feedback.html#Feedback
>>>
>>> Your bug report will be posted to the Org mailing list.
>>> ------------------------------------------------------------------------
>>>
>>> I noticed that org-babel-result-to-file never expands file: links in
>>> src block results when ran from indirect buffer and default-directory
>>> is not same with base buffer file's directory. This is against the
>>> docstring stating that
>>>
>>>> If the `default-directory' is different from the containing
>>>> file's directory then expand relative links.
>>>
>>> The misbehavior happens because buffer-file-name variable is always
>>> nil in indirect buffers. We need to call (file-name-directory
>>> (buffer-file-name (buffer-base-buffer))) instead.
>>>
>>> The patch is attached.
>>>
>>> Best,
>>> Ihor
>>>
>>> diff --git a/lisp/ob-core.el b/lisp/ob-core.el
>>> index f86282f0f..d37eef5f9 100644
>>> --- a/lisp/ob-core.el
>>> +++ b/lisp/ob-core.el
>>> @@ -2561,16 +2561,16 @@ If the `default-directory' is different from the containing
>>>  file's directory then expand relative links."
>>>    (when (stringp result)
>>>      (let ((same-directory?
>>> -	   (and buffer-file-name
>>> +	   (and (buffer-file-name (buffer-base-buffer))
>>>  		(not (string= (expand-file-name default-directory)
>>> -			      (expand-file-name
>>> -			       (file-name-directory buffer-file-name)))))))
>>> +			    (expand-file-name
>>> +			     (file-name-directory (buffer-file-name (buffer-base-buffer)))))))))
>>>        (format "[[file:%s]%s]"
>>> -	      (if (and default-directory buffer-file-name same-directory?)
>>> +	      (if (and default-directory (buffer-file-name (buffer-base-buffer)) same-directory?)
>>>  		  (if (eq org-link-file-path-type 'adaptive)
>>>  		      (file-relative-name
>>>  		       (expand-file-name result default-directory)
>>> -		       (file-name-directory (buffer-file-name)))
>>> +		       (file-name-directory (buffer-file-name (buffer-base-buffer))))
>>>  		    (expand-file-name result default-directory))
>>>  		result)
>>>  	      (if description (concat "[" description "]") "")))))
>>>
>>> Emacs  : GNU Emacs 28.0.50 (build 4, x86_64-pc-linux-gnu, GTK+ Version 3.24.20, cairo version 1.16.0)
>>>  of 2020-07-27
>>> Package: Org mode version 9.3.7 (release_9.3.7-728-g1efc4e @ /home/yantar92/.emacs.d/straight/build/org/)


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

* Re: Bug: [patch] Fix org-babel-result-to-file never expanding links when babel is evaluated in indirect buffer [9.3.7 (release_9.3.7-728-g1efc4e @ /home/yantar92/.emacs.d/straight/build/org/)]
  2020-08-01 16:33     ` John Herrlin
@ 2020-08-02  4:36       ` Ihor Radchenko
  2020-08-02 19:24         ` John Herrlin
  0 siblings, 1 reply; 9+ messages in thread
From: Ihor Radchenko @ 2020-08-02  4:36 UTC (permalink / raw)
  To: John Herrlin; +Cc: emacs-orgmode

> It's intended to change directory and dont clone base buffer values. I
> tried to accomplish "If the `default-directory' is different from the
> containing file's directory then expand relative links."

Not sure if I understand your use-case. Can you explain why you need to
achieve such behaviour? There might be an easier way to get what you
want.

Best,
Ihor


John Herrlin <jherrlin@gmail.com> writes:

> It's intended to change directory and dont clone base buffer values. I
> tried to accomplish "If the `default-directory' is different from the
> containing file's directory then expand relative links." As I
> understands it that's when it inserts a relative link, otherwise it just
> `result'. I was looking from the indirect buffers point of view and from
> there the relative link is off in this case. But from the base buffers
> point of view everything works fine. I guess that it was my use case
> that is strange.
>
> Looks good!
>
> Best regards
> John
>
>>> In my case the result is a relative link, but if I follow the link I end
>>> up in the wrong place.
>>
>> Do you mean that the generated script.sh ends up in your home dir?
>> It is expected since default-directory in your buffer is "~/":
>>
>>>   (dired "~/")
>>>   (make-indirect-buffer "tmp.org" "tmp-indirect")
>>
>> Note that you called make-indirect-buffer without third argument.
>>
>> According to manual:
>>
>>> (make-indirect-buffer BASE-BUFFER NAME &optional CLONE)
>>> CLONE nil means the indirect buffer's state is reset to default values.
>>
>>>     Org first tries to generate the filename from the value of the
>>>     ‘file’ header argument and the directory specified using the
>>>     ‘output-dir’ header arguments.  If ‘output-dir’ is not specified,
>>>     Org assumes it is the current directory.
>>
>> No surprise you got your file in the default-directory (~/).
>>
>> If you use (make-indirect-buffer "tmp.org" "tmp-indirect" t) instead,
>> the script.sh link will be relative, as expected.
>>
>> Let me know if I miss something.
>>
>> Best,
>> Ihor
>>
>>
>>> Hey Ihor,
>>>
>>> Could not get the patch to work.
>>>
>>> I used this use case.
>>>
>>> (with-current-buffer
>>>   (find-file "/tmp/tmp.org")
>>>   (insert "* A\n** b\n** c\n\n   some text")
>>>   (dired "~/")
>>>   (make-indirect-buffer "tmp.org" "tmp-indirect")
>>>   (switch-to-buffer "tmp-indirect")
>>>   (goto-char (point-max))
>>>   (funcall 'org-mode)
>>>   (insert (concat "\n\n| default-directory | " default-directory " |\n"
>>>                   "| org-link-file-path-type | " (symbol-name org-link-file-path-type) " |\n"
>>>                   "| (expand-file-name default-directory) | " (expand-file-name default-directory) " |\n"
>>>                   "| (buffer-file-name (buffer-base-buffer)) | " (buffer-file-name (buffer-base-buffer)) " |\n"))
>>>   (org-table-align)
>>>   (insert "\n\n#+BEGIN_SRC shell :results file :file script.sh
>>>        echo \"#!/bin/bash\"
>>>        echo \"echo Hey\"\n#+END_SRC")
>>>   (org-babel-execute-src-block))
>>>
>>> In my case the result is a relative link, but if I follow the link I end
>>> up in the wrong place.
>>>
>>> src_emacs-lisp{emacs-version} {{{results(=26.3=)}}}
>>> src_emacs-lisp{org-version} {{{results(=9.3.7=)}}}
>>>
>>> Best regards
>>> John
>>>
>>>
>>> Ihor Radchenko <yantar92@gmail.com> writes:
>>>
>>>> Remember to cover the basics, that is, what you expected to happen and
>>>> what in fact did happen.  You don't know how to make a good report?  See
>>>>
>>>>      https://orgmode.org/manual/Feedback.html#Feedback
>>>>
>>>> Your bug report will be posted to the Org mailing list.
>>>> ------------------------------------------------------------------------
>>>>
>>>> I noticed that org-babel-result-to-file never expands file: links in
>>>> src block results when ran from indirect buffer and default-directory
>>>> is not same with base buffer file's directory. This is against the
>>>> docstring stating that
>>>>
>>>>> If the `default-directory' is different from the containing
>>>>> file's directory then expand relative links.
>>>>
>>>> The misbehavior happens because buffer-file-name variable is always
>>>> nil in indirect buffers. We need to call (file-name-directory
>>>> (buffer-file-name (buffer-base-buffer))) instead.
>>>>
>>>> The patch is attached.
>>>>
>>>> Best,
>>>> Ihor
>>>>
>>>> diff --git a/lisp/ob-core.el b/lisp/ob-core.el
>>>> index f86282f0f..d37eef5f9 100644
>>>> --- a/lisp/ob-core.el
>>>> +++ b/lisp/ob-core.el
>>>> @@ -2561,16 +2561,16 @@ If the `default-directory' is different from the containing
>>>>  file's directory then expand relative links."
>>>>    (when (stringp result)
>>>>      (let ((same-directory?
>>>> -	   (and buffer-file-name
>>>> +	   (and (buffer-file-name (buffer-base-buffer))
>>>>  		(not (string= (expand-file-name default-directory)
>>>> -			      (expand-file-name
>>>> -			       (file-name-directory buffer-file-name)))))))
>>>> +			    (expand-file-name
>>>> +			     (file-name-directory (buffer-file-name (buffer-base-buffer)))))))))
>>>>        (format "[[file:%s]%s]"
>>>> -	      (if (and default-directory buffer-file-name same-directory?)
>>>> +	      (if (and default-directory (buffer-file-name (buffer-base-buffer)) same-directory?)
>>>>  		  (if (eq org-link-file-path-type 'adaptive)
>>>>  		      (file-relative-name
>>>>  		       (expand-file-name result default-directory)
>>>> -		       (file-name-directory (buffer-file-name)))
>>>> +		       (file-name-directory (buffer-file-name (buffer-base-buffer))))
>>>>  		    (expand-file-name result default-directory))
>>>>  		result)
>>>>  	      (if description (concat "[" description "]") "")))))
>>>>
>>>> Emacs  : GNU Emacs 28.0.50 (build 4, x86_64-pc-linux-gnu, GTK+ Version 3.24.20, cairo version 1.16.0)
>>>>  of 2020-07-27
>>>> Package: Org mode version 9.3.7 (release_9.3.7-728-g1efc4e @ /home/yantar92/.emacs.d/straight/build/org/)

-- 
Ihor Radchenko,
PhD,
Center for Advancing Materials Performance from the Nanoscale (CAMP-nano)
State Key Laboratory for Mechanical Behavior of Materials, Xi'an Jiaotong University, Xi'an, China
Email: yantar92@gmail.com, ihor_radchenko@alumni.sutd.edu.sg


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

* Re: Bug: [patch] Fix org-babel-result-to-file never expanding links when babel is evaluated in indirect buffer [9.3.7 (release_9.3.7-728-g1efc4e @ /home/yantar92/.emacs.d/straight/build/org/)]
  2020-08-02  4:36       ` Ihor Radchenko
@ 2020-08-02 19:24         ` John Herrlin
  2020-08-02 22:50           ` Ihor Radchenko
  0 siblings, 1 reply; 9+ messages in thread
From: John Herrlin @ 2020-08-02 19:24 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: emacs-orgmode


Ihor Radchenko <yantar92@gmail.com> writes:

>> It's intended to change directory and dont clone base buffer values. I
>> tried to accomplish "If the `default-directory' is different from the
>> containing file's directory then expand relative links."
>
> Not sure if I understand your use-case. Can you explain why you need to
> achieve such behaviour? There might be an easier way to get what you
> want.

I don't have a real world scenario, just the one I made up below. I used
that to play around with the function.

Best regards
John

>
> Best,
> Ihor
>
>
> John Herrlin <jherrlin@gmail.com> writes:
>
>> It's intended to change directory and dont clone base buffer values. I
>> tried to accomplish "If the `default-directory' is different from the
>> containing file's directory then expand relative links." As I
>> understands it that's when it inserts a relative link, otherwise it just
>> `result'. I was looking from the indirect buffers point of view and from
>> there the relative link is off in this case. But from the base buffers
>> point of view everything works fine. I guess that it was my use case
>> that is strange.
>>
>> Looks good!
>>
>> Best regards
>> John
>>
>>>> In my case the result is a relative link, but if I follow the link I end
>>>> up in the wrong place.
>>>
>>> Do you mean that the generated script.sh ends up in your home dir?
>>> It is expected since default-directory in your buffer is "~/":
>>>
>>>>   (dired "~/")
>>>>   (make-indirect-buffer "tmp.org" "tmp-indirect")
>>>
>>> Note that you called make-indirect-buffer without third argument.
>>>
>>> According to manual:
>>>
>>>> (make-indirect-buffer BASE-BUFFER NAME &optional CLONE)
>>>> CLONE nil means the indirect buffer's state is reset to default values.
>>>
>>>>     Org first tries to generate the filename from the value of the
>>>>     ‘file’ header argument and the directory specified using the
>>>>     ‘output-dir’ header arguments.  If ‘output-dir’ is not specified,
>>>>     Org assumes it is the current directory.
>>>
>>> No surprise you got your file in the default-directory (~/).
>>>
>>> If you use (make-indirect-buffer "tmp.org" "tmp-indirect" t) instead,
>>> the script.sh link will be relative, as expected.
>>>
>>> Let me know if I miss something.
>>>
>>> Best,
>>> Ihor
>>>
>>>
>>>> Hey Ihor,
>>>>
>>>> Could not get the patch to work.
>>>>
>>>> I used this use case.
>>>>
>>>> (with-current-buffer
>>>>   (find-file "/tmp/tmp.org")
>>>>   (insert "* A\n** b\n** c\n\n   some text")
>>>>   (dired "~/")
>>>>   (make-indirect-buffer "tmp.org" "tmp-indirect")
>>>>   (switch-to-buffer "tmp-indirect")
>>>>   (goto-char (point-max))
>>>>   (funcall 'org-mode)
>>>>   (insert (concat "\n\n| default-directory | " default-directory " |\n"
>>>>                   "| org-link-file-path-type | " (symbol-name org-link-file-path-type) " |\n"
>>>>                   "| (expand-file-name default-directory) | " (expand-file-name default-directory) " |\n"
>>>>                   "| (buffer-file-name (buffer-base-buffer)) | " (buffer-file-name (buffer-base-buffer)) " |\n"))
>>>>   (org-table-align)
>>>>   (insert "\n\n#+BEGIN_SRC shell :results file :file script.sh
>>>>        echo \"#!/bin/bash\"
>>>>        echo \"echo Hey\"\n#+END_SRC")
>>>>   (org-babel-execute-src-block))
>>>>
>>>> In my case the result is a relative link, but if I follow the link I end
>>>> up in the wrong place.
>>>>
>>>> src_emacs-lisp{emacs-version} {{{results(=26.3=)}}}
>>>> src_emacs-lisp{org-version} {{{results(=9.3.7=)}}}
>>>>
>>>> Best regards
>>>> John
>>>>
>>>>
>>>> Ihor Radchenko <yantar92@gmail.com> writes:
>>>>
>>>>> Remember to cover the basics, that is, what you expected to happen and
>>>>> what in fact did happen.  You don't know how to make a good report?  See
>>>>>
>>>>>      https://orgmode.org/manual/Feedback.html#Feedback
>>>>>
>>>>> Your bug report will be posted to the Org mailing list.
>>>>> ------------------------------------------------------------------------
>>>>>
>>>>> I noticed that org-babel-result-to-file never expands file: links in
>>>>> src block results when ran from indirect buffer and default-directory
>>>>> is not same with base buffer file's directory. This is against the
>>>>> docstring stating that
>>>>>
>>>>>> If the `default-directory' is different from the containing
>>>>>> file's directory then expand relative links.
>>>>>
>>>>> The misbehavior happens because buffer-file-name variable is always
>>>>> nil in indirect buffers. We need to call (file-name-directory
>>>>> (buffer-file-name (buffer-base-buffer))) instead.
>>>>>
>>>>> The patch is attached.
>>>>>
>>>>> Best,
>>>>> Ihor
>>>>>
>>>>> diff --git a/lisp/ob-core.el b/lisp/ob-core.el
>>>>> index f86282f0f..d37eef5f9 100644
>>>>> --- a/lisp/ob-core.el
>>>>> +++ b/lisp/ob-core.el
>>>>> @@ -2561,16 +2561,16 @@ If the `default-directory' is different from the containing
>>>>>  file's directory then expand relative links."
>>>>>    (when (stringp result)
>>>>>      (let ((same-directory?
>>>>> -	   (and buffer-file-name
>>>>> +	   (and (buffer-file-name (buffer-base-buffer))
>>>>>  		(not (string= (expand-file-name default-directory)
>>>>> -			      (expand-file-name
>>>>> -			       (file-name-directory buffer-file-name)))))))
>>>>> +			    (expand-file-name
>>>>> +			     (file-name-directory (buffer-file-name (buffer-base-buffer)))))))))
>>>>>        (format "[[file:%s]%s]"
>>>>> -	      (if (and default-directory buffer-file-name same-directory?)
>>>>> +	      (if (and default-directory (buffer-file-name (buffer-base-buffer)) same-directory?)
>>>>>  		  (if (eq org-link-file-path-type 'adaptive)
>>>>>  		      (file-relative-name
>>>>>  		       (expand-file-name result default-directory)
>>>>> -		       (file-name-directory (buffer-file-name)))
>>>>> +		       (file-name-directory (buffer-file-name (buffer-base-buffer))))
>>>>>  		    (expand-file-name result default-directory))
>>>>>  		result)
>>>>>  	      (if description (concat "[" description "]") "")))))
>>>>>
>>>>> Emacs  : GNU Emacs 28.0.50 (build 4, x86_64-pc-linux-gnu, GTK+ Version 3.24.20, cairo version 1.16.0)
>>>>>  of 2020-07-27
>>>>> Package: Org mode version 9.3.7 (release_9.3.7-728-g1efc4e @ /home/yantar92/.emacs.d/straight/build/org/)


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

* Re: Bug: [patch] Fix org-babel-result-to-file never expanding links when babel is evaluated in indirect buffer [9.3.7 (release_9.3.7-728-g1efc4e @ /home/yantar92/.emacs.d/straight/build/org/)]
  2020-08-02 19:24         ` John Herrlin
@ 2020-08-02 22:50           ` Ihor Radchenko
  0 siblings, 0 replies; 9+ messages in thread
From: Ihor Radchenko @ 2020-08-02 22:50 UTC (permalink / raw)
  To: John Herrlin; +Cc: emacs-orgmode

> I don't have a real world scenario, just the one I made up below. I used
> that to play around with the function.

I see. FYI: I do change default-directory during src block execution in
my config. The following code makes src blocks execute inside attachment
folder by default:

(define-advice org-babel-execute-src-block (:filter-args (&optional args) set-detault-dir-to-org-attach-path)
  "Set working directory to the current entry's attach directory."
  (if (eq major-mode 'org-mode)
      (let* ((directory (file-name-as-directory (org-attach-dir 'create-if-none)))
	     (arg (car args))
             (info (cadr args))
             (params (org-babel-merge-params (nth 2 info) (caddr args)))
             (dir-param (alist-get :dir params)))
        (unless (and dir-param (or (equal (f-full default-directory) (f-full dir-param))
				   (f-absolute-p dir-param)))
          (setf (alist-get :dir params)
		(if dir-param
                    (f-join directory (alist-get :dir params))
                  directory)))
	(list arg info params))
    (list arg info params)))

That's actually how I found the reported bug.

Best,
Ihor

John Herrlin <jherrlin@gmail.com> writes:

> Ihor Radchenko <yantar92@gmail.com> writes:
>
>>> It's intended to change directory and dont clone base buffer values. I
>>> tried to accomplish "If the `default-directory' is different from the
>>> containing file's directory then expand relative links."
>>
>> Not sure if I understand your use-case. Can you explain why you need to
>> achieve such behaviour? There might be an easier way to get what you
>> want.
>
> I don't have a real world scenario, just the one I made up below. I used
> that to play around with the function.
>
> Best regards
> John
>
>>
>> Best,
>> Ihor
>>
>>
>> John Herrlin <jherrlin@gmail.com> writes:
>>
>>> It's intended to change directory and dont clone base buffer values. I
>>> tried to accomplish "If the `default-directory' is different from the
>>> containing file's directory then expand relative links." As I
>>> understands it that's when it inserts a relative link, otherwise it just
>>> `result'. I was looking from the indirect buffers point of view and from
>>> there the relative link is off in this case. But from the base buffers
>>> point of view everything works fine. I guess that it was my use case
>>> that is strange.
>>>
>>> Looks good!
>>>
>>> Best regards
>>> John
>>>
>>>>> In my case the result is a relative link, but if I follow the link I end
>>>>> up in the wrong place.
>>>>
>>>> Do you mean that the generated script.sh ends up in your home dir?
>>>> It is expected since default-directory in your buffer is "~/":
>>>>
>>>>>   (dired "~/")
>>>>>   (make-indirect-buffer "tmp.org" "tmp-indirect")
>>>>
>>>> Note that you called make-indirect-buffer without third argument.
>>>>
>>>> According to manual:
>>>>
>>>>> (make-indirect-buffer BASE-BUFFER NAME &optional CLONE)
>>>>> CLONE nil means the indirect buffer's state is reset to default values.
>>>>
>>>>>     Org first tries to generate the filename from the value of the
>>>>>     ‘file’ header argument and the directory specified using the
>>>>>     ‘output-dir’ header arguments.  If ‘output-dir’ is not specified,
>>>>>     Org assumes it is the current directory.
>>>>
>>>> No surprise you got your file in the default-directory (~/).
>>>>
>>>> If you use (make-indirect-buffer "tmp.org" "tmp-indirect" t) instead,
>>>> the script.sh link will be relative, as expected.
>>>>
>>>> Let me know if I miss something.
>>>>
>>>> Best,
>>>> Ihor
>>>>
>>>>
>>>>> Hey Ihor,
>>>>>
>>>>> Could not get the patch to work.
>>>>>
>>>>> I used this use case.
>>>>>
>>>>> (with-current-buffer
>>>>>   (find-file "/tmp/tmp.org")
>>>>>   (insert "* A\n** b\n** c\n\n   some text")
>>>>>   (dired "~/")
>>>>>   (make-indirect-buffer "tmp.org" "tmp-indirect")
>>>>>   (switch-to-buffer "tmp-indirect")
>>>>>   (goto-char (point-max))
>>>>>   (funcall 'org-mode)
>>>>>   (insert (concat "\n\n| default-directory | " default-directory " |\n"
>>>>>                   "| org-link-file-path-type | " (symbol-name org-link-file-path-type) " |\n"
>>>>>                   "| (expand-file-name default-directory) | " (expand-file-name default-directory) " |\n"
>>>>>                   "| (buffer-file-name (buffer-base-buffer)) | " (buffer-file-name (buffer-base-buffer)) " |\n"))
>>>>>   (org-table-align)
>>>>>   (insert "\n\n#+BEGIN_SRC shell :results file :file script.sh
>>>>>        echo \"#!/bin/bash\"
>>>>>        echo \"echo Hey\"\n#+END_SRC")
>>>>>   (org-babel-execute-src-block))
>>>>>
>>>>> In my case the result is a relative link, but if I follow the link I end
>>>>> up in the wrong place.
>>>>>
>>>>> src_emacs-lisp{emacs-version} {{{results(=26.3=)}}}
>>>>> src_emacs-lisp{org-version} {{{results(=9.3.7=)}}}
>>>>>
>>>>> Best regards
>>>>> John
>>>>>
>>>>>
>>>>> Ihor Radchenko <yantar92@gmail.com> writes:
>>>>>
>>>>>> Remember to cover the basics, that is, what you expected to happen and
>>>>>> what in fact did happen.  You don't know how to make a good report?  See
>>>>>>
>>>>>>      https://orgmode.org/manual/Feedback.html#Feedback
>>>>>>
>>>>>> Your bug report will be posted to the Org mailing list.
>>>>>> ------------------------------------------------------------------------
>>>>>>
>>>>>> I noticed that org-babel-result-to-file never expands file: links in
>>>>>> src block results when ran from indirect buffer and default-directory
>>>>>> is not same with base buffer file's directory. This is against the
>>>>>> docstring stating that
>>>>>>
>>>>>>> If the `default-directory' is different from the containing
>>>>>>> file's directory then expand relative links.
>>>>>>
>>>>>> The misbehavior happens because buffer-file-name variable is always
>>>>>> nil in indirect buffers. We need to call (file-name-directory
>>>>>> (buffer-file-name (buffer-base-buffer))) instead.
>>>>>>
>>>>>> The patch is attached.
>>>>>>
>>>>>> Best,
>>>>>> Ihor
>>>>>>
>>>>>> diff --git a/lisp/ob-core.el b/lisp/ob-core.el
>>>>>> index f86282f0f..d37eef5f9 100644
>>>>>> --- a/lisp/ob-core.el
>>>>>> +++ b/lisp/ob-core.el
>>>>>> @@ -2561,16 +2561,16 @@ If the `default-directory' is different from the containing
>>>>>>  file's directory then expand relative links."
>>>>>>    (when (stringp result)
>>>>>>      (let ((same-directory?
>>>>>> -	   (and buffer-file-name
>>>>>> +	   (and (buffer-file-name (buffer-base-buffer))
>>>>>>  		(not (string= (expand-file-name default-directory)
>>>>>> -			      (expand-file-name
>>>>>> -			       (file-name-directory buffer-file-name)))))))
>>>>>> +			    (expand-file-name
>>>>>> +			     (file-name-directory (buffer-file-name (buffer-base-buffer)))))))))
>>>>>>        (format "[[file:%s]%s]"
>>>>>> -	      (if (and default-directory buffer-file-name same-directory?)
>>>>>> +	      (if (and default-directory (buffer-file-name (buffer-base-buffer)) same-directory?)
>>>>>>  		  (if (eq org-link-file-path-type 'adaptive)
>>>>>>  		      (file-relative-name
>>>>>>  		       (expand-file-name result default-directory)
>>>>>> -		       (file-name-directory (buffer-file-name)))
>>>>>> +		       (file-name-directory (buffer-file-name (buffer-base-buffer))))
>>>>>>  		    (expand-file-name result default-directory))
>>>>>>  		result)
>>>>>>  	      (if description (concat "[" description "]") "")))))
>>>>>>
>>>>>> Emacs  : GNU Emacs 28.0.50 (build 4, x86_64-pc-linux-gnu, GTK+ Version 3.24.20, cairo version 1.16.0)
>>>>>>  of 2020-07-27
>>>>>> Package: Org mode version 9.3.7 (release_9.3.7-728-g1efc4e @ /home/yantar92/.emacs.d/straight/build/org/)

-- 
Ihor Radchenko,
PhD,
Center for Advancing Materials Performance from the Nanoscale (CAMP-nano)
State Key Laboratory for Mechanical Behavior of Materials, Xi'an Jiaotong University, Xi'an, China
Email: yantar92@gmail.com, ihor_radchenko@alumni.sutd.edu.sg


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

* Re: Bug: [patch] Fix org-babel-result-to-file never expanding links when babel is evaluated in indirect buffer [9.3.7 (release_9.3.7-728-g1efc4e @ /home/yantar92/.emacs.d/straight/build/org/)]
  2020-07-31  8:49 Bug: [patch] Fix org-babel-result-to-file never expanding links when babel is evaluated in indirect buffer [9.3.7 (release_9.3.7-728-g1efc4e @ /home/yantar92/.emacs.d/straight/build/org/)] Ihor Radchenko
  2020-07-31 17:24 ` John Herrlin
@ 2020-09-05 14:42 ` Bastien
  2020-09-05 23:05   ` Ihor Radchenko
  1 sibling, 1 reply; 9+ messages in thread
From: Bastien @ 2020-09-05 14:42 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: emacs-orgmode

Hi Ihor,

I applied an updated version of your patch as f45591630,
just adding a changelog.

Thanks,

-- 
 Bastien


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

* Re: Bug: [patch] Fix org-babel-result-to-file never expanding links when babel is evaluated in indirect buffer [9.3.7 (release_9.3.7-728-g1efc4e @ /home/yantar92/.emacs.d/straight/build/org/)]
  2020-09-05 14:42 ` Bastien
@ 2020-09-05 23:05   ` Ihor Radchenko
  0 siblings, 0 replies; 9+ messages in thread
From: Ihor Radchenko @ 2020-09-05 23:05 UTC (permalink / raw)
  To: Bastien; +Cc: emacs-orgmode

Thanks!

Bastien <bzg@gnu.org> writes:

> Hi Ihor,
>
> I applied an updated version of your patch as f45591630,
> just adding a changelog.
>
> Thanks,
>
> -- 
>  Bastien


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

end of thread, other threads:[~2020-09-05 23:06 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-07-31  8:49 Bug: [patch] Fix org-babel-result-to-file never expanding links when babel is evaluated in indirect buffer [9.3.7 (release_9.3.7-728-g1efc4e @ /home/yantar92/.emacs.d/straight/build/org/)] Ihor Radchenko
2020-07-31 17:24 ` John Herrlin
2020-07-31 23:32   ` Ihor Radchenko
2020-08-01 16:33     ` John Herrlin
2020-08-02  4:36       ` Ihor Radchenko
2020-08-02 19:24         ` John Herrlin
2020-08-02 22:50           ` Ihor Radchenko
2020-09-05 14:42 ` Bastien
2020-09-05 23:05   ` Ihor Radchenko

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.