This repository has been archived by the owner on Aug 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtt.c
1206 lines (1105 loc) · 27.2 KB
/
tt.c
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
#include <ncurses.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <stdbool.h>
#include <setjmp.h>
#include <errno.h>
#include "exception.h"
#include "readline.h"
/******************************************************************************
TODO:
Add means to view entries that don't fit onscreen
Handle horizontal scrolling
Show filename in "say" messages
Listen for resize signal
Handle Ctrl+Z signal
Alphabetize functions
Check Delete edge cases for crashes
Check for memory leaks with valgrind
Tab complete for filenames
*/
/* Program info displayed in status bar */
#define PROGRAM "tree tool"
#define VERSION "v0.2"
/* Size in rows of footer sections */
#define HELP_SIZE 2
#define STATUS_SIZE 1
/* Set to 0 to hide help on startup*/
#define SHOW_HELP_DEFAULT 0
#define MAX_ENTRY_LEN 256
/* Duration of blink in ms */
#define SAY_DURATION 96
#define SAY_BLINKS 2
#define MAX_SAY_CHARS 40
enum fold_state {
EMPTY,
EXPANDED,
COLLAPSED
};
struct tree {
int nchild;
int nalloc;
struct tree *parent;
struct tree **child;
struct tree *sibling; /* TODO: use a linked list instead of array for child nodes */
enum fold_state state;
char* text;
};
/* function prototypes */
bool confirm(const char *question);
bool modified_warning();
char *prompt(const char *msgstr, const char *defstr);
int main(int argc, char *argv[]);
struct tree *add_child(struct tree *parent, char* text);
struct tree *add_leaf(struct tree *parent, struct tree *child);
struct tree *del_child(struct tree *child);
struct tree *find_root(struct tree *leaf);
struct tree *read_tree(FILE *f, char delim, int depth);
void delete();
void demote();
void die(const char *error);
void draw_info(int y, int x, const char *key, const char *label);
void edit_entry();
void free_tree(struct tree *t);
void help_normal();
void help_edit();
void init_curses();
void insert_entry();
bool load(const char *fname);
void menu();
void print_tree(struct tree *tree, int depth);
void promote();
void redraw();
void resize();
void save();
void saveas(const char *fname);
void say(const char *str);
void select_down();
void select_up();
void set_fold(enum fold_state f);
WINDOW *set_window(WINDOW *win, int h, int w, int y, int x);
void shove_down();
void shove_up();
void status();
void write_tree(struct tree *t, FILE *f, int depth);
/******************************************************************************
Globals
*/
/* curses windows for each UI section */
WINDOW *tree_window;
WINDOW *status_window;
WINDOW *help_window;
int tree_win_height;
int status_win_height;
int help_win_height;
int input_win_height;
int screenh, screenw;
int vscroll;
int printed_lines;
int selected_index;
enum {
H_HIDE,
H_NORMAL,
H_EDIT
} help_mode;
struct tree **onscreen_entries;
struct tree *selected_entry;
struct tree *root;
unsigned char int_size = sizeof(int);
char filename[MAX_ENTRY_LEN];
bool modified;
char saymsg[MAX_SAY_CHARS];
int sayblink;
/******************************************************************************
Allocate a new node, add it to the parent's list of children,
and set its contents to "text"
*/
struct tree *add_child(struct tree *parent, char* text)
{
struct tree *tree = NULL;
struct tree *child = malloc(sizeof(*child));
int len = strlen(text)+1;
child->nchild = child->nalloc = 0;
child->text = malloc(len);
memcpy(child->text, text, len);
child->text[len-1] = '\0';
child->state = EMPTY;
return add_leaf(parent, child);
}
/******************************************************************************
Places the given node into the parent node's list of
children, allocating space in the parent's list if needed
*/
struct tree *add_leaf(struct tree *parent, struct tree *child)
{
child->parent = NULL;
if (parent == NULL) {
return child;
}
if (parent->nalloc == 0) {
parent->child = malloc(sizeof(parent->child));
parent->nalloc = parent->nchild = 1;
parent->child[0] = child;
parent->state = EXPANDED;
child->parent = parent;
return child;
}
if (parent->nchild < parent->nalloc) {
parent->child[parent->nchild++] = child;
parent->state = EXPANDED;
child->parent = parent;
return child;
}
parent->nalloc *= 2;
parent->child = realloc(parent->child,
sizeof(parent->child) * parent->nalloc);
parent->child[parent->nchild++] = child;
parent->state = EXPANDED;
child->parent = parent;
return child;
}
/******************************************************************************
Remove a child from its parent and return a pointer
to the removed node. Returns NULL if node was not found.
*/
struct tree *del_child(struct tree *child)
{
int i, j;
struct tree *tree = NULL;
if (child == NULL)
return NULL;
tree = child->parent;
if (tree == NULL)
return NULL;
for (i = 0; i < tree->nchild; i++) {
if (tree->child[i] == child) {
for (j = i; j < tree->nchild-1; j++) {
tree->child[j] = tree->child[j+1];
}
tree->nchild--;
child->parent = NULL;
return child;
}
}
return NULL;
}
/******************************************************************************
Release the memory used by a tree and its children
*/
void free_tree(struct tree *t)
{
int i;
for (i = 0; i < t->nchild; i++) {
free_tree(t->child[i]);
}
free(t->text);
free(t);
}
/******************************************************************************
Print an error and quit
*/
void die(const char *error)
{
endwin();
fprintf(stderr, "=====================================\n");
fprintf(stderr, " ERROR \n");
fprintf(stderr, "-------------------------------------\n");
fprintf(stderr, "%s\n", error);
fprintf(stderr, "=====================================\n");
exit(1);
}
/******************************************************************************
Return the topmost node connected to leaf
*/
struct tree *find_root(struct tree *leaf)
{
if (leaf->parent == NULL)
return leaf;
return find_root(leaf->parent);
}
/******************************************************************************
Set the window's position and size, reallocating if necessary
*/
WINDOW *set_window(WINDOW *win, int h, int w, int y, int x)
{
int wy, wx, ww, wh;
if (win != NULL) {
getbegyx(win, wy, wx);
getmaxyx(win, wh, ww);
if (ww == w && wh == h && wx == x && wy == y)
return win;
if (ww == w && wh == h && (wx != x || wy != y)) {
mvwin(win, y, x);
return win;
}
delwin(win);
win = NULL;
}
return newwin(h, w, y, x);
}
/******************************************************************************
Query terminal for size and update windows accordingly
*/
void resize()
{
getmaxyx(stdscr, screenh, screenw);
status_win_height = STATUS_SIZE;
help_win_height = help_mode == H_HIDE ? 0 : HELP_SIZE;
tree_win_height = screenh
- (status_win_height + help_win_height + input_win_height);
/* keep a table of onscreen entries to map cursor row to struct ptr */
onscreen_entries = realloc(onscreen_entries,
sizeof(*onscreen_entries) * tree_win_height);
/* create new tree window of correct size */
tree_window = set_window(tree_window, tree_win_height, screenw, 0, 0);
/* create new status window of correct size */
status_window = set_window(status_window, status_win_height, screenw,
screenh - help_win_height - status_win_height, 0);
/* if visible, create new help window of correct size */
if (help_mode != H_HIDE) {
help_window = set_window(help_window,
help_win_height,
screenw,
screenh - help_win_height, 0);
} else {
if (help_window != NULL) {
delwin(help_window);
help_window = NULL;
}
}
}
/******************************************************************************
Enter curses mode
*/
void init_curses()
{
setlocale(LC_ALL, "");
initscr();
noecho();
raw();
curs_set(0);
resize();
}
/******************************************************************************
Move the selected entry up
*/
void shove_up()
{
if (selected_entry != NULL) {
struct tree *sel = selected_entry;
struct tree *parent = sel->parent;
int i, a, b;
if (parent == NULL || parent->nchild < 2)
return;
a = b = -1;
for (i = 0; i < parent->nchild; i++) {
if (parent->child[i] == sel) {
b = (a = i) - 1;
break;
}
}
if (a >= 0 && b >= 0) {
struct tree *swap = parent->child[b];
parent->child[b] = sel;
parent->child[a] = swap;
modified = true;
}
}
}
/******************************************************************************
Move the selected entry down
*/
void shove_down()
{
if (selected_entry != NULL) {
struct tree *sel = selected_entry;
struct tree *parent = sel->parent;
int i, a, b;
if (parent == NULL || parent->nchild < 2)
return;
a = b = -1;
for (i = 0; i < parent->nchild; i++) {
if (parent->child[i] == sel) {
b = (a = i) + 1;
break;
}
}
if (a >= 0 && b >= 0 && b < parent->nchild) {
struct tree *swap = parent->child[b];
parent->child[b] = sel;
parent->child[a] = swap;
modified = true;
}
}
}
/******************************************************************************
Move the selected entry to a higher tier
*/
void promote()
{
if (selected_entry != NULL && selected_entry->parent != NULL) {
struct tree *sel = selected_entry;
struct tree *parent = sel->parent;
struct tree *new_parent = parent->parent;
int i, shoves = 0;
if (new_parent == NULL)
return;
sel = del_child(sel);
if (sel != NULL) {
/* Find the index of the old parent so we can position the
promoted entry directly above it */
for (i = 0; i < new_parent->nchild; i++) {
if (new_parent->child[i] == parent) {
shoves = new_parent->nchild - i;
break;
}
}
new_parent = add_leaf(new_parent, sel);
for (i = 0; i < shoves; i++) {
shove_up();
}
modified = true;
} else {
die("Lost child while promoting");
}
}
}
/******************************************************************************
Move the selected entry to a lower tier
*/
void demote()
{
if (selected_entry != NULL) {
struct tree *sel = selected_entry;
struct tree *parent = sel->parent;
struct tree *new_parent = NULL;
int i;
if (parent == NULL || parent->nchild < 2) { return; }
for (i = 0; i < parent->nchild-1; i++) {
if (parent->child[i] == sel) {
new_parent = parent->child[i+1];
break;
}
}
/* If we're trying to demote the last child, the new parent is the
one before, not after */
if (new_parent == NULL)
new_parent=parent->child[parent->nchild-2];
sel = del_child(sel);
if (sel != NULL) {
new_parent = add_leaf(new_parent, sel);
/* Position the entry at the top */
for (i = 0; i < new_parent->nchild-1; i++) {
shove_up();
}
modified = true;
}
}
}
/******************************************************************************
Move the current selection up
*/
void select_up()
{
if (selected_entry == NULL || selected_index <= 0) {
selected_entry = onscreen_entries[0];
if (vscroll > 0)
vscroll--;
}
else
selected_entry = onscreen_entries[selected_index-1];
}
/******************************************************************************
Move the current selection down
*/
void select_down()
{
if (selected_entry == NULL || selected_index >= printed_lines-vscroll-1) {
selected_entry = onscreen_entries[printed_lines-vscroll-1];
if (vscroll+tree_win_height <= printed_lines)
vscroll++;
}
else
selected_entry = onscreen_entries[selected_index+1];
}
/******************************************************************************
Expand or collapse selected entry
*/
void set_fold(enum fold_state f)
{
if (selected_entry != NULL) {
selected_entry->state = f;
}
}
/******************************************************************************
Prompt the user to input a string
*/
char *prompt(const char *msgstr, const char *defstr)
{
WINDOW *prompt_win;
WINDOW *input_win;
char *str;
int len;
int c;
struct rlstate *rl;
help_mode = help_mode == H_NORMAL ? H_EDIT : H_HIDE;
input_win_height = 2;
resize();
redraw();
prompt_win = newwin(1, screenw, tree_win_height, 0);
wbkgdset(prompt_win, A_BOLD | A_UNDERLINE);
whline(prompt_win, ' ', screenw);
waddstr(prompt_win, msgstr);
wrefresh(prompt_win);
input_win = newwin(1, screenw, tree_win_height + 1, 0);
wmove(input_win, 0, 0);
wrefresh(input_win);
rl = rl_start(input_win);
if (defstr != NULL)
rl_set(rl, defstr);
do {
if (c == 0x1F) { /* C-? */
help_mode = help_mode == H_HIDE ? H_EDIT : H_HIDE;
resize();
mvwin(input_win, tree_win_height + 1, 0);
mvwin(prompt_win, tree_win_height, 0);
redraw();
wnoutrefresh(tree_window);
wnoutrefresh(status_window);
wnoutrefresh(help_window);
wnoutrefresh(prompt_win);
}
rl_draw(rl);
wnoutrefresh(input_win);
doupdate();
c = rl_read(rl);
} while (c != '\n');
str = rl_finish(rl);
delwin(input_win);
delwin(prompt_win);
help_mode = help_mode == H_EDIT ? H_NORMAL : H_HIDE;
input_win_height = 0;
resize();
if (str[0] == '\0') {
free(str);
say("Input cancelled.");
return NULL;
}
return str;
}
/******************************************************************************
Create a new entry, prompt for its contents, add it to tree
*/
void insert_entry()
{
char *str = prompt("New entry", NULL);
if (str == NULL)
return;
if (strlen(str) > 0) {
if (selected_entry == NULL)
selected_entry = root;
selected_entry = add_child(selected_entry, str);
modified = true;
} else {
say("Entry cancelled.");
}
free(str);
}
/******************************************************************************
Edit an existing entry
*/
void edit_entry()
{
char *str;
if (selected_entry == NULL) {
return;
} else if (selected_entry == root) {
say("Cannot modify root entry.");
return;
}
str = prompt("Edit entry", selected_entry->text);
if (str == NULL)
return;
if (strlen(str) > 0) {
free(selected_entry->text);
selected_entry->text = str;
say("Editing complete.");
modified = true;
} else {
free(str);
say("Edit cancelled.");
}
}
/******************************************************************************
Recursively traverse the tree and print its contents.
Also updates the values of onscreen_entries to simplify
selection and cursor movement
*/
void print_tree(struct tree *tree, int depth)
{
int i = 0;
int col = 0;
if (tree == NULL)
return;
if (tree == root) {
printed_lines = 0;
wmove(tree_window, 0, 0);
}
if (printed_lines - vscroll >= tree_win_height)
return;
if (tree_window == NULL)
return;
if (tree->nchild == 0)
tree->state = EMPTY;
/* only actually print if its onscreen */
if (printed_lines - vscroll >= 0
&& printed_lines - vscroll < tree_win_height) {
/* indent */
for (i = 0; i < depth; i++) {
wprintw(tree_window, " ");
}
switch(tree->state) {
case EMPTY: wprintw(tree_window, "[ ] "); break;
case EXPANDED: wprintw(tree_window, "[-] "); break;
case COLLAPSED: wprintw(tree_window, "[+] "); break;
}
/* indent [ ] */
col = depth * 2 + 4;
/* highlight selection */
if (selected_entry == tree)
wattron(tree_window, A_STANDOUT);
waddnstr(tree_window, tree->text, screenw - 3 - col);
if (strlen(tree->text) > screenw - 3 - col)
waddstr(tree_window, "...");
waddch(tree_window, '\n');
if (selected_entry == tree)
wattroff(tree_window, A_STANDOUT);
onscreen_entries[printed_lines - vscroll] = tree;
}
printed_lines++;
/* print child recursively */
if (tree->state == EXPANDED) {
for (i = 0; i < tree->nchild; i++) {
if (tree->child[i] != NULL)
print_tree(tree->child[i], depth+1);
}
}
/* stuff to run after the original call has finished */
if (tree == root) {
/* find the onscreen index of selected entry */
for (i = 0; i < printed_lines - vscroll; i++) {
if (onscreen_entries[i] == selected_entry) {
selected_index = i;
break;
}
}
/* clear any empty lines below the last entry */
for (i = printed_lines - vscroll; i < tree_win_height; i++) {
onscreen_entries[i] = NULL;
wclrtoeol(tree_window);
wprintw(tree_window, "\n");
}
}
}
/******************************************************************************
Write a tree recursively to file
*/
void write_tree(struct tree *t, FILE *f, int depth)
{
int i;
for (i = 0; i < depth; i++) {
fprintf(f, "\t");
}
fprintf(f, "%s\n", t->text);
for (i = 0; i < t->nchild; i++) {
write_tree(t->child[i], f, depth+1);
}
}
/******************************************************************************
Read a tree recursively from file
*/
struct tree *read_tree(FILE *f, char delim, int indent)
{
char buf[MAX_ENTRY_LEN];
int i;
int len;
struct tree *t;
fpos_t fp;
int c;
int dcount;
t = malloc(sizeof(*t));
t->nchild = t->nalloc = 0;
/* save beginnning of line */
if (fgetpos(f, &fp) < 0 && errno != 0)
raise(ERR_IO, strerror(errno));
/*
If delim is '\0', value will be set to the first whitespace
character encountered at the beginning of a line
*/
if (delim == '\0') {
c = fgetc(f);
if (c == ' ' || c == '\t')
delim = c;
if (ungetc(c, f) == EOF && errno != 0)
raise(ERR_IO, strerror(errno));
}
/* ensure consistent indentation */
dcount = 0;
c = fgetc(f);
while (c == delim) {
dcount++;
c = fgetc(f);
}
if (ungetc(c, f) == EOF && errno != 0)
raise(ERR_IO, strerror(errno));
if (dcount != indent)
raise(ERR_FORMAT, "invalid indentation");
if (fgets(buf, MAX_ENTRY_LEN, f) == NULL && errno != 0)
raise(ERR_IO, strerror(errno));
len = strlen(buf);
if (buf[len-1] == '\n') {
/* remove trailing newline */
buf[--len] = '\0';
} else {
/* we didn't finish reading the line. discard the rest */
int c = fgetc(f);
while (c != '\n' && c != EOF) {
c = fgetc(f);
}
}
t->text = malloc(len+1);
memcpy(t->text, buf, len+1);
t->parent = NULL;
t->state = COLLAPSED;
for (;;) {
if (fgetpos(f, &fp) < 0 && errno != 0)
raise(ERR_IO, strerror(errno));
dcount = 0;
c = fgetc(f);
while (c == delim) {
dcount++;
c = fgetc(f);
}
if (c == EOF)
return t;
if (fsetpos(f, &fp) < 0 && errno != 0)
raise(ERR_IO, strerror(errno));
if (dcount <= indent)
return t;
if (dcount > indent+1)
raise(ERR_FORMAT, "invalid indentation");
t->nalloc++;
t->nchild++;
t->child = realloc(t->child, sizeof(*t->child) * t->nalloc);
if (t->child == NULL)
raise(ERR_ALLOC, "realloc failed");
t->child[t->nchild-1] = read_tree(f, delim, indent+1);
t->child[t->nchild-1]->parent = t;
}
return NULL; /* unreachable */
}
/******************************************************************************
Save the current tree to the specified file
*/
void saveas(const char *fname)
{
FILE *f;
int i;
if (fname == NULL || strlen(fname) == 0) {
say("No filename given.");
return;
}
if (strcmp(fname, filename) != 0) {
/* Warn if overwriting */
f = fopen(fname, "r");
if (f) {
fclose(f);
if (!confirm("File exists, overwrite? (y/n)")) {
say("Save cancelled.");
return;
}
}
}
f = fopen(fname, "w");
if (!f) {
say("Error opening file.");
return;
}
for (i = 0; i < root->nchild; i++) {
write_tree(root->child[i], f, 0);
}
fclose(f);
strcpy(filename, fname);
modified = false;
say("Saved.");
}
/******************************************************************************
If a working file exists, save to it. Otherwise prompt for a filename
*/
void save()
{
if (strlen(filename) == 0) {
saveas(prompt("Save as...", NULL));
return;
}
saveas(filename);
}
/******************************************************************************
Discard current tree and load a new one from file
*/
bool load(const char *fname)
{
bool success = false;
FILE *f = NULL;
if (strlen(fname) == 0) {
say("No filename given.");
return false;
}
if (modified_warning()) {
if (try()) {
f = fopen(fname, "r");
if (!f) {
raise(ERR_FILENOTFOUND, fname);
}
if (root != NULL) {
free_tree(root);
}
root = add_child(NULL, "Entries");
selected_entry = root;
while (!feof(f)) {
add_leaf(root, read_tree(f, '\0', 0));
}
fclose(f);
strcpy(filename, fname);
modified = false;
success = true;
} else { /* error handling */
if (catch(ERR_FILENOTFOUND)
/* || catch(ERR_IO) */
|| catch(ERR_FORMAT)) {
say(get_error());
if (f != NULL) {
fclose(f);
}
/* discard the partial load */
if (root != NULL) {
free_tree(root);
root = add_child(NULL, "Entries");
}
}
}
finally();
} else if (sayblink == 0) {
say("Cancelled.");
return false;
}
return success;
}
/******************************************************************************
Prints a key / description pair for the help screen
*/
void draw_info(int y, int x, const char *key, const char *label)
{
wmove(help_window, y, x);
wattron(help_window, A_REVERSE | A_BOLD);
waddstr(help_window, key);
wattroff(help_window, A_REVERSE | A_BOLD);
waddstr(help_window, " ");
waddstr(help_window, label);
}
/******************************************************************************
Draw the status bar
*/
void status()
{
int plen = strlen(PROGRAM " " VERSION);
int i;
int flen;
wmove(status_window, 0, 0);
wattron(status_window, A_REVERSE);
for (i = 0; i < screenw; i++) {
waddstr(status_window, " ");
}
wmove(status_window, 0, 0);
wattron(status_window, A_BOLD);
if (strlen(filename) == 0) {
waddstr(status_window, "[Untitled]");
flen = 10;
} else {
waddstr(status_window, filename);
flen = strlen(filename);
}
if (modified) {
wmove(status_window, 0, flen);
waddstr(status_window, "*");
flen++;
}
wmove(status_window, 0, screenw-plen);
waddstr(status_window, PROGRAM " " VERSION);
wattroff(status_window, A_BOLD);
if (strlen(saymsg) > 0) {
wmove(status_window, 0, screenw - plen - strlen(saymsg) - 2 - 4);
if (sayblink > 1 && sayblink % 2 == 0) {
wattron(status_window, A_BOLD);
}
waddstr(status_window, "> ");
waddstr(status_window, saymsg);
wattroff(status_window, A_BOLD);
waddstr(status_window, " ");
}
wattroff(status_window, A_REVERSE);
}
/******************************************************************************
Draws the normal mode help info
*/
void help_normal()
{
int col = screenw / 6;
wmove(help_window, 1, 0);
wclrtoeol(help_window);
wmove(help_window, 0, 0);
wclrtoeol(help_window);
draw_info(0, 0 * col, " i ", "New");
draw_info(1, 0 * col, " e ", "Edit");
draw_info(0, 1 * col, " H ", "Promote");
draw_info(1, 1 * col, " L ", "Demote");
draw_info(0, 2 * col, " K ", "Move Up");
draw_info(1, 2 * col, " J ", "Move Dn");
draw_info(0, 3 * col, " D ", "Delete");
draw_info(0, 4 * col, " S ", "Save");
draw_info(1, 4 * col, " O ", "Open");
draw_info(0, 5 * col, " A ", "Save as");
draw_info(1, 5 * col, " Q ", "Quit");
}
/******************************************************************************
Draws the insert / edit mode help info
*/
void help_edit()
{
int col = screenw / 6;
wmove(help_window, 1, 0);
wclrtoeol(help_window);
wmove(help_window, 0, 0);
wclrtoeol(help_window);
draw_info(0, 0 * col, "C-a", "Home");
draw_info(1, 0 * col, "C-e", "End");
draw_info(0, 1 * col, "C-h", "Backsp");
draw_info(1, 1 * col, "C-d", "Delete");
draw_info(0, 2 * col, "C-c", "Cancel");
draw_info(1, 2 * col, "Ret", "Done");
draw_info(0, 3 * col, "C-k", "CutLineR");
draw_info(1, 3 * col, "C-u", "CutLineL");
draw_info(0, 4 * col, "C-w", "CutWordL");
draw_info(1, 4 * col, "C-x", "CutWordR");
draw_info(0, 5 * col, "C-v", "Paste");
draw_info(1, 5 * col, "C-?", "Hide Help");
}
/******************************************************************************
Print a saymsg that briefly blinks in the status bar
*/
void say(const char *str)
{
strcpy(saymsg, str);
if (strlen(str) > 0)
sayblink = 2 * SAY_BLINKS;
}
/******************************************************************************
Suppress a previous call to say
*/