comparison lisp/term/x-win.el @ 44807:ca555c0c4e07

(x-select-text, x-cut-buffer-or-selection-value): Check if any of the available selection sources has new content and if so it will return that content.
author Pavel Janík <Pavel@Janik.cz>
date Wed, 24 Apr 2002 13:17:08 +0000
parents 606bb58da5c4
children b80d723ad7cc
comparison
equal deleted inserted replaced
44806:82c4effdcdc4 44807:ca555c0c4e07
1 ;;; x-win.el --- parse switches controlling interface with X window system 1 ;;; x-win.el --- parse switches controlling interface with X window system
2 2
3 ;; Copyright (C) 1993, 1994, 2001 Free Software Foundation, Inc. 3 ;; Copyright (C) 1993, 1994, 2001, 2002 Free Software Foundation, Inc.
4 4
5 ;; Author: FSF 5 ;; Author: FSF
6 ;; Keywords: terminals 6 ;; Keywords: terminals
7 7
8 ;; This file is part of GNU Emacs. 8 ;; This file is part of GNU Emacs.
1264 1264
1265 ;;;; Selections and cut buffers 1265 ;;;; Selections and cut buffers
1266 1266
1267 ;;; We keep track of the last text selected here, so we can check the 1267 ;;; We keep track of the last text selected here, so we can check the
1268 ;;; current selection against it, and avoid passing back our own text 1268 ;;; current selection against it, and avoid passing back our own text
1269 ;;; from x-cut-buffer-or-selection-value. 1269 ;;; from x-cut-buffer-or-selection-value. We track all three
1270 (defvar x-last-selected-text nil) 1270 ;;; seperately in case another X application only sets one of them
1271 ;;; (say the the cut buffer) we aren't fooled by the PRIMARY or
1272 ;;; CLIPBOARD selection staying the same.
1273 (defvar x-last-selected-text-clipboard nil
1274 "The value of the CLIPBOARD X selection last time we selected or
1275 pasted text.")
1276 (defvar x-last-selected-text-primary nil
1277 "The value of the PRIMARY X selection last time we selected or
1278 pasted text.")
1279 (defvar x-last-selected-text-cut nil
1280 "The vaue of the X cut buffer last time we selected or
1281 pasted text.")
1271 1282
1272 ;;; It is said that overlarge strings are slow to put into the cut buffer. 1283 ;;; It is said that overlarge strings are slow to put into the cut buffer.
1273 ;;; Note this value is overridden below. 1284 ;;; Note this value is overridden below.
1274 (defvar x-cut-buffer-max 20000 1285 (defvar x-cut-buffer-max 20000
1275 "Max number of characters to put in the cut buffer.") 1286 "Max number of characters to put in the cut buffer.")
1286 ;;; gildea@stop.mail-abuse.org says it's not desirable to put kills 1297 ;;; gildea@stop.mail-abuse.org says it's not desirable to put kills
1287 ;;; in the clipboard. 1298 ;;; in the clipboard.
1288 (defun x-select-text (text &optional push) 1299 (defun x-select-text (text &optional push)
1289 ;; Don't send the cut buffer too much text. 1300 ;; Don't send the cut buffer too much text.
1290 ;; It becomes slow, and if really big it causes errors. 1301 ;; It becomes slow, and if really big it causes errors.
1291 (if (< (length text) x-cut-buffer-max) 1302 (cond ((>= (length text) x-cut-buffer-max)
1303 (x-set-cut-buffer "" push)
1304 (setq x-last-selected-text-cut ""))
1305 (t
1292 (x-set-cut-buffer text push) 1306 (x-set-cut-buffer text push)
1293 (x-set-cut-buffer "" push)) 1307 (setq x-last-selected-text-cut text)))
1294 (x-set-selection 'PRIMARY text) 1308 (x-set-selection 'PRIMARY text)
1295 (if x-select-enable-clipboard 1309 (setq x-last-selected-text-primary text)
1296 (x-set-selection 'CLIPBOARD text)) 1310 (when x-select-enable-clipboard
1297 (setq x-last-selected-text text)) 1311 (x-set-selection 'CLIPBOARD text)
1312 (setq x-last-selected-text-clipboard text))
1313 )
1298 1314
1299 ;;; Return the value of the current X selection. 1315 ;;; Return the value of the current X selection.
1300 ;;; Consult the selection, then the cut buffer. Treat empty strings 1316 ;;; Consult the selection, and the cut buffer. Treat empty strings
1301 ;;; as if they were unset. 1317 ;;; as if they were unset.
1302 ;;; If this function is called twice and finds the same text, 1318 ;;; If this function is called twice and finds the same text,
1303 ;;; it returns nil the second time. This is so that a single 1319 ;;; it returns nil the second time. This is so that a single
1304 ;;; selection won't be added to the kill ring over and over. 1320 ;;; selection won't be added to the kill ring over and over.
1305 (defun x-cut-buffer-or-selection-value () 1321 (defun x-cut-buffer-or-selection-value ()
1306 (let (text) 1322 (let (clip-text primary-text cut-text)
1307 (when x-select-enable-clipboard 1323 (when x-select-enable-clipboard
1308 (if (null text) 1324 ;; Don't die if x-get-selection signals an error.
1325 (if (null clip-text)
1309 (condition-case c 1326 (condition-case c
1310 (setq text (x-get-selection 'CLIPBOARD 'COMPOUND_TEXT)) 1327 (setq clip-text (x-get-selection 'CLIPBOARD 'COMPOUND_TEXT))
1311 (error nil))) 1328 (error nil)))
1312 (if (null text) 1329 (if (null clip-text)
1313 (condition-case c 1330 (condition-case c
1314 (setq text (x-get-selection 'CLIPBOARD 'STRING)) 1331 (setq clip-text (x-get-selection 'CLIPBOARD 'STRING))
1315 (error nil))) 1332 (error nil)))
1316 (if (string= text "") (setq text nil))) 1333 (if (string= clip-text "") (setq clip-text nil))
1334
1335 ;; Check the CLIPBOARD selection for 'newness', is it different
1336 ;; from what we remebered them to be last time we did a
1337 ;; cut/paste operation.
1338 (setq clip-text
1339 (cond;; check clipboard
1340 ((or (not clip-text) (string= clip-text ""))
1341 (setq x-last-selected-text-clipboard nil))
1342 ((eq clip-text x-last-selected-text-clipboard) nil)
1343 ((string= clip-text x-last-selected-text-clipboard)
1344 ;; Record the newer string,
1345 ;; so subsequent calls can use the `eq' test.
1346 (setq x-last-selected-text-clipboard clip-text)
1347 nil)
1348 (t
1349 (setq x-last-selected-text-clipboard clip-text))))
1350 )
1317 1351
1318 ;; Don't die if x-get-selection signals an error. 1352 ;; Don't die if x-get-selection signals an error.
1319 (if (null text) 1353 (if (null primary-text)
1320 (condition-case c 1354 (condition-case c
1321 (setq text (x-get-selection 'PRIMARY 'COMPOUND_TEXT)) 1355 (setq primary-text (x-get-selection 'PRIMARY 'COMPOUND_TEXT))
1322 (error nil))) 1356 (error nil)))
1323 (if (null text) 1357 (if (null primary-text)
1324 (condition-case c 1358 (condition-case c
1325 (setq text (x-get-selection 'PRIMARY 'STRING)) 1359 (setq primary-text (x-get-selection 'PRIMARY 'STRING))
1326 (error nil))) 1360 (error nil)))
1327 (if (string= text "") (setq text nil)) 1361 ;; Check the PRIMARY selection for 'newness', is it different
1328 1362 ;; from what we remebered them to be last time we did a
1329 (or text (setq text (x-get-cut-buffer 0))) 1363 ;; cut/paste operation.
1330 (if (string= text "") (setq text nil)) 1364 (setq primary-text
1331 1365 (cond;; check primary selection
1332 (cond 1366 ((or (not primary-text) (string= primary-text ""))
1333 ((not text) nil) 1367 (setq x-last-selected-text-primary nil))
1334 ((eq text x-last-selected-text) nil) 1368 ((eq primary-text x-last-selected-text-primary) nil)
1335 ((string= text x-last-selected-text) 1369 ((string= primary-text x-last-selected-text-primary)
1336 ;; Record the newer string, so subsequent calls can use the `eq' test. 1370 ;; Record the newer string,
1337 (setq x-last-selected-text text) 1371 ;; so subsequent calls can use the `eq' test.
1372 (setq x-last-selected-text-primary primary-text)
1373 nil)
1374 (t
1375 (setq x-last-selected-text-primary primary-text))))
1376
1377 (setq cut-text (x-get-cut-buffer 0))
1378
1379 ;; Check the x cut buffer for 'newness', is it different
1380 ;; from what we remebered them to be last time we did a
1381 ;; cut/paste operation.
1382 (setq cut-text
1383 (cond;; check primary selection
1384 ((or (not cut-text) (string= cut-text ""))
1385 (setq x-last-selected-text-cut nil))
1386 ((eq cut-text x-last-selected-text-cut) nil)
1387 ((string= cut-text x-last-selected-text-cut)
1388 ;; Record the newer string,
1389 ;; so subsequent calls can use the `eq' test.
1390 (setq x-last-selected-text-cut cut-text)
1338 nil) 1391 nil)
1339 (t 1392 (t
1340 (setq x-last-selected-text text))))) 1393 (setq x-last-selected-text-cut cut-text))))
1394
1395 ;; At this point we have recorded the current values for the
1396 ;; selection from clipboard (if we are supposed to) primary,
1397 ;; and cut buffer. So return the first one that has changed
1398 ;; (which is the first non-null one).
1399 ;;
1400 ;; NOTE: There will be cases where more than one of these has
1401 ;; changed and the new values differ. This indicates that
1402 ;; something like the following has happened since the last time
1403 ;; we looked at the selections: Application X set all the
1404 ;; selections, then Application Y set only one or two of them (say
1405 ;; just the cut-buffer). In this case since we don't have
1406 ;; timestamps there is no way to know what the 'correct' value to
1407 ;; return is. The nice thing to do would be to tell the user we
1408 ;; saw multiple possible selections and ask the user which was the
1409 ;; one they wanted.
1410 ;; This code is still a big improvement because now the user can
1411 ;; futz with the current selection and get emacs to pay attention
1412 ;; to the cut buffer again (previously as soon as clipboard or
1413 ;; primary had been set the cut buffer would essentially never be
1414 ;; checked again).
1415 (or clip-text primary-text cut-text)
1416 ))
1341 1417
1342 1418
1343 ;;; Do the actual X Windows setup here; the above code just defines 1419 ;;; Do the actual X Windows setup here; the above code just defines
1344 ;;; functions and variables that we use now. 1420 ;;; functions and variables that we use now.
1345 1421