forked from okbob/pspg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.c
760 lines (704 loc) · 15.9 KB
/
commands.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
/*-------------------------------------------------------------------------
*
* commands.c
* a list of commands and translations between keys and commands
*
* Portions Copyright (c) 2017-2023 Pavel Stehule
*
* IDENTIFICATION
* src/commands.c
*
*-------------------------------------------------------------------------
*/
#include "pspg.h"
#include "commands.h"
int CTRL_HOME;
int CTRL_END;
int CTRL_SHIFT_HOME;
int CTRL_SHIFT_END;
int CTRL_LEFT;
int CTRL_RIGHT;
int SHIFT_LEFT;
int SHIFT_RIGHT;
#ifndef PDCURSES
static int
get_code(const char *capname, int fallback)
{
#ifdef NCURSES_EXT_FUNCS
char *s;
int result;
s = tigetstr((NCURSES_CONST char *) capname);
if (s == NULL || s == (char *) -1)
return fallback;
result = key_defined(s);
return result > 0 ? result : fallback;
#else
return fallback;
#endif
}
#endif
/*
* Set a value of CTRL_HOME and CTRL_END key codes. These codes
* can be redefined on some plaforms.
*/
void
initialize_special_keycodes()
{
#ifdef PDCURSES
CTRL_HOME = CTL_HOME;
CTRL_END = CTL_END;
CTRL_SHIFT_HOME = -1;
CTRL_SHIFT_END = -1;
CTRL_RIGHT = CTL_RIGHT;
CTRL_LEFT = CTL_LEFT;
SHIFT_RIGHT = KEY_SRIGHT;
SHIFT_LEFT = KEY_SLEFT;
#else
#ifdef NCURSES_EXT_FUNCS
use_extended_names(TRUE);
#endif
CTRL_HOME = get_code("kHOM5", 538);
CTRL_END = get_code("kEND5", 533);
CTRL_SHIFT_HOME = get_code("kHOM6", 537);
CTRL_SHIFT_END = get_code("kEND6", 532);
CTRL_RIGHT = get_code("kRIT5", 561);
CTRL_LEFT = get_code("kLFT5", 546);
SHIFT_RIGHT = get_code("kRIT2", 402);
SHIFT_LEFT = get_code("kLFT2", 393);
#endif
}
/*
* For debug purposes
*/
const char *
cmd_string(int cmd)
{
switch (cmd)
{
case cmd_Invalid:
return "Invalid";
case cmd_RESIZE_EVENT:
return "RESIZE";
case cmd_MOUSE_EVENT:
return "MOUSE";
case cmd_ReleaseCols:
return "ReleaseCols";
case cmd_FreezeOneCol:
return "FreezeOneCol";
case cmd_FreezeTwoCols:
return "FreezeTwoCols";
case cmd_FreezeThreeCols:
return "FreezeThreeCols";
case cmd_FreezeFourCols:
return "FreezeFourCols";
case cmd_FreezeFiveCols:
return "FreezeFiveCols";
case cmd_FreezeSixCols:
return "FreezeSixCols";
case cmd_FreezeSevenCols:
return "FreezeSevenCols";
case cmd_FreezeEightCols:
return "FreezeEightCols";
case cmd_FreezeNineCols:
return "FreezeNineCols";
case cmd_SoundToggle:
return "SoundTogle";
case cmd_MouseToggle:
return "MouseTogle";
case cmd_UtfArtToggle:
return "UtfArtToggle";
case cmd_MenuAsciiArtToggle:
return "MenuAsciiArtToggle";
case cmd_CSSearchSet:
return "CSSearchSet";
case cmd_CISearchSet:
return "CISearchSet";
case cmd_USSearchSet:
return "USSearchSet";
case cmd_HighlightLines:
return "HighlightLines";
case cmd_HighlightValues:
return "HighlightValues";
case cmd_NoHighlight:
return "NoHighlight";
case cmd_SetTheme_MidnightBlack:
return "SetTheme_MidnightBlack";
case cmd_SetTheme_Midnight:
return "SetTheme_Midnight";
case cmd_SetTheme_Foxpro:
return "SetTheme_Foxpro";
case cmd_SetTheme_Pdmenu:
return "SetTheme_Pdmenu";
case cmd_SetTheme_White:
return "SetTheme_White";
case cmd_SetTheme_Mutt:
return "SetTheme_Mutt";
case cmd_SetTheme_Pcfand:
return "SetTheme_Pcfand";
case cmd_SetTheme_Green:
return "SetTheme_Green";
case cmd_SetTheme_Blue:
return "SetTheme_Blue";
case cmd_SetTheme_WP:
return "SetTheme_WP";
case cmd_SetTheme_Lowcontrast:
return "SetTheme_Lowcontrast";
case cmd_SetTheme_Darkcyan:
return "SetTheme_Darkcyan";
case cmd_SetTheme_Paradox:
return "SetTheme_Paradox";
case cmd_SetTheme_DBase:
return "SetTheme_DBase";
case cmd_SetTheme_DBasemagenta:
return "SetTheme_DBasemagenta";
case cmd_SetTheme_Red:
return "SetTheme_Red";
case cmd_SetTheme_Simple:
return "SetTheme_Simple";
case cmd_SetTheme_SolarDark:
return "SetTheme_SolarDark";
case cmd_SetTheme_SolarLight:
return "SetTheme_SolarLight";
case cmd_SetTheme_GruvboxLight:
return "SetTheme_GruvboxLight";
case cmd_SetTheme_TaoLight:
return "SetTheme_TaoLight";
case cmd_SetTheme_Flatwhite:
return "SetTheme_Flatwhite";
case cmd_SetTheme_RelationalPipes:
return "SetThemeRelationalPipes";
case cmd_SetTheme_PaperColor:
return "SetTheme_PaperColor";
case cmd_SetTheme:
return "SetTheme";
case cmd_SetCustomTheme:
return "SetCustomTheme";
case cmd_SaveSetup:
return "SaveSetup";
case cmd_Escape:
return "Escape";
case cmd_Quit:
return "Quit";
case cmd_RawOutputQuit:
return "RawOutputQuit";
case cmd_ShowMenu:
return "ShowMenu";
case cmd_FlushBookmarks:
return "FlushBookmarks";
case cmd_ToggleBookmark:
return "ToggleBookmark";
case cmd_PrevBookmark:
return "PrevBookmark";
case cmd_NextBookmark:
return "NextBookmark";
case cmd_CursorUp:
return "CursorUp";
case cmd_CursorDown:
return "CursorDown";
case cmd_ScrollUp:
return "ScrollUp";
case cmd_ScrollDown:
return "ScrollDown";
case cmd_ScrollUpHalfPage:
return "ScrollUpHalfPage";
case cmd_ScrollDownHalfPage:
return "ScrollDownHalfPage";
case cmd_MoveLeft:
return "MoveLeft";
case cmd_MoveRight:
return "MoveRight";
case cmd_MoveCharLeft:
return "MoveCharLeft";
case cmd_MoveCharRight:
return "MoveCharRight";
case cmd_MoveColumnLeft:
return "MoveColumnLeft";
case cmd_MoveColumnRight:
return "MoveColumnRight";
case cmd_CursorFirstRow:
return "CursorFirstRow";
case cmd_CursorLastRow:
return "CursorLastRow";
case cmd_CursorFirstRowPage:
return "CursorFirstRowPage";
case cmd_CursorLastRowPage:
return "CursorLastRowPage";
case cmd_CursorHalfPage:
return "CursorHalfPage";
case cmd_PageUp:
return "PageUp";
case cmd_PageDown:
return "PageDown";
case cmd_ShowFirstCol:
return "ShowFirstCol";
case cmd_ShowLastCol:
return "ShowLastCol";
case cmd_SaveData:
return "SaveData";
case cmd_SaveAsCSV:
return "SaveAsCSV";
case cmd_ForwardSearch:
return "ForwardSearch";
case cmd_BackwardSearch:
return "BackwardSearch";
case cmd_ForwardSearchInSelection:
return "ForwardSearchInSelection";
case cmd_BackwardSearchInSelection:
return "BackwardSearchInSelection";
case cmd_SearchNext:
return "SearchNext";
case cmd_SearchPrev:
return "SearchPrev";
case cmd_SearchColumn:
return "SearchColumn";
case cmd_ShowTopBar:
return "ShowTopBar";
case cmd_ShowBottomBar:
return "ShowBottomBar";
case cmd_RowNumToggle:
return "RowNumToggle";
case cmd_GotoLine:
return "GotoLine";
case cmd_GotoLineRel:
return "GotoLineRel";
case cmd_ShowCursor:
return "ShowCursor";
case cmd_ShowVerticalCursor:
return "ShowVerticalCursor";
case cmd_BoldLabelsToggle:
return "BoldLabelsToggle";
case cmd_BoldCursorToggle:
return "BoldCursorToggle";
case cmd_ShowScrollbar:
return "ShowScrollBar";
case cmd_SortAsc:
return "SortAsc";
case cmd_SortDesc:
return "SortDesc";
case cmd_OriginalSort:
return "OriginalSort";
case cmd_TogglePause:
return "TogglePause";
case cmd_Refresh:
return "Refresh";
case cmd_SetCopyFile:
return "SetCopyFile";
case cmd_SetCopyClipboard:
return "SetCopyClipboard";
case cmd_UseClipboard_CSV:
return "UseClipboardFormatCSV";
case cmd_UseClipboard_TSVC:
return "UseClipboardFormatTSVC";
case cmd_UseClipboard_text:
return "UseClipboardFormattext";
case cmd_UseClipboard_SQL_values:
return "UseClipboardFormatSQLValues";
case cmd_UseClipboard_pipe_separated:
return "UseClipboardFormatPipeSeparated";
case cmd_UseClipboard_INSERT:
return "UseClipboardFormatINSERT";
case cmd_UseClipboard_INSERT_with_comments:
return "UseClipboardFormatINSERTwithcomments";
case cmd_TogleEmptyStringIsNULL:
return "TogleEmptyStringIsNULL";
case cmd_SetOwnNULLString:
return "SetOwnNULLString";
case cmd_Copy:
return "Copy";
case cmd_CopyAllLines:
return "CopyAllLines";
case cmd_CopyTopLines:
return "CopyTopLines";
case cmd_CopyBottomLines:
return "CopyBottomLines";
case cmd_CopyLine:
return "CopyLine";
case cmd_CopyLineExtended:
return "CopyLineExtended";
case cmd_CopyColumn:
return "CopyColumn";
case cmd_CopyMarkedLines:
return "CopyMarkedLines";
case cmd_CopySearchedLines:
return "CopySearchedLines";
case cmd_CopySelected:
return "CopySelected";
case cmd_Mark:
return "Mark";
case cmd_MarkColumn:
return "MarkColumn";
case cmd_MarkAll:
return "MarkAll";
case cmd_Unmark:
return "Unmark";
case cmd_Mark_NestedCursorCommand:
return "MarkNestedCursorCommand";
case cmd_BsCommand:
return "BsCommand";
case cmd_ShowPrimaryScreen:
return "ShowPrimaryScreen";
case cmd_ToggleHighlightOddRec:
return "ToggleHighlightOddRec";
case cmd_ToggleHideHeaderLine:
return "ToggleHideHeaderLine";
default:
return "unknown command";
}
}
bool
is_cmd_RowNumToggle(int c, bool alt)
{
if (alt && c == 'n')
return true;
return false;
}
int
translate_event(int c, bool alt, Options *opts, int *nested_command)
{
if (c == -1)
return cmd_Invalid;
if (alt)
{
switch (c)
{
case 'b':
return cmd_BoldLabelsToggle;
case 'c':
return cmd_ShowCursor;
case 'l':
return cmd_GotoLine;
case 'm':
return cmd_MouseToggle;
case 'n':
return cmd_RowNumToggle;
case 'o':
return cmd_FlushBookmarks;
case 'k':
return cmd_ToggleBookmark;
case 'i':
return cmd_PrevBookmark;
case 'j':
return cmd_NextBookmark;
case 'q':
return cmd_RawOutputQuit;
case 'v':
return cmd_ShowVerticalCursor;
case '2':
return cmd_SaveData;
case '3':
if (opts->quit_on_f3)
return cmd_Quit;
else
return cmd_Mark;
break;
case '7':
return cmd_ForwardSearch;
case '9':
return cmd_ShowMenu;
case PSPG_ESC_CODE:
return cmd_Escape;
case '0':
return cmd_Quit;
case '/':
return cmd_ForwardSearchInSelection;
case '?':
return cmd_BackwardSearchInSelection;
}
}
else
{
switch (c)
{
case KEY_RESIZE:
return cmd_RESIZE_EVENT;
case KEY_MOUSE:
return cmd_MOUSE_EVENT;
case KEY_F(9):
return cmd_ShowMenu;
case KEY_F(10):
case 'q':
return cmd_Quit;
case KEY_F(3):
if (opts->quit_on_f3)
return cmd_Quit;
else
return cmd_Mark;
break;
case KEY_F(15):
return cmd_MarkColumn;
case KEY_IC:
return cmd_Copy;
case KEY_UP:
case 'k':
return cmd_CursorUp;
case KEY_DOWN:
case 'j':
return cmd_CursorDown;
case '0':
return cmd_ReleaseCols;
case '1':
return cmd_FreezeOneCol;
case '2':
return cmd_FreezeTwoCols;
case '3':
return cmd_FreezeThreeCols;
case '4':
return cmd_FreezeFourCols;
case '5':
return cmd_FreezeFiveCols;
case '6':
return cmd_FreezeSixCols;
case '7':
return cmd_FreezeSevenCols;
case '8':
return cmd_FreezeEightCols;
case '9':
return cmd_FreezeNineCols;
case 4: /* CTRL D */
return cmd_ScrollDownHalfPage;
case 21: /* CTRL U */
return cmd_ScrollUpHalfPage;
case 5: /* CTRL E */
return cmd_ScrollDown;
case 25: /* CTRL Y */
return cmd_ScrollUp;
case KEY_LEFT:
case 'h':
return cmd_MoveLeft;
case KEY_RIGHT:
case 'l':
return cmd_MoveRight;
case 'H':
return cmd_CursorFirstRowPage;
case 'L':
return cmd_CursorLastRowPage;
case 'M':
return cmd_CursorHalfPage;
case KEY_PPAGE:
case 2: /* CTRL B */
return cmd_PageUp;
case 3: /* CTRL C */
return cmd_Escape;
case ' ':
return opts->watch_time > 0 ? cmd_TogglePause : cmd_PageDown;
case 6: /* CTRL F */
case KEY_NPAGE:
return cmd_PageDown;
case KEY_HOME:
case '^':
return cmd_ShowFirstCol;
case KEY_END:
case '$':
return cmd_ShowLastCol;
case 's':
return cmd_SaveData;
case '/':
return cmd_ForwardSearch;
case '?':
return cmd_BackwardSearch;
case 'n':
return cmd_SearchNext;
case 'N':
return cmd_SearchPrev;
case 'g':
return cmd_CursorFirstRow;
case 'G':
return cmd_CursorLastRow;
case 'c':
return cmd_SearchColumn;
case 'a':
return cmd_SortAsc;
case 'd':
return cmd_SortDesc;
case 'u':
return cmd_OriginalSort;
case 'R':
case 12: /* CTRL L */
return cmd_Refresh;
case 15: /* CTRL O */
return cmd_ShowPrimaryScreen;
case '\\':
return cmd_BsCommand;
case 1: /* CTRL A */
case '%':
return cmd_MarkAll;
case KEY_SR:
*nested_command = cmd_CursorUp;
return cmd_Mark_NestedCursorCommand;
case KEY_SF:
*nested_command = cmd_CursorDown;
return cmd_Mark_NestedCursorCommand;
case KEY_SNEXT:
*nested_command = cmd_PageDown;
return cmd_Mark_NestedCursorCommand;
case KEY_SPREVIOUS:
*nested_command = cmd_PageUp;
return cmd_Mark_NestedCursorCommand;
}
}
if (c == CTRL_HOME)
return cmd_CursorFirstRow;
else if (c == CTRL_END)
return cmd_CursorLastRow;
else if (c == CTRL_SHIFT_END)
{
*nested_command = cmd_CursorLastRow;
return cmd_Mark_NestedCursorCommand;
}
else if (c == CTRL_SHIFT_HOME)
{
*nested_command = cmd_CursorFirstRow;
return cmd_Mark_NestedCursorCommand;
}
else if (c == CTRL_LEFT)
return cmd_MoveCharLeft;
else if (c == CTRL_RIGHT)
return cmd_MoveCharRight;
else if (c == SHIFT_LEFT)
return cmd_MoveColumnLeft;
else if (c == SHIFT_RIGHT)
return cmd_MoveColumnRight;
return cmd_Invalid;
}
/*
* Returns cmd for theme
*
*/
int
theme_get_cmd(int theme)
{
switch (theme)
{
case 0:
return cmd_SetTheme_MidnightBlack;
case 1:
return cmd_SetTheme_Midnight;
case 2:
return cmd_SetTheme_Foxpro;
case 3:
return cmd_SetTheme_Pdmenu;
case 4:
return cmd_SetTheme_White;
case 5:
return cmd_SetTheme_Mutt;
case 6:
return cmd_SetTheme_Pcfand;
case 7:
return cmd_SetTheme_Green;
case 8:
return cmd_SetTheme_Blue;
case 9:
return cmd_SetTheme_WP;
case 10:
return cmd_SetTheme_Lowcontrast;
case 11:
return cmd_SetTheme_Darkcyan;
case 12:
return cmd_SetTheme_Paradox;
case 13:
return cmd_SetTheme_DBase;
case 14:
return cmd_SetTheme_DBasemagenta;
case 15:
return cmd_SetTheme_Red;
case 16:
return cmd_SetTheme_Simple;
case 17:
return cmd_SetTheme_SolarDark;
case 18:
return cmd_SetTheme_SolarLight;
case 19:
return cmd_SetTheme_GruvboxLight;
case 20:
return cmd_SetTheme_TaoLight;
case 21:
return cmd_SetTheme_Flatwhite;
case 22:
return cmd_SetTheme_RelationalPipes;
case 23:
return cmd_SetTheme_PaperColor;
};
return cmd_Invalid;
}
/*
* Returns theme for cmd
*
*/
int
cmd_get_theme(int cmd)
{
switch (cmd)
{
case cmd_SetTheme_MidnightBlack:
return 0;
case cmd_SetTheme_Midnight:
return 1;
case cmd_SetTheme_Foxpro:
return 2;
case cmd_SetTheme_Pdmenu:
return 3;
case cmd_SetTheme_White:
return 4;
case cmd_SetTheme_Mutt:
return 5;
case cmd_SetTheme_Pcfand:
return 6;
case cmd_SetTheme_Green:
return 7;
case cmd_SetTheme_Blue:
return 8;
case cmd_SetTheme_WP:
return 9;
case cmd_SetTheme_Lowcontrast:
return 10;
case cmd_SetTheme_Darkcyan:
return 11;
case cmd_SetTheme_Paradox:
return 12;
case cmd_SetTheme_DBase:
return 13;
case cmd_SetTheme_DBasemagenta:
return 14;
case cmd_SetTheme_Red:
return 15;
case cmd_SetTheme_Simple:
return 16;
case cmd_SetTheme_SolarDark:
return 17;
case cmd_SetTheme_SolarLight:
return 18;
case cmd_SetTheme_GruvboxLight:
return 19;
case cmd_SetTheme_TaoLight:
return 20;
case cmd_SetTheme_Flatwhite:
return 21;
case cmd_SetTheme_RelationalPipes:
return 22;
case cmd_SetTheme_PaperColor:
return 23;
};
return 1;
}
/*
* This is initial simple design. It can be much
* smarter, but it is good enough for start.
*/
bool
require_complete_load(int cmd)
{
switch (cmd)
{
case cmd_CursorLastRow:
case cmd_SortAsc:
case cmd_SortDesc:
case cmd_OriginalSort:
case cmd_SaveData:
case cmd_Copy:
case cmd_GotoLine:
case cmd_ForwardSearch:
return true;
default:
return false;
}
}