summaryrefslogtreecommitdiff
path: root/lisp/textmodes/reftex-index.el
blob: 5049ffb64b1f72ce97aa24b91c7032f6d6853664 (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
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
;;; reftex-index.el --- index support with RefTeX

;; Copyright (C) 1997-2021 Free Software Foundation, Inc.

;; Author: Carsten Dominik <dominik@science.uva.nl>
;; Maintainer: auctex-devel@gnu.org

;; 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:

;;; Code:

(eval-when-compile (require 'cl-lib))
(declare-function texmathp "ext:texmathp" ())

(require 'reftex)

;; START remove for XEmacs release
(defvar mark-active)
(defvar transient-mark-mode)
(defvar TeX-master)
;; END remove for XEmacs release

;;;###autoload
(defun reftex-index-selection-or-word (&optional arg phrase)
  "Put selection or the word near point into the default index macro.
This uses the information in `reftex-index-default-macro' to make an index
entry.  The phrase indexed is the current selection or the word near point.
When called with one `C-u' prefix, let the user have a chance to edit the
index entry.  When called with 2 `C-u' as prefix, also ask for the index
macro and other stuff.
When called inside TeX math mode as determined by the `texmathp.el' library
which is part of AUCTeX, the string is first processed with the
`reftex-index-math-format', which see."
  (interactive "P")
  (let* ((use-default (not (equal arg '(16))))  ; check for double prefix
         ;; check if we have an active selection
         (active (reftex-region-active-p))
         (beg (if active
                  (region-beginning)
                (save-excursion
                  (skip-syntax-backward "w\\") (point))))
         (end (if active
                  (region-end)
                (save-excursion
                  (skip-syntax-forward "w\\") (point))))
         (sel (buffer-substring beg end))
         (mathp (condition-case nil (texmathp) (error nil)))
         (current-prefix-arg nil) ; we want to call reftex-index without prefix.
         key def-char def-tag full-entry)

    (if phrase
        (progn
          (reftex-index-visit-phrases-buffer)
          (reftex-index-new-phrase sel))

      (if (equal sel "")
          ;; Nothing selected, no word, so use full reftex-index command
          (reftex-index)
        ;; OK, we have something to index here.
        ;; Add the dollars when necessary
        (setq key (if mathp
                      (format reftex-index-math-format sel)
                    sel))
        ;; Get info from `reftex-index-default-macro'
        (setq def-char  (if use-default (car reftex-index-default-macro)))
        (setq def-tag   (if use-default (nth 1 reftex-index-default-macro)))
        ;; Does the user want to edit the entry?
        (setq full-entry (if arg
                             (reftex-index-complete-key
                              def-tag nil (cons key 0))
                           key))
        ;; Delete what is in the buffer and make the index entry
        (delete-region beg end)
        (reftex-index def-char full-entry def-tag sel)))))

;;;###autoload
(defun reftex-index (&optional char key tag sel no-insert)
  "Query for an index macro and insert it along with its arguments.
The index macros available are those defined in `reftex-index-macro' or
by a call to `reftex-add-index-macros', typically from an AUCTeX style file.
RefteX provides completion for the index tag and the index key, and
will prompt for other arguments."

  (interactive)

  ;; Ensure access to scanning info
  (reftex-ensure-index-support t)
  (reftex-access-scan-info current-prefix-arg)

  ;; Find out which macro we are going to use
  (let* ((char (or char
                   (reftex-select-with-char reftex-query-index-macro-prompt
                                            reftex-query-index-macro-help)))
         (macro (nth 1 (assoc char reftex-key-to-index-macro-alist)))
         (entry (or (assoc macro reftex-index-macro-alist)
                    (error "No index macro associated with %c" char)))
         (ntag (nth 1 entry))
         (tag (or tag (nth 1 entry)))
         (nargs (nth 4 entry))
         (nindex (nth 5 entry))
         (opt-args (nth 6 entry))
         (repeat (nth 7 entry))
         opt tag1 value)

    ;; Get the supported arguments
    (if (stringp tag)
        (setq tag1 tag)
      (setq tag1 (or (reftex-index-complete-tag tag opt-args) "")))
    (setq key (or key
                  (reftex-index-complete-key
                   (if (string= tag1 "") "idx" tag1)
                   (member nindex opt-args))))

    ;; Insert the macro and ask for any additional args
    (insert macro)
    (cl-loop for i from 1 to nargs do
      (setq opt (member i opt-args)
            value (cond ((= nindex i) key)
                        ((equal ntag i) tag1)
                        (t (read-string (concat "Macro arg nr. "
                                                (int-to-string i)
                                                (if opt " (optional)" "")
                                                ": ")))))
      (unless (and opt (string= value ""))
        (insert (if opt "[" "{") value (if opt "]" "}"))))
    (and repeat (stringp sel) (insert sel))
    (and key reftex-plug-into-AUCTeX (fboundp 'LaTeX-add-index-entries)
         (LaTeX-add-index-entries key))
    (reftex-index-update-taglist tag1)
    (reftex-notice-new)))

(defun reftex-default-index ()
  (cond ((null reftex-index-default-tag) nil)
        ((stringp reftex-index-default-tag) reftex-index-default-tag)
        (t (or (get reftex-docstruct-symbol 'default-index-tag)
               "idx"))))

(defun reftex-update-default-index (tag &optional tag-list)
  (if (and (not (equal tag ""))
           (stringp tag)
           (eq reftex-index-default-tag 'last)
           (or (null tag-list)
               (member tag tag-list)))
      (put reftex-docstruct-symbol 'default-index-tag tag)))

;;;###autoload
(defun reftex-index-complete-tag (&optional itag opt-args)
  ;; Ask the user for a tag, completing on known tags.
  ;; ITAG is the argument number which contains the tag.
  ;; OPT-ARGS is a list of optional argument indices, as given by
  ;; `reftex-parse-args'.
  (let* ((opt (and (integerp itag) (member itag opt-args)))
	 (index-tags (cdr (assq 'index-tags
				(symbol-value reftex-docstruct-symbol))))
	 (default (reftex-default-index))
	 (prompt (concat "Index tag"
			 (if (or opt default)
			     (format " (%s): "
				     (concat
				      (if opt "optional" "")
				      (if default
					  (concat (if opt ", " "")
						  (format "default %s" default))
					"")))
			   ": ")))
	 (tag (completing-read prompt (mapcar 'list index-tags))))
    (if (and default (equal tag "")) (setq tag default))
    (reftex-update-default-index tag)
    tag))

;;;###autoload
(defun reftex-index-select-tag ()
  ;; Have the user select an index tag.
  ;; FIXME: should we cache tag-alist, prompt and help?
  (let* ((index-tags (cdr (assoc 'index-tags
                                 (symbol-value reftex-docstruct-symbol))))
         (default (reftex-default-index)))
    (cond
     ((null index-tags)
      (error "No index tags available"))

     ((= (length index-tags) 1)
      ;; Just one index, use it
      (car index-tags))

     ((> (length index-tags) 1)
      ;; Several indices, ask.
      (let* ((tags (copy-sequence index-tags))
             (cnt 0)
             tag-alist i val len tag prompt help rpl)
        ;; Move idx and glo up in the list to ensure ?i and ?g shortcuts
        (if (member "glo" tags)
            (setq tags (cons "glo" (delete "glo" tags))))
        (if (member "idx" tags)
            (setq tags (cons "idx" (delete "idx" tags))))
        ;; Find unique shortcuts for each index.
        (while (setq tag (pop tags))
          (setq len (length tag)
                i -1
                val nil)
          (catch 'exit
            (while (and (< (cl-incf i) len) (null val))
              (unless (assq (aref tag i) tag-alist)
                (push (list (aref tag i)
                            tag
                            (concat (substring tag 0 i)
                                    "[" (substring tag i (cl-incf i)) "]"
                                    (substring tag i)))
                      tag-alist)
                (throw 'exit t)))
            (push (list (+ ?0 (cl-incf cnt)) tag
                        (concat "[" (int-to-string cnt) "]:" tag))
                  tag-alist)))
        (setq tag-alist (nreverse tag-alist))
        ;; Compute Prompt and Help strings
        (setq prompt
              (concat
               (format "Select Index%s: "
                       (if default (format " (Default <%s>)" default) ""))
               (mapconcat (lambda(x) (nth 2 x)) tag-alist "  ")))
        (setq help
              (concat "Select an Index\n===============\n"
                      (if default
                          (format "[^M]  %s (the default)\n" default)
                        "")
                      (mapconcat (lambda(x)
                                   (apply 'format "[%c]   %s" x))
                                 tag-alist "\n")))
        ;; Query the user for an index-tag
        (setq rpl (reftex-select-with-char prompt help 3 t))
        (message "")
        (if (and default (equal rpl ?\C-m))
            default
          (if (assq rpl tag-alist)
              (progn
                (reftex-update-default-index (nth 1 (assq rpl tag-alist)))
                (nth 1 (assq rpl tag-alist)))
            (error "No index tag associated with %c" rpl)))))
     (t (error "This should not happen (reftex-index-select-tag)")))))

;;;###autoload
(defun reftex-index-complete-key (&optional tag optional initial)
  ;; Read an index key, with completion.
  ;; Restrict completion table on index tag TAG.
  ;; OPTIONAL indicates if the arg is optional.
  (let* ((table (reftex-sublist-nth
                 (symbol-value reftex-docstruct-symbol) 6
                 (lambda(x) (and (eq (car x) 'index)
                                 (string= (nth 1 x) (or tag ""))))
                 t))
         (prompt (concat "Index key" (if optional " (optional)" "") ": "))
         (key (completing-read prompt table nil nil initial)))
    key))

(defun reftex-index-update-taglist (newtag)
  ;; add NEWTAG to the list of available index tags.
  (let ((cell (assoc 'index-tags (symbol-value reftex-docstruct-symbol))))
    (and newtag (cdr cell) (not (member newtag (cdr cell)))
         (push newtag (cdr cell)))))

(define-obsolete-variable-alias
  'reftex-index-map 'reftex-index-mode-map "24.1")
(defvar reftex-index-mode-map
  (let ((map (make-sparse-keymap)))
    ;; Index map
    (define-key map (if (featurep 'xemacs) [(button2)] [(mouse-2)])
      'reftex-index-mouse-goto-line-and-hide)
    (define-key map [follow-link] 'mouse-face)

    (substitute-key-definition
     'next-line 'reftex-index-next map global-map)
    (substitute-key-definition
     'previous-line 'reftex-index-previous map global-map)

    (define-key map "n" 'reftex-index-next)
    (define-key map "p" 'reftex-index-previous)
    (define-key map "?" 'reftex-index-show-help)
    (define-key map " " 'reftex-index-view-entry)
    (define-key map "\C-m" 'reftex-index-goto-entry-and-hide)
    (define-key map "\C-i" 'reftex-index-goto-entry)
    (define-key map "\C-k" 'reftex-index-kill)
    (define-key map "r" 'reftex-index-rescan)
    (define-key map "R" 'reftex-index-Rescan)
    (define-key map "g" 'revert-buffer)
    (define-key map "q" 'reftex-index-quit)
    (define-key map "k" 'reftex-index-quit-and-kill)
    (define-key map "f" 'reftex-index-toggle-follow)
    (define-key map "s" 'reftex-index-switch-index-tag)
    (define-key map "e" 'reftex-index-edit)
    (define-key map "^" 'reftex-index-level-up)
    (define-key map "_" 'reftex-index-level-down)
    (define-key map "}" 'reftex-index-restrict-to-section)
    (define-key map "{" 'reftex-index-widen)
    (define-key map ">" 'reftex-index-restriction-forward)
    (define-key map "<" 'reftex-index-restriction-backward)
    (define-key map "(" 'reftex-index-toggle-range-beginning)
    (define-key map ")" 'reftex-index-toggle-range-end)
    (define-key map "|" 'reftex-index-edit-attribute)
    (define-key map "@" 'reftex-index-edit-visual)
    (define-key map "*" 'reftex-index-edit-key)
    (define-key map "\C-c=" 'reftex-index-goto-toc)
    (define-key map "c" 'reftex-index-toggle-context)

    ;; The capital letters and the exclamation mark
    (cl-loop for key across (concat "!" reftex-index-section-letters) do
             (define-key map (vector (list key))
               (list 'lambda '() '(interactive)
                     (list 'reftex-index-goto-letter key))))

    (easy-menu-define reftex-index-menu map
      "Menu for Index buffer"
      '("Index"
        ["Goto section A-Z"
         (message "To go to a section, just press any of: !%s"
                  reftex-index-section-letters) t]
        ["Show Entry" reftex-index-view-entry t]
        ["Go To Entry" reftex-index-goto-entry t]
        ["Exit & Go To Entry" reftex-index-goto-entry-and-hide t]
        ["Table of Contents" reftex-index-goto-toc t]
        ["Quit" reftex-index-quit t]
        "--"
        ("Update"
         ["Rebuilt *Index* Buffer" revert-buffer t]
         "--"
         ["Rescan One File" reftex-index-rescan reftex-enable-partial-scans]
         ["Rescan Entire Document" reftex-index-Rescan t])
        ("Restrict"
         ["Restrict to section" reftex-index-restrict-to-section t]
         ["Widen" reftex-index-widen reftex-index-restriction-indicator]
         ["Next Section" reftex-index-restriction-forward
          reftex-index-restriction-indicator]
         ["Previous Section" reftex-index-restriction-backward
          reftex-index-restriction-indicator])
        ("Edit"
         ["Edit Entry" reftex-index-edit t]
         ["Edit Key" reftex-index-edit-key t]
         ["Edit Attribute" reftex-index-edit-attribute t]
         ["Edit Visual" reftex-index-edit-visual t]
         "--"
         ["Add Parentkey" reftex-index-level-down t]
         ["Remove Parentkey " reftex-index-level-up t]
         "--"
         ["Make Start-of-Range" reftex-index-toggle-range-beginning t]
         ["Make End-of-Range" reftex-index-toggle-range-end t]
         "--"
         ["Kill Entry" reftex-index-kill nil]
         "--"
         ["Undo" reftex-index-undo nil])
        ("Options"
         ["Context" reftex-index-toggle-context :style toggle
          :selected reftex-index-include-context]
         "--"
         ["Follow Mode" reftex-index-toggle-follow :style toggle
          :selected reftex-index-follow-mode])
        "--"
        ["Help" reftex-index-show-help t]))

    map)
  "Keymap used for *Index* buffers.")

(defvar reftex-index-menu)

(defvar reftex-last-index-file nil
  "Stores the file name from which `reftex-display-index' was called.")
(defvar reftex-index-tag nil
  "Stores the tag of the index in an index buffer.")

(defvar reftex-index-return-marker (make-marker)
  "Marker which makes it possible to return from index to old position.")

(defvar reftex-index-restriction-indicator nil)
(defvar reftex-index-restriction-data nil)

(define-derived-mode reftex-index-mode special-mode "RefTeX Index"
  "Major mode for managing Index buffers for LaTeX files.
This buffer was created with RefTeX.
Press `?' for a summary of important key bindings, or check the menu.

Here are all local bindings.

\\{reftex-index-mode-map}"
  (set (make-local-variable 'revert-buffer-function) 'reftex-index-revert)
  (set (make-local-variable 'reftex-index-restriction-data) nil)
  (set (make-local-variable 'reftex-index-restriction-indicator) nil)
  (setq mode-line-format
        (list "----  " 'mode-line-buffer-identification
              "   " 'global-mode-string
              "  R<" 'reftex-index-restriction-indicator ">"
              " -%-"))
  (setq truncate-lines t)
  (when (featurep 'xemacs)
    ;; XEmacs needs the call to make-local-hook
    (make-local-hook 'post-command-hook)
    (make-local-hook 'pre-command-hook))
  (make-local-variable 'reftex-last-follow-point)
  (when (featurep 'xemacs)
    (easy-menu-add reftex-index-menu reftex-index-mode-map))
  (add-hook 'post-command-hook 'reftex-index-post-command-hook nil t)
  (add-hook 'pre-command-hook  'reftex-index-pre-command-hook nil t))

(defconst reftex-index-help
"                      AVAILABLE KEYS IN INDEX BUFFER
                      ==============================
! A..Z     Goto the section of entries starting with this letter.
n / p      next-entry / previous-entry
SPC / TAB  Show/Goto the corresponding entry in the LaTeX document.
RET        Goto the entry and hide the *Index* window (also on mouse-2).
q / k      Hide/Kill *Index* buffer.
C-c =      Switch to the TOC buffer.
f / c      Toggle follow mode             / Toggle display of [c]ontext.
g          Refresh *Index* buffer.
r / C-u r  Reparse the LaTeX document     / Reparse entire LaTeX document.
s          Switch to a different index (for documents with multiple indices).
e / C-k    Edit/Kill the entry.
* | @      Edit specific part of entry: [*]key [|]attribute [@]visual
           With prefix: kill that part.
\( )        Toggle entry's beginning/end of page range property.
_ ^        Add/Remove parent key (to make this item a subitem).
} / {      Restrict Index to a single document section / Widen.
< / >      When restricted, move restriction to previous/next section.")

;;;###autoload
(defun reftex-index-show-entry (data &optional no-revisit)
  ;; Find an index entry associated with DATA and display it highlighted
  ;; in another window.  NO-REVISIT means we are not allowed to visit
  ;; files for this.
  ;; Note:  This function just looks for the nearest match of the
  ;; context string and may fail if the entry moved and an identical
  ;; entry is close to the old position.  Frequent rescans make this
  ;; safer.
  (let* ((file (nth 3 data))
         (literal (nth 2 data))
         (pos (nth 4 data))
         (re (regexp-quote literal))
         (match
          (cond
           ((or (not no-revisit)
                (reftex-get-buffer-visiting file))
            (switch-to-buffer-other-window
             (reftex-get-file-buffer-force file nil))
            (goto-char (or pos (point-min)))
            (or (looking-at re)
                (reftex-nearest-match re (length literal))))
           (t (message "%s" reftex-no-follow-message) nil))))
    (when match
      (goto-char (match-beginning 0))
      (recenter '(4))
      (reftex-highlight 0 (match-beginning 0) (match-end 0) (current-buffer)))
    match))

;;;###autoload
(defun reftex-display-index (&optional tag overriding-restriction redo
                                       &rest locations)
  "Display a buffer with an index compiled from the current document.
When the document has multiple indices, first prompts for the correct one.
When index support is turned off, offer to turn it on.
With one or two `C-u' prefixes, rescan document first.
With prefix 2, restrict index to current document section.
With prefix 3, restrict index to region."

  (interactive)

  ;; Ensure access to scanning info and rescan buffer if prefix arg is '(4).
  (let ((current-prefix-arg current-prefix-arg))
    (reftex-ensure-index-support t)
    (reftex-access-scan-info current-prefix-arg))

  (set-marker reftex-index-return-marker (point))
  (setq reftex-last-follow-point 1)

  ;; Determine the correct index to process
  (let* ((docstruct (symbol-value reftex-docstruct-symbol))
         (docstruct-symbol reftex-docstruct-symbol)
         (index-tag (or tag (reftex-index-select-tag)))
         (master (reftex-TeX-master-file))
         (calling-file (buffer-file-name))
         (restriction
          (or overriding-restriction
              (and (not redo)
                   (reftex-get-restriction current-prefix-arg docstruct))))
         (locations
          ;; See if we are on an index macro as initial position
          (or locations
              (let* ((what-macro (reftex-what-macro-safe 1))
                     (macro (car what-macro))
                     (here-I-am (when (member macro reftex-macros-with-index)
                                  (save-excursion
                                    (goto-char (+ (cdr what-macro)
                                                  (length macro)))
                                    (reftex-move-over-touching-args)
                                    (reftex-where-am-I)))))
                (if (eq (car (car here-I-am)) 'index)
                    (list (car here-I-am))))))
         buffer-name)

    (setq buffer-name (reftex-make-index-buffer-name index-tag))

    ;; Goto the buffer and put it into the correct mode

    (when (or restriction current-prefix-arg)
         (reftex-kill-buffer buffer-name))

    (if (get-buffer-window buffer-name)
        (select-window (get-buffer-window buffer-name))
      (switch-to-buffer buffer-name))

    (or (eq major-mode 'reftex-index-mode) (reftex-index-mode))

    ;; If the buffer is currently restricted, empty it to force update.
    (when reftex-index-restriction-data
      (reftex-erase-buffer))
    (set (make-local-variable 'reftex-last-index-file) calling-file)
    (set (make-local-variable 'reftex-index-tag) index-tag)
    (set (make-local-variable 'reftex-docstruct-symbol) docstruct-symbol)
    (if restriction
        (setq reftex-index-restriction-indicator (car restriction)
              reftex-index-restriction-data (cdr restriction))
      (if (not redo)
          (setq reftex-index-restriction-indicator nil
                reftex-index-restriction-data nil)))
    (when (= (buffer-size) 0)
      ;; buffer is empty - fill it
      (message "Building %s buffer..." buffer-name)

      (setq buffer-read-only nil)
      (insert (format
               "INDEX <%s> on %s
Restriction: <%s>
SPC=view TAB=goto RET=goto+hide [e]dit [q]uit [r]escan [f]ollow [?]Help
------------------------------------------------------------------------------
"
               index-tag (abbreviate-file-name master)
               (if (eq (car (car reftex-index-restriction-data)) 'toc)
                   (nth 2 (car reftex-index-restriction-data))
                 reftex-index-restriction-indicator)))

      (if (reftex-use-fonts)
          (put-text-property (point-min) (point)
                             'face reftex-index-header-face))
      (if (fboundp 'cursor-intangible-mode)
          (cursor-intangible-mode 1)
        ;; If `cursor-intangible' is not available, fallback on the old
        ;; intrusive `intangible' property.
        (put-text-property (point-min) (point) 'intangible t))
      (add-text-properties (point-min) (point)
                           '(cursor-intangible t
                             front-sticky (cursor-intangible)
                             rear-nonsticky (cursor-intangible)))

      (reftex-insert-index docstruct index-tag)
      (goto-char (point-min))
      (run-hooks 'reftex-display-copied-context-hook)
      (message "Building %s buffer...done." buffer-name)
      (setq buffer-read-only t))
    (and locations (apply 'reftex-find-start-point (point) locations))
    (if reftex-index-restriction-indicator
        (message "Index restricted: <%s>" reftex-index-restriction-indicator))))

(defun reftex-insert-index (docstruct tag &optional update-one remark)
  ;; Insert an index into the current buffer.  Entries are from the
  ;; DOCSTRUCT.
  ;; TAG is the subindex to process.
  ;; UPDATE-ONE: When non-nil, delete the entry at point and replace
  ;; it with whatever the DOCSTRUCT contains.
  ;; REMARK can be a note to add to the entry.
  (let* ((all docstruct)
         (indent "   ")
         (context reftex-index-include-context)
         (context-indent (concat indent "  "))
         (section-chars (mapcar 'identity reftex-index-section-letters))
         (this-section-char 0)
         (font (reftex-use-fonts))
         (bor (car reftex-index-restriction-data))
         (eor (nth 1 reftex-index-restriction-data))
         (mouse-face
          (if (memq reftex-highlight-selection '(mouse both))
              reftex-mouse-selected-face
            nil))
         (index-face reftex-label-face)
         sublist cell from to first-char)

    ;; Make the sublist and sort it
    (when bor
      (setq all (or (memq bor all) all)))

    (while (setq cell (pop all))
      (if (eq cell eor)
          (setq all nil)
        (and (eq (car cell) 'index)
             (equal (nth 1 cell) tag)
             (push cell sublist))))
    (setq sublist (sort (nreverse sublist)
                        (lambda (a b) (string< (nth 8 a) (nth 8 b)))))

    (when update-one
      ;; Delete the entry at place
      (and (bolp) (forward-char 1))
      (delete-region (previous-single-property-change (1+ (point)) :data)
                     (or (next-single-property-change (point) :data)
                         (point-max))))

    ;; Walk through the list and insert all entries
    (while (setq cell (pop sublist))
      (unless update-one
        (setq first-char (upcase (string-to-char (nth 6 cell))))
        (when (and (not (equal first-char this-section-char))
                   (member first-char section-chars))
          ;; There is a new initial letter, so start a new section
          (reftex-index-insert-new-letter first-char font)
          (setq section-chars (delete first-char section-chars)
                this-section-char first-char))
        (when (= this-section-char 0)
          (setq this-section-char ?!)
          (reftex-index-insert-new-letter this-section-char font)))

      (setq from (point))
      (insert indent (nth 7 cell))
      (when font
        (setq to (point))
        (put-text-property
         (- (point) (length (nth 7 cell))) to
         'face index-face)
        (goto-char to))

      (when (or remark (nth 9 cell))
        (and (< (current-column) 40)
             ;; FIXME: maybe this is too slow?
             (insert (make-string (max (- 40 (current-column)) 0) ?\ )))
        (and (nth 9 cell) (insert "   " (substring (nth 5 cell) (nth 9 cell))))
        (and remark (insert "     " remark)))

      (insert "\n")
      (setq to (point))

      (when context
        (insert context-indent (nth 2 cell) "\n")
        (setq to (point)))
      (put-text-property from to :data cell)
      (when mouse-face
        (put-text-property from (1- to)
                           'mouse-face mouse-face))
      (goto-char to))))


(defun reftex-index-insert-new-letter (letter &optional font)
  ;; Start a new section in the index
  (let ((from (point)))
    (insert "\n" letter letter letter
            "-----------------------------------------------------------------")
    (when font
      (put-text-property from (point) 'face reftex-index-section-face))
    (insert "\n")))

(defun reftex-get-restriction (arg docstruct)
  ;; Interpret the prefix ARG and derive index restriction specs.
  (let* ((beg (min (point) (or (condition-case nil (mark) (error nil))
                               (point-max))))
         (end (max (point) (or (condition-case nil (mark) (error nil))
                               (point-min))))
         bor eor label here-I-am)
    (cond
     ((eq arg 2)
      (setq here-I-am (car (reftex-where-am-I))
            bor (if (eq (car here-I-am) 'toc)
                    here-I-am
                  (reftex-last-assoc-before-elt
                   'toc here-I-am docstruct))
            eor (car (memq (assq 'toc (cdr (memq bor docstruct))) docstruct))
            label (nth 6 bor)))
     ((eq arg 3)
      (save-excursion
        (setq label "region")
        (goto-char beg)
        (setq bor (car (reftex-where-am-I)))
        (setq bor (nth 1 (memq bor docstruct)))
        (goto-char end)
        (setq eor (nth 1 (memq (car (reftex-where-am-I)) docstruct)))))
     (t nil))
    (if (and label (or bor eor))
        (list label bor eor)
      nil)))

(defun reftex-index-pre-command-hook ()
  ;; Used as pre command hook in *Index* buffer
  (reftex-unhighlight 0)
  (reftex-unhighlight 1))

(defun reftex-index-post-command-hook ()
  ;; Used in the post-command-hook for the *Index* buffer
  ;; FIXME: Lots of redundancy with reftex-toc-post-command-hook!
  (when (get-text-property (point) :data)
    (and (> (point) 1) ;FIXME: Is this point-min or do we care about narrowing?
         (not (get-text-property (point) 'cursor-intangible))
         (memq reftex-highlight-selection '(cursor both))
         (reftex-highlight 1
           (or (previous-single-property-change (1+ (point)) :data)
               (point-min))
           (or (next-single-property-change (point) :data)
               (point-max)))))
  (if (integerp reftex-index-follow-mode)
      ;; Remove delayed action
      (setq reftex-index-follow-mode t)
    (and reftex-index-follow-mode
         (not (equal reftex-last-follow-point (point)))
         ;; Show context in other window
         (setq reftex-last-follow-point (point))
         (condition-case nil
             (reftex-index-visit-location nil (not reftex-revisit-to-follow))
           (error t)))))

(defun reftex-index-show-help ()
  "Show a summary of special key bindings."
  (interactive)
  (with-output-to-temp-buffer "*RefTeX Help*"
    (princ reftex-index-help))
  (reftex-enlarge-to-fit "*RefTeX Help*" t)
  ;; If follow mode is active, arrange to delay it one command
  (if reftex-index-follow-mode
      (setq reftex-index-follow-mode 1)))

(defun reftex-index-next (&optional arg)
  "Move to next selectable item."
  (interactive "p")
  (setq reftex-callback-fwd t)
  (or (eobp) (forward-char 1))
  (goto-char (or (next-single-property-change (point) :data)
                 (point)))
  (unless (get-text-property (point) :data)
    (goto-char (or (next-single-property-change (point) :data)
                   (point)))))
(defun reftex-index-previous (&optional arg)
  "Move to previous selectable item."
  (interactive "p")
  (setq reftex-callback-fwd nil)
  (goto-char (or (previous-single-property-change (point) :data)
                 (point)))
  (unless (get-text-property (point) :data)
    (goto-char (or (previous-single-property-change (point) :data)
                   (point)))))
(defun reftex-index-toggle-follow ()
  "Toggle follow (other window follows with context)."
  (interactive)
  (setq reftex-last-follow-point -1)
  (setq reftex-index-follow-mode (not reftex-index-follow-mode)))
(defun reftex-index-toggle-context ()
  "Toggle inclusion of label context in *Index* buffer.
Label context is only displayed when the labels are there as well."
  (interactive)
  (setq reftex-index-include-context (not reftex-index-include-context))
  (reftex-index-revert))
(defun reftex-index-view-entry ()
  "View document location in other window."
  (interactive)
  (reftex-index-visit-location))
(defun reftex-index-goto-entry-and-hide ()
  "Go to document location in other window.  Hide the *Index* window."
  (interactive)
  (reftex-index-visit-location 'hide))
(defun reftex-index-goto-entry ()
  "Go to document location in other window.  *Index* window stays."
  (interactive)
  (reftex-index-visit-location t))
(defun reftex-index-mouse-goto-line-and-hide (ev)
  "Go to document location in other window.  Hide the *Index* window."
  (interactive "e")
  (mouse-set-point ev)
  (reftex-index-visit-location 'hide))
(defun reftex-index-quit ()
  "Hide the *Index* window and do not move point."
  (interactive)
  (or (one-window-p) (delete-window))
  (switch-to-buffer (marker-buffer reftex-index-return-marker))
  (goto-char (or (marker-position reftex-index-return-marker) (point))))
(defun reftex-index-quit-and-kill ()
  "Kill the *Index* buffer."
  (interactive)
  (kill-buffer (current-buffer))
  (or (one-window-p) (delete-window))
  (switch-to-buffer (marker-buffer reftex-index-return-marker))
  (goto-char (or (marker-position reftex-index-return-marker) (point))))
(defun reftex-index-goto-toc (&rest ignore)
  "Switch to the table of contents of the current document.
The function will go to the section where the entry at point was defined."
  (interactive)
  (if (get-text-property (point) :data)
      (reftex-index-goto-entry)
    (switch-to-buffer (marker-buffer reftex-index-return-marker)))
  (delete-other-windows)
  (reftex-toc))
(defun reftex-index-rescan (&rest ignore)
  "Regenerate the *Index* buffer after reparsing file of section at point."
  (interactive)
  (let ((index-tag reftex-index-tag))
    (if (and reftex-enable-partial-scans
             (null current-prefix-arg))
        (let* ((data (get-text-property (point) :data))
               (file (nth 3 data))
               (line (+ (count-lines (point-min) (point)) (if (bolp) 1 0))))
          (if (not file)
              (error "Don't know which file to rescan.  Try `C-u r'")
            (switch-to-buffer (reftex-get-file-buffer-force file))
            (setq current-prefix-arg '(4))
            (reftex-display-index index-tag nil 'redo line)))
      (reftex-index-Rescan))
    (reftex-kill-temporary-buffers)))
(defun reftex-index-Rescan (&rest ignore)
  "Regenerate the *Index* buffer after reparsing the entire document."
  (interactive)
  (let ((index-tag reftex-index-tag)
        (line (+ (count-lines (point-min) (point)) (if (bolp) 1 0))))
    (switch-to-buffer
     (reftex-get-file-buffer-force reftex-last-index-file))
    (setq current-prefix-arg '(16))
    (reftex-display-index index-tag nil 'redo line)))
(defun reftex-index-revert (&rest ignore)
  "Regenerate the *Index* from the internal lists.  No reparsing os done."
  (interactive)
  (let ((buf (current-buffer))
        (index-tag reftex-index-tag)
        (data (get-text-property (point) :data))
        (line (+ (count-lines (point-min) (point)) (if (bolp) 1 0))))
    (switch-to-buffer
     (reftex-get-file-buffer-force reftex-last-index-file))
    (reftex-erase-buffer buf)
    (setq current-prefix-arg nil
          reftex-last-follow-point 1)
    (reftex-display-index index-tag nil 'redo data line)))
(defun reftex-index-switch-index-tag (&rest ignore)
  "Switch to a different index of the same document."
  (interactive)
  (switch-to-buffer
   (reftex-get-file-buffer-force reftex-last-index-file))
  (setq current-prefix-arg nil)
  (reftex-display-index nil nil 'redo))

(defun reftex-index-restrict-to-section (&optional force)
  "Restrict index to entries defined in same document sect. as entry at point."
  ;; Optional FORCE means, even if point is not on an index entry.
  (interactive)
  (let* ((data (get-text-property (point) :data))
         (docstruct (symbol-value reftex-docstruct-symbol))
         bor eor)
    (if (and (not data) force)
        (setq data (assq 'toc docstruct)))
    (when data
      (setq bor (reftex-last-assoc-before-elt 'toc data docstruct)
            eor (car (memq (assq 'toc (cdr (memq bor docstruct)))
                           docstruct))
            reftex-index-restriction-data (list bor eor)
            reftex-index-restriction-indicator (nth 6 bor) )))
  (reftex-index-revert))

(defun reftex-index-widen (&rest ignore)
  "Show the unrestricted index (all entries)."
  (interactive)
  (setq reftex-index-restriction-indicator nil
        reftex-index-restriction-data nil)
  (reftex-index-revert)
  (message "Index widened"))
(defun reftex-index-restriction-forward (&rest ignore)
  "Restrict to previous section.
When index is currently unrestricted, restrict it to a section.
When index is restricted, select the next section as restriction criterion."
  (interactive)
  (let* ((docstruct (symbol-value reftex-docstruct-symbol))
         (bor (nth 1 reftex-index-restriction-data)))
    (if (or (not bor)
            (not (eq (car bor) 'toc)))
        (reftex-index-restrict-to-section t)
      (setq reftex-index-restriction-indicator (nth 6 bor)
            reftex-index-restriction-data
            (list bor
                  (car (memq (assq 'toc (cdr (memq bor docstruct)))
                             docstruct))))
      (reftex-index-revert))))
(defun reftex-index-restriction-backward (&rest ignore)
  "Restrict to next section.
When index is currently unrestricted, restrict it to a section.
When index is restricted, select the previous section as restriction criterion."
  (interactive)
  (let* ((docstruct (symbol-value reftex-docstruct-symbol))
         (eor (car reftex-index-restriction-data))
         (bor (reftex-last-assoc-before-elt 'toc eor docstruct t)))
    (if (or (not bor)
            (not (eq (car bor) 'toc)))
        (reftex-index-restrict-to-section t)
      (setq reftex-index-restriction-indicator (nth 6 bor)
            reftex-index-restriction-data
            (list bor eor))
      (reftex-index-revert))))

(defun reftex-index-visit-location (&optional final no-revisit)
  ;; Visit the tex file corresponding to the index entry on the current line.
  ;; If FINAL is t, stay there
  ;; If FINAL is 'hide, hide the *Index* window.
  ;; Otherwise, move cursor back into *Index* window.
  ;; NO-REVISIT means don't visit files, just use live buffers.

  (let* ((data (get-text-property (point) :data))
         (index-window (selected-window))
         show-window show-buffer match)

    (unless data (error "Don't know which index entry to visit"))

    (if (eq (car data) 'index)
        (setq match (reftex-index-show-entry data no-revisit)))

    (setq show-window (selected-window)
          show-buffer (current-buffer))

    (unless match
      (select-window index-window)
      (error "Cannot find location"))

    (select-window index-window)

    ;; Use the `final' parameter to decide what to do next
    (cond
     ((eq final t)
      (reftex-unhighlight 0)
      (select-window show-window))
     ((eq final 'hide)
      (reftex-unhighlight 0)
      (or (one-window-p) (delete-window))
      (switch-to-buffer show-buffer))
     (t nil))))

(defun reftex-index-analyze-entry (data)
  ;; This splits the index context so that key, attribute and visual
  ;; values are accessible individually.
  (interactive)
  (let* ((arg (nth 5 data))
         (context (nth 2 data))
         (sc reftex-index-special-chars)
         (boa (if (string-match (regexp-quote (concat "{" arg "}")) context)
                  (1+ (match-beginning 0))
                (error "Something is wrong here")))
         (eoa (1- (match-end 0)))
         (boactual (if (string-match (concat "[^" (nth 3 sc) "]" (nth 2 sc))
                                     context boa)
                       (1+ (match-beginning 0))
                     eoa))
         (boattr (if (string-match (concat "[^" (nth 3 sc) "]" (nth 1 sc))
                                   context boa)
                     (1+ (match-beginning 0))
                   boactual))
         (pre (substring context 0 boa))
         (key (substring context boa boattr))
         (attr (substring context boattr boactual))
         (actual (substring context boactual eoa))
         (post (substring context eoa)))
    (list pre key attr actual post)))

(defun reftex-index-edit ()
  "Edit the index entry at point."
  (interactive)
  (let* ((data (get-text-property (point) :data))
         old new)
    (unless data (error "Don't know which index entry to edit"))
    (reftex-index-view-entry)
    (setq old (nth 2 data) new (read-string "Edit: " old))
    (reftex-index-change-entry new)))

(defun reftex-index-toggle-range-beginning ()
  "Toggle the page range start attribute `|('."
  (interactive)
  (let* ((data (get-text-property (point) :data))
         (bor (concat (nth 1 reftex-index-special-chars) "("))
         new analyze attr)
    (unless data (error "Don't know which index entry to edit"))
    (setq analyze (reftex-index-analyze-entry data)
          attr (nth 2 analyze))
    (setf (nth 2 analyze) (if (string= attr bor) "" bor))
    (setq new (apply 'concat analyze))
    (reftex-index-change-entry
     new (if (string= (nth 2 analyze) bor)
             "Entry is now START-OF-PAGE-RANGE"
           "START-OF-PAGE-RANGE canceled"))))

(defun reftex-index-toggle-range-end ()
  "Toggle the page-range-end attribute `|)'."
  (interactive)
  (let* ((data (get-text-property (point) :data))
         (eor (concat (nth 1 reftex-index-special-chars) ")"))
         new analyze attr)
    (unless data (error "Don't know which index entry to edit"))
    (setq analyze (reftex-index-analyze-entry data)
          attr (nth 2 analyze))
    (setf (nth 2 analyze) (if (string= attr eor) "" eor))
    (setq new (apply 'concat analyze))
    (reftex-index-change-entry
     new (if (string= (nth 2 analyze) eor)
             "Entry is now END-OF-PAGE-RANGE"
           "END-OF-PAGE-RANGE canceled"))))

(defun reftex-index-edit-key ()
  "Edit the KEY part of the index entry."
  (interactive)
  (reftex-index-edit-part nil 1 "" "Key: " t))

(defun reftex-index-edit-attribute (&optional arg)
  "EDIT the ATTRIBUTE part of the entry.  With arg: remove entire ATTRIBUTE."
  (interactive "P")
  (reftex-index-edit-part arg 2 (nth 1 reftex-index-special-chars)
                          "Attribute: "))

(defun reftex-index-edit-visual (&optional arg)
  "EDIT the VISUAL part of the entry.  With arg: remove entire VISUAL string."
  (interactive "P")
  (reftex-index-edit-part arg 3 (nth 2 reftex-index-special-chars) "Visual: "))

(defun reftex-index-edit-part (arg n initial prompt &optional dont-allow-empty)
  ;; This function does the work for all partial editing commands
  (let* ((data (get-text-property (point) :data))
         new analyze opart npart)
    (unless data (error "Don't know which index entry to edit"))
    ;; Analyze the whole context string
    (setq analyze (reftex-index-analyze-entry data)
          opart (nth n analyze))
    (and (> (length opart) 0) (setq opart (substring opart 1)))
    ;; Have the user editing the part
    (setq npart (if arg "" (read-string (concat prompt initial) opart)))
    ;; Tests:
    (cond ((string= npart opart)
           (error "Not changed"))
          ((string= npart "")
           (if dont-allow-empty
               (error "Invalid value")
             (setf (nth n analyze) npart)))
          (t (setf (nth n analyze) (concat initial npart))))
    (setq new (apply 'concat analyze))
    ;; Change the entry and insert the changed version into the index.
    (reftex-index-change-entry
     new (if (string= npart "")
             (format "Deleted: %s" opart)
           (format "New value is: %s" npart)))))

(defun reftex-index-level-down ()
  "Make index entry a subitem of another entry."
  (interactive)
  (let* ((data (get-text-property (point) :data))
         (docstruct (symbol-value reftex-docstruct-symbol))
         old new prefix key)
    (unless data (error "Don't know which index entry to change"))
    (setq old (nth 2 data)
          key (nth 6 data)
          prefix (completing-read
                  "Prefix: "
                  (reftex-sublist-nth
                   docstruct 6
                   (lambda (x)
                     (and (eq (car x) 'index)
                          (string= (nth 1 x) reftex-index-tag))) t)))
    (unless (string-match
             (concat (regexp-quote (car reftex-index-special-chars)) "\\'")
             prefix)
      (setq prefix (concat prefix (car reftex-index-special-chars))))
    (if (string-match (regexp-quote key) old)
        (setq new (replace-match (concat prefix key) t t old))
      (error "Cannot construct new index key"))
    (reftex-index-change-entry new (format "Added prefix: %s" prefix))))

(defun reftex-index-level-up ()
  "Remove the highest level of a hierarchical index entry."
  (interactive)
  (let* ((data (get-text-property (point) :data))
         old new prefix)
    (unless data (error "Don't know which entry to change"))
    (setq old (nth 2 data))
    (if (string-match (concat "{\\([^" (nth 0 reftex-index-special-chars) "]*"
                              "[^" (nth 3 reftex-index-special-chars) "]"
                              (regexp-quote (nth 0 reftex-index-special-chars))
                              "\\)")
                      old)
        (setq prefix (substring old (match-beginning 1) (match-end 1))
              new (concat (substring old 0 (match-beginning 1))
                          (substring old (match-end 1))))
      (error "Entry is not a subitem"))
    (reftex-index-change-entry new (format "Removed prefix: %s" prefix))))

(defun reftex-index-kill ()
  "FIXME: Not yet implemented."
  (interactive)
  (error "This function is currently not implemented"))

(defun reftex-index-undo ()
  "FIXME: Not yet implemented."
  (interactive)
  (error "This function is currently not implemented"))

(defun reftex-index-change-entry (new &optional message)
  ;; Change the full context string of the index entry at point to
  ;; NEW.  This actually edits the buffer where the entry is defined.

  (let* ((data (get-text-property (point) :data))
         old beg end info)
    (unless data (error "Cannot change entry"))
    (reftex-index-view-entry)
    (setq beg (match-beginning 0) end (match-end 0))
    (setq old (nth 2 data))
    (and (equal old new) (error "Entry unchanged"))
    (with-current-buffer (get-file-buffer (nth 3 data))
      (goto-char beg)
      (unless (looking-at (regexp-quote old))
        (error "This should not happen (reftex-index-change-entry)"))
      (delete-region beg end)
      (insert new)
      (goto-char (1- beg))
      (when (and (re-search-forward (reftex-everything-regexp) nil t)
                 (match-end 10)
                 (< (abs (- (match-beginning 10) beg)) (length new))
                 (setq info (reftex-index-info-safe buffer-file-name)))
        (setcdr data (cdr info))))
    (let ((buffer-read-only nil))
      (save-excursion
        (reftex-insert-index (list data) reftex-index-tag t
                             "EDITED")))
    (setq reftex-last-follow-point 1)
    (and message (message "%s" message))))

(defun reftex-index-goto-letter (char)
  "Go to the CHAR section in the index."
  (let ((pos (point))
        (case-fold-search nil))
    (goto-char (point-min))
    (forward-line 2)
    (if (re-search-forward (concat "^" (char-to-string char)) nil t)
        (progn
          (beginning-of-line)
          (recenter 0)
          (reftex-index-next))
      (goto-char pos)
      (if (eq char ?!)
          (error "This <%s> index does not contain entries sorted before the letters"
                 reftex-index-tag)
        (error "This <%s> index does not contain entries starting with `%c'"
               reftex-index-tag char)))))


;;----------------------------------------------------------------------
;; The Index Phrases File

;; Some constants and variables
(defconst reftex-index-phrases-comment-regexp "^[ \t]*%.*"
  "Regular expression to match comment lines in phrases buffer")
(defconst reftex-index-phrases-macrodef-regexp
  "^\\(>>>INDEX_MACRO_DEFINITION:\\)[ \t]+\\(\\S-\\)\\( *\t[ \t]*\\)\\([^\t]*[^ \t]\\)\\( *\t[ \t]*\\)\\(\\S-+\\)"
  "Regular expression to match macro definition lines the phrases buffer.")
;(defconst reftex-index-phrases-macrodef-regexp
;  "^\\(>>>INDEX_MACRO_DEFINITION:\\)[ \t]+\\(\\S-\\)\\([ \t]*\\)\\([^\t]*[^ \t]\\)\\([ \t]*\\)\\(nil\\|t\\)[ \t]*$"
;  "Regular expression to match macro definition lines the phrases buffer.
;This version would allow just spaces as separators.")
(defconst reftex-index-phrases-phrase-regexp1
  "^\\(\\S-?\\)\\(\t\\)\\([^\t\n]*\\S-\\)\\([ \t]*\\)$"
  "Regular expression matching phrases which have no separate index key.")
(defconst reftex-index-phrases-phrase-regexp2
  "^\\(\\S-?\\)\\(\t\\)\\([^\t]*\\S-\\)\\(\t[ \t]*\\)\\([^\n\t]*\\S-\\)[ \t]*$"
  "Regular expression matching phrases which have a separate index key.")
(defconst reftex-index-phrases-phrase-regexp12
  "^\\(\\S-?\\)\\(\t\\)\\([^\n\t]*\\S-\\)\\(\\(\t[ \t]*\\)\\([^\n\t]*\\S-\\)\\)?[ \t]*$"
  "Regular expression matching all types of phrase lines.")
(defvar reftex-index-phrases-macro-data nil
  "Alist containing the information taken from the macro definition lines.
This gets refreshed in every phrases command.")
(defvar reftex-index-phrases-files nil
  "List of document files relevant for the phrases file.")

(defvar reftex-index-phrases-font-lock-keywords nil
  "Font lock keywords for reftex-index-phrases-mode.")
(defvar reftex-index-phrases-font-lock-defaults nil
  "Font lock defaults for reftex-index-phrases-mode.")
(define-obsolete-variable-alias
  'reftex-index-phrases-map 'reftex-index-phrases-mode-map "24.1")
(defvar reftex-index-phrases-mode-map
  (let ((map (make-sparse-keymap)))
    ;; Keybindings and Menu for phrases buffer
    (define-key map "\C-c\C-c" 'reftex-index-phrases-save-and-return)
    (define-key map "\C-c\C-x" 'reftex-index-this-phrase)
    (define-key map "\C-c\C-f" 'reftex-index-next-phrase)
    (define-key map "\C-c\C-r" 'reftex-index-region-phrases)
    (define-key map "\C-c\C-a" 'reftex-index-all-phrases)
    (define-key map "\C-c\C-d" 'reftex-index-remaining-phrases)
    (define-key map "\C-c\C-s" 'reftex-index-sort-phrases)
    (define-key map "\C-c\C-n" 'reftex-index-new-phrase)
    (define-key map "\C-c\C-m" 'reftex-index-phrases-set-macro-key)
    (define-key map "\C-c\C-i" 'reftex-index-phrases-info)
    (define-key map "\C-c\C-t" 'reftex-index-find-next-conflict-phrase)
    (define-key map "\C-i" 'self-insert-command)

    (easy-menu-define reftex-index-phrases-menu map
      "Menu for Phrases buffer"
      '("Phrases"
        ["New Phrase" reftex-index-new-phrase t]
        ["Set Phrase Macro" reftex-index-phrases-set-macro-key t]
        ["Recreate File Header" reftex-index-initialize-phrases-buffer t]
        "--"
        ("Sort Phrases"
         ["Sort" reftex-index-sort-phrases t]
         "--"
         "Sort Options"
         ["by Search Phrase" (setq reftex-index-phrases-sort-prefers-entry nil)
          :style radio :selected (not reftex-index-phrases-sort-prefers-entry)]
         ["by Index Entry" (setq reftex-index-phrases-sort-prefers-entry t)
          :style radio :selected reftex-index-phrases-sort-prefers-entry]
         ["in Blocks" (setq reftex-index-phrases-sort-in-blocks
                            (not reftex-index-phrases-sort-in-blocks))
          :style toggle :selected reftex-index-phrases-sort-in-blocks])
        ["Describe Phrase" reftex-index-phrases-info t]
        ["Next Phrase Conflict" reftex-index-find-next-conflict-phrase t]
        "--"
        ("Find and Index in Document"
         ["Current Phrase" reftex-index-this-phrase t]
         ["Next Phrase" reftex-index-next-phrase t]
         ["Current and Following" reftex-index-remaining-phrases t]
         ["Region Phrases" reftex-index-region-phrases t]
         ["All Phrases" reftex-index-all-phrases t]
         "--"
         "Options"
         ["Match Whole Words" (setq reftex-index-phrases-search-whole-words
                                    (not reftex-index-phrases-search-whole-words))
          :style toggle :selected reftex-index-phrases-search-whole-words]
         ["Case Sensitive Search" (setq reftex-index-phrases-case-fold-search
                                        (not  reftex-index-phrases-case-fold-search))
          :style toggle :selected (not
                                   reftex-index-phrases-case-fold-search)]
         ["Wrap Long Lines" (setq reftex-index-phrases-wrap-long-lines
                                  (not reftex-index-phrases-wrap-long-lines))
          :style toggle :selected reftex-index-phrases-wrap-long-lines]
         ["Skip Indexed Matches" (setq reftex-index-phrases-skip-indexed-matches
                                       (not reftex-index-phrases-skip-indexed-matches))
          :style toggle :selected reftex-index-phrases-skip-indexed-matches])
        "--"
        ["Save and Return" reftex-index-phrases-save-and-return t]))

    map)
  "Keymap used for index phrases buffer.")
(defvar reftex-index-phrases-syntax-table
  (let ((table (make-syntax-table)))
    (modify-syntax-entry ?\" "." table)
    table)
  "Syntax table for RefTeX Index Phrases mode.")

;;;###autoload
(defun reftex-index-phrase-selection-or-word (arg)
  "Add current selection or word at point to the phrases buffer.
When you are in transient-mark-mode and the region is active, the
selection will be used - otherwise the word at point.
You get a chance to edit the entry in the phrases buffer - finish with
`C-c C-c'."
  (interactive "P")
  (set-marker reftex-index-return-marker (point))
  (reftex-index-selection-or-word arg 'phrase)
  (if (eq major-mode 'reftex-index-phrases-mode)
      (message "%s"
       (substitute-command-keys
        "Return to LaTeX with \\[reftex-index-phrases-save-and-return]"))))

;;;###autoload
(defun reftex-index-visit-phrases-buffer ()
  "Switch to the phrases buffer, initialize if empty."
  (interactive)
  (reftex-access-scan-info)
  (set-marker reftex-index-return-marker (point))
  (let* ((master (reftex-TeX-master-file))
         (name (concat (file-name-sans-extension master)
                       reftex-index-phrase-file-extension)))
    (find-file name)
    (unless (eq major-mode 'reftex-index-phrases-mode)
      (reftex-index-phrases-mode))
    (if (= (buffer-size) 0)
        (reftex-index-initialize-phrases-buffer master))))

(defun reftex-index-initialize-phrases-buffer (&optional master)
  "Initialize the phrases buffer by creating the header.
If the buffer is non-empty, delete the old header first."
  (interactive)
  (let* ((case-fold-search t)
         (default-key (car reftex-index-default-macro))
         (default-macro (nth 1 (assoc default-key
                                      reftex-key-to-index-macro-alist)))
         (macro-alist
          (sort (copy-sequence reftex-index-macro-alist)
                (lambda (a b) (equal (car a) default-macro))))
         macro entry key repeat)

    (if master (set (make-local-variable 'TeX-master)
                    (file-name-nondirectory master)))

    (when (> (buffer-size) 0)
      (goto-char 1)
      (set-mark (point))
      (while (re-search-forward reftex-index-phrases-macrodef-regexp nil t)
        (end-of-line))
      (beginning-of-line 2)
      (if (looking-at reftex-index-phrases-comment-regexp)
          (beginning-of-line 2))
      (while (looking-at "^[ \t]*$")
          (beginning-of-line 2))
      (if (featurep 'xemacs)
	  (zmacs-activate-region)
	(setq mark-active t))
      (if (yes-or-no-p "Delete and rebuild header? ")
          (delete-region (point-min) (point))))

    ;; Insert the mode line
    (insert
     (format "%% -*- mode: reftex-index-phrases; TeX-master: \"%s\" -*-\n"
             (file-name-nondirectory (reftex-index-phrase-tex-master))))
    ;; Insert the macro definitions
    (insert "%                              Key      Macro Format            Repeat\n")
    (insert "%---------------------------------------------------------------------\n")
    (while (setq entry (pop macro-alist))
      (setq macro (car entry)
            repeat (nth 7 entry)
            key (car (delq nil (mapcar (lambda (x) (if (equal (nth 1 x) macro)
                                                       (car x)
                                                     nil))
                                       reftex-key-to-index-macro-alist))))
      (insert (format ">>>INDEX_MACRO_DEFINITION:\t%s\t%-20s\t%s\n"
                      (char-to-string key) (concat macro "{%s}")
                      (if repeat "t" "nil"))))
    (insert "%---------------------------------------------------------------------\n\n\n")))

(defvar TeX-master)
(defun reftex-index-phrase-tex-master (&optional dir)
  "Return the name of the master file associated with a phrase buffer."
  (if (and (boundp 'TeX-master)
           (local-variable-p 'TeX-master (current-buffer))
           (stringp TeX-master))
      ;; We have a local variable which tells us which file to use
      (expand-file-name TeX-master dir)
    ;; have to guess
    (concat (file-name-sans-extension (buffer-file-name)) ".tex")))

(defun reftex-index-phrases-save-and-return ()
  "Return to where the `reftex-index-phrase-selection-or-word' was called."
  (interactive)
  (save-buffer)
  (switch-to-buffer (marker-buffer reftex-index-return-marker))
  (goto-char (or (marker-position reftex-index-return-marker) (point))))


(defvar reftex-index-phrases-menu)
(defvar reftex-index-phrases-marker)
(defvar reftex-index-phrases-restrict-file nil)
;; NB this is a global autoload - see reftex.el.
;;;###autoload
(define-derived-mode reftex-index-phrases-mode fundamental-mode "Phrases"
  "Major mode for managing the Index phrases of a LaTeX document.
This buffer was created with RefTeX.

To insert new phrases, use
 - `C-c \\' in the LaTeX document to copy selection or word
 - `\\[reftex-index-new-phrase]' in the phrases buffer.

To index phrases use one of:

\\[reftex-index-this-phrase]     index current phrase
\\[reftex-index-next-phrase]     index next phrase (or N with prefix arg)
\\[reftex-index-all-phrases]     index all phrases
\\[reftex-index-remaining-phrases]     index current and following phrases
\\[reftex-index-region-phrases]     index the phrases in the region

You can sort the phrases in this buffer with \\[reftex-index-sort-phrases].
To display information about the phrase at point, use \\[reftex-index-phrases-info].

For more information see the RefTeX User Manual.

Here are all local bindings.

\\{reftex-index-phrases-mode-map}"
  :syntax-table reftex-index-phrases-syntax-table
  (set (make-local-variable 'font-lock-defaults)
       reftex-index-phrases-font-lock-defaults)
  (when (featurep 'xemacs)
    (easy-menu-add reftex-index-phrases-menu reftex-index-phrases-mode-map))
  (set (make-local-variable 'reftex-index-phrases-marker) (make-marker)))
;; (add-hook 'reftex-index-phrases-mode-hook 'turn-on-font-lock)

;; Font Locking stuff
(let ((ss (if (featurep 'xemacs) 'secondary-selection ''secondary-selection)))
  (setq reftex-index-phrases-font-lock-keywords
        (list
         (cons reftex-index-phrases-comment-regexp 'font-lock-comment-face)
         (list reftex-index-phrases-macrodef-regexp
               '(1 font-lock-type-face)
               '(2 font-lock-keyword-face)
               (list 3 ss)
               '(4 font-lock-function-name-face)
               (list 5 ss)
               '(6 font-lock-string-face))
         (list reftex-index-phrases-phrase-regexp1
               '(1 font-lock-keyword-face)
               (list 2 ss)
               '(3 font-lock-string-face)
               (list 4 ss))
         (list reftex-index-phrases-phrase-regexp2
               '(1 font-lock-keyword-face)
               (list 2 ss)
               '(3 font-lock-string-face)
               (list 4 ss)
               '(5 font-lock-function-name-face))
         (cons "^\t$" ss)))
  (setq reftex-index-phrases-font-lock-defaults
        '((reftex-index-phrases-font-lock-keywords)
          nil t nil beginning-of-line))
  (put 'reftex-index-phrases-mode 'font-lock-defaults
       reftex-index-phrases-font-lock-defaults) ; XEmacs
  )

(defun reftex-index-next-phrase (&optional arg)
  "Index the next ARG phrases in the phrases buffer."
  (interactive "p")
  (reftex-index-phrases-parse-header t)
  (while (> arg 0)
    (cl-decf arg)
    (end-of-line)
    (if (re-search-forward reftex-index-phrases-phrase-regexp12 nil t)
        (progn
          (goto-char (match-beginning 0))
          (reftex-index-this-phrase 'slave))
      (error "No more phrase lines after point"))))

(defun reftex-index-this-phrase (&optional slave)
  "Index the phrase in the current line.
Does a global search and replace in the entire document.  At each
match, the user will be asked to confirm the replacement."
  (interactive)
  (if (not slave) (reftex-index-phrases-parse-header t))
  (save-excursion
    (beginning-of-line)
    (cond ((looking-at reftex-index-phrases-comment-regexp)
           (if (not slave) (error "Comment line")))
          ((looking-at "^[ \t]*$")
           (if (not slave) (error "Empty line")))
          ((looking-at reftex-index-phrases-macrodef-regexp)
           (if (not slave) (error "Macro definition line")))
          ((looking-at reftex-index-phrases-phrase-regexp12)
           ;; This is a phrase
           (let* ((char (if (not (equal (match-string 1) ""))
                            (string-to-char (match-string 1))))
                  (phrase (match-string 3))
                  (index-key (match-string 6))
                  (macro-data (cdr (if (null char)
                                       (car reftex-index-phrases-macro-data)
                                     (assoc char reftex-index-phrases-macro-data))))
                  (macro-fmt (car macro-data))
                  (repeat (nth 1 macro-data))
                  (files
                   (cond ((and (stringp reftex-index-phrases-restrict-file)
                               (file-regular-p reftex-index-phrases-restrict-file))
                          (list reftex-index-phrases-restrict-file))
                         ((stringp reftex-index-phrases-restrict-file)
                          (error "Invalid restriction file %s"
                                 reftex-index-phrases-restrict-file))
                         (t reftex-index-phrases-files)))
                  (as-words reftex-index-phrases-search-whole-words))
             (unless macro-data
               (error "No macro associated with key %c" char))
             (unwind-protect
                 (let ((overlay-arrow-string "=>")
                       (overlay-arrow-position
                        reftex-index-phrases-marker)
                       (replace-count 0))
                   ;; Show the overlay arrow
                   (move-marker reftex-index-phrases-marker
                                (match-beginning 0) (current-buffer))
                   ;; Start the query-replace
                   (reftex-query-index-phrase-globally
                    files phrase macro-fmt
                    index-key repeat as-words)
                   (message "%s replaced"
                            (reftex-number replace-count "occurrence"))))))
          (t (error "Cannot parse this line")))))

(defun reftex-index-all-phrases ()
  "Index all phrases in the phrases buffer.
Calls `reftex-index-this-phrase' on each line in the buffer."
  (interactive)
  (reftex-index-region-phrases (point-min) (point-max)))

(defun reftex-index-remaining-phrases ()
  "Index all phrases in the phrases buffer.
Calls `reftex-index-this-phrase' on each line ay and below point in
the buffer."
  (interactive)
  (beginning-of-line)
  (reftex-index-region-phrases (point) (point-max)))

(defun reftex-index-region-phrases (beg end)
  "Index all phrases in the phrases buffer.
Calls `reftex-index-this-phrase' on each line in the region."
  (interactive "r")
  (reftex-index-phrases-parse-header t)
  (goto-char beg)
  (while (not (or (eobp)
                  (>= (point) end)))
    (save-excursion (reftex-index-this-phrase 'slave))
    (beginning-of-line 2)))

(defun reftex-index-phrases-parse-header (&optional get-files)
  "Parse the header of a phrases file to extract the macro definitions.
The definitions get stored in `reftex-index-phrases-macro-data'.
Also switches to the LaTeX document to find out which files belong to
the document and stores the list in `reftex-index-phrases-files'."
  (let* ((master (reftex-index-phrase-tex-master))
         buf)
    (if get-files
        ;; Get the file list
        (save-excursion
          (setq buf (reftex-get-file-buffer-force master))
          (unless buf (error "Master file %s not found" master))
          (set-buffer buf)
          (reftex-access-scan-info)
          (setq reftex-index-phrases-files
                (reftex-all-document-files))))
    ;; Parse the files header for macro definitions
    (setq reftex-index-phrases-macro-data nil)
    (save-excursion
      (goto-char (point-min))
      (while (re-search-forward reftex-index-phrases-macrodef-regexp nil t)
        (push (list
               (string-to-char (match-string 2))
               (match-string 4)
               (equal (match-string 6) "t"))
              reftex-index-phrases-macro-data))
      ;; Reverse the list, so that the first macro is first
      (if (null reftex-index-phrases-macro-data)
          (error "No valid MACRO DEFINITION line in %s file (make sure to use TAB separators)" reftex-index-phrase-file-extension))
      (setq reftex-index-phrases-macro-data
            (nreverse reftex-index-phrases-macro-data))
      (goto-char (point-min)))))

(defun reftex-index-phrases-apply-to-region (beg end)
  "Index all index phrases in the current region.
This works exactly like global indexing from the index phrases buffer,
but operation is restricted to the current region.  This is useful if
you need to add/change text in an already indexed document and want to
index the new part without having to go over the unchanged parts again."
  (interactive "r")
  (let ((win-conf (current-window-configuration))
        (reftex-index-phrases-restrict-file (buffer-file-name)))
  (save-excursion
    (save-restriction
      (narrow-to-region beg end)
      (unwind-protect
          (progn
            ;; Hide the region highlighting
            (if (featurep 'xemacs)
		(zmacs-deactivate-region)
	      (deactivate-mark))
            (delete-other-windows)
            (reftex-index-visit-phrases-buffer)
            (reftex-index-all-phrases))
        (set-window-configuration win-conf))))))

(defun reftex-index-new-phrase (&optional text)
  "Open a new line in the phrases buffer, insert TEXT."
  (interactive)
  (if (and text (stringp text))
      (progn
        ;; Check if the phrase is already in the buffer
        (setq text (reftex-index-simplify-phrase text))
        (goto-char (point-min))
        (if (re-search-forward
             (concat "^\\(\\S-*\\)\t\\(" (regexp-quote text)
                     "\\) *[\t\n]") nil t)
            (progn
              (goto-char (match-end 2))
              (error "Phrase is already in phrases buffer")))))
  ;; Add the new phrase line after the last in the buffer
  (goto-char (point-max))
  (if (re-search-backward reftex-index-phrases-phrase-regexp12 nil t)
      (end-of-line))
  (if (not (bolp))
      (insert "\n"))
  (insert "\t")
  (if (and text (stringp text))
      (insert text)))

(defun reftex-index-find-next-conflict-phrase (&optional arg)
  "Find the next a phrase which is has conflicts in the phrase buffer.
The command helps to find possible conflicts in the phrase indexing process.
It searches downward from point for a phrase which is repeated elsewhere
in the buffer, or which is a subphrase of another phrase.  If such a
phrase is found, the phrase info is displayed.
To check the whole buffer, start at the beginning and continue by calling
this function repeatedly."
  (interactive "P")
  (if (catch 'exit
        (while (re-search-forward reftex-index-phrases-phrase-regexp12 nil t)
          (goto-char (match-beginning 3))
          (let* ((phrase (match-string 3))
                 (case-fold-search reftex-index-phrases-case-fold-search)
                 (re (reftex-index-phrases-find-dup-re phrase t)))
            (if (save-excursion
                  (goto-char (point-min))
                  (and (re-search-forward re nil t)
                       (re-search-forward re nil t)))
                (throw 'exit t)))))
      (progn
        (reftex-index-phrases-info)
        (message "Phrase with match conflict discovered"))
    (goto-char (point-max))
    (error "No further problematic phrases found")))

(defun reftex-index-phrases-info ()
  "Display information about the phrase at point."
  (interactive)
  (save-excursion
    (beginning-of-line)
    (unless (looking-at reftex-index-phrases-phrase-regexp12)
      (error "Not a phrase line"))
    (save-match-data (reftex-index-phrases-parse-header t))
    (let* ((char (if (not (equal (match-string 1) ""))
                     (string-to-char (match-string 1))))
           (phrase (match-string 3))
           (index-key (match-string 6))
           (index-keys (split-string
                        (or index-key phrase)
                        reftex-index-phrases-logical-or-regexp))
           (macro-data (cdr (if (null char)
                                (car reftex-index-phrases-macro-data)
                              (assoc char reftex-index-phrases-macro-data))))
           (macro-fmt (car macro-data))
           (repeat (nth 1 macro-data))
           (as-words reftex-index-phrases-search-whole-words)
           (example (reftex-index-make-replace-string
                     macro-fmt (downcase phrase) (car index-keys) repeat))
           (re (reftex-index-make-phrase-regexp phrase as-words t))
           (re1 (reftex-index-phrases-find-dup-re phrase))
           (re2 (reftex-index-phrases-find-dup-re phrase 'sub))
           superphrases
           (nmatches 0)
           (ntimes1 0)
           (ntimes2 0)
           (case-fold-search reftex-index-phrases-case-fold-search)
           file files buf)
      (setq files reftex-index-phrases-files)
      (save-excursion
        (save-restriction
          (widen)
          (goto-char (point-min))
          (while (re-search-forward re1 nil t)
            (cl-incf ntimes1))
          (goto-char (point-min))
          (while (re-search-forward re2 nil t)
            (push (cons (count-lines 1 (point)) (match-string 1)) superphrases)
            (cl-incf ntimes2))))
      (save-current-buffer
        (while (setq file (pop files))
          (setq buf (reftex-get-file-buffer-force file))
          (when buf
            (set-buffer buf)
            (save-excursion
              (save-restriction
                (widen)
                (goto-char (point-min))
                (let ((case-fold-search reftex-index-phrases-case-fold-search))
                  (while (re-search-forward re nil t)
                    (or (reftex-in-comment)
                        (cl-incf nmatches)))))))))
      (with-output-to-temp-buffer "*Help*"
        (princ (format "       Phrase:  %s\n" phrase))
        (princ (format "    Macro key:  %s\n" char))
        (princ (format " Macro format:  %s\n" macro-fmt))
        (princ (format "       Repeat:  %s\n" repeat))
        (cond
         (index-key
          (let ((iks index-keys) (cnt 0) ik)
            (while (setq ik (pop iks))
              (princ (format "Index entry %d:  %s\n" (cl-incf cnt) ik)))))
         (repeat
          (princ (format "  Index entry:  %s\n" phrase)))
         (t
          (princ (format "    Index key:  <<Given by the match>>\n"))))
        (princ (format "      Example:  %s\n" example))
        (terpri)
        (princ (format "Total matches:  %s in %s\n"
                       (reftex-number nmatches "match" "es")
                       (reftex-number (length reftex-index-phrases-files)
                                      "LaTeX file")))
        (princ (format "   Uniqueness:  Phrase occurs %s in phrase buffer\n"
                       (reftex-number ntimes1 "time")))
        (if (> ntimes2 1)
            (progn
              (princ (format " Superphrases:  Phrase matches the following %s in the phrase buffer:\n"
                             (reftex-number ntimes2 "line")))
              (mapcar (lambda(x)
                        (princ (format "                Line %4d:  %s\n" (car x) (cdr x))))
                      (nreverse superphrases))))))))

(defun reftex-index-phrases-set-macro-key ()
  "Change the macro key for the current line.
Prompts for a macro key and insert is at the beginning of the line.
If you reply with SPACE, the macro keyn will be removed, so that the
default macro will be used.  If you reply with `RET', just prints
information about the currently selected macro."
  (interactive)
  (reftex-index-phrases-parse-header)
  (save-excursion
    (beginning-of-line)
    (unless (or (looking-at reftex-index-phrases-phrase-regexp12)
                (looking-at "\t"))
      (error "This is not a phrase line"))
    (let* ((nc (reftex-index-select-phrases-macro 0))
           (macro-data (assoc nc reftex-index-phrases-macro-data))
           macro-fmt repeat)
      (cond (macro-data)
            ((equal nc ?\ )
             (setq nc ""
                   macro-data (car reftex-index-phrases-macro-data)))
            ((equal nc ?\C-m)
             (setq nc (char-after (point)))
             (if (equal nc ?\t)
                 (setq nc ""
                       macro-data (car reftex-index-phrases-macro-data))
               (setq macro-data (assoc nc reftex-index-phrases-macro-data))))
            (t (error "No macro associated with %c" nc)))

      (setq macro-fmt (nth 1 macro-data)
            repeat (nth 2 macro-data))
      (if macro-data
          (progn
            (if (looking-at "[^\t]") (delete-char 1))
            (insert nc)
            (message "Line will use %s %s repeat" macro-fmt
                     (if repeat "with" "without")))
        (error "Abort")))))

(defun reftex-index-sort-phrases (&optional chars-first)
  "Sort the phrases lines in the buffer alphabetically.
Normally, this looks only at the phrases.  With a prefix arg CHARS-FIRST,
it first compares the macro identifying chars and then the phrases."
  (interactive "P")
  ;; Remember the current line, so that we can return
  (let ((line (buffer-substring (progn (beginning-of-line) (point))
                                (progn (end-of-line) (point))))
        beg end)
    (goto-char (point-min))
    ;; Find first and last phrase line in buffer
    (setq beg
          (and (re-search-forward reftex-index-phrases-phrase-regexp12 nil t)
               (match-beginning 0)))
    (goto-char (point-max))
    (setq end (re-search-backward reftex-index-phrases-phrase-regexp12 nil t))
    (if end (setq end (progn (goto-char end) (end-of-line) (point))))
    ;; Take the lines, sort them and re-insert.
    (if (and beg end)
        (progn
          (message "Sorting lines...")
          (let* ((lines (split-string (buffer-substring beg end) "\n"))
                 (lines1 (sort lines 'reftex-compare-phrase-lines)))
            (message "Sorting lines...done")
            (let ((inhibit-quit t))  ;; make sure we do not lose lines
              (delete-region beg end)
              (insert (mapconcat 'identity lines1 "\n"))))
          (goto-char (point-max))
          (re-search-backward (concat "^" (regexp-quote line) "$") nil t))
      (error "Cannot find phrases lines to sort"))))

(defvar chars-first)
(defun reftex-compare-phrase-lines (a b)
  "The comparison function used for sorting."
  (let (ca cb pa pb c-p p-p)
    (if (string-match reftex-index-phrases-phrase-regexp12 a)
        (progn
          ;; Extract macro char and phrase-or-key for a
          (setq ca (match-string 1 a)
                pa (downcase
                    (or (and reftex-index-phrases-sort-prefers-entry
                             (match-string 6 a))
                        (match-string 3 a))))
          (if (string-match reftex-index-phrases-phrase-regexp12 b)
              (progn
                ;; Extract macro char and phrase-or-key for b
                (setq cb (match-string 1 b)
                      pb (downcase
                          (or (and reftex-index-phrases-sort-prefers-entry
                                   (match-string 6 b))
                              (match-string 3 b))))
                (setq c-p (string< ca cb)
                      p-p (string< pa pb))
                ;; Do the right comparison, based on the value of `chars-first'
                ;; `chars-first' is bound locally in the calling function
                (if chars-first
                    (if (string= ca cb) p-p c-p)
                  (if (string= pa pb) c-p p-p)))))
      ;; If line a does not match, the answer we return determines
      ;; if non-matching lines are collected at the beginning.
      ;; When we return t here, non-matching lines form
      ;; block separators for searches.
      (not reftex-index-phrases-sort-in-blocks))))

(defvar reftex-index-phrases-menu)
(defun reftex-index-make-phrase-regexp (phrase &optional
                                               as-words allow-newline)
  "Return a regexp matching PHRASE, even if distributed over lines.
With optional arg AS-WORDS, require word boundary at beginning and end.
With optional arg ALLOW-NEWLINE, allow single newline between words."
  (let* ((words (split-string phrase))
         (space-re (if allow-newline
                       "\\([ \t]*\\(\n[ \t]*\\)?\\|[ \t]\\)"
                     "\\([ \t]+\\)")))
    (concat (if (and as-words (string-match "\\`\\w" (car words)))
                "\\(\\<\\|[`']\\)" "")
            (mapconcat (lambda (w) (regexp-quote
                                    (if reftex-index-phrases-case-fold-search
                                        (downcase w)
                                      w)))
                       words space-re)
            (if (and as-words
                     (string-match "\\w\\'" (nth (1- (length words)) words)))
                "\\(\\>\\|'\\)" ""))))

(defun reftex-index-simplify-phrase (phrase)
  "Make phrase single spaces and single line."
  (mapconcat 'identity (split-string phrase) " "))

(defun reftex-index-phrases-find-dup-re (phrase &optional sub)
  "Return a regexp which matches variations of PHRASE (with additional space).
When SUB ins non-nil, the regexp will also match when PHRASE is a subphrase
of another phrase.  The regexp works lonly in the phrase buffer."
  (concat (if sub "^\\S-?\t\\([^\t\n]*" "^\\S-?\t")
          (mapconcat 'regexp-quote (split-string phrase) " +")
          (if sub "[^\t\n]*\\)\\([\t\n]\\|$\\)" " *\\([\t\n]\\|$\\)")))

(defun reftex-index-make-replace-string (macro-fmt match index-key
                                                   &optional repeat mathp)
  "Return the string which can be used as replacement.
Treats the logical `and' for index phrases."
  (let ((index-keys (split-string (or index-key match)
                                  reftex-index-phrases-logical-and-regexp)))
    (concat
     (mapconcat (lambda (x)
                  (format macro-fmt
                          (format (if mathp reftex-index-math-format "%s") x)))
                index-keys "")
   (if repeat (reftex-index-simplify-phrase match) ""))))

(defun reftex-query-index-phrase-globally (files &rest args)
  "Call `reftex-query-index-phrase' for all files in FILES."
  (let ((win-conf (current-window-configuration))
        (file))
    (unless files (error "No files"))
    (unwind-protect
        (progn
          (switch-to-buffer-other-window (reftex-get-file-buffer-force
                                          (car files)))
          (catch 'no-more-files
            (while (setq file (pop files))
              (switch-to-buffer (reftex-get-file-buffer-force file))
              (save-excursion
                (save-restriction
                  (unless (stringp reftex-index-phrases-restrict-file)
                    (widen))
                  (goto-char (point-min))
                  (apply 'reftex-query-index-phrase args))))))
      (reftex-unhighlight 0)
      (set-window-configuration win-conf))))

(defconst reftex-index-phrases-help
  "     Keys for query-index search
     ===========================
y       Replace this match
n       Skip this match
!       Replace this and all further matches in this file
q / Q   Skip match, start next file / start next phrase
o       Use a different indexing macro for this match
1 - 9   Select one of the multiple phrases
e       Edit the replacement text
C-r     Recursive edit.
s / S   Save this buffer  /  Save all document buffers
C-g     Abort"
  "The help string for indexing phrases.")

(defvar replace-count)
(defun reftex-query-index-phrase (phrase macro-fmt &optional
                                         index-key repeat as-words)
  "Search through buffer for PHRASE, and offer to replace it with an indexed
version.  The index version is derived by applying `format' with MACRO-FMT
to INDEX-KEY or PHRASE.  When REPEAT is non-nil, the PHRASE is inserted
again after the macro.
AS-WORDS means, the search for PHRASE should require word boundaries at
both ends."
  (let* ((re (reftex-index-make-phrase-regexp phrase as-words 'allow-newline))
         (case-fold-search reftex-index-phrases-case-fold-search)
         (index-keys (split-string
                      (or index-key phrase)
                      reftex-index-phrases-logical-or-regexp))
         (nkeys (length index-keys))
         (ckey (nth 0 index-keys))
         (all-yes nil)
         match rpl char (beg (make-marker)) (end (make-marker)) mathp)
    (move-marker beg 1)
    (move-marker end 1)
    (unwind-protect
        (while (re-search-forward re nil t)
          (catch 'next-match
            (if (reftex-in-comment)
                (throw 'next-match nil))
            (if (and (fboundp reftex-index-verify-function)
                     (not (funcall reftex-index-verify-function)))
                (throw 'next-match nil))
            (setq match (match-string 0))
            (setq mathp
                  (save-match-data
                    (condition-case nil (texmathp) (error nil))))
            (setq beg (move-marker beg (match-beginning 0))
                  end (move-marker end (match-end 0)))
            (if (and reftex-index-phrases-skip-indexed-matches
                     (save-match-data
                       (reftex-index-phrase-match-is-indexed beg
                                                             end)))
                (throw 'next-match nil))
            (reftex-highlight 0 (match-beginning 0) (match-end 0))
            (setq rpl
                  (save-match-data
                    (reftex-index-make-replace-string
                     macro-fmt (match-string 0) ckey repeat mathp)))
            (while
                (not
                 (catch 'loop
                   (message "REPLACE: %s?   (yn!qoe%s?)"
                            rpl
                            (if (> nkeys 1)
                                (concat "1-" (int-to-string nkeys))
                              ""))
                   (setq char (if all-yes ?y (read-char-exclusive)))
                   (cond ((member char '(?y ?Y ?\ ))
                          ;; Yes!
                          (replace-match rpl t t)
                          (cl-incf replace-count)
                          ;; See if we should insert newlines to shorten lines
                          (and reftex-index-phrases-wrap-long-lines
                               (reftex-index-phrases-fixup-line beg end))
                          (throw 'loop t))
                         ((member char '(?n ?N ?\C-h ?\C-?));; FIXME: DEL
                          ;; No
                          (throw 'loop t))
                         ((equal char ?!)
                          ;; Yes for all in this buffer
                          (setq all-yes t))
                         ((equal char ?q)
                          ;; Stop this one in this file
                          (goto-char (point-max))
                          (throw 'loop t))
                         ((equal char ?Q)
                          ;; Stop this one
                          (throw 'no-more-files t))
                         ((equal char ?s)
                          (save-buffer))
                         ((equal char ?S)
                          (reftex-save-all-document-buffers))
                         ((equal char ?\C-g)
                          (keyboard-quit))
                         ((member char '(?o ?O))
                          ;; Select a different macro
                          (let* ((nc (reftex-index-select-phrases-macro 2))
                                 (macro-data
                                  (cdr (assoc nc reftex-index-phrases-macro-data)))
                                 (macro-fmt (car macro-data))
                                 (repeat (nth 1 macro-data)))
                            (if macro-data
                                (setq rpl (save-match-data
                                            (reftex-index-make-replace-string
                                             macro-fmt match
                                             ckey repeat mathp)))
                              (ding))))
                         ((equal char ?\?)
                          ;; Help
                          (with-output-to-temp-buffer "*Help*"
                            (princ reftex-index-phrases-help)))
                         ((equal char ?\C-r)
                          ;; Recursive edit
                          (save-match-data
                            (save-excursion
                              (message "%s"
                               (substitute-command-keys
                                "Recursive edit.  Resume with \\[exit-recursive-edit]"))
                              (recursive-edit))))
                         ((equal char ?e)
                          (setq rpl (read-string "Edit: " rpl)))
                         ((equal char ?0)
                          (setq ckey (or index-key phrase)
                                rpl (save-match-data
                                      (reftex-index-make-replace-string
                                       macro-fmt match ckey repeat mathp))))
                         ((and (> char ?0)
                               (<= char (+ ?0 nkeys)))
                          (setq ckey (nth (1- (- char ?0)) index-keys)
                                rpl (save-match-data
                                      (reftex-index-make-replace-string
                                       macro-fmt match ckey repeat mathp))))
                         (t (ding)))
                   nil)))))
      (message "")
      (move-marker beg nil)
      (move-marker end nil)
      (setq all-yes nil)
      (reftex-unhighlight 0))))

(defun reftex-index-phrase-match-is-indexed (beg end)
  ;; Check if match is in an argument of an index macro, or if an
  ;; index macro is directly attached to the match.
  (save-excursion
    (goto-char end)
    (let* ((all-macros (reftex-what-macro t))
;           (this-macro (car (car all-macros)))
           (before-macro
            (and (> beg 2)
                 (goto-char (1- beg))
                 (memq (char-after (point)) '(?\] ?\}))
                 (car (reftex-what-macro 1))))
           (after-macro
            (and (goto-char end)
                 (looking-at "\\(\\\\[a-zA-Z]+\\*?\\)[[{]")
                 (match-string 1)))
           macro)
      (or (catch 'matched
            (while (setq macro (pop all-macros))
              (if (member (car macro) reftex-macros-with-index)
                  (throw 'matched t)))
            nil)
          (and before-macro
               (member before-macro reftex-macros-with-index))
          (and after-macro
               (member after-macro reftex-macros-with-index))))))

(defun reftex-index-phrases-fixup-line (beg end)
  "Insert newlines before BEG and/or after END to shorten line."
  (let (bol eol space1 space2)
    (save-excursion
      ;; Find line boundaries and possible line breaks near BEG and END
      (beginning-of-line)
      (setq bol (point))
      (end-of-line)
      (setq eol (point))
      (goto-char beg)
      (skip-chars-backward "^ \n")
      (if (and (equal (preceding-char) ?\ )
               (string-match "\\S-" (buffer-substring bol (point))))
          (setq space1 (1- (point))))
      (goto-char end)
      (skip-chars-forward "^ \n")
      (if (and (equal (following-char) ?\ )
               (string-match "\\S-" (buffer-substring (point) eol)))
          (setq space2 (point)))
      ;; Now check what we have and insert the newlines
      (if (<= (- eol bol) fill-column)
          ;; Line is already short
          nil
        (cond
         ((and (not space1) (not space2))) ; No spaces available
         ((not space2)                  ; Do space1
          (reftex-index-phrases-replace-space space1))
         ((not space1)                  ; Do space2
          (reftex-index-phrases-replace-space space2))
         (t ; We have both spaces
          (let ((l1 (- space1 bol))
                (l2 (- space2 space1))
                (l3 (- eol space2)))
            (if (> l2 fill-column)
                ;; The central part alone is more than one line
                (progn
                  (reftex-index-phrases-replace-space space1)
                  (reftex-index-phrases-replace-space space2))
              (if (> (+ l1 l2) fill-column)
                  ;; Need to split beginning
                  (reftex-index-phrases-replace-space space1))
              (if (> (+ l2 l3) fill-column)
                  ;; Need to split end
                  (reftex-index-phrases-replace-space space2))))))))))

(defun reftex-index-phrases-replace-space (pos)
  "If there is a space at POS, replace it with a newline char.
Does not do a save-excursion."
  (when (equal (char-after pos) ?\ )
    (goto-char pos)
    (delete-char 1)
    (insert "\n")))

(defun reftex-index-select-phrases-macro (&optional delay)
  "Offer a list of possible index macros and have the user select one."
  (let* ((prompt (concat "Select macro: ["
                         (mapconcat (lambda (x) (char-to-string (car x)))
                                    reftex-index-phrases-macro-data "")
                         "] "))
         (help (concat "Select an indexing macro\n========================\n"
                       (mapconcat (lambda (x)
                                    (format " [%c]     %s"
                                            (car x) (nth 1 x)))
                                  reftex-index-phrases-macro-data "\n"))))
    (reftex-select-with-char prompt help delay)))

(provide 'reftex-index)

;;; reftex-index.el ends here

;; Local Variables:
;; generated-autoload-file: "reftex-loaddefs.el"
;; End: