all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Visuwesh <visuweshm@gmail.com>
To: Rens Oliemans <hallo@rensoliemans.nl>
Cc: Ihor Radchenko <yantar92@posteo.net>,
	 org-mode-email <emacs-orgmode@gnu.org>
Subject: Re: [PATCH] Add yank-media handler for LibreOffice Calc tables
Date: Fri, 29 Nov 2024 19:35:14 +0530	[thread overview]
Message-ID: <87bjxyqgo5.fsf@gmail.com> (raw)
In-Reply-To: <871q09vj59.fsf@gmail.com> (Visuwesh's message of "Mon, 21 Oct 2024 16:04:58 +0530")

[-- Attachment #1: Type: text/plain, Size: 2713 bytes --]

Sorry for the late reply.  I was busy with uni.

[திங்கள் அக்டோபர் 21, 2024] Visuwesh wrote:

> [திங்கள் அக்டோபர் 21, 2024] Rens Oliemans wrote:
>
>> Visuwesh <visuweshm@gmail.com> writes:
>>
>>> If possible, could you please try LibreOffice 7.6.4? The version Ihor
>>> has installed in his system.  There is an AppImage available for you to
>>> try.
>>
>> Yep, on 7.6.4 the problem occurs.
>>
>>     $ ./LibreOffice-7.6.4.1.basic-x86_64.AppImage --version
>>     LibreOffice 7.6.4.1 e19e193f88cd6c0525a17fb7a176ed8e6a3e2aa1
>>
>>     Debugger entered--entering a function:
>>     * org--libreoffice-table-handler(application/x-libreoffice-tsvc #("f\0o\0o\0\11\0b\0a\0r\0\11\0\n\0001\0\11\0002\0\11\0003\0\n\0" 0 30 (foreign-selection STRING)))
>>       yank-media()
>>
>> , resulting in
>>
>>     | f\0o\0o\0 | \0b\0a\0r\0 | \0    |
>>     | \01\0     | \02\0       | \03\0 |
>>     | \0        |             |       |
>>
>> To narrow it down a bit more: the problem exists in 7.6.5.2, and does not exist in the
>> following release, 7.6.6.3.
>
> Thank you!  So it is most likely not locale related---a major headache
> saved.  I will look into this soon (cannot promise anything this week
> unfortunately).

For reasons that escape me completely, I did not see null bytes in the
selection in

    % ./LibreOffice-7.6.4.1.standard.help-x86_64.AppImage --version
    LibreOffice 7.6.4.1 e19e193f88cd6c0525a17fb7a176ed8e6a3e2aa1

But considering that the chances of having null bytes in documents are
very low, I decided to bruteforce strip them.

[சனி அக்டோபர் 19, 2024] Visuwesh wrote:

> [சனி அக்டோபர் 19, 2024] Ihor Radchenko wrote:
>
>> Visuwesh <visuweshm@gmail.com> writes:
>>
>>>> Also, do note that any text exceeding
>>>> `org-table-convert-region-max-lines' will lead to
>>>> `org-table-convert-region' throwing an error.
>>>
>>> Catching that and messaging the user should be sufficient?
>>
>> Maybe. Or we may catch and ask user to lift the restriction temporarily.
>> Something like:
>>
>> Inserting large table with more than `org-table-convert-region-max-lines' <number>. Continue? (yes/no)
>
> Ah, something like what Calc does.  Yes, I will go with this approach.
> Thanks for the suggestion.

I have now done this in the attached patch.  I use a temporary buffer to
convert the table, and also decode it as per buffer-file-coding-system
of the Org buffer as I could not tell whether LibreOffice always uses
UTF-8 or not.  I believe buffer-file-coding-system should be utf-8 on
most systems so it should be a safe choice.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Add-yank-media-handler-for-LibreOffice-Calc-tables.patch --]
[-- Type: text/x-diff, Size: 4524 bytes --]

From f392dc1f3eb550c3d833683113c2d958f7f9b722 Mon Sep 17 00:00:00 2001
From: Visuwesh <visuweshm@gmail.com>
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


      reply	other threads:[~2024-11-29 14:06 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-14 16:36 [PATCH] Add yank-media handler for LibreOffice Calc tables Visuwesh
2024-10-14 18:58 ` Ihor Radchenko
2024-10-15  5:57   ` Visuwesh
2024-10-15 18:42     ` Ihor Radchenko
2024-10-17 17:19       ` Visuwesh
2024-10-17 17:37         ` Ihor Radchenko
2024-10-18 16:45           ` Visuwesh
2024-10-19  7:10             ` Ihor Radchenko
2024-10-19  7:36               ` Visuwesh
2024-10-19  7:08         ` Ihor Radchenko
2024-10-19  7:35           ` Visuwesh
2024-10-19  7:42             ` Ihor Radchenko
2024-10-19  8:45               ` Visuwesh
2024-10-19  8:51                 ` Ihor Radchenko
2024-10-19  9:06                   ` Visuwesh
2024-10-19 10:09                     ` Rens Oliemans
2024-10-19 10:36                       ` Visuwesh
2024-10-21  8:51                         ` Rens Oliemans
2024-10-21 10:34                           ` Visuwesh
2024-11-29 14:05                             ` Visuwesh [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87bjxyqgo5.fsf@gmail.com \
    --to=visuweshm@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=hallo@rensoliemans.nl \
    --cc=yantar92@posteo.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.