nobiot / org-transclusion

Emacs package to enable transclusion with Org Mode
https://nobiot.github.io/org-transclusion/
GNU General Public License v3.0
917 stars 44 forks source link

Scimax notebook's nb: links do not work #161

Open uliw opened 1 year ago

uliw commented 1 year ago

I have the following link in my file

nb:ESS462::po4-model-2.org::c580

Clicking this link will open the respective file, but

#+transclude: [[nb:ESS462::po4-model-2.org::c580][PO4 Model]]

will result in a no-content found error. Thx for any hints how to go about this

nobiot commented 1 year ago

What does nb: link link to? I have never seen it.

Perhaps you can add support; similar things have been done https://github.com/nobiot/org-transclusion/issues/160

uliw commented 1 year ago

did some digging. It is a link format introduced by J Kitchen and used in scimax.

https://github.com/jkitchin/scimax/blob/master/scimax-notebook.org#notebookproject-links

I looked at #160 and I think John's code provides similar functions so that this solution could be adapted, but my background is in python so I am not sure.

uliw commented 1 year ago

As a workaround, setting (org-link-set-parameters "nb" :store nil) disables the notebook links, and uses id's instead.

nobiot commented 1 year ago

I can't really test this as I don't have scimax, but something like the following should work. Could I assume you know how to use the snippets like this below? If not, that's fine; I can help you. I would just like to know how comfortable you are with Elisp.


(defun org-transclusion-add-nb-org-file (link plist)
  "Return a list for Org file LINK object and PLIST.
Return nil if not found."
  (when (string= "nb" (org-element-property :type link))
    (append '(:tc-type "org-link")
            (org-transclusion-content-nb-link-to-org link plist))))

(defun org-transclusion-content-nb-link-to-org (link plist)
  (nb-follow-other (org-element-property :path link))
  ;; Now in the notes buffer
  ;; Assume it's an Org buffer
  (prog1 (org-transclusion-content-org-buffer-or-element
          'only-element plist)
    ;; No need to stay in the notes buffer
    ;; Kill
    (kill-buffer)))

(add-hook 'org-transclusion-add-functions #'org-transclusion-add-nb-org-file)
uliw commented 1 year ago

Thanks, I tried the above and it works somewhat. Using a regular org file located in ~tmp and adding the following link [[nb:esbmtk::esbmtk/esbmtk.py::c1]] resolves to ~python-scripts/esbmtk/esbmtk.py when I follow the link.

Using your code snippet above, adding the transclusion keyword, and then trying to transclude attempts to open ~tmp/esbmtk/esbmtk.py instead.

So there is some difference between how org resolves the link and how transclude resolves the link.

nobiot commented 1 year ago

OK, thank you for confirming. I only covered the case of the target file being an org file based on the example you gave me, assuming that's probably your use case.

Yes, the nb function nb-follow-other differentiates the two case: target org file or others. I didn't have energy to cover both on a Sunday night.

I'll give another try. Probably not this week or weekend. Some time next week is my guess...

uliw commented 1 year ago

NP. I tried with an org file but then it says. no content found. Cheers

On Mon, Jan 23, 2023 at 9:42 AM nobiot @.***> wrote:

OK, thank you for confirming. I only covered the case of the target file being an org file based on the example you gave me, assuming that's probably your use case.

Yes, the nb function nb-follow-other differentiates the two case: target org file or others. I didn't have energy to cover both on a Sunday night.

I'll give another try. Probably not this week or weekend. Some time next week is my guess...

— Reply to this email directly, view it on GitHub https://github.com/nobiot/org-transclusion/issues/161#issuecomment-1400458458, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABWSVAU3OC6DGB2L3W7H7YLWT2KGZANCNFSM6AAAAAAUCUM2QY . You are receiving this because you authored the thread.Message ID: @.***>

-- Ulrich G. Wortmann https://www.es.utoronto.ca/people/faculty/wortmann-ulrich/ Dept. of Earth Sciences University of Toronto Phone: 416 978 7084 22 Ursula Franklin Street, Toronto, ON, Canada M5S 3B1

nobiot commented 1 year ago

I see. Hmm. It sounds like I will need to install scimax and see how it really works.

uliw commented 1 year ago

it installs cleanly into a separate directory without interfering with your local setup. From the scimax menu, select notebook, new notebook. Storing a link from within the notebook should create the above link format.

nobiot commented 1 year ago

Thank you. Internally, scimax seems to use a lot of packages that I don't use and am not familiar with -- e.g. projectile to group the org and other files into projects, etc. I will see what I can do, but at the moment my gut feel is it is not a quick thing. Stay tuned and wish me luck.

uliw commented 1 year ago

Will do! I think the linking code is in this package

https://github.com/jkitchin/scimax/blob/master/scimax-notebook.org#notebookproject-links

uliw commented 1 year ago

here is another tidbit. The link returned from org-store-link looks like this [[nb:esbmtk::esbmtk/esbmtk.py::c]] if scimax is installed the following lisp command will open the relevant buffer)

#+SRC emacs-lisp
(nb-follow "esbmtk::esbmtk/esbmtk.py::c")
#+END_SRC

note that the link type (nb) has been stripped

nobiot commented 1 year ago

I had some time and got curious, so had a second go. I coded this in scimax, and it worked on my project for both an org file and program files.

Two notes:

  1. As noted in the source comment, evaluate add-hook after org-transclusion has been loaded. The key is that #'org-transclusion-add-nb-org-file must be in an element before other default ones in the list; it must be before org-transclusion-add-src-lines; otherwise add-src-lines takes precedent.

  2. For source files, this code only get the file name and ignore the location specifier (::c10, ::10, etc.). If you wish to specify a starting line number, use :lines parameter. as part of the #+transclude:.

One thing I was not sure about in your case is the use of ~tmp. It seems to be a temporary directory, but I'm not sure how you use it. This could be another source of issues, but this is as far as I can get to in this week. I can't work on it on this weekend, so if there is any issue that requires a bit of work, it will have to wait until some time next week.

;; -*- lexical-binding: t; -*-

(defun org-transclusion-add-nb-org-file (link plist)
  "Return a list for Org file LINK object and PLIST.
Return nil if not found."
  (when (string= "nb" (org-element-property :type link))
    (append '(:tc-type "org-link")
            (org-transclusion-content-nb-link-to-org link plist))))

(defun org-transclusion-content-nb-link-to-org (link plist)
  (let ((orig-buf (current-buffer))
        (target-file nil))
    (nb-follow-other (org-element-property :path link))
    ;; Now in the notes buffer
    ;; Assume it's an Org buffer
    (setq target-file buffer-file-name)
    (prog1
        (cond
         ((derived-mode-p  'org-mode)
          (org-transclusion-content-org-buffer-or-element
           'only-element plist))
         (t
          (let ((new-link (with-temp-buffer
                            (insert "file:")
                            (insert target-file)
                            (beginning-of-buffer)
                            (org-element-link-parser))))
            (org-transclusion-content-src-lines new-link plist))))
      (pop-to-buffer orig-buf))))

;; Run this after org-transclusion has been
;; loaded. `org-transclusion-add-nb-org-file' should be at the beginning
;; of the list
(add-hook 'org-transclusion-add-functions #'org-transclusion-add-nb-org-file)
uliw commented 1 year ago

that works like a charm, for org-files and also for py files. org-transclusion-make-from-link, does not work, but that is only a minor inconvenience. Thanks so much!

nobiot commented 1 year ago

You're welcome. I will look into org-transclusion-make-from-link at some point later. It should just work...

I am thinking of spending a little more time on this. If I get somewhere, I might put it as a scimax notebook support.

Thanks for reporting the issue :)