all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* [PATCH] Add yank-media handler for LibreOffice Calc tables
@ 2024-10-14 16:36 Visuwesh
  2024-10-14 18:58 ` Ihor Radchenko
  0 siblings, 1 reply; 7+ messages in thread
From: Visuwesh @ 2024-10-14 16:36 UTC (permalink / raw)
  To: org-mode-email

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

Attached patch adds support for yanking LibreOffice Calc tables as Org
tables.  Tables from docx files (or LO's equivalent) cannot be yanked
though since the mimetype for them are richtext or HTML.


[-- 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: 3740 bytes --]

From 0cd4b1c5402256ed64c9e1711db11ad215c6c1a3 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        | 14 +++++++++++++-
 3 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/doc/org-manual.org b/doc/org-manual.org
index 451fd72cd..e8da07356 100644
--- a/doc/org-manual.org
+++ b/doc/org-manual.org
@@ -21640,7 +21640,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
@@ -21658,6 +21659,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=.
 
+When yanking tables copied from LibreOffice Calc documents, they are
+inserted as Org tables when yanking them.
+
 ** Repeating commands
 :PROPERTIES:
 :DESCRIPTION: Repeating navigation commands
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 5d421172f..37ee9a52f 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -114,6 +114,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 6177856c3..82ad40527 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -20860,7 +20860,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)))
 
@@ -20953,6 +20955,16 @@ (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."
+  (let ((beg (point)))
+    (insert data)
+    ;; LibreOffice source code defines
+    ;; 'application/x-libreoffice-tsvc' as Text TSV-Calc so it should
+    ;; be safe to handle this as TSV always.
+    (org-table-convert-region beg (point) '(16))))
+
 (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


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] Add yank-media handler for LibreOffice Calc tables
  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
  0 siblings, 1 reply; 7+ messages in thread
From: Ihor Radchenko @ 2024-10-14 18:58 UTC (permalink / raw)
  To: Visuwesh; +Cc: org-mode-email

Visuwesh <visuweshm@gmail.com> writes:

> Attached patch adds support for yanking LibreOffice Calc tables as Org
> tables.  Tables from docx files (or LO's equivalent) cannot be yanked
> though since the mimetype for them are richtext or HTML.

Thanks!

I just tried your patch (make repro) with a simple table

foo	bar	
1	2	3

I got

* This is test
| f\0o\0o\0 | \0b\0a\0r\0 | \0    |
| \01\0     | \02\0       | \03\0 |
| \0        |             |       |

Something is off with encoding.

> +(defun org--libreoffice-table-handler (_mimetype data)
> +  "Insert LibreOffice Calc table DATA as an Org table.
> +DATA is in the TSV format."
> +  (let ((beg (point)))
> +    (insert data)
> +    ;; LibreOffice source code defines
> +    ;; 'application/x-libreoffice-tsvc' as Text TSV-Calc so it should
> +    ;; be safe to handle this as TSV always.
> +    (org-table-convert-region beg (point) '(16))))

What if the point is in the middle of something else?
For example, in the middle of another table.
Then, conversion may yield wild outcomes.
Maybe it is safer to convert region in a temporary buffer and
insert the final result into the Org document being edited.

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Add yank-media handler for LibreOffice Calc tables
  2024-10-14 18:58 ` Ihor Radchenko
@ 2024-10-15  5:57   ` Visuwesh
  2024-10-15 18:42     ` Ihor Radchenko
  0 siblings, 1 reply; 7+ messages in thread
From: Visuwesh @ 2024-10-15  5:57 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: org-mode-email

[திங்கள் அக்டோபர் 14, 2024] Ihor Radchenko wrote:

> Visuwesh <visuweshm@gmail.com> writes:
>
>> Attached patch adds support for yanking LibreOffice Calc tables as Org
>> tables.  Tables from docx files (or LO's equivalent) cannot be yanked
>> though since the mimetype for them are richtext or HTML.
>
> Thanks!
>
> I just tried your patch (make repro) with a simple table
>
> foo	bar	
> 1	2	3
>
> I got
>
> * This is test
> | f\0o\0o\0 | \0b\0a\0r\0 | \0    |
> | \01\0     | \02\0       | \03\0 |
> | \0        |             |       |
>
> Something is off with encoding.

I cannot reproduce it on my end.  I tried with different documents and
document types (Excel and LO format).  Would it be possible to send the
faulty file?

>> +(defun org--libreoffice-table-handler (_mimetype data)
>> +  "Insert LibreOffice Calc table DATA as an Org table.
>> +DATA is in the TSV format."
>> +  (let ((beg (point)))
>> +    (insert data)
>> +    ;; LibreOffice source code defines
>> +    ;; 'application/x-libreoffice-tsvc' as Text TSV-Calc so it should
>> +    ;; be safe to handle this as TSV always.
>> +    (org-table-convert-region beg (point) '(16))))
>
> What if the point is in the middle of something else?
> For example, in the middle of another table.
> Then, conversion may yield wild outcomes.
> Maybe it is safer to convert region in a temporary buffer and
> insert the final result into the Org document being edited.

Even if we do the processing in a temporary buffer, we would still end
up inserting the table after point.  I say we leave it to the user to be
prudent and not call yank-media when in the middle of a table or
somesuch.  We do not do any special handling in other handlers, so why
do it here?


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Add yank-media handler for LibreOffice Calc tables
  2024-10-15  5:57   ` Visuwesh
@ 2024-10-15 18:42     ` Ihor Radchenko
  2024-10-17 17:19       ` Visuwesh
  0 siblings, 1 reply; 7+ messages in thread
From: Ihor Radchenko @ 2024-10-15 18:42 UTC (permalink / raw)
  To: Visuwesh; +Cc: org-mode-email

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

Visuwesh <visuweshm@gmail.com> writes:

>> Something is off with encoding.
>
> I cannot reproduce it on my end.  I tried with different documents and
> document types (Excel and LO format).  Would it be possible to send the
> faulty file?

Here is the recipe
1. make repro (I tried with the latest Emacs master and with Emacs 29)
2. Open test.odt (attached)
3. M-x org-mode
4. Copy the non-empty cell area
5. M-x yank-media <RET> app<TAB> <RET>
6. Observe encoding problems

>>> +    (insert data)
>>> +    ;; LibreOffice source code defines
>>> +    ;; 'application/x-libreoffice-tsvc' as Text TSV-Calc so it should
>>> +    ;; be safe to handle this as TSV always.
>>> +    (org-table-convert-region beg (point) '(16))))
>>
>> What if the point is in the middle of something else?
>> For example, in the middle of another table.
>> Then, conversion may yield wild outcomes.
>> Maybe it is safer to convert region in a temporary buffer and
>> insert the final result into the Org document being edited.
>
> Even if we do the processing in a temporary buffer, we would still end
> up inserting the table after point.  I say we leave it to the user to be
> prudent and not call yank-media when in the middle of a table or
> somesuch.  We do not do any special handling in other handlers, so why
> do it here?

Consider the following example:

* Heading <point>

M-x yank-media ... will slurp the heading

| * Heading a | b | c |

In contrast, the existing handlers will only insert some text at point,
never touching the text around.


[-- Attachment #2: test.ods --]
[-- Type: application/vnd.oasis.opendocument.spreadsheet, Size: 8912 bytes --]

[-- Attachment #3: Type: text/plain, Size: 224 bytes --]


-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Add yank-media handler for LibreOffice Calc tables
  2024-10-15 18:42     ` Ihor Radchenko
@ 2024-10-17 17:19       ` Visuwesh
  2024-10-17 17:37         ` Ihor Radchenko
  0 siblings, 1 reply; 7+ messages in thread
From: Visuwesh @ 2024-10-17 17:19 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: org-mode-email

[செவ்வாய் அக்டோபர் 15, 2024] Ihor Radchenko wrote:

> Visuwesh <visuweshm@gmail.com> writes:
>
>>> Something is off with encoding.
>>
>> I cannot reproduce it on my end.  I tried with different documents and
>> document types (Excel and LO format).  Would it be possible to send the
>> faulty file?
>
> Here is the recipe
> 1. make repro (I tried with the latest Emacs master and with Emacs 29)
> 2. Open test.odt (attached)
> 3. M-x org-mode
> 4. Copy the non-empty cell area
> 5. M-x yank-media <RET> app<TAB> <RET>
> 6. Observe encoding problems

Thanks for the reproducer.  I think this might be locale-dependent.  I
fail to reproduce on my end with the following locale settings:

    LANG=en_GB.UTF-8
    LC_COLLAGE=ta_IN.UTF-8
    LC_MONETARY=ta_IN.UTF-8
    LC_NUMERIC=ta_IN.UTF-8

    locale-coding-system = 'utf-8-unix

I have no idea how to deal with locale issues like this unfortunately.

>>>> +    (insert data)
>>>> +    ;; LibreOffice source code defines
>>>> +    ;; 'application/x-libreoffice-tsvc' as Text TSV-Calc so it should
>>>> +    ;; be safe to handle this as TSV always.
>>>> +    (org-table-convert-region beg (point) '(16))))
>>>
>>> What if the point is in the middle of something else?
>>> For example, in the middle of another table.
>>> Then, conversion may yield wild outcomes.
>>> Maybe it is safer to convert region in a temporary buffer and
>>> insert the final result into the Org document being edited.
>>
>> Even if we do the processing in a temporary buffer, we would still end
>> up inserting the table after point.  I say we leave it to the user to be
>> prudent and not call yank-media when in the middle of a table or
>> somesuch.  We do not do any special handling in other handlers, so why
>> do it here?
>
> Consider the following example:
>
> * Heading <point>
>
> M-x yank-media ... will slurp the heading
>
> | * Heading a | b | c |
>
> In contrast, the existing handlers will only insert some text at point,
> never touching the text around.

Would it be sufficient to unconditionally insert a newline after point?
Maybe even after going to end of current element (if that can be done).


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Add yank-media handler for LibreOffice Calc tables
  2024-10-17 17:19       ` Visuwesh
@ 2024-10-17 17:37         ` Ihor Radchenko
  2024-10-18 16:45           ` Visuwesh
  0 siblings, 1 reply; 7+ messages in thread
From: Ihor Radchenko @ 2024-10-17 17:37 UTC (permalink / raw)
  To: Visuwesh; +Cc: org-mode-email

Visuwesh <visuweshm@gmail.com> writes:

>> Consider the following example:
>>
>> * Heading <point>
>>
>> M-x yank-media ... will slurp the heading
>>
>> | * Heading a | b | c |
>>
>> In contrast, the existing handlers will only insert some text at point,
>> never touching the text around.
>
> Would it be sufficient to unconditionally insert a newline after point?
> Maybe even after going to end of current element (if that can be done).

It will be more fragile compared to using temporary buffer.
As an alternative, you can apply narrowing around the call to
`org-table-convert-region'.

Also, do note that any text exceeding
`org-table-convert-region-max-lines' will lead to
`org-table-convert-region' throwing an error.

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Add yank-media handler for LibreOffice Calc tables
  2024-10-17 17:37         ` Ihor Radchenko
@ 2024-10-18 16:45           ` Visuwesh
  0 siblings, 0 replies; 7+ messages in thread
From: Visuwesh @ 2024-10-18 16:45 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: org-mode-email

[வியாழன் அக்டோபர் 17, 2024] Ihor Radchenko wrote:

> Visuwesh <visuweshm@gmail.com> writes:
>
>>> Consider the following example:
>>>
>>> * Heading <point>
>>>
>>> M-x yank-media ... will slurp the heading
>>>
>>> | * Heading a | b | c |
>>>
>>> In contrast, the existing handlers will only insert some text at point,
>>> never touching the text around.
>>
>> Would it be sufficient to unconditionally insert a newline after point?
>> Maybe even after going to end of current element (if that can be done).
>
> It will be more fragile compared to using temporary buffer.
> As an alternative, you can apply narrowing around the call to
> `org-table-convert-region'.

Thanks for the explanation.  I understand your initial suggestion.  I
will use a temp buffer in an updated patch.

> 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?

I will post an updated patch if someone comments on how to resolve the
encoding issue.


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2024-10-18 16:46 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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

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.