From f392dc1f3eb550c3d833683113c2d958f7f9b722 Mon Sep 17 00:00:00 2001 From: Visuwesh Date: Mon, 14 Oct 2024 22:04:23 +0530 Subject: [PATCH] Add `yank-media' handler for LibreOffice Calc tables * lisp/org.el (org--libreoffice-table-handler): Add new `yank-media' handler for tables copied from LibreOffice Calc documents. (org-setup-yank-dnd-handlers): Register it. * doc/org-manual.org: (Drag and Drop & ~yank-media~): Update the manual to mention the new feature. * etc/ORG-NEWS: Announce the new feature. --- doc/org-manual.org | 6 +++++- etc/ORG-NEWS | 5 +++++ lisp/org.el | 27 ++++++++++++++++++++++++++- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/doc/org-manual.org b/doc/org-manual.org index 95846b475..e23e388a0 100644 --- a/doc/org-manual.org +++ b/doc/org-manual.org @@ -21684,7 +21684,8 @@ value as ~org-attach-method~ (~cp~ by default).] #+cindex: pasting files, images from clipboard Starting from Emacs 29, Org mode supports ~yank-media~ command to yank -images from the clipboard and files from a file manager. +images from the clipboard, files from a file manager and tables copied +from LibreOffice Calc documents. #+vindex: org-yank-image-save-method When yanking images from clipboard, Org saves the image on disk and @@ -21702,6 +21703,9 @@ of ~org-yank-dnd-method~. Image files pasted this way also respect the value of ~org-yank-image-save-method~ when the action to perform is =attach=. +Yanking tables copied from LibreOffice Calc documents are inserted as +Org tables. + ** Repeating commands :PROPERTIES: :DESCRIPTION: Repeating navigation commands diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS index 08857962b..9ad2da5d4 100644 --- a/etc/ORG-NEWS +++ b/etc/ORG-NEWS @@ -136,6 +136,11 @@ The keybindings in the repeat-maps can be changed by customizing See the new [[info:org#Repeating commands]["Repeating commands"]] section in Org mode manual. +*** Tables copied from LibreOffice Calc documents can be pasted as Org tables + +Tables copied into the clipboard from LibreOffice Calc documents can +now be pasted as an Org table using ~yank-media~. + ** New and changed options # Chanes deadling with changing default values of customizations, diff --git a/lisp/org.el b/lisp/org.el index def094a9a..f9c275ca7 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -20354,7 +20354,9 @@ (defun org-setup-yank-dnd-handlers () ;; Looks like different DEs go for different handler names, ;; https://larsee.com/blog/2019/05/clipboard-files/. (yank-media-handler "x/special-\\(?:gnome\\|KDE\\|mate\\)-files" - #'org--copied-files-yank-media-handler)) + #'org--copied-files-yank-media-handler) + (yank-media-handler "application/x-libreoffice-tsvc" + #'org--libreoffice-table-handler)) (when (boundp 'x-dnd-direct-save-function) (setq-local x-dnd-direct-save-function #'org--dnd-xds-function))) @@ -20447,6 +20449,29 @@ (defun org--copied-files-yank-media-handler (_mimetype data) (org--dnd-local-file-handler f action sep) (message "File `%s' is not readable, skipping" f))))) +(defun org--libreoffice-table-handler (_mimetype data) + "Insert LibreOffice Calc table DATA as an Org table. +DATA is in the TSV format." + ;; Some LibreOffice versions have the null byte in the selection. + ;; It should be safe to remove it. + (when (string-search "\0" data) + (setq data (string-replace "\0" "" data))) + (let ((orig-buf (current-buffer))) + (with-temp-buffer + (decode-coding-string data buffer-file-coding-system nil (current-buffer)) + (let ((tmp (current-buffer)) + (nlines (count-lines (point-min) (point-max)))) + (when (> nlines org-table-convert-region-max-lines) + (unless (yes-or-no-p + (format "Inserting large table with %d lines, more than `org-table-convert-region-max-lines'. Continue? " + nlines)) + (user-error "Table is larger than limit `org-table-convert-region-max-lines'"))) + ;; User has chosen to ignore the limit. + (let ((org-table-convert-region-max-lines most-positive-fixnum)) + (org-table-convert-region (point-min) (point-max))) + (with-current-buffer orig-buf + (insert-buffer-substring tmp)))))) + (defcustom org-yank-dnd-method 'ask "Action to perform on the dropped and the pasted files. When the value is the symbol, -- 2.45.2