2016-04-18 13:47:37 +08:00
|
|
|
// Prototypes for history functions, part of the user interface.
|
2005-10-04 23:11:39 +08:00
|
|
|
#ifndef FISH_HISTORY_H
|
|
|
|
#define FISH_HISTORY_H
|
|
|
|
|
2016-04-21 14:00:54 +08:00
|
|
|
// IWYU pragma: no_include <cstddef>
|
2015-07-25 23:14:25 +08:00
|
|
|
#include <pthread.h>
|
2017-02-13 12:24:22 +08:00
|
|
|
#include <stddef.h>
|
2015-07-25 23:14:25 +08:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <time.h>
|
2016-11-15 13:31:51 +08:00
|
|
|
#include <wctype.h>
|
2017-02-14 12:37:27 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
#include <deque>
|
2016-05-04 07:23:30 +08:00
|
|
|
#include <memory>
|
2015-07-25 23:14:25 +08:00
|
|
|
#include <string>
|
2018-01-31 10:30:30 +08:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <unordered_set>
|
2016-04-18 13:47:37 +08:00
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
2016-04-21 14:00:54 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
#include "common.h"
|
2016-04-21 14:00:54 +08:00
|
|
|
#include "wutil.h" // IWYU pragma: keep
|
2012-02-06 08:42:24 +08:00
|
|
|
|
2016-07-14 13:33:50 +08:00
|
|
|
struct io_streams_t;
|
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Fish supports multiple shells writing to history at once. Here is its strategy:
|
|
|
|
//
|
|
|
|
// 1. All history files are append-only. Data, once written, is never modified.
|
|
|
|
//
|
|
|
|
// 2. A history file may be re-written ("vacuumed"). This involves reading in the file and writing a
|
|
|
|
// new one, while performing maintenance tasks: discarding items in an LRU fashion until we reach
|
|
|
|
// the desired maximum count, removing duplicates, and sorting them by timestamp (eventually, not
|
|
|
|
// implemented yet). The new file is atomically moved into place via rename().
|
|
|
|
//
|
|
|
|
// 3. History files are mapped in via mmap(). Before the file is mapped, the file takes a fcntl read
|
|
|
|
// lock. The purpose of this lock is to avoid seeing a transient state where partial data has been
|
|
|
|
// written to the file.
|
|
|
|
//
|
|
|
|
// 4. History is appended to under a fcntl write lock.
|
|
|
|
//
|
|
|
|
// 5. The chaos_mode boolean can be set to true to do things like lower buffer sizes which can
|
|
|
|
// trigger race conditions. This is useful for testing.
|
2012-12-03 17:53:52 +08:00
|
|
|
|
2012-07-25 13:31:31 +08:00
|
|
|
typedef std::vector<wcstring> path_list_t;
|
2012-02-16 03:33:41 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
enum history_search_type_t {
|
2016-07-30 12:24:26 +08:00
|
|
|
// Search for commands exactly matching the given string.
|
|
|
|
HISTORY_SEARCH_TYPE_EXACT = 1,
|
|
|
|
// Search for commands containing the given string.
|
2012-02-07 02:52:13 +08:00
|
|
|
HISTORY_SEARCH_TYPE_CONTAINS,
|
2016-07-30 12:24:26 +08:00
|
|
|
// Search for commands starting with the given string.
|
2017-09-16 04:43:45 +08:00
|
|
|
HISTORY_SEARCH_TYPE_PREFIX,
|
|
|
|
// Search for commands containing the given glob pattern.
|
|
|
|
HISTORY_SEARCH_TYPE_CONTAINS_GLOB,
|
|
|
|
// Search for commands starting with the given glob pattern.
|
2018-02-04 06:34:28 +08:00
|
|
|
HISTORY_SEARCH_TYPE_PREFIX_GLOB
|
2012-02-07 02:52:13 +08:00
|
|
|
};
|
|
|
|
|
2014-03-29 14:22:03 +08:00
|
|
|
typedef uint32_t history_identifier_t;
|
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
class history_item_t {
|
2012-02-06 08:42:24 +08:00
|
|
|
friend class history_t;
|
2012-02-16 16:24:27 +08:00
|
|
|
friend class history_tests_t;
|
2012-02-06 08:42:24 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
private:
|
|
|
|
// Attempts to merge two compatible history items together.
|
2012-03-02 06:56:34 +08:00
|
|
|
bool merge(const history_item_t &item);
|
2012-02-06 08:42:24 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// The actual contents of the entry.
|
2016-09-24 11:12:15 +08:00
|
|
|
wcstring contents; // value as entered by the user
|
|
|
|
wcstring contents_lower; // value normalized to all lowercase for case insensitive comparisons
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Original creation time for the entry.
|
2012-11-19 08:30:30 +08:00
|
|
|
time_t creation_timestamp;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Sometimes unique identifier used for hinting.
|
2014-03-29 14:22:03 +08:00
|
|
|
history_identifier_t identifier;
|
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Paths that we require to be valid for this item to be autosuggested.
|
2012-02-16 03:33:41 +08:00
|
|
|
path_list_t required_paths;
|
2015-09-19 11:47:38 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
public:
|
2016-09-24 11:12:15 +08:00
|
|
|
explicit history_item_t(const wcstring &str, time_t when = 0, history_identifier_t ident = 0);
|
2016-05-06 06:09:31 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
const wcstring &str() const { return contents; }
|
2016-09-24 11:12:15 +08:00
|
|
|
const wcstring &str_lower() const { return contents_lower; }
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
bool empty() const { return contents.empty(); }
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Whether our contents matches a search term.
|
2016-09-24 11:12:15 +08:00
|
|
|
bool matches_search(const wcstring &term, enum history_search_type_t type,
|
|
|
|
bool case_sensitive) const;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
time_t timestamp() const { return creation_timestamp; }
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
const path_list_t &get_required_paths() const { return required_paths; }
|
2017-05-05 13:42:42 +08:00
|
|
|
void set_required_paths(const path_list_t &paths) { required_paths = paths; }
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
bool operator==(const history_item_t &other) const {
|
|
|
|
return contents == other.contents && creation_timestamp == other.creation_timestamp &&
|
2012-02-16 16:24:27 +08:00
|
|
|
required_paths == other.required_paths;
|
|
|
|
}
|
2012-06-16 07:22:37 +08:00
|
|
|
};
|
2012-06-05 12:24:42 +08:00
|
|
|
|
2014-03-29 14:22:03 +08:00
|
|
|
typedef std::deque<history_item_t> history_item_list_t;
|
|
|
|
|
2018-07-15 05:29:19 +08:00
|
|
|
class history_file_contents_t;
|
2012-02-06 08:42:24 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
class history_t {
|
2012-02-16 16:24:27 +08:00
|
|
|
friend class history_tests_t;
|
2012-02-06 08:42:24 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
private:
|
2018-07-15 05:29:19 +08:00
|
|
|
// No copying or moving.
|
|
|
|
history_t() = delete;
|
|
|
|
history_t(const history_t &) = delete;
|
|
|
|
history_t(history_t &&) = delete;
|
|
|
|
history_t &operator=(const history_t &) = delete;
|
|
|
|
history_t &operator=(history_t &&) = delete;
|
2016-04-18 13:47:37 +08:00
|
|
|
|
|
|
|
// Privately add an item. If pending, the item will not be returned by history searches until a
|
|
|
|
// call to resolve_pending.
|
2015-04-20 17:04:17 +08:00
|
|
|
void add(const history_item_t &item, bool pending = false);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Lock for thread safety.
|
2018-12-31 10:15:49 +08:00
|
|
|
std::mutex lock;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Internal function.
|
2012-02-16 16:24:27 +08:00
|
|
|
void clear_file_state();
|
2012-02-06 08:42:24 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// The name of this list. Used for picking a suitable filename and for switching modes.
|
2012-11-19 08:30:30 +08:00
|
|
|
const wcstring name;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// New items. Note that these are NOT discarded on save. We need to keep these around so we can
|
|
|
|
// distinguish between items in our history and items in the history of other shells that were
|
|
|
|
// started after we were started.
|
2014-03-29 14:22:03 +08:00
|
|
|
history_item_list_t new_items;
|
2012-12-03 18:25:08 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// The index of the first new item that we have not yet written.
|
2018-07-15 02:47:34 +08:00
|
|
|
size_t first_unwritten_new_item_index{0};
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Whether we have a pending item. If so, the most recently added item is ignored by
|
|
|
|
// item_at_index.
|
2018-07-15 02:47:34 +08:00
|
|
|
bool has_pending_item{false};
|
2015-09-19 11:47:38 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Whether we should disable saving to the file for a time.
|
2018-07-15 02:47:34 +08:00
|
|
|
uint32_t disable_automatic_save_counter{0};
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Deleted item contents.
|
2017-08-20 07:27:24 +08:00
|
|
|
std::unordered_set<wcstring> deleted_items;
|
2012-06-05 12:24:42 +08:00
|
|
|
|
2018-07-15 05:29:19 +08:00
|
|
|
// The buffer containing the history file contents.
|
|
|
|
std::unique_ptr<history_file_contents_t> file_contents;
|
2012-02-06 08:42:24 +08:00
|
|
|
|
2018-07-15 05:29:19 +08:00
|
|
|
// The file ID of the history file.
|
|
|
|
file_id_t history_file_id;
|
2012-06-16 07:22:37 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// The boundary timestamp distinguishes old items from new items. Items whose timestamps are <=
|
|
|
|
// the boundary are considered "old". Items whose timestemps are > the boundary are new, and are
|
|
|
|
// ignored by this instance (unless they came from this instance). The timestamp may be adjusted
|
|
|
|
// by incorporate_external_changes().
|
2014-07-26 01:08:21 +08:00
|
|
|
time_t boundary_timestamp;
|
2012-03-02 06:56:34 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// How many items we add until the next vacuum. Initially a random value.
|
2018-07-15 02:47:34 +08:00
|
|
|
int countdown_to_vacuum{-1};
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2018-07-15 02:47:34 +08:00
|
|
|
// Whether we've loaded old items.
|
|
|
|
bool loaded_old{false};
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// List of old items, as offsets into out mmap data.
|
2014-03-29 14:22:03 +08:00
|
|
|
std::deque<size_t> old_item_offsets;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2018-07-15 05:29:19 +08:00
|
|
|
// Figure out the offsets of our file contents.
|
|
|
|
void populate_from_file_contents();
|
2018-07-15 02:47:34 +08:00
|
|
|
|
|
|
|
// Loads old items if necessary.
|
2018-07-15 05:29:19 +08:00
|
|
|
void load_old_if_needed();
|
2012-12-06 05:33:07 +08:00
|
|
|
|
2018-07-15 02:47:34 +08:00
|
|
|
// Reads the history file if necessary.
|
|
|
|
bool mmap_if_needed();
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Deletes duplicates in new_items.
|
2012-03-02 06:56:34 +08:00
|
|
|
void compact_new_items();
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-02-07 03:04:07 +08:00
|
|
|
// Attempts to rewrite the existing file to a target temporary file
|
|
|
|
// Returns false on error, true on success
|
2017-05-10 12:01:27 +08:00
|
|
|
bool rewrite_to_temporary_file(int existing_fd, int dst_fd) const;
|
2017-02-07 03:04:07 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Saves history by rewriting the file.
|
2012-12-03 08:39:35 +08:00
|
|
|
bool save_internal_via_rewrite();
|
2012-12-03 18:25:08 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Saves history by appending to the file.
|
2012-12-03 08:39:35 +08:00
|
|
|
bool save_internal_via_appending();
|
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Saves history.
|
2012-12-03 15:38:38 +08:00
|
|
|
void save_internal(bool vacuum);
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Saves history unless doing so is disabled.
|
2014-03-29 14:22:03 +08:00
|
|
|
void save_internal_unless_disabled();
|
2012-12-03 15:38:38 +08:00
|
|
|
|
2018-01-31 10:30:30 +08:00
|
|
|
// Implementation of item_at_index and items_at_indexes
|
|
|
|
history_item_t item_at_index_assume_locked(size_t idx);
|
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
public:
|
2018-07-15 05:29:19 +08:00
|
|
|
explicit history_t(wcstring name);
|
|
|
|
~history_t();
|
2016-03-19 06:14:16 +08:00
|
|
|
|
2018-07-16 02:50:02 +08:00
|
|
|
// Whether we're in maximum chaos mode, useful for testing.
|
|
|
|
// This causes things like locks to fail.
|
|
|
|
static bool chaos_mode;
|
|
|
|
|
|
|
|
// Whether to force the read path instead of mmap. This is useful for testing.
|
|
|
|
static bool never_mmap;
|
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Returns history with the given name, creating it if necessary.
|
|
|
|
static history_t &history_with_name(const wcstring &name);
|
2016-03-19 06:14:16 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Determines whether the history is empty. Unfortunately this cannot be const, since it may
|
|
|
|
// require populating the history.
|
2018-07-15 02:47:34 +08:00
|
|
|
bool is_empty();
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Add a new history item to the end. If pending is set, the item will not be returned by
|
|
|
|
// item_at_index until a call to resolve_pending(). Pending items are tracked with an offset
|
|
|
|
// into the array of new items, so adding a non-pending item has the effect of resolving all
|
|
|
|
// pending items.
|
2015-04-20 17:04:17 +08:00
|
|
|
void add(const wcstring &str, history_identifier_t ident = 0, bool pending = false);
|
2012-06-05 12:24:42 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Remove a history item.
|
2012-06-05 12:24:42 +08:00
|
|
|
void remove(const wcstring &str);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Add a new pending history item to the end, and then begin file detection on the items to
|
|
|
|
// determine which arguments are paths
|
2015-04-20 17:04:17 +08:00
|
|
|
void add_pending_with_file_detection(const wcstring &str);
|
2015-09-19 11:47:38 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Resolves any pending history items, so that they may be returned in history searches.
|
2015-04-20 17:04:17 +08:00
|
|
|
void resolve_pending();
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Saves history.
|
2012-02-06 08:42:24 +08:00
|
|
|
void save();
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-07-14 13:33:50 +08:00
|
|
|
// Searches history.
|
2016-09-19 11:21:27 +08:00
|
|
|
bool search(history_search_type_t search_type, wcstring_list_t search_args,
|
2017-09-15 06:44:17 +08:00
|
|
|
const wchar_t *show_time_format, size_t max_items, bool case_sensitive,
|
|
|
|
bool null_terminate, bool reverse, io_streams_t &streams);
|
2018-08-12 15:30:57 +08:00
|
|
|
|
2017-09-15 06:44:17 +08:00
|
|
|
bool search_with_args(history_search_type_t search_type, wcstring_list_t search_args,
|
|
|
|
const wchar_t *show_time_format, size_t max_items, bool case_sensitive,
|
|
|
|
bool null_terminate, bool reverse, io_streams_t &streams);
|
2016-07-14 13:33:50 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Enable / disable automatic saving. Main thread only!
|
2014-03-29 14:22:03 +08:00
|
|
|
void disable_automatic_saving();
|
|
|
|
void enable_automatic_saving();
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Irreversibly clears history.
|
2012-02-16 16:24:27 +08:00
|
|
|
void clear();
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Populates from older location ()in config path, rather than data path).
|
2015-09-19 11:47:38 +08:00
|
|
|
void populate_from_config_path();
|
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Populates from a bash history file.
|
2012-06-16 07:22:37 +08:00
|
|
|
void populate_from_bash(FILE *f);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Incorporates the history of other shells into this history.
|
2014-07-26 01:08:21 +08:00
|
|
|
void incorporate_external_changes();
|
|
|
|
|
2017-08-25 09:59:50 +08:00
|
|
|
// Gets all the history into a list. This is intended for the $history environment variable.
|
2017-07-09 04:14:30 +08:00
|
|
|
// This may be long!
|
2017-08-25 09:59:50 +08:00
|
|
|
void get_history(wcstring_list_t &result);
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2018-01-31 10:30:30 +08:00
|
|
|
// Let indexes be a list of one-based indexes into the history, matching the interpretation of
|
|
|
|
// $history. That is, $history[1] is the most recently executed command. Values less than one
|
|
|
|
// are skipped. Return a mapping from index to history item text.
|
|
|
|
std::unordered_map<long, wcstring> items_at_indexes(const std::vector<long> &idxs);
|
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Sets the valid file paths for the history item with the given identifier.
|
2014-03-29 14:22:03 +08:00
|
|
|
void set_valid_file_paths(const wcstring_list_t &valid_file_paths, history_identifier_t ident);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Return the specified history at the specified index. 0 is the index of the current
|
|
|
|
// commandline. (So the most recent item is at index 1.)
|
2012-02-06 08:42:24 +08:00
|
|
|
history_item_t item_at_index(size_t idx);
|
2017-09-15 06:44:17 +08:00
|
|
|
|
|
|
|
// Return the number of history entries.
|
|
|
|
size_t size();
|
2012-02-06 08:42:24 +08:00
|
|
|
};
|
|
|
|
|
2018-08-12 15:30:57 +08:00
|
|
|
/// Flags for history searching.
|
|
|
|
enum {
|
|
|
|
// If set, ignore case.
|
|
|
|
history_search_ignore_case = 1 << 0,
|
|
|
|
|
|
|
|
// If set, do not deduplicate, which can help performance.
|
|
|
|
history_search_no_dedup = 1 << 1
|
|
|
|
};
|
|
|
|
using history_search_flags_t = uint32_t;
|
|
|
|
|
|
|
|
/// Support for searching a history backwards.
|
|
|
|
/// Note this does NOT de-duplicate; it is the caller's responsibility to do so.
|
2016-04-18 13:47:37 +08:00
|
|
|
class history_search_t {
|
2016-07-14 13:33:50 +08:00
|
|
|
private:
|
2016-04-18 13:47:37 +08:00
|
|
|
// The history in which we are searching.
|
2018-08-12 15:30:57 +08:00
|
|
|
history_t *history_;
|
|
|
|
|
|
|
|
// The original search term.
|
|
|
|
wcstring orig_term_;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2018-08-12 15:30:57 +08:00
|
|
|
// The (possibly lowercased) search term.
|
|
|
|
wcstring canon_term_;
|
2016-10-17 12:15:40 +08:00
|
|
|
|
|
|
|
// Our search type.
|
2018-08-12 15:30:57 +08:00
|
|
|
enum history_search_type_t search_type_ { HISTORY_SEARCH_TYPE_CONTAINS };
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2018-08-12 15:30:57 +08:00
|
|
|
// Our flags.
|
|
|
|
history_search_flags_t flags_{0};
|
2012-02-06 14:30:42 +08:00
|
|
|
|
2018-08-12 15:30:57 +08:00
|
|
|
// The current history item.
|
|
|
|
maybe_t<history_item_t> current_item_;
|
2012-02-06 08:42:24 +08:00
|
|
|
|
2018-08-12 15:30:57 +08:00
|
|
|
// Index of the current history item.
|
|
|
|
size_t current_index_{0};
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2018-08-12 15:30:57 +08:00
|
|
|
// If deduping, the items we've seen.
|
|
|
|
std::unordered_set<wcstring> deduper_;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2018-08-12 15:30:57 +08:00
|
|
|
// return whether we are case insensitive.
|
|
|
|
bool ignores_case() const { return flags_ & history_search_ignore_case; }
|
2012-02-06 08:42:24 +08:00
|
|
|
|
2018-08-12 15:30:57 +08:00
|
|
|
// return whether we deduplicate items.
|
|
|
|
bool dedup() const { return !(flags_ & history_search_no_dedup); }
|
2012-02-07 03:52:24 +08:00
|
|
|
|
2018-08-12 15:30:57 +08:00
|
|
|
public:
|
|
|
|
// Gets the original search term.
|
|
|
|
const wcstring &original_term() const { return orig_term_; }
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Finds the previous search result (backwards in time). Returns true if one was found.
|
2018-07-15 02:47:34 +08:00
|
|
|
bool go_backwards();
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Returns the current search result item. asserts if there is no current item.
|
2018-07-15 02:47:34 +08:00
|
|
|
history_item_t current_item() const;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Returns the current search result item contents. asserts if there is no current item.
|
2018-07-15 02:47:34 +08:00
|
|
|
wcstring current_string() const;
|
2012-02-16 16:24:27 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Constructor.
|
|
|
|
history_search_t(history_t &hist, const wcstring &str,
|
2016-09-24 11:12:15 +08:00
|
|
|
enum history_search_type_t type = HISTORY_SEARCH_TYPE_CONTAINS,
|
2018-08-12 15:30:57 +08:00
|
|
|
history_search_flags_t flags = 0)
|
|
|
|
: history_(&hist), orig_term_(str), canon_term_(str), search_type_(type), flags_(flags) {
|
|
|
|
if (ignores_case()) {
|
|
|
|
std::transform(canon_term_.begin(), canon_term_.end(), canon_term_.begin(), towlower);
|
2016-09-24 11:12:15 +08:00
|
|
|
}
|
|
|
|
}
|
2012-02-06 08:42:24 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Default constructor.
|
2018-08-12 15:30:57 +08:00
|
|
|
history_search_t() = default;
|
2012-02-06 08:42:24 +08:00
|
|
|
};
|
|
|
|
|
2017-07-01 12:03:05 +08:00
|
|
|
/// Saves the new history to disk.
|
2018-02-11 09:21:55 +08:00
|
|
|
void history_save_all();
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2017-07-01 12:03:05 +08:00
|
|
|
/// Return the prefix for the files to be used for command and read history.
|
2017-07-01 08:13:02 +08:00
|
|
|
wcstring history_session_id();
|
|
|
|
|
2017-07-01 12:03:05 +08:00
|
|
|
/// Given a list of paths and a working directory, return the paths that are valid
|
|
|
|
/// This does disk I/O and may only be called in a background thread
|
2017-01-25 09:52:28 +08:00
|
|
|
path_list_t valid_paths(const path_list_t &paths, const wcstring &working_directory);
|
|
|
|
|
2017-07-01 12:03:05 +08:00
|
|
|
/// Given a list of paths and a working directory,
|
|
|
|
/// return true if all paths in the list are valid
|
|
|
|
/// Returns true for if paths is empty
|
2017-01-25 09:52:28 +08:00
|
|
|
bool all_paths_are_valid(const path_list_t &paths, const wcstring &working_directory);
|
2018-09-20 07:56:08 +08:00
|
|
|
|
|
|
|
/// Sets private mode on. Once in private mode, it cannot be turned off.
|
|
|
|
void start_private_mode();
|
|
|
|
/// Queries private mode status.
|
|
|
|
bool in_private_mode();
|
2005-10-04 23:11:39 +08:00
|
|
|
#endif
|