0

How to open external sourcecode files inside org-mode codeblocks, in a way that files can be viewed and saved while saving the org-mode document.

Kinda of a 'Nested Buffers' feature?

  • 1
    I've never heard of a way to do this, but it sounds like a nice feature. Currently when you save a buffer it does not know that one of the headings is connected to another file and to save that separately. I could imagine that you could write something that would save a heading into a file, and then add-hook when saved to call that function. I assume "viewed and saved" means "viewed, edited and saved" – jtgd Feb 11 '17 at 4:40
  • I am doing some reading in Literate programming with emacs, I hope to find someone done what I am after, before. Shall update then (could take awhile as I am busy with other stuff) – zotherstupidguy Feb 12 '17 at 3:06
  • Ah, I see, yes you could have it export to a file, but that's a one-way street. If you modify the file outside, it won't be read back in. For what you described you would want a hook to do the export upon save of the org file. – jtgd Feb 14 '17 at 6:29
  • @jtgd yup, exactly – zotherstupidguy Feb 14 '17 at 18:55
  • Would you like me to write it for you? – jtgd Feb 17 '17 at 7:23
0

I can think of a few variations and options one might want to add, but this seems to work for me. Try it, @zotherstupidguy, and get back to me with suggestions.

Upon saving the org file it will look for any heading containing a property of SAVE-TO-FILE whose value is the filename to save to, i.e.:

:PROPERTIES:
:SAVE-TO-FILE: ~/my-ext-file
:END:

It will save everything in that heading, including the heading itself and the property drawer.

(defun save-headings-external ()
"Save all heading containing special property to an external file"
(interactive)
(when (string= major-mode "org-mode")
  (goto-char (point-min))
  (let (-extfname -start)
    (while (re-search-forward ":PROPERTIES:" nil t)
      (when (setq -extfname (org-entry-get (point) "SAVE-TO-FILE"))
        (save-excursion
          (org-back-to-heading)
          (setq -start (point))
          (org-forward-heading-same-level 1 t)
          (write-region -start (point) -extfname)
          (message "Wrote '%s'" -extfname)))))))

(add-hook 'write-file-hooks 'save-headings-external)

(You can delete the (message...) if you want)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.