1

I often find myself doing this:

  • Selecting a region of text in an org file.
  • Calling org-md-export-as-markdown.
  • Selecting the converted text in the temporary buffer (can't just do mark-whole-buffer since for these purposes I'm obviously not interested in the empty Table of Contents I get).
  • Killing the temporary buffer and going back to where I was.

EDIT: Here is my question: How can I write a function that will, after selecting a region of text, do all of that for me? In other words, I want a function which will take a region from an org file, copy the result of org-md-export-as-markdown to the clipboard, and return to the org file I called the function from.

Some related attempts:

John Kitchin has a post where he does something similar to what I'm doing. But he uses texutil, since he wants the output in a format that org won't export to. I'm assuming what I need would be much simpler than that.

Oleh Krehel has a function that copies the converted html from a region as part of his emacs config (see here). But he uses xclip which I believe is not available for macOS.

Abo-abo's function is here

(defun ora-org-to-html-to-clipboard ()
   "Export region to HTML, and copy it to the clipboard."
   (interactive)
   (org-export-to-file 'html "/tmp/org.html")
   (apply
    'start-process "xclip" "*xclip*"
    (split-string
     "xclip -verbose -i /tmp/org.html -t text/html -selection clipboard" " ")))

I presume I can modify something like this by replacing

xclip -verbose -i /tmp/org.html -t text/html -selection clipboard

with

pbcopy < /tmp/org.html

but I can't quite get it to work.

My own, failed attempt so far:

This was my blind attempt at adapting the function ora-org-to-html to use pbcopy instead. (Adapting it to use org-md-export-to-markdown would be fairly straightforward, I think.)

(defun org-to-html-to-clipboard ()
   "Export region to HTML, and copy it to the clipboard."
   (interactive)
   (org-export-to-file 'html "/tmp/org.html")
   (apply
    'start-process "pbcopy" "*pbcopy*"
    (split-string
     "pbcopy < /tmp/org.html")))

Running this does create the file /tmp/org.html but the contents aren't sent to the clipboard. Further, the html file contains the entirety of the buffer I run the function from (which I gather is what the original function was supposed to do) rather than just the selected region. Finally, this function requires creating a temporary file, rather than just using the temporary buffer that org-mode generates with the *-export-as-* functions.


I'm completely illiterate when it comes to elisp so any suggestions, no matter how elementary they seem, would be most welcome.

  • The question is too broad. And only one question per post, please. Specific Q&A is what SE is about. There is no doubt at least one specific, operable question in there somewhere - please consider digging it out and posting it instead of this one. – Drew Dec 10 '19 at 18:07
  • I did some editing following your advice. Let me know if this is still too broad. – apc Dec 10 '19 at 18:27
  • OK, thanks. Hope you get some specific answers. – Drew Dec 10 '19 at 19:15
0

I'm posting this here in case it helps anyone else. Following a suggestion by u/ImmediateCurve over at Reddit, I ended up with the following function, which does what I want (it uses simpleclip.el since I mostly rely on it for interacting with the system clipboard on macOS):

(defun apc/org-md-to-clipboard ()
  (interactive)
   (save-window-excursion
    (let ((org-export-with-toc nil))
      (let ((buf (org-export-to-buffer 'md "*tmp*" nil nil t t)))
        (save-excursion
           (set-buffer buf)
        (simpleclip-set-contents (buffer-string))
        (kill-buffer-and-window)
        )))))
| improve this answer | |

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.