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 if (key
!= KEY_RESIZE
)
31 if (command_repetitions
== -1)
32 command_repetitions
= key
- '0';
34 command_repetitions
*= 10;
35 command_repetitions
+= key
- '0';
42 if (previous_key
== 'f') {
43 select_next_line_by_first_letter((char) key
);
44 command_repetitions
= -1;
48 } else if (previous_key
== 'F') {
49 select_previous_line_by_first_letter((char) key
);
50 command_repetitions
= -1;
56 if (command_repetitions
== -1)
57 command_repetitions
= 1;
64 print_lines(first_line
, last_line
, TRUE
);
65 select_line_inside_window();
68 case 0x0c: /* Ctrl + l */
69 clearok(curscr
, TRUE
);
70 if (!G_NODE_IS_ROOT(NODE(selected_line
)))
71 select_file(NODE(selected_line
)->parent
);
72 update_directory(NODE(selected_line
));
74 case 0x1b: /* Escape */
75 command_repetitions
= -1;
80 case 0x06: /* Ctrl + f */
81 select_line(last_line
);
82 scroll_tree(command_repetitions
* (getmaxy(tree_window
) - 1));
85 case 0x02: /* Ctrl + b */
86 select_line(first_line
);
87 scroll_tree(command_repetitions
* (-(getmaxy(tree_window
) - 1)));
89 case 0x04: /* Ctrl + d */
90 scroll_tree(command_repetitions
* getmaxy(tree_window
) / 2);
91 select_nth_line(command_repetitions
* getmaxy(tree_window
) / 2);
93 case 0x15: /* Ctrl + u */
94 scroll_tree(command_repetitions
* -getmaxy(tree_window
) / 2);
95 select_nth_line(command_repetitions
* -getmaxy(tree_window
) / 2);
98 if (previous_key
== 'g')
102 select_line(g_list_last(lines
));
104 case 0x07: /* Ctrl + g */
105 if ((info
= get_file_info(NODE(selected_line
))) != NULL
) {
106 print_info(info
, FALSE
);
111 if (previous_key
== 'z')
112 scroll_tree(g_list_position(first_line
, selected_line
) -
113 getmaxy(tree_window
) / 2);
116 select_line(first_line
);
119 select_line(last_line
);
123 select_nth_line(command_repetitions
);
127 select_nth_line(-command_repetitions
);
131 if (FILE(NODE(selected_line
))->type
== directory_type
&&
132 FILE(NODE(selected_line
))->open
== FALSE
)
133 open_directory(NODE(selected_line
));
134 else if (FILE(NODE(selected_line
))->type
== directory_type
&&
135 FILE(NODE(selected_line
))->open
== TRUE
&&
136 FILE(NODE(selected_line
))->show_dotfiles
== FALSE
)
137 show_dotfiles(NODE(selected_line
));
138 else if (FILE(NODE(selected_line
))->type
== file_type
)
139 read_file(NODE(selected_line
));
143 close_parent
: if (FILE(NODE(selected_line
))->type
== directory_type
&&
144 FILE(NODE(selected_line
))->open
== TRUE
&&
145 FILE(NODE(selected_line
))->show_dotfiles
== TRUE
&&
146 !G_NODE_IS_LEAF(NODE(selected_line
)) &&
147 FILE(g_node_first_child(NODE(selected_line
)))->name
[0] == '.')
148 hide_dotfiles(NODE(selected_line
));
149 else if (FILE(NODE(selected_line
))->type
== directory_type
&&
150 FILE(NODE(selected_line
))->open
== TRUE
&&
151 !G_NODE_IS_LEAF(NODE(selected_line
)))
152 close_directory(NODE(selected_line
));
153 else if (!G_NODE_IS_ROOT(NODE(selected_line
))) {
154 select_file(NODE(selected_line
)->parent
);
163 if (g_node_prev_sibling(NODE(selected_line
)) != NULL
)
164 select_file(g_node_prev_sibling(NODE(selected_line
)));
169 if (g_node_next_sibling(NODE(selected_line
)) != NULL
)
170 select_file(g_node_next_sibling(NODE(selected_line
)));
175 if (previous_key
== '[' && !G_NODE_IS_ROOT(NODE(selected_line
)))
176 select_file(NODE(selected_line
)->parent
);
179 if (!G_NODE_IS_ROOT(NODE(selected_line
)))
180 run_shell(NODE(selected_line
)->parent
);
182 run_shell(NODE(selected_line
));
185 case 0x0d: /* Enter */
186 if (FILE(NODE(selected_line
))->type
== file_type
)
187 edit_file(NODE(selected_line
));
190 if (key
!= KEY_RESIZE
) {
191 command_repetitions
= -1;
196 void run_program(GNode
*current_directory
, char *program_name_variable
,
197 char *default_program_name
, char *arg
, gboolean wait_proccess
)
199 char *path
, *pwd
, *program_name
;
204 if ((child
= fork()) != 0 && wait_proccess
) {
206 waitpid(child
, &status
, 0);
208 } else if (child
== 0) {
209 path
= get_path(current_directory
);
210 if (chdir(path
) == -1)
212 pwd
= g_strconcat("PWD=", path
, NULL
);
216 if (!wait_proccess
) {
222 if (program_name_variable
!= NULL
&&
223 (program_name
= getenv(program_name_variable
)) != NULL
) {
224 if (execlp(program_name
, program_name
, arg
, NULL
) == -1)
227 if (execlp(default_program_name
, default_program_name
, arg
, NULL
) == -1)
233 print_lines(first_line
, last_line
, TRUE
);
234 select_line_inside_window();
236 if (wait_proccess
&& WIFEXITED(status
) && WEXITSTATUS(status
) != EXIT_SUCCESS
)
237 PRINT_ERROR_INFO("Something failed when running the external program");
240 void run_shell(GNode
*directory
)
242 run_program(directory
, "SHELL", DEFAULT_SHELL
, NULL
, TRUE
);
245 void read_file(GNode
*file
)
247 if (g_str_has_suffix(FILE(file
)->name
, ".pdf") ||
248 g_str_has_suffix(FILE(file
)->name
, ".pdf.gz") ||
249 g_str_has_suffix(FILE(file
)->name
, ".pdf.Z"))
250 run_program(file
->parent
, NULL
, DEFAULT_PDF_VIEWER
, FILE(file
)->name
, FALSE
);
251 else if (g_str_has_suffix(FILE(file
)->name
, ".ps") ||
252 g_str_has_suffix(FILE(file
)->name
, ".ps.gz") ||
253 g_str_has_suffix(FILE(file
)->name
, ".ps.Z"))
254 run_program(file
->parent
, NULL
, DEFAULT_PS_VIEWER
, FILE(file
)->name
, FALSE
);
255 else if (g_str_has_suffix(FILE(file
)->name
, ".jpg") ||
256 g_str_has_suffix(FILE(file
)->name
, ".jpeg") ||
257 g_str_has_suffix(FILE(file
)->name
, ".png") ||
258 g_str_has_suffix(FILE(file
)->name
, ".gif"))
259 run_program(file
->parent
, NULL
, DEFAULT_IMAGE_VIEWER
, FILE(file
)->name
, FALSE
);
261 run_program(file
->parent
, "PAGER", DEFAULT_PAGER
, FILE(file
)->name
, TRUE
);
264 void edit_file(GNode
*file
)
266 run_program(file
->parent
, "EDITOR", DEFAULT_EDITOR
, FILE(file
)->name
, TRUE
);
This page took 0.354288 seconds and 4 git commands to generate.