1 /* Copyright (C) 2008 Ricardo Catalinas Jiménez <jimenezrick@gmail.com>
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 static int command_repetitions
= -1;
22 static chtype previous_key
= 0;
27 while ((key
= getch()) == ERR
)
32 if (key
!= KEY_RESIZE
)
36 if (command_repetitions
== -1)
37 command_repetitions
= key
- '0';
39 command_repetitions
*= 10;
40 command_repetitions
+= key
- '0';
47 if (previous_key
== 'f') {
48 select_next_line_by_first_letter((char) key
);
49 command_repetitions
= -1;
53 } else if (previous_key
== 'F') {
54 select_previous_line_by_first_letter((char) key
);
55 command_repetitions
= -1;
61 if (command_repetitions
== -1)
62 command_repetitions
= 1;
69 print_lines(first_line
, last_line
, TRUE
);
70 select_line_inside_window();
73 case 0x0c: /* Ctrl + l */
74 clearok(curscr
, TRUE
);
75 if (!G_NODE_IS_ROOT(NODE(selected_line
)))
76 select_file(NODE(selected_line
)->parent
);
77 update_directory(NODE(selected_line
));
79 case 0x1b: /* Escape */
80 command_repetitions
= -1;
85 case 0x06: /* Ctrl + f */
86 select_line(last_line
);
87 scroll_tree(command_repetitions
* (getmaxy(tree_window
) - 1));
90 case 0x02: /* Ctrl + b */
91 select_line(first_line
);
92 scroll_tree(command_repetitions
* (-(getmaxy(tree_window
) - 1)));
94 case 0x04: /* Ctrl + d */
95 scroll_tree(command_repetitions
* getmaxy(tree_window
) / 2);
96 select_nth_line(command_repetitions
* getmaxy(tree_window
) / 2);
98 case 0x15: /* Ctrl + u */
99 scroll_tree(command_repetitions
* -getmaxy(tree_window
) / 2);
100 select_nth_line(command_repetitions
* -getmaxy(tree_window
) / 2);
103 if (previous_key
== 'g')
107 select_line(g_list_last(lines
));
109 case 0x07: /* Ctrl + g */
110 if ((info
= get_file_info(NODE(selected_line
))) != NULL
) {
111 print_info(info
, FALSE
);
116 if (previous_key
== 'z')
117 scroll_tree(g_list_position(first_line
, selected_line
) -
118 getmaxy(tree_window
) / 2);
121 select_line(first_line
);
124 select_line(last_line
);
128 select_nth_line(command_repetitions
);
132 select_nth_line(-command_repetitions
);
136 if (FILE(NODE(selected_line
))->type
== directory_type
&&
137 FILE(NODE(selected_line
))->open
== FALSE
)
138 open_directory(NODE(selected_line
));
139 else if (FILE(NODE(selected_line
))->type
== directory_type
&&
140 FILE(NODE(selected_line
))->open
== TRUE
&&
141 FILE(NODE(selected_line
))->show_dotfiles
== FALSE
)
142 show_dotfiles(NODE(selected_line
));
143 else if (FILE(NODE(selected_line
))->type
== file_type
)
144 read_file(NODE(selected_line
));
148 close_parent
: if (FILE(NODE(selected_line
))->type
== directory_type
&&
149 FILE(NODE(selected_line
))->open
== TRUE
&&
150 FILE(NODE(selected_line
))->show_dotfiles
== TRUE
&&
151 !G_NODE_IS_LEAF(NODE(selected_line
)) &&
152 FILE(g_node_first_child(NODE(selected_line
)))->name
[0] == '.')
153 hide_dotfiles(NODE(selected_line
));
154 else if (FILE(NODE(selected_line
))->type
== directory_type
&&
155 FILE(NODE(selected_line
))->open
== TRUE
&&
156 !G_NODE_IS_LEAF(NODE(selected_line
)))
157 close_directory(NODE(selected_line
));
158 else if (!G_NODE_IS_ROOT(NODE(selected_line
))) {
159 select_file(NODE(selected_line
)->parent
);
169 if (g_node_prev_sibling(NODE(selected_line
)) != NULL
)
170 select_file(g_node_prev_sibling(NODE(selected_line
)));
175 if (g_node_next_sibling(NODE(selected_line
)) != NULL
)
176 select_file(g_node_next_sibling(NODE(selected_line
)));
181 if (previous_key
== '[' && !G_NODE_IS_ROOT(NODE(selected_line
)))
182 select_file(NODE(selected_line
)->parent
);
185 if (!G_NODE_IS_ROOT(NODE(selected_line
)))
186 run_shell(NODE(selected_line
)->parent
);
188 run_shell(NODE(selected_line
));
191 case 0x0d: /* Enter */
192 if (FILE(NODE(selected_line
))->type
== file_type
)
193 edit_file(NODE(selected_line
));
196 if (key
!= KEY_RESIZE
) {
197 command_repetitions
= -1;
202 void run_program(GNode
*current_directory
, char *program_name_variable
,
203 char *default_program_name
, char *arg
, gboolean wait_proccess
)
205 char *path
, *pwd
, *program_name
;
210 if ((child
= fork()) != 0 && wait_proccess
) {
212 waitpid(child
, &status
, 0);
214 } else if (child
== 0) {
215 path
= get_path(current_directory
);
216 if (chdir(path
) == -1)
218 pwd
= g_strconcat("PWD=", path
, NULL
);
222 if (!wait_proccess
) {
228 if (program_name_variable
!= NULL
&&
229 (program_name
= getenv(program_name_variable
)) != NULL
) {
230 if (execlp(program_name
, program_name
, arg
, NULL
) == -1)
233 if (execlp(default_program_name
, default_program_name
, arg
, NULL
) == -1)
239 print_lines(first_line
, last_line
, TRUE
);
240 select_line_inside_window();
242 if (wait_proccess
&& WIFEXITED(status
) && WEXITSTATUS(status
) != EXIT_SUCCESS
)
243 PRINT_ERROR_INFO("Something failed when running the external program");
246 void run_shell(GNode
*directory
)
248 run_program(directory
, "SHELL", DEFAULT_SHELL
, NULL
, TRUE
);
251 void read_file(GNode
*file
)
253 if (g_str_has_suffix(FILE(file
)->name
, ".pdf") ||
254 g_str_has_suffix(FILE(file
)->name
, ".pdf.gz") ||
255 g_str_has_suffix(FILE(file
)->name
, ".pdf.Z"))
256 run_program(file
->parent
, NULL
, DEFAULT_PDF_VIEWER
, FILE(file
)->name
, FALSE
);
257 else if (g_str_has_suffix(FILE(file
)->name
, ".ps") ||
258 g_str_has_suffix(FILE(file
)->name
, ".ps.gz") ||
259 g_str_has_suffix(FILE(file
)->name
, ".ps.Z"))
260 run_program(file
->parent
, NULL
, DEFAULT_PS_VIEWER
, FILE(file
)->name
, FALSE
);
261 else if (g_str_has_suffix(FILE(file
)->name
, ".jpg") ||
262 g_str_has_suffix(FILE(file
)->name
, ".jpeg") ||
263 g_str_has_suffix(FILE(file
)->name
, ".png") ||
264 g_str_has_suffix(FILE(file
)->name
, ".gif"))
265 run_program(file
->parent
, NULL
, DEFAULT_IMAGE_VIEWER
, FILE(file
)->name
, FALSE
);
267 run_program(file
->parent
, "PAGER", DEFAULT_PAGER
, FILE(file
)->name
, TRUE
);
270 void edit_file(GNode
*file
)
272 run_program(file
->parent
, "EDITOR", DEFAULT_EDITOR
, FILE(file
)->name
, TRUE
);
This page took 0.520219 seconds and 4 git commands to generate.