File: matlab-shell.el

package info (click to toggle)
matlab-mode 7.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,132 kB
  • sloc: lisp: 167,753; sh: 5; makefile: 5
file content (2511 lines) | stat: -rw-r--r-- 105,649 bytes parent folder | download
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
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
;;; matlab-shell.el --- Run MATLAB in an inferior process -*- lexical-binding: t -*-

;; Author: Eric Ludlam <zappo@gnu.org>

;; Copyright (C) 2019-2025 Free Software Foundation, Inc.
;;
;; This program 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.
;;
;; This program 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 this program.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:
;;
;; This library supports a MATLAB shell buffer, which runs MATLAB in
;; an inferior shell.  Supports working with the MATLAB command line,
;; and the MATLAB debugger.
;;

;;; Code:

(require 'subr-x)

(require 'matlab-compat)
(eval-and-compile
  (require 'matlab--access))
(require 'matlab--shell-bridge)
(require 'matlab--shared)

;; Note this should *NOT*
;;   (require 'matlab) ;; or (require 'matlab-mode)
;; or
;;   (require 'matlab-ts-mode)
;; because it is designed to work with both `matlab-mode' and `matlab-ts-mode'

(require 'comint)
(require 'server)

(eval-and-compile
  (require 'mlgud)
  (require 'shell))

;; Silence warnings from company.el
(declare-function company-mode "company")
(defvar company-idle-delay)
(defvar company-mode)

;; Key entry points for matlab-shell-gud
(declare-function matlab-shell-mode-gud-enable-bindings "matlab-shell-gud")
(declare-function matlab-shell-gud-startup "matlab-shell-gud")

;;; Customizations
;;
;; Options to configure using matlab-shell
(defgroup matlab-shell nil
  "MATLAB shell mode."
  :prefix "matlab-shell-"
  :group 'matlab)

;;
;; Shell Startup
;;
(defcustom matlab-shell-mode-hook nil
  "List of functions to call on entry to MATLAB shell mode."
  :type 'hook)

(defcustom matlab-shell-command-switches '("-nodesktop")
  "Command line parameters run with `matlab-shell-command'.
Command switches are a list of strings.  Each entry is one switch."
  :type '(choice (repeat :tag "Switches, one per entry" string)))

(defface matlab-shell-error-face
  (list
   (list t
         (list :background 'unspecified
               :foreground "red1"
               :bold t)))
  "Face to use when errors occur in MATLAB shell.")

(defcustom matlab-custom-startup-command nil
  "Custom MATLAB command to be run at startup."
  :type 'string)

(defcustom matlab-shell-echoes t
  "If `matlab-shell-command' echoes input."
  :type 'boolean)

(defcustom matlab-shell-history-file "~/.matlab/%s/history.m"
  "Location of the history file.
A %s is replaced with the MATLAB version release number, such as R12.
This file is read to initialize the comint input ring."
  :type 'file)

(defcustom matlab-shell-history-ignore "^%\\|%%$\\|emacs.set"
  "Regular expression matching items from history to ignore.
This expression should ignore comments (between sessions) and any command
that ends in 2 or more %%, added to automatic commands."
  :type 'regexp)

(defcustom matlab-shell-autostart-netshell nil
  "Use the netshell side-channel for communicating with MATLAB."
  :type 'boolean)

;;
;; Edit from MATLAB
(defcustom matlab-shell-emacsclient-command
  (matlab--find-emacsclient)
  "The command to use as an external editor for MATLAB.
Using emacsclient allows the currently running Emacs to also be the
external editor for MATLAB.  Setting this to the empty string
will disable use emacsclient as the external editor."
  :type 'integer)

;;
;; Run from Emacs
(defcustom matlab-shell-run-region-function 'auto
  "Technique to use for running a line, region, or code-section.
There are different benefits to different kinds of commands.
Use `auto to guess which to use by looking at the environment.
auto           - guess which to use
`matlab-shell-region->commandline'
               - Extract region, and generate 1 line of ML code.
`matlab-shell-region->script'
               - Extract region and any local fcns, and write to
                 tmp script.  Call that from MATLAB.
`matlab-shell-region->internal'
               - Send region location to MATLAB, and have ML
                 extract and run that region.  Customize
                 `matlab-shell-emacsrunregion' to specify what ML
                 function to use for this."
  :type '(choice (const :tag "Auto" auto)
                 (const :tag "Extract Line" matlab-shell-region->commandline)
                 (const :tag "Extract Script" matlab-shell-region->script)
                 (const :tag "Matlab Extract" matlab-shell-region->internal)))

(defcustom matlab-shell-internal-emacsrunregion "emacsrunregion"
  "The MATLAB command to use for running a region.
This command is used when `matlab-shell-run-region-function' is set
to auto, or `matlab-shell-region->internal'"
  :type 'string)

;;
;; Features in an active shell
(defcustom matlab-shell-input-ring-size 32
  "Number of history elements to keep."
  :type 'integer)

;;
;; Completion handling
(defcustom matlab-shell-ask-MATLAB-for-completions t
  "When Non-nil, ask MATLAB for a completion list.
When nil, complete against file names."
  :type 'boolean)

(defcustom matlab-shell-tab-use-company t
  "Use `company' (complete anything) for TAB completions in `matlab-shell'.
Only effective when  when `company' is installed.  Note, when you type to
narrow completions, you may find the responses slow and if so,
you can try turning this off."
  :type 'boolean)

(defcustom matlab-change-current-directory nil
  "*If non nil, make file's directory the current directory before evaluation.
When visiting *.m files, there's several functions that you can use to
evaluate MATLAB code.  When this is t, before evaluation the change the
current directory in `matlab-shell' to the file's directory."
  :type 'boolean)

(make-variable-buffer-local 'matlab-change-current-directory)

(defvar matlab-shell-tab-company-available (if (locate-library "company") t nil)
  "Non-nil if we have `company' installed.
Use this to override initial check.")

(defvar matlab-shell-errorscanning-syntax-table
  (let ((st (copy-syntax-table (matlab--shell-get-syntax-table))))
    ;; Make \n be whitespace when scanning output.
    (modify-syntax-entry ?\n  " " st)
    st)
  "Syntax table used when scanning MATLAB output.
In this case, comment and \n are not special, as word wrap can get in the way.")

(defvar matlab-shell-prompt-appears-hook nil
  "Hooks run each time a prompt is seen and sent to display.
If multiple prompts are seen together, only call this once.")

(defvar matlab-shell-prompt-hook-cookie nil
  "Cookie used to transfer info about detected prompts from inner filter to outer.")
(make-variable-buffer-local 'matlab-shell-prompt-hook-cookie)

(defvar matlab-shell-suppress-prompt-hooks nil
  "Non-nil to suppress running prompt hooks.")

(defvar matlab-shell-cco-testing nil
  "Non nil when testing `matlab-shell'.")

(defvar matlab-shell-io-testing nil
  "Non-nil to display process output and input log.")

;;; Font Lock
;;
;; Font lock keywords for the MATLAB shell.

(defconst matlab-shell-error-font-lock-keywords
  (list
   ;; How about Errors?
   '("^\\(Error in\\|Syntax error in\\)\\s-+==>\\s-+\\(.+\\)$"
     (1 font-lock-comment-face) (2 font-lock-string-face))
   ;; and line numbers
   '("^\\(\\(On \\)?line [0-9]+\\)" 1 font-lock-comment-face)
   ;; User beep things
   '("\\(\\?\\?\\?[^\n]+\\)" 1 font-lock-comment-face)
   )
  "The matlab-shell error keywords.")

(defconst matlab-shell-object-output-font-lock-keywords
  (list
   ;; Startup notices
   '(" M A T L A B " 0 'underline)
   '("All Rights Reserved" 0 'italic)
   '("\\(\\(?:(c)\\)?\\s-+Copyright[^\n]+\\)" 1 font-lock-comment-face)
   '("\\(Version\\)\\s-+\\([^\n]+\\)"
     (1 font-lock-function-name-face) (2 font-lock-variable-name-face))
   '("\\(R[0-9]+[ab]\\(?: Update [0-9]+\\)\\) \\([^\n]+\\)"
     (1 font-lock-function-name-face) (2 font-lock-variable-name-face))
   '("^To get started, type doc.$" 0 font-lock-comment-face prepend)
   '("For product information, [^\n]+" 0 font-lock-comment-face)

   ;; Useful user commands, but not useful programming constructs
   '("\\<\\(demo\\|whatsnew\\|info\\|subscribe\\|help\\|doc\\|lookfor\\|what\
\\|whos?\\|cd\\|clear\\|load\\|save\\|helpdesk\\|helpwin\\)\\>"
     1 font-lock-keyword-face)
   ;; disp of objects usually looks like this:
   '("^\\s-*\\(\\w+\\) with properties:" (1 font-lock-type-face))
   ;; object output - highlight property names after  'with properties:' indicator
   ;; NOTE: Normally a block like this would require us to use `font-lock-multiline' feature
   ;;       but since this is shell output, and not a thing you edit, we can skip it and rely
   ;;       on matlab-shell dumping the text as a unit.
   '("^\\s-*\\(\\w+ with properties:\\)\n\\s-*\n"
     ("^\\s-*\\(\\w+\\):[^\n]+$" ;; match the property before the :
      ;; Extend search region across lines.
      (save-excursion (re-search-forward "\n\\s-*\n" nil t)
                      (beginning-of-line)
                      (point))
      nil
      (1 font-lock-variable-name-face)))
   '("[[{]\\([0-9]+\\(?:x[0-9]+\\)+ \\w+\\)[]}]" (1 font-lock-comment-face))
   )
  "The matlab-shell output related keywords.")

(defconst matlab-shell-font-lock-keywords
  (append matlab-shell-error-font-lock-keywords
          matlab-shell-object-output-font-lock-keywords)
  "The matlab-shell keywords.")
    

;;; Keymaps & Menus
;;
(defvar matlab-shell-mode-map
  (let ((km (make-sparse-keymap 'matlab-shell-mode-map)))
    ;; Mostly use comint mode's map.
    (set-keymap-parent km comint-mode-map)

    ;; We can jump to errors, so take over this keybinding.
    ;; FIXME: Should we set `next-error-function' instead?
    ;;        https://github.com/mathworks/Emacs-MATLAB-Mode/issues/23
    (substitute-key-definition #'next-error #'matlab-shell-last-error
                               km global-map)

    ;; Interrupt
    (define-key km [(control c) (control c)] #'matlab-shell-interrupt-subjob)

    ;; Help system
    (define-key km [(control h) (control m)] matlab--shell-help-map)

    ;; Completion
    (define-key km (kbd "TAB") #'matlab-shell-tab)
    (define-key km (kbd "<C-tab>") #'matlab-shell-c-tab)

    ;; Command history
    (define-key km [(control up)] #'comint-previous-matching-input-from-input)
    (define-key km [(control down)] #'comint-next-matching-input-from-input)
    (define-key km [up] #'matlab-shell-previous-matching-input-from-input)
    (define-key km [down] #'matlab-shell-next-matching-input-from-input)

    ;; Editing
    (define-key km [(control return)] #'comint-kill-input)
    (define-key km [(backspace)] #'matlab-shell-delete-backwards-no-prompt)

    ;; Files
    (define-key km "\C-c." #'matlab-shell-locate-fcn)

    ;; matlab-shell actions
    (define-key km "\C-c/" #'matlab-shell-sync-buffer-directory)

    km)

  "Keymap used in `matlab-shell-mode'.")

(easy-menu-define matlab-shell-menu
  matlab-shell-mode-map
  "MATLAB shell menu."
  '("MATLAB"
    ["Goto last error" matlab-shell-last-error t]
    "----"
    ["Stop On Errors" matlab-shell-dbstop-error t]
    ["Don't Stop On Errors" matlab-shell-dbclear-error t]
    "----"
    ["Locate MATLAB function" matlab-shell-locate-fcn
     :help "Run 'which FCN' in matlab-shell, then open the file in Emacs"]
    ["Run Command" matlab-shell-run-command t]
    ["Describe Variable" matlab-shell-describe-variable t]
    ["Describe Command" matlab-shell-describe-command t]
    ["Lookfor Command" matlab-shell-apropos t]
    "----"
    ["Complete command" matlab-shell-tab t]
    "----"
    ["Demos" matlab-shell-demos t]
    ["Close Current Figure" matlab-shell-close-current-figure t]
    ["Close Figures" matlab-shell-close-figures t]
    "----"
    ["Sync buffer directory (emacscd)" matlab-shell-sync-buffer-directory
     :help "Sync the matlab-shell buffer `default-directory' with MATLAB's pwd.\n\
These will differ when MATLAB code changes directory without notifying Emacs."]
    ["Customize" (customize-group 'matlab-shell)
     (and (featurep 'custom) (fboundp 'custom-declare-variable))
     ]
    ["Exit" matlab-shell-exit t]))


;;; MODE
;;
;; The Emacs major mode for interacting with the matlab shell process.

(defvar-local matlab-shell-last-error-anchor nil
  "Last point where an error anchor was set.")

(defun matlab-shell-mode ()
  "Run MATLAB as a subprocess in an Emacs buffer.

This mode will allow standard Emacs shell commands/completion to occur
with MATLAB running as an inferior process.  Additionally, this shell
mode is integrated with `matlab-ts-mode' or the older `matlab-mode', a
major mode for editing *.m files.  See the MATLAB menu in these
buffers for the integration with matlab-shell-mode."
  (setq major-mode 'matlab-shell-mode
        mode-name "M-Shell"
        comint-prompt-regexp "^\\(K\\|EDU\\)?>> *"
        comint-delimiter-argument-list (list [ 59 ]) ; semi colon
        comint-dynamic-complete-functions '(comint-replace-by-expanded-history)
        comint-process-echoes matlab-shell-echoes
        comint-get-old-input #'matlab-comint-get-old-input
        )
  ;; Shell Setup
  (require 'shell)

  ;; COMINT History Setup
  (set (make-local-variable 'comint-input-ring-size)
       matlab-shell-input-ring-size)
  (set (make-local-variable 'comint-input-ring-file-name)
       (format matlab-shell-history-file "R12"))
  (if (fboundp 'comint-read-input-ring)
      (comint-read-input-ring t))

  (setq-local comment-start "%")

  (use-local-map matlab-shell-mode-map)
  (set-syntax-table (matlab--shell-get-syntax-table))

  (setq-local font-lock-defaults '((matlab-shell-font-lock-keywords)
                             t   ;; syntactic fontification (strings and comments) is not performed.
                             nil ;; keywords are case sensitive
                             ;; Put _ as a word constituent, simplifying keywords
                             ((?_ . "w"))))

  ;; GUD support
  (matlab-shell-mode-gud-enable-bindings)

  ;; Company mode can be used to display completions for MATLAB in matlab-shell.
  ;; This block enables company mode for this shell, and turns off the idle timer
  ;; so users must press TAB to get the menu.
  (when (and matlab-shell-tab-use-company
             matlab-shell-tab-company-available)
    ;; Only do popup when users presses TAB
    (set (make-local-variable 'company-idle-delay) nil)
    (company-mode))

  ;; Hooks
  (run-hooks 'matlab-shell-mode-hook))


;;; NETSHELL integration
;;
(declare-function matlab-netshell-client "matlab-netshell")
(declare-function matlab-netshell-server-start "matlab-netshell")
(declare-function matlab-netshell-server-active-p "matlab-netshell")
(declare-function matlab-netshell-eval "matlab-netshell")
(defun matlab-netshell-active-p ()
  "Return t if the MATLAB netshell is active."
  (when (featurep 'matlab-netshell)
    (matlab-netshell-client)))

(defun matlab-any-shell-active-p ()
  "Return non-nil of any of the matlab connections are active."
  (or (matlab-netshell-active-p) (matlab-shell-active-p)))


;;; MATLAB SHELL
;;
;; Core shell state handling & startup function.

(defvar matlab-shell-buffer-name "MATLAB"
  "Name used to create `matlab-shell' mode buffers.
This name will have *'s surrounding it.")

(defvar matlab-prompt-seen nil
  "Track visibility of MATLAB prompt in MATLAB Shell.")

(defun matlab-shell-active-p ()
  "Return the MATLAB shell buffer if it active, else nil."
  (let ((msbn (get-buffer (concat "*" matlab-shell-buffer-name "*"))))
    (if msbn
        (with-current-buffer msbn
          (if (comint-check-proc (current-buffer))
              (current-buffer))))))

;;;###autoload
(defun matlab-shell ()
  "Create a buffer with MATLAB running as a subprocess.

MATLAB shell cannot work on the MS Windows platform because MATLAB is not
a console application."
  (interactive)
  ;; MATLAB shell does not work by default on the Windows platform.  Only
  ;; permit it's operation when the shell command string is different from
  ;; the default value.  (True when the engine program is running.)
  (when (and (or (eq window-system 'pc) (eq window-system 'w32))
             (string= matlab-shell-command "matlab"))
    (error "MATLAB cannot be run as a inferior process.  \
Try C-h f matlab-shell RET"))

  (require 'shell)
  (require 'matlab-shell-gud)

  ;; Make sure netshell is started if it is wanted.
  (when (and matlab-shell-autostart-netshell
             (not (matlab-netshell-server-active-p)))
    (matlab-netshell-server-start))

  ;; Show the shell buffer
  (switch-to-buffer (concat "*" matlab-shell-buffer-name "*"))

  ;; If the shell isn't active yet, start it.
  (unless (matlab-shell-active-p)

    ;; Clean up crufty state
    (kill-all-local-variables)

    ;; Thx David Chappaz for reminding me about this patch.
    (let* ((windowid (frame-parameter (selected-frame) 'outer-window-id))
           (newvar (concat "WINDOWID=" windowid))
           (process-environment (cons newvar process-environment))
           (abs-matlab-exe (matlab--get-abs-matlab-exe))
           (matlab-exe (if (file-remote-p abs-matlab-exe)
                           matlab-shell-command
                         abs-matlab-exe)))
      (message "Running: %s" abs-matlab-exe)
      (apply #'make-comint matlab-shell-buffer-name matlab-exe
             nil matlab-shell-command-switches))

    ;; Enable GUD
    (matlab-shell-gud-startup)

    ;; Init our filter and sentinel
    (set-process-filter (get-buffer-process (current-buffer))
                        #'matlab-shell-wrapper-filter)
    (set-process-sentinel (get-buffer-process (current-buffer))
                          #'matlab-shell-wrapper-sentinel)

    ;; XEmacs has problems w/ this variable.  Set it here.
    (set-marker comint-last-output-start (point-max))

    (make-local-variable 'matlab-prompt-seen)
    (setq matlab-prompt-seen nil)

    ;; FILTERS
    ;;

    ;; Add hook for finding the very first prompt - so we know when the buffer is ready to use.
    (add-hook 'matlab-shell-prompt-appears-hook #'matlab-shell-first-prompt-fcn)

    ;; Track current directories when user types cd
    (add-hook 'comint-input-filter-functions #'shell-directory-tracker nil t) ;; patch Eli Merriam

    ;; Add a version scraping logo identification filter.
    (add-hook 'comint-output-filter-functions #'matlab-shell-version-scrape nil t)

    ;; Add pseudo html-renderer
    (add-hook 'comint-output-filter-functions #'matlab-shell-render-html-anchor nil t)
    ;; Scroll to bottom after running code-section/region
    (add-hook 'comint-output-filter-functions #'comint-postoutput-scroll-to-bottom nil t)

    ;; Add error renderer to prompt hook so the prompt is available for resolving names.
    (add-hook 'matlab-shell-prompt-appears-hook #'matlab-shell-render-errors-as-anchor nil t)
    (add-hook 'matlab-shell-prompt-appears-hook #'matlab-shell-colorize-errors nil t)

    ;; Comint and GUD both try to set the mode.  Now reset it to
    ;; matlab mode.
    (matlab-shell-mode))
  )

;;; PROCESS FILTERS & SENTINEL
;;
;; These are wrappers around the GUD filters so we can pre and post process
;; decisions by comint and mlgud.
(defvar matlab-shell-capturetext-start-text "<EMACSCAP>"
  "Text used as simple signal for text that should be captured.")
(defvar matlab-shell-capturetext-end-text "</EMACSCAP>"
  "Text used as simple signal for text that should be captured.")

(defvar matlab-shell-accumulator ""
  "Accumulate text that is being captured.")
(make-variable-buffer-local 'matlab-shell-accumulator)
(defvar matlab-shell-flush-accumulation-buffer nil
  "When non-nil, flush the accumulation buffer.")

(defvar matlab-shell-in-process-filter nil
  "Non-nil when inside `matlab-shell-wrapper-filter'.")

(defun matlab-shell-wrapper-filter (proc string)
  "MATLAB Shell's process filter.  This wraps the GUD and COMINT filters.
PROC is the process with input to this filter.
STRING is the recent output from PROC to be filtered."
  ;; A few words about process sentinel's in the MATLAB shell buffer:
  ;; Our filter calls the GUD filter.
  ;; The GUD filter calls the COMINT filter.
  ;; The COMINT filter writes output to the buffer and runs filters.

  ;; We need to run our error anchor commands AFTER all of the above is done,
  ;; but ONLY when we have an empty prompt and can ask MATLAB more questions.
  ;; We need this filter to provide a hook on prompt display when everything
  ;; has been processed.

  (let ((buff (process-buffer proc))
        (captext nil)
        (matlab-shell-in-process-filter t))

    ;; Cleanup garbage before sending it along to the other filters.
    (let ((garbage (concat "\\(" (regexp-quote "\C-g") "\\|"
                           (regexp-quote "\033[H0") "\\|"
                           (regexp-quote "\033[H\033[2J") "\\|"
                           (regexp-quote "\033H\033[2J") "\\)")))
      (while (string-match garbage string)
        ;;(if (= (aref string (match-beginning 0)) ?\C-g)
        ;;(beep t))
        (setq string (replace-match "" t t string))))

    ;; Engage the accumulator
    (setq matlab-shell-accumulator (concat matlab-shell-accumulator string)
          string "")

    ;; STARTCAP - push preceeding text to output.
    (if (and (not matlab-shell-flush-accumulation-buffer)
             (string-match (regexp-quote matlab-shell-capturetext-start-text) matlab-shell-accumulator))
        (progn
          (setq string (substring matlab-shell-accumulator 0 (match-beginning 0))
                matlab-shell-accumulator (substring matlab-shell-accumulator
                                                    (match-beginning 0)))

          ;; START and ENDCAP - save captured text, and push trailing text to output
          (when (string-match (concat (regexp-quote matlab-shell-capturetext-end-text)
                                      "\\(:?\n\\)?")
                              matlab-shell-accumulator)
            ;; If no end, then send anything before the CAP, and accumulate everything
            ;; else.
            (setq string (concat string (substring matlab-shell-accumulator (match-end 0)))
                  captext (substring matlab-shell-accumulator
                                     0 (match-end 0))
                  matlab-shell-accumulator "")))

      ;; No start capture, or an ended capture, everything goes back to String
      (setq string (concat string matlab-shell-accumulator)
            matlab-shell-accumulator ""
            matlab-shell-flush-accumulation-buffer nil))

    (with-current-buffer buff
      (mlgud-filter proc string))

    ;; In case things get switched around on us
    (with-current-buffer buff
      (when matlab-shell-prompt-hook-cookie
        (setq matlab-shell-prompt-hook-cookie nil)
        (run-hooks 'matlab-shell-prompt-appears-hook))
      )

    ;; If there was some captext, process it, but only after doing all the other important
    ;; stuff.
    (when captext
      (matlab-shell-process-capture-text captext))
    ))

(defun matlab-shell-wrapper-sentinel (proc string)
  "MATLAB Shell's process sentinel.  This wraps the GUD and COMINT filters.
PROC is the function which experienced a change in state.
STRING is a description of what happened."
  (let ((buff (process-buffer proc)))
    (with-current-buffer buff
      (mlgud-sentinel proc string))))

;;; COMINT support fcns
;;
(defun matlab-comint-get-old-input ()
  "Compute text from the current line to evaluate with MATLAB.
This function checks to make sure the line is on a prompt.  If not,
it returns empty string"
  (let ((inhibit-field-text-motion t))
    (save-excursion
      (beginning-of-line)
      (save-match-data
        (if (looking-at comint-prompt-regexp)
            ;; We'll send this line.
            (buffer-substring-no-properties (match-end 0) (line-end-position))
          ;; Otherwise, it's probably junk that is useless.  Don't do it.
          "")))))


;;; STARTUP / VERSION
;;
;; Handlers for startup output / version scraping
;;
;; TODO - these scraped values aren't used anywhere.  Do we care?

(defvar matlab-shell-running-matlab-version nil
  "The version of MATLAB running in the current `matlab-shell' buffer.")
(defvar matlab-shell-running-matlab-release nil
  "The release of MATLAB running in the current `matlab-shell' buffer.")

(defun matlab-shell-version-scrape (str)
  "Scrape the MATLAB Version from the MATLAB startup text.
Argument STR is the string to examine for version information."
  (if (string-match "\\(Version\\)\\s-+\\([.0-9]+\\)\\s-+(\\(R[.0-9]+[ab]?\\))" str)
      ;; OLDER MATLAB'S
      (setq matlab-shell-running-matlab-version
            (match-string 2 str)
            matlab-shell-running-matlab-release
            (match-string 3 str))
    ;; NEWER MATLAB'S
    (if (string-match "\\(R[0-9]+[ab]\\)\\s-+\\(?:Update\\s-+[0-9]+\\s-+\\|Prerelease\\s-+\\)?(\\([0-9]+\\.[0-9]+\\)\\." str)
        (setq matlab-shell-running-matlab-version
              (match-string 2 str)
              matlab-shell-running-matlab-release
              (match-string 1 str))))

  ;; Notice that this worked.
  (when matlab-shell-running-matlab-version
    ;; Remove the scrape from our list of things to do.  We are done getting the version.
    (remove-hook 'comint-output-filter-functions
                 #'matlab-shell-version-scrape t)

    (message "Detected MATLAB %s (%s)  -- Loading history file" matlab-shell-running-matlab-release
             matlab-shell-running-matlab-version)

    ;; Now get our history loaded
    (setq comint-input-ring-file-name
          (format matlab-shell-history-file matlab-shell-running-matlab-release)
          comint-input-history-ignore matlab-shell-history-ignore)

    (if (fboundp 'comint-read-input-ring)
        (comint-read-input-ring t))
    ))

;;; ANCHORS
;;
;; Scan output for text, and turn into navigable links.

(defvar gud-matlab-marker-regexp-prefix "error:\\|opentoline\\|dbhot"
  "A prefix to scan for to know if output might be scarfed later.")

(defvar matlab-shell-html-map
  (let ((km (make-sparse-keymap)))
    (if (featurep 'xemacs)
        (define-key km [button2] #'matlab-shell-html-click)
      (define-key km [mouse-2] #'matlab-shell-html-click)
      (define-key km [mouse-1] #'matlab-shell-html-click))
    (define-key km (kbd "RET") #'matlab-shell-html-go)
    km)
  "Keymap used on overlays that represent errors.")

;; Anchor expressions.
(defvar matlab-anchor-beg "<a href=\"\\(\\(?:matlab:\\)?[^\"]+\\)\">"
  "Beginning of html anchor.")

(defvar matlab-anchor-end "</a>"
  "End of html anchor.")

(defun matlab-shell-render-html-anchor (str)
  "Render html anchors inserted into the MATLAB shell buffer.
Argument STR is the text for the anchor."
  (when (string-match matlab-anchor-end str)
    (save-excursion
      (with-syntax-table matlab-shell-errorscanning-syntax-table
        (while (re-search-backward matlab-anchor-beg
                                   ;; Arbitrary back-buffer.  We don't
                                   ;; usually get text in such huge chunks
                                   (max (point-min) (- (point-max) 8192))
                                   t)
          (let* ((anchor-beg-start (match-beginning 0))
                 (anchor-beg-finish (match-end 0))
                 (anchor-text (match-string 1))
                 (anchor-end-finish (search-forward matlab-anchor-end))
                 (anchor-end-start (match-beginning 0))
                 (o (make-overlay anchor-beg-finish anchor-end-start)))
            (overlay-put o 'mouse-face 'highlight)
            (overlay-put o 'face 'underline)
            (overlay-put o 'matlab-url anchor-text)
            (overlay-put o 'keymap matlab-shell-html-map)
            (overlay-put o 'help-echo anchor-text)
            (delete-region anchor-end-start anchor-end-finish)
            (delete-region anchor-beg-start anchor-beg-finish)
            ))))))

;;; ERROR HANDLING
;;

;; The regular expression covers to forms in tests/erroexamples.shell.m
;;
(defvar matlab-shell-error-anchor-expression
  (concat "^>?\\s-*\\(\\(Error \\(in\\|using\\)\\s-+\\|Syntax error in \\)\\(?:==> \\)?\\|"
          "In\\s-+\\(?:workspace belonging to\\s-+\\)?\\|Error:\\s-+File:\\s-+\\|Warning:\\s-+[^\n]+\n\\)")

  "Expressions used to find errors in MATLAB process output.
This variable contains the anchor, or starting text before
a typical error.  See `matlab-shell-error-location-expression' for
a list of expressions for identifying where the error is
after this anchor.")

(defvar matlab-shell-error-location-expression
  (list
   ;; Pulled from R2019b
   "\\(?:^> In\\s-+\\)?\\([-+>@.a-zA-Z_0-9/ \\\\:]+\\)\\s-+(line \\([0-9]+\\))"

   "\\([-+>@.a-zA-Z_0-9/ \\\\:]+\\)\\s-+Line:\\s-+\\([0-9]+\\)\\s-+Column:\\s-+\\([0-9]+\\)"

   ;; Oldest I have examples for:
   (concat "\\([-+>@.a-zA-Z_0-9/ \\\\:]+\\)\\(?:>[^ ]+\\)?.*[\n ]"
           "\\(?:On\\|at\\)\\(?: line\\)? \\([0-9]+\\) ?")
   )
  "List of Expressions to search for after an error anchor is found.
These expressions are listed as matching from newer MATLAB versions
to older MATLAB's.
Each expression should have the following match strings:
  1 - The matlab function
  2 - The line number
  3 - The column number (if available)")

;; (global-set-key [f7] 'matlab-shell-scan-for-error-test)
(defun matlab-shell-scan-for-error-test ()
  "Interactively try out the error scanning feature."
  (interactive)
  (let ((ans (matlab-shell-scan-for-error (point-min))))
    (when ans
      (pulse-momentary-highlight-region (car ans) (car (cdr ans))))
    (message "Found: %S" ans)))


(defun matlab-shell-scan-for-error (limit)
  "Scan backward for a MATLAB error in the current buffer until LIMIT.
Uses `matlab-shell-error-anchor-expression' to find the error.
Uses `matlab-shell-error-location-expression' to find where the error is.
Returns a list of the form:
  ( STARTPT ENDPT FILE LINE COLUMN )"
  (with-syntax-table matlab-shell-errorscanning-syntax-table
    (let ((ans nil)
          (beginning nil))
      (when (re-search-backward matlab-shell-error-anchor-expression
                                limit
                                t)
        (save-excursion
          (setq beginning (save-excursion (goto-char (match-beginning 0))
                                          (back-to-indentation)
                                          (point)))
          (goto-char (match-end 0))
          (dolist (EXP matlab-shell-error-location-expression)
            (when (looking-at EXP)
              (setq ans (list beginning
                              (match-end 0)
                              (match-string-no-properties 1)
                              (match-string-no-properties 2)
                              (match-string-no-properties 3)
                              )))))
        )
      ans)))

(defvar matlab-shell-last-anchor-as-frame nil
  ;; NOTE: this isn't being used yet.
  "The last error anchor saved, represented as a debugger frame.")

(defun matlab-shell-render-errors-as-anchor (&optional str)
  "Hook function run when process filter sees a prompt.
Detect non-url errors, and treat them as if they were url anchors.
Input STR is provided by comint but is unused."
  (ignore str)
  (save-excursion
    ;; Move to end to make sure we are scanning the new stuff.
    (goto-char (point-max))
    ;; We have found an error stack to investigate.
    (let ((first nil)
          (ans nil)
          (overlaystack nil)
          (starting-anchor matlab-shell-last-error-anchor)
          (newest-anchor matlab-shell-last-error-anchor)
          )
      (while (setq ans (matlab-shell-scan-for-error
                        (or starting-anchor (point-min))))
        (let* ((err-start (nth 0 ans))
               (err-end (nth 1 ans))
               (err-file (string-trim (nth 2 ans)))
               (err-line (nth 3 ans))
               ;; note (nth 4 ans) is err-col
               (o (make-overlay err-start err-end))
               (err-mref-deref (matlab-shell-mref-to-filename err-file))
               (err-full-file (when err-mref-deref (expand-file-name err-mref-deref)))
               (url (concat "opentoline('" (or err-full-file err-file) "'," err-line ",0)"))
               )
          ;; Setup the overlay with the URL.
          (overlay-put o 'mouse-face 'highlight)
          (overlay-put o 'face 'underline)
          ;; The url will recycle opentoline code.
          (overlay-put o 'matlab-url url)
          (overlay-put o 'matlab-fullfile err-full-file)
          (overlay-put o 'keymap matlab-shell-html-map)
          (overlay-put o 'help-echo (concat "Jump to error at " (or err-full-file err-file) "."))
          (setq first url)
          (push o overlaystack)
          ;; Save as a frame
          (setq matlab-shell-last-anchor-as-frame
                (cons err-file err-line))
          (setq newest-anchor (max (or newest-anchor (point-min)) err-end))
          ))
      ;; Keep track of the very first error in this error stack.
      ;; It will represent the "place to go" for "go-to-last-error".
      (dolist (O overlaystack)
        (overlay-put O 'first-in-error-stack first))

      ;; Once we've found something, don't scan it again.
      (when overlaystack
        (setq matlab-shell-last-error-anchor (copy-marker newest-anchor))))))

(defvar matlab-shell-errortext-start-text "<ERRORTXT>\n"
  "Text used as a signal for errors.")
(defvar matlab-shell-errortext-end-text "</ERRORTXT>"
  "Text used as a signal for errors.")

(defun matlab-shell-colorize-errors (&optional str)
  "Hook function run to colorize MATLAB errors.
The filter replaces indicators with <ERRORTXT> text </ERRORTXT>.
This strips out that text, and colorizes the region red.
STR is provided by COMINT but is unused."
  (ignore str)
  (save-excursion
    (let ((start nil) (end nil)
          )
      (goto-char (point-max))

      (while (re-search-backward (regexp-quote matlab-shell-errortext-end-text) nil t)
        ;; Start w/ end text to make sure everything is in the buffer already.

        ;; Then scan for the beginning, and start there.  As we delete text, locations will move,
        ;; so move downward after this.
        (if (not (re-search-backward (regexp-quote matlab-shell-errortext-start-text) nil t))
            (error "Mismatched error text tokens from MATLAB")

          ;; Save off where we start, and delete the indicator.
          (setq start (match-beginning 0))
          (delete-region start (match-end 0))

          ;; Find the end.
          (if (not (re-search-forward (regexp-quote matlab-shell-errortext-end-text) nil t))
              (error "Internal error scanning for error text tokens")

            (setq end (match-beginning 0))
            (delete-region end (match-end 0))

            ;; Now colorize the text.  Use overlay because font-lock messes with font properties.
            (let ((o (make-overlay start end (current-buffer) nil nil))
                  )
              (overlay-put o 'shellerror t)
              (overlay-put o 'face 'matlab-shell-error-face)

              )))

        ;; Setup for next loop
        (goto-char (point-max))))))

;;; Shell Startup

(defun matlab-shell--get-emacsclient-command ()
  "Compute how to call emacsclient so MATLAB will connect to this Emacs.
Handles case of multiple Emacsen from different users running on the same
system."
  (unless (server-running-p)
    ;; We need an Emacs server for ">> edit foo.m" which leverages to
    ;; emacsclient to open the file in the current Emacs session. Be
    ;; safe and start a server with a unique name. This ensures that
    ;; we don't have multiple emacs sessions stealing the server from
    ;; each other.
    (setq server-name (format "server-%d" (emacs-pid)))
    (message "matlab-shell: starting server with name %s" server-name)
    (server-start)
    (unless (server-running-p)
      (user-error "Unable to start server with name %s" server-name)))
  (let ((iq (if (eq system-type 'windows-nt)
                ;; Probably on Windows, probably in "Program Files" -
                ;; we need to quote this thing.
                ;; SADLY - emacs Edit command also wraps the command in
                ;; quotes - but we have to include arguments - so we need
                ;; to add internal quotes so the quotes land in the right place
                ;; when MATLAB adds external quotes.
                "\"" "")))
    (concat
     matlab-shell-emacsclient-command
     iq " -n"
     (if server-use-tcp
         (concat " -f " iq (expand-file-name server-name server-auth-dir))
       (concat " -s " iq (expand-file-name server-name server-socket-dir))))))

(defvar matlab-shell-use-emacs-toolbox
  ;; matlab may not be on path.  (Name change, explicit load, etc)
  (let* ((mlfile (locate-library "matlab"))
         (dir (expand-file-name "toolbox/emacsinit.m"
                                (file-name-directory (or mlfile "")))))
    (and mlfile (file-exists-p dir)))
  "Add the `matlab-shell' MATLAB toolbox to the MATLAB path on startup.")

(defun matlab--shell-toolbox-and-bin-sha1 (matlab-dir &optional recursive-call)
  "Compute the SHA1 of the Emacs MATLAB-DIR toolbox and bin directories.
RECURSIVE-CALL should be nil when called from top-level."
  (let (sha1-all)
    (when (not (directory-name-p matlab-dir))
      (error "Directory, %s, does not end in a /" matlab-dir))
    (when (not (file-directory-p matlab-dir))
      (error "Directory, %s, does not exist" matlab-dir))
    (setq matlab-dir (file-truename matlab-dir))
    (let ((dirs (if recursive-call
                    (list matlab-dir)
                 (list (concat matlab-dir "toolbox/")
                       (concat matlab-dir "bin/")))))
      (dolist (dir dirs)
        (when (not (file-directory-p dir))
          (error "Directory, %s, does not exist" dir))
        (dolist (file-name (sort (directory-files dir) #'string<))
          (when (and (not (string-match "~$" file-name))
                     (not (string-match "^\\(?:#\\|\\.\\)" file-name)))
            ;; Not a: backup~, #backup, ".", ".., or .hidden file.
            (let ((abs-file (concat dir file-name)))
              (if (file-directory-p abs-file)
                  (setq sha1-all
                        (concat sha1-all
                                (matlab--shell-toolbox-and-bin-sha1 (concat abs-file "/") t)))
                ;; Plain file to add to sha1-all.
                (with-temp-buffer
                  (insert-file-contents-literally abs-file)
                  (setq sha1-all (concat sha1-all (secure-hash 'sha1 (current-buffer)))))))))))
    (when (not recursive-call)
      ;; sha1-all contains a long list of the individual hash's, reduce to one hash.
      (when (not sha1-all)
        (error "Directory, %s, contains no plain files" matlab-dir))
      (setq sha1-all (secure-hash 'sha1 sha1-all)))
    sha1-all))

(defun matlab--shell-remote-toolbox-dir (local-toolbox-dir)
  "Return matlab-emacs toolbox directory path on the remote system.
This will be a copy the LOCAL-TOOLBOX-DIR toolbox and ../bin directories
to the remote system.  This will copy files to the remote system if the
remote directory is missing or out of date.  Returns:
~/.emacs-matlab-mode/toolbox/"
  (let* ((matlab-dir (file-name-as-directory
                      (file-name-directory (directory-file-name local-toolbox-dir))))
         (sha1 (matlab--shell-toolbox-and-bin-sha1 matlab-dir))
         (local-dir-on-remote "~/.emacs-matlab-mode/")
         (remote (if (file-remote-p default-directory) (file-remote-p default-directory)
                   (error "%s is not remote" default-directory)))
         (remote-dir (concat remote local-dir-on-remote))
         (remote-sha1-file (concat remote-dir ".sha1.txt")))
    (when (or (not (file-exists-p remote-sha1-file))
              (not (string= sha1 (with-temp-buffer
                                   (insert-file-contents-literally remote-sha1-file)
                                   (buffer-substring (point-min) (point-max))))))
      (delete-directory remote-dir t)
      (copy-directory local-toolbox-dir remote-dir t t)
      (copy-directory (concat local-toolbox-dir "../bin/") remote-dir t t)
      ;; Save SHA1. This is used to avoid future copies when remote is up to date.
      (write-region sha1 nil remote-sha1-file))
    ;; result
    (concat local-dir-on-remote "toolbox/")))

(cl-defun matlab-shell-first-prompt-fcn ()
  "Hook run when the first prompt is seen.
Sends commands to the MATLAB shell to initialize the MATLAB process."
  ;; Don't do this again
  (remove-hook 'matlab-shell-prompt-appears-hook #'matlab-shell-first-prompt-fcn)

  (when (not matlab-shell-use-emacs-toolbox)
    ;; Setup is misconfigured - we need emacsinit because it tells us how to debug
    (error "Unable to initialize matlab, emacsinit.m and other files missing"))

  ;; Run emacsinit.m which sets up the MATLAB environment to include the matlab-mode
  ;; "toolbox".  This is used for items like debugging, e.g. ebstop.m.
  ;; Also setup emacsclient such that ">> edit file" works.
  (let* ((local-toolbox-dir (expand-file-name "toolbox/"
                                              (file-name-directory (locate-library "matlab"))))
         (toolbox-dir (if (file-remote-p default-directory)
                          ;; Case: Remote matlab-shell via tramp
                          (matlab--shell-remote-toolbox-dir local-toolbox-dir)
                        local-toolbox-dir))
         (emacs-init (concat toolbox-dir "emacsinit"))
         (e-client-command (matlab-shell--get-emacsclient-command))
         (remote-location (file-remote-p default-directory))
         (e-set-args (replace-regexp-in-string
                      "^, " "" ;; strip leading ", "
                      (concat (when matlab-shell-autostart-netshell ", 'netshell', true")
                              (when e-client-command (format ", 'clientcmd', '%s'"
                                                             e-client-command))
                              (when remote-location (format ", 'remoteLocation', '%s'"
                                                            remote-location)))))
         (cmd (format "run('%s');%s" emacs-init (if e-set-args
                                                    (format " emacs.set(%s);" e-set-args)
                                                  ""))))
    (matlab-shell-send-command (string-replace (expand-file-name "~/") "~/" cmd)))

  ;; Init any user commands
  (if matlab-custom-startup-command
      ;; Wait for next prompt, then send.
      (add-hook 'matlab-shell-prompt-appears-hook #'matlab-shell-user-startup-fcn)

    ;; No user startup command?  Wait for final prompt to signal we are done.
    (add-hook 'matlab-shell-prompt-appears-hook #'matlab-shell-second-prompt-fcn)
    ))

(defun matlab-shell-user-startup-fcn ()
  "Hook run on second prompt to run user specified startup functions."
  ;; Remove ourselves
  (remove-hook 'matlab-shell-prompt-appears-hook #'matlab-shell-user-startup-fcn)

  ;; Run user's startup
  (matlab-shell-send-command (concat matlab-custom-startup-command ""))

  ;; Wait for the next prompt to appear and finally set that we are ready.
  (add-hook 'matlab-shell-prompt-appears-hook #'matlab-shell-second-prompt-fcn)
  )

(defun matlab-shell-second-prompt-fcn ()
  "Hook to run when the first prompt AFTER the call to emacsinit."
  (remove-hook 'matlab-shell-prompt-appears-hook #'matlab-shell-second-prompt-fcn)
  (setq matlab-prompt-seen t))

;;; OUTPUT Capture
;;
(declare-function matlab-shell-help-mode "matlab-topic")

(defun matlab-shell-process-capture-text (str)
  "Process STR text found between <EMACSCAP> and </EMAACSCAP>.
Text is found in `matlab-shell-wrapper-filter', and then this
function is called before removing text from the output stream.
This function detects the type of output (an eval, or output to buffer)
and then processes it."
  (let ((buffname "*MATLAB Output*")
        (text nil)
        (showbuff nil))
    (save-match-data
      ;; Strip start anchor.
      (unless (string-match (regexp-quote matlab-shell-capturetext-start-text) str)
        (error "Capture text failed to provide start token.  [%s]" str))
      (setq text (substring str (match-end 0)))
      ;; Strip and ID the directive (eval or buffer name)
      (when (and (string-match "[ ]*(\\([^)\n]+\\))" text)
                 (= (match-beginning 0) 0))
        (setq buffname (match-string 1 text))
        (setq text (substring text (match-end 0)))
        )
      ;; Strip the tail.
      (if (string-match (regexp-quote matlab-shell-capturetext-end-text) text)
          (setq text (substring text 0 (match-beginning 0)))
        (error "Capture text failed to provide needed end token.  [%s]" text))

      ;; Act on the content
      (if (string= buffname "eval")
          ;; The desire is to evaluate some Emacs Lisp code instead of
          ;; capture output to display in Emacs.
          (let ((evalforms (read text)))
            ;; Evaluate some forms
            (condition-case nil
                (eval evalforms t)
              (error (message "Failed to evaluate forms from MATLAB: \"%S\"" evalforms))))

        ;; Generate the buffer and contents
        (with-current-buffer (get-buffer-create buffname)

          (setq buffer-read-only nil)
          ;; Clear it if not appending.
          (erase-buffer)
          (insert text)
          (goto-char (point-min))
          (setq showbuff (current-buffer))
          )

        ;; Display the buffer
        (cond
         ((string-match "^\\*MATLAB Help" buffname)
          (with-current-buffer showbuff
            (matlab-shell-help-mode)))
         (t
          (with-current-buffer showbuff
            (view-mode))))

        (display-buffer showbuff
                        '((display-buffer-use-some-window
                           display-buffer-below-selected
                           display-buffer-at-bottom)
                          (inhibit-same-window . t)
                          (window-height . shrink-window-if-larger-than-buffer))))
      )))

;;; COMMANDS
;;
;; Commands for interacting with the MATLAB shell buffer

(defun matlab-shell-interrupt-subjob ()
  "Call `comint-interrupt-subjob' and flush accumulation buffer."
  (interactive)
  ;; Look at the accumulation buffer, and flush it.
  (setq matlab-shell-flush-accumulation-buffer t)

  ;; Continue on to do what comint does.
  (comint-interrupt-subjob)
  )

(defun matlab-shell-next-matching-input-from-input (n)
  "Get the Nth next matching input from for the command line."
  (interactive "p")
  (matlab-shell-previous-matching-input-from-input (- n)))

(defun matlab-shell-previous-matching-input-from-input (n)
  "Get the Nth previous matching input from for the command line."
  (interactive "p")
  (end-of-line) ;; patch: Mark Histed
  (if (comint-after-pmark-p)
      (if (memq last-command '(matlab-shell-previous-matching-input-from-input
                               matlab-shell-next-matching-input-from-input))
          ;; This hack keeps the cycling working well.
          (let ((last-command 'comint-previous-matching-input-from-input))
            (comint-next-matching-input-from-input (- n)))
        ;; first time.
        (comint-next-matching-input-from-input (- n)))

    ;; If somewhere else, just move around.
    (forward-line (- n))))

(defun matlab-shell-delete-backwards-no-prompt (&optional arg)
  "Delete one char backwards without destroying the matlab prompt.
Optional argument ARG describes the number of chars to delete."
  (interactive "P")
  (let ((promptend (save-excursion
                     (beginning-of-line)
                     (if (looking-at "K?>> ")
                         (match-end 0)
                       (point))))
        (numchars (if (integerp arg) (- arg) -1)))
    (if (<= promptend (+ (point) numchars))
        (delete-char numchars)
      (error "Beginning of line"))))


;;; COMPLETION
;;
;; Request list of completions from MATLAB.
;; Support classic emacs in-place completion, or company mode if available.

(defun matlab-shell-completion-list (str)
  "Get a list of completions from MATLAB.
STR is a command substring to complete."
  (let* ((msbn (matlab-shell-buffer-barf-not-running))
         (cmd (concat "emacsdocomplete('" str "')"))
         (comint-scroll-show-maximum-output nil)
         output
         (replacement-text "")
         (cmd-text-to-replace "")
         (completions nil))
    (with-current-buffer msbn
      (unless (matlab-on-prompt-p)
        (user-error "MATLAB shell must be non-busy to do that"))

      (setq output (matlab-shell-collect-command-output cmd))

      (cond
       ;; Case: R2025a and later
       ((string-match "^\s*Completions-Lisp:[ \t\n\r]*\\('(\\(?:.\\|\n\\)+)\\)[ \t\n\r]*$"
                      output)
        ;; Completions that can be provided to `display-completion-list'
        (let ((completions-str (match-string 1 output)))
          (setq completions (eval (car (read-from-string completions-str)) t))))


       ;; Case: R2024b or have "CMD -complete ARGS" results
       ((string-match "^\s*emacs_completions_output =" output)
        (setq output (substring output (match-end 0)))

        (when (string-match "^'\\([^']+\\)' --> '\\([^']*\\)'" output)
          ;; "CMD -complete ARGS" results:
          ;;    STR is of form "CMD ARGS" where CMD is a *.m file and it contains the string
          ;;    "SUPPORTS_DASH_COMPLETE", in this case emacsdocomplete will run "CMD -complete ARGS"
          ;;    providing replacements.
          ;;
          ;; 'CMD_TEXT_TO_REPLACE' --> 'REPLACEMENT_TEXT'
          ;;     'OPTION1'
          ;;     'OPTION2'
          ;;     ...
          ;; Note, the CMD_TEXT_TO_REPLACE line is only present when there needs
          ;; to be replacement, e.g. imagine a command that takes glob patterns
          ;;   >> mycmd foo*ba<TAB>
          ;;   'foo*-bar' --> 'foo-and-bar'
          ;;     '-or-goo'
          ;;     '-or-too'
          ;; which completes to either 'foo-and-bar-or-goo' OR 'foo-and-bar-or-too'.
          ;; If there is only one completion that needs replacement, don't have options:
          ;;   >> mycmd foo*ba*-too<TAB>
          ;;   'foo*ba*-too' --> 'foo-and-bar-or-too'
          ;; The replacement line is not present when the completion just appends to the
          ;; command str, e.g.
          ;;   >> mycmd foo-and-bar<TAB>
          ;;       '-or-goo'
          ;;       '-or-too'
          (setq cmd-text-to-replace (match-string 1 output))
          (setq replacement-text (match-string 2 output))
          ;; Strip the 'CMD_TEXT_TO_REPLACE' --> 'REPLACEMENT_TEXT' from output
          (setq output (substring output (match-end 0))))

        ;; Parse the output string of form:
        ;;   emacs_completions_output =
        ;;     java.lang.String[]:
        ;;        'item1'
        ;;        'item2'
        ;;        ...
        ;;        'itemN'
        (while (string-match "'" output)
          ;; Remove test before the starting quote
          (setq output (substring output (match-end 0)))
          (string-match "'" output)
          ;; we are making a completion list, so that is a list of lists.
          (setq completions (cons (list (substring output 0 (match-beginning 0)))
                                  completions)
                output (substring output (match-end 0))))

        (setq completions (nreverse completions)))

       ;; Case: failure
       (t
        (error "Internal error, '%s' returned unexpected output, %s" cmd output)))

      ;; Result
      (list (cons 'cmd-text-to-replace cmd-text-to-replace)
            (cons 'replacement-text replacement-text)
            (cons 'completions completions)))))

(defun matlab-shell-get-completion-limit-pos (last-cmd completions)
  "Return the starting location of the common substring for completion.
Used by `matlab-shell-tab' to in matching the COMPLETIONS, i.e.
  (substring LAST-CMD limit-pos (length last-cmd))
is the common starting substring of each completion in completions."
  (let ((limit-pos (length last-cmd)))
    (when completions
      (let* ((completion (car (car completions)))
             (i (length completion))
             (chomp-num-chars nil))
        (while (> i 0)
          (let ((part (substring completion 0 i)))
            ;; Do case insensitive comparison on the substring suffix.  Consider
            ;;    set_param(bdroot, 'simulationc<TAB>
            ;; which gives completions == '(("SimulationCommand"))
            (if (string-suffix-p part last-cmd t)
                (progn
                  (setq chomp-num-chars i)
                  (setq i 0))
              (setq i (- i 1)))))
        (if chomp-num-chars
            (setq limit-pos (- (length last-cmd) chomp-num-chars)))))
    limit-pos))

(defun matlab-shell-get-completion-info ()
  "Compute completions needed for `matlab-shell-tab' and `company-matlab-shell'.
Completions are computed based on the prefix on the last command prompt.
No completions are provided anywhere else in the buffer."
  (if (or (not (= (point) (point-max)))
          (not (matlab-on-prompt-p)))
      nil ;; no completions. We can only complete when typing a command.
    (let ((inhibit-field-text-motion t)
          (last-cmd nil)
          (last-cmd-start-point nil)
          (common-substr nil)
          (limit-pos nil)
          (completions nil)
          (common-substr-start-pt nil)
          (common-substr-end-pt nil)
          (did-completion nil))
      ;; Load last-cmd which is the command we are completing on.
      ;; We need to avoid altering point because when we ask for completions, we send
      ;; emacsdocomplete(last-cmd-quoted) to the MATLAB command window and then grab the results
      ;; and erase them.
      (save-excursion
        (goto-char (point-max))
        (beginning-of-line)
        (re-search-forward comint-prompt-regexp)
        (setq last-cmd-start-point (point))
        ;; save the old (last) command
        (setq last-cmd (buffer-substring (point) (line-end-position))))

      ;; Get the list of completions.
      ;; When obtaining completions, we can't use save-excursion because we are
      ;; manipulating the text in the *MATLAB* window at the point and this
      ;; move point-marker which causes save-excursion to move to the wrong
      ;; location.  We need to do this before we manipulate the text in the
      ;; *MATLAB* buffer because `matlab-shell-completion-list' sends
      ;; emacsdocompletion('statement') to matlab and matlab produces output in
      ;; the *MATLAB* buffer, then `matlab-shell-completion-list' removes the
      ;; output from the *MATLAB* buffer.
      (let ((last-cmd-quoted last-cmd))
        ;; Load last-cmd-quoted which the expression typed (e.g. "!mv file.").
        ;; Note, this has single quotes doubled up so we can ask
        ;; MATLAB for completions on last-cmd-quoted.
        (while (string-match "[^']\\('\\)\\($\\|[^']\\)" last-cmd-quoted)
          (setq last-cmd-quoted (replace-match "''" t t last-cmd-quoted 1)))

        (let* ((completion-list (matlab-shell-completion-list last-cmd-quoted))
               (cmd-text-to-replace (cdr (assoc 'cmd-text-to-replace completion-list))))
          (setq completions (cdr (assoc 'completions completion-list)))
          (when (and cmd-text-to-replace (not (string= cmd-text-to-replace "")))
            ;; need to alter the command to replace replacement-text with the common substring
            (let ((replacement-text (cdr (assoc 'replacement-text completion-list)))
                  (last-cmd-start-len (- (length last-cmd) (length cmd-text-to-replace))))
              ;; Replace the text typed in the *MATLAB* and update last-cmd
              (goto-char (+ last-cmd-start-point last-cmd-start-len))
              (delete-region (point) (line-end-position))
              (insert replacement-text)
              (setq last-cmd (concat (substring last-cmd 0 last-cmd-start-len) replacement-text))
              (if (not completions)
                  (setq did-completion t))
              ))))

      ;; Consider
      ;;   >> ! touch foo.ext1 foo.ext2
      ;;   >> ! mv foo.<TAB>
      ;; 'completions' will contain (("foo.ext1") ("foo.ext2")) and
      ;; common-substr will be "foo." which is used in displaying the
      ;; completions. The limit-pos in this case will be 5 and
      ;; last-cmd "! mv foo."
      (setq limit-pos (matlab-shell-get-completion-limit-pos last-cmd completions))
      (setq common-substr (substring last-cmd limit-pos))

      ;; Mark the subfield of the completion result so we can say no completions
      ;; if there aren't any otherwise we need to remove it.
      (save-excursion
        (goto-char (point-max))
        (beginning-of-line)
        (re-search-forward comint-prompt-regexp)
        (setq common-substr-start-pt (+ (point) limit-pos))
        (setq common-substr-end-pt (line-end-position))

        ;; Some MATLAB completions are case insensitive. Consider:
        ;;    set_param(bdroot, 'simulationc<TAB>
        ;; We'll get completions == '(("SimulationCommand")) and the common-substr
        ;; ignoring case will be "simulationc" whereas the common substring in completions
        ;; is "SimulationC". In this case replace "simulationc" with "SimulationC" for the
        ;; completion engine and after TAB completion completes, we'll see
        ;;    set_param(bdroot, 'SimulationCommand
        (when (and (< common-substr-start-pt common-substr-end-pt)
                   (> (length completions) 0))
          (let* ((common-substr-len (- common-substr-end-pt common-substr-start-pt))
                 (c-common-substr (substring (caar completions) 0 common-substr-len)))

            (when (and (not (string-equal c-common-substr common-substr))
                       ;; compare-strings case insensitive
                       (eq t (compare-strings c-common-substr 0 nil common-substr 0 nil t)))
              (save-excursion
                (delete-region common-substr-start-pt common-substr-end-pt)
                (goto-char common-substr-start-pt)
                (insert c-common-substr)))))

        ;; If completion is same as what we have, then it's not a completion
        (when (and (eq (length completions) 1)
                   (string-equal common-substr (car (car completions))))
          (setq completions nil)) ;; force display of "No completions"
        )
      ;; Result
      (list (cons 'last-cmd               last-cmd)
            (cons 'common-substr          common-substr)
            (cons 'limit-pos              limit-pos)
            (cons 'completions            completions)
            (cons 'common-substr-start-pt common-substr-start-pt)
            (cons 'common-substr-end-pt   common-substr-end-pt)
            (cons 'did-completion         did-completion)
            ))))

(defun matlab-shell-c-tab ()
  "Send [TAB] to the currently running matlab process and retrieve completions."
  (interactive)
  (let ((matlab-shell-tab-company-available nil))
    (matlab-shell-tab)))

;; matlab-shell-tab,
;;   This sends the command text at the prompt to emacsdocomplete.m which returns a list
;;   of possible completions. To do this we use comint to 'type' emacsdocomplete(command)
;;   in the *MATLAB* buffer and then we extract the result. The emacsdocomplete returns
;;   a list of possible completions of the command where each completion starts with
;;   zero or more characters of the command "suffix". For example, give a directory
;;   containing files, foo.ext1 and foo.ext2,
;;      >> ! mv foo.<TAB>
;;   will call emacsdocomplete('! mv foo.') which returns (("foo.ext1") ("foo.ext2"))
;;   where we have four characters "foo." matching the suffix of command. There can
;;   be zero suffix match as in
;;      >> !ls /usr/
;;   which on Linux returns a long list of items e.g. (("bin", "include", ...))
;;
;;
;;   Test cases (using R2016b):
;;
;;   >> h=figure;
;;   >> h.Num<TAB>                   Should show Number
;;   >> h.Number<TAB>                Should show Number and NumberTitle
;;   >>          h.Num<TAB>          Should do same. The extra white space shouldn't mess things up.
;;
;;   >> h.NumberTitle<TAB>           Should display message "No completions"
;;   >> set(h,'<TAB>                 Should display a long list
;;      type P<TAB>                  Should narrow to Parent, Position, ...
;;
;;   >> !touch file.ext              Assuming no other file.* names in current directory.
;;   >> !mv file.<TAB>               Should complete to file.ext
;;   >> !mv file.ext<TAB>            Should do nothing
;;
;;   >> !/usr/b<TAB>                 Should complete to /usr/bin
;;   >> !/usr/bin/cpp-4.<TAB>        Should complete to something like /usr/bin/cpp-4.9
;;
;;   >> ls /usr/include/byteswap.<TAB>  Some file with a '.' should complete correctly
;;
;;   >> !touch foo.ext1 foo.ext2
;;   >> ! mv foo.<TAB>               Should give options foo.ext1 foo.ext2
;;   >> ! mv foo.ext1<TAB>           Should say no completions
;;   >> ! mv foo.ext1 <TAB>          Should say no completions (note the space before the TAB)
;;
;;   >> vdp
;;   >> get_param('vdp','Pos<TAB>    Should show several completions
(defun matlab-shell-tab ()
  "Perform completions at the `matlab-shell' command prompt.
By default, uses `matlab-shell' toolbox command emacsdocomplete.m to get
completions.

If `matlab-shell-ask-MATLAB-for-completions' is nil, then use
`comint-dynamic-complete-filename' instead.

If `matlab-shell-tab-use-company' is non-nil, and if `company-mode' is
installed, then use company to display completions in a popup window."
  (interactive)
  (cond
   ;; If we aren't supposed to ask MATLAB for completions, then use
   ;; comint basics.
   ((not matlab-shell-ask-MATLAB-for-completions)
    (call-interactively 'comint-dynamic-complete-filename))

   ;; If company mode is available and we ask for it, use that.
   ((and matlab-shell-tab-company-available matlab-shell-tab-use-company company-mode)
    ;; We don't add to company-backends because we bind TAB to matlab-shell-tab
    ;; which means completions must be explicitly requested. The default
    ;; company-complete tries to complete as you type which doesn't work
    ;; so well because it can take MATLAB a bit to compute completions.
    (call-interactively 'company-matlab-shell))

   ;; Use stock Emacs completion
   (t
    (matlab-shell-do-completion-light))))

(defun matlab-shell-do-completion-light ()
  "Perform completion using `completion-in-region'."
  (let* ((inhibit-field-text-motion t)
         (completion-info        (matlab-shell-get-completion-info))
         (completions            (cdr (assoc 'completions completion-info)))
         (did-completion         (cdr (assoc 'did-completion completion-info)))
         (common-substr-start-pt (cdr (assoc 'common-substr-start-pt completion-info)))
         (common-substr-end-pt   (cdr (assoc 'common-substr-end-pt completion-info))))
    (when (and (not did-completion) common-substr-start-pt common-substr-end-pt)
      (completion-in-region common-substr-start-pt common-substr-end-pt completions))))

;;; Find Files
;;
;; Finding Files with MATLAB shell.

(defun matlab-shell-which-fcn (fcn)
  "Get the location of FCN's M file.
Returns cons (LOCATION . BUILTIN-FLAG) or nil if not found.
LOCATION is a string indicating where it is, and BUILTIN-FLAG is non-nil
if FCN is a builtin.  When BUILTIN-FLAG is t, the LOCATION may be a file
that doesn't exist."
  (save-excursion
    (let* ((msbn (matlab-shell-buffer-barf-not-running))
           (cmd (format "disp(which('%s'))" fcn))
           (comint-scroll-show-maximum-output nil)
           output)
      (set-buffer msbn)
      (goto-char (point-max))
      (if (not (matlab-on-prompt-p))
          (error "MATLAB shell must be non-busy to do that"))
      (setq output (matlab-shell-collect-command-output cmd))
      ;; BUILT-IN
      (cond
       ((string-match "built-in (\\([^)]+\\))" output)
        (cons (concat (substring output (match-beginning 1) (match-end 1))
                      ".m")
              t))
       ;; Error
       ((string-match "not found" output)
        nil)
       ;; JUST AN M FILE
       (t
        (string-match "$" output)
        (cons (substring output 0 (match-beginning 0)) nil))))))

(defun matlab-shell-locate-fcn (fcn)
  "Run \"which FCN\" in the `matlab-shell', then open the file."
  (interactive
   (list
    (let ((default (matlab-read-word-at-point)))
      (if (and default (not (equal default "")))
          (let ((s (read-string (concat "MATLAB locate fcn (default " default "): "))))
            (if (string= s "") default s))
        (read-string "MATLAB locate fcn: ")))))
  (let ((file-pair (matlab-shell-which-fcn fcn)))
    (if file-pair
        (let ((file (car file-pair)))
          (if (or (not (string-match-p "\\.m\\'" file))
                  (not (file-exists-p file)))
              (error "%s is built-in or a non-m-file" file)
            (find-file file)))
      (error "Command which('%s') returned empty" fcn))))

(defvar matlab-shell-matlabroot-run nil
  "Cache of MATLABROOT in this shell.")
(make-variable-buffer-local 'matlab-shell-matlabroot-run)

(defun matlab-shell-matlabroot ()
  "Get the location of this shell's root.
Returns a string path to the root of the executing MATLAB."
  (save-excursion
    (let* ((msbn (matlab-shell-buffer-barf-not-running))
           (cmd "disp(matlabroot)")
           (comint-scroll-show-maximum-output nil)
           output)
      (set-buffer msbn)
      (goto-char (point-max))

      (if matlab-shell-matlabroot-run
          matlab-shell-matlabroot-run

        ;; If we haven't cached it, calculate it now.
        (if (not (matlab-on-prompt-p))
            (error "MATLAB shell must be non-busy to do that"))
        (setq output (matlab-shell-collect-command-output cmd))

        (string-match "$" output)
        (setq matlab-shell-matlabroot-run
              (substring output 0 (match-beginning 0)))))))



;;; MATLAB Shell Commands =====================================================
;;
;; These commands will use matlab-shell as a utility, capture and display output.

(defun matlab-read-word-at-point ()
  "Get the word closest to point, but do not change position.
Has a preference for looking backward when not directly on a symbol.
Snatched and hacked from dired-x.el"
  (let ((word-chars "a-zA-Z0-9_")
        (bol (line-beginning-position))
        (eol (line-end-position)))
    (save-excursion
      ;; First see if just past a word.
      (if (looking-at (concat "[" word-chars "]"))
          nil
        (skip-chars-backward (concat "^" word-chars "{}()[]") bol)
        (if (not (bobp)) (backward-char 1)))
      (if (numberp (string-match (concat "[" word-chars "]")
                                 (char-to-string (following-char))))
          (buffer-substring (progn (skip-chars-backward word-chars bol) (point))
                            (progn (skip-chars-forward word-chars eol) (point)))
        ;; else not found, return empty string
        ""))))

(defun matlab-read-line-at-point ()
  "Get the line under point, if command line."
  (if (eq major-mode 'matlab-shell-mode)
      (save-excursion
        (let ((inhibit-field-text-motion t))
          (beginning-of-line)
          (if (not (looking-at (concat comint-prompt-regexp)))
              ""
            (search-forward-regexp comint-prompt-regexp)
            (buffer-substring (point) (line-end-position)))))
    (matlab--get-command-at-point-to-run)))

(defun matlab-non-empty-lines-in-string (str)
  "Return number of non-empty lines in STR."
  (let ((count 0)
        (start 0))
    (while (string-match "^.+$" str start)
      (setq count (1+ count)
            start (match-end 0)))
    count))

(declare-function matlab-shell-help-mode "matlab-topic")
(defun matlab-output-to-temp-buffer (buffer output)
  "Print output to temp buffer, or a message if empty string.
BUFFER is the buffer to output to, and OUTPUT is the text to insert."
  (let ((lines-found (matlab-non-empty-lines-in-string output)))
    (cond ((= lines-found 0)
           (message "(MATLAB command completed with no output)"))
          ((= lines-found 1)
           (string-match "^.+$" output)
           (message (substring output (match-beginning 0)(match-end 0))))
          (t (with-output-to-temp-buffer buffer (princ output))
             (with-current-buffer buffer
               (matlab-shell-help-mode))))))

(defun matlab-shell-run-command (command)
  "Run COMMAND and display result in a buffer.
This command requires an active MATLAB shell."
  (interactive (list (read-from-minibuffer
                      "MATLAB command line: "
                      (cons (matlab-read-line-at-point) 0))))
  (let ((doc (matlab-shell-collect-command-output command)))
    (matlab-output-to-temp-buffer "*MATLAB Run Command Result*" doc)))

(defun matlab-shell-describe-variable (variable)
  "Get the contents of VARIABLE and display them in a buffer.
This uses the WHOS (MATLAB 5) command to find viable commands.
This command requires an active MATLAB shell."
  (interactive (list (read-from-minibuffer
                      "MATLAB variable: "
                      (cons (matlab-read-word-at-point) 0))))
  (let ((doc (matlab-shell-collect-command-output (concat "whos " variable))))
    (matlab-output-to-temp-buffer "*MATLAB Help*" doc)))

(defun matlab-shell-describe-command (command)
  "Describe COMMAND textually by fetching it's doc from the MATLAB shell.
This uses the lookfor command to find viable commands.
This command requires an active MATLAB shell."
  (interactive
   (let ((fn (matlab--function-called-at-point))
         val)
     (setq val (read-string (if fn
                                (format "Describe function (default %s): " fn)
                              "Describe function: ")))
     (if (string= val "") (list fn) (list val))))
  (let ((doc (matlab-shell-collect-command-output (concat "help -emacs " command))))
    (matlab-output-to-temp-buffer "*MATLAB Help*" doc)))

(defun matlab-shell-apropos (matlabregex)
  "Look for any active commands in MATLAB matching MATLABREGEX.
This uses the lookfor command to find viable commands."
  (interactive (list (read-from-minibuffer
                      "MATLAB command subexpression: "
                      (cons (matlab-read-word-at-point) 0))))
  (let ((ap (matlab-shell-collect-command-output
             (concat "lookfor " matlabregex))))
    (matlab-output-to-temp-buffer "*MATLAB Apropos*" ap)))

(defun matlab-on-prompt-p ()
  "Return t if we MATLAB can accept input."
  (save-excursion
    (let ((inhibit-field-text-motion t))
      (goto-char (point-max))
      (beginning-of-line)
      (looking-at comint-prompt-regexp))))

(defun matlab-on-empty-prompt-p ()
  "Return t if we MATLAB is on an empty prompt."
  (with-current-buffer (matlab-shell-active-p)
    (let ((inhibit-field-text-motion t))
      (goto-char (point-max))
      (beginning-of-line)
      (looking-at (concat comint-prompt-regexp "\\s-*$")))))

(defun matlab-on-debug-prompt-p ()
  "Return t if we MATLAB is on an debug prompt."
  (with-current-buffer (matlab-shell-active-p)
    (let ((inhibit-field-text-motion t))
      (goto-char (point-max))
      (beginning-of-line)
      (looking-at (concat "K>>\\s-*")))))

(defun matlab-shell-buffer-barf-not-running ()
  "Return a running MATLAB buffer iff it is currently active."
  (or (matlab-shell-active-p)
      (error "You need to run the command `matlab-shell' to do that!")))

(defun matlab-shell-busy-checker (action &optional output-start-char)
  "If MATLAB shell prompt is busy, perform ACTION.
If ACTION is \\='error-if-busy, and the MATLAB shell is active and busy, an
error is produced.  If the shell is not active, no error is produced.

If ACTION is \\='wait-for-prompt, then the MATLAB shell must be
active and if it's busy, we'll wait for the prompt to appear.  If
optional OUTPUT-START-CHAR is specified, then `point' must move
past that."
  (let ((msbn (matlab-shell-active-p)))
    (cond

     ((eq action 'error-if-busy)
      (when msbn
        (with-current-buffer (get-buffer msbn)
          (when (and matlab-prompt-seen
                     (not (matlab-on-empty-prompt-p)))
            (error "%s is busy; please retry when the MATLAB shell is waiting for input" msbn)))))

     ((eq action 'wait-for-prompt)

      (unless msbn
        (error "The MATLAB shell buffer does not exist"))

      ;; Note, this function is leveraged by org-mode babel matlab code block evaluation.  In this
      ;; context, the current buffer is not the MATLAB shell buffer.
      (with-current-buffer (get-buffer msbn)
        (goto-char (point-max))

        ;; Turn on C-g by using with-local-quit. This is needed to prevent message:
        ;;  "Blocking call to accept-process-output with quit inhibited!! [115 times]"
        ;; when using `company-matlab-shell' for TAB completions.
        (with-local-quit
          (let ((notimeout t))
            (while (or (and output-start-char (>= output-start-char (point)))
                       (or (not matlab-prompt-seen) ;; not past the startup hooks
                           (not (matlab-on-empty-prompt-p)))
                       notimeout)
              (setq notimeout
                    (accept-process-output (get-buffer-process (current-buffer)) .1))
              (goto-char (point-max)))))))

     (t
      (error "Invalid action, %s" action)))))

(defun matlab-shell-collect-command-output (command)
  "If there is a MATLAB shell, run the MATLAB COMMAND and return it's output.
It's output is returned as a string with no face properties.  The text output
of the command is removed from the MATLAB buffer so there will be no
indication that it ran."
  (let ((msbn (matlab-shell-buffer-barf-not-running))
        (matlab-shell-suppress-prompt-hooks t))
    ;; We are unable to use save-excursion to save point position because we are
    ;; manipulating the *MATLAB* buffer by erasing the current text typed at the
    ;; MATLAB prompt (where point is) and then we send command to MATLAB and
    ;; grab the result. After this we erase the output from command and then
    ;; restore the current text at the MATLAB prompt and move to start-point.
    ;; Note, save-excursion works by tracking `point-marker' and when you manipulate
    ;; the text at point, `point-marker' moves causing save-excursion to move
    ;; the point in to a location we don't want. See:
    ;; http://emacs.stackexchange.com/questions/7574/why-save-excursion-doesnt-save-point-position
    ;; Ideally there would be some way to prevent the *MATLAB* buffer from refreshing
    ;; as we are interacting with it, but I couldn't figure out a way to do that.
    (with-current-buffer msbn
      (save-window-excursion
        (let ((pos nil)
              (str nil)
              (lastcmd)
              (inhibit-field-text-motion t)
              (start-point (point)))
          (if (not (matlab-on-prompt-p))
              (error "MATLAB shell must be non-busy to do that"))

          ;; Save the old command
          (goto-char (point-max))
          (beginning-of-line)
          (re-search-forward comint-prompt-regexp)
          ;; Backup if there are extra spaces. To see why, try tab completion on command with
          ;; leading spaces, e.g.
          ;; >> h=figure;
          ;; >>                    h.Num<TAB>
          (re-search-backward ">")
          (forward-char 2)
          (setq lastcmd (buffer-substring (point) (line-end-position)))
          (delete-region (point) (line-end-position))
          ;; We are done error checking, run the command.
          (setq pos (point))
          (let ((output-start-char
                 ;; We didn't get enough output until we are past the starting point.
                 ;; Starting point depends on if we echo or not.
                 (if matlab-shell-echoes
                     (+ pos 1 (string-width command)) ; 1 is newline
                   pos)))
            ;; Note, comint-simple-send in emacs 24.4 appends a newline and code below assumes
            ;; one prompt indicates command completed, so don't append a newline.
            (comint-simple-send (get-buffer-process (current-buffer)) command)
            ;; Wait for the command to finish, by looking for new prompt.
            (goto-char (point-max))
            (matlab-shell-busy-checker 'wait-for-prompt output-start-char)

            ;; Get result of command into str
            (goto-char pos)
            (setq str (buffer-substring-no-properties output-start-char
                                                      (save-excursion
                                                        (goto-char (point-max))
                                                        (beginning-of-line)
                                                        (point))))
            )

          ;; delete the result of command
          (delete-region pos (point-max))
          ;; restore contents of buffer so it looks like nothing happened.
          (insert lastcmd)
          (goto-char start-point)
          ;; return result 'string' from executing MATLAB command
          str)))))

(defun matlab-shell-send-command (command)
  "Send COMMAND to a MATLAB process.
If there is a `matlab-shell', send it to the command prompt.
If there is only a `matlab-netshell', send it to the netshell."
  (if (matlab-shell-active-p)
      (with-current-buffer (matlab-shell-active-p)
        (matlab-shell-send-string (concat command "\n")))

    ;; As a backup, use netshell.
    (matlab-netshell-eval command)))

(defun matlab-shell-send-string (string)
  "Send STRING to the currently running matlab process."
  (if (not matlab-shell-echoes)
      (let ((proc (get-buffer-process (current-buffer))))
        (goto-char (point-max))
        (insert string)
        (set-marker (process-mark proc) (point))))
  (when matlab-shell-io-testing
    (message "<--[%s]" string))
  (comint-send-string (get-buffer-process (current-buffer)) string))

(defun matlab-url-at (p)
  "Return the matlab-url overlay at P, or nil."
  (let ((url nil) (o (overlays-at p)))
    (while (and o (not url))
      (setq url (overlay-get (car o) 'matlab-url)
            o (cdr o)))
    url))

(defun matlab-url-stack-top-at (p)
  "Return the matlab-url overlay at P, or nil."
  (let ((url nil) (o (overlays-at p)))
    (while (and o (not url))
      (setq url (or (overlay-get (car o) 'first-in-error-stack)
                    (overlay-get (car o) 'matlab-url))
            o (cdr o)))
    url))

(defun matlab-shell-previous-matlab-url (&optional stacktop)
  "Find a previous occurrence of an overlay with a MATLAB URL.
If STACKTOP is non-nil, then also get the top of some stack, which didn't
show up in reverse order."
  (save-excursion
    (let ((url nil) (p (point)))
      (while (and (not url)
                  (setq p (previous-overlay-change p))
                  (not (eq p (point-min))))
        (setq url
              (if stacktop
                  (matlab-url-stack-top-at p)
                (matlab-url-at p))))
      url)))

;; (matlab-shell-mref-to-filename "eltest.utils.testme>localfcn")

(defun matlab-shell-class-mref-to-file (mref &optional fcn-p)
  "Convert a class like reference MREF to a file name.
Optional FCN-P indicates specifies to force treating as a function."
  (let* ((LF (split-string mref ">"))
         (S (split-string (car LF) "\\."))
         (L (last S))
         (ans nil))
    (if (member L '("mlx" "m"))
        nil
      ;; Not a . from a .m file, probably a class ??
      (while S
        (when (and (= (length S) 1) (not fcn-p))
          ;; Is there is a method? strip it off.
          (let ((meth (split-string (car S) "/")))
            (setq S (list (car meth)))))
        ;; Append the parts together.
        (setq ans (concat ans
                          (if (> (length S) 1) "+"
                            (unless fcn-p
                              (concat "@" (car S) "/")))
                          (car S)))
        (setq S (cdr S))
        (if S (setq ans (concat ans "/"))
          (setq ans (concat ans ".m")))
        ))
    ans))

(defun matlab-shell-mref-which-fcn (ref)
  "Try to run `which' on REF to find actual file location.
If the MATLAB shell isn't ready to run a which command, skip and
return nil."
  (unless matlab-shell-in-process-filter
    (save-excursion
      (let* ((msbn (matlab-shell-buffer-barf-not-running)))
        (set-buffer msbn)
        (goto-char (point-max))
        (if (and (matlab-on-prompt-p) (not matlab-shell-cco-testing))
            (matlab-shell-which-fcn ref)
          nil)))))

(defvar matlab-shell-mref-converters
  (list
    ;; Does it work as is?
    (lambda (mref) mref)
    ;; p files
    (lambda (mref) (when (string-match "\\.\\(p\\)\\'" mref)
                     (replace-match "m" nil t mref 1)))
    ;; Function name, no extension.
    (lambda (mref) (unless (string-match "\\.m\\'" mref) (concat mref ".m")))
    ;; Methods in a class
    (lambda (mref) (when (string-match "\\." mref)
                     (matlab-shell-class-mref-to-file mref)))
    ;; A function in a package
    (lambda (mref) (when (string-match "\\." mref)
                     (matlab-shell-class-mref-to-file mref t)))
    ;; Copied from old code, not sure what it matches.
    (lambda (mref) (when (string-match ">" mref)
                     (concat (substring mref 0 (match-beginning 0)) ".m")))
    ;; Ask matlab where it came from.  Keep last b/c expensive, or won't
    ;; work if ML is busy.
    (lambda (mref) (car (matlab-shell-mref-which-fcn mref)))
    )
  "List of converters to convert MATLAB file references into a filename.
Each element is a function that accepts a file ref, and returns
a file name, or nil if no conversion done.")

;; (matlab-shell-mref-to-filename "eltest.utils.testme>localfcn")

(defun matlab-shell-mref-to-filename (fileref)
  "Convert MATLAB file reference FILEREF into an file Emacs can load.
MATLAB can refer to functions on the path by a short name, or by a .p
extension, and a host of different ways.  Convert this reference into
something Emacs can load.  If matlab-shell is running remote via tramp,
returned file will be prefixed with the remote location."
  (interactive "sFileref: ")
  (with-current-buffer (matlab-shell-active-p)
    (let ((remote-location (file-remote-p default-directory))
          (C matlab-shell-mref-converters)
          ans)
      (while (and C (not ans))
        (let ((tmp (funcall (car C) fileref)))
          (when tmp
            (when (and remote-location (not (file-remote-p tmp)))
              (setq tmp (concat remote-location tmp)))
            (when (file-exists-p tmp)
              (setq ans tmp))))
        (setq C (cdr C)))
      (when (called-interactively-p 'any)
        (message "Found: %S" ans))
      ans)))

(defun matlab-find-other-window-file-line-column (ef el ec &optional debug)
  "Find file EF in other window and to go line EL and 1-basec column EC.
If DEBUG is non-nil, then setup GUD debugging features."
  (let ((ef-converted (matlab-shell-mref-to-filename ef)))
    (unless ef-converted
      (error "Failed to translate %s into a filename" ef))
    (find-file-other-window ef-converted)
    (goto-char (point-min))
    (forward-line (1- (string-to-number el)))
    (when debug
      (setq mlgud-last-frame (cons (buffer-file-name) (string-to-number el)))
      (mlgud-display-frame))
    (setq ec (string-to-number ec))
    (if (> ec 0) (forward-char (1- ec)))))

;; TODO: No callers use DEBUG input  Remove?
(defun matlab-find-other-window-via-url (url &optional debug)
  "Find other window using matlab URL and optionally set DEBUG cursor."
  (cond ((string-match "^error:\\(.*\\),\\([0-9]+\\),\\([0-9]+\\)$" url)
         (let ((ef (substring url (match-beginning 1) (match-end 1)))
               (el (substring url (match-beginning 2) (match-end 2)))
               (ec (substring url (match-beginning 3) (match-end 3))))
           (matlab-find-other-window-file-line-column ef el ec debug)))
        ((string-match "opentoline('\\([^']+\\)',\\([0-9]+\\),\\([0-9]+\\))" url)
         (let ((ef (substring url (match-beginning 1) (match-end 1)))
               (el (substring url (match-beginning 2) (match-end 2)))
               (ec (substring url (match-beginning 3) (match-end 3))))
           (matlab-find-other-window-file-line-column ef el ec debug)))
        ((string-match "^matlab:*\\(.*\\)$" url)
         (process-send-string
          (get-buffer-process mlgud-comint-buffer)
          (concat (substring url (match-beginning 1) (match-end 1)) "\n")))))

(defun matlab-shell-last-error ()
  "In the MATLAB interactive buffer, find the last MATLAB error, and go there.
To reference old errors, put the cursor just after the error text."
  (interactive)
  (catch 'done
    (let ((url (matlab-shell-previous-matlab-url t)))
      (if url
          (progn (matlab-find-other-window-via-url url) (throw 'done nil))
        (save-excursion
          (end-of-line) ;; In case we are before the line number 1998/06/05 16:54sk
          (let ((err (matlab-shell-scan-for-error (point-min))))
            (unless err (error "No errors found!"))
            (let ((ef (nth 2 err))
                  (el (nth 3 err))
                  (ec (or (nth 4 err) "0")))
              (matlab-find-other-window-file-line-column ef el ec))))))))

(defun matlab-shell-html-click (e)
  "Go to the error at the location of event E."
  (interactive "e")
  (mouse-set-point e)
  (matlab-shell-html-go))

(defun matlab-shell-html-go ()
  "Go to the error at the location `point'."
  (interactive)
  (let ((url (matlab-url-at (point))))
    (if url (matlab-find-other-window-via-url url))))

(defun matlab-shell-dbstop-error ()
  "Stop on errors."
  (interactive)
  (comint-send-string (get-buffer-process (current-buffer))
                      "dbstop if error\n"))

(defun matlab-shell-dbclear-error ()
  "Don't stop on errors."
  (interactive)
  (comint-send-string (get-buffer-process (current-buffer))
                      "dbclear if error\n"))

(defun matlab-shell-demos ()
  "MATLAB demos."
  (interactive)
  (comint-send-string (get-buffer-process (current-buffer)) "demo\n"))

(defun matlab-shell-close-figures ()
  "Close any open figures."
  (interactive)
  (comint-send-string (get-buffer-process (current-buffer)) "close all\n"))

(defun matlab-shell-close-current-figure ()
  "Close current figure."
  (interactive)
  (comint-send-string (get-buffer-process (current-buffer)) "delete(gcf)\n"))

(defun matlab-shell-sync-buffer-directory ()
  "Sync matlab-shell `default-directory' with MATLAB's pwd.
These will differ when MATLAB code directory without notifying Emacs."
  (interactive)
  (comint-send-string (get-buffer-process (current-buffer)) "emacscd%%\n"))

(defun matlab-shell-exit ()
  "Exit MATLAB shell."
  (interactive)
  (comint-send-string (get-buffer-process (current-buffer)) "exit\n")
  (kill-buffer nil))


;;; MATLAB mode Shell commands ================================================
;;
;; These commands are provided in MATLAB code buffers to interact with
;; the shell.

(defun matlab-show-matlab-shell-buffer ()
  "Switch to the buffer containing the matlab process."
  (interactive)
  (let ((msbn (concat "*" matlab-shell-buffer-name "*")))
    (if (get-buffer msbn)
        (switch-to-buffer-other-window msbn)
      (message "There is not an active MATLAB process."))))

(defvar matlab-shell-save-and-go-history '("()")
  "Keep track of parameters passed to the MATLAB shell.")

(defvar matlab-shell-save-and-go-command nil
  "Command to use for `matlab-shell-save-and-go' instead of current buffer.
This command will override the default computed command if non-nil.
The command will be run in the shell's current directory without checks, so
you will need to make sure MATLAB's pwd is correct.
It is recommended you use directory-local or buffer-local variable settings to
control this.")
(make-variable-buffer-local 'matlab-shell-save-and-go-command)
;; Marking as SAFE b/c we will ask to use this before doing so.
(put 'matlab-shell-save-and-go-command 'safe-local-variable #'stringp)

(defvar matlab-shell-save-and-go-command-enabled nil
  "Remember if it is safe to use `matlab-shell-save-and-go-command' in this buffer.")
(make-variable-buffer-local 'matlab-shell-save-and-go-command-enabled)
(put 'matlab-shell-save-and-go-command 'risky-local-variable t)

(defun matlab-shell-set-save-and-go-command (command)
  "Set `matlab-shell-save-and-go-command' for any file in the current directory.
Value is set to COMMAND."
  (interactive (list (read-string "sCommand: "
                                  (file-name-sans-extension
                                   (file-name-nondirectory (buffer-file-name))))))
  (when (and (not (eq major-mode 'matlab-ts-mode))
             (not (eq major-mode 'matlab-mode)))
      (user-error "Current buffer is not a MATLAB mode"))

  (add-dir-local-variable major-mode 'matlab-shell-save-and-go-command command))

(defun matlab-shell-add-to-input-history (string)
  "Add STRING to the input-ring and run `comint-input-filter-functions' on it.
Similar to  `comint-send-input'."
  (if (and (funcall comint-input-filter string)
           (or (null comint-input-ignoredups)
               (not (ring-p comint-input-ring))
               (ring-empty-p comint-input-ring)
               (not (string-equal (ring-ref comint-input-ring 0) string))))
      (ring-insert comint-input-ring string))
  (run-hook-with-args 'comint-input-filter-functions
                      (concat string "\n"))
  (if (boundp 'comint-save-input-ring-index);only bound in GNU emacs
      (setq comint-save-input-ring-index comint-input-ring-index))
  (setq comint-input-ring-index nil))

(defun matlab-shell-save-and-go ()
  "Save this M file, and evaluate it in a MATLAB shell."
  (interactive)
  (when (and (not (eq major-mode 'matlab-ts-mode))
             (not (eq major-mode 'matlab-mode)))
    (user-error "Current buffer is not a MATLAB mode"))
  (when (not (buffer-file-name (current-buffer)))
    (call-interactively 'write-file))

  (let* ((fn-name (file-name-sans-extension
                   (file-name-nondirectory (buffer-file-name))))
         (msbn (concat "*" matlab-shell-buffer-name "*"))
         (do-local t))

    (when matlab-shell-save-and-go-command
      ;; If an override command is set, run that instead of this file.
      (let* ((cmd matlab-shell-save-and-go-command)
             (use (or matlab-shell-save-and-go-command-enabled
                      (string= cmd fn-name)
                      (y-or-n-p (format "Run \"%s\" instead of %s? "
                                        cmd fn-name)))))
        (if (not use)

            ;; Revert to old behavior.
            nil

          ;; Else, use it.
          (setq do-local nil
                matlab-shell-save-and-go-command-enabled t)

          ;; No buffer?  No net connection?  Make a shell!
          (if (and (not (get-buffer msbn)) (not (matlab-netshell-active-p)))
              (matlab-shell))

          (when (get-buffer msbn)
            ;; Ok, now fun the function in the matlab shell
            (if (get-buffer-window msbn t)
                (select-window (get-buffer-window msbn t))
              (switch-to-buffer-other-window (concat "*" matlab-shell-buffer-name "*")))
            (goto-char (point-max)))

          (matlab-shell-send-command (concat cmd "\n"))
          )))

    (when do-local
      ;; else - try to make something up to run this specific command.
      (let* ((dir (expand-file-name (file-name-directory buffer-file-name)))
             (change-cd matlab-change-current-directory)
             (param ""))
        (save-buffer)
        ;; Do we need parameters?
        (if (save-excursion
              (goto-char (point-min))
              (end-of-line)
              (forward-sexp -1)
              (looking-at "([a-zA-Z]"))
            (setq param (read-string "Parameters: "
                                     (car matlab-shell-save-and-go-history)
                                     'matlab-shell-save-and-go-history)))

        ;; No buffer?  No net connection?  Make a shell!
        (if (and (not (get-buffer msbn)) (not (matlab-netshell-active-p)))
            (matlab-shell))

        (when (get-buffer msbn)
          ;; Ok, now fun the function in the matlab shell
          (if (get-buffer-window msbn t)
              (select-window (get-buffer-window msbn t))
            (switch-to-buffer-other-window (concat "*" matlab-shell-buffer-name "*")))
          (goto-char (point-max)))

        ;; Fixup DIR to be a valid MATLAB command
        (mapc
         (lambda (e)
           (while (string-match (car e) dir)
             (setq dir (replace-match
                        (format "', char(%s), '" (cdr e)) t t dir))))
         '(("ô" . "244")
           ("é" . "233")
           ("è" . "232")
           ("à" . "224")))

        ;; change current directory? - only w/ matlab-shell active.
        (if (and change-cd (get-buffer msbn))
            (progn
              (unless (string= dir default-directory)
                (matlab-shell-send-command (concat "emacscd(['" dir "'])")))

              (let ((cmd (concat fn-name " " param)))
                (matlab-shell-add-to-input-history cmd)

                (matlab-shell-send-string (concat cmd "\n"))
                ))

          ;; If not changing dir, maybe we need to use 'run' command instead?
          (let* ((match 0)
                 (tmp (while (setq match (string-match "'" param match))
                        (setq param (replace-match "''" t t param))
                        (setq match (+ 2 match))))
                 (cmd (concat "emacsrun('" dir fn-name "'"
                              (if (string= param "") "" (concat ", '" param "'"))
                              ")")))
            (ignore tmp)
            (matlab-shell-send-command cmd)))
        ))))

;;; Running buffer subset
;;
;; Run some subset of the buffer in matlab-shell.

(defun matlab-shell-run-region-or-line ()
  "Run region from BEG to END and display result in MATLAB shell.
This should be called from a *.m file in `matlab-ts-mode' or
`matlab-mode'.  If region is not active run the current line.
This command requires an active MATLAB shell."
  (interactive)
  (if (and transient-mark-mode mark-active)
      (matlab-shell-run-region (mark) (point))
    (matlab-shell-run-region (line-beginning-position) (line-end-position))))


;;;###autoload
(defun matlab-shell-run-region (beg end &optional noshow)
  "Run region from BEG to END and display result in MATLAB shell.
If NOSHOW is non-nil, replace newlines with commas to suppress output.
This should be called from a *.m file in `matlab-ts-mode' or
`matlab-mode'.  This command requires an active MATLAB shell."
  (interactive "r")
  (if (> beg end) (let (mid) (setq mid beg  beg end  end mid)))

  (let ((command (matlab-shell-region-command beg end noshow))
        (msbn nil)
        (lastcmd)
        (inhibit-field-text-motion t))

    (if (matlab-netshell-active-p)
        ;; Use netshell to run the command.
        (matlab-netshell-eval command)

      ;; else, send to the command line.
      (save-excursion
        (setq msbn (matlab-shell-buffer-barf-not-running))
        (set-buffer msbn)
        (if (not (matlab-on-prompt-p))
            (error "MATLAB shell must be non-busy to do that"))
        ;; Save the old command
        (beginning-of-line)
        (re-search-forward comint-prompt-regexp)
        (setq lastcmd (buffer-substring (point) (line-end-position)))
        (delete-region (point) (line-end-position))
        ;; We are done error checking, run the command.
        (matlab-shell-send-string command)
        ;; Put the old command back.
        (insert lastcmd)))

    ;; Regardless of how we send it, if there is a shell buffer, show it.
    (setq msbn (matlab-shell-active-p))
    (when msbn
      (set-buffer msbn)
      (goto-char (point-max))
      (display-buffer msbn
                      '((display-buffer-reuse-window display-buffer-at-bottom)
                        (reusable-frames . visible)
                        ))
      )))

;;; Convert regions to runnable text
;;
;; There are two techniques.
;; Option 1: Convert the region into a single command line, suppress output, and eval.
;; Option 2: Newer emacs, use `emacsrunregion.m' to use Editor hack for running regions out of a file.
;; Option 3: Older emacs, or if buffer isn't saved in a file. Copy into a script, and run the script.

(defun matlab-shell-region-command (beg end &optional noshow)
  "Convert the region between BEG and END into a MATLAB command.
Picks between different options for running the commands.
Optional argument NOSHOW specifies if we should echo the region to the
  command line."
  (cond
   ((eq matlab-shell-run-region-function 'auto)

    (let ((cnt (count-lines beg end)))

      (if (< cnt 2)
          ;; OLD WAY
          (matlab-shell-region->commandline beg end noshow)

        ;; else
        ;; NEW WAYS
        (if (file-exists-p (buffer-file-name (current-buffer)))
            (progn
              (save-buffer)
              (matlab-shell-region->internal beg end noshow))

          ;; No file, or older emacs, run region as tmp file.
          (matlab-shell-region->script beg end noshow)))
      ))

   (t
    (funcall matlab-shell-run-region-function beg end noshow))))

(defsubst matlab--cursor-in-string ()
  "Return t if the cursor is in a valid MATLAB character vector or string scalar."
  (nth 3 (syntax-ppss (point))))

(defun matlab-shell-region->commandline (beg end &optional noshow)
  "Convert the region between BEG and END into a MATLAB command.
Squeeze out newlines.
When NOSHOW is non-nil, suppress output by adding ; to commands."
  ;; Assume beg & end are in the right order.
  (let ((str (concat (buffer-substring beg end) "\n")))
    ;; Remove comments
    (with-temp-buffer
      (insert str)
      (goto-char (point-min))
      ;; Delete all the comments
      (while (search-forward "%" nil t)
        (unless (matlab--cursor-in-string)
          (delete-region (1- (point)) (line-end-position))))
      (setq str (buffer-substring-no-properties (point-min) (point-max))))

    ;; Strip out blank lines
    (while (string-match "^\\s-*\n" str)
      (setq str (concat (substring str 0 (match-beginning 0))
                        (substring str (match-end 0)))))
    ;; Strip out large chunks of whitespace
    (while (string-match "\\s-\\s-+" str)
      (setq str (concat (substring str 0 (match-beginning 0))
                        (substring str (match-end 0)))))
    (when noshow
      ;; Remove continuations
      (while (string-match
              (concat "\\s-*"
                      (regexp-quote "...")
                      "\\s-*\n")
              str)
        (setq str (replace-match " " t t str)))
      (while (string-match "\n" str)
        (setq str (replace-match ", " t t str)))
      (setq str (concat str "\n")))
    str))

(defun matlab-shell-region->internal (beg end &optional noshow)
  "Create a command to run the region between BEG and END.
Uses internal MATLAB API to execute the code keeping breakpoints
and local functions active.
Optional argument NOSHOW specifies if we should echo the region to the
  command line."
  (ignore noshow)
  ;; Reduce end by 1 char, as that is how ML treats it
  (setq end (1- end))

  (let ((enc-str (symbol-name buffer-file-coding-system)))
    (when (string-match "\\<dos" enc-str)
      ;; If the file has DOS line endings, we need to modify begin and end since
      ;; Emacs treats it as 1 char, but ML will treat it as 2 char.
      ;; Thus, add to beg and end the # of chars as there are lines.
      (save-excursion
        (goto-char beg)
        (setq beg (+ beg (count-lines (point-min) (point))))
        (goto-char end)
        (setq end (+ end (count-lines (point-min) (point))))
        )))

  (format "%s('%s',%d,%d)\n"
          matlab-shell-internal-emacsrunregion
          (buffer-file-name (current-buffer))
          beg end))


(declare-function matlab-semantic-get-local-functions-for-script "semantic-matlab")
(declare-function matlab-semantic-tag-text "semantic-matlab")
(declare-function semantic-tag-name "semantic/tag")

(defun matlab-shell-region->script (beg end &optional noshow)
  "Extract region between BEG & END into a temporary M file.
The tmp file name is based on the name of the current buffer.
The extracted region is unmodified from src buffer unless NOSHOW is non-nil,
in which case ; are added to quiesce the buffer.
Scan the extracted region for any functions that are in the original
buffer,and include them.
Return the name of the temporary file."
  (interactive "r")
  (ignore noshow)
  (require 'semantic-matlab)
  (let* ((start (count-lines (point-min) beg))
         (len (count-lines beg end))
         (stem (file-name-sans-extension (file-name-nondirectory
                                          (buffer-file-name))))
         (orig (current-buffer))
         (newf (concat stem "_" (number-to-string start) "_"
                       (number-to-string len)))
         (bss (buffer-substring-no-properties beg end))
         (buff (find-file-noselect (concat newf ".m")))
         (intro "%% Automatically created temporary file created to run-region")
         ;; These variables are for script / fcn tracking
         (functions (matlab-semantic-get-local-functions-for-script (current-buffer))))

    ;; TODO : if the directory in which the current buffer is in is READ ONLY
    ;; we should write our tmp buffer to /tmp instead.

    (with-current-buffer buff

      (goto-char (point-min))

      ;; Clean up old extracted regions.
      (when (looking-at intro) (delete-region (point-min) (point-max)))
      ;; Don't stomp on old code.
      (unless (= (point-min) (point-max))
        (error "Region extract to tmp file: Temp file not empty!"))

      (insert intro "\n\n" bss "\n%%\n")

      ;; Some scripts call local functions from the script.  Find them
      ;; and copy those local scripts over.
      (goto-char (point-min))
      (dolist (F functions)
        (save-excursion
          (when (re-search-forward (semantic-tag-name F) nil t)
            ;; Found, copy it in.
            (let ((ft (matlab-semantic-tag-text F orig)))
              (goto-char (point-max))
              (insert "% Copy of " (semantic-tag-name F) "\n\n")
              (insert ft)
              (insert "\n%%\n")))))

      ;; Save buffer, and setup ability to run this new script.
      (save-buffer)

      ;; Flush any pending MATLAB stuff.
      (accept-process-output)

      ;; This sets us up to cleanup our file after it's done running.
      (add-hook 'matlab-shell-prompt-appears-hook `(lambda () (matlab-shell-cleanup-extracted-region ,(buffer-file-name buff))))

      (kill-buffer)
      )

    ;; Return the command.
    (concat "run('" (expand-file-name newf) "')\n")))

(defun matlab-shell-cleanup-extracted-region (fname)
  "Cleanup the file created when we previously extracted a region.
Argument FNAME specifies if we should echo the region to the command line."
  (condition-case nil
      (delete-file fname)
    (error nil))

  (remove-hook 'matlab-shell-prompt-appears-hook
               ;; The below needs to be a perfect match to the setter.
               `(lambda () (matlab-shell-cleanup-extracted-region ,fname)))
  )

(defun matlab-shell-find-file-click (e)
  "Find the file clicked on with event E on the current path."
  (interactive "e")
  (mouse-set-point e)
  (let ((f (matlab-read-word-at-point)))
    (if (not f) (error "To find an M file, click on a word"))
    (matlab-shell-locate-fcn f)))

(provide 'matlab-shell)
;;; matlab-shell.el ends here

;; LocalWords:  Ludlam zappo compat comint mlgud gud defcustom nodesktop defface netshell tmp aref
;; LocalWords:  emacsclient commandline emacsrunregion errorscanning cco defconst defun setq Keymaps
;; LocalWords:  keymap subjob kbd emacscd featurep fboundp EDU msbn pc Thx Chappaz windowid tcp lang
;; LocalWords:  postoutput capturetext EMACSCAP captext STARTCAP progn eol dbhot erroexamples cdr
;; LocalWords:  ENDPT dolist overlaystack mref deref errortext ERRORTXT shellerror Emacsen iq nt buf
;; LocalWords:  auth mlfile EMAACSCAP buffname showbuff symlink'd emacsinit sha dirs ebstop
;; LocalWords:  evalforms Histed pmark memq promptend numchars integerp emacsdocomplete mycmd ba
;; LocalWords:  nreverse emacsdocompletion byteswap stringp cbuff mapcar bw FCN's alist substr usr
;; LocalWords:  dired bol bobp numberp princ minibuffer fn matlabregex lastcmd notimeout
;; LocalWords:  stacktop eltest testme localfcn LF fileref funcall ef ec basec sk nondirectory utils
;; LocalWords:  ignoredups boundp edir sexp Fixup mapc emacsrun noshow cnt ellipsis newf bss noselect
;; LocalWords:  fname mlx xemacs linux darwin truename clientcmd simulationc caar fontification
;; LocalWords:  defsubst ppss