summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKévin Le Gouguec <kevin.legouguec@gmail.com>2022-11-20 00:00:22 +0100
committerKévin Le Gouguec <kevin.legouguec@gmail.com>2022-11-20 00:03:01 +0100
commitaac4dea7862ee3662f1a832217fa605db34d12da (patch)
tree2461554b9f37e2642a3e14c6aafce0b0229416c0
parentfd103d61f795de3dafa3501274ca3419d449deaa (diff)
downloaddotfiles-aac4dea7862ee3662f1a832217fa605db34d12da.tar.xz
Add functions for rich clipboard interaction
In order to copy/paste between word processors and Emacs markup modes. This needs a bit more work (e.g. when copying Org snippets, we need to siphon some #+ macros and copy any #+SETUPFILEs) but it's a start.
-rw-r--r--.emacs40
1 files changed, 40 insertions, 0 deletions
diff --git a/.emacs b/.emacs
index c6e5595..7ad2e5c 100644
--- a/.emacs
+++ b/.emacs
@@ -620,4 +620,44 @@
(save-buffer)
(kill-buffer buf)))
+;; Clipboard interaction.
+(defun my/kill-as-html (text markup)
+ (interactive
+ (list (buffer-substring (region-beginning) (region-end))
+ (or (alist-get major-mode '((markdown-mode . "markdown")
+ (org-mode . "org")
+ (rst-mode . "rst")))
+ (let ((default "plain"))
+ (read-string (format-prompt "Convert from:" default)
+ nil nil default)))))
+ ;; TODO: make this a transient to easily (un)set pandoc extensions.
+ (with-temp-buffer
+ (call-process-region text nil "pandoc" nil t nil
+ "--from" markup "--to" "html")
+ ;; TODO: could gui-set-selection help here? The docstring makes
+ ;; it sound like passing a value with a 'text/html property set to
+ ;; the HTML string should work, but empirically it doesn't.
+ (call-process-region nil nil "xclip" nil nil nil
+ "-selection" "clipboard" "-target" "text/html")))
+
+(defun my/yank-from-html (html markup)
+ (interactive
+ (list
+ (gui-get-selection 'CLIPBOARD 'text/html)
+ (or (alist-get major-mode '((markdown-mode . "markdown")
+ (org-mode . "org")
+ (rst-mode . "rst")))
+ (let ((default "plain"))
+ (read-string (format-prompt "Convert to:" default)
+ nil nil default)))))
+ ;; TODO: make this a transient to easily (un)set pandoc extensions.
+ (let* ((disabled-html-extensions '("native_divs" "native_spans"))
+ (html-spec
+ (funcall 'string-join `("html" ,@disabled-html-extensions) "-"))
+ (disabled-markup-extensions '("smart"))
+ (markup-spec
+ (funcall 'string-join `(,markup ,@disabled-markup-extensions) "-")))
+ (call-process-region html nil "pandoc" nil t t
+ "--from" html-spec "--to" markup-spec)))
+
;; TODO: decruftify mode-line (e.g. remove superflous parens)