unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* gui-get
@ 2021-11-12  6:13 Emanuel Berg via Users list for the GNU Emacs text editor
  2021-11-12  6:22 ` gui-get Po Lu
  0 siblings, 1 reply; 3+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-11-12  6:13 UTC (permalink / raw)
  To: help-gnu-emacs

None of these work:

  (gui-get-primary-selection)

  (gui-get-selection)
  (gui-get-selection 'primary)
  (gui-get-selection 'secondary)
  (gui-get-selection 'clipboard)

My own old interface to xsel(1x) also don't work anymore but
if you set it up with commands it works.

But if you do Ctrl-C in the web browser's URL field and then
try to output it the above don't do anything ("No selection is
available", the first, the others just nil) and the below
Elisp stalls.

Even the xsel(1x) and xclip(1) don't do anything (they also
stall) so maybe it isn't even an Emacs issue. I guess not :P

GNU Emacs 29.0.50 (build 1, x86_64-pc-linux-gnu, cairo version
1.16.0) of 2021-10-04

Hm .. how do you output the version of X(7)? Ah, just 11.
man page is "xorg-docs 1.7.1". Do people still work on X BTW?

;;; xsel.el --- use the X clipboard -*- lexical-binding: t -*-
;;;
;;; Commentary:
;;;
;;; Author: Emanuel Berg (incal) <moasenwood@zoho.eu>
;;; Created: 2021-05-04
;;; Keywords: unix
;;; License: GPL3+
;;; URL: https://dataswamp.org/~incal/emacs-init/xsel.el
;;; Version: 2.0.2
;;;
;;; This gives you access to the X clipboard from a Linux
;;; VT/console/tty Emacs instance (or any Emacs, actually).
;;;
;;; Possible: Insert the X clipboard at point.
;;;
;;; DWIM: If there is a region, replace it with the
;;; X clipboard.
;;;
;;; Also possible: set the X clipboard programmatically in
;;; Elisp or set it interactively to the contents of the
;;; region if there is one, else set it to the most recent
;;; Emacs kill, without popping the kill ring.
;;;
;;; This package can be used to get complete transparency -
;;; but it is probably, err, over"kill" to have it enabled for
;;; every single one. Let's drop X already and do everything
;;; in Emacs anyway, right?
;;;
;;;     (setq interprogram-cut-function ...)
;;;
;;; If DISPLAY is set, it is used, else use ":0".
;;;
;;; Code:

(let ((xsel-x-display (or (getenv "DISPLAY") ":0")))
  (defun insert-x-clipboard ()
    "Insert the X clipboard at point using the X tool xsel.
Then, point is moved to the end of the inserted clipboard.
If there is a region, overwrite its contents with the clipboard.
See `set-x-clipboard-to-string'."
    (interactive)
    (when (use-region-p)
      (delete-region (region-beginning) (region-end)) )
    (shell-command
     (format "xsel --display \"%s\" --clipboard -o" xsel-x-display)
     1) ; insert into the current buffer
    (goto-char (mark)) )
  (declare-function insert-x-clipboard nil)

  (defun set-x-clipboard-to-string (str)
    "Set the X clipboard to STR.
See `set-x-clipboard' and `insert-x-clipboard'."
    (shell-command
     (format "echo -n %s | xsel --display %s -b -i"
       (shell-quote-argument str)
       xsel-x-display) ))
  (declare-function set-x-clipboard-to-string nil) )

(defun set-x-clipboard (string)
  "Set the X clipboard to STRING. When used interactively,
STRING is either the region content, if available,
else it is set to the most recent Emacs kill.
See `insert-x-clipboard'."
  (interactive
   (list (if (use-region-p)
             (buffer-substring-no-properties (region-beginning) (region-end))
           (encode-coding-string (current-kill 0 t) 'utf-8-unix) )))
  (set-x-clipboard-to-string string) )

(defun x-copy-buffer ()
  "Copy the buffer contents to the X clipboard."
  (interactive)
  (let ((str (encode-coding-string (buffer-string) 'utf-8-unix)))
    (set-x-clipboard-to-string str) ))

(defun x-copy-symbol (sym)
  "Copy the value of SYM to the X clipboard."
  (interactive "S Symbol: ")
  (let*((val (symbol-value sym))
        (str (format "%s" val)) )
    (set-x-clipboard-to-string str) ))
;; tests:
;; (progn (x-copy-symbol 'fill-column)         (insert-x-clipboard))
;; (progn (call-interactively #'x-copy-symbol) (insert-x-clipboard))

(defun x-clipboard-dwim ()
  "If the region is active, set the X clipboard, if not, insert it."
  (interactive)
  (call-interactively
   (if (use-region-p)
       #'set-x-clipboard
     #'insert-x-clipboard) ))

(defalias 'x  #'x-clipboard-dwim)
(defalias 'xo #'insert-x-clipboard)

(provide 'xsel)
;;; xsel.el ends here
-- 
underground experts united
https://dataswamp.org/~incal




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

end of thread, other threads:[~2021-11-12  7:12 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-11-12  6:13 gui-get Emanuel Berg via Users list for the GNU Emacs text editor
2021-11-12  6:22 ` gui-get Po Lu
2021-11-12  7:12   ` gui-get Emanuel Berg via Users list for the GNU Emacs text editor

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).