select.el 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. ;;; select.el --- lisp portion of standard selection support
  2. ;; Copyright (C) 1993-1994, 2001-2012 Free Software Foundation, Inc.
  3. ;; Maintainer: FSF
  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. ;;; Code:
  19. (defcustom selection-coding-system nil
  20. "Coding system for communicating with other programs.
  21. For MS-Windows and MS-DOS:
  22. When sending or receiving text via selection and clipboard, the text
  23. is encoded or decoded by this coding system. The default value is
  24. the current system default encoding on 9x/Me, `utf-16le-dos'
  25. \(Unicode) on NT/W2K/XP, and `iso-latin-1-dos' on MS-DOS.
  26. For X Windows:
  27. When sending text via selection and clipboard, if the target
  28. data-type matches with the type of this coding system, it is used
  29. for encoding the text. Otherwise (including the case that this
  30. variable is nil), a proper coding system is used as below:
  31. data-type coding system
  32. --------- -------------
  33. UTF8_STRING utf-8
  34. COMPOUND_TEXT compound-text-with-extensions
  35. STRING iso-latin-1
  36. C_STRING no-conversion
  37. When receiving text, if this coding system is non-nil, it is used
  38. for decoding regardless of the data-type. If this is nil, a
  39. proper coding system is used according to the data-type as above.
  40. See also the documentation of the variable `x-select-request-type' how
  41. to control which data-type to request for receiving text.
  42. The default value is nil."
  43. :type 'coding-system
  44. :group 'mule
  45. ;; Default was compound-text-with-extensions in 22.x (pre-unicode).
  46. :version "23.1"
  47. :set (lambda (symbol value)
  48. (set-selection-coding-system value)
  49. (set symbol value)))
  50. (defvar next-selection-coding-system nil
  51. "Coding system for the next communication with other programs.
  52. Usually, `selection-coding-system' is used for communicating with
  53. other programs (X Windows clients or MS Windows programs). But, if this
  54. variable is set, it is used for the next communication only.
  55. After the communication, this variable is set to nil.")
  56. (declare-function x-get-selection-internal "xselect.c"
  57. (selection-symbol target-type &optional time-stamp terminal))
  58. ;; Only declared obsolete in 23.3.
  59. (define-obsolete-function-alias 'x-selection 'x-get-selection "at least 19.34")
  60. (defun x-get-selection (&optional type data-type)
  61. "Return the value of an X Windows selection.
  62. The argument TYPE (default `PRIMARY') says which selection,
  63. and the argument DATA-TYPE (default `STRING') says
  64. how to convert the data.
  65. TYPE may be any symbol \(but nil stands for `PRIMARY'). However,
  66. only a few symbols are commonly used. They conventionally have
  67. all upper-case names. The most often used ones, in addition to
  68. `PRIMARY', are `SECONDARY' and `CLIPBOARD'.
  69. DATA-TYPE is usually `STRING', but can also be one of the symbols
  70. in `selection-converter-alist', which see."
  71. (let ((data (x-get-selection-internal (or type 'PRIMARY)
  72. (or data-type 'STRING)))
  73. coding)
  74. (when (and (stringp data)
  75. (setq data-type (get-text-property 0 'foreign-selection data)))
  76. (setq coding (or next-selection-coding-system
  77. selection-coding-system
  78. (cond ((eq data-type 'UTF8_STRING)
  79. 'utf-8)
  80. ((eq data-type 'COMPOUND_TEXT)
  81. 'compound-text-with-extensions)
  82. ((eq data-type 'C_STRING)
  83. nil)
  84. ((eq data-type 'STRING)
  85. 'iso-8859-1)
  86. (t
  87. (error "Unknown selection data type: %S" type))))
  88. data (if coding (decode-coding-string data coding)
  89. (string-to-multibyte data)))
  90. (setq next-selection-coding-system nil)
  91. (put-text-property 0 (length data) 'foreign-selection data-type data))
  92. data))
  93. (defun x-get-clipboard ()
  94. "Return text pasted to the clipboard."
  95. (x-get-selection-internal 'CLIPBOARD 'STRING))
  96. (declare-function x-own-selection-internal "xselect.c"
  97. (selection-name selection-value &optional frame))
  98. (declare-function x-disown-selection-internal "xselect.c"
  99. (selection &optional time terminal))
  100. (defun x-set-selection (type data)
  101. "Make an X selection of type TYPE and value DATA.
  102. The argument TYPE (nil means `PRIMARY') says which selection, and
  103. DATA specifies the contents. TYPE must be a symbol. \(It can also
  104. be a string, which stands for the symbol with that name, but this
  105. is considered obsolete.) DATA may be a string, a symbol, an
  106. integer (or a cons of two integers or list of two integers).
  107. The selection may also be a cons of two markers pointing to the same buffer,
  108. or an overlay. In these cases, the selection is considered to be the text
  109. between the markers *at whatever time the selection is examined*.
  110. Thus, editing done in the buffer after you specify the selection
  111. can alter the effective value of the selection.
  112. The data may also be a vector of valid non-vector selection values.
  113. The return value is DATA.
  114. Interactively, this command sets the primary selection. Without
  115. prefix argument, it reads the selection in the minibuffer. With
  116. prefix argument, it uses the text of the region as the selection value.
  117. Note that on MS-Windows, primary and secondary selections set by Emacs
  118. are not available to other programs."
  119. (interactive (if (not current-prefix-arg)
  120. (list 'PRIMARY (read-string "Set text for pasting: "))
  121. (list 'PRIMARY (buffer-substring (region-beginning) (region-end)))))
  122. (if (stringp type) (setq type (intern type)))
  123. (or (x-valid-simple-selection-p data)
  124. (and (vectorp data)
  125. (let ((valid t)
  126. (i (1- (length data))))
  127. (while (>= i 0)
  128. (or (x-valid-simple-selection-p (aref data i))
  129. (setq valid nil))
  130. (setq i (1- i)))
  131. valid))
  132. (signal 'error (list "invalid selection" data)))
  133. (or type (setq type 'PRIMARY))
  134. (if data
  135. (x-own-selection-internal type data)
  136. (x-disown-selection-internal type))
  137. data)
  138. (defun x-valid-simple-selection-p (data)
  139. (or (bufferp data)
  140. (and (consp data)
  141. (markerp (car data))
  142. (markerp (cdr data))
  143. (marker-buffer (car data))
  144. (buffer-name (marker-buffer (car data)))
  145. (eq (marker-buffer (car data))
  146. (marker-buffer (cdr data))))
  147. (stringp data)
  148. (and (overlayp data)
  149. (overlay-buffer data)
  150. (buffer-name (overlay-buffer data)))
  151. (symbolp data)
  152. (integerp data)))
  153. ;; Functions to convert the selection into various other selection types.
  154. ;; Every selection type that Emacs handles is implemented this way, except
  155. ;; for TIMESTAMP, which is a special case.
  156. (defun xselect--selection-bounds (value)
  157. "Return bounds of X selection value VALUE.
  158. The return value is a list (BEG END BUF) if VALUE is a cons of
  159. two markers or an overlay. Otherwise, it is nil."
  160. (cond ((bufferp value)
  161. (with-current-buffer value
  162. (when (mark t)
  163. (list (mark t) (point) value))))
  164. ((and (consp value)
  165. (markerp (car value))
  166. (markerp (cdr value)))
  167. (when (and (marker-buffer (car value))
  168. (buffer-name (marker-buffer (car value)))
  169. (eq (marker-buffer (car value))
  170. (marker-buffer (cdr value))))
  171. (list (marker-position (car value))
  172. (marker-position (cdr value))
  173. (marker-buffer (car value)))))
  174. ((overlayp value)
  175. (when (overlay-buffer value)
  176. (list (overlay-start value)
  177. (overlay-end value)
  178. (overlay-buffer value))))))
  179. (defun xselect--int-to-cons (n)
  180. (cons (ash n -16) (logand n 65535)))
  181. (defun xselect--encode-string (type str &optional can-modify)
  182. (when str
  183. ;; If TYPE is nil, this is a local request; return STR as-is.
  184. (if (null type)
  185. str
  186. ;; Otherwise, encode STR.
  187. (let ((coding (or next-selection-coding-system
  188. selection-coding-system)))
  189. (if coding
  190. (setq coding (coding-system-base coding)))
  191. (let ((inhibit-read-only t))
  192. ;; Suppress producing escape sequences for compositions.
  193. ;; But avoid modifying the string if it's a buffer name etc.
  194. (unless can-modify (setq str (substring str 0)))
  195. (remove-text-properties 0 (length str) '(composition nil) str)
  196. ;; For X selections, TEXT is a polymorphic target; choose
  197. ;; the actual type from `UTF8_STRING', `COMPOUND_TEXT',
  198. ;; `STRING', and `C_STRING'. On Nextstep, always use UTF-8
  199. ;; (see ns_string_to_pasteboard_internal in nsselect.m).
  200. (when (eq type 'TEXT)
  201. (cond
  202. ((featurep 'ns)
  203. (setq type 'UTF8_STRING))
  204. ((not (multibyte-string-p str))
  205. (setq type 'C_STRING))
  206. (t
  207. (let (non-latin-1 non-unicode eight-bit)
  208. (mapc #'(lambda (x)
  209. (if (>= x #x100)
  210. (if (< x #x110000)
  211. (setq non-latin-1 t)
  212. (if (< x #x3FFF80)
  213. (setq non-unicode t)
  214. (setq eight-bit t)))))
  215. str)
  216. (setq type (if non-unicode 'COMPOUND_TEXT
  217. (if non-latin-1 'UTF8_STRING
  218. (if eight-bit 'C_STRING
  219. 'STRING))))))))
  220. (cond
  221. ((eq type 'UTF8_STRING)
  222. (if (or (not coding)
  223. (not (eq (coding-system-type coding) 'utf-8)))
  224. (setq coding 'utf-8))
  225. (setq str (encode-coding-string str coding)))
  226. ((eq type 'STRING)
  227. (if (or (not coding)
  228. (not (eq (coding-system-type coding) 'charset)))
  229. (setq coding 'iso-8859-1))
  230. (setq str (encode-coding-string str coding)))
  231. ((eq type 'COMPOUND_TEXT)
  232. (if (or (not coding)
  233. (not (eq (coding-system-type coding) 'iso-2022)))
  234. (setq coding 'compound-text-with-extensions))
  235. (setq str (encode-coding-string str coding)))
  236. ((eq type 'C_STRING)
  237. (setq str (string-make-unibyte str)))
  238. (t
  239. (error "Unknown selection type: %S" type)))))
  240. (setq next-selection-coding-system nil)
  241. (cons type str))))
  242. (defun xselect-convert-to-string (_selection type value)
  243. (let ((str (cond ((stringp value) value)
  244. ((setq value (xselect--selection-bounds value))
  245. (with-current-buffer (nth 2 value)
  246. (buffer-substring (nth 0 value)
  247. (nth 1 value)))))))
  248. (xselect--encode-string type str t)))
  249. (defun xselect-convert-to-length (_selection _type value)
  250. (let ((len (cond ((stringp value)
  251. (length value))
  252. ((setq value (xselect--selection-bounds value))
  253. (abs (- (nth 0 value) (nth 1 value)))))))
  254. (if len
  255. (xselect--int-to-cons len))))
  256. (defun xselect-convert-to-targets (_selection _type _value)
  257. ;; return a vector of atoms, but remove duplicates first.
  258. (let* ((all (cons 'TIMESTAMP
  259. (cons 'MULTIPLE
  260. (mapcar 'car selection-converter-alist))))
  261. (rest all))
  262. (while rest
  263. (cond ((memq (car rest) (cdr rest))
  264. (setcdr rest (delq (car rest) (cdr rest))))
  265. ((eq (car (cdr rest)) '_EMACS_INTERNAL) ; shh, it's a secret
  266. (setcdr rest (cdr (cdr rest))))
  267. (t
  268. (setq rest (cdr rest)))))
  269. (apply 'vector all)))
  270. (defun xselect-convert-to-delete (selection _type _value)
  271. (x-disown-selection-internal selection)
  272. ;; A return value of nil means that we do not know how to do this conversion,
  273. ;; and replies with an "error". A return value of NULL means that we have
  274. ;; done the conversion (and any side-effects) but have no value to return.
  275. 'NULL)
  276. (defun xselect-convert-to-filename (_selection _type value)
  277. (when (setq value (xselect--selection-bounds value))
  278. (xselect--encode-string 'TEXT (buffer-file-name (nth 2 value)))))
  279. (defun xselect-convert-to-charpos (_selection _type value)
  280. (when (setq value (xselect--selection-bounds value))
  281. (let ((beg (1- (nth 0 value))) ; zero-based
  282. (end (1- (nth 1 value))))
  283. (cons 'SPAN (vector (xselect--int-to-cons (min beg end))
  284. (xselect--int-to-cons (max beg end)))))))
  285. (defun xselect-convert-to-lineno (_selection _type value)
  286. (when (setq value (xselect--selection-bounds value))
  287. (with-current-buffer (nth 2 value)
  288. (let ((beg (line-number-at-pos (nth 0 value)))
  289. (end (line-number-at-pos (nth 1 value))))
  290. (cons 'SPAN (vector (xselect--int-to-cons (min beg end))
  291. (xselect--int-to-cons (max beg end))))))))
  292. (defun xselect-convert-to-colno (_selection _type value)
  293. (when (setq value (xselect--selection-bounds value))
  294. (with-current-buffer (nth 2 value)
  295. (let ((beg (progn (goto-char (nth 0 value)) (current-column)))
  296. (end (progn (goto-char (nth 1 value)) (current-column))))
  297. (cons 'SPAN (vector (xselect--int-to-cons (min beg end))
  298. (xselect--int-to-cons (max beg end))))))))
  299. (defun xselect-convert-to-os (_selection _type _size)
  300. (xselect--encode-string 'TEXT (symbol-name system-type)))
  301. (defun xselect-convert-to-host (_selection _type _size)
  302. (xselect--encode-string 'TEXT (system-name)))
  303. (defun xselect-convert-to-user (_selection _type _size)
  304. (xselect--encode-string 'TEXT (user-full-name)))
  305. (defun xselect-convert-to-class (_selection _type _size)
  306. "Convert selection to class.
  307. This function returns the string \"Emacs\"."
  308. "Emacs")
  309. ;; We do not try to determine the name Emacs was invoked with,
  310. ;; because it is not clean for a program's behavior to depend on that.
  311. (defun xselect-convert-to-name (_selection _type _size)
  312. "Convert selection to name.
  313. This function returns the string \"emacs\"."
  314. "emacs")
  315. (defun xselect-convert-to-integer (_selection _type value)
  316. (and (integerp value)
  317. (xselect--int-to-cons value)))
  318. (defun xselect-convert-to-atom (_selection _type value)
  319. (and (symbolp value) value))
  320. (defun xselect-convert-to-identity (_selection _type value) ; used internally
  321. (vector value))
  322. ;; Null target that tells clipboard managers we support SAVE_TARGETS
  323. ;; (see freedesktop.org Clipboard Manager spec).
  324. (defun xselect-convert-to-save-targets (selection _type _value)
  325. (when (eq selection 'CLIPBOARD)
  326. 'NULL))
  327. (setq selection-converter-alist
  328. '((TEXT . xselect-convert-to-string)
  329. (COMPOUND_TEXT . xselect-convert-to-string)
  330. (STRING . xselect-convert-to-string)
  331. (UTF8_STRING . xselect-convert-to-string)
  332. (TARGETS . xselect-convert-to-targets)
  333. (LENGTH . xselect-convert-to-length)
  334. (DELETE . xselect-convert-to-delete)
  335. (FILE_NAME . xselect-convert-to-filename)
  336. (CHARACTER_POSITION . xselect-convert-to-charpos)
  337. (LINE_NUMBER . xselect-convert-to-lineno)
  338. (COLUMN_NUMBER . xselect-convert-to-colno)
  339. (OWNER_OS . xselect-convert-to-os)
  340. (HOST_NAME . xselect-convert-to-host)
  341. (USER . xselect-convert-to-user)
  342. (CLASS . xselect-convert-to-class)
  343. (NAME . xselect-convert-to-name)
  344. (ATOM . xselect-convert-to-atom)
  345. (INTEGER . xselect-convert-to-integer)
  346. (SAVE_TARGETS . xselect-convert-to-save-targets)
  347. (_EMACS_INTERNAL . xselect-convert-to-identity)))
  348. (provide 'select)
  349. ;;; select.el ends here