aboutsummaryrefslogtreecommitdiffstats
path: root/src/keys.c
blob: 8ad685285bcc45f02a5d6f7a3f3fe9d3b62764c3 (plain) (blame)
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
#include "keys.h"

#include "display.h"
#include "err.h"
#include "fm.h"
#include "log.h"
#include "mem.h"
#include "utils.h"
#include "wm.h"
#include <errno.h>
#include <linux/limits.h>
#include <ncurses.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

bool keymap = false;

void
handling_keys(int ch, fm_info *i)
{
	log_write(LOG_INFO, "ch = %d", ch);
	/* Control(C) or Alt(M) */
	if (ch < 32) {
		switch (ch) {
		case 14: /* C-n, scroll down in Emacs*/
			fm_entries_scroll(i, 1);
			break;
		case 16: /* C-p scroll up in Emacs */
			fm_entries_scroll(i, -1);
			break;
			/* Page down */
		case 4:	 /* C-d, for Vi users' habit */
		case 22: /* C-v, for Emacs users' habit*/
			fm_entries_scroll(i, i->windows[PANE_LEFT]->height / 2);
			break;
			/* Page up */
		case 21: /* C-u */
		case 27: /* M-v */
			fm_entries_scroll(i,
					  -(i->windows[PANE_LEFT]->height / 2));
			break;
		}

		return;
	}

	switch (ch) {
#ifdef KEY_RESIZE
	case KEY_RESIZE:
		handle_resize();
		/* TODO: redraw ui */
		break;
#endif
	/* Quit */
	case 'q':
		endwin();
		exit(0);
	/* Down */
	case 'j':
		fm_entries_scroll(i, 1);
		break;
	case 'k':
		fm_entries_scroll(i, -1);
		break;
	case 'h':
		fm_cd_up(i);
		break;
	case 'l':
		fm_cd_down(i);
		break;
	case 'o':
	case '\n':
	case '\r':
	case KEY_ENTER:
		log_write(LOG_INFO, "Open file");
		if (!(i->cur->items[i->cursor].is_dir)) {
			char filepath[PATH_MAX];
			size_t len =
			    snprintf(filepath, PATH_MAX, "%s/%s", i->cwd,
				     i->cur->items[i->cursor].name);
			if (len >= PATH_MAX) {
				die("snprintf failed");
			}

			char *argv[] = {enveditor, filepath, NULL};
			open_with_editor(argv);
			break;
		} else {
			return;
		}
		break;
	case '+':
		new_dir(i);
		break;
	case 'R':
	case 'r': {
		fm_rename(i);
		break;
	}
	default:
		return;
	}

	need_redraw = true;
}