Browse Source

Add Norton Commander-like actions for F3 and F4

Přemysl Janouch 1 week ago
parent
commit
4ce6454ebb
Signed by: Přemysl Janouch <p@janouch.name> GPG Key ID: A0420B94F92B9493
1 changed files with 45 additions and 2 deletions
  1. 45
    2
      sdn.cpp

+ 45
- 2
sdn.cpp View File

@@ -384,7 +384,7 @@ enum { ALT = 1 << 24, SYM = 1 << 25 };  // Outside the range of Unicode
384 384
 #define CTRL 31 &
385 385
 
386 386
 #define ACTIONS(XX) XX(NONE) XX(HELP) XX(QUIT) XX(QUIT_NO_CHDIR) \
387
-	XX(CHOOSE) XX(CHOOSE_FULL) XX(SORT_LEFT) XX(SORT_RIGHT) \
387
+	XX(CHOOSE) XX(CHOOSE_FULL) XX(VIEW) XX(EDIT) XX(SORT_LEFT) XX(SORT_RIGHT) \
388 388
 	XX(UP) XX(DOWN) XX(TOP) XX(BOTTOM) XX(PAGE_PREVIOUS) XX(PAGE_NEXT) \
389 389
 	XX(SCROLL_UP) XX(SCROLL_DOWN) XX(CHDIR) XX(GO_START) XX(GO_HOME) \
390 390
 	XX(SEARCH) XX(RENAME) XX(RENAME_PREFILL) \
@@ -401,7 +401,8 @@ static const char *g_action_names[] = {ACTIONS(XX)};
401 401
 
402 402
 static map<wint_t, action> g_normal_actions {
403 403
 	{ALT | '\r', ACTION_CHOOSE_FULL}, {ALT | KEY (ENTER), ACTION_CHOOSE_FULL},
404
-	{'\r', ACTION_CHOOSE}, {KEY (ENTER), ACTION_CHOOSE}, {'h', ACTION_HELP},
404
+	{'\r', ACTION_CHOOSE}, {KEY (ENTER), ACTION_CHOOSE},
405
+	{KEY (F (3)), ACTION_VIEW}, {KEY (F (4)), ACTION_EDIT}, {'h', ACTION_HELP},
405 406
 	{'q', ACTION_QUIT}, {ALT | 'q', ACTION_QUIT_NO_CHDIR},
406 407
 	// M-o ought to be the same shortcut the navigator is launched with
407 408
 	{ALT | 'o', ACTION_QUIT},
@@ -800,6 +801,42 @@ fun show_message (const string &message, int ttl = 30) {
800 801
 	g.message_ttl = ttl;
801 802
 }
802 803
 
804
+fun run_program (initializer_list<const char*> list, const string &filename) {
805
+	endwin ();
806
+
807
+	switch (pid_t child = fork ()) {
808
+		int status;
809
+	case -1:
810
+		break;
811
+	case 0:
812
+		// Put the child in a new foreground process group...
813
+		setpgid (0, 0);
814
+		tcsetpgrp (STDOUT_FILENO, getpgid (0));
815
+
816
+		for (auto pager : list)
817
+			if (pager) execl ("/bin/sh", "/bin/sh", "-c", (string (pager)
818
+				+ " " + shell_escape (filename)).c_str (), NULL);
819
+		_exit (EXIT_FAILURE);
820
+	default:
821
+		// ...and make sure of it in the parent as well
822
+		(void) setpgid (child, child);
823
+		waitpid (child, &status, 0);
824
+		tcsetpgrp (STDOUT_FILENO, getpgid (0));
825
+	}
826
+
827
+	refresh ();
828
+	update ();
829
+}
830
+
831
+fun view (const string &filename) {
832
+	run_program ({(const char *) getenv ("PAGER"), "pager", "cat"}, filename);
833
+}
834
+
835
+fun edit (const string &filename) {
836
+	run_program ({(const char *) getenv ("VISUAL"),
837
+		(const char *) getenv ("EDITOR"), "vi"}, filename);
838
+}
839
+
803 840
 fun run_pager (FILE *contents) {
804 841
 	// We don't really need to set O_CLOEXEC, so we're not going to
805 842
 	rewind (contents);
@@ -992,6 +1029,12 @@ fun handle (wint_t c) -> bool {
992 1029
 	case ACTION_CHOOSE:
993 1030
 		choose (current);
994 1031
 		break;
1032
+	case ACTION_VIEW:
1033
+		view (current.filename);
1034
+		break;
1035
+	case ACTION_EDIT:
1036
+		edit (current.filename);
1037
+		break;
995 1038
 	case ACTION_HELP:
996 1039
 		show_help ();
997 1040
 		break;

Loading…
Cancel
Save