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

;; Copyright (c) 2020  Paul William Rankin

;; Author: William Rankin <william@bydasein.com>
;; Keywords: files, outlines, wp, text
;; Version: 0.4.2
;; Package-Requires: ((emacs "24.4") (seq "2.20"))
;; URL: https://github.com/rnkn/binder

;; This file is not part of GNU Emacs.

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

;; # Binder #

;; Binder is global minor mode (and associated major modes) to facilitate
;; working on a writing project in multiple files. It is heavily inspired
;; by the binder feature in the [macOS writing app Scrivener][1].

;; The rationale behind working this way is to split a large writing
;; project into much smaller pieces.

;; [1]: https://www.literatureandlatte.com/scrivener/

;; ## Features ##

;; Primarily, Binder provides a global minor mode binder-mode. This
;; allows working with files in the current binder-project-directory.
;; Data concerning these files is saved in a .binder.el file in the
;; project directory. (You can change the name of this file with the
;; binder-default-file option.)

;; ## Navigation ##

;; At the most basic level, you can navigate back and forth through the
;; files in a project:

;; - binder-next (C-c ]) visits the next file in the binder, and
;; - binder-previous (C-c [) visits the previous.

;; Calling these commands activates a transient map so that each command
;; can be repeated without prefix key/s.

;; ## Sidebar ##

;; You'll mostly interact with the project structure via the sidebar.

;; -  binder-toggle-sidebar (C-c ') toggles the visibility of the binder
;;    sidebar
;; -  binder-reveal-in-sidebar (C-c ;) finds the current file in the
;;    sidebar

;; Each project items is a file reference relative to the project
;; directory. Items are displayed as:

;;     x * name        #tag1 #tag2

;; These things mean:

;; |------|-------------------------------------------------|
;; | x    | item is included when concatenating the project |
;; | *    | item has notes                                  |
;; | ?    | the item's corresponding file cannot be found   |
;; | name | the file name (or custom display name)          |
;; | #tag | arbitrary item tags                             |
;; |------|-------------------------------------------------|

;; An item's display name can be changed with binder-sidebar-rename (r).
;; If a file cannot be found, relocate with binder-sidebar-relocate (R).

;; Calling binder-sidebar-find-file (RET) will visit the corresponding
;; file.

;; To add an existing file, call binder-sidebar-add-file (a) or add all
;; files in directory with binder-sidebar-add-all-files (A).

;; Add a new file with binder-sidebar-new-file (M-RET). This prompts for a
;; file-name and adds this (possibly non-existent) file to the project
;; after the current file's index. If no file-name extension is provided,
;; use binder-default-file-extension.

;; > Hint: you can use an alternate default file extension for different
;; > projects by setting a directory local variable.

;; Files can also be added to a project from outside the sidebar with
;; binder-add-file (C-c :).

;; Remove items with binder-sidebar-remove (d) -- this *does not delete the
;; files*, only removes them from the project, but it *does delete* the
;; corresponding notes and tags.

;; Items can be reordered with binder-sidebar-shift-up (M-p | M-up) and
;; binder-sidebar-shift-down (M-n | M-down).

;; Hide item file extensions by setting the binder-sidebar-hide-file-extensions
;; option. This can be toggled with binder-sidebar-toggle-file-extensions (E).

;; The sidebar can be resized with binder-sidebar-shrink-window ({) and
;; binder-sidebar-enlarge-window (}). The window size is changed by the
;; number of columns specified in option binder-sidebar-resize-window-step.

;; You can customize how the sidebar window is displayed by setting
;; binder-sidebar-display-alist option.

;; ### Marking ###

;; Multiple items can be marked to add tags, toggle include state or
;; delete.

;; Call binder-sidebar-mark (m) to mark an item and binder-sidebar-unmark (u)
;; to unmark an item or binder-sidebar-unmark-all (U) for all sidebar items.

;; ### Tags ###

;; A project is strictly a linear list. As your project grows, you may find
;; the number of items becomes unweidly. Tags can help organize a project.
;; An item can have any number of tags.

;; Add a tag to an item with binder-sidebar-add-tag (t). Remove a tag
;; from an item with binder-sidebar-remove-tag (T). You can tag/untag
;; multiple items at once by using marks.

;; Items listed in the sidebar can be narrowed to only show items with a
;; certain tag with binder-sidebar-narrow-by-tag (/) and/or only show
;; items without a certain tag with binder-sidebar-exclude-by-tag (\).
;; Each of these commands can be called multiple times with additional
;; tags. Reset the sidebar with binder-sidebar-refresh (g).

;; ## Notes ##

;; Project items can have notes, which are stored within the project file.

;; To open the notes buffer from the sidebar, call either
;; binder-sidebar-open-notes (z) to (open and) select the notes window,
;; or binder-sidebar-toggle-notes (i) to toggle the window. To open a
;; project file's notes when visiting that file, call binder-toggle-notes
;; (C-c ").

;; > n.b. Notes are not automatically saved.

;; Calling quit-window (C-c C-q | C-c C-k) or binder-toggle-sidebar
;; does not save notes. You need to call either binder-notes-save (C-x C-s)
;; or binder-notes-save-and-quit-window (C-c C-c).

;; You can embiggen the notes window, to pop it out from the sidebar and
;; edit like a regular buffer window, with binder-notes-expand-window (C-c C-l).

;; If you want the notes buffer to stay in sync with the item under the
;; cursor in the sidebar, change the option binder-notes-keep-in-sync. This
;; can be disconcerting, and again, notes are not automatically saved!

;; You can customize how the notes window is displayed by setting
;; binder-notes-display-alist option.

;; ## Concatenate ##

;; A writing project written in discrete pieces probably has an end goal of
;; being put together. Each Binder project item has a property of being
;; "included" or not. In the sidebar, an item's include state is toggled
;; with binder-sidebar-toggle-include (x).

;; When calling binder-sidebar-concat (c | v), project items marked as
;; included will be concatenated in a new buffer (separated by
;; binder-concat-separator string.) The default mode of this buffer is set
;; by binder-default-concat-mode.

;; > Hint: you can use an alternate default mode for different projects by
;; > setting a directory local variable.

;; In this buffer, calling binder-concat-find-original (C-c RET) will
;; visit the original file corresponding to the text at point.

;; ## Why not just use Org Mode? ##

;; [Org Mode] is nice, but it's also a very *heavy* tool that almost
;; insists that everything be done within Org Mode. This isn't useful if
;; you want to write in a different format, e.g. [Markdown] or
;; [Fountain].

;; Also, I prefer to keep my writing in a collection of separate text
;; files. It feels nicer to work on something small and self-contained than
;; to organize a large file with headings and use indirect buffers with
;; narrowing.

;; [org mode]: https://orgmode.org
;; [markdown]: https://jblevins.org/projects/markdown-mode/
;; [fountain]: https://github.com/rnkn/fountain-mode

;; ## Requirements ##

;; - Emacs 24.4
;; - seq 2.20

;; ## Bugs and Feature Requests ##

;; Report bugs and feature requests at:
;; <https://github.com/rnkn/binder/issues>

;; ## Tutorial ##

;; Binder comes with a tutorial. Calling M-x binder-tutorial will prompt
;; for an empty directory in which to generate the tutorial files.


;;; Code:

(eval-when-compile (require 'cl-lib))
(eval-when-compile (require 'subr-x))
(require 'lisp-mnt)
(require 'seq)

(defgroup binder ()
  "Work with a structured project of files."
  :group 'files)


;;; Core Variables

(defvar binder-format-version 2)
(defvar binder-file-header
  (format "\
;; -*- coding: utf-8; -*-
;; Binder-Format-Version: %s
;; This is a Binder project file. It is meant to be human-readable, but you
;; probably shouldn't edit it.\n\n" binder-format-version)
  "Header information for project file.")

(defvar binder--cache nil)
(defvar binder--modification-time nil)
(defvar binder--modification-count 0)
(defvar binder-narrow-tags nil)
(defvar binder-exclude-tags nil)
(defvar binder-status-filter-in nil)
(defvar binder-status-filter-out nil)


;;; Options

(defcustom binder-mode-lighter
  " B/"
  "Mode-line indicator for `binder-mode'."
  :type '(choice (const :tag "No lighter" "") string)
  :safe 'stringp
  :group 'binder)

(defcustom binder-default-file
  ".binder.el"
  "Default file in which to store Binder project data."
  :type 'string
  :safe 'stringp
  :group 'binder)

(defcustom binder-project-directory
  nil
  "Directory containing current `binder-default-file' or nil.

The value set here will be used as default, but may be changed at
any time with `binder-change-directory'."
  :type '(choice (const nil) directory)
  :safe 'stringp
  :group 'binder)

(defcustom binder-default-file-extension
  "txt"
  "Default extension for new binder files."
  :type 'string
  :safe 'stringp
  :group 'binder)

(defcustom binder-save-threshold
  20
  "Integer of changes before binder file is automatically saved."
  :type 'integer
  :safe 'integerp
  :group 'binder)


;;; Faces

(defgroup binder-sidebar-faces ()
  "Default faces for `binder-sidebar-mode'."
  :group 'binder-sidebar)

(defface binder-sidebar-marked
  '((t (:inherit (warning))))
  "Default face marked items."
  :group 'binder-sidebar-faces)

(defface binder-sidebar-highlight
  '((t (:extend t :inherit (lazy-highlight))))
  "Default face for highlighted items."
  :group 'binder-sidebar-faces)

(defface binder-sidebar-missing
  '((t (:inherit (trailing-whitespace))))
  "Default face for missing items."
  :group 'binder-sidebar-faces)

(defface binder-sidebar-tags
  '((t (:inherit (font-lock-variable-name-face))))
  "Default face for tags."
  :group 'binder-sidebar-faces)


;;; Core Non-interactive Functions

(defun binder-root ()
  "Return the root directory with a project file, or nil."
  (locate-dominating-file default-directory binder-default-file))

(defun binder-set-modified ()
  "Set the project data as modified."
  (cl-incf binder--modification-count)
  (setq binder--modification-time (current-time)))

(defun binder-set-unmodified ()
  "Set the project data as unmodified."
  (setq binder--modification-count 0
        binder--modification-time (current-time)))

(defun binder-cd (directory)
  "Set `binder-project-directory' to DIRECTORY and erase cache."
  (customize-set-variable 'binder-project-directory directory)
  (setq binder-narrow-tags nil
        binder-exclude-tags nil
        binder--notes-fileid nil
        binder--cache nil)
  (binder-set-unmodified))

(defun binder-init ()
  "Initialize an empty project file in `binder-project-directory'."
  (with-temp-buffer
    (insert binder-file-header
            (pp-to-string nil))
    (write-file (expand-file-name binder-default-file binder-project-directory))))

(defun binder-find-project-file ()
  "Find or initialize current project file."
  (let ((binder-file
         (expand-file-name binder-default-file binder-project-directory)))
    (unless (file-exists-p binder-file) (binder-init))
    binder-file))

(defun binder-write ()
  "Write project data to file."
  (let ((binder-file (binder-find-project-file)))
    (with-temp-buffer
      (insert binder-file-header
              (pp-to-string binder--cache))
      (write-file binder-file)))
  (binder-set-unmodified))

(defun binder-write-maybe ()
  "Call `binder-write' if modified threshold has been reached.
Otherwise call `binder-set-modified'."
  (if (<= binder-save-threshold binder--modification-count)
      (binder-write)
    (binder-set-modified)))

(defun binder-filter (data)
  "Filter project DATA by tags.
Included `binder-narrow-tags' and excluded `binder-exclude-tags'."
  (seq-filter
   (lambda (item)
     (and (seq-every-p
           (lambda (tag)
             (member tag (cdr (assq 'tags item))))
           binder-narrow-tags)
          (seq-every-p
           (lambda (tag)
             (not (member tag (cdr (assq 'tags item)))))
           binder-exclude-tags)))
   data))

(defun binder-upgrade (data version)
  "Upgrade project DATA in VERSION to `binder-format-version'."
  (cond ((not (stringp version))
         (cdr (assq 'structure data)))
        ((= (string-to-number version) binder-format-version)
         data)))

(defun binder-read (&optional filter)
  "Read current project data.
Reads from `binder--cache' if valid, or from project file if not.
With optional argument FILTER, call `binder-filter' on data."
  (let ((binder-file (binder-find-project-file))
        version)
    (when binder--cache
      (cond
       ;; If there's no project file found, signal an error.
       ((null binder-file)
        (user-error "No binder file found"))
       ;; If the cache doesn't refer to the project directory, set the cache to
       ;; nil.
       ((not (file-equal-p binder-project-directory (binder-root)))
        (setq binder--cache nil))
       ;; If the project file is newer than the cache, offer to revert from disk
       ;; (and write binder data), regardless, set the cache to nil.
       ((time-less-p binder--modification-time
                     (elt (file-attributes binder-file) 5))
        (unless (y-or-n-p "Project file changed on disk; revert from disk? ")
          (binder-write))
        (setq binder--cache nil))))
    ;; If the cache survived all that, it's valid, otherwise, read from the
    ;; project file.
    (unless binder--cache
      (with-temp-buffer
        (insert-file-contents binder-file)
        ;; Read Binder-Format-Version header
        (setq version (lm-header "binder-format-version"))
        (goto-char (point-min))
        (setq binder--cache
              (binder-upgrade (read (current-buffer)) version)))
      (binder-set-unmodified)))
  ;; Finally, return the cache.
  (if filter (binder-filter binder--cache) binder--cache))

(defun binder-ensure-in-project ()
  "Ensure the current file or directory is in the project."
  (let ((root (binder-root)))
    (cond
     ;; The project directory matches root, we're all good.
     ((and (stringp binder-project-directory)
           (stringp root)
           (file-equal-p binder-project-directory root))
      t)
     ;; The project directory does not match project root; offer to change it to
     ;; current project root.
     ((and (stringp binder-project-directory)
           (stringp root))
      (when (y-or-n-p (format "Outside of current project %s\nSwitch project directory to %s? "
                              binder-project-directory root))
        (binder-cd root)))
     ;; The project directory is set but we're not in a project; this is fine.
     ((stringp binder-project-directory)
      t)
     ;; The project directory is not set, but we're in a project; offer to set
     ;; it to current project root.
     ((stringp root)
      (binder-cd root)
      (message "Set binder directory to %s" root))
     ;; A fresh project; offer to set project directory to default-directory.
     (t
      (when (y-or-n-p (format "Set binder directory to %s? "
                              (abbreviate-file-name default-directory)))
        (binder-cd default-directory)
        (binder-init))))))

(defun binder-file-relative-to-root (filepath)
  "Return FILEPATH relative to binder root directory."
  (string-remove-prefix (expand-file-name (or binder-project-directory ""))
                        (expand-file-name filepath)))

(defun binder-get-item (fileid)
  "Return project item association list for FILEID."
  (assoc-string fileid (binder-read t)))

(defun binder-get-item-prop (fileid prop)
  "Return value of PROP for project item with FILEID."
  (cdr (assq prop (cdr (binder-get-item fileid)))))

(defun binder-set-item-prop (fileid prop value)
  "Set VALUE of PROP for project item with FILEID."
  (let ((item (binder-get-item fileid)))
    (if (or (null value) (and (stringp value) (string-empty-p value)))
        (setf item (assq-delete-all prop item))
      (let ((prop-elt (assq prop item)))
        (if prop-elt
            (setcdr prop-elt value)
          (push (cons prop value) (cdr item)))))))

(defun binder-add-to-item-prop (fileid prop value)
  "Add VALUE to PROP for project item with FILEID."
  (let ((prop-elt (binder-get-item-prop fileid prop)))
    (unless (member value prop-elt)
      (binder-set-item-prop fileid prop (push value prop-elt)))))

(defun binder-remove-from-item-prop (fileid prop value)
  "Remove VALUE from PROP for project item with FILEID."
  (let ((prop-elt (binder-get-item-prop fileid prop)))
    (when (member value (binder-get-item-prop fileid 'tags))
      (binder-set-item-prop fileid prop (remove value prop-elt)))))

(defun binder-get-item-index (fileid &optional filter)
  "Return index position for project item for FILEID.
With option argument FILTER, return index when items are
filtered."
  (seq-position (binder-read filter) (binder-get-item fileid)))

(defun binder-insert-item (item index)
  "Insert project ITEM at position INDEX."
  (unless (listp item) (setq item (list item)))
  (setq binder--cache
       (let ((structure (binder-read)))
         (append (seq-take structure index)
                 (cons item (seq-drop structure index))))))

(defun binder-delete-item (fileid)
  "Delete project item with FILEID."
  (setq binder--cache
        (remove (binder-get-item fileid) (binder-read))))

(defun binder-get-prop-list (prop)
  "Return list of values for PROP."
  (delq nil
        (mapcar
         (lambda (item)
           (let ((value (cdr (assq prop item))))
             (when (and (stringp value) (< 0 (string-width value)))
               value)))
         (binder-read))))

(defun binder-get-tags (&optional filter)
  "Return current project tags.
Optionally pass FILTER to `binder-read'."
(let (tags)
    (mapc
     (lambda (item)
       (mapc
        (lambda (tag)
          (push tag tags))
        (cdr (assq 'tags item))))
     (binder-read filter))
    (seq-uniq tags 'string-equal)))

(defun binder-get-buffer-fileid ()
  "Return buffer binder fileid."
   (cond ((eq major-mode 'binder-sidebar-mode)
          binder--current-fileid)
         ((eq major-mode 'binder-notes-mode)
          binder--notes-fileid)
         (t
          (binder-file-relative-to-root
           (or (buffer-file-name) default-directory)))))


;;; Global Minor Mode

(defun binder-init-project (directory)
  "Initialize empty project in DIRECTORY."
  (interactive "DInitialize empty project in directory: ")
  (binder-cd directory)
  (binder-init))

(defun binder-save (&optional prompt)
  "Save project data (with prompt when PROMPT is non-nil)."
  (interactive)
  (cond ((= 0 binder--modification-count)
         (message "(No changes need to be saved)"))
        ((and prompt
              (y-or-n-p (format "Save binder project %s? "
                                (abbreviate-file-name binder-project-directory))))
         (binder-write))
        (t
         (binder-write))))

(defun binder-exit-hook ()
  "Ensure project data is saved on exit."
  (when binder-mode (binder-save 'prompt)))

(defun binder-change-directory (directory)
  "Change to binder project directory DIRECTORY."
  (interactive "DDirectory: ")
  (binder-save 'prompt)
  (binder-cd directory)
  (binder-sidebar-refresh-window)
  (binder-notes-refresh-window))

(defun binder-next (&optional n)
  "Visit Nth next file in binder.
Or visit Nth previous file if N is negative."
  (interactive "p")
  (when (binder-ensure-in-project)
    ;; Find the current file/directory fileid, if one.
    (let ((this-fileid (binder-get-buffer-fileid))
          (structure (binder-read t))
          index next-index)
      ;; If current file has an INDEX, get the NEXT-INDEX.
      (setq index (or (binder-get-item-index this-fileid t) 0)
            next-index (+ index n))
      ;; If NEXT-INDEX is within the filtered structure length, find the
      ;; Nth next/previous file.
      (if (not (<= 0 next-index (1- (length structure))))
          (message "End of binder")
        (find-file-existing
         (expand-file-name (car (elt structure next-index))
                           binder-project-directory))
        (binder-sidebar-refresh-window))
      ;; Setup the overriding keymap.
      (unless overriding-terminal-local-map
        (let ((prefix-keys (substring (this-single-command-keys) 0 -1))
              (map (cdr binder-navigation-map)))
          (when (< 0 (length prefix-keys))
            (mapc (lambda (k) (setq map (assq k map))) prefix-keys)
            (setq map (cdr-safe map))
            (when (keymapp map) (set-transient-map map t))))))))

(defun binder-previous (&optional n)
  "Visit Nth previous file in binder.
Or visit Nth next file if N is negative."
  (interactive "p")
  (binder-next (- n)))

(defun binder-add-file (fileid &optional index string)
  "Add a (possibly non-existent) FILEID at INDEX containing STRING.
If the current file is in the project, add at index after that
one, otherwise insert at end."
  (interactive "FAdd file (extension optional): ")
  (binder-ensure-in-project)
  (setq fileid (binder-file-relative-to-root fileid))
  (unless (< 0 (string-width fileid))
    (user-error "No file name supplied"))
  ;; If FILEID is not a directory and no file extension was provided,
  ;; add the binder's default file extensions.
  (unless (or (file-directory-p fileid)
              (and (string-match "\\.[^.]+\\'" fileid)
                   (not (= 0 (match-beginning 0)))))
    (setq fileid
          (concat fileid "." binder-default-file-extension)))
  ;; If the file/directory does not exist, create it.
  (let ((filepath (expand-file-name fileid binder-project-directory)))
    (unless (file-exists-p filepath)
      (if (file-directory-p filepath)
          (make-directory filepath t)
        (with-current-buffer (find-file-noselect filepath)
          (when (stringp string) (insert string))
          (write-file filepath))))
    ;; Insert FILEID into binder at INDEX, or after current file.
    (unless (binder-get-item fileid)
      (unless index
        (let ((this-file-index
               (binder-get-item-index (binder-get-buffer-fileid) t)))
          (setq index (if this-file-index
                          (1+ this-file-index)
                        (length (binder-read))))))
      (binder-insert-item fileid index))
    (binder-write-maybe)
    ;; When binder sidebar is active, refresh it.
    (binder-sidebar-refresh-window)
    ;; Finally, visit the file FILEPATH.
    (let ((pop-up-windows binder-sidebar-pop-up-windows))
      (find-file filepath))))

(defun binder-extract-region-to-new-file (beg end fileid)
  "Extract region between BEG and END into new project file FILEID."
  (interactive "r\nFNew file name (extension optional): ")
  (binder-ensure-in-project)
  (let ((string (delete-and-extract-region beg end)))
    (binder-add-file fileid nil string)))

(defvar binder-navigation-map
  (let ((map (make-sparse-keymap)))
    (define-key map (kbd "C-c ]") #'binder-next)
    (define-key map (kbd "C-c [") #'binder-previous)
    map)
  "Navigational mode map for `binder-mode'.")

(defvar binder-mode-map
  (let ((map (make-sparse-keymap)))
    (define-key map (kbd "C-c ;") #'binder-reveal-in-sidebar)
    (define-key map (kbd "C-c '") #'binder-toggle-sidebar)
    (define-key map (kbd "C-c \"") #'binder-toggle-notes)
    (define-key map (kbd "C-c :") #'binder-add-file)
    map)
  "Main mode map for `binder-mode'.")

(set-keymap-parent binder-mode-map binder-navigation-map)

;;;###autoload
(define-minor-mode binder-mode
  "Globally interact with `binder'."
  :init-value nil
  :lighter binder-mode-lighter
  :global t
  (if binder-mode
      (unless noninteractive
        (add-hook 'kill-emacs-hook #'binder-exit-hook)
        (add-hook 'window-configuration-change-hook #'binder-highlight-in-sidebar))
    (binder-save 'prompt)
    (remove-hook 'window-configuration-change-hook #'binder-highlight-in-sidebar)
    (when (window-live-p (get-buffer-window binder-sidebar-buffer))
      (with-selected-window (get-buffer-window binder-sidebar-buffer)
        (quit-window t)))
    (when (window-live-p (get-buffer-window binder-notes-buffer))
      (with-selected-window (get-buffer-window binder-notes-buffer)
        (quit-window t)))
    (setq binder--cache nil)))


;;; Sidebar Major Mode

(defgroup binder-sidebar ()
  "Options for `binder-sidebar-mode'."
  :group 'binder)

(defface binder-sidebar
  '((t nil))
  "Default base-level face for `binder-sidebar-mode' buffers.")

(defcustom binder-sidebar-buffer "*Binder Sidebar*"
  "Default buffer name for binder sidebar."
  :type 'string
  :safe 'stringp
  :group 'binder-sidebar)

(defcustom binder-sidebar-display-alist
  '((side . left)
    (window-width . 40)
    (slot . -1))
  "Association list used to display binder sidebar buffer.

See `display-buffer-in-side-window' for example options."
  :type 'alist
  :safe (lambda (value)
          (and (listp value)
               (seq-every-p 'consp value)))
  :group 'binder-sidebar)

(defcustom binder-sidebar-hide-file-extensions
  nil
  "When non-nil, list items without file extension."
  :type 'boolean
  :safe 'booleanp
  :group 'binder-sidebar)

(defcustom binder-sidebar-hide-tags
  nil
  "When non-nil, list items without tags."
  :type 'boolean
  :safe 'booleanp
  :group 'binder-sidebar)

(defcustom binder-sidebar-pop-up-windows
  nil
  "Non-nil means displaying a new buffer should make a new window."
  :type 'boolean
  :safe 'booleanp
  :group 'binder-sidebar)

(defcustom binder-sidebar-tags-column
  25
  "Integer for column to align tags."
  :type 'integer
  :safe 'integerp
  :group 'binder-sidebar)

(defcustom binder-sidebar-include-char
  ?x
  "Character to display on items including in joining."
  :type 'character
  :safe 'characterp
  :group 'binder-sidebar)

(defcustom binder-sidebar-notes-char
  ?*
  "Character to display on items with notes."
  :type 'character
  :safe 'characterp
  :group 'binder-sidebar)

(defcustom binder-sidebar-tags-char
  ?#
  "Character to prefix to item tags."
  :type 'character
  :safe 'characterp
  :group 'binder-sidebar)

(defcustom binder-sidebar-missing-char
  ??
  "Character to display on items with missing files."
  :type 'character
  :safe 'characterp
  :group 'binder-sidebar)

(defcustom binder-sidebar-select-window
  nil
  "If non-nil, switch to binder sidebar upon displaying it."
  :type 'boolean
  :safe 'booleanp
  :group 'binder-sidebar)

(defcustom binder-sidebar-persistent-window
  t
  "When non-nil, sidebar will persist when calling `delete-other-windows'.

This marks `no-delete-other-windows' window parameter as non-nil.

Use `binder-toggle-sidebar' or `quit-window' to close the sidebar."
  :type 'boolean
  :safe 'booleanp
  :group 'binder-sidebar)

(defcustom binder-sidebar-resize-window-step
  4
  "Integer of columns by which to resize window.
Used by `binder-sidebar-shrink-window' and `binder-sidebar-enlarge-window'."
  :type 'integer
  :safe 'integerp
  :group 'binder-sidebar)

(defvar binder--current-fileid nil)
(defvar binder--sidebar-marked nil)

(defun binder-sidebar-format-header-line ()
  "Format an appropriate window header-line for sidebar."
  (setq header-line-format
        (list :propertize (abbreviate-file-name binder-project-directory)
              'face 'bold)))

(defun binder-sidebar-refresh (&optional clear-filter)
  "Redraw binder sidebar, reading from cache.
When called interactively (or with optional CLEAR-FILTER) clear
filter by tags."
  (interactive "p")
  (with-silent-modifications
    (setq default-directory binder-project-directory)
    ;; FIXME: this can cause a segfault
    ;; (hack-local-variables)
    (binder-sidebar-format-header-line)
    (when clear-filter (setq binder-narrow-tags nil
                             binder-exclude-tags nil))
    (let ((x (point)))
      (erase-buffer)
      (mapc
       (lambda (item)
         (let ((fileid   (car item))
               (include  (cdr (assq 'include item)))
               (display  (cdr (assq 'display item)))
               (notes    (cdr (assq 'notes item)))
               (tags     (cdr (assq 'tags item)))
               marked missing tags-overwrite)
           ;; Set whether FILEID is MARKED and MISSING.
           (when (member fileid binder--sidebar-marked)
             (setq marked t))
           (when (not (file-exists-p fileid))
             (setq missing t))
          ;; Insert the item line elements.
          (insert (cond (marked ">")
                        (include binder-sidebar-include-char)
                        (t " "))
                  " "
                  (cond (missing binder-sidebar-missing-char)
                        ((and notes (not (string-empty-p notes)))
                         binder-sidebar-notes-char)
                        (t " "))
                  " "
                  ;; Use either DISPLAY, or if directory ensure a
                  ;; trailing slash, and finally if we're hiding file
                  ;; extensions, do that, otherwise just the FILEID is
                  ;; fine.
                  (or display
                      (if (file-directory-p fileid)
                          (replace-regexp-in-string "/*$" "/" fileid)
                        (if binder-sidebar-hide-file-extensions
                            (replace-regexp-in-string ".+\\(\\..+\\)" ""
                                                      fileid nil nil 1)
                          fileid))))
          ;; Add the face properties. Make them front-sticky since we
          ;; were previously editing the buffer text (but not anymore).
          (put-text-property (line-beginning-position) (line-end-position)
                             'binder-fileid fileid)
          (put-text-property (line-beginning-position) (line-end-position)
                             'front-sticky '(binder-fileid))
          (when missing
            (put-text-property (line-beginning-position) (line-end-position)
                               'face 'binder-sidebar-missing))
          (when marked
            (put-text-property (line-beginning-position) (line-end-position)
                               'face 'binder-sidebar-marked))
          ;; Add the item TAGS with a hashtag, because hashtags are cool, right?
          (when (and (not binder-sidebar-hide-tags) tags)
            (move-to-column (1- binder-sidebar-tags-column))
            (unless (eolp) (setq tags-overwrite t))
            (move-to-column binder-sidebar-tags-column)
            (indent-to-column binder-sidebar-tags-column)
            (let ((tags-col  (point))
                  (tags-char (char-to-string binder-sidebar-tags-char)))
              (delete-region (1- tags-col) (line-end-position))
              (insert (if tags-overwrite "~" " ")
                      tags-char
                      (string-join tags (concat " " tags-char)))
              (put-text-property tags-col (line-end-position)
                                 'face 'binder-sidebar-tags)))
          (insert "\n")
          (when (string= fileid binder--current-fileid)
            (put-text-property (line-beginning-position 0)
                               (line-beginning-position)
                               'face 'binder-sidebar-highlight))))
       (binder-read t))
      (goto-char x))))

(defun binder-sidebar-refresh-window ()
  "Call `binder-sidebar-refresh' if sidebar window is live."
  (when (window-live-p (get-buffer-window binder-sidebar-buffer))
    (with-current-buffer binder-sidebar-buffer
      (binder-sidebar-refresh))))

(defalias 'binder-sidebar-change-directory #'binder-change-directory)

(defun binder-sidebar-create-buffer ()
  "Return binder sidebar buffer for DIRECTORY."
  (binder-ensure-in-project)
  (with-current-buffer (get-buffer-create binder-sidebar-buffer)
    (binder-sidebar-mode)
    (binder-sidebar-refresh)
    (current-buffer)))

(defun binder-sidebar-create-window ()
  "Return binder sidebar window for DIRECTORY.
Defaults to current directory."
  (let ((display-buffer-mark-dedicated t))
    (display-buffer-in-side-window
     (binder-sidebar-create-buffer)
     (append binder-sidebar-display-alist
             (when binder-sidebar-persistent-window
               (list '(window-parameters (no-delete-other-windows . t))))))))

(defun binder-sidebar-get-fileid ()
  "Return fileid for item at point."
  (save-excursion
    (if (eobp) (forward-line -1) (beginning-of-line))
    (get-text-property (point) 'binder-fileid)))

(defun binder-sidebar-goto-item (fileid)
  "Move point to project item with FILEID."
  (goto-char (point-min))
  ;; It would be nice to use find-next-text-property but that isn't
  ;; available until Emacs 27.
  (let (found)
    (while (not (or (eobp) found))
      (if (string= (binder-sidebar-get-fileid) fileid)
          (setq found t)
        (forward-line 1)))))

(defun binder-sidebar-find-file (arg)
  "Visit project file at point.
When ARG is non-nil, visit in new window."
  (interactive "P")
  (let ((pop-up-windows (or arg binder-sidebar-pop-up-windows))
        (fileid (binder-sidebar-get-fileid))
        filepath)
    (setq filepath (expand-file-name fileid))
    (when (file-exists-p filepath)
      (setq binder--current-fileid fileid)
      (binder-sidebar-refresh)
      (find-file filepath))))

(defun binder-sidebar-find-file-other-window ()
  "Visit project item at point in other window."
  (interactive)
  (binder-sidebar-find-file t))

(defalias 'binder-sidebar-save #'binder-save)

(defun binder-sidebar-get-index ()
  "Return binder index position at point."
  (if (eobp)
      (1- (length (binder-read)))
    (binder-get-item-index (binder-sidebar-get-fileid) t)))

(defun binder-sidebar-mark ()
  "Mark the project item at point."
  (interactive)
  (cl-pushnew (binder-sidebar-get-fileid) binder--sidebar-marked)
  (forward-line 1)
  (binder-sidebar-refresh))

(defun binder-sidebar-unmark ()
  "Unmark the project item at point."
  (interactive)
  (setq binder--sidebar-marked
        (remove (binder-sidebar-get-fileid) binder--sidebar-marked))
  (forward-line 1)
  (binder-sidebar-refresh))

(defun binder-sidebar-unmark-all ()
  "Unmark all project items."
  (interactive)
  (setq binder--sidebar-marked nil)
  (binder-sidebar-refresh))

(defun binder-sidebar-add-file (fileid)
  "Add (possibly non-existent) file to binder as FILEID."
  (interactive "FAdd file: ")
  (setq fileid (binder-file-relative-to-root fileid))
  (unless (binder-get-item fileid)
    (binder-insert-item fileid (1+ (binder-sidebar-get-index))))
  (binder-write-maybe)
  (binder-sidebar-refresh)
  (binder-sidebar-goto-item fileid))

(defun binder-sidebar-add-all-files ()
  "Add all files in current directory to binder."
  (interactive)
  (when (y-or-n-p (format "Add all files in %s? "
                          (abbreviate-file-name default-directory)))
    (dolist (file (directory-files default-directory nil "^[^.]"))
      (binder-sidebar-add-file file))))

(defun binder-sidebar-new-file (fileid)
  "Add a new file to binder as FILEID and visit it."
  (interactive "FAdd file (extension optional): ")
  (unless (eq major-mode 'binder-sidebar-mode)
    (user-error "Not in %S" 'binder-sidebar-mode))
  (binder-add-file fileid (1+ (binder-sidebar-get-index)))
  (binder-sidebar-goto-item fileid))

(defun binder-sidebar-remove (arg)
  "Remove marked items or item at point.
When ARG is non-nil, do not prompt for confirmation."
  (interactive "P")
  (let ((fileid-list (or binder--sidebar-marked
                      (list (binder-sidebar-get-fileid)))))
    (when (or arg (y-or-n-p (format "Really remove %s? "
                                    (string-join fileid-list ", "))))
      (mapc #'binder-delete-item fileid-list)
      (setq binder--sidebar-marked nil)))
  (binder-write)
  (binder-sidebar-refresh))

(defun binder-sidebar-rename ()
  "Change display name of project item at point."
  (interactive)
  (let ((fileid (binder-sidebar-get-fileid))
        name)
    (setq name
          (read-string "New name: "
                       (or (binder-get-item-prop fileid 'display) fileid)))
    (binder-set-item-prop fileid 'display name)
    (binder-write-maybe)
    (binder-sidebar-refresh)))

(defun binder-sidebar-relocate (filepath)
  "Change file path of project item at point to FILEPATH."
  (interactive "fNew file path: ")
  (setq filepath (binder-file-relative-to-root filepath))
  (setcar (binder-get-item (binder-sidebar-get-fileid)) filepath)
  (binder-write-maybe)
  (binder-sidebar-refresh))

(defun binder-sidebar-toggle-include ()
  "Toggle whether marked items or item at point is included in `binder-sidebar-concat'."
  (interactive)
  (dolist (fileid (or binder--sidebar-marked
                      (list (binder-sidebar-get-fileid))))
    (binder-set-item-prop fileid 'include
                          (not (binder-get-item-prop fileid 'include))))
  (setq binder--sidebar-marked nil)
  (binder-write-maybe)
  (binder-sidebar-refresh))

(defun binder-sidebar-clear-include ()
  "Make no items included in `binder-sidebar-concat'."
  (interactive)
  (dolist (item (binder-read t))
    (binder-set-item-prop (car item) 'include nil))
  (binder-write-maybe)
  (binder-sidebar-refresh))

(defun binder-sidebar-add-tag (tag)
  "Add TAG to marked items or item at point."
  (interactive
   (list (completing-read
          "Add tag: " (binder-get-tags))))
  (mapc
   (lambda (fileid)
     (binder-add-to-item-prop fileid 'tags tag))
   (or binder--sidebar-marked
       (list (binder-sidebar-get-fileid))))
  (setq binder--sidebar-marked nil)
  (binder-write-maybe)
  (binder-sidebar-refresh))

(defun binder-sidebar-remove-tag (tag)
  "Remove TAG to marked items or item at point."
  (interactive
   (list (completing-read
          "Remove tag: "
          (binder-get-item-prop (binder-sidebar-get-fileid) 'tags))))
  (mapc
   (lambda (fileid)
     (binder-remove-from-item-prop fileid 'tags tag))
   (or binder--sidebar-marked
       (list (binder-sidebar-get-fileid))))
  (setq binder--sidebar-marked nil)
  (binder-write-maybe)
  (binder-sidebar-refresh))

(defun binder-sidebar-toggle-file-extensions ()
  "Toggle visibility of item file extensions."
  (interactive)
  (customize-set-variable 'binder-sidebar-hide-file-extensions
                          (not binder-sidebar-hide-file-extensions))
  (let ((fileid (binder-sidebar-get-fileid)))
    (binder-sidebar-refresh)
    (binder-sidebar-goto-item fileid))
  (message "%s file extensions"
           (capitalize
            (if binder-sidebar-hide-file-extensions
                "hiding" "showing"))))

(defun binder-sidebar-toggle-tags ()
  "Toggle visibility of tags."
  (interactive)
  (customize-set-variable 'binder-sidebar-hide-tags
                          (not binder-sidebar-hide-tags))
  (let ((fileid (binder-sidebar-get-fileid)))
    (binder-sidebar-refresh)
    (binder-sidebar-goto-item fileid))
  (message "%s tags"
           (capitalize
            (if binder-sidebar-hide-tags
                "hiding" "showing"))))

;; FIXME: fails with filtered structure
(defun binder-sidebar-shift-down (&optional n)
  "Shift index position of item at point down N places in list."
  (interactive "p")
  (let ((p (if (<= n 0) -1 1))
        (fileid (binder-sidebar-get-fileid))
        item fake-index next-index)
    (setq item (binder-get-item fileid)
          fake-index (binder-get-item-index fileid t)
          next-index (seq-position (binder-read)
                                   (elt (binder-read t) (+ fake-index p))))
    (when (<= 0 (+ fake-index p)
              (1- (length (binder-read t))))
      (binder-delete-item fileid)
      (binder-insert-item item next-index)
      (binder-write-maybe)
      (binder-sidebar-refresh)
      (binder-sidebar-goto-item fileid))))

(defun binder-sidebar-shift-up (&optional n)
  "Shift index position of item at point up N places in list."
  (interactive "p")
  (binder-sidebar-shift-down (- n)))

(defun binder-sidebar-jump-to-current ()
  "Jump to current file in sidebar."
  (interactive)
  (binder-sidebar-goto-item binder--current-fileid))

(defun binder-sidebar-narrow-by-tag (tag)
  "\\<binder-sidebar-mode-map>Filter sidebar items to include items with TAG.
To reset filtering call `binder-sidebar-refresh' (\\[binder-sidebar-refresh])."
  (interactive
   (list (completing-read
          "Narrow items by tag: " (binder-get-tags)
          nil t)))
  (unless (or (string-empty-p tag) (member tag binder-narrow-tags))
    (push tag binder-narrow-tags)
    (binder-sidebar-refresh)))

(defun binder-sidebar-exclude-by-tag (tag)
  "\\<binder-sidebar-mode-map>Filter sidebar items to exclude items with TAG.
To reset filtering call `binder-sidebar-refresh' (\\[binder-sidebar-refresh])."
  (interactive
   (list (completing-read
          "Exclude items by tag: " (binder-get-tags)
          nil t)))
  (unless (or (string-empty-p tag) (member tag binder-exclude-tags))
    (push tag binder-exclude-tags)
    (binder-sidebar-refresh)))

(defun binder-highlight-in-sidebar ()
  "Highlight the current file in sidebar.

Added to `window-configuration-change-hook'."
  (unless (or (minibuffer-window-active-p (selected-window))
              (eq major-mode 'binder-sidebar-mode))
    (setq binder--current-fileid (binder-get-buffer-fileid))
    (binder-sidebar-refresh-window)))

(defun binder-sidebar-shrink-window ()
  "Shrink the sidebar window horizontally.

Calls `shrink-window-horizontally' with `binder-sidebar-resize-window-step'."
  (interactive)
  (shrink-window-horizontally binder-sidebar-resize-window-step))

(defun binder-sidebar-enlarge-window ()
  "Enlarge the sidebar window horizontally.

Calls `enlarge-window-horizontally' with `binder-sidebar-resize-window-step'."
  (interactive)
  (enlarge-window-horizontally binder-sidebar-resize-window-step))

(defun binder-sidebar-help (char)
  "Interactively set project-specific properties by CHAR."
  (declare (interactive-only t))
  (interactive
   (list (read-char-choice "\
? = describe-mode, g = refresh (clear filters), q = quit-window, C-g = cancel: "
                           '(?? ?g ?q))))
  (cl-case char
    (?q (quit-window))
    (?g (binder-sidebar-refresh t))
    (?? (describe-mode))))

;;;###autoload
(defun binder-reveal-in-sidebar ()
  "Reveal current file in binder sidebar."
  (interactive)
  (let ((filepath (or (buffer-file-name) default-directory)))
    (select-window (binder-sidebar-create-window))
    (if (file-equal-p filepath (binder-root))
        (binder-sidebar-refresh)
      (let ((fileid (binder-file-relative-to-root filepath)))
        (setq binder--current-fileid fileid)
        (unless (binder-get-item fileid)
          (when (y-or-n-p (format "Add %s to binder? " fileid))
            (binder-sidebar-add-file fileid)))
        (binder-sidebar-refresh)
        (binder-sidebar-goto-item fileid)))))

;;;###autoload
(defun binder-toggle-sidebar ()
  "Toggle visibility of project sidebar window.

Unconditionally activates `binder-mode'."
  (interactive)
  (binder-mode)
  (if (window-live-p (get-buffer-window binder-sidebar-buffer))
      (delete-window (get-buffer-window binder-sidebar-buffer))
    (binder-sidebar-create-window)
    (when binder-sidebar-select-window
      (select-window (get-buffer-window binder-sidebar-buffer)))))

(defvar binder-sidebar-mode-map
  (let ((map (make-sparse-keymap)))
    (define-key map (kbd "?") #'binder-sidebar-help)
    (define-key map (kbd "{") #'binder-sidebar-shrink-window)
    (define-key map (kbd "}") #'binder-sidebar-enlarge-window)
    (define-key map (kbd "g") #'binder-sidebar-refresh)
    (define-key map (kbd "j") #'binder-sidebar-jump-to-current)
    (define-key map (kbd "C") #'binder-sidebar-change-directory)
    (define-key map (kbd "n") #'next-line)
    (define-key map (kbd "p") #'previous-line)
    (define-key map (kbd "RET") #'binder-sidebar-find-file)
    (define-key map (kbd "o") #'binder-sidebar-find-file-other-window)
    (define-key map (kbd "s") #'binder-sidebar-save)
    (define-key map [remap save-buffer] #'binder-sidebar-save)
    (define-key map (kbd "m") #'binder-sidebar-mark)
    (define-key map (kbd "u") #'binder-sidebar-unmark)
    (define-key map (kbd "t") #'binder-sidebar-add-tag)
    (define-key map (kbd "T") #'binder-sidebar-remove-tag)
    (define-key map (kbd "#") #'binder-sidebar-toggle-tags)
    (define-key map (kbd "U") #'binder-sidebar-unmark-all)
    (define-key map (kbd "c") #'binder-sidebar-concat)
    (define-key map (kbd "v") #'binder-sidebar-concat)
    (define-key map (kbd "i") #'binder-sidebar-toggle-notes)
    (define-key map (kbd "z") #'binder-sidebar-open-notes)
    (define-key map (kbd "M-n") #'binder-sidebar-shift-down)
    (define-key map (kbd "<M-down>") #'binder-sidebar-shift-down)
    (define-key map (kbd "M-p") #'binder-sidebar-shift-up)
    (define-key map (kbd "<M-up>") #'binder-sidebar-shift-up)
    (define-key map (kbd "a") #'binder-sidebar-add-file)
    (define-key map (kbd "A") #'binder-sidebar-add-all-files)
    (define-key map (kbd "d") #'binder-sidebar-remove)
    (define-key map (kbd "r") #'binder-sidebar-rename)
    (define-key map (kbd "R") #'binder-sidebar-relocate)
    (define-key map (kbd "E") #'binder-sidebar-toggle-file-extensions)
    (define-key map (kbd "x") #'binder-sidebar-toggle-include)
    (define-key map (kbd "X") #'binder-sidebar-clear-include)
    (define-key map (kbd "/") #'binder-sidebar-narrow-by-tag)
    (define-key map (kbd "\\") #'binder-sidebar-exclude-by-tag)
    (define-key map (kbd "M-RET") #'binder-sidebar-new-file)
    map))

;;;###autoload
(define-derived-mode binder-sidebar-mode
  special-mode "Binder Sidebar"
  "Major mode for working with `binder' projects."
  (face-remap-add-relative 'default 'binder-sidebar)
  (add-hook 'post-command-hook #'binder-sidebar-sync-notes t t))


;;; Notes Major Mode

(defface binder-notes
  '((t nil))
  "Default base-level face for `binder-notes-mode' buffers.")

(defcustom binder-notes-buffer
  "*Binder Notes*"
  "Buffer name for Binder notes."
  :type 'string
  :safe 'stringp
  :group 'binder-sidebar)

(defcustom binder-notes-display-alist
  '((side . left)
    (window-width . 40)
    (slot . 1))
  "Association list used to display binder notes buffer.

See `display-buffer-in-side-window' for example options."
  :type 'alist
  :group 'binder-sidebar)

(defcustom binder-notes-persistent-window
  t
  "When non-nil, notes will persist when calling `delete-other-windows'.

This marks `no-delete-other-windows' window parameter as non-nil.

Use `binder-toggle-notes' or `quit-window' to close notes."
  :type 'boolean
  :safe 'booleanp
  :group 'binder-sidebar)

(defcustom binder-notes-keep-in-sync
  nil
  "If non-nil, moving point in binder sidebar updates notes.

Enabling this can be disconcerting, because notes are not
automatically saved."
  :type 'boolean
  :safe 'booleanp
  :group 'binder-sidebar)

(defcustom binder-notes-mode-hook
  '(visual-line-mode)
  "Hook run after entering Binder Notes Mode."
  :type 'hook
  :options '(visual-line-mode))

(defvar binder--notes-fileid nil)
(defvar binder--notes-display nil)

(defun binder-notes-refresh ()
  "Redraw the notes buffer."
  (setq default-directory binder-project-directory)
  (with-silent-modifications
    (erase-buffer)
    (insert (or (binder-get-item-prop binder--notes-fileid 'notes)
                ""))
    (setq binder--notes-display
          (binder-get-item-prop binder--notes-fileid 'display))
    (setq header-line-format
          (if binder--notes-fileid
              (list (list :propertize (or binder--notes-display binder--notes-fileid)
                          'face 'bold)
                    "  C-c C-c to commit; C-c C-q to quit")
            "Nothing selected; C-c C-q to quit"))))

(defun binder-notes-refresh-window ()
  "Call `binder-notes-refresh' if notes window is live."
  (when (window-live-p (get-buffer-window binder-notes-buffer))
    (with-current-buffer binder-notes-buffer
      (binder-notes-refresh))))

(defun binder-notes-create-buffer ()
  "Create the notes buffer."
  (binder-ensure-in-project)
  (with-current-buffer (get-buffer-create binder-notes-buffer)
    (binder-notes-mode)
    (binder-notes-refresh)
    (current-buffer)))

(defun binder-notes-create-window ()
  "Create the notes window."
  (let ((display-buffer-mark-dedicated t))
    (display-buffer-in-side-window
     (binder-notes-create-buffer)
     (append binder-notes-display-alist
             (when binder-sidebar-persistent-window
               (list '(window-parameters (no-delete-other-windows . t))))))))

(defun binder-show-notes (&optional fileid select)
  "Show the notes for FILEID of the appropriate project item.
If argument SELECT is non-nil, select the notes window."
  (binder-ensure-in-project)
  (if fileid
      (setq binder--notes-fileid fileid)
    (let ((filepath (or (buffer-file-name) default-directory)))
      (unless (file-equal-p filepath (binder-root))
        (setq binder--notes-fileid (binder-file-relative-to-root filepath)))))
  (if (window-live-p (get-buffer-window binder-notes-buffer))
      (binder-notes-refresh-window)
    (binder-notes-create-window))
  (when select (select-window (get-buffer-window binder-notes-buffer))))

(defun binder-sidebar-open-notes ()
  "Open notes for item at point and select the notes window."
  (interactive)
  (binder-show-notes (binder-sidebar-get-fileid) t))

(defun binder-toggle-notes ()
  "Toggle visibility of binder notes window."
  (interactive)
  (if (window-live-p (get-buffer-window binder-notes-buffer))
      (delete-window (get-buffer-window binder-notes-buffer))
    (binder-show-notes)))

(defun binder-sidebar-toggle-notes ()
  "Toggle visibility of binder notes window for item at point."
  (interactive)
  (if (window-live-p (get-buffer-window binder-notes-buffer))
      (delete-window (get-buffer-window binder-notes-buffer))
    (binder-show-notes (binder-sidebar-get-fileid))))

(defun binder-notes-save ()
  "Save notes buffer content to project.

This command writes project data to disk."
  (interactive)
  (unless (derived-mode-p 'binder-notes-mode)
    (user-error "Not in %S" 'binder-notes-mode))
  (if (not (buffer-modified-p))
      (message "(No changes need to be added to binder)")
    (binder-set-item-prop binder--notes-fileid 'notes
                          (string-trim (buffer-substring-no-properties
                                        (point-min) (point-max))))
    (set-buffer-modified-p nil)
    (binder-write)
    (binder-sidebar-refresh-window)
    (message "Saved notes for %s to binder"
             (or binder--notes-display binder--notes-fileid))))

(defun binder-notes-save-and-quit-window ()
  "Call `binder-notes-save' and quit the notes window."
  (declare (interactive-only t))
  (interactive)
  (binder-notes-save)
  (quit-window))

(defun binder-notes-expand-window ()
  "Toggle the notes window from a side window to full window."
  (declare (interactive-only t))
  (interactive)
  (unless (derived-mode-p 'binder-notes-mode)
    (user-error "Not in %S" 'binder-notes-mode))
  (if (window-parameter (selected-window) 'window-side)
      (progn
        (quit-window)
        (pop-to-buffer (get-buffer-create binder-notes-buffer)))
    (quit-window)
    (binder-show-notes binder--notes-fileid t)))

(defun binder-sidebar-sync-notes ()
  "Set the current notes to sidebar item at point."
  (while-no-input
    (redisplay)
    (when binder-notes-keep-in-sync
      (setq binder--notes-fileid (binder-sidebar-get-fileid))
      (binder-notes-refresh-window))))

(defvar binder-notes-mode-map
  (let ((map (make-sparse-keymap)))
    (define-key map (kbd "C-c C-c") #'binder-notes-save-and-quit-window)
    (define-key map [remap save-buffer] #'binder-notes-save)
    (define-key map (kbd "C-c C-l") #'binder-notes-expand-window)
    (define-key map (kbd "C-c C-q") #'quit-window)
    (define-key map (kbd "C-c C-k") #'quit-window)
    map)
  "Mode map for `binder-notes-mode'.")

;;;###autoload
(define-derived-mode binder-notes-mode
  text-mode "Binder Notes Mode"
  "Major mode for editing `binder' notes."
  (face-remap-add-relative 'default 'binder-notes)
  (binder-notes-refresh))


;;; Concat Mode

(defgroup binder-concat ()
  "Options for `binder-concat-mode'."
  :group 'binder)

(defcustom binder-default-concat-mode
  'text-mode
  "Default major mode when concatenating files."
  :type 'function
  :safe 'functionp
  :group 'binder-concat)

(defcustom binder-concat-separator "\n"
  "String to insert between concatenated project files."
  :type 'string
  :group 'binder-concat)

(defcustom binder-concat-buffer
  "*Binder Concat View*"
  "Buffer name for viewing a concatenated project."
  :type 'string
  :safe 'stringp
  :group 'binder-concat)

(defun binder-concat ()
  "Concatenate all project files marked as included.
Creates `binder-concat-buffer' with each file separated by
`binder-concat-separator' and sets the major mode to
`binder-default-concat-mode'.

See `binder-sidebar-toggle-include'."
  (interactive)
  (binder-ensure-in-project)
  (let ((item-list
         (seq-filter
          (lambda (item) (cdr (assq 'include item)))
          (binder-read t)))
        (concat-mode binder-default-concat-mode))
    (with-current-buffer (get-buffer-create binder-concat-buffer)
      (with-silent-modifications
        (erase-buffer)
        (dolist (item item-list)
          (let ((x (point)))
            (insert-file-contents
             (expand-file-name (car item) binder-project-directory))
            (goto-char (point-max))
            (insert binder-concat-separator)
            (put-text-property x (point) 'binder-original-file
                               (expand-file-name (car item) binder-project-directory)))))
      (funcall concat-mode)
      (binder-concat-mode t))
    (if (eq major-mode 'binder-sidebar-mode)
        (let ((pop-up-windows binder-sidebar-pop-up-windows))
          (pop-to-buffer binder-concat-buffer))
      (pop-to-buffer binder-concat-buffer))))

(defalias 'binder-sidebar-concat #'binder-concat)

(defun binder-concat-find-original ()
  "Find the file containing content at point."
  (interactive)
  (unless binder-concat-mode
    (user-error "Not in %S" 'binder-concat-mode))
  (let ((original-file (or (get-text-property (point) 'binder-original-file)
                           (get-text-property (1- (point)) 'binder-original-file))))
        ;; (position
        ;;  (cond ((bobp) 1)
        ;;        ((eobp)
        ;;         (previous-single-property-change
        ;;          (point) 'binder-original-file))
        ;;        ((string= (get-text-property (point) 'binder-original-file)
        ;;                  (get-text-property (1- (point)) 'binder-original-file))
        ;;         (or (previous-single-property-change
        ;;              (point) 'binder-original-file)
        ;;             1))
        ;;        (t (point))))
    ;; (setq position (1+ (- (point) position)))
    (find-file-existing original-file)))

(defcustom binder-concat-mode-on-hook
  '(view-mode)
  "Hook run after entering `binder-concat-mode'."
  :type 'hook
  :group 'binder-concat)

(defvar binder-concat-mode-map
  (let ((map (make-sparse-keymap)))
    (define-key map (kbd "C-c RET") #'binder-concat-find-original)
    map))

(define-minor-mode binder-concat-mode
  "Minor mode for viewing concatenated project files."
  :init-value nil)



(provide 'binder)
;;; binder.el ends here

;; Local Variables:
;; coding: utf-8
;; fill-column: 80
;; indent-tabs-mode: nil
;; require-final-newline: t
;; sentence-end-double-space: nil
;; End: