xaizek / vifm (License: GPLv2+) (since 2018-12-07)
Vifm is a file manager with curses interface, which provides Vi[m]-like environment for managing objects within file systems, extended with some useful ideas from mutt.
<root> / src / menus / menus.c (7c7707c31ff7cff281d1fc61a903359dd2edda04) (31KiB) (mode 100644) [raw]
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 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435
/* vifm
 * Copyright (C) 2001 Ken Steen.
 * Copyright (C) 2011 xaizek.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include "menus.h"

#include <curses.h>

#include <assert.h> /* assert() */
#include <stddef.h> /* NULL size_t */
#include <stdio.h> /* FILE */
#include <stdlib.h> /* free() malloc() */
#include <string.h> /* memmove() memset() strdup() strcat() strncat() strchr()
                       strlen() strrchr() */
#include <wchar.h> /* wchar_t wcscmp() */

#include "../cfg/config.h"
#include "../compat/fs_limits.h"
#include "../compat/os.h"
#include "../compat/reallocarray.h"
#include "../engine/mode.h"
#include "../int/term_title.h"
#include "../int/vim.h"
#include "../modes/dialogs/msg_dialog.h"
#include "../modes/cmdline.h"
#include "../modes/menu.h"
#include "../modes/modes.h"
#include "../ui/cancellation.h"
#include "../ui/color_scheme.h"
#include "../ui/colors.h"
#include "../ui/statusbar.h"
#include "../ui/ui.h"
#include "../utils/fs.h"
#include "../utils/log.h"
#include "../utils/macros.h"
#include "../utils/mem.h"
#include "../utils/path.h"
#include "../utils/regexp.h"
#include "../utils/str.h"
#include "../utils/string_array.h"
#include "../utils/test_helpers.h"
#include "../utils/utf8.h"
#include "../utils/utils.h"
#include "../background.h"
#include "../filelist.h"
#include "../flist_hist.h"
#include "../flist_pos.h"
#include "../macros.h"
#include "../marks.h"
#include "../running.h"
#include "../search.h"
#include "../status.h"

static void deinit_menu_data(menu_data_t *m);
static void show_position_in_menu(const menu_data_t *m);
static void open_selected_file(const char path[], int line_num);
static void navigate_to_selected_file(view_t *view, const char path[]);
static void draw_menu_item(menu_state_t *ms, int pos, int line, int clear);
static void draw_search_match(char str[], int start, int end, int line,
		int width, const cchar_t *attrs);
static void normalize_top(menu_state_t *ms);
static void draw_menu_frame(const menu_state_t *ms);
static void output_handler(const char line[], void *arg);
static void append_to_string(char **str, const char suffix[]);
static char * expand_tabulation_a(const char line[], size_t tab_stops);
static void init_menu_state(menu_state_t *ms, menu_data_t *m, view_t *view);
static void replace_menu_data(menu_data_t *m);
static int can_stash_menu(const menu_data_t *m);
static void stash_menu(menu_data_t *m);
static int stash_is_displayed(void);
static void unstash_menu_at(menu_state_t *ms, int index);
static void move_menu_data(menu_data_t *to, menu_data_t *from);
static const char * get_relative_path_base(const menu_data_t *m,
		const view_t *view);
static int menu_and_view_are_in_sync(const menu_data_t *m, const view_t *view);
static int search_menu(menu_state_t *ms, int print_errors);
static int search_menu_forwards(menu_state_t *ms, int start_pos);
static int search_menu_backwards(menu_state_t *ms, int start_pos);
static int navigate_to_match(menu_state_t *ms, int pos);
static int get_match_index(const menu_state_t *ms);
TSTATIC void menus_drop_stash(void);
TSTATIC void menus_set_active(menu_data_t *m);

struct menu_state_t
{
	/* Pointer to data_storage field which can also point to something else for
	 * testing purposes. */
	menu_data_t *d;
	/* Storage for the active menu.  Before a menu is displayed, its data is moved
	 * here. */
	menu_data_t data_storage;

	int current; /* Cursor position on the menu_win. */
	int win_rows;
	int backward_search; /* Search direction. */
	/* Number of menu entries that actually match the regexp. */
	int matching_entries;
	/* Whether search highlight matches are currently highlighted. */
	int search_highlight;
	/* Start and end positions of search match.  If there is no match, values are
	 * equal to -1. */
	short int (*matches)[2];
	char *regexp;
	/* Number of times to repeat search. */
	int search_repeat;
	/* View associated with the menu (e.g. to navigate to a file in it). */
	view_t *view;
}
menu_state;

/* Storage for data of stashable menus in chronological order (newest to
 * oldest). */
static menu_data_t menu_data_stash[10];
/* Index of the last viewed stashed menu. */
static int menu_stash_index;
/* Number of stashed menus. */
static int menu_stash_depth;

void
menus_remove_current(menu_state_t *ms)
{
	menu_data_t *const m = ms->d;
	menus_erase_current(ms);

	remove_from_string_array(m->items, m->len, m->pos);

	if(m->data != NULL)
	{
		remove_from_string_array(m->data, m->len, m->pos);
	}

	if(m->void_data != NULL)
	{
		memmove(m->void_data + m->pos, m->void_data + m->pos + 1,
				sizeof(*m->void_data)*((m->len - 1) - m->pos));
	}

	if(ms->matches != NULL)
	{
		if(ms->matches[m->pos][0] >= 0)
		{
			--ms->matching_entries;
		}
		memmove(ms->matches + m->pos, ms->matches + m->pos + 1,
				sizeof(*ms->matches)*((m->len - 1) - m->pos));
	}

	--m->len;
	menus_partial_redraw(ms);

	menus_set_pos(ms, m->pos);
}

void
menus_erase_current(menu_state_t *ms)
{
	draw_menu_item(ms, ms->d->pos, ms->current, 1);
}

void
menus_init_data(menu_data_t *m, view_t *view, char title[], char empty_msg[])
{
	if(m->initialized)
	{
		menus_reset_data(m);
	}

	m->title = escape_unreadable(title);
	free(title);

	m->top = 0;
	m->len = 0;
	m->pos = 0;
	m->hor_pos = 0;
	m->items = NULL;
	m->data = NULL;
	m->void_data = NULL;
	m->key_handler = NULL;
	m->extra_data = 0;
	m->execute_handler = NULL;
	m->empty_msg = empty_msg;
	m->cwd = strdup(flist_get_dir(view));
	m->state = &menu_state;
	m->initialized = 1;
}

void
menus_reset_data(menu_data_t *m)
{
	if(!m->initialized)
	{
		return;
	}

	if(can_stash_menu(m))
	{
		stash_menu(m);
	}
	else
	{
		deinit_menu_data(m);
	}
}

/* Frees resources taken up by data at most once.  Accepts zero-initialized
 * input as well. */
static void
deinit_menu_data(menu_data_t *m)
{
	if(!m->initialized)
	{
		return;
	}

	/* Menu elements don't always have data associated with them, but len isn't
	 * zero.  That's why we need this check. */
	if(m->data != NULL)
	{
		free_string_array(m->data, m->len);
		m->data = NULL;
	}
	free_string_array(m->items, m->len);
	free(m->void_data);
	free(m->title);
	free(m->empty_msg);
	free(m->cwd);
	m->initialized = 0;
}

void
menus_set_pos(menu_state_t *ms, int pos)
{
	menu_data_t *const m = ms->d;
	int redraw;

	pos = MIN(m->len - 1, MAX(0, pos));
	if(pos < 0)
	{
		return;
	}

	normalize_top(ms);

	redraw = 0;
	if(pos > modmenu_last_line(m))
	{
		m->top = pos - (ms->win_rows - 2 - 1);
		redraw = 1;
	}
	else if(pos < m->top)
	{
		m->top = pos;
		redraw = 1;
	}

	if(cfg.scroll_off > 0)
	{
		int s = MIN(DIV_ROUND_UP(ms->win_rows - 2, 2), cfg.scroll_off);
		if(pos - m->top < s && m->top > 0)
		{
			m->top -= s - (pos - m->top);
			normalize_top(ms);
			redraw = 1;
		}
		if(pos > modmenu_last_line(m) - s)
		{
			m->top += s - (modmenu_last_line(m) - pos);
			normalize_top(ms);
			redraw = 1;
		}
	}

	ms->current = 1 + (pos - m->top);
	m->pos = pos;

	if(redraw)
	{
		menus_partial_redraw(ms);
	}
	else
	{
		draw_menu_item(ms, m->pos, ms->current, 0);
		show_position_in_menu(m);
	}

	checked_wmove(menu_win, ms->current, 2);
}

/* Displays current menu position on a ruler. */
static void
show_position_in_menu(const menu_data_t *m)
{
	if(modes_is_cmdline_like())
	{
		return;
	}

	char pos_buf[32];
	snprintf(pos_buf, sizeof(pos_buf), " %d-%d ", m->pos + 1, m->len);

	ui_ruler_set(pos_buf);
}

void
menus_full_redraw(menu_state_t *ms)
{
	if(resize_for_menu_like() != 0)
	{
		return;
	}

	ms->win_rows = getmaxy(menu_win);

	menus_partial_redraw(ms);
	menus_set_pos(ms, ms->d->pos);
	ui_refresh_win(menu_win);
}

int
menus_goto_file(menu_data_t *m, view_t *view, const char spec[], int try_open)
{
	char *path_buf;
	int line_num;

	path_buf = parse_file_spec(spec, &line_num, get_relative_path_base(m, view));
	if(path_buf == NULL)
	{
		show_error_msg("Memory Error", "Unable to allocate enough memory");
		return 1;
	}

	if(!path_exists(path_buf, NODEREF))
	{
		show_error_msgf("Missing file", "File \"%s\" doesn't exist", path_buf);
		free(path_buf);
		return 1;
	}

	if(try_open)
	{
		open_selected_file(path_buf, line_num);
	}
	else
	{
		navigate_to_selected_file(view, path_buf);
	}

	free(path_buf);
	return 0;
}

/* Opens file specified by its path on the given line number. */
static void
open_selected_file(const char path[], int line_num)
{
	if(os_access(path, R_OK) == 0)
	{
		(void)vim_view_file(path, line_num, -1, 1);
	}
	else
	{
		show_error_msgf("Can't read file", "File \"%s\" is not readable", path);
	}
}

/* Navigates the view to a given dir/file combination specified by the path. */
static void
navigate_to_selected_file(view_t *view, const char path[])
{
	char name[NAME_MAX + 1];
	char *dir = strdup(path);
	char *const last_slash = find_slashr(dir);

	if(last_slash == NULL)
	{
		copy_str(name, sizeof(name), dir);
	}
	else
	{
		*last_slash = '\0';
		copy_str(name, sizeof(name), last_slash + 1);
	}

	if(change_directory(view, dir) >= 0)
	{
		ui_sb_quick_msgf("%s", "Finding the correct directory...");

		load_dir_list(view, 0);

		(void)fpos_ensure_selected(view, name);
	}
	else
	{
		show_error_msgf("Invalid path", "Cannot change dir to \"%s\"", dir);
	}

	free(dir);
}

void
menus_goto_dir(view_t *view, const char path[])
{
	if(!cfg.auto_ch_pos)
	{
		flist_hist_clear(view);
		curr_stats.ch_pos = 0;
	}
	navigate_to(view, path);
	if(!cfg.auto_ch_pos)
	{
		curr_stats.ch_pos = 1;
	}
}

void
menus_partial_redraw(menu_state_t *ms)
{
	int i, pos;
	const int y = getmaxy(menu_win);

	normalize_top(ms);

	werase(menu_win);
	draw_menu_frame(ms);

	for(i = 0, pos = ms->d->top; i < y - 2 && pos < ms->d->len; ++i, ++pos)
	{
		draw_menu_item(ms, pos, i + 1, 0);
	}

	show_position_in_menu(ms->d);
}

/* Draws single menu item at position specified by line argument.  Non-zero
 * clear argument suppresses drawing current items in different color. */
static void
draw_menu_item(menu_state_t *ms, int pos, int line, int clear)
{
	menu_data_t *const m = ms->d;
	int i;
	int off;
	char *item_tail;
	const int width = (curr_stats.load_stage == 0) ? 100 : getmaxx(menu_win) - 2;

	/* Calculate color for the line. */
	col_attr_t col = cfg.cs.color[WIN_COLOR];
	if(cfg.hl_search && ms->search_highlight &&
			ms->matches != NULL && ms->matches[pos][0] >= 0)
	{
		cs_mix_colors(&col, &cfg.cs.color[SELECTED_COLOR]);
	}
	if(!clear && pos == m->pos)
	{
		cs_mix_colors(&col, &cfg.cs.color[CURR_LINE_COLOR]);
	}
	int color_pair = cs_load_color(&col);

	char *escaped = escape_unreadable(m->items[pos]);

	/* Calculate offset of m->hor_pos's character in item text. */
	off = 0;
	i = m->hor_pos;
	while(i-- > 0 && escaped[0] != '\0')
	{
		off += utf8_chrw(escaped + off);
	}

	item_tail = escaped + off;
	replace_char(item_tail, '\t', ' ');

	ui_set_attr(menu_win, &col, color_pair);

	/* Clear the area. */
	checked_wmove(menu_win, line, 1);
	if(curr_stats.load_stage > 0)
	{
		wprintw(menu_win, "%*s", width, "");
	}

	/* Truncate the item to fit the screen if needed. */
	if(utf8_strsw(item_tail) > (size_t)(width - 2))
	{
		char *ellipsed = right_ellipsis(item_tail, width - 2, curr_stats.ellipsis);
		free(escaped);
		escaped = ellipsed;
		item_tail = ellipsed;
	}
	else
	{
		const size_t len = utf8_nstrsnlen(item_tail, width - 2 + 1);
		item_tail[len] = '\0';
	}

	checked_wmove(menu_win, line, 2);
	wprint(menu_win, item_tail);

	if(ms->search_highlight && ms->matches != NULL && ms->matches[pos][0] >= 0)
	{
		const cchar_t cch = cs_color_to_cchar(&col, color_pair);
		draw_search_match(item_tail, ms->matches[pos][0] - off,
				ms->matches[pos][1] - off, line, width, &cch);
	}

	free(escaped);
}

/* Draws search match highlight on the element. */
static void
draw_search_match(char str[], int start, int end, int line, int width,
		const cchar_t *attrs)
{
	if(start == end)
	{
		/* Empty match is not drawn. */
		return;
	}

	const int len = strlen(str);

	/* XXX: conditions for displaying <<< and >>> are for some reason different in
	 *      file views. */

	if(end <= 0)
	{
		/* Match is completely on the left. */

		checked_wmove(menu_win, line, 2);
		wprinta(menu_win, "<<<", attrs, A_REVERSE);
	}
	else if(start >= len)
	{
		/* Match is completely on the right. */

		checked_wmove(menu_win, line, width - 3);
		wprinta(menu_win, ">>>", attrs, A_REVERSE);
	}
	else
	{
		/* Match is at least partially visible. */

		char c;
		int match_start;

		if(start < 0)
		{
			start = 0;
		}
		if(end < len)
		{
			str[end] = '\0';
		}

		/* Calculate number of screen characters before the match. */
		c = str[start];
		str[start] = '\0';
		match_start = utf8_strsw(str);
		str[start] = c;

		checked_wmove(menu_win, line, 2 + match_start);
		wprinta(menu_win, str + start, attrs, A_REVERSE | A_UNDERLINE);
	}
}

/* Ensures that value of m->top lies in a correct range. */
static void
normalize_top(menu_state_t *ms)
{
	ms->d->top = MAX(0, MIN(ms->d->len - (ms->win_rows - 2), ms->d->top));
}

/* Draws box and title of the menu. */
static void
draw_menu_frame(const menu_state_t *ms)
{
	char *title = menus_format_title(ms->d, ms->view);
	if(stash_is_displayed())
	{
		char *full_title = format_str("[%d/%d] %s",
				menu_stash_depth - menu_stash_index, menu_stash_depth, title);
		free(title);
		title = full_title;
	}

	const size_t title_len = getmaxx(menu_win) - 2*4;
	char *const ellipsed = right_ellipsis(title, title_len, curr_stats.ellipsis);
	free(title);

	ui_set_attr(menu_win, &cfg.cs.color[WIN_COLOR], cfg.cs.pair[WIN_COLOR]);

	box(menu_win, 0, 0);
	wattron(menu_win, A_BOLD);
	checked_wmove(menu_win, 0, 3);
	wprint(menu_win, " ");
	wprint(menu_win, ellipsed);
	wprint(menu_win, " ");
	wattroff(menu_win, A_BOLD);

	free(ellipsed);
}

char *
menus_format_title(const menu_data_t *m, struct view_t *view)
{
	const char *const suffix = menu_and_view_are_in_sync(m, view)
	                         ? ""
	                         : replace_home_part(m->cwd);
	const char *const at = (suffix[0] == '\0' ? "" : " @ ");
	return format_str("%s%s%s", m->title, at, suffix);
}

/* Implements process_cmd_output() callback that loads lines to a menu. */
static void
output_handler(const char line[], void *arg)
{
	menu_data_t *const m = arg;
	char *expanded_line;

	m->items = reallocarray(m->items, m->len + 1, sizeof(char *));
	expanded_line = expand_tabulation_a(line, cfg.tab_stop);
	if(expanded_line != NULL)
	{
		m->items[m->len++] = expanded_line;
	}
}

/* Replaces *str with a copy of the with string extended by the suffix.  *str
 * can be NULL in which case it's treated as empty string, equal to the with
 * (then function does nothing).  Returns non-zero if memory allocation
 * failed. */
static void
append_to_string(char **str, const char suffix[])
{
	const char *const non_null_str = (*str == NULL) ? "" : *str;
	char *const appended_str = format_str("%s%s", non_null_str, suffix);
	if(appended_str != NULL)
	{
		free(*str);
		*str = appended_str;
	}
}

/* Clones the line replacing all occurrences of horizontal tabulation character
 * with appropriate number of spaces.  The tab_stops parameter shows how many
 * character position are taken by one tabulation.  Returns newly allocated
 * string. */
static char *
expand_tabulation_a(const char line[], size_t tab_stops)
{
	const size_t tab_count = chars_in_str(line, '\t');
	const size_t extra_line_len = tab_count*tab_stops;
	const size_t expanded_line_len = (strlen(line) - tab_count) + extra_line_len;
	char *const expanded_line = malloc(expanded_line_len + 1);

	if(expanded_line != NULL)
	{
		const char *const end = expand_tabulation(line, (size_t)-1, tab_stops,
				expanded_line);
		assert(*end == '\0' && "The line should be processed till the end");
		(void)end;
	}

	return expanded_line;
}

int
menus_enter(menu_data_t *m, view_t *view)
{
	if(m->len < 1)
	{
		ui_sb_msg(m->empty_msg);
		menus_reset_data(m);
		return 1;
	}

	if(vle_mode_is(MENU_MODE))
	{
		menus_switch_to(m);
		return 0;
	}

	/* This moves data out of `m`, so don't use it below. */
	init_menu_state(&menu_state, m, view);

	ui_setup_for_menu_like();
	term_title_update(menu_state.d->title);
	menus_partial_redraw(&menu_state);
	menus_set_pos(&menu_state, menu_state.d->pos);
	modmenu_enter(menu_state.d, view);
	return 0;
}

/* Initializes menu state structure with default/initial values. */
static void
init_menu_state(menu_state_t *ms, menu_data_t *m, view_t *view)
{
	free(ms->regexp);
	free(ms->matches);

	/* Move menu data into the state to avoid issues with data initialization on
	 * opening menu from within a menu of the same kind. */
	move_menu_data(&ms->data_storage, m);
	ms->d = &ms->data_storage;

	ms->current = 1;
	ms->win_rows = getmaxy(menu_win);
	ms->backward_search = 0;
	ms->matching_entries = 0;
	ms->search_highlight = 1;
	ms->matches = NULL;
	ms->regexp = NULL;
	ms->search_repeat = 0;
	ms->view = view;
}

void
menus_rotate(menu_data_t *new_m, menu_data_t *current_m)
{
	move_menu_data(current_m, new_m->state->d);
	replace_menu_data(new_m);
}

void
menus_switch_to(menu_data_t *m)
{
	menu_state_t *ms = m->state;
	if(ms->d != NULL)
	{
		if(stash_is_displayed())
		{
			move_menu_data(&menu_data_stash[menu_stash_index], ms->d);
		}
		else if(can_stash_menu(ms->d))
		{
			stash_menu(ms->d);
		}
	}

	replace_menu_data(m);
}

/* Changes active menu data. */
static void
replace_menu_data(menu_data_t *m)
{
	menu_state.current = 1;
	menu_state.matching_entries = 0;
	free(menu_state.matches);
	menu_state.matches = NULL;

	move_menu_data(&menu_state.data_storage, m);
	menu_state.d = &menu_state.data_storage;

	menus_partial_redraw(m->state);
	menus_set_pos(m->state, m->pos);
	ui_refresh_win(menu_win);
}

char *
menus_get_targets(view_t *view)
{
	if(view->selected_files > 0 ||
			(view->pending_marking && flist_count_marked(view) > 0))
	{
		return ma_expand("%f", NULL, NULL, MER_SHELL_OP);
	}

	if(!flist_custom_active(view))
	{
		return strdup(".");
	}

	return (vifm_chdir(flist_get_dir(view)) == 0) ? strdup(".") : NULL;
}

/* Checks whether menu can be stashed.  Returns non-zero if so. */
static int
can_stash_menu(const menu_data_t *m)
{
	/* Interested only in non-empty stashable menus. */
	return (m->stashable && m->len > 0);
}

/* Stores menu data for restoring it later. */
static void
stash_menu(menu_data_t *m)
{
	if(stash_is_displayed())
	{
		/* Re-use the same stash and do not drop newer menus until another one is
		 * added. */
	}
	else if(menu_stash_index > 0)
	{
		/* Release previously stashed menus from the head of the stack, if any. */
		int i;
		for(i = 0; i < menu_stash_index; ++i)
		{
			deinit_menu_data(&menu_data_stash[i]);
		}

		/* Drop the head. */
		mem_shl(menu_data_stash, ARRAY_LEN(menu_data_stash),
				sizeof(menu_data_stash[0]), /*offset=*/(menu_stash_index - 1));
		menu_stash_depth -= menu_stash_index - 1;
		menu_stash_index = 0;
	}
	else
	{
		if(++menu_stash_depth > (int)ARRAY_LEN(menu_data_stash))
		{
			deinit_menu_data(&menu_data_stash[ARRAY_LEN(menu_data_stash) - 1]);
			menu_stash_depth = ARRAY_LEN(menu_data_stash);
		}

		mem_shr(menu_data_stash, ARRAY_LEN(menu_data_stash),
				sizeof(menu_data_stash[0]), /*offset=*/1);

		/* We stole data of this item while moving memory. */
		menu_data_stash[0].initialized = 0;
	}

	move_menu_data(&menu_data_stash[menu_stash_index], m);
}

/* Checks whether menu displays a stash.  Returns non-zero if so. */
static int
stash_is_displayed(void)
{
	return menu_stash_index < menu_stash_depth
	    && !menu_data_stash[menu_stash_index].initialized;
}

void
menus_put_on_stash(menu_state_t *ms)
{
	if(can_stash_menu(ms->d))
	{
		stash_menu(ms->d);

		/* Unstash the menu to keep using it. */
		move_menu_data(ms->d, &menu_data_stash[menu_stash_index]);
	}
}

int
menus_unstash(view_t *view)
{
	if(menu_stash_depth == 0)
	{
		ui_sb_msg("No saved menu to display");
		return 1;
	}

	return menus_enter(&menu_data_stash[menu_stash_index], view);
}

int
menus_unstash_older(menu_state_t *ms)
{
	/* Starting viewing stashes requires special handling. */
	if(!stash_is_displayed())
	{
		if(menu_stash_depth == 0)
		{
			/* An older stash doesn't exist. */
			return 1;
		}

		if(!can_stash_menu(ms->d))
		{
			/* Behave as :copen in this case. */
			unstash_menu_at(ms, menu_stash_index);
			return 0;
		}

		/* Stash current menu the same way it would be done on closing it and
		 * proceed as usual, again producing the same result as leaving the mode,
		 * running :copen and then :colder. */
		stash_menu(ms->d);
	}

	if(menu_stash_index >= menu_stash_depth - 1)
	{
		return 1;
	}

	unstash_menu_at(ms, menu_stash_index + 1);
	return 0;
}

int
menus_unstash_newer(menu_state_t *ms)
{
	if((can_stash_menu(ms->d) && !stash_is_displayed()) || menu_stash_index == 0)
	{
		return 1;
	}

	unstash_menu_at(ms, menu_stash_index - 1);
	return 0;
}

void
menus_unstash_at(menu_state_t *ms, int index)
{
	unstash_menu_at(ms, menu_stash_depth - 1 - index);
}

/* Loads a stashed menu specified by its index.  If another stashed menu was
 * active, it's saved back to the stash. */
static void
unstash_menu_at(menu_state_t *ms, int index)
{
	if(stash_is_displayed())
	{
		move_menu_data(&menu_data_stash[menu_stash_index], ms->d);
	}

	menu_stash_index = index;
	replace_menu_data(&menu_data_stash[index]);
}

/* Changes location of menu data while managing its initialized flag. */
static void
move_menu_data(menu_data_t *to, menu_data_t *from)
{
	assert(to != from && "Can't move menu data into itself.");
	assert(from->initialized && "Uninitialized menus shouldn't be moved.");

	deinit_menu_data(to);
	*to = *from;
	from->initialized = 0;
}

const menu_data_t *
menus_get_stash(int index, int *current)
{
	if(index >= menu_stash_depth)
	{
		return NULL;
	}

	int real_index = menu_stash_depth - 1 - index;
	*current = (real_index == menu_stash_index);
	return &menu_data_stash[real_index];
}

KHandlerResponse
menus_def_khandler(view_t *view, menu_data_t *ms, const wchar_t keys[])
{
	if(wcscmp(keys, L"gf") == 0)
	{
		(void)menus_goto_file(ms, view, ms->items[ms->pos], 0);
		return KHR_CLOSE_MENU;
	}
	else if(wcscmp(keys, L"e") == 0)
	{
		(void)menus_goto_file(ms, view, ms->items[ms->pos], 1);
		return KHR_REFRESH_WINDOW;
	}
	else if(wcscmp(keys, L"c") == 0)
	{
		/* Insert just file name. */
		int line_num;
		const char *const rel_base = get_relative_path_base(ms, view);
		char *const path = parse_file_spec(ms->items[ms->pos], &line_num, rel_base);
		if(path == NULL)
		{
			show_error_msg("Command insertion", "No valid filename found");
			return KHR_REFRESH_WINDOW;
		}
		modmenu_morph_into_cline(CLS_COMMAND, path, 1);
		free(path);
		return KHR_MORPHED_MENU;
	}

	return KHR_UNHANDLED;
}

int
menus_to_custom_view(menu_state_t *ms, view_t *view, int very)
{
	int i;
	char *current = NULL;
	const char *const rel_base = get_relative_path_base(ms->d, view);

	flist_custom_start(view, ms->d->title);

	for(i = 0; i < ms->d->len; ++i)
	{
		char *path;
		int line_num;

		/* Skip empty lines. */
		if(skip_whitespace(ms->d->items[i])[0] == '\0')
		{
			continue;
		}

		path = parse_file_spec(ms->d->items[i], &line_num, rel_base);
		if(path == NULL)
		{
			continue;
		}

		flist_custom_add(view, path);

		/* Use either exact position or the next path. */
		if(i == ms->d->pos || (current == NULL && i > ms->d->pos))
		{
			current = path;
			continue;
		}

		free(path);
	}

	/* If current line and none of the lines below didn't contain valid path, try
	 * to use file above cursor position. */
	if(current == NULL && view->custom.entry_count != 0)
	{
		char full_path[PATH_MAX + 1];
		get_full_path_of(&view->custom.entries[view->custom.entry_count - 1],
				sizeof(full_path), full_path);

		current = strdup(full_path);
	}

	if(flist_custom_finish(view, very ? CV_VERY : CV_REGULAR, 0) != 0)
	{
		free(current);
		return 1;
	}

	if(current != NULL)
	{
		flist_goto_by_path(view, current);
		free(current);
	}

	return 0;
}

/* Gets base for relative paths navigated to from the menu.  Returns the
 * path.  The purpose is to omit "././" or "..././..." in paths shown to a
 * user. */
static const char *
get_relative_path_base(const menu_data_t *m, const view_t *view)
{
	if(menu_and_view_are_in_sync(m, view))
	{
		return ".";
	}
	return m->cwd;
}

/* Checks whether menu working directory and current directory of the view are
 * in sync.  Returns non-zero if so, otherwise zero is returned. */
static int
menu_and_view_are_in_sync(const menu_data_t *m, const view_t *view)
{
	/* NULL check is for tests. */
	return (view == NULL || paths_are_same(m->cwd, flist_get_dir(view)));
}

int
menus_capture(view_t *view, const char cmd[], int user_sh, menu_data_t *m,
		MacroFlags flags)
{
	if(ma_flags_present(flags, MF_CUSTOMVIEW_OUTPUT) ||
			ma_flags_present(flags, MF_VERYCUSTOMVIEW_OUTPUT))
	{
		rn_for_flist(view, cmd, m->title, user_sh, flags);
		menus_reset_data(m);
		return 0;
	}

	FILE *input_tmp = make_in_file(view, flags);

	if(process_cmd_output("Loading menu", cmd, input_tmp, user_sh, 0,
				&output_handler, m) != 0)
	{
		show_error_msgf("Trouble running command", "Unable to run: %s", cmd);
		return 0;
	}

	if(input_tmp != NULL)
	{
		fclose(input_tmp);
	}

	if(ui_cancellation_requested())
	{
		append_to_string(&m->title, "(cancelled)");
		append_to_string(&m->empty_msg, " (cancelled)");
	}

	return menus_enter(m, view);
}

void
menus_search_repeat(menu_state_t *ms, int backward)
{
	if(is_null_or_empty(ms->regexp))
	{
		ui_sb_err("No search pattern set");
		curr_stats.save_msg = 1;
		return;
	}

	ms->backward_search = backward;
	(void)menus_search(NULL, ms->d, 1);
	ui_refresh_win(menu_win);

	if(ms->matching_entries > 0)
	{
		ui_sb_msgf("(%d of %d) %c%s", get_match_index(ms), ms->matching_entries,
				backward ? '?' : '/', ms->regexp);
	}

	curr_stats.save_msg = 1;
}

int
menus_search(const char pattern[], menu_data_t *m, int print_errors)
{
	menu_state_t *const ms = m->state;
	const int do_search = (pattern != NULL || ms->matches == NULL);
	int save = 0;
	int i;

	if(pattern != NULL)
	{
		replace_string(&ms->regexp, pattern);
	}

	if(do_search)
	{
		/* Reactivate match highlighting on search. */
		ms->search_highlight = 1;
		if(search_menu(ms, print_errors) != 0)
		{
			menus_partial_redraw(ms);
			menus_set_pos(ms, m->pos);
			return -1;
		}
		menus_partial_redraw(ms);
	}

	for(i = 0; i < ms->search_repeat; ++i)
	{
		if(ms->backward_search)
		{
			save = search_menu_backwards(ms, m->pos - 1);
		}
		else
		{
			save = search_menu_forwards(ms, m->pos + 1);
		}
	}
	return save;
}

/* Goes through all menu items and marks those that match search pattern.
 * Returns non-zero on error. */
static int
search_menu(menu_state_t *ms, int print_errors)
{
	menu_data_t *const m = ms->d;
	int cflags;
	regex_t re;
	int err;
	int i;

	if(ms->matches == NULL)
	{
		ms->matches = reallocarray(NULL, m->len, sizeof(*ms->matches));
	}

	memset(ms->matches, -1, 2*sizeof(**ms->matches)*m->len);
	ms->matching_entries = 0;

	if(ms->regexp[0] == '\0')
	{
		return 0;
	}

	cflags = get_regexp_cflags(ms->regexp);
	err = regexp_compile(&re, ms->regexp, cflags);
	if(err != 0)
	{
		if(print_errors)
		{
			ui_sb_errf("Regexp error: %s", get_regexp_error(err, &re));
		}
		regfree(&re);
		return -1;
	}

	for(i = 0; i < m->len; ++i)
	{
		regmatch_t matches[1];
		const char *item = m->items[i];
		if(regexec(&re, item, 1, matches, 0) == 0)
		{
			ms->matches[i][0] = matches[0].rm_so
			                  + escape_unreadableo(item, matches[0].rm_so);
			ms->matches[i][1] = matches[0].rm_eo
			                  + escape_unreadableo(item, matches[0].rm_eo);

			++ms->matching_entries;
		}
	}
	regfree(&re);
	return 0;
}

/* Looks for next matching element in forward direction from current position.
 * Returns new value for save_msg flag. */
static int
search_menu_forwards(menu_state_t *ms, int start_pos)
{
	int match_up = -1;
	int match_down = -1;
	int i;

	for(i = 0; i < ms->d->len; ++i)
	{
		if(ms->matches[i][0] < 0)
		{
			continue;
		}

		if(match_up < 0 && i < start_pos)
		{
			match_up = i;
		}
		if(match_down < 0 && i >= start_pos)
		{
			match_down = i;
		}
	}

	if(!cfg.wrap_scan && match_down <= -1)
	{
		ui_sb_errf("Search hit BOTTOM without match for: %s", ms->regexp);
		return 1;
	}

	return navigate_to_match(ms, (match_down > -1) ? match_down : match_up);
}

/* Looks for next matching element in backward direction from current position.
 * Returns new value for save_msg flag. */
static int
search_menu_backwards(menu_state_t *ms, int start_pos)
{
	int match_up = -1;
	int match_down = -1;
	int i;

	for(i = ms->d->len - 1; i > -1; --i)
	{
		if(ms->matches[i][0] < 0)
		{
			continue;
		}

		if(match_up < 0 && i <= start_pos)
		{
			match_up = i;
		}
		if(match_down < 0 && i > start_pos)
		{
			match_down = i;
		}
	}

	if(!cfg.wrap_scan && match_up <= -1)
	{
		ui_sb_errf("Search hit TOP without match for: %s", ms->regexp);
		return 1;
	}

	return navigate_to_match(ms, (match_up > -1) ? match_up : match_down);
}

/* Tries to navigate to menu search match specified via pos argument.  If pos is
 * negative, match wasn't found and the message is printed.  Returns new value
 * for save_msg flag. */
static int
navigate_to_match(menu_state_t *ms, int pos)
{
	if(pos > -1)
	{
		if(!ms->search_highlight)
		{
			/* Might need to highlight other items, so redraw whole menu. */
			ms->search_highlight = 1;
			ms->d->pos = pos;
			menus_partial_redraw(ms);
		}
		else
		{
			menus_erase_current(ms);
			menus_set_pos(ms, pos);
		}
		menus_search_print_msg(ms->d);
	}
	else
	{
		menus_set_pos(ms, ms->d->pos);
		if(cfg.wrap_scan)
		{
			menus_search_print_msg(ms->d);
		}
	}
	return 1;
}

void
menus_search_print_msg(const menu_data_t *m)
{
	const menu_state_t *ms = m->state;
	int cflags;
	regex_t re;
	int err;

	/* Can be NULL after regex compilation failure. */
	if(ms->regexp == NULL)
	{
		return;
	}

	cflags = get_regexp_cflags(ms->regexp);
	err = regexp_compile(&re, ms->regexp, cflags);

	if(err != 0)
	{
		ui_sb_errf("Regexp (%s) error: %s", ms->regexp, get_regexp_error(err, &re));
		regfree(&re);
		return;
	}

	regfree(&re);

	if(ms->matching_entries > 0)
	{
		ui_sb_msgf("%d of %d %s", get_match_index(ms), ms->matching_entries,
				(ms->matching_entries == 1) ? "match" : "matches");
	}
	else
	{
		ui_sb_errf("No matches for: %s", ms->regexp);
	}
}

/* Calculates the index of the current match from the list of matches.  Returns
 * the index. */
static int
get_match_index(const menu_state_t *ms)
{
	int n, i;

	n = (ms->matches[0][0] >= 0 ? 1 : 0);
	i = 0;
	while(i++ < ms->d->pos)
	{
		if(ms->matches[i][0] >= 0)
		{
			++n;
		}
	}

	return n;
}

void
menus_search_reset_hilight(menu_state_t *ms)
{
	ms->search_highlight = 0;
	menus_full_redraw(ms);
}

int
menus_search_matched(const menu_data_t *m)
{
	return m->state->matching_entries;
}

void
menus_search_reset(menu_state_t *ms, int backward, int new_repeat_count)
{
	ms->search_repeat = new_repeat_count;
	ms->backward_search = backward;
	update_string(&ms->regexp, NULL);
}

/* Clears stashed navigation menus. */
TSTATIC void
menus_drop_stash(void)
{
	int i;
	for(i = 0; i < menu_stash_depth; ++i)
	{
		deinit_menu_data(&menu_data_stash[i]);
	}

	menu_stash_depth = 0;
	menu_stash_index = 0;
}

TSTATIC void
menus_set_active(menu_data_t *m)
{
	menu_state.d = m;
}

/* vim: set tabstop=2 softtabstop=2 shiftwidth=2 noexpandtab cinoptions-=(0 : */
/* vim: set cinoptions+=t0 filetype=c : */
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/vifm

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/vifm

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master