summaryrefslogtreecommitdiff
path: root/lisp/term/x-win.el
blob: 8c6c75e7e226aba8ecc526d8ed448f6d2a63acde (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
;;; x-win.el --- parse relevant switches and set up for X  -*- lexical-binding:t -*-

;; Copyright (C) 1993-1994, 2001-2021 Free Software Foundation, Inc.

;; Author: FSF
;; Keywords: terminals, i18n

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; X-win.el: this file defines functions to initialize the X window
;; system and process X-specific command line parameters before
;; creating the first X frame.

;; Beginning in Emacs 23, the act of loading this file should not have
;; the side effect of initializing the window system or processing
;; command line arguments (this file is now loaded in loadup.el).  See
;; `handle-args-function' and `window-system-initialization' for more details.

;; startup.el will then examine startup files, and eventually call the hooks
;; which create the first window(s).

;;; Code:

;; These are the standard X switches from the Xt Initialize.c file of
;; Release 4.

;; Command line		Resource Manager string

;; +rv			*reverseVideo
;; +synchronous		*synchronous
;; -background		*background
;; -bd			*borderColor
;; -bg			*background
;; -bordercolor		*borderColor
;; -borderwidth		.borderWidth
;; -bw			.borderWidth
;; -display		.display
;; -fg			*foreground
;; -fn			*font
;; -font		*font
;; -foreground		*foreground
;; -geometry		.geometry
;; -iconic		.iconic
;; -name		.name
;; -reverse		*reverseVideo
;; -rv			*reverseVideo
;; -selectionTimeout    .selectionTimeout
;; -synchronous		*synchronous
;; -xrm

;; An alist of X options and the function which handles them.  See
;; ../startup.el.

(eval-when-compile (require 'cl-lib))

(if (not (fboundp 'x-create-frame))
    (error "%s: Loading x-win.el but not compiled for X" invocation-name))

(require 'term/common-win)
(require 'frame)
(require 'mouse)
(require 'scroll-bar)
(require 'select)
(require 'menu-bar)
(require 'fontset)
(require 'x-dnd)

(defvar x-invocation-args)
(defvar x-keysym-table)
(defvar x-selection-timeout)
(defvar x-session-id)
(defvar x-session-previous-id)

(defun x-handle-no-bitmap-icon (_switch)
  (setq default-frame-alist (cons '(icon-type) default-frame-alist)))

;; Handle the --parent-id option.
(defun x-handle-parent-id (switch)
  (or (consp x-invocation-args)
      (error "%s: missing argument to `%s' option" invocation-name switch))
  (setq initial-frame-alist (cons
                             (cons 'parent-id
                                   (string-to-number (car x-invocation-args)))
                             initial-frame-alist)
        x-invocation-args (cdr x-invocation-args)))

;; Handle the --smid switch.  This is used by the session manager
;; to give us back our session id we had on the previous run.
(defun x-handle-smid (switch)
  (or (consp x-invocation-args)
      (error "%s: missing argument to `%s' option" invocation-name switch))
  (setq x-session-previous-id (car x-invocation-args)
	x-invocation-args (cdr x-invocation-args)))

(defvar emacs-save-session-functions nil
  "Special hook run when a save-session event occurs.
The functions do not get any argument.
Functions can return non-nil to inform the session manager that the
window system shutdown should be aborted.

See also `emacs-session-save'.")

(defun emacs-session-filename (session-id)
  "Construct a filename to save the session in based on SESSION-ID.
Return a filename in `user-emacs-directory', unless the session file
already exists in the home directory."
  (let ((basename (concat "session." session-id)))
    (locate-user-emacs-file basename
                            (concat ".emacs-" basename))))

(defun emacs-session-save ()
  "This function is called when the window system is shutting down.
If this function returns non-nil, the window system shutdown is canceled.

When a session manager tells Emacs that the window system is shutting
down, this function is called.  It calls the functions in the hook
`emacs-save-session-functions'.  Functions are called with the current
buffer set to a temporary buffer.  Functions should use `insert' to insert
lisp code to save the session state.  The buffer is saved in a file in the
home directory of the user running Emacs.  The file is evaluated when
Emacs is restarted by the session manager.

If any of the functions returns non-nil, no more functions are called
and this function returns non-nil.  This will inform the session manager
that it should abort the window system shutdown."
  (let ((filename (emacs-session-filename x-session-id))
	(buf (get-buffer-create (concat " *SES " x-session-id))))
    (when (file-exists-p filename)
      (delete-file filename))
    (with-current-buffer buf
      (let ((cancel-shutdown (condition-case nil
				 ;; A return of t means cancel the shutdown.
				 (run-hook-with-args-until-success
				  'emacs-save-session-functions)
			       (error t))))
	(unless cancel-shutdown
	  (write-file filename))
	(kill-buffer buf)
	cancel-shutdown))))

(defun emacs-session-restore (previous-session-id)
  "Restore the Emacs session if started by a session manager.
The file saved by `emacs-session-save' is evaluated and deleted if it
exists."
  (let ((filename (emacs-session-filename previous-session-id)))
    (when (file-exists-p filename)
      (load-file filename)
      (delete-file filename)
      (message "Restored session data"))))




;;
;; Standard X cursor shapes, courtesy of Mr. Fox, who wanted ALL of them.
;;

(defconst x-pointer-X-cursor 0)
(defconst x-pointer-arrow 2)
(defconst x-pointer-based-arrow-down 4)
(defconst x-pointer-based-arrow-up 6)
(defconst x-pointer-boat 8)
(defconst x-pointer-bogosity 10)
(defconst x-pointer-bottom-left-corner 12)
(defconst x-pointer-bottom-right-corner 14)
(defconst x-pointer-bottom-side 16)
(defconst x-pointer-bottom-tee 18)
(defconst x-pointer-box-spiral 20)
(defconst x-pointer-center-ptr 22)
(defconst x-pointer-circle 24)
(defconst x-pointer-clock 26)
(defconst x-pointer-coffee-mug 28)
(defconst x-pointer-cross 30)
(defconst x-pointer-cross-reverse 32)
(defconst x-pointer-crosshair 34)
(defconst x-pointer-diamond-cross 36)
(defconst x-pointer-dot 38)
(defconst x-pointer-dotbox 40)
(defconst x-pointer-double-arrow 42)
(defconst x-pointer-draft-large 44)
(defconst x-pointer-draft-small 46)
(defconst x-pointer-draped-box 48)
(defconst x-pointer-exchange 50)
(defconst x-pointer-fleur 52)
(defconst x-pointer-gobbler 54)
(defconst x-pointer-gumby 56)
(defconst x-pointer-hand1 58)
(defconst x-pointer-hand2 60)
(defconst x-pointer-heart 62)
(defconst x-pointer-icon 64)
(defconst x-pointer-iron-cross 66)
(defconst x-pointer-left-ptr 68)
(defconst x-pointer-left-side 70)
(defconst x-pointer-left-tee 72)
(defconst x-pointer-leftbutton 74)
(defconst x-pointer-ll-angle 76)
(defconst x-pointer-lr-angle 78)
(defconst x-pointer-man 80)
(defconst x-pointer-middlebutton 82)
(defconst x-pointer-mouse 84)
(defconst x-pointer-pencil 86)
(defconst x-pointer-pirate 88)
(defconst x-pointer-plus 90)
(defconst x-pointer-question-arrow 92)
(defconst x-pointer-right-ptr 94)
(defconst x-pointer-right-side 96)
(defconst x-pointer-right-tee 98)
(defconst x-pointer-rightbutton 100)
(defconst x-pointer-rtl-logo 102)
(defconst x-pointer-sailboat 104)
(defconst x-pointer-sb-down-arrow 106)
(defconst x-pointer-sb-h-double-arrow 108)
(defconst x-pointer-sb-left-arrow 110)
(defconst x-pointer-sb-right-arrow 112)
(defconst x-pointer-sb-up-arrow 114)
(defconst x-pointer-sb-v-double-arrow 116)
(defconst x-pointer-shuttle 118)
(defconst x-pointer-sizing 120)
(defconst x-pointer-spider 122)
(defconst x-pointer-spraycan 124)
(defconst x-pointer-star 126)
(defconst x-pointer-target 128)
(defconst x-pointer-tcross 130)
(defconst x-pointer-top-left-arrow 132)
(defconst x-pointer-top-left-corner 134)
(defconst x-pointer-top-right-corner 136)
(defconst x-pointer-top-side 138)
(defconst x-pointer-top-tee 140)
(defconst x-pointer-trek 142)
(defconst x-pointer-ul-angle 144)
(defconst x-pointer-umbrella 146)
(defconst x-pointer-ur-angle 148)
(defconst x-pointer-watch 150)
(defconst x-pointer-xterm 152)
(defconst x-pointer-invisible 255)


;;;; Keysyms

(defun vendor-specific-keysyms (vendor)
  "Return the appropriate value of `system-key-alist' for VENDOR.
VENDOR is a string containing the name of the X Server's vendor,
as returned by `x-server-vendor'."
  (cond ((or (string-equal vendor "Hewlett-Packard Incorporated")
	     (string-equal vendor "Hewlett-Packard Company"))
	 '((  168 . mute-acute)
	   (  169 . mute-grave)
	   (  170 . mute-asciicircum)
	   (  171 . mute-diaeresis)
	   (  172 . mute-asciitilde)
	   (  175 . lira)
	   (  190 . guilder)
	   (  252 . block)
	   (  256 . longminus)
	   (65388 . reset)
	   (65389 . system)
	   (65390 . user)
	   (65391 . clearline)
	   (65392 . insertline)
	   (65393 . deleteline)
	   (65394 . insertchar)
	   (65395 . deletechar)
	   (65396 . backtab)
	   (65397 . kp-backtab)))
	;; Fixme: What about non-X11/NeWS sun server?
	((or (string-equal vendor "X11/NeWS - Sun Microsystems Inc.")
	     (string-equal vendor "X Consortium"))
	 '((392976 . f36)
	   (392977 . f37)
	   (393056 . req)
	   ;; These are for Sun under X11R6
	   (393072 . props)
	   (393073 . front)
	   (393074 . copy)
	   (393075 . open)
	   (393076 . paste)
	   (393077 . cut)))
	(t
	 ;; This is used by DEC's X server.
	 '((65280 . remove)))))

;; Latin-1
(let ((i 160))
  (while (< i 256)
    (puthash i i x-keysym-table)
    (setq i (1+ i))))

;; Table from Kuhn's proposed additions to the `KEYSYM Encoding'
;; appendix to the X protocol definition.  As indicated, some of these
;; have been corrected using information found in keysymdef.h which on
;; a typical system is installed at /usr/include/X11/keysymdef.h.  The
;; version used here is from xorgproto version 2019.1 found here:
;; https://gitlab.freedesktop.org/xorg/proto/xorgproto/blob/e0bba743ae7c549c58f92677b239ec7878548228/include/X11/keysymdef.h
(dolist
     (pair
      '(
	;; Latin-2
	(#x1a1 . )
	(#x1a2 . )
	(#x1a3 . )
	(#x1a5 . )
	(#x1a6 . )
	(#x1a9 . )
	(#x1aa . )
	(#x1ab . )
	(#x1ac . )
	(#x1ae . )
	(#x1af . )
	(#x1b1 . )
	(#x1b2 . )
	(#x1b3 . )
	(#x1b5 . )
	(#x1b6 . )
	(#x1b7 . )
	(#x1b9 . )
	(#x1ba . )
	(#x1bb . )
	(#x1bc . )
	(#x1bd . )
	(#x1be . )
	(#x1bf . )
	(#x1c0 . )
	(#x1c3 . )
	(#x1c5 . )
	(#x1c6 . )
	(#x1c8 . )
	(#x1ca . )
	(#x1cc . )
	(#x1cf . )
	(#x1d0 . )
	(#x1d1 . )
	(#x1d2 . )
	(#x1d5 . )
	(#x1d8 . )
	(#x1d9 . )
	(#x1db . )
	(#x1de . )
	(#x1e0 . )
	(#x1e3 . )
	(#x1e5 . )
	(#x1e6 . )
	(#x1e8 . )
	(#x1ea . )
	(#x1ec . )
	(#x1ef . )
	(#x1f0 . )
	(#x1f1 . )
	(#x1f2 . )
	(#x1f5 . )
	(#x1f8 . )
	(#x1f9 . )
	(#x1fb . )
	(#x1fe . )
	(#x1ff . )
	;; Latin-3
	(#x2a1 . )
	(#x2a6 . )
	(#x2a9 . )
	(#x2ab . )
	(#x2ac . )
	(#x2b1 . )
	(#x2b6 . )
	(#x2b9 . )
	(#x2bb . )
	(#x2bc . )
	(#x2c5 . )
	(#x2c6 . )
	(#x2d5 . )
	(#x2d8 . )
	(#x2dd . )
	(#x2de . )
	(#x2e5 . )
	(#x2e6 . )
	(#x2f5 . )
	(#x2f8 . )
	(#x2fd . )
	(#x2fe . )
	;; Latin-4
	(#x3a2 . )
	(#x3a3 . )
	(#x3a5 . )
	(#x3a6 . )
	(#x3aa . )
	(#x3ab . )
	(#x3ac . )
	(#x3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ミ)
	(#x4d1 . ?ム)
	(#x4d2 . ?メ)
	(#x4d3 . ?モ)
	(#x4d4 . ?ヤ)
	(#x4d5 . ?ユ)
	(#x4d6 . ?ヨ)
	(#x4d7 . ?ラ)
	(#x4d8 . ?リ)
	(#x4d9 . ?ル)
	(#x4da . ?レ)
	(#x4db . ?ロ)
	(#x4dc . ?ワ)
	(#x4dd . ?ン)
	(#x4de . ?゛)
	(#x4df . ?゜)
	;; Arabic
	(#x5ac . )
	(#x5bb . )
	(#x5bf . )
	(#x5c1 . )
	(#x5c2 . )
	(#x5c3 . )
	(#x5c4 . )
	(#x5c5 . )
	(#x5c6 . )
	(#x5c7 . )
	(#x5c8 . )
	(#x5c9 . )
	(#x5ca . )
	(#x5cb . )
	(#x5cc . )
	(#x5cd . )
	(#x5ce . )
	(#x5cf . )
	(#x5d0 . )
	(#x5d1 . )
	(#x5d2 . )
	(#x5d3 . )
	(#x5d4 . )
	(#x5d5 . )
	(#x5d6 . )
	(#x5d7 . )
	(#x5d8 . )
	(#x5d9 . )
	(#x5da . )
	(#x5e0 . )
	(#x5e1 . )
	(#x5e2 . )
	(#x5e3 . )
	(#x5e4 . )
	(#x5e5 . )
	(#x5e6 . )
	(#x5e7 . )
	(#x5e8 . )
	(#x5e9 . )
	(#x5ea . )
	(#x5eb . )
	(#x5ec . )
	(#x5ed . )
	(#x5ee . )
	(#x5ef . )
	(#x5f0 . )
	(#x5f1 . )
	(#x5f2 . )
	;; Cyrillic
	(#x680 . )
	(#x681 . )
	(#x682 . )
	(#x683 . )
	(#x684 . )
	(#x685 . )
	(#x686 . )
	(#x687 . )
	(#x688 . )
	(#x689 . )
	(#x68a . )
	(#x68c . )
	(#x68d . )
	(#x68e . )
	(#x68f . )
	(#x690 . )
	(#x691 . )
	(#x692 . )
	(#x693 . )
	(#x694 . )
	(#x695 . )
	(#x696 . )
	(#x697 . )
	(#x698 . )
	(#x699 . )
	(#x69a . )
	(#x69c . )
	(#x69d . )
	(#x69e . )
	(#x69f . )
	(#x6a1 . )
	(#x6a2 . )
	(#x6a3 . )
	(#x6a4 . )
	(#x6a5 . )
	(#x6a6 . )
	(#x6a7 . )
	(#x6a8 . )
	(#x6a9 . )
	(#x6aa . )
	(#x6ab . )
	(#x6ac . )
	(#x6ad . ) ;; Source: keysymdef.h
	(#x6ae . )
	(#x6af . )
	(#x6b0 . ?№)
	(#x6b1 . )
	(#x6b2 . )
	(#x6b3 . )
	(#x6b4 . )
	(#x6b5 . )
	(#x6b6 . )
	(#x6b7 . )
	(#x6b8 . )
	(#x6b9 . )
	(#x6ba . )
	(#x6bb . )
	(#x6bc . )
	(#x6bd . ) ;; Source: keysymdef.h
	(#x6be . )
	(#x6bf . )
	(#x6c0 . )
	(#x6c1 . )
	(#x6c2 . )
	(#x6c3 . )
	(#x6c4 . )
	(#x6c5 . )
	(#x6c6 . )
	(#x6c7 . )
	(#x6c8 . )
	(#x6c9 . )
	(#x6ca . )
	(#x6cb . )
	(#x6cc . )
	(#x6cd . )
	(#x6ce . )
	(#x6cf . )
	(#x6d0 . ?п)
	(#x6d1 . )
	(#x6d2 . )
	(#x6d3 . )
	(#x6d4 . )
	(#x6d5 . )
	(#x6d6 . )
	(#x6d7 . )
	(#x6d8 . )
	(#x6d9 . )
	(#x6da . )
	(#x6db . )
	(#x6dc . )
	(#x6dd . )
	(#x6de . )
	(#x6df . )
	(#x6e0 . )
	(#x6e1 . )
	(#x6e2 . )
	(#x6e3 . )
	(#x6e4 . )
	(#x6e5 . )
	(#x6e6 . )
	(#x6e7 . )
	(#x6e8 . )
	(#x6e9 . )
	(#x6ea . )
	(#x6eb . )
	(#x6ec . )
	(#x6ed . )
	(#x6ee . )
	(#x6ef . )
	(#x6f0 . )
	(#x6f1 . )
	(#x6f2 . )
	(#x6f3 . )
	(#x6f4 . )
	(#x6f5 . )
	(#x6f6 . )
	(#x6f7 . )
	(#x6f8 . )
	(#x6f9 . )
	(#x6fa . )
	(#x6fb . )
	(#x6fc . )
	(#x6fd . )
	(#x6fe . )
	(#x6ff . )
	;; Greek
	(#x7a1 . )
	(#x7a2 . )
	(#x7a3 . )
	(#x7a4 . )
	(#x7a5 . )
	(#x7a7 . )
	(#x7a8 . )
	(#x7a9 . )
	(#x7ab . )
	(#x7ae . )
	(#x7af . ?―)
	(#x7b1 . )
	(#x7b2 . )
	(#x7b3 . )
	(#x7b4 . )
	(#x7b5 . )
	(#x7b6 . )
	(#x7b7 . )
	(#x7b8 . )
	(#x7b9 . )
	(#x7ba . )
	(#x7bb . )
	(#x7c1 . )
	(#x7c2 . )
	(#x7c3 . )
	(#x7c4 . )
	(#x7c5 . )
	(#x7c6 . )
	(#x7c7 . )
	(#x7c8 . )
	(#x7c9 . )
	(#x7ca . )
	(#x7cb . )
	(#x7cc . )
	(#x7cd . )
	(#x7ce . )
	(#x7cf . )
	(#x7d0 . )
	(#x7d1 . )
	(#x7d2 . )
	(#x7d4 . )
	(#x7d5 . )
	(#x7d6 . )
	(#x7d7 . )
	(#x7d8 . )
	(#x7d9 . )
	(#x7e1 . )
	(#x7e2 . )
	(#x7e3 . )
	(#x7e4 . )
	(#x7e5 . )
	(#x7e6 . )
	(#x7e7 . )
	(#x7e8 . )
	(#x7e9 . )
	(#x7ea . )
	(#x7eb . )
	(#x7ec . )
	(#x7ed . )
	(#x7ee . )
	(#x7ef . ?ο)
	(#x7f0 . )
	(#x7f1 . )
	(#x7f2 . )
	(#x7f3 . )
	(#x7f4 . )
	(#x7f5 . )
	(#x7f6 . )
	(#x7f7 . )
	(#x7f8 . )
	(#x7f9 . )
	 ;; Technical
	(#x8a1 . ?⎷)
	(#x8a2 . ?┌)
	(#x8a3 . ?─)
	(#x8a4 . ?⌠)
	(#x8a5 . ?⌡)
	(#x8a6 . ?│)
	(#x8a7 . ?⎡)
	(#x8a8 . ?⎣)
	(#x8a9 . ?⎤)
	(#x8aa . ?⎦)
	(#x8ab . ?⎛)
	(#x8ac . ?⎝)
	(#x8ad . ?⎞)
	(#x8ae . ?⎠)
	(#x8af . ?⎨)
	(#x8b0 . ?⎬)
	(#x8bc . ?≤)
	(#x8bd . ?≠)
	(#x8be . ?≥)
	(#x8bf . ?∫)
	(#x8c0 . ?∴)
	(#x8c1 . ?∝)
	(#x8c2 . ?∞)
	(#x8c5 . ?∇)
	(#x8c8 . ?∼)
	(#x8c9 . ?≃)
	(#x8cd . ?⇔)
	(#x8ce . ?⇒)
	(#x8cf . ?≡)
	(#x8d6 . ?√)
	(#x8da . ?⊂)
	(#x8db . ?⊃)
	(#x8dc . ?∩)
	(#x8dd . ?∪)
	(#x8de . ?∧)
	(#x8df . ?∨)
	(#x8ef . ?∂)
	(#x8f6 . )
	(#x8fb . ?←)
	(#x8fc . ?↑)
	(#x8fd . ?→)
	(#x8fe . ?↓)
	;; Special
	(#x9e0 . ?◆)
	(#x9e1 . ?▒)
	(#x9e2 . ?␉)
	(#x9e3 . ?␌)
	(#x9e4 . ?␍)
	(#x9e5 . ?␊)
	(#x9e8 . ?␤)
	(#x9e9 . ?␋)
	(#x9ea . ?┘)
	(#x9eb . ?┐)
	(#x9ec . ?┌)
	(#x9ed . ?└)
	(#x9ee . ?┼)
	(#x9ef . ?⎺)
	(#x9f0 . ?⎻)
	(#x9f1 . ?─)
	(#x9f2 . ?⎼)
	(#x9f3 . ?⎽)
	(#x9f4 . ?├)
	(#x9f5 . ?┤)
	(#x9f6 . ?┴)
	(#x9f7 . ?┬)
	(#x9f8 . ?│)
	;; Publishing
	(#xaa1 . ? )
	(#xaa2 . ? )
	(#xaa3 . ? )
	(#xaa4 . ? )
	(#xaa5 . ? )
	(#xaa6 . ? )
	(#xaa7 . ? )
	(#xaa8 . ? )
	(#xaa9 . ?—)
	(#xaaa . ?–)
	(#xaac . ?␣) ;; Source: keysymdef.h
	(#xaae . ?…)
	(#xaaf . ?‥)
	(#xab0 . ?⅓)
	(#xab1 . ?⅔)
	(#xab2 . ?⅕)
	(#xab3 . ?⅖)
	(#xab4 . ?⅗)
	(#xab5 . ?⅘)
	(#xab6 . ?⅙)
	(#xab7 . ?⅚)
	(#xab8 . ?℅)
	(#xabb . ?‒)
	;; In keysymdef.h, the keysyms 0xabc and 0xabe are listed as
	;; U+27E8 and U+27E9 respectively.  However, the parentheses
	;; indicate that these mappings are deprecated legacy keysyms
	;; that are either not one-to-one or semantically unclear.  In
	;; order to not introduce any incompatibility with possible
	;; existing workflows that expect these keysyms to map as they
	;; currently do, to 0x2329 and 0x232a, respectively, they are
	;; left as they are.  In particular, PuTTY is known to agree
	;; with this mapping.
	(#xabc . ?〈)
	(#xabd . ?.) ;; Source: keysymdef.h
	(#xabe . ?〉)
	(#xac3 . ?⅛)
	(#xac4 . ?⅜)
	(#xac5 . ?⅝)
	(#xac6 . ?⅞)
	(#xac9 . ?™)
	(#xaca . ?☓)
	(#xacc . ?◁)
	(#xacd . ?▷)
	(#xace . ?○)
	(#xacf . ?▯)
	(#xad0 . ?‘)
	(#xad1 . ?’)
	(#xad2 . ?“)
	(#xad3 . ?”)
	(#xad4 . ?℞)
	(#xad5 . ?‰) ;; Source: keysymdef.h
	(#xad6 . ?′)
	(#xad7 . ?″)
	(#xad9 . ?✝)
	(#xadb . ?▬)
	(#xadc . ?◀)
	(#xadd . ?▶)
	(#xade . ?●)
	(#xadf . ?▮)
	(#xae0 . ?◦)
	(#xae1 . ?▫)
	(#xae2 . ?▭)
	(#xae3 . ?△)
	(#xae4 . ?▽)
	(#xae5 . ?☆)
	(#xae6 . ?•)
	(#xae7 . ?▪)
	(#xae8 . ?▲)
	(#xae9 . ?▼)
	(#xaea . ?☜)
	(#xaeb . ?☞)
	(#xaec . ?♣)
	(#xaed . ?♦)
	(#xaee . ?♥)
	(#xaf0 . ?✠)
	(#xaf1 . ?†)
	(#xaf2 . ?‡)
	(#xaf3 . ?✓)
	(#xaf4 . ?✗)
	(#xaf5 . ?♯)
	(#xaf6 . ?♭)
	(#xaf7 . ?♂)
	(#xaf8 . ?♀)
	(#xaf9 . ?☎)
	(#xafa . ?⌕)
	(#xafb . ?℗)
	(#xafc . ?‸)
	(#xafd . ?‚)
	(#xafe . ?„)
	;; APL
	(#xba3 . ?<)
	(#xba6 . ?>)
	(#xba8 . ?∨)
	(#xba9 . ?∧)
	(#xbc0 . )
	;; Source for #xbc2: keysymdef.h.  Note that the
	;; `KEYSYM Encoding' appendix to the X protocol definition is
	;; incorrect.
	(#xbc2 . ?⊤)
	(#xbc3 . ?∩)
	(#xbc4 . ?⌊)
	(#xbc6 . ?_)
	(#xbca . ?∘)
	(#xbcc . ?⎕)
	;; Source for #xbce: keysymdef.h.  Note that the
	;; `KEYSYM Encoding' appendix to the X protocol definition is
	;; incorrect.
	(#xbce . ?⊥)
	(#xbcf . ?○)
	(#xbd3 . ?⌈)
	(#xbd6 . ?∪)
	(#xbd8 . ?⊃)
	(#xbda . ?⊂)
	;; Source for #xbdc and #xbfc: keysymdef.h.  Note that the
	;; `KEYSYM Encoding' appendix to the X protocol definition is
	;; incorrect.
	(#xbdc . ?⊣)
	(#xbfc . ?⊢)
	;; Hebrew
	(#xcdf . ?‗)
	(#xce0 . )
	(#xce1 . )
	(#xce2 . )
	(#xce3 . )
	(#xce4 . )
	(#xce5 . )
	(#xce6 . )
	(#xce7 . )
	(#xce8 . )
	(#xce9 . )
	(#xcea . )
	(#xceb . )
	(#xcec . )
	(#xced . )
	(#xcee . )
	(#xcef . )
	(#xcf0 . )
	(#xcf1 . )
	(#xcf2 . )
	(#xcf3 . )
	(#xcf4 . )
	(#xcf5 . )
	(#xcf6 . )
	(#xcf7 . )
	(#xcf8 . )
	(#xcf9 . )
	(#xcfa . )
	;; Thai
	(#xda1 . ?ก)
	(#xda2 . ?ข)
	(#xda3 . ?ฃ)
	(#xda4 . ?ค)
	(#xda5 . ?ฅ)
	(#xda6 . ?ฆ)
	(#xda7 . ?ง)
	(#xda8 . ?จ)
	(#xda9 . ?ฉ)
	(#xdaa . ?ช)
	(#xdab . ?ซ)
	(#xdac . ?ฌ)
	(#xdad . ?ญ)
	(#xdae . ?ฎ)
	(#xdaf . ?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?ᆱ)
	(#xede . ?ᆲ)
	(#xedf . ?ᆳ)
	(#xee0 . ?ᆴ)
	(#xee1 . ?ᆵ)
	(#xee2 . ?ᆶ)
	(#xee3 . ?ᆷ)
	(#xee4 . ?ᆸ)
	(#xee5 . ?ᆹ)
	(#xee6 . ?ᆺ)
	(#xee7 . ?ᆻ)
	(#xee8 . ?ᆼ)
	(#xee9 . ?ᆽ)
	(#xeea . ?ᆾ)
	(#xeeb . ?ᆿ)
	(#xeec . ?ᇀ)
	(#xeed . ?ᇁ)
	(#xeee . ?ᇂ)
	(#xeef . ?ㅭ)
	(#xef0 . ?ㅱ)
	(#xef1 . ?ㅸ)
	(#xef2 . ?ㅿ)
	(#xef3 . ?ㆁ)
	(#xef4 . ?ㆄ)
	(#xef5 . ?ㆆ)
	(#xef6 . ?ㆍ)
	(#xef7 . ?ㆎ)
	(#xef8 . ?ᇫ)
	(#xef9 . ?ᇰ)
	(#xefa . ?ᇹ)
	(#xeff . ?₩)
	;; Latin-5
	;; Latin-6
	;; Latin-7
	;; Latin-8
	;; Latin-9
	(#x13bc . )
	(#x13bd . )
	(#x13be . )
	;; Currency
	(#x20a0 . ?₠)
	(#x20a1 . ?₡)
	(#x20a2 . ?₢)
	(#x20a3 . ?₣)
	(#x20a4 . ?₤)
	(#x20a5 . ?₥)
	(#x20a6 . ?₦)
	(#x20a7 . ?₧)
	(#x20a8 . ?₨)
	(#x20aa . ?₪)
	(#x20ab . ?₫)
	(#x20ac . ?€)))
  (puthash (car pair) (cdr pair) x-keysym-table))

;; The following keysym codes for graphics are listed in the document
;; as not having unicodes available:

;; #x08b1	TOP LEFT SUMMATION	Technical
;; #x08b2	BOTTOM LEFT SUMMATION	Technical
;; #x08b3	TOP VERTICAL SUMMATION CONNECTOR	Technical
;; #x08b4	BOTTOM VERTICAL SUMMATION CONNECTOR	Technical
;; #x08b5	TOP RIGHT SUMMATION	Technical
;; #x08b6	BOTTOM RIGHT SUMMATION	Technical
;; #x08b7	RIGHT MIDDLE SUMMATION	Technical
;; #x0aac	SIGNIFICANT BLANK SYMBOL	Publish
;; #x0abd	DECIMAL POINT	Publish
;; #x0abf	MARKER	Publish
;; #x0acb	TRADEMARK SIGN IN CIRCLE	Publish
;; #x0ada	HEXAGRAM	Publish
;; #x0aff	CURSOR	Publish
;; #x0dde	THAI MAIHANAKAT	Thai

;; However, keysymdef.h does have mappings for #x0aac and #x0abd, which
;; are used above.


;;;; Selections

(define-obsolete-function-alias 'x-cut-buffer-or-selection-value
  'x-selection-value "24.1")

;; Arrange for the kill and yank functions to set and check the clipboard.

(defun x-clipboard-yank ()
  "Insert the clipboard contents, or the last stretch of killed text."
  (declare (obsolete clipboard-yank "25.1"))
  (interactive "*")
  (let ((clipboard-text (gui--selection-value-internal 'CLIPBOARD))
	(select-enable-clipboard t))
    (if (and clipboard-text (> (length clipboard-text) 0))
	(kill-new clipboard-text))
    (yank)))

(declare-function accelerate-menu "xmenu.c" (&optional frame) t)

(defun x-menu-bar-open (&optional frame)
  "Open the menu bar if it is shown.
`popup-menu' is used if it is off."
  (interactive "i")
  (cond
   ((and (not (zerop (or (frame-parameter nil 'menu-bar-lines) 0)))
	 (fboundp 'accelerate-menu))
    (accelerate-menu frame))
   (t
    (popup-menu (mouse-menu-bar-map) last-nonmenu-event))))


;;; Window system initialization.

(defun x-win-suspend-error ()
  "Report an error when a suspend is attempted.
This returns an error if any Emacs frames are X frames."
  ;; Don't allow suspending if any of the frames are X frames.
  (if (memq 'x (mapcar #'window-system (frame-list)))
      (error "Cannot suspend Emacs while an X GUI frame exists")))

(defvar x-initialized nil
  "Non-nil if the X window system has been initialized.")

(declare-function x-open-connection "xfns.c"
		  (display &optional xrm-string must-succeed))
(declare-function x-server-max-request-size "xfns.c" (&optional terminal))
(declare-function x-get-resource "frame.c"
		  (attribute class &optional component subclass))
(declare-function x-parse-geometry "frame.c" (string))
(defvar x-resource-name)
(defvar x-display-name)
(defvar x-command-line-resources)

(cl-defmethod window-system-initialization (&context (window-system x)
                                            &optional display)
  "Initialize Emacs for X frames and open the first connection to an X server."
  (cl-assert (not x-initialized))

  ;; Make sure we have a valid resource name.
  (or (stringp x-resource-name)
      (let (i)
	(setq x-resource-name (copy-sequence invocation-name))

	;; Change any . or * characters in x-resource-name to hyphens,
	;; so as not to choke when we use it in X resource queries.
	(while (setq i (string-match "[.*]" x-resource-name))
	  (aset x-resource-name i ?-))))

  (x-open-connection (or display
			 (setq x-display-name (or (getenv "DISPLAY" (selected-frame))
						  (getenv "DISPLAY"))))
		     x-command-line-resources
		     ;; Exit Emacs with fatal error if this fails and we
		     ;; are the initial display.
		     (eq initial-window-system 'x))

  ;; Create the default fontset.
  (create-default-fontset)

  ;; Create the standard fontset.
  (condition-case err
	(create-fontset-from-fontset-spec standard-fontset-spec t)
    (error (display-warning
	    'initialization
	    (format "Creation of the standard fontset failed: %s" err)
	    :error)))

  ;; Create fontset specified in X resources "Fontset-N" (N is 0, 1, ...).
  (create-fontset-from-x-resource)

  ;; Set scroll bar mode to right if set by X resources. Default is left.
  (if (equal (x-get-resource "verticalScrollBars" "ScrollBars") "right")
      (customize-set-variable 'scroll-bar-mode 'right))

  ;; Apply a geometry resource to the initial frame.  Put it at the end
  ;; of the alist, so that anything specified on the command line takes
  ;; precedence.
  (let* ((res-geometry (x-get-resource "geometry" "Geometry"))
	 parsed)
    (if res-geometry
	(progn
	  (setq parsed (x-parse-geometry res-geometry))
	  ;; If the resource specifies a position,
	  ;; call the position and size "user-specified".
	  (if (or (assq 'top parsed) (assq 'left parsed))
	      (setq parsed (cons '(user-position . t)
				 (cons '(user-size . t) parsed))))
	  ;; All geometry parms apply to the initial frame.
	  (setq initial-frame-alist (append initial-frame-alist parsed))
	  ;; The size parms apply to all frames.  Don't set it if there are
	  ;; sizes there already (from command line).
	  (if (and (assq 'height parsed)
		   (not (assq 'height default-frame-alist)))
	      (setq default-frame-alist
		    (cons (cons 'height (cdr (assq 'height parsed)))
			  default-frame-alist)))
	  (if (and (assq 'width parsed)
		   (not (assq 'width default-frame-alist)))
	      (setq default-frame-alist
		    (cons (cons 'width (cdr (assq 'width parsed)))
			  default-frame-alist))))))

  ;; Check the reverseVideo resource.
  (let ((case-fold-search t))
    (let ((rv (x-get-resource "reverseVideo" "ReverseVideo")))
      (if (and rv
	       (string-match "^\\(true\\|yes\\|on\\)$" rv))
	  (setq default-frame-alist
		(cons '(reverse . t) default-frame-alist)))))

  ;; Set x-selection-timeout, measured in milliseconds.
  (let ((res-selection-timeout (x-get-resource "selectionTimeout"
					       "SelectionTimeout")))
    (setq x-selection-timeout
	  (if res-selection-timeout
	      (string-to-number res-selection-timeout)
	    5000)))

  ;; Don't let Emacs suspend under X.
  (add-hook 'suspend-hook 'x-win-suspend-error)

  ;; During initialization, we defer sending size hints to the window
  ;; manager, because that can induce a race condition:
  ;; https://lists.gnu.org/r/emacs-devel/2008-10/msg00033.html
  ;; Send the size hints once initialization is done.
  (add-hook 'after-init-hook 'x-wm-set-size-hint)

  ;; Turn off window-splitting optimization; X is usually fast enough
  ;; that this is only annoying.
  (setq split-window-keep-point t)

  ;; Motif direct handling of f10 wasn't working right,
  ;; So temporarily we've turned it off in lwlib-Xm.c
  ;; and turned the Emacs f10 back on.
  ;; ;; Motif normally handles f10 itself, so don't try to handle it a second time.
  ;; (if (featurep 'motif)
  ;;     (global-set-key [f10] 'ignore))

  ;; Enable CLIPBOARD copy/paste through menu bar commands.
  (menu-bar-enable-clipboard)

  ;; ;; Override Paste so it looks at CLIPBOARD first.
  ;; (define-key menu-bar-edit-menu [paste]
  ;;   (append '(menu-item "Paste" x-clipboard-yank
  ;; 			:enable (not buffer-read-only)
  ;; 			:help "Paste (yank) text most recently cut/copied")
  ;; 	    nil))

  (x-apply-session-resources)
  (setq x-initialized t))

(declare-function x-own-selection-internal "xselect.c"
		  (selection value &optional frame))
(declare-function x-disown-selection-internal "xselect.c"
		  (selection &optional time-object terminal))
(declare-function x-selection-owner-p "xselect.c"
		  (&optional selection terminal))
(declare-function x-selection-exists-p "xselect.c"
		  (&optional selection terminal))
(declare-function x-get-selection-internal "xselect.c"
		  (selection-symbol target-type &optional time-stamp terminal))

(add-to-list 'display-format-alist '("\\`.*:[0-9]+\\(\\.[0-9]+\\)?\\'" . x))
(cl-defmethod handle-args-function (args &context (window-system x))
  (x-handle-args args))

(cl-defmethod frame-creation-function (params &context (window-system x))
  (x-create-frame-with-faces params))

(cl-defmethod gui-backend-set-selection (selection value
                                         &context (window-system x))
  (if value (x-own-selection-internal selection value)
    (x-disown-selection-internal selection)))

(cl-defmethod gui-backend-selection-owner-p (selection
                                             &context (window-system x))
  (x-selection-owner-p selection))

(cl-defmethod gui-backend-selection-exists-p (selection
                                              &context (window-system x))
  (x-selection-exists-p selection))

(cl-defmethod gui-backend-get-selection (selection-symbol target-type
                                         &context (window-system x)
                                         &optional time-stamp terminal)
  (x-get-selection-internal selection-symbol target-type time-stamp terminal))

;; Initiate drag and drop
(add-hook 'after-make-frame-functions 'x-dnd-init-frame)
(define-key special-event-map [drag-n-drop] 'x-dnd-handle-drag-n-drop-event)

(defcustom x-gtk-stock-map
  (mapcar (lambda (arg)
	    (cons (purecopy (car arg)) (purecopy (cdr arg))))
  '(
    ("etc/images/new" . ("document-new" "gtk-new"))
    ("etc/images/open" . ("document-open" "gtk-open"))
    ("etc/images/diropen" . "n:system-file-manager")
    ("etc/images/close" . ("window-close" "gtk-close"))
    ("etc/images/save" . ("document-save" "gtk-save"))
    ("etc/images/saveas" . ("document-save-as" "gtk-save-as"))
    ("etc/images/undo" . ("edit-undo" "gtk-undo"))
    ("etc/images/cut" . ("edit-cut" "gtk-cut"))
    ("etc/images/copy" . ("edit-copy" "gtk-copy"))
    ("etc/images/paste" . ("edit-paste" "gtk-paste"))
    ("etc/images/search" . ("edit-find" "gtk-find"))
    ("etc/images/print" . ("document-print" "gtk-print"))
    ("etc/images/preferences" . ("preferences-system" "gtk-preferences"))
    ("etc/images/help" . ("help-browser" "gtk-help"))
    ("etc/images/left-arrow" . ("go-previous" "gtk-go-back"))
    ("etc/images/right-arrow" . ("go-next" "gtk-go-forward"))
    ("etc/images/home" . ("go-home" "gtk-home"))
    ("etc/images/jump-to" . ("go-jump" "gtk-jump-to"))
    ("etc/images/index" . ("gtk-search" "gtk-index"))
    ("etc/images/exit" . ("application-exit" "gtk-quit"))
    ("etc/images/cancel" . "gtk-cancel")
    ("etc/images/info" . ("dialog-information" "gtk-info"))
    ("etc/images/bookmark_add" . "n:bookmark_add")
    ;; Used in Gnus and/or MH-E:
    ("etc/images/attach" . ("mail-attachment" "gtk-attach"))
    ("etc/images/connect" . "gtk-connect")
    ("etc/images/contact" . "gtk-contact")
    ("etc/images/delete" . ("edit-delete" "gtk-delete"))
    ("etc/images/describe" . ("document-properties" "gtk-properties"))
    ("etc/images/disconnect" . "gtk-disconnect")
    ;; ("etc/images/exit" . "gtk-exit")
    ("etc/images/lock-broken" . "gtk-lock_broken")
    ("etc/images/lock-ok" . "gtk-lock_ok")
    ("etc/images/lock" . "gtk-lock")
    ("etc/images/next-page" . "gtk-next-page")
    ("etc/images/refresh" . ("view-refresh" "gtk-refresh"))
    ("etc/images/search-replace" . "edit-find-replace")
    ("etc/images/sort-ascending" . ("view-sort-ascending" "gtk-sort-ascending"))
    ("etc/images/sort-column-ascending" . "gtk-sort-column-ascending")
    ("etc/images/sort-criteria" . "gtk-sort-criteria")
    ("etc/images/sort-descending" . ("view-sort-descending"
				     "gtk-sort-descending"))
    ("etc/images/sort-row-ascending" . "gtk-sort-row-ascending")
    ("etc/images/spell" . ("tools-check-spelling" "gtk-spell-check"))
    ("images/gnus/toggle-subscription" . "gtk-task-recurring")
    ("images/mail/compose" . ("mail-message-new" "gtk-mail-compose"))
    ("images/mail/copy" . "gtk-mail-copy")
    ("images/mail/forward" . "gtk-mail-forward")
    ("images/mail/inbox" . "gtk-inbox")
    ("images/mail/move" . "gtk-mail-move")
    ("images/mail/not-spam" . "gtk-not-spam")
    ("images/mail/outbox" . "gtk-outbox")
    ("images/mail/reply-all" . "gtk-mail-reply-to-all")
    ("images/mail/reply" . "gtk-mail-reply")
    ("images/mail/save-draft" . "gtk-mail-handling")
    ("images/mail/send" . ("mail-send" "gtk-mail-send"))
    ("images/mail/spam" . "gtk-spam")
    ;; Used for GDB Graphical Interface
    ("images/gud/break" . "gtk-no")
    ("images/gud/recstart" . ("media-record" "gtk-media-record"))
    ("images/gud/recstop" . ("media-playback-stop" "gtk-media-stop"))
    ;; No themed versions available:
    ;; mail/preview (combining stock_mail and stock_zoom)
    ;; mail/save    (combining stock_mail, stock_save and stock_convert)
    ))
  "How icons for tool bars are mapped to Gtk+ stock items.
Emacs must be compiled with the Gtk+ toolkit for this to have any effect.
A value that begins with n: denotes a named icon instead of a stock icon."
  :version "22.2"
  :type '(choice (repeat
		  (choice symbol
			  (cons (string :tag "Emacs icon")
				(choice (group (string :tag "Named")
					       (string :tag "Stock"))
					(string :tag "Stock/named"))))))
  :group 'x)

(defcustom icon-map-list '(x-gtk-stock-map)
  "A list of alists that map icon file names to stock/named icons.
The alists are searched in the order they appear.  The first match is used.
The keys in the alists are file names without extension and with two directory
components.  For example, to map /usr/share/emacs/22.1.1/etc/images/open.xpm
to stock item gtk-open, use:

  (\"etc/images/open\" . \"gtk-open\")

Themes also have named icons.  To map to one of those, use n: before the name:

  (\"etc/images/diropen\" . \"n:system-file-manager\")

The list elements are either the symbol name for the alist or the
alist itself.

If you don't want stock icons, set the variable to nil."
  :version "22.2"
  :type '(choice (const :tag "Don't use stock icons" nil)
		 (repeat (choice symbol
				 (cons (string :tag "Emacs icon")
				       (string :tag "Stock/named")))))
  :group 'x)

(defconst x-gtk-stock-cache (make-hash-table :weakness t :test 'equal))

(defun x-gtk-map-stock (file)
  "Map icon with file name FILE to a Gtk+ stock name.
This uses `icon-map-list' to map icon file names to stock icon names."
  (when (stringp file)
    (or (gethash file x-gtk-stock-cache)
	(puthash
	 file
	 (save-match-data
	   (let* ((file-sans (file-name-sans-extension file))
		  (key (and (string-match "/\\([^/]+/[^/]+/[^/]+$\\)"
					  file-sans)
			    (match-string 1 file-sans)))
		  (icon-map icon-map-list)
		  elem value)
	     (while (and (null value) icon-map)
	       (setq elem (car icon-map)
		     value (assoc-string (or key file-sans)
					 (if (symbolp elem)
					     (symbol-value elem)
					   elem))
		     icon-map (cdr icon-map)))
	     (and value (cdr value))))
	 x-gtk-stock-cache))))

(global-set-key [XF86WakeUp] 'ignore)

(provide 'x-win)
(provide 'term/x-win)

;;; x-win.el ends here