select.el 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. ;;; select.el --- lisp portion of standard selection support -*- lexical-binding:t -*-
  2. ;; Copyright (C) 1993-1994, 2001-2015 Free Software Foundation, Inc.
  3. ;; Maintainer: emacs-devel@gnu.org
  4. ;; Keywords: internal
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; Based partially on earlier release by Lucid.
  18. ;; The functionality here is divided in two parts:
  19. ;; - Low-level: gui-get-selection, gui-set-selection, gui-selection-owner-p,
  20. ;; gui-selection-exists-p are the backend-dependent functions meant to access
  21. ;; various kinds of selections (CLIPBOARD, PRIMARY, SECONDARY).
  22. ;; - Higher-level: gui-select-text and gui-selection-value go together to
  23. ;; access the general notion of "GUI selection" for interoperation with other
  24. ;; applications. This can use either the clipboard or the primary selection,
  25. ;; or both or none according to select-enable-clipboard/primary. These are
  26. ;; the default values of interprogram-cut/paste-function.
  27. ;; Additionally, there's gui-get-primary-selection which is used to get the
  28. ;; PRIMARY selection, specifically for mouse-yank-primary.
  29. ;;; Code:
  30. (defcustom selection-coding-system nil
  31. "Coding system for communicating with other programs.
  32. For MS-Windows and MS-DOS:
  33. When sending or receiving text via selection and clipboard, the text
  34. is encoded or decoded by this coding system. The default value is
  35. the current system default encoding on 9x/Me, `utf-16le-dos'
  36. \(Unicode) on NT/W2K/XP, and `iso-latin-1-dos' on MS-DOS.
  37. For X Windows:
  38. When sending text via selection and clipboard, if the target
  39. data-type matches with the type of this coding system, it is used
  40. for encoding the text. Otherwise (including the case that this
  41. variable is nil), a proper coding system is used as below:
  42. data-type coding system
  43. --------- -------------
  44. UTF8_STRING utf-8
  45. COMPOUND_TEXT compound-text-with-extensions
  46. STRING iso-latin-1
  47. C_STRING no-conversion
  48. When receiving text, if this coding system is non-nil, it is used
  49. for decoding regardless of the data-type. If this is nil, a
  50. proper coding system is used according to the data-type as above.
  51. See also the documentation of the variable `x-select-request-type' how
  52. to control which data-type to request for receiving text.
  53. The default value is nil."
  54. :type 'coding-system
  55. :group 'mule
  56. ;; Default was compound-text-with-extensions in 22.x (pre-unicode).
  57. :version "23.1"
  58. :set (lambda (symbol value)
  59. (set-selection-coding-system value)
  60. (set symbol value)))
  61. (defvar next-selection-coding-system nil
  62. "Coding system for the next communication with other programs.
  63. Usually, `selection-coding-system' is used for communicating with
  64. other programs (X Windows clients or MS Windows programs). But, if this
  65. variable is set, it is used for the next communication only.
  66. After the communication, this variable is set to nil.")
  67. ;; Only declared obsolete in 23.3.
  68. (define-obsolete-function-alias 'x-selection 'x-get-selection "at least 19.34")
  69. (defcustom select-enable-clipboard t
  70. "Non-nil means cutting and pasting uses the clipboard.
  71. This can be in addition to, but in preference to, the primary selection,
  72. if applicable (i.e. under X11)."
  73. :type 'boolean
  74. :group 'killing
  75. ;; The GNU/Linux version changed in 24.1, the MS-Windows version did not.
  76. :version "24.1")
  77. (define-obsolete-variable-alias 'x-select-enable-clipboard
  78. 'select-enable-clipboard "25.1")
  79. (defcustom select-enable-primary nil
  80. "Non-nil means cutting and pasting uses the primary selection
  81. The existence of a primary selection depends on the underlying GUI you use.
  82. E.g. it doesn't exist under MS-Windows."
  83. :type 'boolean
  84. :group 'killing
  85. :version "24.1")
  86. (define-obsolete-variable-alias 'x-select-enable-primary
  87. 'select-enable-primary "25.1")
  88. ;; We keep track of the last text selected here, so we can check the
  89. ;; current selection against it, and avoid passing back our own text
  90. ;; from gui-selection-value. We track both
  91. ;; separately in case another X application only sets one of them
  92. ;; we aren't fooled by the PRIMARY or CLIPBOARD selection staying the same.
  93. (defvar gui--last-selected-text-clipboard nil
  94. "The value of the CLIPBOARD selection last seen.")
  95. (defvar gui--last-selected-text-primary nil
  96. "The value of the PRIMARY selection last seen.")
  97. (defun gui-select-text (text)
  98. "Select TEXT, a string, according to the window system.
  99. if `select-enable-clipboard' is non-nil, copy TEXT to the system's clipboard.
  100. If `select-enable-primary' is non-nil, put TEXT in the primary selection.
  101. MS-Windows does not have a \"primary\" selection."
  102. (when select-enable-primary
  103. (gui-set-selection 'PRIMARY text)
  104. (setq gui--last-selected-text-primary text))
  105. (when select-enable-clipboard
  106. ;; When cutting, the selection is cleared and PRIMARY
  107. ;; set to the empty string. Prevent that, PRIMARY
  108. ;; should not be reset by cut (Bug#16382).
  109. (setq saved-region-selection text)
  110. (gui-set-selection 'CLIPBOARD text)
  111. (setq gui--last-selected-text-clipboard text)))
  112. (define-obsolete-function-alias 'x-select-text 'gui-select-text "25.1")
  113. (defcustom x-select-request-type nil
  114. "Data type request for X selection.
  115. The value is one of the following data types, a list of them, or nil:
  116. `COMPOUND_TEXT', `UTF8_STRING', `STRING', `TEXT'
  117. If the value is one of the above symbols, try only the specified type.
  118. If the value is a list of them, try each of them in the specified
  119. order until succeed.
  120. The value nil is the same as the list (UTF8_STRING COMPOUND_TEXT STRING)."
  121. :type '(choice (const :tag "Default" nil)
  122. (const COMPOUND_TEXT)
  123. (const UTF8_STRING)
  124. (const STRING)
  125. (const TEXT)
  126. (set :tag "List of values"
  127. (const COMPOUND_TEXT)
  128. (const UTF8_STRING)
  129. (const STRING)
  130. (const TEXT)))
  131. :group 'killing)
  132. ;; Get a selection value of type TYPE by calling gui-get-selection with
  133. ;; an appropriate DATA-TYPE argument decided by `x-select-request-type'.
  134. ;; The return value is already decoded. If gui-get-selection causes an
  135. ;; error, this function return nil.
  136. (defun gui--selection-value-internal (type)
  137. (let ((request-type (if (eq window-system 'x)
  138. (or x-select-request-type
  139. '(UTF8_STRING COMPOUND_TEXT STRING))
  140. 'STRING))
  141. text)
  142. (with-demoted-errors "gui-get-selection: %S"
  143. (if (consp request-type)
  144. (while (and request-type (not text))
  145. (setq text (gui-get-selection type (car request-type)))
  146. (setq request-type (cdr request-type)))
  147. (setq text (gui-get-selection type request-type))))
  148. (if text
  149. (remove-text-properties 0 (length text) '(foreign-selection nil) text))
  150. text))
  151. (defun gui-selection-value ()
  152. (let ((clip-text
  153. (when select-enable-clipboard
  154. (let ((text (gui--selection-value-internal 'CLIPBOARD)))
  155. (if (string= text "") (setq text nil))
  156. ;; Check the CLIPBOARD selection for 'newness', is it different
  157. ;; from what we remembered them to be last time we did a
  158. ;; cut/paste operation.
  159. (prog1
  160. (unless (equal text gui--last-selected-text-clipboard)
  161. text)
  162. (setq gui--last-selected-text-clipboard text)))))
  163. (primary-text
  164. (when select-enable-primary
  165. (let ((text (gui--selection-value-internal 'PRIMARY)))
  166. (if (string= text "") (setq text nil))
  167. ;; Check the PRIMARY selection for 'newness', is it different
  168. ;; from what we remembered them to be last time we did a
  169. ;; cut/paste operation.
  170. (prog1
  171. (unless (equal text gui--last-selected-text-primary)
  172. text)
  173. (setq gui--last-selected-text-primary text))))))
  174. ;; As we have done one selection, clear this now.
  175. (setq next-selection-coding-system nil)
  176. ;; At this point we have recorded the current values for the
  177. ;; selection from clipboard (if we are supposed to) and primary.
  178. ;; So return the first one that has changed
  179. ;; (which is the first non-null one).
  180. ;;
  181. ;; NOTE: There will be cases where more than one of these has
  182. ;; changed and the new values differ. This indicates that
  183. ;; something like the following has happened since the last time
  184. ;; we looked at the selections: Application X set all the
  185. ;; selections, then Application Y set only one of them.
  186. ;; In this case since we don't have
  187. ;; timestamps there is no way to know what the 'correct' value to
  188. ;; return is. The nice thing to do would be to tell the user we
  189. ;; saw multiple possible selections and ask the user which was the
  190. ;; one they wanted.
  191. (or clip-text primary-text)
  192. ))
  193. (define-obsolete-function-alias 'x-selection-value 'gui-selection-value "25.1")
  194. (defun x-get-clipboard ()
  195. "Return text pasted to the clipboard."
  196. (declare (obsolete gui-get-selection "25.1"))
  197. (gui-backend-get-selection 'CLIPBOARD 'STRING))
  198. (defun gui-get-primary-selection ()
  199. "Return the PRIMARY selection, or the best emulation thereof."
  200. (or (gui-get-selection 'PRIMARY)
  201. (and (fboundp 'w32-get-selection-value)
  202. (eq (framep (selected-frame)) 'w32)
  203. ;; MS-Windows emulates PRIMARY in x-get-selection, but only
  204. ;; within the Emacs session, so consult the clipboard if
  205. ;; primary is not found.
  206. (w32-get-selection-value))
  207. (error "No selection is available")))
  208. (define-obsolete-function-alias 'x-get-selection-value
  209. 'gui-get-primary-selection "25.1")
  210. ;;; Lower-level, backend dependent selection handling.
  211. (cl-defgeneric gui-backend-get-selection (_selection-symbol _target-type)
  212. "Return selected text.
  213. SELECTION-SYMBOL is typically `PRIMARY', `SECONDARY', or `CLIPBOARD'.
  214. \(Those are literal upper-case symbol names, since that's what X expects.)
  215. TARGET-TYPE is the type of data desired, typically `STRING'."
  216. nil)
  217. (cl-defgeneric gui-backend-set-selection (_selection _value)
  218. "Method to assert a selection of type SELECTION and value VALUE.
  219. SELECTION is a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'.
  220. If VALUE is nil and we own the selection SELECTION, disown it instead.
  221. Disowning it means there is no such selection.
  222. \(Those are literal upper-case symbol names, since that's what X expects.)
  223. VALUE is typically a string, or a cons of two markers, but may be
  224. anything that the functions on `selection-converter-alist' know about."
  225. nil)
  226. (cl-defgeneric gui-backend-selection-owner-p (_selection)
  227. "Whether the current Emacs process owns the given X Selection.
  228. The arg should be the name of the selection in question, typically one of
  229. the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'.
  230. \(Those are literal upper-case symbol names, since that's what X expects.)"
  231. nil)
  232. (cl-defgeneric gui-backend-selection-exists-p (_selection)
  233. "Whether there is an owner for the given X Selection.
  234. The arg should be the name of the selection in question, typically one of
  235. the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'.
  236. \(Those are literal upper-case symbol names, since that's what X expects.)"
  237. nil)
  238. (defun gui-get-selection (&optional type data-type)
  239. "Return the value of an X Windows selection.
  240. The argument TYPE (default `PRIMARY') says which selection,
  241. and the argument DATA-TYPE (default `STRING') says
  242. how to convert the data.
  243. TYPE may be any symbol \(but nil stands for `PRIMARY'). However,
  244. only a few symbols are commonly used. They conventionally have
  245. all upper-case names. The most often used ones, in addition to
  246. `PRIMARY', are `SECONDARY' and `CLIPBOARD'.
  247. DATA-TYPE is usually `STRING', but can also be one of the symbols
  248. in `selection-converter-alist', which see. This argument is
  249. ignored on NS, MS-Windows and MS-DOS."
  250. (let ((data (gui-backend-get-selection (or type 'PRIMARY)
  251. (or data-type 'STRING))))
  252. (when (and (stringp data)
  253. (setq data-type (get-text-property 0 'foreign-selection data)))
  254. (let ((coding (or next-selection-coding-system
  255. selection-coding-system
  256. (pcase data-type
  257. ('UTF8_STRING 'utf-8)
  258. ('COMPOUND_TEXT 'compound-text-with-extensions)
  259. ('C_STRING nil)
  260. ('STRING 'iso-8859-1)
  261. (_ (error "Unknown selection data type: %S"
  262. type))))))
  263. (setq data (if coding (decode-coding-string data coding)
  264. (string-to-multibyte data))))
  265. (setq next-selection-coding-system nil)
  266. (put-text-property 0 (length data) 'foreign-selection data-type data))
  267. data))
  268. (define-obsolete-function-alias 'x-get-selection 'gui-get-selection "25.1")
  269. (defun gui-set-selection (type data)
  270. "Make an X selection of type TYPE and value DATA.
  271. The argument TYPE (nil means `PRIMARY') says which selection, and
  272. DATA specifies the contents. TYPE must be a symbol. \(It can also
  273. be a string, which stands for the symbol with that name, but this
  274. is considered obsolete.) DATA may be a string, a symbol, an
  275. integer (or a cons of two integers or list of two integers).
  276. The selection may also be a cons of two markers pointing to the same buffer,
  277. or an overlay. In these cases, the selection is considered to be the text
  278. between the markers *at whatever time the selection is examined*.
  279. Thus, editing done in the buffer after you specify the selection
  280. can alter the effective value of the selection.
  281. The data may also be a vector of valid non-vector selection values.
  282. The return value is DATA.
  283. Interactively, this command sets the primary selection. Without
  284. prefix argument, it reads the selection in the minibuffer. With
  285. prefix argument, it uses the text of the region as the selection value.
  286. Note that on MS-Windows, primary and secondary selections set by Emacs
  287. are not available to other programs."
  288. (interactive (if (not current-prefix-arg)
  289. (list 'PRIMARY (read-string "Set text for pasting: "))
  290. (list 'PRIMARY (buffer-substring (region-beginning) (region-end)))))
  291. (if (stringp type) (setq type (intern type)))
  292. (or (gui--valid-simple-selection-p data)
  293. (and (vectorp data)
  294. (let ((valid t))
  295. (dotimes (i (length data))
  296. (or (gui--valid-simple-selection-p (aref data i))
  297. (setq valid nil)))
  298. valid))
  299. (signal 'error (list "invalid selection" data)))
  300. (or type (setq type 'PRIMARY))
  301. (gui-backend-set-selection type data)
  302. data)
  303. (define-obsolete-function-alias 'x-set-selection 'gui-set-selection "25.1")
  304. (defun gui--valid-simple-selection-p (data)
  305. (or (bufferp data)
  306. (and (consp data)
  307. (markerp (car data))
  308. (markerp (cdr data))
  309. (marker-buffer (car data))
  310. (buffer-live-p (marker-buffer (car data)))
  311. (eq (marker-buffer (car data))
  312. (marker-buffer (cdr data))))
  313. (stringp data)
  314. (and (overlayp data)
  315. (overlay-buffer data)
  316. (buffer-live-p (overlay-buffer data)))
  317. (symbolp data)
  318. (integerp data)))
  319. ;; Functions to convert the selection into various other selection types.
  320. ;; Every selection type that Emacs handles is implemented this way, except
  321. ;; for TIMESTAMP, which is a special case.
  322. (defun xselect--selection-bounds (value)
  323. "Return bounds of X selection value VALUE.
  324. The return value is a list (BEG END BUF) if VALUE is a cons of
  325. two markers or an overlay. Otherwise, it is nil."
  326. (cond ((bufferp value)
  327. (with-current-buffer value
  328. (when (mark t)
  329. (list (mark t) (point) value))))
  330. ((and (consp value)
  331. (markerp (car value))
  332. (markerp (cdr value)))
  333. (when (and (marker-buffer (car value))
  334. (buffer-name (marker-buffer (car value)))
  335. (eq (marker-buffer (car value))
  336. (marker-buffer (cdr value))))
  337. (list (marker-position (car value))
  338. (marker-position (cdr value))
  339. (marker-buffer (car value)))))
  340. ((overlayp value)
  341. (when (overlay-buffer value)
  342. (list (overlay-start value)
  343. (overlay-end value)
  344. (overlay-buffer value))))))
  345. (defun xselect--int-to-cons (n)
  346. (cons (ash n -16) (logand n 65535)))
  347. (defun xselect--encode-string (type str &optional can-modify)
  348. (when str
  349. ;; If TYPE is nil, this is a local request; return STR as-is.
  350. (if (null type)
  351. str
  352. ;; Otherwise, encode STR.
  353. (let ((coding (or next-selection-coding-system
  354. selection-coding-system)))
  355. (if coding
  356. (setq coding (coding-system-base coding)))
  357. (let ((inhibit-read-only t))
  358. ;; Suppress producing escape sequences for compositions.
  359. ;; But avoid modifying the string if it's a buffer name etc.
  360. (unless can-modify (setq str (substring str 0)))
  361. (remove-text-properties 0 (length str) '(composition nil) str)
  362. ;; For X selections, TEXT is a polymorphic target; choose
  363. ;; the actual type from `UTF8_STRING', `COMPOUND_TEXT',
  364. ;; `STRING', and `C_STRING'. On Nextstep, always use UTF-8
  365. ;; (see ns_string_to_pasteboard_internal in nsselect.m).
  366. (when (eq type 'TEXT)
  367. (cond
  368. ((featurep 'ns)
  369. (setq type 'UTF8_STRING))
  370. ((not (multibyte-string-p str))
  371. (setq type 'C_STRING))
  372. (t
  373. (let (non-latin-1 non-unicode eight-bit)
  374. (mapc #'(lambda (x)
  375. (if (>= x #x100)
  376. (if (< x #x110000)
  377. (setq non-latin-1 t)
  378. (if (< x #x3FFF80)
  379. (setq non-unicode t)
  380. (setq eight-bit t)))))
  381. str)
  382. (setq type (if (or non-unicode
  383. (and
  384. non-latin-1
  385. ;; If a coding is specified for
  386. ;; selection, and that is
  387. ;; compatible with COMPOUND_TEXT,
  388. ;; use it.
  389. coding
  390. (eq (coding-system-get coding :mime-charset)
  391. 'x-ctext)))
  392. 'COMPOUND_TEXT
  393. (if non-latin-1 'UTF8_STRING
  394. (if eight-bit 'C_STRING
  395. 'STRING))))))))
  396. (cond
  397. ((eq type 'UTF8_STRING)
  398. (if (or (not coding)
  399. (not (eq (coding-system-type coding) 'utf-8)))
  400. (setq coding 'utf-8))
  401. (setq str (encode-coding-string str coding)))
  402. ((eq type 'STRING)
  403. (if (or (not coding)
  404. (not (eq (coding-system-type coding) 'charset)))
  405. (setq coding 'iso-8859-1))
  406. (setq str (encode-coding-string str coding)))
  407. ((eq type 'COMPOUND_TEXT)
  408. (if (or (not coding)
  409. (not (eq (coding-system-type coding) 'iso-2022)))
  410. (setq coding 'compound-text-with-extensions))
  411. (setq str (encode-coding-string str coding)))
  412. ((eq type 'C_STRING)
  413. (setq str (string-make-unibyte str)))
  414. (t
  415. (error "Unknown selection type: %S" type)))))
  416. (setq next-selection-coding-system nil)
  417. (cons type str))))
  418. (defun xselect-convert-to-string (_selection type value)
  419. (let ((str (cond ((stringp value) value)
  420. ((setq value (xselect--selection-bounds value))
  421. (with-current-buffer (nth 2 value)
  422. (buffer-substring (nth 0 value)
  423. (nth 1 value)))))))
  424. (xselect--encode-string type str t)))
  425. (defun xselect-convert-to-length (_selection _type value)
  426. (let ((len (cond ((stringp value)
  427. (length value))
  428. ((setq value (xselect--selection-bounds value))
  429. (abs (- (nth 0 value) (nth 1 value)))))))
  430. (if len
  431. (xselect--int-to-cons len))))
  432. (defun xselect-convert-to-targets (_selection _type _value)
  433. ;; return a vector of atoms, but remove duplicates first.
  434. (let* ((all (cons 'TIMESTAMP
  435. (cons 'MULTIPLE
  436. (mapcar 'car selection-converter-alist))))
  437. (rest all))
  438. (while rest
  439. (cond ((memq (car rest) (cdr rest))
  440. (setcdr rest (delq (car rest) (cdr rest))))
  441. ((eq (car (cdr rest)) '_EMACS_INTERNAL) ; shh, it's a secret
  442. (setcdr rest (cdr (cdr rest))))
  443. (t
  444. (setq rest (cdr rest)))))
  445. (apply 'vector all)))
  446. (defun xselect-convert-to-delete (selection _type _value)
  447. (gui-backend-set-selection selection nil)
  448. ;; A return value of nil means that we do not know how to do this conversion,
  449. ;; and replies with an "error". A return value of NULL means that we have
  450. ;; done the conversion (and any side-effects) but have no value to return.
  451. 'NULL)
  452. (defun xselect-convert-to-filename (_selection _type value)
  453. (when (setq value (xselect--selection-bounds value))
  454. (xselect--encode-string 'TEXT (buffer-file-name (nth 2 value)))))
  455. (defun xselect-convert-to-charpos (_selection _type value)
  456. (when (setq value (xselect--selection-bounds value))
  457. (let ((beg (1- (nth 0 value))) ; zero-based
  458. (end (1- (nth 1 value))))
  459. (cons 'SPAN (vector (xselect--int-to-cons (min beg end))
  460. (xselect--int-to-cons (max beg end)))))))
  461. (defun xselect-convert-to-lineno (_selection _type value)
  462. (when (setq value (xselect--selection-bounds value))
  463. (with-current-buffer (nth 2 value)
  464. (let ((beg (line-number-at-pos (nth 0 value)))
  465. (end (line-number-at-pos (nth 1 value))))
  466. (cons 'SPAN (vector (xselect--int-to-cons (min beg end))
  467. (xselect--int-to-cons (max beg end))))))))
  468. (defun xselect-convert-to-colno (_selection _type value)
  469. (when (setq value (xselect--selection-bounds value))
  470. (with-current-buffer (nth 2 value)
  471. (let ((beg (progn (goto-char (nth 0 value)) (current-column)))
  472. (end (progn (goto-char (nth 1 value)) (current-column))))
  473. (cons 'SPAN (vector (xselect--int-to-cons (min beg end))
  474. (xselect--int-to-cons (max beg end))))))))
  475. (defun xselect-convert-to-os (_selection _type _size)
  476. (xselect--encode-string 'TEXT (symbol-name system-type)))
  477. (defun xselect-convert-to-host (_selection _type _size)
  478. (xselect--encode-string 'TEXT (system-name)))
  479. (defun xselect-convert-to-user (_selection _type _size)
  480. (xselect--encode-string 'TEXT (user-full-name)))
  481. (defun xselect-convert-to-class (_selection _type _size)
  482. "Convert selection to class.
  483. This function returns the string \"Emacs\"."
  484. "Emacs")
  485. ;; We do not try to determine the name Emacs was invoked with,
  486. ;; because it is not clean for a program's behavior to depend on that.
  487. (defun xselect-convert-to-name (_selection _type _size)
  488. "Convert selection to name.
  489. This function returns the string \"emacs\"."
  490. "emacs")
  491. (defun xselect-convert-to-integer (_selection _type value)
  492. (and (integerp value)
  493. (xselect--int-to-cons value)))
  494. (defun xselect-convert-to-atom (_selection _type value)
  495. (and (symbolp value) value))
  496. (defun xselect-convert-to-identity (_selection _type value) ; used internally
  497. (vector value))
  498. ;; Null target that tells clipboard managers we support SAVE_TARGETS
  499. ;; (see freedesktop.org Clipboard Manager spec).
  500. (defun xselect-convert-to-save-targets (selection _type _value)
  501. (when (eq selection 'CLIPBOARD)
  502. 'NULL))
  503. (setq selection-converter-alist
  504. '((TEXT . xselect-convert-to-string)
  505. (COMPOUND_TEXT . xselect-convert-to-string)
  506. (STRING . xselect-convert-to-string)
  507. (UTF8_STRING . xselect-convert-to-string)
  508. (TARGETS . xselect-convert-to-targets)
  509. (LENGTH . xselect-convert-to-length)
  510. (DELETE . xselect-convert-to-delete)
  511. (FILE_NAME . xselect-convert-to-filename)
  512. (CHARACTER_POSITION . xselect-convert-to-charpos)
  513. (LINE_NUMBER . xselect-convert-to-lineno)
  514. (COLUMN_NUMBER . xselect-convert-to-colno)
  515. (OWNER_OS . xselect-convert-to-os)
  516. (HOST_NAME . xselect-convert-to-host)
  517. (USER . xselect-convert-to-user)
  518. (CLASS . xselect-convert-to-class)
  519. (NAME . xselect-convert-to-name)
  520. (ATOM . xselect-convert-to-atom)
  521. (INTEGER . xselect-convert-to-integer)
  522. (SAVE_TARGETS . xselect-convert-to-save-targets)
  523. (_EMACS_INTERNAL . xselect-convert-to-identity)))
  524. (provide 'select)
  525. ;;; select.el ends here