rolandwalker / simpleclip

Simplified access to the system clipboard in Emacs.
174 stars 16 forks source link

doest not work one Emacs 25.1 when use emacs-nox #6

Open redguardtoo opened 7 years ago

redguardtoo commented 7 years ago

(x-get-selection 'CLIPBOARD) return nil

same issue happened on Debian Linux and Cygwin

Boruch-Baum commented 7 years ago

I can confirm this trouble. The failure extends even to simpleclip-get-contents', ie.simpleclip-copy' indicates success, but simpleclip-get-contents' returns nothing andsimpleclip-paste' returns a message "nothing to paste". Also, setting select-enable-clipboard' to t does not help, nor use in conjunction with the emacs built-in functionsclipboard-kill-region' and clipboard-yank'. Also, now when usingsimpleclip-copy', the region is not de-activated, indicating that the command did not really complete properly.

@redguardtoo : Do be aware that x-get-selection' is deprecated as of emacs25. The replacement is functiongui-get-selection'.

fikovnik commented 7 years ago

I have the same problem. It indicates success upon copy, but nothing copied. Using it on fedora in urxvt.

Boruch-Baum commented 7 years ago

Well, it has been nine+ months since the initial report, so I thought I would share my work-around, that doesn't require the simpleclip package at all. However, it does require the linux command line program xsel, which is probably already installed by default by your distro.

    ; x-clip support for emacs-nox / emacs-nw
    (defun my-copy-to-xclipboard(arg)
      (interactive "P")
      (cond
        ((not (use-region-p))
          (message "Nothing to yank to X-clipboard"))
        ((and (not (display-graphic-p))
             (/= 0 (shell-command-on-region
                     (region-beginning) (region-end) "xsel -i -b")))
          (error "Is program `xsel' installed?"))
        (t
          (when (display-graphic-p)
            (call-interactively 'clipboard-kill-ring-save))
          (message "Yanked region to X-clipboard")
          (when arg
            (kill-region  (region-beginning) (region-end)))
          (deactivate-mark))))

    (defun my-cut-to-xclipboard()
      (interactive)
      (my-copy-to-xclipboard t))

    (defun my-paste-from-xclipboard()
      "Uses shell command `xsel -o' to paste from x-clipboard. With
    one prefix arg, pastes from X-PRIMARY, and with two prefix args,
    pastes from X-SECONDARY."
      (interactive)
      (if (display-graphic-p)
        (clipboard-yank)
       (let*
         ((opt (prefix-numeric-value current-prefix-arg))
          (opt (cond
           ((=  1 opt) "b")
           ((=  4 opt) "p")
           ((= 16 opt) "s"))))
        (insert (shell-command-to-string (concat "xsel -o -" opt))))))

    (global-set-key (kbd "C-c C-w") 'my-cut-to-xclipboard)
    (global-set-key (kbd "C-c M-w") 'my-copy-to-xclipboard)
    (global-set-key (kbd "C-c C-y") 'my-paste-from-xclipboard)
fikovnik commented 6 years ago

@Boruch-Baum - this is excellent. Thanks a lot for sharing.

fikovnik commented 6 years ago

I actually made a small change and decided to use OSC 52 which will yank the text into the terminal. The advantage of this is that it works with tmux on a remote host without the need to forward X display. here is the code based on the one from @Boruch-Baum

(defun my-copy-to-xclipboard(arg)
  (interactive "P")
  (if (use-region-p)
      (if (not (display-graphic-p))
          (letrec ((s (buffer-substring-no-properties (region-beginning) (region-end)))
                   (s-length (+ (* (length s) 3) 2)))
            (if (<= s-length 16384) ; magic number set to the same as ESC_BUF_SIZ of suckless termial (st.c)
                (progn
                  (send-string-to-terminal (concat "\e]52;c;"
                                                   (base64-encode-string (encode-coding-string s 'utf-8) t)
                                                   "\07"))
                  (message "Yanked region to terminal clipboard")
                  (when arg
                    (kill-region (region-beginning) (region-end)))
                  (deactivate-mark))
              (message "Selection too long (%d) to send to terminal." s-length))))
      (message "Nothing to yank to terminal clipboard")))
Boruch-Baum commented 6 years ago

@fikovnik : Cool. I have a bunch of questions / comments / feedback:

  1. How did you get the lisp colorized in your comment? What markdown did you use?

  2. Should your version of the function really still be called my-copy-to-xclipboard? Is it doing that, or is it just now copying to the parent xterm-compatable process?

  3. I'm not certain that I'm clear about your use-case, so just to be sure: Are you saying that you are running emacs-nox in a remote tmux session and that this function transfers data to your local xterm-compatable process (ie. transfer data from a remote host to your local xterm)?

  4. If you insist on limiting the selection size for the benefit of users of suckless terminal, it would still be more user-friendly for the case of an over-sized selection to truncate the selection and process the truncated selection, maybe with a message to that effect. That way, the user would get SOMETHING done immediately, and would know immediately where to start performing a second selection.

  5. As I mentioned in # 3 above, I'm not sure about your use-case. It would be nice to be able to support the limitation of the suckless terminal, but is there a way to identify when that is the case, and when not, so that the arbitrary byte limit need not be enforced?

  6. About your use of buffer-substring-no-properties: It seems like a safe improvement to the code, but many general use-cases will want to have properties transferred along with the text.

  7. For a single function to cover all use cases, maybe retain some version of my original code for the general X11 use-cases, and if the function is invoked with a prefix argument, perform your modification?

fikovnik commented 6 years ago
  1. How did you get the lisp colorized in your comment? What markdown did you use?

You just add the name of the language used next to the fenced block (cf. the code section in examples in https://guides.github.com/features/mastering-markdown/)

  1. Should your version of the function really still be called my-copy-to-xclipboard? Is it doing that, or is it just now copying to the parent xterm-compatable process?

Good point! Thanks!

  1. I'm not certain that I'm clear about your use-case, so just to be sure: Are you saying that you are running emacs-nox in a remote tmux session and that this function transfers data to your local xterm-compatable process (ie. transfer data from a remote host to your local xterm)?

Yes, I run emacs-nox in a tmux in an ssh session on a remote host. I did not want to keep forwarding X display and plus on some servers I cannot install xsel or xclip. I have not find any other way how to copy selected text.

  1. If you insist on limiting the selection size for the benefit of users of suckless terminal, it would still be more user-friendly for the case of an over-sized selection to truncate the selection and process the truncated selection, maybe with a message to that effect. That way, the user would get SOMETHING done immediately, and would know immediately where to start performing a second selection.

This is a good idea. It would be actually interesting to see what is the limit for other terminal.

  1. As I mentioned in # 3 above, I'm not sure about your use-case. It would be nice to be able to support the limitation of the suckless terminal, but is there a way to identify when that is the case, and when not, so that the arbitrary byte limit need not be enforced?

I guess there should be some limit. I don't think the terminal wants to allocate infinite memory. What this limit should I have no idea. This magic number is the one I actually set in my instance of st. The default was 128 ~ 380 characters.

  1. About your use of buffer-substring-no-properties: It seems like a safe improvement to the code, but many general use-cases will want to have properties transferred along with the text.

Another good point.

  1. For a single function to cover all use cases, maybe retain some version of my original code for the general X11 use-cases, and if the function is invoked with a prefix argument, perform your modification?

Sure, this was just an alternative that fits my use case. For local use, yours is better since OSC is not supported in many terminal emulators. Anyway, thanks a lot for sharing your original code which helped me a lot to figure out the basics!

fikovnik commented 6 years ago

It would be great if somebody (with more elisp knowledge) can put these things into working package :-)

Boruch-Baum commented 6 years ago

I just tried your code, using stterm as packaged by debian, and haven't got it to work. Any tricks, gotchas, or code patches necessary? Debian claims that the version of stterm is 5+..., but stterm --version says 0.4.1

I agree about the idea of producing a working package with you, with the caveat that it not be specific to only stterm.

fikovnik commented 6 years ago

I think the version is too old, i.e. without OSC 52 support. You can try my fork https://github.com/fikovnik/st

The OSC 52, while being terminal agnostic, is not supported widely. I know only of xterm, urxvt and st that handle it.

berenddeboer commented 5 years ago

Same here: this mode advertises itself as seamless, but did not work. I'm running emacs -nw (I think still the gtk version, not nox) in a tmux terminal on my desktop. @Boruch-Baum version at least let's me copy to the X clipboard.

rolandwalker commented 5 years ago

this mode advertises itself as seamless

Yes, arguably the TTY functionality is not "simple", and doesn't belong in this library.