unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* elpa xclip 1.11.1: copying from emacs GUI hangs emacs terminal
@ 2024-12-09 16:59 pinmacs
  2024-12-10  3:55 ` Madhu
  0 siblings, 1 reply; 2+ messages in thread
From: pinmacs @ 2024-12-09 16:59 UTC (permalink / raw)
  To: emacs-devel

Hi,

I first contacted Leo, the maintainer privately, and she proposed me to 
discuss with you guys here broadly.

Context: I am a Debian GNU/Linux 12 (bookworm) user that runs the 
i3wm.org desktop, but I use emacs compiled on my own [0] from banch 
emacs-30 commit 44c2614, I think this is unrelated for the issue because 
I am having the issue since long time but recently I dedicated some 
hours to see if I could fix it:

Bug report: When I run an emacsclient sessions through X11 (named later, 
x11 emacs), and I copy (with common M-w) some text to the X11 clipboard 
and I paste (with common C-y) X11 clipboard it with Alacritty terminal 
(emacs running on terminal), then emacs hangs and I need to press C-g 
several times to kill the process that is being run and I don't get the 
clipboard content.

More details:

Everything else works perfectly, my config is simple on that matter:

#+begin_src emacs-lisp
;; https://elpa.gnu.org/packages/xclip.html
(use-package xclip :ensure nil
   :config
   (xclip-mode 1)
   )
#+end_src

This problem is somehow well-known [1] (that does not mean that I 
understand what's going on, just reporting that this happens, not only 
in xclip context)

Here, I am able to reproduce the hang with org-babel [1]:

#+begin_src bash
xclip -i /dev/null -selection clipboard
xclip -o -selection clipboard
#+end_src

it also hangs with the built-in shell-command elisp function calls:

(shell-command "echo 'clipboard content' | xclip -selection clipboard")
(shell-command "xclip -o -selection clipboard")


But I cannot reproduce the hang in a normal terminal (vterm, alacritty, 
even in a linux console; fails because cannot open display).


Inside the xclip.el, a process-call with the xclip program is invoked, 
then hangs, and then you need to press C-g several times, this happens 
with the following configurations:

     (setq xclip-method 'xclip)
     (setq xclip-method 'xsel)

Here, I suggest, to reduce UX-frustration throwing a timeout error; but 
I don't know how to do it. I just know the POSIX timeout command [2].

There is a workaround, unrelated to emacs: using the typical terminal 
clipboard paste command Ctrl+Shift+V, but I don't like to think if I am 
in a terminal emacs buffer or X11 emacs buffer, I want to flow and have 
something that works everywhere flawlessly. In fact, I also like to 
access the whole previous kill ring with M-y.


Then I found that the emacs xclip-method works fine for that corner case 
that I am describing, but then sometimes "looses the synchronization" 
(?) with the X11 clipboard and provides an old-wrong clipboard; so I end 
up copying and recopying from a random X11 app to emacs terminal without 
success (specially when doing it to the terminal window).

     (setq xclip-method 'emacs)


I also tried to hack on the xclip code, it is weird that when I override 
the function definition does not work, I think it would be nice to know 
the details/documentation on how to hack or change on this.

Anyway, trying stuff, when I replaced the paste content with hello 
world, it does not hang and it works, so it is clearly a problem when 
tries to get the xclip content from terminal and the last content was 
put in clipboard by emacs X11.

By the way, disabling xclip package, makes pasting the emacs X11 to 
emacs terminal work; but then, does not work with the rest of X11 
clipboard apps.


Thanks for your attention,
pinmacs

[0] my compilation details:
   ./configure --with-native-compilation --with-native-compilation=aot 
--with-x-toolkit=lucid
   make -j4
   make bootstrap

[1] 
https://emacs.stackexchange.com/questions/39019/xclip-hangs-shell-command

[2] https://orgmode.org/worg/org-contrib/babel/

[3] https://pubs.opengroup.org/onlinepubs/9799919799/utilities/timeout.html




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

* Re: elpa xclip 1.11.1: copying from emacs GUI hangs emacs terminal
  2024-12-09 16:59 elpa xclip 1.11.1: copying from emacs GUI hangs emacs terminal pinmacs
@ 2024-12-10  3:55 ` Madhu
  0 siblings, 0 replies; 2+ messages in thread
From: Madhu @ 2024-12-10  3:55 UTC (permalink / raw)
  To: emacs-devel

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


* pinmacs <38c19494-4abb-4316-a307-5c9e2f43fe23@cas.cat> :
Wrote on Mon, 9 Dec 2024 17:59:32 +0100:
> I first contacted Leo, the maintainer privately, and she proposed me to
> discuss with you guys here broadly.
>
> Context: I am a Debian GNU/Linux 12 (bookworm) user that runs the
> i3wm.org desktop, but I use emacs compiled on my own [0] from banch
> emacs-30 commit 44c2614, I think this is unrelated for the issue because
> I am having the issue since long time but recently I dedicated some
> hours to see if I could fix it:
>
> Bug report: When I run an emacsclient sessions through X11 (named later,
> x11 emacs), and I copy (with common M-w) some text to the X11 clipboard
> and I paste (with common C-y) X11 clipboard it with Alacritty terminal
> (emacs running on terminal), then emacs hangs and I need to press C-g
> several times to kill the process that is being run and I don't get the
> clipboard content.


FWIW I'm attaching a patch I've been using for a long time, ever since
the the clipboard stuff was converted to elisp cl-generics. (I only
understood enough of it to work around the hangs at that time.  and
haven't reviewed it for mistakes before posting)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: xclip fixes --]
[-- Type: text/x-patch, Size: 4592 bytes --]

From 28e7363314061e2a98ba29919146bf51e8f06770 Mon Sep 17 00:00:00 2001
From: Madhu <enometh@net.meer>
Date: Thu, 10 Mar 2016 14:37:20 +0530
Subject: [PATCH] initial fixes

;; <Thu Mar 22 13:07:37 2012 +0530> madhu 120322 1. avoid deadlock
;; when calling C-y (call-process("xclip-o")) in a terminal when an
;; emacs X frame owns the selection. 1a. (current-kill 0) when emacs
;; owns the primary. 2. a sit-for in xclip-select-text avoids deadlock
;; when calling C-y in a frame when xclip owns the selection. this
;; happens when M-w'ing a text which has text-properties and C-y'ing

sitfor workaround. (selection-value--internal): new dispatch to
(xclip-selection-value-synchrnous):new or gui-selection-value
internal.
---
 xclip.el | 45 +++++++++++++++++++++++++++++++++++++++------
 1 file changed, 39 insertions(+), 6 deletions(-)

diff --git a/xclip.el b/xclip.el
index 4fe07d3..c76e130 100644
--- a/xclip.el
+++ b/xclip.el
@@ -50,6 +50,15 @@
 ;; copy/pasting from an external application to Emacs and not from Emacs to
 ;; another application (for which it relies on the default code).
 
+;; <Thu Mar 22 13:07:37 2012 +0530> madhu 120322 1. avoid deadlock
+;; when calling C-y (call-process("xclip-o")) in a terminal when an
+;; emacs X frame owns the selection. 1a. (current-kill 0) when emacs
+;; owns the primary. 2. a sit-for in xclip-select-text avoids deadlock
+;; when calling C-y in a frame when xclip owns the selection. this
+;; happens when M-w'ing a text which has text-properties and C-y'ing
+;; it.
+;;
+
 ;;; Code:
 
 (defgroup xclip ()
@@ -156,7 +165,8 @@ (defun xclip-set-selection (type data)
               (method (error "Unknown `xclip-method': %S" method)))))
       (when proc
         (process-send-string proc data)
-        (process-send-eof proc))
+        (process-send-eof proc)
+	(sit-for .2))
       data)))
 
 (defun xclip-get-selection (type)
@@ -268,7 +278,13 @@ (defun xclip--hidden-frame ()
                                                &context (window-system nil))
         (if (not xclip-mode)
             (cl-call-next-method)
-          (xclip-get-selection selection-symbol)))
+          (cond ((fboundp 'x-selection-exists-p) ; X build
+		 (cond ((x-selection-exists-p selection-symbol)
+			(cond ((x-selection-owner-p selection-symbol)
+			       (cl-call-next-method))
+			      (t (xclip-get-selection selection-symbol))))
+		       (t (xclip-get-selection selection-symbol))))
+		(t (xclip-get-selection selection-symbol)))))
 
       (cl-defmethod gui-backend-set-selection (selection-symbol value
                                                &context (window-system nil))
@@ -311,10 +327,29 @@ (defun xclip--hidden-frame ()
       (xclip-set-selection 'clipboard text)
       (setq xclip-last-selected-text-clipboard text)))
 
+  (defun xclip-selection-value--synchronous (type)
+    (with-output-to-string
+      (process-file xclip-program nil standard-output nil
+                    "-o" "-selection" (symbol-name type))))
+
+  (defun xclip-selection-value--internal (type)
+    (cond ((and (fboundp 'x-selection-exists-p)	; X build
+		;(eq window-system 'x)
+		)
+	   (cond ((x-selection-exists-p type)
+		  (cond ((x-selection-owner-p type)
+			 ;;thanks stefan monnier for spit cl-generic
+			 (let ((window-system 'x))
+			   (gui--selection-value-internal type)))
+			(t (xclip-selection-value--synchronous type))))
+		 (t
+		  (xclip-selection-value--synchronous type))))
+	  (t (xclip-selection-value--synchronous type))))
+
   (defun xclip-selection-value ()
     "See `x-selection-value'."
     (let ((clip-text (when xclip-select-enable-clipboard
-                       (xclip-get-selection 'CLIPBOARD))))
+		       (xclip-selection-value--internal 'CLIPBOARD))))
       (setq clip-text
             (cond                       ; Check clipboard selection.
              ((or (not clip-text) (string= clip-text ""))
@@ -329,9 +364,7 @@ (defun xclip--hidden-frame ()
              (t (setq xclip-last-selected-text-clipboard clip-text))))
       (or clip-text
           (when (and (memq xclip-method '(xsel xclip)) (getenv "DISPLAY"))
-            (let ((primary-text (with-output-to-string
-                                  (call-process xclip-program nil
-                                                standard-output nil "-o"))))
+            (let ((primary-text (xclip-selection-value--internal 'PRIMARY)))
               (setq primary-text
                     (cond               ; Check primary selection.
                      ((or (not primary-text) (string= primary-text ""))
-- 
2.46.0.27.gfa3b914457


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



>
> More details:
>
> Everything else works perfectly, my config is simple on that matter:
>
> #+begin_src emacs-lisp
> ;; https://elpa.gnu.org/packages/xclip.html
> (use-package xclip :ensure nil
>    :config
>    (xclip-mode 1)
>    )
> #+end_src
>
>
> This problem is somehow well-known [1] (that does not mean that I
> understand what's going on, just reporting that this happens, not only
> in xclip context)
>
> Here, I am able to reproduce the hang with org-babel [1]:
>
> #+begin_src bash
> xclip -i /dev/null -selection clipboard
> xclip -o -selection clipboard
> #+end_src
>
> it also hangs with the built-in shell-command elisp function calls:
>
> (shell-command "echo 'clipboard content' | xclip -selection clipboard")
> (shell-command "xclip -o -selection clipboard")
>
>
> But I cannot reproduce the hang in a normal terminal (vterm, alacritty,
> even in a linux console; fails because cannot open display).
>
>
> Inside the xclip.el, a process-call with the xclip program is invoked,
> then hangs, and then you need to press C-g several times, this happens
> with the following configurations:
>
>      (setq xclip-method 'xclip)
>      (setq xclip-method 'xsel)
>
> Here, I suggest, to reduce UX-frustration throwing a timeout error; but
> I don't know how to do it. I just know the POSIX timeout command [2].
>
> There is a workaround, unrelated to emacs: using the typical terminal
> clipboard paste command Ctrl+Shift+V, but I don't like to think if I am
> in a terminal emacs buffer or X11 emacs buffer, I want to flow and have
> something that works everywhere flawlessly. In fact, I also like to
> access the whole previous kill ring with M-y.
>
>
> Then I found that the emacs xclip-method works fine for that corner case
> that I am describing, but then sometimes "looses the synchronization"
> (?) with the X11 clipboard and provides an old-wrong clipboard; so I end
> up copying and recopying from a random X11 app to emacs terminal without
> success (specially when doing it to the terminal window).
>
>      (setq xclip-method 'emacs)
>
>
> I also tried to hack on the xclip code, it is weird that when I override
> the function definition does not work, I think it would be nice to know
> the details/documentation on how to hack or change on this.
>
> Anyway, trying stuff, when I replaced the paste content with hello
> world, it does not hang and it works, so it is clearly a problem when
> tries to get the xclip content from terminal and the last content was
> put in clipboard by emacs X11.
>
> By the way, disabling xclip package, makes pasting the emacs X11 to
> emacs terminal work; but then, does not work with the rest of X11
> clipboard apps.
>
>
> Thanks for your attention,
> pinmacs
>
> [0] my compilation details:
>    ./configure --with-native-compilation --with-native-compilation=aot
> --with-x-toolkit=lucid
>    make -j4
>    make bootstrap
>
> [1]
> https://emacs.stackexchange.com/questions/39019/xclip-hangs-shell-command
> [2] https://orgmode.org/worg/org-contrib/babel/
> [3] https://pubs.opengroup.org/onlinepubs/9799919799/utilities/timeout.html

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

end of thread, other threads:[~2024-12-10  3:55 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-12-09 16:59 elpa xclip 1.11.1: copying from emacs GUI hangs emacs terminal pinmacs
2024-12-10  3:55 ` Madhu

Code repositories for project(s) associated with this public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).