From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Madhu Newsgroups: gmane.emacs.devel Subject: Re: elpa xclip 1.11.1: copying from emacs GUI hangs emacs terminal Date: Tue, 10 Dec 2024 09:25:56 +0530 Message-ID: References: <38c19494-4abb-4316-a307-5c9e2f43fe23@cas.cat> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="19717"; mail-complaints-to="usenet@ciao.gmane.io" To: emacs-devel@gnu.org Cancel-Lock: sha1:lXkXIBo9Raa57EjjUh3GPhaqSiI= Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Tue Dec 10 13:14:58 2024 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1tKz8j-0004vC-Tk for ged-emacs-devel@m.gmane-mx.org; Tue, 10 Dec 2024 13:14:58 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tKz7x-0005Yv-ET; Tue, 10 Dec 2024 07:14:09 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tKrM9-00062q-Pf for emacs-devel@gnu.org; Mon, 09 Dec 2024 22:56:17 -0500 Original-Received: from ciao.gmane.io ([116.202.254.214]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tKrLv-0007cZ-Cs for emacs-devel@gnu.org; Mon, 09 Dec 2024 22:56:07 -0500 Original-Received: from list by ciao.gmane.io with local (Exim 4.92) (envelope-from ) id 1tKrLt-0004Vc-47 for emacs-devel@gnu.org; Tue, 10 Dec 2024 04:56:01 +0100 X-Injected-Via-Gmane: http://gmane.org/ Received-SPF: pass client-ip=116.202.254.214; envelope-from=ged-emacs-devel@m.gmane-mx.org; helo=ciao.gmane.io X-Spam_score_int: -15 X-Spam_score: -1.6 X-Spam_bar: - X-Spam_report: (-1.6 / 5.0 requ) BAYES_00=-1.9, HEADER_FROM_DIFFERENT_DOMAINS=0.249, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Tue, 10 Dec 2024 07:14:02 -0500 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:326279 Archived-At: --=-=-= Content-Type: text/plain * 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) --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-initial-fixes.patch Content-Description: xclip fixes >From 28e7363314061e2a98ba29919146bf51e8f06770 Mon Sep 17 00:00:00 2001 From: Madhu Date: Thu, 10 Mar 2016 14:37:20 +0530 Subject: [PATCH] initial fixes ;; 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). +;; 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 --=-=-= Content-Type: text/plain > > 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 --=-=-=--