-
Notifications
You must be signed in to change notification settings - Fork 1
/
ee.el
executable file
·2480 lines (2209 loc) · 97.7 KB
/
ee.el
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
;;; ee.el --- Emacs information manager
;; Copyright (C) 2002, 2003, 2004, 2010 Juri Linkov <[email protected]>
;; Author: Juri Linkov <[email protected]>
;; Keywords: ee, convenience, tools, outlines
;; This file is [not yet] part of GNU Emacs.
;; This package 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 2, or (at your option)
;; any later version.
;; This package 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 package; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Commentary:
;; This file is the core of the ee package.
;; See the file README and documentation for more information.
;;; Code:
;;; Constants
(defconst ee-version "0.1.0"
"Version numbers of this version of Ee.")
;;; Customizable Variables
(defgroup ee nil
"Emacs information manager."
:prefix "ee-"
:group 'data
:group 'tools
:group 'convenience)
(defcustom ee-data-directory "~/.emacs.d/ee/"
"Name of default directory where persistent data files are saved
and searched to read when relative filename is given."
:type 'directory
:group 'ee)
(defcustom ee-view-data-directory "~/.emacs.d/ee/"
"Name of default directory where persistent view data files are saved
and searched to read when relative filename is given."
:type 'directory
:group 'ee)
(defcustom ee-data-file-save-format 'records
"Format used in the saved data files.
If 'pp, then save data to the file pretty-printed.
If 'records, then save every record on own line.
If nil, then save unformatted."
:type 'symbol
:group 'ee)
;; TODO: move to view-data?
(defcustom ee-quick-keys t ;; TODO: nil
"Define additional key bindings for faster tree navigation.
Additonal key bindings include arrow keys with combinations
of C- and S- modifiers.
Also useful mouse actions are bound to [mouse-1] button.
This takes effect when first loading the ee package."
:type 'boolean
:group 'ee)
(defcustom ee-mouse-expansion-sensitivity 3
"Number of positions mouse should be moved on the expansion line
in one direction to the left or to the right before expansion
is shown or hidden."
:type 'integer
:group 'ee)
(defcustom ee-mouse-scroll-margin 2
"Number of lines of margin at the top and bottom of a window.
Scroll the window downward or upward whenever mouse motions
get within this many lines of the top or bottom of the window.
The scrolling margin is ignored when window already displays
the beginning of buffer."
:type 'integer
:group 'ee)
(defcustom ee-mouse-scroll-lines 1
"Number of lines to scroll at the top and bottom of a window.
Scroll the window downward or upward this many lines
when mouse is moved of the top or bottom of the window."
:type 'integer
:group 'ee)
(defcustom ee-mode-hook nil
"Hook functions run after entering ee mode."
:type 'hook
:group 'ee)
(defcustom ee-view-goto-hook '(ee-view-goto-hook-default)
"Hook functions run after moving point into some line in the ee buffer."
:type 'hook
:group 'ee)
(defcustom ee-view-expansion-show-hook nil
"Hook functions run after expansion is expanded."
:type 'hook
:group 'ee)
(defcustom ee-view-expansion-hide-hook nil
"Hook functions run after expansion is collapsed."
:type 'hook
:group 'ee)
(defcustom ee-view-buffer-revert-function 'ee-view-buffer-revert-function-default
"Function to use to revert ee buffer."
:type 'function
:group 'ee)
(defcustom ee-root-data-file "ee.ee"
"Name of data file that holds the index of available ee extensions."
:type 'file
:group 'ee)
;;; Global Variables
(defvar ee-mode-map nil
"Local keymap for ee-mode buffers.")
(defvar data-getters)
(defvar data-setters)
;;; Buffer-Local Variables
(defvar ee-data-file nil
"Name of data file where persistent data are saved.")
(make-variable-buffer-local 'ee-data-file)
(defvar ee-data-file-auto-save nil
"*If this is t, then save data to the file automatically without confirmation,
when data are modified.")
(make-variable-buffer-local 'ee-data-file-auto-save)
(defvar ee-data nil
"Data structure loaded from data file or extracted
from alternate data source.")
(make-variable-buffer-local 'ee-data)
(defvar ee-view-data-file nil
"Name of view data file where view description data are saved.")
(make-variable-buffer-local 'ee-view-data-file)
(defvar ee-view-data nil
"Data structure containing all views descriptions.")
(make-variable-buffer-local 'ee-view-data)
(defvar ee-view nil
"Data structure containing part of `ee-view-data'
that describes only the current view.")
(make-variable-buffer-local 'ee-view)
;; TODO: move to view-data?
;; TODO: rename to ee-folded-expansions?
(defvar ee-hidden-expansions nil
"Folded expansions.")
(make-variable-buffer-local 'ee-hidden-expansions)
(defvar ee-parent-buffer nil
"Buffer associated with the current ee buffer.
Most often, it is a buffer where current ee command was invoked.
For example, ee-outline view buffer can be associated
with Emacs buffer where command `ee-outline' was invoked
and from which the ee-outline view buffer was generated
and where ee-outline commands operate.")
(make-variable-buffer-local 'ee-parent-buffer)
(defvar ee-view-filter-omit nil)
(make-variable-buffer-local 'ee-view-filter-omit)
;; TODO: replace next by ee-goal-field, e.g. (setq ee-goal-field 'buffer-name)
(defvar ee-goal-column nil
"*Goal column for vertical motion.")
(make-variable-buffer-local 'ee-goal-column)
(defvar ee-current-c-key-field nil
"Key used to identify current record.")
(make-variable-buffer-local 'ee-current-c-key-field)
(defvar ee-current-r-key-field nil
"Key used to identify current record.")
(make-variable-buffer-local 'ee-current-r-key-field)
;; TODO: move to view descriptions
;; (setq ee-faces '(nil info-xref font-lock-type-face nil))
;; qv vc-annotate-color-map
;; qv outline-font-lock-keywords
;; TODO: defface: ee-category-1-face ee-category-2-face ee-category-3-face ee-category-4-face
;; qv outline-font-lock-faces
(defvar ee-faces-default '[font-lock-warning-face
ee-category;font-lock-function-name-face
font-lock-type-face
font-lock-keyword-face
font-lock-builtin-face
font-lock-variable-name-face
font-lock-constant-face
font-lock-comment-face
font-lock-type-face
font-lock-string-face])
;; another alternative
;; (defvar ee-faces-default '[font-lock-warning-face
;; font-lock-function-name-face
;; font-lock-type-face
;; font-lock-string-face
;; font-lock-keyword-face
;; font-lock-variable-name-face
;; font-lock-builtin-face
;; font-lock-constant-face])
;; yet another alternative
;; '(font-lock-variable-name-face
;; font-lock-constant-face
;; font-lock-function-name-face
;; font-lock-builtin-face
;; font-lock-comment-face
;; font-lock-keyword-face
;; font-lock-type-face
;; font-lock-string-face)
;; TODO: make variable with hash for all view data instead of next:
(defvar ee-c-faces nil)
(make-variable-buffer-local 'ee-c-faces)
(defvar ee-r-faces nil)
(make-variable-buffer-local 'ee-r-faces)
;;; Logical Faces
(defface ee-category
'((((type tty) (class color)) (:foreground "blue"))
(((background light)) (:foreground "Blue"))
(((background dark)) (:foreground "LightSkyBlue"))
(t (:italic t)))
"Face used to display category lines."
:group 'ee)
(put 'ee-face-category-face 'face-alias 'ee-category)
;; (defface ee-faded
;; '((((type tty) (class color)) (:foreground "gray"))
;; (((background light)) (:foreground "DimGray"))
;; (((background dark)) (:foreground "DimGray"))
;; (t (:italic t)))
;; "Face used to display faded items."
;; :group 'ee)
;; (put 'ee-face-faded-face 'face-alias 'ee-faded)
;; TODO: better name ee-dim?
;; TODO: better name ee-fuzzy?
;; TODO: better name ee-shaded?
;; TODO: better name ee-faded?
;; TODO: try color #cccccc for faded-face
(if (facep 'shadow)
(defface ee-shadow
'((t :inherit shadow))
"Face used to display shadowed items."
:group 'ee)
(defface ee-shadow
'((((type tty pc) (class color)) (:foreground "yellow"))
(((background dark)) (:foreground "grey50"))
(((background light)) (:foreground "grey50")))
"Face used to display shadowed items."
:group 'ee))
(put 'ee-face-faded-face 'face-alias 'ee-shadow)
;; TODO: rename to ee-ignored similar to dired-face-ignored
(defface ee-omitted
'((((type tty) (class color)) (:foreground "green"))
(((class grayscale) (background light)) (:foreground "DimGray"))
(((class grayscale) (background dark)) (:foreground "LightGray"))
(((class color) (background light)) (:foreground "RosyBrown"))
(((class color) (background dark)) (:foreground "LightSalmon"))
(t (:italic t)))
"Face used to display omitted items."
:group 'ee)
(put 'ee-face-omitted-face 'face-alias 'ee-omitted)
(defface ee-marked
'((((type tty) (class color)) (:foreground "red"))
(((class color) (background light)) (:foreground "Red"))
(((class color) (background dark)) (:foreground "Red"))
(t (:inverse-video t :bold t)))
"Face used to display marked items."
:group 'ee)
(put 'ee-face-marked-face 'face-alias 'ee-marked)
(defface ee-bookmarked ;; TODO: better name?
'((((type tty) (class color)) (:foreground "gray"))
(((class grayscale) (background light))
(:foreground "LightGray" :bold t :underline t))
(((class grayscale) (background dark))
(:foreground "Gray50" :bold t :underline t))
(((class color) (background light)) (:foreground "CadetBlue"))
(((class color) (background dark)) (:foreground "Aquamarine"))
(t (:bold t :underline t)))
"Face used to display bookmarked items."
:group 'ee)
(put 'ee-face-bookmarked-face 'face-alias 'ee-bookmarked)
;; backward compatibility for pre-22.0 Emacs
(or (get 'link 'face-defface-spec)
(defface link
'((((class color) (background light))
:foreground "blue" :underline t)
(((class color) (background dark))
:foreground "cyan" :underline t)
(t :inherit underline))
"Basic face for unvisited links."
:group 'basic-faces))
(defface ee-link
'((t :inherit link))
"Face used to display link items."
:group 'ee)
(put 'ee-face-link-face 'face-alias 'ee-link)
;; backward compatibility for pre-22.0 Emacs
(or (get 'link-visited 'face-defface-spec)
(defface link-visited
'((((class color) (background light))
:foreground "magenta4" :underline t)
(((class color) (background dark))
:foreground "violet" :underline t)
(t :inherit underline))
"Basic face for visited links."
:group 'basic-faces))
(defface ee-link-visited
'((t :inherit link-visited))
"Face used to display visited link items."
:group 'ee)
(put 'ee-face-visited-link-face 'face-alias 'ee-link-visited)
;;; Markers
(defconst ee-mark-attr-alist '_a ;; '__@
"Unique symbol used for marking the beginning of attrubutes alist
in the internal view tree structure.")
(defconst ee-mark-record-tree '_r ;; '__/
"Unique symbol used for marking the beginning of record tree
in the internal view tree structure.")
(defconst ee-mark-subcategory-tree '_s ;; '__|
"Unique symbol used for marking the beginning of subcategory tree
in the internal view tree structure.")
(defvar ee-data-mark-field-name 'mark
"The name of data field used for marking.")
(make-variable-buffer-local 'ee-data-mark-field-name)
(defvar ee-mark-mark '*
"The generic mark symbol.")
;; not used yet, because visited records are marked by separate field
(defvar ee-mark-visited 'R
"Symbol used to mark visited records.")
;; not used yet, because bookmarked records are marked by separate field
(defvar ee-mark-bookmarked 'B
"Symbol used to mark bookmarked records.")
(defvar ee-mark-del 'D ;; TODO: ?D - character instead of symbol?
"Symbol used to mark records for deletion.")
;;; Key Bindings
;; TODO: move to defvar?
(defun ee-mode-map-make-default ()
"Defines default key bindings for `ee-mode-map'.
If `ee-quick-keys' is t, then defines additional key bindings
for faster tree navigation."
(let ((map (make-keymap)))
(suppress-keymap map)
;; Expansion visibility
(define-key map "+" 'ee-view-expansion-show)
(define-key map "-" 'ee-view-expansion-hide)
(define-key map "=" 'ee-view-expansion-show) ; on some keyboards "=" is on same key as "+", but typed w/o shift
(define-key map "*" 'ee-view-expansion-show-subtree)
(define-key map "/" 'ee-view-expansion-hide-subtree)
;; Record motion
(define-key map " " 'ee-view-record-next)
(define-key map [tab] 'ee-view-record-next)
(define-key map "\t" 'ee-view-record-next) ; for tty
(define-key map [backtab] 'ee-view-record-prev)
(define-key map [(shift tab)] 'ee-view-record-prev)
(define-key map [return] 'ee-view-record-select-or-expansion-show-or-hide)
(define-key map "\C-m" 'ee-view-record-select-or-expansion-show-or-hide) ; for tty
;; Generic motion
(define-key map [down-mouse-2] 'ee-mouse-navigation)
(define-key map "n" 'next-line)
(define-key map "p" 'previous-line)
;; View definition
(define-key map "v" 'ee-views)
(define-key map "V" 'ee-view-change-view-file)
(define-key map "f" 'ee-fields)
;; Record manipulation
(define-key map "c" 'ee-view-record-copy)
(define-key map "D" 'ee-view-records-delete)
;; TODO: mark by regexp as in dired
(define-key map "m" 'ee-view-records-mark)
(define-key map "d" 'ee-view-records-mark-delete)
(define-key map "k" 'ee-view-records-mark-delete)
(define-key map "\C-d" 'ee-view-records-mark-delete-backwards)
(define-key map "\C-k" 'ee-view-records-delete)
(define-key map "\C-y" 'ee-view-records-yank)
(define-key map "x" 'ee-view-records-execute)
(define-key map "u" 'ee-view-records-unmark)
(define-key map [backspace] 'ee-view-records-unmark-backwards)
(define-key map "\177" 'ee-view-records-unmark-backwards) ; for tty
;; Help
(define-key map "?" 'describe-mode)
(define-key map "h" 'describe-mode)
(define-key map "r" (lambda () (interactive) (message "%S" (ee-view-record-get))))
(define-key map "\C-c\C-hr" (lambda () (interactive) (message "%S" (ee-view-record-get))))
;; Buffer
(define-key map "g" 'ee-view-buffer-revert)
(define-key map "\C-x\C-s" 'ee-data-file-save)
(define-key map "q" 'quit-window)
;; outline-like key bindings
(define-key map "\C-c\C-n" 'ee-view-expansion-next-visible)
(define-key map "\C-c\C-p" 'ee-view-expansion-prev-visible)
(define-key map "\C-c\C-f" 'ee-view-expansion-next-same-level)
(define-key map "\C-c\C-b" 'ee-view-expansion-prev-same-level)
(define-key map "\C-c\C-u" 'ee-view-expansion-up)
(define-key map "\C-c\C-i" 'ee-view-expansion-show-children)
(define-key map "\C-c\C-s" 'ee-view-expansion-show-subtree)
(define-key map "\C-c\C-d" 'ee-view-expansion-hide-subtree)
(define-key map "\C-c\C-t" 'ee-view-expansion-hide-body)
(define-key map "\C-c\C-a" 'ee-view-expansion-show-all)
(define-key map "\C-c\C-l" 'ee-view-expansion-hide-leaves)
(define-key map "\C-c\C-k" 'ee-view-expansion-show-branches)
(define-key map "\C-c\C-q" 'ee-view-expansion-hide-sublevels)
(define-key map "\C-c\C-o" 'ee-view-expansion-hide-other)
;; dired-like key bindings
(define-key map "$" 'ee-view-expansion-show-or-hide)
(define-key map ">" 'ee-view-expansion-next)
(define-key map "<" 'ee-view-expansion-prev)
(define-key map "^" 'ee-view-expansion-up)
(define-key map [(meta ?o)] 'ee-view-filter-omit)
(when ee-quick-keys
(define-key map [down-mouse-1] 'ee-mouse-navigation)
(define-key map [right] 'ee-view-expansion-show-or-next)
(define-key map [left] 'ee-view-expansion-hide-or-up-or-prev)
(define-key map [(meta up)] 'ee-view-expansion-prev-sibling)
(define-key map [(meta down)] 'ee-view-expansion-next-sibling)
(define-key map [(meta right)] 'ee-view-expansion-up)
(define-key map [(meta left)] 'ee-view-expansion-down)
(define-key map [(control ?+)] 'ee-view-expansion-show-all)
(define-key map [(control ?-)] 'ee-view-expansion-hide-all)
;; TODO: self-insertion letters could start isearch
;; (add-hook 'isearch-mode-hook '(lambda () (setq isearch-string "ise" isearch-message "ise") (isearch-message)))
;; (add-hook 'isearch-mode-end-hook '(lambda () (setq isearch-mode-hook nil)))
)
(setq ee-mode-map map)))
(or ee-mode-map
(ee-mode-map-make-default))
;;; Major Mode
(defun ee-mode ()
;; TODO: write more here
"Mode for displaying and manipulating a relational data
grouped into hierarchical categories.
ee mode provides commands for browsing through the expansion tree.
Typing \\<ee-mode-map>\\[ee-views] will select the different view.
Typing \\<ee-mode-map>\\[ee-fields] will edit the record's fields.
\\{ee-mode-map}
"
;; NOT (interactive)
(kill-all-local-variables)
(use-local-map ee-mode-map)
(setq major-mode 'ee-mode)
(setq mode-name "ee")
(set (make-local-variable 'line-move-ignore-invisible) t)
(setq buffer-file-name nil)
(set-buffer-modified-p nil)
(setq buffer-read-only t)
(auto-save-mode 0)
(buffer-disable-undo (current-buffer))
(setq truncate-lines t)
(if ee-view-buffer-revert-function
(set (make-local-variable 'revert-buffer-function)
ee-view-buffer-revert-function))
(run-hooks 'ee-mode-hook))
;; ee mode is suitable only for specially formatted data
(put 'ee-mode 'mode-class 'special)
;;; View Generation Functions
(defun ee-view-buffer-create (name mode keymap data &optional view-data parent-buffer new-buffer-p data-file data-file-auto-save)
(let ((view-buffer
(ee-view-buffer-create-noselect name mode keymap data view-data parent-buffer new-buffer-p data-file data-file-auto-save)))
(if view-buffer
(switch-to-buffer view-buffer))))
(defun ee-view-buffer-create-noselect (name mode keymap data &optional view-data parent-buffer new-buffer-p data-file data-file-auto-save)
(let ((curr-buffer (or parent-buffer (current-buffer)))
(view-buffer (get-buffer name)))
(condition-case err
(if (and (not new-buffer-p) view-buffer)
(with-current-buffer view-buffer
(setq ee-parent-buffer curr-buffer)
(ee-view-buffer-update t))
(setq view-buffer (get-buffer-create name))
(with-current-buffer view-buffer
(ee-mode)
(if mode-name (setq mode-name mode))
(if keymap (use-local-map keymap))
(setq ee-parent-buffer curr-buffer)
(if data-file
(ee-data-update 'data data data-file data-file-auto-save)
(ee-data-update 'data data data-file data-file-auto-save t))
(ee-data-update 'view-data view-data)
(ee-view-buffer-update)))
(bad ;; TODO: here should be `error' symbol instead of `bad'!
(if view-buffer (kill-buffer view-buffer))
(setq view-buffer nil)
;; TODO: any better way to continue standard error handling after that?
;; (message "%s" (error-message-string err))
(error err)))
view-buffer))
(defun ee-view-buffer-generate ()
"Generate in the current buffer the hierarchical representation of
relational data from `ee-data' according to descritions in `ee-view-data'."
(if (and ee-data ee-view-data)
(save-excursion
;; Erase buffer before generation
;; TODO: maybe much faster to del buffer, and create a new?
(let ((inhibit-read-only t))
(widen)
(goto-char (point-min))
(remove-overlays)
(set-text-properties (point-min) (point-max) nil)
(erase-buffer)
(set-buffer-modified-p nil))))
(setq ee-view (and ee-view-data
(or (car (ee-data-records-find ee-view-data '(default . t)))
(aref ee-view-data 1))))
(if (and ee-data ee-view-data ee-view)
(let* ((data-getters (ee-data-meta-getters-get-set ee-data))
(data-setters (ee-data-meta-setters-get-set ee-data))
(view-getters (ee-data-meta-getters-get-set ee-view-data))
(r-filter (if (null ee-view-filter-omit) (ee-data-field-get 'r-filter ee-view view-getters)))
(c-path-finder (ee-data-field-get 'c-path-finder ee-view view-getters))
(c-tree-builder (ee-data-field-get 'c-tree-builder ee-view view-getters))
(r-tree-builder (ee-data-field-get 'r-tree-builder ee-view view-getters))
(parent-child-key-fields (ee-data-field-get 'r-parent-child-key-fields ee-view view-getters))
(child-parent-key-fields (ee-data-field-get 'r-child-parent-key-fields ee-view view-getters))
(parent-field
(cond (parent-child-key-fields (car parent-child-key-fields))
(child-parent-key-fields (cadr child-parent-key-fields))))
(child-field
(cond (parent-child-key-fields (cadr parent-child-key-fields))
(child-parent-key-fields (car child-parent-key-fields))))
(view-atree (list "root"))
(inhibit-read-only t))
(setq ee-c-faces (or (ee-data-field-get 'c-faces ee-view view-getters) ee-faces-default))
(setq ee-r-faces (or (ee-data-field-get 'r-faces ee-view view-getters) ee-faces-default))
(cond
(c-tree-builder
(ee-gen-add-path-atree nil (funcall c-tree-builder) view-atree))
(t
(ee-data-records-do
ee-data
(lambda (r ri)
(if (or (null r-filter)
(and (functionp r-filter)
(funcall r-filter r)))
(let ((c-path-list
(delq nil (and (functionp c-path-finder)
(funcall c-path-finder r)))))
(if c-path-list
(while c-path-list
(ee-gen-add-path-atree
(append (if (consp (car c-path-list))
(car c-path-list)
(list (car c-path-list)))
(list ee-mark-record-tree))
(cond
(r-tree-builder
(funcall r-tree-builder ri))
(parent-child-key-fields
;; TODO: make start-index-p configurable
(ee-gen-r-tree-parent-child ee-data data-getters parent-field child-field ri t))
(child-parent-key-fields
(ee-gen-r-tree-child-parent ee-data data-getters parent-field child-field ri))
(t ri))
view-atree)
(setq c-path-list (cdr c-path-list)))
(ee-gen-add-path-atree
(list ee-mark-record-tree)
(cond
(r-tree-builder
(funcall r-tree-builder ri))
(parent-child-key-fields
;; TODO: make start-index-p configurable
(ee-gen-r-tree-parent-child ee-data data-getters parent-field child-field ri t))
(child-parent-key-fields
(ee-gen-r-tree-child-parent ee-data data-getters parent-field child-field ri))
(t ri))
view-atree)))))))
;; (setcdr view-atree (sort (cdr view-atree) 'test-csort))
;; (ee-sort-atree (cdr view-atree)) ; TODO: if unsorted then nreverse
)
;; (funcall (cadr (assq 'r-printer ee-view))
;; (apply 'vector (cadr (assq 'r-fields ee-view))) 0 0 nil)
(insert (or (ee-data-field-get 'c-title ee-view view-getters) ""))
(insert (or (ee-data-field-get 'r-title ee-view view-getters) ""))
(if (ee-data-field-get 'c-title-printer ee-view view-getters)
(funcall (ee-data-field-get 'c-title-printer ee-view view-getters)))
(if (ee-data-field-get 'r-title-printer ee-view view-getters)
(funcall (ee-data-field-get 'r-title-printer ee-view view-getters)))
(let ((hidden-expansions ee-hidden-expansions))
(while hidden-expansions
(ee-gen-add-to-path-atree
(car hidden-expansions)
(list ee-mark-attr-alist '(hide t))
view-atree)
(setq hidden-expansions (cdr hidden-expansions))))
;; Separate sort step not used now;
;; currently sort performed simultaneously with print
;; (if (or (cadr (assq 'c-sorter ee-view))
;; (cadr (assq 'r-sorter ee-view)))
;; (ee-gen-c-tree-sort (cdr view-atree) 0 0 0 nil
;; (ee-data-field-get 'c-sorter ee-view view-getters)
;; (ee-data-field-get 'r-sorter ee-view view-getters))
;; )
;; TODO: make generic calculator with arbitrary names, e.g.:
;; (c-calculator (lambda (r a-level r-level children-p result)
;; (list (list 'size (ee-field 'size r))
;; (list 'count 1)
;; (list 'marked (if (ee-field ee-data-mark-field-name r) 1 0)))))
;; TODO: make generic collector (very low priority, needed?), e.g.:
;; (c-collector (lambda (r a-level r-level children-p result)
;; (list (list 'max-size (max result (ee-field 'size r))))))
(if (or (ee-data-field-get 'c-counter ee-view view-getters)
(ee-data-field-get 'r-counter ee-view view-getters))
(ee-gen-c-tree-calc (cdr view-atree) 0 0 0
(ee-data-field-get 'c-counter ee-view view-getters)
(ee-data-field-get 'r-counter ee-view view-getters)
'counter))
(if (or (ee-data-field-get 'c-calculator ee-view view-getters)
(ee-data-field-get 'r-calculator ee-view view-getters))
(ee-gen-c-tree-calc (cdr view-atree) 0 0 0
(ee-data-field-get 'c-calculator ee-view view-getters)
(ee-data-field-get 'r-calculator ee-view view-getters)
'result))
;; (message "%s" (pp view-atree))
(ee-gen-c-tree-print (cdr view-atree) 0 0 0 nil
(ee-data-field-get 'c-printer ee-view view-getters)
(ee-data-field-get 'c-sorter ee-view view-getters)
(ee-data-field-get 'r-printer ee-view view-getters)
(ee-data-field-get 'r-sorter ee-view view-getters))
(insert "\n") ; Insert final newline
(goto-char (point-min))
(and (eolp) (delete-char 1)) ; Delete 1st empty line
(set-buffer-modified-p nil)
(if (ee-data-field-get 'post-generate ee-view view-getters)
(funcall (ee-data-field-get 'post-generate ee-view view-getters))))))
(defun ee-gen-r-tree-parent-child (data data-getters parent-field-name child-field-name ri &optional start-index-p)
(if ri
(cons ri
(nreverse
(mapcar
(lambda (child-field-value)
(ee-gen-r-tree-parent-child
data data-getters parent-field-name child-field-name
(ee-data-record-index-find
data parent-field-name child-field-value
data-getters
(if start-index-p ri))))
(ee-data-field-get child-field-name (aref data ri) data-getters))))))
(defun ee-gen-r-tree-child-parent (data data-getters child-field-name parent-field-name ri &optional ri-done)
(if ri
(cons ri
(nreverse
(delq nil
(mapcar
(lambda (child-ri)
(when (not (memq child-ri ri-done))
(setq ri-done (cons ri (cons child-ri ri-done)))
(ee-gen-r-tree-child-parent
data data-getters child-field-name parent-field-name
child-ri
ri-done)))
(ee-data-record-indexes-find
data (cons child-field-name
(ee-data-field-get
parent-field-name
(aref data ri) data-getters))
data-getters)))))))
(defun ee-gen-add-path-atree (path new-atree atree)
"Create path and add new atree."
(if path
(let ((aelem (assoc (car path) atree)))
(if aelem
(ee-gen-add-path-atree (cdr path) new-atree aelem)
(setcdr atree (cons (ee-gen-list-to-atree path new-atree) (cdr atree)))))
(setcdr atree (cons new-atree (cdr atree)))))
(defun ee-gen-add-to-path-atree (path new-atree atree)
"Don't create path, but instead follow path, if possible, and then add new atree."
(if path
(let ((aelem (assoc (car path) atree)))
(if aelem
(ee-gen-add-to-path-atree (cdr path) new-atree aelem)
;; (setcdr atree (cons (ee-gen-list-to-atree path new-atree) (cdr atree)))
))
(setcdr atree (cons new-atree (cdr atree)))))
(defun ee-gen-list-to-atree (list atree)
(if list
(cons (car list) (list (ee-gen-list-to-atree (cdr list) atree)))
atree))
;; (ee-split-string "a/b/c/" "/") -> ("a" _s "b" _s "c")
(defun ee-split-string (string string-separator)
"Split string `string' by `string-separator' into list,
and separates element of new list by value of variable
`ee-mark-subcategory-tree', which marks subcategory subtrees."
(if (stringp string)
(let ((list (delete ""
;; starting from Emacs 21.3.50 the default behavior
;; of `split-string' was changed, so delete ""
;; to make it compatible with earlier versions
(split-string string string-separator)))
(list-separator ee-mark-subcategory-tree))
(if (car list)
(cons (car list)
(mapcan (lambda (elt)
(list list-separator elt))
(cdr list)))))))
(defun ee-gen-c-tree-print (atree a-level c-level s-level root-path c-printer c-sorter r-printer r-sorter)
;; Input:
;; a-level - absolute level
;; c-level - category level
;; s-level - relative subcategory level
(let ((a-level (1+ a-level))
(c-level (1+ c-level)))
(let ((sorter (if c-sorter
(funcall c-sorter a-level c-level (abs s-level)
(assq ee-mark-attr-alist atree)))))
(if sorter
(setq atree (sort atree sorter))
(setq atree (nreverse atree)))) ; reverse is hack
(while atree
(let* ((asubtree (car atree))
(aname (car asubtree))
b p)
(cond
((eq aname ee-mark-attr-alist)) ; Skip attr-alist
((eq aname ee-mark-record-tree) ; Start record hierarchy
(setq asubtree (cdr asubtree))
(let ((sorter (if r-sorter
(funcall r-sorter a-level 0
(assq ee-mark-attr-alist asubtree)))))
(if sorter
(setq asubtree (sort asubtree sorter))
(setq asubtree (nreverse asubtree)))) ; reverse is hack
(while asubtree
(ee-gen-r-tree-print (car asubtree) a-level 0 r-printer r-sorter)
(setq asubtree (cdr asubtree))))
((eq aname ee-mark-subcategory-tree) ; Subcategory separator
(ee-gen-c-tree-print
;; Retain c-level, increase a-level and s-level;
;; note that a-level and c-level are automatically increased twice
;; at the beginning of current call and next call of ee-gen-c-tree-print
(cdr asubtree) (- a-level 1) (- c-level 2) (1+ (abs s-level))
(cons aname root-path) c-printer c-sorter r-printer r-sorter))
(t ; else insert category line
(insert "\n")
(setq b (point))
;; Setting s-level to 0 is postponed to second level:
;; at first call it is set to negative value,
;; if there are no next subcategory separator,
;; converting it back to positive value,
;; then negative value at next call is set to 0
(let ((s-level (if (< s-level 0) 0 (- s-level))))
(if c-printer
(funcall c-printer a-level c-level (abs s-level)
aname (assq ee-mark-attr-alist asubtree)))
(setq p (point))
(ee-gen-c-tree-print
(cdr asubtree) a-level c-level s-level
(cons aname root-path) c-printer c-sorter r-printer r-sorter)
(if (< p (point)) ; Make expansion only if children exist
(add-text-properties b (1+ b) ;; p
(list 'ee-expansion (make-overlay p (point))
'ee-level a-level
'ee-path (cons aname root-path)))
;; needed?: (add-text-properties b p (list 'mouse-face 'highlight))
)
;; Make overlay invisible if property "hide" exists
(if (assq 'hide (assq ee-mark-attr-alist asubtree))
(save-excursion (goto-char b) (ee-view-expansion-hide)))))))
(setq atree (cdr atree)))))
(defun ee-gen-r-tree-print (atree a-level r-level r-printer r-sorter)
(let ((head (if (consp atree) (car atree) atree))
(tail (if (consp atree) (cdr atree)))
(attr-alist (if (consp atree) (assq ee-mark-attr-alist atree)))
b p)
(insert "\n")
(setq b (point))
(let ((r (if (integerp head) (aref ee-data head) head)))
(if r-printer
(funcall r-printer r a-level r-level
(and (> (- (length tail) (if attr-alist 1 0)) 0) t) attr-alist))
(add-text-properties b (1+ b);;(point)
;; "ri" is "record index"
(list 'ee-ri head
'ee-level a-level
'ee-rlevel r-level
;; TODO: add children if not null
'ee-childrenp (and (> (- (length tail) (if attr-alist 1 0)) 0) t)
;; TODO: split attr to separate props
'ee-attrs attr-alist)))
(setq p (point))
(when tail
(let ((sorter (if r-sorter
(funcall r-sorter a-level r-level (assq ee-mark-attr-alist tail)))))
(if sorter
(setq tail (sort tail sorter))
(setq tail (nreverse tail)))) ; reverse
(while tail
(if (not (eq (caar tail) ee-mark-attr-alist)) ; if not attr-alist
(ee-gen-r-tree-print (car tail) (1+ a-level) (1+ r-level) r-printer r-sorter))
(setq tail (cdr tail)))
;; Make expansion only if children exist
(if (< p (point))
(add-text-properties b (1+ b);;p
(list 'ee-expansion (make-overlay p (point))
'ee-level a-level))))))
(defmacro ee-sort! (list predicate)
`(let ((new-list (sort (cons (car ,list) (cdr ,list)) ,predicate)))
(setcar ,list (car new-list))
(setcdr ,list (cdr new-list))))
;; Alternative function instead of macro `ee-sort!'
;; (defun ee-sort! (list predicate)
;; (let ((new-list (sort (cons (car list) (cdr list)) predicate)))
;; (setcar list (car new-list))
;; (setcdr list (cdr new-list))))
(defmacro ee-reverse! (list)
`(let ((new-list (nreverse (cons (car ,list) (cdr ,list)))))
(setcar ,list (car new-list))
(setcdr ,list (cdr new-list))))
;; Alternative function instead of macro `ee-reverse!'
;; (defun ee-reverse! (list)
;; (let ((new-list (nreverse (cons (car list) (cdr list)))))
;; (setcar list (car new-list))
;; (setcdr list (cdr new-list))))
;; NOT USED currently, because sort is done simultaneously with print
(defun ee-gen-c-tree-sort (atree a-level c-level s-level root-path c-sorter r-sorter)
(let* ((a-level (1+ a-level))
(c-level (1+ c-level)))
(if c-sorter
(ee-sort! atree (funcall c-sorter a-level c-level s-level (assq ee-mark-attr-alist atree)))
(ee-reverse! atree))
(while atree
(let* ((asubtree (car atree))
(aname (car asubtree)))
(cond
((eq aname ee-mark-attr-alist)) ; Skip attr-alist
((eq aname ee-mark-record-tree) ; Start record hierarchy
(setq asubtree (cdr asubtree))
; (if r-sorter (setq asubtree (sort asubtree (car r-sorter))))
; (while asubtree
; (ee-gen-r-tree-sort (car asubtree) a-level 0 r-sorter)
; (setq asubtree (cdr asubtree)))
; (ee-gen-r-tree-sort (list nil asubtree) ; fake root
; a-level 0 r-sorter)
(ee-gen-r-tree-sort (cons nil asubtree) ; fake root
a-level 0 r-sorter)
)
((eq aname ee-mark-subcategory-tree) ; Subcategory separator
(ee-gen-c-tree-sort
(cdr asubtree) (- a-level 1) (- c-level 2) (1+ (abs s-level))
(cons aname root-path) c-sorter r-sorter))
(t ; else insert category line
(let ((s-level (if (< s-level 0) 0 (- s-level))))
(ee-gen-c-tree-sort
(cdr asubtree) a-level c-level s-level
(cons aname root-path) c-sorter r-sorter)))))
(setq atree (cdr atree)))))
;; NOT USED currently, because sort is done simultaneously with print
(defun ee-gen-r-tree-sort (atree a-level r-level r-sorter)
(let ((head (if (consp atree) (car atree) atree))
(tail (if (consp atree) (cdr atree)))
; (attr-alist (if (consp atree) (assq ee-mark-attr-alist atree)))
)
; (let ((r (if (integerp head) (aref ee-data head) head)))
; (funcall r-sorter r a-level r-level
; (and (> (- (length tail) (length attr-alist)) 0) t) attr-alist))
(when tail
(if r-sorter
(ee-sort! tail (funcall r-sorter a-level r-level (assq ee-mark-attr-alist atree)))
(ee-reverse! tail))
; (if r-sorter
; (setcdr atree (sort tail (funcall r-sorter a-level r-level attr-alist))))
(while tail
; (if (not (eq (caar tail) ee-mark-attr-alist)) ; if not attr-alist
(if (consp (car tail))
(ee-gen-r-tree-sort (car tail) (1+ a-level) (1+ r-level) r-sorter))
; )
(setq tail (cdr tail))))))
(defun ee-gen-c-tree-calc (atree a-level c-level s-level c-calculator r-calculator result-aname)
(let* ((orig-atree atree)
(a-level (1+ a-level))
(c-level (1+ c-level))
(result 0))
(while atree
(let* ((asubtree (car atree))
(aname (car asubtree)))
(cond
((eq aname ee-mark-attr-alist))
((eq aname ee-mark-record-tree)
(setq asubtree (cdr asubtree))
(while asubtree
(setq result (+ result (ee-gen-r-tree-calc (car asubtree) a-level 0 r-calculator result-aname)))
(setq asubtree (cdr asubtree))))
((eq aname ee-mark-subcategory-tree)
(setq result (+ result (ee-gen-c-tree-calc (cdr asubtree) (- a-level 1) (- c-level 2) (1+ (abs s-level)) c-calculator r-calculator result-aname))))
(t
(let ((s-level (if (< s-level 0) 0 (- s-level))))
(if c-calculator
(setq result (+ result (funcall c-calculator a-level c-level (abs s-level) aname (assq ee-mark-attr-alist asubtree)))))
(setq result (+ result (ee-gen-c-tree-calc (cdr asubtree) a-level c-level s-level c-calculator r-calculator result-aname)))))))
(setq atree (cdr atree)))
(if (> result 0)
(ee-gen-add-path-atree (list ee-mark-attr-alist) (list result-aname result) orig-atree))
result))
(defun ee-gen-r-tree-calc (atree a-level r-level r-calculator result-aname)
(let ((head (if (consp atree) (car atree) atree))
(tail (if (consp atree) (cdr atree)))
(result 0))
(when tail
(while tail
(setq result (+ result (ee-gen-r-tree-calc (car tail) (1+ a-level) (1+ r-level) r-calculator result-aname)))
(setq tail (cdr tail))))
;; Add sum of subnodes to attribute alist of current node
(if (> result 0)
(ee-gen-add-path-atree (list ee-mark-attr-alist) (list result-aname result) (if (consp atree) atree (list atree))))
;; Calculate value of current node after the adding result to attribute alist,
;; so value of current node is not added to node's result
(let ((r (if (integerp head) (aref ee-data head) head)))
(if r-calculator
(setq result (+ result (funcall r-calculator r a-level r-level (and tail t) result)))))
result))
;;; Useful functions to use in view descriptions (TODO: add more)
;; c-printer: category exapansion line printer
(defun ee-c-printer-1 (a-level c-level s-level header)
(insert (make-string a-level ?\040) "- " header))
(defun ee-c-printer-2 (a-level c-level s-level header)
(insert (make-string (* 2 a-level) ?\040) "- " header))
(defun ee-c-printer-1-1 (a-level c-level s-level header)
(insert (make-string (1- a-level) ?\040) "- " header))