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 / flist_hist.c (95f20e4b488f40fc230c70b1deacd4190b2c5573) (10KiB) (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
/* vifm
 * Copyright (C) 2016 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 "flist_hist.h"

#include <string.h> /* memmove() */

#include "cfg/config.h"
#include "compat/reallocarray.h"
#include "ui/fileview.h"
#include "ui/ui.h"
#include "utils/fs.h"
#include "utils/macros.h"
#include "utils/path.h"
#include "utils/str.h"
#include "filelist.h"
#include "flist_pos.h"

static void navigate_to_history_pos(view_t *view, int pos);
static void free_view_history(view_t *view);
static void reduce_view_history(view_t *view, int new_size);
static void free_view_history_items(const history_t history[], size_t len);
static int find_in_hist(const view_t *view, const view_t *source, int *pos,
		int *rel_pos);
static history_t * find_hist_entry(const view_t *view, const char dir[]);

void
flist_hist_go_back(view_t *view)
{
	/* When in custom view, we don't want to skip top history item. */
	int pos = flist_custom_active(view)
	        ? view->history_pos
	        : (view->history_pos - 1);

	while(pos >= 0)
	{
		const char *const dir = view->history[pos].dir;
		if(is_valid_dir(dir) && !paths_are_equal(view->curr_dir, dir))
		{
			break;
		}

		--pos;
	}

	if(pos >= 0)
	{
		navigate_to_history_pos(view, pos);
	}
}

void
flist_hist_go_forward(view_t *view)
{
	int pos = view->history_pos + 1;

	while(pos <= view->history_num - 1)
	{
		const char *const dir = view->history[pos].dir;
		if(is_valid_dir(dir) && !paths_are_equal(view->curr_dir, dir))
		{
			break;
		}

		++pos;
	}

	if(pos <= view->history_num - 1)
	{
		navigate_to_history_pos(view, pos);
	}
}

/* Changes current directory of the view to one of previously visited
 * locations. */
static void
navigate_to_history_pos(view_t *view, int pos)
{
	curr_stats.drop_new_dir_hist = 1;
	if(change_directory(view, view->history[pos].dir) < 0)
	{
		curr_stats.drop_new_dir_hist = 0;
		return;
	}
	curr_stats.drop_new_dir_hist = 0;

	load_dir_list(view, 0);
	fpos_set_pos(view, fpos_find_by_name(view, view->history[pos].file));

	view->history_pos = pos;
}

void
flist_hist_resize(view_t *view, int new_size)
{
	const int old_size = MAX(cfg.history_len, 0);
	const int delta = new_size - old_size;

	if(new_size <= 0)
	{
		free_view_history(view);
		return;
	}

	if(delta < 0)
	{
		reduce_view_history(view, new_size);
	}

	if(delta > 0 && view->history_num >= new_size)
	{
		/* We must be restarting, don't truncate history. */
		return;
	}

	view->history = reallocarray(view->history, new_size, sizeof(history_t));

	if(delta > 0)
	{
		int real_delta = new_size - MAX(view->history_num, old_size);
		if(real_delta > 0)
		{
			size_t hist_item_len = sizeof(history_t)*real_delta;
			memset(view->history + view->history_num, 0, hist_item_len);
		}
	}
}

/* Clears and frees directory history of the view. */
static void
free_view_history(view_t *view)
{
	free_view_history_items(view->history, view->history_num);
	free(view->history);
	view->history = NULL;

	view->history_num = 0;
	view->history_pos = 0;
}

/* Moves items of directory history when size of history becomes smaller. */
static void
reduce_view_history(view_t *view, int new_size)
{
	const int delta = MIN(view->history_num - new_size, view->history_pos);
	if(delta <= 0)
	{
		return;
	}

	free_view_history_items(view->history, delta);
	memmove(view->history, view->history + delta,
			sizeof(history_t)*(view->history_num - delta));

	if(view->history_num > new_size)
	{
		view->history_num = new_size;
	}
	view->history_pos -= delta;
}

void
flist_hist_save(view_t *view)
{
	flist_hist_setup(view, NULL, NULL, -1, -1);
}

void
flist_hist_setup(view_t *view, const char path[], const char file[],
		int rel_pos, time_t timestamp)
{
	int x;

	/* This could happen on FUSE error. */
	if(view->list_rows <= 0 && file == NULL)
		return;

	if(cfg.history_len <= 0)
		return;

	if(flist_custom_active(view))
		return;

	if(path == NULL)
		path = view->curr_dir;
	if(file == NULL)
		file = get_current_entry(view)->name;
	if(rel_pos < 0)
		rel_pos = view->list_pos - view->top_line;

	if(view->history_num > 0 &&
			stroscmp(view->history[view->history_pos].dir, path) == 0)
	{
		if(curr_stats.load_stage < 2 || file[0] == '\0')
			return;
		x = view->history_pos;
		(void)replace_string(&view->history[x].file, file);
		view->history[x].rel_pos = rel_pos;
		return;
	}

	if(curr_stats.drop_new_dir_hist)
	{
		return;
	}

	if(view->history_num > 0 && view->history_pos != view->history_num - 1)
	{
		x = view->history_num - 1;
		while(x > view->history_pos)
		{
			free_view_history_items(&view->history[x--], 1);
		}
		view->history_num = view->history_pos + 1;
	}
	x = view->history_num;

	/* Directory history can exceed cfg.history_len during restart. */
	if(x >= cfg.history_len)
	{
		int surplus = x - cfg.history_len + 1;
		free_view_history_items(view->history, surplus);
		memmove(view->history, view->history + surplus,
				sizeof(history_t)*(cfg.history_len - 1));

		x = cfg.history_len - 1;
		view->history_num = x;
	}
	view->history[x].dir = strdup(path);
	view->history[x].file = strdup(file);
	view->history[x].timestamp = timestamp;
	view->history[x].rel_pos = rel_pos;
	++view->history_num;
	view->history_pos = view->history_num - 1;
}

/* Frees memory previously allocated for specified history items. */
static void
free_view_history_items(const history_t history[], size_t len)
{
	size_t i;
	for(i = 0; i < len; ++i)
	{
		free(history[i].dir);
		free(history[i].file);
	}
}

void
flist_hist_clear(view_t *view)
{
	int i;
	for(i = 0; i <= view->history_pos && i < view->history_num; ++i)
	{
		view->history[i].file[0] = '\0';
	}
}

void
flist_hist_lookup(view_t *view, const view_t *source)
{
	int pos = 0;
	int rel_pos = -1;

	if(cfg.history_len > 0 && source->history_num > 0 && curr_stats.ch_pos)
	{
		if(!find_in_hist(view, source, &pos, &rel_pos))
		{
			view->list_pos = 0;
			view->curr_line = 0;
			view->top_line = 0;
			return;
		}
	}

	if(pos < 0)
		pos = 0;
	view->list_pos = pos;
	if(rel_pos >= 0)
	{
		view->top_line = pos - MIN(view->window_cells - 1, rel_pos);
		if(view->top_line < 0)
			view->top_line = 0;
		view->curr_line = pos - view->top_line;
	}
	else
	{
		const int last = fpos_get_last_visible_cell(view);
		if(view->list_pos < view->window_cells)
		{
			fview_scroll_back_by(view, view->top_line);
		}
		else if(view->list_pos > last)
		{
			fview_scroll_fwd_by(view, view->list_pos - last);
		}
	}
	(void)fview_enforce_scroll_offset(view);
}

int
flist_hist_find(const view_t *view, entries_t entries, const char dir[],
		int *top)
{
	int pos;

	const history_t *const hist_entry = find_hist_entry(view, dir);
	if(hist_entry == NULL)
	{
		*top = 0;
		return 0;
	}

	for(pos = 0; pos < entries.nentries; ++pos)
	{
		if(stroscmp(entries.entries[pos].name, hist_entry->file) == 0)
		{
			break;
		}
	}
	if(pos >= entries.nentries)
	{
		pos = 0;
	}

	*top = pos - MIN(entries.nentries, hist_entry->rel_pos);
	if(*top < 0)
	{
		*top = 0;
	}

	return pos;
}

/* Searches for current directory of the view in the history of source.  Returns
 * non-zero if something was found, otherwise zero is returned.  On success,
 * *pos and *rel_pos are set, but might be negative if they aren't valid when
 * applied to existing list of files. */
static int
find_in_hist(const view_t *view, const view_t *source, int *pos, int *rel_pos)
{
	const history_t *const hist_entry = find_hist_entry(source, view->curr_dir);
	if(hist_entry != NULL)
	{
		*pos = fpos_find_by_name(view, hist_entry->file);
		*rel_pos = hist_entry->rel_pos;
		return 1;
	}

	if(view->last_dir != NULL &&
			path_starts_with(view->last_dir, view->curr_dir) &&
			stroscmp(view->last_dir, view->curr_dir) != 0 &&
			strchr(view->last_dir + strlen(view->curr_dir) + 1, '/') == NULL)
	{
		/* This handles positioning of cursor on directory we just left by doing
		 * `cd ..` or equivalent. */

		const char *const dir_name = view->last_dir + strlen(view->curr_dir) + 1U;
		*pos = fpos_find_by_name(view, dir_name);
		*rel_pos = -1;
		return 1;
	}

	return 0;
}

void
flist_hist_update(view_t *view, const char dir[], const char file[],
		int rel_pos)
{
	history_t *const hist_entry = find_hist_entry(view, dir);
	if(hist_entry != NULL)
	{
		(void)replace_string(&hist_entry->file, file);
		hist_entry->rel_pos = rel_pos;
	}
}

/* Finds entry in view history by directory path.  Returns pointer to the entry
 * or NULL. */
static history_t *
find_hist_entry(const view_t *view, const char dir[])
{
	history_t *const history = view->history;
	int i = view->history_pos;

	if(view->history_num <= 0)
	{
		return NULL;
	}

	if(stroscmp(history[i].dir, dir) == 0 && history[i].file[0] == '\0')
	{
		--i;
	}

	for(; i >= 0 && history[i].dir[0] != '\0'; --i)
	{
		if(stroscmp(history[i].dir, dir) == 0)
		{
			return &history[i];
		}
	}

	return NULL;
}

void
flist_hist_clone(view_t *dst, const view_t *src)
{
	int i;

	free_view_history_items(dst->history, dst->history_num);
	dst->history_pos = 0;
	dst->history_num = 0;

	for(i = 0; i < src->history_num; ++i)
	{
		const history_t *const hist_entry = &src->history[i];
		flist_hist_setup(dst, hist_entry->dir, hist_entry->file,
				hist_entry->rel_pos, hist_entry->timestamp);
	}

	dst->history_pos = MIN(src->history_pos, dst->history_num - 1);
}

/* vim: set tabstop=2 softtabstop=2 shiftwidth=2 noexpandtab cinoptions-=(0 : */
/* vim: set cinoptions+=t0 : */
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